veryfront 0.1.150 → 0.1.152

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.
@@ -1,8 +1,6 @@
1
1
  import { z } from "zod";
2
+ import { AgUiRuntimeContextItemSchema, AgUiRuntimeContextSchema, AgUiRuntimeInjectedToolSchema, AgUiRuntimeMessageSchema, AgUiRuntimeRequestSchema, AgUiRuntimeRunIdSchema, } from "../agent/runtime-ag-ui-contract.js";
2
3
  const AGENT_ID_PATTERN = /^[a-zA-Z0-9_-]+$/;
3
- const MAX_TOOL_PARAMETERS_BYTES = 16_384;
4
- const MAX_CONTEXT_ITEM_BYTES = 16_384;
5
- const MAX_CONTEXT_TOTAL_BYTES = 65_536;
6
4
  const MAX_FORWARDED_PROPS_BYTES = 65_536;
7
5
  const MAX_TOOL_RESULT_BYTES = 65_536;
8
6
  const MAX_RUNTIME_MESSAGES = 100;
@@ -15,37 +13,8 @@ function isWithinJsonSizeLimit(value, maxBytes) {
15
13
  return false;
16
14
  }
17
15
  }
18
- export const RunIdSchema = z.string().min(1).max(128).regex(AGENT_ID_PATTERN);
16
+ export const RunIdSchema = AgUiRuntimeRunIdSchema;
19
17
  export const AgentIdSchema = z.string().min(1).max(128).regex(AGENT_ID_PATTERN);
