agents 0.0.0-c3e8618 → 0.0.0-c41ebbc
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/README.md +255 -27
- package/dist/_esm-LV5FJ3HK.js +3922 -0
- package/dist/_esm-LV5FJ3HK.js.map +1 -0
- package/dist/ai-chat-agent.d.ts +78 -23
- package/dist/ai-chat-agent.js +657 -0
- package/dist/ai-chat-agent.js.map +1 -0
- package/dist/ai-chat-v5-migration.d.ts +152 -0
- package/dist/ai-chat-v5-migration.js +20 -0
- package/dist/ai-chat-v5-migration.js.map +1 -0
- package/dist/ai-react.d.ts +86 -47
- package/dist/ai-react.js +356 -0
- package/dist/ai-react.js.map +1 -0
- package/dist/ai-types.d.ts +84 -41
- package/dist/ai-types.js +8 -0
- package/dist/ai-types.js.map +1 -0
- package/dist/ccip-CMBYN64O.js +15 -0
- package/dist/ccip-CMBYN64O.js.map +1 -0
- package/dist/chunk-5Y6BEZDY.js +276 -0
- package/dist/chunk-5Y6BEZDY.js.map +1 -0
- package/dist/chunk-BER7KXUJ.js +18 -0
- package/dist/chunk-BER7KXUJ.js.map +1 -0
- package/dist/chunk-C2OEBJZ2.js +113 -0
- package/dist/chunk-C2OEBJZ2.js.map +1 -0
- package/dist/chunk-JJBFIGUC.js +5202 -0
- package/dist/chunk-JJBFIGUC.js.map +1 -0
- package/dist/chunk-PR4QN5HX.js +43 -0
- package/dist/chunk-PR4QN5HX.js.map +1 -0
- package/dist/chunk-QEVM4BVL.js +116 -0
- package/dist/chunk-QEVM4BVL.js.map +1 -0
- package/dist/chunk-TYAY6AU6.js +159 -0
- package/dist/chunk-TYAY6AU6.js.map +1 -0
- package/dist/chunk-UJVEAURM.js +150 -0
- package/dist/chunk-UJVEAURM.js.map +1 -0
- package/dist/chunk-XGMKNUJA.js +1343 -0
- package/dist/chunk-XGMKNUJA.js.map +1 -0
- package/dist/chunk-ZMMHNOMZ.js +942 -0
- package/dist/chunk-ZMMHNOMZ.js.map +1 -0
- package/dist/client-D1Fa-xS_.d.ts +5120 -0
- package/dist/client.d.ts +71 -37
- package/dist/client.js +13 -0
- package/dist/client.js.map +1 -0
- package/dist/codemode/ai.d.ts +25 -0
- package/dist/codemode/ai.js +5200 -0
- package/dist/codemode/ai.js.map +1 -0
- package/dist/index.d.ts +479 -181
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/client.d.ts +12 -675
- package/dist/mcp/client.js +10 -0
- package/dist/mcp/client.js.map +1 -0
- package/dist/mcp/do-oauth-client-provider.d.ts +42 -0
- package/dist/mcp/do-oauth-client-provider.js +8 -0
- package/dist/mcp/do-oauth-client-provider.js.map +1 -0
- package/dist/mcp/index.d.ts +71 -33
- package/dist/mcp/index.js +1099 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/x402.d.ts +39 -0
- package/dist/mcp/x402.js +3195 -0
- package/dist/mcp/x402.js.map +1 -0
- package/dist/mcp-BH1fJeiU.d.ts +58 -0
- package/dist/observability/index.d.ts +34 -0
- package/dist/observability/index.js +12 -0
- package/dist/observability/index.js.map +1 -0
- package/dist/react.d.ts +115 -18
- package/dist/react.js +216 -0
- package/dist/react.js.map +1 -0
- package/dist/schedule.d.ts +106 -22
- package/dist/schedule.js +86 -0
- package/dist/schedule.js.map +1 -0
- package/dist/secp256k1-M22GZP2U.js +2193 -0
- package/dist/secp256k1-M22GZP2U.js.map +1 -0
- package/dist/serializable.d.ts +32 -0
- package/dist/serializable.js +1 -0
- package/dist/serializable.js.map +1 -0
- package/package.json +103 -52
- package/src/index.ts +1268 -156
|
@@ -0,0 +1,657 @@
|
|
|
1
|
+
import {
|
|
2
|
+
autoTransformMessages
|
|
3
|
+
} from "./chunk-UJVEAURM.js";
|
|
4
|
+
import {
|
|
5
|
+
Agent
|
|
6
|
+
} from "./chunk-XGMKNUJA.js";
|
|
7
|
+
import "./chunk-ZMMHNOMZ.js";
|
|
8
|
+
import "./chunk-C2OEBJZ2.js";
|
|
9
|
+
import "./chunk-QEVM4BVL.js";
|
|
10
|
+
import "./chunk-BER7KXUJ.js";
|
|
11
|
+
import "./chunk-PR4QN5HX.js";
|
|
12
|
+
|
|
13
|
+
// src/ai-chat-agent.ts
|
|
14
|
+
import {
|
|
15
|
+
getToolName,
|
|
16
|
+
isToolUIPart,
|
|
17
|
+
parsePartialJson
|
|
18
|
+
} from "ai";
|
|
19
|
+
var decoder = new TextDecoder();
|
|
20
|
+
var AIChatAgent = class extends Agent {
|
|
21
|
+
constructor(ctx, env) {
|
|
22
|
+
super(ctx, env);
|
|
23
|
+
this.sql`create table if not exists cf_ai_chat_agent_messages (
|
|
24
|
+
id text primary key,
|
|
25
|
+
message text not null,
|
|
26
|
+
created_at datetime default current_timestamp
|
|
27
|
+
)`;
|
|
28
|
+
const rawMessages = (this.sql`select * from cf_ai_chat_agent_messages` || []).map((row) => {
|
|
29
|
+
return JSON.parse(row.message);
|
|
30
|
+
});
|
|
31
|
+
this.messages = autoTransformMessages(rawMessages);
|
|
32
|
+
this._chatMessageAbortControllers = /* @__PURE__ */ new Map();
|
|
33
|
+
}
|
|
34
|
+
_broadcastChatMessage(message, exclude) {
|
|
35
|
+
this.broadcast(JSON.stringify(message), exclude);
|
|
36
|
+
}
|
|
37
|
+
async onMessage(connection, message) {
|
|
38
|
+
if (typeof message === "string") {
|
|
39
|
+
let data;
|
|
40
|
+
try {
|
|
41
|
+
data = JSON.parse(message);
|
|
42
|
+
} catch (_error) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (data.type === "cf_agent_use_chat_request" /* CF_AGENT_USE_CHAT_REQUEST */ && data.init.method === "POST") {
|
|
46
|
+
const {
|
|
47
|
+
// method,
|
|
48
|
+
// keepalive,
|
|
49
|
+
// headers,
|
|
50
|
+
body
|
|
51
|
+
// we're reading this
|
|
52
|
+
//
|
|
53
|
+
// // these might not exist?
|
|
54
|
+
// dispatcher,
|
|
55
|
+
// duplex
|
|
56
|
+
} = data.init;
|
|
57
|
+
const { messages } = JSON.parse(body);
|
|
58
|
+
const transformedMessages = autoTransformMessages(messages);
|
|
59
|
+
this._broadcastChatMessage(
|
|
60
|
+
{
|
|
61
|
+
messages: transformedMessages,
|
|
62
|
+
type: "cf_agent_chat_messages" /* CF_AGENT_CHAT_MESSAGES */
|
|
63
|
+
},
|
|
64
|
+
[connection.id]
|
|
65
|
+
);
|
|
66
|
+
await this.persistMessages(transformedMessages, [connection.id]);
|
|
67
|
+
this.observability?.emit(
|
|
68
|
+
{
|
|
69
|
+
displayMessage: "Chat message request",
|
|
70
|
+
id: data.id,
|
|
71
|
+
payload: {},
|
|
72
|
+
timestamp: Date.now(),
|
|
73
|
+
type: "message:request"
|
|
74
|
+
},
|
|
75
|
+
this.ctx
|
|
76
|
+
);
|
|
77
|
+
const chatMessageId = data.id;
|
|
78
|
+
const abortSignal = this._getAbortSignal(chatMessageId);
|
|
79
|
+
return this._tryCatchChat(async () => {
|
|
80
|
+
const response = await this.onChatMessage(
|
|
81
|
+
async (_finishResult) => {
|
|
82
|
+
this._removeAbortController(chatMessageId);
|
|
83
|
+
this.observability?.emit(
|
|
84
|
+
{
|
|
85
|
+
displayMessage: "Chat message response",
|
|
86
|
+
id: data.id,
|
|
87
|
+
payload: {},
|
|
88
|
+
timestamp: Date.now(),
|
|
89
|
+
type: "message:response"
|
|
90
|
+
},
|
|
91
|
+
this.ctx
|
|
92
|
+
);
|
|
93
|
+
},
|
|
94
|
+
abortSignal ? { abortSignal } : void 0
|
|
95
|
+
);
|
|
96
|
+
if (response) {
|
|
97
|
+
await this._reply(data.id, response);
|
|
98
|
+
} else {
|
|
99
|
+
console.warn(
|
|
100
|
+
`[AIChatAgent] onChatMessage returned no response for chatMessageId: ${chatMessageId}`
|
|
101
|
+
);
|
|
102
|
+
this._broadcastChatMessage(
|
|
103
|
+
{
|
|
104
|
+
body: "No response was generated by the agent.",
|
|
105
|
+
done: true,
|
|
106
|
+
id: data.id,
|
|
107
|
+
type: "cf_agent_use_chat_response" /* CF_AGENT_USE_CHAT_RESPONSE */
|
|
108
|
+
},
|
|
109
|
+
[connection.id]
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
if (data.type === "cf_agent_chat_clear" /* CF_AGENT_CHAT_CLEAR */) {
|
|
115
|
+
this._destroyAbortControllers();
|
|
116
|
+
this.sql`delete from cf_ai_chat_agent_messages`;
|
|
117
|
+
this.messages = [];
|
|
118
|
+
this._broadcastChatMessage(
|
|
119
|
+
{
|
|
120
|
+
type: "cf_agent_chat_clear" /* CF_AGENT_CHAT_CLEAR */
|
|
121
|
+
},
|
|
122
|
+
[connection.id]
|
|
123
|
+
);
|
|
124
|
+
} else if (data.type === "cf_agent_chat_messages" /* CF_AGENT_CHAT_MESSAGES */) {
|
|
125
|
+
const transformedMessages = autoTransformMessages(data.messages);
|
|
126
|
+
await this.persistMessages(transformedMessages, [connection.id]);
|
|
127
|
+
} else if (data.type === "cf_agent_chat_request_cancel" /* CF_AGENT_CHAT_REQUEST_CANCEL */) {
|
|
128
|
+
this._cancelChatRequest(data.id);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
async onRequest(request) {
|
|
133
|
+
return this._tryCatchChat(() => {
|
|
134
|
+
const url = new URL(request.url);
|
|
135
|
+
if (url.pathname.endsWith("/get-messages")) {
|
|
136
|
+
const messages = (this.sql`select * from cf_ai_chat_agent_messages` || []).map((row) => {
|
|
137
|
+
return JSON.parse(row.message);
|
|
138
|
+
});
|
|
139
|
+
return Response.json(messages);
|
|
140
|
+
}
|
|
141
|
+
return super.onRequest(request);
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
async _tryCatchChat(fn) {
|
|
145
|
+
try {
|
|
146
|
+
return await fn();
|
|
147
|
+
} catch (e) {
|
|
148
|
+
throw this.onError(e);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Handle incoming chat messages and generate a response
|
|
153
|
+
* @param onFinish Callback to be called when the response is finished
|
|
154
|
+
* @param options.signal A signal to pass to any child requests which can be used to cancel them
|
|
155
|
+
* @returns Response to send to the client or undefined
|
|
156
|
+
*/
|
|
157
|
+
async onChatMessage(onFinish, options) {
|
|
158
|
+
throw new Error(
|
|
159
|
+
"recieved a chat message, override onChatMessage and return a Response to send to the client"
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Save messages on the server side
|
|
164
|
+
* @param messages Chat messages to save
|
|
165
|
+
*/
|
|
166
|
+
async saveMessages(messages) {
|
|
167
|
+
await this.persistMessages(messages);
|
|
168
|
+
await this._tryCatchChat(async () => {
|
|
169
|
+
const response = await this.onChatMessage(() => {
|
|
170
|
+
});
|
|
171
|
+
if (response) this._reply(crypto.randomUUID(), response);
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
async persistMessages(messages, excludeBroadcastIds = []) {
|
|
175
|
+
this.sql`delete from cf_ai_chat_agent_messages`;
|
|
176
|
+
for (const message of messages) {
|
|
177
|
+
this.sql`insert into cf_ai_chat_agent_messages (id, message) values (${message.id},${JSON.stringify(message)})`;
|
|
178
|
+
}
|
|
179
|
+
this.messages = messages;
|
|
180
|
+
this._broadcastChatMessage(
|
|
181
|
+
{
|
|
182
|
+
messages,
|
|
183
|
+
type: "cf_agent_chat_messages" /* CF_AGENT_CHAT_MESSAGES */
|
|
184
|
+
},
|
|
185
|
+
excludeBroadcastIds
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
async _reply(id, response) {
|
|
189
|
+
return this._tryCatchChat(async () => {
|
|
190
|
+
if (!response.body) {
|
|
191
|
+
this._broadcastChatMessage({
|
|
192
|
+
body: "",
|
|
193
|
+
done: true,
|
|
194
|
+
id,
|
|
195
|
+
type: "cf_agent_use_chat_response" /* CF_AGENT_USE_CHAT_RESPONSE */
|
|
196
|
+
});
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const reader = response.body.getReader();
|
|
200
|
+
const message = {
|
|
201
|
+
id: `assistant_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`,
|
|
202
|
+
// default
|
|
203
|
+
role: "assistant",
|
|
204
|
+
parts: []
|
|
205
|
+
};
|
|
206
|
+
let activeTextParts = {};
|
|
207
|
+
let activeReasoningParts = {};
|
|
208
|
+
const partialToolCalls = {};
|
|
209
|
+
function updateDynamicToolPart(options) {
|
|
210
|
+
const part = message.parts.find(
|
|
211
|
+
(part2) => part2.type === "dynamic-tool" && part2.toolCallId === options.toolCallId
|
|
212
|
+
);
|
|
213
|
+
const anyOptions = options;
|
|
214
|
+
const anyPart = part;
|
|
215
|
+
if (part != null) {
|
|
216
|
+
part.state = options.state;
|
|
217
|
+
anyPart.toolName = options.toolName;
|
|
218
|
+
anyPart.input = anyOptions.input;
|
|
219
|
+
anyPart.output = anyOptions.output;
|
|
220
|
+
anyPart.errorText = anyOptions.errorText;
|
|
221
|
+
anyPart.rawInput = anyOptions.rawInput ?? anyPart.rawInput;
|
|
222
|
+
anyPart.preliminary = anyOptions.preliminary;
|
|
223
|
+
if (anyOptions.providerMetadata != null && part.state === "input-available") {
|
|
224
|
+
part.callProviderMetadata = anyOptions.providerMetadata;
|
|
225
|
+
}
|
|
226
|
+
} else {
|
|
227
|
+
message.parts.push({
|
|
228
|
+
type: "dynamic-tool",
|
|
229
|
+
toolName: options.toolName,
|
|
230
|
+
toolCallId: options.toolCallId,
|
|
231
|
+
state: options.state,
|
|
232
|
+
input: anyOptions.input,
|
|
233
|
+
output: anyOptions.output,
|
|
234
|
+
errorText: anyOptions.errorText,
|
|
235
|
+
preliminary: anyOptions.preliminary,
|
|
236
|
+
...anyOptions.providerMetadata != null ? { callProviderMetadata: anyOptions.providerMetadata } : {}
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function updateToolPart(options) {
|
|
241
|
+
const part = message.parts.find(
|
|
242
|
+
(part2) => isToolUIPart(part2) && part2.toolCallId === options.toolCallId
|
|
243
|
+
);
|
|
244
|
+
const anyOptions = options;
|
|
245
|
+
const anyPart = part;
|
|
246
|
+
if (part != null) {
|
|
247
|
+
part.state = options.state;
|
|
248
|
+
anyPart.input = anyOptions.input;
|
|
249
|
+
anyPart.output = anyOptions.output;
|
|
250
|
+
anyPart.errorText = anyOptions.errorText;
|
|
251
|
+
anyPart.rawInput = anyOptions.rawInput;
|
|
252
|
+
anyPart.preliminary = anyOptions.preliminary;
|
|
253
|
+
anyPart.providerExecuted = anyOptions.providerExecuted ?? part.providerExecuted;
|
|
254
|
+
if (anyOptions.providerMetadata != null && part.state === "input-available") {
|
|
255
|
+
part.callProviderMetadata = anyOptions.providerMetadata;
|
|
256
|
+
}
|
|
257
|
+
} else {
|
|
258
|
+
message.parts.push({
|
|
259
|
+
type: `tool-${options.toolName}`,
|
|
260
|
+
toolCallId: options.toolCallId,
|
|
261
|
+
state: options.state,
|
|
262
|
+
input: anyOptions.input,
|
|
263
|
+
output: anyOptions.output,
|
|
264
|
+
rawInput: anyOptions.rawInput,
|
|
265
|
+
errorText: anyOptions.errorText,
|
|
266
|
+
providerExecuted: anyOptions.providerExecuted,
|
|
267
|
+
preliminary: anyOptions.preliminary,
|
|
268
|
+
...anyOptions.providerMetadata != null ? { callProviderMetadata: anyOptions.providerMetadata } : {}
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
async function updateMessageMetadata(metadata) {
|
|
273
|
+
if (metadata != null) {
|
|
274
|
+
const mergedMetadata = message.metadata != null ? { ...message.metadata, ...metadata } : metadata;
|
|
275
|
+
message.metadata = mergedMetadata;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
try {
|
|
279
|
+
while (true) {
|
|
280
|
+
const { done, value } = await reader.read();
|
|
281
|
+
if (done) {
|
|
282
|
+
this._broadcastChatMessage({
|
|
283
|
+
body: "",
|
|
284
|
+
done: true,
|
|
285
|
+
id,
|
|
286
|
+
type: "cf_agent_use_chat_response" /* CF_AGENT_USE_CHAT_RESPONSE */
|
|
287
|
+
});
|
|
288
|
+
break;
|
|
289
|
+
}
|
|
290
|
+
const chunk = decoder.decode(value);
|
|
291
|
+
const contentType = response.headers.get("content-type") || "";
|
|
292
|
+
const isSSE = contentType.includes("text/event-stream");
|
|
293
|
+
if (isSSE) {
|
|
294
|
+
const lines = chunk.split("\n");
|
|
295
|
+
for (const line of lines) {
|
|
296
|
+
if (line.startsWith("data: ") && line !== "data: [DONE]") {
|
|
297
|
+
try {
|
|
298
|
+
const data = JSON.parse(line.slice(6));
|
|
299
|
+
switch (data.type) {
|
|
300
|
+
case "text-start": {
|
|
301
|
+
const textPart = {
|
|
302
|
+
type: "text",
|
|
303
|
+
text: "",
|
|
304
|
+
providerMetadata: data.providerMetadata,
|
|
305
|
+
state: "streaming"
|
|
306
|
+
};
|
|
307
|
+
activeTextParts[data.id] = textPart;
|
|
308
|
+
message.parts.push(textPart);
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
case "text-delta": {
|
|
312
|
+
const textPart = activeTextParts[data.id];
|
|
313
|
+
textPart.text += data.delta;
|
|
314
|
+
textPart.providerMetadata = data.providerMetadata ?? textPart.providerMetadata;
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
case "text-end": {
|
|
318
|
+
const textPart = activeTextParts[data.id];
|
|
319
|
+
textPart.state = "done";
|
|
320
|
+
textPart.providerMetadata = data.providerMetadata ?? textPart.providerMetadata;
|
|
321
|
+
delete activeTextParts[data.id];
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
case "reasoning-start": {
|
|
325
|
+
const reasoningPart = {
|
|
326
|
+
type: "reasoning",
|
|
327
|
+
text: "",
|
|
328
|
+
providerMetadata: data.providerMetadata,
|
|
329
|
+
state: "streaming"
|
|
330
|
+
};
|
|
331
|
+
activeReasoningParts[data.id] = reasoningPart;
|
|
332
|
+
message.parts.push(reasoningPart);
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
case "reasoning-delta": {
|
|
336
|
+
const reasoningPart = activeReasoningParts[data.id];
|
|
337
|
+
reasoningPart.text += data.delta;
|
|
338
|
+
reasoningPart.providerMetadata = data.providerMetadata ?? reasoningPart.providerMetadata;
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
case "reasoning-end": {
|
|
342
|
+
const reasoningPart = activeReasoningParts[data.id];
|
|
343
|
+
reasoningPart.providerMetadata = data.providerMetadata ?? reasoningPart.providerMetadata;
|
|
344
|
+
reasoningPart.state = "done";
|
|
345
|
+
delete activeReasoningParts[data.id];
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
case "file": {
|
|
349
|
+
message.parts.push({
|
|
350
|
+
type: "file",
|
|
351
|
+
mediaType: data.mediaType,
|
|
352
|
+
url: data.url
|
|
353
|
+
});
|
|
354
|
+
break;
|
|
355
|
+
}
|
|
356
|
+
case "source-url": {
|
|
357
|
+
message.parts.push({
|
|
358
|
+
type: "source-url",
|
|
359
|
+
sourceId: data.sourceId,
|
|
360
|
+
url: data.url,
|
|
361
|
+
title: data.title,
|
|
362
|
+
providerMetadata: data.providerMetadata
|
|
363
|
+
});
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
case "source-document": {
|
|
367
|
+
message.parts.push({
|
|
368
|
+
type: "source-document",
|
|
369
|
+
sourceId: data.sourceId,
|
|
370
|
+
mediaType: data.mediaType,
|
|
371
|
+
title: data.title,
|
|
372
|
+
filename: data.filename,
|
|
373
|
+
providerMetadata: data.providerMetadata
|
|
374
|
+
});
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
case "tool-input-start": {
|
|
378
|
+
const toolInvocations = message.parts.filter(isToolUIPart);
|
|
379
|
+
partialToolCalls[data.toolCallId] = {
|
|
380
|
+
text: "",
|
|
381
|
+
toolName: data.toolName,
|
|
382
|
+
index: toolInvocations.length,
|
|
383
|
+
dynamic: data.dynamic
|
|
384
|
+
};
|
|
385
|
+
if (data.dynamic) {
|
|
386
|
+
updateDynamicToolPart({
|
|
387
|
+
toolCallId: data.toolCallId,
|
|
388
|
+
toolName: data.toolName,
|
|
389
|
+
state: "input-streaming",
|
|
390
|
+
input: void 0
|
|
391
|
+
});
|
|
392
|
+
} else {
|
|
393
|
+
updateToolPart({
|
|
394
|
+
toolCallId: data.toolCallId,
|
|
395
|
+
toolName: data.toolName,
|
|
396
|
+
state: "input-streaming",
|
|
397
|
+
input: void 0
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
case "tool-input-delta": {
|
|
403
|
+
const partialToolCall = partialToolCalls[data.toolCallId];
|
|
404
|
+
partialToolCall.text += data.inputTextDelta;
|
|
405
|
+
const { value: partialArgs } = await parsePartialJson(
|
|
406
|
+
partialToolCall.text
|
|
407
|
+
);
|
|
408
|
+
if (partialToolCall.dynamic) {
|
|
409
|
+
updateDynamicToolPart({
|
|
410
|
+
toolCallId: data.toolCallId,
|
|
411
|
+
toolName: partialToolCall.toolName,
|
|
412
|
+
state: "input-streaming",
|
|
413
|
+
input: partialArgs
|
|
414
|
+
});
|
|
415
|
+
} else {
|
|
416
|
+
updateToolPart({
|
|
417
|
+
toolCallId: data.toolCallId,
|
|
418
|
+
toolName: partialToolCall.toolName,
|
|
419
|
+
state: "input-streaming",
|
|
420
|
+
input: partialArgs
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
break;
|
|
424
|
+
}
|
|
425
|
+
case "tool-input-available": {
|
|
426
|
+
if (data.dynamic) {
|
|
427
|
+
updateDynamicToolPart({
|
|
428
|
+
toolCallId: data.toolCallId,
|
|
429
|
+
toolName: data.toolName,
|
|
430
|
+
state: "input-available",
|
|
431
|
+
input: data.input,
|
|
432
|
+
providerMetadata: data.providerMetadata
|
|
433
|
+
});
|
|
434
|
+
} else {
|
|
435
|
+
updateToolPart({
|
|
436
|
+
toolCallId: data.toolCallId,
|
|
437
|
+
toolName: data.toolName,
|
|
438
|
+
state: "input-available",
|
|
439
|
+
input: data.input,
|
|
440
|
+
providerExecuted: data.providerExecuted,
|
|
441
|
+
providerMetadata: data.providerMetadata
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
break;
|
|
445
|
+
}
|
|
446
|
+
case "tool-input-error": {
|
|
447
|
+
if (data.dynamic) {
|
|
448
|
+
updateDynamicToolPart({
|
|
449
|
+
toolCallId: data.toolCallId,
|
|
450
|
+
toolName: data.toolName,
|
|
451
|
+
state: "output-error",
|
|
452
|
+
input: data.input,
|
|
453
|
+
errorText: data.errorText,
|
|
454
|
+
providerMetadata: data.providerMetadata
|
|
455
|
+
});
|
|
456
|
+
} else {
|
|
457
|
+
updateToolPart({
|
|
458
|
+
toolCallId: data.toolCallId,
|
|
459
|
+
toolName: data.toolName,
|
|
460
|
+
state: "output-error",
|
|
461
|
+
input: void 0,
|
|
462
|
+
rawInput: data.input,
|
|
463
|
+
errorText: data.errorText,
|
|
464
|
+
providerExecuted: data.providerExecuted,
|
|
465
|
+
providerMetadata: data.providerMetadata
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
break;
|
|
469
|
+
}
|
|
470
|
+
case "tool-output-available": {
|
|
471
|
+
if (data.dynamic) {
|
|
472
|
+
const toolInvocations = message.parts.filter(
|
|
473
|
+
(part) => part.type === "dynamic-tool"
|
|
474
|
+
);
|
|
475
|
+
const toolInvocation = toolInvocations.find(
|
|
476
|
+
(invocation) => invocation.toolCallId === data.toolCallId
|
|
477
|
+
);
|
|
478
|
+
if (!toolInvocation)
|
|
479
|
+
throw new Error("Tool invocation not found");
|
|
480
|
+
updateDynamicToolPart({
|
|
481
|
+
toolCallId: data.toolCallId,
|
|
482
|
+
toolName: toolInvocation.toolName,
|
|
483
|
+
state: "output-available",
|
|
484
|
+
input: toolInvocation.input,
|
|
485
|
+
output: data.output,
|
|
486
|
+
preliminary: data.preliminary
|
|
487
|
+
});
|
|
488
|
+
} else {
|
|
489
|
+
const toolInvocations = message.parts.filter(isToolUIPart);
|
|
490
|
+
const toolInvocation = toolInvocations.find(
|
|
491
|
+
(invocation) => invocation.toolCallId === data.toolCallId
|
|
492
|
+
);
|
|
493
|
+
if (!toolInvocation)
|
|
494
|
+
throw new Error("Tool invocation not found");
|
|
495
|
+
updateToolPart({
|
|
496
|
+
toolCallId: data.toolCallId,
|
|
497
|
+
toolName: getToolName(toolInvocation),
|
|
498
|
+
state: "output-available",
|
|
499
|
+
input: toolInvocation.input,
|
|
500
|
+
output: data.output,
|
|
501
|
+
providerExecuted: data.providerExecuted,
|
|
502
|
+
preliminary: data.preliminary
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
break;
|
|
506
|
+
}
|
|
507
|
+
case "tool-output-error": {
|
|
508
|
+
if (data.dynamic) {
|
|
509
|
+
const toolInvocations = message.parts.filter(
|
|
510
|
+
(part) => part.type === "dynamic-tool"
|
|
511
|
+
);
|
|
512
|
+
const toolInvocation = toolInvocations.find(
|
|
513
|
+
(invocation) => invocation.toolCallId === data.toolCallId
|
|
514
|
+
);
|
|
515
|
+
if (!toolInvocation)
|
|
516
|
+
throw new Error("Tool invocation not found");
|
|
517
|
+
updateDynamicToolPart({
|
|
518
|
+
toolCallId: data.toolCallId,
|
|
519
|
+
toolName: toolInvocation.toolName,
|
|
520
|
+
state: "output-error",
|
|
521
|
+
input: toolInvocation.input,
|
|
522
|
+
errorText: data.errorText
|
|
523
|
+
});
|
|
524
|
+
} else {
|
|
525
|
+
const toolInvocations = message.parts.filter(isToolUIPart);
|
|
526
|
+
const toolInvocation = toolInvocations.find(
|
|
527
|
+
(invocation) => invocation.toolCallId === data.toolCallId
|
|
528
|
+
);
|
|
529
|
+
if (!toolInvocation)
|
|
530
|
+
throw new Error("Tool invocation not found");
|
|
531
|
+
updateToolPart({
|
|
532
|
+
toolCallId: data.toolCallId,
|
|
533
|
+
toolName: getToolName(toolInvocation),
|
|
534
|
+
state: "output-error",
|
|
535
|
+
input: toolInvocation.input,
|
|
536
|
+
rawInput: "rawInput" in toolInvocation ? toolInvocation.rawInput : void 0,
|
|
537
|
+
errorText: data.errorText
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
break;
|
|
541
|
+
}
|
|
542
|
+
case "start-step": {
|
|
543
|
+
message.parts.push({ type: "step-start" });
|
|
544
|
+
break;
|
|
545
|
+
}
|
|
546
|
+
case "finish-step": {
|
|
547
|
+
activeTextParts = {};
|
|
548
|
+
activeReasoningParts = {};
|
|
549
|
+
break;
|
|
550
|
+
}
|
|
551
|
+
case "start": {
|
|
552
|
+
if (data.messageId != null) {
|
|
553
|
+
message.id = data.messageId;
|
|
554
|
+
}
|
|
555
|
+
await updateMessageMetadata(data.messageMetadata);
|
|
556
|
+
break;
|
|
557
|
+
}
|
|
558
|
+
case "finish": {
|
|
559
|
+
await updateMessageMetadata(data.messageMetadata);
|
|
560
|
+
break;
|
|
561
|
+
}
|
|
562
|
+
case "message-metadata": {
|
|
563
|
+
await updateMessageMetadata(data.messageMetadata);
|
|
564
|
+
break;
|
|
565
|
+
}
|
|
566
|
+
case "error": {
|
|
567
|
+
this._broadcastChatMessage({
|
|
568
|
+
error: true,
|
|
569
|
+
body: data.errorText ?? JSON.stringify(data),
|
|
570
|
+
done: false,
|
|
571
|
+
id,
|
|
572
|
+
type: "cf_agent_use_chat_response" /* CF_AGENT_USE_CHAT_RESPONSE */
|
|
573
|
+
});
|
|
574
|
+
break;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
this._broadcastChatMessage({
|
|
578
|
+
body: JSON.stringify(data),
|
|
579
|
+
done: false,
|
|
580
|
+
id,
|
|
581
|
+
type: "cf_agent_use_chat_response" /* CF_AGENT_USE_CHAT_RESPONSE */
|
|
582
|
+
});
|
|
583
|
+
} catch (_e) {
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
} else {
|
|
588
|
+
if (chunk.length > 0) {
|
|
589
|
+
message.parts.push({ type: "text", text: chunk });
|
|
590
|
+
this._broadcastChatMessage({
|
|
591
|
+
body: JSON.stringify({ type: "text-delta", delta: chunk }),
|
|
592
|
+
done: false,
|
|
593
|
+
id,
|
|
594
|
+
type: "cf_agent_use_chat_response" /* CF_AGENT_USE_CHAT_RESPONSE */
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
} finally {
|
|
600
|
+
reader.releaseLock();
|
|
601
|
+
}
|
|
602
|
+
if (message.parts.length > 0) {
|
|
603
|
+
await this.persistMessages([...this.messages, message]);
|
|
604
|
+
}
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* For the given message id, look up its associated AbortController
|
|
609
|
+
* If the AbortController does not exist, create and store one in memory
|
|
610
|
+
*
|
|
611
|
+
* returns the AbortSignal associated with the AbortController
|
|
612
|
+
*/
|
|
613
|
+
_getAbortSignal(id) {
|
|
614
|
+
if (typeof id !== "string") {
|
|
615
|
+
return void 0;
|
|
616
|
+
}
|
|
617
|
+
if (!this._chatMessageAbortControllers.has(id)) {
|
|
618
|
+
this._chatMessageAbortControllers.set(id, new AbortController());
|
|
619
|
+
}
|
|
620
|
+
return this._chatMessageAbortControllers.get(id)?.signal;
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* Remove an abort controller from the cache of pending message responses
|
|
624
|
+
*/
|
|
625
|
+
_removeAbortController(id) {
|
|
626
|
+
this._chatMessageAbortControllers.delete(id);
|
|
627
|
+
}
|
|
628
|
+
/**
|
|
629
|
+
* Propagate an abort signal for any requests associated with the given message id
|
|
630
|
+
*/
|
|
631
|
+
_cancelChatRequest(id) {
|
|
632
|
+
if (this._chatMessageAbortControllers.has(id)) {
|
|
633
|
+
const abortController = this._chatMessageAbortControllers.get(id);
|
|
634
|
+
abortController?.abort();
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* Abort all pending requests and clear the cache of AbortControllers
|
|
639
|
+
*/
|
|
640
|
+
_destroyAbortControllers() {
|
|
641
|
+
for (const controller of this._chatMessageAbortControllers.values()) {
|
|
642
|
+
controller?.abort();
|
|
643
|
+
}
|
|
644
|
+
this._chatMessageAbortControllers.clear();
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* When the DO is destroyed, cancel all pending requests
|
|
648
|
+
*/
|
|
649
|
+
async destroy() {
|
|
650
|
+
this._destroyAbortControllers();
|
|
651
|
+
await super.destroy();
|
|
652
|
+
}
|
|
653
|
+
};
|
|
654
|
+
export {
|
|
655
|
+
AIChatAgent
|
|
656
|
+
};
|
|
657
|
+
//# sourceMappingURL=ai-chat-agent.js.map
|