agents 0.0.0-2cc0f02 → 0.0.0-2e73791

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 (68) hide show
  1. package/README.md +126 -3
  2. package/dist/ai-chat-agent.d.ts +231 -22
  3. package/dist/ai-chat-agent.js +691 -121
  4. package/dist/ai-chat-agent.js.map +1 -1
  5. package/dist/{ai-chat-v5-migration-gdyLiTd8.js → ai-chat-v5-migration-DguhuLKF.js} +2 -2
  6. package/dist/{ai-chat-v5-migration-gdyLiTd8.js.map → ai-chat-v5-migration-DguhuLKF.js.map} +1 -1
  7. package/dist/ai-chat-v5-migration.js +1 -1
  8. package/dist/ai-react.d.ts +145 -16
  9. package/dist/ai-react.js +389 -75
  10. package/dist/ai-react.js.map +1 -1
  11. package/dist/{ai-types-B0GBFDwi.js → ai-types-DEtF_8Km.js} +10 -2
  12. package/dist/ai-types-DEtF_8Km.js.map +1 -0
  13. package/dist/{ai-types-BWW4umHY.d.ts → ai-types-U8lYA0o8.d.ts} +41 -9
  14. package/dist/ai-types.d.ts +4 -4
  15. package/dist/ai-types.js +1 -1
  16. package/dist/cli/index.d.ts +1 -0
  17. package/dist/cli/index.js +28 -0
  18. package/dist/cli/index.js.map +1 -0
  19. package/dist/client-Cfw92Wb_.d.ts +834 -0
  20. package/dist/{client-CmMi85Sj.d.ts → client-ClORm6f0.d.ts} +10 -10
  21. package/dist/{client-zS-OCVJA.js → client-DjTPRM8-.js} +3 -3
  22. package/dist/{client-zS-OCVJA.js.map → client-DjTPRM8-.js.map} +1 -1
  23. package/dist/client-QZa2Rq0l.js +1105 -0
  24. package/dist/client-QZa2Rq0l.js.map +1 -0
  25. package/dist/client.d.ts +8 -8
  26. package/dist/client.js +2 -2
  27. package/dist/codemode/ai.js +7 -6
  28. package/dist/codemode/ai.js.map +1 -1
  29. package/dist/context-BkKbAa1R.js +8 -0
  30. package/dist/context-BkKbAa1R.js.map +1 -0
  31. package/dist/context-_sPQqJWv.d.ts +24 -0
  32. package/dist/context.d.ts +6 -0
  33. package/dist/context.js +3 -0
  34. package/dist/{do-oauth-client-provider-CCwGwnrA.d.ts → do-oauth-client-provider-B-ryFIPr.d.ts} +21 -6
  35. package/dist/{do-oauth-client-provider-B2jr6UNq.js → do-oauth-client-provider-B1fVIshX.js} +71 -9
  36. package/dist/do-oauth-client-provider-B1fVIshX.js.map +1 -0
  37. package/dist/{index-W4JUkafc.d.ts → index-CyDpAVHZ.d.ts} +7 -3
  38. package/dist/{index-DWcUTPtX.d.ts → index-xIS9I1YX.d.ts} +83 -65
  39. package/dist/index.d.ts +36 -36
  40. package/dist/index.js +6 -5
  41. package/dist/mcp/client.d.ts +4 -4
  42. package/dist/mcp/client.js +2 -1
  43. package/dist/mcp/do-oauth-client-provider.d.ts +1 -1
  44. package/dist/mcp/do-oauth-client-provider.js +1 -1
  45. package/dist/mcp/index.d.ts +140 -12
  46. package/dist/mcp/index.js +636 -41
  47. package/dist/mcp/index.js.map +1 -1
  48. package/dist/mcp/x402.js +10 -6
  49. package/dist/mcp/x402.js.map +1 -1
  50. package/dist/{mcp-BEwaCsxO.d.ts → mcp-CzbSsLfc.d.ts} +2 -2
  51. package/dist/observability/index.d.ts +2 -2
  52. package/dist/observability/index.js +6 -5
  53. package/dist/{react-B8BT6PYZ.d.ts → react-ElpIreHg.d.ts} +35 -17
  54. package/dist/react.d.ts +15 -10
  55. package/dist/react.js +57 -57
  56. package/dist/react.js.map +1 -1
  57. package/dist/{serializable-gtr9YMhp.d.ts → serializable-C4GLimgv.d.ts} +8 -3
  58. package/dist/serializable.d.ts +5 -5
  59. package/dist/{src-C9xZ0CrH.js → src-BZDh910Z.js} +130 -128
  60. package/dist/src-BZDh910Z.js.map +1 -0
  61. package/package.json +61 -44
  62. package/dist/ai-types-B0GBFDwi.js.map +0 -1
  63. package/dist/client-C-u-lCFT.d.ts +0 -5311
  64. package/dist/client-WbaRgKYN.js +0 -788
  65. package/dist/client-WbaRgKYN.js.map +0 -1
  66. package/dist/do-oauth-client-provider-B2jr6UNq.js.map +0 -1
  67. package/dist/src-C9xZ0CrH.js.map +0 -1
  68. package/src/index.ts +0 -2031
@@ -1,24 +1,46 @@
1
- import { __require } from "./client-WbaRgKYN.js";
2
- import { MessageType } from "./ai-types-B0GBFDwi.js";
3
- import "./client-zS-OCVJA.js";
4
- import "./do-oauth-client-provider-B2jr6UNq.js";
5
- import { Agent } from "./src-C9xZ0CrH.js";
6
- import { autoTransformMessages } from "./ai-chat-v5-migration-gdyLiTd8.js";
1
+ import { t as agentContext } from "./context-BkKbAa1R.js";
2
+ import { t as MessageType } from "./ai-types-DEtF_8Km.js";
3
+ import "./client-DjTPRM8-.js";
4
+ import "./client-QZa2Rq0l.js";
5
+ import "./do-oauth-client-provider-B1fVIshX.js";
6
+ import { t as Agent } from "./src-BZDh910Z.js";
7
+ import { r as autoTransformMessages } from "./ai-chat-v5-migration-DguhuLKF.js";
8
+ import { jsonSchema, tool } from "ai";
9
+ import { nanoid } from "nanoid";
7
10
 
8
11
  //#region src/ai-chat-agent.ts
