veryfront 0.1.64 → 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.
Files changed (67) hide show
  1. package/esm/deno.js +1 -1
  2. package/esm/src/agent/runtime/index.d.ts +1 -0
  3. package/esm/src/agent/runtime/index.d.ts.map +1 -1
  4. package/esm/src/agent/runtime/index.js +10 -2
  5. package/esm/src/channels/control-plane.d.ts +259 -0
  6. package/esm/src/channels/control-plane.d.ts.map +1 -0
  7. package/esm/src/channels/control-plane.js +212 -0
  8. package/esm/src/channels/invoke.d.ts +3 -40
  9. package/esm/src/channels/invoke.d.ts.map +1 -1
  10. package/esm/src/channels/invoke.js +9 -106
  11. package/esm/src/internal-agents/ag-ui-sse.d.ts +35 -0
  12. package/esm/src/internal-agents/ag-ui-sse.d.ts.map +1 -0
  13. package/esm/src/internal-agents/ag-ui-sse.js +263 -0
  14. package/esm/src/internal-agents/control-plane-auth.d.ts +20 -0
  15. package/esm/src/internal-agents/control-plane-auth.d.ts.map +1 -0
  16. package/esm/src/internal-agents/control-plane-auth.js +56 -0
  17. package/esm/src/internal-agents/request-body.d.ts +9 -0
  18. package/esm/src/internal-agents/request-body.d.ts.map +1 -0
  19. package/esm/src/internal-agents/request-body.js +28 -0
  20. package/esm/src/internal-agents/run-stream.d.ts +14 -0
  21. package/esm/src/internal-agents/run-stream.d.ts.map +1 -0
  22. package/esm/src/internal-agents/run-stream.js +259 -0
  23. package/esm/src/internal-agents/schema.d.ts +268 -0
  24. package/esm/src/internal-agents/schema.d.ts.map +1 -0
  25. package/esm/src/internal-agents/schema.js +71 -0
  26. package/esm/src/internal-agents/session-manager.d.ts +63 -0
  27. package/esm/src/internal-agents/session-manager.d.ts.map +1 -0
  28. package/esm/src/internal-agents/session-manager.js +258 -0
  29. package/esm/src/platform/adapters/runtime/deno/adapter.d.ts.map +1 -1
  30. package/esm/src/platform/adapters/runtime/deno/adapter.js +4 -13
  31. package/esm/src/platform/compat/process.d.ts.map +1 -1
  32. package/esm/src/platform/compat/process.js +42 -5
  33. package/esm/src/server/handlers/request/agent-run-cancel.handler.d.ts +11 -0
  34. package/esm/src/server/handlers/request/agent-run-cancel.handler.d.ts.map +1 -0
  35. package/esm/src/server/handlers/request/agent-run-cancel.handler.js +62 -0
  36. package/esm/src/server/handlers/request/agent-run-resume.handler.d.ts +11 -0
  37. package/esm/src/server/handlers/request/agent-run-resume.handler.d.ts.map +1 -0
  38. package/esm/src/server/handlers/request/agent-run-resume.handler.js +77 -0
  39. package/esm/src/server/handlers/request/agent-stream.handler.d.ts +14 -0
  40. package/esm/src/server/handlers/request/agent-stream.handler.d.ts.map +1 -0
  41. package/esm/src/server/handlers/request/agent-stream.handler.js +86 -0
  42. package/esm/src/server/handlers/request/{channel-assistants.handler.d.ts → internal-agents-list.handler.d.ts} +4 -4
  43. package/esm/src/server/handlers/request/internal-agents-list.handler.d.ts.map +1 -0
  44. package/esm/src/server/handlers/request/internal-agents-list.handler.js +73 -0
  45. package/esm/src/server/runtime-handler/index.d.ts.map +1 -1
  46. package/esm/src/server/runtime-handler/index.js +8 -2
  47. package/package.json +1 -1
  48. package/src/deno.js +1 -1
  49. package/src/src/agent/runtime/index.ts +12 -2
  50. package/src/src/channels/control-plane.ts +332 -0
  51. package/src/src/channels/invoke.ts +12 -157
  52. package/src/src/internal-agents/ag-ui-sse.ts +327 -0
  53. package/src/src/internal-agents/control-plane-auth.ts +82 -0
  54. package/src/src/internal-agents/request-body.ts +42 -0
  55. package/src/src/internal-agents/run-stream.ts +354 -0
  56. package/src/src/internal-agents/schema.ts +102 -0
  57. package/src/src/internal-agents/session-manager.ts +358 -0
  58. package/src/src/platform/adapters/runtime/deno/adapter.ts +9 -11
  59. package/src/src/platform/compat/process.ts +56 -3
  60. package/src/src/server/handlers/request/agent-run-cancel.handler.ts +86 -0
  61. package/src/src/server/handlers/request/agent-run-resume.handler.ts +108 -0
  62. package/src/src/server/handlers/request/agent-stream.handler.ts +125 -0
  63. package/src/src/server/handlers/request/internal-agents-list.handler.ts +100 -0
  64. package/src/src/server/runtime-handler/index.ts +8 -2
  65. package/esm/src/server/handlers/request/channel-assistants.handler.d.ts.map +0 -1
  66. package/esm/src/server/handlers/request/channel-assistants.handler.js +0 -71
  67. package/src/src/server/handlers/request/channel-assistants.handler.ts +0 -94
