@anvia/client 1.0.0-rc.2 → 1.0.0-rc.3
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 +4 -1
- package/dist/index.d.ts +21 -59
- package/dist/index.js +377 -317
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -51,21 +51,23 @@ function uiMessagesToMessages(messages) {
|
|
|
51
51
|
if (part.type === "text") {
|
|
52
52
|
content2.push(textContent(part.text, part.signature));
|
|
53
53
|
} else if (part.type === "reasoning") {
|
|
54
|
-
|
|
54
|
+
let reasoning = {
|
|
55
55
|
type: "reasoning",
|
|
56
|
-
text: part.text
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
56
|
+
text: part.text
|
|
57
|
+
};
|
|
58
|
+
if (part.reasoningId !== void 0) reasoning = { ...reasoning, id: part.reasoningId };
|
|
59
|
+
if (part.content !== void 0) reasoning = { ...reasoning, details: part.content };
|
|
60
|
+
content2.push(reasoning);
|
|
60
61
|
} else if (part.type === "tool") {
|
|
61
|
-
|
|
62
|
+
let toolCall = {
|
|
62
63
|
type: "tool-call",
|
|
63
64
|
toolCallId: part.toolCallId,
|
|
64
|
-
...part.callId === void 0 ? {} : { callId: part.callId },
|
|
65
65
|
toolName: part.toolName,
|
|
66
|
-
input: part.input ?? {}
|
|
67
|
-
|
|
68
|
-
}
|
|
66
|
+
input: part.input ?? {}
|
|
67
|
+
};
|
|
68
|
+
if (part.callId !== void 0) toolCall = { ...toolCall, callId: part.callId };
|
|
69
|
+
if (part.signature !== void 0) toolCall = { ...toolCall, signature: part.signature };
|
|
70
|
+
content2.push(toolCall);
|
|
69
71
|
if (part.state === "output-available" || part.state === "error") {
|
|
70
72
|
toolResults.push(toolResultFromPart(part));
|
|
71
73
|
}
|
|
@@ -73,12 +75,15 @@ function uiMessagesToMessages(messages) {
|
|
|
73
75
|
content2.push(attachmentToAssistantContent(part.attachment));
|
|
74
76
|
}
|
|
75
77
|
}
|
|
76
|
-
|
|
78
|
+
let assistant = {
|
|
77
79
|
role: "assistant",
|
|
78
80
|
content: content2,
|
|
79
|
-
...message.modelMessageId === void 0 ? {} : { id: message.modelMessageId },
|
|
80
81
|
...metadataField(message.metadata)
|
|
81
|
-
}
|
|
82
|
+
};
|
|
83
|
+
if (message.modelMessageId !== void 0) {
|
|
84
|
+
assistant = { ...assistant, id: message.modelMessageId };
|
|
85
|
+
}
|
|
86
|
+
result.push(assistant);
|
|
82
87
|
if (toolResults.length > 0) {
|
|
83
88
|
result.push({ role: "tool", content: toolResults });
|
|
84
89
|
}
|
|
@@ -121,13 +126,13 @@ function messagesToUIMessages(messages) {
|
|
|
121
126
|
if (message.role === "assistant") {
|
|
122
127
|
const content = typeof message.content === "string" ? [{ type: "text", text: message.content }] : message.content;
|
|
123
128
|
const parts = content.map(assistantContentToUIMessagePart);
|
|
124
|
-
|
|
129
|
+
let uiMessage = {
|
|
125
130
|
id: createId("msg"),
|
|
126
131
|
role: "assistant",
|
|
127
132
|
parts,
|
|
128
|
-
...message.id === void 0 ? {} : { modelMessageId: message.id },
|
|
129
133
|
...metadataField(message.metadata)
|
|
130
134
|
};
|
|
135
|
+
if (message.id !== void 0) uiMessage = { ...uiMessage, modelMessageId: message.id };
|
|
131
136
|
const messageIndex = result.length;
|
|
132
137
|
result.push(uiMessage);
|
|
133
138
|
for (const [partIndex, part] of parts.entries()) {
|
|
@@ -140,6 +145,7 @@ function messagesToUIMessages(messages) {
|
|
|
140
145
|
}
|
|
141
146
|
const unmerged = [];
|
|
142
147
|
for (const content of message.content) {
|
|
148
|
+
if (content.type !== "tool-result") continue;
|
|
143
149
|
const location = (content.callId === void 0 ? void 0 : byCallId.get(content.callId)) ?? byToolCallId.get(content.toolCallId);
|
|
144
150
|
const part = toolResultToUIMessagePart(content, toolNameAt(result, location));
|
|
145
151
|
if (message.metadata === void 0 && location !== void 0) {
|
|
@@ -167,12 +173,13 @@ function messagesToUIMessages(messages) {
|
|
|
167
173
|
}
|
|
168
174
|
function contentToUIMessagePart(content) {
|
|
169
175
|
if (content.type === "text") {
|
|
170
|
-
|
|
176
|
+
const part = {
|
|
171
177
|
id: createId("part"),
|
|
172
178
|
type: "text",
|
|
173
|
-
text: content.text
|
|
174
|
-
...content.signature === void 0 ? {} : { signature: content.signature }
|
|
179
|
+
text: content.text
|
|
175
180
|
};
|
|
181
|
+
if (content.signature !== void 0) part.signature = content.signature;
|
|
182
|
+
return part;
|
|
176
183
|
}
|
|
177
184
|
return {
|
|
178
185
|
id: createId("part"),
|
|
@@ -183,25 +190,27 @@ function contentToUIMessagePart(content) {
|
|
|
183
190
|
function assistantContentToUIMessagePart(content) {
|
|
184
191
|
if (content.type === "text") return contentToUIMessagePart(content);
|
|
185
192
|
if (content.type === "reasoning") {
|
|
186
|
-
|
|
193
|
+
const part = {
|
|
187
194
|
id: createId("part"),
|
|
188
195
|
type: "reasoning",
|
|
189
|
-
text: content.text
|
|
190
|
-
...content.id === void 0 ? {} : { reasoningId: content.id },
|
|
191
|
-
...content.details === void 0 ? {} : { content: content.details }
|
|
196
|
+
text: content.text
|
|
192
197
|
};
|
|
198
|
+
if (content.id !== void 0) part.reasoningId = content.id;
|
|
199
|
+
if (content.details !== void 0) part.content = content.details;
|
|
200
|
+
return part;
|
|
193
201
|
}
|
|
194
202
|
if (content.type === "tool-call") {
|
|
195
|
-
|
|
203
|
+
const part = {
|
|
196
204
|
id: toolPartId(content.toolCallId),
|
|
197
205
|
type: "tool",
|
|
198
206
|
toolName: content.toolName,
|
|
199
207
|
toolCallId: content.toolCallId,
|
|
200
208
|
state: "input-available",
|
|
201
|
-
input: content.input
|
|
202
|
-
...content.callId === void 0 ? {} : { callId: content.callId },
|
|
203
|
-
...content.signature === void 0 ? {} : { signature: content.signature }
|
|
209
|
+
input: content.input
|
|
204
210
|
};
|
|
211
|
+
if (content.callId !== void 0) part.callId = content.callId;
|
|
212
|
+
if (content.signature !== void 0) part.signature = content.signature;
|
|
213
|
+
return part;
|
|
205
214
|
}
|
|
206
215
|
return {
|
|
207
216
|
id: createId("part"),
|
|
@@ -213,13 +222,14 @@ function textContent(text, signature) {
|
|
|
213
222
|
return signature === void 0 ? { type: "text", text } : { type: "text", text, signature };
|
|
214
223
|
}
|
|
215
224
|
function toolResultFromPart(part) {
|
|
216
|
-
|
|
225
|
+
let result = {
|
|
217
226
|
type: "tool-result",
|
|
218
227
|
toolCallId: part.toolCallId,
|
|
219
|
-
...part.callId === void 0 ? {} : { callId: part.callId },
|
|
220
228
|
toolName: part.toolName,
|
|
221
229
|
output: toolOutputFromPart(part)
|
|
222
230
|
};
|
|
231
|
+
if (part.callId !== void 0) result = { ...result, callId: part.callId };
|
|
232
|
+
return result;
|
|
223
233
|
}
|
|
224
234
|
function toolOutputFromPart(part) {
|
|
225
235
|
if (part.state === "error") {
|
|
@@ -244,23 +254,25 @@ function attachmentToImageContent(attachment) {
|
|
|
244
254
|
const image = attachment.url !== void 0 ? { type: "url", url: attachment.url } : attachment.data !== void 0 ? { type: "data", data: attachment.data } : (() => {
|
|
245
255
|
throw new TypeError("Image attachments require a URL or base64 data.");
|
|
246
256
|
})();
|
|
247
|
-
|
|
257
|
+
let content = {
|
|
248
258
|
type: "image",
|
|
249
|
-
image
|
|
250
|
-
...attachment.mediaType === void 0 ? {} : { mediaType: attachment.mediaType },
|
|
251
|
-
...attachment.detail === void 0 ? {} : { detail: attachment.detail }
|
|
259
|
+
image
|
|
252
260
|
};
|
|
261
|
+
if (attachment.mediaType !== void 0) content = { ...content, mediaType: attachment.mediaType };
|
|
262
|
+
if (attachment.detail !== void 0) content = { ...content, detail: attachment.detail };
|
|
263
|
+
return content;
|
|
253
264
|
}
|
|
254
265
|
function attachmentToFileContent(attachment) {
|
|
255
266
|
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 } : (() => {
|
|
256
267
|
throw new TypeError("File attachments require a URL, base64 data, or text.");
|
|
257
268
|
})();
|
|
258
|
-
|
|
269
|
+
let content = {
|
|
259
270
|
type: "file",
|
|
260
271
|
data,
|
|
261
|
-
mediaType: attachment.mediaType ?? "application/octet-stream"
|
|
262
|
-
...attachment.name === void 0 ? {} : { filename: attachment.name }
|
|
272
|
+
mediaType: attachment.mediaType ?? "application/octet-stream"
|
|
263
273
|
};
|
|
274
|
+
if (attachment.name !== void 0) content = { ...content, filename: attachment.name };
|
|
275
|
+
return content;
|
|
264
276
|
}
|
|
265
277
|
function contentToAttachment(content) {
|
|
266
278
|
const attachment = {
|
|
@@ -287,9 +299,9 @@ function toolResultToUIMessagePart(content, fallbackToolName) {
|
|
|
287
299
|
id: toolPartId(content.toolCallId),
|
|
288
300
|
type: "tool",
|
|
289
301
|
toolName: content.toolName || fallbackToolName || "tool",
|
|
290
|
-
toolCallId: content.toolCallId
|
|
291
|
-
...content.callId === void 0 ? {} : { callId: content.callId }
|
|
302
|
+
toolCallId: content.toolCallId
|
|
292
303
|
};
|
|
304
|
+
if (content.callId !== void 0) Object.assign(common, { callId: content.callId });
|
|
293
305
|
if (output.type === "error-text" || output.type === "error-json") {
|
|
294
306
|
return {
|
|
295
307
|
...common,
|
|
@@ -306,12 +318,13 @@ function toolResultToUIMessagePart(content, fallbackToolName) {
|
|
|
306
318
|
error: { message: output.reason ?? "Tool execution was denied." }
|
|
307
319
|
};
|
|
308
320
|
}
|
|
309
|
-
|
|
321
|
+
const part = {
|
|
310
322
|
...common,
|
|
311
323
|
state: "output-available",
|
|
312
|
-
output: toolResultOutput(output)
|
|
313
|
-
...output.type === "content" ? { resultContent: output.value } : {}
|
|
324
|
+
output: toolResultOutput(output)
|
|
314
325
|
};
|
|
326
|
+
if (output.type === "content") part.resultContent = output.value;
|
|
327
|
+
return part;
|
|
315
328
|
}
|
|
316
329
|
function toolResultOutput(output) {
|
|
317
330
|
if (output.type === "text" || output.type === "error-text") return output.value;
|
|
@@ -359,10 +372,11 @@ function createId(prefix) {
|
|
|
359
372
|
}
|
|
360
373
|
|
|
361
374
|
// src/protocol.ts
|
|
375
|
+
import { parseAgentInteractionRequest, parseAgentInteractionResponse } from "@anvia/core/agent";
|
|
362
376
|
import { isJsonValue as isJsonValue2, parseMessages } from "@anvia/core/completion";
|
|
363
377
|
|
|
364
378
|
// src/types.ts
|
|
365
|
-
var CLIENT_STREAM_PROTOCOL = "anvia.client.
|
|
379
|
+
var CLIENT_STREAM_PROTOCOL = "anvia.client.v3";
|
|
366
380
|
|
|
367
381
|
// src/protocol.ts
|
|
368
382
|
var ClientProtocolError = class extends Error {
|
|
@@ -394,14 +408,19 @@ function parseUIMessage(value, options = {}) {
|
|
|
394
408
|
if (message.generation !== void 0 && !isUIMessageGeneration(message.generation)) {
|
|
395
409
|
invalid("UI message.generation", value);
|
|
396
410
|
}
|
|
397
|
-
|
|
411
|
+
let parsed = {
|
|
398
412
|
id: message.id,
|
|
399
413
|
role: message.role,
|
|
400
|
-
parts
|
|
401
|
-
...message.modelMessageId === void 0 ? {} : { modelMessageId: message.modelMessageId },
|
|
402
|
-
...metadata === void 0 ? {} : { metadata },
|
|
403
|
-
...message.generation === void 0 ? {} : { generation: message.generation }
|
|
414
|
+
parts
|
|
404
415
|
};
|
|
416
|
+
if (message.modelMessageId !== void 0) {
|
|
417
|
+
parsed = { ...parsed, modelMessageId: message.modelMessageId };
|
|
418
|
+
}
|
|
419
|
+
if (metadata !== void 0) parsed = { ...parsed, metadata };
|
|
420
|
+
if (message.generation !== void 0) {
|
|
421
|
+
parsed = { ...parsed, generation: message.generation };
|
|
422
|
+
}
|
|
423
|
+
return parsed;
|
|
405
424
|
}
|
|
406
425
|
function parseUIMessages(value, options = {}) {
|
|
407
426
|
if (!Array.isArray(value)) {
|
|
@@ -411,17 +430,52 @@ function parseUIMessages(value, options = {}) {
|
|
|
411
430
|
}
|
|
412
431
|
function parseClientStreamRequest(value) {
|
|
413
432
|
const input = requireRecord(value, "Client stream request");
|
|
414
|
-
requireOnlyKeys(
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
433
|
+
requireOnlyKeys(
|
|
434
|
+
input,
|
|
435
|
+
["type", "messages", "interactionId", "response", "metadata", "resume"],
|
|
436
|
+
"Client stream request"
|
|
437
|
+
);
|
|
438
|
+
requireOneOf(
|
|
439
|
+
input.type,
|
|
440
|
+
["messages", "interaction_response"],
|
|
441
|
+
"Client stream request.type",
|
|
442
|
+
value
|
|
443
|
+
);
|
|
421
444
|
if (input.metadata !== void 0 && !isJsonObject(input.metadata)) {
|
|
422
445
|
throw new ClientProtocolError("Client stream request.metadata must be a JSON object.", value);
|
|
423
446
|
}
|
|
424
|
-
|
|
447
|
+
let request;
|
|
448
|
+
if (input.type === "messages") {
|
|
449
|
+
if (input.interactionId !== void 0 || input.response !== void 0) {
|
|
450
|
+
throw new ClientProtocolError(
|
|
451
|
+
"Messages requests cannot include interaction response fields.",
|
|
452
|
+
value
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
let messages;
|
|
456
|
+
try {
|
|
457
|
+
messages = parseMessages(input.messages);
|
|
458
|
+
} catch {
|
|
459
|
+
throw new ClientProtocolError("Client stream request.messages must be Message[].", value);
|
|
460
|
+
}
|
|
461
|
+
request = { type: "messages", messages };
|
|
462
|
+
} else {
|
|
463
|
+
if (input.messages !== void 0 || typeof input.interactionId !== "string") {
|
|
464
|
+
throw new ClientProtocolError(
|
|
465
|
+
"Interaction response requests require interactionId and cannot include messages.",
|
|
466
|
+
value
|
|
467
|
+
);
|
|
468
|
+
}
|
|
469
|
+
try {
|
|
470
|
+
request = {
|
|
471
|
+
type: "interaction_response",
|
|
472
|
+
interactionId: input.interactionId,
|
|
473
|
+
response: parseAgentInteractionResponse(input.response)
|
|
474
|
+
};
|
|
475
|
+
} catch {
|
|
476
|
+
throw new ClientProtocolError("Client interaction response is invalid.", value);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
425
479
|
if (input.metadata !== void 0) request.metadata = input.metadata;
|
|
426
480
|
if (input.resume !== void 0) {
|
|
427
481
|
const resume = requireRecord(input.resume, "Client stream request.resume");
|
|
@@ -582,13 +636,13 @@ function parseClientStreamEvent(value, options = {}) {
|
|
|
582
636
|
requireEventKeys(event, ["toolCall"]);
|
|
583
637
|
if (!isProviderToolCall(event.toolCall)) invalid("provider_tool_call.toolCall", value);
|
|
584
638
|
break;
|
|
585
|
-
case "
|
|
586
|
-
requireEventKeys(event, ["
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
639
|
+
case "interaction":
|
|
640
|
+
requireEventKeys(event, ["interaction"]);
|
|
641
|
+
try {
|
|
642
|
+
parseAgentInteractionRequest(event.interaction);
|
|
643
|
+
} catch {
|
|
644
|
+
invalid("interaction.interaction", value);
|
|
645
|
+
}
|
|
592
646
|
break;
|
|
593
647
|
case "guardrail_decision":
|
|
594
648
|
requireEventKeys(event, ["decision"]);
|
|
@@ -661,7 +715,7 @@ function parseClientStreamEvent(value, options = {}) {
|
|
|
661
715
|
]);
|
|
662
716
|
requireOneOf(
|
|
663
717
|
event.status,
|
|
664
|
-
["completed", "blocked", "
|
|
718
|
+
["completed", "blocked", "suspended", "cancelled", "error"],
|
|
665
719
|
"run_end.status",
|
|
666
720
|
value
|
|
667
721
|
);
|
|
@@ -708,8 +762,11 @@ function parseClientStreamFrame(value, options = {}) {
|
|
|
708
762
|
["type", "protocol", "streamId", "eventId", "resumable"],
|
|
709
763
|
"stream_start frame"
|
|
710
764
|
);
|
|
711
|
-
if (frame.protocol === "anvia.client.v1") {
|
|
712
|
-
throw new ClientProtocolError(
|
|
765
|
+
if (frame.protocol === "anvia.client.v1" || frame.protocol === "anvia.client.v2") {
|
|
766
|
+
throw new ClientProtocolError(
|
|
767
|
+
`Client stream protocol ${frame.protocol.slice(-2)} is not supported.`,
|
|
768
|
+
value
|
|
769
|
+
);
|
|
713
770
|
}
|
|
714
771
|
if (frame.protocol !== CLIENT_STREAM_PROTOCOL || typeof frame.streamId !== "string" || frame.eventId !== 0 || typeof frame.resumable !== "boolean") {
|
|
715
772
|
throw new ClientProtocolError("Invalid client stream_start frame.", value);
|
|
@@ -792,7 +849,7 @@ function isUIMessageGeneration(value) {
|
|
|
792
849
|
if (!hasOnlyKeys(value, ["runId", "status", "usage", "contextUsage", "trace", "memoryCompaction"])) {
|
|
793
850
|
return false;
|
|
794
851
|
}
|
|
795
|
-
return (value.runId === void 0 || typeof value.runId === "string") && (value.status === void 0 || ["completed", "blocked", "
|
|
852
|
+
return (value.runId === void 0 || typeof value.runId === "string") && (value.status === void 0 || ["completed", "blocked", "suspended", "cancelled", "error"].includes(
|
|
796
853
|
value.status
|
|
797
854
|
)) && (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(value.memoryCompaction) && validateMemoryCompactionForGuard(value.memoryCompaction));
|
|
798
855
|
}
|
|
@@ -924,50 +981,6 @@ function isCompletionSource(value) {
|
|
|
924
981
|
function isProviderToolCall(value) {
|
|
925
982
|
return isRecord(value) && hasOnlyKeys(value, ["id", "name", "status", "details"]) && typeof value.id === "string" && typeof value.name === "string" && (value.status === void 0 || typeof value.status === "string") && (value.details === void 0 || isRecord(value.details) && isJsonValue2(value.details));
|
|
926
983
|
}
|
|
927
|
-
function isToolApproval(value) {
|
|
928
|
-
if (!isRecord(value)) return false;
|
|
929
|
-
const optionalStrings = [
|
|
930
|
-
"runId",
|
|
931
|
-
"agentId",
|
|
932
|
-
"sessionId",
|
|
933
|
-
"callId",
|
|
934
|
-
"internalCallId",
|
|
935
|
-
"requestedAt",
|
|
936
|
-
"resolvedAt",
|
|
937
|
-
"reason"
|
|
938
|
-
];
|
|
939
|
-
return hasOnlyKeys(value, ["id", "toolName", "input", "status", ...optionalStrings]) && typeof value.id === "string" && typeof value.toolName === "string" && optionalStrings.every((key) => value[key] === void 0 || typeof value[key] === "string") && (value.input === void 0 || isJsonValue2(value.input)) && typeof value.status === "string" && ["pending", "approved", "rejected", "timed_out", "cancelled"].includes(value.status);
|
|
940
|
-
}
|
|
941
|
-
function isToolQuestion(value) {
|
|
942
|
-
if (!isRecord(value)) return false;
|
|
943
|
-
const optionalStrings = [
|
|
944
|
-
"runId",
|
|
945
|
-
"agentId",
|
|
946
|
-
"sessionId",
|
|
947
|
-
"callId",
|
|
948
|
-
"internalCallId",
|
|
949
|
-
"requestedAt",
|
|
950
|
-
"answeredAt",
|
|
951
|
-
"cancelledAt"
|
|
952
|
-
];
|
|
953
|
-
return hasOnlyKeys(value, [
|
|
954
|
-
"id",
|
|
955
|
-
"toolName",
|
|
956
|
-
"input",
|
|
957
|
-
"questions",
|
|
958
|
-
"status",
|
|
959
|
-
"answers",
|
|
960
|
-
...optionalStrings
|
|
961
|
-
]) && typeof value.id === "string" && typeof value.toolName === "string" && optionalStrings.every((key) => value[key] === void 0 || typeof value[key] === "string") && (value.input === void 0 || isJsonValue2(value.input)) && Array.isArray(value.questions) && value.questions.every(isToolQuestionPrompt) && typeof value.status === "string" && ["pending", "answered", "cancelled"].includes(value.status) && (value.answers === void 0 || Array.isArray(value.answers) && value.answers.every(isToolQuestionAnswer));
|
|
962
|
-
}
|
|
963
|
-
function isToolQuestionPrompt(value) {
|
|
964
|
-
return isRecord(value) && hasOnlyKeys(value, ["id", "question", "choices"]) && typeof value.id === "string" && typeof value.question === "string" && Array.isArray(value.choices) && value.choices.every(
|
|
965
|
-
(choice) => isRecord(choice) && hasOnlyKeys(choice, ["label", "value"]) && typeof choice.label === "string" && typeof choice.value === "string"
|
|
966
|
-
);
|
|
967
|
-
}
|
|
968
|
-
function isToolQuestionAnswer(value) {
|
|
969
|
-
return isRecord(value) && hasOnlyKeys(value, ["questionId", "answer", "choice", "custom"]) && typeof value.questionId === "string" && typeof value.answer === "string" && (value.choice === void 0 || typeof value.choice === "string") && (value.custom === void 0 || typeof value.custom === "boolean");
|
|
970
|
-
}
|
|
971
984
|
function isGuardrailDecision(value) {
|
|
972
985
|
return isRecord(value) && hasOnlyKeys(value, [
|
|
973
986
|
"policyId",
|
|
@@ -1087,11 +1100,12 @@ function completionToClientStream(options) {
|
|
|
1087
1100
|
return propagateCancellation(events, (source) => ({
|
|
1088
1101
|
async *[Symbol.asyncIterator]() {
|
|
1089
1102
|
const state = createMessageState({ runId });
|
|
1090
|
-
|
|
1103
|
+
const runStart = {
|
|
1091
1104
|
type: "run_start",
|
|
1092
|
-
source: "completion"
|
|
1093
|
-
|
|
1094
|
-
|
|
1105
|
+
source: "completion"
|
|
1106
|
+
};
|
|
1107
|
+
if (options.metadata !== void 0) runStart.metadata = options.metadata;
|
|
1108
|
+
yield clientEvent(state, runStart);
|
|
1095
1109
|
if (options.model !== void 0) {
|
|
1096
1110
|
yield clientEvent(state, { type: "generation_start", model: options.model });
|
|
1097
1111
|
}
|
|
@@ -1125,21 +1139,27 @@ function completionToClientStream(options) {
|
|
|
1125
1139
|
case "final": {
|
|
1126
1140
|
const result = event.result;
|
|
1127
1141
|
if (result.messageId !== void 0) state.modelMessageId = result.messageId;
|
|
1128
|
-
|
|
1129
|
-
usage: result.usage
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
}
|
|
1142
|
+
const finalMetadata = {
|
|
1143
|
+
usage: result.usage
|
|
1144
|
+
};
|
|
1145
|
+
if (result.contextUsage !== void 0) {
|
|
1146
|
+
finalMetadata.contextUsage = result.contextUsage;
|
|
1147
|
+
}
|
|
1148
|
+
if (result.sources !== void 0) finalMetadata.sources = result.sources;
|
|
1149
|
+
if (result.providerToolCalls !== void 0) {
|
|
1150
|
+
finalMetadata.providerToolCalls = result.providerToolCalls;
|
|
1151
|
+
}
|
|
1152
|
+
yield* finishMessage(state, result.content, finalMetadata);
|
|
1134
1153
|
const output = clientOutput(result.output, options.mapOutput);
|
|
1135
|
-
|
|
1154
|
+
const runEnd = {
|
|
1136
1155
|
type: "run_end",
|
|
1137
1156
|
status: "completed",
|
|
1138
1157
|
text: result.text,
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1158
|
+
usage: result.usage
|
|
1159
|
+
};
|
|
1160
|
+
if (output !== void 0) runEnd.output = output;
|
|
1161
|
+
if (result.contextUsage !== void 0) runEnd.contextUsage = result.contextUsage;
|
|
1162
|
+
yield clientEvent(state, runEnd);
|
|
1143
1163
|
terminal = true;
|
|
1144
1164
|
break;
|
|
1145
1165
|
}
|
|
@@ -1174,11 +1194,12 @@ function agentToClientStream(options) {
|
|
|
1174
1194
|
messages: /* @__PURE__ */ new Map(),
|
|
1175
1195
|
startedScopes: /* @__PURE__ */ new Set(["root"])
|
|
1176
1196
|
};
|
|
1177
|
-
|
|
1197
|
+
const runStart = {
|
|
1178
1198
|
type: "run_start",
|
|
1179
|
-
source: "agent"
|
|
1180
|
-
|
|
1181
|
-
|
|
1199
|
+
source: "agent"
|
|
1200
|
+
};
|
|
1201
|
+
if (options.metadata !== void 0) runStart.metadata = options.metadata;
|
|
1202
|
+
yield rootEvent(runId, runStart);
|
|
1182
1203
|
let terminal = false;
|
|
1183
1204
|
try {
|
|
1184
1205
|
for await (const event of source) {
|
|
@@ -1215,12 +1236,12 @@ async function* translateAgentEvent(runtime, event, scope) {
|
|
|
1215
1236
|
if (event.type === "agent_tool_event") {
|
|
1216
1237
|
const childScope = {
|
|
1217
1238
|
agentId: event.agentId,
|
|
1218
|
-
...event.agentName === void 0 ? {} : { agentName: event.agentName },
|
|
1219
1239
|
parentRunId: runtime.runId,
|
|
1220
1240
|
parentToolName: event.toolName,
|
|
1221
1241
|
parentToolCallId: event.toolCallId ?? event.internalCallId,
|
|
1222
1242
|
parentInternalToolCallId: event.internalCallId
|
|
1223
1243
|
};
|
|
1244
|
+
if (event.agentName !== void 0) childScope.agentName = event.agentName;
|
|
1224
1245
|
const key = scopeKey(childScope);
|
|
1225
1246
|
if (!runtime.startedScopes.has(key)) {
|
|
1226
1247
|
runtime.startedScopes.add(key);
|
|
@@ -1302,35 +1323,44 @@ async function* translateAgentEvent(runtime, event, scope) {
|
|
|
1302
1323
|
event.internalCallId
|
|
1303
1324
|
);
|
|
1304
1325
|
tool.result = clientToolResult(event.output, event.result);
|
|
1305
|
-
|
|
1326
|
+
const resultEvent = {
|
|
1306
1327
|
type: "tool_result",
|
|
1307
1328
|
messageId: state.messageId,
|
|
1308
1329
|
partId: tool.partId,
|
|
1309
1330
|
toolCallId: tool.toolCallId,
|
|
1310
|
-
...tool.callId === void 0 ? {} : { callId: tool.callId },
|
|
1311
1331
|
internalCallId: event.internalCallId,
|
|
1312
1332
|
toolName: event.toolName,
|
|
1313
1333
|
input: parseJsonOrString(event.args),
|
|
1314
1334
|
result: tool.result
|
|
1315
|
-
}
|
|
1335
|
+
};
|
|
1336
|
+
if (tool.callId !== void 0) resultEvent.callId = tool.callId;
|
|
1337
|
+
yield clientEvent(state, resultEvent);
|
|
1316
1338
|
return;
|
|
1317
1339
|
}
|
|
1318
1340
|
case "turn_end": {
|
|
1319
1341
|
const state = getAgentMessage(runtime, event.turn, scope);
|
|
1320
1342
|
yield* startMessage(state);
|
|
1321
1343
|
if (event.response.messageId !== void 0) state.modelMessageId = event.response.messageId;
|
|
1322
|
-
|
|
1323
|
-
usage: event.response.usage
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
}
|
|
1328
|
-
|
|
1344
|
+
const finalMetadata = {
|
|
1345
|
+
usage: event.response.usage
|
|
1346
|
+
};
|
|
1347
|
+
if (event.response.contextUsage !== void 0) {
|
|
1348
|
+
finalMetadata.contextUsage = event.response.contextUsage;
|
|
1349
|
+
}
|
|
1350
|
+
if (event.response.sources !== void 0) finalMetadata.sources = event.response.sources;
|
|
1351
|
+
if (event.response.providerToolCalls !== void 0) {
|
|
1352
|
+
finalMetadata.providerToolCalls = event.response.providerToolCalls;
|
|
1353
|
+
}
|
|
1354
|
+
yield* finishMessage(state, event.response.choice, finalMetadata);
|
|
1355
|
+
const turnEnd = {
|
|
1329
1356
|
type: "turn_end",
|
|
1330
|
-
usage: event.response.usage
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1357
|
+
usage: event.response.usage
|
|
1358
|
+
};
|
|
1359
|
+
if (event.response.contextUsage !== void 0) {
|
|
1360
|
+
turnEnd.contextUsage = event.response.contextUsage;
|
|
1361
|
+
}
|
|
1362
|
+
if (event.firstDeltaMs !== void 0) turnEnd.firstDeltaMs = event.firstDeltaMs;
|
|
1363
|
+
yield scopedEvent(runtime.runId, event.turn, scope, turnEnd);
|
|
1334
1364
|
return;
|
|
1335
1365
|
}
|
|
1336
1366
|
case "guardrail_decision":
|
|
@@ -1339,43 +1369,29 @@ async function* translateAgentEvent(runtime, event, scope) {
|
|
|
1339
1369
|
decision: event.decision
|
|
1340
1370
|
});
|
|
1341
1371
|
return;
|
|
1342
|
-
case "approval_required": {
|
|
1343
|
-
const input = jsonValueOrUndefined(event.approval.input);
|
|
1344
|
-
yield scopedEvent(runtime.runId, turn, scope, {
|
|
1345
|
-
type: "tool_approval",
|
|
1346
|
-
approval: {
|
|
1347
|
-
id: event.approval.id,
|
|
1348
|
-
runId: event.runId,
|
|
1349
|
-
toolName: event.approval.toolName,
|
|
1350
|
-
...event.approval.toolCallId === void 0 ? {} : { callId: event.approval.toolCallId },
|
|
1351
|
-
...input === void 0 ? {} : { input },
|
|
1352
|
-
status: "pending",
|
|
1353
|
-
...event.approval.reason === void 0 ? {} : { reason: event.approval.reason }
|
|
1354
|
-
}
|
|
1355
|
-
});
|
|
1356
|
-
yield scopedEvent(runtime.runId, turn, scope, {
|
|
1357
|
-
type: "run_end",
|
|
1358
|
-
status: "approval_required",
|
|
1359
|
-
usage: event.usage,
|
|
1360
|
-
...event.memoryCompaction === void 0 ? {} : { memoryCompaction: event.memoryCompaction },
|
|
1361
|
-
...runtime.options.metadata === void 0 ? {} : { metadata: runtime.options.metadata }
|
|
1362
|
-
});
|
|
1363
|
-
return;
|
|
1364
|
-
}
|
|
1365
1372
|
case "final": {
|
|
1366
1373
|
const result = event.result;
|
|
1374
|
+
if (result.status === "suspended" && scope === void 0) {
|
|
1375
|
+
yield scopedEvent(runtime.runId, turn, scope, {
|
|
1376
|
+
type: "interaction",
|
|
1377
|
+
interaction: result.interaction
|
|
1378
|
+
});
|
|
1379
|
+
}
|
|
1367
1380
|
const output = result.status === "completed" ? clientOutput(result.output, runtime.options.mapOutput) : void 0;
|
|
1368
|
-
|
|
1381
|
+
const runEnd = {
|
|
1369
1382
|
type: "run_end",
|
|
1370
1383
|
status: result.status,
|
|
1371
1384
|
text: result.text,
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1385
|
+
usage: result.usage
|
|
1386
|
+
};
|
|
1387
|
+
if (output !== void 0) runEnd.output = output;
|
|
1388
|
+
if (result.contextUsage !== void 0) runEnd.contextUsage = result.contextUsage;
|
|
1389
|
+
if (result.trace !== void 0) runEnd.trace = clientTrace(result.trace);
|
|
1390
|
+
if (result.memoryCompaction !== void 0) {
|
|
1391
|
+
runEnd.memoryCompaction = result.memoryCompaction;
|
|
1392
|
+
}
|
|
1393
|
+
if (runtime.options.metadata !== void 0) runEnd.metadata = runtime.options.metadata;
|
|
1394
|
+
yield scopedEvent(runtime.runId, turn, scope, runEnd);
|
|
1379
1395
|
return;
|
|
1380
1396
|
}
|
|
1381
1397
|
case "error": {
|
|
@@ -1389,11 +1405,12 @@ async function* translateAgentEvent(runtime, event, scope) {
|
|
|
1389
1405
|
return;
|
|
1390
1406
|
}
|
|
1391
1407
|
default: {
|
|
1392
|
-
const
|
|
1393
|
-
runId: runtime.runId
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1408
|
+
const context = {
|
|
1409
|
+
runId: runtime.runId
|
|
1410
|
+
};
|
|
1411
|
+
if (turn !== void 0) context.turn = turn;
|
|
1412
|
+
if (scope !== void 0) context.scope = scope;
|
|
1413
|
+
const mapped = runtime.options.mapCustomEvent?.(event, context);
|
|
1397
1414
|
if (mapped === void 0) return;
|
|
1398
1415
|
if (Array.isArray(mapped)) {
|
|
1399
1416
|
yield* mapped;
|
|
@@ -1404,11 +1421,9 @@ async function* translateAgentEvent(runtime, event, scope) {
|
|
|
1404
1421
|
}
|
|
1405
1422
|
}
|
|
1406
1423
|
function createMessageState(input) {
|
|
1407
|
-
|
|
1424
|
+
const state = {
|
|
1408
1425
|
runId: input.runId,
|
|
1409
1426
|
messageId: createClientId("msg"),
|
|
1410
|
-
...input.turn === void 0 ? {} : { turn: input.turn },
|
|
1411
|
-
...input.scope === void 0 ? {} : { scope: input.scope },
|
|
1412
1427
|
started: false,
|
|
1413
1428
|
ended: false,
|
|
1414
1429
|
textPartId: createClientId("text"),
|
|
@@ -1420,16 +1435,20 @@ function createMessageState(input) {
|
|
|
1420
1435
|
sourceFingerprints: /* @__PURE__ */ new Set(),
|
|
1421
1436
|
providerToolCallFingerprints: /* @__PURE__ */ new Set()
|
|
1422
1437
|
};
|
|
1438
|
+
if (input.turn !== void 0) state.turn = input.turn;
|
|
1439
|
+
if (input.scope !== void 0) state.scope = input.scope;
|
|
1440
|
+
return state;
|
|
1423
1441
|
}
|
|
1424
1442
|
function getAgentMessage(runtime, turn, scope) {
|
|
1425
1443
|
const key = `${scopeKey(scope)}:${turn}`;
|
|
1426
1444
|
const current = runtime.messages.get(key);
|
|
1427
1445
|
if (current !== void 0) return current;
|
|
1428
|
-
const
|
|
1446
|
+
const input = {
|
|
1429
1447
|
runId: runtime.runId,
|
|
1430
|
-
turn
|
|
1431
|
-
|
|
1432
|
-
|
|
1448
|
+
turn
|
|
1449
|
+
};
|
|
1450
|
+
if (scope !== void 0) input.scope = scope;
|
|
1451
|
+
const created = createMessageState(input);
|
|
1433
1452
|
runtime.messages.set(key, created);
|
|
1434
1453
|
return created;
|
|
1435
1454
|
}
|
|
@@ -1482,40 +1501,43 @@ async function* appendReasoning(state, event) {
|
|
|
1482
1501
|
if (reasoning === void 0) {
|
|
1483
1502
|
reasoning = {
|
|
1484
1503
|
partId: createClientId("reasoning"),
|
|
1485
|
-
...event.id === void 0 ? {} : { reasoningId: event.id },
|
|
1486
1504
|
text: "",
|
|
1487
1505
|
ended: false
|
|
1488
1506
|
};
|
|
1507
|
+
if (event.id !== void 0) reasoning.reasoningId = event.id;
|
|
1489
1508
|
state.reasoning.set(key, reasoning);
|
|
1490
|
-
|
|
1509
|
+
const reasoningStart = {
|
|
1491
1510
|
type: "reasoning_start",
|
|
1492
1511
|
messageId: state.messageId,
|
|
1493
|
-
partId: reasoning.partId
|
|
1494
|
-
|
|
1495
|
-
|
|
1512
|
+
partId: reasoning.partId
|
|
1513
|
+
};
|
|
1514
|
+
if (event.id !== void 0) reasoningStart.reasoningId = event.id;
|
|
1515
|
+
yield clientEvent(state, reasoningStart);
|
|
1496
1516
|
}
|
|
1497
1517
|
reasoning.text += event.delta;
|
|
1498
|
-
|
|
1518
|
+
const reasoningDelta = {
|
|
1499
1519
|
type: "reasoning_delta",
|
|
1500
1520
|
messageId: state.messageId,
|
|
1501
1521
|
partId: reasoning.partId,
|
|
1502
|
-
delta: event.delta
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1522
|
+
delta: event.delta
|
|
1523
|
+
};
|
|
1524
|
+
if (event.contentType !== void 0) reasoningDelta.contentType = event.contentType;
|
|
1525
|
+
if (event.signature !== void 0) reasoningDelta.signature = event.signature;
|
|
1526
|
+
yield clientEvent(state, reasoningDelta);
|
|
1506
1527
|
}
|
|
1507
1528
|
async function* appendToolCall(state, event) {
|
|
1508
1529
|
const tool = getToolState(state, event.id, event.name);
|
|
1509
1530
|
if (!tool.started) {
|
|
1510
1531
|
tool.started = true;
|
|
1511
|
-
|
|
1532
|
+
const toolCallStart = {
|
|
1512
1533
|
type: "tool_call_start",
|
|
1513
1534
|
messageId: state.messageId,
|
|
1514
1535
|
partId: tool.partId,
|
|
1515
|
-
toolCallId: event.id
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1536
|
+
toolCallId: event.id
|
|
1537
|
+
};
|
|
1538
|
+
if (event.callId !== void 0) toolCallStart.callId = event.callId;
|
|
1539
|
+
if (event.name !== void 0) toolCallStart.toolName = event.name;
|
|
1540
|
+
yield clientEvent(state, toolCallStart);
|
|
1519
1541
|
}
|
|
1520
1542
|
if (event.callId !== void 0) tool.callId = event.callId;
|
|
1521
1543
|
if (event.name !== void 0) tool.toolName = event.name;
|
|
@@ -1524,31 +1546,33 @@ async function* appendToolCall(state, event) {
|
|
|
1524
1546
|
const mode = event.argumentsMode ?? "append";
|
|
1525
1547
|
const prior = typeof tool.input === "string" ? tool.input : "";
|
|
1526
1548
|
tool.input = mode === "replace" ? event.argumentsDelta : `${prior}${event.argumentsDelta}`;
|
|
1527
|
-
|
|
1549
|
+
const toolCallDelta = {
|
|
1528
1550
|
type: "tool_call_delta",
|
|
1529
1551
|
messageId: state.messageId,
|
|
1530
1552
|
partId: tool.partId,
|
|
1531
1553
|
toolCallId: event.id,
|
|
1532
|
-
...event.callId === void 0 ? {} : { callId: event.callId },
|
|
1533
|
-
...event.name === void 0 ? {} : { toolName: event.name },
|
|
1534
1554
|
delta: event.argumentsDelta,
|
|
1535
|
-
mode
|
|
1536
|
-
|
|
1537
|
-
|
|
1555
|
+
mode
|
|
1556
|
+
};
|
|
1557
|
+
if (event.callId !== void 0) toolCallDelta.callId = event.callId;
|
|
1558
|
+
if (event.name !== void 0) toolCallDelta.toolName = event.name;
|
|
1559
|
+
if (event.signature !== void 0) toolCallDelta.signature = event.signature;
|
|
1560
|
+
yield clientEvent(state, toolCallDelta);
|
|
1538
1561
|
}
|
|
1539
1562
|
async function* finishToolCall(state, toolCall) {
|
|
1540
1563
|
const tool = getToolState(state, toolCall.toolCallId, toolCall.toolName);
|
|
1541
1564
|
const wasEnded = tool.ended;
|
|
1542
1565
|
if (!tool.started) {
|
|
1543
1566
|
tool.started = true;
|
|
1544
|
-
|
|
1567
|
+
const toolCallStart = {
|
|
1545
1568
|
type: "tool_call_start",
|
|
1546
1569
|
messageId: state.messageId,
|
|
1547
1570
|
partId: tool.partId,
|
|
1548
1571
|
toolCallId: toolCall.toolCallId,
|
|
1549
|
-
...toolCall.callId === void 0 ? {} : { callId: toolCall.callId },
|
|
1550
1572
|
toolName: toolCall.toolName
|
|
1551
|
-
}
|
|
1573
|
+
};
|
|
1574
|
+
if (toolCall.callId !== void 0) toolCallStart.callId = toolCall.callId;
|
|
1575
|
+
yield clientEvent(state, toolCallStart);
|
|
1552
1576
|
}
|
|
1553
1577
|
if (toolCall.callId === void 0) delete tool.callId;
|
|
1554
1578
|
else tool.callId = toolCall.callId;
|
|
@@ -1558,16 +1582,17 @@ async function* finishToolCall(state, toolCall) {
|
|
|
1558
1582
|
else tool.signature = toolCall.signature;
|
|
1559
1583
|
if (wasEnded) return;
|
|
1560
1584
|
tool.ended = true;
|
|
1561
|
-
|
|
1585
|
+
const toolCallEnd = {
|
|
1562
1586
|
type: "tool_call_end",
|
|
1563
1587
|
messageId: state.messageId,
|
|
1564
1588
|
partId: tool.partId,
|
|
1565
1589
|
toolCallId: toolCall.toolCallId,
|
|
1566
|
-
...toolCall.callId === void 0 ? {} : { callId: toolCall.callId },
|
|
1567
1590
|
toolName: toolCall.toolName,
|
|
1568
|
-
input: toolCall.input
|
|
1569
|
-
|
|
1570
|
-
|
|
1591
|
+
input: toolCall.input
|
|
1592
|
+
};
|
|
1593
|
+
if (toolCall.callId !== void 0) toolCallEnd.callId = toolCall.callId;
|
|
1594
|
+
if (toolCall.signature !== void 0) toolCallEnd.signature = toolCall.signature;
|
|
1595
|
+
yield clientEvent(state, toolCallEnd);
|
|
1571
1596
|
}
|
|
1572
1597
|
async function* finishMessage(state, content, metadata) {
|
|
1573
1598
|
if (state.ended) return;
|
|
@@ -1586,13 +1611,16 @@ async function* finishMessage(state, content, metadata) {
|
|
|
1586
1611
|
}
|
|
1587
1612
|
state.text = text;
|
|
1588
1613
|
state.textEnded = true;
|
|
1589
|
-
|
|
1614
|
+
const textEnd = {
|
|
1590
1615
|
type: "text_end",
|
|
1591
1616
|
messageId: state.messageId,
|
|
1592
1617
|
partId: state.textPartId,
|
|
1593
|
-
text
|
|
1594
|
-
|
|
1595
|
-
|
|
1618
|
+
text
|
|
1619
|
+
};
|
|
1620
|
+
if (textContent2.length === 1 && textContent2[0]?.signature !== void 0) {
|
|
1621
|
+
textEnd.signature = textContent2[0].signature;
|
|
1622
|
+
}
|
|
1623
|
+
yield clientEvent(state, textEnd);
|
|
1596
1624
|
}
|
|
1597
1625
|
const finalReasoning = content.filter(
|
|
1598
1626
|
(part) => part.type === "reasoning"
|
|
@@ -1606,23 +1634,25 @@ async function* finishMessage(state, content, metadata) {
|
|
|
1606
1634
|
const current = existing ?? createReasoningState(reasoning.id);
|
|
1607
1635
|
if (existing === void 0) {
|
|
1608
1636
|
state.reasoning.set(reasoning.id ?? `final:${index}`, current);
|
|
1609
|
-
|
|
1637
|
+
const reasoningStart = {
|
|
1610
1638
|
type: "reasoning_start",
|
|
1611
1639
|
messageId: state.messageId,
|
|
1612
|
-
partId: current.partId
|
|
1613
|
-
|
|
1614
|
-
|
|
1640
|
+
partId: current.partId
|
|
1641
|
+
};
|
|
1642
|
+
if (reasoning.id !== void 0) reasoningStart.reasoningId = reasoning.id;
|
|
1643
|
+
yield clientEvent(state, reasoningStart);
|
|
1615
1644
|
}
|
|
1616
1645
|
if (!current.ended) {
|
|
1617
1646
|
current.ended = true;
|
|
1618
1647
|
current.text = reasoning.text;
|
|
1619
|
-
|
|
1648
|
+
const reasoningEnd = {
|
|
1620
1649
|
type: "reasoning_end",
|
|
1621
1650
|
messageId: state.messageId,
|
|
1622
1651
|
partId: current.partId,
|
|
1623
|
-
text: reasoning.text
|
|
1624
|
-
|
|
1625
|
-
|
|
1652
|
+
text: reasoning.text
|
|
1653
|
+
};
|
|
1654
|
+
if (reasoning.details !== void 0) reasoningEnd.content = reasoning.details;
|
|
1655
|
+
yield clientEvent(state, reasoningEnd);
|
|
1626
1656
|
}
|
|
1627
1657
|
}
|
|
1628
1658
|
for (const reasoning of state.reasoning.values()) {
|
|
@@ -1652,14 +1682,15 @@ async function* finishMessage(state, content, metadata) {
|
|
|
1652
1682
|
yield* emitProviderToolCall(state, toolCall);
|
|
1653
1683
|
}
|
|
1654
1684
|
state.ended = true;
|
|
1655
|
-
|
|
1685
|
+
const messageEnd = {
|
|
1656
1686
|
type: "message_end",
|
|
1657
1687
|
messageId: state.messageId,
|
|
1658
|
-
...state.modelMessageId === void 0 ? {} : { modelMessageId: state.modelMessageId },
|
|
1659
1688
|
parts: contentToUIMessageParts(content, state),
|
|
1660
|
-
usage: metadata.usage
|
|
1661
|
-
|
|
1662
|
-
|
|
1689
|
+
usage: metadata.usage
|
|
1690
|
+
};
|
|
1691
|
+
if (state.modelMessageId !== void 0) messageEnd.modelMessageId = state.modelMessageId;
|
|
1692
|
+
if (metadata.contextUsage !== void 0) messageEnd.contextUsage = metadata.contextUsage;
|
|
1693
|
+
yield clientEvent(state, messageEnd);
|
|
1663
1694
|
}
|
|
1664
1695
|
async function* finishErroredRun(state, error, usage, mapper) {
|
|
1665
1696
|
const mapped = mapClientError(error, mapper);
|
|
@@ -1672,25 +1703,28 @@ async function* finishErroredRun(state, error, usage, mapper) {
|
|
|
1672
1703
|
text: state.text
|
|
1673
1704
|
});
|
|
1674
1705
|
}
|
|
1675
|
-
|
|
1706
|
+
const errorEvent = {
|
|
1676
1707
|
type: "error",
|
|
1677
|
-
error: mapped
|
|
1678
|
-
|
|
1679
|
-
|
|
1708
|
+
error: mapped
|
|
1709
|
+
};
|
|
1710
|
+
if (usage !== void 0) errorEvent.usage = usage;
|
|
1711
|
+
yield clientEvent(state, errorEvent);
|
|
1680
1712
|
if (!state.ended) {
|
|
1681
1713
|
state.ended = true;
|
|
1682
|
-
|
|
1714
|
+
const messageEnd = {
|
|
1683
1715
|
type: "message_end",
|
|
1684
|
-
messageId: state.messageId
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1716
|
+
messageId: state.messageId
|
|
1717
|
+
};
|
|
1718
|
+
if (state.modelMessageId !== void 0) messageEnd.modelMessageId = state.modelMessageId;
|
|
1719
|
+
if (usage !== void 0) messageEnd.usage = usage;
|
|
1720
|
+
yield clientEvent(state, messageEnd);
|
|
1688
1721
|
}
|
|
1689
|
-
|
|
1722
|
+
const runEnd = {
|
|
1690
1723
|
type: "run_end",
|
|
1691
|
-
status: "error"
|
|
1692
|
-
|
|
1693
|
-
|
|
1724
|
+
status: "error"
|
|
1725
|
+
};
|
|
1726
|
+
if (usage !== void 0) runEnd.usage = usage;
|
|
1727
|
+
yield clientEvent(state, runEnd);
|
|
1694
1728
|
}
|
|
1695
1729
|
function contentToUIMessageParts(content, state) {
|
|
1696
1730
|
let textIndex = 0;
|
|
@@ -1756,11 +1790,11 @@ function getToolState(state, id, name) {
|
|
|
1756
1790
|
tool = {
|
|
1757
1791
|
partId: createClientId("tool"),
|
|
1758
1792
|
toolCallId: id,
|
|
1759
|
-
...name === void 0 ? {} : { toolName: name },
|
|
1760
1793
|
input: "",
|
|
1761
1794
|
started: false,
|
|
1762
1795
|
ended: false
|
|
1763
1796
|
};
|
|
1797
|
+
if (name !== void 0) tool.toolName = name;
|
|
1764
1798
|
state.tools.set(id, tool);
|
|
1765
1799
|
} else if (name !== void 0) {
|
|
1766
1800
|
tool.toolName = name;
|
|
@@ -1777,12 +1811,13 @@ function getToolResultState(state, toolName, toolCallId, callId, internalCallId)
|
|
|
1777
1811
|
return tool;
|
|
1778
1812
|
}
|
|
1779
1813
|
function createReasoningState(id) {
|
|
1780
|
-
|
|
1814
|
+
const state = {
|
|
1781
1815
|
partId: createClientId("reasoning"),
|
|
1782
|
-
...id === void 0 ? {} : { reasoningId: id },
|
|
1783
1816
|
text: "",
|
|
1784
1817
|
ended: false
|
|
1785
1818
|
};
|
|
1819
|
+
if (id !== void 0) state.reasoningId = id;
|
|
1820
|
+
return state;
|
|
1786
1821
|
}
|
|
1787
1822
|
function contentAttachment(content) {
|
|
1788
1823
|
const attachment = {
|
|
@@ -1863,15 +1898,13 @@ function valueFingerprint(value) {
|
|
|
1863
1898
|
}
|
|
1864
1899
|
return `${typeof value}:${JSON.stringify(value)}`;
|
|
1865
1900
|
}
|
|
1866
|
-
function jsonValueOrUndefined(value) {
|
|
1867
|
-
return isJsonValue3(value) ? value : void 0;
|
|
1868
|
-
}
|
|
1869
1901
|
function clientTrace(trace) {
|
|
1870
|
-
|
|
1871
|
-
observer: trace.observer
|
|
1872
|
-
...trace.traceId === void 0 ? {} : { traceId: trace.traceId },
|
|
1873
|
-
...trace.observationId === void 0 ? {} : { observationId: trace.observationId }
|
|
1902
|
+
const result = {
|
|
1903
|
+
observer: trace.observer
|
|
1874
1904
|
};
|
|
1905
|
+
if (trace.traceId !== void 0) result.traceId = trace.traceId;
|
|
1906
|
+
if (trace.observationId !== void 0) result.observationId = trace.observationId;
|
|
1907
|
+
return result;
|
|
1875
1908
|
}
|
|
1876
1909
|
function mapClientError(error, mapper) {
|
|
1877
1910
|
const mapped = mapper?.(error) ?? maskedClientError();
|
|
@@ -1887,12 +1920,10 @@ function rootEvent(runId, event, scope) {
|
|
|
1887
1920
|
return scopedEvent(runId, void 0, scope, event);
|
|
1888
1921
|
}
|
|
1889
1922
|
function scopedEvent(runId, turn, scope, event) {
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
...event
|
|
1895
|
-
};
|
|
1923
|
+
let scoped = { runId, ...event };
|
|
1924
|
+
if (turn !== void 0) scoped = { ...scoped, turn };
|
|
1925
|
+
if (scope !== void 0) scoped = { ...scoped, scope };
|
|
1926
|
+
return scoped;
|
|
1896
1927
|
}
|
|
1897
1928
|
function scopeKey(scope) {
|
|
1898
1929
|
if (scope === void 0) return "root";
|
|
@@ -1905,7 +1936,7 @@ function scopeKey(scope) {
|
|
|
1905
1936
|
].join(":");
|
|
1906
1937
|
}
|
|
1907
1938
|
function isRootTerminalAgentEvent(event) {
|
|
1908
|
-
return event.type === "final" || event.type === "
|
|
1939
|
+
return event.type === "final" || event.type === "error";
|
|
1909
1940
|
}
|
|
1910
1941
|
function propagateCancellation(source, transform) {
|
|
1911
1942
|
return {
|
|
@@ -1958,6 +1989,15 @@ function createDirectClientTransport(options) {
|
|
|
1958
1989
|
if (resume !== void 0) {
|
|
1959
1990
|
throw new ClientProtocolError("Direct client transports do not support resume cursors.");
|
|
1960
1991
|
}
|
|
1992
|
+
const handlerOptions = {
|
|
1993
|
+
request: sendOptions.request
|
|
1994
|
+
};
|
|
1995
|
+
if (sendOptions.abortSignal !== void 0) {
|
|
1996
|
+
handlerOptions.abortSignal = sendOptions.abortSignal;
|
|
1997
|
+
}
|
|
1998
|
+
if (sendOptions.headers !== void 0) handlerOptions.headers = sendOptions.headers;
|
|
1999
|
+
const events = await options.handler(handlerOptions);
|
|
2000
|
+
const iterator = events[Symbol.asyncIterator]();
|
|
1961
2001
|
const streamId = createClientId("stream");
|
|
1962
2002
|
let eventId = 0;
|
|
1963
2003
|
yield {
|
|
@@ -1969,12 +2009,6 @@ function createDirectClientTransport(options) {
|
|
|
1969
2009
|
};
|
|
1970
2010
|
let status = "completed";
|
|
1971
2011
|
try {
|
|
1972
|
-
const events = await options.handler({
|
|
1973
|
-
request: sendOptions.request,
|
|
1974
|
-
...sendOptions.abortSignal === void 0 ? {} : { abortSignal: sendOptions.abortSignal },
|
|
1975
|
-
...sendOptions.headers === void 0 ? {} : { headers: sendOptions.headers }
|
|
1976
|
-
});
|
|
1977
|
-
const iterator = events[Symbol.asyncIterator]();
|
|
1978
2012
|
let closePromise;
|
|
1979
2013
|
const close = () => {
|
|
1980
2014
|
closePromise ??= Promise.resolve(iterator.return?.());
|
|
@@ -2046,10 +2080,10 @@ async function* validateFrameSequence(frames, resume) {
|
|
|
2046
2080
|
}
|
|
2047
2081
|
}
|
|
2048
2082
|
function protocolOptions(options) {
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2083
|
+
const result = {};
|
|
2084
|
+
if (options.metadataSchema !== void 0) result.metadataSchema = options.metadataSchema;
|
|
2085
|
+
if (options.dataSchemas !== void 0) result.dataSchemas = options.dataSchemas;
|
|
2086
|
+
return result;
|
|
2053
2087
|
}
|
|
2054
2088
|
function resumeCursorFromRequest(value) {
|
|
2055
2089
|
if (typeof value !== "object" || value === null || !("resume" in value)) return void 0;
|
|
@@ -2066,16 +2100,16 @@ function applyClientStreamEvent(messages, event) {
|
|
|
2066
2100
|
return messages;
|
|
2067
2101
|
}
|
|
2068
2102
|
switch (event.type) {
|
|
2069
|
-
case "message_start":
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2103
|
+
case "message_start": {
|
|
2104
|
+
if (messages.some((message2) => message2.id === event.messageId)) return messages;
|
|
2105
|
+
let message = {
|
|
2106
|
+
id: event.messageId,
|
|
2107
|
+
role: event.role,
|
|
2108
|
+
parts: []
|
|
2109
|
+
};
|
|
2110
|
+
if (event.metadata !== void 0) message = { ...message, metadata: event.metadata };
|
|
2111
|
+
return [...messages, message];
|
|
2112
|
+
}
|
|
2079
2113
|
case "text_start":
|
|
2080
2114
|
return upsertPart(messages, event.messageId, {
|
|
2081
2115
|
id: event.partId,
|
|
@@ -2098,21 +2132,25 @@ function applyClientStreamEvent(messages, event) {
|
|
|
2098
2132
|
if (event.signature !== void 0) next.signature = event.signature;
|
|
2099
2133
|
return next;
|
|
2100
2134
|
});
|
|
2101
|
-
case "reasoning_start":
|
|
2102
|
-
|
|
2135
|
+
case "reasoning_start": {
|
|
2136
|
+
const reasoning = {
|
|
2103
2137
|
id: event.partId,
|
|
2104
2138
|
type: "reasoning",
|
|
2105
|
-
text: ""
|
|
2106
|
-
|
|
2107
|
-
|
|
2139
|
+
text: ""
|
|
2140
|
+
};
|
|
2141
|
+
if (event.reasoningId !== void 0) reasoning.reasoningId = event.reasoningId;
|
|
2142
|
+
return upsertPart(messages, event.messageId, reasoning);
|
|
2143
|
+
}
|
|
2108
2144
|
case "reasoning_delta":
|
|
2109
2145
|
return updatePart(messages, event.messageId, event.partId, (part) => {
|
|
2110
2146
|
const reasoning = {
|
|
2111
2147
|
id: event.partId,
|
|
2112
2148
|
type: "reasoning",
|
|
2113
|
-
text: part?.type === "reasoning" ? `${part.text}${event.delta}` : event.delta
|
|
2114
|
-
...part?.type === "reasoning" && part.reasoningId !== void 0 ? { reasoningId: part.reasoningId } : {}
|
|
2149
|
+
text: part?.type === "reasoning" ? `${part.text}${event.delta}` : event.delta
|
|
2115
2150
|
};
|
|
2151
|
+
if (part?.type === "reasoning" && part.reasoningId !== void 0) {
|
|
2152
|
+
reasoning.reasoningId = part.reasoningId;
|
|
2153
|
+
}
|
|
2116
2154
|
const content = appendReasoningContent(
|
|
2117
2155
|
part?.type === "reasoning" ? part.content : void 0,
|
|
2118
2156
|
event
|
|
@@ -2121,13 +2159,22 @@ function applyClientStreamEvent(messages, event) {
|
|
|
2121
2159
|
return reasoning;
|
|
2122
2160
|
});
|
|
2123
2161
|
case "reasoning_end":
|
|
2124
|
-
return updatePart(messages, event.messageId, event.partId, (part) =>
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2162
|
+
return updatePart(messages, event.messageId, event.partId, (part) => {
|
|
2163
|
+
const reasoning = {
|
|
2164
|
+
id: event.partId,
|
|
2165
|
+
type: "reasoning",
|
|
2166
|
+
text: event.text ?? (part?.type === "reasoning" ? part.text : "")
|
|
2167
|
+
};
|
|
2168
|
+
if (part?.type === "reasoning" && part.reasoningId !== void 0) {
|
|
2169
|
+
reasoning.reasoningId = part.reasoningId;
|
|
2170
|
+
}
|
|
2171
|
+
if (event.content !== void 0) {
|
|
2172
|
+
reasoning.content = event.content;
|
|
2173
|
+
} else if (part?.type === "reasoning" && part.content !== void 0) {
|
|
2174
|
+
reasoning.content = part.content;
|
|
2175
|
+
}
|
|
2176
|
+
return reasoning;
|
|
2177
|
+
});
|
|
2131
2178
|
case "tool_call_start": {
|
|
2132
2179
|
const part = {
|
|
2133
2180
|
id: event.partId,
|
|
@@ -2220,28 +2267,40 @@ function applyClientStreamEvent(messages, event) {
|
|
|
2220
2267
|
return messages.map((message) => {
|
|
2221
2268
|
if (message.id !== event.messageId) return message;
|
|
2222
2269
|
const metadata = mergeMetadata(message.metadata, event.metadata);
|
|
2223
|
-
const
|
|
2224
|
-
runId: event.runId
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2270
|
+
const generationUpdate = {
|
|
2271
|
+
runId: event.runId
|
|
2272
|
+
};
|
|
2273
|
+
if (event.usage !== void 0) generationUpdate.usage = event.usage;
|
|
2274
|
+
if (event.contextUsage !== void 0) {
|
|
2275
|
+
generationUpdate.contextUsage = event.contextUsage;
|
|
2276
|
+
}
|
|
2277
|
+
const generation = mergeGeneration(message.generation, generationUpdate);
|
|
2278
|
+
let updated = {
|
|
2229
2279
|
...message,
|
|
2230
|
-
...event.parts === void 0 ? {} : { parts: reconcileFinalParts(message.parts, event.parts) },
|
|
2231
|
-
...event.modelMessageId === void 0 ? {} : { modelMessageId: event.modelMessageId },
|
|
2232
|
-
...metadata === void 0 ? {} : { metadata },
|
|
2233
2280
|
generation
|
|
2234
2281
|
};
|
|
2282
|
+
if (event.parts !== void 0) {
|
|
2283
|
+
updated = { ...updated, parts: reconcileFinalParts(message.parts, event.parts) };
|
|
2284
|
+
}
|
|
2285
|
+
if (event.modelMessageId !== void 0) {
|
|
2286
|
+
updated = { ...updated, modelMessageId: event.modelMessageId };
|
|
2287
|
+
}
|
|
2288
|
+
if (metadata !== void 0) updated = { ...updated, metadata };
|
|
2289
|
+
return updated;
|
|
2235
2290
|
});
|
|
2236
|
-
case "run_end":
|
|
2237
|
-
|
|
2291
|
+
case "run_end": {
|
|
2292
|
+
const generation = {
|
|
2238
2293
|
runId: event.runId,
|
|
2239
|
-
status: event.status
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2294
|
+
status: event.status
|
|
2295
|
+
};
|
|
2296
|
+
if (event.usage !== void 0) generation.usage = event.usage;
|
|
2297
|
+
if (event.contextUsage !== void 0) generation.contextUsage = event.contextUsage;
|
|
2298
|
+
if (event.trace !== void 0) generation.trace = event.trace;
|
|
2299
|
+
if (event.memoryCompaction !== void 0) {
|
|
2300
|
+
generation.memoryCompaction = event.memoryCompaction;
|
|
2301
|
+
}
|
|
2302
|
+
return updateLastAssistantGeneration(messages, generation);
|
|
2303
|
+
}
|
|
2245
2304
|
case "error":
|
|
2246
2305
|
return appendToLastAssistant(messages, {
|
|
2247
2306
|
id: createClientId("error"),
|
|
@@ -2267,11 +2326,12 @@ function appendReasoningContent(current, event) {
|
|
|
2267
2326
|
if (last?.type === "text" && last.signature === event.signature) {
|
|
2268
2327
|
next[next.length - 1] = { ...last, text: `${last.text}${event.delta}` };
|
|
2269
2328
|
} else {
|
|
2270
|
-
|
|
2329
|
+
let detail = {
|
|
2271
2330
|
type,
|
|
2272
|
-
text: event.delta
|
|
2273
|
-
|
|
2274
|
-
}
|
|
2331
|
+
text: event.delta
|
|
2332
|
+
};
|
|
2333
|
+
if (event.signature !== void 0) detail = { ...detail, signature: event.signature };
|
|
2334
|
+
next.push(detail);
|
|
2275
2335
|
}
|
|
2276
2336
|
return next;
|
|
2277
2337
|
}
|