20
- export const ClientToolNameSchema = z
21
- .string()
22
- .min(1)
23
- .max(128)
24
- .regex(/^[a-zA-Z][a-zA-Z0-9._:-]*$/, "Tool names must start with a letter and use a valid client-tool format");
25
- export const RuntimeInjectedToolSchema = z.object({
26
- name: ClientToolNameSchema,
27
- description: z.string().max(1024).optional(),
28
- parameters: z.record(z.string(), z.unknown()).optional().refine((value) => value === undefined || isWithinJsonSizeLimit(value, MAX_TOOL_PARAMETERS_BYTES), { message: "Tool parameters must be less than 16 KB" }),
29
- });
30
- export const RuntimeContextItemSchema = z.discriminatedUnion("type", [
31
- z.object({
32
- type: z.literal("text"),
33
- title: z.string().max(256).optional(),
34
- text: z.string().max(MAX_CONTEXT_ITEM_BYTES),
35
- }),
36
- z.object({
37
- type: z.literal("json"),
38
- title: z.string().max(256).optional(),
39
- data: z.record(z.string(), z.unknown()).refine((value) => isWithinJsonSizeLimit(value, MAX_CONTEXT_ITEM_BYTES), { message: "JSON context item must be less than 16 KB" }),
40
- }),
41
- z.object({
42
- type: z.literal("resource"),
43
- title: z.string().max(256).optional(),
44
- uri: z.string().max(2048),
45
- mimeType: z.string().max(256).optional(),
46
- text: z.string().max(MAX_CONTEXT_ITEM_BYTES).optional(),
47
- }),
48
- ]);
49
18
  export const RuntimeAgentSourceContextSchema = z.discriminatedUnion("type", [
50
19
  z.object({
51
20
  type: z.literal("branch"),
@@ -61,70 +30,11 @@ export const RuntimeAgentSourceContextSchema = z.discriminatedUnion("type", [
61
30
  releaseId: z.string().min(1).max(255),
62
31
  }),
63
32
  ]);
64
- const RuntimeMessageExtensionFieldsSchema = {
65
- name: z.string().max(256).optional(),
66
- metadata: z.record(z.string(), z.unknown()).optional(),
67
- createdAt: z.string().optional(),
68
- };
69
- export const RuntimeToolFunctionCallSchema = z.object({
70
- name: ClientToolNameSchema,
71
- arguments: z.string().max(MAX_TOOL_PARAMETERS_BYTES),
72
- }).strict();
73
- export const RuntimeToolCallSchema = z.object({
74
- id: z.string().min(1).max(128),
75
- type: z.literal("function"),
76
- function: RuntimeToolFunctionCallSchema,
77
- }).strict();
78
- export const RuntimeSystemMessageSchema = z.object({
79
- id: z.string().min(1),
80
- role: z.literal("system"),
81
- content: z.string(),
82
- ...RuntimeMessageExtensionFieldsSchema,
83
- }).strict();
84
- export const RuntimeUserMessageSchema = z.object({
85
- id: z.string().min(1),
86
- role: z.literal("user"),
87
- content: z.string(),
88
- ...RuntimeMessageExtensionFieldsSchema,
89
- }).strict();
90
- export const RuntimeAssistantMessageSchema = z.object({
91
- id: z.string().min(1),
92
- role: z.literal("assistant"),
93
- content: z.string().optional(),
94
- toolCalls: z.array(RuntimeToolCallSchema).optional(),
95
- ...RuntimeMessageExtensionFieldsSchema,
96
- }).strict();
97
- export const RuntimeToolMessageSchema = z.object({
98
- id: z.string().min(1),
99
- role: z.literal("tool"),
100
- toolCallId: z.string().min(1).max(128),
101
- content: z.string(),
102
- error: z.string().optional(),
103
- ...RuntimeMessageExtensionFieldsSchema,
104
- }).strict();
105
- export const RuntimeMessageSchema = z.discriminatedUnion("role", [
106
- RuntimeSystemMessageSchema,
107
- RuntimeUserMessageSchema,
108
- RuntimeAssistantMessageSchema,
109
- RuntimeToolMessageSchema,
110
- ]);
111
- export const RuntimeContextSchema = z.union([
112
- z.object({
113
- description: z.string().max(1024),
114
- value: z.string().max(MAX_CONTEXT_ITEM_BYTES),
115
- }),
116
- RuntimeContextItemSchema,
117
- ]);
118
- export const RuntimeRunAgentInputSchema = z.object({
119
- threadId: z.string().uuid(),
120
- runId: RunIdSchema,
121
- parentRunId: RunIdSchema.optional(),
122
- state: z.unknown().optional(),
123
- messages: z.array(RuntimeMessageSchema).max(MAX_RUNTIME_MESSAGES),
124
- tools: z.array(RuntimeInjectedToolSchema).max(50).default([]),
125
- context: z.array(RuntimeContextSchema).max(10).default([]).refine((value) => isWithinJsonSizeLimit(value, MAX_CONTEXT_TOTAL_BYTES), { message: "context must be less than 64 KB total" }),
126
- forwardedProps: z.record(z.string(), z.unknown()).optional().refine((value) => value === undefined || isWithinJsonSizeLimit(value, MAX_FORWARDED_PROPS_BYTES), { message: "forwardedProps must be less than 64 KB" }),
127
- });
33
+ export const RuntimeInjectedToolSchema = AgUiRuntimeInjectedToolSchema;
34
+ export const RuntimeContextItemSchema = AgUiRuntimeContextItemSchema;
35
+ export const RuntimeMessageSchema = AgUiRuntimeMessageSchema;
36
+ export const RuntimeContextSchema = AgUiRuntimeContextSchema;
37
+ export const RuntimeRunAgentInputSchema = AgUiRuntimeRequestSchema;
128
38
  export const InternalAgentCompatibilityMessageSchema = z.object({
129
39
  id: z.string().min(1),
130
40
  role: z.enum(["user", "assistant", "system", "tool"]),
@@ -140,7 +50,7 @@ export const InternalAgentStreamRequestSchema = z.object({
140
50
  state: z.unknown().optional(),
141
51
  messages: z.array(z.union([RuntimeMessageSchema, InternalAgentCompatibilityMessageSchema])).max(MAX_RUNTIME_MESSAGES),
142
52
  tools: z.array(RuntimeInjectedToolSchema).max(50).default([]),
143
- context: z.array(RuntimeContextSchema).max(10).default([]).refine((value) => isWithinJsonSizeLimit(value, MAX_CONTEXT_TOTAL_BYTES), { message: "context must be less than 64 KB total" }),
53
+ context: z.array(RuntimeContextSchema).max(10).default([]).refine((value) => isWithinJsonSizeLimit(value, 65_536), { message: "context must be less than 64 KB total" }),
144
54
  agentSource: RuntimeAgentSourceContextSchema.optional(),
145
55
  forwardedProps: z.record(z.string(), z.unknown()).optional().refine((value) => value === undefined || isWithinJsonSizeLimit(value, MAX_FORWARDED_PROPS_BYTES), { message: "forwardedProps must be less than 64 KB" }),
146
56
  });
@@ -1 +1 @@
1
- {"version":3,"file":"runtime-loader.d.ts","sourceRoot":"","sources":["../../../src/src/provider/runtime-loader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,qBAAqB,CAAC;AAC/C,OAAO,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAEjE,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,OAAO,OAAO,CAAC,KAAK,CAAC;CAC9B;AAED,MAAM,WAAW,sBAAsB;IACrC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,OAAO,OAAO,CAAC,KAAK,CAAC;CAC9B;AAED,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,OAAO,OAAO,CAAC,KAAK,CAAC;CAC9B;AAqhDD,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,mBAAmB,EAC3B,OAAO,EAAE,MAAM,GACd,YAAY,CAkDd;AAED,wBAAgB,2BAA2B,CACzC,MAAM,EAAE,sBAAsB,EAC9B,OAAO,EAAE,MAAM,GACd,YAAY,CA4Dd;AAED,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,mBAAmB,EAC3B,OAAO,EAAE,MAAM,GACd,YAAY,CAkDd;AAED,wBAAgB,4BAA4B,CAC1C,MAAM,EAAE,mBAAmB,EAC3B,OAAO,EAAE,MAAM,GACd,gBAAgB,CA0ClB;AAED,wBAAgB,4BAA4B,CAC1C,MAAM,EAAE,mBAAmB,EAC3B,OAAO,EAAE,MAAM,GACd,gBAAgB,CAgDlB"}
1
+ {"version":3,"file":"runtime-loader.d.ts","sourceRoot":"","sources":["../../../src/src/provider/runtime-loader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,qBAAqB,CAAC;AAC/C,OAAO,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAEjE,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,OAAO,OAAO,CAAC,KAAK,CAAC;CAC9B;AAED,MAAM,WAAW,sBAAsB;IACrC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,OAAO,OAAO,CAAC,KAAK,CAAC;CAC9B;AAED,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,OAAO,OAAO,CAAC,KAAK,CAAC;CAC9B;AAu/CD,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,mBAAmB,EAC3B,OAAO,EAAE,MAAM,GACd,YAAY,CAkDd;AAED,wBAAgB,2BAA2B,CACzC,MAAM,EAAE,sBAAsB,EAC9B,OAAO,EAAE,MAAM,GACd,YAAY,CA4Dd;AAED,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,mBAAmB,EAC3B,OAAO,EAAE,MAAM,GACd,YAAY,CAkDd;AAED,wBAAgB,4BAA4B,CAC1C,MAAM,EAAE,mBAAmB,EAC3B,OAAO,EAAE,MAAM,GACd,gBAAgB,CA0ClB;AAED,wBAAgB,4BAA4B,CAC1C,MAAM,EAAE,mBAAmB,EAC3B,OAAO,EAAE,MAAM,GACd,gBAAgB,CAgDlB"}
@@ -404,11 +404,11 @@ function buildAnthropicGenerateResult(payload) {
404
404
  usage: extractAnthropicUsage(payload),
405
405
  };
406
406
  }
407
- function parseAnthropicSseChunk(chunk) {
408
- const blocks = chunk.split("\n\n");
407
+ function parseSseChunk(chunk) {
408
+ const blocks = chunk.split(/\r?\n\r?\n/);
409
409
  const remainder = blocks.pop() ?? "";
410
410
  const events = blocks.flatMap((block) => {
411
- const dataLines = block.split("\n")
411
+ const dataLines = block.split(/\r?\n/)
412
412
  .filter((line) => line.startsWith("data:"))
413
413
  .map((line) => line.slice(5).trimStart());
414
414
  if (!dataLines.length) {
@@ -435,7 +435,7 @@ async function* streamAnthropicCompatibleParts(stream) {
435
435
  let usage;
436
436
  for await (const chunk of stream) {
437
437
  buffer += decoder.decode(chunk, { stream: true });
438
- const parsed = parseAnthropicSseChunk(buffer);
438
+ const parsed = parseSseChunk(buffer);
439
439
  buffer = parsed.remainder;
440
440
  for (const event of parsed.events) {
441
441
  if (event === "[DONE]") {
@@ -547,7 +547,7 @@ async function* streamAnthropicCompatibleParts(stream) {
547
547
  }
548
548
  }
549
549
  if (buffer.trim().length > 0) {
550
- const parsed = parseAnthropicSseChunk(`${buffer}\n\n`);
550
+ const parsed = parseSseChunk(`${buffer}\n\n`);
551
551
  for (const event of parsed.events) {
552
552
  if (event === "[DONE]") {
553
553
  continue;
@@ -855,7 +855,7 @@ async function* streamGoogleCompatibleParts(stream) {
855
855
  let usage;
856
856
  for await (const chunk of stream) {
857
857
  buffer += decoder.decode(chunk, { stream: true });
858
- const parsed = parseOpenAISseChunk(buffer);
858
+ const parsed = parseSseChunk(buffer);
859
859
  buffer = parsed.remainder;
860
860
  for (const event of parsed.events) {
861
861
  if (event === "[DONE]") {
@@ -902,7 +902,7 @@ async function* streamGoogleCompatibleParts(stream) {
902
902
  }
903
903
  }
904
904
  if (buffer.trim().length > 0) {
905
- const parsed = parseOpenAISseChunk(`${buffer}\n\n`);
905
+ const parsed = parseSseChunk(`${buffer}\n\n`);
906
906
  for (const event of parsed.events) {
907
907
  if (event === "[DONE]") {
908
908
  continue;
@@ -947,29 +947,6 @@ function buildOpenAIGenerateResult(payload) {
947
947
  usage: extractOpenAIUsage(payload),
948
948
  };
949
949
  }
950
- function parseOpenAISseChunk(chunk) {
951
- const blocks = chunk.split("\n\n");
952
- const remainder = blocks.pop() ?? "";
953
- const events = blocks.flatMap((block) => {
954
- const dataLines = block.split("\n")
955
- .filter((line) => line.startsWith("data:"))
956
- .map((line) => line.slice(5).trimStart());
957
- if (!dataLines.length) {
958
- return [];
959
- }
960
- const payload = dataLines.join("\n").trim();
961
- if (payload === "[DONE]") {
962
- return ["[DONE]"];
963
- }
964
- try {
965
- return [JSON.parse(payload)];
966
- }
967
- catch {
968
- return [];
969
- }
970
- });
971
- return { events, remainder };
972
- }
973
950
  async function* streamOpenAICompatibleParts(stream) {
974
951
  const decoder = new TextDecoder();
975
952
  let buffer = "";
@@ -978,7 +955,7 @@ async function* streamOpenAICompatibleParts(stream) {
978
955
  let usage;
979
956
  for await (const chunk of stream) {
980
957
  buffer += decoder.decode(chunk, { stream: true });
981
- const parsed = parseOpenAISseChunk(buffer);
958
+ const parsed = parseSseChunk(buffer);
982
959
  buffer = parsed.remainder;
983
960
  for (const event of parsed.events) {
984
961
  if (event === "[DONE]") {
@@ -1040,7 +1017,7 @@ async function* streamOpenAICompatibleParts(stream) {
1040
1017
  }
1041
1018
  }
1042
1019
  if (buffer.trim().length > 0) {
1043
- const parsed = parseOpenAISseChunk(`${buffer}\n\n`);
1020
+ const parsed = parseSseChunk(`${buffer}\n\n`);
1044
1021
  for (const event of parsed.events) {
1045
1022
  if (event === "[DONE]") {
1046
1023
  continue;
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "0.1.150";
1
+ export declare const VERSION = "0.1.152";
2
2
  //# sourceMappingURL=version-constant.d.ts.map
@@ -1,3 +1,3 @@
1
1
  // Keep in sync with deno.json version.
2
2
  // scripts/release.ts updates this constant during releases.
3
- export const VERSION = "0.1.150";
3
+ export const VERSION = "0.1.152";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "veryfront",
3
- "version": "0.1.150",
3
+ "version": "0.1.152",
4
4
  "description": "The simplest way to build AI-powered apps",
5
5
  "keywords": [
6
6
  "react",
package/src/deno.js CHANGED
@@ -1,6 +1,6 @@
1
1
  export default {
2
2
  "name": "veryfront",
3
- "version": "0.1.150",
3
+ "version": "0.1.152",
4
4
  "license": "Apache-2.0",
5
5
  "nodeModulesDir": "auto",
6
6
  "exclude": [
@@ -288,6 +288,12 @@ async function createAgUiStreamResponse(
288
288
  if (!enqueueEvent(controller, "RunStarted", { runId, threadId, agentId: agent.id })) {
289
289
  return;
290
290
  }
291
+ if (!enqueueEvent(controller, "StateSnapshot", { snapshot: {} })) {
292
+ return;
293
+ }
294
+ if (!enqueueEvent(controller, "MessagesSnapshot", { messages: request.messages })) {
295
+ return;
296
+ }
291
297
 
292
298
  try {
293
299
  if (!upstreamBody) {
@@ -135,6 +135,16 @@ export {
135
135
  } from "./composition/index.js";
136
136
 
137
137
  export { agent } from "./factory.js";
138
+ export {
139
+ type AgUiRuntimeContextItem,
140
+ AgUiRuntimeContextItemSchema,
141
+ type AgUiRuntimeInjectedTool,
142
+ AgUiRuntimeInjectedToolSchema,
143
+ type AgUiRuntimeMessage,
144
+ AgUiRuntimeMessageSchema,
145
+ type AgUiRuntimeRequest,
146
+ AgUiRuntimeRequestSchema,
147
+ } from "./runtime-ag-ui-contract.js";
138
148
  export {
139
149
  type AgUiContextItem,
140
150
  type AgUiHandlerConfigWithAgent,
@@ -0,0 +1,144 @@
1
+ import { z } from "zod";
2
+
3
+ const AGENT_ID_PATTERN = /^[a-zA-Z0-9_-]+$/;
4
+ const MAX_TOOL_PARAMETERS_BYTES = 16_384;
5
+ const MAX_CONTEXT_ITEM_BYTES = 16_384;
6
+ const MAX_CONTEXT_TOTAL_BYTES = 65_536;
7
+ const MAX_FORWARDED_PROPS_BYTES = 65_536;
8
+ const MAX_RUNTIME_MESSAGES = 100;
9
+
10
+ const encoder = new TextEncoder();
11
+
12
+ function isWithinJsonSizeLimit(value: unknown, maxBytes: number): boolean {
13
+ try {
14
+ return encoder.encode(JSON.stringify(value)).byteLength <= maxBytes;
15
+ } catch {
16
+ return false;
17
+ }
18
+ }
19
+
20
+ export const AgUiRuntimeRunIdSchema = z.string().min(1).max(128).regex(AGENT_ID_PATTERN);
21
+
22
+ export const AgUiRuntimeInjectedToolSchema = z.object({
23
+ name: z
24
+ .string()
25
+ .min(1)
26
+ .max(128)
27
+ .regex(
28
+ /^[a-zA-Z][a-zA-Z0-9._:-]*$/,
29
+ "Tool names must start with a letter and use a valid client-tool format",
30
+ ),
31
+ description: z.string().max(1024).optional(),
32
+ parameters: z.record(z.string(), z.unknown()).optional().refine(
33
+ (value) => value === undefined || isWithinJsonSizeLimit(value, MAX_TOOL_PARAMETERS_BYTES),
34
+ { message: "Tool parameters must be less than 16 KB" },
35
+ ),
36
+ });
37
+
38
+ export const AgUiRuntimeContextItemSchema = z.discriminatedUnion("type", [
39
+ z.object({
40
+ type: z.literal("text"),
41
+ title: z.string().max(256).optional(),
42
+ text: z.string().max(MAX_CONTEXT_ITEM_BYTES),
43
+ }),
44
+ z.object({
45
+ type: z.literal("json"),
46
+ title: z.string().max(256).optional(),
47
+ data: z.record(z.string(), z.unknown()).refine(
48
+ (value) => isWithinJsonSizeLimit(value, MAX_CONTEXT_ITEM_BYTES),
49
+ { message: "JSON context item must be less than 16 KB" },
50
+ ),
51
+ }),
52
+ z.object({
53
+ type: z.literal("resource"),
54
+ title: z.string().max(256).optional(),
55
+ uri: z.string().max(2048),
56
+ mimeType: z.string().max(256).optional(),
57
+ text: z.string().max(MAX_CONTEXT_ITEM_BYTES).optional(),
58
+ }),
59
+ ]);
60
+
61
+ const RuntimeMessageExtensionFieldsSchema = {
62
+ name: z.string().max(256).optional(),
63
+ metadata: z.record(z.string(), z.unknown()).optional(),
64
+ createdAt: z.string().optional(),
65
+ } as const;
66
+
67
+ export const AgUiRuntimeToolFunctionCallSchema = z.object({
68
+ name: AgUiRuntimeInjectedToolSchema.shape.name,
69
+ arguments: z.string().max(MAX_TOOL_PARAMETERS_BYTES),
70
+ }).strict();
71
+
72
+ export const AgUiRuntimeToolCallSchema = z.object({
73
+ id: z.string().min(1).max(128),
74
+ type: z.literal("function"),
75
+ function: AgUiRuntimeToolFunctionCallSchema,
76
+ }).strict();
77
+
78
+ export const AgUiRuntimeSystemMessageSchema = z.object({
79
+ id: z.string().min(1),
80
+ role: z.literal("system"),
81
+ content: z.string(),
82
+ ...RuntimeMessageExtensionFieldsSchema,
83
+ }).strict();
84
+
85
+ export const AgUiRuntimeUserMessageSchema = z.object({
86
+ id: z.string().min(1),
87
+ role: z.literal("user"),
88
+ content: z.string(),
89
+ ...RuntimeMessageExtensionFieldsSchema,
90
+ }).strict();
91
+
92
+ export const AgUiRuntimeAssistantMessageSchema = z.object({
93
+ id: z.string().min(1),
94
+ role: z.literal("assistant"),
95
+ content: z.string().optional(),
96
+ toolCalls: z.array(AgUiRuntimeToolCallSchema).optional(),
97
+ ...RuntimeMessageExtensionFieldsSchema,
98
+ }).strict();
99
+
100
+ export const AgUiRuntimeToolMessageSchema = z.object({
101
+ id: z.string().min(1),
102
+ role: z.literal("tool"),
103
+ toolCallId: z.string().min(1).max(128),
104
+ content: z.string(),
105
+ error: z.string().optional(),
106
+ ...RuntimeMessageExtensionFieldsSchema,
107
+ }).strict();
108
+
109
+ export const AgUiRuntimeMessageSchema = z.discriminatedUnion("role", [
110
+ AgUiRuntimeSystemMessageSchema,
111
+ AgUiRuntimeUserMessageSchema,
112
+ AgUiRuntimeAssistantMessageSchema,
113
+ AgUiRuntimeToolMessageSchema,
114
+ ]);
115
+
116
+ export const AgUiRuntimeContextSchema = z.union([
117
+ z.object({
118
+ description: z.string().max(1024),
119
+ value: z.string().max(MAX_CONTEXT_ITEM_BYTES),
120
+ }),
121
+ AgUiRuntimeContextItemSchema,
122
+ ]);
123
+
124
+ export const AgUiRuntimeRequestSchema = z.object({
125
+ threadId: z.string().uuid(),
126
+ runId: AgUiRuntimeRunIdSchema,
127
+ parentRunId: AgUiRuntimeRunIdSchema.optional(),
128
+ state: z.unknown().optional(),
129
+ messages: z.array(AgUiRuntimeMessageSchema).max(MAX_RUNTIME_MESSAGES),
130
+ tools: z.array(AgUiRuntimeInjectedToolSchema).max(50).default([]),
131
+ context: z.array(AgUiRuntimeContextSchema).max(10).default([]).refine(
132
+ (value) => isWithinJsonSizeLimit(value, MAX_CONTEXT_TOTAL_BYTES),
133
+ { message: "context must be less than 64 KB total" },
134
+ ),
135
+ forwardedProps: z.record(z.string(), z.unknown()).optional().refine(
136
+ (value) => value === undefined || isWithinJsonSizeLimit(value, MAX_FORWARDED_PROPS_BYTES),
137
+ { message: "forwardedProps must be less than 64 KB" },
138
+ ),
139
+ });
140
+
141
+ export type AgUiRuntimeInjectedTool = z.infer<typeof AgUiRuntimeInjectedToolSchema>;
142
+ export type AgUiRuntimeContextItem = z.infer<typeof AgUiRuntimeContextItemSchema>;
143
+ export type AgUiRuntimeMessage = z.infer<typeof AgUiRuntimeMessageSchema>;
144
+ export type AgUiRuntimeRequest = z.infer<typeof AgUiRuntimeRequestSchema>;
@@ -41,6 +41,18 @@ const agUiEventPayloadSchemas = {
41
41
  threadId: z.string().min(1),
42
42
  agentId: z.string().min(1),
43
43
  }),
44
+ StateSnapshot: z.object({
45
+ snapshot: z.record(z.string(), z.unknown()),
46
+ }),
47
+ MessagesSnapshot: z.object({
48
+ messages: z.array(z.object({
49
+ id: z.string().min(1),
50
+ role: z.enum(["user", "assistant", "system", "tool"]),
51
+ parts: z.array(z.record(z.string(), z.unknown())),
52
+ metadata: z.record(z.string(), z.unknown()).optional(),
53
+ createdAt: z.string().optional(),
54
+ })),
55
+ }),
44
56
  TextMessageStart: z.object({
45
57
  messageId: z.string().min(1),
46
58
  role: z.literal("assistant"),
@@ -1,9 +1,17 @@
1
1
  import { z } from "zod";
2
+ import {
3
+ AgUiRuntimeContextItemSchema,
4
+ AgUiRuntimeContextSchema,
5
+ AgUiRuntimeInjectedToolSchema,
6
+ type AgUiRuntimeMessage,
7
+ AgUiRuntimeMessageSchema,
8
+ type AgUiRuntimeRequest,
9
+ AgUiRuntimeRequestSchema,
10
+ AgUiRuntimeRunIdSchema,
11
+ AgUiRuntimeToolCallSchema,
12
+ } from "../agent/runtime-ag-ui-contract.js";
2
13
 
3
14
  const AGENT_ID_PATTERN = /^[a-zA-Z0-9_-]+$/;
4
- const MAX_TOOL_PARAMETERS_BYTES = 16_384;
5
- const MAX_CONTEXT_ITEM_BYTES = 16_384;
6
- const MAX_CONTEXT_TOTAL_BYTES = 65_536;
7
15
  const MAX_FORWARDED_PROPS_BYTES = 65_536;
8
16
  const MAX_TOOL_RESULT_BYTES = 65_536;
9
17
  const MAX_RUNTIME_MESSAGES = 100;
@@ -18,51 +26,10 @@ function isWithinJsonSizeLimit(value: unknown, maxBytes: number): boolean {
18
26
  }
19
27
  }
20
28
 
21
- export const RunIdSchema = z.string().min(1).max(128).regex(AGENT_ID_PATTERN);
29
+ export const RunIdSchema = AgUiRuntimeRunIdSchema;
22
30
 
23
31
  export const AgentIdSchema = z.string().min(1).max(128).regex(AGENT_ID_PATTERN);
24
32
 
25
- export const ClientToolNameSchema = z
26
- .string()
27
- .min(1)
28
- .max(128)
29
- .regex(
30
- /^[a-zA-Z][a-zA-Z0-9._:-]*$/,
31
- "Tool names must start with a letter and use a valid client-tool format",
32
- );
33
-
34
- export const RuntimeInjectedToolSchema = z.object({
35
- name: ClientToolNameSchema,
36
- description: z.string().max(1024).optional(),
37
- parameters: z.record(z.string(), z.unknown()).optional().refine(
38
- (value) => value === undefined || isWithinJsonSizeLimit(value, MAX_TOOL_PARAMETERS_BYTES),
39
- { message: "Tool parameters must be less than 16 KB" },
40
- ),
41
- });
42
-
43
- export const RuntimeContextItemSchema = z.discriminatedUnion("type", [
44
- z.object({
45
- type: z.literal("text"),
46
- title: z.string().max(256).optional(),
47
- text: z.string().max(MAX_CONTEXT_ITEM_BYTES),
48
- }),
49
- z.object({
50
- type: z.literal("json"),
51
- title: z.string().max(256).optional(),
52
- data: z.record(z.string(), z.unknown()).refine(
53
- (value) => isWithinJsonSizeLimit(value, MAX_CONTEXT_ITEM_BYTES),
54
- { message: "JSON context item must be less than 16 KB" },
55
- ),
56
- }),
57
- z.object({
58
- type: z.literal("resource"),
59
- title: z.string().max(256).optional(),
60
- uri: z.string().max(2048),
61
- mimeType: z.string().max(256).optional(),
62
- text: z.string().max(MAX_CONTEXT_ITEM_BYTES).optional(),
63
- }),
64
- ]);
65
-
66
33
  export const RuntimeAgentSourceContextSchema = z.discriminatedUnion("type", [
67
34
  z.object({
68
35
  type: z.literal("branch"),
@@ -79,85 +46,11 @@ export const RuntimeAgentSourceContextSchema = z.discriminatedUnion("type", [
79
46
  }),
80
47
  ]);
81
48
 
82
- const RuntimeMessageExtensionFieldsSchema = {
83
- name: z.string().max(256).optional(),
84
- metadata: z.record(z.string(), z.unknown()).optional(),
85
- createdAt: z.string().optional(),
86
- } as const;
87
-
88
- export const RuntimeToolFunctionCallSchema = z.object({
89
- name: ClientToolNameSchema,
90
- arguments: z.string().max(MAX_TOOL_PARAMETERS_BYTES),
91
- }).strict();
92
-
93
- export const RuntimeToolCallSchema = z.object({
94
- id: z.string().min(1).max(128),
95
- type: z.literal("function"),
96
- function: RuntimeToolFunctionCallSchema,
97
- }).strict();
98
-
99
- export const RuntimeSystemMessageSchema = z.object({
100
- id: z.string().min(1),
101
- role: z.literal("system"),
102
- content: z.string(),
103
- ...RuntimeMessageExtensionFieldsSchema,
104
- }).strict();
105
-
106
- export const RuntimeUserMessageSchema = z.object({
107
- id: z.string().min(1),
108
- role: z.literal("user"),
109
- content: z.string(),
110
- ...RuntimeMessageExtensionFieldsSchema,
111
- }).strict();
112
-
113
- export const RuntimeAssistantMessageSchema = z.object({
114
- id: z.string().min(1),
115
- role: z.literal("assistant"),
116
- content: z.string().optional(),
117
- toolCalls: z.array(RuntimeToolCallSchema).optional(),
118
- ...RuntimeMessageExtensionFieldsSchema,
119
- }).strict();
120
-
121
- export const RuntimeToolMessageSchema = z.object({
122
- id: z.string().min(1),
123
- role: z.literal("tool"),
124
- toolCallId: z.string().min(1).max(128),
125
- content: z.string(),
126
- error: z.string().optional(),
127
- ...RuntimeMessageExtensionFieldsSchema,
128
- }).strict();
129
-
130
- export const RuntimeMessageSchema = z.discriminatedUnion("role", [
131
- RuntimeSystemMessageSchema,
132
- RuntimeUserMessageSchema,
133
- RuntimeAssistantMessageSchema,
134
- RuntimeToolMessageSchema,
135
- ]);
136
-
137
- export const RuntimeContextSchema = z.union([
138
- z.object({
139
- description: z.string().max(1024),
140
- value: z.string().max(MAX_CONTEXT_ITEM_BYTES),
141
- }),
142
- RuntimeContextItemSchema,
143
- ]);
144
-
145
- export const RuntimeRunAgentInputSchema = z.object({
146
- threadId: z.string().uuid(),
147
- runId: RunIdSchema,
148
- parentRunId: RunIdSchema.optional(),
149
- state: z.unknown().optional(),
150
- messages: z.array(RuntimeMessageSchema).max(MAX_RUNTIME_MESSAGES),
151
- tools: z.array(RuntimeInjectedToolSchema).max(50).default([]),
152
- context: z.array(RuntimeContextSchema).max(10).default([]).refine(
153
- (value) => isWithinJsonSizeLimit(value, MAX_CONTEXT_TOTAL_BYTES),
154
- { message: "context must be less than 64 KB total" },
155
- ),
156
- forwardedProps: z.record(z.string(), z.unknown()).optional().refine(
157
- (value) => value === undefined || isWithinJsonSizeLimit(value, MAX_FORWARDED_PROPS_BYTES),
158
- { message: "forwardedProps must be less than 64 KB" },
159
- ),
160
- });
49
+ export const RuntimeInjectedToolSchema = AgUiRuntimeInjectedToolSchema;
50
+ export const RuntimeContextItemSchema = AgUiRuntimeContextItemSchema;
51
+ export const RuntimeMessageSchema = AgUiRuntimeMessageSchema;
52
+ export const RuntimeContextSchema = AgUiRuntimeContextSchema;
53
+ export const RuntimeRunAgentInputSchema = AgUiRuntimeRequestSchema;
161
54
 
162
55
  export const InternalAgentCompatibilityMessageSchema = z.object({
163
56
  id: z.string().min(1),
@@ -178,7 +71,7 @@ export const InternalAgentStreamRequestSchema = z.object({
178
71
  ),
179
72
  tools: z.array(RuntimeInjectedToolSchema).max(50).default([]),
180
73
  context: z.array(RuntimeContextSchema).max(10).default([]).refine(
181
- (value) => isWithinJsonSizeLimit(value, MAX_CONTEXT_TOTAL_BYTES),
74
+ (value) => isWithinJsonSizeLimit(value, 65_536),
182
75
  { message: "context must be less than 64 KB total" },
183
76
  ),
184
77
  agentSource: RuntimeAgentSourceContextSchema.optional(),
@@ -188,7 +81,7 @@ export const InternalAgentStreamRequestSchema = z.object({
188
81
  ),
189
82
  });
190
83
 
191
- type RuntimeMessage = z.infer<typeof RuntimeMessageSchema>;
84
+ type RuntimeMessage = AgUiRuntimeMessage;
192
85
  type InternalAgentCompatibilityMessage = z.infer<typeof InternalAgentCompatibilityMessageSchema>;
193
86
 
194
87
  function extractToolArgs(
@@ -243,7 +136,7 @@ function isCanonicalToolCallPart(part: Record<string, unknown>): boolean {
243
136
 
244
137
  function getToolCallShape(
245
138
  part: Record<string, unknown>,
246
- ): z.infer<typeof RuntimeToolCallSchema> | null {
139
+ ): z.infer<typeof AgUiRuntimeToolCallSchema> | null {
247
140
  const id = getPartString(part, "toolCallId", "tool_call_id", "id");
248
141
  const name = getPartString(part, "toolName", "tool_name", "name");
249
142
 
@@ -357,7 +250,7 @@ function toRuntimeMessage(
357
250
 
358
251
  export function toRuntimeRunAgentInput(
359
252
  input: z.infer<typeof InternalAgentStreamRequestSchema>,
360
- ): z.infer<typeof RuntimeRunAgentInputSchema> {
253
+ ): AgUiRuntimeRequest {
361
254
  return {
362
255
  threadId: input.threadId,
363
256
  runId: input.runId,
@@ -385,6 +278,6 @@ export const ResumeSignalSchema = z.discriminatedUnion("type", [
385
278
  export type RuntimeInjectedTool = z.infer<typeof RuntimeInjectedToolSchema>;
386
279
  export type RuntimeContextItem = z.infer<typeof RuntimeContextItemSchema>;
387
280
  export type RuntimeAgentSourceContext = z.infer<typeof RuntimeAgentSourceContextSchema>;
388
- export type RuntimeRunAgentInput = z.infer<typeof RuntimeRunAgentInputSchema>;
281
+ export type RuntimeRunAgentInput = AgUiRuntimeRequest;
389
282
  export type InternalAgentStreamRequest = z.infer<typeof InternalAgentStreamRequestSchema>;
390
283
  export type ResumeSignal = z.infer<typeof ResumeSignalSchema>;