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