pi-freeflow 1.44.0 → 1.44.1

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.
@@ -16,16 +16,17 @@ import {
16
16
  WATCHDOG_SSE_MIN_SAMPLES,
17
17
  WATCHDOG_SSE_WINDOW,
18
18
  } from "./config.ts";
19
+ import { looksLikeSse } from "./sse-aggregator.ts";
20
+ import type { ToolcallStreamAudit } from "./toolcall-audit.ts";
19
21
  import {
20
- accumulateToolCallName,
21
- cloakMessagesContent,
22
- looksLikeSse,
23
- type CaseRestoreMap,
24
- type FindGlobRestore,
25
- } from "./opencode-fingerprint.ts";
26
- import { restoreToolNameForCaller } from "./tool-translation.ts";
27
- import { repairToolArguments } from "./tool-args.ts";
28
- import type { ToolcallRepairSink, ToolcallStreamAudit } from "./toolcall-audit.ts";
22
+ createSseStreamCloakState,
23
+ flushBufferedToolArgs,
24
+ rewriteSseBlock,
25
+ shouldBypassStreamCloak,
26
+ type StreamCloakOptions,
27
+ } from "./stream-cloak.ts";
28
+
29
+ export * from "./stream-cloak.ts";
29
30
 
30
31
  /**
31
32
  * A premature stream end counts as "substantial" only when BOTH thresholds
@@ -288,869 +289,9 @@ export function _resetSseStatsForTest(): void {
288
289
  sseOutcomes.length = 0;
289
290
  lastForwardedByteAt = 0;
290
291
  }
291
- /**
292
- * Per-request streaming cloak for the OpenCode fingerprint placeholder tools.
293
- *
294
- * The non-streaming aggregator (convertSseToJson in opencode-fingerprint.ts)
295
- * strips injected empty-schema placeholders and restores caller names before
296
- * the host ever sees them. Raw SSE passthrough would leak those placeholders
297
- * on the streamed path, so this section mirrors the aggregate semantics
298
- * event-by-event: function_call deltas whose name is in this request's
299
- * injected list are dropped, surviving names get caller casing (Bash) and the
300
- * Pi find->glob rename restored. Completed tool arguments are additionally
301
- * repaired against `argSchemas` on the terminal events that carry the whole
302
- * payload. State is per-stream (no globals); the non-stream path is untouched.
303
- */
304
- export interface StreamCloakOptions {
305
- /** False when the caller declared no tools (chat folds dropped args to text). */
306
- callerHadTools: boolean;
307
- /** Lowercase placeholder -> caller casing (Bash), built per request. */
308
- caseRestore?: CaseRestoreMap;
309
- /** True when the caller sent Pi `find` (upstream `glob` maps back). */
310
- findGlob?: FindGlobRestore;
311
- /** Lowercase names this request injected (only these are ever dropped). */
312
- injected?: readonly string[];
313
- /** Request pathname; decides the Responses vs Chat event shapes. */
314
- pathname: string;
315
- /** Caller tool schemas for argument repair; undefined/empty means byte-identical. */
316
- argSchemas?: ReadonlyMap<string, Record<string, unknown>>;
317
- /** Normalize delta.reasoning to delta.reasoning_content for OpenAI/OMP compatibility. */
318
- normalizeReasoning?: boolean;
319
- }
320
-
321
- /** Per-stream drop tracking (delta events carry no name, only an index/id). */
322
- export interface SseStreamCloakState {
323
- responsesDroppedByIndex: Map<number, boolean>;
324
- responsesDroppedByItemId: Map<string, boolean>;
325
- messagesDroppedByIndex: Map<number, boolean>;
326
- /** Output index -> tool name, so nameless argument deltas can find their schema. */
327
- responsesNameByIndex: Map<number, string>;
328
- /** Anthropic block index -> tool name, same purpose for input_json deltas. */
329
- messagesNameByIndex: Map<number, string>;
330
- /**
331
- * Argument fragments for Anthropic tool_use blocks whose schema admits a
332
- * repair, keyed the same way as the name map above. Streaming APIs deliver
333
- * arguments in fragments, so a malformed packet can only be judged once
334
- * complete: those fragments are held here and re-emitted, repaired, on the
335
- * block's terminal event. Tools absent from `argSchemas` are never buffered,
336
- * so their arguments still stream incrementally exactly as before.
337
- */
338
- messagesArgsByIndex: Map<number, string>;
339
- /**
340
- * Chat tool calls tracked PER CALL (defect K1): keyed by the upstream
341
- * tool-call id whenever the provider sends one (`id:X`), else by the legacy
342
- * index bucket (`idx:N`). Free models label EVERY parallel call `index: 0`
343
- * while still sending per-call ids, so index-only keying merged their
344
- * fragment streams: one call was silently lost and the other received a
345
- * salvage of the merged junk. Drops, names, buffered arguments and the
346
- * once-only flush guard are per call; an injected placeholder's drop
347
- * verdict is never inherited by a real call that reuses its index.
348
- */
349
- chatCalls: Map<string, ChatTrackedCall>;
350
- /**
351
- * Observed delta index -> key of the call its id-less fragments currently
352
- * belong to. Resolution mirrors the aggregate cloak exactly
353
- * (sseToChatCompletionJson): an id-bearing delta (re)binds the index to its
354
- * id-call; id-less deltas follow the current owner, else the legacy
355
- * `idx:N` bucket. Never consulted for host-visible indices — that is what
356
- * {@link chatIndexClaims} is for.
357
- */
358
- chatIndexOwner: Map<number, string>;
359
- /** Host-index claims: index -> key of the call that FIRST assigned it. Set once, never rebound. */
360
- chatIndexClaims: Map<number, string>;
361
- /**
362
- * Fresh sequential index handed out when a call's upstream index is already
363
- * claimed by another tracked call; strictly above every index seen or
364
- * assigned so far in this stream.
365
- */
366
- chatNextIndex: number;
367
- /**
368
- * Per-turn tool-call audit sink for the rewrites this state performs
369
- * (index collisions, dropped name restatements, withheld arguments, repaired
370
- * arguments). Absent when the caller did not request an audit.
371
- */
372
- audit?: ToolcallRepairSink;
373
- }
374
-
375
- /**
376
- * Streaming state of ONE chat tool call. Exported only because it hangs off
377
- * the exported per-stream state; treat it as pipe-internal.
378
- */
379
- export interface ChatTrackedCall {
380
- /** Never emit this call: injected placeholder, or a tool-less caller's call. */
381
- dropped: boolean;
382
- /**
383
- * Restored host-facing name, folded from every name-bearing delta:
384
- * fragments append, a delta that also carries arguments restates the whole
385
- * name and replaces (see accumulateToolCallName).
386
- */
387
- name: string | undefined;
388
- /**
389
- * True once a name-bearing delta decided this tool is repairable and its
390
- * fragments must be withheld: the packet is only judgeable once whole, so
391
- * the fragments re-emerge whole+repaired on the call's terminal event.
392
- */
393
- buffered: boolean;
394
- /** Buffered argument prefix while {@link buffered} is true ("" until a fragment arrives). */
395
- args: string;
396
- /**
397
- * Host-visible index: the call's FIRST assigned index — its first observed
398
- * index — unless that upstream index was claimed by another tracked call,
399
- * in which case a fresh sequential index was handed out in arrival order.
400
- */
401
- hostIndex: number;
402
- /** Emitted-once guard: a flush entry already went out for this call (terminal or end-of-stream). */
403
- flushed: boolean;
404
- }
405
-
406
- /** Map key of a chat tool call that announced an upstream id. */
407
- function chatCallIdKey(id: string): string {
408
- return `id:${id}`;
409
- }
410
-
411
- /** Legacy map key of the id-less fragment bucket for one observed delta index. */
412
- function chatIndexKey(idx: number): string {
413
- return `idx:${idx}`;
414
- }
415
-
416
- export function createSseStreamCloakState(audit?: ToolcallRepairSink): SseStreamCloakState {
417
- const state: SseStreamCloakState = {
418
- responsesDroppedByIndex: new Map(),
419
- responsesDroppedByItemId: new Map(),
420
- messagesDroppedByIndex: new Map(),
421
- responsesNameByIndex: new Map(),
422
- messagesNameByIndex: new Map(),
423
- messagesArgsByIndex: new Map(),
424
- chatCalls: new Map(),
425
- chatIndexOwner: new Map(),
426
- chatIndexClaims: new Map(),
427
- chatNextIndex: 0,
428
- };
429
- if (audit !== undefined) state.audit = audit;
430
- return state;
431
- }
432
-
433
- /**
434
- * Emit every tool call still holding buffered arguments, then clear the
435
- * buffers. The fragments were withheld so the packet could be judged whole
436
- * (see {@link SseStreamCloakState}); the terminal event that normally releases
437
- * them never arrived when upstream cut the stream without one, and without
438
- * this the client would receive a tool call whose `arguments` were deleted and
439
- * never re-emitted. Repaired exactly as the terminal path repairs.
440
- *
441
- * Shape follows the pathname: Chat re-emits one `tool_calls` delta, Anthropic
442
- * one `input_json_delta` per open block. Responses carries whole arguments on
443
- * its `.done` event and buffers nothing, so it has nothing to flush.
444
- * Returns null when nothing is buffered.
445
- */
446
- export function flushBufferedToolArgs(
447
- state: SseStreamCloakState,
448
- cloak: StreamCloakOptions,
449
- ): string | null {
450
- if (state.messagesArgsByIndex.size > 0) {
451
- const blocks: string[] = [];
452
- for (const [index, args] of state.messagesArgsByIndex) {
453
- const name = state.messagesNameByIndex.get(index) ?? "";
454
- const repaired = repairToolArguments(name, args, cloak.argSchemas);
455
- if (repaired !== args) state.audit?.repair("arguments-repaired", name);
456
- blocks.push(
457
- `event: content_block_delta\ndata: ${JSON.stringify({
458
- type: "content_block_delta",
459
- index,
460
- delta: { type: "input_json_delta", partial_json: repaired },
461
- })}\n\n`,
462
- );
463
- }
464
- state.messagesArgsByIndex.clear();
465
- state.messagesNameByIndex.clear();
466
- return blocks.join("");
467
- }
468
- const calls: unknown[] = [];
469
- for (const call of state.chatCalls.values()) {
470
- // The flushed guard makes this one-shot even when a terminal flush
471
- // already emitted the call whole earlier in the stream.
472
- if (!call.buffered || call.flushed) continue;
473
- const name = call.name;
474
- if (name === undefined) continue;
475
- const args = call.args;
476
- call.flushed = true;
477
- call.buffered = false;
478
- call.args = "";
479
- const repairedArgs = repairToolArguments(name, args, cloak.argSchemas);
480
- if (repairedArgs !== args) state.audit?.repair("arguments-repaired", name);
481
- calls.push({ index: call.hostIndex, type: "function", function: { name, arguments: repairedArgs } });
482
- }
483
- if (calls.length === 0) return null;
484
- return `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { tool_calls: calls } }] })}\n\n`;
485
- }
486
-
487
- /**
488
- * True when the stream needs no rewrite and must flow byte-identical:
489
- * nothing injected, no casing to restore, no find->glob rename, and the
490
- * caller declared tools. Tool-less callers must still cloak on every path
491
- * when the injected record exists (legacy direct calls keep everything).
492
- */
493
- export function shouldBypassStreamCloak(cloak?: StreamCloakOptions): boolean {
494
- if (!cloak) return true;
495
- if (cloak.injected !== undefined && cloak.injected.length > 0) return false;
496
- if (cloak.caseRestore && Object.keys(cloak.caseRestore).length > 0) return false;
497
- if (cloak.findGlob?.renamedFindToGlob === true) return false;
498
- // Tool-less callers must still see tool calls stripped when the
499
- // injected record exists (legacy direct calls keep everything).
500
- if (!cloak.callerHadTools && cloak.injected !== undefined) return false;
501
- // A repairable schema means fragmented arguments must be buffered.
502
- if (cloak.argSchemas !== undefined && cloak.argSchemas.size > 0) return false;
503
- if (cloak.normalizeReasoning === true) return false;
504
- return true;
505
- }
506
-
507
- function isInjectedName(name: unknown, injected?: readonly string[]): boolean {
508
- return typeof name === "string" && injected !== undefined && injected.includes(name.toLowerCase());
509
- }
510
-
511
- /** True when the request declared a schema for this tool, so its arguments can be repaired. */
512
- function isRepairableTool(
513
- name: string,
514
- schemas: ReadonlyMap<string, Record<string, unknown>> | undefined,
515
- ): boolean {
516
- return schemas !== undefined && schemas.size > 0 && schemas.has(name.trim().toLowerCase());
517
- }
518
-
519
- /** Caller casing first, then the Pi find->glob rename (upstream glob->find). */
520
- function restoreStreamName(name: string, cloak: StreamCloakOptions): string {
521
- const cased = cloak.caseRestore?.[name.toLowerCase()] ?? name;
522
- return cloak.findGlob ? restoreToolNameForCaller(cased, cloak.findGlob) : cased;
523
- }
524
-
525
- /** Split a raw SSE block into its event name and data payload (if any). */
526
- function splitSseBlock(block: string): { event?: string; data?: string; hasData: boolean } {
527
- let eventName: string | undefined;
528
- const dataLines: string[] = [];
529
- let hasData = false;
530
- for (const line of block.split(/\r?\n/)) {
531
- if (line.startsWith("event:")) {
532
- eventName = line.slice(6).trim();
533
- } else if (line.startsWith("data:")) {
534
- hasData = true;
535
- dataLines.push(line.slice(5).trimStart());
536
- }
537
- }
538
- if (!hasData) return { event: eventName, hasData };
539
- return { event: eventName, data: dataLines.join("\n"), hasData };
540
- }
541
-
542
- /** Re-emit a rewritten event, preserving the original event/data framing. */
543
- function emitSseBlock(event: string | undefined, payload: string, hadEventPrefix: boolean): string {
544
- if (hadEventPrefix) return `event: ${event ?? ""}\ndata: ${payload}`;
545
- return `data: ${payload}`;
546
- }
547
-
548
-
549
- /**
550
- * Rewrite one Responses SSE block: drop injected function_call items/deltas,
551
- * restore surviving names, cloak completed/incomplete/failed response objects.
552
- * Returns null to drop the block, otherwise the (possibly rewritten) block.
553
- */
554
- function rewriteResponsesBlock(
555
- block: string,
556
- state: SseStreamCloakState,
557
- cloak: StreamCloakOptions,
558
- ): string | null {
559
- const { event, data, hasData } = splitSseBlock(block);
560
- if (!hasData || data === undefined || data === "[DONE]") return block;
561
- let parsed: Record<string, unknown>;
562
- try {
563
- const p: unknown = JSON.parse(data);
564
- if (!p || typeof p !== "object" || Array.isArray(p)) return block;
565
- parsed = p as Record<string, unknown>;
566
- } catch {
567
- return block;
568
- }
569
- const type = parsed.type;
570
- const hadEventPrefix = /^\s*event:/m.test(block);
571
-
572
- // New function_call item announced: drop when injected; tool-less callers
573
- // drop every function_call, never leaking calls downstream. Ids ride
574
- // verbatim; `output_item.done` carries the whole argument string, so that is
575
- // where schema repair applies (name-only deltas cannot be repaired yet).
576
- if (type === "response.output_item.added" || type === "response.output_item.done") {
577
- const item = parsed.item;
578
- if (item && typeof item === "object" && !Array.isArray(item)) {
579
- const rec = item as Record<string, unknown>;
580
- if (rec.type === "function_call" && typeof rec.name === "string") {
581
- if (isInjectedName(rec.name, cloak.injected) || !cloak.callerHadTools) {
582
- const itemId = rec.id ?? rec.call_id;
583
- if (typeof parsed.output_index === "number") state.responsesDroppedByIndex.set(parsed.output_index, true);
584
- if (typeof itemId === "string") state.responsesDroppedByItemId.set(itemId, true);
585
- return null;
586
- }
587
- const restored = restoreStreamName(rec.name, cloak);
588
- if (typeof parsed.output_index === "number") state.responsesNameByIndex.set(parsed.output_index, restored);
589
- if (type === "response.output_item.done") {
590
- rec.name = restored;
591
- const originalArgs = typeof rec.arguments === "string" ? rec.arguments : "";
592
- const repairedArgs = repairToolArguments(restored, originalArgs, cloak.argSchemas);
593
- if (repairedArgs !== originalArgs) state.audit?.repair("arguments-repaired", restored);
594
- rec.arguments = repairedArgs;
595
- return emitSseBlock(event, JSON.stringify(parsed), hadEventPrefix);
596
- }
597
- if (restored !== rec.name) {
598
- rec.name = restored;
599
- return emitSseBlock(event, JSON.stringify(parsed), hadEventPrefix);
600
- }
601
- }
602
- }
603
- return block;
604
- }
605
-
606
- // Argument deltas carry no name: earlier added/done decisions rule by index/id.
607
- // Tool-less callers drop every function_call_arguments delta outright. The
608
- // `.done` event carries the whole `arguments` string, so it is repairable.
609
- if (type === "response.function_call_arguments.delta" || type === "response.function_call_arguments.done") {
610
- if (!cloak.callerHadTools) return null;
611
- const droppedByIndex = typeof parsed.output_index === "number" && state.responsesDroppedByIndex.get(parsed.output_index) === true;
612
- const droppedById = typeof parsed.item_id === "string" && state.responsesDroppedByItemId.get(parsed.item_id) === true;
613
- if (droppedByIndex || droppedById) return null;
614
- if (type === "response.function_call_arguments.done" && typeof parsed.arguments === "string") {
615
- const name = typeof parsed.output_index === "number" ? state.responsesNameByIndex.get(parsed.output_index) : undefined;
616
- if (name !== undefined) {
617
- const repairedArgs = repairToolArguments(name, parsed.arguments, cloak.argSchemas);
618
- if (repairedArgs !== parsed.arguments) {
619
- state.audit?.repair("arguments-repaired", name);
620
- parsed.arguments = repairedArgs;
621
- return emitSseBlock(event, JSON.stringify(parsed), hadEventPrefix);
622
- }
623
- }
624
- }
625
- return block;
626
- }
627
-
628
- // Terminal/fallback objects may still embed injected calls: cloak in place.
629
- if (
630
- (type === "response.completed" || type === "response.incomplete" || type === "response.failed") &&
631
- parsed.response &&
632
- typeof parsed.response === "object" &&
633
- !Array.isArray(parsed.response)
634
- ) {
635
- const before = JSON.stringify((parsed.response as Record<string, unknown>).output);
636
- cloakStreamResponsesObject(parsed.response, cloak);
637
- const after = JSON.stringify((parsed.response as Record<string, unknown>).output);
638
- if (before !== after) return emitSseBlock(event, JSON.stringify(parsed), hadEventPrefix);
639
- // Names may have been restored without length change: compare whole object.
640
- const beforeAll = data;
641
- const rewritten = JSON.stringify(parsed);
642
- if (rewritten !== beforeAll) return emitSseBlock(event, rewritten, hadEventPrefix);
643
- return block;
644
- }
645
- return block;
646
- }
647
-
648
- /** Aggregate-cloak mirror for a Responses object: drop injected (all calls when tool-less), restore kept. */
649
- function cloakStreamResponsesObject(resp: unknown, cloak: StreamCloakOptions): void {
650
- if (!resp || typeof resp !== "object" || Array.isArray(resp)) return;
651
- const output = (resp as Record<string, unknown>).output;
652
- if (!Array.isArray(output)) return;
653
- const kept: unknown[] = [];
654
- for (const item of output) {
655
- if (item && typeof item === "object" && !Array.isArray(item)) {
656
- const rec = item as Record<string, unknown>;
657
- if (rec.type === "function_call") {
658
- if (isInjectedName(rec.name, cloak.injected)) continue;
659
- if (!cloak.callerHadTools) continue;
660
- if (typeof rec.name === "string") rec.name = restoreStreamName(rec.name, cloak);
661
- }
662
- }
663
- kept.push(item);
664
- }
665
- (resp as Record<string, unknown>).output = kept;
666
- }
667
-
668
- /**
669
- * Resolve ONE chat tool_calls record to its tracked call, registering the
670
- * call on first sight.
671
- *
672
- * Keying mirrors the aggregate cloak (sseToChatCompletionJson): a record with
673
- * a non-empty id is keyed `id:<id>`; an id-less record joins whatever call
674
- * currently OWNS its index (`chatIndexOwner`, which id-bearing deltas
675
- * rebind), else the legacy `idx:<index>` bucket — so id-less parallel calls
676
- * sharing `index: 0` still merge into one fragment stream, exactly as
677
- * before. Host index = the call's first observed index unless that upstream
678
- * index is ALREADY CLAIMED by another tracked call (`chatIndexClaims`), in
679
- * which case the next fresh sequential index is handed out in arrival order.
680
- * The claim deliberately counts dropped calls too: the ticket rule is
681
- * "claimed by ANOTHER tracked call", and a dropped placeholder has already
682
- * moved the claim bookkeeping even though it emits nothing.
683
- */
684
- function chatTrackedCall(
685
- state: SseStreamCloakState,
686
- rec: Record<string, unknown>,
687
- ): { call: ChatTrackedCall; key: string; idx: number } {
688
- const idx = typeof rec.index === "number" ? rec.index : 0;
689
- if (idx + 1 > state.chatNextIndex) state.chatNextIndex = idx + 1;
690
- const id = typeof rec.id === "string" && rec.id !== "" ? rec.id : undefined;
691
- let key: string;
692
- if (id !== undefined) {
693
- key = chatCallIdKey(id);
694
- // Id-bearing deltas (re)bind the index owner; later id-less fragments
695
- // follow, and a real call reusing an injected placeholder's index takes
696
- // the connection over instead of inheriting its drop verdict.
697
- state.chatIndexOwner.set(idx, key);
698
- } else {
699
- key = state.chatIndexOwner.get(idx) ?? chatIndexKey(idx);
700
- }
701
- const known = state.chatCalls.get(key);
702
- if (known !== undefined) return { call: known, key, idx };
703
- // First sight: assign the host-visible index. A claimed upstream index
704
- // forces a fresh sequential index above every index seen or assigned so
705
- // far; the fresh index is itself claimed so a later real call keeps clear.
706
- const claim = state.chatIndexClaims.get(idx);
707
- let call: ChatTrackedCall;
708
- if (claim === undefined) {
709
- state.chatIndexClaims.set(idx, key);
710
- call = { dropped: false, name: undefined, buffered: false, args: "", hostIndex: idx, flushed: false };
711
- } else {
712
- call = { dropped: false, name: undefined, buffered: false, args: "", hostIndex: state.chatNextIndex++, flushed: false };
713
- state.chatIndexClaims.set(call.hostIndex, key);
714
- // Audit: two tracked calls claimed one upstream index; the host index was
715
- // re-homed sequentially so the wire stays well-formed.
716
- state.audit?.repair("parallel-index-collision", `idx:${idx}`);
717
- }
718
- state.chatCalls.set(key, call);
719
- return { call, key, idx };
720
- }
721
-
722
- /**
723
- * Point one EMITTED chat tool_call delta at its call's host index. The
724
- * rewrite must only ever fire in index-collision cases, so byte-identity
725
- * holds for every single-call stream: an unchanged index is left untouched,
726
- * and a record with NO index field (host-defaulted 0) only gains one when
727
- * the host index is not 0.
728
- */
729
- function chatRewriteDeltaIndex(rec: Record<string, unknown>, call: ChatTrackedCall): boolean {
730
- if (typeof rec.index === "number") {
731
- if (rec.index === call.hostIndex) return false;
732
- rec.index = call.hostIndex;
733
- return true;
734
- }
735
- if (call.hostIndex === 0) return false;
736
- rec.index = call.hostIndex;
737
- return true;
738
- }
739
-
740
- /**
741
- * Terminal chunk for chat choice: absorb any tool-call argument fragment
742
- * riding the finish_reason chunk into its call's buffered prefix, then
743
- * re-emit every buffered call's whole argument packet, repaired, at ITS host
744
- * index. Returns true if delta was mutated.
745
- *
746
- * Flush-entry id policy: entries keep today's exact shape
747
- * {index, type:"function", function:{name,arguments}} WITHOUT the call id.
748
- * Hosts assemble streamed tool_calls by index, and every delta of the call —
749
- * including the id-bearing name-delta — is emitted at the same rewritten
750
- * host index, so the host has already bound the id; the stable shape also
751
- * keeps the single-call flush byte-identical.
752
- */
753
- function flushChatTerminalToolCalls(
754
- d: Record<string, unknown>,
755
- state: SseStreamCloakState,
756
- cloak: StreamCloakOptions,
757
- flushedKeys: Set<string>,
758
- flushedEntries: Set<Record<string, unknown>>,
759
- ): boolean {
760
- let rewrote = false;
761
-
762
- for (const tc of Array.isArray(d.tool_calls) ? d.tool_calls : []) {
763
- if (!tc || typeof tc !== "object" || Array.isArray(tc)) continue;
764
- const rec = tc as Record<string, unknown>;
765
- const { call } = chatTrackedCall(state, rec);
766
- const fn = rec.function;
767
- const fnRec = fn && typeof fn === "object" && !Array.isArray(fn) ? (fn as Record<string, unknown>) : null;
768
- if (call.buffered && !call.flushed && fnRec && typeof fnRec.arguments === "string") {
769
- call.args += fnRec.arguments;
770
- }
771
- }
772
-
773
- const flushed: unknown[] = [];
774
- for (const [key, call] of state.chatCalls) {
775
- if (!call.buffered || call.flushed) continue;
776
- const name = call.name;
777
- if (name === undefined) continue;
778
- call.flushed = true;
779
- call.buffered = false;
780
- const args = call.args;
781
- call.args = "";
782
- flushedKeys.add(key);
783
- const repairedArgs = repairToolArguments(name, args, cloak.argSchemas);
784
- if (repairedArgs !== args) state.audit?.repair("arguments-repaired", name);
785
- const entry = { index: call.hostIndex, type: "function", function: { name, arguments: repairedArgs } };
786
- flushedEntries.add(entry);
787
- flushed.push(entry);
788
- }
789
-
790
- if (flushed.length === 0 && !rewrote) return false;
791
-
792
- // Records riding the finish chunk whose call was just flushed are removed
793
- // (their fragments were absorbed above); surviving records keep flowing but
794
- // are first pointed at their call's host index.
795
- const remaining = (Array.isArray(d.tool_calls) ? d.tool_calls : []).filter((tc) => {
796
- if (!tc || typeof tc !== "object" || Array.isArray(tc)) return true;
797
- const rec = tc as Record<string, unknown>;
798
- const { call, key } = chatTrackedCall(state, rec);
799
- if (flushedKeys.has(key)) return false;
800
- if (chatRewriteDeltaIndex(rec, call)) rewrote = true;
801
- return true;
802
- });
803
- d.tool_calls = [...remaining, ...flushed];
804
- return true;
805
- }
806
-
807
- /**
808
- * Rewrite one Chat Completions SSE block: drop injected delta.tool_calls
809
- * (including nameless continuations via per-call tracking), restore surviving
810
- * names, and keep host-visible indices true: collisions on one upstream
811
- * index (free models label every parallel call index 0) are rewritten to
812
- * distinct sequential indices while the call stays one call.
813
- * Tool-less callers lose all tool_calls with arguments folded to content,
814
- * mirroring the aggregate cloak. Null drops the block.
815
- */
816
- function rewriteChatBlock(
817
- block: string,
818
- state: SseStreamCloakState,
819
- cloak: StreamCloakOptions,
820
- ): string | null {
821
- const { data, hasData } = splitSseBlock(block);
822
- if (!hasData || data === undefined || data === "[DONE]") return block;
823
- let parsed: Record<string, unknown>;
824
- try {
825
- const p: unknown = JSON.parse(data);
826
- if (!p || typeof p !== "object" || Array.isArray(p)) return block;
827
- parsed = p as Record<string, unknown>;
828
- } catch {
829
- return block;
830
- }
831
- if (!Array.isArray(parsed.choices)) return block;
832
- let changed = false;
833
- let allDeltasEmpty = true;
834
- for (const choice of parsed.choices) {
835
- if (!choice || typeof choice !== "object" || Array.isArray(choice)) continue;
836
- const c = choice as Record<string, unknown>;
837
- // Pre-assembled message riding a stream: cloak like the aggregate path.
838
- if (c.message && typeof c.message === "object" && !Array.isArray(c.message)) {
839
- cloakStreamChatMessage(c.message, cloak);
840
- changed = true;
841
- allDeltasEmpty = false;
842
- continue;
843
- }
844
- const delta = c.delta;
845
- if (!delta || typeof delta !== "object" || Array.isArray(delta)) {
846
- if (c.finish_reason || c.text || c.logprobs) allDeltasEmpty = false;
847
- continue;
848
- }
849
- const d = delta as Record<string, unknown>;
850
- if (cloak.normalizeReasoning && typeof d.reasoning === "string" && typeof d.reasoning_content !== "string") {
851
- d.reasoning_content = d.reasoning;
852
- changed = true;
853
- allDeltasEmpty = false;
854
- }
855
- // Terminal chunk for this choice: re-emit every buffered tool call's whole
856
- // argument packet, repaired, at its host index. The fragments never went
857
- // out, so this is the first and only time the client sees them. Runs
858
- // before the tool_calls branch because the terminal chunk usually carries
859
- // an empty delta. Call keys flushed on this block: their arguments are
860
- // final, so the per-delta loop below must not buffer them a second time.
861
- const flushedKeys = new Set<string>();
862
- const flushedEntries = new Set<Record<string, unknown>>();
863
- if (c.finish_reason && flushChatTerminalToolCalls(d, state, cloak, flushedKeys, flushedEntries)) {
864
- changed = true;
865
- }
866
- if (!Array.isArray(d.tool_calls)) {
867
- if (Object.keys(d).length > 0) allDeltasEmpty = false;
868
- continue;
869
- }
870
- const kept: unknown[] = [];
871
- let folded = "";
872
- for (const tc of d.tool_calls as unknown[]) {
873
- if (!tc || typeof tc !== "object" || Array.isArray(tc)) continue;
874
- // Flush entry rebuilt by the terminal flush above: pure passthrough.
875
- // These carry no id, so key resolution would misbind them to whichever
876
- // call currently owns the index — today's code skipped them the same way
877
- // via the index-keyed flushedIndices set.
878
- if (flushedEntries.has(tc as Record<string, unknown>)) {
879
- kept.push(tc);
880
- continue;
881
- }
882
- const rec = tc as Record<string, unknown>;
883
- const { call, key } = chatTrackedCall(state, rec);
884
- const fn = rec.function;
885
- const fnRec = fn && typeof fn === "object" && !Array.isArray(fn) ? (fn as Record<string, unknown>) : null;
886
- const name = fnRec && typeof fnRec.name === "string" && fnRec.name !== "" ? (fnRec.name as string) : "";
887
- if (!cloak.callerHadTools) {
888
- call.dropped = true;
889
- if (fnRec) {
890
- folded += (typeof fnRec.arguments === "string" && fnRec.arguments) || name;
891
- }
892
- changed = true;
893
- continue;
894
- }
895
- if (name !== "") {
896
- if (isInjectedName(name, cloak.injected)) {
897
- // The verdict is the CALL's, not the index's: a real id-bearing call
898
- // reusing this index rebinds the fragment owner (chatIndexOwner) and
899
- // inherits nothing.
900
- call.dropped = true;
901
- changed = true;
902
- continue;
903
- }
904
- const restored = restoreStreamName(name, cloak);
905
- // Fold this delta's name into the call's accumulated name: a name-only
906
- // delta is a fragment (append), a delta that ALSO carries arguments
907
- // restates the whole name (replace) — same rule as the aggregate cloak,
908
- // see accumulateToolCallName. When the restated name is the one the host
909
- // already holds, it is dropped from the wire instead of re-sent: the host
910
- // concatenates streamed name deltas, so a second copy would hand it
911
- // `get_weatherget_weather`.
912
- const previousName = call.name;
913
- const hasArgs = fnRec !== null && typeof fnRec.arguments === "string" && fnRec.arguments !== "";
914
- const toolName = accumulateToolCallName(previousName, restored, hasArgs);
915
- const nameRepeated = hasArgs && previousName !== undefined && toolName === previousName;
916
- call.name = toolName;
917
- if (nameRepeated && fnRec) {
918
- delete fnRec.name;
919
- changed = true;
920
- // Audit: the upstream restated a name the host already holds; the copy
921
- // was kept off the wire so the host does not concatenate it.
922
- state.audit?.repair("name-restated", toolName);
923
- } else if (restored !== name && fnRec) {
924
- fnRec.name = restored;
925
- changed = true;
926
- }
927
- call.dropped = false;
928
- if (isRepairableTool(toolName, cloak.argSchemas) && !call.flushed && !flushedKeys.has(key)) {
929
- // Hold this call's arguments: the packet is only judgeable once whole,
930
- // so it is re-emitted repaired on the terminal finish_reason chunk.
931
- // Reset-then-absorb mirrors the old per-index buffer exactly.
932
- if (!call.buffered) state.audit?.repair("arguments-buffered", toolName);
933
- call.buffered = true;
934
- call.args = "";
935
- if (fnRec && typeof fnRec.arguments === "string" && fnRec.arguments !== "") {
936
- call.args = fnRec.arguments;
937
- delete fnRec.arguments;
938
- changed = true;
939
- }
940
- if (nameRepeated && fnRec && Object.keys(fnRec).length === 0) {
941
- // Repeat header whose name and arguments both moved elsewhere: the
942
- // delta has nothing left to tell the host.
943
- continue;
944
- }
945
- kept.push(tc);
946
- if (chatRewriteDeltaIndex(rec, call)) changed = true;
947
- continue;
948
- }
949
- kept.push(tc);
950
- if (chatRewriteDeltaIndex(rec, call)) changed = true;
951
- continue;
952
- }
953
- // Nameless continuation (argument chunk): follows the CALL's verdict.
954
- if (call.dropped) {
955
- changed = true;
956
- continue;
957
- }
958
- if (call.buffered && fnRec && typeof fnRec.arguments === "string") {
959
- call.args += fnRec.arguments;
960
- delete fnRec.arguments;
961
- changed = true;
962
- continue;
963
- }
964
- kept.push(tc);
965
- if (chatRewriteDeltaIndex(rec, call)) changed = true;
966
- }
967
- if (kept.length > 0) {
968
- d.tool_calls = kept;
969
- allDeltasEmpty = false;
970
- } else {
971
- delete d.tool_calls;
972
- changed = true;
973
- if (folded !== "") {
974
- d.content = `${typeof d.content === "string" ? d.content : ""}${folded}`;
975
- }
976
- if (Object.keys(d).length > 0) allDeltasEmpty = false;
977
- }
978
- if (Object.keys(d).length === 0 && !c.finish_reason) {
979
- // Fully emptied delta: contributes nothing downstream.
980
- } else if (Object.keys(d).length > 0) {
981
- allDeltasEmpty = false;
982
- }
983
- }
984
- if (allDeltasEmpty && changed) return null;
985
- if (!changed) return block;
986
- const hadEventPrefix = /^\s*event:/m.test(block);
987
- const { event } = splitSseBlock(block);
988
- return emitSseBlock(event, JSON.stringify(parsed), hadEventPrefix);
989
- }
990
292
 
