veryfront 0.1.193 → 0.1.196
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.d.ts +2 -0
- package/esm/deno.js +3 -1
- package/esm/src/chat/ag-ui.d.ts +306 -0
- package/esm/src/chat/ag-ui.d.ts.map +1 -0
- package/esm/src/chat/ag-ui.js +609 -0
- package/esm/src/chat/protocol.d.ts +0 -1
- package/esm/src/chat/protocol.js +0 -1
- package/esm/src/utils/version-constant.d.ts +1 -1
- package/esm/src/utils/version-constant.js +1 -1
- package/package.json +4 -1
- package/src/deno.js +3 -1
- package/src/src/chat/ag-ui.ts +753 -0
- package/src/src/utils/version-constant.ts +1 -1
|
@@ -0,0 +1,609 @@
|
|
|
1
|
+
import { mergeToolInputDelta, parseToolInputObject } from "../agent/data-stream.js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
export const AgUiRunFinishedMetadataSchema = z.object({
|
|
4
|
+
provider: z.string().optional(),
|
|
5
|
+
model: z.string().optional(),
|
|
6
|
+
inputTokens: z.number().int().nonnegative().optional(),
|
|
7
|
+
outputTokens: z.number().int().nonnegative().optional(),
|
|
8
|
+
totalTokens: z.number().int().nonnegative().optional(),
|
|
9
|
+
cachedInputTokens: z.number().int().nonnegative().optional(),
|
|
10
|
+
reasoningTokens: z.number().int().nonnegative().optional(),
|
|
11
|
+
finishReason: z.string().optional(),
|
|
12
|
+
providerRequestId: z.string().optional(),
|
|
13
|
+
});
|
|
14
|
+
export const AgUiSnapshotToolCallSchema = z.object({
|
|
15
|
+
id: z.string().min(1),
|
|
16
|
+
type: z.literal("function"),
|
|
17
|
+
function: z.object({
|
|
18
|
+
name: z.string().min(1),
|
|
19
|
+
arguments: z.string(),
|
|
20
|
+
}),
|
|
21
|
+
encryptedValue: z.string().optional(),
|
|
22
|
+
});
|
|
23
|
+
const AgUiUserInputContentSchema = z.discriminatedUnion("type", [
|
|
24
|
+
z.object({
|
|
25
|
+
type: z.literal("text"),
|
|
26
|
+
text: z.string(),
|
|
27
|
+
}),
|
|
28
|
+
z.object({
|
|
29
|
+
type: z.literal("binary"),
|
|
30
|
+
mimeType: z.string(),
|
|
31
|
+
id: z.string().optional(),
|
|
32
|
+
url: z.string().optional(),
|
|
33
|
+
data: z.string().optional(),
|
|
34
|
+
filename: z.string().optional(),
|
|
35
|
+
}),
|
|
36
|
+
]);
|
|
37
|
+
export const AgUiSnapshotMessageSchema = z.discriminatedUnion("role", [
|
|
38
|
+
z.object({
|
|
39
|
+
id: z.string(),
|
|
40
|
+
role: z.literal("assistant"),
|
|
41
|
+
content: z.string().optional(),
|
|
42
|
+
name: z.string().optional(),
|
|
43
|
+
encryptedValue: z.string().optional(),
|
|
44
|
+
toolCalls: z.array(AgUiSnapshotToolCallSchema).optional(),
|
|
45
|
+
}),
|
|
46
|
+
z.object({
|
|
47
|
+
id: z.string(),
|
|
48
|
+
role: z.literal("user"),
|
|
49
|
+
content: z.union([z.string(), z.array(AgUiUserInputContentSchema)]),
|
|
50
|
+
name: z.string().optional(),
|
|
51
|
+
encryptedValue: z.string().optional(),
|
|
52
|
+
}),
|
|
53
|
+
z.object({
|
|
54
|
+
id: z.string(),
|
|
55
|
+
role: z.literal("tool"),
|
|
56
|
+
toolCallId: z.string(),
|
|
57
|
+
content: z.string(),
|
|
58
|
+
error: z.string().optional(),
|
|
59
|
+
encryptedValue: z.string().optional(),
|
|
60
|
+
}),
|
|
61
|
+
z.object({
|
|
62
|
+
id: z.string(),
|
|
63
|
+
role: z.literal("reasoning"),
|
|
64
|
+
content: z.string(),
|
|
65
|
+
name: z.string().optional(),
|
|
66
|
+
encryptedValue: z.string().optional(),
|
|
67
|
+
}),
|
|
68
|
+
]);
|
|
69
|
+
export const AgUiWireEventNameSchema = z.enum([
|
|
70
|
+
"RunStarted",
|
|
71
|
+
"Custom",
|
|
72
|
+
"TextMessageStart",
|
|
73
|
+
"TextMessageContent",
|
|
74
|
+
"TextMessageEnd",
|
|
75
|
+
"ToolCallStart",
|
|
76
|
+
"ToolCallArgs",
|
|
77
|
+
"ToolCallChunk",
|
|
78
|
+
"ToolCallEnd",
|
|
79
|
+
"ToolCallResult",
|
|
80
|
+
"StateSnapshot",
|
|
81
|
+
"MessagesSnapshot",
|
|
82
|
+
"ReasoningMessageStart",
|
|
83
|
+
"ReasoningMessageContent",
|
|
84
|
+
"ReasoningMessageEnd",
|
|
85
|
+
"StateDelta",
|
|
86
|
+
"RunFinished",
|
|
87
|
+
"RunError",
|
|
88
|
+
]);
|
|
89
|
+
export const AgUiWireEventSchema = z.discriminatedUnion("eventName", [
|
|
90
|
+
z.object({
|
|
91
|
+
eventName: z.literal("RunStarted"),
|
|
92
|
+
payload: z.object({
|
|
93
|
+
runId: z.string().optional(),
|
|
94
|
+
threadId: z.string().optional(),
|
|
95
|
+
agentId: z.string().optional(),
|
|
96
|
+
}),
|
|
97
|
+
}),
|
|
98
|
+
z.object({
|
|
99
|
+
eventName: z.literal("Custom"),
|
|
100
|
+
payload: z.object({
|
|
101
|
+
name: z.string(),
|
|
102
|
+
value: z.unknown(),
|
|
103
|
+
}),
|
|
104
|
+
}),
|
|
105
|
+
z.object({
|
|
106
|
+
eventName: z.literal("TextMessageStart"),
|
|
107
|
+
payload: z.object({
|
|
108
|
+
messageId: z.string().min(1),
|
|
109
|
+
id: z.string().min(1).optional(),
|
|
110
|
+
contentId: z.string().min(1).optional(),
|
|
111
|
+
role: z.string().optional(),
|
|
112
|
+
}),
|
|
113
|
+
}),
|
|
114
|
+
z.object({
|
|
115
|
+
eventName: z.literal("TextMessageContent"),
|
|
116
|
+
payload: z.object({
|
|
117
|
+
messageId: z.string().min(1),
|
|
118
|
+
id: z.string().min(1).optional(),
|
|
119
|
+
contentId: z.string().min(1).optional(),
|
|
120
|
+
delta: z.string(),
|
|
121
|
+
}),
|
|
122
|
+
}),
|
|
123
|
+
z.object({
|
|
124
|
+
eventName: z.literal("TextMessageEnd"),
|
|
125
|
+
payload: z.object({
|
|
126
|
+
messageId: z.string().min(1),
|
|
127
|
+
id: z.string().min(1).optional(),
|
|
128
|
+
contentId: z.string().min(1).optional(),
|
|
129
|
+
}),
|
|
130
|
+
}),
|
|
131
|
+
z.object({
|
|
132
|
+
eventName: z.literal("ToolCallStart"),
|
|
133
|
+
payload: z.object({
|
|
134
|
+
toolCallId: z.string().min(1),
|
|
135
|
+
toolCallName: z.string().min(1),
|
|
136
|
+
}),
|
|
137
|
+
}),
|
|
138
|
+
z.object({
|
|
139
|
+
eventName: z.literal("ToolCallArgs"),
|
|
140
|
+
payload: z.object({
|
|
141
|
+
toolCallId: z.string().min(1),
|
|
142
|
+
delta: z.string(),
|
|
143
|
+
}),
|
|
144
|
+
}),
|
|
145
|
+
z.object({
|
|
146
|
+
eventName: z.literal("ToolCallChunk"),
|
|
147
|
+
payload: z.object({
|
|
148
|
+
toolCallId: z.string().min(1),
|
|
149
|
+
delta: z.string(),
|
|
150
|
+
}),
|
|
151
|
+
}),
|
|
152
|
+
z.object({
|
|
153
|
+
eventName: z.literal("ToolCallEnd"),
|
|
154
|
+
payload: z.object({
|
|
155
|
+
toolCallId: z.string().min(1),
|
|
156
|
+
}),
|
|
157
|
+
}),
|
|
158
|
+
z.object({
|
|
159
|
+
eventName: z.literal("ToolCallResult"),
|
|
160
|
+
payload: z.object({
|
|
161
|
+
messageId: z.string().min(1).optional(),
|
|
162
|
+
toolCallId: z.string().min(1),
|
|
163
|
+
content: z.unknown().optional(),
|
|
164
|
+
result: z.unknown().optional(),
|
|
165
|
+
role: z.literal("tool").optional(),
|
|
166
|
+
isError: z.boolean().optional(),
|
|
167
|
+
}),
|
|
168
|
+
}),
|
|
169
|
+
z.object({
|
|
170
|
+
eventName: z.literal("StateSnapshot"),
|
|
171
|
+
payload: z.object({
|
|
172
|
+
snapshot: z.record(z.string(), z.unknown()),
|
|
173
|
+
}),
|
|
174
|
+
}),
|
|
175
|
+
z.object({
|
|
176
|
+
eventName: z.literal("MessagesSnapshot"),
|
|
177
|
+
payload: z.object({
|
|
178
|
+
messages: z.array(AgUiSnapshotMessageSchema),
|
|
179
|
+
}),
|
|
180
|
+
}),
|
|
181
|
+
z.object({
|
|
182
|
+
eventName: z.literal("ReasoningMessageStart"),
|
|
183
|
+
payload: z.object({
|
|
184
|
+
id: z.string().optional(),
|
|
185
|
+
messageId: z.string().min(1).optional(),
|
|
186
|
+
role: z.string().optional(),
|
|
187
|
+
}),
|
|
188
|
+
}),
|
|
189
|
+
z.object({
|
|
190
|
+
eventName: z.literal("ReasoningMessageContent"),
|
|
191
|
+
payload: z.object({
|
|
192
|
+
id: z.string().optional(),
|
|
193
|
+
messageId: z.string().min(1).optional(),
|
|
194
|
+
delta: z.string(),
|
|
195
|
+
}),
|
|
196
|
+
}),
|
|
197
|
+
z.object({
|
|
198
|
+
eventName: z.literal("ReasoningMessageEnd"),
|
|
199
|
+
payload: z.object({
|
|
200
|
+
id: z.string().optional(),
|
|
201
|
+
messageId: z.string().min(1).optional(),
|
|
202
|
+
}),
|
|
203
|
+
}),
|
|
204
|
+
z.object({
|
|
205
|
+
eventName: z.literal("StateDelta"),
|
|
206
|
+
payload: z.object({
|
|
207
|
+
delta: z.union([
|
|
208
|
+
z.record(z.string(), z.unknown()),
|
|
209
|
+
z.array(z.object({
|
|
210
|
+
op: z.enum(["add", "remove", "replace", "move", "copy", "test"]),
|
|
211
|
+
path: z.string().min(1),
|
|
212
|
+
from: z.string().min(1).optional(),
|
|
213
|
+
value: z.unknown().optional(),
|
|
214
|
+
})),
|
|
215
|
+
]),
|
|
216
|
+
}),
|
|
217
|
+
}),
|
|
218
|
+
z.object({
|
|
219
|
+
eventName: z.literal("RunFinished"),
|
|
220
|
+
payload: z.object({
|
|
221
|
+
metadata: AgUiRunFinishedMetadataSchema.optional(),
|
|
222
|
+
}),
|
|
223
|
+
}),
|
|
224
|
+
z.object({
|
|
225
|
+
eventName: z.literal("RunError"),
|
|
226
|
+
payload: z.object({
|
|
227
|
+
code: z.string().optional(),
|
|
228
|
+
message: z.string().optional(),
|
|
229
|
+
}),
|
|
230
|
+
}),
|
|
231
|
+
]);
|
|
232
|
+
function isRecord(value) {
|
|
233
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
234
|
+
}
|
|
235
|
+
function normalizeNewlines(value) {
|
|
236
|
+
return value.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
237
|
+
}
|
|
238
|
+
function toRenderableCustomChunk(value) {
|
|
239
|
+
if (!isRecord(value) || typeof value.type !== "string") {
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
if (value.type === "source-url" && typeof value.url === "string") {
|
|
243
|
+
return {
|
|
244
|
+
type: "source-url",
|
|
245
|
+
sourceId: typeof value.sourceId === "string" && value.sourceId.length > 0
|
|
246
|
+
? value.sourceId
|
|
247
|
+
: value.url,
|
|
248
|
+
url: value.url,
|
|
249
|
+
...(typeof value.title === "string" ? { title: value.title } : {}),
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
if (value.type === "source-document" &&
|
|
253
|
+
typeof value.sourceId === "string" &&
|
|
254
|
+
typeof value.mediaType === "string" &&
|
|
255
|
+
typeof value.title === "string") {
|
|
256
|
+
return {
|
|
257
|
+
type: "source-document",
|
|
258
|
+
sourceId: value.sourceId,
|
|
259
|
+
mediaType: value.mediaType,
|
|
260
|
+
title: value.title,
|
|
261
|
+
...(typeof value.filename === "string" ? { filename: value.filename } : {}),
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
if (value.type === "file" && typeof value.url === "string" && typeof value.mediaType === "string") {
|
|
265
|
+
return {
|
|
266
|
+
type: "file",
|
|
267
|
+
url: value.url,
|
|
268
|
+
mediaType: value.mediaType,
|
|
269
|
+
...(typeof value.filename === "string" ? { filename: value.filename } : {}),
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
function parseSerializedToolResult(value) {
|
|
275
|
+
if (typeof value !== "string") {
|
|
276
|
+
return value;
|
|
277
|
+
}
|
|
278
|
+
const trimmed = value.trim();
|
|
279
|
+
if (!trimmed.startsWith("{") &&
|
|
280
|
+
!trimmed.startsWith("[") &&
|
|
281
|
+
trimmed !== "null" &&
|
|
282
|
+
trimmed !== "true" &&
|
|
283
|
+
trimmed !== "false" &&
|
|
284
|
+
!/^[-]?\d+(\.\d+)?$/.test(trimmed)) {
|
|
285
|
+
return value;
|
|
286
|
+
}
|
|
287
|
+
try {
|
|
288
|
+
return JSON.parse(trimmed);
|
|
289
|
+
}
|
|
290
|
+
catch {
|
|
291
|
+
return value;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
function formatToolErrorText(result) {
|
|
295
|
+
if (typeof result === "string" && result.length > 0) {
|
|
296
|
+
return result;
|
|
297
|
+
}
|
|
298
|
+
if (isRecord(result)) {
|
|
299
|
+
if (typeof result.error === "string" && result.error.length > 0) {
|
|
300
|
+
return result.error;
|
|
301
|
+
}
|
|
302
|
+
if (typeof result.message === "string" && result.message.length > 0) {
|
|
303
|
+
return result.message;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return JSON.stringify(result ?? { error: "Tool execution failed" });
|
|
307
|
+
}
|
|
308
|
+
function mapFinishReason(reason) {
|
|
309
|
+
if (!reason)
|
|
310
|
+
return undefined;
|
|
311
|
+
switch (reason.trim().toLowerCase()) {
|
|
312
|
+
case "stop":
|
|
313
|
+
case "end_turn":
|
|
314
|
+
case "stop_sequence":
|
|
315
|
+
return "stop";
|
|
316
|
+
case "length":
|
|
317
|
+
case "max_tokens":
|
|
318
|
+
return "length";
|
|
319
|
+
case "tool_calls":
|
|
320
|
+
case "tool_use":
|
|
321
|
+
return "tool-calls";
|
|
322
|
+
case "content_filter":
|
|
323
|
+
case "content-filter":
|
|
324
|
+
return "content-filter";
|
|
325
|
+
case "error":
|
|
326
|
+
return "error";
|
|
327
|
+
default:
|
|
328
|
+
return "other";
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
function getReasoningPartId(state, payload, phase) {
|
|
332
|
+
if (typeof payload.id === "string" && payload.id.length > 0) {
|
|
333
|
+
return payload.id;
|
|
334
|
+
}
|
|
335
|
+
if (typeof payload.messageId === "string" && payload.messageId.length > 0) {
|
|
336
|
+
return `agui-reasoning:${payload.messageId}`;
|
|
337
|
+
}
|
|
338
|
+
if (state.activeFallbackReasoningPartId) {
|
|
339
|
+
const fallbackId = state.activeFallbackReasoningPartId;
|
|
340
|
+
if (phase === "end") {
|
|
341
|
+
state.activeFallbackReasoningPartId = null;
|
|
342
|
+
}
|
|
343
|
+
return fallbackId;
|
|
344
|
+
}
|
|
345
|
+
state.reasoningFallbackIndex += 1;
|
|
346
|
+
const fallbackId = `agui-reasoning:${state.reasoningFallbackIndex}`;
|
|
347
|
+
if (phase !== "end") {
|
|
348
|
+
state.activeFallbackReasoningPartId = fallbackId;
|
|
349
|
+
}
|
|
350
|
+
return fallbackId;
|
|
351
|
+
}
|
|
352
|
+
function splitSseFrames(value) {
|
|
353
|
+
const blocks = value.split("\n\n");
|
|
354
|
+
return {
|
|
355
|
+
frames: blocks.slice(0, -1),
|
|
356
|
+
remainder: blocks.at(-1) ?? "",
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
function isCommentOnlySseFrame(raw) {
|
|
360
|
+
return raw
|
|
361
|
+
.split("\n")
|
|
362
|
+
.every((line) => line.trim().length === 0 || line.trimStart().startsWith(":"));
|
|
363
|
+
}
|
|
364
|
+
function parseAgUiWireEvent(frame) {
|
|
365
|
+
if (frame.data === "[DONE]" || !frame.event || !frame.data) {
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
const eventName = AgUiWireEventNameSchema.safeParse(frame.event);
|
|
369
|
+
if (!eventName.success) {
|
|
370
|
+
return null;
|
|
371
|
+
}
|
|
372
|
+
let payload;
|
|
373
|
+
try {
|
|
374
|
+
payload = JSON.parse(frame.data);
|
|
375
|
+
}
|
|
376
|
+
catch {
|
|
377
|
+
return null;
|
|
378
|
+
}
|
|
379
|
+
if (!isRecord(payload)) {
|
|
380
|
+
return null;
|
|
381
|
+
}
|
|
382
|
+
const parsed = AgUiWireEventSchema.safeParse({
|
|
383
|
+
eventName: eventName.data,
|
|
384
|
+
payload,
|
|
385
|
+
});
|
|
386
|
+
return parsed.success ? parsed.data : null;
|
|
387
|
+
}
|
|
388
|
+
function mapWireEventToChatEvents(state, wireEvent) {
|
|
389
|
+
switch (wireEvent.eventName) {
|
|
390
|
+
case "RunStarted":
|
|
391
|
+
return [{
|
|
392
|
+
type: "start",
|
|
393
|
+
messageMetadata: wireEvent.payload,
|
|
394
|
+
}];
|
|
395
|
+
case "TextMessageStart":
|
|
396
|
+
return [{
|
|
397
|
+
type: "text-start",
|
|
398
|
+
id: wireEvent.payload.messageId,
|
|
399
|
+
}];
|
|
400
|
+
case "TextMessageContent":
|
|
401
|
+
return [{
|
|
402
|
+
type: "text-delta",
|
|
403
|
+
id: wireEvent.payload.messageId,
|
|
404
|
+
delta: wireEvent.payload.delta,
|
|
405
|
+
}];
|
|
406
|
+
case "TextMessageEnd":
|
|
407
|
+
return [{
|
|
408
|
+
type: "text-end",
|
|
409
|
+
id: wireEvent.payload.messageId,
|
|
410
|
+
}];
|
|
411
|
+
case "ReasoningMessageStart":
|
|
412
|
+
return [{
|
|
413
|
+
type: "reasoning-start",
|
|
414
|
+
id: getReasoningPartId(state, wireEvent.payload, "start"),
|
|
415
|
+
}];
|
|
416
|
+
case "ReasoningMessageContent":
|
|
417
|
+
return [{
|
|
418
|
+
type: "reasoning-delta",
|
|
419
|
+
id: getReasoningPartId(state, wireEvent.payload, "content"),
|
|
420
|
+
delta: wireEvent.payload.delta,
|
|
421
|
+
}];
|
|
422
|
+
case "ReasoningMessageEnd":
|
|
423
|
+
return [{
|
|
424
|
+
type: "reasoning-end",
|
|
425
|
+
id: getReasoningPartId(state, wireEvent.payload, "end"),
|
|
426
|
+
}];
|
|
427
|
+
case "ToolCallStart":
|
|
428
|
+
state.toolCalls.set(wireEvent.payload.toolCallId, {
|
|
429
|
+
toolName: wireEvent.payload.toolCallName,
|
|
430
|
+
argsText: "",
|
|
431
|
+
});
|
|
432
|
+
return [{
|
|
433
|
+
type: "tool-input-start",
|
|
434
|
+
toolCallId: wireEvent.payload.toolCallId,
|
|
435
|
+
toolName: wireEvent.payload.toolCallName,
|
|
436
|
+
providerExecuted: true,
|
|
437
|
+
}];
|
|
438
|
+
case "ToolCallArgs":
|
|
439
|
+
case "ToolCallChunk": {
|
|
440
|
+
const toolCall = state.toolCalls.get(wireEvent.payload.toolCallId);
|
|
441
|
+
if (!toolCall) {
|
|
442
|
+
return [];
|
|
443
|
+
}
|
|
444
|
+
toolCall.argsText = mergeToolInputDelta(toolCall.argsText, wireEvent.payload.delta);
|
|
445
|
+
return [{
|
|
446
|
+
type: "tool-input-delta",
|
|
447
|
+
toolCallId: wireEvent.payload.toolCallId,
|
|
448
|
+
inputTextDelta: wireEvent.payload.delta,
|
|
449
|
+
}];
|
|
450
|
+
}
|
|
451
|
+
case "ToolCallEnd": {
|
|
452
|
+
const toolCall = state.toolCalls.get(wireEvent.payload.toolCallId);
|
|
453
|
+
if (!toolCall) {
|
|
454
|
+
return [];
|
|
455
|
+
}
|
|
456
|
+
return [{
|
|
457
|
+
type: "tool-input-available",
|
|
458
|
+
toolCallId: wireEvent.payload.toolCallId,
|
|
459
|
+
toolName: toolCall.toolName,
|
|
460
|
+
input: parseToolInputObject(toolCall.argsText),
|
|
461
|
+
providerExecuted: true,
|
|
462
|
+
}];
|
|
463
|
+
}
|
|
464
|
+
case "ToolCallResult": {
|
|
465
|
+
const toolCall = state.toolCalls.get(wireEvent.payload.toolCallId);
|
|
466
|
+
const parsedResult = parseSerializedToolResult(wireEvent.payload.content ?? wireEvent.payload.result);
|
|
467
|
+
if (wireEvent.payload.isError) {
|
|
468
|
+
return [{
|
|
469
|
+
type: "tool-output-error",
|
|
470
|
+
toolCallId: wireEvent.payload.toolCallId,
|
|
471
|
+
errorText: formatToolErrorText(parsedResult),
|
|
472
|
+
providerExecuted: true,
|
|
473
|
+
}];
|
|
474
|
+
}
|
|
475
|
+
const events = [];
|
|
476
|
+
if (!toolCall) {
|
|
477
|
+
events.push({
|
|
478
|
+
type: "tool-input-available",
|
|
479
|
+
toolCallId: wireEvent.payload.toolCallId,
|
|
480
|
+
toolName: "tool",
|
|
481
|
+
input: {},
|
|
482
|
+
dynamic: true,
|
|
483
|
+
providerExecuted: true,
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
events.push({
|
|
487
|
+
type: "tool-output-available",
|
|
488
|
+
toolCallId: wireEvent.payload.toolCallId,
|
|
489
|
+
output: parsedResult,
|
|
490
|
+
providerExecuted: true,
|
|
491
|
+
});
|
|
492
|
+
return events;
|
|
493
|
+
}
|
|
494
|
+
case "StateSnapshot":
|
|
495
|
+
return [{ type: "data-state-snapshot", data: wireEvent.payload.snapshot }];
|
|
496
|
+
case "StateDelta":
|
|
497
|
+
return [{
|
|
498
|
+
type: "data-state-delta",
|
|
499
|
+
data: wireEvent.payload.delta,
|
|
500
|
+
}];
|
|
501
|
+
case "MessagesSnapshot":
|
|
502
|
+
return [{ type: "data-messages-snapshot", data: wireEvent.payload.messages }];
|
|
503
|
+
case "Custom": {
|
|
504
|
+
const renderableChunk = toRenderableCustomChunk(wireEvent.payload.value);
|
|
505
|
+
if (renderableChunk) {
|
|
506
|
+
return [renderableChunk];
|
|
507
|
+
}
|
|
508
|
+
return [{
|
|
509
|
+
type: `data-${wireEvent.payload.name}`,
|
|
510
|
+
data: wireEvent.payload.value,
|
|
511
|
+
}];
|
|
512
|
+
}
|
|
513
|
+
case "RunFinished":
|
|
514
|
+
return [{
|
|
515
|
+
type: "finish",
|
|
516
|
+
...(mapFinishReason(wireEvent.payload.metadata?.finishReason)
|
|
517
|
+
? { finishReason: mapFinishReason(wireEvent.payload.metadata?.finishReason) }
|
|
518
|
+
: {}),
|
|
519
|
+
}];
|
|
520
|
+
case "RunError":
|
|
521
|
+
if (wireEvent.payload.code === "CANCELLED") {
|
|
522
|
+
return [{ type: "abort" }];
|
|
523
|
+
}
|
|
524
|
+
return [{
|
|
525
|
+
type: "error",
|
|
526
|
+
errorText: wireEvent.payload.message?.length
|
|
527
|
+
? wireEvent.payload.message
|
|
528
|
+
: "Conversation agent run failed",
|
|
529
|
+
}];
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
export function parseSseEvent(raw) {
|
|
533
|
+
let id = null;
|
|
534
|
+
let event = null;
|
|
535
|
+
const dataLines = [];
|
|
536
|
+
for (const line of raw.split("\n")) {
|
|
537
|
+
if (!line || line.startsWith(":")) {
|
|
538
|
+
continue;
|
|
539
|
+
}
|
|
540
|
+
if (line.startsWith("id:")) {
|
|
541
|
+
const parsed = Number(line.slice(3).trim());
|
|
542
|
+
if (Number.isFinite(parsed)) {
|
|
543
|
+
id = parsed;
|
|
544
|
+
}
|
|
545
|
+
continue;
|
|
546
|
+
}
|
|
547
|
+
if (line.startsWith("event:")) {
|
|
548
|
+
const value = line.slice(6).trim();
|
|
549
|
+
event = value.length > 0 ? value : null;
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
if (line.startsWith("data:")) {
|
|
553
|
+
const value = line.slice(5);
|
|
554
|
+
dataLines.push(value.startsWith(" ") ? value.slice(1) : value);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
return { id, event, data: dataLines.join("\n") };
|
|
558
|
+
}
|
|
559
|
+
export function createAgUiChatEventDecoderState(input = {}) {
|
|
560
|
+
return {
|
|
561
|
+
remainder: "",
|
|
562
|
+
lastEventId: input.lastEventId ?? -1,
|
|
563
|
+
toolCalls: new Map(),
|
|
564
|
+
reasoningFallbackIndex: 0,
|
|
565
|
+
activeFallbackReasoningPartId: null,
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
export function decodeAgUiSseChunk(state, chunk) {
|
|
569
|
+
const normalized = `${state.remainder}${normalizeNewlines(chunk)}`;
|
|
570
|
+
const { frames, remainder } = splitSseFrames(normalized);
|
|
571
|
+
state.remainder = remainder;
|
|
572
|
+
const events = [];
|
|
573
|
+
for (const rawFrame of frames) {
|
|
574
|
+
if (isCommentOnlySseFrame(rawFrame)) {
|
|
575
|
+
continue;
|
|
576
|
+
}
|
|
577
|
+
const frame = parseSseEvent(rawFrame);
|
|
578
|
+
if (frame.id !== null) {
|
|
579
|
+
if (frame.id <= state.lastEventId) {
|
|
580
|
+
continue;
|
|
581
|
+
}
|
|
582
|
+
state.lastEventId = frame.id;
|
|
583
|
+
}
|
|
584
|
+
const wireEvent = parseAgUiWireEvent(frame);
|
|
585
|
+
if (!wireEvent) {
|
|
586
|
+
continue;
|
|
587
|
+
}
|
|
588
|
+
events.push({
|
|
589
|
+
eventId: frame.id,
|
|
590
|
+
wireEvent,
|
|
591
|
+
chatEvents: mapWireEventToChatEvents(state, wireEvent),
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
return {
|
|
595
|
+
events,
|
|
596
|
+
remainder: state.remainder,
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
export function flushAgUiSseChunk(state) {
|
|
600
|
+
if (state.remainder.length === 0) {
|
|
601
|
+
return { events: [], remainder: "" };
|
|
602
|
+
}
|
|
603
|
+
const flushed = decodeAgUiSseChunk(state, "\n\n");
|
|
604
|
+
state.remainder = "";
|
|
605
|
+
return {
|
|
606
|
+
events: flushed.events,
|
|
607
|
+
remainder: "",
|
|
608
|
+
};
|
|
609
|
+
}
|
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
* These types describe the framework-owned message parts and stream events used
|
|
5
5
|
* by AG-UI-aligned chat clients, hooks, and adapters.
|
|
6
6
|
*/
|
|
7
|
-
import "../../_dnt.polyfills.js";
|
|
8
7
|
export type ChatPartState = "streaming" | "done";
|
|
9
8
|
export interface ChatTextPart {
|
|
10
9
|
type: "text";
|
package/esm/src/chat/protocol.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const VERSION = "0.1.
|
|
1
|
+
export declare const VERSION = "0.1.196";
|
|
2
2
|
//# sourceMappingURL=version-constant.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "veryfront",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.196",
|
|
4
4
|
"description": "The simplest way to build AI-powered apps",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -43,6 +43,9 @@
|
|
|
43
43
|
"./chat": {
|
|
44
44
|
"import": "./esm/src/chat/index.js"
|
|
45
45
|
},
|
|
46
|
+
"./chat/ag-ui": {
|
|
47
|
+
"import": "./esm/src/chat/ag-ui.js"
|
|
48
|
+
},
|
|
46
49
|
"./chat/protocol": {
|
|
47
50
|
"import": "./esm/src/chat/protocol.js"
|
|
48
51
|
},
|
package/src/deno.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export default {
|
|
2
2
|
"name": "veryfront",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.196",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"nodeModulesDir": "auto",
|
|
6
6
|
"exclude": [
|
|
@@ -21,6 +21,7 @@ export default {
|
|
|
21
21
|
"./context": "./src/react/context/index.tsx",
|
|
22
22
|
"./fonts": "./src/react/fonts/index.ts",
|
|
23
23
|
"./chat": "./src/chat/index.ts",
|
|
24
|
+
"./chat/ag-ui": "./src/chat/ag-ui.ts",
|
|
24
25
|
"./chat/protocol": "./src/chat/protocol.ts",
|
|
25
26
|
"./markdown": "./src/markdown/index.ts",
|
|
26
27
|
"./mdx": "./src/mdx/index.ts",
|
|
@@ -51,6 +52,7 @@ export default {
|
|
|
51
52
|
"veryfront/context": "./src/react/runtime/core.ts",
|
|
52
53
|
"veryfront/fonts": "./src/react/fonts/index.ts",
|
|
53
54
|
"veryfront/chat": "./src/chat/index.ts",
|
|
55
|
+
"veryfront/chat/ag-ui": "./src/chat/ag-ui.ts",
|
|
54
56
|
"veryfront/chat/protocol": "./src/chat/protocol.ts",
|
|
55
57
|
"veryfront/markdown": "./src/markdown/index.ts",
|
|
56
58
|
"veryfront/mdx": "./src/mdx/index.ts",
|