@anvia/client 1.5.0 → 1.7.0
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/README.md +28 -0
- package/dist/durable-chat.d.ts +51 -0
- package/dist/durable-chat.d.ts.map +1 -0
- package/dist/durable-chat.js +933 -0
- package/dist/durable-chat.js.map +1 -0
- package/dist/durable.d.ts +71 -65
- package/dist/durable.d.ts.map +1 -0
- package/dist/durable.js +213 -253
- package/dist/durable.js.map +1 -1
- package/dist/event-transport-CcP1zaST.js +103 -0
- package/dist/event-transport-CcP1zaST.js.map +1 -0
- package/dist/event-transport-rXxwo4BK.d.ts +88 -0
- package/dist/event-transport-rXxwo4BK.d.ts.map +1 -0
- package/dist/index.d.ts +62 -415
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1968 -2382
- package/dist/index.js.map +1 -1
- package/dist/streams-BXxF27Hp.js +156 -0
- package/dist/streams-BXxF27Hp.js.map +1 -0
- package/dist/transport/index.d.ts +2 -91
- package/dist/transport/index.js +3 -23
- package/dist/types-Bt4tUcST.d.ts +348 -0
- package/dist/types-Bt4tUcST.d.ts.map +1 -0
- package/dist/types-CqJxMo_1.js +466 -0
- package/dist/types-CqJxMo_1.js.map +1 -0
- package/package.json +11 -7
- package/dist/chunk-PD72KNCV.js +0 -150
- package/dist/chunk-PD72KNCV.js.map +0 -1
- package/dist/chunk-RK5U6WVJ.js +0 -127
- package/dist/chunk-RK5U6WVJ.js.map +0 -1
- package/dist/transport/index.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,2571 +1,2157 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
} from "
|
|
4
|
-
import "
|
|
5
|
-
|
|
6
|
-
// src/adapters.ts
|
|
7
|
-
import {
|
|
8
|
-
isJsonValue as isJsonValue3
|
|
9
|
-
} from "@anvia/core/completion";
|
|
10
|
-
|
|
11
|
-
// src/messages.ts
|
|
12
|
-
import { getAssistantGenerationMetadata, isJsonValue } from "@anvia/core/completion";
|
|
13
|
-
function uiMessagesToMessages(messages) {
|
|
14
|
-
const result = [];
|
|
15
|
-
for (const message of messages) {
|
|
16
|
-
assertMetadata(message.metadata);
|
|
17
|
-
if (message.role === "system") {
|
|
18
|
-
assertOnlyParts(message, ["text", "data", "error"]);
|
|
19
|
-
result.push({
|
|
20
|
-
role: "system",
|
|
21
|
-
content: textFromParts(message.parts),
|
|
22
|
-
...metadataField(message.metadata)
|
|
23
|
-
});
|
|
24
|
-
continue;
|
|
25
|
-
}
|
|
26
|
-
if (message.role === "user") {
|
|
27
|
-
assertOnlyParts(message, ["text", "attachment", "data", "error"]);
|
|
28
|
-
const content2 = [];
|
|
29
|
-
for (const part of message.parts) {
|
|
30
|
-
if (part.type === "text") {
|
|
31
|
-
content2.push(textContent(part.text, part.signature));
|
|
32
|
-
} else if (part.type === "attachment") {
|
|
33
|
-
content2.push(attachmentToUserContent(part.attachment));
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
result.push({ role: "user", content: content2, ...metadataField(message.metadata) });
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
if (message.role === "assistant") {
|
|
40
|
-
assertOnlyParts(message, [
|
|
41
|
-
"text",
|
|
42
|
-
"reasoning",
|
|
43
|
-
"tool",
|
|
44
|
-
"attachment",
|
|
45
|
-
"source",
|
|
46
|
-
"data",
|
|
47
|
-
"error"
|
|
48
|
-
]);
|
|
49
|
-
const content2 = [];
|
|
50
|
-
const toolResults = [];
|
|
51
|
-
for (const part of message.parts) {
|
|
52
|
-
if (part.type === "text") {
|
|
53
|
-
content2.push(textContent(part.text, part.signature));
|
|
54
|
-
} else if (part.type === "reasoning") {
|
|
55
|
-
let reasoning = {
|
|
56
|
-
type: "reasoning",
|
|
57
|
-
text: part.text
|
|
58
|
-
};
|
|
59
|
-
if (part.reasoningId !== void 0) reasoning = { ...reasoning, id: part.reasoningId };
|
|
60
|
-
if (part.content !== void 0) reasoning = { ...reasoning, details: part.content };
|
|
61
|
-
content2.push(reasoning);
|
|
62
|
-
} else if (part.type === "tool") {
|
|
63
|
-
assertReplayableToolPart(part);
|
|
64
|
-
let toolCall = {
|
|
65
|
-
type: "tool-call",
|
|
66
|
-
toolCallId: part.toolCallId,
|
|
67
|
-
toolName: part.toolName,
|
|
68
|
-
input: part.input
|
|
69
|
-
};
|
|
70
|
-
if (part.callId !== void 0) toolCall = { ...toolCall, callId: part.callId };
|
|
71
|
-
if (part.signature !== void 0) toolCall = { ...toolCall, signature: part.signature };
|
|
72
|
-
content2.push(toolCall);
|
|
73
|
-
if (part.state === "output-available" || part.state === "error") {
|
|
74
|
-
toolResults.push(toolResultFromPart(part));
|
|
75
|
-
}
|
|
76
|
-
} else if (part.type === "attachment") {
|
|
77
|
-
content2.push(attachmentToAssistantContent(part.attachment));
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
let assistant = {
|
|
81
|
-
role: "assistant",
|
|
82
|
-
content: content2,
|
|
83
|
-
...metadataField(message.metadata)
|
|
84
|
-
};
|
|
85
|
-
if (message.modelMessageId !== void 0) {
|
|
86
|
-
assistant = { ...assistant, id: message.modelMessageId };
|
|
87
|
-
}
|
|
88
|
-
result.push(assistant);
|
|
89
|
-
if (toolResults.length > 0) {
|
|
90
|
-
result.push({ role: "tool", content: toolResults });
|
|
91
|
-
}
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
assertOnlyParts(message, ["tool", "data", "error"]);
|
|
95
|
-
const content = [];
|
|
96
|
-
for (const part of message.parts) {
|
|
97
|
-
if (part.type !== "tool") continue;
|
|
98
|
-
assertToolResultPart(part);
|
|
99
|
-
content.push(toolResultFromPart(part));
|
|
100
|
-
}
|
|
101
|
-
result.push({ role: "tool", content, ...metadataField(message.metadata) });
|
|
102
|
-
}
|
|
103
|
-
return result;
|
|
104
|
-
}
|
|
105
|
-
function messagesToUIMessages(messages) {
|
|
106
|
-
const result = [];
|
|
107
|
-
const byToolCallId = /* @__PURE__ */ new Map();
|
|
108
|
-
const byCallId = /* @__PURE__ */ new Map();
|
|
109
|
-
for (const message of messages) {
|
|
110
|
-
assertMetadata(message.metadata);
|
|
111
|
-
if (message.role === "system") {
|
|
112
|
-
result.push({
|
|
113
|
-
id: createId("msg"),
|
|
114
|
-
role: "system",
|
|
115
|
-
parts: [{ id: createId("part"), type: "text", text: message.content }],
|
|
116
|
-
...metadataField(message.metadata)
|
|
117
|
-
});
|
|
118
|
-
continue;
|
|
119
|
-
}
|
|
120
|
-
if (message.role === "user") {
|
|
121
|
-
const content = typeof message.content === "string" ? [{ type: "text", text: message.content }] : message.content;
|
|
122
|
-
const parts = content.map((part) => contentToUIMessagePart(part));
|
|
123
|
-
result.push({
|
|
124
|
-
id: createId("msg"),
|
|
125
|
-
role: "user",
|
|
126
|
-
parts,
|
|
127
|
-
...metadataField(message.metadata)
|
|
128
|
-
});
|
|
129
|
-
continue;
|
|
130
|
-
}
|
|
131
|
-
if (message.role === "assistant") {
|
|
132
|
-
const generationMetadata = getAssistantGenerationMetadata(message);
|
|
133
|
-
const content = typeof message.content === "string" ? [{ type: "text", text: message.content }] : message.content;
|
|
134
|
-
const parts = content.map(assistantContentToUIMessagePart);
|
|
135
|
-
for (const source of generationMetadata?.sources ?? []) {
|
|
136
|
-
parts.push({ id: createId("source"), type: "source", source });
|
|
137
|
-
}
|
|
138
|
-
let uiMessage = {
|
|
139
|
-
id: createId("msg"),
|
|
140
|
-
role: "assistant",
|
|
141
|
-
parts,
|
|
142
|
-
...metadataField(message.metadata)
|
|
143
|
-
};
|
|
144
|
-
if (generationMetadata !== void 0) {
|
|
145
|
-
const generation = { usage: generationMetadata.usage };
|
|
146
|
-
if (generationMetadata.contextUsage !== void 0) {
|
|
147
|
-
generation.contextUsage = generationMetadata.contextUsage;
|
|
148
|
-
}
|
|
149
|
-
uiMessage = {
|
|
150
|
-
...uiMessage,
|
|
151
|
-
generation
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
if (message.id !== void 0) uiMessage = { ...uiMessage, modelMessageId: message.id };
|
|
155
|
-
const messageIndex = result.length;
|
|
156
|
-
result.push(uiMessage);
|
|
157
|
-
for (const [partIndex, part] of parts.entries()) {
|
|
158
|
-
if (part.type !== "tool") continue;
|
|
159
|
-
const location = { messageIndex, partIndex };
|
|
160
|
-
byToolCallId.set(part.toolCallId, location);
|
|
161
|
-
if (part.callId !== void 0) byCallId.set(part.callId, location);
|
|
162
|
-
}
|
|
163
|
-
continue;
|
|
164
|
-
}
|
|
165
|
-
const unmerged = [];
|
|
166
|
-
for (const content of message.content) {
|
|
167
|
-
if (content.type !== "tool-result") continue;
|
|
168
|
-
const location = (content.callId === void 0 ? void 0 : byCallId.get(content.callId)) ?? byToolCallId.get(content.toolCallId);
|
|
169
|
-
const ownerPart = toolPartAt(result, location);
|
|
170
|
-
if (ownerPart === void 0 || ownerPart.state === "input-streaming") {
|
|
171
|
-
throw new TypeError(
|
|
172
|
-
`Tool result "${content.toolCallId}" requires a matching completed tool call.`
|
|
173
|
-
);
|
|
174
|
-
}
|
|
175
|
-
const part = toolResultToUIMessagePart(content, ownerPart.toolName, ownerPart.input);
|
|
176
|
-
if (message.metadata === void 0 && location !== void 0) {
|
|
177
|
-
const owner = result[location.messageIndex];
|
|
178
|
-
const existing = owner?.parts[location.partIndex];
|
|
179
|
-
if (owner !== void 0 && existing?.type === "tool") {
|
|
180
|
-
const parts = [...owner.parts];
|
|
181
|
-
parts[location.partIndex] = { ...existing, ...part, id: existing.id };
|
|
182
|
-
result[location.messageIndex] = { ...owner, parts };
|
|
183
|
-
continue;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
unmerged.push(part);
|
|
187
|
-
}
|
|
188
|
-
if (unmerged.length > 0 || message.metadata !== void 0) {
|
|
189
|
-
result.push({
|
|
190
|
-
id: createId("msg"),
|
|
191
|
-
role: "tool",
|
|
192
|
-
parts: unmerged,
|
|
193
|
-
...metadataField(message.metadata)
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return result;
|
|
198
|
-
}
|
|
199
|
-
function contentToUIMessagePart(content) {
|
|
200
|
-
if (content.type === "text") {
|
|
201
|
-
const part = {
|
|
202
|
-
id: createId("part"),
|
|
203
|
-
type: "text",
|
|
204
|
-
text: content.text
|
|
205
|
-
};
|
|
206
|
-
if (content.signature !== void 0) part.signature = content.signature;
|
|
207
|
-
return part;
|
|
208
|
-
}
|
|
209
|
-
return {
|
|
210
|
-
id: createId("part"),
|
|
211
|
-
type: "attachment",
|
|
212
|
-
attachment: contentToAttachment(content)
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
function assistantContentToUIMessagePart(content) {
|
|
216
|
-
if (content.type === "text") return contentToUIMessagePart(content);
|
|
217
|
-
if (content.type === "reasoning") {
|
|
218
|
-
const part = {
|
|
219
|
-
id: createId("part"),
|
|
220
|
-
type: "reasoning",
|
|
221
|
-
text: content.text
|
|
222
|
-
};
|
|
223
|
-
if (content.id !== void 0) part.reasoningId = content.id;
|
|
224
|
-
if (content.details !== void 0) part.content = content.details;
|
|
225
|
-
return part;
|
|
226
|
-
}
|
|
227
|
-
if (content.type === "tool-call") {
|
|
228
|
-
const part = {
|
|
229
|
-
id: toolPartId(content.toolCallId),
|
|
230
|
-
type: "tool",
|
|
231
|
-
toolName: content.toolName,
|
|
232
|
-
toolCallId: content.toolCallId,
|
|
233
|
-
state: "input-available",
|
|
234
|
-
input: content.input
|
|
235
|
-
};
|
|
236
|
-
if (content.callId !== void 0) part.callId = content.callId;
|
|
237
|
-
if (content.signature !== void 0) part.signature = content.signature;
|
|
238
|
-
return part;
|
|
239
|
-
}
|
|
240
|
-
return {
|
|
241
|
-
id: createId("part"),
|
|
242
|
-
type: "attachment",
|
|
243
|
-
attachment: contentToAttachment(content)
|
|
244
|
-
};
|
|
245
|
-
}
|
|
246
|
-
function textContent(text, signature) {
|
|
247
|
-
return signature === void 0 ? { type: "text", text } : { type: "text", text, signature };
|
|
248
|
-
}
|
|
249
|
-
function toolResultFromPart(part) {
|
|
250
|
-
let result = {
|
|
251
|
-
type: "tool-result",
|
|
252
|
-
toolCallId: part.toolCallId,
|
|
253
|
-
toolName: part.toolName,
|
|
254
|
-
output: toolOutputFromPart(part)
|
|
255
|
-
};
|
|
256
|
-
if (part.callId !== void 0) result = { ...result, callId: part.callId };
|
|
257
|
-
return result;
|
|
258
|
-
}
|
|
259
|
-
function toolOutputFromPart(part) {
|
|
260
|
-
if (part.state === "error") {
|
|
261
|
-
return { type: "error-text", value: part.error.message };
|
|
262
|
-
}
|
|
263
|
-
if (part.resultContent !== void 0) {
|
|
264
|
-
return { type: "content", value: part.resultContent };
|
|
265
|
-
}
|
|
266
|
-
const output = part.output;
|
|
267
|
-
return typeof output === "string" ? { type: "text", value: output } : { type: "json", value: output };
|
|
268
|
-
}
|
|
269
|
-
function attachmentToUserContent(attachment) {
|
|
270
|
-
if (attachment.type === "image" || attachment.mediaType?.startsWith("image/") === true) {
|
|
271
|
-
return attachmentToImageContent(attachment);
|
|
272
|
-
}
|
|
273
|
-
return attachmentToFileContent(attachment);
|
|
274
|
-
}
|
|
275
|
-
function attachmentToAssistantContent(attachment) {
|
|
276
|
-
return attachment.type === "image" || attachment.mediaType?.startsWith("image/") === true ? attachmentToImageContent(attachment) : attachmentToFileContent(attachment);
|
|
277
|
-
}
|
|
278
|
-
function attachmentToImageContent(attachment) {
|
|
279
|
-
const image = attachment.url !== void 0 ? { type: "url", url: attachment.url } : attachment.data !== void 0 ? { type: "data", data: attachment.data } : (() => {
|
|
280
|
-
throw new TypeError("Image attachments require a URL or base64 data.");
|
|
281
|
-
})();
|
|
282
|
-
let content = {
|
|
283
|
-
type: "image",
|
|
284
|
-
image
|
|
285
|
-
};
|
|
286
|
-
if (attachment.mediaType !== void 0) content = { ...content, mediaType: attachment.mediaType };
|
|
287
|
-
if (attachment.detail !== void 0) content = { ...content, detail: attachment.detail };
|
|
288
|
-
return content;
|
|
289
|
-
}
|
|
290
|
-
function attachmentToFileContent(attachment) {
|
|
291
|
-
const data = attachment.url !== void 0 ? { type: "url", url: attachment.url } : attachment.data !== void 0 ? { type: "data", data: attachment.data } : attachment.text !== void 0 ? { type: "text", text: attachment.text } : (() => {
|
|
292
|
-
throw new TypeError("File attachments require a URL, base64 data, or text.");
|
|
293
|
-
})();
|
|
294
|
-
let content = {
|
|
295
|
-
type: "file",
|
|
296
|
-
data,
|
|
297
|
-
mediaType: attachment.mediaType ?? "application/octet-stream"
|
|
298
|
-
};
|
|
299
|
-
if (attachment.name !== void 0) content = { ...content, filename: attachment.name };
|
|
300
|
-
return content;
|
|
301
|
-
}
|
|
302
|
-
function contentToAttachment(content) {
|
|
303
|
-
const attachment = {
|
|
304
|
-
id: createId("attachment"),
|
|
305
|
-
type: content.type === "image" ? "image" : "file"
|
|
306
|
-
};
|
|
307
|
-
if (content.type === "image") {
|
|
308
|
-
if (content.detail !== void 0) attachment.detail = content.detail;
|
|
309
|
-
if (content.mediaType !== void 0) attachment.mediaType = content.mediaType;
|
|
310
|
-
if (content.image.type === "url") attachment.url = content.image.url;
|
|
311
|
-
else attachment.data = content.image.data;
|
|
312
|
-
return attachment;
|
|
313
|
-
}
|
|
314
|
-
attachment.mediaType = content.mediaType;
|
|
315
|
-
if (content.filename !== void 0) attachment.name = content.filename;
|
|
316
|
-
if (content.data.type === "url") attachment.url = content.data.url;
|
|
317
|
-
else if (content.data.type === "data") attachment.data = content.data.data;
|
|
318
|
-
else attachment.text = content.data.text;
|
|
319
|
-
return attachment;
|
|
320
|
-
}
|
|
321
|
-
function toolResultToUIMessagePart(content, fallbackToolName, input) {
|
|
322
|
-
const output = content.output;
|
|
323
|
-
const common = {
|
|
324
|
-
id: toolPartId(content.toolCallId),
|
|
325
|
-
type: "tool",
|
|
326
|
-
toolName: content.toolName || fallbackToolName,
|
|
327
|
-
toolCallId: content.toolCallId,
|
|
328
|
-
input
|
|
329
|
-
};
|
|
330
|
-
if (content.callId !== void 0) Object.assign(common, { callId: content.callId });
|
|
331
|
-
if (output.type === "error-text" || output.type === "error-json") {
|
|
332
|
-
return {
|
|
333
|
-
...common,
|
|
334
|
-
state: "error",
|
|
335
|
-
error: {
|
|
336
|
-
message: output.type === "error-text" ? output.value : JSON.stringify(output.value)
|
|
337
|
-
}
|
|
338
|
-
};
|
|
339
|
-
}
|
|
340
|
-
if (output.type === "execution-denied") {
|
|
341
|
-
return {
|
|
342
|
-
...common,
|
|
343
|
-
state: "error",
|
|
344
|
-
error: { message: output.reason ?? "Tool execution was denied." }
|
|
345
|
-
};
|
|
346
|
-
}
|
|
347
|
-
const part = {
|
|
348
|
-
...common,
|
|
349
|
-
state: "output-available",
|
|
350
|
-
output: toolResultOutput(output)
|
|
351
|
-
};
|
|
352
|
-
if (output.type === "content") part.resultContent = output.value;
|
|
353
|
-
return part;
|
|
354
|
-
}
|
|
355
|
-
function toolResultOutput(output) {
|
|
356
|
-
if (output.type === "text" || output.type === "error-text") return output.value;
|
|
357
|
-
if (output.type === "json" || output.type === "error-json") return output.value;
|
|
358
|
-
if (output.type === "execution-denied") return output.reason ?? "Tool execution was denied.";
|
|
359
|
-
return output.value.length === 1 && output.value[0]?.type === "text" ? output.value[0].text : output.value.map(
|
|
360
|
-
(part) => part.type === "text" ? { ...part } : { ...part, data: { ...part.data } }
|
|
361
|
-
);
|
|
362
|
-
}
|
|
363
|
-
function toolPartAt(messages, location) {
|
|
364
|
-
if (location === void 0) return void 0;
|
|
365
|
-
const part = messages[location.messageIndex]?.parts[location.partIndex];
|
|
366
|
-
return part?.type === "tool" ? part : void 0;
|
|
367
|
-
}
|
|
368
|
-
function assertReplayableToolPart(part) {
|
|
369
|
-
if (part.state === "input-streaming") {
|
|
370
|
-
throw new TypeError(
|
|
371
|
-
`Tool call "${part.toolCallId}" is still streaming and cannot be converted to a model message.`
|
|
372
|
-
);
|
|
373
|
-
}
|
|
374
|
-
if (!isJsonValue(part.input)) {
|
|
375
|
-
throw new TypeError(`Tool call "${part.toolCallId}" input must be a strict JSON value.`);
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
function assertToolResultPart(part) {
|
|
379
|
-
assertReplayableToolPart(part);
|
|
380
|
-
if (part.state === "input-available") {
|
|
381
|
-
throw new TypeError(
|
|
382
|
-
`Tool message part "${part.toolCallId}" requires an output or execution error.`
|
|
383
|
-
);
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
function textFromParts(parts) {
|
|
387
|
-
return parts.flatMap((part) => part.type === "text" ? [part.text] : []).join("");
|
|
388
|
-
}
|
|
389
|
-
function assertOnlyParts(message, allowed) {
|
|
390
|
-
for (const part of message.parts) {
|
|
391
|
-
if (!allowed.includes(part.type)) {
|
|
392
|
-
throw new TypeError(`${message.role} UI messages cannot contain ${part.type} parts.`);
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
function assertMetadata(metadata) {
|
|
397
|
-
if (metadata !== void 0 && (!isJsonValue(metadata) || Array.isArray(metadata))) {
|
|
398
|
-
throw new TypeError("Message metadata must be a strict JSON object.");
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
function metadataField(metadata) {
|
|
402
|
-
return metadata === void 0 ? {} : { metadata };
|
|
403
|
-
}
|
|
404
|
-
function toolPartId(toolCallId) {
|
|
405
|
-
return `tool_${toolCallId}`;
|
|
406
|
-
}
|
|
407
|
-
var nextId = 0;
|
|
408
|
-
function createClientId(prefix) {
|
|
409
|
-
const random = globalThis.crypto?.randomUUID?.();
|
|
410
|
-
if (random !== void 0) return `${prefix}_${random}`;
|
|
411
|
-
nextId += 1;
|
|
412
|
-
return `${prefix}_${nextId.toString(36)}`;
|
|
413
|
-
}
|
|
414
|
-
function createId(prefix) {
|
|
415
|
-
return createClientId(prefix);
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
// src/protocol.ts
|
|
419
|
-
import {
|
|
420
|
-
parseAgentInteractionRequest,
|
|
421
|
-
parseAgentInteractionResponse
|
|
422
|
-
} from "@anvia/core/agent/interactions";
|
|
423
|
-
import { isJsonValue as isJsonValue2, parseMessages } from "@anvia/core/completion";
|
|
424
|
-
|
|
425
|
-
// src/types.ts
|
|
426
|
-
var CLIENT_STREAM_PROTOCOL = "anvia.client.v3";
|
|
427
|
-
|
|
1
|
+
import { i as uiMessagesToMessages, n as createClientId, r as messagesToUIMessages, t as CLIENT_STREAM_PROTOCOL } from "./types-CqJxMo_1.js";
|
|
2
|
+
import { n as createFetchEventTransport } from "./event-transport-CcP1zaST.js";
|
|
3
|
+
import { isJsonValue, isJsonValue as isJsonValue$1, parseMessages } from "@anvia/core/completion";
|
|
4
|
+
import { parseAgentInteractionRequest, parseAgentInteractionResponse } from "@anvia/core/agent/interactions";
|
|
428
5
|
// src/protocol.ts
|
|
429
6
|
var ClientProtocolError = class extends Error {
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
7
|
+
value;
|
|
8
|
+
constructor(message, value) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.value = value;
|
|
11
|
+
this.name = "ClientProtocolError";
|
|
12
|
+
}
|
|
436
13
|
};
|
|
437
14
|
function parseUIMessage(value, options = {}) {
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
15
|
+
const message = requireRecord(value, "UI message");
|
|
16
|
+
if (!isJsonValue$1(message)) throw new ClientProtocolError("UI messages must be strict JSON values.", value);
|
|
17
|
+
requireOnlyKeys(message, [
|
|
18
|
+
"id",
|
|
19
|
+
"role",
|
|
20
|
+
"parts",
|
|
21
|
+
"modelMessageId",
|
|
22
|
+
"metadata",
|
|
23
|
+
"generation"
|
|
24
|
+
], "UI message");
|
|
25
|
+
if (typeof message.id !== "string") invalid("UI message.id", value);
|
|
26
|
+
requireOneOf(message.role, [
|
|
27
|
+
"system",
|
|
28
|
+
"user",
|
|
29
|
+
"assistant",
|
|
30
|
+
"tool"
|
|
31
|
+
], "UI message.role", value);
|
|
32
|
+
if (!Array.isArray(message.parts)) invalid("UI message.parts", value);
|
|
33
|
+
const parts = message.parts.map((part) => parseUIMessagePart(part, options.dataSchemas, value));
|
|
34
|
+
if (message.modelMessageId !== void 0 && typeof message.modelMessageId !== "string") invalid("UI message.modelMessageId", value);
|
|
35
|
+
const metadata = message.metadata === void 0 ? void 0 : parseMetadata(message.metadata, options.metadataSchema, value);
|
|
36
|
+
if (message.generation !== void 0 && !isUIMessageGeneration(message.generation)) invalid("UI message.generation", value);
|
|
37
|
+
let parsed = {
|
|
38
|
+
id: message.id,
|
|
39
|
+
role: message.role,
|
|
40
|
+
parts
|
|
41
|
+
};
|
|
42
|
+
if (message.modelMessageId !== void 0) parsed = {
|
|
43
|
+
...parsed,
|
|
44
|
+
modelMessageId: message.modelMessageId
|
|
45
|
+
};
|
|
46
|
+
if (metadata !== void 0) parsed = {
|
|
47
|
+
...parsed,
|
|
48
|
+
metadata
|
|
49
|
+
};
|
|
50
|
+
if (message.generation !== void 0) parsed = {
|
|
51
|
+
...parsed,
|
|
52
|
+
generation: message.generation
|
|
53
|
+
};
|
|
54
|
+
return parsed;
|
|
471
55
|
}
|
|
472
56
|
function parseUIMessages(value, options = {}) {
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
}
|
|
476
|
-
return value.map((message) => parseUIMessage(message, options));
|
|
57
|
+
if (!Array.isArray(value)) throw new ClientProtocolError("UI messages must be an array.", value);
|
|
58
|
+
return value.map((message) => parseUIMessage(message, options));
|
|
477
59
|
}
|
|
478
60
|
function parseClientStreamRequest(value) {
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
if (input.metadata !== void 0) request.metadata = input.metadata;
|
|
527
|
-
if (input.resume !== void 0) {
|
|
528
|
-
const resume = requireRecord(input.resume, "Client stream request.resume");
|
|
529
|
-
requireOnlyKeys(resume, ["streamId", "after"], "Client stream request.resume");
|
|
530
|
-
if (typeof resume.streamId !== "string" || !isEventId(resume.after)) {
|
|
531
|
-
throw new ClientProtocolError(
|
|
532
|
-
"Client stream resume cursor requires streamId and a nonnegative safe integer after.",
|
|
533
|
-
value
|
|
534
|
-
);
|
|
535
|
-
}
|
|
536
|
-
request.resume = { streamId: resume.streamId, after: resume.after };
|
|
537
|
-
}
|
|
538
|
-
return request;
|
|
61
|
+
const input = requireRecord(value, "Client stream request");
|
|
62
|
+
requireOnlyKeys(input, [
|
|
63
|
+
"type",
|
|
64
|
+
"messages",
|
|
65
|
+
"interactionId",
|
|
66
|
+
"response",
|
|
67
|
+
"metadata",
|
|
68
|
+
"resume"
|
|
69
|
+
], "Client stream request");
|
|
70
|
+
requireOneOf(input.type, ["messages", "interaction_response"], "Client stream request.type", value);
|
|
71
|
+
if (input.metadata !== void 0 && !isJsonObject(input.metadata)) throw new ClientProtocolError("Client stream request.metadata must be a JSON object.", value);
|
|
72
|
+
let request;
|
|
73
|
+
if (input.type === "messages") {
|
|
74
|
+
if (input.interactionId !== void 0 || input.response !== void 0) throw new ClientProtocolError("Messages requests cannot include interaction response fields.", value);
|
|
75
|
+
let messages;
|
|
76
|
+
try {
|
|
77
|
+
messages = parseMessages(input.messages);
|
|
78
|
+
} catch {
|
|
79
|
+
throw new ClientProtocolError("Client stream request.messages must be Message[].", value);
|
|
80
|
+
}
|
|
81
|
+
request = {
|
|
82
|
+
type: "messages",
|
|
83
|
+
messages
|
|
84
|
+
};
|
|
85
|
+
} else {
|
|
86
|
+
if (input.messages !== void 0 || typeof input.interactionId !== "string") throw new ClientProtocolError("Interaction response requests require interactionId and cannot include messages.", value);
|
|
87
|
+
try {
|
|
88
|
+
request = {
|
|
89
|
+
type: "interaction_response",
|
|
90
|
+
interactionId: input.interactionId,
|
|
91
|
+
response: parseAgentInteractionResponse(input.response)
|
|
92
|
+
};
|
|
93
|
+
} catch {
|
|
94
|
+
throw new ClientProtocolError("Client interaction response is invalid.", value);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (input.metadata !== void 0) request.metadata = input.metadata;
|
|
98
|
+
if (input.resume !== void 0) {
|
|
99
|
+
const resume = requireRecord(input.resume, "Client stream request.resume");
|
|
100
|
+
requireOnlyKeys(resume, ["streamId", "after"], "Client stream request.resume");
|
|
101
|
+
if (typeof resume.streamId !== "string" || !isEventId(resume.after)) throw new ClientProtocolError("Client stream resume cursor requires streamId and a nonnegative safe integer after.", value);
|
|
102
|
+
request.resume = {
|
|
103
|
+
streamId: resume.streamId,
|
|
104
|
+
after: resume.after
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
return request;
|
|
539
108
|
}
|
|
540
109
|
function parseClientStreamEvent(value, options = {}) {
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
110
|
+
const event = requireRecord(value, "Client stream event");
|
|
111
|
+
if (!isJsonValue$1(event)) throw new ClientProtocolError("Client stream events must be strict JSON values.", value);
|
|
112
|
+
if (typeof event.type !== "string" || typeof event.runId !== "string") throw new ClientProtocolError("Client stream events require type and runId.", value);
|
|
113
|
+
if (event.turn !== void 0 && !isPositiveEventId(event.turn)) throw new ClientProtocolError("Client stream event.turn must be a positive safe integer.", value);
|
|
114
|
+
if (event.scope !== void 0 && !isScope(event.scope)) throw new ClientProtocolError("Client stream event.scope is invalid.", value);
|
|
115
|
+
switch (event.type) {
|
|
116
|
+
case "run_start":
|
|
117
|
+
requireEventKeys(event, ["source", "metadata"]);
|
|
118
|
+
requireOneOf(event.source, ["completion", "agent"], "run_start.source", value);
|
|
119
|
+
break;
|
|
120
|
+
case "turn_start":
|
|
121
|
+
requireEventKeys(event, []);
|
|
122
|
+
if (!isPositiveEventId(event.turn)) invalid("turn_start.turn", value);
|
|
123
|
+
break;
|
|
124
|
+
case "generation_start":
|
|
125
|
+
requireEventKeys(event, ["model"]);
|
|
126
|
+
if (event.model !== void 0 && (!isRecord$1(event.model) || !hasOnlyKeys(event.model, ["provider", "modelId"]) || typeof event.model.provider !== "string" || typeof event.model.modelId !== "string")) invalid("generation_start.model", value);
|
|
127
|
+
break;
|
|
128
|
+
case "message_reset":
|
|
129
|
+
requireEventKeys(event, ["messageId"]);
|
|
130
|
+
requireStrings(event, ["messageId"], value);
|
|
131
|
+
break;
|
|
132
|
+
case "message_start":
|
|
133
|
+
requireEventKeys(event, [
|
|
134
|
+
"messageId",
|
|
135
|
+
"role",
|
|
136
|
+
"metadata"
|
|
137
|
+
]);
|
|
138
|
+
requireStrings(event, ["messageId"], value);
|
|
139
|
+
if (event.role !== "assistant") invalid("message_start.role", value);
|
|
140
|
+
break;
|
|
141
|
+
case "text_start":
|
|
142
|
+
requireEventKeys(event, ["messageId", "partId"]);
|
|
143
|
+
requireStrings(event, ["messageId", "partId"], value);
|
|
144
|
+
break;
|
|
145
|
+
case "text_delta":
|
|
146
|
+
requireEventKeys(event, [
|
|
147
|
+
"messageId",
|
|
148
|
+
"partId",
|
|
149
|
+
"delta"
|
|
150
|
+
]);
|
|
151
|
+
requireStrings(event, [
|
|
152
|
+
"messageId",
|
|
153
|
+
"partId",
|
|
154
|
+
"delta"
|
|
155
|
+
], value);
|
|
156
|
+
break;
|
|
157
|
+
case "reasoning_delta":
|
|
158
|
+
requireEventKeys(event, [
|
|
159
|
+
"messageId",
|
|
160
|
+
"partId",
|
|
161
|
+
"delta",
|
|
162
|
+
"contentType",
|
|
163
|
+
"signature"
|
|
164
|
+
]);
|
|
165
|
+
requireStrings(event, [
|
|
166
|
+
"messageId",
|
|
167
|
+
"partId",
|
|
168
|
+
"delta"
|
|
169
|
+
], value);
|
|
170
|
+
requireOptionalStrings(event, ["signature"], value);
|
|
171
|
+
if (event.contentType !== void 0 && ![
|
|
172
|
+
"text",
|
|
173
|
+
"summary",
|
|
174
|
+
"encrypted",
|
|
175
|
+
"redacted"
|
|
176
|
+
].includes(event.contentType)) invalid("reasoning_delta.contentType", value);
|
|
177
|
+
break;
|
|
178
|
+
case "text_end":
|
|
179
|
+
requireEventKeys(event, [
|
|
180
|
+
"messageId",
|
|
181
|
+
"partId",
|
|
182
|
+
"text",
|
|
183
|
+
"signature"
|
|
184
|
+
]);
|
|
185
|
+
requireStrings(event, ["messageId", "partId"], value);
|
|
186
|
+
requireOptionalStrings(event, ["text", "signature"], value);
|
|
187
|
+
break;
|
|
188
|
+
case "reasoning_start":
|
|
189
|
+
requireEventKeys(event, [
|
|
190
|
+
"messageId",
|
|
191
|
+
"partId",
|
|
192
|
+
"reasoningId"
|
|
193
|
+
]);
|
|
194
|
+
requireStrings(event, ["messageId", "partId"], value);
|
|
195
|
+
requireOptionalStrings(event, ["reasoningId"], value);
|
|
196
|
+
break;
|
|
197
|
+
case "reasoning_end":
|
|
198
|
+
requireEventKeys(event, [
|
|
199
|
+
"messageId",
|
|
200
|
+
"partId",
|
|
201
|
+
"text",
|
|
202
|
+
"content"
|
|
203
|
+
]);
|
|
204
|
+
requireStrings(event, ["messageId", "partId"], value);
|
|
205
|
+
requireOptionalStrings(event, ["text"], value);
|
|
206
|
+
if (event.content !== void 0 && (!Array.isArray(event.content) || !event.content.every(isReasoningContent))) invalid("reasoning_end.content", value);
|
|
207
|
+
break;
|
|
208
|
+
case "tool_call_start":
|
|
209
|
+
requireEventKeys(event, [
|
|
210
|
+
"messageId",
|
|
211
|
+
"partId",
|
|
212
|
+
"toolCallId",
|
|
213
|
+
"callId",
|
|
214
|
+
"toolName"
|
|
215
|
+
]);
|
|
216
|
+
requireStrings(event, [
|
|
217
|
+
"messageId",
|
|
218
|
+
"partId",
|
|
219
|
+
"toolCallId"
|
|
220
|
+
], value);
|
|
221
|
+
requireOptionalStrings(event, ["callId", "toolName"], value);
|
|
222
|
+
break;
|
|
223
|
+
case "tool_call_delta":
|
|
224
|
+
requireEventKeys(event, [
|
|
225
|
+
"messageId",
|
|
226
|
+
"partId",
|
|
227
|
+
"toolCallId",
|
|
228
|
+
"callId",
|
|
229
|
+
"toolName",
|
|
230
|
+
"delta",
|
|
231
|
+
"mode",
|
|
232
|
+
"signature"
|
|
233
|
+
]);
|
|
234
|
+
requireStrings(event, [
|
|
235
|
+
"messageId",
|
|
236
|
+
"partId",
|
|
237
|
+
"toolCallId",
|
|
238
|
+
"delta"
|
|
239
|
+
], value);
|
|
240
|
+
requireOptionalStrings(event, ["callId", "toolName"], value);
|
|
241
|
+
requireOptionalStrings(event, ["signature"], value);
|
|
242
|
+
requireOneOf(event.mode, ["append", "replace"], "tool_call_delta.mode", value);
|
|
243
|
+
break;
|
|
244
|
+
case "tool_call_end":
|
|
245
|
+
requireEventKeys(event, [
|
|
246
|
+
"messageId",
|
|
247
|
+
"partId",
|
|
248
|
+
"toolCallId",
|
|
249
|
+
"callId",
|
|
250
|
+
"toolName",
|
|
251
|
+
"input",
|
|
252
|
+
"signature"
|
|
253
|
+
]);
|
|
254
|
+
requireStrings(event, [
|
|
255
|
+
"messageId",
|
|
256
|
+
"partId",
|
|
257
|
+
"toolCallId",
|
|
258
|
+
"toolName"
|
|
259
|
+
], value);
|
|
260
|
+
requireOptionalStrings(event, ["callId", "signature"], value);
|
|
261
|
+
if (!isJsonValue$1(event.input)) invalid("tool_call_end.input", value);
|
|
262
|
+
break;
|
|
263
|
+
case "tool_result":
|
|
264
|
+
requireEventKeys(event, [
|
|
265
|
+
"messageId",
|
|
266
|
+
"partId",
|
|
267
|
+
"toolCallId",
|
|
268
|
+
"callId",
|
|
269
|
+
"internalCallId",
|
|
270
|
+
"toolName",
|
|
271
|
+
"input",
|
|
272
|
+
"result"
|
|
273
|
+
]);
|
|
274
|
+
requireStrings(event, [
|
|
275
|
+
"messageId",
|
|
276
|
+
"partId",
|
|
277
|
+
"toolCallId",
|
|
278
|
+
"toolName"
|
|
279
|
+
], value);
|
|
280
|
+
requireOptionalStrings(event, ["callId", "internalCallId"], value);
|
|
281
|
+
if (!isJsonValue$1(event.input)) invalid("tool_result.input", value);
|
|
282
|
+
if (!isRecord$1(event.result)) invalid("tool_result.result", value);
|
|
283
|
+
if (event.result.status === "success") {
|
|
284
|
+
if (!hasOnlyKeys(event.result, [
|
|
285
|
+
"status",
|
|
286
|
+
"output",
|
|
287
|
+
"content"
|
|
288
|
+
]) || !isJsonValue$1(event.result.output) || event.result.content !== void 0 && (!Array.isArray(event.result.content) || !event.result.content.every(isToolResultContent))) invalid("tool_result.result", value);
|
|
289
|
+
} else if (event.result.status === "error") {
|
|
290
|
+
if (!hasOnlyKeys(event.result, ["status", "error"]) || !isClientStreamError(event.result.error)) invalid("tool_result.result", value);
|
|
291
|
+
} else invalid("tool_result.result.status", value);
|
|
292
|
+
break;
|
|
293
|
+
case "source":
|
|
294
|
+
requireEventKeys(event, [
|
|
295
|
+
"messageId",
|
|
296
|
+
"partId",
|
|
297
|
+
"source"
|
|
298
|
+
]);
|
|
299
|
+
requireStrings(event, ["messageId", "partId"], value);
|
|
300
|
+
if (!isCompletionSource(event.source)) invalid("source.source", value);
|
|
301
|
+
break;
|
|
302
|
+
case "attachment":
|
|
303
|
+
requireEventKeys(event, [
|
|
304
|
+
"messageId",
|
|
305
|
+
"partId",
|
|
306
|
+
"attachment"
|
|
307
|
+
]);
|
|
308
|
+
requireStrings(event, ["messageId", "partId"], value);
|
|
309
|
+
if (!isAttachment(event.attachment)) invalid("attachment.attachment", value);
|
|
310
|
+
break;
|
|
311
|
+
case "provider_tool_call":
|
|
312
|
+
requireEventKeys(event, ["toolCall"]);
|
|
313
|
+
if (!isProviderToolCall(event.toolCall)) invalid("provider_tool_call.toolCall", value);
|
|
314
|
+
break;
|
|
315
|
+
case "interaction":
|
|
316
|
+
requireEventKeys(event, ["interaction"]);
|
|
317
|
+
try {
|
|
318
|
+
parseAgentInteractionRequest(event.interaction);
|
|
319
|
+
} catch {
|
|
320
|
+
invalid("interaction.interaction", value);
|
|
321
|
+
}
|
|
322
|
+
break;
|
|
323
|
+
case "guardrail_decision":
|
|
324
|
+
requireEventKeys(event, ["decision"]);
|
|
325
|
+
if (!isGuardrailDecision(event.decision)) invalid("guardrail_decision.decision", value);
|
|
326
|
+
break;
|
|
327
|
+
case "data":
|
|
328
|
+
requireEventKeys(event, [
|
|
329
|
+
"name",
|
|
330
|
+
"data",
|
|
331
|
+
"transient"
|
|
332
|
+
]);
|
|
333
|
+
requireStrings(event, ["name"], value);
|
|
334
|
+
if (!isJsonValue$1(event.data)) invalid("data.data", value);
|
|
335
|
+
if (event.transient !== void 0 && typeof event.transient !== "boolean") invalid("data.transient", value);
|
|
336
|
+
return {
|
|
337
|
+
...event,
|
|
338
|
+
data: parseDataEvent(event.name, event.data, options.dataSchemas, value)
|
|
339
|
+
};
|
|
340
|
+
case "message_end":
|
|
341
|
+
requireEventKeys(event, [
|
|
342
|
+
"messageId",
|
|
343
|
+
"modelMessageId",
|
|
344
|
+
"parts",
|
|
345
|
+
"usage",
|
|
346
|
+
"contextUsage",
|
|
347
|
+
"metadata"
|
|
348
|
+
]);
|
|
349
|
+
requireStrings(event, ["messageId"], value);
|
|
350
|
+
requireOptionalStrings(event, ["modelMessageId"], value);
|
|
351
|
+
if (event.parts !== void 0) {
|
|
352
|
+
if (!Array.isArray(event.parts)) invalid("message_end.parts", value);
|
|
353
|
+
const parts = event.parts.map((part) => parseUIMessagePart(part, options.dataSchemas, value));
|
|
354
|
+
validateUsageAndContext(event, value);
|
|
355
|
+
return parseEventMetadata({
|
|
356
|
+
...event,
|
|
357
|
+
parts
|
|
358
|
+
}, options.metadataSchema, value);
|
|
359
|
+
}
|
|
360
|
+
validateUsageAndContext(event, value);
|
|
361
|
+
break;
|
|
362
|
+
case "turn_end":
|
|
363
|
+
requireEventKeys(event, [
|
|
364
|
+
"usage",
|
|
365
|
+
"contextUsage",
|
|
366
|
+
"firstDeltaMs"
|
|
367
|
+
]);
|
|
368
|
+
if (!isPositiveEventId(event.turn)) invalid("turn_end.turn", value);
|
|
369
|
+
if (event.firstDeltaMs !== void 0 && !isNonnegativeNumber(event.firstDeltaMs)) invalid("turn_end.firstDeltaMs", value);
|
|
370
|
+
validateUsageAndContext(event, value);
|
|
371
|
+
break;
|
|
372
|
+
case "memory_compaction":
|
|
373
|
+
requireEventKeys(event, [
|
|
374
|
+
"originalMessageCount",
|
|
375
|
+
"compactedMessageCount",
|
|
376
|
+
"retainedMessageCount",
|
|
377
|
+
"originalTokenCount",
|
|
378
|
+
"compactedTokenCount",
|
|
379
|
+
"retainedTokenCount",
|
|
380
|
+
"resultTokenCount",
|
|
381
|
+
"attempts",
|
|
382
|
+
"usage"
|
|
383
|
+
]);
|
|
384
|
+
validateMemoryCompactionInfo(event, value);
|
|
385
|
+
break;
|
|
386
|
+
case "run_end":
|
|
387
|
+
requireEventKeys(event, [
|
|
388
|
+
"status",
|
|
389
|
+
"text",
|
|
390
|
+
"output",
|
|
391
|
+
"usage",
|
|
392
|
+
"contextUsage",
|
|
393
|
+
"trace",
|
|
394
|
+
"memoryCompaction",
|
|
395
|
+
"metadata"
|
|
396
|
+
]);
|
|
397
|
+
requireOneOf(event.status, [
|
|
398
|
+
"completed",
|
|
399
|
+
"blocked",
|
|
400
|
+
"suspended",
|
|
401
|
+
"cancelled",
|
|
402
|
+
"error"
|
|
403
|
+
], "run_end.status", value);
|
|
404
|
+
requireOptionalStrings(event, ["text"], value);
|
|
405
|
+
validateUsageAndContext(event, value);
|
|
406
|
+
if (event.trace !== void 0 && !isTrace(event.trace)) invalid("run_end.trace", value);
|
|
407
|
+
if (event.memoryCompaction !== void 0) {
|
|
408
|
+
if (!isRecord$1(event.memoryCompaction)) invalid("run_end.memoryCompaction", value);
|
|
409
|
+
requireOnlyKeys(event.memoryCompaction, [
|
|
410
|
+
"originalMessageCount",
|
|
411
|
+
"compactedMessageCount",
|
|
412
|
+
"retainedMessageCount",
|
|
413
|
+
"originalTokenCount",
|
|
414
|
+
"compactedTokenCount",
|
|
415
|
+
"retainedTokenCount",
|
|
416
|
+
"resultTokenCount",
|
|
417
|
+
"attempts",
|
|
418
|
+
"usage"
|
|
419
|
+
], "run_end.memoryCompaction");
|
|
420
|
+
validateMemoryCompactionInfo(event.memoryCompaction, value);
|
|
421
|
+
}
|
|
422
|
+
break;
|
|
423
|
+
case "error":
|
|
424
|
+
requireEventKeys(event, ["error", "usage"]);
|
|
425
|
+
if (!isClientStreamError(event.error)) invalid("error.error", value);
|
|
426
|
+
if (event.usage !== void 0 && !isUsage(event.usage)) invalid("error.usage", value);
|
|
427
|
+
break;
|
|
428
|
+
default: throw new ClientProtocolError(`Unknown client stream event type "${event.type}".`, value);
|
|
429
|
+
}
|
|
430
|
+
return parseEventMetadata(event, options.metadataSchema, value);
|
|
806
431
|
}
|
|
807
432
|
function parseClientStreamFrame(value, options = {}) {
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
433
|
+
const frame = requireRecord(value, "Client stream frame");
|
|
434
|
+
if (!isJsonValue$1(frame)) throw new ClientProtocolError("Client stream frames must be strict JSON values.", value);
|
|
435
|
+
if (typeof frame.type !== "string") throw new ClientProtocolError("Client stream frames require type.", value);
|
|
436
|
+
if (frame.type === "stream_start") {
|
|
437
|
+
requireOnlyKeys(frame, [
|
|
438
|
+
"type",
|
|
439
|
+
"protocol",
|
|
440
|
+
"streamId",
|
|
441
|
+
"eventId",
|
|
442
|
+
"resumable"
|
|
443
|
+
], "stream_start frame");
|
|
444
|
+
if (frame.protocol === "anvia.client.v1" || frame.protocol === "anvia.client.v2") throw new ClientProtocolError(`Client stream protocol ${frame.protocol.slice(-2)} is not supported.`, value);
|
|
445
|
+
if (frame.protocol !== "anvia.client.v3" || typeof frame.streamId !== "string" || frame.eventId !== 0 || typeof frame.resumable !== "boolean") throw new ClientProtocolError("Invalid client stream_start frame.", value);
|
|
446
|
+
return frame;
|
|
447
|
+
}
|
|
448
|
+
if (frame.type === "stream_event") {
|
|
449
|
+
requireOnlyKeys(frame, [
|
|
450
|
+
"type",
|
|
451
|
+
"streamId",
|
|
452
|
+
"eventId",
|
|
453
|
+
"event"
|
|
454
|
+
], "stream_event frame");
|
|
455
|
+
if (typeof frame.streamId !== "string" || !isPositiveEventId(frame.eventId)) throw new ClientProtocolError("Invalid client stream_event frame.", value);
|
|
456
|
+
const event = parseClientStreamEvent(frame.event, options);
|
|
457
|
+
return {
|
|
458
|
+
...frame,
|
|
459
|
+
event
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
if (frame.type === "stream_end") {
|
|
463
|
+
requireOnlyKeys(frame, [
|
|
464
|
+
"type",
|
|
465
|
+
"streamId",
|
|
466
|
+
"eventId",
|
|
467
|
+
"status"
|
|
468
|
+
], "stream_end frame");
|
|
469
|
+
if (typeof frame.streamId !== "string" || !isEventId(frame.eventId)) throw new ClientProtocolError("Invalid client stream_end frame.", value);
|
|
470
|
+
requireOneOf(frame.status, [
|
|
471
|
+
"completed",
|
|
472
|
+
"error",
|
|
473
|
+
"missing"
|
|
474
|
+
], "stream_end.status", value);
|
|
475
|
+
return frame;
|
|
476
|
+
}
|
|
477
|
+
throw new ClientProtocolError(`Unknown client stream frame type "${frame.type}".`, value);
|
|
849
478
|
}
|
|
850
479
|
function maskedClientError() {
|
|
851
|
-
|
|
480
|
+
return { message: "An unexpected error occurred." };
|
|
852
481
|
}
|
|
853
482
|
function normalizeClientError(value) {
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
483
|
+
if (value instanceof Error) return value;
|
|
484
|
+
if (isClientStreamError(value)) {
|
|
485
|
+
const error = new Error(value.message);
|
|
486
|
+
if (value.name !== void 0) error.name = value.name;
|
|
487
|
+
return error;
|
|
488
|
+
}
|
|
489
|
+
return new Error(typeof value === "string" ? value : "An unexpected error occurred.");
|
|
861
490
|
}
|
|
862
491
|
function parseDataEvent(name, data, schemas, value) {
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
if (parsed.success !== true || !isJsonValue2(parsed.data)) {
|
|
870
|
-
throw new ClientProtocolError(`Invalid data event "${name}".`, value);
|
|
871
|
-
}
|
|
872
|
-
return parsed.data;
|
|
492
|
+
if (schemas === void 0) return data;
|
|
493
|
+
const schema = schemas[name];
|
|
494
|
+
if (schema === void 0) throw new ClientProtocolError(`Invalid data event "${name}".`, value);
|
|
495
|
+
const parsed = schema.safeParse(data);
|
|
496
|
+
if (parsed.success !== true || !isJsonValue$1(parsed.data)) throw new ClientProtocolError(`Invalid data event "${name}".`, value);
|
|
497
|
+
return parsed.data;
|
|
873
498
|
}
|
|
874
499
|
function parseUIMessagePart(value, schemas, root) {
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
return {
|
|
882
|
-
...value,
|
|
883
|
-
data: parseDataEvent(value.name, value.data, schemas, root)
|
|
884
|
-
};
|
|
500
|
+
if (!isUIMessagePart(value)) throw new ClientProtocolError("UI message contains an invalid part.", root);
|
|
501
|
+
if (value.type !== "data") return value;
|
|
502
|
+
return {
|
|
503
|
+
...value,
|
|
504
|
+
data: parseDataEvent(value.name, value.data, schemas, root)
|
|
505
|
+
};
|
|
885
506
|
}
|
|
886
507
|
function parseMetadata(value, schema, root) {
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
if (parsed.success !== true || !isJsonObject(parsed.data)) {
|
|
895
|
-
throw new ClientProtocolError("Invalid client metadata.", root);
|
|
896
|
-
}
|
|
897
|
-
return parsed.data;
|
|
508
|
+
if (schema === void 0) {
|
|
509
|
+
if (!isJsonObject(value)) throw new ClientProtocolError("Client metadata must be a JSON object.", root);
|
|
510
|
+
return value;
|
|
511
|
+
}
|
|
512
|
+
const parsed = schema.safeParse(value);
|
|
513
|
+
if (parsed.success !== true || !isJsonObject(parsed.data)) throw new ClientProtocolError("Invalid client metadata.", root);
|
|
514
|
+
return parsed.data;
|
|
898
515
|
}
|
|
899
516
|
function parseEventMetadata(event, schema, root) {
|
|
900
|
-
|
|
901
|
-
|
|
517
|
+
if (event.metadata === void 0) return event;
|
|
518
|
+
return {
|
|
519
|
+
...event,
|
|
520
|
+
metadata: parseMetadata(event.metadata, schema, root)
|
|
521
|
+
};
|
|
902
522
|
}
|
|
903
523
|
function isUIMessageGeneration(value) {
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
524
|
+
if (!isRecord$1(value)) return false;
|
|
525
|
+
if (!hasOnlyKeys(value, [
|
|
526
|
+
"runId",
|
|
527
|
+
"status",
|
|
528
|
+
"usage",
|
|
529
|
+
"contextUsage",
|
|
530
|
+
"trace",
|
|
531
|
+
"memoryCompaction"
|
|
532
|
+
])) return false;
|
|
533
|
+
return (value.runId === void 0 || typeof value.runId === "string") && (value.status === void 0 || [
|
|
534
|
+
"completed",
|
|
535
|
+
"blocked",
|
|
536
|
+
"suspended",
|
|
537
|
+
"cancelled",
|
|
538
|
+
"error"
|
|
539
|
+
].includes(value.status)) && (value.usage === void 0 || isUsage(value.usage)) && (value.contextUsage === void 0 || isContextUsage(value.contextUsage)) && (value.trace === void 0 || isTrace(value.trace)) && (value.memoryCompaction === void 0 || isRecord$1(value.memoryCompaction) && validateMemoryCompactionForGuard(value.memoryCompaction));
|
|
911
540
|
}
|
|
912
541
|
function validateMemoryCompactionForGuard(value) {
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
return true;
|
|
931
|
-
} catch {
|
|
932
|
-
return false;
|
|
933
|
-
}
|
|
542
|
+
try {
|
|
543
|
+
requireOnlyKeys(value, [
|
|
544
|
+
"originalMessageCount",
|
|
545
|
+
"compactedMessageCount",
|
|
546
|
+
"retainedMessageCount",
|
|
547
|
+
"originalTokenCount",
|
|
548
|
+
"compactedTokenCount",
|
|
549
|
+
"retainedTokenCount",
|
|
550
|
+
"resultTokenCount",
|
|
551
|
+
"attempts",
|
|
552
|
+
"usage"
|
|
553
|
+
], "memory compaction");
|
|
554
|
+
validateMemoryCompactionInfo(value, value);
|
|
555
|
+
return true;
|
|
556
|
+
} catch {
|
|
557
|
+
return false;
|
|
558
|
+
}
|
|
934
559
|
}
|
|
935
560
|
function isJsonObject(value) {
|
|
936
|
-
|
|
561
|
+
return isRecord$1(value) && isJsonValue$1(value);
|
|
937
562
|
}
|
|
938
563
|
function isClientStreamError(value) {
|
|
939
|
-
|
|
564
|
+
return isRecord$1(value) && hasOnlyKeys(value, [
|
|
565
|
+
"name",
|
|
566
|
+
"message",
|
|
567
|
+
"code",
|
|
568
|
+
"retryable",
|
|
569
|
+
"details"
|
|
570
|
+
]) && typeof value.message === "string" && (value.name === void 0 || typeof value.name === "string") && (value.code === void 0 || typeof value.code === "string") && (value.retryable === void 0 || typeof value.retryable === "boolean") && (value.details === void 0 || isJsonValue$1(value.details));
|
|
940
571
|
}
|
|
941
572
|
function isScope(value) {
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
573
|
+
if (!isRecord$1(value)) return false;
|
|
574
|
+
const keys = [
|
|
575
|
+
"agentId",
|
|
576
|
+
"agentName",
|
|
577
|
+
"parentRunId",
|
|
578
|
+
"parentToolName",
|
|
579
|
+
"parentToolCallId",
|
|
580
|
+
"parentInternalToolCallId"
|
|
581
|
+
];
|
|
582
|
+
return hasOnlyKeys(value, keys) && keys.every((key) => value[key] === void 0 || typeof value[key] === "string");
|
|
952
583
|
}
|
|
953
584
|
function isAttachment(value) {
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
585
|
+
if (!isRecord$1(value) || typeof value.id !== "string") return false;
|
|
586
|
+
if (value.type !== "image" && value.type !== "document" && value.type !== "file") return false;
|
|
587
|
+
return hasOnlyKeys(value, [
|
|
588
|
+
"id",
|
|
589
|
+
"type",
|
|
590
|
+
"name",
|
|
591
|
+
"mediaType",
|
|
592
|
+
"url",
|
|
593
|
+
"data",
|
|
594
|
+
"text",
|
|
595
|
+
"detail",
|
|
596
|
+
"metadata"
|
|
597
|
+
]) && (value.name === void 0 || typeof value.name === "string") && (value.mediaType === void 0 || typeof value.mediaType === "string") && (value.url === void 0 || typeof value.url === "string") && (value.data === void 0 || typeof value.data === "string") && (value.text === void 0 || typeof value.text === "string") && (value.detail === void 0 || [
|
|
598
|
+
"auto",
|
|
599
|
+
"low",
|
|
600
|
+
"high"
|
|
601
|
+
].includes(value.detail)) && (value.metadata === void 0 || isJsonValue$1(value.metadata));
|
|
967
602
|
}
|
|
968
603
|
function isUIMessagePart(value) {
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
604
|
+
if (!isRecord$1(value) || typeof value.id !== "string" || typeof value.type !== "string") return false;
|
|
605
|
+
if (value.type === "text") return hasOnlyKeys(value, [
|
|
606
|
+
"id",
|
|
607
|
+
"type",
|
|
608
|
+
"text",
|
|
609
|
+
"signature"
|
|
610
|
+
]) && typeof value.text === "string" && (value.signature === void 0 || typeof value.signature === "string");
|
|
611
|
+
if (value.type === "reasoning") return hasOnlyKeys(value, [
|
|
612
|
+
"id",
|
|
613
|
+
"type",
|
|
614
|
+
"text",
|
|
615
|
+
"reasoningId",
|
|
616
|
+
"content"
|
|
617
|
+
]) && typeof value.text === "string" && (value.reasoningId === void 0 || typeof value.reasoningId === "string") && (value.content === void 0 || Array.isArray(value.content) && value.content.every(isReasoningContent));
|
|
618
|
+
if (value.type === "source") return hasOnlyKeys(value, [
|
|
619
|
+
"id",
|
|
620
|
+
"type",
|
|
621
|
+
"source"
|
|
622
|
+
]) && isCompletionSource(value.source);
|
|
623
|
+
if (value.type === "data") return hasOnlyKeys(value, [
|
|
624
|
+
"id",
|
|
625
|
+
"type",
|
|
626
|
+
"name",
|
|
627
|
+
"data"
|
|
628
|
+
]) && typeof value.name === "string" && isJsonValue$1(value.data);
|
|
629
|
+
if (value.type === "attachment") return hasOnlyKeys(value, [
|
|
630
|
+
"id",
|
|
631
|
+
"type",
|
|
632
|
+
"attachment"
|
|
633
|
+
]) && isAttachment(value.attachment);
|
|
634
|
+
if (value.type === "error") return hasOnlyKeys(value, [
|
|
635
|
+
"id",
|
|
636
|
+
"type",
|
|
637
|
+
"error"
|
|
638
|
+
]) && isClientStreamError(value.error);
|
|
639
|
+
if (value.type !== "tool" || !hasOnlyKeys(value, [
|
|
640
|
+
"id",
|
|
641
|
+
"type",
|
|
642
|
+
"toolName",
|
|
643
|
+
"toolCallId",
|
|
644
|
+
"callId",
|
|
645
|
+
"internalCallId",
|
|
646
|
+
"turn",
|
|
647
|
+
"state",
|
|
648
|
+
"input",
|
|
649
|
+
"output",
|
|
650
|
+
"resultContent",
|
|
651
|
+
"signature",
|
|
652
|
+
"error"
|
|
653
|
+
]) || typeof value.toolName !== "string" || typeof value.toolCallId !== "string" || value.callId !== void 0 && typeof value.callId !== "string" || value.internalCallId !== void 0 && typeof value.internalCallId !== "string" || value.turn !== void 0 && !isPositiveEventId(value.turn) || value.signature !== void 0 && typeof value.signature !== "string") return false;
|
|
654
|
+
if (value.state === "input-streaming") return typeof value.input === "string" && value.output === void 0 && value.resultContent === void 0 && value.error === void 0;
|
|
655
|
+
if (value.state === "input-available") return isJsonValue$1(value.input) && value.output === void 0 && value.resultContent === void 0 && value.error === void 0;
|
|
656
|
+
if (value.state === "output-available") return isJsonValue$1(value.input) && isJsonValue$1(value.output) && (value.resultContent === void 0 || Array.isArray(value.resultContent) && value.resultContent.every(isToolResultContent)) && value.error === void 0;
|
|
657
|
+
return value.state === "error" && isJsonValue$1(value.input) && value.output === void 0 && value.resultContent === void 0 && isClientStreamError(value.error);
|
|
1017
658
|
}
|
|
1018
659
|
function isReasoningContent(value) {
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
}
|
|
1029
|
-
return false;
|
|
660
|
+
if (!isRecord$1(value) || typeof value.type !== "string") return false;
|
|
661
|
+
if (value.type === "text") return hasOnlyKeys(value, [
|
|
662
|
+
"type",
|
|
663
|
+
"text",
|
|
664
|
+
"signature"
|
|
665
|
+
]) && typeof value.text === "string" && (value.signature === void 0 || typeof value.signature === "string");
|
|
666
|
+
if (value.type === "summary") return hasOnlyKeys(value, ["type", "text"]) && typeof value.text === "string";
|
|
667
|
+
if (value.type === "encrypted" || value.type === "redacted") return hasOnlyKeys(value, ["type", "data"]) && typeof value.data === "string";
|
|
668
|
+
return false;
|
|
1030
669
|
}
|
|
1031
670
|
function isToolResultContent(value) {
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
671
|
+
if (!isRecord$1(value)) return false;
|
|
672
|
+
if (value.type === "text") return hasOnlyKeys(value, [
|
|
673
|
+
"type",
|
|
674
|
+
"text",
|
|
675
|
+
"signature"
|
|
676
|
+
]) && typeof value.text === "string" && (value.signature === void 0 || typeof value.signature === "string");
|
|
677
|
+
return value.type === "file" && hasOnlyKeys(value, [
|
|
678
|
+
"type",
|
|
679
|
+
"data",
|
|
680
|
+
"mediaType",
|
|
681
|
+
"filename"
|
|
682
|
+
]) && isFileData(value.data) && typeof value.mediaType === "string" && (value.filename === void 0 || typeof value.filename === "string");
|
|
1037
683
|
}
|
|
1038
684
|
function isFileData(value) {
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
if (value.type === "data") {
|
|
1044
|
-
return hasOnlyKeys(value, ["type", "data"]) && typeof value.data === "string";
|
|
1045
|
-
}
|
|
1046
|
-
return value.type === "text" && hasOnlyKeys(value, ["type", "text"]) && typeof value.text === "string";
|
|
685
|
+
if (!isRecord$1(value)) return false;
|
|
686
|
+
if (value.type === "url") return hasOnlyKeys(value, ["type", "url"]) && typeof value.url === "string";
|
|
687
|
+
if (value.type === "data") return hasOnlyKeys(value, ["type", "data"]) && typeof value.data === "string";
|
|
688
|
+
return value.type === "text" && hasOnlyKeys(value, ["type", "text"]) && typeof value.text === "string";
|
|
1047
689
|
}
|
|
1048
690
|
function isCompletionSource(value) {
|
|
1049
|
-
|
|
691
|
+
return isRecord$1(value) && value.type === "url" && hasOnlyKeys(value, [
|
|
692
|
+
"type",
|
|
693
|
+
"url",
|
|
694
|
+
"title",
|
|
695
|
+
"id",
|
|
696
|
+
"startIndex",
|
|
697
|
+
"endIndex"
|
|
698
|
+
]) && typeof value.url === "string" && (value.title === void 0 || typeof value.title === "string") && (value.id === void 0 || typeof value.id === "string") && (value.startIndex === void 0 || isNonnegativeNumber(value.startIndex)) && (value.endIndex === void 0 || isNonnegativeNumber(value.endIndex));
|
|
1050
699
|
}
|
|
1051
700
|
function isProviderToolCall(value) {
|
|
1052
|
-
|
|
701
|
+
return isRecord$1(value) && hasOnlyKeys(value, [
|
|
702
|
+
"id",
|
|
703
|
+
"name",
|
|
704
|
+
"status",
|
|
705
|
+
"details"
|
|
706
|
+
]) && typeof value.id === "string" && typeof value.name === "string" && (value.status === void 0 || typeof value.status === "string") && (value.details === void 0 || isRecord$1(value.details) && isJsonValue$1(value.details));
|
|
1053
707
|
}
|
|
1054
708
|
function isGuardrailDecision(value) {
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
709
|
+
return isRecord$1(value) && hasOnlyKeys(value, [
|
|
710
|
+
"policyId",
|
|
711
|
+
"guardrailId",
|
|
712
|
+
"boundary",
|
|
713
|
+
"mode",
|
|
714
|
+
"action",
|
|
715
|
+
"applied",
|
|
716
|
+
"reason",
|
|
717
|
+
"message",
|
|
718
|
+
"metadata",
|
|
719
|
+
"latencyMs"
|
|
720
|
+
]) && typeof value.policyId === "string" && typeof value.guardrailId === "string" && (value.boundary === "input" || value.boundary === "output") && (value.mode === "enforce" || value.mode === "observe") && typeof value.action === "string" && [
|
|
721
|
+
"allow",
|
|
722
|
+
"block",
|
|
723
|
+
"rewrite",
|
|
724
|
+
"error"
|
|
725
|
+
].includes(value.action) && typeof value.applied === "boolean" && (value.reason === void 0 || typeof value.reason === "string") && (value.message === void 0 || typeof value.message === "string") && (value.metadata === void 0 || isRecord$1(value.metadata) && isJsonValue$1(value.metadata)) && isNonnegativeNumber(value.latencyMs);
|
|
1067
726
|
}
|
|
1068
727
|
function isUsage(value) {
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
}
|
|
1085
|
-
return value.details === void 0 || isRecord(value.details) && Object.values(value.details).every(isNonnegativeNumber);
|
|
728
|
+
if (!isRecord$1(value) || !hasOnlyKeys(value, [
|
|
729
|
+
"inputTokens",
|
|
730
|
+
"outputTokens",
|
|
731
|
+
"totalTokens",
|
|
732
|
+
"cachedInputTokens",
|
|
733
|
+
"cacheCreationInputTokens",
|
|
734
|
+
"details"
|
|
735
|
+
]) || ![
|
|
736
|
+
value.inputTokens,
|
|
737
|
+
value.outputTokens,
|
|
738
|
+
value.totalTokens,
|
|
739
|
+
value.cachedInputTokens,
|
|
740
|
+
value.cacheCreationInputTokens
|
|
741
|
+
].every(isNonnegativeNumber)) return false;
|
|
742
|
+
return value.details === void 0 || isRecord$1(value.details) && Object.values(value.details).every(isNonnegativeNumber);
|
|
1086
743
|
}
|
|
1087
744
|
function isContextUsage(value) {
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
745
|
+
if (!isRecord$1(value) || !isRecord$1(value.model) || !isRecord$1(value.model.context)) return false;
|
|
746
|
+
const context = value.model.context;
|
|
747
|
+
return hasOnlyKeys(value, [
|
|
748
|
+
"model",
|
|
749
|
+
"usedTokens",
|
|
750
|
+
"remainingTokens",
|
|
751
|
+
"usedPercent",
|
|
752
|
+
"remainingPercent"
|
|
753
|
+
]) && hasOnlyKeys(value.model, ["modelId", "context"]) && typeof value.model.modelId === "string" && hasOnlyKeys(context, [
|
|
754
|
+
"contextWindow",
|
|
755
|
+
"maxInputTokens",
|
|
756
|
+
"maxOutputTokens"
|
|
757
|
+
]) && isNonnegativeNumber(context.contextWindow) && (context.maxInputTokens === void 0 || isNonnegativeNumber(context.maxInputTokens)) && (context.maxOutputTokens === void 0 || isNonnegativeNumber(context.maxOutputTokens)) && isNonnegativeNumber(value.usedTokens) && isNonnegativeNumber(value.remainingTokens) && isNonnegativeNumber(value.usedPercent) && isNonnegativeNumber(value.remainingPercent);
|
|
1097
758
|
}
|
|
1098
759
|
function validateUsageAndContext(event, original) {
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
}
|
|
1102
|
-
if (event.contextUsage !== void 0 && !isContextUsage(event.contextUsage)) {
|
|
1103
|
-
invalid(`${String(event.type)}.contextUsage`, original);
|
|
1104
|
-
}
|
|
760
|
+
if (event.usage !== void 0 && !isUsage(event.usage)) invalid(`${String(event.type)}.usage`, original);
|
|
761
|
+
if (event.contextUsage !== void 0 && !isContextUsage(event.contextUsage)) invalid(`${String(event.type)}.contextUsage`, original);
|
|
1105
762
|
}
|
|
1106
763
|
function validateMemoryCompactionInfo(value, original) {
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
if (!isPositiveEventId(value.attempts)) invalid("memory_compaction.attempts", original);
|
|
1119
|
-
if (!isUsage(value.usage)) invalid("memory_compaction.usage", original);
|
|
764
|
+
for (const field of [
|
|
765
|
+
"originalMessageCount",
|
|
766
|
+
"compactedMessageCount",
|
|
767
|
+
"retainedMessageCount",
|
|
768
|
+
"originalTokenCount",
|
|
769
|
+
"compactedTokenCount",
|
|
770
|
+
"retainedTokenCount",
|
|
771
|
+
"resultTokenCount"
|
|
772
|
+
]) if (!isEventId(value[field])) invalid(`memory_compaction.${field}`, original);
|
|
773
|
+
if (!isPositiveEventId(value.attempts)) invalid("memory_compaction.attempts", original);
|
|
774
|
+
if (!isUsage(value.usage)) invalid("memory_compaction.usage", original);
|
|
1120
775
|
}
|
|
1121
776
|
function isTrace(value) {
|
|
1122
|
-
|
|
777
|
+
return isRecord$1(value) && hasOnlyKeys(value, [
|
|
778
|
+
"observer",
|
|
779
|
+
"traceId",
|
|
780
|
+
"observationId"
|
|
781
|
+
]) && typeof value.observer === "string" && value.observer.trim().length > 0 && (value.traceId === void 0 || typeof value.traceId === "string") && (value.observationId === void 0 || typeof value.observationId === "string");
|
|
1123
782
|
}
|
|
1124
783
|
function requireRecord(value, label) {
|
|
1125
|
-
|
|
1126
|
-
|
|
784
|
+
if (!isRecord$1(value)) throw new ClientProtocolError(`${label} must be an object.`, value);
|
|
785
|
+
return value;
|
|
1127
786
|
}
|
|
1128
787
|
function requireOnlyKeys(value, keys, label) {
|
|
1129
|
-
|
|
1130
|
-
if (!keys.includes(key)) throw new ClientProtocolError(`${label} has unknown field "${key}".`);
|
|
1131
|
-
}
|
|
788
|
+
for (const key of Object.keys(value)) if (!keys.includes(key)) throw new ClientProtocolError(`${label} has unknown field "${key}".`);
|
|
1132
789
|
}
|
|
1133
790
|
function requireEventKeys(value, keys) {
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
791
|
+
requireOnlyKeys(value, [
|
|
792
|
+
"type",
|
|
793
|
+
"runId",
|
|
794
|
+
"turn",
|
|
795
|
+
"scope",
|
|
796
|
+
...keys
|
|
797
|
+
], `${String(value.type)} event`);
|
|
1139
798
|
}
|
|
1140
799
|
function requireStrings(value, keys, original) {
|
|
1141
|
-
|
|
1142
|
-
if (typeof value[key] !== "string") invalid(`${String(value.type)}.${key}`, original);
|
|
1143
|
-
}
|
|
800
|
+
for (const key of keys) if (typeof value[key] !== "string") invalid(`${String(value.type)}.${key}`, original);
|
|
1144
801
|
}
|
|
1145
802
|
function requireOptionalStrings(value, keys, original) {
|
|
1146
|
-
|
|
1147
|
-
if (value[key] !== void 0 && typeof value[key] !== "string") {
|
|
1148
|
-
invalid(`${String(value.type)}.${key}`, original);
|
|
1149
|
-
}
|
|
1150
|
-
}
|
|
803
|
+
for (const key of keys) if (value[key] !== void 0 && typeof value[key] !== "string") invalid(`${String(value.type)}.${key}`, original);
|
|
1151
804
|
}
|
|
1152
805
|
function requireOneOf(value, allowed, label, original) {
|
|
1153
|
-
|
|
806
|
+
if (typeof value !== "string" || !allowed.includes(value)) invalid(label, original);
|
|
1154
807
|
}
|
|
1155
808
|
function invalid(label, value) {
|
|
1156
|
-
|
|
809
|
+
throw new ClientProtocolError(`Invalid ${label}.`, value);
|
|
1157
810
|
}
|
|
1158
811
|
function isEventId(value) {
|
|
1159
|
-
|
|
812
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
1160
813
|
}
|
|
1161
814
|
function isPositiveEventId(value) {
|
|
1162
|
-
|
|
815
|
+
return isEventId(value) && value > 0;
|
|
1163
816
|
}
|
|
1164
817
|
function isNonnegativeNumber(value) {
|
|
1165
|
-
|
|
818
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0;
|
|
1166
819
|
}
|
|
1167
820
|
function hasOnlyKeys(value, keys) {
|
|
1168
|
-
|
|
821
|
+
return Object.keys(value).every((key) => keys.includes(key));
|
|
1169
822
|
}
|
|
1170
|
-
function isRecord(value) {
|
|
1171
|
-
|
|
823
|
+
function isRecord$1(value) {
|
|
824
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1172
825
|
}
|
|
1173
|
-
|
|
1174
826
|
// src/adapters.ts
|
|
1175
827
|
function completionToClientStream(options) {
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
}
|
|
1249
|
-
}
|
|
1250
|
-
if (!terminal) {
|
|
1251
|
-
yield* finishErroredRun(
|
|
1252
|
-
state,
|
|
1253
|
-
new Error("Completion stream ended without a terminal event."),
|
|
1254
|
-
void 0,
|
|
1255
|
-
options.mapError
|
|
1256
|
-
);
|
|
1257
|
-
}
|
|
1258
|
-
} catch (error) {
|
|
1259
|
-
if (!terminal) yield* finishErroredRun(state, error, void 0, options.mapError);
|
|
1260
|
-
}
|
|
1261
|
-
}
|
|
1262
|
-
}));
|
|
828
|
+
const { events } = options;
|
|
829
|
+
const runId = options.runId ?? createClientId("run");
|
|
830
|
+
return propagateCancellation(events, (source) => ({ async *[Symbol.asyncIterator]() {
|
|
831
|
+
const state = createMessageState({ runId });
|
|
832
|
+
const runStart = {
|
|
833
|
+
type: "run_start",
|
|
834
|
+
source: "completion"
|
|
835
|
+
};
|
|
836
|
+
if (options.metadata !== void 0) runStart.metadata = options.metadata;
|
|
837
|
+
yield clientEvent(state, runStart);
|
|
838
|
+
if (options.model !== void 0) yield clientEvent(state, {
|
|
839
|
+
type: "generation_start",
|
|
840
|
+
model: options.model
|
|
841
|
+
});
|
|
842
|
+
yield* startMessage(state);
|
|
843
|
+
let terminal = false;
|
|
844
|
+
try {
|
|
845
|
+
for await (const event of source) {
|
|
846
|
+
if (terminal) break;
|
|
847
|
+
switch (event.type) {
|
|
848
|
+
case "text_delta":
|
|
849
|
+
yield* appendText(state, event.delta);
|
|
850
|
+
break;
|
|
851
|
+
case "reasoning_delta":
|
|
852
|
+
yield* appendReasoning(state, event);
|
|
853
|
+
break;
|
|
854
|
+
case "tool_call_delta":
|
|
855
|
+
yield* appendToolCall(state, event);
|
|
856
|
+
break;
|
|
857
|
+
case "tool_call":
|
|
858
|
+
yield* finishToolCall(state, event.toolCall);
|
|
859
|
+
break;
|
|
860
|
+
case "source":
|
|
861
|
+
yield* emitSource(state, event.source);
|
|
862
|
+
break;
|
|
863
|
+
case "provider_tool_call":
|
|
864
|
+
yield* emitProviderToolCall(state, event.toolCall);
|
|
865
|
+
break;
|
|
866
|
+
case "message_id":
|
|
867
|
+
state.modelMessageId = event.id;
|
|
868
|
+
break;
|
|
869
|
+
case "final": {
|
|
870
|
+
const result = event.result;
|
|
871
|
+
if (result.messageId !== void 0) state.modelMessageId = result.messageId;
|
|
872
|
+
const finalMetadata = { usage: result.usage };
|
|
873
|
+
if (result.contextUsage !== void 0) finalMetadata.contextUsage = result.contextUsage;
|
|
874
|
+
if (result.sources !== void 0) finalMetadata.sources = result.sources;
|
|
875
|
+
if (result.providerToolCalls !== void 0) finalMetadata.providerToolCalls = result.providerToolCalls;
|
|
876
|
+
yield* finishMessage(state, result.content, finalMetadata);
|
|
877
|
+
const output = clientOutput(result.output, options.mapOutput);
|
|
878
|
+
const runEnd = {
|
|
879
|
+
type: "run_end",
|
|
880
|
+
status: "completed",
|
|
881
|
+
text: result.text,
|
|
882
|
+
usage: result.usage
|
|
883
|
+
};
|
|
884
|
+
if (output !== void 0) runEnd.output = output;
|
|
885
|
+
if (result.contextUsage !== void 0) runEnd.contextUsage = result.contextUsage;
|
|
886
|
+
yield clientEvent(state, runEnd);
|
|
887
|
+
terminal = true;
|
|
888
|
+
break;
|
|
889
|
+
}
|
|
890
|
+
case "error":
|
|
891
|
+
yield* finishErroredRun(state, event.error, event.usage, options.mapError);
|
|
892
|
+
terminal = true;
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
if (!terminal) yield* finishErroredRun(state, /* @__PURE__ */ new Error("Completion stream ended without a terminal event."), void 0, options.mapError);
|
|
896
|
+
} catch (error) {
|
|
897
|
+
if (!terminal) yield* finishErroredRun(state, error, void 0, options.mapError);
|
|
898
|
+
}
|
|
899
|
+
} }));
|
|
1263
900
|
}
|
|
1264
901
|
function agentToClientStream(options) {
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
902
|
+
const { events } = options;
|
|
903
|
+
const runId = options.runId ?? createClientId("run");
|
|
904
|
+
return propagateCancellation(events, (source) => ({ async *[Symbol.asyncIterator]() {
|
|
905
|
+
const runtime = {
|
|
906
|
+
runId,
|
|
907
|
+
options,
|
|
908
|
+
messages: /* @__PURE__ */ new Map(),
|
|
909
|
+
startedScopes: /* @__PURE__ */ new Set(["root"])
|
|
910
|
+
};
|
|
911
|
+
const runStart = {
|
|
912
|
+
type: "run_start",
|
|
913
|
+
source: "agent"
|
|
914
|
+
};
|
|
915
|
+
if (options.metadata !== void 0) runStart.metadata = options.metadata;
|
|
916
|
+
yield rootEvent(runId, runStart);
|
|
917
|
+
let terminal = false;
|
|
918
|
+
try {
|
|
919
|
+
for await (const event of source) {
|
|
920
|
+
if (terminal) break;
|
|
921
|
+
yield* translateAgentEvent(runtime, event, void 0);
|
|
922
|
+
terminal = isRootTerminalAgentEvent(event);
|
|
923
|
+
}
|
|
924
|
+
if (!terminal) {
|
|
925
|
+
const error = mapClientError(/* @__PURE__ */ new Error("Agent stream ended without a terminal event."), options.mapError);
|
|
926
|
+
yield rootEvent(runId, {
|
|
927
|
+
type: "error",
|
|
928
|
+
error
|
|
929
|
+
});
|
|
930
|
+
yield rootEvent(runId, {
|
|
931
|
+
type: "run_end",
|
|
932
|
+
status: "error"
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
} catch (error) {
|
|
936
|
+
if (!terminal) {
|
|
937
|
+
yield rootEvent(runId, {
|
|
938
|
+
type: "error",
|
|
939
|
+
error: mapClientError(error, options.mapError)
|
|
940
|
+
});
|
|
941
|
+
yield rootEvent(runId, {
|
|
942
|
+
type: "run_end",
|
|
943
|
+
status: "error"
|
|
944
|
+
});
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
} }));
|
|
1307
948
|
}
|
|
1308
949
|
function customAgentEventsToClientStream(options) {
|
|
1309
|
-
|
|
1310
|
-
options
|
|
1311
|
-
);
|
|
950
|
+
return agentToClientStream(options);
|
|
1312
951
|
}
|
|
1313
952
|
async function* translateAgentEvent(runtime, event, scope) {
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
if (turn !== void 0) context.turn = turn;
|
|
1496
|
-
if (scope !== void 0) context.scope = scope;
|
|
1497
|
-
const mapped = runtime.options.mapCustomEvent?.(event, context);
|
|
1498
|
-
if (mapped === void 0) return;
|
|
1499
|
-
if (Array.isArray(mapped)) {
|
|
1500
|
-
yield* mapped;
|
|
1501
|
-
} else {
|
|
1502
|
-
yield mapped;
|
|
1503
|
-
}
|
|
1504
|
-
}
|
|
1505
|
-
}
|
|
953
|
+
if (event.type === "agent_tool_event") {
|
|
954
|
+
const childScope = {
|
|
955
|
+
agentId: event.agentId,
|
|
956
|
+
parentRunId: runtime.runId,
|
|
957
|
+
parentToolName: event.toolName,
|
|
958
|
+
parentToolCallId: event.toolCallId ?? event.internalCallId,
|
|
959
|
+
parentInternalToolCallId: event.internalCallId
|
|
960
|
+
};
|
|
961
|
+
if (event.agentName !== void 0) childScope.agentName = event.agentName;
|
|
962
|
+
const key = scopeKey(childScope);
|
|
963
|
+
if (!runtime.startedScopes.has(key)) {
|
|
964
|
+
runtime.startedScopes.add(key);
|
|
965
|
+
yield rootEvent(runtime.runId, {
|
|
966
|
+
type: "run_start",
|
|
967
|
+
source: "agent"
|
|
968
|
+
}, childScope);
|
|
969
|
+
}
|
|
970
|
+
yield* translateAgentEvent(runtime, event.event, childScope);
|
|
971
|
+
return;
|
|
972
|
+
}
|
|
973
|
+
const turn = "turn" in event && typeof event.turn === "number" ? event.turn : void 0;
|
|
974
|
+
switch (event.type) {
|
|
975
|
+
case "memory_compaction":
|
|
976
|
+
yield scopedEvent(runtime.runId, turn, scope, {
|
|
977
|
+
type: "memory_compaction",
|
|
978
|
+
originalMessageCount: event.originalMessageCount,
|
|
979
|
+
compactedMessageCount: event.compactedMessageCount,
|
|
980
|
+
retainedMessageCount: event.retainedMessageCount,
|
|
981
|
+
originalTokenCount: event.originalTokenCount,
|
|
982
|
+
compactedTokenCount: event.compactedTokenCount,
|
|
983
|
+
retainedTokenCount: event.retainedTokenCount,
|
|
984
|
+
resultTokenCount: event.resultTokenCount,
|
|
985
|
+
attempts: event.attempts,
|
|
986
|
+
usage: event.usage
|
|
987
|
+
});
|
|
988
|
+
return;
|
|
989
|
+
case "turn_start":
|
|
990
|
+
yield scopedEvent(runtime.runId, turn, scope, { type: "turn_start" });
|
|
991
|
+
return;
|
|
992
|
+
case "generation_start": {
|
|
993
|
+
let state = getAgentMessage(runtime, event.turn, scope);
|
|
994
|
+
if (state.started && !state.ended) {
|
|
995
|
+
state = getAgentMessage(runtime, event.turn, scope, true);
|
|
996
|
+
yield clientEvent(state, {
|
|
997
|
+
type: "message_reset",
|
|
998
|
+
messageId: state.messageId
|
|
999
|
+
});
|
|
1000
|
+
}
|
|
1001
|
+
yield scopedEvent(runtime.runId, event.turn, scope, {
|
|
1002
|
+
type: "generation_start",
|
|
1003
|
+
model: {
|
|
1004
|
+
provider: event.modelInfo.provider,
|
|
1005
|
+
modelId: event.modelInfo.modelId
|
|
1006
|
+
}
|
|
1007
|
+
});
|
|
1008
|
+
yield* startMessage(state);
|
|
1009
|
+
return;
|
|
1010
|
+
}
|
|
1011
|
+
case "text_delta": {
|
|
1012
|
+
const state = getAgentMessage(runtime, event.turn, scope);
|
|
1013
|
+
yield* startMessage(state);
|
|
1014
|
+
yield* appendText(state, event.delta);
|
|
1015
|
+
return;
|
|
1016
|
+
}
|
|
1017
|
+
case "reasoning_delta": {
|
|
1018
|
+
const state = getAgentMessage(runtime, event.turn, scope);
|
|
1019
|
+
yield* startMessage(state);
|
|
1020
|
+
yield* appendReasoning(state, event);
|
|
1021
|
+
return;
|
|
1022
|
+
}
|
|
1023
|
+
case "tool_call_delta": {
|
|
1024
|
+
const state = getAgentMessage(runtime, event.turn, scope);
|
|
1025
|
+
yield* startMessage(state);
|
|
1026
|
+
yield* appendToolCall(state, event);
|
|
1027
|
+
return;
|
|
1028
|
+
}
|
|
1029
|
+
case "tool_call": {
|
|
1030
|
+
const state = getAgentMessage(runtime, event.turn, scope);
|
|
1031
|
+
yield* startMessage(state);
|
|
1032
|
+
yield* finishToolCall(state, event.toolCall);
|
|
1033
|
+
return;
|
|
1034
|
+
}
|
|
1035
|
+
case "source": {
|
|
1036
|
+
const state = getAgentMessage(runtime, event.turn, scope);
|
|
1037
|
+
yield* startMessage(state);
|
|
1038
|
+
yield* emitSource(state, event.source);
|
|
1039
|
+
return;
|
|
1040
|
+
}
|
|
1041
|
+
case "provider_tool_call":
|
|
1042
|
+
yield* emitProviderToolCall(getAgentMessage(runtime, event.turn, scope), event.toolCall);
|
|
1043
|
+
return;
|
|
1044
|
+
case "tool_result": {
|
|
1045
|
+
const state = getAgentMessage(runtime, event.turn, scope);
|
|
1046
|
+
yield* startMessage(state);
|
|
1047
|
+
const tool = getToolResultState(state, event.toolName, event.toolCallId, event.callId, event.internalCallId);
|
|
1048
|
+
tool.result = clientToolResult(event.output, event.result);
|
|
1049
|
+
const resultEvent = {
|
|
1050
|
+
type: "tool_result",
|
|
1051
|
+
messageId: state.messageId,
|
|
1052
|
+
partId: tool.partId,
|
|
1053
|
+
toolCallId: tool.toolCallId,
|
|
1054
|
+
internalCallId: event.internalCallId,
|
|
1055
|
+
toolName: event.toolName,
|
|
1056
|
+
input: parseJsonOrString(event.args),
|
|
1057
|
+
result: tool.result
|
|
1058
|
+
};
|
|
1059
|
+
if (tool.callId !== void 0) resultEvent.callId = tool.callId;
|
|
1060
|
+
yield clientEvent(state, resultEvent);
|
|
1061
|
+
return;
|
|
1062
|
+
}
|
|
1063
|
+
case "turn_end": {
|
|
1064
|
+
const state = getAgentMessage(runtime, event.turn, scope);
|
|
1065
|
+
yield* startMessage(state);
|
|
1066
|
+
if (event.response.messageId !== void 0) state.modelMessageId = event.response.messageId;
|
|
1067
|
+
const finalMetadata = { usage: event.response.usage };
|
|
1068
|
+
if (event.response.contextUsage !== void 0) finalMetadata.contextUsage = event.response.contextUsage;
|
|
1069
|
+
if (event.response.sources !== void 0) finalMetadata.sources = event.response.sources;
|
|
1070
|
+
if (event.response.providerToolCalls !== void 0) finalMetadata.providerToolCalls = event.response.providerToolCalls;
|
|
1071
|
+
yield* finishMessage(state, event.response.choice, finalMetadata);
|
|
1072
|
+
const turnEnd = {
|
|
1073
|
+
type: "turn_end",
|
|
1074
|
+
usage: event.response.usage
|
|
1075
|
+
};
|
|
1076
|
+
if (event.response.contextUsage !== void 0) turnEnd.contextUsage = event.response.contextUsage;
|
|
1077
|
+
if (event.firstDeltaMs !== void 0) turnEnd.firstDeltaMs = event.firstDeltaMs;
|
|
1078
|
+
yield scopedEvent(runtime.runId, event.turn, scope, turnEnd);
|
|
1079
|
+
return;
|
|
1080
|
+
}
|
|
1081
|
+
case "guardrail_decision":
|
|
1082
|
+
yield scopedEvent(runtime.runId, event.turn, scope, {
|
|
1083
|
+
type: "guardrail_decision",
|
|
1084
|
+
decision: event.decision
|
|
1085
|
+
});
|
|
1086
|
+
return;
|
|
1087
|
+
case "response":
|
|
1088
|
+
case "interaction":
|
|
1089
|
+
case "blocked": {
|
|
1090
|
+
const result = event;
|
|
1091
|
+
if (result.type === "interaction" && scope === void 0) yield scopedEvent(runtime.runId, turn, scope, {
|
|
1092
|
+
type: "interaction",
|
|
1093
|
+
interaction: result.interaction
|
|
1094
|
+
});
|
|
1095
|
+
const output = result.type === "response" ? clientOutput(result.output, runtime.options.mapOutput) : void 0;
|
|
1096
|
+
const runEnd = {
|
|
1097
|
+
type: "run_end",
|
|
1098
|
+
status: agentOutcomeStatus(result.type),
|
|
1099
|
+
text: result.text,
|
|
1100
|
+
usage: result.usage
|
|
1101
|
+
};
|
|
1102
|
+
if (output !== void 0) runEnd.output = output;
|
|
1103
|
+
if (result.contextUsage !== void 0) runEnd.contextUsage = result.contextUsage;
|
|
1104
|
+
if (result.trace !== void 0) runEnd.trace = clientTrace(result.trace);
|
|
1105
|
+
if (result.memoryCompaction !== void 0) runEnd.memoryCompaction = result.memoryCompaction;
|
|
1106
|
+
if (runtime.options.metadata !== void 0) runEnd.metadata = runtime.options.metadata;
|
|
1107
|
+
yield scopedEvent(runtime.runId, turn, scope, runEnd);
|
|
1108
|
+
return;
|
|
1109
|
+
}
|
|
1110
|
+
case "error": {
|
|
1111
|
+
const error = mapClientError(event.error, runtime.options.mapError);
|
|
1112
|
+
yield scopedEvent(runtime.runId, turn, scope, {
|
|
1113
|
+
type: "error",
|
|
1114
|
+
error,
|
|
1115
|
+
usage: event.usage
|
|
1116
|
+
});
|
|
1117
|
+
yield scopedEvent(runtime.runId, turn, scope, {
|
|
1118
|
+
type: "run_end",
|
|
1119
|
+
status: "error",
|
|
1120
|
+
usage: event.usage
|
|
1121
|
+
});
|
|
1122
|
+
return;
|
|
1123
|
+
}
|
|
1124
|
+
default: {
|
|
1125
|
+
const context = { runId: runtime.runId };
|
|
1126
|
+
if (turn !== void 0) context.turn = turn;
|
|
1127
|
+
if (scope !== void 0) context.scope = scope;
|
|
1128
|
+
const mapped = runtime.options.mapCustomEvent?.(event, context);
|
|
1129
|
+
if (mapped === void 0) return;
|
|
1130
|
+
if (Array.isArray(mapped)) yield* mapped;
|
|
1131
|
+
else yield mapped;
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1506
1134
|
}
|
|
1507
1135
|
function createMessageState(input) {
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
}
|
|
1526
|
-
function getAgentMessage(runtime, turn, scope) {
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1136
|
+
const state = {
|
|
1137
|
+
runId: input.runId,
|
|
1138
|
+
messageId: createClientId("msg"),
|
|
1139
|
+
started: false,
|
|
1140
|
+
ended: false,
|
|
1141
|
+
textPartId: createClientId("text"),
|
|
1142
|
+
text: "",
|
|
1143
|
+
textStarted: false,
|
|
1144
|
+
textEnded: false,
|
|
1145
|
+
reasoning: /* @__PURE__ */ new Map(),
|
|
1146
|
+
tools: /* @__PURE__ */ new Map(),
|
|
1147
|
+
sourceFingerprints: /* @__PURE__ */ new Set(),
|
|
1148
|
+
providerToolCallFingerprints: /* @__PURE__ */ new Set()
|
|
1149
|
+
};
|
|
1150
|
+
if (input.turn !== void 0) state.turn = input.turn;
|
|
1151
|
+
if (input.scope !== void 0) state.scope = input.scope;
|
|
1152
|
+
return state;
|
|
1153
|
+
}
|
|
1154
|
+
function getAgentMessage(runtime, turn, scope, reset = false) {
|
|
1155
|
+
const key = `${scopeKey(scope)}:${turn}`;
|
|
1156
|
+
const current = runtime.messages.get(key);
|
|
1157
|
+
if (current !== void 0 && !reset) return current;
|
|
1158
|
+
const input = {
|
|
1159
|
+
runId: runtime.runId,
|
|
1160
|
+
turn
|
|
1161
|
+
};
|
|
1162
|
+
if (scope !== void 0) input.scope = scope;
|
|
1163
|
+
const created = createMessageState(input);
|
|
1164
|
+
if (current !== void 0) {
|
|
1165
|
+
created.messageId = current.messageId;
|
|
1166
|
+
created.started = current.started;
|
|
1167
|
+
}
|
|
1168
|
+
runtime.messages.set(key, created);
|
|
1169
|
+
return created;
|
|
1538
1170
|
}
|
|
1539
1171
|
async function* startMessage(state) {
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1172
|
+
if (state.started) return;
|
|
1173
|
+
state.started = true;
|
|
1174
|
+
yield clientEvent(state, {
|
|
1175
|
+
type: "message_start",
|
|
1176
|
+
messageId: state.messageId,
|
|
1177
|
+
role: "assistant"
|
|
1178
|
+
});
|
|
1547
1179
|
}
|
|
1548
1180
|
async function* appendText(state, delta) {
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1181
|
+
if (!state.textStarted) {
|
|
1182
|
+
state.textStarted = true;
|
|
1183
|
+
yield clientEvent(state, {
|
|
1184
|
+
type: "text_start",
|
|
1185
|
+
messageId: state.messageId,
|
|
1186
|
+
partId: state.textPartId
|
|
1187
|
+
});
|
|
1188
|
+
}
|
|
1189
|
+
state.text += delta;
|
|
1190
|
+
yield clientEvent(state, {
|
|
1191
|
+
type: "text_delta",
|
|
1192
|
+
messageId: state.messageId,
|
|
1193
|
+
partId: state.textPartId,
|
|
1194
|
+
delta
|
|
1195
|
+
});
|
|
1564
1196
|
}
|
|
1565
1197
|
async function* emitSource(state, source) {
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1198
|
+
const fingerprint = valueFingerprint(source);
|
|
1199
|
+
if (state.sourceFingerprints.has(fingerprint)) return;
|
|
1200
|
+
state.sourceFingerprints.add(fingerprint);
|
|
1201
|
+
yield clientEvent(state, {
|
|
1202
|
+
type: "source",
|
|
1203
|
+
messageId: state.messageId,
|
|
1204
|
+
partId: createClientId("source"),
|
|
1205
|
+
source
|
|
1206
|
+
});
|
|
1575
1207
|
}
|
|
1576
1208
|
async function* emitProviderToolCall(state, toolCall) {
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1209
|
+
const fingerprint = valueFingerprint(toolCall);
|
|
1210
|
+
if (state.providerToolCallFingerprints.has(fingerprint)) return;
|
|
1211
|
+
state.providerToolCallFingerprints.add(fingerprint);
|
|
1212
|
+
yield clientEvent(state, {
|
|
1213
|
+
type: "provider_tool_call",
|
|
1214
|
+
toolCall
|
|
1215
|
+
});
|
|
1581
1216
|
}
|
|
1582
1217
|
async function* appendReasoning(state, event) {
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1218
|
+
const key = event.id ?? "default";
|
|
1219
|
+
let reasoning = state.reasoning.get(key);
|
|
1220
|
+
if (reasoning === void 0) {
|
|
1221
|
+
reasoning = {
|
|
1222
|
+
partId: createClientId("reasoning"),
|
|
1223
|
+
text: "",
|
|
1224
|
+
ended: false
|
|
1225
|
+
};
|
|
1226
|
+
if (event.id !== void 0) reasoning.reasoningId = event.id;
|
|
1227
|
+
state.reasoning.set(key, reasoning);
|
|
1228
|
+
const reasoningStart = {
|
|
1229
|
+
type: "reasoning_start",
|
|
1230
|
+
messageId: state.messageId,
|
|
1231
|
+
partId: reasoning.partId
|
|
1232
|
+
};
|
|
1233
|
+
if (event.id !== void 0) reasoningStart.reasoningId = event.id;
|
|
1234
|
+
yield clientEvent(state, reasoningStart);
|
|
1235
|
+
}
|
|
1236
|
+
reasoning.text += event.delta;
|
|
1237
|
+
const reasoningDelta = {
|
|
1238
|
+
type: "reasoning_delta",
|
|
1239
|
+
messageId: state.messageId,
|
|
1240
|
+
partId: reasoning.partId,
|
|
1241
|
+
delta: event.delta
|
|
1242
|
+
};
|
|
1243
|
+
if (event.contentType !== void 0) reasoningDelta.contentType = event.contentType;
|
|
1244
|
+
if (event.signature !== void 0) reasoningDelta.signature = event.signature;
|
|
1245
|
+
yield clientEvent(state, reasoningDelta);
|
|
1611
1246
|
}
|
|
1612
1247
|
async function* appendToolCall(state, event) {
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1248
|
+
const tool = getToolState(state, event.id, event.name);
|
|
1249
|
+
if (!tool.started) {
|
|
1250
|
+
tool.started = true;
|
|
1251
|
+
const toolCallStart = {
|
|
1252
|
+
type: "tool_call_start",
|
|
1253
|
+
messageId: state.messageId,
|
|
1254
|
+
partId: tool.partId,
|
|
1255
|
+
toolCallId: event.id
|
|
1256
|
+
};
|
|
1257
|
+
if (event.callId !== void 0) toolCallStart.callId = event.callId;
|
|
1258
|
+
if (event.name !== void 0) toolCallStart.toolName = event.name;
|
|
1259
|
+
yield clientEvent(state, toolCallStart);
|
|
1260
|
+
}
|
|
1261
|
+
if (event.callId !== void 0) tool.callId = event.callId;
|
|
1262
|
+
if (event.name !== void 0) tool.toolName = event.name;
|
|
1263
|
+
if (event.signature !== void 0) tool.signature = event.signature;
|
|
1264
|
+
if (event.argumentsDelta === void 0) return;
|
|
1265
|
+
const mode = event.argumentsMode ?? "append";
|
|
1266
|
+
const prior = typeof tool.input === "string" ? tool.input : "";
|
|
1267
|
+
tool.input = mode === "replace" ? event.argumentsDelta : `${prior}${event.argumentsDelta}`;
|
|
1268
|
+
const toolCallDelta = {
|
|
1269
|
+
type: "tool_call_delta",
|
|
1270
|
+
messageId: state.messageId,
|
|
1271
|
+
partId: tool.partId,
|
|
1272
|
+
toolCallId: event.id,
|
|
1273
|
+
delta: event.argumentsDelta,
|
|
1274
|
+
mode
|
|
1275
|
+
};
|
|
1276
|
+
if (event.callId !== void 0) toolCallDelta.callId = event.callId;
|
|
1277
|
+
if (event.name !== void 0) toolCallDelta.toolName = event.name;
|
|
1278
|
+
if (event.signature !== void 0) toolCallDelta.signature = event.signature;
|
|
1279
|
+
yield clientEvent(state, toolCallDelta);
|
|
1645
1280
|
}
|
|
1646
1281
|
async function* finishToolCall(state, toolCall) {
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1282
|
+
const tool = getToolState(state, toolCall.toolCallId, toolCall.toolName);
|
|
1283
|
+
const wasEnded = tool.ended;
|
|
1284
|
+
if (!tool.started) {
|
|
1285
|
+
tool.started = true;
|
|
1286
|
+
const toolCallStart = {
|
|
1287
|
+
type: "tool_call_start",
|
|
1288
|
+
messageId: state.messageId,
|
|
1289
|
+
partId: tool.partId,
|
|
1290
|
+
toolCallId: toolCall.toolCallId,
|
|
1291
|
+
toolName: toolCall.toolName
|
|
1292
|
+
};
|
|
1293
|
+
if (toolCall.callId !== void 0) toolCallStart.callId = toolCall.callId;
|
|
1294
|
+
yield clientEvent(state, toolCallStart);
|
|
1295
|
+
}
|
|
1296
|
+
if (toolCall.callId === void 0) delete tool.callId;
|
|
1297
|
+
else tool.callId = toolCall.callId;
|
|
1298
|
+
tool.toolName = toolCall.toolName;
|
|
1299
|
+
tool.input = toolCall.input;
|
|
1300
|
+
if (toolCall.signature === void 0) delete tool.signature;
|
|
1301
|
+
else tool.signature = toolCall.signature;
|
|
1302
|
+
if (wasEnded) return;
|
|
1303
|
+
tool.ended = true;
|
|
1304
|
+
const toolCallEnd = {
|
|
1305
|
+
type: "tool_call_end",
|
|
1306
|
+
messageId: state.messageId,
|
|
1307
|
+
partId: tool.partId,
|
|
1308
|
+
toolCallId: toolCall.toolCallId,
|
|
1309
|
+
toolName: toolCall.toolName,
|
|
1310
|
+
input: toolCall.input
|
|
1311
|
+
};
|
|
1312
|
+
if (toolCall.callId !== void 0) toolCallEnd.callId = toolCall.callId;
|
|
1313
|
+
if (toolCall.signature !== void 0) toolCallEnd.signature = toolCall.signature;
|
|
1314
|
+
yield clientEvent(state, toolCallEnd);
|
|
1680
1315
|
}
|
|
1681
1316
|
async function* finishMessage(state, content, metadata) {
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
state.ended = true;
|
|
1769
|
-
const messageEnd = {
|
|
1770
|
-
type: "message_end",
|
|
1771
|
-
messageId: state.messageId,
|
|
1772
|
-
parts: contentToUIMessageParts(content, state),
|
|
1773
|
-
usage: metadata.usage
|
|
1774
|
-
};
|
|
1775
|
-
if (state.modelMessageId !== void 0) messageEnd.modelMessageId = state.modelMessageId;
|
|
1776
|
-
if (metadata.contextUsage !== void 0) messageEnd.contextUsage = metadata.contextUsage;
|
|
1777
|
-
yield clientEvent(state, messageEnd);
|
|
1317
|
+
if (state.ended) return;
|
|
1318
|
+
const textContent = content.filter((part) => part.type === "text");
|
|
1319
|
+
const text = textContent.map((part) => part.text).join("");
|
|
1320
|
+
if (state.textStarted || text.length > 0) {
|
|
1321
|
+
if (!state.textStarted) {
|
|
1322
|
+
state.textStarted = true;
|
|
1323
|
+
yield clientEvent(state, {
|
|
1324
|
+
type: "text_start",
|
|
1325
|
+
messageId: state.messageId,
|
|
1326
|
+
partId: state.textPartId
|
|
1327
|
+
});
|
|
1328
|
+
}
|
|
1329
|
+
state.text = text;
|
|
1330
|
+
state.textEnded = true;
|
|
1331
|
+
const textEnd = {
|
|
1332
|
+
type: "text_end",
|
|
1333
|
+
messageId: state.messageId,
|
|
1334
|
+
partId: state.textPartId,
|
|
1335
|
+
text
|
|
1336
|
+
};
|
|
1337
|
+
if (textContent.length === 1 && textContent[0]?.signature !== void 0) textEnd.signature = textContent[0].signature;
|
|
1338
|
+
yield clientEvent(state, textEnd);
|
|
1339
|
+
}
|
|
1340
|
+
const finalReasoning = content.filter((part) => part.type === "reasoning");
|
|
1341
|
+
const anonymousReasoning = [...state.reasoning.values()].filter((reasoning) => reasoning.reasoningId === void 0);
|
|
1342
|
+
let anonymousIndex = 0;
|
|
1343
|
+
for (const [index, reasoning] of finalReasoning.entries()) {
|
|
1344
|
+
const existing = reasoning.id === void 0 ? anonymousReasoning[anonymousIndex++] : state.reasoning.get(reasoning.id);
|
|
1345
|
+
const current = existing ?? createReasoningState(reasoning.id);
|
|
1346
|
+
if (existing === void 0) {
|
|
1347
|
+
state.reasoning.set(reasoning.id ?? `final:${index}`, current);
|
|
1348
|
+
const reasoningStart = {
|
|
1349
|
+
type: "reasoning_start",
|
|
1350
|
+
messageId: state.messageId,
|
|
1351
|
+
partId: current.partId
|
|
1352
|
+
};
|
|
1353
|
+
if (reasoning.id !== void 0) reasoningStart.reasoningId = reasoning.id;
|
|
1354
|
+
yield clientEvent(state, reasoningStart);
|
|
1355
|
+
}
|
|
1356
|
+
if (!current.ended) {
|
|
1357
|
+
current.ended = true;
|
|
1358
|
+
current.text = reasoning.text;
|
|
1359
|
+
const reasoningEnd = {
|
|
1360
|
+
type: "reasoning_end",
|
|
1361
|
+
messageId: state.messageId,
|
|
1362
|
+
partId: current.partId,
|
|
1363
|
+
text: reasoning.text
|
|
1364
|
+
};
|
|
1365
|
+
if (reasoning.details !== void 0) reasoningEnd.content = reasoning.details;
|
|
1366
|
+
yield clientEvent(state, reasoningEnd);
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
for (const reasoning of state.reasoning.values()) {
|
|
1370
|
+
if (reasoning.ended) continue;
|
|
1371
|
+
reasoning.ended = true;
|
|
1372
|
+
yield clientEvent(state, {
|
|
1373
|
+
type: "reasoning_end",
|
|
1374
|
+
messageId: state.messageId,
|
|
1375
|
+
partId: reasoning.partId,
|
|
1376
|
+
text: reasoning.text
|
|
1377
|
+
});
|
|
1378
|
+
}
|
|
1379
|
+
for (const part of content) {
|
|
1380
|
+
if (part.type === "tool-call") yield* finishToolCall(state, part);
|
|
1381
|
+
if (part.type === "image" || part.type === "file") {
|
|
1382
|
+
const attachment = contentAttachment(part);
|
|
1383
|
+
yield clientEvent(state, {
|
|
1384
|
+
type: "attachment",
|
|
1385
|
+
messageId: state.messageId,
|
|
1386
|
+
partId: createClientId("attachment_part"),
|
|
1387
|
+
attachment
|
|
1388
|
+
});
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
for (const source of metadata.sources ?? []) yield* emitSource(state, source);
|
|
1392
|
+
for (const toolCall of metadata.providerToolCalls ?? []) yield* emitProviderToolCall(state, toolCall);
|
|
1393
|
+
state.ended = true;
|
|
1394
|
+
const messageEnd = {
|
|
1395
|
+
type: "message_end",
|
|
1396
|
+
messageId: state.messageId,
|
|
1397
|
+
parts: contentToUIMessageParts(content, state),
|
|
1398
|
+
usage: metadata.usage
|
|
1399
|
+
};
|
|
1400
|
+
if (state.modelMessageId !== void 0) messageEnd.modelMessageId = state.modelMessageId;
|
|
1401
|
+
if (metadata.contextUsage !== void 0) messageEnd.contextUsage = metadata.contextUsage;
|
|
1402
|
+
yield clientEvent(state, messageEnd);
|
|
1778
1403
|
}
|
|
1779
1404
|
async function* finishErroredRun(state, error, usage, mapper) {
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1405
|
+
const mapped = mapClientError(error, mapper);
|
|
1406
|
+
if (state.textStarted && !state.textEnded) {
|
|
1407
|
+
state.textEnded = true;
|
|
1408
|
+
yield clientEvent(state, {
|
|
1409
|
+
type: "text_end",
|
|
1410
|
+
messageId: state.messageId,
|
|
1411
|
+
partId: state.textPartId,
|
|
1412
|
+
text: state.text
|
|
1413
|
+
});
|
|
1414
|
+
}
|
|
1415
|
+
const errorEvent = {
|
|
1416
|
+
type: "error",
|
|
1417
|
+
error: mapped
|
|
1418
|
+
};
|
|
1419
|
+
if (usage !== void 0) errorEvent.usage = usage;
|
|
1420
|
+
yield clientEvent(state, errorEvent);
|
|
1421
|
+
if (!state.ended) {
|
|
1422
|
+
state.ended = true;
|
|
1423
|
+
const messageEnd = {
|
|
1424
|
+
type: "message_end",
|
|
1425
|
+
messageId: state.messageId
|
|
1426
|
+
};
|
|
1427
|
+
if (state.modelMessageId !== void 0) messageEnd.modelMessageId = state.modelMessageId;
|
|
1428
|
+
if (usage !== void 0) messageEnd.usage = usage;
|
|
1429
|
+
yield clientEvent(state, messageEnd);
|
|
1430
|
+
}
|
|
1431
|
+
const runEnd = {
|
|
1432
|
+
type: "run_end",
|
|
1433
|
+
status: "error"
|
|
1434
|
+
};
|
|
1435
|
+
if (usage !== void 0) runEnd.usage = usage;
|
|
1436
|
+
yield clientEvent(state, runEnd);
|
|
1812
1437
|
}
|
|
1813
1438
|
function contentToUIMessageParts(content, state) {
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
};
|
|
1880
|
-
}
|
|
1881
|
-
const attachment = contentAttachment(contentPart);
|
|
1882
|
-
return {
|
|
1883
|
-
id: createClientId("attachment_part"),
|
|
1884
|
-
type: "attachment",
|
|
1885
|
-
attachment
|
|
1886
|
-
};
|
|
1887
|
-
});
|
|
1439
|
+
let textIndex = 0;
|
|
1440
|
+
let anonymousReasoningIndex = 0;
|
|
1441
|
+
const anonymousReasoning = [...state.reasoning.values()].filter((reasoning) => reasoning.reasoningId === void 0);
|
|
1442
|
+
return content.map((contentPart) => {
|
|
1443
|
+
if (contentPart.type === "text") {
|
|
1444
|
+
const part = {
|
|
1445
|
+
id: textIndex === 0 ? state.textPartId : createClientId("text"),
|
|
1446
|
+
type: "text",
|
|
1447
|
+
text: contentPart.text
|
|
1448
|
+
};
|
|
1449
|
+
textIndex += 1;
|
|
1450
|
+
if (contentPart.signature !== void 0) part.signature = contentPart.signature;
|
|
1451
|
+
return part;
|
|
1452
|
+
}
|
|
1453
|
+
if (contentPart.type === "reasoning") {
|
|
1454
|
+
const part = {
|
|
1455
|
+
id: (contentPart.id === void 0 ? anonymousReasoning[anonymousReasoningIndex++] : state.reasoning.get(contentPart.id))?.partId ?? createClientId("reasoning"),
|
|
1456
|
+
type: "reasoning",
|
|
1457
|
+
text: contentPart.text
|
|
1458
|
+
};
|
|
1459
|
+
if (contentPart.id !== void 0) part.reasoningId = contentPart.id;
|
|
1460
|
+
if (contentPart.details !== void 0) part.content = contentPart.details;
|
|
1461
|
+
return part;
|
|
1462
|
+
}
|
|
1463
|
+
if (contentPart.type === "tool-call") {
|
|
1464
|
+
const tool = getToolState(state, contentPart.toolCallId, contentPart.toolName);
|
|
1465
|
+
const partBase = {
|
|
1466
|
+
id: tool.partId,
|
|
1467
|
+
type: "tool",
|
|
1468
|
+
toolName: contentPart.toolName,
|
|
1469
|
+
toolCallId: contentPart.toolCallId
|
|
1470
|
+
};
|
|
1471
|
+
if (contentPart.callId !== void 0) Object.assign(partBase, { callId: contentPart.callId });
|
|
1472
|
+
if (tool.internalCallId !== void 0) Object.assign(partBase, { internalCallId: tool.internalCallId });
|
|
1473
|
+
if (state.turn !== void 0) Object.assign(partBase, { turn: state.turn });
|
|
1474
|
+
if (contentPart.signature !== void 0) Object.assign(partBase, { signature: contentPart.signature });
|
|
1475
|
+
if (tool.result?.status === "success") {
|
|
1476
|
+
const part = {
|
|
1477
|
+
...partBase,
|
|
1478
|
+
state: "output-available",
|
|
1479
|
+
input: contentPart.input,
|
|
1480
|
+
output: tool.result.output
|
|
1481
|
+
};
|
|
1482
|
+
if (tool.result.content !== void 0) part.resultContent = tool.result.content;
|
|
1483
|
+
return part;
|
|
1484
|
+
}
|
|
1485
|
+
if (tool.result?.status === "error") return {
|
|
1486
|
+
...partBase,
|
|
1487
|
+
state: "error",
|
|
1488
|
+
input: contentPart.input,
|
|
1489
|
+
error: tool.result.error
|
|
1490
|
+
};
|
|
1491
|
+
return {
|
|
1492
|
+
...partBase,
|
|
1493
|
+
state: "input-available",
|
|
1494
|
+
input: contentPart.input
|
|
1495
|
+
};
|
|
1496
|
+
}
|
|
1497
|
+
const attachment = contentAttachment(contentPart);
|
|
1498
|
+
return {
|
|
1499
|
+
id: createClientId("attachment_part"),
|
|
1500
|
+
type: "attachment",
|
|
1501
|
+
attachment
|
|
1502
|
+
};
|
|
1503
|
+
});
|
|
1888
1504
|
}
|
|
1889
1505
|
function getToolState(state, id, name) {
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
}
|
|
1904
|
-
return tool;
|
|
1506
|
+
let tool = state.tools.get(id);
|
|
1507
|
+
if (tool === void 0) {
|
|
1508
|
+
tool = {
|
|
1509
|
+
partId: createClientId("tool"),
|
|
1510
|
+
toolCallId: id,
|
|
1511
|
+
input: "",
|
|
1512
|
+
started: false,
|
|
1513
|
+
ended: false
|
|
1514
|
+
};
|
|
1515
|
+
if (name !== void 0) tool.toolName = name;
|
|
1516
|
+
state.tools.set(id, tool);
|
|
1517
|
+
} else if (name !== void 0) tool.toolName = name;
|
|
1518
|
+
return tool;
|
|
1905
1519
|
}
|
|
1906
1520
|
function getToolResultState(state, toolName, toolCallId, callId, internalCallId) {
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1521
|
+
const matchedByToolCallId = state.tools.get(toolCallId);
|
|
1522
|
+
const matchedByCallId = callId === void 0 ? void 0 : [...state.tools.values()].find((tool) => tool.callId === callId);
|
|
1523
|
+
const tool = matchedByToolCallId ?? matchedByCallId ?? getToolState(state, toolCallId, toolName);
|
|
1524
|
+
tool.toolName = toolName;
|
|
1525
|
+
if (callId !== void 0) tool.callId = callId;
|
|
1526
|
+
tool.internalCallId = internalCallId;
|
|
1527
|
+
return tool;
|
|
1914
1528
|
}
|
|
1915
1529
|
function createReasoningState(id) {
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1530
|
+
const state = {
|
|
1531
|
+
partId: createClientId("reasoning"),
|
|
1532
|
+
text: "",
|
|
1533
|
+
ended: false
|
|
1534
|
+
};
|
|
1535
|
+
if (id !== void 0) state.reasoningId = id;
|
|
1536
|
+
return state;
|
|
1923
1537
|
}
|
|
1924
1538
|
function contentAttachment(content) {
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
} else {
|
|
1943
|
-
attachment.text = content.data.text;
|
|
1944
|
-
}
|
|
1945
|
-
return attachment;
|
|
1539
|
+
const attachment = {
|
|
1540
|
+
id: createClientId("attachment"),
|
|
1541
|
+
type: content.type === "image" ? "image" : "file"
|
|
1542
|
+
};
|
|
1543
|
+
if (content.type === "image") {
|
|
1544
|
+
if (content.detail !== void 0) attachment.detail = content.detail;
|
|
1545
|
+
if (content.mediaType !== void 0) attachment.mediaType = content.mediaType;
|
|
1546
|
+
if (content.image.type === "url") attachment.url = content.image.url;
|
|
1547
|
+
else attachment.data = content.image.data;
|
|
1548
|
+
return attachment;
|
|
1549
|
+
}
|
|
1550
|
+
attachment.mediaType = content.mediaType;
|
|
1551
|
+
if (content.filename !== void 0) attachment.name = content.filename;
|
|
1552
|
+
if (content.data.type === "url") attachment.url = content.data.url;
|
|
1553
|
+
else if (content.data.type === "data") attachment.data = content.data.data;
|
|
1554
|
+
else attachment.text = content.data.text;
|
|
1555
|
+
return attachment;
|
|
1946
1556
|
}
|
|
1947
1557
|
function clientToolResult(output, result) {
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1558
|
+
switch (output.type) {
|
|
1559
|
+
case "text": return {
|
|
1560
|
+
status: "success",
|
|
1561
|
+
output: output.value
|
|
1562
|
+
};
|
|
1563
|
+
case "json": return {
|
|
1564
|
+
status: "success",
|
|
1565
|
+
output: output.value
|
|
1566
|
+
};
|
|
1567
|
+
case "content": return {
|
|
1568
|
+
status: "success",
|
|
1569
|
+
output: result,
|
|
1570
|
+
content: output.value
|
|
1571
|
+
};
|
|
1572
|
+
case "execution-denied": return {
|
|
1573
|
+
status: "error",
|
|
1574
|
+
error: {
|
|
1575
|
+
message: output.reason ?? "Tool execution was denied.",
|
|
1576
|
+
code: "tool_execution_denied"
|
|
1577
|
+
}
|
|
1578
|
+
};
|
|
1579
|
+
case "error-text": return {
|
|
1580
|
+
status: "error",
|
|
1581
|
+
error: {
|
|
1582
|
+
message: output.value,
|
|
1583
|
+
code: "tool_execution_error"
|
|
1584
|
+
}
|
|
1585
|
+
};
|
|
1586
|
+
case "error-json": return {
|
|
1587
|
+
status: "error",
|
|
1588
|
+
error: {
|
|
1589
|
+
message: "Tool execution failed.",
|
|
1590
|
+
code: "tool_execution_error",
|
|
1591
|
+
details: output.value
|
|
1592
|
+
}
|
|
1593
|
+
};
|
|
1594
|
+
}
|
|
1978
1595
|
}
|
|
1979
1596
|
function parseJsonOrString(value) {
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1597
|
+
try {
|
|
1598
|
+
const parsed = JSON.parse(value);
|
|
1599
|
+
return isJsonValue$1(parsed) ? parsed : value;
|
|
1600
|
+
} catch {
|
|
1601
|
+
return value;
|
|
1602
|
+
}
|
|
1986
1603
|
}
|
|
1987
1604
|
function clientOutput(output, mapper) {
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
}
|
|
1992
|
-
return mapped;
|
|
1605
|
+
const mapped = mapper === void 0 ? isJsonValue$1(output) ? output : void 0 : mapper(output);
|
|
1606
|
+
if (mapped !== void 0 && !isJsonValue$1(mapped)) throw new TypeError("mapOutput must return a strict JSON value or undefined.");
|
|
1607
|
+
return mapped;
|
|
1993
1608
|
}
|
|
1994
1609
|
function valueFingerprint(value) {
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
}
|
|
2001
|
-
return `${typeof value}:${JSON.stringify(value)}`;
|
|
1610
|
+
if (value === null) return "null";
|
|
1611
|
+
if (value === void 0) return "undefined";
|
|
1612
|
+
if (Array.isArray(value)) return `[${value.map(valueFingerprint).join(",")}]`;
|
|
1613
|
+
if (typeof value === "object") return `{${Object.entries(value).sort(([left], [right]) => left.localeCompare(right)).map(([key, entry]) => `${JSON.stringify(key)}:${valueFingerprint(entry)}`).join(",")}}`;
|
|
1614
|
+
return `${typeof value}:${JSON.stringify(value)}`;
|
|
2002
1615
|
}
|
|
2003
1616
|
function clientTrace(trace) {
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
if (trace.observationId !== void 0) result.observationId = trace.observationId;
|
|
2009
|
-
return result;
|
|
1617
|
+
const result = { observer: trace.observer };
|
|
1618
|
+
if (trace.traceId !== void 0) result.traceId = trace.traceId;
|
|
1619
|
+
if (trace.observationId !== void 0) result.observationId = trace.observationId;
|
|
1620
|
+
return result;
|
|
2010
1621
|
}
|
|
2011
1622
|
function mapClientError(error, mapper) {
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
}
|
|
2016
|
-
return mapped;
|
|
1623
|
+
const mapped = mapper?.(error) ?? maskedClientError();
|
|
1624
|
+
if (typeof mapped.message !== "string" || mapped.details !== void 0 && !isJsonValue$1(mapped.details)) throw new TypeError("mapError must return a JSON-safe ClientStreamError.");
|
|
1625
|
+
return mapped;
|
|
2017
1626
|
}
|
|
2018
1627
|
function clientEvent(state, event) {
|
|
2019
|
-
|
|
1628
|
+
return scopedEvent(state.runId, state.turn, state.scope, event);
|
|
2020
1629
|
}
|
|
2021
1630
|
function rootEvent(runId, event, scope) {
|
|
2022
|
-
|
|
1631
|
+
return scopedEvent(runId, void 0, scope, event);
|
|
2023
1632
|
}
|
|
2024
1633
|
function scopedEvent(runId, turn, scope, event) {
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
1634
|
+
let scoped = {
|
|
1635
|
+
runId,
|
|
1636
|
+
...event
|
|
1637
|
+
};
|
|
1638
|
+
if (turn !== void 0) scoped = {
|
|
1639
|
+
...scoped,
|
|
1640
|
+
turn
|
|
1641
|
+
};
|
|
1642
|
+
if (scope !== void 0) scoped = {
|
|
1643
|
+
...scoped,
|
|
1644
|
+
scope
|
|
1645
|
+
};
|
|
1646
|
+
return scoped;
|
|
2029
1647
|
}
|
|
2030
1648
|
function scopeKey(scope) {
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
1649
|
+
if (scope === void 0) return "root";
|
|
1650
|
+
return [
|
|
1651
|
+
scope.agentId,
|
|
1652
|
+
scope.parentRunId,
|
|
1653
|
+
scope.parentToolName,
|
|
1654
|
+
scope.parentToolCallId,
|
|
1655
|
+
scope.parentInternalToolCallId
|
|
1656
|
+
].join(":");
|
|
2039
1657
|
}
|
|
2040
1658
|
function isRootTerminalAgentEvent(event) {
|
|
2041
|
-
|
|
1659
|
+
return event.type === "response" || event.type === "interaction" || event.type === "blocked" || event.type === "error";
|
|
2042
1660
|
}
|
|
2043
1661
|
function agentOutcomeStatus(type) {
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
1662
|
+
if (type === "response") return "completed";
|
|
1663
|
+
if (type === "interaction") return "suspended";
|
|
1664
|
+
return "blocked";
|
|
2047
1665
|
}
|
|
2048
1666
|
function propagateCancellation(source, transform) {
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
1667
|
+
return { [Symbol.asyncIterator]() {
|
|
1668
|
+
const sourceIterator = source[Symbol.asyncIterator]();
|
|
1669
|
+
const outputIterator = transform({ [Symbol.asyncIterator]: () => sourceIterator })[Symbol.asyncIterator]();
|
|
1670
|
+
let returned = false;
|
|
1671
|
+
return {
|
|
1672
|
+
next: () => outputIterator.next(),
|
|
1673
|
+
async return() {
|
|
1674
|
+
if (returned) return {
|
|
1675
|
+
done: true,
|
|
1676
|
+
value: void 0
|
|
1677
|
+
};
|
|
1678
|
+
returned = true;
|
|
1679
|
+
const sourceReturn = sourceIterator.return?.();
|
|
1680
|
+
const outputReturn = outputIterator.return?.();
|
|
1681
|
+
await Promise.all([sourceReturn, outputReturn]);
|
|
1682
|
+
return {
|
|
1683
|
+
done: true,
|
|
1684
|
+
value: void 0
|
|
1685
|
+
};
|
|
1686
|
+
}
|
|
1687
|
+
};
|
|
1688
|
+
} };
|
|
2070
1689
|
}
|
|
2071
|
-
|
|
2072
1690
|
// src/client-transport.ts
|
|
2073
1691
|
function createHttpClientTransport(options) {
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
return {
|
|
2086
|
-
async *send(sendOptions) {
|
|
2087
|
-
const resume = sendOptions.resume ?? resumeCursorFromRequest(sendOptions.request);
|
|
2088
|
-
yield* validateFrameSequence(transport.send(sendOptions), resume);
|
|
2089
|
-
}
|
|
2090
|
-
};
|
|
1692
|
+
const transport = createFetchEventTransport({
|
|
1693
|
+
...options,
|
|
1694
|
+
mapEvent: (event) => parseClientStreamFrame(event, protocolOptions(options)),
|
|
1695
|
+
validateResponse(response) {
|
|
1696
|
+
if (response.headers.get("x-anvia-stream-protocol") !== "anvia.client.v3") throw new ClientProtocolError(`Expected x-anvia-stream-protocol: ${CLIENT_STREAM_PROTOCOL}.`);
|
|
1697
|
+
}
|
|
1698
|
+
});
|
|
1699
|
+
return { async *send(sendOptions) {
|
|
1700
|
+
const resume = sendOptions.resume ?? resumeCursorFromRequest(sendOptions.request);
|
|
1701
|
+
yield* validateFrameSequence(transport.send(sendOptions), resume);
|
|
1702
|
+
} };
|
|
2091
1703
|
}
|
|
2092
1704
|
function createDirectClientTransport(options) {
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
yield { type: "stream_end", streamId, eventId, status };
|
|
2147
|
-
}
|
|
2148
|
-
};
|
|
1705
|
+
return { async *send(sendOptions) {
|
|
1706
|
+
if ((sendOptions.resume ?? resumeCursorFromRequest(sendOptions.request)) !== void 0) throw new ClientProtocolError("Direct client transports do not support resume cursors.");
|
|
1707
|
+
const handlerOptions = { request: sendOptions.request };
|
|
1708
|
+
if (sendOptions.abortSignal !== void 0) handlerOptions.abortSignal = sendOptions.abortSignal;
|
|
1709
|
+
if (sendOptions.headers !== void 0) handlerOptions.headers = sendOptions.headers;
|
|
1710
|
+
const iterator = (await options.handler(handlerOptions))[Symbol.asyncIterator]();
|
|
1711
|
+
const streamId = createClientId("stream");
|
|
1712
|
+
let eventId = 0;
|
|
1713
|
+
yield {
|
|
1714
|
+
type: "stream_start",
|
|
1715
|
+
protocol: CLIENT_STREAM_PROTOCOL,
|
|
1716
|
+
streamId,
|
|
1717
|
+
eventId: 0,
|
|
1718
|
+
resumable: false
|
|
1719
|
+
};
|
|
1720
|
+
let status = "completed";
|
|
1721
|
+
try {
|
|
1722
|
+
let closePromise;
|
|
1723
|
+
const close = () => {
|
|
1724
|
+
closePromise ??= Promise.resolve(iterator.return?.());
|
|
1725
|
+
return closePromise;
|
|
1726
|
+
};
|
|
1727
|
+
const onAbort = () => {
|
|
1728
|
+
close();
|
|
1729
|
+
};
|
|
1730
|
+
sendOptions.abortSignal?.addEventListener("abort", onAbort, { once: true });
|
|
1731
|
+
try {
|
|
1732
|
+
while (!sendOptions.abortSignal?.aborted) {
|
|
1733
|
+
const next = await iterator.next();
|
|
1734
|
+
if (next.done) break;
|
|
1735
|
+
const event = parseClientStreamEvent(next.value, protocolOptions(options));
|
|
1736
|
+
eventId += 1;
|
|
1737
|
+
yield {
|
|
1738
|
+
type: "stream_event",
|
|
1739
|
+
streamId,
|
|
1740
|
+
eventId,
|
|
1741
|
+
event
|
|
1742
|
+
};
|
|
1743
|
+
}
|
|
1744
|
+
} finally {
|
|
1745
|
+
sendOptions.abortSignal?.removeEventListener("abort", onAbort);
|
|
1746
|
+
await close();
|
|
1747
|
+
}
|
|
1748
|
+
} catch {
|
|
1749
|
+
status = "error";
|
|
1750
|
+
}
|
|
1751
|
+
yield {
|
|
1752
|
+
type: "stream_end",
|
|
1753
|
+
streamId,
|
|
1754
|
+
eventId,
|
|
1755
|
+
status
|
|
1756
|
+
};
|
|
1757
|
+
} };
|
|
2149
1758
|
}
|
|
2150
1759
|
async function* validateFrameSequence(frames, resume) {
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
if (frame.eventId !== lastEventId + 1) {
|
|
2174
|
-
throw new ClientProtocolError("Client stream event IDs must be contiguous.", frame);
|
|
2175
|
-
}
|
|
2176
|
-
lastEventId = frame.eventId;
|
|
2177
|
-
} else if (frame.type === "stream_end") {
|
|
2178
|
-
if (frame.eventId !== lastEventId) {
|
|
2179
|
-
throw new ClientProtocolError("stream_end has an invalid eventId.", frame);
|
|
2180
|
-
}
|
|
2181
|
-
ended = true;
|
|
2182
|
-
}
|
|
2183
|
-
yield frame;
|
|
2184
|
-
}
|
|
2185
|
-
if (streamId === void 0 || !ended) {
|
|
2186
|
-
throw new ClientProtocolError("Client stream ended without a complete frame sequence.");
|
|
2187
|
-
}
|
|
1760
|
+
let streamId;
|
|
1761
|
+
let lastEventId = resume?.after ?? 0;
|
|
1762
|
+
let ended = false;
|
|
1763
|
+
for await (const frame of frames) {
|
|
1764
|
+
if (ended) throw new ClientProtocolError("Received a frame after stream_end.", frame);
|
|
1765
|
+
if (streamId === void 0) {
|
|
1766
|
+
if (frame.type !== "stream_start") throw new ClientProtocolError("The first client stream frame must be stream_start.", frame);
|
|
1767
|
+
streamId = frame.streamId;
|
|
1768
|
+
if (resume !== void 0 && frame.streamId !== resume.streamId) throw new ClientProtocolError("Resume response streamId does not match the cursor.", frame);
|
|
1769
|
+
if (resume !== void 0 && !frame.resumable) throw new ClientProtocolError("Resume response must identify a resumable stream.", frame);
|
|
1770
|
+
} else if (frame.type === "stream_start") throw new ClientProtocolError("Received more than one stream_start frame.", frame);
|
|
1771
|
+
else if (frame.streamId !== streamId) throw new ClientProtocolError("Client streamId changed during the response.", frame);
|
|
1772
|
+
if (frame.type === "stream_event") {
|
|
1773
|
+
if (frame.eventId !== lastEventId + 1) throw new ClientProtocolError("Client stream event IDs must be contiguous.", frame);
|
|
1774
|
+
lastEventId = frame.eventId;
|
|
1775
|
+
} else if (frame.type === "stream_end") {
|
|
1776
|
+
if (frame.eventId !== lastEventId) throw new ClientProtocolError("stream_end has an invalid eventId.", frame);
|
|
1777
|
+
ended = true;
|
|
1778
|
+
}
|
|
1779
|
+
yield frame;
|
|
1780
|
+
}
|
|
1781
|
+
if (streamId === void 0 || !ended) throw new ClientProtocolError("Client stream ended without a complete frame sequence.");
|
|
2188
1782
|
}
|
|
2189
1783
|
function protocolOptions(options) {
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
1784
|
+
const result = {};
|
|
1785
|
+
if (options.metadataSchema !== void 0) result.metadataSchema = options.metadataSchema;
|
|
1786
|
+
if (options.dataSchemas !== void 0) result.dataSchemas = options.dataSchemas;
|
|
1787
|
+
return result;
|
|
2194
1788
|
}
|
|
2195
1789
|
function resumeCursorFromRequest(value) {
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
1790
|
+
if (typeof value !== "object" || value === null || !("resume" in value)) return void 0;
|
|
1791
|
+
const resume = value.resume;
|
|
1792
|
+
if (typeof resume !== "object" || resume === null) return void 0;
|
|
1793
|
+
if (!("streamId" in resume) || !("after" in resume)) return void 0;
|
|
1794
|
+
return typeof resume.streamId === "string" && typeof resume.after === "number" && Number.isSafeInteger(resume.after) && resume.after >= 0 ? {
|
|
1795
|
+
streamId: resume.streamId,
|
|
1796
|
+
after: resume.after
|
|
1797
|
+
} : void 0;
|
|
2201
1798
|
}
|
|
2202
|
-
|
|
2203
|
-
// src/index.ts
|
|
2204
|
-
import { isJsonValue as isJsonValue5 } from "@anvia/core/completion";
|
|
2205
|
-
|
|
2206
1799
|
// src/reducer.ts
|
|
2207
|
-
import { isJsonValue as isJsonValue4 } from "@anvia/core/completion";
|
|
2208
1800
|
function applyClientStreamEvent(messages, event) {
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
runId: event.runId,
|
|
2410
|
-
status: event.status
|
|
2411
|
-
};
|
|
2412
|
-
if (event.contextUsage !== void 0) generation.contextUsage = event.contextUsage;
|
|
2413
|
-
if (event.trace !== void 0) generation.trace = event.trace;
|
|
2414
|
-
if (event.memoryCompaction !== void 0) {
|
|
2415
|
-
generation.memoryCompaction = event.memoryCompaction;
|
|
2416
|
-
}
|
|
2417
|
-
return updateLastAssistantGeneration(messages, generation);
|
|
2418
|
-
}
|
|
2419
|
-
case "error":
|
|
2420
|
-
return appendToLastAssistant(messages, {
|
|
2421
|
-
id: createClientId("error"),
|
|
2422
|
-
type: "error",
|
|
2423
|
-
error: event.error
|
|
2424
|
-
});
|
|
2425
|
-
default:
|
|
2426
|
-
return messages;
|
|
2427
|
-
}
|
|
1801
|
+
if (event.scope?.parentToolCallId !== void 0) return messages;
|
|
1802
|
+
switch (event.type) {
|
|
1803
|
+
case "message_reset": return messages.map((message) => message.id === event.messageId ? {
|
|
1804
|
+
id: message.id,
|
|
1805
|
+
role: message.role,
|
|
1806
|
+
parts: []
|
|
1807
|
+
} : message);
|
|
1808
|
+
case "message_start": {
|
|
1809
|
+
if (messages.some((message) => message.id === event.messageId)) return messages;
|
|
1810
|
+
let message = {
|
|
1811
|
+
id: event.messageId,
|
|
1812
|
+
role: event.role,
|
|
1813
|
+
parts: []
|
|
1814
|
+
};
|
|
1815
|
+
if (event.metadata !== void 0) message = {
|
|
1816
|
+
...message,
|
|
1817
|
+
metadata: event.metadata
|
|
1818
|
+
};
|
|
1819
|
+
return [...messages, message];
|
|
1820
|
+
}
|
|
1821
|
+
case "text_start": return upsertPart(messages, event.messageId, {
|
|
1822
|
+
id: event.partId,
|
|
1823
|
+
type: "text",
|
|
1824
|
+
text: ""
|
|
1825
|
+
});
|
|
1826
|
+
case "text_delta": return updatePart(messages, event.messageId, event.partId, (part) => ({
|
|
1827
|
+
id: event.partId,
|
|
1828
|
+
type: "text",
|
|
1829
|
+
text: part?.type === "text" ? `${part.text}${event.delta}` : event.delta
|
|
1830
|
+
}));
|
|
1831
|
+
case "text_end": return updatePart(messages, event.messageId, event.partId, (part) => {
|
|
1832
|
+
const next = {
|
|
1833
|
+
id: event.partId,
|
|
1834
|
+
type: "text",
|
|
1835
|
+
text: event.text ?? (part?.type === "text" ? part.text : "")
|
|
1836
|
+
};
|
|
1837
|
+
if (event.signature !== void 0) next.signature = event.signature;
|
|
1838
|
+
return next;
|
|
1839
|
+
});
|
|
1840
|
+
case "reasoning_start": {
|
|
1841
|
+
const reasoning = {
|
|
1842
|
+
id: event.partId,
|
|
1843
|
+
type: "reasoning",
|
|
1844
|
+
text: ""
|
|
1845
|
+
};
|
|
1846
|
+
if (event.reasoningId !== void 0) reasoning.reasoningId = event.reasoningId;
|
|
1847
|
+
return upsertPart(messages, event.messageId, reasoning);
|
|
1848
|
+
}
|
|
1849
|
+
case "reasoning_delta": return updatePart(messages, event.messageId, event.partId, (part) => {
|
|
1850
|
+
const reasoning = {
|
|
1851
|
+
id: event.partId,
|
|
1852
|
+
type: "reasoning",
|
|
1853
|
+
text: part?.type === "reasoning" ? `${part.text}${event.delta}` : event.delta
|
|
1854
|
+
};
|
|
1855
|
+
if (part?.type === "reasoning" && part.reasoningId !== void 0) reasoning.reasoningId = part.reasoningId;
|
|
1856
|
+
const content = appendReasoningContent(part?.type === "reasoning" ? part.content : void 0, event);
|
|
1857
|
+
if (content !== void 0) reasoning.content = content;
|
|
1858
|
+
return reasoning;
|
|
1859
|
+
});
|
|
1860
|
+
case "reasoning_end": return updatePart(messages, event.messageId, event.partId, (part) => {
|
|
1861
|
+
const reasoning = {
|
|
1862
|
+
id: event.partId,
|
|
1863
|
+
type: "reasoning",
|
|
1864
|
+
text: event.text ?? (part?.type === "reasoning" ? part.text : "")
|
|
1865
|
+
};
|
|
1866
|
+
if (part?.type === "reasoning" && part.reasoningId !== void 0) reasoning.reasoningId = part.reasoningId;
|
|
1867
|
+
if (event.content !== void 0) reasoning.content = event.content;
|
|
1868
|
+
else if (part?.type === "reasoning" && part.content !== void 0) reasoning.content = part.content;
|
|
1869
|
+
return reasoning;
|
|
1870
|
+
});
|
|
1871
|
+
case "tool_call_start": {
|
|
1872
|
+
const part = {
|
|
1873
|
+
id: event.partId,
|
|
1874
|
+
type: "tool",
|
|
1875
|
+
toolName: event.toolName ?? "",
|
|
1876
|
+
toolCallId: event.toolCallId,
|
|
1877
|
+
state: "input-streaming",
|
|
1878
|
+
input: ""
|
|
1879
|
+
};
|
|
1880
|
+
if (event.callId !== void 0) part.callId = event.callId;
|
|
1881
|
+
if (event.turn !== void 0) part.turn = event.turn;
|
|
1882
|
+
return upsertPart(messages, event.messageId, part);
|
|
1883
|
+
}
|
|
1884
|
+
case "tool_call_delta": return updatePart(messages, event.messageId, event.partId, (current) => {
|
|
1885
|
+
const part = current?.type === "tool" ? toolPartBase(current) : {
|
|
1886
|
+
id: event.partId,
|
|
1887
|
+
type: "tool",
|
|
1888
|
+
toolName: event.toolName ?? "",
|
|
1889
|
+
toolCallId: event.toolCallId
|
|
1890
|
+
};
|
|
1891
|
+
const previous = current?.type === "tool" && typeof current.input === "string" ? current.input : "";
|
|
1892
|
+
if (event.toolName !== void 0) part.toolName = event.toolName;
|
|
1893
|
+
if (event.callId !== void 0) part.callId = event.callId;
|
|
1894
|
+
if (event.signature !== void 0) part.signature = event.signature;
|
|
1895
|
+
if (event.turn !== void 0) part.turn = event.turn;
|
|
1896
|
+
return {
|
|
1897
|
+
...part,
|
|
1898
|
+
state: "input-streaming",
|
|
1899
|
+
input: event.mode === "replace" ? event.delta : `${previous}${event.delta}`
|
|
1900
|
+
};
|
|
1901
|
+
});
|
|
1902
|
+
case "tool_call_end": {
|
|
1903
|
+
const part = {
|
|
1904
|
+
id: event.partId,
|
|
1905
|
+
type: "tool",
|
|
1906
|
+
toolName: event.toolName,
|
|
1907
|
+
toolCallId: event.toolCallId,
|
|
1908
|
+
state: "input-available",
|
|
1909
|
+
input: event.input
|
|
1910
|
+
};
|
|
1911
|
+
if (event.callId !== void 0) part.callId = event.callId;
|
|
1912
|
+
if (event.turn !== void 0) part.turn = event.turn;
|
|
1913
|
+
if (event.signature !== void 0) part.signature = event.signature;
|
|
1914
|
+
return upsertPart(messages, event.messageId, part);
|
|
1915
|
+
}
|
|
1916
|
+
case "tool_result": return updatePart(messages, event.messageId, event.partId, (current) => {
|
|
1917
|
+
const part = current?.type === "tool" ? toolPartBase(current) : {
|
|
1918
|
+
id: event.partId,
|
|
1919
|
+
type: "tool",
|
|
1920
|
+
toolName: event.toolName,
|
|
1921
|
+
toolCallId: event.toolCallId
|
|
1922
|
+
};
|
|
1923
|
+
if (event.callId !== void 0) part.callId = event.callId;
|
|
1924
|
+
if (event.internalCallId !== void 0) part.internalCallId = event.internalCallId;
|
|
1925
|
+
if (event.turn !== void 0) part.turn = event.turn;
|
|
1926
|
+
if (event.result.status === "error") return {
|
|
1927
|
+
...part,
|
|
1928
|
+
state: "error",
|
|
1929
|
+
input: event.input,
|
|
1930
|
+
error: event.result.error
|
|
1931
|
+
};
|
|
1932
|
+
const completed = {
|
|
1933
|
+
...part,
|
|
1934
|
+
state: "output-available",
|
|
1935
|
+
input: event.input,
|
|
1936
|
+
output: event.result.output
|
|
1937
|
+
};
|
|
1938
|
+
if (event.result.content !== void 0) completed.resultContent = event.result.content;
|
|
1939
|
+
return completed;
|
|
1940
|
+
});
|
|
1941
|
+
case "source": return upsertPart(messages, event.messageId, {
|
|
1942
|
+
id: event.partId,
|
|
1943
|
+
type: "source",
|
|
1944
|
+
source: event.source
|
|
1945
|
+
});
|
|
1946
|
+
case "attachment": return upsertPart(messages, event.messageId, {
|
|
1947
|
+
id: event.partId,
|
|
1948
|
+
type: "attachment",
|
|
1949
|
+
attachment: event.attachment
|
|
1950
|
+
});
|
|
1951
|
+
case "data":
|
|
1952
|
+
if (event.transient === true) return messages;
|
|
1953
|
+
return appendToLastAssistant(messages, {
|
|
1954
|
+
id: createClientId("data"),
|
|
1955
|
+
type: "data",
|
|
1956
|
+
name: event.name,
|
|
1957
|
+
data: event.data
|
|
1958
|
+
});
|
|
1959
|
+
case "message_end": return messages.map((message) => {
|
|
1960
|
+
if (message.id !== event.messageId) return message;
|
|
1961
|
+
const metadata = mergeMetadata(message.metadata, event.metadata);
|
|
1962
|
+
const generationUpdate = { runId: event.runId };
|
|
1963
|
+
if (event.usage !== void 0) generationUpdate.usage = event.usage;
|
|
1964
|
+
if (event.contextUsage !== void 0) generationUpdate.contextUsage = event.contextUsage;
|
|
1965
|
+
const generation = mergeGeneration(message.generation, generationUpdate);
|
|
1966
|
+
let updated = {
|
|
1967
|
+
...message,
|
|
1968
|
+
generation
|
|
1969
|
+
};
|
|
1970
|
+
if (event.parts !== void 0) updated = {
|
|
1971
|
+
...updated,
|
|
1972
|
+
parts: reconcileFinalParts(message.parts, event.parts)
|
|
1973
|
+
};
|
|
1974
|
+
if (event.modelMessageId !== void 0) updated = {
|
|
1975
|
+
...updated,
|
|
1976
|
+
modelMessageId: event.modelMessageId
|
|
1977
|
+
};
|
|
1978
|
+
if (metadata !== void 0) updated = {
|
|
1979
|
+
...updated,
|
|
1980
|
+
metadata
|
|
1981
|
+
};
|
|
1982
|
+
return updated;
|
|
1983
|
+
});
|
|
1984
|
+
case "run_end": {
|
|
1985
|
+
const generation = {
|
|
1986
|
+
runId: event.runId,
|
|
1987
|
+
status: event.status
|
|
1988
|
+
};
|
|
1989
|
+
if (event.contextUsage !== void 0) generation.contextUsage = event.contextUsage;
|
|
1990
|
+
if (event.trace !== void 0) generation.trace = event.trace;
|
|
1991
|
+
if (event.memoryCompaction !== void 0) generation.memoryCompaction = event.memoryCompaction;
|
|
1992
|
+
return updateLastAssistantGeneration(messages, generation);
|
|
1993
|
+
}
|
|
1994
|
+
case "error": return appendToLastAssistant(messages, {
|
|
1995
|
+
id: createClientId("error"),
|
|
1996
|
+
type: "error",
|
|
1997
|
+
error: event.error
|
|
1998
|
+
});
|
|
1999
|
+
default: return messages;
|
|
2000
|
+
}
|
|
2428
2001
|
}
|
|
2429
2002
|
function toolPartBase(part) {
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
if (part.signature !== void 0) Object.assign(base, { signature: part.signature });
|
|
2442
|
-
return base;
|
|
2003
|
+
const base = {
|
|
2004
|
+
id: part.id,
|
|
2005
|
+
type: part.type,
|
|
2006
|
+
toolName: part.toolName,
|
|
2007
|
+
toolCallId: part.toolCallId
|
|
2008
|
+
};
|
|
2009
|
+
if (part.callId !== void 0) Object.assign(base, { callId: part.callId });
|
|
2010
|
+
if (part.internalCallId !== void 0) Object.assign(base, { internalCallId: part.internalCallId });
|
|
2011
|
+
if (part.turn !== void 0) Object.assign(base, { turn: part.turn });
|
|
2012
|
+
if (part.signature !== void 0) Object.assign(base, { signature: part.signature });
|
|
2013
|
+
return base;
|
|
2443
2014
|
}
|
|
2444
2015
|
function reconcileFinalParts(current, final) {
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
);
|
|
2448
|
-
return [...final, ...clientOnly];
|
|
2016
|
+
const clientOnly = current.filter((part) => part.type === "source" || part.type === "data" || part.type === "error");
|
|
2017
|
+
return [...final, ...clientOnly];
|
|
2449
2018
|
}
|
|
2450
2019
|
function appendReasoningContent(current, event) {
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2020
|
+
if (event.contentType === void 0 && event.signature === void 0) return current;
|
|
2021
|
+
const type = event.contentType ?? "text";
|
|
2022
|
+
const next = [...current ?? []];
|
|
2023
|
+
const last = next.at(-1);
|
|
2024
|
+
if (type === "text") {
|
|
2025
|
+
if (last?.type === "text" && last.signature === event.signature) next[next.length - 1] = {
|
|
2026
|
+
...last,
|
|
2027
|
+
text: `${last.text}${event.delta}`
|
|
2028
|
+
};
|
|
2029
|
+
else {
|
|
2030
|
+
let detail = {
|
|
2031
|
+
type,
|
|
2032
|
+
text: event.delta
|
|
2033
|
+
};
|
|
2034
|
+
if (event.signature !== void 0) detail = {
|
|
2035
|
+
...detail,
|
|
2036
|
+
signature: event.signature
|
|
2037
|
+
};
|
|
2038
|
+
next.push(detail);
|
|
2039
|
+
}
|
|
2040
|
+
return next;
|
|
2041
|
+
}
|
|
2042
|
+
if (type === "summary") {
|
|
2043
|
+
if (last?.type === "summary") next[next.length - 1] = {
|
|
2044
|
+
...last,
|
|
2045
|
+
text: `${last.text}${event.delta}`
|
|
2046
|
+
};
|
|
2047
|
+
else next.push({
|
|
2048
|
+
type,
|
|
2049
|
+
text: event.delta
|
|
2050
|
+
});
|
|
2051
|
+
return next;
|
|
2052
|
+
}
|
|
2053
|
+
if (type === "encrypted") {
|
|
2054
|
+
if (last?.type === "encrypted") next[next.length - 1] = {
|
|
2055
|
+
...last,
|
|
2056
|
+
data: `${last.data}${event.delta}`
|
|
2057
|
+
};
|
|
2058
|
+
else next.push({
|
|
2059
|
+
type,
|
|
2060
|
+
data: event.delta
|
|
2061
|
+
});
|
|
2062
|
+
return next;
|
|
2063
|
+
}
|
|
2064
|
+
if (last?.type === "redacted") next[next.length - 1] = {
|
|
2065
|
+
...last,
|
|
2066
|
+
data: `${last.data}${event.delta}`
|
|
2067
|
+
};
|
|
2068
|
+
else next.push({
|
|
2069
|
+
type,
|
|
2070
|
+
data: event.delta
|
|
2071
|
+
});
|
|
2072
|
+
return next;
|
|
2484
2073
|
}
|
|
2485
2074
|
function messageText(message) {
|
|
2486
|
-
|
|
2075
|
+
return message.parts.flatMap((part) => part.type === "text" ? [part.text] : []).join("");
|
|
2487
2076
|
}
|
|
2488
2077
|
function assistantText(messages) {
|
|
2489
|
-
|
|
2490
|
-
|
|
2078
|
+
const assistant = [...messages].reverse().find((message) => message.role === "assistant");
|
|
2079
|
+
return assistant === void 0 ? "" : messageText(assistant);
|
|
2491
2080
|
}
|
|
2492
2081
|
function upsertPart(messages, messageId, part) {
|
|
2493
|
-
|
|
2082
|
+
return updatePart(messages, messageId, part.id, () => part);
|
|
2494
2083
|
}
|
|
2495
2084
|
function updatePart(messages, messageId, partId, update) {
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2085
|
+
return ensureAssistant(messages, messageId).map((message) => {
|
|
2086
|
+
if (message.id !== messageId) return message;
|
|
2087
|
+
const index = message.parts.findIndex((part) => part.id === partId);
|
|
2088
|
+
if (index === -1) return {
|
|
2089
|
+
...message,
|
|
2090
|
+
parts: [...message.parts, update(void 0)]
|
|
2091
|
+
};
|
|
2092
|
+
const parts = [...message.parts];
|
|
2093
|
+
parts[index] = update(parts[index]);
|
|
2094
|
+
return {
|
|
2095
|
+
...message,
|
|
2096
|
+
parts
|
|
2097
|
+
};
|
|
2098
|
+
});
|
|
2505
2099
|
}
|
|
2506
2100
|
function ensureAssistant(messages, messageId) {
|
|
2507
|
-
|
|
2101
|
+
return messages.some((message) => message.id === messageId) ? messages : [...messages, {
|
|
2102
|
+
id: messageId,
|
|
2103
|
+
role: "assistant",
|
|
2104
|
+
parts: []
|
|
2105
|
+
}];
|
|
2508
2106
|
}
|
|
2509
2107
|
function appendToLastAssistant(messages, part) {
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2108
|
+
const index = findLastIndex(messages, (message) => message.role === "assistant");
|
|
2109
|
+
if (index === -1) return [...messages, {
|
|
2110
|
+
id: createClientId("msg"),
|
|
2111
|
+
role: "assistant",
|
|
2112
|
+
parts: [part]
|
|
2113
|
+
}];
|
|
2114
|
+
const next = [...messages];
|
|
2115
|
+
const message = next[index];
|
|
2116
|
+
next[index] = {
|
|
2117
|
+
...message,
|
|
2118
|
+
parts: [...message.parts, part]
|
|
2119
|
+
};
|
|
2120
|
+
return next;
|
|
2518
2121
|
}
|
|
2519
2122
|
function updateLastAssistantGeneration(messages, value) {
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
|
|
2123
|
+
const index = findLastIndex(messages, (message) => message.role === "assistant");
|
|
2124
|
+
if (index === -1) return messages;
|
|
2125
|
+
const next = [...messages];
|
|
2126
|
+
const message = next[index];
|
|
2127
|
+
next[index] = {
|
|
2128
|
+
...message,
|
|
2129
|
+
generation: mergeGeneration(message.generation, value)
|
|
2130
|
+
};
|
|
2131
|
+
return next;
|
|
2526
2132
|
}
|
|
2527
2133
|
function mergeMetadata(current, provided) {
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2134
|
+
if (provided === void 0) return current;
|
|
2135
|
+
if (!isRecord(current) || !isRecord(provided)) return provided;
|
|
2136
|
+
const next = {
|
|
2137
|
+
...current,
|
|
2138
|
+
...provided
|
|
2139
|
+
};
|
|
2140
|
+
return isJsonValue$1(next) ? next : provided;
|
|
2532
2141
|
}
|
|
2533
2142
|
function mergeGeneration(current, provided) {
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2143
|
+
return {
|
|
2144
|
+
...current,
|
|
2145
|
+
...Object.fromEntries(Object.entries(provided).filter(([, value]) => value !== void 0))
|
|
2146
|
+
};
|
|
2538
2147
|
}
|
|
2539
2148
|
function findLastIndex(items, predicate) {
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
export {
|
|
2549
|
-
CLIENT_STREAM_PROTOCOL,
|
|
2550
|
-
ClientProtocolError,
|
|
2551
|
-
agentToClientStream,
|
|
2552
|
-
applyClientStreamEvent,
|
|
2553
|
-
assistantText,
|
|
2554
|
-
completionToClientStream,
|
|
2555
|
-
createClientId,
|
|
2556
|
-
createDirectClientTransport,
|
|
2557
|
-
createHttpClientTransport,
|
|
2558
|
-
customAgentEventsToClientStream,
|
|
2559
|
-
isJsonValue5 as isJsonValue,
|
|
2560
|
-
maskedClientError,
|
|
2561
|
-
messageText,
|
|
2562
|
-
messagesToUIMessages,
|
|
2563
|
-
normalizeClientError,
|
|
2564
|
-
parseClientStreamEvent,
|
|
2565
|
-
parseClientStreamFrame,
|
|
2566
|
-
parseClientStreamRequest,
|
|
2567
|
-
parseUIMessage,
|
|
2568
|
-
parseUIMessages,
|
|
2569
|
-
uiMessagesToMessages
|
|
2570
|
-
};
|
|
2149
|
+
for (let index = items.length - 1; index >= 0; index -= 1) if (predicate(items[index])) return index;
|
|
2150
|
+
return -1;
|
|
2151
|
+
}
|
|
2152
|
+
function isRecord(value) {
|
|
2153
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2154
|
+
}
|
|
2155
|
+
export { CLIENT_STREAM_PROTOCOL, ClientProtocolError, agentToClientStream, applyClientStreamEvent, assistantText, completionToClientStream, createClientId, createDirectClientTransport, createHttpClientTransport, customAgentEventsToClientStream, isJsonValue, maskedClientError, messageText, messagesToUIMessages, normalizeClientError, parseClientStreamEvent, parseClientStreamFrame, parseClientStreamRequest, parseUIMessage, parseUIMessages, uiMessagesToMessages };
|
|
2156
|
+
|
|
2571
2157
|
//# sourceMappingURL=index.js.map
|