agents 0.0.0-7856b4d → 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.
Files changed (74) hide show
  1. package/README.md +255 -27
  2. package/dist/_esm-LV5FJ3HK.js +3922 -0
  3. package/dist/_esm-LV5FJ3HK.js.map +1 -0
  4. package/dist/ai-chat-agent.d.ts +14 -8
  5. package/dist/ai-chat-agent.js +494 -69
  6. package/dist/ai-chat-agent.js.map +1 -1
  7. package/dist/ai-chat-v5-migration.d.ts +152 -0
  8. package/dist/ai-chat-v5-migration.js +20 -0
  9. package/dist/ai-chat-v5-migration.js.map +1 -0
  10. package/dist/ai-react.d.ts +70 -72
  11. package/dist/ai-react.js +255 -108
  12. package/dist/ai-react.js.map +1 -1
  13. package/dist/ai-types.d.ts +37 -19
  14. package/dist/ai-types.js +7 -0
  15. package/dist/ccip-CMBYN64O.js +15 -0
  16. package/dist/ccip-CMBYN64O.js.map +1 -0
  17. package/dist/chunk-5Y6BEZDY.js +276 -0
  18. package/dist/chunk-5Y6BEZDY.js.map +1 -0
  19. package/dist/chunk-BER7KXUJ.js +18 -0
  20. package/dist/chunk-BER7KXUJ.js.map +1 -0
  21. package/dist/{chunk-BZXOAZUX.js → chunk-C2OEBJZ2.js} +17 -10
  22. package/dist/chunk-C2OEBJZ2.js.map +1 -0
  23. package/dist/chunk-JJBFIGUC.js +5202 -0
  24. package/dist/chunk-JJBFIGUC.js.map +1 -0
  25. package/dist/chunk-PR4QN5HX.js +43 -0
  26. package/dist/chunk-PR4QN5HX.js.map +1 -0
  27. package/dist/{chunk-VCSB47AK.js → chunk-QEVM4BVL.js} +10 -10
  28. package/dist/chunk-QEVM4BVL.js.map +1 -0
  29. package/dist/chunk-TYAY6AU6.js +159 -0
  30. package/dist/chunk-TYAY6AU6.js.map +1 -0
  31. package/dist/chunk-UJVEAURM.js +150 -0
  32. package/dist/chunk-UJVEAURM.js.map +1 -0
  33. package/dist/chunk-XGMKNUJA.js +1343 -0
  34. package/dist/chunk-XGMKNUJA.js.map +1 -0
  35. package/dist/chunk-ZMMHNOMZ.js +942 -0
  36. package/dist/chunk-ZMMHNOMZ.js.map +1 -0
  37. package/dist/client-DVoPb3-C.d.ts +5120 -0
  38. package/dist/client.d.ts +2 -2
  39. package/dist/client.js +3 -1
  40. package/dist/codemode/ai.d.ts +25 -0
  41. package/dist/codemode/ai.js +5200 -0
  42. package/dist/codemode/ai.js.map +1 -0
  43. package/dist/index.d.ts +198 -52
  44. package/dist/index.js +14 -4
  45. package/dist/mcp/client.d.ts +10 -781
  46. package/dist/mcp/client.js +2 -1
  47. package/dist/mcp/do-oauth-client-provider.d.ts +1 -0
  48. package/dist/mcp/do-oauth-client-provider.js +2 -1
  49. package/dist/mcp/index.d.ts +59 -64
  50. package/dist/mcp/index.js +956 -639
  51. package/dist/mcp/index.js.map +1 -1
  52. package/dist/mcp/x402.d.ts +39 -0
  53. package/dist/mcp/x402.js +3195 -0
  54. package/dist/mcp/x402.js.map +1 -0
  55. package/dist/mcp-BH1fJeiU.d.ts +58 -0
  56. package/dist/observability/index.d.ts +34 -0
  57. package/dist/observability/index.js +12 -0
  58. package/dist/observability/index.js.map +1 -0
  59. package/dist/react.d.ts +21 -13
  60. package/dist/react.js +112 -12
  61. package/dist/react.js.map +1 -1
  62. package/dist/schedule.d.ts +81 -7
  63. package/dist/schedule.js +21 -6
  64. package/dist/schedule.js.map +1 -1
  65. package/dist/secp256k1-M22GZP2U.js +2193 -0
  66. package/dist/secp256k1-M22GZP2U.js.map +1 -0
  67. package/package.json +103 -70
  68. package/src/index.ts +993 -217
  69. package/dist/chunk-6RPGDIE2.js +0 -786
  70. package/dist/chunk-6RPGDIE2.js.map +0 -1
  71. package/dist/chunk-BZXOAZUX.js.map +0 -1
  72. package/dist/chunk-OYJXQRRH.js +0 -465
  73. package/dist/chunk-OYJXQRRH.js.map +0 -1
  74. package/dist/chunk-VCSB47AK.js.map +0 -1