991
- /** Aggregate-cloak mirror for a Chat message: drop injected, restore kept. */
992
- function cloakStreamChatMessage(msg: unknown, cloak: StreamCloakOptions): void {
993
- if (!msg || typeof msg !== "object" || Array.isArray(msg)) return;
994
- const m = msg as Record<string, unknown>;
995
- if (!Array.isArray(m.tool_calls)) return;
996
- const calls = m.tool_calls as unknown[];
997
- if (!cloak.callerHadTools) {
998
- if ((!m.content || m.content === "") && calls.length > 0) {
999
- m.content = calls
1000
- .map((tc) => {
1001
- if (!tc || typeof tc !== "object" || Array.isArray(tc)) return "";
1002
- const fn = (tc as Record<string, unknown>).function;
1003
- if (!fn || typeof fn !== "object" || Array.isArray(fn)) return "";
1004
- const f = fn as Record<string, unknown>;
1005
- return (typeof f.arguments === "string" && f.arguments) || (typeof f.name === "string" && f.name) || "";
1006
- })
1007
- .join("\n");
1008
- }
1009
- delete m.tool_calls;
1010
- return;
1011
- }
1012
- const kept: unknown[] = [];
1013
- for (const tc of calls) {
1014
- let callName = "";
1015
- if (tc && typeof tc === "object" && !Array.isArray(tc)) {
1016
- const fn = (tc as Record<string, unknown>).function;
1017
- if (fn && typeof fn === "object" && !Array.isArray(fn) && typeof (fn as Record<string, unknown>).name === "string") {
1018
- callName = (fn as Record<string, unknown>).name as string;
1019
- }
1020
- }
1021
- if (callName && isInjectedName(callName, cloak.injected)) continue;
1022
- if (tc && typeof tc === "object" && !Array.isArray(tc)) {
1023
- const fn = (tc as Record<string, unknown>).function;
1024
- if (fn && typeof fn === "object" && !Array.isArray(fn) && typeof (fn as Record<string, unknown>).name === "string") {
1025
- (fn as Record<string, unknown>).name = restoreStreamName(
1026
- (fn as Record<string, unknown>).name as string,
1027
- cloak,
1028
- );
1029
- }
1030
- }
1031
- kept.push(tc);
1032
- }
1033
- if (kept.length > 0) m.tool_calls = kept;
1034
- else delete m.tool_calls;
1035
- }
1036
293
 
