File size: 23,335 Bytes
fd211b3
 
 
 
 
 
 
 
e3e4fcf
99f8658
 
e3e4fcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1357b6
e3e4fcf
 
f1357b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3e4fcf
 
 
 
 
 
f1357b6
e3e4fcf
f1357b6
e3e4fcf
 
f1357b6
e3e4fcf
 
 
 
 
f1357b6
e3e4fcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1357b6
e3e4fcf
 
 
 
 
 
 
 
 
f1357b6
e3e4fcf
 
 
 
 
 
 
 
 
fd211b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3e4fcf
 
 
 
 
 
 
 
 
 
 
fd211b3
e3e4fcf
fd211b3
e3e4fcf
 
 
 
fd211b3
e3e4fcf
fd211b3
 
 
 
 
 
 
e3e4fcf
fd211b3
 
 
 
e3e4fcf
fd211b3
 
 
 
 
 
 
e3e4fcf
fd211b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3e4fcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd211b3
e3e4fcf
fd211b3
e3e4fcf
 
 
 
 
 
 
 
 
 
 
fd211b3
e3e4fcf
fd211b3
 
 
 
 
 
 
 
 
 
 
e3e4fcf
fd211b3
 
 
 
e3e4fcf
fd211b3
 
 
 
 
 
 
e3e4fcf
fd211b3
 
 
 
 
 
99f8658
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3e4fcf
 
99f8658
 
 
 
 
 
 
 
e3e4fcf
 
 
 
 
99f8658
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3e4fcf
 
99f8658
 
 
 
 
 
 
 
e3e4fcf
 
 
 
 
 
 
 
 
 
99f8658
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd211b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3e4fcf
 
fd211b3
 
 
 
 
e3e4fcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/**

 * stream-transform.js - chataibot JSON 流 → OpenAI/Anthropic SSE 转换器

 *

 * chataibot.pro 流式响应格式 (紧凑 JSON 对象流,无换行分隔):

 *   {"type":"botType","data":"gpt-4o"}{"type":"chunk","data":"Hello"}...

 *   {"type":"finalResult","data":{"mainText":"...","questions":[...]}}

 */

import { PassThrough } from 'stream';
import { parseToolCalls, toOpenAIToolCalls, toAnthropicToolUse, detectToolCallStart } from './tool-prompt.js';

/**

 * 流式重复内容检测器

 *

 * chataibot.pro 的搜索模型有时会返回重复内容 (同一段回答重复 2-3 次)。

 * 策略: 滑动窗口检测 — 当新增文本中出现之前已输出内容的大段重复时,截断。

 *

 * @param {number} minRepeatLen - 最小重复片段长度 (默认 150 字符)

 * @returns {{ feed(text): { emit: string, repeated: boolean }, getText(): string }}

 */
function createRepeatDetector(minRepeatLen = 150) {
  let fullText = '';

  return {
    /**

     * 送入新的 chunk 文本,返回应该输出的部分

     * @returns {{ emit: string, repeated: boolean }}

     */
    feed(chunk) {
      if (!chunk) return { emit: '', repeated: false };

      const prevLen = fullText.length;
      fullText += chunk;

      // 文本太短时不检测
      if (fullText.length < minRepeatLen * 2) {
        return { emit: chunk, repeated: false };
      }

      // 检测: 取已有文本的后 minRepeatLen 字符作为 needle,
      // 在之前的文本 (不含最后 needle 自身) 中查找
      const windowSize = Math.min(minRepeatLen, Math.floor(fullText.length / 3));
      if (windowSize < 80) return { emit: chunk, repeated: false };

      // 从新加入的文本末尾往回取 windowSize 长度的片段
      const needle = fullText.substring(fullText.length - windowSize);
      const searchArea = fullText.substring(0, fullText.length - windowSize);
      const idx = searchArea.indexOf(needle);

      if (idx >= 0) {
        // 找到重复 — 计算需要截断的位置
        // needle 在 fullText 中首次出现于 idx,第二次出现于 fullText.length - windowSize
        // 我们需要保留到第一次出现结束的部分,截掉重复
        const repeatStart = fullText.length - windowSize;

        // 只输出 chunk 中在 repeatStart 之前的部分
        const safeEnd = repeatStart - prevLen;
        const emit = safeEnd > 0 ? chunk.substring(0, safeEnd) : '';

        // 回滚 fullText 到截断点
        fullText = fullText.substring(0, repeatStart);

        console.log(`[RepeatDetect] 检测到重复 (${windowSize} chars),截断输出`);
        return { emit, repeated: true };
      }

      return { emit: chunk, repeated: false };
    },

    getText() {
      return fullText;
    },
  };
}

