@code-yeongyu/senpi-ai 2026.7.28 → 2026.7.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/api/openai-client-auth.d.ts +7 -0
- package/dist/api/openai-client-auth.d.ts.map +1 -0
- package/dist/api/openai-client-auth.js +15 -0
- package/dist/api/openai-client-auth.js.map +1 -0
- package/dist/api/openai-completions.d.ts.map +1 -1
- package/dist/api/openai-completions.js +4 -20
- package/dist/api/openai-completions.js.map +1 -1
- package/dist/api/openai-responses.d.ts.map +1 -1
- package/dist/api/openai-responses.js +9 -22
- package/dist/api/openai-responses.js.map +1 -1
- package/dist/auth/headers.d.ts +5 -0
- package/dist/auth/headers.d.ts.map +1 -0
- package/dist/auth/headers.js +41 -0
- package/dist/auth/headers.js.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/providers/data/.manifest.json +1 -1
- package/dist/providers/data/openrouter.json +1 -1
- package/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/dist/tool-call-middleware/context-transformer.d.ts.map +1 -1
- package/dist/tool-call-middleware/context-transformer.js +9 -0
- package/dist/tool-call-middleware/context-transformer.js.map +1 -1
- package/dist/tool-call-middleware/index.d.ts +5 -0
- package/dist/tool-call-middleware/index.d.ts.map +1 -1
- package/dist/tool-call-middleware/index.js +12 -2
- package/dist/tool-call-middleware/index.js.map +1 -1
- package/dist/tool-call-middleware/protocols/kimi-xtml/format.d.ts +5 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/format.d.ts.map +1 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/format.js +54 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/format.js.map +1 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/index.d.ts +4 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/index.d.ts.map +1 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/index.js +4 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/index.js.map +1 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/markers.d.ts +10 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/markers.d.ts.map +1 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/markers.js +30 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/markers.js.map +1 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/parse.d.ts +12 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/parse.d.ts.map +1 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/parse.js +95 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/parse.js.map +1 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/recovery-stream.d.ts +5 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/recovery-stream.d.ts.map +1 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/recovery-stream.js +207 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/recovery-stream.js.map +1 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/stream.d.ts +4 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/stream.d.ts.map +1 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/stream.js +181 -0
- package/dist/tool-call-middleware/protocols/kimi-xtml/stream.js.map +1 -0
- package/dist/tool-call-middleware/recovery-stream-wrapper.d.ts +2 -1
- package/dist/tool-call-middleware/recovery-stream-wrapper.d.ts.map +1 -1
- package/dist/tool-call-middleware/recovery-stream-wrapper.js +3 -2
- package/dist/tool-call-middleware/recovery-stream-wrapper.js.map +1 -1
- package/dist/tool-call-middleware/recovery-text-projection.d.ts +2 -1
- package/dist/tool-call-middleware/recovery-text-projection.d.ts.map +1 -1
- package/dist/tool-call-middleware/recovery-text-projection.js +2 -2
- package/dist/tool-call-middleware/recovery-text-projection.js.map +1 -1
- package/dist/tool-call-middleware/types.d.ts +2 -1
- package/dist/tool-call-middleware/types.d.ts.map +1 -1
- package/dist/tool-call-middleware/types.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/retry.d.ts +7 -0
- package/dist/utils/retry.d.ts.map +1 -1
- package/dist/utils/retry.js +29 -0
- package/dist/utils/retry.js.map +1 -1
- package/package.json +14 -14
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { getPartialXtmlSuffix, parseXtmlAttributes, XTML_ARGUMENT_CLOSE, XTML_ARGUMENT_OPEN, XTML_CALL_CLOSE, XTML_CALL_OPEN, XTML_SEP, XTML_TOOLS_CLOSE, XTML_TOOLS_OPEN, } from "./markers.js";
|
|
2
|
+
import { coerceXtmlArgumentValue } from "./parse.js";
|
|
3
|
+
const CHANNEL_MARKER_PATTERN = /^<\|(?:open|close)\|>[a-zA-Z_][a-zA-Z0-9_]*<\|sep\|>/;
|
|
4
|
+
const OPEN_PREFIX = "<|open|>";
|
|
5
|
+
const CLOSE_PREFIX = "<|close|>";
|
|
6
|
+
function couldBeMarkerPrefix(tail) {
|
|
7
|
+
if (OPEN_PREFIX.startsWith(tail) || CLOSE_PREFIX.startsWith(tail))
|
|
8
|
+
return true;
|
|
9
|
+
for (const prefix of [OPEN_PREFIX, CLOSE_PREFIX]) {
|
|
10
|
+
if (!tail.startsWith(prefix))
|
|
11
|
+
continue;
|
|
12
|
+
const rest = tail.slice(prefix.length);
|
|
13
|
+
const angleIndex = rest.indexOf("<");
|
|
14
|
+
if (angleIndex === -1)
|
|
15
|
+
return /^[a-zA-Z0-9_]*$/.test(rest);
|
|
16
|
+
if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(rest.slice(0, angleIndex)))
|
|
17
|
+
return false;
|
|
18
|
+
return XTML_SEP.startsWith(rest.slice(angleIndex));
|
|
19
|
+
}
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
export function createXtmlRecoveryStreamParser(tools, options) {
|
|
23
|
+
let buffer = "";
|
|
24
|
+
let mode = "text";
|
|
25
|
+
let callIndex = -1;
|
|
26
|
+
let callName = "";
|
|
27
|
+
let callArgs = {};
|
|
28
|
+
let callStarted = false;
|
|
29
|
+
let callInvalidReason = null;
|
|
30
|
+
let argumentKey = "";
|
|
31
|
+
let argumentType;
|
|
32
|
+
function resetCall() {
|
|
33
|
+
callName = "";
|
|
34
|
+
callArgs = {};
|
|
35
|
+
callStarted = false;
|
|
36
|
+
callInvalidReason = null;
|
|
37
|
+
argumentKey = "";
|
|
38
|
+
argumentType = undefined;
|
|
39
|
+
}
|
|
40
|
+
function endCall(events, incomplete) {
|
|
41
|
+
if (!callStarted) {
|
|
42
|
+
resetCall();
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const event = {
|
|
46
|
+
type: "toolcall_end",
|
|
47
|
+
index: callIndex,
|
|
48
|
+
name: callName,
|
|
49
|
+
id: `recovered-xtml-${callIndex}`,
|
|
50
|
+
arguments: callArgs,
|
|
51
|
+
};
|
|
52
|
+
if (incomplete) {
|
|
53
|
+
event.incomplete = true;
|
|
54
|
+
if (callInvalidReason)
|
|
55
|
+
event.errorMessage = callInvalidReason;
|
|
56
|
+
}
|
|
57
|
+
events.push(event);
|
|
58
|
+
resetCall();
|
|
59
|
+
mode = "tools";
|
|
60
|
+
}
|
|
61
|
+
function processText(events) {
|
|
62
|
+
const openIndex = buffer.indexOf(OPEN_PREFIX);
|
|
63
|
+
const closeIndex = buffer.indexOf(CLOSE_PREFIX);
|
|
64
|
+
const markerIndex = [openIndex, closeIndex].filter((index) => index !== -1).sort((a, b) => a - b)[0];
|
|
65
|
+
if (markerIndex === undefined) {
|
|
66
|
+
const partial = getPartialXtmlSuffix(buffer, [OPEN_PREFIX, CLOSE_PREFIX]);
|
|
67
|
+
const flushable = partial ? buffer.slice(0, -partial.length) : buffer;
|
|
68
|
+
if (flushable)
|
|
69
|
+
events.push({ type: "text", text: flushable });
|
|
70
|
+
buffer = partial;
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
if (markerIndex > 0) {
|
|
74
|
+
events.push({ type: "text", text: buffer.slice(0, markerIndex) });
|
|
75
|
+
buffer = buffer.slice(markerIndex);
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
const markerMatch = CHANNEL_MARKER_PATTERN.exec(buffer);
|
|
79
|
+
if (markerMatch) {
|
|
80
|
+
const marker = markerMatch[0];
|
|
81
|
+
buffer = buffer.slice(marker.length);
|
|
82
|
+
if (marker === XTML_TOOLS_OPEN)
|
|
83
|
+
mode = "tools";
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
if (couldBeMarkerPrefix(buffer))
|
|
87
|
+
return false;
|
|
88
|
+
events.push({ type: "text", text: buffer.slice(0, 2) });
|
|
89
|
+
buffer = buffer.slice(2);
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
function process(events) {
|
|
93
|
+
for (;;) {
|
|
94
|
+
if (mode === "text") {
|
|
95
|
+
if (!processText(events))
|
|
96
|
+
return;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (mode === "tools") {
|
|
100
|
+
const callStart = buffer.indexOf(XTML_CALL_OPEN);
|
|
101
|
+
const toolsEnd = buffer.indexOf(XTML_TOOLS_CLOSE);
|
|
102
|
+
if (toolsEnd !== -1 && (callStart === -1 || toolsEnd < callStart)) {
|
|
103
|
+
buffer = buffer.slice(toolsEnd + XTML_TOOLS_CLOSE.length);
|
|
104
|
+
mode = "text";
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (callStart === -1)
|
|
108
|
+
return;
|
|
109
|
+
buffer = buffer.slice(callStart + XTML_CALL_OPEN.length);
|
|
110
|
+
mode = "call-header";
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (mode === "call-header" || mode === "argument-header") {
|
|
114
|
+
const sepIndex = buffer.indexOf(XTML_SEP);
|
|
115
|
+
if (sepIndex === -1)
|
|
116
|
+
return;
|
|
117
|
+
const attributes = parseXtmlAttributes(buffer.slice(0, sepIndex));
|
|
118
|
+
buffer = buffer.slice(sepIndex + XTML_SEP.length);
|
|
119
|
+
if (mode === "call-header") {
|
|
120
|
+
const name = attributes.tool ?? "";
|
|
121
|
+
if (!tools.some((candidate) => candidate.name === name)) {
|
|
122
|
+
options?.onError?.(`kimi-xtml recovery: call for unknown tool "${name}".`, {});
|
|
123
|
+
mode = "discard-call";
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
callIndex += 1;
|
|
127
|
+
callName = name;
|
|
128
|
+
callStarted = true;
|
|
129
|
+
events.push({ type: "toolcall_start", index: callIndex, name, id: `recovered-xtml-${callIndex}` });
|
|
130
|
+
mode = "call-body";
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
argumentKey = attributes.key ?? "";
|
|
134
|
+
argumentType = attributes.type;
|
|
135
|
+
mode = "argument-value";
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (mode === "call-body") {
|
|
139
|
+
const argStart = buffer.indexOf(XTML_ARGUMENT_OPEN);
|
|
140
|
+
const callEnd = buffer.indexOf(XTML_CALL_CLOSE);
|
|
141
|
+
if (callEnd !== -1 && (argStart === -1 || callEnd < argStart)) {
|
|
142
|
+
buffer = buffer.slice(callEnd + XTML_CALL_CLOSE.length);
|
|
143
|
+
endCall(events, callInvalidReason !== null);
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (argStart === -1)
|
|
147
|
+
return;
|
|
148
|
+
buffer = buffer.slice(argStart + XTML_ARGUMENT_OPEN.length);
|
|
149
|
+
mode = "argument-header";
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (mode === "argument-value") {
|
|
153
|
+
const valueEnd = buffer.indexOf(XTML_ARGUMENT_CLOSE);
|
|
154
|
+
if (valueEnd === -1)
|
|
155
|
+
return;
|
|
156
|
+
const coerced = coerceXtmlArgumentValue(buffer.slice(0, valueEnd), argumentType);
|
|
157
|
+
buffer = buffer.slice(valueEnd + XTML_ARGUMENT_CLOSE.length);
|
|
158
|
+
if (!coerced.ok) {
|
|
159
|
+
callInvalidReason = `kimi-xtml recovery: invalid value for argument "${argumentKey}".`;
|
|
160
|
+
options?.onError?.(callInvalidReason, { toolCall: callName });
|
|
161
|
+
}
|
|
162
|
+
else if (argumentKey) {
|
|
163
|
+
callArgs[argumentKey] = coerced.value;
|
|
164
|
+
events.push({ type: "toolcall_delta", index: callIndex, argumentsDelta: JSON.stringify(callArgs) });
|
|
165
|
+
}
|
|
166
|
+
mode = "call-body";
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
const callEnd = buffer.indexOf(XTML_CALL_CLOSE);
|
|
170
|
+
if (callEnd === -1)
|
|
171
|
+
return;
|
|
172
|
+
buffer = buffer.slice(callEnd + XTML_CALL_CLOSE.length);
|
|
173
|
+
resetCall();
|
|
174
|
+
mode = "tools";
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return {
|
|
178
|
+
feed(textDelta) {
|
|
179
|
+
buffer += textDelta;
|
|
180
|
+
const events = [];
|
|
181
|
+
process(events);
|
|
182
|
+
return events;
|
|
183
|
+
},
|
|
184
|
+
interrupt() {
|
|
185
|
+
const events = [];
|
|
186
|
+
if (mode === "text" && buffer) {
|
|
187
|
+
events.push({ type: "text", text: buffer });
|
|
188
|
+
buffer = "";
|
|
189
|
+
}
|
|
190
|
+
return events;
|
|
191
|
+
},
|
|
192
|
+
finish() {
|
|
193
|
+
const events = [];
|
|
194
|
+
if (mode === "text") {
|
|
195
|
+
if (buffer)
|
|
196
|
+
events.push({ type: "text", text: buffer });
|
|
197
|
+
}
|
|
198
|
+
else if (callStarted) {
|
|
199
|
+
endCall(events, true);
|
|
200
|
+
}
|
|
201
|
+
buffer = "";
|
|
202
|
+
mode = "text";
|
|
203
|
+
return events;
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
//# sourceMappingURL=recovery-stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recovery-stream.js","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/kimi-xtml/recovery-stream.ts"],"names":[],"mappings":"AAGA,OAAO,EACN,oBAAoB,EACpB,mBAAmB,EACnB,mBAAmB,EACnB,kBAAkB,EAClB,eAAe,EACf,cAAc,EACd,QAAQ,EACR,gBAAgB,EAChB,eAAe,GACf,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC;AAWrD,MAAM,sBAAsB,GAAG,sDAAsD,CAAC;AACtF,MAAM,WAAW,GAAG,UAAU,CAAC;AAC/B,MAAM,YAAY,GAAG,WAAW,CAAC;AAEjC,SAAS,mBAAmB,CAAC,IAAY;IACxC,IAAI,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,YAAY,CAAC,UAAU,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IAC/E,KAAK,MAAM,MAAM,IAAI,CAAC,WAAW,EAAE,YAAY,CAAC,EAAE,CAAC;QAClD,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC;YAAE,SAAS;QACvC,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACvC,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACrC,IAAI,UAAU,KAAK,CAAC,CAAC;YAAE,OAAO,iBAAiB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC3D,IAAI,CAAC,0BAA0B,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC;YAAE,OAAO,KAAK,CAAC;QAC9E,OAAO,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC;IACpD,CAAC;IACD,OAAO,KAAK,CAAC;AACd,CAAC;AAED,MAAM,UAAU,8BAA8B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EAAE,CAAC,CAAC;YACzD,CAAC;iBAAM,IAAI,WAAW,EAAE,CAAC;gBACxB,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;YACvB,CAAC;YACD,MAAM,GAAG,EAAE,CAAC;YACZ,IAAI,GAAG,MAAM,CAAC;YACd,OAAO,MAAM,CAAC;QACf,CAAC;KACD,CAAC;AACH,CAAC","sourcesContent":["import type { Tool } from \"../../../types.ts\";\nimport type { ParserOptions, StreamParserEvent } from \"../../types.ts\";\nimport type { RecoveryStreamParser } from \"../anthropic-xml/recovery-stream.ts\";\nimport {\n\tgetPartialXtmlSuffix,\n\tparseXtmlAttributes,\n\tXTML_ARGUMENT_CLOSE,\n\tXTML_ARGUMENT_OPEN,\n\tXTML_CALL_CLOSE,\n\tXTML_CALL_OPEN,\n\tXTML_SEP,\n\tXTML_TOOLS_CLOSE,\n\tXTML_TOOLS_OPEN,\n} from \"./markers.ts\";\nimport { coerceXtmlArgumentValue } from \"./parse.ts\";\n\ntype ParserMode =\n\t| \"text\"\n\t| \"tools\"\n\t| \"call-header\"\n\t| \"call-body\"\n\t| \"argument-header\"\n\t| \"argument-value\"\n\t| \"discard-call\";\n\nconst CHANNEL_MARKER_PATTERN = /^<\\|(?:open|close)\\|>[a-zA-Z_][a-zA-Z0-9_]*<\\|sep\\|>/;\nconst OPEN_PREFIX = \"<|open|>\";\nconst CLOSE_PREFIX = \"<|close|>\";\n\nfunction couldBeMarkerPrefix(tail: string): boolean {\n\tif (OPEN_PREFIX.startsWith(tail) || CLOSE_PREFIX.startsWith(tail)) return true;\n\tfor (const prefix of [OPEN_PREFIX, CLOSE_PREFIX]) {\n\t\tif (!tail.startsWith(prefix)) continue;\n\t\tconst rest = tail.slice(prefix.length);\n\t\tconst angleIndex = rest.indexOf(\"<\");\n\t\tif (angleIndex === -1) return /^[a-zA-Z0-9_]*$/.test(rest);\n\t\tif (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(rest.slice(0, angleIndex))) return false;\n\t\treturn XTML_SEP.startsWith(rest.slice(angleIndex));\n\t}\n\treturn false;\n}\n\nexport function createXtmlRecoveryStreamParser(tools: readonly Tool[], options?: ParserOptions): RecoveryStreamParser {\n\tlet buffer = \"\";\n\tlet mode: ParserMode = \"text\";\n\tlet callIndex = -1;\n\tlet callName = \"\";\n\tlet callArgs: Record<string, unknown> = {};\n\tlet callStarted = false;\n\tlet callInvalidReason: string | null = null;\n\tlet argumentKey = \"\";\n\tlet argumentType: string | undefined;\n\n\tfunction resetCall(): void {\n\t\tcallName = \"\";\n\t\tcallArgs = {};\n\t\tcallStarted = false;\n\t\tcallInvalidReason = null;\n\t\targumentKey = \"\";\n\t\targumentType = undefined;\n\t}\n\n\tfunction endCall(events: StreamParserEvent[], incomplete: boolean): void {\n\t\tif (!callStarted) {\n\t\t\tresetCall();\n\t\t\treturn;\n\t\t}\n\t\tconst event: StreamParserEvent = {\n\t\t\ttype: \"toolcall_end\",\n\t\t\tindex: callIndex,\n\t\t\tname: callName,\n\t\t\tid: `recovered-xtml-${callIndex}`,\n\t\t\targuments: callArgs,\n\t\t};\n\t\tif (incomplete) {\n\t\t\tevent.incomplete = true;\n\t\t\tif (callInvalidReason) event.errorMessage = callInvalidReason;\n\t\t}\n\t\tevents.push(event);\n\t\tresetCall();\n\t\tmode = \"tools\";\n\t}\n\n\tfunction processText(events: StreamParserEvent[]): boolean {\n\t\tconst openIndex = buffer.indexOf(OPEN_PREFIX);\n\t\tconst closeIndex = buffer.indexOf(CLOSE_PREFIX);\n\t\tconst markerIndex = [openIndex, closeIndex].filter((index) => index !== -1).sort((a, b) => a - b)[0];\n\t\tif (markerIndex === undefined) {\n\t\t\tconst partial = getPartialXtmlSuffix(buffer, [OPEN_PREFIX, CLOSE_PREFIX]);\n\t\t\tconst flushable = partial ? buffer.slice(0, -partial.length) : buffer;\n\t\t\tif (flushable) events.push({ type: \"text\", text: flushable });\n\t\t\tbuffer = partial;\n\t\t\treturn false;\n\t\t}\n\t\tif (markerIndex > 0) {\n\t\t\tevents.push({ type: \"text\", text: buffer.slice(0, markerIndex) });\n\t\t\tbuffer = buffer.slice(markerIndex);\n\t\t\treturn true;\n\t\t}\n\t\tconst markerMatch = CHANNEL_MARKER_PATTERN.exec(buffer);\n\t\tif (markerMatch) {\n\t\t\tconst marker = markerMatch[0];\n\t\t\tbuffer = buffer.slice(marker.length);\n\t\t\tif (marker === XTML_TOOLS_OPEN) mode = \"tools\";\n\t\t\treturn true;\n\t\t}\n\t\tif (couldBeMarkerPrefix(buffer)) return false;\n\t\tevents.push({ type: \"text\", text: buffer.slice(0, 2) });\n\t\tbuffer = buffer.slice(2);\n\t\treturn true;\n\t}\n\n\tfunction process(events: StreamParserEvent[]): void {\n\t\tfor (;;) {\n\t\t\tif (mode === \"text\") {\n\t\t\t\tif (!processText(events)) return;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (mode === \"tools\") {\n\t\t\t\tconst callStart = buffer.indexOf(XTML_CALL_OPEN);\n\t\t\t\tconst toolsEnd = buffer.indexOf(XTML_TOOLS_CLOSE);\n\t\t\t\tif (toolsEnd !== -1 && (callStart === -1 || toolsEnd < callStart)) {\n\t\t\t\t\tbuffer = buffer.slice(toolsEnd + XTML_TOOLS_CLOSE.length);\n\t\t\t\t\tmode = \"text\";\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (callStart === -1) return;\n\t\t\t\tbuffer = buffer.slice(callStart + XTML_CALL_OPEN.length);\n\t\t\t\tmode = \"call-header\";\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (mode === \"call-header\" || mode === \"argument-header\") {\n\t\t\t\tconst sepIndex = buffer.indexOf(XTML_SEP);\n\t\t\t\tif (sepIndex === -1) return;\n\t\t\t\tconst attributes = parseXtmlAttributes(buffer.slice(0, sepIndex));\n\t\t\t\tbuffer = buffer.slice(sepIndex + XTML_SEP.length);\n\t\t\t\tif (mode === \"call-header\") {\n\t\t\t\t\tconst name = attributes.tool ?? \"\";\n\t\t\t\t\tif (!tools.some((candidate) => candidate.name === name)) {\n\t\t\t\t\t\toptions?.onError?.(`kimi-xtml recovery: call for unknown tool \"${name}\".`, {});\n\t\t\t\t\t\tmode = \"discard-call\";\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tcallIndex += 1;\n\t\t\t\t\tcallName = name;\n\t\t\t\t\tcallStarted = true;\n\t\t\t\t\tevents.push({ type: \"toolcall_start\", index: callIndex, name, id: `recovered-xtml-${callIndex}` });\n\t\t\t\t\tmode = \"call-body\";\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\targumentKey = attributes.key ?? \"\";\n\t\t\t\targumentType = attributes.type;\n\t\t\t\tmode = \"argument-value\";\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (mode === \"call-body\") {\n\t\t\t\tconst argStart = buffer.indexOf(XTML_ARGUMENT_OPEN);\n\t\t\t\tconst callEnd = buffer.indexOf(XTML_CALL_CLOSE);\n\t\t\t\tif (callEnd !== -1 && (argStart === -1 || callEnd < argStart)) {\n\t\t\t\t\tbuffer = buffer.slice(callEnd + XTML_CALL_CLOSE.length);\n\t\t\t\t\tendCall(events, callInvalidReason !== null);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (argStart === -1) return;\n\t\t\t\tbuffer = buffer.slice(argStart + XTML_ARGUMENT_OPEN.length);\n\t\t\t\tmode = \"argument-header\";\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (mode === \"argument-value\") {\n\t\t\t\tconst valueEnd = buffer.indexOf(XTML_ARGUMENT_CLOSE);\n\t\t\t\tif (valueEnd === -1) return;\n\t\t\t\tconst coerced = coerceXtmlArgumentValue(buffer.slice(0, valueEnd), argumentType);\n\t\t\t\tbuffer = buffer.slice(valueEnd + XTML_ARGUMENT_CLOSE.length);\n\t\t\t\tif (!coerced.ok) {\n\t\t\t\t\tcallInvalidReason = `kimi-xtml recovery: invalid value for argument \"${argumentKey}\".`;\n\t\t\t\t\toptions?.onError?.(callInvalidReason, { toolCall: callName });\n\t\t\t\t} else if (argumentKey) {\n\t\t\t\t\tcallArgs[argumentKey] = coerced.value;\n\t\t\t\t\tevents.push({ type: \"toolcall_delta\", index: callIndex, argumentsDelta: JSON.stringify(callArgs) });\n\t\t\t\t}\n\t\t\t\tmode = \"call-body\";\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst callEnd = buffer.indexOf(XTML_CALL_CLOSE);\n\t\t\tif (callEnd === -1) return;\n\t\t\tbuffer = buffer.slice(callEnd + XTML_CALL_CLOSE.length);\n\t\t\tresetCall();\n\t\t\tmode = \"tools\";\n\t\t}\n\t}\n\n\treturn {\n\t\tfeed(textDelta: string): StreamParserEvent[] {\n\t\t\tbuffer += textDelta;\n\t\t\tconst events: StreamParserEvent[] = [];\n\t\t\tprocess(events);\n\t\t\treturn events;\n\t\t},\n\t\tinterrupt(): StreamParserEvent[] {\n\t\t\tconst events: StreamParserEvent[] = [];\n\t\t\tif (mode === \"text\" && buffer) {\n\t\t\t\tevents.push({ type: \"text\", text: buffer });\n\t\t\t\tbuffer = \"\";\n\t\t\t}\n\t\t\treturn events;\n\t\t},\n\t\tfinish(): StreamParserEvent[] {\n\t\t\tconst events: StreamParserEvent[] = [];\n\t\t\tif (mode === \"text\") {\n\t\t\t\tif (buffer) events.push({ type: \"text\", text: buffer });\n\t\t\t} else if (callStarted) {\n\t\t\t\tendCall(events, true);\n\t\t\t}\n\t\t\tbuffer = \"\";\n\t\t\tmode = \"text\";\n\t\t\treturn events;\n\t\t},\n\t};\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/kimi-xtml/stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAqB,MAAM,gBAAgB,CAAC;AA0BrF,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,YAAY,CA8K/F"}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { getPartialXtmlSuffix, parseXtmlAttributes, XTML_ARGUMENT_CLOSE, XTML_ARGUMENT_OPEN, XTML_CALL_CLOSE, XTML_CALL_OPEN, XTML_SEP, XTML_TOOLS_CLOSE, XTML_TOOLS_OPEN, } from "./markers.js";
|
|
2
|
+
import { coerceXtmlArgumentValue } from "./parse.js";
|
|
3
|
+
const TEXT_BOUNDARY_TOKENS = [XTML_TOOLS_OPEN];
|
|
4
|
+
const TOOLS_BOUNDARY_TOKENS = [XTML_CALL_OPEN, XTML_TOOLS_CLOSE];
|
|
5
|
+
export function createKimiXtmlStreamParser(tools, options) {
|
|
6
|
+
let buffer = "";
|
|
7
|
+
let mode = "text";
|
|
8
|
+
let callIndex = -1;
|
|
9
|
+
let callName = "";
|
|
10
|
+
let callArgs = {};
|
|
11
|
+
let callStarted = false;
|
|
12
|
+
let callInvalidReason = null;
|
|
13
|
+
let argumentKey = "";
|
|
14
|
+
let argumentType;
|
|
15
|
+
function resetCall() {
|
|
16
|
+
callName = "";
|
|
17
|
+
callArgs = {};
|
|
18
|
+
callStarted = false;
|
|
19
|
+
callInvalidReason = null;
|
|
20
|
+
argumentKey = "";
|
|
21
|
+
argumentType = undefined;
|
|
22
|
+
}
|
|
23
|
+
function startCall(events, name) {
|
|
24
|
+
callIndex += 1;
|
|
25
|
+
callName = name;
|
|
26
|
+
callStarted = true;
|
|
27
|
+
events.push({ type: "toolcall_start", index: callIndex, name, id: `kimi-xtml-tool-${callIndex}` });
|
|
28
|
+
}
|
|
29
|
+
function endCall(events, incomplete) {
|
|
30
|
+
if (!callStarted) {
|
|
31
|
+
resetCall();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const event = {
|
|
35
|
+
type: "toolcall_end",
|
|
36
|
+
index: callIndex,
|
|
37
|
+
name: callName,
|
|
38
|
+
id: `kimi-xtml-tool-${callIndex}`,
|
|
39
|
+
arguments: callArgs,
|
|
40
|
+
};
|
|
41
|
+
if (incomplete) {
|
|
42
|
+
event.incomplete = true;
|
|
43
|
+
if (callInvalidReason)
|
|
44
|
+
event.errorMessage = callInvalidReason;
|
|
45
|
+
}
|
|
46
|
+
events.push(event);
|
|
47
|
+
resetCall();
|
|
48
|
+
mode = "tools";
|
|
49
|
+
}
|
|
50
|
+
function hold(events, tokens, flushAsText) {
|
|
51
|
+
const partial = getPartialXtmlSuffix(buffer, tokens);
|
|
52
|
+
if (!flushAsText)
|
|
53
|
+
return;
|
|
54
|
+
const flushable = partial ? buffer.slice(0, -partial.length) : buffer;
|
|
55
|
+
if (flushable)
|
|
56
|
+
events.push({ type: "text", text: flushable });
|
|
57
|
+
buffer = partial;
|
|
58
|
+
}
|
|
59
|
+
function process(events) {
|
|
60
|
+
for (;;) {
|
|
61
|
+
if (mode === "text") {
|
|
62
|
+
const start = buffer.indexOf(XTML_TOOLS_OPEN);
|
|
63
|
+
if (start === -1) {
|
|
64
|
+
hold(events, TEXT_BOUNDARY_TOKENS, true);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (start > 0)
|
|
68
|
+
events.push({ type: "text", text: buffer.slice(0, start) });
|
|
69
|
+
buffer = buffer.slice(start + XTML_TOOLS_OPEN.length);
|
|
70
|
+
mode = "tools";
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (mode === "tools") {
|
|
74
|
+
const callStart = buffer.indexOf(XTML_CALL_OPEN);
|
|
75
|
+
const toolsEnd = buffer.indexOf(XTML_TOOLS_CLOSE);
|
|
76
|
+
if (toolsEnd !== -1 && (callStart === -1 || toolsEnd < callStart)) {
|
|
77
|
+
buffer = buffer.slice(toolsEnd + XTML_TOOLS_CLOSE.length);
|
|
78
|
+
mode = "text";
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (callStart === -1) {
|
|
82
|
+
hold(events, TOOLS_BOUNDARY_TOKENS, false);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
buffer = buffer.slice(callStart + XTML_CALL_OPEN.length);
|
|
86
|
+
mode = "call-header";
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (mode === "call-header" || mode === "argument-header") {
|
|
90
|
+
const sepIndex = buffer.indexOf(XTML_SEP);
|
|
91
|
+
if (sepIndex === -1) {
|
|
92
|
+
hold(events, [XTML_SEP], false);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const attributes = parseXtmlAttributes(buffer.slice(0, sepIndex));
|
|
96
|
+
buffer = buffer.slice(sepIndex + XTML_SEP.length);
|
|
97
|
+
if (mode === "call-header") {
|
|
98
|
+
const name = attributes.tool ?? "";
|
|
99
|
+
if (!tools.some((candidate) => candidate.name === name)) {
|
|
100
|
+
options?.onError?.(`kimi-xtml: call for unknown tool "${name}".`, {});
|
|
101
|
+
mode = "discard-call";
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
startCall(events, name);
|
|
105
|
+
mode = "call-body";
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
argumentKey = attributes.key ?? "";
|
|
109
|
+
argumentType = attributes.type;
|
|
110
|
+
mode = "argument-value";
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (mode === "call-body") {
|
|
114
|
+
const argStart = buffer.indexOf(XTML_ARGUMENT_OPEN);
|
|
115
|
+
const callEnd = buffer.indexOf(XTML_CALL_CLOSE);
|
|
116
|
+
if (callEnd !== -1 && (argStart === -1 || callEnd < argStart)) {
|
|
117
|
+
buffer = buffer.slice(callEnd + XTML_CALL_CLOSE.length);
|
|
118
|
+
endCall(events, callInvalidReason !== null);
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (argStart === -1) {
|
|
122
|
+
hold(events, [XTML_ARGUMENT_OPEN, XTML_CALL_CLOSE], false);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
buffer = buffer.slice(argStart + XTML_ARGUMENT_OPEN.length);
|
|
126
|
+
mode = "argument-header";
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (mode === "argument-value") {
|
|
130
|
+
const valueEnd = buffer.indexOf(XTML_ARGUMENT_CLOSE);
|
|
131
|
+
if (valueEnd === -1) {
|
|
132
|
+
hold(events, [XTML_ARGUMENT_CLOSE], false);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const coerced = coerceXtmlArgumentValue(buffer.slice(0, valueEnd), argumentType);
|
|
136
|
+
buffer = buffer.slice(valueEnd + XTML_ARGUMENT_CLOSE.length);
|
|
137
|
+
if (!coerced.ok) {
|
|
138
|
+
callInvalidReason = `kimi-xtml: invalid value for argument "${argumentKey}".`;
|
|
139
|
+
options?.onError?.(callInvalidReason, { toolCall: callName });
|
|
140
|
+
}
|
|
141
|
+
else if (argumentKey) {
|
|
142
|
+
callArgs[argumentKey] = coerced.value;
|
|
143
|
+
events.push({ type: "toolcall_delta", index: callIndex, argumentsDelta: JSON.stringify(callArgs) });
|
|
144
|
+
}
|
|
145
|
+
mode = "call-body";
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
const callEnd = buffer.indexOf(XTML_CALL_CLOSE);
|
|
149
|
+
if (callEnd === -1) {
|
|
150
|
+
hold(events, [XTML_CALL_CLOSE], false);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
buffer = buffer.slice(callEnd + XTML_CALL_CLOSE.length);
|
|
154
|
+
resetCall();
|
|
155
|
+
mode = "tools";
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
feed(textDelta) {
|
|
160
|
+
buffer += textDelta;
|
|
161
|
+
const events = [];
|
|
162
|
+
process(events);
|
|
163
|
+
return events;
|
|
164
|
+
},
|
|
165
|
+
finish() {
|
|
166
|
+
const events = [];
|
|
167
|
+
if (mode === "text") {
|
|
168
|
+
if (buffer)
|
|
169
|
+
events.push({ type: "text", text: buffer });
|
|
170
|
+
buffer = "";
|
|
171
|
+
return events;
|
|
172
|
+
}
|
|
173
|
+
if (callStarted)
|
|
174
|
+
endCall(events, true);
|
|
175
|
+
buffer = "";
|
|
176
|
+
mode = "text";
|
|
177
|
+
return events;
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
//# sourceMappingURL=stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/kimi-xtml/stream.ts"],"names":[],"mappings":"AAEA,OAAO,EACN,oBAAoB,EACpB,mBAAmB,EACnB,mBAAmB,EACnB,kBAAkB,EAClB,eAAe,EACf,cAAc,EACd,QAAQ,EACR,gBAAgB,EAChB,eAAe,GACf,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC;AAWrD,MAAM,oBAAoB,GAAG,CAAC,eAAe,CAAU,CAAC;AACxD,MAAM,qBAAqB,GAAG,CAAC,cAAc,EAAE,gBAAgB,CAAU,CAAC;AAE1E,MAAM,UAAU,0BAA0B,CAAC,KAAa,EAAE,OAAuB;IAChF,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,IAAI,IAAI,GAAe,MAAM,CAAC;IAC9B,IAAI,SAAS,GAAG,CAAC,CAAC,CAAC;IACnB,IAAI,QAAQ,GAAG,EAAE,CAAC;IAClB,IAAI,QAAQ,GAA4B,EAAE,CAAC;IAC3C,IAAI,WAAW,GAAG,KAAK,CAAC;IACxB,IAAI,iBAAiB,GAAkB,IAAI,CAAC;IAC5C,IAAI,WAAW,GAAG,EAAE,CAAC;IACrB,IAAI,YAAgC,CAAC;IAErC,SAAS,SAAS;QACjB,QAAQ,GAAG,EAAE,CAAC;QACd,QAAQ,GAAG,EAAE,CAAC;QACd,WAAW,GAAG,KAAK,CAAC;QACpB,iB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AAC;QACf,CAAC;QACD,MAAM;YACL,MAAM,MAAM,GAAwB,EAAE,CAAC;YACvC,IAAI,IAAI,KAAK,MAAM,EAAE,CAAC;gBACrB,IAAI,MAAM;oBAAE,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;gBACxD,MAAM,GAAG,EAAE,CAAC;gBACZ,OAAO,MAAM,CAAC;YACf,CAAC;YACD,IAAI,WAAW;gBAAE,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;YACvC,MAAM,GAAG,EAAE,CAAC;YACZ,IAAI,GAAG,MAAM,CAAC;YACd,OAAO,MAAM,CAAC;QACf,CAAC;KACD,CAAC;AACH,CAAC","sourcesContent":["import type { Tool } from \"../../../types.ts\";\nimport type { ParserOptions, StreamParser, StreamParserEvent } from \"../../types.ts\";\nimport {\n\tgetPartialXtmlSuffix,\n\tparseXtmlAttributes,\n\tXTML_ARGUMENT_CLOSE,\n\tXTML_ARGUMENT_OPEN,\n\tXTML_CALL_CLOSE,\n\tXTML_CALL_OPEN,\n\tXTML_SEP,\n\tXTML_TOOLS_CLOSE,\n\tXTML_TOOLS_OPEN,\n} from \"./markers.ts\";\nimport { coerceXtmlArgumentValue } from \"./parse.ts\";\n\ntype ParserMode =\n\t| \"text\"\n\t| \"tools\"\n\t| \"call-header\"\n\t| \"call-body\"\n\t| \"argument-header\"\n\t| \"argument-value\"\n\t| \"discard-call\";\n\nconst TEXT_BOUNDARY_TOKENS = [XTML_TOOLS_OPEN] as const;\nconst TOOLS_BOUNDARY_TOKENS = [XTML_CALL_OPEN, XTML_TOOLS_CLOSE] as const;\n\nexport function createKimiXtmlStreamParser(tools: Tool[], options?: ParserOptions): StreamParser {\n\tlet buffer = \"\";\n\tlet mode: ParserMode = \"text\";\n\tlet callIndex = -1;\n\tlet callName = \"\";\n\tlet callArgs: Record<string, unknown> = {};\n\tlet callStarted = false;\n\tlet callInvalidReason: string | null = null;\n\tlet argumentKey = \"\";\n\tlet argumentType: string | undefined;\n\n\tfunction resetCall(): void {\n\t\tcallName = \"\";\n\t\tcallArgs = {};\n\t\tcallStarted = false;\n\t\tcallInvalidReason = null;\n\t\targumentKey = \"\";\n\t\targumentType = undefined;\n\t}\n\n\tfunction startCall(events: StreamParserEvent[], name: string): void {\n\t\tcallIndex += 1;\n\t\tcallName = name;\n\t\tcallStarted = true;\n\t\tevents.push({ type: \"toolcall_start\", index: callIndex, name, id: `kimi-xtml-tool-${callIndex}` });\n\t}\n\n\tfunction endCall(events: StreamParserEvent[], incomplete: boolean): void {\n\t\tif (!callStarted) {\n\t\t\tresetCall();\n\t\t\treturn;\n\t\t}\n\t\tconst event: StreamParserEvent = {\n\t\t\ttype: \"toolcall_end\",\n\t\t\tindex: callIndex,\n\t\t\tname: callName,\n\t\t\tid: `kimi-xtml-tool-${callIndex}`,\n\t\t\targuments: callArgs,\n\t\t};\n\t\tif (incomplete) {\n\t\t\tevent.incomplete = true;\n\t\t\tif (callInvalidReason) event.errorMessage = callInvalidReason;\n\t\t}\n\t\tevents.push(event);\n\t\tresetCall();\n\t\tmode = \"tools\";\n\t}\n\n\tfunction hold(events: StreamParserEvent[], tokens: readonly string[], flushAsText: boolean): void {\n\t\tconst partial = getPartialXtmlSuffix(buffer, tokens);\n\t\tif (!flushAsText) return;\n\t\tconst flushable = partial ? buffer.slice(0, -partial.length) : buffer;\n\t\tif (flushable) events.push({ type: \"text\", text: flushable });\n\t\tbuffer = partial;\n\t}\n\n\tfunction process(events: StreamParserEvent[]): void {\n\t\tfor (;;) {\n\t\t\tif (mode === \"text\") {\n\t\t\t\tconst start = buffer.indexOf(XTML_TOOLS_OPEN);\n\t\t\t\tif (start === -1) {\n\t\t\t\t\thold(events, TEXT_BOUNDARY_TOKENS, true);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif (start > 0) events.push({ type: \"text\", text: buffer.slice(0, start) });\n\t\t\t\tbuffer = buffer.slice(start + XTML_TOOLS_OPEN.length);\n\t\t\t\tmode = \"tools\";\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (mode === \"tools\") {\n\t\t\t\tconst callStart = buffer.indexOf(XTML_CALL_OPEN);\n\t\t\t\tconst toolsEnd = buffer.indexOf(XTML_TOOLS_CLOSE);\n\t\t\t\tif (toolsEnd !== -1 && (callStart === -1 || toolsEnd < callStart)) {\n\t\t\t\t\tbuffer = buffer.slice(toolsEnd + XTML_TOOLS_CLOSE.length);\n\t\t\t\t\tmode = \"text\";\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (callStart === -1) {\n\t\t\t\t\thold(events, TOOLS_BOUNDARY_TOKENS, false);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tbuffer = buffer.slice(callStart + XTML_CALL_OPEN.length);\n\t\t\t\tmode = \"call-header\";\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (mode === \"call-header\" || mode === \"argument-header\") {\n\t\t\t\tconst sepIndex = buffer.indexOf(XTML_SEP);\n\t\t\t\tif (sepIndex === -1) {\n\t\t\t\t\thold(events, [XTML_SEP], false);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst attributes = parseXtmlAttributes(buffer.slice(0, sepIndex));\n\t\t\t\tbuffer = buffer.slice(sepIndex + XTML_SEP.length);\n\t\t\t\tif (mode === \"call-header\") {\n\t\t\t\t\tconst name = attributes.tool ?? \"\";\n\t\t\t\t\tif (!tools.some((candidate) => candidate.name === name)) {\n\t\t\t\t\t\toptions?.onError?.(`kimi-xtml: call for unknown tool \"${name}\".`, {});\n\t\t\t\t\t\tmode = \"discard-call\";\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tstartCall(events, name);\n\t\t\t\t\tmode = \"call-body\";\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\targumentKey = attributes.key ?? \"\";\n\t\t\t\targumentType = attributes.type;\n\t\t\t\tmode = \"argument-value\";\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (mode === \"call-body\") {\n\t\t\t\tconst argStart = buffer.indexOf(XTML_ARGUMENT_OPEN);\n\t\t\t\tconst callEnd = buffer.indexOf(XTML_CALL_CLOSE);\n\t\t\t\tif (callEnd !== -1 && (argStart === -1 || callEnd < argStart)) {\n\t\t\t\t\tbuffer = buffer.slice(callEnd + XTML_CALL_CLOSE.length);\n\t\t\t\t\tendCall(events, callInvalidReason !== null);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (argStart === -1) {\n\t\t\t\t\thold(events, [XTML_ARGUMENT_OPEN, XTML_CALL_CLOSE], false);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tbuffer = buffer.slice(argStart + XTML_ARGUMENT_OPEN.length);\n\t\t\t\tmode = \"argument-header\";\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (mode === \"argument-value\") {\n\t\t\t\tconst valueEnd = buffer.indexOf(XTML_ARGUMENT_CLOSE);\n\t\t\t\tif (valueEnd === -1) {\n\t\t\t\t\thold(events, [XTML_ARGUMENT_CLOSE], false);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tconst coerced = coerceXtmlArgumentValue(buffer.slice(0, valueEnd), argumentType);\n\t\t\t\tbuffer = buffer.slice(valueEnd + XTML_ARGUMENT_CLOSE.length);\n\t\t\t\tif (!coerced.ok) {\n\t\t\t\t\tcallInvalidReason = `kimi-xtml: invalid value for argument \"${argumentKey}\".`;\n\t\t\t\t\toptions?.onError?.(callInvalidReason, { toolCall: callName });\n\t\t\t\t} else if (argumentKey) {\n\t\t\t\t\tcallArgs[argumentKey] = coerced.value;\n\t\t\t\t\tevents.push({ type: \"toolcall_delta\", index: callIndex, argumentsDelta: JSON.stringify(callArgs) });\n\t\t\t\t}\n\t\t\t\tmode = \"call-body\";\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst callEnd = buffer.indexOf(XTML_CALL_CLOSE);\n\t\t\tif (callEnd === -1) {\n\t\t\t\thold(events, [XTML_CALL_CLOSE], false);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tbuffer = buffer.slice(callEnd + XTML_CALL_CLOSE.length);\n\t\t\tresetCall();\n\t\t\tmode = \"tools\";\n\t\t}\n\t}\n\n\treturn {\n\t\tfeed(textDelta: string): StreamParserEvent[] {\n\t\t\tbuffer += textDelta;\n\t\t\tconst events: StreamParserEvent[] = [];\n\t\t\tprocess(events);\n\t\t\treturn events;\n\t\t},\n\t\tfinish(): StreamParserEvent[] {\n\t\t\tconst events: StreamParserEvent[] = [];\n\t\t\tif (mode === \"text\") {\n\t\t\t\tif (buffer) events.push({ type: \"text\", text: buffer });\n\t\t\t\tbuffer = \"\";\n\t\t\t\treturn events;\n\t\t\t}\n\t\t\tif (callStarted) endCall(events, true);\n\t\t\tbuffer = \"\";\n\t\t\tmode = \"text\";\n\t\t\treturn events;\n\t\t},\n\t};\n}\n"]}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import type { AssistantMessageEventStream, Tool } from "../types.ts";
|
|
2
|
-
|
|
2
|
+
import type { RecoveryStreamParser } from "./protocols/anthropic-xml/recovery-stream.ts";
|
|
3
|
+
export declare function wrapStreamWithInvokeRecovery(innerStream: AssistantMessageEventStream, tools: readonly Tool[], createParser?: (tools: readonly Tool[]) => RecoveryStreamParser): AssistantMessageEventStream;
|
|
3
4
|
//# sourceMappingURL=recovery-stream-wrapper.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"recovery-stream-wrapper.d.ts","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-stream-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA2C,2BAA2B,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"recovery-stream-wrapper.d.ts","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-stream-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA2C,2BAA2B,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAC9G,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,8CAA8C,CAAC;AAUzF,wBAAgB,4BAA4B,CAC3C,WAAW,EAAE,2BAA2B,EACxC,KAAK,EAAE,SAAS,IAAI,EAAE,EACtB,YAAY,GAAE,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,KAAK,oBAA4D,GACpG,2BAA2B,CA8N7B"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createAntmlInvokeRecoveryStreamParser } from "./protocols/antml/recovery-stream.js";
|
|
1
2
|
import { RecoveryContentLifecycle } from "./recovery-content-lifecycle.js";
|
|
2
3
|
import { RecoveryAssistantMessageEventStream } from "./recovery-event-stream.js";
|
|
3
4
|
import { RecoveryNativeProjection } from "./recovery-native-projection.js";
|
|
@@ -5,7 +6,7 @@ import { terminateRecoveryStreamForFailure } from "./recovery-stream-failure.js"
|
|
|
5
6
|
import { RecoveryStreamTerminal } from "./recovery-stream-terminal.js";
|
|
6
7
|
import { RecoveryTextProjection } from "./recovery-text-projection.js";
|
|
7
8
|
import { StreamMessageProjection } from "./stream-wrapper-shared.js";
|
|
8
|
-
export function wrapStreamWithInvokeRecovery(innerStream, tools) {
|
|
9
|
+
export function wrapStreamWithInvokeRecovery(innerStream, tools, createParser = createAntmlInvokeRecoveryStreamParser) {
|
|
9
10
|
const outerStream = new RecoveryAssistantMessageEventStream();
|
|
10
11
|
void (async () => {
|
|
11
12
|
const innerIterator = innerStream[Symbol.asyncIterator]();
|
|
@@ -105,7 +106,7 @@ export function wrapStreamWithInvokeRecovery(innerStream, tools) {
|
|
|
105
106
|
return;
|
|
106
107
|
if (!prepareContentEvent(event.partial, event.contentIndex) || !projection || !nativeProjection)
|
|
107
108
|
return;
|
|
108
|
-
const nextText = new RecoveryTextProjection(tools, projection, nativeProjection, event.contentIndex);
|
|
109
|
+
const nextText = new RecoveryTextProjection(tools, projection, nativeProjection, event.contentIndex, createParser);
|
|
109
110
|
if (!nextText.start(event.partial)) {
|
|
110
111
|
terminateForFailure(event.partial, "invalid_native_event_order");
|
|
111
112
|
return;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"recovery-stream-wrapper.js","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-stream-wrapper.ts"],"names":[],"mappings":"AACA,OAAO,EAA4B,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AACrG,OAAO,EAAE,mCAAmC,EAAE,MAAM,4BAA4B,CAAC;AACjF,OAAO,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAC3E,OAAO,EAA8B,iCAAiC,EAAE,MAAM,8BAA8B,CAAC;AAC7G,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AAErE,MAAM,UAAU,4BAA4B,CAC3C,WAAwC,EACxC,KAAsB;IAEtB,MAAM,WAAW,GAAG,IAAI,mCAAmC,EAAE,CAAC;IAE9D,KAAK,CAAC,KAAK,IAAmB,EAAE;QAC/B,MAAM,aAAa,GAAG,WAAW,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,CAAC;QAC1D,MAAM,WAAW,GAAyC,EAAE,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,GAAG,EAAE,CAAC,aAAa,EAAE,CAAC;QAC1G,IAAI,UAAU,GAAmC,IAAI,CAAC;QACtD,IAAI,gBAAgB,GAAoC,IAAI,CAAC;QAC7D,IAAI,cAAc,GAAkC,IAAI,CAAC;QACzD,IAAI,WAAW,GAAG,KAAK,CAAC;QACxB,IAAI,qBAAqB,GAAG,KAAK,CAAC;QAClC,MAAM,gBAAgB,GAAG,IAAI,wBAAwB,EAAE,CAAC;QACxD,MAAM,QAAQ,GAAG,IAAI,sBAAsB,CAAC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AAK,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;wBAChF,MAAM;oBACP,KAAK,cAAc;wBAClB,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,YAAY,EAAE,UAAU,CAAC;4BAAE,OAAO;wBACrE,IAAI,CAAC,mBAAmB,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC,UAAU;4BAAE,OAAO;wBACnF,UAAU,CAAC,cAAc,CAAC,KAAK,CAAC,YAAY,EAAE,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;wBAC5E,gBAAgB,CAAC,GAAG,CAAC,KAAK,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC;wBACrD,MAAM;gBACR,CAAC;YACF,CAAC;YAED,IAAI,qBAAqB,IAAI,CAAC,WAAW,CAAC,gBAAgB,EAAE;gBAAE,OAAO;YACrE,UAAU,EAAE,CAAC;YACb,QAAQ,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;QAChC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,IAAI,qBAAqB,IAAI,CAAC,WAAW,CAAC,gBAAgB,EAAE;gBAAE,OAAO;YACrE,UAAU,EAAE,CAAC;YACb,QAAQ,CAAC,eAAe,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QAC7C,CAAC;IACF,CAAC,CAAC,EAAE,CAAC;IAEL,OAAO,WAAW,CAAC;AACpB,CAAC","sourcesContent":["import type { AssistantMessage, AssistantMessageEvent, AssistantMessageEventStream, Tool } from \"../types.ts\";\nimport { type RecoveryContentKind, RecoveryContentLifecycle } from \"./recovery-content-lifecycle.ts\";\nimport { RecoveryAssistantMessageEventStream } from \"./recovery-event-stream.ts\";\nimport { RecoveryNativeProjection } from \"./recovery-native-projection.ts\";\nimport { type RecoveryStreamFailure, terminateRecoveryStreamForFailure } from \"./recovery-stream-failure.ts\";\nimport { RecoveryStreamTerminal } from \"./recovery-stream-terminal.ts\";\nimport { RecoveryTextProjection } from \"./recovery-text-projection.ts\";\nimport { StreamMessageProjection } from \"./stream-wrapper-shared.ts\";\n\nexport function wrapStreamWithInvokeRecovery(\n\tinnerStream: AssistantMessageEventStream,\n\ttools: readonly Tool[],\n): AssistantMessageEventStream {\n\tconst outerStream = new RecoveryAssistantMessageEventStream();\n\n\tvoid (async (): Promise<void> => {\n\t\tconst innerIterator = innerStream[Symbol.asyncIterator]();\n\t\tconst innerEvents: AsyncIterable<AssistantMessageEvent> = { [Symbol.asyncIterator]: () => innerIterator };\n\t\tlet projection: StreamMessageProjection | null = null;\n\t\tlet nativeProjection: RecoveryNativeProjection | null = null;\n\t\tlet textProjection: RecoveryTextProjection | null = null;\n\t\tlet sawToolCall = false;\n\t\tlet cancellationRequested = false;\n\t\tconst contentLifecycle = new RecoveryContentLifecycle();\n\t\tconst terminal = new RecoveryStreamTerminal(outerStream);\n\n\t\tconst finishText = (): void => {\n\t\t\tif (!textProjection) return;\n\t\t\tsawToolCall = textProjection.finish() || sawToolCall;\n\t\t\ttextProjection = null;\n\t\t};\n\n\t\touterStream.setCancellationHandler(async () => {\n\t\t\tcancellationRequested = true;\n\t\t\ttry {\n\t\t\t\tawait innerIterator.return?.();\n\t\t\t\tfinishText();\n\t\t\t\tterminal.cancelled(projection);\n\t\t\t} catch (error) {\n\t\t\t\tfinishText();\n\t\t\t\tterminal.iteratorFailure(projection, error);\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t});\n\n\t\tconst terminateForFailure = (source: AssistantMessage, failure: RecoveryStreamFailure): void => {\n\t\t\tif (!projection || !outerStream.markSourceClosed()) return;\n\t\t\tfinishText();\n\t\t\tterminateRecoveryStreamForFailure(outerStream, projection, source, failure);\n\t\t};\n\n\t\tconst prepareContentEvent = (source: AssistantMessage, contentIndex: number): boolean => {\n\t\t\tif (!projection || !nativeProjection) return false;\n\t\t\tif (!Number.isSafeInteger(contentIndex) || contentIndex < 0 || contentIndex >= source.content.length) {\n\t\t\t\tterminateForFailure(source, \"invalid_native_event_order\");\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tprojection.sync(source);\n\t\t\tnativeProjection.reserveVisibleIds(source);\n\t\t\tif (nativeProjection.synchronizeLower(source, contentIndex)) return true;\n\t\t\tterminateForFailure(source, \"collision\");\n\t\t\treturn false;\n\t\t};\n\n\t\tconst canStart = (\n\t\t\tsource: AssistantMessage,\n\t\t\tcontentIndex: number,\n\t\t\tkind: RecoveryContentKind,\n\t\t\tfailure: RecoveryStreamFailure = \"invalid_content_event_order\",\n\t\t): boolean => {\n\t\t\tif (!Number.isSafeInteger(contentIndex) || contentIndex < 0 || contentIndex >= source.content.length) {\n\t\t\t\tterminateForFailure(source, failure);\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tconst block = source.content[contentIndex];\n\t\t\tconst blockMatches =\n\t\t\t\t(kind === \"text\" && block?.type === \"text\") ||\n\t\t\t\t(kind === \"thinking\" && block?.type === \"thinking\") ||\n\t\t\t\t(kind === \"toolCall\" && block?.type === \"toolCall\");\n\t\t\tif (blockMatches && contentLifecycle.canStart(contentIndex)) return true;\n\t\t\tterminateForFailure(source, failure);\n\t\t\treturn false;\n\t\t};\n\n\t\tconst isActive = (\n\t\t\tsource: AssistantMessage,\n\t\t\tcontentIndex: number,\n\t\t\tkind: RecoveryContentKind,\n\t\t\tfailure: RecoveryStreamFailure = \"invalid_content_event_order\",\n\t\t): boolean => {\n\t\t\tif (contentLifecycle.isActive(contentIndex, kind)) return true;\n\t\t\tterminateForFailure(source, failure);\n\t\t\treturn false;\n\t\t};\n\n\t\tconst synchronizeTerminal = (source: AssistantMessage): boolean => {\n\t\t\tprojection ??= new StreamMessageProjection(outerStream, source, { preserveSourceMetadata: true });\n\t\t\tnativeProjection ??= new RecoveryNativeProjection(outerStream, projection.message);\n\t\t\tprojection.sync(source);\n\t\t\tnativeProjection.reserveVisibleIds(source);\n\t\t\tfinishText();\n\t\t\tif (nativeProjection.synchronizeRemaining(source)) return true;\n\t\t\tterminateForFailure(source, \"collision\");\n\t\t\treturn false;\n\t\t};\n\n\t\ttry {\n\t\t\tfor await (const event of innerEvents) {\n\t\t\t\tif (cancellationRequested) continue;\n\t\t\t\tswitch (event.type) {\n\t\t\t\t\tcase \"start\":\n\t\t\t\t\t\tprojection = new StreamMessageProjection(outerStream, event.partial, {\n\t\t\t\t\t\t\tpreserveSourceMetadata: true,\n\t\t\t\t\t\t});\n\t\t\t\t\t\tnativeProjection = new RecoveryNativeProjection(outerStream, projection.message);\n\t\t\t\t\t\tnativeProjection.reserveVisibleIds(event.partial);\n\t\t\t\t\t\touterStream.push({ type: \"start\", partial: projection.message });\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"text_start\": {\n\t\t\t\t\t\tif (!canStart(event.partial, event.contentIndex, \"text\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !projection || !nativeProjection)\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\tconst nextText = new RecoveryTextProjection(tools, projection, nativeProjection, event.contentIndex);\n\t\t\t\t\t\tif (!nextText.start(event.partial)) {\n\t\t\t\t\t\t\tterminateForFailure(event.partial, \"invalid_native_event_order\");\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\ttextProjection = nextText;\n\t\t\t\t\t\tcontentLifecycle.start(event.contentIndex, \"text\");\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tcase \"text_delta\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"text\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !textProjection) return;\n\t\t\t\t\t\tsawToolCall = textProjection.feed(event.delta) || sawToolCall;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"text_end\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"text\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex)) return;\n\t\t\t\t\t\tfinishText();\n\t\t\t\t\t\tcontentLifecycle.end(event.contentIndex, \"text\");\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"done\":\n\t\t\t\t\t\tif (!synchronizeTerminal(event.message) || !projection) return;\n\t\t\t\t\t\tif (!outerStream.markSourceClosed()) return;\n\t\t\t\t\t\tterminal.done(projection, event.message, sawToolCall, event.reason);\n\t\t\t\t\t\treturn;\n\t\t\t\t\tcase \"error\":\n\t\t\t\t\t\tif (!projection) {\n\t\t\t\t\t\t\tif (!outerStream.markSourceClosed()) return;\n\t\t\t\t\t\t\touterStream.push(event);\n\t\t\t\t\t\t\touterStream.end();\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (!synchronizeTerminal(event.error)) return;\n\t\t\t\t\t\tif (!outerStream.markSourceClosed()) return;\n\t\t\t\t\t\tterminal.sourceError(projection, event.error, sawToolCall, event.reason);\n\t\t\t\t\t\treturn;\n\t\t\t\t\tcase \"toolcall_start\": {\n\t\t\t\t\t\tif (!canStart(event.partial, event.contentIndex, \"toolCall\", \"invalid_native_event_order\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !nativeProjection) return;\n\t\t\t\t\t\tconst status = nativeProjection.projectNativeStart(event.partial, event.contentIndex);\n\t\t\t\t\t\tif (status !== \"projected\") {\n\t\t\t\t\t\t\tterminateForFailure(\n\t\t\t\t\t\t\t\tevent.partial,\n\t\t\t\t\t\t\t\tstatus === \"collision\" ? \"collision\" : \"invalid_native_event_order\",\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcontentLifecycle.start(event.contentIndex, \"toolCall\");\n\t\t\t\t\t\tsawToolCall = true;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tcase \"toolcall_delta\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"toolCall\", \"invalid_native_event_order\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !nativeProjection) return;\n\t\t\t\t\t\tif (!nativeProjection.projectNativeDelta(event.partial, event.contentIndex, event.delta)) {\n\t\t\t\t\t\t\tterminateForFailure(event.partial, \"invalid_native_event_order\");\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"toolcall_end\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"toolCall\", \"invalid_native_event_order\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !nativeProjection) return;\n\t\t\t\t\t\tif (!nativeProjection.projectNativeEnd(event.contentIndex, event.toolCall)) {\n\t\t\t\t\t\t\tterminateForFailure(event.partial, \"invalid_native_event_order\");\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcontentLifecycle.end(event.contentIndex, \"toolCall\");\n\t\t\t\t\t\tsawToolCall = true;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"thinking_start\": {\n\t\t\t\t\t\tif (!canStart(event.partial, event.contentIndex, \"thinking\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !projection || !nativeProjection)\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\tconst outerIndex = projection.startThinking(event.contentIndex, event.partial);\n\t\t\t\t\t\tif (!nativeProjection.recordProjectedBlock(event.contentIndex, outerIndex)) {\n\t\t\t\t\t\t\tterminateForFailure(event.partial, \"invalid_native_event_order\");\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcontentLifecycle.start(event.contentIndex, \"thinking\");\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tcase \"thinking_delta\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"thinking\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !projection) return;\n\t\t\t\t\t\tprojection.projectThinkingDelta(event.contentIndex, event.delta, event.partial);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"thinking_end\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"thinking\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !projection) return;\n\t\t\t\t\t\tprojection.finishThinking(event.contentIndex, event.content, event.partial);\n\t\t\t\t\t\tcontentLifecycle.end(event.contentIndex, \"thinking\");\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (cancellationRequested || !outerStream.markSourceClosed()) return;\n\t\t\tfinishText();\n\t\t\tterminal.exhausted(projection);\n\t\t} catch (error) {\n\t\t\tif (cancellationRequested || !outerStream.markSourceClosed()) return;\n\t\t\tfinishText();\n\t\t\tterminal.iteratorFailure(projection, error);\n\t\t}\n\t})();\n\n\treturn outerStream;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"recovery-stream-wrapper.js","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-stream-wrapper.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qCAAqC,EAAE,MAAM,sCAAsC,CAAC;AAC7F,OAAO,EAA4B,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AACrG,OAAO,EAAE,mCAAmC,EAAE,MAAM,4BAA4B,CAAC;AACjF,OAAO,EAAE,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAC3E,OAAO,EAA8B,iCAAiC,EAAE,MAAM,8BAA8B,CAAC;AAC7G,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AAErE,MAAM,UAAU,4BAA4B,CAC3C,WAAwC,EACxC,KAAsB,EACtB,YAAY,GAAqD,qCAAqC;IAEt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AAC,OAAO,KAAK,EAAE,CAAC;YAChB,IAAI,qBAAqB,IAAI,CAAC,WAAW,CAAC,gBAAgB,EAAE;gBAAE,OAAO;YACrE,UAAU,EAAE,CAAC;YACb,QAAQ,CAAC,eAAe,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QAC7C,CAAC;IACF,CAAC,CAAC,EAAE,CAAC;IAEL,OAAO,WAAW,CAAC;AACpB,CAAC","sourcesContent":["import type { AssistantMessage, AssistantMessageEvent, AssistantMessageEventStream, Tool } from \"../types.ts\";\nimport type { RecoveryStreamParser } from \"./protocols/anthropic-xml/recovery-stream.ts\";\nimport { createAntmlInvokeRecoveryStreamParser } from \"./protocols/antml/recovery-stream.ts\";\nimport { type RecoveryContentKind, RecoveryContentLifecycle } from \"./recovery-content-lifecycle.ts\";\nimport { RecoveryAssistantMessageEventStream } from \"./recovery-event-stream.ts\";\nimport { RecoveryNativeProjection } from \"./recovery-native-projection.ts\";\nimport { type RecoveryStreamFailure, terminateRecoveryStreamForFailure } from \"./recovery-stream-failure.ts\";\nimport { RecoveryStreamTerminal } from \"./recovery-stream-terminal.ts\";\nimport { RecoveryTextProjection } from \"./recovery-text-projection.ts\";\nimport { StreamMessageProjection } from \"./stream-wrapper-shared.ts\";\n\nexport function wrapStreamWithInvokeRecovery(\n\tinnerStream: AssistantMessageEventStream,\n\ttools: readonly Tool[],\n\tcreateParser: (tools: readonly Tool[]) => RecoveryStreamParser = createAntmlInvokeRecoveryStreamParser,\n): AssistantMessageEventStream {\n\tconst outerStream = new RecoveryAssistantMessageEventStream();\n\n\tvoid (async (): Promise<void> => {\n\t\tconst innerIterator = innerStream[Symbol.asyncIterator]();\n\t\tconst innerEvents: AsyncIterable<AssistantMessageEvent> = { [Symbol.asyncIterator]: () => innerIterator };\n\t\tlet projection: StreamMessageProjection | null = null;\n\t\tlet nativeProjection: RecoveryNativeProjection | null = null;\n\t\tlet textProjection: RecoveryTextProjection | null = null;\n\t\tlet sawToolCall = false;\n\t\tlet cancellationRequested = false;\n\t\tconst contentLifecycle = new RecoveryContentLifecycle();\n\t\tconst terminal = new RecoveryStreamTerminal(outerStream);\n\n\t\tconst finishText = (): void => {\n\t\t\tif (!textProjection) return;\n\t\t\tsawToolCall = textProjection.finish() || sawToolCall;\n\t\t\ttextProjection = null;\n\t\t};\n\n\t\touterStream.setCancellationHandler(async () => {\n\t\t\tcancellationRequested = true;\n\t\t\ttry {\n\t\t\t\tawait innerIterator.return?.();\n\t\t\t\tfinishText();\n\t\t\t\tterminal.cancelled(projection);\n\t\t\t} catch (error) {\n\t\t\t\tfinishText();\n\t\t\t\tterminal.iteratorFailure(projection, error);\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t});\n\n\t\tconst terminateForFailure = (source: AssistantMessage, failure: RecoveryStreamFailure): void => {\n\t\t\tif (!projection || !outerStream.markSourceClosed()) return;\n\t\t\tfinishText();\n\t\t\tterminateRecoveryStreamForFailure(outerStream, projection, source, failure);\n\t\t};\n\n\t\tconst prepareContentEvent = (source: AssistantMessage, contentIndex: number): boolean => {\n\t\t\tif (!projection || !nativeProjection) return false;\n\t\t\tif (!Number.isSafeInteger(contentIndex) || contentIndex < 0 || contentIndex >= source.content.length) {\n\t\t\t\tterminateForFailure(source, \"invalid_native_event_order\");\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tprojection.sync(source);\n\t\t\tnativeProjection.reserveVisibleIds(source);\n\t\t\tif (nativeProjection.synchronizeLower(source, contentIndex)) return true;\n\t\t\tterminateForFailure(source, \"collision\");\n\t\t\treturn false;\n\t\t};\n\n\t\tconst canStart = (\n\t\t\tsource: AssistantMessage,\n\t\t\tcontentIndex: number,\n\t\t\tkind: RecoveryContentKind,\n\t\t\tfailure: RecoveryStreamFailure = \"invalid_content_event_order\",\n\t\t): boolean => {\n\t\t\tif (!Number.isSafeInteger(contentIndex) || contentIndex < 0 || contentIndex >= source.content.length) {\n\t\t\t\tterminateForFailure(source, failure);\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tconst block = source.content[contentIndex];\n\t\t\tconst blockMatches =\n\t\t\t\t(kind === \"text\" && block?.type === \"text\") ||\n\t\t\t\t(kind === \"thinking\" && block?.type === \"thinking\") ||\n\t\t\t\t(kind === \"toolCall\" && block?.type === \"toolCall\");\n\t\t\tif (blockMatches && contentLifecycle.canStart(contentIndex)) return true;\n\t\t\tterminateForFailure(source, failure);\n\t\t\treturn false;\n\t\t};\n\n\t\tconst isActive = (\n\t\t\tsource: AssistantMessage,\n\t\t\tcontentIndex: number,\n\t\t\tkind: RecoveryContentKind,\n\t\t\tfailure: RecoveryStreamFailure = \"invalid_content_event_order\",\n\t\t): boolean => {\n\t\t\tif (contentLifecycle.isActive(contentIndex, kind)) return true;\n\t\t\tterminateForFailure(source, failure);\n\t\t\treturn false;\n\t\t};\n\n\t\tconst synchronizeTerminal = (source: AssistantMessage): boolean => {\n\t\t\tprojection ??= new StreamMessageProjection(outerStream, source, { preserveSourceMetadata: true });\n\t\t\tnativeProjection ??= new RecoveryNativeProjection(outerStream, projection.message);\n\t\t\tprojection.sync(source);\n\t\t\tnativeProjection.reserveVisibleIds(source);\n\t\t\tfinishText();\n\t\t\tif (nativeProjection.synchronizeRemaining(source)) return true;\n\t\t\tterminateForFailure(source, \"collision\");\n\t\t\treturn false;\n\t\t};\n\n\t\ttry {\n\t\t\tfor await (const event of innerEvents) {\n\t\t\t\tif (cancellationRequested) continue;\n\t\t\t\tswitch (event.type) {\n\t\t\t\t\tcase \"start\":\n\t\t\t\t\t\tprojection = new StreamMessageProjection(outerStream, event.partial, {\n\t\t\t\t\t\t\tpreserveSourceMetadata: true,\n\t\t\t\t\t\t});\n\t\t\t\t\t\tnativeProjection = new RecoveryNativeProjection(outerStream, projection.message);\n\t\t\t\t\t\tnativeProjection.reserveVisibleIds(event.partial);\n\t\t\t\t\t\touterStream.push({ type: \"start\", partial: projection.message });\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"text_start\": {\n\t\t\t\t\t\tif (!canStart(event.partial, event.contentIndex, \"text\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !projection || !nativeProjection)\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\tconst nextText = new RecoveryTextProjection(\n\t\t\t\t\t\t\ttools,\n\t\t\t\t\t\t\tprojection,\n\t\t\t\t\t\t\tnativeProjection,\n\t\t\t\t\t\t\tevent.contentIndex,\n\t\t\t\t\t\t\tcreateParser,\n\t\t\t\t\t\t);\n\t\t\t\t\t\tif (!nextText.start(event.partial)) {\n\t\t\t\t\t\t\tterminateForFailure(event.partial, \"invalid_native_event_order\");\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\ttextProjection = nextText;\n\t\t\t\t\t\tcontentLifecycle.start(event.contentIndex, \"text\");\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tcase \"text_delta\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"text\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !textProjection) return;\n\t\t\t\t\t\tsawToolCall = textProjection.feed(event.delta) || sawToolCall;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"text_end\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"text\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex)) return;\n\t\t\t\t\t\tfinishText();\n\t\t\t\t\t\tcontentLifecycle.end(event.contentIndex, \"text\");\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"done\":\n\t\t\t\t\t\tif (!synchronizeTerminal(event.message) || !projection) return;\n\t\t\t\t\t\tif (!outerStream.markSourceClosed()) return;\n\t\t\t\t\t\tterminal.done(projection, event.message, sawToolCall, event.reason);\n\t\t\t\t\t\treturn;\n\t\t\t\t\tcase \"error\":\n\t\t\t\t\t\tif (!projection) {\n\t\t\t\t\t\t\tif (!outerStream.markSourceClosed()) return;\n\t\t\t\t\t\t\touterStream.push(event);\n\t\t\t\t\t\t\touterStream.end();\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (!synchronizeTerminal(event.error)) return;\n\t\t\t\t\t\tif (!outerStream.markSourceClosed()) return;\n\t\t\t\t\t\tterminal.sourceError(projection, event.error, sawToolCall, event.reason);\n\t\t\t\t\t\treturn;\n\t\t\t\t\tcase \"toolcall_start\": {\n\t\t\t\t\t\tif (!canStart(event.partial, event.contentIndex, \"toolCall\", \"invalid_native_event_order\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !nativeProjection) return;\n\t\t\t\t\t\tconst status = nativeProjection.projectNativeStart(event.partial, event.contentIndex);\n\t\t\t\t\t\tif (status !== \"projected\") {\n\t\t\t\t\t\t\tterminateForFailure(\n\t\t\t\t\t\t\t\tevent.partial,\n\t\t\t\t\t\t\t\tstatus === \"collision\" ? \"collision\" : \"invalid_native_event_order\",\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcontentLifecycle.start(event.contentIndex, \"toolCall\");\n\t\t\t\t\t\tsawToolCall = true;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tcase \"toolcall_delta\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"toolCall\", \"invalid_native_event_order\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !nativeProjection) return;\n\t\t\t\t\t\tif (!nativeProjection.projectNativeDelta(event.partial, event.contentIndex, event.delta)) {\n\t\t\t\t\t\t\tterminateForFailure(event.partial, \"invalid_native_event_order\");\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"toolcall_end\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"toolCall\", \"invalid_native_event_order\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !nativeProjection) return;\n\t\t\t\t\t\tif (!nativeProjection.projectNativeEnd(event.contentIndex, event.toolCall)) {\n\t\t\t\t\t\t\tterminateForFailure(event.partial, \"invalid_native_event_order\");\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcontentLifecycle.end(event.contentIndex, \"toolCall\");\n\t\t\t\t\t\tsawToolCall = true;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"thinking_start\": {\n\t\t\t\t\t\tif (!canStart(event.partial, event.contentIndex, \"thinking\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !projection || !nativeProjection)\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\tconst outerIndex = projection.startThinking(event.contentIndex, event.partial);\n\t\t\t\t\t\tif (!nativeProjection.recordProjectedBlock(event.contentIndex, outerIndex)) {\n\t\t\t\t\t\t\tterminateForFailure(event.partial, \"invalid_native_event_order\");\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcontentLifecycle.start(event.contentIndex, \"thinking\");\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tcase \"thinking_delta\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"thinking\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !projection) return;\n\t\t\t\t\t\tprojection.projectThinkingDelta(event.contentIndex, event.delta, event.partial);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"thinking_end\":\n\t\t\t\t\t\tif (!isActive(event.partial, event.contentIndex, \"thinking\")) return;\n\t\t\t\t\t\tif (!prepareContentEvent(event.partial, event.contentIndex) || !projection) return;\n\t\t\t\t\t\tprojection.finishThinking(event.contentIndex, event.content, event.partial);\n\t\t\t\t\t\tcontentLifecycle.end(event.contentIndex, \"thinking\");\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (cancellationRequested || !outerStream.markSourceClosed()) return;\n\t\t\tfinishText();\n\t\t\tterminal.exhausted(projection);\n\t\t} catch (error) {\n\t\t\tif (cancellationRequested || !outerStream.markSourceClosed()) return;\n\t\t\tfinishText();\n\t\t\tterminal.iteratorFailure(projection, error);\n\t\t}\n\t})();\n\n\treturn outerStream;\n}\n"]}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { AssistantMessage, Tool } from "../types.ts";
|
|
2
|
+
import type { RecoveryStreamParser } from "./protocols/anthropic-xml/recovery-stream.ts";
|
|
2
3
|
import type { RecoveryNativeProjection } from "./recovery-native-projection.ts";
|
|
3
4
|
import type { StreamMessageProjection } from "./stream-wrapper-shared.ts";
|
|
4
5
|
/** Owns one ordinary assistant text block's mask/parser/projection lifecycle. */
|
|
@@ -12,7 +13,7 @@ export declare class RecoveryTextProjection {
|
|
|
12
13
|
private textBuffer;
|
|
13
14
|
private sawToolCall;
|
|
14
15
|
private finished;
|
|
15
|
-
constructor(tools: readonly Tool[], projection: StreamMessageProjection, nativeProjection: RecoveryNativeProjection, innerIndex: number);
|
|
16
|
+
constructor(tools: readonly Tool[], projection: StreamMessageProjection, nativeProjection: RecoveryNativeProjection, innerIndex: number, createParser?: (tools: readonly Tool[]) => RecoveryStreamParser);
|
|
16
17
|
start(source: AssistantMessage): boolean;
|
|
17
18
|
feed(text: string): boolean;
|
|
18
19
|
finish(): boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"recovery-text-projection.d.ts","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-text-projection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"recovery-text-projection.d.ts","sourceRoot":"","sources":["../../src/tool-call-middleware/recovery-text-projection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAA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