@@ -0,0 +1,259 @@
1
+ import * as dntShim from "../../_dnt.shims.js";
2
+ import { AgentRuntime, } from "../agent/index.js";
3
+ import { SKILL_TOOL_IDS } from "../skill/types.js";
4
+ import { toolRegistry } from "../tool/index.js";
5
+ import { z } from "zod";
6
+ import { createStreamTransformState, finalizeRunEvents, formatAgUiEvent, mapRuntimeEventToAgUi, parseSseJsonEvents, } from "./ag-ui-sse.js";
7
+ import { AgentRunCancelledError } from "./session-manager.js";
8
+ const anyObjectSchema = z.record(z.unknown());
9
+ function createInjectedStudioTool(runId, toolName, description, parameters, sessionManager) {
10
+ return {
11
+ id: toolName,
12
+ type: "function",
13
+ description: description ?? toolName,
14
+ inputSchema: anyObjectSchema,
15
+ inputSchemaJson: (parameters ??
16
+ { type: "object", properties: {}, additionalProperties: true }),
17
+ execute: async (_input, context) => {
18
+ const toolCallId = typeof context?.toolCallId === "string" ? context.toolCallId : null;
19
+ if (!toolCallId) {
20
+ throw new Error(`Missing toolCallId for injected tool "${toolName}"`);
21
+ }
22
+ const waitResult = await sessionManager.waitForToolResult(runId, toolCallId);
23
+ if (waitResult.isError) {
24
+ throw new Error(typeof waitResult.result === "string"
25
+ ? waitResult.result
26
+ : JSON.stringify(waitResult.result));
27
+ }
28
+ return waitResult.result;
29
+ },
30
+ };
31
+ }
32
+ function buildMergedTools(agent, input, sessionManager) {
33
+ const injectedTools = Object.fromEntries(input.tools.map((tool) => [
34
+ tool.name,
35
+ createInjectedStudioTool(input.runId, tool.name, tool.description, tool.parameters, sessionManager),
36
+ ]));
37
+ if (!agent.config.tools) {
38
+ return Object.keys(injectedTools).length ? injectedTools : undefined;
39
+ }
40
+ if (agent.config.tools === true) {
41
+ const merged = {};
42
+ for (const [toolId] of toolRegistry.getAll()) {
43
+ if (!agent.config.skills && SKILL_TOOL_IDS.has(toolId)) {
44
+ continue;
45
+ }
46
+ merged[toolId] = true;
47
+ }
48
+ return { ...merged, ...injectedTools };
49
+ }
50
+ return { ...agent.config.tools, ...injectedTools };
51
+ }
52
+ function isRecord(value) {
53
+ return typeof value === "object" && value !== null && !Array.isArray(value);
54
+ }
55
+ function normalizeToolArgs(part) {
56
+ if (isRecord(part.args)) {
57
+ return part.args;
58
+ }
59
+ if (isRecord(part.input)) {
60
+ return part.input;
61
+ }
62
+ return {};
63
+ }
64
+ function normalizeMessagePart(part) {
65
+ if (part.type === "text" && typeof part.text === "string") {
66
+ return { type: "text", text: part.text };
67
+ }
68
+ if (part.type === "tool_call" &&
69
+ typeof part.id === "string" &&
70
+ typeof part.name === "string") {
71
+ return {
72
+ type: "tool-call",
73
+ toolCallId: part.id,
74
+ toolName: part.name,
75
+ args: normalizeToolArgs(part),
76
+ };
77
+ }
78
+ if (part.type === "tool-call" &&
79
+ typeof part.toolCallId === "string" &&
80
+ typeof part.toolName === "string") {
81
+ return {
82
+ type: "tool-call",
83
+ toolCallId: part.toolCallId,
84
+ toolName: part.toolName,
85
+ args: normalizeToolArgs(part),
86
+ };
87
+ }
88
+ if (typeof part.type === "string" &&
89
+ part.type.startsWith("tool-") &&
90
+ part.type !== "tool-result" &&
91
+ typeof part.toolCallId === "string" &&
92
+ typeof part.toolName === "string") {
93
+ return {
94
+ type: part.type,
95
+ toolCallId: part.toolCallId,
96
+ toolName: part.toolName,
97
+ args: normalizeToolArgs(part),
98
+ };
99
+ }
100
+ if (part.type === "tool_result" && typeof part.tool_call_id === "string") {
101
+ return {
102
+ type: "tool-result",
103
+ toolCallId: part.tool_call_id,
104
+ toolName: typeof part.tool_name === "string" ? part.tool_name : "unknown",
105
+ result: "output" in part ? part.output : undefined,
106
+ };
107
+ }
108
+ if (part.type === "tool-result" && typeof part.toolCallId === "string") {
109
+ return {
110
+ type: "tool-result",
111
+ toolCallId: part.toolCallId,
112
+ toolName: typeof part.toolName === "string" ? part.toolName : "unknown",
113
+ result: "result" in part ? part.result : undefined,
114
+ };
115
+ }
116
+ return null;
117
+ }
118
+ function normalizeRuntimeMessages(messages) {
119
+ return messages.map((message) => ({
120
+ id: message.id,
121
+ role: message.role,
122
+ parts: message.parts
123
+ .map((part) => isRecord(part) ? normalizeMessagePart(part) : null)
124
+ .filter((part) => part !== null),
125
+ ...(message.createdAt ? { timestamp: Date.parse(message.createdAt) || undefined } : {}),
126
+ ...(message.metadata ? { metadata: message.metadata } : {}),
127
+ }));
128
+ }
129
+ export async function createRuntimeAgentStreamResponse(input, agent, deps) {
130
+ const abortSignal = deps.sessionManager.startRun({
131
+ runId: input.runId,
132
+ threadId: input.threadId,
133
+ });
134
+ const mergedTools = buildMergedTools(agent, input, deps.sessionManager);
135
+ const runtime = deps.createRuntime?.(agent, mergedTools) ?? new AgentRuntime(agent.id, {
136
+ ...agent.config,
137
+ tools: mergedTools,
138
+ });
139
+ let completedResponse = null;
140
+ const runtimeMessages = normalizeRuntimeMessages(input.messages);
141
+ let runtimeStream;
142
+ let clientAttached = true;
143
+ try {
144
+ runtimeStream = await runtime.stream(runtimeMessages, {
145
+ threadId: input.threadId,
146
+ runId: input.runId,
147
+ context: input.context,
148
+ forwardedProps: input.forwardedProps,
149
+ }, {
150
+ onFinish: (response) => {
151
+ completedResponse = response;
152
+ },
153
+ });
154
+ }
155
+ catch (error) {
156
+ deps.sessionManager.failRun(input.runId);
157
+ throw error;
158
+ }
159
+ const response = new ReadableStream({
160
+ start: async (controller) => {
161
+ const state = createStreamTransformState();
162
+ const reader = runtimeStream.getReader();
163
+ const decoder = new TextDecoder();
164
+ let remainder = "";
165
+ let aborted = false;
166
+ const enqueueIfAttached = (event, payload) => {
167
+ const encodedEvent = formatAgUiEvent(event, payload);
168
+ if (!clientAttached) {
169
+ return;
170
+ }
171
+ try {
172
+ controller.enqueue(encodedEvent);
173
+ }
174
+ catch {
175
+ clientAttached = false;
176
+ }
177
+ };
178
+ const throwIfAborted = () => {
179
+ if (aborted || abortSignal.aborted) {
180
+ throw new AgentRunCancelledError();
181
+ }
182
+ };
183
+ const abortHandler = () => {
184
+ aborted = true;
185
+ reader.cancel(new AgentRunCancelledError()).catch(() => { });
186
+ };
187
+ abortSignal.addEventListener("abort", abortHandler, { once: true });
188
+ enqueueIfAttached("RunStarted", {
189
+ runId: input.runId,
190
+ threadId: input.threadId,
191
+ agentId: input.agentId,
192
+ });
193
+ try {
194
+ while (true) {
195
+ throwIfAborted();
196
+ const { done, value } = await reader.read();
197
+ throwIfAborted();
198
+ if (done) {
199
+ break;
200
+ }
201
+ remainder += decoder.decode(value, { stream: true });
202
+ const parsed = parseSseJsonEvents(remainder);
203
+ remainder = parsed.remainder;
204
+ for (const event of parsed.events) {
205
+ for (const mappedEvent of mapRuntimeEventToAgUi(state, event)) {
206
+ enqueueIfAttached(mappedEvent.event, mappedEvent.payload);
207
+ }
208
+ }
209
+ }
210
+ throwIfAborted();
211
+ const trailingEvents = parseSseJsonEvents(`${remainder}\n\n`);
212
+ for (const event of trailingEvents.events) {
213
+ for (const mappedEvent of mapRuntimeEventToAgUi(state, event)) {
214
+ enqueueIfAttached(mappedEvent.event, mappedEvent.payload);
215
+ }
216
+ }
217
+ throwIfAborted();
218
+ for (const mappedEvent of finalizeRunEvents(state, completedResponse)) {
219
+ enqueueIfAttached(mappedEvent.event, mappedEvent.payload);
220
+ }
221
+ deps.sessionManager.completeRun(input.runId);
222
+ }
223
+ catch (error) {
224
+ if (error instanceof AgentRunCancelledError) {
225
+ deps.sessionManager.cancelRun(input.runId);
226
+ enqueueIfAttached("RunError", {
227
+ code: "CANCELLED",
228
+ message: error.message,
229
+ });
230
+ }
231
+ else {
232
+ deps.sessionManager.failRun(input.runId);
233
+ enqueueIfAttached("RunError", {
234
+ code: "RUNTIME_ERROR",
235
+ message: error instanceof Error ? error.message : String(error),
236
+ });
237
+ }
238
+ }
239
+ finally {
240
+ abortSignal.removeEventListener("abort", abortHandler);
241
+ if (clientAttached) {
242
+ controller.close();
243
+ }
244
+ }
245
+ },
246
+ cancel() {
247
+ clientAttached = false;
248
+ return Promise.resolve();
249
+ },
250
+ });
251
+ return new dntShim.Response(response, {
252
+ status: 200,
253
+ headers: {
254
+ "Content-Type": "text/event-stream",
255
+ "Cache-Control": "no-cache",
256
+ Connection: "keep-alive",
257
+ },
258
+ });
259
+ }
@@ -0,0 +1,268 @@
1
+ import { z } from "zod";
2
+ export declare const RunIdSchema: z.ZodString;
3
+ export declare const AgentIdSchema: z.ZodString;
4
+ export declare const StudioToolNameSchema: z.ZodString;
5
+ export declare const RuntimeInjectedToolSchema: z.ZodObject<{
6
+ name: z.ZodString;
7
+ description: z.ZodOptional<z.ZodString>;
8
+ parameters: z.ZodEffects<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>, Record<string, unknown> | undefined, Record<string, unknown> | undefined>;
9
+ }, "strip", z.ZodTypeAny, {
10
+ name: string;
11
+ description?: string | undefined;
12
+ parameters?: Record<string, unknown> | undefined;
13
+ }, {
14
+ name: string;
15
+ description?: string | undefined;
16
+ parameters?: Record<string, unknown> | undefined;
17
+ }>;
18
+ export declare const RuntimeContextItemSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
19
+ type: z.ZodLiteral<"text">;
20
+ title: z.ZodOptional<z.ZodString>;
21
+ text: z.ZodString;
22
+ }, "strip", z.ZodTypeAny, {
23
+ type: "text";
24
+ text: string;
25
+ title?: string | undefined;
26
+ }, {
27
+ type: "text";
28
+ text: string;
29
+ title?: string | undefined;
30
+ }>, z.ZodObject<{
31
+ type: z.ZodLiteral<"json">;
32
+ title: z.ZodOptional<z.ZodString>;
33
+ data: z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodUnknown>, Record<string, unknown>, Record<string, unknown>>;
34
+ }, "strip", z.ZodTypeAny, {
35
+ type: "json";
36
+ data: Record<string, unknown>;
37
+ title?: string | undefined;
38
+ }, {
39
+ type: "json";
40
+ data: Record<string, unknown>;
41
+ title?: string | undefined;
42
+ }>, z.ZodObject<{
43
+ type: z.ZodLiteral<"resource">;
44
+ title: z.ZodOptional<z.ZodString>;
45
+ uri: z.ZodString;
46
+ mimeType: z.ZodOptional<z.ZodString>;
47
+ text: z.ZodOptional<z.ZodString>;
48
+ }, "strip", z.ZodTypeAny, {
49
+ type: "resource";
50
+ uri: string;
51
+ title?: string | undefined;
52
+ text?: string | undefined;
53
+ mimeType?: string | undefined;
54
+ }, {
55
+ type: "resource";
56
+ uri: string;
57
+ title?: string | undefined;
58
+ text?: string | undefined;
59
+ mimeType?: string | undefined;
60
+ }>]>;
61
+ export declare const RuntimeRunAgentInputSchema: z.ZodObject<{
62
+ agentId: z.ZodString;
63
+ threadId: z.ZodString;
64
+ runId: z.ZodString;
65
+ messages: z.ZodArray<z.ZodObject<{
66
+ id: z.ZodString;
67
+ role: z.ZodEnum<["user", "assistant", "system", "tool"]>;
68
+ parts: z.ZodDefault<z.ZodArray<z.ZodObject<{
69
+ type: z.ZodString;
70
+ }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
71
+ type: z.ZodString;
72
+ }, z.ZodTypeAny, "passthrough">, z.objectInputType<{
73
+ type: z.ZodString;
74
+ }, z.ZodTypeAny, "passthrough">>, "many">>;
75
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
76
+ createdAt: z.ZodOptional<z.ZodString>;
77
+ }, "strip", z.ZodTypeAny, {
78
+ id: string;
79
+ role: "tool" | "user" | "assistant" | "system";
80
+ parts: z.objectOutputType<{
81
+ type: z.ZodString;
82
+ }, z.ZodTypeAny, "passthrough">[];
83
+ metadata?: Record<string, unknown> | undefined;
84
+ createdAt?: string | undefined;
85
+ }, {
86
+ id: string;
87
+ role: "tool" | "user" | "assistant" | "system";
88
+ metadata?: Record<string, unknown> | undefined;
89
+ createdAt?: string | undefined;
90
+ parts?: z.objectInputType<{
91
+ type: z.ZodString;
92
+ }, z.ZodTypeAny, "passthrough">[] | undefined;
93
+ }>, "many">;
94
+ tools: z.ZodDefault<z.ZodArray<z.ZodObject<{
95
+ name: z.ZodString;
96
+ description: z.ZodOptional<z.ZodString>;
97
+ parameters: z.ZodEffects<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>, Record<string, unknown> | undefined, Record<string, unknown> | undefined>;
98
+ }, "strip", z.ZodTypeAny, {
99
+ name: string;
100
+ description?: string | undefined;
101
+ parameters?: Record<string, unknown> | undefined;
102
+ }, {
103
+ name: string;
104
+ description?: string | undefined;
105
+ parameters?: Record<string, unknown> | undefined;
106
+ }>, "many">>;
107
+ context: z.ZodEffects<z.ZodDefault<z.ZodArray<z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
108
+ type: z.ZodLiteral<"text">;
109
+ title: z.ZodOptional<z.ZodString>;
110
+ text: z.ZodString;
111
+ }, "strip", z.ZodTypeAny, {
112
+ type: "text";
113
+ text: string;
114
+ title?: string | undefined;
115
+ }, {
116
+ type: "text";
117
+ text: string;
118
+ title?: string | undefined;
119
+ }>, z.ZodObject<{
120
+ type: z.ZodLiteral<"json">;
121
+ title: z.ZodOptional<z.ZodString>;
122
+ data: z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodUnknown>, Record<string, unknown>, Record<string, unknown>>;
123
+ }, "strip", z.ZodTypeAny, {
124
+ type: "json";
125
+ data: Record<string, unknown>;
126
+ title?: string | undefined;
127
+ }, {
128
+ type: "json";
129
+ data: Record<string, unknown>;
130
+ title?: string | undefined;
131
+ }>, z.ZodObject<{
132
+ type: z.ZodLiteral<"resource">;
133
+ title: z.ZodOptional<z.ZodString>;
134
+ uri: z.ZodString;
135
+ mimeType: z.ZodOptional<z.ZodString>;
136
+ text: z.ZodOptional<z.ZodString>;
137
+ }, "strip", z.ZodTypeAny, {
138
+ type: "resource";
139
+ uri: string;
140
+ title?: string | undefined;
141
+ text?: string | undefined;
142
+ mimeType?: string | undefined;
143
+ }, {
144
+ type: "resource";
145
+ uri: string;
146
+ title?: string | undefined;
147
+ text?: string | undefined;
148
+ mimeType?: string | undefined;
149
+ }>]>, "many">>, ({
150
+ type: "text";
151
+ text: string;
152
+ title?: string | undefined;
153
+ } | {
154
+ type: "json";
155
+ data: Record<string, unknown>;
156
+ title?: string | undefined;
157
+ } | {
158
+ type: "resource";
159
+ uri: string;
160
+ title?: string | undefined;
161
+ text?: string | undefined;
162
+ mimeType?: string | undefined;
163
+ })[], ({
164
+ type: "text";
165
+ text: string;
166
+ title?: string | undefined;
167
+ } | {
168
+ type: "json";
169
+ data: Record<string, unknown>;
170
+ title?: string | undefined;
171
+ } | {
172
+ type: "resource";
173
+ uri: string;
174
+ title?: string | undefined;
175
+ text?: string | undefined;
176
+ mimeType?: string | undefined;
177
+ })[] | undefined>;
178
+ forwardedProps: z.ZodEffects<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>, Record<string, unknown> | undefined, Record<string, unknown> | undefined>;
179
+ }, "strip", z.ZodTypeAny, {
180
+ context: ({
181
+ type: "text";
182
+ text: string;
183
+ title?: string | undefined;
184
+ } | {
185
+ type: "json";
186
+ data: Record<string, unknown>;
187
+ title?: string | undefined;
188
+ } | {
189
+ type: "resource";
190
+ uri: string;
191
+ title?: string | undefined;
192
+ text?: string | undefined;
193
+ mimeType?: string | undefined;
194
+ })[];
195
+ tools: {
196
+ name: string;
197
+ description?: string | undefined;
198
+ parameters?: Record<string, unknown> | undefined;
199
+ }[];
200
+ agentId: string;
201
+ messages: {
202
+ id: string;
203
+ role: "tool" | "user" | "assistant" | "system";
204
+ parts: z.objectOutputType<{
205
+ type: z.ZodString;
206
+ }, z.ZodTypeAny, "passthrough">[];
207
+ metadata?: Record<string, unknown> | undefined;
208
+ createdAt?: string | undefined;
209
+ }[];
210
+ runId: string;
211
+ threadId: string;
212
+ forwardedProps?: Record<string, unknown> | undefined;
213
+ }, {
214
+ agentId: string;
215
+ messages: {
216
+ id: string;
217
+ role: "tool" | "user" | "assistant" | "system";
218
+ metadata?: Record<string, unknown> | undefined;
219
+ createdAt?: string | undefined;
220
+ parts?: z.objectInputType<{
221
+ type: z.ZodString;
222
+ }, z.ZodTypeAny, "passthrough">[] | undefined;
223
+ }[];
224
+ runId: string;
225
+ threadId: string;
226
+ context?: ({
227
+ type: "text";
228
+ text: string;
229
+ title?: string | undefined;
230
+ } | {
231
+ type: "json";
232
+ data: Record<string, unknown>;
233
+ title?: string | undefined;
234
+ } | {
235
+ type: "resource";
236
+ uri: string;
237
+ title?: string | undefined;
238
+ text?: string | undefined;
239
+ mimeType?: string | undefined;
240
+ })[] | undefined;
241
+ tools?: {
242
+ name: string;
243
+ description?: string | undefined;
244
+ parameters?: Record<string, unknown> | undefined;
245
+ }[] | undefined;
246
+ forwardedProps?: Record<string, unknown> | undefined;
247
+ }>;
248
+ export declare const ResumeSignalSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
249
+ type: z.ZodLiteral<"tool_result">;
250
+ toolCallId: z.ZodString;
251
+ result: z.ZodEffects<z.ZodUnknown, unknown, unknown>;
252
+ isError: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
253
+ }, "strip", z.ZodTypeAny, {
254
+ type: "tool_result";
255
+ toolCallId: string;
256
+ isError: boolean;
257
+ result?: unknown;
258
+ }, {
259
+ type: "tool_result";
260
+ toolCallId: string;
261
+ result?: unknown;
262
+ isError?: boolean | undefined;
263
+ }>]>;
264
+ export type RuntimeInjectedTool = z.infer<typeof RuntimeInjectedToolSchema>;
265
+ export type RuntimeContextItem = z.infer<typeof RuntimeContextItemSchema>;
266
+ export type RuntimeRunAgentInput = z.infer<typeof RuntimeRunAgentInputSchema>;
267
+ export type ResumeSignal = z.infer<typeof ResumeSignalSchema>;
268
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../../src/src/internal-agents/schema.ts"],"names":[],"mappings":"AAAA,O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_536;
8
+ const MAX_RUNTIME_MESSAGES = 100;
9
+ const encoder = new TextEncoder();
10
+ function isWithinJsonSizeLimit(value, maxBytes) {
11
+ try {
12
+ return encoder.encode(JSON.stringify(value)).byteLength <= maxBytes;
13
+ }
14
+ catch {
15
+ return false;
16
+ }
17
+ }
18
+ export const RunIdSchema = z.string().min(1).max(128).regex(AGENT_ID_PATTERN);
19
+ export const AgentIdSchema = z.string().min(1).max(128).regex(AGENT_ID_PATTERN);
20
+ export const StudioToolNameSchema = z
21
+ .string()
22
+ .min(1)
23
+ .max(128)
24
+ .regex(/^studio_[a-zA-Z0-9_]+$/, "Tool names must use the studio_ prefix");
25
+ export const RuntimeInjectedToolSchema = z.object({
26
+ name: StudioToolNameSchema,
27
+ description: z.string().max(1024).optional(),
28
+ parameters: z.record(z.unknown()).optional().refine((value) => value === undefined || isWithinJsonSizeLimit(value, MAX_TOOL_PARAMETERS_BYTES), { message: "Tool parameters must be less than 16 KB" }),
29
+ });
30
+ export const RuntimeContextItemSchema = z.discriminatedUnion("type", [
31
+ z.object({
32
+ type: z.literal("text"),
33
+ title: z.string().max(256).optional(),
34
+ text: z.string().max(MAX_CONTEXT_ITEM_BYTES),
35
+ }),
36
+ z.object({
37
+ type: z.literal("json"),
38
+ title: z.string().max(256).optional(),
39
+ data: z.record(z.unknown()).refine((value) => isWithinJsonSizeLimit(value, MAX_CONTEXT_ITEM_BYTES), { message: "JSON context item must be less than 16 KB" }),
40
+ }),
41
+ z.object({
42
+ type: z.literal("resource"),
43
+ title: z.string().max(256).optional(),
44
+ uri: z.string().max(2048),
45
+ mimeType: z.string().max(256).optional(),
46
+ text: z.string().max(MAX_CONTEXT_ITEM_BYTES).optional(),
47
+ }),
48
+ ]);
49
+ export const RuntimeRunAgentInputSchema = z.object({
50
+ agentId: AgentIdSchema,
51
+ threadId: z.string().uuid(),
52
+ runId: RunIdSchema,
53
+ messages: z.array(z.object({
54
+ id: z.string().min(1),
55
+ role: z.enum(["user", "assistant", "system", "tool"]),
56
+ parts: z.array(z.object({ type: z.string().min(1) }).passthrough()).default([]),
57
+ metadata: z.record(z.unknown()).optional(),
58
+ createdAt: z.string().optional(),
59
+ })).max(MAX_RUNTIME_MESSAGES),
60
+ tools: z.array(RuntimeInjectedToolSchema).max(50).default([]),
61
+ context: z.array(RuntimeContextItemSchema).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.unknown()).optional().refine((value) => value === undefined || isWithinJsonSizeLimit(value, MAX_FORWARDED_PROPS_BYTES), { message: "forwardedProps must be less than 64 KB" }),
63
+ });
64
+ export const ResumeSignalSchema = z.discriminatedUnion("type", [
65
+ z.object({
66
+ type: z.literal("tool_result"),
67
+ toolCallId: z.string().min(1).max(128),
68
+ result: z.unknown().refine((value) => isWithinJsonSizeLimit(value, MAX_TOOL_RESULT_BYTES), { message: "Tool result must be less than 64 KB" }),
69
+ isError: z.boolean().optional().default(false),
70
+ }),
71
+ ]);
@@ -0,0 +1,63 @@
1
+ import * as dntShim from "../../_dnt.shims.js";
2
+ export declare class AgentRunCancelledError extends Error {
3
+ constructor(message?: string);
4
+ }
5
+ export declare class AgentRunAlreadyExistsError extends Error {
6
+ constructor(runId: string);
7
+ }
8
+ export declare class RunNotActiveError extends Error {
9
+ constructor(runId: string);
10
+ }
11
+ export declare class ToolResultNotWaitingError extends Error {
12
+ constructor(runId: string, toolCallId: string);
13
+ }
14
+ export declare class ToolResultConflictError extends Error {
15
+ constructor(runId: string, toolCallId: string);
16
+ }
17
+ type SessionStatus = "running" | "waiting" | "completed" | "cancelled" | "failed";
18
+ export interface SubmitToolResultOutcome {
19
+ accepted: true;
20
+ duplicate?: true;
21
+ }
22
+ export declare class AgentRunSessionManager {
23
+ private readonly options;
24
+ private readonly sessions;
25
+ constructor(options?: {
26
+ waitingForToolTtlMs?: number;
27
+ sessionTtlMs?: number | null;
28
+ maxConcurrentSessions?: number;
29
+ setTimeoutFn?: typeof dntShim.setTimeout;
30
+ clearTimeoutFn?: typeof clearTimeout;
31
+ });
32
+ private get waitingForToolTtlMs();
33
+ private get sessionTtlMs();
34
+ private get setTimeoutFn();
35
+ private get clearTimeoutFn();
36
+ private clearWaitingTimeout;
37
+ private clearSessionTimeout;
38
+ private scheduleSessionTimeout;
39
+ private scheduleWaitingTimeout;
40
+ private touchSession;
41
+ private get maxConcurrentSessions();
42
+ startRun(input: {
43
+ runId: string;
44
+ threadId: string;
45
+ }): AbortSignal;
46
+ waitForToolResult(runId: string, toolCallId: string): Promise<{
47
+ result: unknown;
48
+ isError: boolean;
49
+ }>;
50
+ submitToolResult(runId: string, input: {
51
+ toolCallId: string;
52
+ result: unknown;
53
+ isError?: boolean;
54
+ }): SubmitToolResultOutcome;
55
+ cancelRun(runId: string): boolean;
56
+ completeRun(runId: string): void;
57
+ failRun(runId: string): void;
58
+ getRunStatus(runId: string): SessionStatus | null;
59
+ reset(): void;
60
+ }
61
+ export declare const agentRunSessionManager: AgentRunSessionManager;
62
+ export {};
63
+ //# sourceMappingURL=session-manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session-manager.d.ts","sourceRoot":"","sources":["../../../src/src/internal-agents/session-manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,qBAAqB,CAAC;AAyB/C,qBAAa,sBAAuB,SAAQ,KAAK;gBACnC,OAAO,SAAkB;CAItC;AAED,qBAAa,0BAA2B,SAAQ,KAAK;gBACvC,KAAK,EAAE,MAAM;CAI1B;AAED,qBAAa,iBAAkB,SAAQ,KAAK;gBAC9B,KAAK,EAAE,MAAM;CAI1B;AAED,qBAAa,yBAA0B,SAAQ,KAAK;gBACtC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM;CAI9C;AAED,qBAAa,uBAAwB,SAAQ,KAAK;gBACpC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM;CAI9C;AAMD,KAAK,aAAa,G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