agents 0.0.0-e416962 → 0.0.0-e4a2352

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 (48) hide show
  1. package/README.md +128 -22
  2. package/dist/ai-chat-agent.d.ts +35 -6
  3. package/dist/ai-chat-agent.js +149 -115
  4. package/dist/ai-chat-agent.js.map +1 -1
  5. package/dist/ai-react.d.ts +20 -5
  6. package/dist/ai-react.js +28 -29
  7. package/dist/ai-react.js.map +1 -1
  8. package/dist/{chunk-Q5ZBHY4Z.js → chunk-HY7ZLHJB.js} +193 -51
  9. package/dist/chunk-HY7ZLHJB.js.map +1 -0
  10. package/dist/chunk-JXN5WZFQ.js +1287 -0
  11. package/dist/chunk-JXN5WZFQ.js.map +1 -0
  12. package/dist/chunk-KUH345EY.js +116 -0
  13. package/dist/chunk-KUH345EY.js.map +1 -0
  14. package/dist/chunk-PVQZBKN7.js +106 -0
  15. package/dist/chunk-PVQZBKN7.js.map +1 -0
  16. package/dist/client-DgyzBU_8.d.ts +4601 -0
  17. package/dist/client.d.ts +16 -2
  18. package/dist/client.js +6 -126
  19. package/dist/client.js.map +1 -1
  20. package/dist/index-BCJclX6q.d.ts +615 -0
  21. package/dist/index.d.ts +35 -308
  22. package/dist/index.js +10 -3
  23. package/dist/mcp/client.d.ts +9 -775
  24. package/dist/mcp/client.js +1 -2
  25. package/dist/mcp/do-oauth-client-provider.d.ts +3 -3
  26. package/dist/mcp/do-oauth-client-provider.js +3 -103
  27. package/dist/mcp/do-oauth-client-provider.js.map +1 -1
  28. package/dist/mcp/index.d.ts +42 -14
  29. package/dist/mcp/index.js +286 -180
  30. package/dist/mcp/index.js.map +1 -1
  31. package/dist/observability/index.d.ts +14 -0
  32. package/dist/observability/index.js +10 -0
  33. package/dist/react.d.ts +87 -5
  34. package/dist/react.js +20 -8
  35. package/dist/react.js.map +1 -1
  36. package/dist/schedule.d.ts +10 -10
  37. package/dist/schedule.js +4 -6
  38. package/dist/schedule.js.map +1 -1
  39. package/dist/serializable.d.ts +32 -0
  40. package/dist/serializable.js +1 -0
  41. package/dist/serializable.js.map +1 -0
  42. package/package.json +76 -68
  43. package/src/index.ts +1120 -143
  44. package/dist/chunk-HMLY7DHA.js +0 -16
  45. package/dist/chunk-HN5JVKAZ.js +0 -606
  46. package/dist/chunk-HN5JVKAZ.js.map +0 -1
  47. package/dist/chunk-Q5ZBHY4Z.js.map +0 -1
  48. /package/dist/{chunk-HMLY7DHA.js.map → observability/index.js.map} +0 -0
package/README.md CHANGED
@@ -66,13 +66,13 @@ export class AIAgent extends Agent {
66
66
  async onRequest(request) {
67
67
  // Connect with AI capabilities
68
68
  const ai = new OpenAI({
69
- apiKey: this.env.OPENAI_API_KEY,
69
+ apiKey: this.env.OPENAI_API_KEY
70
70
  });
71
71
 
72
72
  // Process and understand
73
73
  const response = await ai.chat.completions.create({
74
74
  model: "gpt-4",
75
- messages: [{ role: "user", content: await request.text() }],
75
+ messages: [{ role: "user", content: await request.text() }]
76
76
  });
77
77
 
78
78
  return new Response(response.choices[0].message.content);
@@ -96,17 +96,17 @@ Define your agent's domain:
96
96
  "bindings": [
97
97
  {
98
98
  "name": "AIAgent",
99
- "class_name": "AIAgent",
100
- },
101
- ],
99
+ "class_name": "AIAgent"
100
+ }
101
+ ]
102
102
  },
