veryfront 0.1.289 → 0.1.291
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/esm/deno.js +1 -1
- package/esm/src/chat/ag-ui.d.ts +30 -0
- package/esm/src/chat/ag-ui.d.ts.map +1 -1
- package/esm/src/chat/ag-ui.js +142 -0
- package/esm/src/chat/final-step-fallback.js +2 -3
- package/esm/src/utils/version-constant.d.ts +1 -1
- package/esm/src/utils/version-constant.js +1 -1
- package/package.json +1 -1
- package/src/deno.js +1 -1
- package/src/src/chat/ag-ui.ts +224 -0
- package/src/src/chat/final-step-fallback.ts +2 -2
- package/src/src/utils/version-constant.ts +1 -1
package/esm/deno.js
CHANGED
package/esm/src/chat/ag-ui.d.ts
CHANGED
|
@@ -1,9 +1,38 @@
|
|
|
1
1
|
import type { ChatStreamEvent } from "./protocol.js";
|
|
2
|
+
import type { ChatUiMessage } from "./types.js";
|
|
2
3
|
import { z } from "zod";
|
|
3
4
|
type ToolCallState = {
|
|
4
5
|
toolName: string;
|
|
5
6
|
argsText: string;
|
|
6
7
|
};
|
|
8
|
+
export type AgUiRuntimeToolCall = {
|
|
9
|
+
id: string;
|
|
10
|
+
type: "function";
|
|
11
|
+
function: {
|
|
12
|
+
name: string;
|
|
13
|
+
arguments: string;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export type AgUiRuntimeMessage = {
|
|
17
|
+
id: string;
|
|
18
|
+
role: "system";
|
|
19
|
+
content: string;
|
|
20
|
+
} | {
|
|
21
|
+
id: string;
|
|
22
|
+
role: "user";
|
|
23
|
+
content: string;
|
|
24
|
+
} | {
|
|
25
|
+
id: string;
|
|
26
|
+
role: "assistant";
|
|
27
|
+
content?: string;
|
|
28
|
+
toolCalls?: AgUiRuntimeToolCall[];
|
|
29
|
+
} | {
|
|
30
|
+
id: string;
|
|
31
|
+
role: "tool";
|
|
32
|
+
toolCallId: string;
|
|
33
|
+
content: string;
|
|
34
|
+
error?: string;
|
|
35
|
+
};
|
|
7
36
|
export type ParsedSseEvent = {
|
|
8
37
|
id: number | null;
|
|
9
38
|
event: string | null;
|
|
@@ -116,6 +145,7 @@ export declare const AgUiWireEventNameSchema: z.ZodEnum<{
|
|
|
116
145
|
ToolCallChunk: "ToolCallChunk";
|
|
117
146
|
StateDelta: "StateDelta";
|
|
118
147
|
}>;
|
|
148
|
+
export declare function mapAgUiRuntimeMessagesToChatUiMessages(messages: AgUiRuntimeMessage[]): ChatUiMessage[];
|
|
119
149
|
export declare const AgUiWireEventSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
120
150
|
eventName: z.ZodLiteral<"RunStarted">;
|
|
121
151
|
payload: z.ZodObject<{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ag-ui.d.ts","sourceRoot":"","sources":["../../../src/src/chat/ag-ui.ts"],"names":[],"mappings":"AAAA,OAAO,yBAAyB,CAAC;AAYjC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AASxB,KAAK,aAAa,GAAG;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC;IAClB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,SAAS,EAAE,aAAa,CAAC;IACzB,UAAU,EAAE,eAAe,EAAE,CAAC;CAC/B,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,MAAM,EAAE,gBAAgB,EAAE,CAAC;IAC3B,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,yBAAy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;AAuPhE,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,cAAc,CA+BzD;AAED,wBAAgB,+BAA+B,CAC7C,KAAK,GAAE;IACL,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,cAAc,CAAC,EAAE,yBAAyB,CAAC;IAC3C,aAAa,CAAC,EAAE,CAAC,OAAO,EAAE;QAAE,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAC;CAChF,GACL,yBAAyB,CAU3B;AAED,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,yBAAyB,EAChC,KAAK,EAAE,MAAM,GACZ,gBAAgB,CAuClB;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,yBAAyB,GAAG,gBAAgB,CAWpF"}
|
package/esm/src/chat/ag-ui.js
CHANGED
|
@@ -87,6 +87,148 @@ export const AgUiWireEventNameSchema = z.enum([
|
|
|
87
87
|
"RunFinished",
|
|
88
88
|
"RunError",
|
|
89
89
|
]);
|
|
90
|
+
function parseRuntimeToolInput(rawArguments) {
|
|
91
|
+
try {
|
|
92
|
+
return JSON.parse(rawArguments);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return { raw: rawArguments };
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function parseRuntimeToolOutput(rawContent) {
|
|
99
|
+
try {
|
|
100
|
+
return JSON.parse(rawContent);
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
return rawContent;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function createRuntimeSystemMessage(message) {
|
|
107
|
+
return {
|
|
108
|
+
id: message.id,
|
|
109
|
+
role: "system",
|
|
110
|
+
parts: [{ type: "text", text: message.content }],
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function createRuntimeUserMessage(message) {
|
|
114
|
+
return {
|
|
115
|
+
id: message.id,
|
|
116
|
+
role: "user",
|
|
117
|
+
parts: [{ type: "text", text: message.content }],
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function createRuntimeAssistantMessage(message) {
|
|
121
|
+
const parts = [];
|
|
122
|
+
if (typeof message.content === "string" && message.content.trim().length > 0) {
|
|
123
|
+
parts.push({ type: "text", text: message.content });
|
|
124
|
+
}
|
|
125
|
+
for (const toolCall of message.toolCalls ?? []) {
|
|
126
|
+
parts.push({
|
|
127
|
+
type: "dynamic-tool",
|
|
128
|
+
toolName: toolCall.function.name,
|
|
129
|
+
toolCallId: toolCall.id,
|
|
130
|
+
input: parseRuntimeToolInput(toolCall.function.arguments),
|
|
131
|
+
state: "input-available",
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
if (parts.length === 0) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
id: message.id,
|
|
139
|
+
role: "assistant",
|
|
140
|
+
parts,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function buildResolvedRuntimeToolPart(input) {
|
|
144
|
+
if (typeof input.error === "string" && input.error.length > 0) {
|
|
145
|
+
return {
|
|
146
|
+
type: "dynamic-tool",
|
|
147
|
+
toolName: input.toolName,
|
|
148
|
+
toolCallId: input.toolCallId,
|
|
149
|
+
...(input.title ? { title: input.title } : {}),
|
|
150
|
+
...(input.providerExecuted !== undefined ? { providerExecuted: input.providerExecuted } : {}),
|
|
151
|
+
input: input.toolInput,
|
|
152
|
+
state: "output-error",
|
|
153
|
+
errorText: input.error,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
return {
|
|
157
|
+
type: "dynamic-tool",
|
|
158
|
+
toolName: input.toolName,
|
|
159
|
+
toolCallId: input.toolCallId,
|
|
160
|
+
...(input.title ? { title: input.title } : {}),
|
|
161
|
+
...(input.providerExecuted !== undefined ? { providerExecuted: input.providerExecuted } : {}),
|
|
162
|
+
input: input.toolInput,
|
|
163
|
+
state: "output-available",
|
|
164
|
+
output: parseRuntimeToolOutput(input.content),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
function applyRuntimeToolResultMessage(messages, message) {
|
|
168
|
+
for (let messageIndex = messages.length - 1; messageIndex >= 0; messageIndex -= 1) {
|
|
169
|
+
const currentMessage = messages[messageIndex];
|
|
170
|
+
if (currentMessage.role !== "assistant") {
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
const partIndex = currentMessage.parts.findIndex((part) => part.type === "dynamic-tool" &&
|
|
174
|
+
part.toolCallId === message.toolCallId &&
|
|
175
|
+
(part.state === "input-available" || part.state === "input-streaming"));
|
|
176
|
+
if (partIndex === -1) {
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const part = currentMessage.parts[partIndex];
|
|
180
|
+
if (part.type !== "dynamic-tool") {
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
currentMessage.parts.splice(partIndex, 1, buildResolvedRuntimeToolPart({
|
|
184
|
+
toolName: part.toolName,
|
|
185
|
+
toolCallId: part.toolCallId,
|
|
186
|
+
...(part.title ? { title: part.title } : {}),
|
|
187
|
+
...(part.providerExecuted !== undefined ? { providerExecuted: part.providerExecuted } : {}),
|
|
188
|
+
toolInput: part.input,
|
|
189
|
+
error: message.error,
|
|
190
|
+
content: message.content,
|
|
191
|
+
}));
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
messages.push({
|
|
195
|
+
id: message.id,
|
|
196
|
+
role: "assistant",
|
|
197
|
+
parts: [
|
|
198
|
+
buildResolvedRuntimeToolPart({
|
|
199
|
+
toolName: "unknown",
|
|
200
|
+
toolCallId: message.toolCallId,
|
|
201
|
+
toolInput: {},
|
|
202
|
+
error: message.error,
|
|
203
|
+
content: message.content,
|
|
204
|
+
}),
|
|
205
|
+
],
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
export function mapAgUiRuntimeMessagesToChatUiMessages(messages) {
|
|
209
|
+
const mappedMessages = [];
|
|
210
|
+
for (const message of messages) {
|
|
211
|
+
switch (message.role) {
|
|
212
|
+
case "system":
|
|
213
|
+
mappedMessages.push(createRuntimeSystemMessage(message));
|
|
214
|
+
break;
|
|
215
|
+
case "user":
|
|
216
|
+
mappedMessages.push(createRuntimeUserMessage(message));
|
|
217
|
+
break;
|
|
218
|
+
case "assistant": {
|
|
219
|
+
const assistantMessage = createRuntimeAssistantMessage(message);
|
|
220
|
+
if (assistantMessage) {
|
|
221
|
+
mappedMessages.push(assistantMessage);
|
|
222
|
+
}
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
case "tool":
|
|
226
|
+
applyRuntimeToolResultMessage(mappedMessages, message);
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return mappedMessages;
|
|
231
|
+
}
|
|
90
232
|
export const AgUiWireEventSchema = z.discriminatedUnion("eventName", [
|
|
91
233
|
z.object({
|
|
92
234
|
eventName: z.literal("RunStarted"),
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import * as dntShim from "../../_dnt.shims.js";
|
|
2
1
|
import { isRecord, isTextPart, isToolCallPart, isToolResultPart } from "./conversation.js";
|
|
3
2
|
import { parseKnownProblemBody, safeJsonParse } from "./provider-errors.js";
|
|
4
3
|
function toToolInput(value) {
|
|
@@ -515,7 +514,7 @@ async function resolveStreamPromiseWithTimeout(promise, timeoutMs, fallback) {
|
|
|
515
514
|
const resolved = await Promise.race([
|
|
516
515
|
Promise.resolve(promise),
|
|
517
516
|
new Promise((resolve) => {
|
|
518
|
-
timeoutId =
|
|
517
|
+
timeoutId = globalThis.setTimeout(() => resolve(STREAM_PROMISE_TIMEOUT_TOKEN), timeoutMs);
|
|
519
518
|
}),
|
|
520
519
|
]);
|
|
521
520
|
if (resolved === STREAM_PROMISE_TIMEOUT_TOKEN) {
|
|
@@ -528,7 +527,7 @@ async function resolveStreamPromiseWithTimeout(promise, timeoutMs, fallback) {
|
|
|
528
527
|
}
|
|
529
528
|
finally {
|
|
530
529
|
if (timeoutId) {
|
|
531
|
-
clearTimeout(timeoutId);
|
|
530
|
+
globalThis.clearTimeout(timeoutId);
|
|
532
531
|
}
|
|
533
532
|
}
|
|
534
533
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const VERSION = "0.1.
|
|
1
|
+
export declare const VERSION = "0.1.291";
|
|
2
2
|
//# sourceMappingURL=version-constant.d.ts.map
|
package/package.json
CHANGED
package/src/deno.js
CHANGED
package/src/src/chat/ag-ui.ts
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
toRenderableCustomChunk,
|
|
12
12
|
} from "./ag-ui-helpers.js";
|
|
13
13
|
import type { ChatStreamEvent } from "./protocol.js";
|
|
14
|
+
import type { ChatUiMessage, ChatUiMessagePart } from "./types.js";
|
|
14
15
|
import { z } from "zod";
|
|
15
16
|
|
|
16
17
|
type JsonPatchOperation = {
|
|
@@ -25,6 +26,40 @@ type ToolCallState = {
|
|
|
25
26
|
argsText: string;
|
|
26
27
|
};
|
|
27
28
|
|
|
29
|
+
export type AgUiRuntimeToolCall = {
|
|
30
|
+
id: string;
|
|
31
|
+
type: "function";
|
|
32
|
+
function: {
|
|
33
|
+
name: string;
|
|
34
|
+
arguments: string;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type AgUiRuntimeMessage =
|
|
39
|
+
| {
|
|
40
|
+
id: string;
|
|
41
|
+
role: "system";
|
|
42
|
+
content: string;
|
|
43
|
+
}
|
|
44
|
+
| {
|
|
45
|
+
id: string;
|
|
46
|
+
role: "user";
|
|
47
|
+
content: string;
|
|
48
|
+
}
|
|
49
|
+
| {
|
|
50
|
+
id: string;
|
|
51
|
+
role: "assistant";
|
|
52
|
+
content?: string;
|
|
53
|
+
toolCalls?: AgUiRuntimeToolCall[];
|
|
54
|
+
}
|
|
55
|
+
| {
|
|
56
|
+
id: string;
|
|
57
|
+
role: "tool";
|
|
58
|
+
toolCallId: string;
|
|
59
|
+
content: string;
|
|
60
|
+
error?: string;
|
|
61
|
+
};
|
|
62
|
+
|
|
28
63
|
export type ParsedSseEvent = {
|
|
29
64
|
id: number | null;
|
|
30
65
|
event: string | null;
|
|
@@ -145,6 +180,195 @@ export const AgUiWireEventNameSchema = z.enum([
|
|
|
145
180
|
"RunError",
|
|
146
181
|
]);
|
|
147
182
|
|
|
183
|
+
function parseRuntimeToolInput(rawArguments: string): unknown {
|
|
184
|
+
try {
|
|
185
|
+
return JSON.parse(rawArguments);
|
|
186
|
+
} catch {
|
|
187
|
+
return { raw: rawArguments };
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function parseRuntimeToolOutput(rawContent: string): unknown {
|
|
192
|
+
try {
|
|
193
|
+
return JSON.parse(rawContent);
|
|
194
|
+
} catch {
|
|
195
|
+
return rawContent;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function createRuntimeSystemMessage(
|
|
200
|
+
message: Extract<AgUiRuntimeMessage, { role: "system" }>,
|
|
201
|
+
): ChatUiMessage {
|
|
202
|
+
return {
|
|
203
|
+
id: message.id,
|
|
204
|
+
role: "system",
|
|
205
|
+
parts: [{ type: "text", text: message.content }],
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function createRuntimeUserMessage(
|
|
210
|
+
message: Extract<AgUiRuntimeMessage, { role: "user" }>,
|
|
211
|
+
): ChatUiMessage {
|
|
212
|
+
return {
|
|
213
|
+
id: message.id,
|
|
214
|
+
role: "user",
|
|
215
|
+
parts: [{ type: "text", text: message.content }],
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function createRuntimeAssistantMessage(
|
|
220
|
+
message: Extract<AgUiRuntimeMessage, { role: "assistant" }>,
|
|
221
|
+
): ChatUiMessage | null {
|
|
222
|
+
const parts: ChatUiMessagePart[] = [];
|
|
223
|
+
|
|
224
|
+
if (typeof message.content === "string" && message.content.trim().length > 0) {
|
|
225
|
+
parts.push({ type: "text", text: message.content });
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
for (const toolCall of message.toolCalls ?? []) {
|
|
229
|
+
parts.push({
|
|
230
|
+
type: "dynamic-tool",
|
|
231
|
+
toolName: toolCall.function.name,
|
|
232
|
+
toolCallId: toolCall.id,
|
|
233
|
+
input: parseRuntimeToolInput(toolCall.function.arguments),
|
|
234
|
+
state: "input-available",
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (parts.length === 0) {
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return {
|
|
243
|
+
id: message.id,
|
|
244
|
+
role: "assistant",
|
|
245
|
+
parts,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function buildResolvedRuntimeToolPart(input: {
|
|
250
|
+
toolName: string;
|
|
251
|
+
toolCallId: string;
|
|
252
|
+
toolInput: unknown;
|
|
253
|
+
title?: string;
|
|
254
|
+
providerExecuted?: boolean;
|
|
255
|
+
error?: string;
|
|
256
|
+
content: string;
|
|
257
|
+
}): ChatUiMessagePart {
|
|
258
|
+
if (typeof input.error === "string" && input.error.length > 0) {
|
|
259
|
+
return {
|
|
260
|
+
type: "dynamic-tool",
|
|
261
|
+
toolName: input.toolName,
|
|
262
|
+
toolCallId: input.toolCallId,
|
|
263
|
+
...(input.title ? { title: input.title } : {}),
|
|
264
|
+
...(input.providerExecuted !== undefined ? { providerExecuted: input.providerExecuted } : {}),
|
|
265
|
+
input: input.toolInput,
|
|
266
|
+
state: "output-error",
|
|
267
|
+
errorText: input.error,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return {
|
|
272
|
+
type: "dynamic-tool",
|
|
273
|
+
toolName: input.toolName,
|
|
274
|
+
toolCallId: input.toolCallId,
|
|
275
|
+
...(input.title ? { title: input.title } : {}),
|
|
276
|
+
...(input.providerExecuted !== undefined ? { providerExecuted: input.providerExecuted } : {}),
|
|
277
|
+
input: input.toolInput,
|
|
278
|
+
state: "output-available",
|
|
279
|
+
output: parseRuntimeToolOutput(input.content),
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function applyRuntimeToolResultMessage(
|
|
284
|
+
messages: ChatUiMessage[],
|
|
285
|
+
message: Extract<AgUiRuntimeMessage, { role: "tool" }>,
|
|
286
|
+
): void {
|
|
287
|
+
for (let messageIndex = messages.length - 1; messageIndex >= 0; messageIndex -= 1) {
|
|
288
|
+
const currentMessage = messages[messageIndex];
|
|
289
|
+
if (currentMessage.role !== "assistant") {
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const partIndex = currentMessage.parts.findIndex(
|
|
294
|
+
(part) =>
|
|
295
|
+
part.type === "dynamic-tool" &&
|
|
296
|
+
part.toolCallId === message.toolCallId &&
|
|
297
|
+
(part.state === "input-available" || part.state === "input-streaming"),
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
if (partIndex === -1) {
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const part = currentMessage.parts[partIndex];
|
|
305
|
+
if (part.type !== "dynamic-tool") {
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
currentMessage.parts.splice(
|
|
310
|
+
partIndex,
|
|
311
|
+
1,
|
|
312
|
+
buildResolvedRuntimeToolPart({
|
|
313
|
+
toolName: part.toolName,
|
|
314
|
+
toolCallId: part.toolCallId,
|
|
315
|
+
...(part.title ? { title: part.title } : {}),
|
|
316
|
+
...(part.providerExecuted !== undefined ? { providerExecuted: part.providerExecuted } : {}),
|
|
317
|
+
toolInput: part.input,
|
|
318
|
+
error: message.error,
|
|
319
|
+
content: message.content,
|
|
320
|
+
}),
|
|
321
|
+
);
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
messages.push({
|
|
326
|
+
id: message.id,
|
|
327
|
+
role: "assistant",
|
|
328
|
+
parts: [
|
|
329
|
+
buildResolvedRuntimeToolPart({
|
|
330
|
+
toolName: "unknown",
|
|
331
|
+
toolCallId: message.toolCallId,
|
|
332
|
+
toolInput: {},
|
|
333
|
+
error: message.error,
|
|
334
|
+
content: message.content,
|
|
335
|
+
}),
|
|
336
|
+
],
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export function mapAgUiRuntimeMessagesToChatUiMessages(
|
|
341
|
+
messages: AgUiRuntimeMessage[],
|
|
342
|
+
): ChatUiMessage[] {
|
|
343
|
+
const mappedMessages: ChatUiMessage[] = [];
|
|
344
|
+
|
|
345
|
+
for (const message of messages) {
|
|
346
|
+
switch (message.role) {
|
|
347
|
+
case "system":
|
|
348
|
+
mappedMessages.push(createRuntimeSystemMessage(message));
|
|
349
|
+
break;
|
|
350
|
+
|
|
351
|
+
case "user":
|
|
352
|
+
mappedMessages.push(createRuntimeUserMessage(message));
|
|
353
|
+
break;
|
|
354
|
+
|
|
355
|
+
case "assistant": {
|
|
356
|
+
const assistantMessage = createRuntimeAssistantMessage(message);
|
|
357
|
+
if (assistantMessage) {
|
|
358
|
+
mappedMessages.push(assistantMessage);
|
|
359
|
+
}
|
|
360
|
+
break;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
case "tool":
|
|
364
|
+
applyRuntimeToolResultMessage(mappedMessages, message);
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
return mappedMessages;
|
|
370
|
+
}
|
|
371
|
+
|
|
148
372
|
export const AgUiWireEventSchema = z.discriminatedUnion("eventName", [
|
|
149
373
|
z.object({
|
|
150
374
|
eventName: z.literal("RunStarted"),
|
|
@@ -736,7 +736,7 @@ async function resolveStreamPromiseWithTimeout<T>(
|
|
|
736
736
|
const resolved = await Promise.race([
|
|
737
737
|
Promise.resolve(promise),
|
|
738
738
|
new Promise<typeof STREAM_PROMISE_TIMEOUT_TOKEN>((resolve) => {
|
|
739
|
-
timeoutId = dntShim.setTimeout(() => resolve(STREAM_PROMISE_TIMEOUT_TOKEN), timeoutMs);
|
|
739
|
+
timeoutId = dntShim.dntGlobalThis.setTimeout(() => resolve(STREAM_PROMISE_TIMEOUT_TOKEN), timeoutMs);
|
|
740
740
|
}),
|
|
741
741
|
]);
|
|
742
742
|
|
|
@@ -749,7 +749,7 @@ async function resolveStreamPromiseWithTimeout<T>(
|
|
|
749
749
|
return fallback;
|
|
750
750
|
} finally {
|
|
751
751
|
if (timeoutId) {
|
|
752
|
-
clearTimeout(timeoutId);
|
|
752
|
+
globalThis.clearTimeout(timeoutId);
|
|
753
753
|
}
|
|
754
754
|
}
|
|
755
755
|
}
|