@@ -1,12 +1,21 @@
1
+ import {
2
+ autoTransformMessages
3
+ } from "./chunk-UJVEAURM.js";
1
4
  import {
2
5
  Agent
3
- } from "./chunk-6RPGDIE2.js";
4
- import "./chunk-OYJXQRRH.js";
5
- import "./chunk-BZXOAZUX.js";
6
- import "./chunk-VCSB47AK.js";
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 { appendResponseMessages } from "ai";
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,59 +25,80 @@ var AIChatAgent = class extends Agent {
16
25
  message text not null,
17
26
  created_at datetime default current_timestamp
18
27
  )`;
19
- this.messages = (this.sql`select * from cf_ai_chat_agent_messages` || []).map((row) => {
20
- return JSON.parse(row.message);
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;
30
49
  try {
31
50
  data = JSON.parse(message);
32
- } catch (error) {
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
- method,
38
- keepalive,
39
- headers,
40
- body,
56
+ // method,
57
+ // keepalive,
58
+ // headers,
59
+ body
41
60
  // we're reading this
42
- redirect,
43
- integrity,
44
- credentials,
45
- mode,
46
- referrer,
47
- referrerPolicy,
48
- window
61
+ //
62
+ // // these might not exist?
49
63
  // dispatcher,
50
64
  // duplex
51
65
  } = data.init;
52
66
  const { messages } = JSON.parse(body);
67
+ const transformedMessages = autoTransformMessages(messages);
53
68
  this._broadcastChatMessage(
54
69
  {
55
- type: "cf_agent_chat_messages",
56
- messages
70
+ messages: transformedMessages,
71
+ type: "cf_agent_chat_messages" /* CF_AGENT_CHAT_MESSAGES */
57
72
  },
58
73
  [connection.id]
59
74
  );
60
- await this.persistMessages(messages, [connection.id]);
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
+ );
61
86
  const chatMessageId = data.id;
62
87
  const abortSignal = this._getAbortSignal(chatMessageId);
63
88
  return this._tryCatchChat(async () => {
64
89
  const response = await this.onChatMessage(
65
- async ({ response: response2 }) => {
66
- const finalMessages = appendResponseMessages({
67
- messages,
68
- responseMessages: response2.messages
69
- });
70
- await this.persistMessages(finalMessages, [connection.id]);
90
+ async (_finishResult) => {
71
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
+ );
72
102
  },
73
103
  abortSignal ? { abortSignal } : void 0
74
104
  );
@@ -80,29 +110,30 @@ var AIChatAgent = class extends Agent {
80
110
  );
81
111
  this._broadcastChatMessage(
82
112
  {
83
- id: data.id,
84
- type: "cf_agent_use_chat_response",
85
113
  body: "No response was generated by the agent.",
86
- done: true
114
+ done: true,
115
+ id: data.id,
116
+ type: "cf_agent_use_chat_response" /* CF_AGENT_USE_CHAT_RESPONSE */
87
117
  },
88
118
  [connection.id]
89
119
  );
90
120
  }
91
121
  });
92
122
  }
93
- if (data.type === "cf_agent_chat_clear") {
123
+ if (data.type === "cf_agent_chat_clear" /* CF_AGENT_CHAT_CLEAR */) {
94
124
  this._destroyAbortControllers();
95
125
  this.sql`delete from cf_ai_chat_agent_messages`;
96
126
  this.messages = [];
97
127
  this._broadcastChatMessage(
98
128
  {
99
- type: "cf_agent_chat_clear"
129
+ type: "cf_agent_chat_clear" /* CF_AGENT_CHAT_CLEAR */
100
130
  },
101
131
  [connection.id]
102
132
  );
103
- } else if (data.type === "cf_agent_chat_messages") {
104
- await this.persistMessages(data.messages, [connection.id]);
105
- } else if (data.type === "cf_agent_chat_request_cancel") {
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 */) {
106
137
  this._cancelChatRequest(data.id);
107
138
  }
108
139
  }
@@ -111,9 +142,7 @@ var AIChatAgent = class extends Agent {
111
142
  return this._tryCatchChat(() => {
112
143
  const url = new URL(request.url);
113
144
  if (url.pathname.endsWith("/get-messages")) {
114
- const messages = (this.sql`select * from cf_ai_chat_agent_messages` || []).map((row) => {
115
- return JSON.parse(row.message);
116
- });
145
+ const messages = this._loadMessagesFromDb();
117
146
  return Response.json(messages);
118
147
  }
119
148
  return super.onRequest(request);
@@ -138,56 +167,452 @@ var AIChatAgent = class extends Agent {
138
167
  );
139
168
  }
140
169
  /**
141
- * Save messages on the server side and trigger AI response
170
+ * Save messages on the server side
142
171
  * @param messages Chat messages to save
143
172
  */
144
173
  async saveMessages(messages) {
145
174
  await this.persistMessages(messages);
146
- const response = await this.onChatMessage(async ({ response: response2 }) => {
147
- const finalMessages = appendResponseMessages({
148
- messages,
149
- responseMessages: response2.messages
175
+ await this._tryCatchChat(async () => {
176
+ const response = await this.onChatMessage(() => {
150
177
  });
151
- await this.persistMessages(finalMessages, []);
178
+ if (response) this._reply(crypto.randomUUID(), response);
152
179
  });
153
- if (response) {
154
- for await (const chunk of response.body) {
155
- decoder.decode(chunk);
156
- }
157
- response.body?.cancel();
158
- }
159
180
  }
160
181
  async persistMessages(messages, excludeBroadcastIds = []) {
161
- this.sql`delete from cf_ai_chat_agent_messages`;
162
182
  for (const message of messages) {
163
- this.sql`insert into cf_ai_chat_agent_messages (id, message) values (${message.id},${JSON.stringify(message)})`;
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
+ `;
164
188
  }
165
- this.messages = messages;
189
+ const persisted = this._loadMessagesFromDb();
190
+ this.messages = autoTransformMessages(persisted);
166
191
  this._broadcastChatMessage(
167
192
  {
168
- type: "cf_agent_chat_messages",
169
- messages
193
+ messages,
194
+ type: "cf_agent_chat_messages" /* CF_AGENT_CHAT_MESSAGES */
170
195
  },
171
196
  excludeBroadcastIds
172
197
  );
173
198
  }
174
199
  async _reply(id, response) {
175
200
  return this._tryCatchChat(async () => {
176
- for await (const chunk of response.body) {
177
- const body = decoder.decode(chunk);
201
+ if (!response.body) {
178
202
  this._broadcastChatMessage({
203
+ body: "",
204
+ done: true,
179
205
  id,
180
- type: "cf_agent_use_chat_response",
181
- body,
182
- done: false
206
+ type: "cf_agent_use_chat_response" /* CF_AGENT_USE_CHAT_RESPONSE */
183
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]);
184
615
  }
185
- this._broadcastChatMessage({
186
- id,
187
- type: "cf_agent_use_chat_response",
188
- body: "",
189
- done: true
190
- });
191
616
  });
192
617
  }
193
618
  /**