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