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