File size: 8,558 Bytes
391c43e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, it, expect, vi } from 'vitest';
import { parseStreamingResponse } from '../streaming-parser';

function makeSSEStream(chunks: string[]): Response {
  const encoder = new TextEncoder();
  const stream = new ReadableStream({
    start(controller) {
      for (const chunk of chunks) {
        controller.enqueue(encoder.encode(chunk));
      }
      controller.close();
    },
  });
  return new Response(stream);
}

function sseChunk(data: object): string {
  return `data: ${JSON.stringify(data)}\n\n`;
}

describe('streaming-parser reasoning merge', () => {
  it('merges id-less reasoning_details into a single entry', async () => {
    const chunks = [
      sseChunk({
        choices: [{ delta: { reasoning_details: [{ type: 'thinking', text: 'First ' }] }, index: 0 }],
      }),
      sseChunk({
        choices: [{ delta: { reasoning_details: [{ type: 'thinking', text: 'second ' }] }, index: 0 }],
      }),
      sseChunk({
        choices: [{ delta: { reasoning_details: [{ type: 'thinking', text: 'third.' }] }, index: 0 }],
      }),
      sseChunk({ choices: [{ delta: { content: 'Answer.' }, index: 0, finish_reason: 'stop' }] }),
      'data: [DONE]\n\n',
    ];

    const response = makeSSEStream(chunks);
    const result = await parseStreamingResponse(response, { provider: 'openrouter', model: 'test' });

    expect(result.reasoningDetails).toHaveLength(1);
    expect(result.reasoningDetails![0].text).toBe('First second third.');
    expect(result.content).toBe('Answer.');
  });

  it('keeps entries with different ids separate', async () => {
    const chunks = [
      sseChunk({
        choices: [{ delta: { reasoning_details: [{ id: 'r1', type: 'thinking', text: 'Block 1' }] }, index: 0 }],
      }),
      sseChunk({
        choices: [{ delta: { reasoning_details: [{ id: 'r2', type: 'thinking', text: 'Block 2' }] }, index: 0 }],
      }),
      sseChunk({ choices: [{ delta: { content: 'Done.' }, index: 0, finish_reason: 'stop' }] }),
      'data: [DONE]\n\n',
    ];

    const response = makeSSEStream(chunks);
    const result = await parseStreamingResponse(response, { provider: 'openrouter', model: 'test' });

    expect(result.reasoningDetails).toHaveLength(2);
    expect(result.reasoningDetails![0].text).toBe('Block 1');
    expect(result.reasoningDetails![1].text).toBe('Block 2');
  });

  it('updates existing entry by id (cumulative snapshot pattern)', async () => {
    const chunks = [
      sseChunk({
        choices: [{ delta: { reasoning_details: [{ id: 'r1', type: 'thinking', text: 'Hello' }] }, index: 0 }],
      }),
      sseChunk({
        choices: [{ delta: { reasoning_details: [{ id: 'r1', type: 'thinking', text: 'Hello world' }] }, index: 0 }],
      }),
      sseChunk({ choices: [{ delta: { content: 'Done.' }, index: 0, finish_reason: 'stop' }] }),
      'data: [DONE]\n\n',
    ];

    const response = makeSSEStream(chunks);
    const result = await parseStreamingResponse(response, { provider: 'gemini', model: 'test' });

    expect(result.reasoningDetails).toHaveLength(1);
    expect(result.reasoningDetails![0].text).toBe('Hello world');
  });

  it('creates synthetic reasoningDetails entry from reasoning buffer', async () => {
    const chunks = [
      sseChunk({
        choices: [{ delta: { reasoning: 'Thinking about this...' }, index: 0 }],
      }),
      sseChunk({ choices: [{ delta: { content: 'Answer.' }, index: 0, finish_reason: 'stop' }] }),
      'data: [DONE]\n\n',
    ];

    const response = makeSSEStream(chunks);
    const result = await parseStreamingResponse(response, { provider: 'openrouter', model: 'deepseek-reasoner' });

    expect(result.reasoning).toBe('Thinking about this...');
    expect(result.reasoningDetails).toHaveLength(1);
    expect(result.reasoningDetails![0].type).toBe('thinking');
    expect(result.reasoningDetails![0].text).toBe('Thinking about this...');
  });

  it('does not create synthetic entry when reasoningDetails already has text', async () => {
    const chunks = [
      sseChunk({
        choices: [{ delta: { reasoning: 'incremental', reasoning_details: [{ type: 'thinking', text: 'incremental' }] }, index: 0 }],
      }),
      sseChunk({ choices: [{ delta: { content: 'Done.' }, index: 0, finish_reason: 'stop' }] }),
      'data: [DONE]\n\n',
    ];

    const response = makeSSEStream(chunks);
    const result = await parseStreamingResponse(response, { provider: 'openrouter', model: 'test' });

    expect(result.reasoningDetails).toHaveLength(1);
    expect(result.reasoningDetails![0].text).toBe('incremental');
  });

  it('emits reasoning_delta progress events for merged chunks', async () => {
    const progressEvents: { event: string; data: any }[] = [];
    const onProgress = (event: string, data?: any) => progressEvents.push({ event, data });

    const chunks = [
      sseChunk({
        choices: [{ delta: { reasoning_details: [{ type: 'thinking', text: 'A' }] }, index: 0 }],
      }),
      sseChunk({
        choices: [{ delta: { reasoning_details: [{ type: 'thinking', text: 'B' }] }, index: 0 }],
      }),
      sseChunk({ choices: [{ delta: { content: 'X' }, index: 0, finish_reason: 'stop' }] }),
      'data: [DONE]\n\n',
    ];

    const response = makeSSEStream(chunks);
    await parseStreamingResponse(response, { provider: 'openrouter', model: 'test', onProgress });

    const reasoningDeltas = progressEvents.filter(e => e.event === 'reasoning_delta');
    expect(reasoningDeltas).toHaveLength(2);
    expect(reasoningDeltas[0].data.text).toBe('A');
    expect(reasoningDeltas[1].data.text).toBe('B');
  });
});