/**

 * 流预检 — 缓冲流的前几个 JSON 对象,检测是否为 streamingError

 *

 * chataibot 有时在流开始后才返回 "Insufficient credits" 等错误。

 * 此函数先读取前几个对象,如果是错误就 reject (允许上层换号重试),

 * 否则返回一个包装过的 stream (先重放缓冲的原始数据,再接续剩余流),

 * transform 函数无需任何修改。

 *

 * @param {ReadableStream} upstreamStream

 * @returns {Promise<ReadableStream>}

 * @throws {Error} 如果流的第一个实质性对象就是 streamingError

 */
export function probeStream(upstreamStream, timeoutMs = 30000) {
  return new Promise((resolve, reject) => {
    let resolved = false;
    let rawChunks = [];

    function cleanup() {
      clearTimeout(timer);
      upstreamStream.removeListener('data', onData);
      upstreamStream.removeListener('end', onEnd);
      upstreamStream.removeListener('error', onError);
    }

    // 超时保护: 防止上游永不响应导致请求永久挂起
    const timer = setTimeout(() => {
      if (resolved) return;
      resolved = true;
      cleanup();
      upstreamStream.destroy();
      reject(new Error('probeStream timeout'));
    }, timeoutMs);

    const parser = createJsonStreamParser((obj) => {
      if (resolved) return;

      if (obj.type === 'streamingError') {
        resolved = true;
        cleanup();
        upstreamStream.resume();
        reject(Object.assign(
          new Error(obj.data || 'Streaming error'),
          { statusCode: 429 }
        ));
        return;
      }

      if (obj.type === 'chunk' || obj.type === 'reasoningContent') {
        resolved = true;
        cleanup();
        upstreamStream.pause();

        const wrapped = new PassThrough();
        for (const chunk of rawChunks) wrapped.write(chunk);
        upstreamStream.pipe(wrapped);
        upstreamStream.resume();

        resolve(wrapped);
      }
    });

    function onData(chunk) {
      rawChunks.push(chunk);
      parser.feed(chunk);
    }
    function onEnd() {
      if (resolved) return;
      parser.flush();
      if (!resolved) {
        resolved = true;
        cleanup();
        const wrapped = new PassThrough();
        for (const chunk of rawChunks) wrapped.write(chunk);
        wrapped.end();
        resolve(wrapped);
      }
    }
    function onError(err) {
      if (resolved) return;
      resolved = true;
      cleanup();
      reject(err);
    }

    upstreamStream.on('data', onData);
    upstreamStream.on('end', onEnd);
    upstreamStream.on('error', onError);
  });
}

/**

 * JSON 对象流解析器 — 通过花括号计数提取完整 JSON 对象

 * chataibot 返回的不是 NDJSON(无换行),而是紧凑的 JSON 对象序列

 */
