veryfront 0.1.151 → 0.1.154
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-run-control.d.ts +33 -0
- package/esm/src/agent/ag-ui-run-control.d.ts.map +1 -0
- package/esm/src/agent/ag-ui-run-control.js +102 -0
- package/esm/src/agent/index.d.ts +2 -0
- package/esm/src/agent/index.d.ts.map +1 -1
- package/esm/src/agent/index.js +2 -0
- package/esm/src/agent/runtime/chat-stream-handler.d.ts.map +1 -1
- package/esm/src/agent/runtime/chat-stream-handler.js +1 -25
- package/esm/src/agent/runtime/error-utils.d.ts +4 -0
- package/esm/src/agent/runtime/error-utils.d.ts.map +1 -0
- package/esm/src/agent/runtime/error-utils.js +25 -0
- package/esm/src/agent/runtime/index.d.ts.map +1 -1
- package/esm/src/agent/runtime/index.js +1 -25
- package/esm/src/agent/runtime-ag-ui-contract.d.ts +207 -0
- package/esm/src/agent/runtime-ag-ui-contract.d.ts.map +1 -0
- package/esm/src/agent/runtime-ag-ui-contract.js +109 -0
- package/esm/src/internal-agents/schema.d.ts +14 -68
- package/esm/src/internal-agents/schema.d.ts.map +1 -1
- package/esm/src/internal-agents/schema.js +8 -98
- package/esm/src/provider/runtime-loader.d.ts.map +1 -1
- package/esm/src/provider/runtime-loader.js +9 -32
- package/esm/src/routing/api/module-loader/loader.js +26 -28
- 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-run-control.ts +161 -0
- package/src/src/agent/index.ts +18 -0
- package/src/src/agent/runtime/chat-stream-handler.ts +1 -33
- package/src/src/agent/runtime/error-utils.ts +32 -0
- package/src/src/agent/runtime/index.ts +1 -33
- package/src/src/agent/runtime-ag-ui-contract.ts +144 -0
- package/src/src/internal-agents/schema.ts +22 -129
- package/src/src/provider/runtime-loader.ts +9 -39
- package/src/src/routing/api/module-loader/loader.ts +37 -38
- package/src/src/utils/version-constant.ts +1 -1
|
@@ -15,6 +15,7 @@ import { serverLogger } from "../../utils/index.js";
|
|
|
15
15
|
import { isAnyDebugEnabled } from "../../utils/constants/env.js";
|
|
16
16
|
import { setActiveSpanAttributes, withSpan } from "../../observability/tracing/otlp-setup.js";
|
|
17
17
|
import { getHostEnv } from "../../platform/compat/process.js";
|
|
18
|
+
import { stringifyToolError, throwIfAborted } from "./error-utils.js";
|
|
18
19
|
|
|
19
20
|
const logger = serverLogger.component("agent");
|
|
20
21
|
|
|
@@ -84,39 +85,6 @@ function normalizeToolInputObject(input: unknown): Record<string, unknown> {
|
|
|
84
85
|
return {};
|
|
85
86
|
}
|
|
86
87
|
|
|
87
|
-
function createAbortError(reason?: unknown): Error {
|
|
88
|
-
if (reason instanceof Error) {
|
|
89
|
-
return reason;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return new DOMException(
|
|
93
|
-
typeof reason === "string" && reason.length > 0 ? reason : "The operation was aborted",
|
|
94
|
-
"AbortError",
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function throwIfAborted(abortSignal?: AbortSignal): void {
|
|
99
|
-
if (abortSignal?.aborted) {
|
|
100
|
-
throw createAbortError(abortSignal.reason);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function stringifyToolError(output: unknown): string {
|
|
105
|
-
if (typeof output === "string" && output.length > 0) {
|
|
106
|
-
return output;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (output instanceof Error && typeof output.message === "string" && output.message.length > 0) {
|
|
110
|
-
return output.message;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
try {
|
|
114
|
-
return JSON.stringify(output);
|
|
115
|
-
} catch {
|
|
116
|
-
return String(output);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
88
|
function summarizeDebugValue(value: unknown): unknown {
|
|
121
89
|
if (value instanceof Error) {
|
|
122
90
|
return {
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export function createAbortError(reason?: unknown): Error {
|
|
2
|
+
if (reason instanceof Error) {
|
|
3
|
+
return reason;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
return new DOMException(
|
|
7
|
+
typeof reason === "string" && reason.length > 0 ? reason : "The operation was aborted",
|
|
8
|
+
"AbortError",
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function throwIfAborted(abortSignal?: AbortSignal): void {
|
|
13
|
+
if (abortSignal?.aborted) {
|
|
14
|
+
throw createAbortError(abortSignal.reason);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function stringifyToolError(error: unknown): string {
|
|
19
|
+
if (typeof error === "string" && error.length > 0) {
|
|
20
|
+
return error;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (error instanceof Error && typeof error.message === "string" && error.message.length > 0) {
|
|
24
|
+
return error.message;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
return JSON.stringify(error);
|
|
29
|
+
} catch {
|
|
30
|
+
return String(error);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -100,6 +100,7 @@ import {
|
|
|
100
100
|
} from "../../skill/allowed-tools.js";
|
|
101
101
|
import { resolveConfiguredAgentModel, resolveRuntimeModel } from "./model-resolution.js";
|
|
102
102
|
import type { RuntimeGenerateToolResult } from "./runtime-tool-types.js";
|
|
103
|
+
import { stringifyToolError, throwIfAborted } from "./error-utils.js";
|
|
103
104
|
|
|
104
105
|
const logger = serverLogger.component("agent");
|
|
105
106
|
const LOAD_SKILL_TOOL_ID = "load-skill";
|
|
@@ -108,23 +109,6 @@ type RuntimeToolFilterConfig = AgentConfig & {
|
|
|
108
109
|
__vfAllowedRemoteTools?: string[];
|
|
109
110
|
};
|
|
110
111
|
|
|
111
|
-
function createAbortError(reason?: unknown): Error {
|
|
112
|
-
if (reason instanceof Error) {
|
|
113
|
-
return reason;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return new DOMException(
|
|
117
|
-
typeof reason === "string" && reason.length > 0 ? reason : "The operation was aborted",
|
|
118
|
-
"AbortError",
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function throwIfAborted(abortSignal?: AbortSignal): void {
|
|
123
|
-
if (abortSignal?.aborted) {
|
|
124
|
-
throw createAbortError(abortSignal.reason);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
112
|
function isAbortError(error: unknown, abortSignal?: AbortSignal): boolean {
|
|
129
113
|
if (abortSignal?.aborted && error === abortSignal.reason) {
|
|
130
114
|
return true;
|
|
@@ -133,22 +117,6 @@ function isAbortError(error: unknown, abortSignal?: AbortSignal): boolean {
|
|
|
133
117
|
return error instanceof DOMException && error.name === "AbortError";
|
|
134
118
|
}
|
|
135
119
|
|
|
136
|
-
function stringifyToolError(error: unknown): string {
|
|
137
|
-
if (typeof error === "string" && error.length > 0) {
|
|
138
|
-
return error;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (error instanceof Error && typeof error.message === "string" && error.message.length > 0) {
|
|
142
|
-
return error.message;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
try {
|
|
146
|
-
return JSON.stringify(error);
|
|
147
|
-
} catch {
|
|
148
|
-
return String(error);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
120
|
function getToolResultError(result: unknown): string | undefined {
|
|
153
121
|
if (!result || typeof result !== "object" || !("error" in result)) {
|
|
154
122
|
return undefined;
|
|
@@ -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>;
|
|
@@ -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 =
|
|
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
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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,
|
|
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 =
|
|
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
|
|
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
|
-
):
|
|
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 =
|
|
281
|
+
export type RuntimeRunAgentInput = AgUiRuntimeRequest;
|
|
389
282
|
export type InternalAgentStreamRequest = z.infer<typeof InternalAgentStreamRequestSchema>;
|
|
390
283
|
export type ResumeSignal = z.infer<typeof ResumeSignalSchema>;
|
|
@@ -732,14 +732,14 @@ function buildAnthropicGenerateResult(payload: unknown): {
|
|
|
732
732
|
};
|
|
733
733
|
}
|
|
734
734
|
|
|
735
|
-
function
|
|
735
|
+
function parseSseChunk(chunk: string): {
|
|
736
736
|
events: Array<unknown | "[DONE]">;
|
|
737
737
|
remainder: string;
|
|
738
738
|
} {
|
|
739
|
-
const blocks = chunk.split(
|
|
739
|
+
const blocks = chunk.split(/\r?\n\r?\n/);
|
|
740
740
|
const remainder = blocks.pop() ?? "";
|
|
741
741
|
const events = blocks.flatMap((block) => {
|
|
742
|
-
const dataLines = block.split(
|
|
742
|
+
const dataLines = block.split(/\r?\n/)
|
|
743
743
|
.filter((line) => line.startsWith("data:"))
|
|
744
744
|
.map((line) => line.slice(5).trimStart());
|
|
745
745
|
|
|
@@ -773,7 +773,7 @@ async function* streamAnthropicCompatibleParts(
|
|
|
773
773
|
|
|
774
774
|
for await (const chunk of stream) {
|
|
775
775
|
buffer += decoder.decode(chunk, { stream: true });
|
|
776
|
-
const parsed =
|
|
776
|
+
const parsed = parseSseChunk(buffer);
|
|
777
777
|
buffer = parsed.remainder;
|
|
778
778
|
|
|
779
779
|
for (const event of parsed.events) {
|
|
@@ -912,7 +912,7 @@ async function* streamAnthropicCompatibleParts(
|
|
|
912
912
|
}
|
|
913
913
|
|
|
914
914
|
if (buffer.trim().length > 0) {
|
|
915
|
-
const parsed =
|
|
915
|
+
const parsed = parseSseChunk(`${buffer}\n\n`);
|
|
916
916
|
for (const event of parsed.events) {
|
|
917
917
|
if (event === "[DONE]") {
|
|
918
918
|
continue;
|
|
@@ -1313,7 +1313,7 @@ async function* streamGoogleCompatibleParts(
|
|
|
1313
1313
|
|
|
1314
1314
|
for await (const chunk of stream) {
|
|
1315
1315
|
buffer += decoder.decode(chunk, { stream: true });
|
|
1316
|
-
const parsed =
|
|
1316
|
+
const parsed = parseSseChunk(buffer);
|
|
1317
1317
|
buffer = parsed.remainder;
|
|
1318
1318
|
|
|
1319
1319
|
for (const event of parsed.events) {
|
|
@@ -1367,7 +1367,7 @@ async function* streamGoogleCompatibleParts(
|
|
|
1367
1367
|
}
|
|
1368
1368
|
|
|
1369
1369
|
if (buffer.trim().length > 0) {
|
|
1370
|
-
const parsed =
|
|
1370
|
+
const parsed = parseSseChunk(`${buffer}\n\n`);
|
|
1371
1371
|
for (const event of parsed.events) {
|
|
1372
1372
|
if (event === "[DONE]") {
|
|
1373
1373
|
continue;
|
|
@@ -1430,36 +1430,6 @@ function buildOpenAIGenerateResult(payload: unknown): {
|
|
|
1430
1430
|
};
|
|
1431
1431
|
}
|
|
1432
1432
|
|
|
1433
|
-
function parseOpenAISseChunk(chunk: string): {
|
|
1434
|
-
events: Array<unknown | "[DONE]">;
|
|
1435
|
-
remainder: string;
|
|
1436
|
-
} {
|
|
1437
|
-
const blocks = chunk.split("\n\n");
|
|
1438
|
-
const remainder = blocks.pop() ?? "";
|
|
1439
|
-
const events = blocks.flatMap((block) => {
|
|
1440
|
-
const dataLines = block.split("\n")
|
|
1441
|
-
.filter((line) => line.startsWith("data:"))
|
|
1442
|
-
.map((line) => line.slice(5).trimStart());
|
|
1443
|
-
|
|
1444
|
-
if (!dataLines.length) {
|
|
1445
|
-
return [];
|
|
1446
|
-
}
|
|
1447
|
-
|
|
1448
|
-
const payload = dataLines.join("\n").trim();
|
|
1449
|
-
if (payload === "[DONE]") {
|
|
1450
|
-
return ["[DONE]" as const];
|
|
1451
|
-
}
|
|
1452
|
-
|
|
1453
|
-
try {
|
|
1454
|
-
return [JSON.parse(payload) as unknown];
|
|
1455
|
-
} catch {
|
|
1456
|
-
return [];
|
|
1457
|
-
}
|
|
1458
|
-
});
|
|
1459
|
-
|
|
1460
|
-
return { events, remainder };
|
|
1461
|
-
}
|
|
1462
|
-
|
|
1463
1433
|
async function* streamOpenAICompatibleParts(
|
|
1464
1434
|
stream: ReadableStream<Uint8Array>,
|
|
1465
1435
|
): AsyncIterable<unknown> {
|
|
@@ -1471,7 +1441,7 @@ async function* streamOpenAICompatibleParts(
|
|
|
1471
1441
|
|
|
1472
1442
|
for await (const chunk of stream) {
|
|
1473
1443
|
buffer += decoder.decode(chunk, { stream: true });
|
|
1474
|
-
const parsed =
|
|
1444
|
+
const parsed = parseSseChunk(buffer);
|
|
1475
1445
|
buffer = parsed.remainder;
|
|
1476
1446
|
|
|
1477
1447
|
for (const event of parsed.events) {
|
|
@@ -1545,7 +1515,7 @@ async function* streamOpenAICompatibleParts(
|
|
|
1545
1515
|
}
|
|
1546
1516
|
|
|
1547
1517
|
if (buffer.trim().length > 0) {
|
|
1548
|
-
const parsed =
|
|
1518
|
+
const parsed = parseSseChunk(`${buffer}\n\n`);
|
|
1549
1519
|
for (const event of parsed.events) {
|
|
1550
1520
|
if (event === "[DONE]") {
|
|
1551
1521
|
continue;
|
|
@@ -355,31 +355,45 @@ function createImportMapPlugin(
|
|
|
355
355
|
|
|
356
356
|
build.onLoad(
|
|
357
357
|
{ filter: /.*/, namespace: "import-map" },
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
args.path,
|
|
363
|
-
FILE_EXTENSIONS,
|
|
364
|
-
projectDir,
|
|
365
|
-
);
|
|
366
|
-
|
|
367
|
-
return {
|
|
368
|
-
contents,
|
|
369
|
-
loader: getLoaderForFile(filePath),
|
|
370
|
-
resolveDir: pathHelper.dirname(filePath),
|
|
371
|
-
};
|
|
372
|
-
} catch (error) {
|
|
373
|
-
const msg = error instanceof Error ? error.message : String(error);
|
|
374
|
-
logger.error(`Failed to load file via import map: ${args.path}`, error);
|
|
375
|
-
return { errors: [{ text: `Failed to load: ${msg}` }] };
|
|
376
|
-
}
|
|
358
|
+
createNamespaceOnLoadHandler({
|
|
359
|
+
adapter,
|
|
360
|
+
projectDir,
|
|
361
|
+
errorLabel: "file via import map",
|
|
377
362
|
}),
|
|
378
363
|
);
|
|
379
364
|
},
|
|
380
365
|
};
|
|
381
366
|
}
|
|
382
367
|
|
|
368
|
+
function createNamespaceOnLoadHandler(options: {
|
|
369
|
+
adapter: RuntimeAdapter;
|
|
370
|
+
projectDir: string;
|
|
371
|
+
errorLabel: string;
|
|
372
|
+
}) {
|
|
373
|
+
const { adapter, projectDir, errorLabel } = options;
|
|
374
|
+
|
|
375
|
+
return wrapWithCurrentContext(async (args: { path: string }) => {
|
|
376
|
+
try {
|
|
377
|
+
const { filePath, contents } = await readFileWithExtensions(
|
|
378
|
+
adapter,
|
|
379
|
+
args.path,
|
|
380
|
+
FILE_EXTENSIONS,
|
|
381
|
+
projectDir,
|
|
382
|
+
);
|
|
383
|
+
|
|
384
|
+
return {
|
|
385
|
+
contents,
|
|
386
|
+
loader: getLoaderForFile(filePath),
|
|
387
|
+
resolveDir: pathHelper.dirname(filePath),
|
|
388
|
+
};
|
|
389
|
+
} catch (error) {
|
|
390
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
391
|
+
logger.error(`Failed to load ${errorLabel}: ${args.path}`, error);
|
|
392
|
+
return { errors: [{ text: `Failed to load: ${msg}` }] };
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
|
|
383
397
|
/** Resolves relative imports through the adapter's virtual FS for remote projects. */
|
|
384
398
|
function createAdapterResolvePlugin(
|
|
385
399
|
adapter: RuntimeAdapter,
|
|
@@ -420,25 +434,10 @@ function createAdapterResolvePlugin(
|
|
|
420
434
|
// cannot resolve the per-project adapter and all file reads fail silently.
|
|
421
435
|
build.onLoad(
|
|
422
436
|
{ filter: /.*/, namespace: "vf-adapter" },
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
args.path,
|
|
428
|
-
FILE_EXTENSIONS,
|
|
429
|
-
projectDir,
|
|
430
|
-
);
|
|
431
|
-
|
|
432
|
-
return {
|
|
433
|
-
contents,
|
|
434
|
-
loader: getLoaderForFile(filePath),
|
|
435
|
-
resolveDir: pathHelper.dirname(filePath),
|
|
436
|
-
};
|
|
437
|
-
} catch (error) {
|
|
438
|
-
const msg = error instanceof Error ? error.message : String(error);
|
|
439
|
-
logger.error(`Failed to load via adapter: ${args.path}`, error);
|
|
440
|
-
return { errors: [{ text: `Failed to load: ${msg}` }] };
|
|
441
|
-
}
|
|
437
|
+
createNamespaceOnLoadHandler({
|
|
438
|
+
adapter,
|
|
439
|
+
projectDir,
|
|
440
|
+
errorLabel: "via adapter",
|
|
442
441
|
}),
|
|
443
442
|
);
|
|
444
443
|
},
|