veryfront 0.1.220 → 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 +1 -1
- package/esm/src/agent/ag-ui-detached-start.d.ts.map +1 -1
- package/esm/src/agent/ag-ui-detached-start.js +2 -77
- package/esm/src/agent/ag-ui-handler.d.ts +1 -66
- package/esm/src/agent/ag-ui-handler.d.ts.map +1 -1
- package/esm/src/agent/ag-ui-handler.js +8 -136
- package/esm/src/agent/ag-ui-host-support.d.ts +76 -0
- package/esm/src/agent/ag-ui-host-support.d.ts.map +1 -0
- package/esm/src/agent/ag-ui-host-support.js +182 -0
- package/esm/src/agent/index.d.ts +1 -0
- package/esm/src/agent/index.d.ts.map +1 -1
- package/esm/src/agent/index.js +1 -0
- 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/agent/ag-ui-detached-start.ts +7 -93
- package/src/src/agent/ag-ui-handler.ts +23 -176
- package/src/src/agent/ag-ui-host-support.ts +238 -0
- package/src/src/agent/index.ts +8 -0
- package/src/src/utils/version-constant.ts +1 -1
package/esm/deno.js
CHANGED
|
@@ -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;
|
|
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;AAUxB,OAAO,EAGL,KAAK,uBAAuB,EAC7B,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAIxC,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;AA8IvF,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,UAAU,+BAA+B;IACvC,OAAO,EAAE,wBAAwB,CAAC;IAClC,YAAY,EAAE,OAAO,CAAC;IACtB,UAAU,EAAE,OAAO,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,WAAW,EAAE,WAAW,CAAC;CAC1B;AAED,KAAK,4BAA4B,GAAG,CAClC,KAAK,EAAE,+BAA+B,KACnC,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;AAE1B,UAAU,mCAAmC;IAC3C,cAAc,EAAE,uBAAuB,CAAC,eAAe,CAAC,CAAC;IACzD,OAAO,CAAC,EAAE,gBAAgB,CAAC;IAC3B,sBAAsB,CAAC,EAAE,4BAA4B,CAAC;IACtD,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE;QACnB,OAAO,EAAE,wBAAwB,CAAC;QAClC,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE,MAAM,CAAC;KAClB,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IAC3B,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE;QACpB,OAAO,EAAE,wBAAwB,CAAC;QAClC,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE,MAAM,CAAC;KAClB,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IAC3B,QAAQ,CAAC,EAAE,CAAC,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-
|
|
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 &&
|
|
@@ -195,7 +120,7 @@ async function startDefaultDetachedExecution(input) {
|
|
|
195
120
|
...input.agent.config,
|
|
196
121
|
tools: buildMergedTools(input.agent, input.request, input.sessionManager),
|
|
197
122
|
});
|
|
198
|
-
const runtimeStream = await runtime.stream(
|
|
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);
|
|
199
124
|
await drainRuntimeStream(runtimeStream);
|
|
200
125
|
}
|
|
201
126
|
export function createAgUiDetachedStartHandler(options) {
|
|
@@ -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
|
-
|
|
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":"
|
|
1
|
+
{"version":3,"file":"ag-ui-handler.d.ts","sourceRoot":"","sources":["../../../src/src/agent/ag-ui-handler.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAGL,KAAK,uBAAuB,EAC7B,MAAM,oBAAoB,CAAC;AAkB5B,OAAO,EACL,KAAK,eAAe,EACpB,qBAAqB,EACrB,KAAK,gBAAgB,EACrB,sBAAsB,EACtB,KAAK,WAAW,EAChB,iBAAiB,GAClB,MAAM,yBAAyB,CAAC;AAQjC,KAAK,eAAe,GAAG;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC;AAkT7D,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,WAAW,0BAA2B,SAAQ,kBAAkB;IACpE,KAAK,EAAE,KAAK,CAAC;CACd;AAUD,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,MAAM,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
|
-
|
|
11
|
-
|
|
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:
|
|
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(
|
|
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 =
|
|
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,eAAO,MAAM,sBAAsB;;;;iBAOjC,CAAC;AAEH,eAAO,MAAM,qBAAqB;;;;;;;;;;;;;;2BAqBhC,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;AA0E5D,wBAAsB,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,WAAW,CAAC,CAE7E;AAED,wBAAsB,uBAAuB,CAC3C,OAAO,EAAE,OAAO,GACf,OAAO,CAAC,WAAW,GAAG,QAAQ,CAAC,CA6BjC;AAED,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,WAAW,CAAC,UAAU,CAAC,GAAG,OAAO,EAAE,CAUlF;AAED,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,YAAY,CAQpF;AAED,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,GAAG,QAAQ,CAUxF"}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { formatAgUiEvent } from "../internal-agents/ag-ui-sse.js";
|
|
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_TEXT_PART_LENGTH = 10_000;
|
|
9
|
+
const MAX_MESSAGES_PER_REQUEST = 100;
|
|
10
|
+
const encoder = new TextEncoder();
|
|
11
|
+
const decoder = new TextDecoder();
|
|
12
|
+
function isWithinJsonSizeLimit(value, maxBytes) {
|
|
13
|
+
try {
|
|
14
|
+
return encoder.encode(JSON.stringify(value)).byteLength <= maxBytes;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function isRecord(value) {
|
|
21
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
22
|
+
}
|
|
23
|
+
const AgUiRunIdSchema = z.string().min(1).max(128).regex(AGENT_ID_PATTERN);
|
|
24
|
+
export const AgUiInjectedToolSchema = z.object({
|
|
25
|
+
name: z.string().min(1).max(128),
|
|
26
|
+
description: z.string().max(1024).optional(),
|
|
27
|
+
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" }),
|
|
28
|
+
});
|
|
29
|
+
export const AgUiContextItemSchema = z.discriminatedUnion("type", [
|
|
30
|
+
z.object({
|
|
31
|
+
type: z.literal("text"),
|
|
32
|
+
title: z.string().max(256).optional(),
|
|
33
|
+
text: z.string().max(MAX_CONTEXT_ITEM_BYTES),
|
|
34
|
+
}),
|
|
35
|
+
z.object({
|
|
36
|
+
type: z.literal("json"),
|
|
37
|
+
title: z.string().max(256).optional(),
|
|
38
|
+
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" }),
|
|
39
|
+
}),
|
|
40
|
+
z.object({
|
|
41
|
+
type: z.literal("resource"),
|
|
42
|
+
title: z.string().max(256).optional(),
|
|
43
|
+
uri: z.string().max(2048),
|
|
44
|
+
mimeType: z.string().max(256).optional(),
|
|
45
|
+
text: z.string().max(MAX_CONTEXT_ITEM_BYTES).optional(),
|
|
46
|
+
}),
|
|
47
|
+
]);
|
|
48
|
+
const AgUiMessagePartSchema = z.object({ type: z.string().min(1) }).passthrough();
|
|
49
|
+
const AgUiMessageSchema = z.object({
|
|
50
|
+
id: z.string().min(1),
|
|
51
|
+
role: z.enum(["user", "assistant", "system", "tool"]),
|
|
52
|
+
parts: z.array(AgUiMessagePartSchema).default([]),
|
|
53
|
+
metadata: z.record(z.string(), z.unknown()).optional(),
|
|
54
|
+
createdAt: z.string().optional(),
|
|
55
|
+
});
|
|
56
|
+
export const AgUiRequestSchema = z.object({
|
|
57
|
+
threadId: z.string().uuid().optional(),
|
|
58
|
+
runId: AgUiRunIdSchema.optional(),
|
|
59
|
+
messages: z.array(AgUiMessageSchema).min(1).max(MAX_MESSAGES_PER_REQUEST),
|
|
60
|
+
tools: z.array(AgUiInjectedToolSchema).max(50).default([]),
|
|
61
|
+
context: z.array(AgUiContextItemSchema).max(10).default([]).refine((value) => isWithinJsonSizeLimit(value, MAX_CONTEXT_TOTAL_BYTES), { message: "context must be less than 64 KB total" }),
|
|
62
|
+
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" }),
|
|
63
|
+
model: z.string().optional(),
|
|
64
|
+
maxOutputTokens: z.number().int().positive().optional(),
|
|
65
|
+
});
|
|
66
|
+
function normalizeToolArgs(part) {
|
|
67
|
+
if (isRecord(part.args))
|
|
68
|
+
return part.args;
|
|
69
|
+
if (isRecord(part.input))
|
|
70
|
+
return part.input;
|
|
71
|
+
return {};
|
|
72
|
+
}
|
|
73
|
+
function normalizeMessagePart(part) {
|
|
74
|
+
if (part.type === "text" && typeof part.text === "string" &&
|
|
75
|
+
part.text.length <= MAX_TEXT_PART_LENGTH) {
|
|
76
|
+
return { type: "text", text: part.text };
|
|
77
|
+
}
|
|
78
|
+
if (part.type === "tool_call" && typeof part.id === "string" && typeof part.name === "string") {
|
|
79
|
+
return {
|
|
80
|
+
type: "tool-call",
|
|
81
|
+
toolCallId: part.id,
|
|
82
|
+
toolName: part.name,
|
|
83
|
+
args: normalizeToolArgs(part),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
if (part.type === "tool-call" &&
|
|
87
|
+
typeof part.toolCallId === "string" &&
|
|
88
|
+
typeof part.toolName === "string") {
|
|
89
|
+
return {
|
|
90
|
+
type: "tool-call",
|
|
91
|
+
toolCallId: part.toolCallId,
|
|
92
|
+
toolName: part.toolName,
|
|
93
|
+
args: normalizeToolArgs(part),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
if (typeof part.type === "string" &&
|
|
97
|
+
part.type.startsWith("tool-") &&
|
|
98
|
+
part.type !== "tool-result" &&
|
|
99
|
+
typeof part.toolCallId === "string" &&
|
|
100
|
+
typeof part.toolName === "string") {
|
|
101
|
+
return {
|
|
102
|
+
type: part.type,
|
|
103
|
+
toolCallId: part.toolCallId,
|
|
104
|
+
toolName: part.toolName,
|
|
105
|
+
args: normalizeToolArgs(part),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
if (part.type === "tool_result" && typeof part.tool_call_id === "string") {
|
|
109
|
+
return {
|
|
110
|
+
type: "tool-result",
|
|
111
|
+
toolCallId: part.tool_call_id,
|
|
112
|
+
toolName: typeof part.tool_name === "string" ? part.tool_name : "unknown",
|
|
113
|
+
result: "output" in part ? part.output : undefined,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
if (part.type === "tool-result" && typeof part.toolCallId === "string") {
|
|
117
|
+
return {
|
|
118
|
+
type: "tool-result",
|
|
119
|
+
toolCallId: part.toolCallId,
|
|
120
|
+
toolName: typeof part.toolName === "string" ? part.toolName : "unknown",
|
|
121
|
+
result: "result" in part ? part.result : undefined,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
export async function parseAgUiRequest(request) {
|
|
127
|
+
return AgUiRequestSchema.parse(await request.json());
|
|
128
|
+
}
|
|
129
|
+
export async function parseAgUiRequestOrError(request) {
|
|
130
|
+
try {
|
|
131
|
+
return await parseAgUiRequest(request);
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
if (error instanceof z.ZodError) {
|
|
135
|
+
return Response.json({
|
|
136
|
+
error: "Invalid AG-UI request",
|
|
137
|
+
details: error.issues.map((issue) => ({
|
|
138
|
+
path: issue.path,
|
|
139
|
+
message: issue.message,
|
|
140
|
+
})),
|
|
141
|
+
}, { status: 400 });
|
|
142
|
+
}
|
|
143
|
+
if (error instanceof SyntaxError || error instanceof TypeError) {
|
|
144
|
+
return Response.json({
|
|
145
|
+
error: "Invalid AG-UI request",
|
|
146
|
+
details: [{ path: [], message: "Malformed JSON request body" }],
|
|
147
|
+
}, { status: 400 });
|
|
148
|
+
}
|
|
149
|
+
throw error;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
export function normalizeAgUiMessages(messages) {
|
|
153
|
+
return messages.map((message) => ({
|
|
154
|
+
id: message.id,
|
|
155
|
+
role: message.role,
|
|
156
|
+
parts: message.parts
|
|
157
|
+
.map((part) => normalizeMessagePart(part))
|
|
158
|
+
.filter((part) => part !== null),
|
|
159
|
+
...(message.createdAt ? { timestamp: Date.parse(message.createdAt) || undefined } : {}),
|
|
160
|
+
...(message.metadata ? { metadata: message.metadata } : {}),
|
|
161
|
+
}));
|
|
162
|
+
}
|
|
163
|
+
export function createAgUiRunErrorEvent(message, code) {
|
|
164
|
+
return {
|
|
165
|
+
event: "RunError",
|
|
166
|
+
payload: {
|
|
167
|
+
message,
|
|
168
|
+
...(code ? { code } : {}),
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
export function createAgUiSseErrorResponse(event, status) {
|
|
173
|
+
return new Response(decoder.decode(formatAgUiEvent(event.event, event.payload)), {
|
|
174
|
+
status,
|
|
175
|
+
headers: {
|
|
176
|
+
"Content-Type": "text/event-stream; charset=utf-8",
|
|
177
|
+
"Cache-Control": "no-cache, no-transform",
|
|
178
|
+
"Connection": "keep-alive",
|
|
179
|
+
"X-Accel-Buffering": "no",
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
}
|
package/esm/src/agent/index.d.ts
CHANGED
|
@@ -90,6 +90,7 @@ export { mergeToolCallInput, mergeToolInputDelta, parseDataStreamSseEvents, pars
|
|
|
90
90
|
export { expandAllowedRemoteToolNames, getProviderNativeToolNames, type ProviderNativeToolInventoryOptions, } from "./provider-native-tool-inventory.js";
|
|
91
91
|
export { type AgUiDetachedStartAccepted, AgUiDetachedStartAcceptedSchema, type AgUiDetachedStartHandlerOptions, type AgUiDetachedStartRequest, AgUiDetachedStartRequestSchema, createAgUiDetachedStartHandler, } from "./ag-ui-detached-start.js";
|
|
92
92
|
export { type AgUiCancelHandlerOptions, type AgUiResumeHandlerOptions, type AgUiResumeSignal, AgUiResumeSignalSchema, createAgUiCancelHandler, createAgUiResumeHandler, } from "./ag-ui-run-control.js";
|
|
93
|
+
export { type AgUiSseEvent, createAgUiRunErrorEvent, createAgUiSseErrorResponse, normalizeAgUiMessages, parseAgUiRequest, parseAgUiRequestOrError, } from "./ag-ui-host-support.js";
|
|
93
94
|
export { type AgUiContextItem, type AgUiHandlerConfigWithAgent, type AgUiHandlerOptions, type AgUiInjectedTool, type AgUiRequest, AgUiRequestSchema, createAgUiHandler, } from "./ag-ui-handler.js";
|
|
94
95
|
export { type HumanInputField, type HumanInputFieldInput, HumanInputFieldSchema, type HumanInputOption, HumanInputOptionSchema, type HumanInputPendingRequest, HumanInputPendingRequestSchema, type HumanInputRequest, type HumanInputRequestInput, HumanInputRequestSchema, type HumanInputResult, HumanInputResultSchema, HumanInputResumeError, InvalidHumanInputResultError, waitForHumanInput, type WaitForHumanInputOptions, } from "./human-input.js";
|
|
95
96
|
export { type ChatHandlerBeforeStream, type ChatHandlerBeforeStreamContext, type ChatHandlerBeforeStreamResult, type ChatHandlerConfigWithAgent, type ChatHandlerMessageInput, type ChatHandlerOptions, createChatHandler, } from "./chat-handler.js";
|