function createJsonStreamParser(onObject) {
  let buffer = '';
  let depth = 0;
  let inString = false;
  let escape = false;
  let objStart = -1;

  return {
    feed(chunk) {
      buffer += chunk;
      for (let i = 0; i < buffer.length; i++) {
        const ch = buffer[i];

        if (escape) { escape = false; continue; }
        if (ch === '\\' && inString) { escape = true; continue; }
        if (ch === '"') { inString = !inString; continue; }
        if (inString) continue;

        if (ch === '{') {
          if (depth === 0) objStart = i;
          depth++;
        } else if (ch === '}') {
          depth--;
          if (depth === 0 && objStart >= 0) {
            const jsonStr = buffer.substring(objStart, i + 1);
            try { onObject(JSON.parse(jsonStr)); } catch {}
            objStart = -1;
          }
        }
      }

      // 清理已消费的部分
      if (objStart >= 0) {
        buffer = buffer.substring(objStart);
        objStart = 0;
      } else if (depth === 0) {
        buffer = '';
      }
    },
    flush() {
      if (buffer.trim()) {
        try { onObject(JSON.parse(buffer.trim())); } catch {}
      }
      buffer = '';
    },
  };
}

/**

 * chataibot NDJSON → OpenAI SSE 格式

 * @param {Function} onStreamError - 可选回调,流中出现 streamingError 时调用

 */
export function transformToOpenAISSE(upstreamStream, res, model, requestId, onStreamError) {
  res.writeHead(200, {
    'Content-Type': 'text/event-stream',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Access-Control-Allow-Origin': '*',
  });

  const ts = Math.floor(Date.now() / 1000);

  function writeChunk(delta, finishReason = null) {
    const obj = {
      id: requestId,
      object: 'chat.completion.chunk',
      created: ts,
      model,
      choices: [{ index: 0, delta, finish_reason: finishReason }],
    };
    res.write(`data: ${JSON.stringify(obj)}\n\n`);
  }

  // 先发送 role chunk
  writeChunk({ role: 'assistant', content: '' });

  const detector = createRepeatDetector();
  let stopped = false;

  function finishStream() {
    if (res.writableEnded || stopped) return;
    stopped = true;
    writeChunk({}, 'stop');
    res.write('data: [DONE]\n\n');
    res.end();
  }

  const parser = createJsonStreamParser((obj) => {
    if (stopped) return;
    switch (obj.type) {
      case 'chunk': {
        const { emit, repeated } = detector.feed(obj.data);
        if (emit) writeChunk({ content: emit });
        if (repeated) finishStream();
        break;
      }

      case 'reasoningContent':
        // 兼容 OpenAI o-series reasoning 输出
        writeChunk({ reasoning_content: obj.data });
        break;

      case 'finalResult':
        finishStream();
        break;

      case 'streamingError':
        if (onStreamError) onStreamError(obj.data);
        finishStream();
        break;
    }
  });

  upstreamStream.on('data', (chunk) => parser.feed(chunk));
  upstreamStream.on('end', () => {
    parser.flush();
    finishStream();
  });
  upstreamStream.on('error', () => {
    if (!res.writableEnded) {
      res.write('data: [DONE]\n\n');
      res.end();
    }
  });
}

/**

 * chataibot NDJSON → Anthropic SSE 格式

 * @param {Function} onStreamError - 可选回调,流中出现 streamingError 时调用

 */
