veryfront 0.1.63 → 0.1.65
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/runtime/index.d.ts +1 -0
- package/esm/src/agent/runtime/index.d.ts.map +1 -1
- package/esm/src/agent/runtime/index.js +10 -2
- package/esm/src/channels/control-plane.d.ts +259 -0
- package/esm/src/channels/control-plane.d.ts.map +1 -0
- package/esm/src/channels/control-plane.js +212 -0
- package/esm/src/channels/invoke.d.ts +71 -44
- package/esm/src/channels/invoke.d.ts.map +1 -1
- package/esm/src/channels/invoke.js +33 -114
- package/esm/src/integrations/endpoint-executor.d.ts +1 -0
- package/esm/src/integrations/endpoint-executor.d.ts.map +1 -1
- package/esm/src/integrations/endpoint-executor.js +44 -0
- package/esm/src/internal-agents/ag-ui-sse.d.ts +35 -0
- package/esm/src/internal-agents/ag-ui-sse.d.ts.map +1 -0
- package/esm/src/internal-agents/ag-ui-sse.js +263 -0
- package/esm/src/internal-agents/control-plane-auth.d.ts +20 -0
- package/esm/src/internal-agents/control-plane-auth.d.ts.map +1 -0
- package/esm/src/internal-agents/control-plane-auth.js +56 -0
- package/esm/src/internal-agents/request-body.d.ts +9 -0
- package/esm/src/internal-agents/request-body.d.ts.map +1 -0
- package/esm/src/internal-agents/request-body.js +28 -0
- package/esm/src/internal-agents/run-stream.d.ts +14 -0
- package/esm/src/internal-agents/run-stream.d.ts.map +1 -0
- package/esm/src/internal-agents/run-stream.js +259 -0
- package/esm/src/internal-agents/schema.d.ts +268 -0
- package/esm/src/internal-agents/schema.d.ts.map +1 -0
- package/esm/src/internal-agents/schema.js +71 -0
- package/esm/src/internal-agents/session-manager.d.ts +63 -0
- package/esm/src/internal-agents/session-manager.d.ts.map +1 -0
- package/esm/src/internal-agents/session-manager.js +258 -0
- package/esm/src/platform/adapters/runtime/deno/adapter.d.ts.map +1 -1
- package/esm/src/platform/adapters/runtime/deno/adapter.js +4 -13
- package/esm/src/platform/compat/process.d.ts.map +1 -1
- package/esm/src/platform/compat/process.js +42 -5
- package/esm/src/server/handlers/request/agent-run-cancel.handler.d.ts +11 -0
- package/esm/src/server/handlers/request/agent-run-cancel.handler.d.ts.map +1 -0
- package/esm/src/server/handlers/request/agent-run-cancel.handler.js +62 -0
- package/esm/src/server/handlers/request/agent-run-resume.handler.d.ts +11 -0
- package/esm/src/server/handlers/request/agent-run-resume.handler.d.ts.map +1 -0
- package/esm/src/server/handlers/request/agent-run-resume.handler.js +77 -0
- package/esm/src/server/handlers/request/agent-stream.handler.d.ts +14 -0
- package/esm/src/server/handlers/request/agent-stream.handler.d.ts.map +1 -0
- package/esm/src/server/handlers/request/agent-stream.handler.js +86 -0
- package/esm/src/server/handlers/request/internal-agents-list.handler.d.ts +11 -0
- package/esm/src/server/handlers/request/internal-agents-list.handler.d.ts.map +1 -0
- package/esm/src/server/handlers/request/internal-agents-list.handler.js +73 -0
- package/esm/src/server/runtime-handler/index.d.ts.map +1 -1
- package/esm/src/server/runtime-handler/index.js +8 -0
- package/package.json +1 -1
- package/src/deno.js +1 -1
- package/src/src/agent/runtime/index.ts +12 -2
- package/src/src/channels/control-plane.ts +332 -0
- package/src/src/channels/invoke.ts +44 -164
- package/src/src/integrations/endpoint-executor.ts +51 -0
- package/src/src/internal-agents/ag-ui-sse.ts +327 -0
- package/src/src/internal-agents/control-plane-auth.ts +82 -0
- package/src/src/internal-agents/request-body.ts +42 -0
- package/src/src/internal-agents/run-stream.ts +354 -0
- package/src/src/internal-agents/schema.ts +102 -0
- package/src/src/internal-agents/session-manager.ts +358 -0
- package/src/src/platform/adapters/runtime/deno/adapter.ts +9 -11
- package/src/src/platform/compat/process.ts +56 -3
- package/src/src/server/handlers/request/agent-run-cancel.handler.ts +86 -0
- package/src/src/server/handlers/request/agent-run-resume.handler.ts +108 -0
- package/src/src/server/handlers/request/agent-stream.handler.ts +125 -0
- package/src/src/server/handlers/request/internal-agents-list.handler.ts +100 -0
- package/src/src/server/runtime-handler/index.ts +8 -0
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import * as dntShim from "../../_dnt.shims.js";
|
|
2
|
+
import {
|
|
3
|
+
type Agent,
|
|
4
|
+
type AgentMessage as Message,
|
|
5
|
+
type AgentResponse,
|
|
6
|
+
AgentRuntime,
|
|
7
|
+
} from "../agent/index.js";
|
|
8
|
+
import { SKILL_TOOL_IDS } from "../skill/types.js";
|
|
9
|
+
import { type Tool, toolRegistry } from "../tool/index.js";
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
import {
|
|
12
|
+
createStreamTransformState,
|
|
13
|
+
finalizeRunEvents,
|
|
14
|
+
formatAgUiEvent,
|
|
15
|
+
mapRuntimeEventToAgUi,
|
|
16
|
+
parseSseJsonEvents,
|
|
17
|
+
} from "./ag-ui-sse.js";
|
|
18
|
+
import { AgentRunCancelledError, type AgentRunSessionManager } from "./session-manager.js";
|
|
19
|
+
import type { RuntimeRunAgentInput } from "./schema.js";
|
|
20
|
+
|
|
21
|
+
const anyObjectSchema = z.record(z.unknown());
|
|
22
|
+
|
|
23
|
+
export interface RuntimeAgentStreamExecutionDeps {
|
|
24
|
+
sessionManager: AgentRunSessionManager;
|
|
25
|
+
createRuntime?: (
|
|
26
|
+
agent: Agent,
|
|
27
|
+
mergedTools: Agent["config"]["tools"],
|
|
28
|
+
) => {
|
|
29
|
+
stream: (
|
|
30
|
+
messages: Message[],
|
|
31
|
+
context?: Record<string, unknown>,
|
|
32
|
+
callbacks?: {
|
|
33
|
+
onFinish?: (response: AgentResponse) => void;
|
|
34
|
+
},
|
|
35
|
+
) => Promise<ReadableStream<Uint8Array>>;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function createInjectedStudioTool(
|
|
40
|
+
runId: string,
|
|
41
|
+
toolName: string,
|
|
42
|
+
description: string | undefined,
|
|
43
|
+
parameters: Record<string, unknown> | undefined,
|
|
44
|
+
sessionManager: AgentRunSessionManager,
|
|
45
|
+
): Tool {
|
|
46
|
+
return {
|
|
47
|
+
id: toolName,
|
|
48
|
+
type: "function",
|
|
49
|
+
description: description ?? toolName,
|
|
50
|
+
inputSchema: anyObjectSchema,
|
|
51
|
+
inputSchemaJson: (parameters ??
|
|
52
|
+
{ type: "object", properties: {}, additionalProperties: true }) as Tool["inputSchemaJson"],
|
|
53
|
+
execute: async (_input, context) => {
|
|
54
|
+
const toolCallId = typeof context?.toolCallId === "string" ? context.toolCallId : null;
|
|
55
|
+
if (!toolCallId) {
|
|
56
|
+
throw new Error(`Missing toolCallId for injected tool "${toolName}"`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const waitResult = await sessionManager.waitForToolResult(runId, toolCallId);
|
|
60
|
+
if (waitResult.isError) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
typeof waitResult.result === "string"
|
|
63
|
+
? waitResult.result
|
|
64
|
+
: JSON.stringify(waitResult.result),
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return waitResult.result;
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function buildMergedTools(
|
|
73
|
+
agent: Agent,
|
|
74
|
+
input: RuntimeRunAgentInput,
|
|
75
|
+
sessionManager: AgentRunSessionManager,
|
|
76
|
+
) {
|
|
77
|
+
const injectedTools = Object.fromEntries(
|
|
78
|
+
input.tools.map((tool) => [
|
|
79
|
+
tool.name,
|
|
80
|
+
createInjectedStudioTool(
|
|
81
|
+
input.runId,
|
|
82
|
+
tool.name,
|
|
83
|
+
tool.description,
|
|
84
|
+
tool.parameters,
|
|
85
|
+
sessionManager,
|
|
86
|
+
),
|
|
87
|
+
]),
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
if (!agent.config.tools) {
|
|
91
|
+
return Object.keys(injectedTools).length ? injectedTools : undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (agent.config.tools === true) {
|
|
95
|
+
const merged: Record<string, Tool | boolean> = {};
|
|
96
|
+
for (const [toolId] of toolRegistry.getAll()) {
|
|
97
|
+
if (!agent.config.skills && SKILL_TOOL_IDS.has(toolId)) {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
merged[toolId] = true;
|
|
101
|
+
}
|
|
102
|
+
return { ...merged, ...injectedTools };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return { ...agent.config.tools, ...injectedTools };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
109
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function normalizeToolArgs(part: Record<string, unknown>): Record<string, unknown> {
|
|
113
|
+
if (isRecord(part.args)) {
|
|
114
|
+
return part.args;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (isRecord(part.input)) {
|
|
118
|
+
return part.input;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return {};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function normalizeMessagePart(part: Record<string, unknown>): Message["parts"][number] | null {
|
|
125
|
+
if (part.type === "text" && typeof part.text === "string") {
|
|
126
|
+
return { type: "text", text: part.text };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (
|
|
130
|
+
part.type === "tool_call" &&
|
|
131
|
+
typeof part.id === "string" &&
|
|
132
|
+
typeof part.name === "string"
|
|
133
|
+
) {
|
|
134
|
+
return {
|
|
135
|
+
type: "tool-call",
|
|
136
|
+
toolCallId: part.id,
|
|
137
|
+
toolName: part.name,
|
|
138
|
+
args: normalizeToolArgs(part),
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (
|
|
143
|
+
part.type === "tool-call" &&
|
|
144
|
+
typeof part.toolCallId === "string" &&
|
|
145
|
+
typeof part.toolName === "string"
|
|
146
|
+
) {
|
|
147
|
+
return {
|
|
148
|
+
type: "tool-call",
|
|
149
|
+
toolCallId: part.toolCallId,
|
|
150
|
+
toolName: part.toolName,
|
|
151
|
+
args: normalizeToolArgs(part),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (
|
|
156
|
+
typeof part.type === "string" &&
|
|
157
|
+
part.type.startsWith("tool-") &&
|
|
158
|
+
part.type !== "tool-result" &&
|
|
159
|
+
typeof part.toolCallId === "string" &&
|
|
160
|
+
typeof part.toolName === "string"
|
|
161
|
+
) {
|
|
162
|
+
return {
|
|
163
|
+
type: part.type,
|
|
164
|
+
toolCallId: part.toolCallId,
|
|
165
|
+
toolName: part.toolName,
|
|
166
|
+
args: normalizeToolArgs(part),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (part.type === "tool_result" && typeof part.tool_call_id === "string") {
|
|
171
|
+
return {
|
|
172
|
+
type: "tool-result",
|
|
173
|
+
toolCallId: part.tool_call_id,
|
|
174
|
+
toolName: typeof part.tool_name === "string" ? part.tool_name : "unknown",
|
|
175
|
+
result: "output" in part ? part.output : undefined,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (part.type === "tool-result" && typeof part.toolCallId === "string") {
|
|
180
|
+
return {
|
|
181
|
+
type: "tool-result",
|
|
182
|
+
toolCallId: part.toolCallId,
|
|
183
|
+
toolName: typeof part.toolName === "string" ? part.toolName : "unknown",
|
|
184
|
+
result: "result" in part ? part.result : undefined,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function normalizeRuntimeMessages(messages: RuntimeRunAgentInput["messages"]): Message[] {
|
|
192
|
+
return messages.map((message) => ({
|
|
193
|
+
id: message.id,
|
|
194
|
+
role: message.role,
|
|
195
|
+
parts: message.parts
|
|
196
|
+
.map((part) => isRecord(part) ? normalizeMessagePart(part) : null)
|
|
197
|
+
.filter((part): part is Message["parts"][number] => part !== null),
|
|
198
|
+
...(message.createdAt ? { timestamp: Date.parse(message.createdAt) || undefined } : {}),
|
|
199
|
+
...(message.metadata ? { metadata: message.metadata } : {}),
|
|
200
|
+
}));
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export async function createRuntimeAgentStreamResponse(
|
|
204
|
+
input: RuntimeRunAgentInput,
|
|
205
|
+
agent: Agent,
|
|
206
|
+
deps: RuntimeAgentStreamExecutionDeps,
|
|
207
|
+
): Promise<dntShim.Response> {
|
|
208
|
+
const abortSignal = deps.sessionManager.startRun({
|
|
209
|
+
runId: input.runId,
|
|
210
|
+
threadId: input.threadId,
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
const mergedTools = buildMergedTools(agent, input, deps.sessionManager);
|
|
214
|
+
const runtime = deps.createRuntime?.(agent, mergedTools) ?? new AgentRuntime(agent.id, {
|
|
215
|
+
...agent.config,
|
|
216
|
+
tools: mergedTools,
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
let completedResponse: AgentResponse | null = null;
|
|
220
|
+
const runtimeMessages = normalizeRuntimeMessages(input.messages);
|
|
221
|
+
let runtimeStream: ReadableStream<Uint8Array>;
|
|
222
|
+
let clientAttached = true;
|
|
223
|
+
try {
|
|
224
|
+
runtimeStream = await runtime.stream(
|
|
225
|
+
runtimeMessages,
|
|
226
|
+
{
|
|
227
|
+
threadId: input.threadId,
|
|
228
|
+
runId: input.runId,
|
|
229
|
+
context: input.context,
|
|
230
|
+
forwardedProps: input.forwardedProps,
|
|
231
|
+
},
|
|
232
|
+
{
|
|
233
|
+
onFinish: (response) => {
|
|
234
|
+
completedResponse = response;
|
|
235
|
+
},
|
|
236
|
+
},
|
|
237
|
+
);
|
|
238
|
+
} catch (error) {
|
|
239
|
+
deps.sessionManager.failRun(input.runId);
|
|
240
|
+
throw error;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const response = new ReadableStream<Uint8Array>({
|
|
244
|
+
start: async (controller) => {
|
|
245
|
+
const state = createStreamTransformState();
|
|
246
|
+
const reader = runtimeStream.getReader();
|
|
247
|
+
const decoder = new TextDecoder();
|
|
248
|
+
let remainder = "";
|
|
249
|
+
let aborted = false;
|
|
250
|
+
|
|
251
|
+
const enqueueIfAttached = (event: string, payload: Record<string, unknown>) => {
|
|
252
|
+
const encodedEvent = formatAgUiEvent(event, payload);
|
|
253
|
+
if (!clientAttached) {
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
try {
|
|
258
|
+
controller.enqueue(encodedEvent);
|
|
259
|
+
} catch {
|
|
260
|
+
clientAttached = false;
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
const throwIfAborted = () => {
|
|
265
|
+
if (aborted || abortSignal.aborted) {
|
|
266
|
+
throw new AgentRunCancelledError();
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
const abortHandler = () => {
|
|
271
|
+
aborted = true;
|
|
272
|
+
reader.cancel(new AgentRunCancelledError()).catch(() => {});
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
abortSignal.addEventListener("abort", abortHandler, { once: true });
|
|
276
|
+
enqueueIfAttached("RunStarted", {
|
|
277
|
+
runId: input.runId,
|
|
278
|
+
threadId: input.threadId,
|
|
279
|
+
agentId: input.agentId,
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
try {
|
|
283
|
+
while (true) {
|
|
284
|
+
throwIfAborted();
|
|
285
|
+
|
|
286
|
+
const { done, value } = await reader.read();
|
|
287
|
+
throwIfAborted();
|
|
288
|
+
|
|
289
|
+
if (done) {
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
remainder += decoder.decode(value, { stream: true });
|
|
294
|
+
const parsed = parseSseJsonEvents(remainder);
|
|
295
|
+
remainder = parsed.remainder;
|
|
296
|
+
|
|
297
|
+
for (const event of parsed.events) {
|
|
298
|
+
for (const mappedEvent of mapRuntimeEventToAgUi(state, event)) {
|
|
299
|
+
enqueueIfAttached(mappedEvent.event, mappedEvent.payload);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
throwIfAborted();
|
|
305
|
+
|
|
306
|
+
const trailingEvents = parseSseJsonEvents(`${remainder}\n\n`);
|
|
307
|
+
for (const event of trailingEvents.events) {
|
|
308
|
+
for (const mappedEvent of mapRuntimeEventToAgUi(state, event)) {
|
|
309
|
+
enqueueIfAttached(mappedEvent.event, mappedEvent.payload);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
throwIfAborted();
|
|
314
|
+
|
|
315
|
+
for (const mappedEvent of finalizeRunEvents(state, completedResponse)) {
|
|
316
|
+
enqueueIfAttached(mappedEvent.event, mappedEvent.payload);
|
|
317
|
+
}
|
|
318
|
+
deps.sessionManager.completeRun(input.runId);
|
|
319
|
+
} catch (error) {
|
|
320
|
+
if (error instanceof AgentRunCancelledError) {
|
|
321
|
+
deps.sessionManager.cancelRun(input.runId);
|
|
322
|
+
enqueueIfAttached("RunError", {
|
|
323
|
+
code: "CANCELLED",
|
|
324
|
+
message: error.message,
|
|
325
|
+
});
|
|
326
|
+
} else {
|
|
327
|
+
deps.sessionManager.failRun(input.runId);
|
|
328
|
+
enqueueIfAttached("RunError", {
|
|
329
|
+
code: "RUNTIME_ERROR",
|
|
330
|
+
message: error instanceof Error ? error.message : String(error),
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
} finally {
|
|
334
|
+
abortSignal.removeEventListener("abort", abortHandler);
|
|
335
|
+
if (clientAttached) {
|
|
336
|
+
controller.close();
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
},
|
|
340
|
+
cancel() {
|
|
341
|
+
clientAttached = false;
|
|
342
|
+
return Promise.resolve();
|
|
343
|
+
},
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
return new dntShim.Response(response, {
|
|
347
|
+
status: 200,
|
|
348
|
+
headers: {
|
|
349
|
+
"Content-Type": "text/event-stream",
|
|
350
|
+
"Cache-Control": "no-cache",
|
|
351
|
+
Connection: "keep-alive",
|
|
352
|
+
},
|
|
353
|
+
});
|
|
354
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
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_TOOL_RESULT_BYTES = 65_536;
|
|
9
|
+
const MAX_RUNTIME_MESSAGES = 100;
|
|
10
|
+
|
|
11
|
+
const encoder = new TextEncoder();
|
|
12
|
+
|
|
13
|
+
function isWithinJsonSizeLimit(value: unknown, maxBytes: number): boolean {
|
|
14
|
+
try {
|
|
15
|
+
return encoder.encode(JSON.stringify(value)).byteLength <= maxBytes;
|
|
16
|
+
} catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const RunIdSchema = z.string().min(1).max(128).regex(AGENT_ID_PATTERN);
|
|
22
|
+
|
|
23
|
+
export const AgentIdSchema = z.string().min(1).max(128).regex(AGENT_ID_PATTERN);
|
|
24
|
+
|
|
25
|
+
export const StudioToolNameSchema = z
|
|
26
|
+
.string()
|
|
27
|
+
.min(1)
|
|
28
|
+
.max(128)
|
|
29
|
+
.regex(/^studio_[a-zA-Z0-9_]+$/, "Tool names must use the studio_ prefix");
|
|
30
|
+
|
|
31
|
+
export const RuntimeInjectedToolSchema = z.object({
|
|
32
|
+
name: StudioToolNameSchema,
|
|
33
|
+
description: z.string().max(1024).optional(),
|
|
34
|
+
parameters: z.record(z.unknown()).optional().refine(
|
|
35
|
+
(value) => value === undefined || isWithinJsonSizeLimit(value, MAX_TOOL_PARAMETERS_BYTES),
|
|
36
|
+
{ message: "Tool parameters must be less than 16 KB" },
|
|
37
|
+
),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
export const RuntimeContextItemSchema = z.discriminatedUnion("type", [
|
|
41
|
+
z.object({
|
|
42
|
+
type: z.literal("text"),
|
|
43
|
+
title: z.string().max(256).optional(),
|
|
44
|
+
text: z.string().max(MAX_CONTEXT_ITEM_BYTES),
|
|
45
|
+
}),
|
|
46
|
+
z.object({
|
|
47
|
+
type: z.literal("json"),
|
|
48
|
+
title: z.string().max(256).optional(),
|
|
49
|
+
data: z.record(z.unknown()).refine(
|
|
50
|
+
(value) => isWithinJsonSizeLimit(value, MAX_CONTEXT_ITEM_BYTES),
|
|
51
|
+
{ message: "JSON context item must be less than 16 KB" },
|
|
52
|
+
),
|
|
53
|
+
}),
|
|
54
|
+
z.object({
|
|
55
|
+
type: z.literal("resource"),
|
|
56
|
+
title: z.string().max(256).optional(),
|
|
57
|
+
uri: z.string().max(2048),
|
|
58
|
+
mimeType: z.string().max(256).optional(),
|
|
59
|
+
text: z.string().max(MAX_CONTEXT_ITEM_BYTES).optional(),
|
|
60
|
+
}),
|
|
61
|
+
]);
|
|
62
|
+
|
|
63
|
+
export const RuntimeRunAgentInputSchema = z.object({
|
|
64
|
+
agentId: AgentIdSchema,
|
|
65
|
+
threadId: z.string().uuid(),
|
|
66
|
+
runId: RunIdSchema,
|
|
67
|
+
messages: z.array(
|
|
68
|
+
z.object({
|
|
69
|
+
id: z.string().min(1),
|
|
70
|
+
role: z.enum(["user", "assistant", "system", "tool"]),
|
|
71
|
+
parts: z.array(z.object({ type: z.string().min(1) }).passthrough()).default([]),
|
|
72
|
+
metadata: z.record(z.unknown()).optional(),
|
|
73
|
+
createdAt: z.string().optional(),
|
|
74
|
+
}),
|
|
75
|
+
).max(MAX_RUNTIME_MESSAGES),
|
|
76
|
+
tools: z.array(RuntimeInjectedToolSchema).max(50).default([]),
|
|
77
|
+
context: z.array(RuntimeContextItemSchema).max(10).default([]).refine(
|
|
78
|
+
(value) => isWithinJsonSizeLimit(value, MAX_CONTEXT_TOTAL_BYTES),
|
|
79
|
+
{ message: "context must be less than 64 KB total" },
|
|
80
|
+
),
|
|
81
|
+
forwardedProps: z.record(z.unknown()).optional().refine(
|
|
82
|
+
(value) => value === undefined || isWithinJsonSizeLimit(value, MAX_FORWARDED_PROPS_BYTES),
|
|
83
|
+
{ message: "forwardedProps must be less than 64 KB" },
|
|
84
|
+
),
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
export const ResumeSignalSchema = z.discriminatedUnion("type", [
|
|
88
|
+
z.object({
|
|
89
|
+
type: z.literal("tool_result"),
|
|
90
|
+
toolCallId: z.string().min(1).max(128),
|
|
91
|
+
result: z.unknown().refine(
|
|
92
|
+
(value) => isWithinJsonSizeLimit(value, MAX_TOOL_RESULT_BYTES),
|
|
93
|
+
{ message: "Tool result must be less than 64 KB" },
|
|
94
|
+
),
|
|
95
|
+
isError: z.boolean().optional().default(false),
|
|
96
|
+
}),
|
|
97
|
+
]);
|
|
98
|
+
|
|
99
|
+
export type RuntimeInjectedTool = z.infer<typeof RuntimeInjectedToolSchema>;
|
|
100
|
+
export type RuntimeContextItem = z.infer<typeof RuntimeContextItemSchema>;
|
|
101
|
+
export type RuntimeRunAgentInput = z.infer<typeof RuntimeRunAgentInputSchema>;
|
|
102
|
+
export type ResumeSignal = z.infer<typeof ResumeSignalSchema>;
|