@librechat/agents 1.9.94 → 1.9.95
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/common/enum.cjs +3 -0
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/stream.cjs +24 -0
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/esm/common/enum.mjs +3 -0
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/stream.mjs +24 -0
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/types/common/enum.d.ts +3 -0
- package/dist/types/graphs/Graph.d.ts +1 -1
- package/dist/types/mockStream.d.ts +32 -0
- package/dist/types/splitStream.d.ts +35 -0
- package/dist/types/types/stream.d.ts +54 -4
- package/package.json +11 -11
- package/src/common/enum.ts +3 -0
- package/src/graphs/Graph.ts +1 -1
- package/src/mockStream.ts +99 -0
- package/src/splitStream.test.ts +539 -0
- package/src/splitStream.ts +193 -0
- package/src/stream.ts +26 -0
- package/src/types/stream.ts +47 -4
- package/src/utils/llmConfig.ts +3 -1
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
import { nanoid } from 'nanoid';
|
|
2
|
+
import { MessageContentText } from '@langchain/core/messages';
|
|
3
|
+
import type * as t from '@/types';
|
|
4
|
+
import { GraphEvents , StepTypes, ContentTypes } from '@/common';
|
|
5
|
+
import { createContentAggregator } from './stream';
|
|
6
|
+
import { SplitStreamHandler } from './splitStream';
|
|
7
|
+
import { createMockStream } from './mockStream';
|
|
8
|
+
|
|
9
|
+
// Mock sleep to speed up tests
|
|
10
|
+
jest.mock('@/utils', () => ({
|
|
11
|
+
sleep: (): Promise<void> => Promise.resolve(),
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
describe('Stream Generation and Handling', () => {
|
|
15
|
+
let mockHandlers: {
|
|
16
|
+
[GraphEvents.ON_RUN_STEP]: jest.Mock;
|
|
17
|
+
[GraphEvents.ON_MESSAGE_DELTA]: jest.Mock;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
mockHandlers = {
|
|
22
|
+
[GraphEvents.ON_RUN_STEP]: jest.fn(),
|
|
23
|
+
[GraphEvents.ON_MESSAGE_DELTA]: jest.fn(),
|
|
24
|
+
};
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should properly stream tokens including spaces', async () => {
|
|
28
|
+
const stream = createMockStream({
|
|
29
|
+
text: 'Hello world!',
|
|
30
|
+
streamRate: 0,
|
|
31
|
+
})();
|
|
32
|
+
|
|
33
|
+
const tokens: string[] = [];
|
|
34
|
+
for await (const chunk of stream) {
|
|
35
|
+
const content = chunk.choices?.[0]?.delta.content ?? '';
|
|
36
|
+
if (content) tokens.push(content);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
expect(tokens).toEqual(['Hello', ' ', 'world!']);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should handle code blocks without splitting them', async () => {
|
|
43
|
+
const runId = nanoid();
|
|
44
|
+
const handler = new SplitStreamHandler({
|
|
45
|
+
runId,
|
|
46
|
+
blockThreshold: 10,
|
|
47
|
+
handlers: mockHandlers,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const codeText = `Code:
|
|
51
|
+
\`\`\`
|
|
52
|
+
const x = 1;
|
|
53
|
+
const y = 2;
|
|
54
|
+
const z = 2;
|
|
55
|
+
const a = 2;
|
|
56
|
+
const b = 2;
|
|
57
|
+
const c = 2;
|
|
58
|
+
const d = 2;
|
|
59
|
+
const e = 2;
|
|
60
|
+
const f = 2;
|
|
61
|
+
const g = 2;
|
|
62
|
+
const h = 2;
|
|
63
|
+
\`\`\`
|
|
64
|
+
End code.`;
|
|
65
|
+
|
|
66
|
+
const stream = createMockStream({
|
|
67
|
+
text: codeText,
|
|
68
|
+
streamRate: 0,
|
|
69
|
+
})();
|
|
70
|
+
|
|
71
|
+
for await (const chunk of stream) {
|
|
72
|
+
handler.handle(chunk);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Verify that only one message block was created for the code section
|
|
76
|
+
const runSteps = mockHandlers[GraphEvents.ON_RUN_STEP].mock.calls;
|
|
77
|
+
expect(runSteps.length).toBe(2); // Should only create one message block
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should split content when exceeding threshold', async () => {
|
|
81
|
+
const runId = nanoid();
|
|
82
|
+
const handler = new SplitStreamHandler({
|
|
83
|
+
runId,
|
|
84
|
+
handlers: mockHandlers,
|
|
85
|
+
// Set a very low threshold for testing
|
|
86
|
+
blockThreshold: 10,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// Make the text longer and ensure it has clear breaking points
|
|
90
|
+
const longText = 'This is the first sentence. And here is another sentence. And yet another one here. Finally one more.';
|
|
91
|
+
|
|
92
|
+
const stream = createMockStream({
|
|
93
|
+
text: longText,
|
|
94
|
+
streamRate: 0,
|
|
95
|
+
})();
|
|
96
|
+
|
|
97
|
+
// For debugging
|
|
98
|
+
// let totalLength = 0;
|
|
99
|
+
for await (const chunk of stream) {
|
|
100
|
+
handler.handle(chunk);
|
|
101
|
+
// For debugging
|
|
102
|
+
// const content = chunk.choices?.[0]?.delta.content;
|
|
103
|
+
// if (content) {
|
|
104
|
+
// totalLength += content.length;
|
|
105
|
+
// console.log(`Current length: ${totalLength}, Content: "${content}"`);
|
|
106
|
+
// }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Verify multiple message blocks were created
|
|
110
|
+
const runSteps = mockHandlers[GraphEvents.ON_RUN_STEP].mock.calls;
|
|
111
|
+
// console.log('Number of run steps:', runSteps.length);
|
|
112
|
+
expect(runSteps.length).toEqual(handler.currentIndex + 1);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should handle reasoning text separately', async () => {
|
|
116
|
+
const runId = nanoid();
|
|
117
|
+
new SplitStreamHandler({
|
|
118
|
+
runId,
|
|
119
|
+
handlers: mockHandlers,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const stream = createMockStream({
|
|
123
|
+
text: 'Main content',
|
|
124
|
+
reasoningText: 'Reasoning text',
|
|
125
|
+
streamRate: 0,
|
|
126
|
+
})();
|
|
127
|
+
|
|
128
|
+
const reasoningTokens: string[] = [];
|
|
129
|
+
const contentTokens: string[] = [];
|
|
130
|
+
|
|
131
|
+
for await (const chunk of stream) {
|
|
132
|
+
const reasoning = chunk.choices?.[0]?.delta.reasoning_content ?? '';
|
|
133
|
+
const content = chunk.choices?.[0]?.delta.content ?? '';
|
|
134
|
+
|
|
135
|
+
if (reasoning) reasoningTokens.push(reasoning);
|
|
136
|
+
if (content) contentTokens.push(content);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
expect(reasoningTokens.length).toBeGreaterThan(0);
|
|
140
|
+
expect(contentTokens.length).toBeGreaterThan(0);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('should preserve empty strings and whitespace', async () => {
|
|
144
|
+
const stream = createMockStream({
|
|
145
|
+
text: 'Hello world', // Note double space
|
|
146
|
+
streamRate: 0,
|
|
147
|
+
})();
|
|
148
|
+
|
|
149
|
+
const tokens: string[] = [];
|
|
150
|
+
for await (const chunk of stream) {
|
|
151
|
+
const content = chunk.choices?.[0]?.delta.content ?? '';
|
|
152
|
+
if (!content) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
tokens.push(content);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
expect(tokens).toContain(' ');
|
|
159
|
+
expect(tokens.join('')).toBe('Hello world');
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
describe('ContentAggregator with SplitStreamHandler', () => {
|
|
164
|
+
it('should aggregate content from multiple message blocks', async () => {
|
|
165
|
+
const runId = nanoid();
|
|
166
|
+
const { contentParts, aggregateContent } = createContentAggregator();
|
|
167
|
+
|
|
168
|
+
const handler = new SplitStreamHandler({
|
|
169
|
+
runId,
|
|
170
|
+
handlers: {
|
|
171
|
+
[GraphEvents.ON_RUN_STEP]: aggregateContent,
|
|
172
|
+
[GraphEvents.ON_MESSAGE_DELTA]: aggregateContent,
|
|
173
|
+
},
|
|
174
|
+
blockThreshold: 10,
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
const text = 'First sentence. Second sentence. Third sentence.';
|
|
178
|
+
const stream = createMockStream({ text, streamRate: 0 })();
|
|
179
|
+
|
|
180
|
+
for await (const chunk of stream) {
|
|
181
|
+
handler.handle(chunk);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
expect(contentParts.length).toBeGreaterThan(1);
|
|
185
|
+
contentParts.forEach(part => {
|
|
186
|
+
expect(part?.type).toBe(ContentTypes.TEXT);
|
|
187
|
+
if (part?.type === ContentTypes.TEXT) {
|
|
188
|
+
expect(typeof part.text).toBe('string');
|
|
189
|
+
expect(part.text.length).toBeGreaterThan(0);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
const fullText = contentParts
|
|
194
|
+
.filter(part => part?.type === ContentTypes.TEXT)
|
|
195
|
+
.map(part => (part?.type === ContentTypes.TEXT ? part.text : ''))
|
|
196
|
+
.join('');
|
|
197
|
+
expect(fullText).toBe(text);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('should maintain content order across splits', async () => {
|
|
201
|
+
const runId = nanoid();
|
|
202
|
+
const { contentParts, aggregateContent } = createContentAggregator();
|
|
203
|
+
|
|
204
|
+
const handler = new SplitStreamHandler({
|
|
205
|
+
runId,
|
|
206
|
+
handlers: {
|
|
207
|
+
[GraphEvents.ON_RUN_STEP]: aggregateContent,
|
|
208
|
+
[GraphEvents.ON_MESSAGE_DELTA]: aggregateContent,
|
|
209
|
+
},
|
|
210
|
+
blockThreshold: 15,
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
const text = 'First part. Second part. Third part.';
|
|
214
|
+
const stream = createMockStream({ text, streamRate: 0 })();
|
|
215
|
+
|
|
216
|
+
for await (const chunk of stream) {
|
|
217
|
+
handler.handle(chunk);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const texts = contentParts
|
|
221
|
+
.filter(part => part?.type === ContentTypes.TEXT)
|
|
222
|
+
.map(part => (part?.type === ContentTypes.TEXT ? part.text : ''));
|
|
223
|
+
|
|
224
|
+
expect(texts[0]).toContain('First');
|
|
225
|
+
expect(texts[texts.length - 1]).toContain('Third');
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('should handle code blocks as single content parts', async () => {
|
|
229
|
+
const runId = nanoid();
|
|
230
|
+
const { contentParts, aggregateContent } = createContentAggregator();
|
|
231
|
+
|
|
232
|
+
const handler = new SplitStreamHandler({
|
|
233
|
+
runId,
|
|
234
|
+
handlers: {
|
|
235
|
+
[GraphEvents.ON_RUN_STEP]: aggregateContent,
|
|
236
|
+
[GraphEvents.ON_MESSAGE_DELTA]: aggregateContent,
|
|
237
|
+
},
|
|
238
|
+
blockThreshold: 10,
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
const text = `Before code.
|
|
242
|
+
\`\`\`python
|
|
243
|
+
def test():
|
|
244
|
+
return True
|
|
245
|
+
\`\`\`
|
|
246
|
+
After code.`;
|
|
247
|
+
|
|
248
|
+
const stream = createMockStream({ text, streamRate: 0 })();
|
|
249
|
+
|
|
250
|
+
for await (const chunk of stream) {
|
|
251
|
+
handler.handle(chunk);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const codeBlockPart = contentParts.find(part =>
|
|
255
|
+
part?.type === ContentTypes.TEXT &&
|
|
256
|
+
part.text.includes('```python')
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
expect(codeBlockPart).toBeDefined();
|
|
260
|
+
if (codeBlockPart?.type === ContentTypes.TEXT) {
|
|
261
|
+
expect(codeBlockPart.text).toContain('def test()');
|
|
262
|
+
expect(codeBlockPart.text).toContain('return True');
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it('should properly map steps to their content', async () => {
|
|
267
|
+
const runId = nanoid();
|
|
268
|
+
const { contentParts, aggregateContent, stepMap } = createContentAggregator();
|
|
269
|
+
|
|
270
|
+
const handler = new SplitStreamHandler({
|
|
271
|
+
runId,
|
|
272
|
+
handlers: {
|
|
273
|
+
[GraphEvents.ON_RUN_STEP]: aggregateContent,
|
|
274
|
+
[GraphEvents.ON_MESSAGE_DELTA]: aggregateContent,
|
|
275
|
+
},
|
|
276
|
+
blockThreshold: 5,
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
const text = 'Hi. Ok. Yes.';
|
|
280
|
+
const stream = createMockStream({ text, streamRate: 0 })();
|
|
281
|
+
|
|
282
|
+
for await (const chunk of stream) {
|
|
283
|
+
handler.handle(chunk);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
Array.from(stepMap.entries()).forEach(([_stepId, step]) => {
|
|
287
|
+
expect(step?.type).toBe(StepTypes.MESSAGE_CREATION);
|
|
288
|
+
const currentIndex = step?.index ?? -1;
|
|
289
|
+
const stepContent = contentParts[currentIndex];
|
|
290
|
+
if (!stepContent && currentIndex > 0) {
|
|
291
|
+
const prevStepContent = contentParts[currentIndex - 1];
|
|
292
|
+
expect((prevStepContent as MessageContentText | undefined)?.text).toEqual(text);
|
|
293
|
+
} else if (stepContent?.type === ContentTypes.TEXT) {
|
|
294
|
+
expect(stepContent.text.length).toBeGreaterThan(0);
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
contentParts.forEach((part, index) => {
|
|
299
|
+
const hasMatchingStep = Array.from(stepMap.values()).some(
|
|
300
|
+
step => step?.index === index
|
|
301
|
+
);
|
|
302
|
+
expect(hasMatchingStep).toBe(true);
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('should aggregate content across multiple splits while preserving order', async () => {
|
|
307
|
+
const runId = nanoid();
|
|
308
|
+
const { contentParts, aggregateContent } = createContentAggregator();
|
|
309
|
+
|
|
310
|
+
const handler = new SplitStreamHandler({
|
|
311
|
+
runId,
|
|
312
|
+
handlers: {
|
|
313
|
+
[GraphEvents.ON_RUN_STEP]: aggregateContent,
|
|
314
|
+
[GraphEvents.ON_MESSAGE_DELTA]: aggregateContent,
|
|
315
|
+
},
|
|
316
|
+
blockThreshold: 10,
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
const text = 'A. B. C. D. E. F.';
|
|
320
|
+
const stream = createMockStream({ text, streamRate: 0 })();
|
|
321
|
+
|
|
322
|
+
for await (const chunk of stream) {
|
|
323
|
+
handler.handle(chunk);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const letters = ['A', 'B', 'C', 'D', 'E', 'F'];
|
|
327
|
+
let letterIndex = 0;
|
|
328
|
+
|
|
329
|
+
contentParts.forEach(part => {
|
|
330
|
+
if (part?.type === ContentTypes.TEXT) {
|
|
331
|
+
while (letterIndex < letters.length &&
|
|
332
|
+
part.text.includes(letters[letterIndex]) === true) {
|
|
333
|
+
letterIndex++;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
expect(letterIndex).toBe(letters.length);
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
describe('SplitStreamHandler with Reasoning Tokens', () => {
|
|
343
|
+
it('should apply same splitting rules to both content types', async () => {
|
|
344
|
+
const runId = nanoid();
|
|
345
|
+
const mockHandlers: t.SplitStreamHandlers = {
|
|
346
|
+
[GraphEvents.ON_RUN_STEP]: jest.fn(),
|
|
347
|
+
[GraphEvents.ON_MESSAGE_DELTA]: jest.fn(),
|
|
348
|
+
[GraphEvents.ON_REASONING_DELTA]: jest.fn(),
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
const handler = new SplitStreamHandler({
|
|
352
|
+
runId,
|
|
353
|
+
handlers: mockHandlers,
|
|
354
|
+
blockThreshold: 10,
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
const stream = createMockStream({
|
|
358
|
+
text: 'First text. Second text. Third text.',
|
|
359
|
+
reasoningText: 'First thought. Second thought. Third thought.',
|
|
360
|
+
streamRate: 0,
|
|
361
|
+
})();
|
|
362
|
+
|
|
363
|
+
for await (const chunk of stream) {
|
|
364
|
+
handler.handle(chunk);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const runSteps = (mockHandlers[GraphEvents.ON_RUN_STEP] as jest.Mock).mock.calls;
|
|
368
|
+
const reasoningDeltas = (mockHandlers[GraphEvents.ON_REASONING_DELTA] as jest.Mock).mock.calls;
|
|
369
|
+
const messageDeltas = (mockHandlers[GraphEvents.ON_MESSAGE_DELTA] as jest.Mock).mock.calls;
|
|
370
|
+
|
|
371
|
+
// Both content types should create multiple blocks
|
|
372
|
+
expect(runSteps.length).toBeGreaterThan(2);
|
|
373
|
+
expect(reasoningDeltas.length).toBeGreaterThan(0);
|
|
374
|
+
expect(messageDeltas.length).toBeGreaterThan(0);
|
|
375
|
+
|
|
376
|
+
// Verify splitting behavior for both types
|
|
377
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
378
|
+
const getStepTypes = (calls: any[]): string[] => calls.map(([{ data }]) =>
|
|
379
|
+
data.stepDetails?.type === StepTypes.MESSAGE_CREATION ?
|
|
380
|
+
data.stepDetails.message_creation.message_id : null
|
|
381
|
+
).filter(Boolean);
|
|
382
|
+
|
|
383
|
+
const messageSteps = getStepTypes(runSteps);
|
|
384
|
+
expect(new Set(messageSteps).size).toBeGreaterThan(1);
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it('should properly map steps to their reasoning content', async () => {
|
|
388
|
+
const runId = nanoid();
|
|
389
|
+
const { contentParts, aggregateContent, stepMap } = createContentAggregator();
|
|
390
|
+
|
|
391
|
+
const handler = new SplitStreamHandler({
|
|
392
|
+
runId,
|
|
393
|
+
handlers: {
|
|
394
|
+
[GraphEvents.ON_RUN_STEP]: aggregateContent,
|
|
395
|
+
[GraphEvents.ON_MESSAGE_DELTA]: aggregateContent,
|
|
396
|
+
[GraphEvents.ON_REASONING_DELTA]: aggregateContent,
|
|
397
|
+
},
|
|
398
|
+
blockThreshold: 5,
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
const text = 'Main content.';
|
|
402
|
+
const reasoningText = 'First thought. Second thought. Third thought.';
|
|
403
|
+
const stream = createMockStream({
|
|
404
|
+
text,
|
|
405
|
+
reasoningText,
|
|
406
|
+
streamRate: 0
|
|
407
|
+
})();
|
|
408
|
+
|
|
409
|
+
for await (const chunk of stream) {
|
|
410
|
+
handler.handle(chunk);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
Array.from(stepMap.entries()).forEach(([_stepId, step]) => {
|
|
414
|
+
expect(step?.type).toBe(StepTypes.MESSAGE_CREATION);
|
|
415
|
+
const currentIndex = step?.index ?? -1;
|
|
416
|
+
const stepContent = contentParts[currentIndex];
|
|
417
|
+
|
|
418
|
+
if (stepContent?.type === ContentTypes.THINK) {
|
|
419
|
+
// Verify reasoning content structure
|
|
420
|
+
expect(stepContent).toHaveProperty('think');
|
|
421
|
+
expect(typeof stepContent.think).toBe('string');
|
|
422
|
+
expect(stepContent.think.length).toBeGreaterThan(0);
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
// Verify at least one reasoning content part exists
|
|
427
|
+
const reasoningParts = contentParts.filter(
|
|
428
|
+
part => part?.type === ContentTypes.THINK
|
|
429
|
+
);
|
|
430
|
+
expect(reasoningParts.length).toBeGreaterThan(0);
|
|
431
|
+
|
|
432
|
+
// Verify the content order (reasoning should come before main content)
|
|
433
|
+
const contentTypes = contentParts
|
|
434
|
+
.filter(part => part !== undefined)
|
|
435
|
+
.map(part => part.type);
|
|
436
|
+
|
|
437
|
+
expect(contentTypes).toContain(ContentTypes.THINK);
|
|
438
|
+
expect(contentTypes).toContain(ContentTypes.TEXT);
|
|
439
|
+
|
|
440
|
+
// Verify the complete reasoning content is preserved
|
|
441
|
+
const fullReasoningText = reasoningParts
|
|
442
|
+
.map(part => (part?.type === ContentTypes.THINK ? part.think : ''))
|
|
443
|
+
.join('');
|
|
444
|
+
expect(fullReasoningText).toBe(reasoningText);
|
|
445
|
+
});
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
describe('SplitStreamHandler', () => {
|
|
449
|
+
it('should handle think blocks correctly', async () => {
|
|
450
|
+
const runId = nanoid();
|
|
451
|
+
const messageDeltaEvents: t.MessageDeltaEvent[] = [];
|
|
452
|
+
const reasoningDeltaEvents: t.ReasoningDeltaEvent[] = [];
|
|
453
|
+
|
|
454
|
+
const streamHandler = new SplitStreamHandler({
|
|
455
|
+
runId,
|
|
456
|
+
handlers: {
|
|
457
|
+
[GraphEvents.ON_MESSAGE_DELTA]: ({ data }): void => {
|
|
458
|
+
messageDeltaEvents.push(data);
|
|
459
|
+
},
|
|
460
|
+
[GraphEvents.ON_REASONING_DELTA]: ({ data }): void => {
|
|
461
|
+
reasoningDeltaEvents.push(data);
|
|
462
|
+
},
|
|
463
|
+
},
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
const content = 'Here\'s some regular text. <think>Now I\'m thinking deeply about something important. This should all be reasoning.</think> Back to regular text.';
|
|
467
|
+
|
|
468
|
+
const stream = createMockStream({
|
|
469
|
+
text: content,
|
|
470
|
+
streamRate: 5,
|
|
471
|
+
})();
|
|
472
|
+
|
|
473
|
+
for await (const chunk of stream) {
|
|
474
|
+
streamHandler.handle(chunk);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// Check that content before <think> was handled as regular text
|
|
478
|
+
expect(messageDeltaEvents.some(event =>
|
|
479
|
+
(event.delta.content?.[0] as t.MessageDeltaUpdate | undefined)?.text.includes('Here\'s')
|
|
480
|
+
)).toBe(true);
|
|
481
|
+
|
|
482
|
+
// Check that <think> tag was handled as reasoning
|
|
483
|
+
expect(reasoningDeltaEvents.some(event =>
|
|
484
|
+
(event.delta.content?.[0] as t.ReasoningDeltaUpdate | undefined)?.think.includes('<think>')
|
|
485
|
+
)).toBe(true);
|
|
486
|
+
|
|
487
|
+
// Check that content inside <think> tags was handled as reasoning
|
|
488
|
+
expect(reasoningDeltaEvents.some(event =>
|
|
489
|
+
(event.delta.content?.[0] as t.ReasoningDeltaUpdate | undefined)?.think.includes('thinking')
|
|
490
|
+
)).toBe(true);
|
|
491
|
+
|
|
492
|
+
// Check that </think> tag was handled as reasoning
|
|
493
|
+
expect(reasoningDeltaEvents.some(event =>
|
|
494
|
+
(event.delta.content?.[0] as t.ReasoningDeltaUpdate | undefined)?.think.includes('</think>')
|
|
495
|
+
)).toBe(true);
|
|
496
|
+
|
|
497
|
+
// Check that content after </think> was handled as regular text
|
|
498
|
+
expect(messageDeltaEvents.some(event =>
|
|
499
|
+
(event.delta.content?.[0] as t.MessageDeltaUpdate | undefined)?.text.includes('Back')
|
|
500
|
+
)).toBe(true);
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
it('should ignore think tags inside code blocks', async () => {
|
|
504
|
+
const runId = nanoid();
|
|
505
|
+
const messageDeltaEvents: t.MessageDeltaEvent[] = [];
|
|
506
|
+
const reasoningDeltaEvents: t.ReasoningDeltaEvent[] = [];
|
|
507
|
+
|
|
508
|
+
const streamHandler = new SplitStreamHandler({
|
|
509
|
+
runId,
|
|
510
|
+
handlers: {
|
|
511
|
+
[GraphEvents.ON_MESSAGE_DELTA]: ({ data }): void => {
|
|
512
|
+
messageDeltaEvents.push(data);
|
|
513
|
+
},
|
|
514
|
+
[GraphEvents.ON_REASONING_DELTA]: ({ data }): void => {
|
|
515
|
+
reasoningDeltaEvents.push(data);
|
|
516
|
+
},
|
|
517
|
+
},
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
const content = 'Regular text. ```<think>This should stay as code</think>``` More text.';
|
|
521
|
+
|
|
522
|
+
const stream = createMockStream({
|
|
523
|
+
text: content,
|
|
524
|
+
streamRate: 5,
|
|
525
|
+
})();
|
|
526
|
+
|
|
527
|
+
for await (const chunk of stream) {
|
|
528
|
+
streamHandler.handle(chunk);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// Check that think tags inside code blocks were treated as regular text
|
|
532
|
+
expect(messageDeltaEvents.some(event =>
|
|
533
|
+
(event.delta.content?.[0] as t.MessageDeltaUpdate | undefined)?.text.includes('Regular')
|
|
534
|
+
)).toBe(true);
|
|
535
|
+
|
|
536
|
+
// Verify no reasoning events were generated
|
|
537
|
+
expect(reasoningDeltaEvents.length).toBe(0);
|
|
538
|
+
});
|
|
539
|
+
});
|