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