veryfront 0.1.290 → 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/src/deno.js CHANGED
@@ -1,6 +1,6 @@
1
1
  export default {
2
2
  "name": "veryfront",
3
- "version": "0.1.290",
3
+ "version": "0.1.291",
4
4
  "license": "Apache-2.0",
5
5
  "nodeModulesDir": "auto",
6
6
  "workspace": [
@@ -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"),