-
Notifications
You must be signed in to change notification settings - Fork 91
fix(openai-responses): emit reasoning blocks from responses stream #1156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+2,948
−44
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9d194bb
fix(openai-responses): emit reasoning blocks from responses stream
acoliver b32af10
fix(openai-responses): capture reasoning summary events
acoliver 7ca2ba9
fix: use separate buffers for reasoning_text and reasoning_summary_te…
acoliver 7fed2d2
feat(openai-responses): add text.verbosity setting for Codex thinking…
acoliver 37318d5
fix(openai-responses): respect reasoning.includeInResponse setting an…
acoliver 8c191ae
fix(interactive): prevent duplicate thinking blocks in UI streaming
acoliver 268aa73
fix(coderabbit): address all CodeRabbit review comments
acoliver 75be1cb
fix(coderabbit): guard against undefined thought in reasoning summary
acoliver 508c975
chore: lint fixes and package-lock regeneration
acoliver cb389aa
Merge origin/main into issue922
acoliver e0b23fc
fix(ui): clear thinking blocks after commit to prevent pyramid accumu…
acoliver bd2fb76
fix(test): add getModelBehavior mock to codex.malformedCallId test
acoliver ba82d58
fix: address CodeRabbit review comments
acoliver 8d3401a
fix(ui): deduplicate thought events in useGeminiStream
acoliver c6112a7
fix: preserve reasoning items in Codex mode for better model performance
acoliver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
196 changes: 196 additions & 0 deletions
196
packages/core/src/providers/openai/parseResponsesStream.reasoning.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { parseResponsesStream } from './parseResponsesStream.js'; | ||
|
|
||
| function createSSEStream(chunks: string[]): ReadableStream<Uint8Array> { | ||
| const encoder = new TextEncoder(); | ||
| let index = 0; | ||
|
|
||
| return new ReadableStream<Uint8Array>({ | ||
| async pull(controller) { | ||
| if (index < chunks.length) { | ||
| const chunk = chunks[index++]; | ||
| controller.enqueue(encoder.encode(chunk)); | ||
| } else { | ||
| controller.close(); | ||
| } | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| describe('parseResponsesStream - Reasoning/Thinking Support', () => { | ||
| it('should parse reasoning-only stream with delta and done events', async () => { | ||
| const chunks = [ | ||
| 'data: {"type":"response.reasoning_text.delta","sequence_number":1,"delta":"Let me think about this..."}\n\n', | ||
| 'data: {"type":"response.reasoning_text.delta","sequence_number":2,"delta":" The user wants to know..."}\n\n', | ||
| 'data: {"type":"response.reasoning_text.done","sequence_number":3}\n\n', | ||
| ]; | ||
|
|
||
| const stream = createSSEStream(chunks); | ||
| const messages = []; | ||
|
|
||
| for await (const message of parseResponsesStream(stream)) { | ||
| messages.push(message); | ||
| } | ||
|
|
||
| // Should have one thinking block message | ||
| const thinkingMessage = messages.find((m) => | ||
| m.blocks.some((block) => block.type === 'thinking'), | ||
| ); | ||
| expect(thinkingMessage).toBeDefined(); | ||
| expect(thinkingMessage?.speaker).toBe('ai'); | ||
| expect(thinkingMessage?.blocks).toHaveLength(1); | ||
| expect(thinkingMessage?.blocks[0]).toEqual({ | ||
| type: 'thinking', | ||
| thought: 'Let me think about this... The user wants to know...', | ||
| sourceField: 'reasoning_content', | ||
| }); | ||
| }); | ||
|
|
||
| it('should handle interleaved reasoning, text, and tool calls', async () => { | ||
| const chunks = [ | ||
| // Reasoning starts | ||
| 'data: {"type":"response.reasoning_text.delta","sequence_number":1,"delta":"I need to search for this information."}\n\n', | ||
| 'data: {"type":"response.reasoning_text.done","sequence_number":2}\n\n', | ||
| // Text content | ||
| 'data: {"type":"response.output_text.delta","delta":"Let me search for that..."}\n\n', | ||
| // Tool call | ||
| 'data: {"type":"response.output_item.added","sequence_number":4,"output_index":1,"item":{"id":"fc_search","type":"function_call","status":"in_progress","arguments":"","call_id":"call_search","name":"search"}}\n\n', | ||
| 'data: {"type":"response.function_call_arguments.delta","sequence_number":5,"item_id":"fc_search","output_index":1,"delta":"{\\"query\\":\\"test\\"}"}\n\n', | ||
| 'data: {"type":"response.output_item.done","sequence_number":6,"output_index":1,"item":{"id":"fc_search","type":"function_call","status":"completed","arguments":"{\\"query\\":\\"test\\"}","call_id":"call_search","name":"search"}}\n\n', | ||
| ]; | ||
|
|
||
| const stream = createSSEStream(chunks); | ||
| const messages = []; | ||
|
|
||
| for await (const message of parseResponsesStream(stream)) { | ||
| messages.push(message); | ||
| } | ||
|
|
||
| // Should have reasoning, text, and tool call messages | ||
| expect( | ||
| messages.some((m) => m.blocks.some((block) => block.type === 'thinking')), | ||
| ).toBe(true); | ||
| expect( | ||
| messages.some((m) => | ||
| m.blocks.some( | ||
| (block) => | ||
| block.type === 'text' && | ||
| (block as { type: 'text'; text: string }).text === | ||
| 'Let me search for that...', | ||
| ), | ||
| ), | ||
| ).toBe(true); | ||
| expect( | ||
| messages.some((m) => | ||
| m.blocks.some((block) => block.type === 'tool_call'), | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('should not yield thinking block for empty/whitespace-only reasoning', async () => { | ||
| const chunks = [ | ||
| 'data: {"type":"response.reasoning_text.delta","sequence_number":1,"delta":" "}\n\n', | ||
| 'data: {"type":"response.reasoning_text.delta","sequence_number":2,"delta":"\\n\\t"}\n\n', | ||
| 'data: {"type":"response.reasoning_text.done","sequence_number":3}\n\n', | ||
| 'data: {"type":"response.output_text.delta","delta":"Hello!"}\n\n', | ||
| ]; | ||
|
|
||
| const stream = createSSEStream(chunks); | ||
| const messages = []; | ||
|
|
||
| for await (const message of parseResponsesStream(stream)) { | ||
| messages.push(message); | ||
| } | ||
|
|
||
| // Should not have thinking block, only text | ||
| expect( | ||
| messages.some((m) => m.blocks.some((block) => block.type === 'thinking')), | ||
| ).toBe(false); | ||
| expect( | ||
| messages.some((m) => m.blocks.some((block) => block.type === 'text')), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('should accumulate multiple reasoning deltas into single block', async () => { | ||
| const chunks = [ | ||
| 'data: {"type":"response.reasoning_text.delta","sequence_number":1,"delta":"First chunk."}\n\n', | ||
| 'data: {"type":"response.reasoning_text.delta","sequence_number":2,"delta":" Second chunk."}\n\n', | ||
| 'data: {"type":"response.reasoning_text.delta","sequence_number":3,"delta":" Third chunk."}\n\n', | ||
| 'data: {"type":"response.reasoning_text.done","sequence_number":4}\n\n', | ||
| ]; | ||
|
|
||
| const stream = createSSEStream(chunks); | ||
| const messages = []; | ||
|
|
||
| for await (const message of parseResponsesStream(stream)) { | ||
| messages.push(message); | ||
| } | ||
|
|
||
| const thinkingMessages = messages.filter((m) => | ||
| m.blocks.some((block) => block.type === 'thinking'), | ||
| ); | ||
| // Should have exactly one thinking message with all deltas accumulated | ||
| expect(thinkingMessages).toHaveLength(1); | ||
| const thinkingBlock = thinkingMessages[0]?.blocks[0] as { | ||
| type: 'thinking'; | ||
| thought: string; | ||
| }; | ||
| expect(thinkingBlock.thought).toBe( | ||
| 'First chunk. Second chunk. Third chunk.', | ||
| ); | ||
| }); | ||
|
|
||
| it('should handle reasoning with usage metadata', async () => { | ||
| const chunks = [ | ||
| 'data: {"type":"response.reasoning_text.delta","sequence_number":1,"delta":"Thinking deeply..."}\n\n', | ||
| 'data: {"type":"response.reasoning_text.done","sequence_number":2}\n\n', | ||
| 'data: {"type":"response.output_text.delta","delta":"Here is my answer."}\n\n', | ||
| 'data: {"type":"response.completed","sequence_number":4,"response":{"id":"resp_123","object":"response","model":"gpt-5.2","status":"completed","usage":{"input_tokens":100,"output_tokens":50,"total_tokens":150}}}\n\n', | ||
| ]; | ||
|
|
||
| const stream = createSSEStream(chunks); | ||
| const messages = []; | ||
|
|
||
| for await (const message of parseResponsesStream(stream)) { | ||
| messages.push(message); | ||
| } | ||
|
|
||
| // Should have thinking block, text, and usage | ||
| expect( | ||
| messages.some((m) => m.blocks.some((block) => block.type === 'thinking')), | ||
| ).toBe(true); | ||
| expect( | ||
| messages.some((m) => m.blocks.some((block) => block.type === 'text')), | ||
| ).toBe(true); | ||
| const usageMessage = messages.find((m) => m.metadata?.usage); | ||
| expect(usageMessage?.metadata?.usage).toEqual({ | ||
| promptTokens: 100, | ||
| completionTokens: 50, | ||
| totalTokens: 150, | ||
| cachedTokens: 0, | ||
| }); | ||
| }); | ||
|
|
||
| it('should yield reasoning before response.completed', async () => { | ||
| const chunks = [ | ||
| 'data: {"type":"response.reasoning_text.delta","sequence_number":1,"delta":"Reasoning content"}\n\n', | ||
| 'data: {"type":"response.completed","sequence_number":2,"response":{"id":"resp_123","object":"response","model":"gpt-5.2","status":"completed","usage":{"input_tokens":10,"output_tokens":5,"total_tokens":15}}}\n\n', | ||
| ]; | ||
|
|
||
| const stream = createSSEStream(chunks); | ||
| const messages = []; | ||
|
|
||
| for await (const message of parseResponsesStream(stream)) { | ||
| messages.push(message); | ||
| } | ||
|
|
||
| // Thinking should come before usage | ||
| const thinkingIndex = messages.findIndex((m) => | ||
| m.blocks.some((block) => block.type === 'thinking'), | ||
| ); | ||
| const usageIndex = messages.findIndex((m) => m.metadata?.usage); | ||
| expect(thinkingIndex).toBeGreaterThanOrEqual(0); | ||
| expect(usageIndex).toBeGreaterThanOrEqual(0); | ||
| expect(thinkingIndex).toBeLessThan(usageIndex); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.