@kingsoftcloud/agentkit-harness-node 0.0.27 → 0.0.29
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/engine-view.html +1 -1
- package/dist/index.mjs +155 -52
- package/dist/rpc-entry.mjs +155 -52
- package/package.json +2 -2
- package/src/native/e2b-command-tools.mjs +27 -10
- package/src/native/engine-runtime.mjs +10 -7
- package/src/native/engine-service.mjs +4 -1
- package/src/native/local-command-state.mjs +10 -2
- package/src/native/model-anthropic.mjs +10 -1
- package/src/native/model-openai-chat.mjs +14 -4
- package/src/native/model-port.mjs +42 -11
- package/src/native/model-responses.mjs +26 -9
- package/src/native/model-sse.mjs +44 -24
- package/src/native/model-stream-budget.mjs +31 -0
- package/src/native/native-terminal-failure.mjs +5 -0
- package/src/native/windows-private-state.mjs +41 -0
- package/src/native/workspace-state.mjs +3 -3
- package/src/native/workspace-tools.mjs +3 -3
package/dist/index.mjs
CHANGED
|
@@ -1522,13 +1522,16 @@ function toAsyncIterator(body) {
|
|
|
1522
1522
|
throw new SseFramingError("model SSE requires a byte stream");
|
|
1523
1523
|
}
|
|
1524
1524
|
var SseFramer = class {
|
|
1525
|
-
constructor() {
|
|
1525
|
+
constructor(maxFrameBytes) {
|
|
1526
|
+
this.maxFrameBytes = maxFrameBytes;
|
|
1527
|
+
this.lineBytes = 0;
|
|
1528
|
+
this.dataBytes = 0;
|
|
1526
1529
|
this.lineParts = [];
|
|
1527
1530
|
this.dataLines = [];
|
|
1528
1531
|
this.eventName = "";
|
|
1529
1532
|
this.afterCr = false;
|
|
1530
1533
|
}
|
|
1531
|
-
feed(text
|
|
1534
|
+
*feed(text) {
|
|
1532
1535
|
if (!text) return;
|
|
1533
1536
|
if (this.afterCr) {
|
|
1534
1537
|
if (text.startsWith("\n")) text = text.slice(1);
|
|
@@ -1536,15 +1539,26 @@ var SseFramer = class {
|
|
|
1536
1539
|
}
|
|
1537
1540
|
let start = 0;
|
|
1538
1541
|
for (const m of text.matchAll(/\r\n|\r|\n/g)) {
|
|
1539
|
-
this.
|
|
1542
|
+
this.appendLine(text.slice(start, m.index));
|
|
1540
1543
|
const line = this.lineParts.join("");
|
|
1541
1544
|
this.lineParts = [];
|
|
1545
|
+
this.lineBytes = 0;
|
|
1542
1546
|
start = m.index + m[0].length;
|
|
1543
1547
|
this.afterCr = m[0] === "\r" && start === text.length;
|
|
1544
1548
|
const frame = this.handleLine(line);
|
|
1545
|
-
if (frame)
|
|
1549
|
+
if (frame) yield frame;
|
|
1550
|
+
}
|
|
1551
|
+
if (start < text.length) this.appendLine(text.slice(start));
|
|
1552
|
+
}
|
|
1553
|
+
appendLine(part) {
|
|
1554
|
+
this.lineBytes += Buffer.byteLength(part);
|
|
1555
|
+
this.checkFrameBytes(this.lineBytes);
|
|
1556
|
+
this.lineParts.push(part);
|
|
1557
|
+
}
|
|
1558
|
+
checkFrameBytes(bytes) {
|
|
1559
|
+
if (bytes > this.maxFrameBytes) {
|
|
1560
|
+
throw new SseFramingError("model SSE line or event exceeded the byte limit", "stream_frame_limit");
|
|
1546
1561
|
}
|
|
1547
|
-
if (start < text.length) this.lineParts.push(text.slice(start));
|
|
1548
1562
|
}
|
|
1549
1563
|
handleLine(line) {
|
|
1550
1564
|
if (line === "") {
|
|
@@ -1554,6 +1568,7 @@ var SseFramer = class {
|
|
|
1554
1568
|
}
|
|
1555
1569
|
const frame = { data: this.dataLines.join("\n"), event: this.eventName || "" };
|
|
1556
1570
|
this.dataLines = [];
|
|
1571
|
+
this.dataBytes = 0;
|
|
1557
1572
|
this.eventName = "";
|
|
1558
1573
|
return frame;
|
|
1559
1574
|
}
|
|
@@ -1562,8 +1577,14 @@ var SseFramer = class {
|
|
|
1562
1577
|
const name = sep === -1 ? line : line.slice(0, sep);
|
|
1563
1578
|
let value = sep === -1 ? "" : line.slice(sep + 1);
|
|
1564
1579
|
if (value.startsWith(" ")) value = value.slice(1);
|
|
1565
|
-
if (name === "data")
|
|
1566
|
-
|
|
1580
|
+
if (name === "data") {
|
|
1581
|
+
this.dataBytes += Buffer.byteLength(value) + Number(this.dataLines.length > 0);
|
|
1582
|
+
this.checkFrameBytes(this.dataBytes + Buffer.byteLength(this.eventName));
|
|
1583
|
+
this.dataLines.push(value);
|
|
1584
|
+
} else if (name === "event") {
|
|
1585
|
+
this.checkFrameBytes(this.dataBytes + Buffer.byteLength(value));
|
|
1586
|
+
this.eventName = value;
|
|
1587
|
+
}
|
|
1567
1588
|
return null;
|
|
1568
1589
|
}
|
|
1569
1590
|
finish() {
|
|
@@ -1573,14 +1594,14 @@ var SseFramer = class {
|
|
|
1573
1594
|
}
|
|
1574
1595
|
}
|
|
1575
1596
|
};
|
|
1576
|
-
async function* readModelSse(body, {
|
|
1577
|
-
if (!Number.isSafeInteger(maxBytes) || maxBytes <= 0) {
|
|
1597
|
+
async function* readModelSse(body, { maxFrameBytes = 8 * 1024 * 1024, maxBytes, idleTimeoutMs, abort, observeBytes } = {}) {
|
|
1598
|
+
if (!Number.isSafeInteger(maxFrameBytes) || maxFrameBytes <= 0 || maxBytes !== void 0 && (!Number.isSafeInteger(maxBytes) || maxBytes <= 0)) {
|
|
1578
1599
|
throw new SseFramingError("model SSE needs a positive byte budget");
|
|
1579
1600
|
}
|
|
1580
1601
|
const iter = toAsyncIterator(body);
|
|
1581
1602
|
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
1582
|
-
const framer = new SseFramer();
|
|
1583
|
-
let
|
|
1603
|
+
const framer = new SseFramer(maxFrameBytes);
|
|
1604
|
+
let wireBytes = 0;
|
|
1584
1605
|
const idleEnabled = typeof idleTimeoutMs === "number" && idleTimeoutMs > 0 && typeof abort === "function";
|
|
1585
1606
|
let idleTimer = null;
|
|
1586
1607
|
const armIdle = () => {
|
|
@@ -1596,7 +1617,6 @@ async function* readModelSse(body, { maxBytes = 8 * 1024 * 1024, idleTimeoutMs,
|
|
|
1596
1617
|
}
|
|
1597
1618
|
};
|
|
1598
1619
|
armIdle();
|
|
1599
|
-
const frames = [];
|
|
1600
1620
|
try {
|
|
1601
1621
|
while (true) {
|
|
1602
1622
|
let result;
|
|
@@ -1606,18 +1626,18 @@ async function* readModelSse(body, { maxBytes = 8 * 1024 * 1024, idleTimeoutMs,
|
|
|
1606
1626
|
if (!(chunk instanceof Uint8Array) && !(chunk instanceof ArrayBuffer)) {
|
|
1607
1627
|
throw new SseFramingError("model SSE requires byte chunks");
|
|
1608
1628
|
}
|
|
1609
|
-
total += chunk.byteLength;
|
|
1610
1629
|
observeBytes?.(chunk.byteLength);
|
|
1611
|
-
if (
|
|
1630
|
+
if (maxBytes !== void 0) {
|
|
1631
|
+
wireBytes += chunk.byteLength;
|
|
1632
|
+
if (wireBytes > maxBytes) throw new SseFramingError("model SSE byte budget exceeded", "body_limit");
|
|
1633
|
+
}
|
|
1612
1634
|
let text;
|
|
1613
1635
|
try {
|
|
1614
1636
|
text = decoder.decode(chunk, { stream: true });
|
|
1615
1637
|
} catch {
|
|
1616
1638
|
throw new SseFramingError("invalid utf-8 in model stream", "body_encoding");
|
|
1617
1639
|
}
|
|
1618
|
-
|
|
1619
|
-
framer.feed(text, frames);
|
|
1620
|
-
for (const frame of frames) {
|
|
1640
|
+
for (const frame of framer.feed(text)) {
|
|
1621
1641
|
yield frame;
|
|
1622
1642
|
if (frame.data && frame.data.trim()) armIdle();
|
|
1623
1643
|
}
|
|
@@ -1628,11 +1648,7 @@ async function* readModelSse(body, { maxBytes = 8 * 1024 * 1024, idleTimeoutMs,
|
|
|
1628
1648
|
} catch {
|
|
1629
1649
|
throw new SseFramingError("invalid utf-8 in model stream", "body_encoding");
|
|
1630
1650
|
}
|
|
1631
|
-
|
|
1632
|
-
frames.length = 0;
|
|
1633
|
-
framer.feed(tail, frames);
|
|
1634
|
-
for (const frame of frames) yield frame;
|
|
1635
|
-
}
|
|
1651
|
+
yield* framer.feed(tail);
|
|
1636
1652
|
framer.finish();
|
|
1637
1653
|
} finally {
|
|
1638
1654
|
disarmIdle();
|
|
@@ -1646,6 +1662,38 @@ async function* readModelSse(body, { maxBytes = 8 * 1024 * 1024, idleTimeoutMs,
|
|
|
1646
1662
|
}
|
|
1647
1663
|
}
|
|
1648
1664
|
|
|
1665
|
+
// packages/agentkit-harness-node/src/native/model-stream-budget.mjs
|
|
1666
|
+
var ModelStreamLimitError = class extends ProtocolError {
|
|
1667
|
+
};
|
|
1668
|
+
function createModelStreamBudget(maxOutputBytes, maxToolBytes) {
|
|
1669
|
+
let outputBytes = 0;
|
|
1670
|
+
const toolBytes = /* @__PURE__ */ new WeakMap();
|
|
1671
|
+
const retain = (bytes, code = "stream_output_limit") => {
|
|
1672
|
+
if (outputBytes + bytes > maxOutputBytes) {
|
|
1673
|
+
throw new ModelStreamLimitError("assembled model output exceeded the byte limit", code);
|
|
1674
|
+
}
|
|
1675
|
+
outputBytes += bytes;
|
|
1676
|
+
};
|
|
1677
|
+
return {
|
|
1678
|
+
output(text) {
|
|
1679
|
+
retain(Buffer.byteLength(text));
|
|
1680
|
+
},
|
|
1681
|
+
// Indexed items also retain state when their payload is empty.
|
|
1682
|
+
slot() {
|
|
1683
|
+
retain(128);
|
|
1684
|
+
},
|
|
1685
|
+
tool(call, fragment) {
|
|
1686
|
+
const bytes = Buffer.byteLength(fragment);
|
|
1687
|
+
const total = (toolBytes.get(call) ?? 0) + bytes;
|
|
1688
|
+
if (total > maxToolBytes) {
|
|
1689
|
+
throw new ModelStreamLimitError("assembled tool input exceeded the byte limit", "tool_argument_limit");
|
|
1690
|
+
}
|
|
1691
|
+
retain(bytes, "tool_argument_limit");
|
|
1692
|
+
toolBytes.set(call, total);
|
|
1693
|
+
}
|
|
1694
|
+
};
|
|
1695
|
+
}
|
|
1696
|
+
|
|
1649
1697
|
// packages/agentkit-harness-node/src/native/model-replay.mjs
|
|
1650
1698
|
import { createHash as createHash4 } from "node:crypto";
|
|
1651
1699
|
import { canonical as canonical7, modelWireToolCall } from "@kingsoftcloud/agentkit-harness-core";
|
|
@@ -2657,7 +2705,7 @@ function chatReasoning(message) {
|
|
|
2657
2705
|
if (native && alias && native !== alias) throw new ProtocolError("conflicting private reasoning fields", "delta_invalid");
|
|
2658
2706
|
return native || alias || "";
|
|
2659
2707
|
}
|
|
2660
|
-
function createOpenAIStreamTurn(profile, binding) {
|
|
2708
|
+
function createOpenAIStreamTurn(profile, binding, budget) {
|
|
2661
2709
|
const compatibility = resolveProviderCompatibility(profile.providerCompatibilityPolicy, binding);
|
|
2662
2710
|
const modern = compatibility?.modern === true;
|
|
2663
2711
|
const finishOnEof = compatibility?.finishOnEof === true;
|
|
@@ -2757,6 +2805,8 @@ function createOpenAIStreamTurn(profile, binding) {
|
|
|
2757
2805
|
if (typeof content !== "string") throw new ProtocolError("invalid visible content delta", "delta_invalid");
|
|
2758
2806
|
const reasoning = chatReasoning(delta);
|
|
2759
2807
|
if (this.finish != null && reasoning) throw new ProtocolError("private reasoning continued after finish", "termination_conflict");
|
|
2808
|
+
budget?.output(content);
|
|
2809
|
+
budget?.output(reasoning);
|
|
2760
2810
|
if (captureReplay) this.reasoning += reasoning;
|
|
2761
2811
|
if (this.finish != null && (content || delta.tool_calls)) {
|
|
2762
2812
|
throw new ProtocolError("content continued after finish", "termination_conflict");
|
|
@@ -2777,7 +2827,8 @@ function createOpenAIStreamTurn(profile, binding) {
|
|
|
2777
2827
|
try {
|
|
2778
2828
|
if ("function_call" in delta) throw new ProtocolError("legacy function calls are unsupported");
|
|
2779
2829
|
this.addToolFragments(delta.tool_calls, emit);
|
|
2780
|
-
} catch {
|
|
2830
|
+
} catch (error) {
|
|
2831
|
+
if (error instanceof ModelStreamLimitError) throw error;
|
|
2781
2832
|
this.malformedTools = true;
|
|
2782
2833
|
}
|
|
2783
2834
|
},
|
|
@@ -2796,6 +2847,7 @@ function createOpenAIStreamTurn(profile, binding) {
|
|
|
2796
2847
|
if (typeof value !== "string" || !value || call[field] != null && call[field] !== value) {
|
|
2797
2848
|
throw new ProtocolError("conflicting tool identity", "identity_conflict");
|
|
2798
2849
|
}
|
|
2850
|
+
if (call[field] == null) budget?.output(value);
|
|
2799
2851
|
call[field] = value;
|
|
2800
2852
|
}
|
|
2801
2853
|
}
|
|
@@ -2806,11 +2858,16 @@ function createOpenAIStreamTurn(profile, binding) {
|
|
|
2806
2858
|
if (field in fn) {
|
|
2807
2859
|
if (fn[field] == null) continue;
|
|
2808
2860
|
if (typeof fn[field] !== "string") throw new ProtocolError("function fragment must be text");
|
|
2861
|
+
if (field === "arguments") budget?.tool(call, fn[field]);
|
|
2862
|
+
else budget?.output(fn[field]);
|
|
2809
2863
|
call.function[field] += fn[field];
|
|
2810
2864
|
}
|
|
2811
2865
|
}
|
|
2812
2866
|
}
|
|
2813
|
-
if (!this.tools.has(fragment.index))
|
|
2867
|
+
if (!this.tools.has(fragment.index)) {
|
|
2868
|
+
budget?.slot();
|
|
2869
|
+
this.tools.set(fragment.index, call);
|
|
2870
|
+
}
|
|
2814
2871
|
if (fn && "arguments" in fn && typeof fn.arguments === "string" && fn.arguments) {
|
|
2815
2872
|
emit({ kind: "tool_delta", text: fn.arguments, metadata: {
|
|
2816
2873
|
index: fragment.index,
|
|
@@ -3334,7 +3391,7 @@ function classifyAnthropicStreamError(errorObj) {
|
|
|
3334
3391
|
}
|
|
3335
3392
|
return { kind: "provider_terminal_error", failureKind: "invalid_response" };
|
|
3336
3393
|
}
|
|
3337
|
-
function createAnthropicStreamState(profile) {
|
|
3394
|
+
function createAnthropicStreamState(profile, budget) {
|
|
3338
3395
|
const captureReplay = profile.providerReplayMode !== "none";
|
|
3339
3396
|
const emitThinking = profile.providerThinkingDisplay ?? profile.providerReplayMode !== "none";
|
|
3340
3397
|
return {
|
|
@@ -3448,6 +3505,9 @@ function createAnthropicStreamState(profile) {
|
|
|
3448
3505
|
if (this.blockTypes.has(index) || this.closed.has(index)) {
|
|
3449
3506
|
throw new ProtocolError("duplicate content block index");
|
|
3450
3507
|
}
|
|
3508
|
+
budget?.slot();
|
|
3509
|
+
const { input: _input, ...replayMetadata } = block;
|
|
3510
|
+
budget?.output(stringifyModelJson2(replayMetadata));
|
|
3451
3511
|
this.blockTypes.set(index, blockType);
|
|
3452
3512
|
this.originals.set(index, deepCopy(block));
|
|
3453
3513
|
for (const field of ["text", "thinking", "signature"]) {
|
|
@@ -3476,6 +3536,7 @@ function createAnthropicStreamState(profile) {
|
|
|
3476
3536
|
}
|
|
3477
3537
|
if (Object.keys(block.input).length > 0) {
|
|
3478
3538
|
tool.arguments = stringifyModelJson2(block.input);
|
|
3539
|
+
budget?.tool(tool, tool.arguments);
|
|
3479
3540
|
tool.initialInput = true;
|
|
3480
3541
|
this.initialInputs.add(index);
|
|
3481
3542
|
}
|
|
@@ -3506,6 +3567,7 @@ function createAnthropicStreamState(profile) {
|
|
|
3506
3567
|
throw new ProtocolError("delta type does not match block type");
|
|
3507
3568
|
}
|
|
3508
3569
|
if (expected != null && previous == null && (expected === "text" || expected === "thinking")) {
|
|
3570
|
+
budget?.slot();
|
|
3509
3571
|
this.blockTypes.set(index, expected);
|
|
3510
3572
|
this.implicitIndexes.add(index);
|
|
3511
3573
|
this.originals.set(index, { type: expected });
|
|
@@ -3513,19 +3575,23 @@ function createAnthropicStreamState(profile) {
|
|
|
3513
3575
|
}
|
|
3514
3576
|
if (deltaType === "text_delta") {
|
|
3515
3577
|
if (typeof delta.text !== "string") throw new ProtocolError("invalid text delta");
|
|
3578
|
+
budget?.output(delta.text);
|
|
3516
3579
|
this.values.set(index, (this.values.get(index) || "") + delta.text);
|
|
3517
3580
|
} else if (deltaType === "thinking_delta") {
|
|
3518
3581
|
if (typeof delta.thinking !== "string") throw new ProtocolError("invalid thinking delta");
|
|
3582
|
+
budget?.output(delta.thinking);
|
|
3519
3583
|
this.values.set(index, (this.values.get(index) || "") + delta.thinking);
|
|
3520
3584
|
} else if (deltaType === "signature_delta") {
|
|
3521
3585
|
if (typeof delta.signature !== "string") throw new ProtocolError("invalid signature delta");
|
|
3522
3586
|
const original = this.originals.get(index) || { type: "thinking" };
|
|
3587
|
+
budget?.output(delta.signature);
|
|
3523
3588
|
original.signature = (original.signature || "") + delta.signature;
|
|
3524
3589
|
this.originals.set(index, original);
|
|
3525
3590
|
} else if (deltaType === "input_json_delta") {
|
|
3526
3591
|
if (typeof delta.partial_json !== "string") throw new ProtocolError("invalid input_json delta");
|
|
3527
3592
|
const tool = this.tools.get(index);
|
|
3528
3593
|
if (!tool || this.initialInputs.has(index)) throw new ProtocolError("tool delta without an open block");
|
|
3594
|
+
budget?.tool(tool, delta.partial_json);
|
|
3529
3595
|
tool.arguments += delta.partial_json;
|
|
3530
3596
|
if (delta.partial_json) {
|
|
3531
3597
|
emit({ kind: "tool_delta", text: delta.partial_json, metadata: {
|
|
@@ -4094,7 +4160,7 @@ function validateBufferedIdentity(payload) {
|
|
|
4094
4160
|
const identity = createWireIdentity();
|
|
4095
4161
|
identity.accept({ type: "response.created", response: payload });
|
|
4096
4162
|
}
|
|
4097
|
-
function createResponsesItems() {
|
|
4163
|
+
function createResponsesItems(budget) {
|
|
4098
4164
|
return {
|
|
4099
4165
|
indexes: /* @__PURE__ */ new Map(),
|
|
4100
4166
|
// output_index -> item_id
|
|
@@ -4106,6 +4172,10 @@ function createResponsesItems() {
|
|
|
4106
4172
|
const itemId = item != null ? identityOf(item.id) : identityOf(event.item_id);
|
|
4107
4173
|
if ("item_id" in event && identityOf(event.item_id) !== itemId) throw new ProtocolError("invalid model frame", "envelope_invalid");
|
|
4108
4174
|
const index = indexOf(event, "output_index");
|
|
4175
|
+
if ((index != null || item != null || kind) && !this.itemIndexes.has(itemId) && !this.kinds.has(itemId)) {
|
|
4176
|
+
budget?.slot();
|
|
4177
|
+
budget?.output(itemId);
|
|
4178
|
+
}
|
|
4109
4179
|
if (index != null) {
|
|
4110
4180
|
if (this.indexes.get(index) != null && this.indexes.get(index) !== itemId) throw new ProtocolError("invalid model frame", "envelope_invalid");
|
|
4111
4181
|
if (this.itemIndexes.get(itemId) != null && this.itemIndexes.get(itemId) !== index) throw new ProtocolError("invalid model frame", "envelope_invalid");
|
|
@@ -4131,7 +4201,7 @@ var DELTA_KINDS = {
|
|
|
4131
4201
|
function createContentPart(kind) {
|
|
4132
4202
|
return { kind, text: "", closed: false };
|
|
4133
4203
|
}
|
|
4134
|
-
function createResponsesContent(items) {
|
|
4204
|
+
function createResponsesContent(items, budget) {
|
|
4135
4205
|
const state = {
|
|
4136
4206
|
items,
|
|
4137
4207
|
// shared ResponsesItems
|
|
@@ -4156,6 +4226,7 @@ function createResponsesContent(items) {
|
|
|
4156
4226
|
return previous;
|
|
4157
4227
|
}
|
|
4158
4228
|
if (item.closed) throw new ProtocolError("invalid model frame", "envelope_invalid");
|
|
4229
|
+
budget?.slot();
|
|
4159
4230
|
const part = createContentPart(kind);
|
|
4160
4231
|
item.parts.set(partKey, part);
|
|
4161
4232
|
return part;
|
|
@@ -4168,6 +4239,7 @@ function createResponsesContent(items) {
|
|
|
4168
4239
|
const part = this.getPart(item, group, index, kind);
|
|
4169
4240
|
if (delta) {
|
|
4170
4241
|
if (part.closed) throw new ProtocolError("invalid model frame", "envelope_invalid");
|
|
4242
|
+
budget?.output(text);
|
|
4171
4243
|
part.text += text;
|
|
4172
4244
|
} else {
|
|
4173
4245
|
this.snapshotPart(part, text, closed);
|
|
@@ -4178,6 +4250,7 @@ function createResponsesContent(items) {
|
|
|
4178
4250
|
if (part.closed && text !== part.text || !text.startsWith(part.text)) {
|
|
4179
4251
|
throw new ProtocolError("invalid model frame", "envelope_invalid");
|
|
4180
4252
|
}
|
|
4253
|
+
budget?.output(text.slice(part.text.length));
|
|
4181
4254
|
part.text = text;
|
|
4182
4255
|
part.closed = part.closed || closed;
|
|
4183
4256
|
},
|
|
@@ -4296,11 +4369,13 @@ function createResponsesContent(items) {
|
|
|
4296
4369
|
throw new ProtocolError("invalid model frame", "envelope_invalid");
|
|
4297
4370
|
}
|
|
4298
4371
|
tail = text.slice(previous.length);
|
|
4372
|
+
budget?.output(tail);
|
|
4299
4373
|
this[attr] = text;
|
|
4300
4374
|
this.legacyClosed.add(attr);
|
|
4301
4375
|
} else {
|
|
4302
4376
|
if (this.legacyClosed.has(attr)) throw new ProtocolError("invalid model frame", "envelope_invalid");
|
|
4303
4377
|
tail = text;
|
|
4378
|
+
budget?.output(tail);
|
|
4304
4379
|
this[attr] = previous + text;
|
|
4305
4380
|
}
|
|
4306
4381
|
const indexed = attr === "legacyText" ? this.emittedText : this.emittedReasoning;
|
|
@@ -4390,8 +4465,8 @@ function sealTool(tool) {
|
|
|
4390
4465
|
identityOf(tool.name);
|
|
4391
4466
|
tool.closed = true;
|
|
4392
4467
|
}
|
|
4393
|
-
function createResponsesToolCalls() {
|
|
4394
|
-
const items = createResponsesItems();
|
|
4468
|
+
function createResponsesToolCalls(budget) {
|
|
4469
|
+
const items = createResponsesItems(budget);
|
|
4395
4470
|
return {
|
|
4396
4471
|
items,
|
|
4397
4472
|
calls: /* @__PURE__ */ new Map(),
|
|
@@ -4413,7 +4488,11 @@ function createResponsesToolCalls() {
|
|
|
4413
4488
|
if (current != null) throw new ProtocolError("invalid model frame", "envelope_invalid");
|
|
4414
4489
|
current = createWireTool();
|
|
4415
4490
|
pinToolIdentity(current, item, "call_id");
|
|
4416
|
-
if ("arguments" in item)
|
|
4491
|
+
if ("arguments" in item) {
|
|
4492
|
+
const arguments_ = optionalText(item.arguments);
|
|
4493
|
+
budget?.tool(current, arguments_);
|
|
4494
|
+
current.arguments = arguments_;
|
|
4495
|
+
}
|
|
4417
4496
|
this.calls.set(key, current);
|
|
4418
4497
|
return null;
|
|
4419
4498
|
}
|
|
@@ -4427,6 +4506,7 @@ function createResponsesToolCalls() {
|
|
|
4427
4506
|
if (!arguments_.startsWith(current.arguments) || (current.closed || current.argumentsDone) && arguments_ !== current.arguments) {
|
|
4428
4507
|
throw new ProtocolError("invalid model frame", "envelope_invalid");
|
|
4429
4508
|
}
|
|
4509
|
+
budget?.tool(current, arguments_.slice(current.arguments.length));
|
|
4430
4510
|
current.arguments = arguments_;
|
|
4431
4511
|
}
|
|
4432
4512
|
if (current.closed) return null;
|
|
@@ -4438,10 +4518,13 @@ function createResponsesToolCalls() {
|
|
|
4438
4518
|
const current = this.calls.get(key);
|
|
4439
4519
|
if (current == null || current.closed || current.argumentsDone) throw new ProtocolError("invalid model frame", "envelope_invalid");
|
|
4440
4520
|
if (type.endsWith(".delta")) {
|
|
4441
|
-
|
|
4521
|
+
const delta = optionalText(event.delta);
|
|
4522
|
+
budget?.tool(current, delta);
|
|
4523
|
+
current.arguments += delta;
|
|
4442
4524
|
} else {
|
|
4443
4525
|
const arguments_ = optionalText(event.arguments);
|
|
4444
4526
|
if (!arguments_.startsWith(current.arguments)) throw new ProtocolError("invalid model frame", "envelope_invalid");
|
|
4527
|
+
budget?.tool(current, arguments_.slice(current.arguments.length));
|
|
4445
4528
|
current.arguments = arguments_;
|
|
4446
4529
|
current.argumentsDone = true;
|
|
4447
4530
|
}
|
|
@@ -4501,11 +4584,11 @@ function createWireIdentity() {
|
|
|
4501
4584
|
}
|
|
4502
4585
|
};
|
|
4503
4586
|
}
|
|
4504
|
-
function createResponsesStreamState(profile) {
|
|
4587
|
+
function createResponsesStreamState(profile, budget) {
|
|
4505
4588
|
const captureReplay = profile.providerReplayMode !== "none";
|
|
4506
4589
|
const emitThinking = profile.providerThinkingDisplay ?? profile.providerReplayMode !== "none";
|
|
4507
|
-
const toolItems = createResponsesToolCalls();
|
|
4508
|
-
const contentItems = createResponsesContent(toolItems.items);
|
|
4590
|
+
const toolItems = createResponsesToolCalls(budget);
|
|
4591
|
+
const contentItems = createResponsesContent(toolItems.items, budget);
|
|
4509
4592
|
const identity = createWireIdentity();
|
|
4510
4593
|
return {
|
|
4511
4594
|
text: "",
|
|
@@ -4832,6 +4915,8 @@ function observeModelTransportPort(generate) {
|
|
|
4832
4915
|
|
|
4833
4916
|
// packages/agentkit-harness-node/src/native/model-port.mjs
|
|
4834
4917
|
var DEFAULT_MAX_BYTES = 8 * 1024 * 1024;
|
|
4918
|
+
var DEFAULT_MAX_FRAME_BYTES = 6 * DEFAULT_MAX_BYTES + 64 * 1024;
|
|
4919
|
+
var MAX_TOOL_ARGUMENT_BYTES = 8 * 1024 * 1024;
|
|
4835
4920
|
var DEFAULT_IDLE_MS = 18e4;
|
|
4836
4921
|
function nativeModelRecoveryPolicy({ protocol, parameters = {} } = {}) {
|
|
4837
4922
|
return ["openai-chat", "openai-responses", "anthropic-messages"].includes(protocol) && parameters !== null && typeof parameters === "object" && !Array.isArray(parameters) && Object.keys(parameters).length === 0 ? "pure-inference/v1" : "manual";
|
|
@@ -4845,7 +4930,7 @@ var OverflowError = class extends Error {
|
|
|
4845
4930
|
function createNativeModelPort({
|
|
4846
4931
|
resolveProfile,
|
|
4847
4932
|
fetchImpl = fetch,
|
|
4848
|
-
maxResponseBytes
|
|
4933
|
+
maxResponseBytes,
|
|
4849
4934
|
idleTimeoutMs = DEFAULT_IDLE_MS
|
|
4850
4935
|
} = {}) {
|
|
4851
4936
|
if (typeof resolveProfile !== "function") {
|
|
@@ -4854,9 +4939,14 @@ function createNativeModelPort({
|
|
|
4854
4939
|
if (typeof fetchImpl !== "function") {
|
|
4855
4940
|
throw new TypeError("createNativeModelPort requires a fetchImpl function");
|
|
4856
4941
|
}
|
|
4857
|
-
|
|
4942
|
+
const maxBytes = maxResponseBytes === void 0 ? DEFAULT_MAX_BYTES : maxResponseBytes;
|
|
4943
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes <= 0) throw new RangeError("maxResponseBytes must be a positive safe integer");
|
|
4858
4944
|
if (!Number.isFinite(idleTimeoutMs) || idleTimeoutMs <= 0) throw new RangeError("idleTimeoutMs must be positive and finite");
|
|
4859
|
-
const
|
|
4945
|
+
const streamBounds = {
|
|
4946
|
+
maxFrameBytes: maxResponseBytes ?? DEFAULT_MAX_FRAME_BYTES,
|
|
4947
|
+
maxOutputBytes: maxBytes,
|
|
4948
|
+
maxBytes: maxResponseBytes
|
|
4949
|
+
};
|
|
4860
4950
|
const idleTimeout = idleTimeoutMs;
|
|
4861
4951
|
return observeModelTransportPort(async (request, emitEvent, observation) => {
|
|
4862
4952
|
if (!request || typeof request !== "object" || !request.profile) {
|
|
@@ -4920,16 +5010,10 @@ function createNativeModelPort({
|
|
|
4920
5010
|
clearTimer(overallTimer);
|
|
4921
5011
|
overallTimer = null;
|
|
4922
5012
|
}
|
|
4923
|
-
return await streamResponse(
|
|
4924
|
-
|
|
4925
|
-
|
|
4926
|
-
|
|
4927
|
-
emitEvent,
|
|
4928
|
-
internal,
|
|
4929
|
-
maxBytes,
|
|
4930
|
-
idlePolicy && overallMs > 0 ? Math.min(idleTimeout, overallMs) : idleTimeout,
|
|
4931
|
-
observation
|
|
4932
|
-
);
|
|
5013
|
+
return await streamResponse(response, request, binding, emitEvent, internal, {
|
|
5014
|
+
...streamBounds,
|
|
5015
|
+
idleTimeoutMs: idlePolicy && overallMs > 0 ? Math.min(idleTimeout, overallMs) : idleTimeout
|
|
5016
|
+
}, observation);
|
|
4933
5017
|
}
|
|
4934
5018
|
observation.phase("buffered");
|
|
4935
5019
|
return await bufferedResponse(response, request, binding, maxBytes, emitEvent, observation);
|
|
@@ -4945,10 +5029,11 @@ function createNativeModelPort({
|
|
|
4945
5029
|
}
|
|
4946
5030
|
});
|
|
4947
5031
|
}
|
|
4948
|
-
async function streamResponse(response, request, binding, emit, internal,
|
|
5032
|
+
async function streamResponse(response, request, binding, emit, internal, streamBounds, observation) {
|
|
4949
5033
|
const profile = request.profile;
|
|
4950
5034
|
const idlePolicy = profile.streaming !== false && profile.streamTimeoutPolicy === "idle/v1";
|
|
4951
|
-
const
|
|
5035
|
+
const budget = createModelStreamBudget(streamBounds.maxOutputBytes, streamBounds.maxBytes ?? MAX_TOOL_ARGUMENT_BYTES);
|
|
5036
|
+
const state = binding.protocol === "anthropic-messages" ? createAnthropicStreamState(profile, budget) : binding.protocol === "openai-responses" ? createResponsesStreamState(profile, budget) : createOpenAIStreamTurn(profile, binding, budget);
|
|
4952
5037
|
const abort = (reason) => internal.abort(reason);
|
|
4953
5038
|
let frameIndex = -1;
|
|
4954
5039
|
let reasoning = request.purpose == null || request.purpose === "task" ? "" : null;
|
|
@@ -4982,7 +5067,7 @@ async function streamResponse(response, request, binding, emit, internal, maxByt
|
|
|
4982
5067
|
}
|
|
4983
5068
|
);
|
|
4984
5069
|
try {
|
|
4985
|
-
for await (const frame of readModelSse(response.body, {
|
|
5070
|
+
for await (const frame of readModelSse(response.body, { ...streamBounds, abort, observeBytes: observation.bytes })) {
|
|
4986
5071
|
if (idlePolicy && !frame.data.trim()) continue;
|
|
4987
5072
|
frameIndex++;
|
|
4988
5073
|
observation.frame();
|
|
@@ -5007,9 +5092,27 @@ async function streamResponse(response, request, binding, emit, internal, maxByt
|
|
|
5007
5092
|
if (error instanceof IdleTimeout) {
|
|
5008
5093
|
return failed(state.finalize({ errorKind: "provider_retryable_error", failureKind: "request_timeout" }), "stream_timeout");
|
|
5009
5094
|
}
|
|
5095
|
+
if ((error instanceof SseFramingError || error instanceof ModelStreamLimitError) && streamBounds.maxBytes === void 0 && ["stream_frame_limit", "stream_output_limit"].includes(error.code) && (request.purpose == null || request.purpose === "task") && !state.refused && nativeModelRecoveryPolicy(binding) === "pure-inference/v1") {
|
|
5096
|
+
const partial = state.finalize({ errorKind: "output_limit" });
|
|
5097
|
+
return failed({
|
|
5098
|
+
...partial,
|
|
5099
|
+
toolCalls: [],
|
|
5100
|
+
textCommitVersion: "provider-finished-text/v1",
|
|
5101
|
+
outputBoundary: partial.text.trim() ? "assistant-text/v1" : "no-visible-output/v1"
|
|
5102
|
+
}, error.code);
|
|
5103
|
+
}
|
|
5010
5104
|
if (error instanceof SseFramingError) {
|
|
5011
5105
|
return failed(state.finalize({ errorKind: "provider_terminal_error", failureKind: "invalid_response" }), protocolCode(error));
|
|
5012
5106
|
}
|
|
5107
|
+
if (error instanceof ModelStreamLimitError) {
|
|
5108
|
+
const task = request.purpose == null || request.purpose === "task";
|
|
5109
|
+
const retryable = error.code === "tool_argument_limit" && task && !state.refused && nativeModelRecoveryPolicy(binding) === "pure-inference/v1";
|
|
5110
|
+
const result2 = state.finalize({
|
|
5111
|
+
errorKind: retryable ? "malformed_tool_call" : "provider_terminal_error",
|
|
5112
|
+
failureKind: "invalid_response"
|
|
5113
|
+
});
|
|
5114
|
+
return failed({ ...result2, toolCalls: [] }, error.code);
|
|
5115
|
+
}
|
|
5013
5116
|
if (error instanceof ProtocolError) {
|
|
5014
5117
|
if (error.streamError) {
|
|
5015
5118
|
return failed(
|
|
@@ -5103,7 +5206,7 @@ function protocolCode(error) {
|
|
|
5103
5206
|
return (error instanceof ProtocolError || error instanceof SseFramingError) && MODEL_FAILURE_CODES.includes(error.code) ? error.code : "protocol_invalid";
|
|
5104
5207
|
}
|
|
5105
5208
|
function withFailure(result, binding, phase, code, fields2 = {}) {
|
|
5106
|
-
if (!["provider_terminal_error", "provider_retryable_error", "policy_abort", "malformed_tool_call"].includes(result.kind) && !(result.kind === "output_limit" &&
|
|
5209
|
+
if (!["provider_terminal_error", "provider_retryable_error", "policy_abort", "malformed_tool_call"].includes(result.kind) && !(result.kind === "output_limit" && ["reasoning_repetition", "stream_frame_limit", "stream_output_limit"].includes(code)) && !(result.kind === "context_overflow" && fields2.gateway)) return result;
|
|
5107
5210
|
const protocol = ["openai-chat", "openai-responses", "anthropic-messages"].includes(binding?.protocol) ? binding.protocol : "unknown";
|
|
5108
5211
|
return { ...result, failureDiagnostic: { contract: fields2.gateway ? "model-failure-diagnostic/v4" : fields2.providerError ? "model-failure-diagnostic/v3" : fields2.usageConflict ? "model-failure-diagnostic/v2" : "model-failure-diagnostic/v1", protocol, phase, code, ...fields2 } };
|
|
5109
5212
|
}
|