veryfront 0.1.219 → 0.1.221

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  export default {
2
2
  "name": "veryfront",
3
- "version": "0.1.219",
3
+ "version": "0.1.221",
4
4
  "license": "Apache-2.0",
5
5
  "nodeModulesDir": "auto",
6
6
  "workspace": [
@@ -55,10 +55,18 @@ export declare const AgUiDetachedStartAcceptedSchema: z.ZodObject<{
55
55
  }, z.core.$strip>;
56
56
  export type AgUiDetachedStartRequest = z.infer<typeof AgUiDetachedStartRequestSchema>;
57
57
  export type AgUiDetachedStartAccepted = z.infer<typeof AgUiDetachedStartAcceptedSchema>;
58
- export interface AgUiDetachedStartHandlerOptions {
59
- agent: Agent;
58
+ interface AgUiDetachedStartExecutionInput {
59
+ request: AgUiDetachedStartRequest;
60
+ requestOrCtx: unknown;
61
+ rawRequest: Request;
62
+ context: Record<string, unknown>;
63
+ abortSignal: AbortSignal;
64
+ }
65
+ type AgUiDetachedExecutionStarter = (input: AgUiDetachedStartExecutionInput) => Promise<void> | void;
66
+ interface AgUiDetachedStartHandlerOptionsBase {
60
67
  sessionManager: RunResumeSessionManager<AgUiResumeValue>;
61
68
  context?: AgUiContextValue;
69
+ startDetachedExecution?: AgUiDetachedExecutionStarter;
62
70
  onAccepted?: (input: {
63
71
  request: AgUiDetachedStartRequest;
64
72
  runId: string;
@@ -79,6 +87,12 @@ export interface AgUiDetachedStartHandlerOptions {
79
87
  error: unknown;
80
88
  }) => Promise<void> | void;
81
89
  }
90
+ export type AgUiDetachedStartHandlerOptions = (AgUiDetachedStartHandlerOptionsBase & {
91
+ agent: Agent;
92
+ }) | (AgUiDetachedStartHandlerOptionsBase & {
93
+ agent?: undefined;
94
+ startDetachedExecution: AgUiDetachedExecutionStarter;
95
+ });
82
96
  export declare function createAgUiDetachedStartHandler(options: AgUiDetachedStartHandlerOptions): (requestOrCtx: unknown) => Promise<Response>;
83
97
  export {};
84
98
  //# sourceMappingURL=ag-ui-detached-start.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ag-ui-detached-start.d.ts","sourceRoot":"","sources":["../../../src/src/agent/ag-ui-detached-start.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAMxB,OAAO,EAGL,KAAK,uBAAuB,EAC7B,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,KAAK,EAAW,MAAM,YAAY,CAAC;AAMjD,KAAK,eAAe,GAAG;IACrB,MAAM,EAAE,OAAO,CAAC;IAChB,OAAO,EAAE,OAAO,CAAC;CAClB,CAAC;AAEF,KAAK,gBAAgB,GACjB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACvB,CAAC,CAAC,OAAO,EAAE,OAAO,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;AAsOvF,eAAO,MAAM,8BAA8B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAGzC,CAAC;AAEH,eAAO,MAAM,+BAA+B;;;;;iBAK1C,CAAC;AAEH,MAAM,MAAM,wBAAwB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,8BAA8B,CAAC,CAAC;AACtF,MAAM,MAAM,yBAAyB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,+BAA+B,CAAC,CAAC;AAExF,MAAM,WAAW,+BAA+B;IAC9C,KAAK,EAAE,KAAK,CAAC;IACb,cAAc,EAAE,uBAAuB,CAAC,eAAe,CAAC,CAAC;IACzD,OAAO,CAAC,EAAE,gBAAgB,CAAC;IAC3B,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE;QACnB,OAAO,EAAE,wBAAwB,CAAC;QAClC,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE,MAAM,CAAC;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AC,KAAK,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IAChF,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,OAAO,CAAA;KAAE,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;CAChG;AAED,MAAM,MAAM,+BAA+B,GACvC,CAAC,mCAAmC,GAAG;IAAE,KAAK,EAAE,KAAK,CAAA;CAAE,CAAC,GACxD,CAAC,mCAAmC,GAAG;IACvC,KAAK,CAAC,EAAE,SAAS,CAAC;IAClB,sBAAsB,EAAE,4BAA4B,CAAC;CACtD,CAAC,CAAC;AA0BL,wBAAgB,8BAA8B,CAC5C,OAAO,EAAE,+BAA+B,GACvC,CAAC,YAAY,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAwH9C"}
@@ -3,85 +3,10 @@ import { INVALID_ARGUMENT } from "../errors/index.js";
3
3
  import { SKILL_TOOL_IDS } from "../skill/types.js";
4
4
  import { toolRegistry } from "../tool/index.js";
5
5
  import { streamDataStreamEvents } from "./data-stream.js";
6
- import { AgUiRequestSchema } from "./ag-ui-handler.js";
6
+ import { AgUiRequestSchema, normalizeAgUiMessages, } from "./ag-ui-host-support.js";
7
7
  import { AgentRuntime, RunAlreadyExistsError, } from "./runtime/index.js";
8
8
  const AGENT_ID_PATTERN = /^[a-zA-Z0-9_-]+$/;
9
9
  const AG_UI_DETACHED_RUN_ID_SCHEMA = z.string().min(1).max(128).regex(AGENT_ID_PATTERN);
10
- const MAX_TEXT_PART_LENGTH = 10_000;
11
- function isRecord(value) {
12
- return typeof value === "object" && value !== null && !Array.isArray(value);
13
- }
14
- function normalizeToolArgs(part) {
15
- if (isRecord(part.args))
16
- return part.args;
17
- if (isRecord(part.input))
18
- return part.input;
19
- return {};
20
- }
21
- function normalizeMessagePart(part) {
22
- if (part.type === "text" && typeof part.text === "string" &&
23
- part.text.length <= MAX_TEXT_PART_LENGTH) {
24
- return { type: "text", text: part.text };
25
- }
26
- if (part.type === "tool_call" && typeof part.id === "string" && typeof part.name === "string") {
27
- return {
28
- type: "tool-call",
29
- toolCallId: part.id,
30
- toolName: part.name,
31
- args: normalizeToolArgs(part),
32
- };
33
- }
34
- if (part.type === "tool-call" &&
35
- typeof part.toolCallId === "string" &&
36
- typeof part.toolName === "string") {
37
- return {
38
- type: "tool-call",
39
- toolCallId: part.toolCallId,
40
- toolName: part.toolName,
41
- args: normalizeToolArgs(part),
42
- };
43
- }
44
- if (typeof part.type === "string" &&
45
- part.type.startsWith("tool-") &&
46
- part.type !== "tool-result" &&
47
- typeof part.toolCallId === "string" &&
48
- typeof part.toolName === "string") {
49
- return {
50
- type: part.type,
51
- toolCallId: part.toolCallId,
52
- toolName: part.toolName,
53
- args: normalizeToolArgs(part),
54
- };
55
- }
56
- if (part.type === "tool_result" && typeof part.tool_call_id === "string") {
57
- return {
58
- type: "tool-result",
59
- toolCallId: part.tool_call_id,
60
- toolName: typeof part.tool_name === "string" ? part.tool_name : "unknown",
61
- result: "output" in part ? part.output : undefined,
62
- };
63
- }
64
- if (part.type === "tool-result" && typeof part.toolCallId === "string") {
65
- return {
66
- type: "tool-result",
67
- toolCallId: part.toolCallId,
68
- toolName: typeof part.toolName === "string" ? part.toolName : "unknown",
69
- result: "result" in part ? part.result : undefined,
70
- };
71
- }
72
- return null;
73
- }
74
- function normalizeMessages(messages) {
75
- return messages.map((message) => ({
76
- id: message.id,
77
- role: message.role,
78
- parts: message.parts
79
- .map((part) => normalizeMessagePart(part))
80
- .filter((part) => part !== null),
81
- ...(message.createdAt ? { timestamp: Date.parse(message.createdAt) || undefined } : {}),
82
- ...(message.metadata ? { metadata: message.metadata } : {}),
83
- }));
84
- }
85
10
  function isRequest(obj) {
86
11
  return (typeof obj === "object" &&
87
12
  obj !== null &&
@@ -190,7 +115,18 @@ export const AgUiDetachedStartAcceptedSchema = z.object({
190
115
  runId: AG_UI_DETACHED_RUN_ID_SCHEMA,
191
116
  threadId: z.string().uuid(),
192
117
  });
118
+ async function startDefaultDetachedExecution(input) {
119
+ const runtime = new AgentRuntime(input.agent.id, {
120
+ ...input.agent.config,
121
+ tools: buildMergedTools(input.agent, input.request, input.sessionManager),
122
+ });
123
+ const runtimeStream = await runtime.stream(normalizeAgUiMessages(input.request.messages), buildStreamContext(input.request, input.context, input.request.threadId, input.request.runId), undefined, input.request.model, input.request.maxOutputTokens, input.abortSignal);
124
+ await drainRuntimeStream(runtimeStream);
125
+ }
193
126
  export function createAgUiDetachedStartHandler(options) {
127
+ if (!options.agent && !options.startDetachedExecution) {
128
+ throw new Error("Detached AG-UI start requires either an agent or startDetachedExecution handler.");
129
+ }
194
130
  return async function POST(requestOrCtx) {
195
131
  const request = extractRequest(requestOrCtx);
196
132
  try {
@@ -201,11 +137,6 @@ export function createAgUiDetachedStartHandler(options) {
201
137
  runId: parsed.runId,
202
138
  threadId: parsed.threadId,
203
139
  });
204
- const runtime = new AgentRuntime(options.agent.id, {
205
- ...options.agent.config,
206
- tools: buildMergedTools(options.agent, parsed, options.sessionManager),
207
- });
208
- const runtimeStream = await runtime.stream(normalizeMessages(parsed.messages), buildStreamContext(parsed, context, parsed.threadId, parsed.runId), undefined, parsed.model, parsed.maxOutputTokens, abortSignal);
209
140
  await options.onAccepted?.({
210
141
  request: parsed,
211
142
  runId: parsed.runId,
@@ -213,7 +144,27 @@ export function createAgUiDetachedStartHandler(options) {
213
144
  });
214
145
  const detachedTask = (async () => {
215
146
  try {
216
- await drainRuntimeStream(runtimeStream);
147
+ if (options.startDetachedExecution) {
148
+ await options.startDetachedExecution({
149
+ request: parsed,
150
+ requestOrCtx,
151
+ rawRequest: request,
152
+ context,
153
+ abortSignal,
154
+ });
155
+ }
156
+ else if (options.agent) {
157
+ await startDefaultDetachedExecution({
158
+ agent: options.agent,
159
+ request: parsed,
160
+ context,
161
+ abortSignal,
162
+ sessionManager: options.sessionManager,
163
+ });
164
+ }
165
+ else {
166
+ throw new Error("Detached AG-UI start configuration became invalid during execution.");
167
+ }
217
168
  options.sessionManager.completeRun(parsed.runId);
218
169
  await options.onFinish?.({
219
170
  runId: parsed.runId,
@@ -1,70 +1,6 @@
1
- import { z } from "zod";
2
1
  import type { Agent } from "./types.js";
3
2
  import { type RunResumeSessionManager } from "./runtime/index.js";
4
- declare const AgUiInjectedToolSchema: z.ZodObject<{
5
- name: z.ZodString;
6
- description: z.ZodOptional<z.ZodString>;
7
- parameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
8
- }, z.core.$strip>;
9
- declare const AgUiContextItemSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
10
- type: z.ZodLiteral<"text">;
11
- title: z.ZodOptional<z.ZodString>;
12
- text: z.ZodString;
13
- }, z.core.$strip>, z.ZodObject<{
14
- type: z.ZodLiteral<"json">;
15
- title: z.ZodOptional<z.ZodString>;
16
- data: z.ZodRecord<z.ZodString, z.ZodUnknown>;
17
- }, z.core.$strip>, z.ZodObject<{
18
- type: z.ZodLiteral<"resource">;
19
- title: z.ZodOptional<z.ZodString>;
20
- uri: z.ZodString;
21
- mimeType: z.ZodOptional<z.ZodString>;
22
- text: z.ZodOptional<z.ZodString>;
23
- }, z.core.$strip>], "type">;
24
- export declare const AgUiRequestSchema: z.ZodObject<{
25
- threadId: z.ZodOptional<z.ZodString>;
26
- runId: z.ZodOptional<z.ZodString>;
27
- messages: z.ZodArray<z.ZodObject<{
28
- id: z.ZodString;
29
- role: z.ZodEnum<{
30
- tool: "tool";
31
- user: "user";
32
- assistant: "assistant";
33
- system: "system";
34
- }>;
35
- parts: z.ZodDefault<z.ZodArray<z.ZodObject<{
36
- type: z.ZodString;
37
- }, z.core.$loose>>>;
38
- metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
39
- createdAt: z.ZodOptional<z.ZodString>;
40
- }, z.core.$strip>>;
41
- tools: z.ZodDefault<z.ZodArray<z.ZodObject<{
42
- name: z.ZodString;
43
- description: z.ZodOptional<z.ZodString>;
44
- parameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
45
- }, z.core.$strip>>>;
46
- context: z.ZodDefault<z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
47
- type: z.ZodLiteral<"text">;
48
- title: z.ZodOptional<z.ZodString>;
49
- text: z.ZodString;
50
- }, z.core.$strip>, z.ZodObject<{
51
- type: z.ZodLiteral<"json">;
52
- title: z.ZodOptional<z.ZodString>;
53
- data: z.ZodRecord<z.ZodString, z.ZodUnknown>;
54
- }, z.core.$strip>, z.ZodObject<{
55
- type: z.ZodLiteral<"resource">;
56
- title: z.ZodOptional<z.ZodString>;
57
- uri: z.ZodString;
58
- mimeType: z.ZodOptional<z.ZodString>;
59
- text: z.ZodOptional<z.ZodString>;
60
- }, z.core.$strip>], "type">>>;
61
- forwardedProps: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
62
- model: z.ZodOptional<z.ZodString>;
63
- maxOutputTokens: z.ZodOptional<z.ZodNumber>;
64
- }, z.core.$strip>;
65
- export type AgUiInjectedTool = z.infer<typeof AgUiInjectedToolSchema>;
66
- export type AgUiContextItem = z.infer<typeof AgUiContextItemSchema>;
67
- export type AgUiRequest = z.infer<typeof AgUiRequestSchema>;
3
+ export { type AgUiContextItem, AgUiContextItemSchema, type AgUiInjectedTool, AgUiInjectedToolSchema, type AgUiRequest, AgUiRequestSchema, } from "./ag-ui-host-support.js";
68
4
  type AgUiResumeValue = {
69
5
  result: unknown;
70
6
  isError: boolean;
@@ -78,5 +14,4 @@ export interface AgUiHandlerConfigWithAgent extends AgUiHandlerOptions {
78
14
  }
79
15
  export declare function createAgUiHandler(agentId: string, options?: AgUiHandlerOptions): (requestOrCtx: unknown) => Promise<Response>;
80
16
  export declare function createAgUiHandler(config: AgUiHandlerConfigWithAgent, options?: AgUiHandlerOptions): (requestOrCtx: unknown) => Promise<Response>;
81
- export {};
82
17
  //# sourceMappingURL=ag-ui-handler.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ag-ui-handler.d.ts","sourceRoot":"","sources":["../../../src/src/agent/ag-ui-handler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,KAAK,EAAE,KAAK,EAAW,MAAM,YAAY,CAAC;AACjD,OAAO,EAGL,KAAK,uBAAuB,EAC7B,MAAM,oBAAoB,CAAC;AAsC5B,QAAA,MAAM,sBAAsB;;;;iBAO1B,CAAC;AAEH,QAAA,MAAM,qBAAqB;;;;;;;;;;;;;;2BAqBzB,CAAC;AAYH,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAe5B,CAAC;AAEH,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,sBAAsB,CAAC,CAAC;AACtE,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,qBAAqB,CAAC,CAAC;AACpE,MAAM,MAAM,WAAW,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,iBAAiB,CAAC,CAAC;AAG5D,KAAK,eAAe,GAAG;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC;AAyY7D,MAAM,WAAW,kBAAkB;IACjC,OAAO,CAAC,EACJ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACvB,CAAC,CAAC,OAAO,EAAE,OAAO,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IACvF,cAAc,CAAC,EAAE,uBAAuB,CAAC,eAAe,CAAC,CAAC;CAC3D;AAED,MAAM,W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M,EACf,OAAO,CAAC,EAAE,kBAAkB,GAC3B,CAAC,YAAY,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;AAChD,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,0BAA0B,EAClC,OAAO,CAAC,EAAE,kBAAkB,GAC3B,CAAC,YAAY,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC"}
@@ -7,144 +7,13 @@ import { SKILL_TOOL_IDS } from "../skill/types.js";
7
7
  import { toolRegistry } from "../tool/index.js";
8
8
  import { createStreamTransformState, finalizeRunEvents, formatAgUiEvent, mapRuntimeEventToAgUi, } from "../internal-agents/ag-ui-sse.js";
9
9
  import { streamDataStreamEvents } from "./data-stream.js";
10
- const AGENT_ID_PATTERN = /^[a-zA-Z0-9_-]+$/;
11
- const MAX_TOOL_PARAMETERS_BYTES = 16_384;
12
- const MAX_CONTEXT_ITEM_BYTES = 16_384;
13
- const MAX_CONTEXT_TOTAL_BYTES = 65_536;
14
- const MAX_FORWARDED_PROPS_BYTES = 65_536;
15
- const MAX_TEXT_PART_LENGTH = 10_000;
16
- const MAX_MESSAGES_PER_REQUEST = 100;
17
- const encoder = new TextEncoder();
10
+ import { normalizeAgUiMessages, parseAgUiRequestOrError, } from "./ag-ui-host-support.js";
11
+ export { AgUiContextItemSchema, AgUiInjectedToolSchema, AgUiRequestSchema, } from "./ag-ui-host-support.js";
18
12
  const AG_UI_HEADERS = {
19
13
  "Content-Type": "text/event-stream",
20
14
  "Cache-Control": "no-cache",
21
15
  Connection: "keep-alive",
22
16
  };
23
- function isWithinJsonSizeLimit(value, maxBytes) {
24
- try {
25
- return encoder.encode(JSON.stringify(value)).byteLength <= maxBytes;
26
- }
27
- catch {
28
- return false;
29
- }
30
- }
31
- const AgUiRunIdSchema = z.string().min(1).max(128).regex(AGENT_ID_PATTERN);
32
- const AgUiInjectedToolSchema = z.object({
33
- name: z.string().min(1).max(128),
34
- description: z.string().max(1024).optional(),
35
- 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" }),
36
- });
37
- const AgUiContextItemSchema = z.discriminatedUnion("type", [
38
- z.object({
39
- type: z.literal("text"),
40
- title: z.string().max(256).optional(),
41
- text: z.string().max(MAX_CONTEXT_ITEM_BYTES),
42
- }),
43
- z.object({
44
- type: z.literal("json"),
45
- title: z.string().max(256).optional(),
46
- 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" }),
47
- }),
48
- z.object({
49
- type: z.literal("resource"),
50
- title: z.string().max(256).optional(),
51
- uri: z.string().max(2048),
52
- mimeType: z.string().max(256).optional(),
53
- text: z.string().max(MAX_CONTEXT_ITEM_BYTES).optional(),
54
- }),
55
- ]);
56
- const AgUiMessagePartSchema = z.object({ type: z.string().min(1) }).passthrough();
57
- const AgUiMessageSchema = z.object({
58
- id: z.string().min(1),
59
- role: z.enum(["user", "assistant", "system", "tool"]),
60
- parts: z.array(AgUiMessagePartSchema).default([]),
61
- metadata: z.record(z.string(), z.unknown()).optional(),
62
- createdAt: z.string().optional(),
63
- });
64
- export const AgUiRequestSchema = z.object({
65
- threadId: z.string().uuid().optional(),
66
- runId: AgUiRunIdSchema.optional(),
67
- messages: z.array(AgUiMessageSchema).min(1).max(MAX_MESSAGES_PER_REQUEST),
68
- tools: z.array(AgUiInjectedToolSchema).max(50).default([]),
69
- context: z.array(AgUiContextItemSchema).max(10).default([]).refine((value) => isWithinJsonSizeLimit(value, MAX_CONTEXT_TOTAL_BYTES), { message: "context must be less than 64 KB total" }),
70
- 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" }),
71
- model: z.string().optional(),
72
- maxOutputTokens: z.number().int().positive().optional(),
73
- });
74
- function isRecord(value) {
75
- return typeof value === "object" && value !== null && !Array.isArray(value);
76
- }
77
- function normalizeToolArgs(part) {
78
- if (isRecord(part.args))
79
- return part.args;
80
- if (isRecord(part.input))
81
- return part.input;
82
- return {};
83
- }
84
- function normalizeMessagePart(part) {
85
- if (part.type === "text" && typeof part.text === "string" &&
86
- part.text.length <= MAX_TEXT_PART_LENGTH) {
87
- return { type: "text", text: part.text };
88
- }
89
- if (part.type === "tool_call" && typeof part.id === "string" && typeof part.name === "string") {
90
- return {
91
- type: "tool-call",
92
- toolCallId: part.id,
93
- toolName: part.name,
94
- args: normalizeToolArgs(part),
95
- };
96
- }
97
- if (part.type === "tool-call" &&
98
- typeof part.toolCallId === "string" &&
99
- typeof part.toolName === "string") {
100
- return {
101
- type: "tool-call",
102
- toolCallId: part.toolCallId,
103
- toolName: part.toolName,
104
- args: normalizeToolArgs(part),
105
- };
106
- }
107
- if (typeof part.type === "string" &&
108
- part.type.startsWith("tool-") &&
109
- part.type !== "tool-result" &&
110
- typeof part.toolCallId === "string" &&
111
- typeof part.toolName === "string") {
112
- return {
113
- type: part.type,
114
- toolCallId: part.toolCallId,
115
- toolName: part.toolName,
116
- args: normalizeToolArgs(part),
117
- };
118
- }
119
- if (part.type === "tool_result" && typeof part.tool_call_id === "string") {
120
- return {
121
- type: "tool-result",
122
- toolCallId: part.tool_call_id,
123
- toolName: typeof part.tool_name === "string" ? part.tool_name : "unknown",
124
- result: "output" in part ? part.output : undefined,
125
- };
126
- }
127
- if (part.type === "tool-result" && typeof part.toolCallId === "string") {
128
- return {
129
- type: "tool-result",
130
- toolCallId: part.toolCallId,
131
- toolName: typeof part.toolName === "string" ? part.toolName : "unknown",
132
- result: "result" in part ? part.result : undefined,
133
- };
134
- }
135
- return null;
136
- }
137
- function normalizeMessages(messages) {
138
- return messages.map((message) => ({
139
- id: message.id,
140
- role: message.role,
141
- parts: message.parts
142
- .map((part) => normalizeMessagePart(part))
143
- .filter((part) => part !== null),
144
- ...(message.createdAt ? { timestamp: Date.parse(message.createdAt) || undefined } : {}),
145
- ...(message.metadata ? { metadata: message.metadata } : {}),
146
- }));
147
- }
148
17
  function isRequest(obj) {
149
18
  return (typeof obj === "object" &&
150
19
  obj !== null &&
@@ -270,7 +139,7 @@ async function createAgUiDirectStreamResponse(agent, request, baseContext) {
270
139
  const runId = request.runId ?? generateRunId();
271
140
  await agent.clearMemory();
272
141
  const result = await agent.stream({
273
- messages: normalizeMessages(request.messages),
142
+ messages: normalizeAgUiMessages(request.messages),
274
143
  context: buildStreamContext(request, baseContext, threadId, runId),
275
144
  ...(request.model ? { model: request.model } : {}),
276
145
  ...(request.maxOutputTokens ? { maxOutputTokens: request.maxOutputTokens } : {}),
@@ -339,7 +208,7 @@ async function createAgUiInjectedToolsStreamResponse(agent, request, baseContext
339
208
  });
340
209
  let upstreamBody;
341
210
  try {
342
- upstreamBody = await runtime.stream(normalizeMessages(request.messages), buildStreamContext(request, baseContext, threadId, runId), undefined, request.model, request.maxOutputTokens);
211
+ upstreamBody = await runtime.stream(normalizeAgUiMessages(request.messages), buildStreamContext(request, baseContext, threadId, runId), undefined, request.model, request.maxOutputTokens);
343
212
  }
344
213
  catch (error) {
345
214
  sessionManager.failRun(runId);
@@ -392,7 +261,10 @@ export function createAgUiHandler(agentIdOrConfig, options) {
392
261
  return Response.json({ error: "Agent not found" }, { status: 404 });
393
262
  }
394
263
  try {
395
- const parsed = AgUiRequestSchema.parse(await request.json());
264
+ const parsed = await parseAgUiRequestOrError(request);
265
+ if (parsed instanceof Response) {
266
+ return parsed;
267
+ }
396
268
  if (parsed.tools.length > 0) {
397
269
  if (!options?.sessionManager) {
398
270
  return Response.json({
@@ -0,0 +1,76 @@
1
+ import { z } from "zod";
2
+ import type { Message } from "./types.js";
3
+ export interface AgUiSseEvent {
4
+ event: string;
5
+ payload: Record<string, unknown>;
6
+ }
7
+ export declare const AgUiInjectedToolSchema: z.ZodObject<{
8
+ name: z.ZodString;
9
+ description: z.ZodOptional<z.ZodString>;
10
+ parameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
11
+ }, z.core.$strip>;
12
+ export declare const AgUiContextItemSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
13
+ type: z.ZodLiteral<"text">;
14
+ title: z.ZodOptional<z.ZodString>;
15
+ text: z.ZodString;
16
+ }, z.core.$strip>, z.ZodObject<{
17
+ type: z.ZodLiteral<"json">;
18
+ title: z.ZodOptional<z.ZodString>;
19
+ data: z.ZodRecord<z.ZodString, z.ZodUnknown>;
20
+ }, z.core.$strip>, z.ZodObject<{
21
+ type: z.ZodLiteral<"resource">;
22
+ title: z.ZodOptional<z.ZodString>;
23
+ uri: z.ZodString;
24
+ mimeType: z.ZodOptional<z.ZodString>;
25
+ text: z.ZodOptional<z.ZodString>;
26
+ }, z.core.$strip>], "type">;
27
+ export declare const AgUiRequestSchema: z.ZodObject<{
28
+ threadId: z.ZodOptional<z.ZodString>;
29
+ runId: z.ZodOptional<z.ZodString>;
30
+ messages: z.ZodArray<z.ZodObject<{
31
+ id: z.ZodString;
32
+ role: z.ZodEnum<{
33
+ tool: "tool";
34
+ user: "user";
35
+ assistant: "assistant";
36
+ system: "system";
37
+ }>;
38
+ parts: z.ZodDefault<z.ZodArray<z.ZodObject<{
39
+ type: z.ZodString;
40
+ }, z.core.$loose>>>;
41
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
42
+ createdAt: z.ZodOptional<z.ZodString>;
43
+ }, z.core.$strip>>;
44
+ tools: z.ZodDefault<z.ZodArray<z.ZodObject<{
45
+ name: z.ZodString;
46
+ description: z.ZodOptional<z.ZodString>;
47
+ parameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
48
+ }, z.core.$strip>>>;
49
+ context: z.ZodDefault<z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
50
+ type: z.ZodLiteral<"text">;
51
+ title: z.ZodOptional<z.ZodString>;
52
+ text: z.ZodString;
53
+ }, z.core.$strip>, z.ZodObject<{
54
+ type: z.ZodLiteral<"json">;
55
+ title: z.ZodOptional<z.ZodString>;
56
+ data: z.ZodRecord<z.ZodString, z.ZodUnknown>;
57
+ }, z.core.$strip>, z.ZodObject<{
58
+ type: z.ZodLiteral<"resource">;
59
+ title: z.ZodOptional<z.ZodString>;
60
+ uri: z.ZodString;
61
+ mimeType: z.ZodOptional<z.ZodString>;
62
+ text: z.ZodOptional<z.ZodString>;
63
+ }, z.core.$strip>], "type">>>;
64
+ forwardedProps: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
65
+ model: z.ZodOptional<z.ZodString>;
66
+ maxOutputTokens: z.ZodOptional<z.ZodNumber>;
67
+ }, z.core.$strip>;
68
+ export type AgUiInjectedTool = z.infer<typeof AgUiInjectedToolSchema>;
69
+ export type AgUiContextItem = z.infer<typeof AgUiContextItemSchema>;
70
+ export type AgUiRequest = z.infer<typeof AgUiRequestSchema>;
71
+ export declare function parseAgUiRequest(request: Request): Promise<AgUiRequest>;
72
+ export declare function parseAgUiRequestOrError(request: Request): Promise<AgUiRequest | Response>;
73
+ export declare function normalizeAgUiMessages(messages: AgUiRequest["messages"]): Message[];
74
+ export declare function createAgUiRunErrorEvent(message: string, code?: string): AgUiSseEvent;
75
+ export declare function createAgUiSseErrorResponse(event: AgUiSseEvent, status: number): Response;
76
+ //# sourceMappingURL=ag-ui-host-support.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ag-ui-host-support.d.ts","sourceRoot":"","sources":["../../../src/src/agent/ag-ui-host-support.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAY1C,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAClC;AAgBD,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