9
- const decoder = new TextDecoder();
10
- let aiSDK;
11
- function getAISDK() {
12
- if (!aiSDK) {
13
- const { getToolName, isToolUIPart, parsePartialJson } = __require("ai");
14
- aiSDK = {
15
- getToolName,
16
- isToolUIPart,
17
- parsePartialJson
18
- };
12
+ /**
13
+ * Converts client tool schemas to AI SDK tool format.
14
+ *
15
+ * These tools have no `execute` function - when the AI model calls them,
16
+ * the tool call is sent back to the client for execution.
17
+ *
18
+ * @param clientTools - Array of tool schemas from the client
19
+ * @returns Record of AI SDK tools that can be spread into your tools object
20
+ */
21
+ function createToolsFromClientSchemas(clientTools) {
22
+ if (!clientTools || clientTools.length === 0) return {};
23
+ const seenNames = /* @__PURE__ */ new Set();
24
+ for (const t of clientTools) {
25
+ if (seenNames.has(t.name)) console.warn(`[createToolsFromClientSchemas] Duplicate tool name "${t.name}" found. Later definitions will override earlier ones.`);
26
+ seenNames.add(t.name);
19
27
  }
20
- return aiSDK;
28
+ return Object.fromEntries(clientTools.map((t) => [t.name, tool({
29
+ description: t.description ?? "",
30
+ inputSchema: jsonSchema(t.parameters ?? { type: "object" })
31
+ })]));
21
32
  }
33
+ /** Number of chunks to buffer before flushing to SQLite */
34
+ const CHUNK_BUFFER_SIZE = 10;
35
+ /** Maximum buffer size to prevent memory issues on rapid reconnections */
36
+ const CHUNK_BUFFER_MAX_SIZE = 100;
37
+ /** Maximum age for a "streaming" stream before considering it stale (ms) - 5 minutes */
38
+ const STREAM_STALE_THRESHOLD_MS = 300 * 1e3;
39
+ /** Default cleanup interval for old streams (ms) - every 10 minutes */
40
+ const CLEANUP_INTERVAL_MS = 600 * 1e3;
41
+ /** Default age threshold for cleaning up completed streams (ms) - 24 hours */
42
+ const CLEANUP_AGE_THRESHOLD_MS = 1440 * 60 * 1e3;
43
+ const decoder = new TextDecoder();
22
44
  /**
23
45
  * Extension of Agent with built-in chat capabilities
24
46
  * @template Env Environment type containing bindings
@@ -26,13 +48,333 @@ function getAISDK() {
26
48
  var AIChatAgent = class extends Agent {
27
49
  constructor(ctx, env) {
28
50
  super(ctx, env);
51
+ this._activeStreamId = null;
52
+ this._activeRequestId = null;
53
+ this._streamingMessage = null;
54
+ this._streamCompletionPromise = null;
55
+ this._streamCompletionResolve = null;
56
+ this._streamChunkIndex = 0;
57
+ this._chunkBuffer = [];
58
+ this._isFlushingChunks = false;
59
+ this._lastCleanupTime = 0;
29
60
  this.sql`create table if not exists cf_ai_chat_agent_messages (
30
61
  id text primary key,
31
62
  message text not null,
32
63
  created_at datetime default current_timestamp
33
64
  )`;
65
+ this.sql`create table if not exists cf_ai_chat_stream_chunks (
66
+ id text primary key,
67
+ stream_id text not null,
68
+ body text not null,
69
+ chunk_index integer not null,
70
+ created_at integer not null
71
+ )`;
72
+ this.sql`create table if not exists cf_ai_chat_stream_metadata (
73
+ id text primary key,
74
+ request_id text not null,
75
+ status text not null,
76
+ created_at integer not null,
77
+ completed_at integer
78
+ )`;
79
+ this.sql`create index if not exists idx_stream_chunks_stream_id
80
+ on cf_ai_chat_stream_chunks(stream_id, chunk_index)`;
34
81
  this.messages = autoTransformMessages(this._loadMessagesFromDb());
35
82
  this._chatMessageAbortControllers = /* @__PURE__ */ new Map();
83
+ this._restoreActiveStream();
84
+ const _onConnect = this.onConnect.bind(this);
85
+ this.onConnect = async (connection, ctx$1) => {
86
+ if (this._activeStreamId) this._notifyStreamResuming(connection);
87
+ return _onConnect(connection, ctx$1);
88
+ };
89
+ const _onMessage = this.onMessage.bind(this);
90
+ this.onMessage = async (connection, message) => {
91
+ if (typeof message === "string") {
92
+ let data;
93
+ try {
94
+ data = JSON.parse(message);
95
+ } catch (_error) {
96
+ return _onMessage(connection, message);
97
+ }
98
+ if (data.type === MessageType.CF_AGENT_USE_CHAT_REQUEST && data.init.method === "POST") {
99
+ const { body } = data.init;
100
+ const { messages, clientTools } = JSON.parse(body);
101
+ const transformedMessages = autoTransformMessages(messages);
102
+ this._broadcastChatMessage({
103
+ messages: transformedMessages,
104
+ type: MessageType.CF_AGENT_CHAT_MESSAGES
105
+ }, [connection.id]);
106
+ await this.persistMessages(transformedMessages, [connection.id]);
107
+ this.observability?.emit({
108
+ displayMessage: "Chat message request",
109
+ id: data.id,
110
+ payload: {},
111
+ timestamp: Date.now(),
112
+ type: "message:request"
113
+ }, this.ctx);
114
+ const chatMessageId = data.id;
115
+ const abortSignal = this._getAbortSignal(chatMessageId);
116
+ return this._tryCatchChat(async () => {
117
+ return agentContext.run({
118
+ agent: this,
119
+ connection,
120
+ request: void 0,
121
+ email: void 0
122
+ }, async () => {
123
+ const response = await this.onChatMessage(async (_finishResult) => {
124
+ this._removeAbortController(chatMessageId);
125
+ this.observability?.emit({
126
+ displayMessage: "Chat message response",
127
+ id: data.id,
128
+ payload: {},
129
+ timestamp: Date.now(),
130
+ type: "message:response"
131
+ }, this.ctx);
132
+ }, {
133
+ abortSignal,
134
+ clientTools
135
+ });
136
+ if (response) await this._reply(data.id, response, [connection.id]);
137
+ else {
138
+ console.warn(`[AIChatAgent] onChatMessage returned no response for chatMessageId: ${chatMessageId}`);
139
+ this._broadcastChatMessage({
140
+ body: "No response was generated by the agent.",
141
+ done: true,
142
+ id: data.id,
143
+ type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
144
+ }, [connection.id]);
145
+ }
146
+ });
147
+ });
148
+ }
149
+ if (data.type === MessageType.CF_AGENT_CHAT_CLEAR) {
150
+ this._destroyAbortControllers();
151
+ this.sql`delete from cf_ai_chat_agent_messages`;
152
+ this.sql`delete from cf_ai_chat_stream_chunks`;
153
+ this.sql`delete from cf_ai_chat_stream_metadata`;
154
+ this._activeStreamId = null;
155
+ this._activeRequestId = null;
156
+ this._streamChunkIndex = 0;
157
+ this.messages = [];
158
+ this._broadcastChatMessage({ type: MessageType.CF_AGENT_CHAT_CLEAR }, [connection.id]);
159
+ return;
160
+ }
161
+ if (data.type === MessageType.CF_AGENT_CHAT_MESSAGES) {
162
+ const transformedMessages = autoTransformMessages(data.messages);
163
+ await this.persistMessages(transformedMessages, [connection.id]);
164
+ return;
165
+ }
166
+ if (data.type === MessageType.CF_AGENT_CHAT_REQUEST_CANCEL) {
167
+ this._cancelChatRequest(data.id);
168
+ return;
169
+ }
170
+ if (data.type === MessageType.CF_AGENT_STREAM_RESUME_ACK) {
171
+ if (this._activeStreamId && this._activeRequestId && this._activeRequestId === data.id) this._sendStreamChunks(connection, this._activeStreamId, this._activeRequestId);
172
+ return;
173
+ }
174
+ if (data.type === MessageType.CF_AGENT_TOOL_RESULT) {
175
+ const { toolCallId, toolName, output, autoContinue } = data;
176
+ this._applyToolResult(toolCallId, toolName, output).then((applied) => {
177
+ if (applied && autoContinue) {
178
+ const waitForStream = async () => {
179
+ if (this._streamCompletionPromise) await this._streamCompletionPromise;
180
+ else await new Promise((resolve) => setTimeout(resolve, 500));
181
+ };
182
+ waitForStream().then(() => {
183
+ const continuationId = nanoid();
184
+ const abortSignal = this._getAbortSignal(continuationId);
185
+ this._tryCatchChat(async () => {
186
+ return agentContext.run({
187
+ agent: this,
188
+ connection,
189
+ request: void 0,
190
+ email: void 0
191
+ }, async () => {
192
+ const response = await this.onChatMessage(async (_finishResult) => {
193
+ this._removeAbortController(continuationId);
194
+ this.observability?.emit({
195
+ displayMessage: "Chat message response (tool continuation)",
196
+ id: continuationId,
197
+ payload: {},
198
+ timestamp: Date.now(),
199
+ type: "message:response"
200
+ }, this.ctx);
201
+ }, { abortSignal });
202
+ if (response) await this._reply(continuationId, response, [], { continuation: true });
203
+ });
204
+ });
205
+ });
206
+ }
207
+ });
208
+ return;
209
+ }
210
+ }
211
+ return _onMessage(connection, message);
212
+ };
213
+ }
214
+ /**
215
+ * Restore active stream state if the agent was restarted during streaming.
216
+ * Called during construction to recover any interrupted streams.
217
+ * Validates stream freshness to avoid sending stale resume notifications.
218
+ * @internal Protected for testing purposes.
219
+ */
220
+ _restoreActiveStream() {
221
+ const activeStreams = this.sql`
222
+ select * from cf_ai_chat_stream_metadata
223
+ where status = 'streaming'
224
+ order by created_at desc
225
+ limit 1
226
+ `;
227
+ if (activeStreams && activeStreams.length > 0) {
228
+ const stream = activeStreams[0];
229
+ const streamAge = Date.now() - stream.created_at;
230
+ if (streamAge > STREAM_STALE_THRESHOLD_MS) {
231
+ this.sql`delete from cf_ai_chat_stream_chunks where stream_id = ${stream.id}`;
232
+ this.sql`delete from cf_ai_chat_stream_metadata where id = ${stream.id}`;
233
+ console.warn(`[AIChatAgent] Deleted stale stream ${stream.id} (age: ${Math.round(streamAge / 1e3)}s)`);
234
+ return;
235
+ }
236
+ this._activeStreamId = stream.id;
237
+ this._activeRequestId = stream.request_id;
238
+ const lastChunk = this.sql`
239
+ select max(chunk_index) as max_index
240
+ from cf_ai_chat_stream_chunks
241
+ where stream_id = ${this._activeStreamId}
242
+ `;
243
+ this._streamChunkIndex = lastChunk && lastChunk[0]?.max_index != null ? lastChunk[0].max_index + 1 : 0;
244
+ }
245
+ }
246
+ /**
247
+ * Notify a connection about an active stream that can be resumed.
248
+ * The client should respond with CF_AGENT_STREAM_RESUME_ACK to receive chunks.
249
+ * Uses in-memory state for request ID - no extra DB lookup needed.
250
+ * @param connection - The WebSocket connection to notify
251
+ */
252
+ _notifyStreamResuming(connection) {
253
+ if (!this._activeStreamId || !this._activeRequestId) return;
254
+ connection.send(JSON.stringify({
255
+ type: MessageType.CF_AGENT_STREAM_RESUMING,
256
+ id: this._activeRequestId
257
+ }));
258
+ }
259
+ /**
260
+ * Send stream chunks to a connection after receiving ACK.
261
+ * @param connection - The WebSocket connection
262
+ * @param streamId - The stream to replay
263
+ * @param requestId - The original request ID
264
+ */
265
+ _sendStreamChunks(connection, streamId, requestId) {
266
+ this._flushChunkBuffer();
267
+ const chunks = this.sql`
268
+ select * from cf_ai_chat_stream_chunks
269
+ where stream_id = ${streamId}
270
+ order by chunk_index asc
271
+ `;
272
+ for (const chunk of chunks || []) connection.send(JSON.stringify({
273
+ body: chunk.body,
274
+ done: false,
275
+ id: requestId,
276
+ type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
277
+ }));
278
+ if (this._activeStreamId !== streamId) connection.send(JSON.stringify({
279
+ body: "",
280
+ done: true,
281
+ id: requestId,
282
+ type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
283
+ }));
284
+ }
285
+ /**
286
+ * Buffer a stream chunk for batch write to SQLite.
287
+ * @param streamId - The stream this chunk belongs to
288
+ * @param body - The serialized chunk body
289
+ * @internal Protected for testing purposes.
290
+ */
291
+ _storeStreamChunk(streamId, body) {
292
+ if (this._chunkBuffer.length >= CHUNK_BUFFER_MAX_SIZE) this._flushChunkBuffer();
293
+ this._chunkBuffer.push({
294
+ id: nanoid(),
295
+ streamId,
296
+ body,
297
+ index: this._streamChunkIndex
298
+ });
299
+ this._streamChunkIndex++;
300
+ if (this._chunkBuffer.length >= CHUNK_BUFFER_SIZE) this._flushChunkBuffer();
301
+ }
302
+ /**
303
+ * Flush buffered chunks to SQLite in a single batch.
304
+ * Uses a lock to prevent concurrent flush operations.
305
+ * @internal Protected for testing purposes.
306
+ */
307
+ _flushChunkBuffer() {
308
+ if (this._isFlushingChunks || this._chunkBuffer.length === 0) return;
309
+ this._isFlushingChunks = true;
310
+ try {
311
+ const chunks = this._chunkBuffer;
312
+ this._chunkBuffer = [];
313
+ const now = Date.now();
314
+ for (const chunk of chunks) this.sql`
315
+ insert into cf_ai_chat_stream_chunks (id, stream_id, body, chunk_index, created_at)
316
+ values (${chunk.id}, ${chunk.streamId}, ${chunk.body}, ${chunk.index}, ${now})
317
+ `;
318
+ } finally {
319
+ this._isFlushingChunks = false;
320
+ }
321
+ }
322
+ /**
323
+ * Start tracking a new stream for resumable streaming.
324
+ * Creates metadata entry in SQLite and sets up tracking state.
325
+ * @param requestId - The unique ID of the chat request
326
+ * @returns The generated stream ID
327
+ * @internal Protected for testing purposes.
328
+ */
329
+ _startStream(requestId) {
330
+ this._flushChunkBuffer();
331
+ const streamId = nanoid();
332
+ this._activeStreamId = streamId;
333
+ this._activeRequestId = requestId;
334
+ this._streamChunkIndex = 0;
335
+ this.sql`
336
+ insert into cf_ai_chat_stream_metadata (id, request_id, status, created_at)
337
+ values (${streamId}, ${requestId}, 'streaming', ${Date.now()})
338
+ `;
339
+ return streamId;
340
+ }
341
+ /**
342
+ * Mark a stream as completed and flush any pending chunks.
343
+ * @param streamId - The stream to mark as completed
344
+ * @internal Protected for testing purposes.
345
+ */
346
+ _completeStream(streamId) {
347
+ this._flushChunkBuffer();
348
+ this.sql`
349
+ update cf_ai_chat_stream_metadata
350
+ set status = 'completed', completed_at = ${Date.now()}
351
+ where id = ${streamId}
352
+ `;
353
+ this._activeStreamId = null;
354
+ this._activeRequestId = null;
355
+ this._streamChunkIndex = 0;
356
+ this._maybeCleanupOldStreams();
357
+ }
358
+ /**
359
+ * Clean up old completed streams if enough time has passed since last cleanup.
360
+ * This prevents database growth while avoiding cleanup overhead on every stream completion.
361
+ */
362
+ _maybeCleanupOldStreams() {
363
+ const now = Date.now();
364
+ if (now - this._lastCleanupTime < CLEANUP_INTERVAL_MS) return;
365
+ this._lastCleanupTime = now;
366
+ const cutoff = now - CLEANUP_AGE_THRESHOLD_MS;
367
+ this.sql`
368
+ delete from cf_ai_chat_stream_chunks
369
+ where stream_id in (
370
+ select id from cf_ai_chat_stream_metadata
371
+ where status = 'completed' and completed_at < ${cutoff}
372
+ )
373
+ `;
374
+ this.sql`
375
+ delete from cf_ai_chat_stream_metadata
376
+ where status = 'completed' and completed_at < ${cutoff}
377
+ `;
36
378
  }
37
379
  _broadcastChatMessage(message, exclude) {
38
380
  this.broadcast(JSON.stringify(message), exclude);
@@ -47,68 +389,8 @@ var AIChatAgent = class extends Agent {
47
389
  }
48
390
  }).filter((msg) => msg !== null);
