billion-context 0.1.98-pr.674.491 → 0.1.98
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/index.js +32 -215
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -51498,44 +51498,20 @@ ${body}`;
|
|
|
51498
51498
|
}
|
|
51499
51499
|
|
|
51500
51500
|
// src/loop/tag-echo-filter.ts
|
|
51501
|
-
|
|
51502
|
-
|
|
51503
|
-
|
|
51504
|
-
|
|
51505
|
-
|
|
51506
|
-
|
|
51507
|
-
|
|
51508
|
-
}
|
|
51509
|
-
return [...names].sort((a, b2) => b2.length - a.length).join("|");
|
|
51510
|
-
}
|
|
51511
|
-
var ACP_NAME_ALT = `(?:${buildAcplikeName()})`;
|
|
51512
|
-
var NAME = ACP_NAME_ALT;
|
|
51513
|
-
var PAIRED = new RegExp("<" + NAME + "\\s[^<>]{0,256}>([^<>]{0,64})<\\/" + NAME + ">");
|
|
51514
|
-
var LONE_OPEN = new RegExp("<" + NAME + "(?:\\s[^<>]{0,256})?>");
|
|
51515
|
-
var LONE_CLOSE = new RegExp("<\\/" + NAME + "(?=[\\s>])[^<>]{0,32}>");
|
|
51516
|
-
var PARTIAL_TAIL = new RegExp("(<" + NAME + "\\s[^<>]*|<\\/" + NAME + "(?:\\s[^<>]{0,32})?|<\\/?[acip]*)$");
|
|
51517
|
-
var TRUNC_OPEN = new RegExp("<" + NAME + "\\s[^<>]*$");
|
|
51518
|
-
var TRUNC_CLOSE = new RegExp("<\\/" + NAME + "(?:\\s[^<>]{0,32})?$");
|
|
51519
|
-
var DEFINITE_TAIL = new RegExp("^<" + NAME + "\\s|^<\\/" + NAME);
|
|
51520
|
-
var OPEN_WITH_ATTRS = new RegExp("^<" + NAME + "\\s");
|
|
51521
|
-
var CLOSE_HEAD = "</";
|
|
51522
|
-
var CLOSE_NAME_ANCHORED = new RegExp("^" + NAME);
|
|
51501
|
+
var PAIRED = /\x3cacp\s[^<>]{0,256}>([^<>]{0,64})<\/acp>/;
|
|
51502
|
+
var LONE_OPEN = /\x3cacp(?:\s[^<>]{0,256})?>/;
|
|
51503
|
+
var LONE_CLOSE = /<\/acp(?=[\s>])[^<>]{0,32}>/;
|
|
51504
|
+
var PARTIAL_TAIL = /(\x3cacp\s[^<>]*|\x3c\/acp(?:\s[^<>]{0,32})?|<\/?a?c?p?)$/;
|
|
51505
|
+
var TRUNC_OPEN = /\x3cacp\s[^<>]*$/;
|
|
51506
|
+
var TRUNC_CLOSE = /\x3c\/acp(?:\s[^<>]{0,32})?$/;
|
|
51507
|
+
var CLOSE_TAG = "</acp";
|
|
51523
51508
|
var HOLD_LIMIT = 128;
|
|
51524
51509
|
var TAG_OPEN_CAP = 4096;
|
|
51525
51510
|
var SWALLOW_CAP = 80;
|
|
51526
|
-
function looseCloseEnd(s3) {
|
|
51527
|
-
let idx = s3.indexOf(CLOSE_HEAD);
|
|
51528
|
-
while (idx >= 0) {
|
|
51529
|
-
const m2 = CLOSE_NAME_ANCHORED.exec(s3.slice(idx + 2));
|
|
51530
|
-
if (m2 && s3[idx + 2 + m2[0].length] === ">") return idx + 2 + m2[0].length + 1;
|
|
51531
|
-
idx = s3.indexOf(CLOSE_HEAD, idx + 1);
|
|
51532
|
-
}
|
|
51533
|
-
return -1;
|
|
51534
|
-
}
|
|
51535
51511
|
function stripAcpTags(text) {
|
|
51536
51512
|
return text.replace(new RegExp(PAIRED.source, "g"), "").replace(new RegExp(LONE_OPEN.source, "g"), "").replace(new RegExp(LONE_CLOSE.source, "g"), "").replace(new RegExp(TRUNC_OPEN.source), "").replace(new RegExp(TRUNC_CLOSE.source), "");
|
|
51537
51513
|
}
|
|
51538
|
-
var RENDER_TAG_DETECT =
|
|
51514
|
+
var RENDER_TAG_DETECT = /\x3c\/?acp(?=[\s>])|\\u003c\/?acp(?=[\s>\\])/;
|
|
51539
51515
|
function containsRenderTagText(s3) {
|
|
51540
51516
|
return RENDER_TAG_DETECT.test(s3);
|
|
51541
51517
|
}
|
|
@@ -51552,8 +51528,6 @@ function createTagEchoFilter(onDrop) {
|
|
|
51552
51528
|
let swallowed = "";
|
|
51553
51529
|
let droppedAny = false;
|
|
51554
51530
|
let notified = false;
|
|
51555
|
-
let inputChars = 0;
|
|
51556
|
-
let outputChars = 0;
|
|
51557
51531
|
const drop = (snippet) => {
|
|
51558
51532
|
droppedAny = true;
|
|
51559
51533
|
if (onDrop && !notified) {
|
|
@@ -51567,9 +51541,10 @@ function createTagEchoFilter(onDrop) {
|
|
|
51567
51541
|
for (; ; ) {
|
|
51568
51542
|
if (swallowUntilClose) {
|
|
51569
51543
|
const combined = swallowed + buf;
|
|
51570
|
-
const
|
|
51571
|
-
if (
|
|
51572
|
-
|
|
51544
|
+
const closeIdx = combined.indexOf(CLOSE_TAG);
|
|
51545
|
+
if (closeIdx >= 0 && combined[closeIdx + CLOSE_TAG.length] === ">") {
|
|
51546
|
+
const end = closeIdx + CLOSE_TAG.length + 1;
|
|
51547
|
+
drop(swallowed + combined.slice(0, end));
|
|
51573
51548
|
swallowed = "";
|
|
51574
51549
|
swallowUntilClose = false;
|
|
51575
51550
|
buf = combined.slice(end);
|
|
@@ -51594,7 +51569,7 @@ function createTagEchoFilter(onDrop) {
|
|
|
51594
51569
|
if (!m2) {
|
|
51595
51570
|
const t = PARTIAL_TAIL.exec(buf);
|
|
51596
51571
|
if (t) {
|
|
51597
|
-
const definite =
|
|
51572
|
+
const definite = /^\x3cacp\s/.test(t[0]) || /^\x3c\/acp/.test(t[0]);
|
|
51598
51573
|
const cap = definite ? TAG_OPEN_CAP : HOLD_LIMIT;
|
|
51599
51574
|
if (t[0].length <= cap) {
|
|
51600
51575
|
held = t[0];
|
|
@@ -51613,7 +51588,7 @@ function createTagEchoFilter(onDrop) {
|
|
|
51613
51588
|
drop(m2[0]);
|
|
51614
51589
|
out += buf.slice(0, m2.index);
|
|
51615
51590
|
buf = buf.slice(m2.index + m2[0].length);
|
|
51616
|
-
if (m2 === o &&
|
|
51591
|
+
if (m2 === o && /^\x3cacp\s/.test(m2[0])) {
|
|
51617
51592
|
swallowUntilClose = true;
|
|
51618
51593
|
swallowed = "";
|
|
51619
51594
|
}
|
|
@@ -51622,12 +51597,9 @@ function createTagEchoFilter(onDrop) {
|
|
|
51622
51597
|
};
|
|
51623
51598
|
return {
|
|
51624
51599
|
push(delta) {
|
|
51625
|
-
inputChars += delta.length;
|
|
51626
51600
|
const chunk = held + delta;
|
|
51627
51601
|
held = "";
|
|
51628
|
-
|
|
51629
|
-
outputChars += r.length;
|
|
51630
|
-
return r;
|
|
51602
|
+
return process2(chunk);
|
|
51631
51603
|
},
|
|
51632
51604
|
flush() {
|
|
51633
51605
|
const rest = swallowed + held;
|
|
@@ -51635,36 +51607,27 @@ function createTagEchoFilter(onDrop) {
|
|
|
51635
51607
|
swallowed = "";
|
|
51636
51608
|
held = "";
|
|
51637
51609
|
swallowUntilClose = false;
|
|
51638
|
-
let result;
|
|
51639
51610
|
if (wasSwallowing) {
|
|
51640
51611
|
if (rest.length > 0) drop(rest);
|
|
51641
|
-
|
|
51642
|
-
} else {
|
|
51643
|
-
const t = new RegExp(TRUNC_OPEN.source).exec(rest);
|
|
51644
|
-
if (t) {
|
|
51645
|
-
drop(t[0]);
|
|
51646
|
-
result = rest.slice(0, t.index);
|
|
51647
|
-
} else {
|
|
51648
|
-
const tc = new RegExp(TRUNC_CLOSE.source).exec(rest);
|
|
51649
|
-
if (tc) {
|
|
51650
|
-
drop(tc[0]);
|
|
51651
|
-
result = rest.slice(0, tc.index);
|
|
51652
|
-
} else {
|
|
51653
|
-
result = rest;
|
|
51654
|
-
}
|
|
51655
|
-
}
|
|
51612
|
+
return "";
|
|
51656
51613
|
}
|
|
51657
|
-
|
|
51658
|
-
|
|
51614
|
+
const t = new RegExp(TRUNC_OPEN.source).exec(rest);
|
|
51615
|
+
if (t) {
|
|
51616
|
+
drop(t[0]);
|
|
51617
|
+
return rest.slice(0, t.index);
|
|
51618
|
+
}
|
|
51619
|
+
const tc = new RegExp(TRUNC_CLOSE.source).exec(rest);
|
|
51620
|
+
if (tc) {
|
|
51621
|
+
drop(tc[0]);
|
|
51622
|
+
return rest.slice(0, tc.index);
|
|
51623
|
+
}
|
|
51624
|
+
return rest;
|
|
51659
51625
|
},
|
|
51660
51626
|
dropped() {
|
|
51661
51627
|
return droppedAny;
|
|
51662
51628
|
},
|
|
51663
51629
|
pending() {
|
|
51664
51630
|
return held.length > 0 || swallowUntilClose;
|
|
51665
|
-
},
|
|
51666
|
-
stats() {
|
|
51667
|
-
return { inputChars, outputChars, dropped: droppedAny };
|
|
51668
51631
|
}
|
|
51669
51632
|
};
|
|
51670
51633
|
}
|
|
@@ -53697,18 +53660,6 @@ async function* runCompressLoop(upstream, ctx, requestBody, requestOptions, adap
|
|
|
53697
53660
|
}
|
|
53698
53661
|
}
|
|
53699
53662
|
|
|
53700
|
-
// src/degenerate-turn.ts
|
|
53701
|
-
function degenerateTurnWarning(o) {
|
|
53702
|
-
if ((o.reason ?? o.terminalReason) !== o.terminalReason) return null;
|
|
53703
|
-
if (o.toolCalls > 0) return null;
|
|
53704
|
-
if (o.text.outputChars > 0) return null;
|
|
53705
|
-
const bits = [];
|
|
53706
|
-
if (o.sawThinking) bits.push("thinking present");
|
|
53707
|
-
if (o.text.dropped && o.text.inputChars > 0) bits.push(`${o.text.inputChars} chars of emitted text stripped as render-tag echo`);
|
|
53708
|
-
else bits.push("no visible text emitted");
|
|
53709
|
-
return `[degenerate-turn] ${o.wire}: turn ended ${o.terminalReason} with zero visible text and zero tool calls (${bits.join("; ")}) \u2014 the agent receives an empty turn and may stall until nudged (#673)`;
|
|
53710
|
-
}
|
|
53711
|
-
|
|
53712
53663
|
// src/loop/adapter-responses.ts
|
|
53713
53664
|
var RESPONSES_ITEM_ID_MAX = 64;
|
|
53714
53665
|
function normalizeResponsesMessageItems(input) {
|
|
@@ -53772,7 +53723,7 @@ function dropWhitespaceResponsesMessages(input) {
|
|
|
53772
53723
|
}
|
|
53773
53724
|
return dropped;
|
|
53774
53725
|
}
|
|
53775
|
-
var RENDER_TAG_RE =
|
|
53726
|
+
var RENDER_TAG_RE = /\x3cacp\s[^>]*\x3e[^<]*\x3c\/acp\x3e|\x3cacp\s[^>]*\/\x3e/g;
|
|
53776
53727
|
function stripRenderTags(text) {
|
|
53777
53728
|
return text.replace(RENDER_TAG_RE, "");
|
|
53778
53729
|
}
|
|
@@ -53987,17 +53938,6 @@ data: ${JSON.stringify({ type: "response.created", response: { id: createdRespId
|
|
|
53987
53938
|
yield { kind: "text", delta: tail, ...raw ? { raw } : {} };
|
|
53988
53939
|
}
|
|
53989
53940
|
};
|
|
53990
|
-
let sawReasoning = false;
|
|
53991
|
-
let toolCallsEmitted = 0;
|
|
53992
|
-
let degenerateWarned = false;
|
|
53993
|
-
const maybeWarnDegenerate = (reason) => {
|
|
53994
|
-
if (degenerateWarned) return;
|
|
53995
|
-
const msg = degenerateTurnWarning({ reason, terminalReason: "completed", toolCalls: toolCallsEmitted, text: tagFilter.stats(), sawThinking: sawReasoning, wire: "responses" });
|
|
53996
|
-
if (msg) {
|
|
53997
|
-
degenerateWarned = true;
|
|
53998
|
-
log("warn", msg);
|
|
53999
|
-
}
|
|
54000
|
-
};
|
|
54001
53941
|
for await (const eventStr of iterSseEvents(upstream)) {
|
|
54002
53942
|
const explicitType = extractEventType(eventStr);
|
|
54003
53943
|
const dataLine = extractDataLine(eventStr);
|
|
@@ -54010,7 +53950,6 @@ data: ${JSON.stringify({ type: "response.created", response: { id: createdRespId
|
|
|
54010
53950
|
}
|
|
54011
53951
|
const type = explicitType ?? (typeof obj.type === "string" ? obj.type : null);
|
|
54012
53952
|
if (!type) continue;
|
|
54013
|
-
if (type.startsWith("response.reasoning")) sawReasoning = true;
|
|
54014
53953
|
const rawBuf = Buffer.from(eventStr + "\n\n", "utf8");
|
|
54015
53954
|
if (round === 1 && typeof obj.output_index === "number") {
|
|
54016
53955
|
outputIndex = Math.max(outputIndex, obj.output_index + 1);
|
|
@@ -54094,7 +54033,6 @@ data: ${JSON.stringify({ type: "response.created", response: { id: createdRespId
|
|
|
54094
54033
|
if (fc) {
|
|
54095
54034
|
if (typeof item.arguments === "string" && item.arguments) fc.arguments = item.arguments;
|
|
54096
54035
|
pending.delete(itemId);
|
|
54097
|
-
toolCallsEmitted++;
|
|
54098
54036
|
yield {
|
|
54099
54037
|
kind: "tool_call",
|
|
54100
54038
|
name: fc.name,
|
|
@@ -54103,7 +54041,6 @@ data: ${JSON.stringify({ type: "response.created", response: { id: createdRespId
|
|
|
54103
54041
|
};
|
|
54104
54042
|
} else {
|
|
54105
54043
|
yield { kind: "meta", chunk: rawBuf, firstRoundOnly: false };
|
|
54106
|
-
toolCallsEmitted++;
|
|
54107
54044
|
yield {
|
|
54108
54045
|
kind: "tool_call",
|
|
54109
54046
|
name: typeof item.name === "string" ? item.name : "",
|
|
@@ -54140,7 +54077,6 @@ data: ${JSON.stringify({ type: "response.created", response: { id: createdRespId
|
|
|
54140
54077
|
outputTokens: typeof respUsage?.output_tokens === "number" ? respUsage.output_tokens : void 0,
|
|
54141
54078
|
cachedTokens: typeof pd?.cached_tokens === "number" ? pd.cached_tokens : void 0
|
|
54142
54079
|
};
|
|
54143
|
-
maybeWarnDegenerate("completed");
|
|
54144
54080
|
yield { kind: "done", finishReason: "completed" };
|
|
54145
54081
|
} else if (type === "response.incomplete") {
|
|
54146
54082
|
yield* flushFilter();
|
|
@@ -54458,23 +54394,11 @@ function createOpenaiAdapter(requestBody, clientSystem, hostCredit = 0, absorbNa
|
|
|
54458
54394
|
yield { kind: "text", delta: tail, raw: buildContent(tail) };
|
|
54459
54395
|
}
|
|
54460
54396
|
};
|
|
54461
|
-
let sawReasoning = false;
|
|
54462
|
-
let toolCallsEmitted = 0;
|
|
54463
|
-
let degenerateWarned = false;
|
|
54464
|
-
const maybeWarnDegenerate = (reason) => {
|
|
54465
|
-
if (degenerateWarned) return;
|
|
54466
|
-
const msg = degenerateTurnWarning({ reason, terminalReason: "stop", toolCalls: toolCallsEmitted, text: tagFilter.stats(), sawThinking: sawReasoning, wire: "openai" });
|
|
54467
|
-
if (msg) {
|
|
54468
|
-
degenerateWarned = true;
|
|
54469
|
-
log("warn", msg);
|
|
54470
|
-
}
|
|
54471
|
-
};
|
|
54472
54397
|
const rawToolChunks = [];
|
|
54473
54398
|
let sawRealToolCall = false;
|
|
54474
54399
|
const flushPendingAsStructured = function* () {
|
|
54475
54400
|
for (const [, tc] of pending) {
|
|
54476
54401
|
if (tc.name.length > 0 || tc.id.length > 0) {
|
|
54477
|
-
toolCallsEmitted++;
|
|
54478
54402
|
yield {
|
|
54479
54403
|
kind: "tool_call",
|
|
54480
54404
|
name: tc.name,
|
|
@@ -54497,7 +54421,6 @@ function createOpenaiAdapter(requestBody, clientSystem, hostCredit = 0, absorbNa
|
|
|
54497
54421
|
}
|
|
54498
54422
|
for (const [idx, tc] of pending) {
|
|
54499
54423
|
if (realIndexes.has(idx) && (tc.name.length > 0 || tc.id.length > 0)) {
|
|
54500
|
-
toolCallsEmitted++;
|
|
54501
54424
|
yield {
|
|
54502
54425
|
kind: "tool_call",
|
|
54503
54426
|
name: tc.name,
|
|
@@ -54517,7 +54440,6 @@ function createOpenaiAdapter(requestBody, clientSystem, hostCredit = 0, absorbNa
|
|
|
54517
54440
|
}
|
|
54518
54441
|
for (const [idx, tc] of pending) {
|
|
54519
54442
|
if (!realIndexes.has(idx) && tc.name.length > 0) {
|
|
54520
|
-
toolCallsEmitted++;
|
|
54521
54443
|
yield {
|
|
54522
54444
|
kind: "tool_call",
|
|
54523
54445
|
name: tc.name,
|
|
@@ -54542,7 +54464,6 @@ function createOpenaiAdapter(requestBody, clientSystem, hostCredit = 0, absorbNa
|
|
|
54542
54464
|
if (sawRealToolCall) {
|
|
54543
54465
|
yield { kind: "meta", chunk: Buffer.from(eventStr + "\n\n", "utf8") };
|
|
54544
54466
|
}
|
|
54545
|
-
maybeWarnDegenerate("stop");
|
|
54546
54467
|
yield { kind: "done", finishReason: "stop", ...sawRealToolCall ? { suppressCompletion: true } : {} };
|
|
54547
54468
|
continue;
|
|
54548
54469
|
}
|
|
@@ -54588,11 +54509,9 @@ function createOpenaiAdapter(requestBody, clientSystem, hostCredit = 0, absorbNa
|
|
|
54588
54509
|
if (sawRealToolCall) {
|
|
54589
54510
|
const chunk = patchUsageChunk(eventStr, parsed, u2 ?? {}, hostCredit);
|
|
54590
54511
|
yield { kind: "meta", chunk };
|
|
54591
|
-
maybeWarnDegenerate(finishReason);
|
|
54592
54512
|
yield { kind: "done", finishReason, suppressCompletion: true };
|
|
54593
54513
|
continue;
|
|
54594
54514
|
} else {
|
|
54595
|
-
maybeWarnDegenerate(finishReason);
|
|
54596
54515
|
yield {
|
|
54597
54516
|
kind: "done",
|
|
54598
54517
|
finishReason: hadToolCalls && finishReason === "stop" ? "tool_calls" : finishReason
|
|
@@ -54601,7 +54520,6 @@ function createOpenaiAdapter(requestBody, clientSystem, hostCredit = 0, absorbNa
|
|
|
54601
54520
|
}
|
|
54602
54521
|
if (!delta) continue;
|
|
54603
54522
|
if (typeof delta.reasoning_content === "string" && delta.reasoning_content.length > 0) {
|
|
54604
|
-
sawReasoning = true;
|
|
54605
54523
|
yield { kind: "reasoning", delta: delta.reasoning_content, raw: finishReason ? stripFinishReasonChunk(rawBuf) : rawBuf };
|
|
54606
54524
|
}
|
|
54607
54525
|
if (delta.tool_calls) {
|
|
@@ -54879,17 +54797,6 @@ ${systemPrompt}` : systemPrompt;
|
|
|
54879
54797
|
log("warn", `[tag-echo] stripped model-emitted render tag: ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
|
|
54880
54798
|
});
|
|
54881
54799
|
let lastTextIndex = null;
|
|
54882
|
-
let sawThinking = false;
|
|
54883
|
-
let toolCallsEmitted = 0;
|
|
54884
|
-
let degenerateWarned = false;
|
|
54885
|
-
const maybeWarnDegenerate = (reason) => {
|
|
54886
|
-
if (degenerateWarned) return;
|
|
54887
|
-
const msg = degenerateTurnWarning({ reason, terminalReason: "end_turn", toolCalls: toolCallsEmitted, text: tagFilter.stats(), sawThinking, wire: "anthropic" });
|
|
54888
|
-
if (msg) {
|
|
54889
|
-
degenerateWarned = true;
|
|
54890
|
-
log("warn", msg);
|
|
54891
|
-
}
|
|
54892
|
-
};
|
|
54893
54800
|
for await (const eventStr of iterSseEvents2(upstream)) {
|
|
54894
54801
|
const parsed = parseAnthropicSse(eventStr);
|
|
54895
54802
|
if (!parsed) continue;
|
|
@@ -54926,10 +54833,7 @@ ${systemPrompt}` : systemPrompt;
|
|
|
54926
54833
|
const id = typeof block.id === "string" ? block.id : `toolu_${upstreamIndex}`;
|
|
54927
54834
|
pending.set(upstreamIndex, { id, name, json: "" });
|
|
54928
54835
|
} else {
|
|
54929
|
-
if (block.type === "thinking" || block.type === "redacted_thinking")
|
|
54930
|
-
thinkingIndexes.add(upstreamIndex);
|
|
54931
|
-
sawThinking = true;
|
|
54932
|
-
}
|
|
54836
|
+
if (block.type === "thinking" || block.type === "redacted_thinking") thinkingIndexes.add(upstreamIndex);
|
|
54933
54837
|
const ci2 = clientIndex++;
|
|
54934
54838
|
indexMap.set(upstreamIndex, ci2);
|
|
54935
54839
|
openBlocks.push(ci2);
|
|
@@ -54974,7 +54878,6 @@ ${systemPrompt}` : systemPrompt;
|
|
|
54974
54878
|
const tb = pending.get(upstreamIndex);
|
|
54975
54879
|
if (tb) {
|
|
54976
54880
|
pending.delete(upstreamIndex);
|
|
54977
|
-
toolCallsEmitted++;
|
|
54978
54881
|
yield {
|
|
54979
54882
|
kind: "tool_call",
|
|
54980
54883
|
name: tb.name,
|
|
@@ -55021,7 +54924,6 @@ ${systemPrompt}` : systemPrompt;
|
|
|
55021
54924
|
cachedTokens: roundCached
|
|
55022
54925
|
};
|
|
55023
54926
|
}
|
|
55024
|
-
maybeWarnDegenerate(stopReason);
|
|
55025
54927
|
yield { kind: "done", finishReason: stopReason };
|
|
55026
54928
|
} else if (type === "message_stop") {
|
|
55027
54929
|
if (lastTextIndex !== null) {
|
|
@@ -55040,7 +54942,6 @@ ${systemPrompt}` : systemPrompt;
|
|
|
55040
54942
|
cachedTokens: roundCached
|
|
55041
54943
|
};
|
|
55042
54944
|
}
|
|
55043
|
-
maybeWarnDegenerate(stopReason);
|
|
55044
54945
|
yield { kind: "done", finishReason: stopReason ?? "end_turn" };
|
|
55045
54946
|
} else if (round === 1) {
|
|
55046
54947
|
yield { kind: "meta", chunk: rawBuf, firstRoundOnly: true };
|
|
@@ -56651,18 +56552,11 @@ data: ${JSON.stringify({ type: "content_block_delta", index, delta: { type: delt
|
|
|
56651
56552
|
|
|
56652
56553
|
`;
|
|
56653
56554
|
};
|
|
56654
|
-
let sawToolUse = false;
|
|
56655
|
-
let sawThinking = false;
|
|
56656
|
-
let visibleTextChars = 0;
|
|
56657
|
-
let finalFinishReason;
|
|
56658
56555
|
const flushTails = () => {
|
|
56659
56556
|
let out = "";
|
|
56660
56557
|
for (const s3 of streams.values()) {
|
|
56661
56558
|
const tail = s3.filter.flush();
|
|
56662
|
-
if (tail.length > 0)
|
|
56663
|
-
out += syntheticTail(s3.field, s3.index, tail);
|
|
56664
|
-
if (s3.field === "content" || s3.field === "text") visibleTextChars += tail.length;
|
|
56665
|
-
}
|
|
56559
|
+
if (tail.length > 0) out += syntheticTail(s3.field, s3.index, tail);
|
|
56666
56560
|
}
|
|
56667
56561
|
return out;
|
|
56668
56562
|
};
|
|
@@ -56685,28 +56579,6 @@ data: ${JSON.stringify({ type: "content_block_delta", index, delta: { type: delt
|
|
|
56685
56579
|
reason: "plugin chat passthrough stream ended without a completion event"
|
|
56686
56580
|
});
|
|
56687
56581
|
};
|
|
56688
|
-
const maybeWarnDegenerate = () => {
|
|
56689
|
-
if (!sawTerminal || res.destroyed || res.writableEnded) return;
|
|
56690
|
-
let inputChars = 0;
|
|
56691
|
-
let dropped = false;
|
|
56692
|
-
for (const s3 of streams.values()) {
|
|
56693
|
-
const st2 = s3.filter.stats();
|
|
56694
|
-
inputChars += st2.inputChars;
|
|
56695
|
-
dropped = dropped || st2.dropped;
|
|
56696
|
-
}
|
|
56697
|
-
const msg = degenerateTurnWarning({
|
|
56698
|
-
reason: finalFinishReason,
|
|
56699
|
-
terminalReason: protocol === "anthropic" ? "end_turn" : "stop",
|
|
56700
|
-
toolCalls: sawToolUse ? 1 : 0,
|
|
56701
|
-
text: { inputChars, outputChars: visibleTextChars, dropped },
|
|
56702
|
-
sawThinking,
|
|
56703
|
-
wire: `plugin-passthrough-${protocol}`
|
|
56704
|
-
});
|
|
56705
|
-
if (msg) {
|
|
56706
|
-
log("warn", msg);
|
|
56707
|
-
log2?.(msg);
|
|
56708
|
-
}
|
|
56709
|
-
};
|
|
56710
56582
|
const pushField = (field, index, text) => {
|
|
56711
56583
|
const s3 = filterFor(field, index);
|
|
56712
56584
|
const clean = s3.filter.push(text);
|
|
@@ -56726,28 +56598,21 @@ data: ${JSON.stringify({ type: "content_block_delta", index, delta: { type: delt
|
|
|
56726
56598
|
let hadText = false;
|
|
56727
56599
|
for (let ci2 = 0; ci2 < choices.length; ci2++) {
|
|
56728
56600
|
const ch = choices[ci2];
|
|
56729
|
-
if (ch && typeof ch["finish_reason"] === "string") finalFinishReason = ch["finish_reason"];
|
|
56730
56601
|
const d = ch?.["delta"];
|
|
56731
56602
|
if (!d || typeof d !== "object") continue;
|
|
56732
56603
|
const dd = d;
|
|
56733
|
-
if (dd["tool_calls"] !== void 0) sawToolUse = true;
|
|
56734
56604
|
for (const field of ["content", "reasoning_content", "reasoning"]) {
|
|
56735
56605
|
const v2 = dd[field];
|
|
56736
56606
|
if (typeof v2 !== "string") continue;
|
|
56737
56607
|
hadText = true;
|
|
56738
|
-
if (field !== "content" && v2.length > 0) sawThinking = true;
|
|
56739
56608
|
if (!mayStartRenderTag(v2) && !anyPending()) {
|
|
56740
56609
|
if (v2.length > 0) keptText = true;
|
|
56741
|
-
if (field === "content") visibleTextChars += v2.length;
|
|
56742
56610
|
continue;
|
|
56743
56611
|
}
|
|
56744
56612
|
const index = typeof ch?.["index"] === "number" ? ch["index"] : ci2;
|
|
56745
56613
|
const [clean, changed] = pushField(field, index, v2);
|
|
56746
56614
|
if (clean.length === 0) droppedText = true;
|
|
56747
|
-
else
|
|
56748
|
-
keptText = true;
|
|
56749
|
-
if (field === "content") visibleTextChars += clean.length;
|
|
56750
|
-
}
|
|
56615
|
+
else keptText = true;
|
|
56751
56616
|
if (changed) {
|
|
56752
56617
|
if (!rebuilt) {
|
|
56753
56618
|
rebuilt = { ...ev, choices: choices.map((c) => ({ ...c, delta: { ...c["delta"] } })) };
|
|
@@ -56766,13 +56631,6 @@ data: ${JSON.stringify({ type: "content_block_delta", index, delta: { type: delt
|
|
|
56766
56631
|
return rawEvent + "\n\n";
|
|
56767
56632
|
};
|
|
56768
56633
|
const processAnthropic = (ev, rawEvent) => {
|
|
56769
|
-
if (ev["type"] === "content_block_start") {
|
|
56770
|
-
const cb = ev["content_block"];
|
|
56771
|
-
const bt2 = cb && typeof cb === "object" ? cb["type"] : void 0;
|
|
56772
|
-
if (bt2 === "tool_use") sawToolUse = true;
|
|
56773
|
-
else if (bt2 === "thinking" || bt2 === "redacted_thinking") sawThinking = true;
|
|
56774
|
-
return anyPending() ? flushTails() + rawEvent + "\n\n" : rawEvent + "\n\n";
|
|
56775
|
-
}
|
|
56776
56634
|
if (ev["type"] !== "content_block_delta") {
|
|
56777
56635
|
return anyPending() ? flushTails() + rawEvent + "\n\n" : rawEvent + "\n\n";
|
|
56778
56636
|
}
|
|
@@ -56783,17 +56641,11 @@ data: ${JSON.stringify({ type: "content_block_delta", index, delta: { type: delt
|
|
|
56783
56641
|
return rawEvent + "\n\n";
|
|
56784
56642
|
}
|
|
56785
56643
|
const raw = d[field];
|
|
56786
|
-
if (field === "thinking" && raw.length > 0) sawThinking = true;
|
|
56787
56644
|
if (!mayStartRenderTag(raw) && !anyPending()) {
|
|
56788
|
-
if (field === "text" && raw.length > 0) visibleTextChars += raw.length;
|
|
56789
56645
|
return rawEvent + "\n\n";
|
|
56790
56646
|
}
|
|
56791
56647
|
const [clean, changed] = pushField(field, index, raw);
|
|
56792
|
-
if (!changed)
|
|
56793
|
-
if (field === "text" && raw.length > 0) visibleTextChars += raw.length;
|
|
56794
|
-
return rawEvent + "\n\n";
|
|
56795
|
-
}
|
|
56796
|
-
if (field === "text" && clean.length > 0) visibleTextChars += clean.length;
|
|
56648
|
+
if (!changed) return rawEvent + "\n\n";
|
|
56797
56649
|
if (clean.length === 0 && Object.keys(d ?? {}).length <= 2) return "";
|
|
56798
56650
|
return rebuildEvent(rawEvent, { ...ev, delta: { ...d, [field]: clean } });
|
|
56799
56651
|
};
|
|
@@ -56824,10 +56676,6 @@ data: ${JSON.stringify({ type: "content_block_delta", index, delta: { type: delt
|
|
|
56824
56676
|
continue;
|
|
56825
56677
|
}
|
|
56826
56678
|
if (ev["type"] === "message_stop") sawTerminal = true;
|
|
56827
|
-
if (ev["type"] === "message_delta") {
|
|
56828
|
-
const d = ev["delta"];
|
|
56829
|
-
if (d && typeof d["stop_reason"] === "string") finalFinishReason = d["stop_reason"];
|
|
56830
|
-
}
|
|
56831
56679
|
const sample = usageFromSseEvent(ev);
|
|
56832
56680
|
if (sample) mergeUsageSample(acc, sample);
|
|
56833
56681
|
let backfilled = false;
|
|
@@ -56851,7 +56699,6 @@ data: ${JSON.stringify({ type: "content_block_delta", index, delta: { type: delt
|
|
|
56851
56699
|
if (rest.length > 0 && !res.destroyed && !res.writableEnded) await write(rest);
|
|
56852
56700
|
settleUsage();
|
|
56853
56701
|
maybeNoteTruncated();
|
|
56854
|
-
maybeWarnDegenerate();
|
|
56855
56702
|
} catch (e) {
|
|
56856
56703
|
settleUsage();
|
|
56857
56704
|
maybeNoteTruncated();
|
|
@@ -56888,9 +56735,6 @@ async function pipePluginResponsesWithStrip(stream2, res, session, log2) {
|
|
|
56888
56735
|
log("warn", `[tag-echo] stripped model-emitted render tag (plugin passthrough): ${snippet.slice(0, 80).replace(/\n/g, " ")}`);
|
|
56889
56736
|
log2?.(`[tag-echo] stripped model-emitted render tag from plugin passthrough text`);
|
|
56890
56737
|
});
|
|
56891
|
-
let sawFunctionCall = false;
|
|
56892
|
-
let sawReasoning = false;
|
|
56893
|
-
let responseStatus;
|
|
56894
56738
|
const write = (s3) => {
|
|
56895
56739
|
if (!res.write(Buffer.from(s3, "utf8"))) {
|
|
56896
56740
|
return new Promise((r) => res.once("drain", () => r()));
|
|
@@ -56911,22 +56755,6 @@ async function pipePluginResponsesWithStrip(stream2, res, session, log2) {
|
|
|
56911
56755
|
reason: "plugin responses passthrough stream ended without a completion event"
|
|
56912
56756
|
});
|
|
56913
56757
|
};
|
|
56914
|
-
const maybeWarnDegenerate = () => {
|
|
56915
|
-
if (!sawTerminal || res.destroyed || res.writableEnded) return;
|
|
56916
|
-
const st2 = tagFilter.stats();
|
|
56917
|
-
const msg = degenerateTurnWarning({
|
|
56918
|
-
reason: responseStatus,
|
|
56919
|
-
terminalReason: "completed",
|
|
56920
|
-
toolCalls: sawFunctionCall ? 1 : 0,
|
|
56921
|
-
text: st2,
|
|
56922
|
-
sawThinking: sawReasoning,
|
|
56923
|
-
wire: "plugin-passthrough-responses"
|
|
56924
|
-
});
|
|
56925
|
-
if (msg) {
|
|
56926
|
-
log("warn", msg);
|
|
56927
|
-
log2?.(msg);
|
|
56928
|
-
}
|
|
56929
|
-
};
|
|
56930
56758
|
let lastDeltaMeta = null;
|
|
56931
56759
|
const flushTail = (after) => {
|
|
56932
56760
|
const tail = tagFilter.flush();
|
|
@@ -56966,16 +56794,6 @@ async function pipePluginResponsesWithStrip(stream2, res, session, log2) {
|
|
|
56966
56794
|
const sample = usageFromSseEvent(ev);
|
|
56967
56795
|
if (sample) mergeUsageSample(acc, sample);
|
|
56968
56796
|
const type = ev["type"];
|
|
56969
|
-
if (typeof type === "string") {
|
|
56970
|
-
if (type.startsWith("response.reasoning")) sawReasoning = true;
|
|
56971
|
-
if (type === "response.output_item.added" || type === "response.output_item.done") {
|
|
56972
|
-
const item = ev["item"];
|
|
56973
|
-
const it2 = item?.["type"];
|
|
56974
|
-
if (it2 === "function_call" || it2 === "custom_tool_call") sawFunctionCall = true;
|
|
56975
|
-
}
|
|
56976
|
-
const resp = ev["response"];
|
|
56977
|
-
if (resp && typeof resp["status"] === "string") responseStatus = resp["status"];
|
|
56978
|
-
}
|
|
56979
56797
|
if (type === "response.output_text.done" || type === "response.content_part.done" || type === "response.output_item.done" || type === "response.completed" || type === "response.failed" || type === "response.incomplete") {
|
|
56980
56798
|
if (type === "response.completed" || type === "response.failed" || type === "response.incomplete") sawTerminal = true;
|
|
56981
56799
|
let evOut = ev;
|
|
@@ -57021,7 +56839,6 @@ async function pipePluginResponsesWithStrip(stream2, res, session, log2) {
|
|
|
57021
56839
|
const rest = flushTail("");
|
|
57022
56840
|
if (rest.length > 0) await write(rest);
|
|
57023
56841
|
}
|
|
57024
|
-
maybeWarnDegenerate();
|
|
57025
56842
|
settleUsage();
|
|
57026
56843
|
maybeNoteTruncated();
|
|
57027
56844
|
} catch (e) {
|