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