export function transformToAnthropicSSE(upstreamStream, res, model, requestId, onStreamError) {
  res.writeHead(200, {
    'Content-Type': 'text/event-stream',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Access-Control-Allow-Origin': '*',
  });

  function writeEvent(event, data) {
    res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
  }

  let headerSent = false;
  let blockIndex = 0;

  function ensureHeader() {
    if (headerSent) return;
    headerSent = true;

    writeEvent('message_start', {
      type: 'message_start',
      message: {
        id: requestId,
        type: 'message',
        role: 'assistant',
        model,
        content: [],
        stop_reason: null,
        usage: { input_tokens: 0, output_tokens: 0 },
      },
    });

    writeEvent('content_block_start', {
      type: 'content_block_start',
      index: blockIndex,
      content_block: { type: 'text', text: '' },
    });
  }

  const detector = createRepeatDetector();
  let stopped = false;

  function finishAnthropicStream() {
    if (res.writableEnded || stopped) return;
    stopped = true;
    ensureHeader();
    writeEvent('content_block_stop', { type: 'content_block_stop', index: blockIndex });
    writeEvent('message_delta', {
      type: 'message_delta',
      delta: { stop_reason: 'end_turn' },
      usage: { output_tokens: 0 },
    });
    writeEvent('message_stop', { type: 'message_stop' });
    res.end();
  }

  const parser = createJsonStreamParser((obj) => {
    if (stopped) return;
    switch (obj.type) {
      case 'chunk': {
        const { emit, repeated } = detector.feed(obj.data);
        if (emit) {
          ensureHeader();
          writeEvent('content_block_delta', {
            type: 'content_block_delta',
            index: blockIndex,
            delta: { type: 'text_delta', text: emit },
          });
        }
        if (repeated) finishAnthropicStream();
        break;
      }

      case 'reasoningContent':
        ensureHeader();
        writeEvent('content_block_delta', {
          type: 'content_block_delta',
          index: blockIndex,
          delta: { type: 'text_delta', text: obj.data },
        });
        break;

      case 'finalResult':
        finishAnthropicStream();
        break;

      case 'streamingError':
        if (onStreamError) onStreamError(obj.data);
        finishAnthropicStream();
        break;
    }
  });

  upstreamStream.on('data', (chunk) => parser.feed(chunk));
  upstreamStream.on('end', () => {
    parser.flush();
    finishAnthropicStream();
  });
  upstreamStream.on('error', () => {
    if (!res.writableEnded) res.end();
  });
}

/**

 * chataibot → OpenAI SSE (带工具调用检测)

 *

 * 策略: 先正常流式输出文本。当检测到 ```tool_calls 开头时,

 * 停止流式文本输出,缓冲剩余内容。流结束后解析完整文本,

 * 如果包含工具调用则发送 tool_calls chunk,否则补发剩余文本。

 */
export function transformToOpenAISSEWithTools(upstreamStream, res, model, requestId, onStreamError) {
  res.writeHead(200, {
    'Content-Type': 'text/event-stream',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Access-Control-Allow-Origin': '*',
  });

  const ts = Math.floor(Date.now() / 1000);
  let fullText = '';
  let toolCallDetected = false;
  let streamError = false;

  function writeChunk(delta, finishReason = null) {
    const obj = {
      id: requestId,
      object: 'chat.completion.chunk',
      created: ts,
      model,
      choices: [{ index: 0, delta, finish_reason: finishReason }],
    };
    res.write(`data: ${JSON.stringify(obj)}\n\n`);
  }

  writeChunk({ role: 'assistant', content: '' });

  const detector = createRepeatDetector();

  const parser = createJsonStreamParser((obj) => {
    switch (obj.type) {
      case 'chunk':
        fullText += obj.data;
        if (!toolCallDetected) {
          if (detectToolCallStart(fullText)) {
            toolCallDetected = true;
          } else {
            const { emit, repeated } = detector.feed(obj.data);
            if (emit) writeChunk({ content: emit });
            if (repeated) {
              // 重复内容,停止流式输出但不影响 tool call 解析
            }
          }
        }
        break;

      case 'reasoningContent':
        if (!toolCallDetected) {
          writeChunk({ reasoning_content: obj.data });
        }
        break;

      case 'finalResult':
        if (obj.data?.mainText) fullText = obj.data.mainText;
        // 最终处理在 end 事件中
        break;

      case 'streamingError':
        streamError = true;
        if (onStreamError) onStreamError(obj.data);
        break;
    }
  });

  upstreamStream.on('data', (chunk) => parser.feed(chunk));
  upstreamStream.on('end', () => {
    parser.flush();
    if (res.writableEnded) return;

    if (streamError) {
      writeChunk({}, 'stop');
      res.write('data: [DONE]\n\n');
      res.end();
      return;
    }

    // 解析完整文本中的 tool calls
    const { hasToolCalls, toolCalls, textContent } = parseToolCalls(fullText);

    if (hasToolCalls) {
      // 如果之前已经流式输出了部分文本 (tool_calls 标记之前的文本)
      // 而解析后 textContent 为空或很少,无需额外处理

      // 发送 tool_calls
      const openaiToolCalls = toOpenAIToolCalls(toolCalls);
      for (let i = 0; i < openaiToolCalls.length; i++) {
        const tc = openaiToolCalls[i];
        // 首个 chunk: 含 tool call id, type, function.name, function.arguments 开始部分
        writeChunk({
          tool_calls: [{
            index: i,
            id: tc.id,
            type: 'function',
            function: { name: tc.function.name, arguments: tc.function.arguments },
          }],
        });
      }
      writeChunk({}, 'tool_calls');
    } else {
      // 没有工具调用 — 如果之前因检测到 ```tool_calls 停了输出
      // 需要补发被缓冲的文本
      if (toolCallDetected) {
        // 找到之前已输出的部分,补发剩余
        const markerIdx = fullText.indexOf('```tool_calls');
        if (markerIdx >= 0) {
          writeChunk({ content: fullText.substring(markerIdx) });
        }
      }
      writeChunk({}, 'stop');
    }

    res.write('data: [DONE]\n\n');
    res.end();
  });

  upstreamStream.on('error', () => {
    if (!res.writableEnded) {
      res.write('data: [DONE]\n\n');
      res.end();
    }
  });
}