1037
- /**
1038
- * Rewrite one Anthropic Messages SSE block: drop injected tool_use blocks
1039
- * (including input_json continuations via per-index tracking), restore
1040
- * surviving names. Tool-less callers drop every tool_use, never leaking
1041
- * calls downstream. Null drops the block.
1042
- */
1043
- function rewriteMessagesBlock(
1044
- block: string,
1045
- state: SseStreamCloakState,
1046
- cloak: StreamCloakOptions,
1047
- ): string | null {
1048
- const { event, data, hasData } = splitSseBlock(block);
1049
- if (!hasData || data === undefined || data === "[DONE]") return block;
1050
- let parsed: Record<string, unknown>;
1051
- try {
1052
- const p: unknown = JSON.parse(data);
1053
- if (!p || typeof p !== "object" || Array.isArray(p)) return block;
1054
- parsed = p as Record<string, unknown>;
1055
- } catch {
1056
- return block;
1057
- }
1058
- const type = typeof parsed.type === "string" ? parsed.type : "";
1059
- const hadEventPrefix = /^\s*event:/m.test(block);
1060
- const index = typeof parsed.index === "number" ? parsed.index : 0;
1061
- // New content block announced: drop injected tool_use, else restore name.
1062
- if (type === "content_block_start") {
1063
- const cb = parsed.content_block;
1064
- if (cb && typeof cb === "object" && !Array.isArray(cb) && (cb as Record<string, unknown>).type === "tool_use") {
1065
- const rec = cb as Record<string, unknown>;
1066
- if (typeof rec.name === "string" && (isInjectedName(rec.name, cloak.injected) || !cloak.callerHadTools)) {
1067
- state.messagesDroppedByIndex.set(index, true);
1068
- return null;
1069
- }
1070
- if (typeof rec.name === "string") {
1071
- const restored = restoreStreamName(rec.name, cloak);
1072
- rec.name = restored;
1073
- state.messagesNameByIndex.set(index, restored);
1074
- if (isRepairableTool(restored, cloak.argSchemas)) {
1075
- if (!state.messagesArgsByIndex.has(index)) state.audit?.repair("arguments-buffered", restored);
1076
- state.messagesArgsByIndex.set(index, typeof rec.input === "string" ? rec.input : "");
1077
- }
1078
- return emitSseBlock(event, JSON.stringify(parsed), hadEventPrefix);
1079
- }
1080
- }
1081
- return block;
1082
- }
1083
- // Input deltas carry no name: the start verdict rules by index. Repairable
1084
- // tools buffer their fragments, re-emitted whole on content_block_stop.
1085
- if (type === "content_block_delta") {
1086
- if (state.messagesDroppedByIndex.get(index) === true) return null;
1087
- const delta = parsed.delta as Record<string, unknown> | undefined;
1088
- if (state.messagesArgsByIndex.has(index) && delta && typeof delta.partial_json === "string") {
1089
- state.messagesArgsByIndex.set(index, (state.messagesArgsByIndex.get(index) ?? "") + delta.partial_json);
1090
- return null;
1091
- }
1092
- return block;
1093
- }
1094
- // Block close for a dropped tool_use carries no name: drop it too.
1095
- if (type === "content_block_stop") {
1096
- if (state.messagesDroppedByIndex.get(index) === true) {
1097
- state.messagesDroppedByIndex.delete(index);
1098
- return null;
1099
- }
1100
- const buffered = state.messagesArgsByIndex.get(index);
1101
- if (buffered === undefined) return block;
1102
- const name = state.messagesNameByIndex.get(index) ?? "";
1103
- state.messagesArgsByIndex.delete(index);
1104
- state.messagesNameByIndex.delete(index);
1105
- const repaired = repairToolArguments(name, buffered, cloak.argSchemas);
1106
- if (repaired !== buffered) state.audit?.repair("arguments-repaired", name);
1107
- const flushed: Record<string, unknown> = {
1108
- type: "content_block_delta",
1109
- index,
1110
- delta: { type: "input_json_delta", partial_json: repaired },
1111
- };
1112
- const stopBlock = emitSseBlock(event, JSON.stringify(parsed), hadEventPrefix);
1113
- return `${emitSseBlock("content_block_delta", JSON.stringify(flushed), true)}\n\n${stopBlock}`;
1114
- }
1115
- // A `message` frame carries a whole finished message mid-stream. It needs the
1116
- // same treatment the aggregate path gives it — injected tool_use dropped,
1117
- // caller names restored, inputs repaired — otherwise a placeholder call leaks
1118
- // downstream and a malformed one reaches the host unrepaired. Any block still
1119
- // buffering arguments is completed first so nothing is orphaned.
1120
- if (type === "message") {
1121
- const flushed = flushBufferedToolArgs(state, cloak);
1122
- cloakMessagesContent(parsed.message ?? parsed, cloak.callerHadTools, cloak.caseRestore, cloak.findGlob, cloak.injected, cloak.argSchemas);
1123
- if (flushed === null) return emitSseBlock(event, JSON.stringify(parsed), hadEventPrefix);
1124
- return `${flushed}${emitSseBlock(event, JSON.stringify(parsed), hadEventPrefix)}`;
1125
- }
1126
- // A tool_use block still holding buffered arguments is completed HERE, while
1127
- // its own event order is intact: `message_delta` / `message_stop` close the
1128
- // message, so the recovered `input_json_delta` has to precede them. (An
1129
- // end-of-stream flush would emit it after `message_stop`, where a strict
1130
- // Anthropic client has already closed the message.)
1131
- if (type === "message_delta" || type === "message_stop") {
1132
- const flushed = flushBufferedToolArgs(state, cloak);
1133
- if (flushed === null) return block;
1134
- return `${flushed}${emitSseBlock(event, JSON.stringify(parsed), hadEventPrefix)}`;
1135
- }
1136
- return block;
1137
- }
1138
294
 
1139
- /**
1140
- * Rewrite one raw SSE block for the streaming cloak. Returns null to drop
1141
- * the block, otherwise the block to forward (identical string when no change,
1142
- * so full-inventory streams stay byte-identical).
1143
- */
1144
- export function rewriteSseBlock(
1145
- block: string,
1146
- state: SseStreamCloakState,
1147
- cloak: StreamCloakOptions,
1148
- ): string | null {
1149
- if (block.trim() === "") return block;
1150
- if (cloak.pathname.endsWith("/responses")) return rewriteResponsesBlock(block, state, cloak);
1151
- if (cloak.pathname.endsWith("/messages")) return rewriteMessagesBlock(block, state, cloak);
1152
- return rewriteChatBlock(block, state, cloak);
1153
- }
1154
295
  export type MidStreamRefireReason = "truncation" | "model" | "rate_limit";
1155
296
  /**
1156
297
  * Options for pipeUpstreamStream.