Skip to content

feat(core): add onError callback to createSpecStreamCompiler#217

Open
nil957 wants to merge 1 commit intovercel-labs:mainfrom
nil957:feat/spec-stream-error-callback
Open

feat(core): add onError callback to createSpecStreamCompiler#217
nil957 wants to merge 1 commit intovercel-labs:mainfrom
nil957:feat/spec-stream-error-callback

Conversation

@nil957
Copy link

@nil957 nil957 commented Mar 13, 2026

Summary

Adds optional onError callback to createSpecStreamCompiler for debugging malformed JSON patches during streaming.

Problem

Currently, when createSpecStreamCompiler encounters malformed JSON or patch application failures, it silently ignores them. This makes debugging difficult in production when LLMs generate invalid patches.

Solution

New Options Interface

interface SpecStreamCompilerOptions<T> {
  initial?: Partial<T>;
  onError?: (line: string, error?: unknown) => void;
}

createSpecStreamCompiler({ onError: (line) => console.warn(line) })

When onError is Called

  1. Parse failures: Line starts with { but parseSpecStreamLine returns null
  2. Application failures: Patch parsed successfully but applySpecStreamPatch throws (e.g., test operation mismatch)

Example Usage

const compiler = createSpecStreamCompiler<Spec>({
  onError: (line, error) => {
    logger.warn('Invalid patch line:', line);
    if (error) {
      logger.error('Patch application failed:', error);
    }
  },
});

Backward Compatibility

Old signature still works:

// Still valid
createSpecStreamCompiler({ root: '', elements: {} })

New signature:

// With options
createSpecStreamCompiler({ 
  initial: { root: '', elements: {} },
  onError: (line) => console.warn(line)
})

Use Cases

  • Production error monitoring
  • Development debugging
  • Analytics (tracking LLM patch quality)
  • Auto-repair triggers

Checklist

  • Tests added for error callback scenarios
  • Backward compatible
  • No breaking changes
  • Error callback is optional

Adds optional `onError` callback to SpecStreamCompiler for debugging
malformed JSON patches during streaming.

Changes:
- New `SpecStreamCompilerOptions` interface with `initial` and `onError`
- Updated function signature (backward compatible via options object)
- Calls onError when:
  - Line starts with { but fails to parse
  - Patch application fails (e.g., test operation mismatch)

Example:
```typescript
const compiler = createSpecStreamCompiler({
  onError: (line, error) => {
    console.warn('Failed to parse:', line);
    if (error) console.error(error);
  },
});
```

This makes debugging easier in production when LLMs generate invalid patches.
@vercel
Copy link
Contributor

vercel bot commented Mar 13, 2026

Someone is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

*/
export function createSpecStreamCompiler<T = Record<string, unknown>>(
initial: Partial<T> = {},
options: SpecStreamCompilerOptions<T> = {},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Breaking API change in createSpecStreamCompiler signature causes type errors in 3 example files that still pass Partial<T> instead of SpecStreamCompilerOptions<T>.

Fix on Vercel

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