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