/**

 * chataibot → Anthropic SSE (带工具调用检测)

 *

 * 同 OpenAI 版本,先流式输出文本,检测到工具调用后缓冲,

 * 最终解析并发送 tool_use content blocks。

 */
export function transformToAnthropicSSEWithTools(upstreamStream, res, model, requestId, onStreamError) {
  res.writeHead(200, {
    'Content-Type': 'text/event-stream',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Access-Control-Allow-Origin': '*',
  });

  function writeEvent(event, data) {
    res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
  }

  let headerSent = false;
  let textBlockIndex = 0;
  let fullText = '';
  let toolCallDetected = false;
  let streamError = false;

  function ensureHeader() {
    if (headerSent) return;
    headerSent = true;
    writeEvent('message_start', {
      type: 'message_start',
      message: {
        id: requestId,
        type: 'message',
        role: 'assistant',
        model,
        content: [],
        stop_reason: null,
        usage: { input_tokens: 0, output_tokens: 0 },
      },
    });
    writeEvent('content_block_start', {
      type: 'content_block_start',
      index: textBlockIndex,
      content_block: { type: 'text', text: '' },
    });
  }

  const detector = createRepeatDetector();

  const parser = createJsonStreamParser((obj) => {
    switch (obj.type) {
      case 'chunk':
        fullText += obj.data;
        if (!toolCallDetected) {
          if (detectToolCallStart(fullText)) {
            toolCallDetected = true;
          } else {
            const { emit, repeated } = detector.feed(obj.data);
            if (emit) {
              ensureHeader();
              writeEvent('content_block_delta', {
                type: 'content_block_delta',
                index: textBlockIndex,
                delta: { type: 'text_delta', text: emit },
              });
            }
            // repeated 时停止流式输出,但不影响 tool call 解析
          }
        }
        break;

      case 'reasoningContent':
        if (!toolCallDetected) {
          ensureHeader();
          writeEvent('content_block_delta', {
            type: 'content_block_delta',
            index: textBlockIndex,
            delta: { type: 'text_delta', text: obj.data },
          });
        }
        break;

      case 'finalResult':
        if (obj.data?.mainText) fullText = obj.data.mainText;
        break;

      case 'streamingError':
        streamError = true;
        if (onStreamError) onStreamError(obj.data);
        break;
    }
  });

  upstreamStream.on('data', (chunk) => parser.feed(chunk));
  upstreamStream.on('end', () => {
    parser.flush();
    if (res.writableEnded) return;
    ensureHeader();

    if (streamError) {
      writeEvent('content_block_stop', { type: 'content_block_stop', index: textBlockIndex });
      writeEvent('message_delta', {
        type: 'message_delta',
        delta: { stop_reason: 'end_turn' },
        usage: { output_tokens: 0 },
      });
      writeEvent('message_stop', { type: 'message_stop' });
      res.end();
      return;
    }

    const { hasToolCalls, toolCalls, textContent } = parseToolCalls(fullText);

    if (hasToolCalls) {
      // 关闭文本 block
      writeEvent('content_block_stop', { type: 'content_block_stop', index: textBlockIndex });

      // 发送 tool_use blocks
      const toolUseBlocks = toAnthropicToolUse(toolCalls);
      for (let i = 0; i < toolUseBlocks.length; i++) {
        const blockIdx = textBlockIndex + 1 + i;
        const tu = toolUseBlocks[i];
        writeEvent('content_block_start', {
          type: 'content_block_start',
          index: blockIdx,
          content_block: { type: 'tool_use', id: tu.id, name: tu.name, input: {} },
        });
        writeEvent('content_block_delta', {
          type: 'content_block_delta',
          index: blockIdx,
          delta: { type: 'input_json_delta', partial_json: JSON.stringify(tu.input) },
        });
        writeEvent('content_block_stop', { type: 'content_block_stop', index: blockIdx });
      }

      writeEvent('message_delta', {
        type: 'message_delta',
        delta: { stop_reason: 'tool_use' },
        usage: { output_tokens: 0 },
      });
    } else {
      // 没有工具调用 — 补发被缓冲的文本
      if (toolCallDetected) {
        const markerIdx = fullText.indexOf('```tool_calls');
        if (markerIdx >= 0) {
          writeEvent('content_block_delta', {
            type: 'content_block_delta',
            index: textBlockIndex,
            delta: { type: 'text_delta', text: fullText.substring(markerIdx) },
          });
        }
      }
      writeEvent('content_block_stop', { type: 'content_block_stop', index: textBlockIndex });
      writeEvent('message_delta', {
        type: 'message_delta',
        delta: { stop_reason: 'end_turn' },
        usage: { output_tokens: 0 },
      });
    }

    writeEvent('message_stop', { type: 'message_stop' });
    res.end();
  });

  upstreamStream.on('error', () => {
    if (!res.writableEnded) res.end();
  });
}