103
103
  "migrations": [
104
104
  {
105
105
  "tag": "v1",
106
106
  // Mandatory for the Agent to store state
107
- "new_sqlite_classes": ["AIAgent"],
108
- },
109
- ],
107
+ "new_sqlite_classes": ["AIAgent"]
108
+ }
109
+ ]
110
110
  }
111
111
  ```
112
112
 
@@ -123,7 +123,7 @@ const agent = env.AIAgent.get(id);
123
123
  await agent.processTask({
124
124
  type: "analysis",
125
125
  context: "incoming_data",
126
- parameters: initialConfig,
126
+ parameters: initialConfig
127
127
  });
128
128
 
129
129
  // Or reconnect with an existing one
@@ -143,7 +143,7 @@ export class APIAgent extends Agent {
143
143
 
144
144
  return Response.json({
145
145
  insight: await this.process(data),
146
- moment: Date.now(),
146
+ moment: Date.now()
147
147
  });
148
148
  }
149
149
  }
@@ -175,7 +175,7 @@ import { AgentClient } from "agents/client";
175
175
 
176
176
  const connection = new AgentClient({
177
177
  agent: "dialogue-agent",
178
- name: "insight-seeker",
178
+ name: "insight-seeker"
179
179
  });
180
180
 
181
181
  connection.addEventListener("message", (event) => {
@@ -185,7 +185,7 @@ connection.addEventListener("message", (event) => {
185
185
  connection.send(
186
186
  JSON.stringify({
187
187
  type: "inquiry",
188
- content: "What patterns do you see?",
188
+ content: "What patterns do you see?"
189
189
  })
190
190
  );
191
191
  ```
@@ -205,14 +205,14 @@ function AgentInterface() {
205
205
  console.log("Understanding received:", message.data);
206
206
  },
207
207
  onOpen: () => console.log("Connection established"),
208
- onClose: () => console.log("Connection closed"),
208
+ onClose: () => console.log("Connection closed")
209
209
  });
210
210
 
211
211
  const inquire = () => {
212
212
  connection.send(
213
213
  JSON.stringify({
214
214
  type: "inquiry",
215
- content: "What insights have you gathered?",
215
+ content: "What insights have you gathered?"
216
216
  })
217
217
  );
218
218
  };
@@ -235,14 +235,14 @@ export class ThinkingAgent extends Agent {
235
235
  this.setState({
236
236
  ...this.state,
237
237
  insights: [...(this.state.insights || []), newInsight],
238
- understanding: this.state.understanding + 1,
238
+ understanding: this.state.understanding + 1
239
239
  });
240
240
  }
241
241
 
242
242
  onStateUpdate(state, source) {
243
243
  console.log("Understanding deepened:", {
244
244
  newState: state,
245
- origin: source,
245
+ origin: source
246
246
  });
247
247
  }
248
248
  }
@@ -259,7 +259,7 @@ function StateInterface() {
259
259
 
260
260
  const agent = useAgent({
261
261
  agent: "thinking-agent",
262
- onStateUpdate: (newState) => setState(newState),
262
+ onStateUpdate: (newState) => setState(newState)
263
263
  });
264
264
 
265
265
  const increment = () => {
@@ -289,7 +289,7 @@ export class TimeAwareAgent extends Agent {
289
289
 
290
290
  // Daily synthesis
291
291
  this.schedule("0 0 * * *", "dailySynthesis", {
292
- depth: "comprehensive",
292
+ depth: "comprehensive"
293
293
  });
294
294
 
295
295
  // Milestone review
@@ -325,11 +325,11 @@ export class DialogueAgent extends AIChatAgent {
325
325
  const stream = streamText({
326
326
  model: openai("gpt-4o"),
327
327
  messages: this.messages,
328
- onFinish, // call onFinish so that messages get saved
328
+ onFinish // call onFinish so that messages get saved
329
329
  });
330
330
 
331
331
  stream.mergeIntoDataStream(dataStream);
332
- },
332
+ }
333
333
  });
334
334
  }
335
335
  }
