agents 0.0.0-b4d83b9 → 0.0.0-b507395

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 (104) hide show
  1. package/README.md +5 -3
  2. package/dist/ai-chat-agent.d.ts +100 -20
  3. package/dist/ai-chat-agent.js +812 -644
  4. package/dist/ai-chat-agent.js.map +1 -1
  5. package/dist/ai-chat-v5-migration-DguhuLKF.js +155 -0
  6. package/dist/ai-chat-v5-migration-DguhuLKF.js.map +1 -0
  7. package/dist/ai-chat-v5-migration.d.ts +7 -4
  8. package/dist/ai-chat-v5-migration.js +3 -20
  9. package/dist/ai-react.d.ts +20 -24
  10. package/dist/ai-react.js +408 -349
  11. package/dist/ai-react.js.map +1 -1
  12. package/dist/ai-types-CwgHzwUb.js +24 -0
  13. package/dist/ai-types-CwgHzwUb.js.map +1 -0
  14. package/dist/ai-types-D_hTbf25.d.ts +103 -0
  15. package/dist/ai-types.d.ts +6 -92
  16. package/dist/ai-types.js +3 -8
  17. package/dist/cli/index.d.ts +1 -0
  18. package/dist/cli/index.js +28 -0
  19. package/dist/cli/index.js.map +1 -0
  20. package/dist/client-CcyhkGfN.js +117 -0
  21. package/dist/client-CcyhkGfN.js.map +1 -0
  22. package/dist/client-ClORm6f0.d.ts +104 -0
  23. package/dist/client-DfIOsabL.d.ts +834 -0
  24. package/dist/client-QZa2Rq0l.js +1105 -0
  25. package/dist/client-QZa2Rq0l.js.map +1 -0
  26. package/dist/client.d.ts +11 -92
  27. package/dist/client.js +4 -13
  28. package/dist/codemode/ai.d.ts +19 -17
  29. package/dist/codemode/ai.js +91 -5139
  30. package/dist/codemode/ai.js.map +1 -1
  31. package/dist/context-BkKbAa1R.js +8 -0
  32. package/dist/context-BkKbAa1R.js.map +1 -0
  33. package/dist/context-_sPQqJWv.d.ts +24 -0
  34. package/dist/context.d.ts +6 -0
  35. package/dist/context.js +3 -0
  36. package/dist/do-oauth-client-provider-B-ryFIPr.d.ts +70 -0
  37. package/dist/do-oauth-client-provider-B1fVIshX.js +155 -0
  38. package/dist/do-oauth-client-provider-B1fVIshX.js.map +1 -0
  39. package/dist/index-CyDpAVHZ.d.ts +58 -0
  40. package/dist/index-DPJ32qQn.d.ts +577 -0
  41. package/dist/index.d.ts +55 -542
  42. package/dist/index.js +8 -32
  43. package/dist/mcp/client.d.ts +4 -12
  44. package/dist/mcp/client.js +4 -10
  45. package/dist/mcp/do-oauth-client-provider.d.ts +2 -42
  46. package/dist/mcp/do-oauth-client-provider.js +3 -8
  47. package/dist/mcp/index.d.ts +191 -76
  48. package/dist/mcp/index.js +1429 -1080
  49. package/dist/mcp/index.js.map +1 -1
  50. package/dist/mcp/x402.d.ts +22 -27
  51. package/dist/mcp/x402.js +190 -3187
  52. package/dist/mcp/x402.js.map +1 -1
  53. package/dist/{mcp-BH1fJeiU.d.ts → mcp-CzbSsLfc.d.ts} +6 -3
  54. package/dist/observability/index.d.ts +3 -34
  55. package/dist/observability/index.js +8 -12
  56. package/dist/react-MaOSl7Pr.d.ts +113 -0
  57. package/dist/react.d.ts +11 -127
  58. package/dist/react.js +173 -200
  59. package/dist/react.js.map +1 -1
  60. package/dist/schedule.d.ts +13 -10
  61. package/dist/schedule.js +43 -33
  62. package/dist/schedule.js.map +1 -1
  63. package/dist/serializable-C4GLimgv.d.ts +39 -0
  64. package/dist/serializable.d.ts +7 -32
  65. package/dist/serializable.js +1 -1
  66. package/dist/src-BmbDclOA.js +1181 -0
  67. package/dist/src-BmbDclOA.js.map +1 -0
  68. package/package.json +56 -30
  69. package/dist/_esm-LV5FJ3HK.js +0 -3922
  70. package/dist/_esm-LV5FJ3HK.js.map +0 -1
  71. package/dist/ai-chat-v5-migration.js.map +0 -1
  72. package/dist/ai-types.js.map +0 -1
  73. package/dist/ccip-CMBYN64O.js +0 -15
  74. package/dist/ccip-CMBYN64O.js.map +0 -1
  75. package/dist/chunk-5Y6BEZDY.js +0 -276
  76. package/dist/chunk-5Y6BEZDY.js.map +0 -1
  77. package/dist/chunk-BER7KXUJ.js +0 -18
  78. package/dist/chunk-BER7KXUJ.js.map +0 -1
  79. package/dist/chunk-C2OEBJZ2.js +0 -113
  80. package/dist/chunk-C2OEBJZ2.js.map +0 -1
  81. package/dist/chunk-JJBFIGUC.js +0 -5202
  82. package/dist/chunk-JJBFIGUC.js.map +0 -1
  83. package/dist/chunk-PR4QN5HX.js +0 -43
  84. package/dist/chunk-PR4QN5HX.js.map +0 -1
  85. package/dist/chunk-QEVM4BVL.js +0 -116
  86. package/dist/chunk-QEVM4BVL.js.map +0 -1
  87. package/dist/chunk-TYAY6AU6.js +0 -159
  88. package/dist/chunk-TYAY6AU6.js.map +0 -1
  89. package/dist/chunk-UJVEAURM.js +0 -150
  90. package/dist/chunk-UJVEAURM.js.map +0 -1
  91. package/dist/chunk-XGMKNUJA.js +0 -1343
  92. package/dist/chunk-XGMKNUJA.js.map +0 -1
  93. package/dist/chunk-ZMMHNOMZ.js +0 -942
  94. package/dist/chunk-ZMMHNOMZ.js.map +0 -1
  95. package/dist/client-DVoPb3-C.d.ts +0 -5120
  96. package/dist/client.js.map +0 -1
  97. package/dist/index.js.map +0 -1
  98. package/dist/mcp/client.js.map +0 -1
  99. package/dist/mcp/do-oauth-client-provider.js.map +0 -1
  100. package/dist/observability/index.js.map +0 -1
  101. package/dist/secp256k1-M22GZP2U.js +0 -2193
  102. package/dist/secp256k1-M22GZP2U.js.map +0 -1
  103. package/dist/serializable.js.map +0 -1
  104. package/src/index.ts +0 -2031
