Skip to content

Refactor duplicated code patterns into shared utilities - #1

Closed
devin-ai-integration[bot] wants to merge 1 commit into
masterfrom
devin/1783610906-shared-utils
Closed

Refactor duplicated code patterns into shared utilities#1
devin-ai-integration[bot] wants to merge 1 commit into
masterfrom
devin/1783610906-shared-utils

Conversation

@devin-ai-integration

Copy link
Copy Markdown

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.).

// src/utils/errors.ts
export function getErrorMessage(err: unknown, fallback?: string): string {
  return err instanceof Error ? err.message : (fallback ?? String(err))
}

The optional fallback preserves the variants that used a custom default instead of String(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:

// src/utils/sse.ts
export async function readSSEStream(
  reader: ReadableStreamDefaultReader<Uint8Array>,
  onEvent: (eventType: string, data: any) => boolean | void, // return true to stop early
): Promise<void>

Each caller keeps its own no-body handling and try/catch (their behaviors differ) and just supplies an onEvent handler, returning true on the terminal done event. The buffering/line-splitting/JSON-parse semantics are unchanged (including the existing limitation that an event: line and its data: 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×.

// src/utils/http.ts
export function jsonRequest(body: unknown, init?: RequestInit): RequestInit
// fetch(url, jsonRequest(data))                        -> POST
// fetch(url, jsonRequest(body, { method: 'PUT' }))     -> PUT
// fetch(url, jsonRequest(body, { signal }))            -> POST + abort signal

Tests

Added tests/errors.test.ts, tests/http.test.ts, tests/sse.test.ts. Full suite: 41 passing; tsc --noEmit clean.

Link to Devin session: https://app.devin.ai/sessions/96bdc26167854eee8f85a0ce5742c714
Requested by: @JeffGuo7

- 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>
@JeffGuo7 JeffGuo7 self-assigned this Jul 9, 2026
@devin-ai-integration

Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@JeffGuo7 JeffGuo7 closed this Jul 10, 2026
@JeffGuo7
JeffGuo7 deleted the devin/1783610906-shared-utils branch July 10, 2026 03:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant