agents 0.0.0-19a3a25 → 0.0.0-1a3d226
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +126 -3
- package/dist/ai-chat-agent.d.ts +229 -22
- package/dist/ai-chat-agent.js +692 -106
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-chat-v5-migration.js +154 -2
- package/dist/ai-chat-v5-migration.js.map +1 -0
- package/dist/ai-react.d.ts +141 -16
- package/dist/ai-react.js +381 -68
- package/dist/ai-react.js.map +1 -1
- package/dist/{ai-types-D5YoPrBZ.d.ts → ai-types-0OnT3FHg.d.ts} +40 -8
- package/dist/{ai-types-B3aQaFv3.js → ai-types-DEtF_8Km.js} +9 -1
- package/dist/ai-types-DEtF_8Km.js.map +1 -0
- package/dist/ai-types.d.ts +1 -1
- package/dist/ai-types.js +1 -1
- package/dist/cli/index.d.ts +1 -0
- package/dist/{cli.js → cli/index.js} +7 -6
- package/dist/cli/index.js.map +1 -0
- package/dist/client-BINtT7y-.d.ts +834 -0
- package/dist/{client-CbWe9FBd.d.ts → client-CdM5I962.d.ts} +2 -2
- package/dist/{client-BfiZ3HQd.js → client-DjTPRM8-.js} +2 -2
- package/dist/{client-BfiZ3HQd.js.map → client-DjTPRM8-.js.map} +1 -1
- package/dist/client-QZa2Rq0l.js +1105 -0
- package/dist/client-QZa2Rq0l.js.map +1 -0
- package/dist/client.d.ts +1 -2
- package/dist/client.js +1 -2
- package/dist/codemode/ai.js +6 -6
- package/dist/codemode/ai.js.map +1 -1
- package/dist/context-BkKbAa1R.js +8 -0
- package/dist/context-BkKbAa1R.js.map +1 -0
- package/dist/context-DcbQ8o7k.d.ts +24 -0
- package/dist/context.d.ts +6 -0
- package/dist/context.js +3 -0
- package/dist/{do-oauth-client-provider-DGc5pP0l.d.ts → do-oauth-client-provider--To1Tsjj.d.ts} +20 -5
- package/dist/{do-oauth-client-provider-CswoD5Lu.js → do-oauth-client-provider-B1fVIshX.js} +70 -8
- package/dist/do-oauth-client-provider-B1fVIshX.js.map +1 -0
- package/dist/{index-CuqiHyhT.d.ts → index-CfZ2mfMI.d.ts} +80 -64
- package/dist/{index-DhJCaDWd.d.ts → index-DLuxm_9W.d.ts} +2 -2
- package/dist/index.d.ts +31 -34
- package/dist/index.js +5 -5
- package/dist/mcp/client.d.ts +2 -4
- package/dist/mcp/client.js +2 -1
- package/dist/mcp/do-oauth-client-provider.d.ts +1 -1
- package/dist/mcp/do-oauth-client-provider.js +1 -1
- package/dist/mcp/index.d.ts +23 -16
- package/dist/mcp/index.js +71 -69
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/x402.js +10 -6
- package/dist/mcp/x402.js.map +1 -1
- package/dist/{mcp-Dw5vDrY8.d.ts → mcp-CPSfGUgd.d.ts} +1 -1
- package/dist/observability/index.d.ts +1 -2
- package/dist/observability/index.js +5 -5
- package/dist/react.d.ts +134 -10
- package/dist/react.js +57 -57
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +18 -72
- package/dist/{serializable-CymX8ovI.d.ts → serializable-Crsj26mx.d.ts} +1 -1
- package/dist/serializable.d.ts +1 -1
- package/dist/{src-CwIW7tU2.js → src-BZDh910Z.js} +104 -139
- package/dist/src-BZDh910Z.js.map +1 -0
- package/package.json +31 -16
- package/dist/ai-chat-v5-migration-DBHGW4Hv.js +0 -155
- package/dist/ai-chat-v5-migration-DBHGW4Hv.js.map +0 -1
- package/dist/ai-types-B3aQaFv3.js.map +0 -1
- package/dist/cli.d.ts +0 -8
- package/dist/cli.js.map +0 -1
- package/dist/client-BnO9jNtu.d.ts +0 -5283
- package/dist/client-DZhjV_XA.js +0 -719
- package/dist/client-DZhjV_XA.js.map +0 -1
- package/dist/do-oauth-client-provider-CswoD5Lu.js.map +0 -1
- package/dist/react-Cx9uE2Jd.d.ts +0 -113
- package/dist/src-CwIW7tU2.js.map +0 -1
package/dist/ai-chat-agent.js
CHANGED
|
@@ -1,11 +1,45 @@
|
|
|
1
|
-
import { t as
|
|
2
|
-
import "./
|
|
3
|
-
import "./client-
|
|
4
|
-
import "./
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
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 { autoTransformMessages } from "./ai-chat-v5-migration.js";
|
|
8
|
+
import { jsonSchema, tool } from "ai";
|
|
9
|
+
import { nanoid } from "nanoid";
|
|
7
10
|
|
|
8
11
|
//#region src/ai-chat-agent.ts
|
|
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);
|
|
27
|
+
}
|
|
28
|
+
return Object.fromEntries(clientTools.map((t) => [t.name, tool({
|
|
29
|
+
description: t.description ?? "",
|
|
30
|
+
inputSchema: jsonSchema(t.parameters ?? { type: "object" })
|
|
31
|
+
})]));
|
|
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;
|
|
9
43
|
const decoder = new TextDecoder();
|
|
10
44
|
/**
|
|
11
45
|
* Extension of Agent with built-in chat capabilities
|
|
@@ -14,13 +48,333 @@ const decoder = new TextDecoder();
|
|
|
14
48
|
var AIChatAgent = class extends Agent {
|
|
15
49
|
constructor(ctx, env) {
|
|
16
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;
|
|
17
60
|
this.sql`create table if not exists cf_ai_chat_agent_messages (
|
|
18
61
|
id text primary key,
|
|
19
62
|
message text not null,
|
|
20
63
|
created_at datetime default current_timestamp
|
|
21
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)`;
|
|
22
81
|
this.messages = autoTransformMessages(this._loadMessagesFromDb());
|
|
23
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
|
+
`;
|
|
24
378
|
}
|
|
25
379
|
_broadcastChatMessage(message, exclude) {
|
|
26
380
|
this.broadcast(JSON.stringify(message), exclude);
|
|
@@ -35,68 +389,8 @@ var AIChatAgent = class extends Agent {
|
|
|
35
389
|
}
|
|
36
390
|
}).filter((msg) => msg !== null);
|
|
37
391
|
}
|
|
38
|
-
async onMessage(connection, message) {
|
|
39
|
-
if (typeof message === "string") {
|
|
40
|
-
let data;
|
|
41
|
-
try {
|
|
42
|
-
data = JSON.parse(message);
|
|
43
|
-
} catch (_error) {
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
if (data.type === MessageType.CF_AGENT_USE_CHAT_REQUEST && data.init.method === "POST") {
|
|
47
|
-
const { body } = data.init;
|
|
48
|
-
const { messages } = JSON.parse(body);
|
|
49
|
-
const transformedMessages = autoTransformMessages(messages);
|
|
50
|
-
this._broadcastChatMessage({
|
|
51
|
-
messages: transformedMessages,
|
|
52
|
-
type: MessageType.CF_AGENT_CHAT_MESSAGES
|
|
53
|
-
}, [connection.id]);
|
|
54
|
-
await this.persistMessages(transformedMessages, [connection.id]);
|
|
55
|
-
this.observability?.emit({
|
|
56
|
-
displayMessage: "Chat message request",
|
|
57
|
-
id: data.id,
|
|
58
|
-
payload: {},
|
|
59
|
-
timestamp: Date.now(),
|
|
60
|
-
type: "message:request"
|
|
61
|
-
}, this.ctx);
|
|
62
|
-
const chatMessageId = data.id;
|
|
63
|
-
const abortSignal = this._getAbortSignal(chatMessageId);
|
|
64
|
-
return this._tryCatchChat(async () => {
|
|
65
|
-
const response = await this.onChatMessage(async (_finishResult) => {
|
|
66
|
-
this._removeAbortController(chatMessageId);
|
|
67
|
-
this.observability?.emit({
|
|
68
|
-
displayMessage: "Chat message response",
|
|
69
|
-
id: data.id,
|
|
70
|
-
payload: {},
|
|
71
|
-
timestamp: Date.now(),
|
|
72
|
-
type: "message:response"
|
|
73
|
-
}, this.ctx);
|
|
74
|
-
}, abortSignal ? { abortSignal } : void 0);
|
|
75
|
-
if (response) await this._reply(data.id, response);
|
|
76
|
-
else {
|
|
77
|
-
console.warn(`[AIChatAgent] onChatMessage returned no response for chatMessageId: ${chatMessageId}`);
|
|
78
|
-
this._broadcastChatMessage({
|
|
79
|
-
body: "No response was generated by the agent.",
|
|
80
|
-
done: true,
|
|
81
|
-
id: data.id,
|
|
82
|
-
type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
|
|
83
|
-
}, [connection.id]);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
if (data.type === MessageType.CF_AGENT_CHAT_CLEAR) {
|
|
88
|
-
this._destroyAbortControllers();
|
|
89
|
-
this.sql`delete from cf_ai_chat_agent_messages`;
|
|
90
|
-
this.messages = [];
|
|
91
|
-
this._broadcastChatMessage({ type: MessageType.CF_AGENT_CHAT_CLEAR }, [connection.id]);
|
|
92
|
-
} else if (data.type === MessageType.CF_AGENT_CHAT_MESSAGES) {
|
|
93
|
-
const transformedMessages = autoTransformMessages(data.messages);
|
|
94
|
-
await this.persistMessages(transformedMessages, [connection.id]);
|
|
95
|
-
} else if (data.type === MessageType.CF_AGENT_CHAT_REQUEST_CANCEL) this._cancelChatRequest(data.id);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
392
|
async onRequest(request) {
|
|
99
|
-
return this._tryCatchChat(() => {
|
|
393
|
+
return this._tryCatchChat(async () => {
|
|
100
394
|
if (new URL(request.url).pathname.endsWith("/get-messages")) {
|
|
101
395
|
const messages = this._loadMessagesFromDb();
|
|
102
396
|
return Response.json(messages);
|
|
@@ -114,7 +408,7 @@ var AIChatAgent = class extends Agent {
|
|
|
114
408
|
/**
|
|
115
409
|
* Handle incoming chat messages and generate a response
|
|
116
410
|
* @param onFinish Callback to be called when the response is finished
|
|
117
|
-
* @param options
|
|
411
|
+
* @param options Options including abort signal and client-defined tools
|
|
118
412
|
* @returns Response to send to the client or undefined
|
|
119
413
|
*/
|
|
120
414
|
async onChatMessage(onFinish, options) {
|
|
@@ -132,45 +426,262 @@ var AIChatAgent = class extends Agent {
|
|
|
132
426
|
});
|
|
133
427
|
}
|
|
134
428
|
async persistMessages(messages, excludeBroadcastIds = []) {
|
|
135
|
-
|
|
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`
|
|
136
434
|
insert into cf_ai_chat_agent_messages (id, message)
|
|
137
|
-
values (${
|
|
435
|
+
values (${messageToSave.id}, ${JSON.stringify(messageToSave)})
|
|
138
436
|
on conflict(id) do update set message = excluded.message
|
|
139
437
|
`;
|
|
438
|
+
}
|
|
140
439
|
this.messages = autoTransformMessages(this._loadMessagesFromDb());
|
|
141
440
|
this._broadcastChatMessage({
|
|
142
|
-
messages,
|
|
441
|
+
messages: mergedMessages,
|
|
143
442
|
type: MessageType.CF_AGENT_CHAT_MESSAGES
|
|
144
443
|
}, excludeBroadcastIds);
|
|
145
444
|
}
|
|
146
|
-
|
|
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;
|
|
147
652
|
return this._tryCatchChat(async () => {
|
|
148
653
|
if (!response.body) {
|
|
149
654
|
this._broadcastChatMessage({
|
|
150
655
|
body: "",
|
|
151
656
|
done: true,
|
|
152
657
|
id,
|
|
153
|
-
type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
|
|
658
|
+
type: MessageType.CF_AGENT_USE_CHAT_RESPONSE,
|
|
659
|
+
...continuation && { continuation: true }
|
|
154
660
|
});
|
|
155
661
|
return;
|
|
156
662
|
}
|
|
157
|
-
const
|
|
663
|
+
const streamId = this._startStream(id);
|
|
664
|
+
const { getToolName: getToolName$1, isToolUIPart: isToolUIPart$1, parsePartialJson } = await import("ai");
|
|
158
665
|
const reader = response.body.getReader();
|
|
159
666
|
const message = {
|
|
160
667
|
id: `assistant_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`,
|
|
161
668
|
role: "assistant",
|
|
162
669
|
parts: []
|
|
163
670
|
};
|
|
671
|
+
this._streamingMessage = message;
|
|
672
|
+
this._streamCompletionPromise = new Promise((resolve) => {
|
|
673
|
+
this._streamCompletionResolve = resolve;
|
|
674
|
+
});
|
|
164
675
|
let activeTextParts = {};
|
|
165
676
|
let activeReasoningParts = {};
|
|
166
677
|
const partialToolCalls = {};
|
|
167
|
-
function updateDynamicToolPart(options) {
|
|
168
|
-
const part = message.parts.find((part$1) => part$1.type === "dynamic-tool" && part$1.toolCallId === options.toolCallId);
|
|
169
|
-
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;
|
|
170
681
|
const anyPart = part;
|
|
171
682
|
if (part != null) {
|
|
172
|
-
part.state = options.state;
|
|
173
|
-
anyPart.toolName = options.toolName;
|
|
683
|
+
part.state = options$1.state;
|
|
684
|
+
anyPart.toolName = options$1.toolName;
|
|
174
685
|
anyPart.input = anyOptions.input;
|
|
175
686
|
anyPart.output = anyOptions.output;
|
|
176
687
|
anyPart.errorText = anyOptions.errorText;
|
|
@@ -179,9 +690,9 @@ var AIChatAgent = class extends Agent {
|
|
|
179
690
|
if (anyOptions.providerMetadata != null && part.state === "input-available") part.callProviderMetadata = anyOptions.providerMetadata;
|
|
180
691
|
} else message.parts.push({
|
|
181
692
|
type: "dynamic-tool",
|
|
182
|
-
toolName: options.toolName,
|
|
183
|
-
toolCallId: options.toolCallId,
|
|
184
|
-
state: options.state,
|
|
693
|
+
toolName: options$1.toolName,
|
|
694
|
+
toolCallId: options$1.toolCallId,
|
|
695
|
+
state: options$1.state,
|
|
185
696
|
input: anyOptions.input,
|
|
186
697
|
output: anyOptions.output,
|
|
187
698
|
errorText: anyOptions.errorText,
|
|
@@ -189,12 +700,12 @@ var AIChatAgent = class extends Agent {
|
|
|
189
700
|
...anyOptions.providerMetadata != null ? { callProviderMetadata: anyOptions.providerMetadata } : {}
|
|
190
701
|
});
|
|
191
702
|
}
|
|
192
|
-
function updateToolPart(options) {
|
|
193
|
-
const part = message.parts.find((part$1) => isToolUIPart(part$1) && part$1.toolCallId === options.toolCallId);
|
|
194
|
-
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;
|
|
195
706
|
const anyPart = part;
|
|
196
707
|
if (part != null) {
|
|
197
|
-
part.state = options.state;
|
|
708
|
+
part.state = options$1.state;
|
|
198
709
|
anyPart.input = anyOptions.input;
|
|
199
710
|
anyPart.output = anyOptions.output;
|
|
200
711
|
anyPart.errorText = anyOptions.errorText;
|
|
@@ -203,9 +714,9 @@ var AIChatAgent = class extends Agent {
|
|
|
203
714
|
anyPart.providerExecuted = anyOptions.providerExecuted ?? part.providerExecuted;
|
|
204
715
|
if (anyOptions.providerMetadata != null && part.state === "input-available") part.callProviderMetadata = anyOptions.providerMetadata;
|
|
205
716
|
} else message.parts.push({
|
|
206
|
-
type: `tool-${options.toolName}`,
|
|
207
|
-
toolCallId: options.toolCallId,
|
|
208
|
-
state: options.state,
|
|
717
|
+
type: `tool-${options$1.toolName}`,
|
|
718
|
+
toolCallId: options$1.toolCallId,
|
|
719
|
+
state: options$1.state,
|
|
209
720
|
input: anyOptions.input,
|
|
210
721
|
output: anyOptions.output,
|
|
211
722
|
rawInput: anyOptions.rawInput,
|
|
@@ -221,15 +732,19 @@ var AIChatAgent = class extends Agent {
|
|
|
221
732
|
...metadata
|
|
222
733
|
} : metadata;
|
|
223
734
|
}
|
|
735
|
+
let streamCompleted = false;
|
|
224
736
|
try {
|
|
225
737
|
while (true) {
|
|
226
738
|
const { done, value } = await reader.read();
|
|
227
739
|
if (done) {
|
|
740
|
+
this._completeStream(streamId);
|
|
741
|
+
streamCompleted = true;
|
|
228
742
|
this._broadcastChatMessage({
|
|
229
743
|
body: "",
|
|
230
744
|
done: true,
|
|
231
745
|
id,
|
|
232
|
-
type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
|
|
746
|
+
type: MessageType.CF_AGENT_USE_CHAT_RESPONSE,
|
|
747
|
+
...continuation && { continuation: true }
|
|
233
748
|
});
|
|
234
749
|
break;
|
|
235
750
|
}
|
|
@@ -314,7 +829,7 @@ var AIChatAgent = class extends Agent {
|
|
|
314
829
|
});
|
|
315
830
|
break;
|
|
316
831
|
case "tool-input-start": {
|
|
317
|
-
const toolInvocations = message.parts.filter(isToolUIPart);
|
|
832
|
+
const toolInvocations = message.parts.filter(isToolUIPart$1);
|
|
318
833
|
partialToolCalls[data.toolCallId] = {
|
|
319
834
|
text: "",
|
|
320
835
|
toolName: data.toolName,
|
|
@@ -403,11 +918,11 @@ var AIChatAgent = class extends Agent {
|
|
|
403
918
|
preliminary: data.preliminary
|
|
404
919
|
});
|
|
405
920
|
} else {
|
|
406
|
-
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);
|
|
407
922
|
if (!toolInvocation) throw new Error("Tool invocation not found");
|
|
408
923
|
updateToolPart({
|
|
409
924
|
toolCallId: data.toolCallId,
|
|
410
|
-
toolName: getToolName(toolInvocation),
|
|
925
|
+
toolName: getToolName$1(toolInvocation),
|
|
411
926
|
state: "output-available",
|
|
412
927
|
input: toolInvocation.input,
|
|
413
928
|
output: data.output,
|
|
@@ -428,11 +943,11 @@ var AIChatAgent = class extends Agent {
|
|
|
428
943
|
errorText: data.errorText
|
|
429
944
|
});
|
|
430
945
|
} else {
|
|
431
|
-
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);
|
|
432
947
|
if (!toolInvocation) throw new Error("Tool invocation not found");
|
|
433
948
|
updateToolPart({
|
|
434
949
|
toolCallId: data.toolCallId,
|
|
435
|
-
toolName: getToolName(toolInvocation),
|
|
950
|
+
toolName: getToolName$1(toolInvocation),
|
|
436
951
|
state: "output-error",
|
|
437
952
|
input: toolInvocation.input,
|
|
438
953
|
rawInput: "rawInput" in toolInvocation ? toolInvocation.rawInput : void 0,
|
|
@@ -467,36 +982,102 @@ var AIChatAgent = class extends Agent {
|
|
|
467
982
|
});
|
|
468
983
|
break;
|
|
469
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);
|
|
470
996
|
this._broadcastChatMessage({
|
|
471
|
-
body:
|
|
997
|
+
body: chunkBody,
|
|
472
998
|
done: false,
|
|
473
999
|
id,
|
|
474
|
-
type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
|
|
1000
|
+
type: MessageType.CF_AGENT_USE_CHAT_RESPONSE,
|
|
1001
|
+
...continuation && { continuation: true }
|
|
475
1002
|
});
|
|
476
|
-
} catch (
|
|
1003
|
+
} catch (_error) {}
|
|
477
1004
|
} else if (chunk.length > 0) {
|
|
478
1005
|
message.parts.push({
|
|
479
1006
|
type: "text",
|
|
480
1007
|
text: chunk
|
|
481
1008
|
});
|
|
1009
|
+
const chunkBody = JSON.stringify({
|
|
1010
|
+
type: "text-delta",
|
|
1011
|
+
delta: chunk
|
|
1012
|
+
});
|
|
1013
|
+
this._storeStreamChunk(streamId, chunkBody);
|
|
482
1014
|
this._broadcastChatMessage({
|
|
483
|
-
body:
|
|
484
|
-
type: "text-delta",
|
|
485
|
-
delta: chunk
|
|
486
|
-
}),
|
|
1015
|
+
body: chunkBody,
|
|
487
1016
|
done: false,
|
|
488
1017
|
id,
|
|
489
|
-
type: MessageType.CF_AGENT_USE_CHAT_RESPONSE
|
|
1018
|
+
type: MessageType.CF_AGENT_USE_CHAT_RESPONSE,
|
|
1019
|
+
...continuation && { continuation: true }
|
|
490
1020
|
});
|
|
491
1021
|
}
|
|
492
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;
|
|
493
1036
|
} finally {
|
|
494
1037
|
reader.releaseLock();
|
|
495
1038
|
}
|
|
496
|
-
if (message.parts.length > 0)
|
|
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
|
+
}
|
|
497
1062
|
});
|
|
498
1063
|
}
|
|
499
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
|
+
/**
|
|
500
1081
|
* For the given message id, look up its associated AbortController
|
|
501
1082
|
* If the AbortController does not exist, create and store one in memory
|
|
502
1083
|
*
|
|
@@ -527,14 +1108,19 @@ var AIChatAgent = class extends Agent {
|
|
|
527
1108
|
this._chatMessageAbortControllers.clear();
|
|
528
1109
|
}
|
|
529
1110
|
/**
|
|
530
|
-
* When the DO is destroyed, cancel all pending requests
|
|
1111
|
+
* When the DO is destroyed, cancel all pending requests and clean up resources
|
|
531
1112
|
*/
|
|
532
1113
|
async destroy() {
|
|
533
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;
|
|
534
1120
|
await super.destroy();
|
|
535
1121
|
}
|
|
536
1122
|
};
|
|
537
1123
|
|
|
538
1124
|
//#endregion
|
|
539
|
-
export { AIChatAgent };
|
|
1125
|
+
export { AIChatAgent, createToolsFromClientSchemas };
|
|
540
1126
|
//# sourceMappingURL=ai-chat-agent.js.map
|