@@ -1,652 +1,820 @@
1
- import {
2
- autoTransformMessages
3
- } from "./chunk-UJVEAURM.js";
4
- import {
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";
1
+ import { t as agentContext } from "./context-BkKbAa1R.js";
2
+ import { t as MessageType } from "./ai-types-CwgHzwUb.js";
3
+ import "./client-CcyhkGfN.js";
4
+ import "./client-QZa2Rq0l.js";
5
+ import "./do-oauth-client-provider-B1fVIshX.js";
6
+ import { t as Agent } from "./src-BmbDclOA.js";
7
+ import { r as autoTransformMessages } from "./ai-chat-v5-migration-DguhuLKF.js";
8
+ import { nanoid } from "nanoid";
12
9
 
13
- // src/ai-chat-agent.ts
14
- import {
15
- getToolName,
16
- isToolUIPart,
17
- parsePartialJson
18
- } from "ai";
19
- var decoder = new TextDecoder();
10
+ //#region src/ai-chat-agent.ts
11
+ /** Number of chunks to buffer before flushing to SQLite */
12
+ const CHUNK_BUFFER_SIZE = 10;
13
+ /** Maximum buffer size to prevent memory issues on rapid reconnections */
14
+ const CHUNK_BUFFER_MAX_SIZE = 100;
15
+ /** Maximum age for a "streaming" stream before considering it stale (ms) - 5 minutes */
16
+ const STREAM_STALE_THRESHOLD_MS = 300 * 1e3;
17
+ /** Default cleanup interval for old streams (ms) - every 10 minutes */
18
+ const CLEANUP_INTERVAL_MS = 600 * 1e3;
19
+ /** Default age threshold for cleaning up completed streams (ms) - 24 hours */
20
+ const CLEANUP_AGE_THRESHOLD_MS = 1440 * 60 * 1e3;
21
+ const decoder = new TextDecoder();
22
+ /**
23
+ * Extension of Agent with built-in chat capabilities
24
+ * @template Env Environment type containing bindings
25
+ */
20
26
  var AIChatAgent = class extends Agent {
21
- constructor(ctx, env) {
22
- super(ctx, env);
23
- this.sql`create table if not exists cf_ai_chat_agent_messages (
27
+ constructor(ctx, env) {
28
+ super(ctx, env);
29
+ this._activeStreamId = null;
30
+ this._activeRequestId = null;
31
+ this._streamChunkIndex = 0;
32
+ this._chunkBuffer = [];
33
+ this._isFlushingChunks = false;
34
+ this._lastCleanupTime = 0;
35
+ this.sql`create table if not exists cf_ai_chat_agent_messages (
24
36
  id text primary key,
25
37
  message text not null,
26
38
  created_at datetime default current_timestamp
27
39
  )`;
28
- const rawMessages = (this.sql`select * from cf_ai_chat_agent_messages` || []).map((row) => {
29
- return JSON.parse(row.message);
30
- });
31
- this.messages = autoTransformMessages(rawMessages);
32
- this._chatMessageAbortControllers = /* @__PURE__ */ new Map();
33
- }
34
- _broadcastChatMessage(message, exclude) {
35
- this.broadcast(JSON.stringify(message), exclude);
36
- }
37
- async onMessage(connection, message) {
38
- if (typeof message === "string") {
39
- let data;
40
- try {
41
- data = JSON.parse(message);
42
- } catch (_error) {
43
- return;
44
- }
45
- if (data.type === "cf_agent_use_chat_request" /* CF_AGENT_USE_CHAT_REQUEST */ && data.init.method === "POST") {
46
- const {
47
- // method,
48
- // keepalive,
49
- // headers,
50
- body
51
- // we're reading this
52
- //
53
- // // these might not exist?
54
- // dispatcher,
55
- // duplex
56
- } = data.init;
57
- const { messages } = JSON.parse(body);
58
- const transformedMessages = autoTransformMessages(messages);
59
- this._broadcastChatMessage(
60
- {
61
- messages: transformedMessages,
62
- type: "cf_agent_chat_messages" /* CF_AGENT_CHAT_MESSAGES */
63
- },
64
- [connection.id]
65
- );
66
- await this.persistMessages(transformedMessages, [connection.id]);
67
- this.observability?.emit(
68
- {
69
- displayMessage: "Chat message request",
70
- id: data.id,
71
- payload: {},
72
- timestamp: Date.now(),
73
- type: "message:request"
74
- },
75
- this.ctx
76
- );
77
- const chatMessageId = data.id;
78
- const abortSignal = this._getAbortSignal(chatMessageId);
79
- return this._tryCatchChat(async () => {
80
- const response = await this.onChatMessage(
81
- async (_finishResult) => {
82
- this._removeAbortController(chatMessageId);
83
- this.observability?.emit(
84
- {
85
- displayMessage: "Chat message response",
86
- id: data.id,
87
- payload: {},
88
- timestamp: Date.now(),
89
- type: "message:response"
90
- },
91
- this.ctx
92
- );
93
- },
94
- abortSignal ? { abortSignal } : void 0
95
- );
96
- if (response) {
97
- await this._reply(data.id, response);
98
- } else {
99
- console.warn(
100
- `[AIChatAgent] onChatMessage returned no response for chatMessageId: ${chatMessageId}`
101
- );
102
- this._broadcastChatMessage(
103
- {
104
- body: "No response was generated by the agent.",
105
- done: true,
106
- id: data.id,
107
- type: "cf_agent_use_chat_response" /* CF_AGENT_USE_CHAT_RESPONSE */
108
- },
109
- [connection.id]
110
- );
111
- }
112
- });
113
- }
114
- if (data.type === "cf_agent_chat_clear" /* CF_AGENT_CHAT_CLEAR */) {
115
- this._destroyAbortControllers();
116
- this.sql`delete from cf_ai_chat_agent_messages`;
117
- this.messages = [];
118
- this._broadcastChatMessage(
119
- {
120
- type: "cf_agent_chat_clear" /* CF_AGENT_CHAT_CLEAR */
121
- },
122
- [connection.id]
123
- );
124
- } else if (data.type === "cf_agent_chat_messages" /* CF_AGENT_CHAT_MESSAGES */) {
125
- const transformedMessages = autoTransformMessages(data.messages);
126
- await this.persistMessages(transformedMessages, [connection.id]);
127
- } else if (data.type === "cf_agent_chat_request_cancel" /* CF_AGENT_CHAT_REQUEST_CANCEL */) {
128
- this._cancelChatRequest(data.id);
129
- }
130
- }
131
- }
132
- async onRequest(request) {
133
- return this._tryCatchChat(() => {
134
- const url = new URL(request.url);
135
- if (url.pathname.endsWith("/get-messages")) {
136
- const messages = (this.sql`select * from cf_ai_chat_agent_messages` || []).map((row) => {
137
- return JSON.parse(row.message);
138
- });
139
- return Response.json(messages);
140
- }
141
- return super.onRequest(request);
142
- });
143
- }
144
- async _tryCatchChat(fn) {
145
- try {
146
- return await fn();
147
- } catch (e) {
148
- throw this.onError(e);
149
- }
150
- }
151
- /**
152
- * Handle incoming chat messages and generate a response
153
- * @param onFinish Callback to be called when the response is finished
154
- * @param options.signal A signal to pass to any child requests which can be used to cancel them
155
- * @returns Response to send to the client or undefined
156
- */
157
- async onChatMessage(onFinish, options) {
158
- throw new Error(
159
- "recieved a chat message, override onChatMessage and return a Response to send to the client"
160
- );
161
- }
162
- /**
163
- * Save messages on the server side
164
- * @param messages Chat messages to save
165
- */
166
- async saveMessages(messages) {
167
- await this.persistMessages(messages);
168
- }
169
- async persistMessages(messages, excludeBroadcastIds = []) {
170
- this.sql`delete from cf_ai_chat_agent_messages`;
171
- for (const message of messages) {
172
- this.sql`insert into cf_ai_chat_agent_messages (id, message) values (${message.id},${JSON.stringify(message)})`;
173
- }
174
- this.messages = messages;
175
- this._broadcastChatMessage(
176
- {
177
- messages,
178
- type: "cf_agent_chat_messages" /* CF_AGENT_CHAT_MESSAGES */
179
- },
180
- excludeBroadcastIds
181
- );
182
- }
183
- async _reply(id, response) {
184
- return this._tryCatchChat(async () => {
185
- if (!response.body) {
186
- this._broadcastChatMessage({
187
- body: "",
188
- done: true,
189
- id,
190
- type: "cf_agent_use_chat_response" /* CF_AGENT_USE_CHAT_RESPONSE */
191
- });
192
- return;
193
- }
194
- const reader = response.body.getReader();
195
- const message = {
196
- id: `assistant_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`,
197
- // default
198
- role: "assistant",
199
- parts: []
200
- };
201
- let activeTextParts = {};
202
- let activeReasoningParts = {};
203
- const partialToolCalls = {};
204
- function updateDynamicToolPart(options) {
205
- const part = message.parts.find(
206
- (part2) => part2.type === "dynamic-tool" && part2.toolCallId === options.toolCallId
207
- );
208
- const anyOptions = options;
209
- const anyPart = part;
210
- if (part != null) {
211
- part.state = options.state;
212
- anyPart.toolName = options.toolName;
213
- anyPart.input = anyOptions.input;
214
- anyPart.output = anyOptions.output;
215
- anyPart.errorText = anyOptions.errorText;
216
- anyPart.rawInput = anyOptions.rawInput ?? anyPart.rawInput;
217
- anyPart.preliminary = anyOptions.preliminary;
218
- if (anyOptions.providerMetadata != null && part.state === "input-available") {
219
- part.callProviderMetadata = anyOptions.providerMetadata;
220
- }
221
- } else {
222
- message.parts.push({
223
- type: "dynamic-tool",
224
- toolName: options.toolName,
225
- toolCallId: options.toolCallId,
226
- state: options.state,
227
- input: anyOptions.input,
228
- output: anyOptions.output,
229
- errorText: anyOptions.errorText,
230
- preliminary: anyOptions.preliminary,
231
- ...anyOptions.providerMetadata != null ? { callProviderMetadata: anyOptions.providerMetadata } : {}
232
- });
233
- }
234
- }
235
- function updateToolPart(options) {
236
- const part = message.parts.find(
237
- (part2) => isToolUIPart(part2) && part2.toolCallId === options.toolCallId
238
- );
239
- const anyOptions = options;
240
- const anyPart = part;
241
- if (part != null) {
242
- part.state = options.state;
243
- anyPart.input = anyOptions.input;
244
- anyPart.output = anyOptions.output;
245
- anyPart.errorText = anyOptions.errorText;
246
- anyPart.rawInput = anyOptions.rawInput;
247
- anyPart.preliminary = anyOptions.preliminary;
248
- anyPart.providerExecuted = anyOptions.providerExecuted ?? part.providerExecuted;
249
- if (anyOptions.providerMetadata != null && part.state === "input-available") {
250
- part.callProviderMetadata = anyOptions.providerMetadata;
251
- }
252
- } else {
253
- message.parts.push({
254
- type: `tool-${options.toolName}`,
255
- toolCallId: options.toolCallId,
256
- state: options.state,
257
- input: anyOptions.input,
258
- output: anyOptions.output,
259
- rawInput: anyOptions.rawInput,
260
- errorText: anyOptions.errorText,
261
- providerExecuted: anyOptions.providerExecuted,
262
- preliminary: anyOptions.preliminary,
263
- ...anyOptions.providerMetadata != null ? { callProviderMetadata: anyOptions.providerMetadata } : {}
264
- });
265
- }
266
- }
267
- async function updateMessageMetadata(metadata) {
268
- if (metadata != null) {
269
- const mergedMetadata = message.metadata != null ? { ...message.metadata, ...metadata } : metadata;
270
- message.metadata = mergedMetadata;
271
- }
272
- }
273
- try {
274
- while (true) {
275
- const { done, value } = await reader.read();
276
- if (done) {
277
- this._broadcastChatMessage({
278
- body: "",
279
- done: true,
280
- id,
281
- type: "cf_agent_use_chat_response" /* CF_AGENT_USE_CHAT_RESPONSE */
282
- });
283
- break;
284
- }
285
- const chunk = decoder.decode(value);
286
- const contentType = response.headers.get("content-type") || "";
287
- const isSSE = contentType.includes("text/event-stream");
288
- if (isSSE) {
289
- const lines = chunk.split("\n");
290
- for (const line of lines) {
291
- if (line.startsWith("data: ") && line !== "data: [DONE]") {
292
- try {
293
- const data = JSON.parse(line.slice(6));
294
- switch (data.type) {
295
- case "text-start": {
296
- const textPart = {
297
- type: "text",
298
- text: "",
299
- providerMetadata: data.providerMetadata,
300
- state: "streaming"
301
- };
302
- activeTextParts[data.id] = textPart;
303
- message.parts.push(textPart);
304
- break;
305
- }
306
- case "text-delta": {
307
- const textPart = activeTextParts[data.id];
308
- textPart.text += data.delta;
309
- textPart.providerMetadata = data.providerMetadata ?? textPart.providerMetadata;
310
- break;
311
- }
312
- case "text-end": {
313
- const textPart = activeTextParts[data.id];
314
- textPart.state = "done";
315
- textPart.providerMetadata = data.providerMetadata ?? textPart.providerMetadata;
316
- delete activeTextParts[data.id];
317
- break;
318
- }
319
- case "reasoning-start": {
320
- const reasoningPart = {
321
- type: "reasoning",
322
- text: "",
323
- providerMetadata: data.providerMetadata,
324
- state: "streaming"
325
- };
326
- activeReasoningParts[data.id] = reasoningPart;
327
- message.parts.push(reasoningPart);
328
- break;
329
- }
330
- case "reasoning-delta": {
331
- const reasoningPart = activeReasoningParts[data.id];
332
- reasoningPart.text += data.delta;
333
- reasoningPart.providerMetadata = data.providerMetadata ?? reasoningPart.providerMetadata;
334
- break;
335
- }
336
- case "reasoning-end": {
337
- const reasoningPart = activeReasoningParts[data.id];
338
- reasoningPart.providerMetadata = data.providerMetadata ?? reasoningPart.providerMetadata;
339
- reasoningPart.state = "done";
340
- delete activeReasoningParts[data.id];
341
- break;
342
- }
343
- case "file": {
344
- message.parts.push({
345
- type: "file",
346
- mediaType: data.mediaType,
347
- url: data.url
348
- });
349
- break;
350
- }
351
- case "source-url": {
352
- message.parts.push({
353
- type: "source-url",
354
- sourceId: data.sourceId,
355
- url: data.url,
356
- title: data.title,
357
- providerMetadata: data.providerMetadata
358
- });
359
- break;
360
- }
361
- case "source-document": {
362
- message.parts.push({
363
- type: "source-document",
364
- sourceId: data.sourceId,
365
- mediaType: data.mediaType,
366
- title: data.title,
367
- filename: data.filename,
368
- providerMetadata: data.providerMetadata
369
- });
370
- break;
371
- }
372
- case "tool-input-start": {
373
- const toolInvocations = message.parts.filter(isToolUIPart);
374
- partialToolCalls[data.toolCallId] = {
375
- text: "",
376
- toolName: data.toolName,
377
- index: toolInvocations.length,
378
- dynamic: data.dynamic
379
- };
380
- if (data.dynamic) {
381
- updateDynamicToolPart({
382
- toolCallId: data.toolCallId,
383
- toolName: data.toolName,
384
- state: "input-streaming",
385
- input: void 0
386
- });
387
- } else {
388
- updateToolPart({
389
- toolCallId: data.toolCallId,
390
- toolName: data.toolName,
391
- state: "input-streaming",
392
- input: void 0
393
- });
394
- }
395
- break;
396
- }
397
- case "tool-input-delta": {
398
- const partialToolCall = partialToolCalls[data.toolCallId];
399
- partialToolCall.text += data.inputTextDelta;
400
- const { value: partialArgs } = await parsePartialJson(
401
- partialToolCall.text
402
- );
403
- if (partialToolCall.dynamic) {
404
- updateDynamicToolPart({
405
- toolCallId: data.toolCallId,
406
- toolName: partialToolCall.toolName,
407
- state: "input-streaming",
408
- input: partialArgs
409
- });
410
- } else {
411
- updateToolPart({
412
- toolCallId: data.toolCallId,
413
- toolName: partialToolCall.toolName,
414
- state: "input-streaming",
415
- input: partialArgs
416
- });
417
- }
418
- break;
419
- }
420
- case "tool-input-available": {
421
- if (data.dynamic) {
422
- updateDynamicToolPart({
423
- toolCallId: data.toolCallId,
424
- toolName: data.toolName,
425
- state: "input-available",
426
- input: data.input,
427
- providerMetadata: data.providerMetadata
428
- });
429
- } else {
430
- updateToolPart({
431
- toolCallId: data.toolCallId,
432
- toolName: data.toolName,
433
- state: "input-available",
434
- input: data.input,
435
- providerExecuted: data.providerExecuted,
436
- providerMetadata: data.providerMetadata
437
- });
438
- }
439
- break;
440
- }
441
- case "tool-input-error": {
442
- if (data.dynamic) {
443
- updateDynamicToolPart({
444
- toolCallId: data.toolCallId,
445
- toolName: data.toolName,
446
- state: "output-error",
447
- input: data.input,
448
- errorText: data.errorText,
449
- providerMetadata: data.providerMetadata
450
- });
451
- } else {
452
- updateToolPart({
453
- toolCallId: data.toolCallId,
454
- toolName: data.toolName,
455
- state: "output-error",
456
- input: void 0,
457
- rawInput: data.input,
458
- errorText: data.errorText,
459
- providerExecuted: data.providerExecuted,
460
- providerMetadata: data.providerMetadata
461
- });
462
- }
463
- break;
464
- }
465
- case "tool-output-available": {
466
- if (data.dynamic) {
467
- const toolInvocations = message.parts.filter(
468
- (part) => part.type === "dynamic-tool"
469
- );
470
- const toolInvocation = toolInvocations.find(
471
- (invocation) => invocation.toolCallId === data.toolCallId
472
- );
473
- if (!toolInvocation)
474
- throw new Error("Tool invocation not found");
475
- updateDynamicToolPart({
476
- toolCallId: data.toolCallId,
477
- toolName: toolInvocation.toolName,
478
- state: "output-available",
479
- input: toolInvocation.input,
480
- output: data.output,
481
- preliminary: data.preliminary
482
- });
483
- } else {
484
- const toolInvocations = message.parts.filter(isToolUIPart);
485
- const toolInvocation = toolInvocations.find(
486
- (invocation) => invocation.toolCallId === data.toolCallId
487
- );
488
- if (!toolInvocation)
489
- throw new Error("Tool invocation not found");
490
- updateToolPart({
491
- toolCallId: data.toolCallId,
492
- toolName: getToolName(toolInvocation),
493
- state: "output-available",
494
- input: toolInvocation.input,
495
- output: data.output,
496
- providerExecuted: data.providerExecuted,
497
- preliminary: data.preliminary
498
- });
499
- }
500
- break;
501
- }
502
- case "tool-output-error": {
503
- if (data.dynamic) {
504
- const toolInvocations = message.parts.filter(
505
- (part) => part.type === "dynamic-tool"
506
- );
507
- const toolInvocation = toolInvocations.find(
508
- (invocation) => invocation.toolCallId === data.toolCallId
509
- );
510
- if (!toolInvocation)
511
- throw new Error("Tool invocation not found");
512
- updateDynamicToolPart({
513
- toolCallId: data.toolCallId,
514
- toolName: toolInvocation.toolName,
515
- state: "output-error",
516
- input: toolInvocation.input,
517
- errorText: data.errorText
518
- });
519
- } else {
520
- const toolInvocations = message.parts.filter(isToolUIPart);
521
- const toolInvocation = toolInvocations.find(
522
- (invocation) => invocation.toolCallId === data.toolCallId
523
- );
524
- if (!toolInvocation)
525
- throw new Error("Tool invocation not found");
526
- updateToolPart({
527
- toolCallId: data.toolCallId,
528
- toolName: getToolName(toolInvocation),
529
- state: "output-error",
530
- input: toolInvocation.input,
531
- rawInput: "rawInput" in toolInvocation ? toolInvocation.rawInput : void 0,
532
- errorText: data.errorText
533
- });
534
- }
535
- break;
536
- }
537
- case "start-step": {
538
- message.parts.push({ type: "step-start" });
539
- break;
540
- }
541
- case "finish-step": {
542
- activeTextParts = {};
543
- activeReasoningParts = {};
544
- break;
545
- }
546
- case "start": {
547
- if (data.messageId != null) {
548
- message.id = data.messageId;
549
- }
550
- await updateMessageMetadata(data.messageMetadata);
551
- break;
552
- }
553
- case "finish": {
554
- await updateMessageMetadata(data.messageMetadata);
555
- break;
556
- }
557
- case "message-metadata": {
558
- await updateMessageMetadata(data.messageMetadata);
559
- break;
560
- }
561
- case "error": {
562
- this._broadcastChatMessage({
563
- error: true,
564
- body: data.errorText ?? JSON.stringify(data),
565
- done: false,
566
- id,
567
- type: "cf_agent_use_chat_response" /* CF_AGENT_USE_CHAT_RESPONSE */
568
- });
569
- break;
570
- }
571
- }
572
- this._broadcastChatMessage({
573
- body: JSON.stringify(data),
574
- done: false,
575
- id,
576
- type: "cf_agent_use_chat_response" /* CF_AGENT_USE_CHAT_RESPONSE */
577
- });
578
- } catch (_e) {
579
- }
580
- }
581
- }
582
- } else {
583
- if (chunk.length > 0) {
584
- message.parts.push({ type: "text", text: chunk });
585
- this._broadcastChatMessage({
586
- body: JSON.stringify({ type: "text-delta", delta: chunk }),
587
- done: false,
588
- id,
589
- type: "cf_agent_use_chat_response" /* CF_AGENT_USE_CHAT_RESPONSE */
590
- });
591
- }
592
- }
593
- }
594
- } finally {
595
- reader.releaseLock();
596
- }
597
- if (message.parts.length > 0) {
598
- await this.persistMessages([...this.messages, message]);
599
- }
600
- });
601
- }
602
- /**
603
- * For the given message id, look up its associated AbortController
604
- * If the AbortController does not exist, create and store one in memory
605
- *
606
- * returns the AbortSignal associated with the AbortController
607
- */
608
- _getAbortSignal(id) {
609
- if (typeof id !== "string") {
610
- return void 0;
611
- }
612
- if (!this._chatMessageAbortControllers.has(id)) {
613
- this._chatMessageAbortControllers.set(id, new AbortController());
614
- }
615
- return this._chatMessageAbortControllers.get(id)?.signal;
616
- }
617
- /**
618
- * Remove an abort controller from the cache of pending message responses
619
- */
620
- _removeAbortController(id) {
621
- this._chatMessageAbortControllers.delete(id);
622
- }
623
- /**
624
- * Propagate an abort signal for any requests associated with the given message id
625
- */
626
- _cancelChatRequest(id) {
627
- if (this._chatMessageAbortControllers.has(id)) {
628
- const abortController = this._chatMessageAbortControllers.get(id);
629
- abortController?.abort();
630
- }
631
- }
632
- /**
633
- * Abort all pending requests and clear the cache of AbortControllers
634
- */
635
- _destroyAbortControllers() {
636
- for (const controller of this._chatMessageAbortControllers.values()) {
637
- controller?.abort();
638
- }
639
- this._chatMessageAbortControllers.clear();
640
- }
641
- /**
642
- * When the DO is destroyed, cancel all pending requests
643
- */
644
- async destroy() {
645
- this._destroyAbortControllers();
646
- await super.destroy();
647
- }
648
- };
649
- export {
650
- AIChatAgent
40
+ this.sql`create table if not exists cf_ai_chat_stream_chunks (
41
+ id text primary key,
42
+ stream_id text not null,
43
+ body text not null,
44
+ chunk_index integer not null,
45
+ created_at integer not null
46
+ )`;
47
+ this.sql`create table if not exists cf_ai_chat_stream_metadata (
48
+ id text primary key,
49
+ request_id text not null,
50
+ status text not null,
51
+ created_at integer not null,
52
+ completed_at integer
53
+ )`;
54
+ this.sql`create index if not exists idx_stream_chunks_stream_id
55
+ on cf_ai_chat_stream_chunks(stream_id, chunk_index)`;
56
+ this.messages = autoTransformMessages(this._loadMessagesFromDb());
57
+ this._chatMessageAbortControllers = /* @__PURE__ */ new Map();
58
+ this._restoreActiveStream();
59
+ const _onConnect = this.onConnect.bind(this);
60
+ this.onConnect = async (connection, ctx$1) => {
61
+ if (this._activeStreamId) this._notifyStreamResuming(connection);
62
+ return _onConnect(connection, ctx$1);
63
+ };
64
+ const _onMessage = this.onMessage.bind(this);
65
+ this.onMessage = async (connection, message) => {
66
+ if (typeof message === "string") {
67
+ let data;
68
+ try {
69
+ data = JSON.parse(message);
70
+ } catch (_error) {
71
+ return _onMessage(connection, message);
72
+ }
73
+ if (data.type === MessageType.CF_AGENT_USE_CHAT_REQUEST && data.init.method === "POST") {
74
+ const { body } = data.init;
75
+ const { messages } = JSON.parse(body);
76
+ const transformedMessages = autoTransformMessages(messages);
77
+ this._broadcastChatMessage({
78
+ messages: transformedMessages,
79
+ type: MessageType.CF_AGENT_CHAT_MESSAGES
80
+ }, [connection.id]);
81
+ await this.persistMessages(transformedMessages, [connection.id]);
82
+ this.observability?.emit({
83
+ displayMessage: "Chat message request",
84
+ id: data.id,
85
+ payload: {},
86
+ timestamp: Date.now(),
87
+ type: "message:request"
88
+ }, this.ctx);
89
+ const chatMessageId = data.id;
90
+ const abortSignal = this._getAbortSignal(chatMessageId);
91
+ return this._tryCatchChat(async () => {
92
+ return agentContext.run({
93
+ agent: this,
94
+ connection,
95
+ request: void 0,
96
+ email: void 0
97
+ }, async () => {
98
+ const response = await this.onChatMessage(async (_finishResult) => {
99
+ this._removeAbortController(chatMessageId);
100
+ this.observability?.emit({
101
+ displayMessage: "Chat message response",
102
+ id: data.id,
103
+ payload: {},
104
+ timestamp: Date.now(),
105
+ type: "message:response"
106
+ }, this.ctx);
107
+ }, abortSignal ? { abortSignal } : void 0);
108
+ if (response) await this._reply(data.id, response);
109
+ else {
110
+ console.warn(`[AIChatAgent] onChatMessage returned no response for chatMessageId: ${chatMessageId}`);
111
+ this._broadcastChatMessage({
112
+ body: "No response was generated by the agent.",
113
+ done: true,
114
+ id: data.id,
115
+ type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
116
+ }, [connection.id]);
117
+ }
118
+ });
119
+ });
120
+ }
121
+ if (data.type === MessageType.CF_AGENT_CHAT_CLEAR) {
122
+ this._destroyAbortControllers();
123
+ this.sql`delete from cf_ai_chat_agent_messages`;
124
+ this.sql`delete from cf_ai_chat_stream_chunks`;
125
+ this.sql`delete from cf_ai_chat_stream_metadata`;
126
+ this._activeStreamId = null;
127
+ this._activeRequestId = null;
128
+ this._streamChunkIndex = 0;
129
+ this.messages = [];
130
+ this._broadcastChatMessage({ type: MessageType.CF_AGENT_CHAT_CLEAR }, [connection.id]);
131
+ return;
132
+ }
133
+ if (data.type === MessageType.CF_AGENT_CHAT_MESSAGES) {
134
+ const transformedMessages = autoTransformMessages(data.messages);
135
+ await this.persistMessages(transformedMessages, [connection.id]);
136
+ return;
137
+ }
138
+ if (data.type === MessageType.CF_AGENT_CHAT_REQUEST_CANCEL) {
139
+ this._cancelChatRequest(data.id);
140
+ return;
141
+ }
142
+ if (data.type === MessageType.CF_AGENT_STREAM_RESUME_ACK) {
143
+ if (this._activeStreamId && this._activeRequestId && this._activeRequestId === data.id) this._sendStreamChunks(connection, this._activeStreamId, this._activeRequestId);
144
+ return;
145
+ }
146
+ }
147
+ return _onMessage(connection, message);
148
+ };
149
+ }
150
+ /**
151
+ * Restore active stream state if the agent was restarted during streaming.
152
+ * Called during construction to recover any interrupted streams.
153
+ * Validates stream freshness to avoid sending stale resume notifications.
154
+ * @internal Protected for testing purposes.
155
+ */
156
+ _restoreActiveStream() {
157
+ const activeStreams = this.sql`
158
+ select * from cf_ai_chat_stream_metadata
159
+ where status = 'streaming'
160
+ order by created_at desc
161
+ limit 1
162
+ `;
163
+ if (activeStreams && activeStreams.length > 0) {
164
+ const stream = activeStreams[0];
165
+ const streamAge = Date.now() - stream.created_at;
166
+ if (streamAge > STREAM_STALE_THRESHOLD_MS) {
167
+ this.sql`delete from cf_ai_chat_stream_chunks where stream_id = ${stream.id}`;
168
+ this.sql`delete from cf_ai_chat_stream_metadata where id = ${stream.id}`;
169
+ console.warn(`[AIChatAgent] Deleted stale stream ${stream.id} (age: ${Math.round(streamAge / 1e3)}s)`);
170
+ return;
171
+ }
172
+ this._activeStreamId = stream.id;
173
+ this._activeRequestId = stream.request_id;
174
+ const lastChunk = this.sql`
175
+ select max(chunk_index) as max_index
176
+ from cf_ai_chat_stream_chunks
177
+ where stream_id = ${this._activeStreamId}
178
+ `;
179
+ this._streamChunkIndex = lastChunk && lastChunk[0]?.max_index != null ? lastChunk[0].max_index + 1 : 0;
180
+ }
181
+ }
182
+ /**
183
+ * Notify a connection about an active stream that can be resumed.
184
+ * The client should respond with CF_AGENT_STREAM_RESUME_ACK to receive chunks.
185
+ * Uses in-memory state for request ID - no extra DB lookup needed.
186
+ * @param connection - The WebSocket connection to notify
187
+ */
188
+ _notifyStreamResuming(connection) {
189
+ if (!this._activeStreamId || !this._activeRequestId) return;
190
+ connection.send(JSON.stringify({
191
+ type: MessageType.CF_AGENT_STREAM_RESUMING,
192
+ id: this._activeRequestId
193
+ }));
194
+ }
195
+ /**
196
+ * Send stream chunks to a connection after receiving ACK.
197
+ * @param connection - The WebSocket connection
198
+ * @param streamId - The stream to replay
199
+ * @param requestId - The original request ID
200
+ */
201
+ _sendStreamChunks(connection, streamId, requestId) {
202
+ this._flushChunkBuffer();
203
+ const chunks = this.sql`
204
+ select * from cf_ai_chat_stream_chunks
205
+ where stream_id = ${streamId}
206
+ order by chunk_index asc
207
+ `;
208
+ for (const chunk of chunks || []) connection.send(JSON.stringify({
209
+ body: chunk.body,
210
+ done: false,
211
+ id: requestId,
212
+ type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
213
+ }));
214
+ if (this._activeStreamId !== streamId) connection.send(JSON.stringify({
215
+ body: "",
216
+ done: true,
217
+ id: requestId,
218
+ type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
219
+ }));
220
+ }
221
+ /**
222
+ * Buffer a stream chunk for batch write to SQLite.
223
+ * @param streamId - The stream this chunk belongs to
224
+ * @param body - The serialized chunk body
225
+ * @internal Protected for testing purposes.
226
+ */
227
+ _storeStreamChunk(streamId, body) {
228
+ if (this._chunkBuffer.length >= CHUNK_BUFFER_MAX_SIZE) this._flushChunkBuffer();
229
+ this._chunkBuffer.push({
230
+ id: nanoid(),
231
+ streamId,
232
+ body,
233
+ index: this._streamChunkIndex
234
+ });
235
+ this._streamChunkIndex++;
236
+ if (this._chunkBuffer.length >= CHUNK_BUFFER_SIZE) this._flushChunkBuffer();
237
+ }
238
+ /**
239
+ * Flush buffered chunks to SQLite in a single batch.
240
+ * Uses a lock to prevent concurrent flush operations.
241
+ * @internal Protected for testing purposes.
242
+ */
243
+ _flushChunkBuffer() {
244
+ if (this._isFlushingChunks || this._chunkBuffer.length === 0) return;
245
+ this._isFlushingChunks = true;
246
+ try {
247
+ const chunks = this._chunkBuffer;
248
+ this._chunkBuffer = [];
249
+ const now = Date.now();
250
+ for (const chunk of chunks) this.sql`
251
+ insert into cf_ai_chat_stream_chunks (id, stream_id, body, chunk_index, created_at)
252
+ values (${chunk.id}, ${chunk.streamId}, ${chunk.body}, ${chunk.index}, ${now})
253
+ `;
254
+ } finally {
255
+ this._isFlushingChunks = false;
256
+ }
257
+ }
258
+ /**
259
+ * Start tracking a new stream for resumable streaming.
260
+ * Creates metadata entry in SQLite and sets up tracking state.
261
+ * @param requestId - The unique ID of the chat request
262
+ * @returns The generated stream ID
263
+ * @internal Protected for testing purposes.
264
+ */
265
+ _startStream(requestId) {
266
+ this._flushChunkBuffer();
267
+ const streamId = nanoid();
268
+ this._activeStreamId = streamId;
269
+ this._activeRequestId = requestId;
270
+ this._streamChunkIndex = 0;
271
+ this.sql`
272
+ insert into cf_ai_chat_stream_metadata (id, request_id, status, created_at)
273
+ values (${streamId}, ${requestId}, 'streaming', ${Date.now()})
274
+ `;
275
+ return streamId;
276
+ }
277
+ /**
278
+ * Mark a stream as completed and flush any pending chunks.
279
+ * @param streamId - The stream to mark as completed
280
+ * @internal Protected for testing purposes.
281
+ */
282
+ _completeStream(streamId) {
283
+ this._flushChunkBuffer();
284
+ this.sql`
285
+ update cf_ai_chat_stream_metadata
286
+ set status = 'completed', completed_at = ${Date.now()}
287
+ where id = ${streamId}
288
+ `;
289
+ this._activeStreamId = null;
290
+ this._activeRequestId = null;
291
+ this._streamChunkIndex = 0;
292
+ this._maybeCleanupOldStreams();
293
+ }
294
+ /**
295
+ * Clean up old completed streams if enough time has passed since last cleanup.
296
+ * This prevents database growth while avoiding cleanup overhead on every stream completion.
297
+ */
298
+ _maybeCleanupOldStreams() {
299
+ const now = Date.now();
300
+ if (now - this._lastCleanupTime < CLEANUP_INTERVAL_MS) return;
301
+ this._lastCleanupTime = now;
302
+ const cutoff = now - CLEANUP_AGE_THRESHOLD_MS;
303
+ this.sql`
304
+ delete from cf_ai_chat_stream_chunks
305
+ where stream_id in (
306
+ select id from cf_ai_chat_stream_metadata
307
+ where status = 'completed' and completed_at < ${cutoff}
308
+ )
309
+ `;
310
+ this.sql`
311
+ delete from cf_ai_chat_stream_metadata
312
+ where status = 'completed' and completed_at < ${cutoff}
313
+ `;
314
+ }
315
+ _broadcastChatMessage(message, exclude) {
316
+ this.broadcast(JSON.stringify(message), exclude);
317
+ }
318
+ _loadMessagesFromDb() {
319
+ return (this.sql`select * from cf_ai_chat_agent_messages order by created_at` || []).map((row) => {
320
+ try {
321
+ return JSON.parse(row.message);
322
+ } catch (error) {
323
+ console.error(`Failed to parse message ${row.id}:`, error);
324
+ return null;
325
+ }
326
+ }).filter((msg) => msg !== null);
327
+ }
328
+ async onRequest(request) {
329
+ return this._tryCatchChat(async () => {
330
+ if (new URL(request.url).pathname.endsWith("/get-messages")) {
331
+ const messages = this._loadMessagesFromDb();
332
+ return Response.json(messages);
333
+ }
334
+ return super.onRequest(request);
335
+ });
336
+ }
337
+ async _tryCatchChat(fn) {
338
+ try {
339
+ return await fn();
340
+ } catch (e) {
341
+ throw this.onError(e);
342
+ }
343
+ }
344
+ /**
345
+ * Handle incoming chat messages and generate a response
346
+ * @param onFinish Callback to be called when the response is finished
347
+ * @param options.signal A signal to pass to any child requests which can be used to cancel them
348
+ * @returns Response to send to the client or undefined
349
+ */
350
+ async onChatMessage(onFinish, options) {
351
+ throw new Error("recieved a chat message, override onChatMessage and return a Response to send to the client");
352
+ }
353
+ /**
354
+ * Save messages on the server side
355
+ * @param messages Chat messages to save
356
+ */
357
+ async saveMessages(messages) {
358
+ await this.persistMessages(messages);
359
+ await this._tryCatchChat(async () => {
360
+ const response = await this.onChatMessage(() => {});
361
+ if (response) this._reply(crypto.randomUUID(), response);
362
+ });
363
+ }
364
+ async persistMessages(messages, excludeBroadcastIds = []) {
365
+ for (const message of messages) this.sql`
366
+ insert into cf_ai_chat_agent_messages (id, message)
367
+ values (${message.id}, ${JSON.stringify(message)})
368
+ on conflict(id) do update set message = excluded.message
369
+ `;
370
+ this.messages = autoTransformMessages(this._loadMessagesFromDb());
371
+ this._broadcastChatMessage({
372
+ messages,
373
+ type: MessageType.CF_AGENT_CHAT_MESSAGES
374
+ }, excludeBroadcastIds);
375
+ }
376
+ async _reply(id, response) {
377
+ return this._tryCatchChat(async () => {
378
+ if (!response.body) {
379
+ this._broadcastChatMessage({
380
+ body: "",
381
+ done: true,
382
+ id,
383
+ type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
384
+ });
385
+ return;
386
+ }
387
+ const streamId = this._startStream(id);
388
+ const { getToolName, isToolUIPart, parsePartialJson } = await import("ai");
389
+ const reader = response.body.getReader();
390
+ const message = {
391
+ id: `assistant_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`,
392
+ role: "assistant",
393
+ parts: []
394
+ };
395
+ let activeTextParts = {};
396
+ let activeReasoningParts = {};
397
+ const partialToolCalls = {};
398
+ function updateDynamicToolPart(options) {
399
+ const part = message.parts.find((part$1) => part$1.type === "dynamic-tool" && part$1.toolCallId === options.toolCallId);
400
+ const anyOptions = options;
401
+ const anyPart = part;
402
+ if (part != null) {
403
+ part.state = options.state;
404
+ anyPart.toolName = options.toolName;
405
+ anyPart.input = anyOptions.input;
406
+ anyPart.output = anyOptions.output;
407
+ anyPart.errorText = anyOptions.errorText;
408
+ anyPart.rawInput = anyOptions.rawInput ?? anyPart.rawInput;
409
+ anyPart.preliminary = anyOptions.preliminary;
410
+ if (anyOptions.providerMetadata != null && part.state === "input-available") part.callProviderMetadata = anyOptions.providerMetadata;
411
+ } else message.parts.push({
412
+ type: "dynamic-tool",
413
+ toolName: options.toolName,
414
+ toolCallId: options.toolCallId,
415
+ state: options.state,
416
+ input: anyOptions.input,
417
+ output: anyOptions.output,
418
+ errorText: anyOptions.errorText,
419
+ preliminary: anyOptions.preliminary,
420
+ ...anyOptions.providerMetadata != null ? { callProviderMetadata: anyOptions.providerMetadata } : {}
421
+ });
422
+ }
423
+ function updateToolPart(options) {
424
+ const part = message.parts.find((part$1) => isToolUIPart(part$1) && part$1.toolCallId === options.toolCallId);
425
+ const anyOptions = options;
426
+ const anyPart = part;
427
+ if (part != null) {
428
+ part.state = options.state;
429
+ anyPart.input = anyOptions.input;
430
+ anyPart.output = anyOptions.output;
431
+ anyPart.errorText = anyOptions.errorText;
432
+ anyPart.rawInput = anyOptions.rawInput;
433
+ anyPart.preliminary = anyOptions.preliminary;
434
+ anyPart.providerExecuted = anyOptions.providerExecuted ?? part.providerExecuted;
435
+ if (anyOptions.providerMetadata != null && part.state === "input-available") part.callProviderMetadata = anyOptions.providerMetadata;
436
+ } else message.parts.push({
437
+ type: `tool-${options.toolName}`,
438
+ toolCallId: options.toolCallId,
439
+ state: options.state,
440
+ input: anyOptions.input,
441
+ output: anyOptions.output,
442
+ rawInput: anyOptions.rawInput,
443
+ errorText: anyOptions.errorText,
444
+ providerExecuted: anyOptions.providerExecuted,
445
+ preliminary: anyOptions.preliminary,
446
+ ...anyOptions.providerMetadata != null ? { callProviderMetadata: anyOptions.providerMetadata } : {}
447
+ });
448
+ }
449
+ async function updateMessageMetadata(metadata) {
450
+ if (metadata != null) message.metadata = message.metadata != null ? {
451
+ ...message.metadata,
452
+ ...metadata
453
+ } : metadata;
454
+ }
455
+ let streamCompleted = false;
456
+ try {
457
+ while (true) {
458
+ const { done, value } = await reader.read();
459
+ if (done) {
460
+ this._completeStream(streamId);
461
+ streamCompleted = true;
462
+ this._broadcastChatMessage({
463
+ body: "",
464
+ done: true,
465
+ id,
466
+ type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
467
+ });
468
+ break;
469
+ }
470
+ const chunk = decoder.decode(value);
471
+ if ((response.headers.get("content-type") || "").includes("text/event-stream")) {
472
+ const lines = chunk.split("\n");
473
+ for (const line of lines) if (line.startsWith("data: ") && line !== "data: [DONE]") try {
474
+ const data = JSON.parse(line.slice(6));
475
+ switch (data.type) {
476
+ case "text-start": {
477
+ const textPart = {
478
+ type: "text",
479
+ text: "",
480
+ providerMetadata: data.providerMetadata,
481
+ state: "streaming"
482
+ };
483
+ activeTextParts[data.id] = textPart;
484
+ message.parts.push(textPart);
485
+ break;
486
+ }
487
+ case "text-delta": {
488
+ const textPart = activeTextParts[data.id];
489
+ textPart.text += data.delta;
490
+ textPart.providerMetadata = data.providerMetadata ?? textPart.providerMetadata;
491
+ break;
492
+ }
493
+ case "text-end": {
494
+ const textPart = activeTextParts[data.id];
495
+ textPart.state = "done";
496
+ textPart.providerMetadata = data.providerMetadata ?? textPart.providerMetadata;
497
+ delete activeTextParts[data.id];
498
+ break;
499
+ }
500
+ case "reasoning-start": {
501
+ const reasoningPart = {
502
+ type: "reasoning",
503
+ text: "",
504
+ providerMetadata: data.providerMetadata,
505
+ state: "streaming"
506
+ };
507
+ activeReasoningParts[data.id] = reasoningPart;
508
+ message.parts.push(reasoningPart);
509
+ break;
510
+ }
511
+ case "reasoning-delta": {
512
+ const reasoningPart = activeReasoningParts[data.id];
513
+ reasoningPart.text += data.delta;
514
+ reasoningPart.providerMetadata = data.providerMetadata ?? reasoningPart.providerMetadata;
515
+ break;
516
+ }
517
+ case "reasoning-end": {
518
+ const reasoningPart = activeReasoningParts[data.id];
519
+ reasoningPart.providerMetadata = data.providerMetadata ?? reasoningPart.providerMetadata;
520
+ reasoningPart.state = "done";
521
+ delete activeReasoningParts[data.id];
522
+ break;
523
+ }
524
+ case "file":
525
+ message.parts.push({
526
+ type: "file",
527
+ mediaType: data.mediaType,
528
+ url: data.url
529
+ });
530
+ break;
531
+ case "source-url":
532
+ message.parts.push({
533
+ type: "source-url",
534
+ sourceId: data.sourceId,
535
+ url: data.url,
536
+ title: data.title,
537
+ providerMetadata: data.providerMetadata
538
+ });
539
+ break;
540
+ case "source-document":
541
+ message.parts.push({
542
+ type: "source-document",
543
+ sourceId: data.sourceId,
544
+ mediaType: data.mediaType,
545
+ title: data.title,
546
+ filename: data.filename,
547
+ providerMetadata: data.providerMetadata
548
+ });
549
+ break;
550
+ case "tool-input-start": {
551
+ const toolInvocations = message.parts.filter(isToolUIPart);
552
+ partialToolCalls[data.toolCallId] = {
553
+ text: "",
554
+ toolName: data.toolName,
555
+ index: toolInvocations.length,
556
+ dynamic: data.dynamic
557
+ };
558
+ if (data.dynamic) updateDynamicToolPart({
559
+ toolCallId: data.toolCallId,
560
+ toolName: data.toolName,
561
+ state: "input-streaming",
562
+ input: void 0
563
+ });
564
+ else updateToolPart({
565
+ toolCallId: data.toolCallId,
566
+ toolName: data.toolName,
567
+ state: "input-streaming",
568
+ input: void 0
569
+ });
570
+ break;
571
+ }
572
+ case "tool-input-delta": {
573
+ const partialToolCall = partialToolCalls[data.toolCallId];
574
+ partialToolCall.text += data.inputTextDelta;
575
+ const partialArgs = (await parsePartialJson(partialToolCall.text)).value;
576
+ if (partialToolCall.dynamic) updateDynamicToolPart({
577
+ toolCallId: data.toolCallId,
578
+ toolName: partialToolCall.toolName,
579
+ state: "input-streaming",
580
+ input: partialArgs
581
+ });
582
+ else updateToolPart({
583
+ toolCallId: data.toolCallId,
584
+ toolName: partialToolCall.toolName,
585
+ state: "input-streaming",
586
+ input: partialArgs
587
+ });
588
+ break;
589
+ }
590
+ case "tool-input-available":
591
+ if (data.dynamic) updateDynamicToolPart({
592
+ toolCallId: data.toolCallId,
593
+ toolName: data.toolName,
594
+ state: "input-available",
595
+ input: data.input,
596
+ providerMetadata: data.providerMetadata
597
+ });
598
+ else updateToolPart({
599
+ toolCallId: data.toolCallId,
600
+ toolName: data.toolName,
601
+ state: "input-available",
602
+ input: data.input,
603
+ providerExecuted: data.providerExecuted,
604
+ providerMetadata: data.providerMetadata
605
+ });
606
+ break;
607
+ case "tool-input-error":
608
+ if (data.dynamic) updateDynamicToolPart({
609
+ toolCallId: data.toolCallId,
610
+ toolName: data.toolName,
611
+ state: "output-error",
612
+ input: data.input,
613
+ errorText: data.errorText,
614
+ providerMetadata: data.providerMetadata
615
+ });
616
+ else updateToolPart({
617
+ toolCallId: data.toolCallId,
618
+ toolName: data.toolName,
619
+ state: "output-error",
620
+ input: void 0,
621
+ rawInput: data.input,
622
+ errorText: data.errorText,
623
+ providerExecuted: data.providerExecuted,
624
+ providerMetadata: data.providerMetadata
625
+ });
626
+ break;
627
+ case "tool-output-available":
628
+ if (data.dynamic) {
629
+ const toolInvocation = message.parts.filter((part) => part.type === "dynamic-tool").find((invocation) => invocation.toolCallId === data.toolCallId);
630
+ if (!toolInvocation) throw new Error("Tool invocation not found");
631
+ updateDynamicToolPart({
632
+ toolCallId: data.toolCallId,
633
+ toolName: toolInvocation.toolName,
634
+ state: "output-available",
635
+ input: toolInvocation.input,
636
+ output: data.output,
637
+ preliminary: data.preliminary
638
+ });
639
+ } else {
640
+ const toolInvocation = message.parts.filter(isToolUIPart).find((invocation) => invocation.toolCallId === data.toolCallId);
641
+ if (!toolInvocation) throw new Error("Tool invocation not found");
642
+ updateToolPart({
643
+ toolCallId: data.toolCallId,
644
+ toolName: getToolName(toolInvocation),
645
+ state: "output-available",
646
+ input: toolInvocation.input,
647
+ output: data.output,
648
+ providerExecuted: data.providerExecuted,
649
+ preliminary: data.preliminary
650
+ });
651
+ }
652
+ break;
653
+ case "tool-output-error":
654
+ if (data.dynamic) {
655
+ const toolInvocation = message.parts.filter((part) => part.type === "dynamic-tool").find((invocation) => invocation.toolCallId === data.toolCallId);
656
+ if (!toolInvocation) throw new Error("Tool invocation not found");
657
+ updateDynamicToolPart({
658
+ toolCallId: data.toolCallId,
659
+ toolName: toolInvocation.toolName,
660
+ state: "output-error",
661
+ input: toolInvocation.input,
662
+ errorText: data.errorText
663
+ });
664
+ } else {
665
+ const toolInvocation = message.parts.filter(isToolUIPart).find((invocation) => invocation.toolCallId === data.toolCallId);
666
+ if (!toolInvocation) throw new Error("Tool invocation not found");
667
+ updateToolPart({
668
+ toolCallId: data.toolCallId,
669
+ toolName: getToolName(toolInvocation),
670
+ state: "output-error",
671
+ input: toolInvocation.input,
672
+ rawInput: "rawInput" in toolInvocation ? toolInvocation.rawInput : void 0,
673
+ errorText: data.errorText
674
+ });
675
+ }
676
+ break;
677
+ case "start-step":
678
+ message.parts.push({ type: "step-start" });
679
+ break;
680
+ case "finish-step":
681
+ activeTextParts = {};
682
+ activeReasoningParts = {};
683
+ break;
684
+ case "start":
685
+ if (data.messageId != null) message.id = data.messageId;
686
+ await updateMessageMetadata(data.messageMetadata);
687
+ break;
688
+ case "finish":
689
+ await updateMessageMetadata(data.messageMetadata);
690
+ break;
691
+ case "message-metadata":
692
+ await updateMessageMetadata(data.messageMetadata);
693
+ break;
694
+ case "error":
695
+ this._broadcastChatMessage({
696
+ error: true,
697
+ body: data.errorText ?? JSON.stringify(data),
698
+ done: false,
699
+ id,
700
+ type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
701
+ });
702
+ break;
703
+ }
704
+ let eventToSend = data;
705
+ if (data.type === "finish" && "finishReason" in data) {
706
+ const { finishReason, ...rest } = data;
707
+ eventToSend = {
708
+ ...rest,
709
+ type: "finish",
710
+ messageMetadata: { finishReason }
711
+ };
712
+ }
713
+ const chunkBody = JSON.stringify(eventToSend);
714
+ this._storeStreamChunk(streamId, chunkBody);
715
+ this._broadcastChatMessage({
716
+ body: chunkBody,
717
+ done: false,
718
+ id,
719
+ type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
720
+ });
721
+ } catch (_error) {}
722
+ } else if (chunk.length > 0) {
723
+ message.parts.push({
724
+ type: "text",
725
+ text: chunk
726
+ });
727
+ const chunkBody = JSON.stringify({
728
+ type: "text-delta",
729
+ delta: chunk
730
+ });
731
+ this._storeStreamChunk(streamId, chunkBody);
732
+ this._broadcastChatMessage({
733
+ body: chunkBody,
734
+ done: false,
735
+ id,
736
+ type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
737
+ });
738
+ }
739
+ }
740
+ } catch (error) {
741
+ if (!streamCompleted) {
742
+ this._markStreamError(streamId);
743
+ this._broadcastChatMessage({
744
+ body: error instanceof Error ? error.message : "Stream error",
745
+ done: true,
746
+ error: true,
747
+ id,
748
+ type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
749
+ });
750
+ }
751
+ throw error;
752
+ } finally {
753
+ reader.releaseLock();
754
+ }
755
+ if (message.parts.length > 0) await this.persistMessages([...this.messages, message]);
756
+ });
757
+ }
758
+ /**
759
+ * Mark a stream as errored and clean up state.
760
+ * @param streamId - The stream to mark as errored
761
+ * @internal Protected for testing purposes.
762
+ */
763
+ _markStreamError(streamId) {
764
+ this._flushChunkBuffer();
765
+ this.sql`
766
+ update cf_ai_chat_stream_metadata
767
+ set status = 'error', completed_at = ${Date.now()}
768
+ where id = ${streamId}
769
+ `;
770
+ this._activeStreamId = null;
771
+ this._activeRequestId = null;
772
+ this._streamChunkIndex = 0;
773
+ }
774
+ /**
775
+ * For the given message id, look up its associated AbortController
776
+ * If the AbortController does not exist, create and store one in memory
777
+ *
778
+ * returns the AbortSignal associated with the AbortController
779
+ */
780
+ _getAbortSignal(id) {
781
+ if (typeof id !== "string") return;
782
+ if (!this._chatMessageAbortControllers.has(id)) this._chatMessageAbortControllers.set(id, new AbortController());
783
+ return this._chatMessageAbortControllers.get(id)?.signal;
784
+ }
785
+ /**
786
+ * Remove an abort controller from the cache of pending message responses
787
+ */
788
+ _removeAbortController(id) {
789
+ this._chatMessageAbortControllers.delete(id);
790
+ }
791
+ /**
792
+ * Propagate an abort signal for any requests associated with the given message id
793
+ */
794
+ _cancelChatRequest(id) {
795
+ if (this._chatMessageAbortControllers.has(id)) this._chatMessageAbortControllers.get(id)?.abort();
796
+ }
797
+ /**
798
+ * Abort all pending requests and clear the cache of AbortControllers
799
+ */
800
+ _destroyAbortControllers() {
801
+ for (const controller of this._chatMessageAbortControllers.values()) controller?.abort();
802
+ this._chatMessageAbortControllers.clear();
803
+ }
804
+ /**
805
+ * When the DO is destroyed, cancel all pending requests and clean up resources
806
+ */
807
+ async destroy() {
808
+ this._destroyAbortControllers();
809
+ this._flushChunkBuffer();
810
+ this.sql`drop table if exists cf_ai_chat_stream_chunks`;
811
+ this.sql`drop table if exists cf_ai_chat_stream_metadata`;
812
+ this._activeStreamId = null;
813
+ this._activeRequestId = null;
814
+ await super.destroy();
815
+ }
651
816
  };
817
+
818
+ //#endregion
819
+ export { AIChatAgent };
652
820
  //# sourceMappingURL=ai-chat-agent.js.map