describe('streaming-parser reasoning_complete closure', () => {
  function collectProgress() {
    const progressEvents: { event: string; data?: any }[] = [];
    const onProgress = (event: string, data?: any) => progressEvents.push({ event, data });
    return { progressEvents, onProgress };
  }

  it('emits reasoning_complete at stream end for a reasoning-only response (delta.reasoning)', async () => {
    const { progressEvents, onProgress } = collectProgress();
    const chunks = [
      sseChunk({ choices: [{ delta: { reasoning: 'Thinking about it. ' }, index: 0 }] }),
      sseChunk({ choices: [{ delta: { reasoning: 'Still thinking.' }, index: 0, finish_reason: 'stop' }] }),
      'data: [DONE]\n\n',
    ];

    await parseStreamingResponse(makeSSEStream(chunks), { provider: 'openrouter', model: 'test', onProgress });

    const completes = progressEvents.filter(e => e.event === 'reasoning_complete');
    expect(completes).toHaveLength(1);
    expect(completes[0].data.reasoning).toBe('Thinking about it. Still thinking.');
  });

  it('emits reasoning_complete exactly once when delta.reasoning is followed by content', async () => {
    const { progressEvents, onProgress } = collectProgress();
    const chunks = [
      sseChunk({ choices: [{ delta: { reasoning: 'Plan first.' }, index: 0 }] }),
      sseChunk({ choices: [{ delta: { content: 'The answer.' }, index: 0, finish_reason: 'stop' }] }),
      'data: [DONE]\n\n',
    ];

    await parseStreamingResponse(makeSSEStream(chunks), { provider: 'openrouter', model: 'test', onProgress });

    expect(progressEvents.filter(e => e.event === 'reasoning_complete')).toHaveLength(1);
  });

  it('does not duplicate reasoning_complete when the think-block was already closed by tool calls', async () => {
    const { progressEvents, onProgress } = collectProgress();
    const chunks = [
      sseChunk({ choices: [{ delta: { content: '<think>Let me act.' }, index: 0 }] }),
      sseChunk({
        choices: [{
          delta: { tool_calls: [{ index: 0, id: 'tc1', function: { name: 'bash', arguments: '{"command":"ls"}' } }] },
          index: 0,
          finish_reason: 'tool_calls',
        }],
      }),
      'data: [DONE]\n\n',
    ];

    await parseStreamingResponse(makeSSEStream(chunks), { provider: 'openrouter', model: 'test', onProgress });

    expect(progressEvents.filter(e => e.event === 'reasoning_complete')).toHaveLength(1);
  });

  it('does not emit reasoning_complete when there was no reasoning', async () => {
    const { progressEvents, onProgress } = collectProgress();
    const chunks = [
      sseChunk({ choices: [{ delta: { content: 'Plain answer.' }, index: 0, finish_reason: 'stop' }] }),
      'data: [DONE]\n\n',
    ];

    await parseStreamingResponse(makeSSEStream(chunks), { provider: 'openrouter', model: 'test', onProgress });

    expect(progressEvents.filter(e => e.event === 'reasoning_complete')).toHaveLength(0);
  });
});