agents 0.0.0-77368ff → 0.0.0-789141e
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 +130 -7
- package/dist/_esm-LV5FJ3HK.js +3922 -0
- package/dist/_esm-LV5FJ3HK.js.map +1 -0
- package/dist/ai-chat-agent.d.ts +12 -8
- package/dist/ai-chat-agent.js +466 -66
- 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-chat-v5-migration.js.map +1 -0
- package/dist/ai-react.d.ts +67 -70
- package/dist/ai-react.js +246 -99
- 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-PVQZBKN7.js → chunk-C2OEBJZ2.js} +14 -7
- 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-KUH345EY.js → chunk-QEVM4BVL.js} +5 -5
- 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-Z2OUUKK4.js → chunk-XGMKNUJA.js} +188 -115
- 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-DVoPb3-C.d.ts +5120 -0
- package/dist/client.js +3 -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 +548 -31
- package/dist/index.js +8 -4
- package/dist/mcp/client.d.ts +10 -1052
- package/dist/mcp/client.js +2 -1
- package/dist/mcp/do-oauth-client-provider.d.ts +1 -0
- package/dist/mcp/do-oauth-client-provider.js +2 -1
- package/dist/mcp/index.d.ts +59 -87
- package/dist/mcp/index.js +948 -637
- 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 -13
- package/dist/observability/index.js +6 -4
- package/dist/react.d.ts +14 -7
- package/dist/react.js +107 -7
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +83 -9
- package/dist/schedule.js +17 -2
- package/dist/schedule.js.map +1 -1
- package/dist/secp256k1-M22GZP2U.js +2193 -0
- package/dist/secp256k1-M22GZP2U.js.map +1 -0
- package/package.json +38 -10
- package/src/index.ts +273 -136
- package/dist/chunk-KUH345EY.js.map +0 -1
- package/dist/chunk-PVQZBKN7.js.map +0 -1
- package/dist/chunk-UNG3FXYX.js +0 -525
- package/dist/chunk-UNG3FXYX.js.map +0 -1
- package/dist/chunk-Z2OUUKK4.js.map +0 -1
- package/dist/index-BIJvkfYt.d.ts +0 -614
package/dist/ai-chat-agent.js
CHANGED
|
@@ -1,12 +1,21 @@
|
|
|
1
|
+
import {
|
|
2
|
+
autoTransformMessages
|
|
3
|
+
} from "./chunk-UJVEAURM.js";
|
|
1
4
|
import {
|
|
2
5
|
Agent
|
|
3
|
-
} from "./chunk-
|
|
4
|
-
import "./chunk-
|
|
5
|
-
import "./chunk-
|
|
6
|
-
import "./chunk-
|
|
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";
|
|
7
12
|
|
|
8
13
|
// src/ai-chat-agent.ts
|
|
9
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
getToolName,
|
|
16
|
+
isToolUIPart,
|
|
17
|
+
parsePartialJson
|
|
18
|
+
} from "ai";
|
|
10
19
|
var decoder = new TextDecoder();
|
|
11
20
|
var AIChatAgent = class extends Agent {
|
|
12
21
|
constructor(ctx, env) {
|
|
@@ -16,14 +25,24 @@ var AIChatAgent = class extends Agent {
|
|
|
16
25
|
message text not null,
|
|
17
26
|
created_at datetime default current_timestamp
|
|
18
27
|
)`;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
});
|
|
28
|
+
const rawMessages = this._loadMessagesFromDb();
|
|
29
|
+
this.messages = autoTransformMessages(rawMessages);
|
|
22
30
|
this._chatMessageAbortControllers = /* @__PURE__ */ new Map();
|
|
23
31
|
}
|
|
24
32
|
_broadcastChatMessage(message, exclude) {
|
|
25
33
|
this.broadcast(JSON.stringify(message), exclude);
|
|
26
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);
|
|
45
|
+
}
|
|
27
46
|
async onMessage(connection, message) {
|
|
28
47
|
if (typeof message === "string") {
|
|
29
48
|
let data;
|
|
@@ -32,7 +51,7 @@ var AIChatAgent = class extends Agent {
|
|
|
32
51
|
} catch (_error) {
|
|
33
52
|
return;
|
|
34
53
|
}
|
|
35
|
-
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") {
|
|
36
55
|
const {
|
|
37
56
|
// method,
|
|
38
57
|
// keepalive,
|
|
@@ -45,22 +64,20 @@ var AIChatAgent = class extends Agent {
|
|
|
45
64
|
// duplex
|
|
46
65
|
} = data.init;
|
|
47
66
|
const { messages } = JSON.parse(body);
|
|
67
|
+
const transformedMessages = autoTransformMessages(messages);
|
|
48
68
|
this._broadcastChatMessage(
|
|
49
69
|
{
|
|
50
|
-
messages,
|
|
51
|
-
type: "cf_agent_chat_messages"
|
|
70
|
+
messages: transformedMessages,
|
|
71
|
+
type: "cf_agent_chat_messages" /* CF_AGENT_CHAT_MESSAGES */
|
|
52
72
|
},
|
|
53
73
|
[connection.id]
|
|
54
74
|
);
|
|
55
|
-
|
|
56
|
-
await this.persistMessages(messages, [connection.id]);
|
|
75
|
+
await this.persistMessages(transformedMessages, [connection.id]);
|
|
57
76
|
this.observability?.emit(
|
|
58
77
|
{
|
|
59
78
|
displayMessage: "Chat message request",
|
|
60
79
|
id: data.id,
|
|
61
|
-
payload: {
|
|
62
|
-
message: incomingMessages
|
|
63
|
-
},
|
|
80
|
+
payload: {},
|
|
64
81
|
timestamp: Date.now(),
|
|
65
82
|
type: "message:request"
|
|
66
83
|
},
|
|
@@ -70,21 +87,13 @@ var AIChatAgent = class extends Agent {
|
|
|
70
87
|
const abortSignal = this._getAbortSignal(chatMessageId);
|
|
71
88
|
return this._tryCatchChat(async () => {
|
|
72
89
|
const response = await this.onChatMessage(
|
|
73
|
-
async (
|
|
74
|
-
const finalMessages = appendResponseMessages({
|
|
75
|
-
messages,
|
|
76
|
-
responseMessages: response2.messages
|
|
77
|
-
});
|
|
78
|
-
const outgoingMessages = this._messagesNotAlreadyInAgent(finalMessages);
|
|
79
|
-
await this.persistMessages(finalMessages, [connection.id]);
|
|
90
|
+
async (_finishResult) => {
|
|
80
91
|
this._removeAbortController(chatMessageId);
|
|
81
92
|
this.observability?.emit(
|
|
82
93
|
{
|
|
83
94
|
displayMessage: "Chat message response",
|
|
84
95
|
id: data.id,
|
|
85
|
-
payload: {
|
|
86
|
-
message: outgoingMessages
|
|
87
|
-
},
|
|
96
|
+
payload: {},
|
|
88
97
|
timestamp: Date.now(),
|
|
89
98
|
type: "message:response"
|
|
90
99
|
},
|
|
@@ -104,26 +113,27 @@ var AIChatAgent = class extends Agent {
|
|
|
104
113
|
body: "No response was generated by the agent.",
|
|
105
114
|
done: true,
|
|
106
115
|
id: data.id,
|
|
107
|
-
type: "cf_agent_use_chat_response"
|
|
116
|
+
type: "cf_agent_use_chat_response" /* CF_AGENT_USE_CHAT_RESPONSE */
|
|
108
117
|
},
|
|
109
118
|
[connection.id]
|
|
110
119
|
);
|
|
111
120
|
}
|
|
112
121
|
});
|
|
113
122
|
}
|
|
114
|
-
if (data.type === "cf_agent_chat_clear") {
|
|
123
|
+
if (data.type === "cf_agent_chat_clear" /* CF_AGENT_CHAT_CLEAR */) {
|
|
115
124
|
this._destroyAbortControllers();
|
|
116
125
|
this.sql`delete from cf_ai_chat_agent_messages`;
|
|
117
126
|
this.messages = [];
|
|
118
127
|
this._broadcastChatMessage(
|
|
119
128
|
{
|
|
120
|
-
type: "cf_agent_chat_clear"
|
|
129
|
+
type: "cf_agent_chat_clear" /* CF_AGENT_CHAT_CLEAR */
|
|
121
130
|
},
|
|
122
131
|
[connection.id]
|
|
123
132
|
);
|
|
124
|
-
} else if (data.type === "cf_agent_chat_messages") {
|
|
125
|
-
|
|
126
|
-
|
|
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 */) {
|
|
127
137
|
this._cancelChatRequest(data.id);
|
|
128
138
|
}
|
|
129
139
|
}
|
|
@@ -132,9 +142,7 @@ var AIChatAgent = class extends Agent {
|
|
|
132
142
|
return this._tryCatchChat(() => {
|
|
133
143
|
const url = new URL(request.url);
|
|
134
144
|
if (url.pathname.endsWith("/get-messages")) {
|
|
135
|
-
const messages =
|
|
136
|
-
return JSON.parse(row.message);
|
|
137
|
-
});
|
|
145
|
+
const messages = this._loadMessagesFromDb();
|
|
138
146
|
return Response.json(messages);
|
|
139
147
|
}
|
|
140
148
|
return super.onRequest(request);
|
|
@@ -159,60 +167,452 @@ var AIChatAgent = class extends Agent {
|
|
|
159
167
|
);
|
|
160
168
|
}
|
|
161
169
|
/**
|
|
162
|
-
* Save messages on the server side
|
|
170
|
+
* Save messages on the server side
|
|
163
171
|
* @param messages Chat messages to save
|
|
164
172
|
*/
|
|
165
173
|
async saveMessages(messages) {
|
|
166
174
|
await this.persistMessages(messages);
|
|
167
|
-
|
|
168
|
-
const
|
|
169
|
-
messages,
|
|
170
|
-
responseMessages: response2.messages
|
|
175
|
+
await this._tryCatchChat(async () => {
|
|
176
|
+
const response = await this.onChatMessage(() => {
|
|
171
177
|
});
|
|
172
|
-
|
|
178
|
+
if (response) this._reply(crypto.randomUUID(), response);
|
|
173
179
|
});
|
|
174
|
-
if (response) {
|
|
175
|
-
for await (const chunk of response.body) {
|
|
176
|
-
decoder.decode(chunk);
|
|
177
|
-
}
|
|
178
|
-
response.body?.cancel();
|
|
179
|
-
}
|
|
180
180
|
}
|
|
181
181
|
async persistMessages(messages, excludeBroadcastIds = []) {
|
|
182
|
-
this.sql`delete from cf_ai_chat_agent_messages`;
|
|
183
182
|
for (const message of messages) {
|
|
184
|
-
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
|
+
`;
|
|
185
188
|
}
|
|
186
|
-
|
|
189
|
+
const persisted = this._loadMessagesFromDb();
|
|
190
|
+
this.messages = autoTransformMessages(persisted);
|
|
187
191
|
this._broadcastChatMessage(
|
|
188
192
|
{
|
|
189
193
|
messages,
|
|
190
|
-
type: "cf_agent_chat_messages"
|
|
194
|
+
type: "cf_agent_chat_messages" /* CF_AGENT_CHAT_MESSAGES */
|
|
191
195
|
},
|
|
192
196
|
excludeBroadcastIds
|
|
193
197
|
);
|
|
194
198
|
}
|
|
195
|
-
_messagesNotAlreadyInAgent(messages) {
|
|
196
|
-
const existingIds = new Set(this.messages.map((message) => message.id));
|
|
197
|
-
return messages.filter((message) => !existingIds.has(message.id));
|
|
198
|
-
}
|
|
199
199
|
async _reply(id, response) {
|
|
200
200
|
return this._tryCatchChat(async () => {
|
|
201
|
-
|
|
202
|
-
const body = decoder.decode(chunk);
|
|
201
|
+
if (!response.body) {
|
|
203
202
|
this._broadcastChatMessage({
|
|
204
|
-
body,
|
|
205
|
-
done:
|
|
203
|
+
body: "",
|
|
204
|
+
done: true,
|
|
206
205
|
id,
|
|
207
|
-
type: "cf_agent_use_chat_response"
|
|
206
|
+
type: "cf_agent_use_chat_response" /* CF_AGENT_USE_CHAT_RESPONSE */
|
|
208
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]);
|
|
209
615
|
}
|
|
210
|
-
this._broadcastChatMessage({
|
|
211
|
-
body: "",
|
|
212
|
-
done: true,
|
|
213
|
-
id,
|
|
214
|
-
type: "cf_agent_use_chat_response"
|
|
215
|
-
});
|
|
216
616
|
});
|
|
217
617
|
}
|
|
218
618
|
/**
|