-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathparseResponsesStream.reasoning.test.ts
More file actions
223 lines (193 loc) · 8.78 KB
/
Copy pathparseResponsesStream.reasoning.test.ts
File metadata and controls
223 lines (193 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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 handle reasoning_summary_text events separately from reasoning_text', async () => {
const chunks = [
'data: {"type":"response.reasoning_text.delta","sequence_number":1,"delta":"Raw reasoning."}\n\n',
'data: {"type":"response.reasoning_text.done","sequence_number":2}\n\n',
'data: {"type":"response.reasoning_summary_text.delta","sequence_number":3,"delta":"Summary: Key insight."}\n\n',
'data: {"type":"response.reasoning_summary_text.done","sequence_number":4,"text":"Summary: Key insight."}\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 two separate ThinkingBlocks
expect(thinkingMessages).toHaveLength(2);
const thoughts = thinkingMessages.map(
(m) => (m.blocks[0] as { type: 'thinking'; thought: string }).thought,
);
expect(thoughts).toContain('Raw reasoning.');
expect(thoughts).toContain('Summary: Key insight.');
});
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);
});
});