@copilotkitnext/agent 1.51.4 → 1.51.5-next.1
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/CHANGELOG.md +8 -0
- package/dist/index.cjs +611 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +232 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +147 -140
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +574 -623
- package/dist/index.mjs.map +1 -1
- package/package.json +17 -13
- package/src/__tests__/basic-agent.test.ts +427 -0
- package/src/__tests__/test-helpers.ts +32 -0
- package/src/index.ts +75 -4
- package/{tsup.config.ts → tsdown.config.ts} +4 -3
- package/dist/index.d.ts +0 -225
- package/dist/index.js +0 -675
- package/dist/index.js.map +0 -1
package/CHANGELOG.md
CHANGED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,611 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
let _ag_ui_client = require("@ag-ui/client");
|
|
3
|
+
let ai = require("ai");
|
|
4
|
+
let _ai_sdk_mcp = require("@ai-sdk/mcp");
|
|
5
|
+
let rxjs = require("rxjs");
|
|
6
|
+
let _ai_sdk_openai = require("@ai-sdk/openai");
|
|
7
|
+
let _ai_sdk_anthropic = require("@ai-sdk/anthropic");
|
|
8
|
+
let _ai_sdk_google = require("@ai-sdk/google");
|
|
9
|
+
let crypto = require("crypto");
|
|
10
|
+
let zod = require("zod");
|
|
11
|
+
let _modelcontextprotocol_sdk_client_streamableHttp_js = require("@modelcontextprotocol/sdk/client/streamableHttp.js");
|
|
12
|
+
let _modelcontextprotocol_sdk_client_sse_js = require("@modelcontextprotocol/sdk/client/sse.js");
|
|
13
|
+
|
|
14
|
+
//#region src/index.ts
|
|
15
|
+
/**
|
|
16
|
+
* Resolves a model specifier to a LanguageModel instance
|
|
17
|
+
* @param spec - Model string (e.g., "openai/gpt-4o") or LanguageModel instance
|
|
18
|
+
* @param apiKey - Optional API key to use instead of environment variables
|
|
19
|
+
* @returns LanguageModel instance
|
|
20
|
+
*/
|
|
21
|
+
function resolveModel(spec, apiKey) {
|
|
22
|
+
if (typeof spec !== "string") return spec;
|
|
23
|
+
const parts = spec.replace("/", ":").trim().split(":");
|
|
24
|
+
const rawProvider = parts[0];
|
|
25
|
+
const rest = parts.slice(1);
|
|
26
|
+
if (!rawProvider) throw new Error(`Invalid model string "${spec}". Use "openai/gpt-5", "anthropic/claude-sonnet-4.5", or "google/gemini-2.5-pro".`);
|
|
27
|
+
const provider = rawProvider.toLowerCase();
|
|
28
|
+
const model = rest.join(":").trim();
|
|
29
|
+
if (!model) throw new Error(`Invalid model string "${spec}". Use "openai/gpt-5", "anthropic/claude-sonnet-4.5", or "google/gemini-2.5-pro".`);
|
|
30
|
+
switch (provider) {
|
|
31
|
+
case "openai": return (0, _ai_sdk_openai.createOpenAI)({ apiKey: apiKey || process.env.OPENAI_API_KEY })(model);
|
|
32
|
+
case "anthropic": return (0, _ai_sdk_anthropic.createAnthropic)({ apiKey: apiKey || process.env.ANTHROPIC_API_KEY })(model);
|
|
33
|
+
case "google":
|
|
34
|
+
case "gemini":
|
|
35
|
+
case "google-gemini": return (0, _ai_sdk_google.createGoogleGenerativeAI)({ apiKey: apiKey || process.env.GOOGLE_API_KEY })(model);
|
|
36
|
+
default: throw new Error(`Unknown provider "${provider}" in "${spec}". Supported: openai, anthropic, google (gemini).`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Define a tool for use with BuiltInAgent
|
|
41
|
+
* @param name - The name of the tool
|
|
42
|
+
* @param description - Description of what the tool does
|
|
43
|
+
* @param parameters - Zod schema for the tool's input parameters
|
|
44
|
+
* @param execute - Function to execute the tool server-side
|
|
45
|
+
* @returns Tool definition
|
|
46
|
+
*/
|
|
47
|
+
function defineTool(config) {
|
|
48
|
+
return {
|
|
49
|
+
name: config.name,
|
|
50
|
+
description: config.description,
|
|
51
|
+
parameters: config.parameters,
|
|
52
|
+
execute: config.execute
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function flattenUserMessageContent(content) {
|
|
56
|
+
if (!content) return "";
|
|
57
|
+
if (typeof content === "string") return content;
|
|
58
|
+
return content.map((part) => {
|
|
59
|
+
if (part && typeof part === "object" && "type" in part && part.type === "text" && typeof part.text === "string") return part.text;
|
|
60
|
+
return "";
|
|
61
|
+
}).filter((text) => text.length > 0).join("\n");
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Converts AG-UI messages to Vercel AI SDK ModelMessage format
|
|
65
|
+
*/
|
|
66
|
+
function convertMessagesToVercelAISDKMessages(messages, options = {}) {
|
|
67
|
+
const result = [];
|
|
68
|
+
for (const message of messages) if (message.role === "system" && options.forwardSystemMessages) {
|
|
69
|
+
const systemMsg = {
|
|
70
|
+
role: "system",
|
|
71
|
+
content: message.content ?? ""
|
|
72
|
+
};
|
|
73
|
+
result.push(systemMsg);
|
|
74
|
+
} else if (message.role === "developer" && options.forwardDeveloperMessages) {
|
|
75
|
+
const systemMsg = {
|
|
76
|
+
role: "system",
|
|
77
|
+
content: message.content ?? ""
|
|
78
|
+
};
|
|
79
|
+
result.push(systemMsg);
|
|
80
|
+
} else if (message.role === "assistant") {
|
|
81
|
+
const parts = message.content ? [{
|
|
82
|
+
type: "text",
|
|
83
|
+
text: message.content
|
|
84
|
+
}] : [];
|
|
85
|
+
for (const toolCall of message.toolCalls ?? []) {
|
|
86
|
+
const toolCallPart = {
|
|
87
|
+
type: "tool-call",
|
|
88
|
+
toolCallId: toolCall.id,
|
|
89
|
+
toolName: toolCall.function.name,
|
|
90
|
+
input: JSON.parse(toolCall.function.arguments)
|
|
91
|
+
};
|
|
92
|
+
parts.push(toolCallPart);
|
|
93
|
+
}
|
|
94
|
+
const assistantMsg = {
|
|
95
|
+
role: "assistant",
|
|
96
|
+
content: parts
|
|
97
|
+
};
|
|
98
|
+
result.push(assistantMsg);
|
|
99
|
+
} else if (message.role === "user") {
|
|
100
|
+
const userMsg = {
|
|
101
|
+
role: "user",
|
|
102
|
+
content: flattenUserMessageContent(message.content)
|
|
103
|
+
};
|
|
104
|
+
result.push(userMsg);
|
|
105
|
+
} else if (message.role === "tool") {
|
|
106
|
+
let toolName = "unknown";
|
|
107
|
+
for (const msg of messages) if (msg.role === "assistant") {
|
|
108
|
+
for (const toolCall of msg.toolCalls ?? []) if (toolCall.id === message.toolCallId) {
|
|
109
|
+
toolName = toolCall.function.name;
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const toolMsg = {
|
|
114
|
+
role: "tool",
|
|
115
|
+
content: [{
|
|
116
|
+
type: "tool-result",
|
|
117
|
+
toolCallId: message.toolCallId,
|
|
118
|
+
toolName,
|
|
119
|
+
output: {
|
|
120
|
+
type: "text",
|
|
121
|
+
value: message.content
|
|
122
|
+
}
|
|
123
|
+
}]
|
|
124
|
+
};
|
|
125
|
+
result.push(toolMsg);
|
|
126
|
+
}
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Converts JSON Schema to Zod schema
|
|
131
|
+
*/
|
|
132
|
+
function convertJsonSchemaToZodSchema(jsonSchema, required) {
|
|
133
|
+
if (!jsonSchema.type) return required ? zod.z.object({}) : zod.z.object({}).optional();
|
|
134
|
+
if (jsonSchema.type === "object") {
|
|
135
|
+
const spec = {};
|
|
136
|
+
if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) return !required ? zod.z.object(spec).optional() : zod.z.object(spec);
|
|
137
|
+
for (const [key, value] of Object.entries(jsonSchema.properties)) spec[key] = convertJsonSchemaToZodSchema(value, jsonSchema.required ? jsonSchema.required.includes(key) : false);
|
|
138
|
+
let schema = zod.z.object(spec).describe(jsonSchema.description ?? "");
|
|
139
|
+
return required ? schema : schema.optional();
|
|
140
|
+
} else if (jsonSchema.type === "string") {
|
|
141
|
+
let schema = zod.z.string().describe(jsonSchema.description ?? "");
|
|
142
|
+
return required ? schema : schema.optional();
|
|
143
|
+
} else if (jsonSchema.type === "number" || jsonSchema.type === "integer") {
|
|
144
|
+
let schema = zod.z.number().describe(jsonSchema.description ?? "");
|
|
145
|
+
return required ? schema : schema.optional();
|
|
146
|
+
} else if (jsonSchema.type === "boolean") {
|
|
147
|
+
let schema = zod.z.boolean().describe(jsonSchema.description ?? "");
|
|
148
|
+
return required ? schema : schema.optional();
|
|
149
|
+
} else if (jsonSchema.type === "array") {
|
|
150
|
+
if (!jsonSchema.items) throw new Error("Array type must have items property");
|
|
151
|
+
let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);
|
|
152
|
+
let schema = zod.z.array(itemSchema).describe(jsonSchema.description ?? "");
|
|
153
|
+
return required ? schema : schema.optional();
|
|
154
|
+
}
|
|
155
|
+
console.error("Invalid JSON schema:", JSON.stringify(jsonSchema, null, 2));
|
|
156
|
+
throw new Error("Invalid JSON schema");
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Converts AG-UI tools to Vercel AI SDK ToolSet
|
|
160
|
+
*/
|
|
161
|
+
function isJsonSchema(obj) {
|
|
162
|
+
if (typeof obj !== "object" || obj === null) return false;
|
|
163
|
+
const schema = obj;
|
|
164
|
+
if (Object.keys(schema).length === 0) return true;
|
|
165
|
+
return typeof schema.type === "string" && [
|
|
166
|
+
"object",
|
|
167
|
+
"string",
|
|
168
|
+
"number",
|
|
169
|
+
"integer",
|
|
170
|
+
"boolean",
|
|
171
|
+
"array"
|
|
172
|
+
].includes(schema.type);
|
|
173
|
+
}
|
|
174
|
+
function convertToolsToVercelAITools(tools) {
|
|
175
|
+
const result = {};
|
|
176
|
+
for (const tool of tools) {
|
|
177
|
+
if (!isJsonSchema(tool.parameters)) throw new Error(`Invalid JSON schema for tool ${tool.name}`);
|
|
178
|
+
const zodSchema = convertJsonSchemaToZodSchema(tool.parameters, true);
|
|
179
|
+
result[tool.name] = (0, ai.tool)({
|
|
180
|
+
description: tool.description,
|
|
181
|
+
inputSchema: zodSchema
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
return result;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Converts ToolDefinition array to Vercel AI SDK ToolSet
|
|
188
|
+
*/
|
|
189
|
+
function convertToolDefinitionsToVercelAITools(tools) {
|
|
190
|
+
const result = {};
|
|
191
|
+
for (const tool of tools) result[tool.name] = (0, ai.tool)({
|
|
192
|
+
description: tool.description,
|
|
193
|
+
inputSchema: tool.parameters,
|
|
194
|
+
execute: tool.execute
|
|
195
|
+
});
|
|
196
|
+
return result;
|
|
197
|
+
}
|
|
198
|
+
var BuiltInAgent = class BuiltInAgent extends _ag_ui_client.AbstractAgent {
|
|
199
|
+
abortController;
|
|
200
|
+
constructor(config) {
|
|
201
|
+
super();
|
|
202
|
+
this.config = config;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Check if a property can be overridden by forwardedProps
|
|
206
|
+
*/
|
|
207
|
+
canOverride(property) {
|
|
208
|
+
return this.config?.overridableProperties?.includes(property) ?? false;
|
|
209
|
+
}
|
|
210
|
+
run(input) {
|
|
211
|
+
return new rxjs.Observable((subscriber) => {
|
|
212
|
+
const startEvent = {
|
|
213
|
+
type: _ag_ui_client.EventType.RUN_STARTED,
|
|
214
|
+
threadId: input.threadId,
|
|
215
|
+
runId: input.runId
|
|
216
|
+
};
|
|
217
|
+
subscriber.next(startEvent);
|
|
218
|
+
const model = resolveModel(this.config.model, this.config.apiKey);
|
|
219
|
+
let systemPrompt = void 0;
|
|
220
|
+
const hasPrompt = !!this.config.prompt;
|
|
221
|
+
const hasContext = input.context && input.context.length > 0;
|
|
222
|
+
const hasState = input.state !== void 0 && input.state !== null && !(typeof input.state === "object" && Object.keys(input.state).length === 0);
|
|
223
|
+
if (hasPrompt || hasContext || hasState) {
|
|
224
|
+
const parts = [];
|
|
225
|
+
if (hasPrompt) parts.push(this.config.prompt);
|
|
226
|
+
if (hasContext) {
|
|
227
|
+
parts.push("\n## Context from the application\n");
|
|
228
|
+
for (const ctx of input.context) parts.push(`${ctx.description}:\n${ctx.value}\n`);
|
|
229
|
+
}
|
|
230
|
+
if (hasState) parts.push(`
|
|
231
|
+
## Application State
|
|
232
|
+
This is state from the application that you can edit by calling AGUISendStateSnapshot or AGUISendStateDelta.
|
|
233
|
+
\`\`\`json\n${JSON.stringify(input.state, null, 2)}\n\`\`\`\n`);
|
|
234
|
+
systemPrompt = parts.join("");
|
|
235
|
+
}
|
|
236
|
+
const messages = convertMessagesToVercelAISDKMessages(input.messages, {
|
|
237
|
+
forwardSystemMessages: this.config.forwardSystemMessages,
|
|
238
|
+
forwardDeveloperMessages: this.config.forwardDeveloperMessages
|
|
239
|
+
});
|
|
240
|
+
if (systemPrompt) messages.unshift({
|
|
241
|
+
role: "system",
|
|
242
|
+
content: systemPrompt
|
|
243
|
+
});
|
|
244
|
+
let allTools = convertToolsToVercelAITools(input.tools);
|
|
245
|
+
if (this.config.tools && this.config.tools.length > 0) {
|
|
246
|
+
const configTools = convertToolDefinitionsToVercelAITools(this.config.tools);
|
|
247
|
+
allTools = {
|
|
248
|
+
...allTools,
|
|
249
|
+
...configTools
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
const streamTextParams = {
|
|
253
|
+
model,
|
|
254
|
+
messages,
|
|
255
|
+
tools: allTools,
|
|
256
|
+
toolChoice: this.config.toolChoice,
|
|
257
|
+
stopWhen: this.config.maxSteps ? (0, ai.stepCountIs)(this.config.maxSteps) : void 0,
|
|
258
|
+
maxOutputTokens: this.config.maxOutputTokens,
|
|
259
|
+
temperature: this.config.temperature,
|
|
260
|
+
topP: this.config.topP,
|
|
261
|
+
topK: this.config.topK,
|
|
262
|
+
presencePenalty: this.config.presencePenalty,
|
|
263
|
+
frequencyPenalty: this.config.frequencyPenalty,
|
|
264
|
+
stopSequences: this.config.stopSequences,
|
|
265
|
+
seed: this.config.seed,
|
|
266
|
+
providerOptions: this.config.providerOptions,
|
|
267
|
+
maxRetries: this.config.maxRetries
|
|
268
|
+
};
|
|
269
|
+
if (input.forwardedProps && typeof input.forwardedProps === "object") {
|
|
270
|
+
const props = input.forwardedProps;
|
|
271
|
+
if (props.model !== void 0 && this.canOverride("model")) {
|
|
272
|
+
if (typeof props.model === "string" || typeof props.model === "object") streamTextParams.model = resolveModel(props.model, this.config.apiKey);
|
|
273
|
+
}
|
|
274
|
+
if (props.toolChoice !== void 0 && this.canOverride("toolChoice")) {
|
|
275
|
+
const toolChoice = props.toolChoice;
|
|
276
|
+
if (toolChoice === "auto" || toolChoice === "required" || toolChoice === "none" || typeof toolChoice === "object" && toolChoice !== null && "type" in toolChoice && toolChoice.type === "tool") streamTextParams.toolChoice = toolChoice;
|
|
277
|
+
}
|
|
278
|
+
if (typeof props.maxOutputTokens === "number" && this.canOverride("maxOutputTokens")) streamTextParams.maxOutputTokens = props.maxOutputTokens;
|
|
279
|
+
if (typeof props.temperature === "number" && this.canOverride("temperature")) streamTextParams.temperature = props.temperature;
|
|
280
|
+
if (typeof props.topP === "number" && this.canOverride("topP")) streamTextParams.topP = props.topP;
|
|
281
|
+
if (typeof props.topK === "number" && this.canOverride("topK")) streamTextParams.topK = props.topK;
|
|
282
|
+
if (typeof props.presencePenalty === "number" && this.canOverride("presencePenalty")) streamTextParams.presencePenalty = props.presencePenalty;
|
|
283
|
+
if (typeof props.frequencyPenalty === "number" && this.canOverride("frequencyPenalty")) streamTextParams.frequencyPenalty = props.frequencyPenalty;
|
|
284
|
+
if (Array.isArray(props.stopSequences) && this.canOverride("stopSequences")) {
|
|
285
|
+
if (props.stopSequences.every((item) => typeof item === "string")) streamTextParams.stopSequences = props.stopSequences;
|
|
286
|
+
}
|
|
287
|
+
if (typeof props.seed === "number" && this.canOverride("seed")) streamTextParams.seed = props.seed;
|
|
288
|
+
if (typeof props.maxRetries === "number" && this.canOverride("maxRetries")) streamTextParams.maxRetries = props.maxRetries;
|
|
289
|
+
if (props.providerOptions !== void 0 && this.canOverride("providerOptions")) {
|
|
290
|
+
if (typeof props.providerOptions === "object" && props.providerOptions !== null) streamTextParams.providerOptions = props.providerOptions;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
const mcpClients = [];
|
|
294
|
+
(async () => {
|
|
295
|
+
const abortController = new AbortController();
|
|
296
|
+
this.abortController = abortController;
|
|
297
|
+
let terminalEventEmitted = false;
|
|
298
|
+
try {
|
|
299
|
+
streamTextParams.tools = {
|
|
300
|
+
...streamTextParams.tools,
|
|
301
|
+
AGUISendStateSnapshot: (0, ai.tool)({
|
|
302
|
+
description: "Replace the entire application state with a new snapshot",
|
|
303
|
+
inputSchema: zod.z.object({ snapshot: zod.z.any().describe("The complete new state object") }),
|
|
304
|
+
execute: async ({ snapshot }) => {
|
|
305
|
+
return {
|
|
306
|
+
success: true,
|
|
307
|
+
snapshot
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
}),
|
|
311
|
+
AGUISendStateDelta: (0, ai.tool)({
|
|
312
|
+
description: "Apply incremental updates to application state using JSON Patch operations",
|
|
313
|
+
inputSchema: zod.z.object({ delta: zod.z.array(zod.z.object({
|
|
314
|
+
op: zod.z.enum([
|
|
315
|
+
"add",
|
|
316
|
+
"replace",
|
|
317
|
+
"remove"
|
|
318
|
+
]).describe("The operation to perform"),
|
|
319
|
+
path: zod.z.string().describe("JSON Pointer path (e.g., '/foo/bar')"),
|
|
320
|
+
value: zod.z.any().optional().describe("The value to set. Required for 'add' and 'replace' operations, ignored for 'remove'.")
|
|
321
|
+
})).describe("Array of JSON Patch operations") }),
|
|
322
|
+
execute: async ({ delta }) => {
|
|
323
|
+
return {
|
|
324
|
+
success: true,
|
|
325
|
+
delta
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
})
|
|
329
|
+
};
|
|
330
|
+
if (this.config.mcpServers && this.config.mcpServers.length > 0) for (const serverConfig of this.config.mcpServers) {
|
|
331
|
+
let transport;
|
|
332
|
+
if (serverConfig.type === "http") transport = new _modelcontextprotocol_sdk_client_streamableHttp_js.StreamableHTTPClientTransport(new URL(serverConfig.url), serverConfig.options);
|
|
333
|
+
else if (serverConfig.type === "sse") transport = new _modelcontextprotocol_sdk_client_sse_js.SSEClientTransport(new URL(serverConfig.url), serverConfig.headers);
|
|
334
|
+
if (transport) {
|
|
335
|
+
const mcpClient = await (0, _ai_sdk_mcp.experimental_createMCPClient)({ transport });
|
|
336
|
+
mcpClients.push(mcpClient);
|
|
337
|
+
const mcpTools = await mcpClient.tools();
|
|
338
|
+
streamTextParams.tools = {
|
|
339
|
+
...streamTextParams.tools,
|
|
340
|
+
...mcpTools
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
const response = (0, ai.streamText)({
|
|
345
|
+
...streamTextParams,
|
|
346
|
+
abortSignal: abortController.signal
|
|
347
|
+
});
|
|
348
|
+
let messageId = (0, crypto.randomUUID)();
|
|
349
|
+
let reasoningMessageId = (0, crypto.randomUUID)();
|
|
350
|
+
const toolCallStates = /* @__PURE__ */ new Map();
|
|
351
|
+
const ensureToolCallState = (toolCallId) => {
|
|
352
|
+
let state = toolCallStates.get(toolCallId);
|
|
353
|
+
if (!state) {
|
|
354
|
+
state = {
|
|
355
|
+
started: false,
|
|
356
|
+
hasArgsDelta: false,
|
|
357
|
+
ended: false
|
|
358
|
+
};
|
|
359
|
+
toolCallStates.set(toolCallId, state);
|
|
360
|
+
}
|
|
361
|
+
return state;
|
|
362
|
+
};
|
|
363
|
+
for await (const part of response.fullStream) switch (part.type) {
|
|
364
|
+
case "abort": {
|
|
365
|
+
const abortEndEvent = {
|
|
366
|
+
type: _ag_ui_client.EventType.RUN_FINISHED,
|
|
367
|
+
threadId: input.threadId,
|
|
368
|
+
runId: input.runId
|
|
369
|
+
};
|
|
370
|
+
subscriber.next(abortEndEvent);
|
|
371
|
+
terminalEventEmitted = true;
|
|
372
|
+
subscriber.complete();
|
|
373
|
+
break;
|
|
374
|
+
}
|
|
375
|
+
case "reasoning-start": {
|
|
376
|
+
const providedId = "id" in part ? part.id : void 0;
|
|
377
|
+
if (providedId && providedId !== "0") reasoningMessageId = providedId;
|
|
378
|
+
const reasoningStartEvent = {
|
|
379
|
+
type: _ag_ui_client.EventType.REASONING_START,
|
|
380
|
+
messageId: reasoningMessageId
|
|
381
|
+
};
|
|
382
|
+
subscriber.next(reasoningStartEvent);
|
|
383
|
+
const reasoningMessageStart = {
|
|
384
|
+
type: _ag_ui_client.EventType.REASONING_MESSAGE_START,
|
|
385
|
+
messageId: reasoningMessageId,
|
|
386
|
+
role: "reasoning"
|
|
387
|
+
};
|
|
388
|
+
subscriber.next(reasoningMessageStart);
|
|
389
|
+
break;
|
|
390
|
+
}
|
|
391
|
+
case "reasoning-delta": {
|
|
392
|
+
const reasoningDeltaEvent = {
|
|
393
|
+
type: _ag_ui_client.EventType.REASONING_MESSAGE_CONTENT,
|
|
394
|
+
messageId: reasoningMessageId,
|
|
395
|
+
delta: ("text" in part ? part.text : part.delta) ?? ""
|
|
396
|
+
};
|
|
397
|
+
subscriber.next(reasoningDeltaEvent);
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
case "reasoning-end": {
|
|
401
|
+
const reasoningMessageEnd = {
|
|
402
|
+
type: _ag_ui_client.EventType.REASONING_MESSAGE_END,
|
|
403
|
+
messageId: reasoningMessageId
|
|
404
|
+
};
|
|
405
|
+
subscriber.next(reasoningMessageEnd);
|
|
406
|
+
const reasoningEndEvent = {
|
|
407
|
+
type: _ag_ui_client.EventType.REASONING_END,
|
|
408
|
+
messageId: reasoningMessageId
|
|
409
|
+
};
|
|
410
|
+
subscriber.next(reasoningEndEvent);
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
case "tool-input-start": {
|
|
414
|
+
const toolCallId = part.id;
|
|
415
|
+
const state = ensureToolCallState(toolCallId);
|
|
416
|
+
state.toolName = part.toolName;
|
|
417
|
+
if (!state.started) {
|
|
418
|
+
state.started = true;
|
|
419
|
+
const startEvent = {
|
|
420
|
+
type: _ag_ui_client.EventType.TOOL_CALL_START,
|
|
421
|
+
parentMessageId: messageId,
|
|
422
|
+
toolCallId,
|
|
423
|
+
toolCallName: part.toolName
|
|
424
|
+
};
|
|
425
|
+
subscriber.next(startEvent);
|
|
426
|
+
}
|
|
427
|
+
break;
|
|
428
|
+
}
|
|
429
|
+
case "tool-input-delta": {
|
|
430
|
+
const toolCallId = part.id;
|
|
431
|
+
const state = ensureToolCallState(toolCallId);
|
|
432
|
+
state.hasArgsDelta = true;
|
|
433
|
+
const argsEvent = {
|
|
434
|
+
type: _ag_ui_client.EventType.TOOL_CALL_ARGS,
|
|
435
|
+
toolCallId,
|
|
436
|
+
delta: part.delta
|
|
437
|
+
};
|
|
438
|
+
subscriber.next(argsEvent);
|
|
439
|
+
break;
|
|
440
|
+
}
|
|
441
|
+
case "tool-input-end": break;
|
|
442
|
+
case "text-start": {
|
|
443
|
+
const providedId = "id" in part ? part.id : void 0;
|
|
444
|
+
messageId = providedId && providedId !== "0" ? providedId : (0, crypto.randomUUID)();
|
|
445
|
+
break;
|
|
446
|
+
}
|
|
447
|
+
case "text-delta": {
|
|
448
|
+
const textDelta = "text" in part ? part.text : "";
|
|
449
|
+
const textEvent = {
|
|
450
|
+
type: _ag_ui_client.EventType.TEXT_MESSAGE_CHUNK,
|
|
451
|
+
role: "assistant",
|
|
452
|
+
messageId,
|
|
453
|
+
delta: textDelta
|
|
454
|
+
};
|
|
455
|
+
subscriber.next(textEvent);
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
case "tool-call": {
|
|
459
|
+
const toolCallId = part.toolCallId;
|
|
460
|
+
const state = ensureToolCallState(toolCallId);
|
|
461
|
+
state.toolName = part.toolName ?? state.toolName;
|
|
462
|
+
if (!state.started) {
|
|
463
|
+
state.started = true;
|
|
464
|
+
const startEvent = {
|
|
465
|
+
type: _ag_ui_client.EventType.TOOL_CALL_START,
|
|
466
|
+
parentMessageId: messageId,
|
|
467
|
+
toolCallId,
|
|
468
|
+
toolCallName: part.toolName
|
|
469
|
+
};
|
|
470
|
+
subscriber.next(startEvent);
|
|
471
|
+
}
|
|
472
|
+
if (!state.hasArgsDelta && "input" in part && part.input !== void 0) {
|
|
473
|
+
let serializedInput = "";
|
|
474
|
+
if (typeof part.input === "string") serializedInput = part.input;
|
|
475
|
+
else try {
|
|
476
|
+
serializedInput = JSON.stringify(part.input);
|
|
477
|
+
} catch {
|
|
478
|
+
serializedInput = String(part.input);
|
|
479
|
+
}
|
|
480
|
+
if (serializedInput.length > 0) {
|
|
481
|
+
const argsEvent = {
|
|
482
|
+
type: _ag_ui_client.EventType.TOOL_CALL_ARGS,
|
|
483
|
+
toolCallId,
|
|
484
|
+
delta: serializedInput
|
|
485
|
+
};
|
|
486
|
+
subscriber.next(argsEvent);
|
|
487
|
+
state.hasArgsDelta = true;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
if (!state.ended) {
|
|
491
|
+
state.ended = true;
|
|
492
|
+
const endEvent = {
|
|
493
|
+
type: _ag_ui_client.EventType.TOOL_CALL_END,
|
|
494
|
+
toolCallId
|
|
495
|
+
};
|
|
496
|
+
subscriber.next(endEvent);
|
|
497
|
+
}
|
|
498
|
+
break;
|
|
499
|
+
}
|
|
500
|
+
case "tool-result": {
|
|
501
|
+
const toolResult = "output" in part ? part.output : null;
|
|
502
|
+
const toolName = "toolName" in part ? part.toolName : "";
|
|
503
|
+
toolCallStates.delete(part.toolCallId);
|
|
504
|
+
if (toolName === "AGUISendStateSnapshot" && toolResult && typeof toolResult === "object") {
|
|
505
|
+
const stateSnapshotEvent = {
|
|
506
|
+
type: _ag_ui_client.EventType.STATE_SNAPSHOT,
|
|
507
|
+
snapshot: toolResult.snapshot
|
|
508
|
+
};
|
|
509
|
+
subscriber.next(stateSnapshotEvent);
|
|
510
|
+
} else if (toolName === "AGUISendStateDelta" && toolResult && typeof toolResult === "object") {
|
|
511
|
+
const stateDeltaEvent = {
|
|
512
|
+
type: _ag_ui_client.EventType.STATE_DELTA,
|
|
513
|
+
delta: toolResult.delta
|
|
514
|
+
};
|
|
515
|
+
subscriber.next(stateDeltaEvent);
|
|
516
|
+
}
|
|
517
|
+
const resultEvent = {
|
|
518
|
+
type: _ag_ui_client.EventType.TOOL_CALL_RESULT,
|
|
519
|
+
role: "tool",
|
|
520
|
+
messageId: (0, crypto.randomUUID)(),
|
|
521
|
+
toolCallId: part.toolCallId,
|
|
522
|
+
content: JSON.stringify(toolResult)
|
|
523
|
+
};
|
|
524
|
+
subscriber.next(resultEvent);
|
|
525
|
+
break;
|
|
526
|
+
}
|
|
527
|
+
case "finish": {
|
|
528
|
+
const finishedEvent = {
|
|
529
|
+
type: _ag_ui_client.EventType.RUN_FINISHED,
|
|
530
|
+
threadId: input.threadId,
|
|
531
|
+
runId: input.runId
|
|
532
|
+
};
|
|
533
|
+
subscriber.next(finishedEvent);
|
|
534
|
+
terminalEventEmitted = true;
|
|
535
|
+
subscriber.complete();
|
|
536
|
+
break;
|
|
537
|
+
}
|
|
538
|
+
case "error": {
|
|
539
|
+
if (abortController.signal.aborted) break;
|
|
540
|
+
const runErrorEvent = {
|
|
541
|
+
type: _ag_ui_client.EventType.RUN_ERROR,
|
|
542
|
+
message: part.error + ""
|
|
543
|
+
};
|
|
544
|
+
subscriber.next(runErrorEvent);
|
|
545
|
+
terminalEventEmitted = true;
|
|
546
|
+
subscriber.error(part.error);
|
|
547
|
+
break;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
if (!terminalEventEmitted) {
|
|
551
|
+
if (abortController.signal.aborted) {} else {
|
|
552
|
+
const finishedEvent = {
|
|
553
|
+
type: _ag_ui_client.EventType.RUN_FINISHED,
|
|
554
|
+
threadId: input.threadId,
|
|
555
|
+
runId: input.runId
|
|
556
|
+
};
|
|
557
|
+
subscriber.next(finishedEvent);
|
|
558
|
+
}
|
|
559
|
+
terminalEventEmitted = true;
|
|
560
|
+
subscriber.complete();
|
|
561
|
+
}
|
|
562
|
+
} catch (error) {
|
|
563
|
+
if (abortController.signal.aborted) subscriber.complete();
|
|
564
|
+
else {
|
|
565
|
+
const runErrorEvent = {
|
|
566
|
+
type: _ag_ui_client.EventType.RUN_ERROR,
|
|
567
|
+
message: error + ""
|
|
568
|
+
};
|
|
569
|
+
subscriber.next(runErrorEvent);
|
|
570
|
+
terminalEventEmitted = true;
|
|
571
|
+
subscriber.error(error);
|
|
572
|
+
}
|
|
573
|
+
} finally {
|
|
574
|
+
this.abortController = void 0;
|
|
575
|
+
await Promise.all(mcpClients.map((client) => client.close()));
|
|
576
|
+
}
|
|
577
|
+
})();
|
|
578
|
+
return () => {
|
|
579
|
+
Promise.all(mcpClients.map((client) => client.close())).catch(() => {});
|
|
580
|
+
};
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
clone() {
|
|
584
|
+
const cloned = new BuiltInAgent(this.config);
|
|
585
|
+
cloned.middlewares = [...this.middlewares];
|
|
586
|
+
return cloned;
|
|
587
|
+
}
|
|
588
|
+
abortRun() {
|
|
589
|
+
this.abortController?.abort();
|
|
590
|
+
}
|
|
591
|
+
};
|
|
592
|
+
/**
|
|
593
|
+
* @deprecated Use BuiltInAgent instead
|
|
594
|
+
*/
|
|
595
|
+
var BasicAgent = class extends BuiltInAgent {
|
|
596
|
+
constructor(config) {
|
|
597
|
+
super(config);
|
|
598
|
+
console.warn("BasicAgent is deprecated, use BuiltInAgent instead");
|
|
599
|
+
}
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
//#endregion
|
|
603
|
+
exports.BasicAgent = BasicAgent;
|
|
604
|
+
exports.BuiltInAgent = BuiltInAgent;
|
|
605
|
+
exports.convertJsonSchemaToZodSchema = convertJsonSchemaToZodSchema;
|
|
606
|
+
exports.convertMessagesToVercelAISDKMessages = convertMessagesToVercelAISDKMessages;
|
|
607
|
+
exports.convertToolDefinitionsToVercelAITools = convertToolDefinitionsToVercelAITools;
|
|
608
|
+
exports.convertToolsToVercelAITools = convertToolsToVercelAITools;
|
|
609
|
+
exports.defineTool = defineTool;
|
|
610
|
+
exports.resolveModel = resolveModel;
|
|
611
|
+
//# sourceMappingURL=index.cjs.map
|