49
391
  }
50
- async onMessage(connection, message) {
51
- if (typeof message === "string") {
52
- let data;
53
- try {
54
- data = JSON.parse(message);
55
- } catch (_error) {
56
- return;
57
- }
58
- if (data.type === MessageType.CF_AGENT_USE_CHAT_REQUEST && data.init.method === "POST") {
59
- const { body } = data.init;
60
- const { messages } = JSON.parse(body);
61
- const transformedMessages = autoTransformMessages(messages);
62
- this._broadcastChatMessage({
63
- messages: transformedMessages,
64
- type: MessageType.CF_AGENT_CHAT_MESSAGES
65
- }, [connection.id]);
66
- await this.persistMessages(transformedMessages, [connection.id]);
67
- this.observability?.emit({
68
- displayMessage: "Chat message request",
69
- id: data.id,
70
- payload: {},
71
- timestamp: Date.now(),
72
- type: "message:request"
73
- }, this.ctx);
74
- const chatMessageId = data.id;
75
- const abortSignal = this._getAbortSignal(chatMessageId);
76
- return this._tryCatchChat(async () => {
77
- const response = await this.onChatMessage(async (_finishResult) => {
78
- this._removeAbortController(chatMessageId);
79
- this.observability?.emit({
80
- displayMessage: "Chat message response",
81
- id: data.id,
82
- payload: {},
83
- timestamp: Date.now(),
84
- type: "message:response"
85
- }, this.ctx);
86
- }, abortSignal ? { abortSignal } : void 0);
87
- if (response) await this._reply(data.id, response);
88
- else {
89
- console.warn(`[AIChatAgent] onChatMessage returned no response for chatMessageId: ${chatMessageId}`);
90
- this._broadcastChatMessage({
91
- body: "No response was generated by the agent.",
92
- done: true,
93
- id: data.id,
94
- type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
95
- }, [connection.id]);
96
- }
97
- });
98
- }
99
- if (data.type === MessageType.CF_AGENT_CHAT_CLEAR) {
100
- this._destroyAbortControllers();
101
- this.sql`delete from cf_ai_chat_agent_messages`;
102
- this.messages = [];
103
- this._broadcastChatMessage({ type: MessageType.CF_AGENT_CHAT_CLEAR }, [connection.id]);
104
- } else if (data.type === MessageType.CF_AGENT_CHAT_MESSAGES) {
105
- const transformedMessages = autoTransformMessages(data.messages);
106
- await this.persistMessages(transformedMessages, [connection.id]);
107
- } else if (data.type === MessageType.CF_AGENT_CHAT_REQUEST_CANCEL) this._cancelChatRequest(data.id);
108
- }
109
- }
110
392
  async onRequest(request) {
111
- return this._tryCatchChat(() => {
393
+ return this._tryCatchChat(async () => {
112
394
  if (new URL(request.url).pathname.endsWith("/get-messages")) {
113
395
  const messages = this._loadMessagesFromDb();
114
396
  return Response.json(messages);
@@ -126,7 +408,7 @@ var AIChatAgent = class extends Agent {
126
408
  /**
127
409
  * Handle incoming chat messages and generate a response
128
410
  * @param onFinish Callback to be called when the response is finished
129
- * @param options.signal A signal to pass to any child requests which can be used to cancel them
411
+ * @param options Options including abort signal and client-defined tools
130
412
  * @returns Response to send to the client or undefined
131
413
  */
132
414
  async onChatMessage(onFinish, options) {
@@ -144,44 +426,262 @@ var AIChatAgent = class extends Agent {
144
426
  });
145
427
  }
146
428
  async persistMessages(messages, excludeBroadcastIds = []) {
147
- for (const message of messages) this.sql`
429
+ const mergedMessages = this._mergeIncomingWithServerState(messages);
430
+ for (const message of mergedMessages) {
431
+ const sanitizedMessage = this._sanitizeMessageForPersistence(message);
432
+ const messageToSave = this._resolveMessageForToolMerge(sanitizedMessage);
433
+ this.sql`
148
434
  insert into cf_ai_chat_agent_messages (id, message)
149
- values (${message.id}, ${JSON.stringify(message)})
435
+ values (${messageToSave.id}, ${JSON.stringify(messageToSave)})
150
436
  on conflict(id) do update set message = excluded.message
151
437
  `;
438
+ }
152
439
  this.messages = autoTransformMessages(this._loadMessagesFromDb());
153
440
  this._broadcastChatMessage({
154
- messages,
441
+ messages: mergedMessages,
155
442
  type: MessageType.CF_AGENT_CHAT_MESSAGES
156
443
  }, excludeBroadcastIds);
157
444
  }
158
- async _reply(id, response) {
445
+ /**
446
+ * Merges incoming messages with existing server state.
447
+ * This preserves tool outputs that the server has (via _applyToolResult)
448
+ * but the client doesn't have yet.
449
+ *
450
+ * @param incomingMessages - Messages from the client
451
+ * @returns Messages with server's tool outputs preserved
452
+ */
453
+ _mergeIncomingWithServerState(incomingMessages) {
454
+ const serverToolOutputs = /* @__PURE__ */ new Map();
455
+ for (const msg of this.messages) {
456
+ if (msg.role !== "assistant") continue;
457
+ for (const part of msg.parts) if ("toolCallId" in part && "state" in part && part.state === "output-available" && "output" in part) serverToolOutputs.set(part.toolCallId, part.output);
458
+ }
459
+ if (serverToolOutputs.size === 0) return incomingMessages;
460
+ return incomingMessages.map((msg) => {
461
+ if (msg.role !== "assistant") return msg;
462
+ let hasChanges = false;
463
+ const updatedParts = msg.parts.map((part) => {
464
+ if ("toolCallId" in part && "state" in part && part.state === "input-available" && serverToolOutputs.has(part.toolCallId)) {
465
+ hasChanges = true;
466
+ return {
467
+ ...part,
468
+ state: "output-available",
469
+ output: serverToolOutputs.get(part.toolCallId)
470
+ };
471
+ }
472
+ return part;
473
+ });
474
+ return hasChanges ? {
475
+ ...msg,
476
+ parts: updatedParts
477
+ } : msg;
478
+ });
479
+ }
480
+ /**
481
+ * Resolves a message for persistence, handling tool result merging.
482
+ * If the message contains tool parts with output-available state, checks if there's
483
+ * an existing message with the same toolCallId that should be updated instead of
484
+ * creating a duplicate. This prevents the "Duplicate item found" error from OpenAI
485
+ * when client-side tool results arrive in a new request.
486
+ *
487
+ * @param message - The message to potentially merge
488
+ * @returns The message with the correct ID (either original or merged)
489
+ */
490
+ _resolveMessageForToolMerge(message) {
491
+ if (message.role !== "assistant") return message;
492
+ for (const part of message.parts) if ("toolCallId" in part && "state" in part && part.state === "output-available") {
493
+ const toolCallId = part.toolCallId;
494
+ const existingMessage = this._findMessageByToolCallId(toolCallId);
495
+ if (existingMessage && existingMessage.id !== message.id) return {
496
+ ...message,
497
+ id: existingMessage.id
498
+ };
499
+ }
500
+ return message;
501
+ }
502
+ /**
503
+ * Finds an existing assistant message that contains a tool part with the given toolCallId.
504
+ * Used to detect when a tool result should update an existing message rather than
505
+ * creating a new one.
506
+ *
507
+ * @param toolCallId - The tool call ID to search for
508
+ * @returns The existing message if found, undefined otherwise
509
+ */
510
+ _findMessageByToolCallId(toolCallId) {
511
+ for (const msg of this.messages) {
512
+ if (msg.role !== "assistant") continue;
513
+ for (const part of msg.parts) if ("toolCallId" in part && part.toolCallId === toolCallId) return msg;
514
+ }
515
+ }
516
+ /**
517
+ * Sanitizes a message for persistence by removing ephemeral provider-specific
518
+ * data that should not be stored or sent back in subsequent requests.
519
+ *
520
+ * This handles two issues with the OpenAI Responses API:
521
+ *
522
+ * 1. **Duplicate item IDs**: The AI SDK's @ai-sdk/openai provider (v2.0.x+)
523
+ * defaults to using OpenAI's Responses API which assigns unique itemIds
524
+ * to each message part. When these IDs are persisted and sent back,
525
+ * OpenAI rejects them as duplicates.
526
+ *
527
+ * 2. **Empty reasoning parts**: OpenAI may return reasoning parts with empty
528
+ * text and encrypted content. These cause "Non-OpenAI reasoning parts are
529
+ * not supported" warnings when sent back via convertToModelMessages().
530
+ *
531
+ * @param message - The message to sanitize
532
+ * @returns A new message with ephemeral provider data removed
533
+ */
534
+ _sanitizeMessageForPersistence(message) {
535
+ const sanitizedParts = message.parts.filter((part) => {
536
+ if (part.type === "reasoning") {
537
+ const reasoningPart = part;
538
+ if (!reasoningPart.text || reasoningPart.text.trim() === "") return false;
539
+ }
540
+ return true;
541
+ }).map((part) => {
542
+ let sanitizedPart = part;
543
+ if ("providerMetadata" in sanitizedPart && sanitizedPart.providerMetadata && typeof sanitizedPart.providerMetadata === "object" && "openai" in sanitizedPart.providerMetadata) sanitizedPart = this._stripOpenAIMetadata(sanitizedPart, "providerMetadata");
544
+ if ("callProviderMetadata" in sanitizedPart && sanitizedPart.callProviderMetadata && typeof sanitizedPart.callProviderMetadata === "object" && "openai" in sanitizedPart.callProviderMetadata) sanitizedPart = this._stripOpenAIMetadata(sanitizedPart, "callProviderMetadata");
545
+ return sanitizedPart;
546
+ });
547
+ return {
548
+ ...message,
549
+ parts: sanitizedParts
550
+ };
551
+ }
552
+ /**
553
+ * Helper to strip OpenAI-specific ephemeral fields from a metadata object.
554
+ * Removes itemId and reasoningEncryptedContent while preserving other fields.
555
+ */
556
+ _stripOpenAIMetadata(part, metadataKey) {
557
+ const metadata = part[metadataKey];
558
+ if (!metadata?.openai) return part;
559
+ const { itemId: _itemId, reasoningEncryptedContent: _rec, ...restOpenai } = metadata.openai;
560
+ const hasOtherOpenaiFields = Object.keys(restOpenai).length > 0;
561
+ const { openai: _openai, ...restMetadata } = metadata;
562
+ let newMetadata;
563
+ if (hasOtherOpenaiFields) newMetadata = {
564
+ ...restMetadata,
565
+ openai: restOpenai
566
+ };
567
+ else if (Object.keys(restMetadata).length > 0) newMetadata = restMetadata;
568
+ const { [metadataKey]: _oldMeta, ...restPart } = part;
569
+ if (newMetadata) return {
570
+ ...restPart,
571
+ [metadataKey]: newMetadata
572
+ };
573
+ return restPart;
574
+ }
575
+ /**
576
+ * Applies a tool result to an existing assistant message.
577
+ * This is used when the client sends CF_AGENT_TOOL_RESULT for client-side tools.
578
+ * The server is the source of truth, so we update the message here and broadcast
579
+ * the update to all clients.
580
+ *
581
+ * @param toolCallId - The tool call ID this result is for
582
+ * @param toolName - The name of the tool
583
+ * @param output - The output from the tool execution
584
+ * @returns true if the result was applied, false if the message was not found
585
+ */
586
+ async _applyToolResult(toolCallId, _toolName, output) {
587
+ let message;
588
+ if (this._streamingMessage) {
589
+ for (const part of this._streamingMessage.parts) if ("toolCallId" in part && part.toolCallId === toolCallId) {
590
+ message = this._streamingMessage;
591
+ break;
592
+ }
593
+ }
594
+ if (!message) for (let attempt = 0; attempt < 10; attempt++) {
595
+ message = this._findMessageByToolCallId(toolCallId);
596
+ if (message) break;
597
+ await new Promise((resolve) => setTimeout(resolve, 100));
598
+ }
599
+ if (!message) {
600
+ console.warn(`[AIChatAgent] _applyToolResult: Could not find message with toolCallId ${toolCallId} after retries`);
601
+ return false;
602
+ }
603
+ const isStreamingMessage = message === this._streamingMessage;
604
+ let updated = false;
605
+ if (isStreamingMessage) {
606
+ for (const part of message.parts) if ("toolCallId" in part && part.toolCallId === toolCallId && "state" in part && part.state === "input-available") {
607
+ part.state = "output-available";
608
+ part.output = output;
609
+ updated = true;
610
+ break;
611
+ }
612
+ } else {
613
+ const updatedParts = message.parts.map((part) => {
614
+ if ("toolCallId" in part && part.toolCallId === toolCallId && "state" in part && part.state === "input-available") {
615
+ updated = true;
616
+ return {
617
+ ...part,
618
+ state: "output-available",
619
+ output
620
+ };
621
+ }
622
+ return part;
623
+ });
624
+ if (updated) {
625
+ const updatedMessage = this._sanitizeMessageForPersistence({
626
+ ...message,
627
+ parts: updatedParts
628
+ });
629
+ this.sql`
630
+ update cf_ai_chat_agent_messages
631
+ set message = ${JSON.stringify(updatedMessage)}
632
+ where id = ${message.id}
633
+ `;
634
+ this.messages = autoTransformMessages(this._loadMessagesFromDb());
635
+ }
636
+ }
637
+ if (!updated) {
638
+ console.warn(`[AIChatAgent] _applyToolResult: Tool part with toolCallId ${toolCallId} not in input-available state`);
639
+ return false;
640
+ }
641
+ if (!isStreamingMessage) {
642
+ const broadcastMessage = this._findMessageByToolCallId(toolCallId);
643
+ if (broadcastMessage) this._broadcastChatMessage({
644
+ type: MessageType.CF_AGENT_MESSAGE_UPDATED,
645
+ message: broadcastMessage
646
+ });
647
+ }
648
+ return true;
649
+ }
650
+ async _reply(id, response, excludeBroadcastIds = [], options = {}) {
651
+ const { continuation = false } = options;
159
652
  return this._tryCatchChat(async () => {
160
653
  if (!response.body) {
161
654
  this._broadcastChatMessage({
162
655
  body: "",
163
656
  done: true,
164
657
  id,
165
- type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
658
+ type: MessageType.CF_AGENT_USE_CHAT_RESPONSE,
659
+ ...continuation && { continuation: true }
166
660
  });
167
661
  return;
168
662
  }
663
+ const streamId = this._startStream(id);
664
+ const { getToolName: getToolName$1, isToolUIPart: isToolUIPart$1, parsePartialJson } = await import("ai");
169
665
  const reader = response.body.getReader();
170
666
  const message = {
171
667
  id: `assistant_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`,
172
668
  role: "assistant",
173
669
  parts: []
174
670
  };
671
+ this._streamingMessage = message;
672
+ this._streamCompletionPromise = new Promise((resolve) => {
673
+ this._streamCompletionResolve = resolve;
674
+ });
175
675
  let activeTextParts = {};
176
676
  let activeReasoningParts = {};
177
677
  const partialToolCalls = {};
178
- function updateDynamicToolPart(options) {
179
- const part = message.parts.find((part$1) => part$1.type === "dynamic-tool" && part$1.toolCallId === options.toolCallId);
180
- const anyOptions = options;
678
+ function updateDynamicToolPart(options$1) {
679
+ const part = message.parts.find((part$1) => part$1.type === "dynamic-tool" && part$1.toolCallId === options$1.toolCallId);
680
+ const anyOptions = options$1;
181
681
  const anyPart = part;
182
682
  if (part != null) {
183
- part.state = options.state;
184
- anyPart.toolName = options.toolName;
683
+ part.state = options$1.state;
684
+ anyPart.toolName = options$1.toolName;
185
685
  anyPart.input = anyOptions.input;
186
686
  anyPart.output = anyOptions.output;
187
687
  anyPart.errorText = anyOptions.errorText;
@@ -190,9 +690,9 @@ var AIChatAgent = class extends Agent {
190
690
  if (anyOptions.providerMetadata != null && part.state === "input-available") part.callProviderMetadata = anyOptions.providerMetadata;
191
691
  } else message.parts.push({
192
692
  type: "dynamic-tool",
193
- toolName: options.toolName,
194
- toolCallId: options.toolCallId,
195
- state: options.state,
693
+ toolName: options$1.toolName,
694
+ toolCallId: options$1.toolCallId,
695
+ state: options$1.state,
196
696
  input: anyOptions.input,
197
697
  output: anyOptions.output,
198
698
  errorText: anyOptions.errorText,
@@ -200,13 +700,12 @@ var AIChatAgent = class extends Agent {
200
700
  ...anyOptions.providerMetadata != null ? { callProviderMetadata: anyOptions.providerMetadata } : {}
201
701
  });
202
702
  }
203
- function updateToolPart(options) {
204
- const { isToolUIPart } = getAISDK();
205
- const part = message.parts.find((part$1) => isToolUIPart(part$1) && part$1.toolCallId === options.toolCallId);
206
- const anyOptions = options;
703
+ function updateToolPart(options$1) {
704
+ const part = message.parts.find((part$1) => isToolUIPart$1(part$1) && part$1.toolCallId === options$1.toolCallId);
705
+ const anyOptions = options$1;
207
706
  const anyPart = part;
208
707
  if (part != null) {
209
- part.state = options.state;
708
+ part.state = options$1.state;
210
709
  anyPart.input = anyOptions.input;
211
710
  anyPart.output = anyOptions.output;
212
711
  anyPart.errorText = anyOptions.errorText;
@@ -215,9 +714,9 @@ var AIChatAgent = class extends Agent {
215
714
  anyPart.providerExecuted = anyOptions.providerExecuted ?? part.providerExecuted;
216
715
  if (anyOptions.providerMetadata != null && part.state === "input-available") part.callProviderMetadata = anyOptions.providerMetadata;
217
716
  } else message.parts.push({
218
- type: `tool-${options.toolName}`,
219
- toolCallId: options.toolCallId,
220
- state: options.state,
717
+ type: `tool-${options$1.toolName}`,
718
+ toolCallId: options$1.toolCallId,
719
+ state: options$1.state,
221
720
  input: anyOptions.input,
222
721
  output: anyOptions.output,
223
722
  rawInput: anyOptions.rawInput,
@@ -233,15 +732,19 @@ var AIChatAgent = class extends Agent {
233
732
  ...metadata
234
733
  } : metadata;
235
734
  }
735
+ let streamCompleted = false;
236
736
  try {
237
737
  while (true) {
238
738
  const { done, value } = await reader.read();
239
739
  if (done) {
740
+ this._completeStream(streamId);
741
+ streamCompleted = true;
240
742
  this._broadcastChatMessage({
241
743
  body: "",
242
744
  done: true,
243
745
  id,
244
- type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
746
+ type: MessageType.CF_AGENT_USE_CHAT_RESPONSE,
747
+ ...continuation && { continuation: true }
245
748
  });
246
749
  break;
247
750
  }
@@ -326,8 +829,7 @@ var AIChatAgent = class extends Agent {
326
829
  });
327
830
  break;
328
831
  case "tool-input-start": {
329
- const { isToolUIPart } = getAISDK();
330
- const toolInvocations = message.parts.filter(isToolUIPart);
832
+ const toolInvocations = message.parts.filter(isToolUIPart$1);
331
833
  partialToolCalls[data.toolCallId] = {
332
834
  text: "",
333
835
  toolName: data.toolName,
@@ -351,7 +853,6 @@ var AIChatAgent = class extends Agent {
351
853
  case "tool-input-delta": {
352
854
  const partialToolCall = partialToolCalls[data.toolCallId];
353
855
  partialToolCall.text += data.inputTextDelta;
354
- const { parsePartialJson } = getAISDK();
355
856
  const partialArgs = (await parsePartialJson(partialToolCall.text)).value;
356
857
  if (partialToolCall.dynamic) updateDynamicToolPart({
357
858
  toolCallId: data.toolCallId,
@@ -417,12 +918,11 @@ var AIChatAgent = class extends Agent {
417
918
  preliminary: data.preliminary
418
919
  });
419
920
  } else {
420
- const { isToolUIPart, getToolName } = getAISDK();
421
- const toolInvocation = message.parts.filter(isToolUIPart).find((invocation) => invocation.toolCallId === data.toolCallId);
921
+ const toolInvocation = message.parts.filter(isToolUIPart$1).find((invocation) => invocation.toolCallId === data.toolCallId);
422
922
  if (!toolInvocation) throw new Error("Tool invocation not found");
423
923
  updateToolPart({
424
924
  toolCallId: data.toolCallId,
425
- toolName: getToolName(toolInvocation),
925
+ toolName: getToolName$1(toolInvocation),
426
926
  state: "output-available",
427
927
  input: toolInvocation.input,
428
928
  output: data.output,
@@ -443,12 +943,11 @@ var AIChatAgent = class extends Agent {
443
943
  errorText: data.errorText
444
944
  });
445
945
  } else {
446
- const { isToolUIPart, getToolName } = getAISDK();
447
- const toolInvocation = message.parts.filter(isToolUIPart).find((invocation) => invocation.toolCallId === data.toolCallId);
946
+ const toolInvocation = message.parts.filter(isToolUIPart$1).find((invocation) => invocation.toolCallId === data.toolCallId);
448
947
  if (!toolInvocation) throw new Error("Tool invocation not found");
449
948
  updateToolPart({
450
949
  toolCallId: data.toolCallId,
451
- toolName: getToolName(toolInvocation),
950
+ toolName: getToolName$1(toolInvocation),
452
951
  state: "output-error",
453
952
  input: toolInvocation.input,
454
953
  rawInput: "rawInput" in toolInvocation ? toolInvocation.rawInput : void 0,
@@ -483,36 +982,102 @@ var AIChatAgent = class extends Agent {
483
982
  });
484
983
  break;
485
984
  }
985
+ let eventToSend = data;
986
+ if (data.type === "finish" && "finishReason" in data) {
987
+ const { finishReason, ...rest } = data;
988
+ eventToSend = {
989
+ ...rest,
990
+ type: "finish",
991
+ messageMetadata: { finishReason }
992
+ };
993
+ }
994
+ const chunkBody = JSON.stringify(eventToSend);
995
+ this._storeStreamChunk(streamId, chunkBody);
486
996
  this._broadcastChatMessage({
487
- body: JSON.stringify(data),
997
+ body: chunkBody,
488
998
  done: false,
489
999
  id,
490
- type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
1000
+ type: MessageType.CF_AGENT_USE_CHAT_RESPONSE,
1001
+ ...continuation && { continuation: true }
491
1002
  });
492
- } catch (_e) {}
1003
+ } catch (_error) {}
493
1004
  } else if (chunk.length > 0) {
494
1005
  message.parts.push({
495
1006
  type: "text",
496
1007
  text: chunk
497
1008
  });
1009
+ const chunkBody = JSON.stringify({
1010
+ type: "text-delta",
1011
+ delta: chunk
1012
+ });
1013
+ this._storeStreamChunk(streamId, chunkBody);
498
1014
  this._broadcastChatMessage({
499
- body: JSON.stringify({
500
- type: "text-delta",
501
- delta: chunk
502
- }),
1015
+ body: chunkBody,
503
1016
  done: false,
504
1017
  id,
505
- type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
1018
+ type: MessageType.CF_AGENT_USE_CHAT_RESPONSE,
1019
+ ...continuation && { continuation: true }
506
1020
  });
507
1021
  }
508
1022
  }
1023
+ } catch (error) {
1024
+ if (!streamCompleted) {
1025
+ this._markStreamError(streamId);
1026
+ this._broadcastChatMessage({
1027
+ body: error instanceof Error ? error.message : "Stream error",
1028
+ done: true,
1029
+ error: true,
1030
+ id,
1031
+ type: MessageType.CF_AGENT_USE_CHAT_RESPONSE,
1032
+ ...continuation && { continuation: true }
1033
+ });
1034
+ }
1035
+ throw error;
509
1036
  } finally {
510
1037
  reader.releaseLock();
511
1038
  }
512
- if (message.parts.length > 0) await this.persistMessages([...this.messages, message]);
1039
+ if (message.parts.length > 0) if (continuation) {
1040
+ let lastAssistantIdx = -1;
1041
+ for (let i = this.messages.length - 1; i >= 0; i--) if (this.messages[i].role === "assistant") {
1042
+ lastAssistantIdx = i;
1043
+ break;
1044
+ }
1045
+ if (lastAssistantIdx >= 0) {
1046
+ const lastAssistant = this.messages[lastAssistantIdx];
1047
+ const mergedMessage = {
1048
+ ...lastAssistant,
1049
+ parts: [...lastAssistant.parts, ...message.parts]
1050
+ };
1051
+ const updatedMessages = [...this.messages];
1052
+ updatedMessages[lastAssistantIdx] = mergedMessage;
1053
+ await this.persistMessages(updatedMessages, excludeBroadcastIds);
1054
+ } else await this.persistMessages([...this.messages, message], excludeBroadcastIds);
1055
+ } else await this.persistMessages([...this.messages, message], excludeBroadcastIds);
1056
+ this._streamingMessage = null;
1057
+ if (this._streamCompletionResolve) {
1058
+ this._streamCompletionResolve();
1059
+ this._streamCompletionResolve = null;
1060
+ this._streamCompletionPromise = null;
1061
+ }
513
1062
  });
514
1063
  }
515
1064
  /**
1065
+ * Mark a stream as errored and clean up state.
1066
+ * @param streamId - The stream to mark as errored
1067
+ * @internal Protected for testing purposes.
1068
+ */
1069
+ _markStreamError(streamId) {
1070
+ this._flushChunkBuffer();
1071
+ this.sql`
1072
+ update cf_ai_chat_stream_metadata
1073
+ set status = 'error', completed_at = ${Date.now()}
1074
+ where id = ${streamId}
1075
+ `;
1076
+ this._activeStreamId = null;
1077
+ this._activeRequestId = null;
1078
+ this._streamChunkIndex = 0;
1079
+ }
1080
+ /**
516
1081
  * For the given message id, look up its associated AbortController
517
1082
  * If the AbortController does not exist, create and store one in memory
518
1083
  *
@@ -543,14 +1108,19 @@ var AIChatAgent = class extends Agent {
543
1108
  this._chatMessageAbortControllers.clear();
544
1109
  }
545
1110
  /**
546
- * When the DO is destroyed, cancel all pending requests
1111
+ * When the DO is destroyed, cancel all pending requests and clean up resources
547
1112
  */
548
1113
  async destroy() {
549
1114
  this._destroyAbortControllers();
1115
+ this._flushChunkBuffer();
1116
+ this.sql`drop table if exists cf_ai_chat_stream_chunks`;
1117
+ this.sql`drop table if exists cf_ai_chat_stream_metadata`;
1118
+ this._activeStreamId = null;
1119
+ this._activeRequestId = null;
550
1120
  await super.destroy();
551
1121
  }
552
1122
  };
553
1123
 
554
1124
  //#endregion
555
- export { AIChatAgent };
1125
+ export { AIChatAgent, createToolsFromClientSchemas };
556
1126
  //# sourceMappingURL=ai-chat-agent.js.map