/**

 * 消费 NDJSON 流,收集完整响应 (用于非流式请求)

 */
export function collectFullResponse(upstreamStream) {
  return new Promise((resolve, reject) => {
    let text = '';
    let reasoning = '';
    let actualModel = '';

    const parser = createJsonStreamParser((obj) => {
      switch (obj.type) {
        case 'botType':
          actualModel = obj.data;
          break;
        case 'chunk':
          text += obj.data;
          break;
        case 'reasoningContent':
          reasoning += obj.data;
          break;
        case 'finalResult':
          if (obj.data?.mainText) text = obj.data.mainText;
          break;
        case 'streamingError':
          reject(new Error(obj.data || 'Streaming error'));
          break;
      }
    });

    upstreamStream.on('data', (chunk) => parser.feed(chunk));
    upstreamStream.on('end', () => {
      parser.flush();
      // 去重: 检测并移除重复的大段文本
      text = deduplicateText(text);
      resolve({ text, reasoning, model: actualModel });
    });
    upstreamStream.on('error', reject);
  });
}

/**

 * 对完整文本做后处理去重

 * 检测是否有大段内容重复出现,保留第一次出现

 */
function deduplicateText(text) {
  if (!text || text.length < 300) return text;

  // 策略: 尝试不同的片段长度 (200, 300, 500),
  // 从文本中间位置取一个 needle,看后半部分是否有重复
  for (const windowSize of [500, 300, 200]) {
    if (text.length < windowSize * 2) continue;

    // 取文本前 1/3 处的片段作为 needle
    const start = Math.floor(text.length / 3);
    const needle = text.substring(start, start + windowSize);

    // 在 needle 之后的文本中查找重复
    const searchFrom = start + windowSize;
    const repeatIdx = text.indexOf(needle, searchFrom);

    if (repeatIdx >= 0) {
      // 找到重复 — 截断到重复开始处
      console.log(`[Dedup] 非流式去重: ${text.length}${repeatIdx} (window=${windowSize})`);
      return text.substring(0, repeatIdx).trimEnd();
    }
  }

  return text;
}