Refactor duplicated code patterns into shared utilities - #1
Closed
devin-ai-integration[bot] wants to merge 1 commit into
Closed
Refactor duplicated code patterns into shared utilities#1devin-ai-integration[bot] wants to merge 1 commit into
devin-ai-integration[bot] wants to merge 1 commit into
Conversation
- Add getErrorMessage() for the repeated 'err instanceof Error ? err.message : String(err)' pattern - Add readSSEStream() consolidating the duplicated SSE parsing loop (piClient.prompt, piClient.runGoal, goalStore.startGoal) - Add jsonRequest() for the repeated POST/PUT JSON fetch init boilerplate - Add unit tests for the new utilities Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
⚙️ Control Options:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three code patterns were duplicated across the frontend. This PR extracts each into a small, tested shared utility under
src/utils/and updates all call sites. Net ~140 fewer lines, no behavior change.1. Error normalization — the idiom
err instanceof Error ? err.message : String(err)appeared 38× across 20 files (stores, previews, chat, skills, etc.).The optional
fallbackpreserves the variants that used a custom default instead ofString(err)(e.g.getErrorMessage(err, t('skills.zipParseError'))).2. SSE stream parsing — the byte-level
event:/data:parser loop was copy-pasted three times (piClient.prompt,piClient.runGoal,goalStore.startGoal). Extracted verbatim into:Each caller keeps its own no-body handling and
try/catch(their behaviors differ) and just supplies anonEventhandler, returningtrueon the terminaldoneevent. The buffering/line-splitting/JSON-parse semantics are unchanged (including the existing limitation that anevent:line and itsdata:line must arrive in the same buffer).3. JSON request init — the
{ method, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(x) }boilerplate appeared 18×.Tests
Added
tests/errors.test.ts,tests/http.test.ts,tests/sse.test.ts. Full suite: 41 passing;tsc --noEmitclean.Link to Devin session: https://app.devin.ai/sessions/96bdc26167854eee8f85a0ce5742c714
Requested by: @JeffGuo7