@@ -346,14 +346,14 @@ import { useAgentChat } from "agents/ai-react";
346
346
  function ChatInterface() {
347
347
  // Connect to the agent
348
348
  const agent = useAgent({
349
- agent: "dialogue-agent",
349
+ agent: "dialogue-agent"
350
350
  });
351
351
 
352
352
  // Set up the chat interaction
353
353
  const { messages, input, handleInputChange, handleSubmit, clearHistory } =
354
354
  useAgentChat({
355
355
  agent,
356
- maxSteps: 5,
356
+ maxSteps: 5
357
357
  });
358
358
 
359
359
  return (
@@ -393,6 +393,112 @@ This creates:
393
393
  - Intuitive input handling
394
394
  - Easy conversation reset
395
395
 
396
+ ### 🔗 MCP (Model Context Protocol) Integration
397
+
398
+ Agents can seamlessly integrate with the Model Context Protocol, allowing them to act as both MCP servers (providing tools to AI assistants) and MCP clients (using tools from other services).
399
+
400
+ #### Creating an MCP Server
401
+
402
+ ```typescript
403
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
404
+ import { McpAgent } from "agents/mcp";
405
+ import { z } from "zod";
406
+
407
+ type Env = {
408
+ MyMCP: DurableObjectNamespace<MyMCP>;
409
+ };
410
+
411
+ type State = { counter: number };
412
+
413
+ export class MyMCP extends McpAgent<Env, State, {}> {
414
+ server = new McpServer({
415
+ name: "Demo",
416
+ version: "1.0.0"
417
+ });
418
+
419
+ initialState: State = {
420
+ counter: 1
421
+ };
422
+
423
+ async init() {
424
+ this.server.resource("counter", "mcp://resource/counter", (uri) => {
425
+ return {
426
+ contents: [{ text: String(this.state.counter), uri: uri.href }]
427
+ };
428
+ });
429
+
430
+ this.server.tool(
431
+ "add",
432
+ "Add to the counter, stored in the MCP",
433
+ { a: z.number() },
434
+ async ({ a }) => {
435
+ this.setState({ ...this.state, counter: this.state.counter + a });
436
+
437
+ return {
438
+ content: [
439
+ {
440
+ text: String(`Added ${a}, total is now ${this.state.counter}`),
441
+ type: "text"
442
+ }
443
+ ]
444
+ };
445
+ }
446
+ );
447
+ }
448
+
449
+ onStateUpdate(state: State) {
450
+ console.log({ stateUpdate: state });
451
+ }
452
+ }
453
+
454
+ // HTTP Streamable transport (recommended)
455
+ export default MyMCP.serve("/mcp", {
456
+ binding: "MyMCP"
457
+ });
458
+
459
+ // Or SSE transport for legacy compatibility
460
+ // export default MyMCP.serveSSE("/mcp", { binding: "MyMCP" });
461
+ ```
462
+
463
+ #### Using MCP Tools
464
+
465
+ ```typescript
466
+ import { MCPClientManager } from "agents/mcp";
467
+
468
+ const client = new MCPClientManager("my-app", "1.0.0");
469
+
470
+ // Connect to an MCP server
471
+ await client.connect("https://weather-service.com/mcp", {
472
+ transport: { type: "streamable-http" }
473
+ });
474
+
475
+ // Use tools from the server
476
+ const weather = await client.callTool({
477
+ serverId: "weather-service",
478
+ name: "getWeather",
479
+ arguments: { location: "San Francisco" }
480
+ });
481
+ ```
482
+
483
+ #### AI SDK Integration
484
+
485
+ ```typescript
486
+ import { generateText } from "ai";
487
+
488
+ // Convert MCP tools for AI use
489
+ const result = await generateText({
490
+ model: openai("gpt-4"),
491
+ tools: client.unstable_getAITools(),
492
+ prompt: "What's the weather in Tokyo?"
493
+ });
494
+ ```
495
+
496
+ **Transport Options:**
497
+
498
+ - **Auto**: Automatically determine the correct transport
499
+ - **HTTP Streamable**: Best performance, batch requests, session management
500
+ - **SSE**: Simple setup, legacy compatibility
501
+
396
502
  ### 💬 The Path Forward
397
503
 
398
504
  We're developing new dimensions of agent capability:
@@ -1,15 +1,17 @@
1
- import { Agent, AgentContext } from "./index.js";
2
1
  import { Message, StreamTextOnFinishCallback, ToolSet } from "ai";
2
+ import { A as Agent, a as AgentContext } from "./index-BCJclX6q.js";
3
3
  import { Connection, WSMessage } from "partyserver";
4
- import "./mcp/client.js";
5
- import "zod";
6
- import "@modelcontextprotocol/sdk/types.js";
4
+ import "cloudflare:workers";
7
5
  import "@modelcontextprotocol/sdk/client/index.js";
6
+ import "@modelcontextprotocol/sdk/types.js";
7
+ import "./client-DgyzBU_8.js";
8
+ import "zod";
9
+ import "@modelcontextprotocol/sdk/shared/protocol.js";
8
10
  import "@modelcontextprotocol/sdk/client/sse.js";
11
+ import "@modelcontextprotocol/sdk/client/streamableHttp.js";
9
12
  import "./mcp/do-oauth-client-provider.js";
10
13
  import "@modelcontextprotocol/sdk/client/auth.js";
11
14
  import "@modelcontextprotocol/sdk/shared/auth.js";
12
- import "@modelcontextprotocol/sdk/shared/protocol.js";
13
15
 
14
16
  /**
15
17
  * Extension of Agent with built-in chat capabilities
@@ -19,12 +21,18 @@ declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<
19
21
  Env,
20
22
  State
21
23
  > {
22
- #private;
24
+ /**
25
+ * Map of message `id`s to `AbortController`s
26
+ * useful to propagate request cancellation signals for any external calls made by the agent
27
+ */
28
+ private _chatMessageAbortControllers;
23
29
  /** Array of chat messages for the current conversation */
24
30
  messages: Message[];
25
31
  constructor(ctx: AgentContext, env: Env);
32
+ private _broadcastChatMessage;
26
33
  onMessage(connection: Connection, message: WSMessage): Promise<void>;
27
34
  onRequest(request: Request): Promise<Response>;
35
+ private _tryCatchChat;
28
36
  /**
29
37
  * Handle incoming chat messages and generate a response
30
38
  * @param onFinish Callback to be called when the response is finished
@@ -46,6 +54,27 @@ declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<
46
54
  messages: Message[],
47
55
  excludeBroadcastIds?: string[]
48
56
  ): Promise<void>;
57
+ private _messagesNotAlreadyInAgent;
58
+ private _reply;
59
+ /**
60
+ * For the given message id, look up its associated AbortController
61
+ * If the AbortController does not exist, create and store one in memory
62
+ *
63
+ * returns the AbortSignal associated with the AbortController
64
+ */
65
+ private _getAbortSignal;
66
+ /**
67
+ * Remove an abort controller from the cache of pending message responses
68
+ */
69
+ private _removeAbortController;
70
+ /**
71
+ * Propagate an abort signal for any requests associated with the given message id
72
+ */
73
+ private _cancelChatRequest;
74
+ /**
75
+ * Abort all pending requests and clear the cache of AbortControllers
76
+ */
77
+ private _destroyAbortControllers;
49
78
  /**
50
79
  * When the DO is destroyed, cancel all pending requests
51
80
  */
@@ -1,27 +1,16 @@
1
1
  import {
2
2
  Agent
3
- } from "./chunk-HN5JVKAZ.js";
4
- import "./chunk-Q5ZBHY4Z.js";
5
- import {
6
- __privateAdd,
7
- __privateGet,
8
- __privateMethod,
9
- __privateSet
10
- } from "./chunk-HMLY7DHA.js";
3
+ } from "./chunk-JXN5WZFQ.js";
4
+ import "./chunk-HY7ZLHJB.js";
5
+ import "./chunk-PVQZBKN7.js";
6
+ import "./chunk-KUH345EY.js";
11
7
 
12
8
  // src/ai-chat-agent.ts
13
9
  import { appendResponseMessages } from "ai";
14
10
  var decoder = new TextDecoder();
15
- var _chatMessageAbortControllers, _AIChatAgent_instances, broadcastChatMessage_fn, tryCatch_fn, reply_fn, getAbortSignal_fn, removeAbortController_fn, cancelChatRequest_fn, destroyAbortControllers_fn;
16
11
  var AIChatAgent = class extends Agent {
17
12
  constructor(ctx, env) {
18
13
  super(ctx, env);
19
- __privateAdd(this, _AIChatAgent_instances);
20
- /**
21
- * Map of message `id`s to `AbortController`s
22
- * useful to propagate request cancellation signals for any external calls made by the agent
23
- */
24
- __privateAdd(this, _chatMessageAbortControllers);
25
14
  this.sql`create table if not exists cf_ai_chat_agent_messages (
26
15
  id text primary key,
27
16
  message text not null,
@@ -30,74 +19,117 @@ var AIChatAgent = class extends Agent {
30
19
  this.messages = (this.sql`select * from cf_ai_chat_agent_messages` || []).map((row) => {
31
20
  return JSON.parse(row.message);
32
21
  });
33
- __privateSet(this, _chatMessageAbortControllers, /* @__PURE__ */ new Map());
22
+ this._chatMessageAbortControllers = /* @__PURE__ */ new Map();
23
+ }
24
+ _broadcastChatMessage(message, exclude) {
25
+ this.broadcast(JSON.stringify(message), exclude);
34
26
  }
35
27
  async onMessage(connection, message) {
36
28
  if (typeof message === "string") {
37
29
  let data;
38
30
  try {
39
31
  data = JSON.parse(message);
40
- } catch (error) {
32
+ } catch (_error) {
41
33
  return;
42
34
  }
43
35
  if (data.type === "cf_agent_use_chat_request" && data.init.method === "POST") {
44
36
  const {
45
- method,
46
- keepalive,
47
- headers,
48
- body,
37
+ // method,
38
+ // keepalive,
39
+ // headers,
40
+ body
49
41
  // we're reading this
50
- redirect,
51
- integrity,
52
- credentials,
53
- mode,
54
- referrer,
55
- referrerPolicy,
56
- window
42
+ //
43
+ // // these might not exist?
57
44
  // dispatcher,
58
45
  // duplex
59
46
  } = data.init;
60
47
  const { messages } = JSON.parse(body);
61
- __privateMethod(this, _AIChatAgent_instances, broadcastChatMessage_fn).call(this, {
62
- type: "cf_agent_chat_messages",
63
- messages
64
- }, [connection.id]);
48
+ this._broadcastChatMessage(
49
+ {
50
+ messages,
51
+ type: "cf_agent_chat_messages"
52
+ },
53
+ [connection.id]
54
+ );
55
+ const incomingMessages = this._messagesNotAlreadyInAgent(messages);
65
56
  await this.persistMessages(messages, [connection.id]);
57
+ this.observability?.emit(
58
+ {
59
+ displayMessage: "Chat message request",
60
+ id: data.id,
61
+ payload: {
62
+ message: incomingMessages
63
+ },
64
+ timestamp: Date.now(),
65
+ type: "message:request"
66
+ },
67
+ this.ctx
68
+ );
66
69
  const chatMessageId = data.id;
67
- const abortSignal = __privateMethod(this, _AIChatAgent_instances, getAbortSignal_fn).call(this, chatMessageId);
68
- return __privateMethod(this, _AIChatAgent_instances, tryCatch_fn).call(this, async () => {
70
+ const abortSignal = this._getAbortSignal(chatMessageId);
71
+ return this._tryCatchChat(async () => {
69
72
  const response = await this.onChatMessage(
70
73
  async ({ response: response2 }) => {
71
74
  const finalMessages = appendResponseMessages({
72
75
  messages,
73
76
  responseMessages: response2.messages
74
77
  });
78
+ const outgoingMessages = this._messagesNotAlreadyInAgent(finalMessages);
75
79
  await this.persistMessages(finalMessages, [connection.id]);
76
- __privateMethod(this, _AIChatAgent_instances, removeAbortController_fn).call(this, chatMessageId);
80
+ this._removeAbortController(chatMessageId);
81
+ this.observability?.emit(
82
+ {
83
+ displayMessage: "Chat message response",
84
+ id: data.id,
85
+ payload: {
86
+ message: outgoingMessages
87
+ },
88
+ timestamp: Date.now(),
89
+ type: "message:response"
90
+ },
91
+ this.ctx
92
+ );
77
93
  },
78
94
  abortSignal ? { abortSignal } : void 0
79
95
  );
80
96
  if (response) {
81
- await __privateMethod(this, _AIChatAgent_instances, reply_fn).call(this, data.id, response);
97
+ await this._reply(data.id, response);
98
+ } else {
99
+ console.warn(
100
+ `[AIChatAgent] onChatMessage returned no response for chatMessageId: ${chatMessageId}`
101
+ );
102
+ this._broadcastChatMessage(
103
+ {
104
+ body: "No response was generated by the agent.",
105
+ done: true,
106
+ id: data.id,
107
+ type: "cf_agent_use_chat_response"
108
+ },
109
+ [connection.id]
110
+ );
82
111
  }
83
112
  });
84
113
  }
85
114
  if (data.type === "cf_agent_chat_clear") {
86
- __privateMethod(this, _AIChatAgent_instances, destroyAbortControllers_fn).call(this);
115
+ this._destroyAbortControllers();
87
116
  this.sql`delete from cf_ai_chat_agent_messages`;
88
117
  this.messages = [];
89
- __privateMethod(this, _AIChatAgent_instances, broadcastChatMessage_fn).call(this, {
90
- type: "cf_agent_chat_clear"
91
- }, [connection.id]);
118
+ this._broadcastChatMessage(
119
+ {
120
+ type: "cf_agent_chat_clear"
121
+ },
122
+ [connection.id]
123
+ );
92
124
  } else if (data.type === "cf_agent_chat_messages") {
93
125
  await this.persistMessages(data.messages, [connection.id]);
94
126
  } else if (data.type === "cf_agent_chat_request_cancel") {
95
- __privateMethod(this, _AIChatAgent_instances, cancelChatRequest_fn).call(this, data.id);
127
+ this._cancelChatRequest(data.id);
96
128
  }
97
129
  }
98
130
  }
99
131
  async onRequest(request) {
100
- return __privateMethod(this, _AIChatAgent_instances, tryCatch_fn).call(this, () => {
132
+ return this._tryCatchChat(() => {
101
133
  const url = new URL(request.url);
102
134
  if (url.pathname.endsWith("/get-messages")) {
103
135
  const messages = (this.sql`select * from cf_ai_chat_agent_messages` || []).map((row) => {
@@ -108,6 +140,13 @@ var AIChatAgent = class extends Agent {
108
140
  return super.onRequest(request);
109
141
  });
110
142
  }
143
+ async _tryCatchChat(fn) {
144
+ try {
145
+ return await fn();
146
+ } catch (e) {
147
+ throw this.onError(e);
148
+ }
149
+ }
111
150
  /**
112
151
  * Handle incoming chat messages and generate a response
113
152
  * @param onFinish Callback to be called when the response is finished
@@ -145,88 +184,83 @@ var AIChatAgent = class extends Agent {
145
184
  this.sql`insert into cf_ai_chat_agent_messages (id, message) values (${message.id},${JSON.stringify(message)})`;
146
185
  }
147
186
  this.messages = messages;
148
- __privateMethod(this, _AIChatAgent_instances, broadcastChatMessage_fn).call(this, {
149
- type: "cf_agent_chat_messages",
150
- messages
151
- }, excludeBroadcastIds);
152
- }
153
- /**
154
- * When the DO is destroyed, cancel all pending requests
155
- */
156
- async destroy() {
157
- __privateMethod(this, _AIChatAgent_instances, destroyAbortControllers_fn).call(this);
158
- await super.destroy();
187
+ this._broadcastChatMessage(
188
+ {
189
+ messages,
190
+ type: "cf_agent_chat_messages"
191
+ },
192
+ excludeBroadcastIds
193
+ );
159
194
  }
160
- };
161
- _chatMessageAbortControllers = new WeakMap();
162
- _AIChatAgent_instances = new WeakSet();
163
- broadcastChatMessage_fn = function(message, exclude) {
164
- this.broadcast(JSON.stringify(message), exclude);
165
- };
166
- tryCatch_fn = async function(fn) {
167
- try {
168
- return await fn();
169
- } catch (e) {
170
- throw this.onError(e);
195
+ _messagesNotAlreadyInAgent(messages) {
196
+ const existingIds = new Set(this.messages.map((message) => message.id));
197
+ return messages.filter((message) => !existingIds.has(message.id));
171
198
  }
172
- };
173
- reply_fn = async function(id, response) {
174
- return __privateMethod(this, _AIChatAgent_instances, tryCatch_fn).call(this, async () => {
175
- for await (const chunk of response.body) {
176
- const body = decoder.decode(chunk);
177
- __privateMethod(this, _AIChatAgent_instances, broadcastChatMessage_fn).call(this, {
199
+ async _reply(id, response) {
200
+ return this._tryCatchChat(async () => {
201
+ for await (const chunk of response.body) {
202
+ const body = decoder.decode(chunk);
203
+ this._broadcastChatMessage({
204
+ body,
205
+ done: false,
206
+ id,
207
+ type: "cf_agent_use_chat_response"
208
+ });
209
+ }
210
+ this._broadcastChatMessage({
211
+ body: "",
212
+ done: true,
178
213
  id,
179
- type: "cf_agent_use_chat_response",
180
- body,
181
- done: false
214
+ type: "cf_agent_use_chat_response"
182
215
  });
183
- }
184
- __privateMethod(this, _AIChatAgent_instances, broadcastChatMessage_fn).call(this, {
185
- id,
186
- type: "cf_agent_use_chat_response",
187
- body: "",
188
- done: true
189
216
  });
190
- });
191
- };
192
- /**
193
- * For the given message id, look up its associated AbortController
194
- * If the AbortController does not exist, create and store one in memory
195
- *
196
- * returns the AbortSignal associated with the AbortController
197
- */
198
- getAbortSignal_fn = function(id) {
199
- if (typeof id !== "string") {
200
- return void 0;
201
217
  }
202
- if (!__privateGet(this, _chatMessageAbortControllers).has(id)) {
203
- __privateGet(this, _chatMessageAbortControllers).set(id, new AbortController());
218
+ /**
219
+ * For the given message id, look up its associated AbortController
220
+ * If the AbortController does not exist, create and store one in memory
221
+ *
222
+ * returns the AbortSignal associated with the AbortController
223
+ */
224
+ _getAbortSignal(id) {
225
+ if (typeof id !== "string") {
226
+ return void 0;
227
+ }
228
+ if (!this._chatMessageAbortControllers.has(id)) {
229
+ this._chatMessageAbortControllers.set(id, new AbortController());
230
+ }
231
+ return this._chatMessageAbortControllers.get(id)?.signal;
204
232
  }
205
- return __privateGet(this, _chatMessageAbortControllers).get(id)?.signal;
206
- };
207
- /**
208
- * Remove an abort controller from the cache of pending message responses
209
- */
210
- removeAbortController_fn = function(id) {
211
- __privateGet(this, _chatMessageAbortControllers).delete(id);
212
- };
213
- /**
214
- * Propagate an abort signal for any requests associated with the given message id
215
- */
216
- cancelChatRequest_fn = function(id) {
217
- if (__privateGet(this, _chatMessageAbortControllers).has(id)) {
218
- const abortController = __privateGet(this, _chatMessageAbortControllers).get(id);
219
- abortController?.abort();
233
+ /**
234
+ * Remove an abort controller from the cache of pending message responses
235
+ */
236
+ _removeAbortController(id) {
237
+ this._chatMessageAbortControllers.delete(id);
220
238
  }
221
- };
222
- /**
223
- * Abort all pending requests and clear the cache of AbortControllers
224
- */
225
- destroyAbortControllers_fn = function() {
226
- for (const controller of __privateGet(this, _chatMessageAbortControllers).values()) {
227
- controller?.abort();
239
+ /**
240
+ * Propagate an abort signal for any requests associated with the given message id
241
+ */
242
+ _cancelChatRequest(id) {
243
+ if (this._chatMessageAbortControllers.has(id)) {
244
+ const abortController = this._chatMessageAbortControllers.get(id);
245
+ abortController?.abort();
246
+ }
247
+ }
248
+ /**
249
+ * Abort all pending requests and clear the cache of AbortControllers
250
+ */
251
+ _destroyAbortControllers() {
252
+ for (const controller of this._chatMessageAbortControllers.values()) {
253
+ controller?.abort();
254
+ }
255
+ this._chatMessageAbortControllers.clear();
256
+ }
257
+ /**
258
+ * When the DO is destroyed, cancel all pending requests
259
+ */
260
+ async destroy() {
261
+ this._destroyAbortControllers();
262
+ await super.destroy();
228
263
  }
229
- __privateGet(this, _chatMessageAbortControllers).clear();
230
264
  };
231
265
  export {
232
266
  AIChatAgent