agents 0.0.0-e376805 → 0.0.0-e416962

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.
@@ -1,8 +1,15 @@
1
1
  import { Agent, AgentContext } from "./index.js";
2
2
  import { Message, StreamTextOnFinishCallback, ToolSet } from "ai";
3
3
  import { Connection, WSMessage } from "partyserver";
4
- import "node:async_hooks";
5
- import "cloudflare:workers";
4
+ import "./mcp/client.js";
5
+ import "zod";
6
+ import "@modelcontextprotocol/sdk/types.js";
7
+ import "@modelcontextprotocol/sdk/client/index.js";
8
+ import "@modelcontextprotocol/sdk/client/sse.js";
9
+ import "./mcp/do-oauth-client-provider.js";
10
+ import "@modelcontextprotocol/sdk/client/auth.js";
11
+ import "@modelcontextprotocol/sdk/shared/auth.js";
12
+ import "@modelcontextprotocol/sdk/shared/protocol.js";
6
13
 
7
14
  /**
8
15
  * Extension of Agent with built-in chat capabilities
@@ -21,16 +28,28 @@ declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<
21
28
  /**
22
29
  * Handle incoming chat messages and generate a response
23
30
  * @param onFinish Callback to be called when the response is finished
31
+ * @param options.signal A signal to pass to any child requests which can be used to cancel them
24
32
  * @returns Response to send to the client or undefined
25
33
  */
26
34
  onChatMessage(
27
- onFinish: StreamTextOnFinishCallback<ToolSet>
35
+ onFinish: StreamTextOnFinishCallback<ToolSet>,
36
+ options?: {
37
+ abortSignal: AbortSignal | undefined;
38
+ }
28
39
  ): Promise<Response | undefined>;
29
40
  /**
30
41
  * Save messages on the server side and trigger AI response
31
42
  * @param messages Chat messages to save
32
43
  */
33
44
  saveMessages(messages: Message[]): Promise<void>;
45
+ persistMessages(
46
+ messages: Message[],
47
+ excludeBroadcastIds?: string[]
48
+ ): Promise<void>;
49
+ /**
50
+ * When the DO is destroyed, cancel all pending requests
51
+ */
52
+ destroy(): Promise<void>;
34
53
  }
35
54
 
36
55
  export { AIChatAgent };
@@ -1,19 +1,27 @@
1
1
  import {
2
2
  Agent
3
- } from "./chunk-YMUU7QHV.js";
3
+ } from "./chunk-HN5JVKAZ.js";
4
+ import "./chunk-Q5ZBHY4Z.js";
4
5
  import {
5
6
  __privateAdd,
6
- __privateMethod
7
+ __privateGet,
8
+ __privateMethod,
9
+ __privateSet
7
10
  } from "./chunk-HMLY7DHA.js";
8
11
 
9
12
  // src/ai-chat-agent.ts
10
13
  import { appendResponseMessages } from "ai";
11
14
  var decoder = new TextDecoder();
12
- var _AIChatAgent_instances, broadcastChatMessage_fn, tryCatch_fn, persistMessages_fn, reply_fn;
15
+ var _chatMessageAbortControllers, _AIChatAgent_instances, broadcastChatMessage_fn, tryCatch_fn, reply_fn, getAbortSignal_fn, removeAbortController_fn, cancelChatRequest_fn, destroyAbortControllers_fn;
13
16
  var AIChatAgent = class extends Agent {
14
17
  constructor(ctx, env) {
15
18
  super(ctx, env);
16
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);
17
25
  this.sql`create table if not exists cf_ai_chat_agent_messages (
18
26
  id text primary key,
19
27
  message text not null,
@@ -22,6 +30,7 @@ var AIChatAgent = class extends Agent {
22
30
  this.messages = (this.sql`select * from cf_ai_chat_agent_messages` || []).map((row) => {
23
31
  return JSON.parse(row.message);
24
32
  });
33
+ __privateSet(this, _chatMessageAbortControllers, /* @__PURE__ */ new Map());
25
34
  }
26
35
  async onMessage(connection, message) {
27
36
  if (typeof message === "string") {
@@ -53,28 +62,37 @@ var AIChatAgent = class extends Agent {
53
62
  type: "cf_agent_chat_messages",
54
63
  messages
55
64
  }, [connection.id]);
56
- await __privateMethod(this, _AIChatAgent_instances, persistMessages_fn).call(this, messages, [connection.id]);
65
+ await this.persistMessages(messages, [connection.id]);
66
+ const chatMessageId = data.id;
67
+ const abortSignal = __privateMethod(this, _AIChatAgent_instances, getAbortSignal_fn).call(this, chatMessageId);
57
68
  return __privateMethod(this, _AIChatAgent_instances, tryCatch_fn).call(this, async () => {
58
- const response = await this.onChatMessage(async ({ response: response2 }) => {
59
- const finalMessages = appendResponseMessages({
60
- messages,
61
- responseMessages: response2.messages
62
- });
63
- await __privateMethod(this, _AIChatAgent_instances, persistMessages_fn).call(this, finalMessages, [connection.id]);
64
- });
69
+ const response = await this.onChatMessage(
70
+ async ({ response: response2 }) => {
71
+ const finalMessages = appendResponseMessages({
72
+ messages,
73
+ responseMessages: response2.messages
74
+ });
75
+ await this.persistMessages(finalMessages, [connection.id]);
76
+ __privateMethod(this, _AIChatAgent_instances, removeAbortController_fn).call(this, chatMessageId);
77
+ },
78
+ abortSignal ? { abortSignal } : void 0
79
+ );
65
80
  if (response) {
66
81
  await __privateMethod(this, _AIChatAgent_instances, reply_fn).call(this, data.id, response);
67
82
  }
68
83
  });
69
84
  }
70
85
  if (data.type === "cf_agent_chat_clear") {
86
+ __privateMethod(this, _AIChatAgent_instances, destroyAbortControllers_fn).call(this);
71
87
  this.sql`delete from cf_ai_chat_agent_messages`;
72
88
  this.messages = [];
73
89
  __privateMethod(this, _AIChatAgent_instances, broadcastChatMessage_fn).call(this, {
74
90
  type: "cf_agent_chat_clear"
75
91
  }, [connection.id]);
76
92
  } else if (data.type === "cf_agent_chat_messages") {
77
- await __privateMethod(this, _AIChatAgent_instances, persistMessages_fn).call(this, data.messages, [connection.id]);
93
+ await this.persistMessages(data.messages, [connection.id]);
94
+ } else if (data.type === "cf_agent_chat_request_cancel") {
95
+ __privateMethod(this, _AIChatAgent_instances, cancelChatRequest_fn).call(this, data.id);
78
96
  }
79
97
  }
80
98
  }
@@ -93,9 +111,10 @@ var AIChatAgent = class extends Agent {
93
111
  /**
94
112
  * Handle incoming chat messages and generate a response
95
113
  * @param onFinish Callback to be called when the response is finished
114
+ * @param options.signal A signal to pass to any child requests which can be used to cancel them
96
115
  * @returns Response to send to the client or undefined
97
116
  */
98
- async onChatMessage(onFinish) {
117
+ async onChatMessage(onFinish, options) {
99
118
  throw new Error(
100
119
  "recieved a chat message, override onChatMessage and return a Response to send to the client"
101
120
  );
@@ -105,13 +124,13 @@ var AIChatAgent = class extends Agent {
105
124
  * @param messages Chat messages to save
106
125
  */
107
126
  async saveMessages(messages) {
108
- await __privateMethod(this, _AIChatAgent_instances, persistMessages_fn).call(this, messages);
127
+ await this.persistMessages(messages);
109
128
  const response = await this.onChatMessage(async ({ response: response2 }) => {
110
129
  const finalMessages = appendResponseMessages({
111
130
  messages,
112
131
  responseMessages: response2.messages
113
132
  });
114
- await __privateMethod(this, _AIChatAgent_instances, persistMessages_fn).call(this, finalMessages, []);
133
+ await this.persistMessages(finalMessages, []);
115
134
  });
116
135
  if (response) {
117
136
  for await (const chunk of response.body) {
@@ -120,7 +139,26 @@ var AIChatAgent = class extends Agent {
120
139
  response.body?.cancel();
121
140
  }
122
141
  }
142
+ async persistMessages(messages, excludeBroadcastIds = []) {
143
+ this.sql`delete from cf_ai_chat_agent_messages`;
144
+ for (const message of messages) {
145
+ this.sql`insert into cf_ai_chat_agent_messages (id, message) values (${message.id},${JSON.stringify(message)})`;
146
+ }
147
+ 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();
159
+ }
123
160
  };
161
+ _chatMessageAbortControllers = new WeakMap();
124
162
  _AIChatAgent_instances = new WeakSet();
125
163
  broadcastChatMessage_fn = function(message, exclude) {
126
164
  this.broadcast(JSON.stringify(message), exclude);
@@ -132,17 +170,6 @@ tryCatch_fn = async function(fn) {
132
170
  throw this.onError(e);
133
171
  }
134
172
  };
135
- persistMessages_fn = async function(messages, excludeBroadcastIds = []) {
136
- this.sql`delete from cf_ai_chat_agent_messages`;
137
- for (const message of messages) {
138
- this.sql`insert into cf_ai_chat_agent_messages (id, message) values (${message.id},${JSON.stringify(message)})`;
139
- }
140
- this.messages = messages;
141
- __privateMethod(this, _AIChatAgent_instances, broadcastChatMessage_fn).call(this, {
142
- type: "cf_agent_chat_messages",
143
- messages
144
- }, excludeBroadcastIds);
145
- };
146
173
  reply_fn = async function(id, response) {
147
174
  return __privateMethod(this, _AIChatAgent_instances, tryCatch_fn).call(this, async () => {
148
175
  for await (const chunk of response.body) {
@@ -162,6 +189,45 @@ reply_fn = async function(id, response) {
162
189
  });
163
190
  });
164
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
+ }
202
+ if (!__privateGet(this, _chatMessageAbortControllers).has(id)) {
203
+ __privateGet(this, _chatMessageAbortControllers).set(id, new AbortController());
204
+ }
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();
220
+ }
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();
228
+ }
229
+ __privateGet(this, _chatMessageAbortControllers).clear();
230
+ };
165
231
  export {
166
232
  AIChatAgent
167
233
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/ai-chat-agent.ts"],"sourcesContent":["import { Agent, type AgentContext, type Connection, type WSMessage } from \"./\";\nimport type {\n Message as ChatMessage,\n StreamTextOnFinishCallback,\n ToolSet,\n} from \"ai\";\nimport { appendResponseMessages } from \"ai\";\nimport type { OutgoingMessage, IncomingMessage } from \"./ai-types\";\nconst decoder = new TextDecoder();\n\n/**\n * Extension of Agent with built-in chat capabilities\n * @template Env Environment type containing bindings\n */\nexport class AIChatAgent<Env = unknown, State = unknown> extends Agent<\n Env,\n State\n> {\n /** Array of chat messages for the current conversation */\n messages: ChatMessage[];\n constructor(ctx: AgentContext, env: Env) {\n super(ctx, env);\n this.sql`create table if not exists cf_ai_chat_agent_messages (\n id text primary key,\n message text not null,\n created_at datetime default current_timestamp\n )`;\n this.messages = (\n this.sql`select * from cf_ai_chat_agent_messages` || []\n ).map((row) => {\n return JSON.parse(row.message as string);\n });\n }\n\n #broadcastChatMessage(message: OutgoingMessage, exclude?: string[]) {\n this.broadcast(JSON.stringify(message), exclude);\n }\n\n override async onMessage(connection: Connection, message: WSMessage) {\n if (typeof message === \"string\") {\n let data: IncomingMessage;\n try {\n data = JSON.parse(message) as IncomingMessage;\n } catch (error) {\n // silently ignore invalid messages for now\n // TODO: log errors with log levels\n return;\n }\n if (\n data.type === \"cf_agent_use_chat_request\" &&\n data.init.method === \"POST\"\n ) {\n const {\n method,\n keepalive,\n headers,\n body, // we're reading this\n redirect,\n integrity,\n credentials,\n mode,\n referrer,\n referrerPolicy,\n window,\n // dispatcher,\n // duplex\n } = data.init;\n const { messages } = JSON.parse(body as string);\n this.#broadcastChatMessage(\n {\n type: \"cf_agent_chat_messages\",\n messages,\n },\n [connection.id]\n );\n await this.#persistMessages(messages, [connection.id]);\n return this.#tryCatch(async () => {\n const response = await this.onChatMessage(async ({ response }) => {\n const finalMessages = appendResponseMessages({\n messages,\n responseMessages: response.messages,\n });\n\n await this.#persistMessages(finalMessages, [connection.id]);\n });\n if (response) {\n await this.#reply(data.id, response);\n }\n });\n }\n if (data.type === \"cf_agent_chat_clear\") {\n this.sql`delete from cf_ai_chat_agent_messages`;\n this.messages = [];\n this.#broadcastChatMessage(\n {\n type: \"cf_agent_chat_clear\",\n },\n [connection.id]\n );\n } else if (data.type === \"cf_agent_chat_messages\") {\n // replace the messages with the new ones\n await this.#persistMessages(data.messages, [connection.id]);\n }\n }\n }\n\n override async onRequest(request: Request): Promise<Response> {\n return this.#tryCatch(() => {\n const url = new URL(request.url);\n if (url.pathname.endsWith(\"/get-messages\")) {\n const messages = (\n this.sql`select * from cf_ai_chat_agent_messages` || []\n ).map((row) => {\n return JSON.parse(row.message as string);\n });\n return Response.json(messages);\n }\n return super.onRequest(request);\n });\n }\n\n async #tryCatch<T>(fn: () => T | Promise<T>) {\n try {\n return await fn();\n } catch (e) {\n throw this.onError(e);\n }\n }\n\n /**\n * Handle incoming chat messages and generate a response\n * @param onFinish Callback to be called when the response is finished\n * @returns Response to send to the client or undefined\n */\n async onChatMessage(\n onFinish: StreamTextOnFinishCallback<ToolSet>\n ): Promise<Response | undefined> {\n throw new Error(\n \"recieved a chat message, override onChatMessage and return a Response to send to the client\"\n );\n }\n\n /**\n * Save messages on the server side and trigger AI response\n * @param messages Chat messages to save\n */\n async saveMessages(messages: ChatMessage[]) {\n await this.#persistMessages(messages);\n const response = await this.onChatMessage(async ({ response }) => {\n const finalMessages = appendResponseMessages({\n messages,\n responseMessages: response.messages,\n });\n\n await this.#persistMessages(finalMessages, []);\n });\n if (response) {\n // we're just going to drain the body\n // @ts-ignore TODO: fix this type error\n for await (const chunk of response.body!) {\n decoder.decode(chunk);\n }\n response.body?.cancel();\n }\n }\n\n async #persistMessages(\n messages: ChatMessage[],\n excludeBroadcastIds: string[] = []\n ) {\n this.sql`delete from cf_ai_chat_agent_messages`;\n for (const message of messages) {\n this.sql`insert into cf_ai_chat_agent_messages (id, message) values (${\n message.id\n },${JSON.stringify(message)})`;\n }\n this.messages = messages;\n this.#broadcastChatMessage(\n {\n type: \"cf_agent_chat_messages\",\n messages: messages,\n },\n excludeBroadcastIds\n );\n }\n\n async #reply(id: string, response: Response) {\n // now take chunks out from dataStreamResponse and send them to the client\n return this.#tryCatch(async () => {\n // @ts-expect-error TODO: fix this type error\n for await (const chunk of response.body!) {\n const body = decoder.decode(chunk);\n\n this.#broadcastChatMessage({\n id,\n type: \"cf_agent_use_chat_response\",\n body,\n done: false,\n });\n }\n\n this.#broadcastChatMessage({\n id,\n type: \"cf_agent_use_chat_response\",\n body: \"\",\n done: true,\n });\n });\n }\n}\n"],"mappings":";;;;;;;;;AAMA,SAAS,8BAA8B;AAEvC,IAAM,UAAU,IAAI,YAAY;AARhC;AAcO,IAAM,cAAN,cAA0D,MAG/D;AAAA,EAGA,YAAY,KAAmB,KAAU;AACvC,UAAM,KAAK,GAAG;AAPX;AAQH,SAAK;AAAA;AAAA;AAAA;AAAA;AAKL,SAAK,YACH,KAAK,gDAAgD,CAAC,GACtD,IAAI,CAAC,QAAQ;AACb,aAAO,KAAK,MAAM,IAAI,OAAiB;AAAA,IACzC,CAAC;AAAA,EACH;AAAA,EAMA,MAAe,UAAU,YAAwB,SAAoB;AACnE,QAAI,OAAO,YAAY,UAAU;AAC/B,UAAI;AACJ,UAAI;AACF,eAAO,KAAK,MAAM,OAAO;AAAA,MAC3B,SAAS,OAAO;AAGd;AAAA,MACF;AACA,UACE,KAAK,SAAS,+BACd,KAAK,KAAK,WAAW,QACrB;AACA,cAAM;AAAA,UACJ;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA;AAAA;AAAA,QAGF,IAAI,KAAK;AACT,cAAM,EAAE,SAAS,IAAI,KAAK,MAAM,IAAc;AAC9C,8BAAK,iDAAL,WACE;AAAA,UACE,MAAM;AAAA,UACN;AAAA,QACF,GACA,CAAC,WAAW,EAAE;AAEhB,cAAM,sBAAK,4CAAL,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F;","names":["response"]}
1
+ {"version":3,"sources":["../src/ai-chat-agent.ts"],"sourcesContent":["import { Agent, type AgentContext, type Connection, type WSMessage } from \"./\";\nimport type {\n Message as ChatMessage,\n StreamTextOnFinishCallback,\n ToolSet,\n} from \"ai\";\nimport { appendResponseMessages } from \"ai\";\nimport type { OutgoingMessage, IncomingMessage } from \"./ai-types\";\n\nconst decoder = new TextDecoder();\n\n/**\n * Extension of Agent with built-in chat capabilities\n * @template Env Environment type containing bindings\n */\nexport class AIChatAgent<Env = unknown, State = unknown> extends Agent<\n Env,\n State\n> {\n /**\n * Map of message `id`s to `AbortController`s\n * useful to propagate request cancellation signals for any external calls made by the agent\n */\n #chatMessageAbortControllers: Map<string, AbortController>;\n /** Array of chat messages for the current conversation */\n messages: ChatMessage[];\n constructor(ctx: AgentContext, env: Env) {\n super(ctx, env);\n this.sql`create table if not exists cf_ai_chat_agent_messages (\n id text primary key,\n message text not null,\n created_at datetime default current_timestamp\n )`;\n this.messages = (\n this.sql`select * from cf_ai_chat_agent_messages` || []\n ).map((row) => {\n return JSON.parse(row.message as string);\n });\n\n this.#chatMessageAbortControllers = new Map();\n }\n\n #broadcastChatMessage(message: OutgoingMessage, exclude?: string[]) {\n this.broadcast(JSON.stringify(message), exclude);\n }\n\n override async onMessage(connection: Connection, message: WSMessage) {\n if (typeof message === \"string\") {\n let data: IncomingMessage;\n try {\n data = JSON.parse(message) as IncomingMessage;\n } catch (error) {\n // silently ignore invalid messages for now\n // TODO: log errors with log levels\n return;\n }\n if (\n data.type === \"cf_agent_use_chat_request\" &&\n data.init.method === \"POST\"\n ) {\n const {\n method,\n keepalive,\n headers,\n body, // we're reading this\n redirect,\n integrity,\n credentials,\n mode,\n referrer,\n referrerPolicy,\n window,\n // dispatcher,\n // duplex\n } = data.init;\n const { messages } = JSON.parse(body as string);\n this.#broadcastChatMessage(\n {\n type: \"cf_agent_chat_messages\",\n messages,\n },\n [connection.id]\n );\n await this.persistMessages(messages, [connection.id]);\n\n const chatMessageId = data.id;\n const abortSignal = this.#getAbortSignal(chatMessageId);\n\n return this.#tryCatch(async () => {\n const response = await this.onChatMessage(\n async ({ response }) => {\n const finalMessages = appendResponseMessages({\n messages,\n responseMessages: response.messages,\n });\n\n await this.persistMessages(finalMessages, [connection.id]);\n this.#removeAbortController(chatMessageId);\n },\n abortSignal ? { abortSignal } : undefined\n );\n\n if (response) {\n await this.#reply(data.id, response);\n }\n });\n }\n if (data.type === \"cf_agent_chat_clear\") {\n this.#destroyAbortControllers();\n this.sql`delete from cf_ai_chat_agent_messages`;\n this.messages = [];\n this.#broadcastChatMessage(\n {\n type: \"cf_agent_chat_clear\",\n },\n [connection.id]\n );\n } else if (data.type === \"cf_agent_chat_messages\") {\n // replace the messages with the new ones\n await this.persistMessages(data.messages, [connection.id]);\n } else if (data.type === \"cf_agent_chat_request_cancel\") {\n // propagate an abort signal for the associated request\n this.#cancelChatRequest(data.id);\n }\n }\n }\n\n override async onRequest(request: Request): Promise<Response> {\n return this.#tryCatch(() => {\n const url = new URL(request.url);\n if (url.pathname.endsWith(\"/get-messages\")) {\n const messages = (\n this.sql`select * from cf_ai_chat_agent_messages` || []\n ).map((row) => {\n return JSON.parse(row.message as string);\n });\n return Response.json(messages);\n }\n return super.onRequest(request);\n });\n }\n\n async #tryCatch<T>(fn: () => T | Promise<T>) {\n try {\n return await fn();\n } catch (e) {\n throw this.onError(e);\n }\n }\n\n /**\n * Handle incoming chat messages and generate a response\n * @param onFinish Callback to be called when the response is finished\n * @param options.signal A signal to pass to any child requests which can be used to cancel them\n * @returns Response to send to the client or undefined\n */\n async onChatMessage(\n onFinish: StreamTextOnFinishCallback<ToolSet>,\n options?: { abortSignal: AbortSignal | undefined }\n ): Promise<Response | undefined> {\n throw new Error(\n \"recieved a chat message, override onChatMessage and return a Response to send to the client\"\n );\n }\n\n /**\n * Save messages on the server side and trigger AI response\n * @param messages Chat messages to save\n */\n async saveMessages(messages: ChatMessage[]) {\n await this.persistMessages(messages);\n const response = await this.onChatMessage(async ({ response }) => {\n const finalMessages = appendResponseMessages({\n messages,\n responseMessages: response.messages,\n });\n\n await this.persistMessages(finalMessages, []);\n });\n if (response) {\n // we're just going to drain the body\n // @ts-ignore TODO: fix this type error\n for await (const chunk of response.body!) {\n decoder.decode(chunk);\n }\n response.body?.cancel();\n }\n }\n\n async persistMessages(\n messages: ChatMessage[],\n excludeBroadcastIds: string[] = []\n ) {\n this.sql`delete from cf_ai_chat_agent_messages`;\n for (const message of messages) {\n this.sql`insert into cf_ai_chat_agent_messages (id, message) values (${\n message.id\n },${JSON.stringify(message)})`;\n }\n this.messages = messages;\n this.#broadcastChatMessage(\n {\n type: \"cf_agent_chat_messages\",\n messages: messages,\n },\n excludeBroadcastIds\n );\n }\n\n async #reply(id: string, response: Response) {\n // now take chunks out from dataStreamResponse and send them to the client\n return this.#tryCatch(async () => {\n // @ts-expect-error TODO: fix this type error\n for await (const chunk of response.body!) {\n const body = decoder.decode(chunk);\n\n this.#broadcastChatMessage({\n id,\n type: \"cf_agent_use_chat_response\",\n body,\n done: false,\n });\n }\n\n this.#broadcastChatMessage({\n id,\n type: \"cf_agent_use_chat_response\",\n body: \"\",\n done: true,\n });\n });\n }\n\n /**\n * For the given message id, look up its associated AbortController\n * If the AbortController does not exist, create and store one in memory\n *\n * returns the AbortSignal associated with the AbortController\n */\n #getAbortSignal(id: string): AbortSignal | undefined {\n // Defensive check, since we're coercing message types at the moment\n if (typeof id !== \"string\") {\n return undefined;\n }\n\n if (!this.#chatMessageAbortControllers.has(id)) {\n this.#chatMessageAbortControllers.set(id, new AbortController());\n }\n\n return this.#chatMessageAbortControllers.get(id)?.signal;\n }\n\n /**\n * Remove an abort controller from the cache of pending message responses\n */\n #removeAbortController(id: string) {\n this.#chatMessageAbortControllers.delete(id);\n }\n\n /**\n * Propagate an abort signal for any requests associated with the given message id\n */\n #cancelChatRequest(id: string) {\n if (this.#chatMessageAbortControllers.has(id)) {\n const abortController = this.#chatMessageAbortControllers.get(id);\n abortController?.abort();\n }\n }\n\n /**\n * Abort all pending requests and clear the cache of AbortControllers\n */\n #destroyAbortControllers() {\n for (const controller of this.#chatMessageAbortControllers.values()) {\n controller?.abort();\n }\n this.#chatMessageAbortControllers.clear();\n }\n\n /**\n * When the DO is destroyed, cancel all pending requests\n */\n async destroy() {\n this.#destroyAbortControllers();\n await super.destroy();\n }\n}\n"],"mappings":";;;;;;;;;;;;AAMA,SAAS,8BAA8B;AAGvC,IAAM,UAAU,IAAI,YAAY;AAThC;AAeO,IAAM,cAAN,cAA0D,MAG/D;AAAA,EAQA,YAAY,KAAmB,KAAU;AACvC,UAAM,KAAK,GAAG;AAZX;AAQL;AAAA;AAAA;AAAA;AAAA;AAKE,SAAK;AAAA;AAAA;AAAA;AAAA;AAKL,SAAK,YACH,KAAK,gDAAgD,CAAC,GACtD,IAAI,CAAC,QAAQ;AACb,aAAO,KAAK,MAAM,IAAI,OAAiB;AAAA,IACzC,CAAC;AAED,uBAAK,8BAA+B,oBAAI,IAAI;AAAA,EAC9C;AAAA,EAMA,MAAe,UAAU,YAAwB,SAAoB;AACnE,QAAI,OAAO,YAAY,UAAU;AAC/B,UAAI;AACJ,UAAI;AACF,eAAO,KAAK,MAAM,OAAO;AAAA,MAC3B,SAAS,OAAO;AAGd;AAAA,MACF;AACA,UACE,KAAK,SAAS,+BACd,KAAK,KAAK,WAAW,QACrB;AACA,cAAM;AAAA,UACJ;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA;AAAA;AAAA,QAGF,IAAI,KAAK;AACT,cAAM,EAAE,SAAS,IAAI,KAAK,MAAM,IAAc;AAC9C,8BAAK,iDAAL,WACE;AAAA,UACE,MAAM;AAAA,UACN;AAAA,QACF,GACA,CAAC,WAAW,EAAE;AAEhB,cAAM,KAAK,gBAAgB,UAAU,CAAC,WAAW,EAAE,CAAC;AAEpD,cAAM,gBAAg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qCAAL,WAAe,YAAY;AAEhC,qBAAiB,SAAS,SAAS,MAAO;AACxC,YAAM,OAAO,QAAQ,OAAO,KAAK;AAEjC,4BAAK,iDAAL,WAA2B;AAAA,QACzB;AAAA,QACA,MAAM;AAAA,QACN;AAAA,QACA,MAAM;AAAA,MACR;AAAA,IACF;AAEA,0BAAK,iDAAL,WAA2B;AAAA,MACzB;AAAA,MACA,MAAM;AAAA,MACN,MAAM;AAAA,MACN,MAAM;AAAA,IACR;AAAA,EACF;AACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,oBAAe,SAAC,IAAqC;AAEnD,MAAI,OAAO,OAAO,UAAU;AAC1B,WAAO;AAAA,EACT;AAEA,MAAI,CAAC,mBAAK,8BAA6B,IAAI,EAAE,GAAG;AAC9C,uBAAK,8BAA6B,IAAI,IAAI,IAAI,gBAAgB,CAAC;AAAA,EACjE;AAEA,SAAO,mBAAK,8BAA6B,IAAI,EAAE,GAAG;AACpD;AAAA;AAAA;AAAA;AAKA,2BAAsB,SAAC,IAAY;AACjC,qBAAK,8BAA6B,OAAO,EAAE;AAC7C;AAAA;AAAA;AAAA;AAKA,uBAAkB,SAAC,IAAY;AAC7B,MAAI,mBAAK,8BAA6B,IAAI,EAAE,GAAG;AAC7C,UAAM,kBAAkB,mBAAK,8BAA6B,IAAI,EAAE;AAChE,qBAAiB,MAAM;AAAA,EACzB;AACF;AAAA;AAAA;AAAA;AAKA,6BAAwB,WAAG;AACzB,aAAW,cAAc,mBAAK,8BAA6B,OAAO,GAAG;AACnE,gBAAY,MAAM;AAAA,EACpB;AACA,qBAAK,8BAA6B,MAAM;AAC1C;","names":["response"]}
package/dist/ai-react.js CHANGED
@@ -2,7 +2,8 @@ import "./chunk-HMLY7DHA.js";
2
2
 
3
3
  // src/ai-react.tsx
4
4
  import { useChat } from "@ai-sdk/react";
5
- import { useEffect, use } from "react";
5
+ import { use, useEffect } from "react";
6
+ import { nanoid } from "nanoid";
6
7
  var requestCache = /* @__PURE__ */ new Map();
7
8
  function useAgentChat(options) {
8
9
  const { agent, getInitialMessages, ...rest } = options;
@@ -32,18 +33,23 @@ function useAgentChat(options) {
32
33
  requestCache.set(agentUrlString, promise);
33
34
  return promise;
34
35
  }
35
- const initialMessages = getInitialMessages !== null ? use(
36
- doGetInitialMessages({
37
- agent: agent.agent,
38
- name: agent.name,
39
- url: agentUrlString
40
- })
41
- ) : rest.initialMessages;
36
+ const initialMessagesPromise = getInitialMessages === null ? null : doGetInitialMessages({
37
+ agent: agent.agent,
38
+ name: agent.name,
39
+ url: agentUrlString
40
+ });
41
+ const initialMessages = initialMessagesPromise ? use(initialMessagesPromise) : rest.initialMessages ?? [];
42
42
  useEffect(() => {
43
+ if (!initialMessagesPromise) {
44
+ return;
45
+ }
46
+ requestCache.set(agentUrlString, initialMessagesPromise);
43
47
  return () => {
44
- requestCache.delete(agentUrlString);
48
+ if (requestCache.get(agentUrlString) === initialMessagesPromise) {
49
+ requestCache.delete(agentUrlString);
50
+ }
45
51
  };
46
- }, [agentUrlString]);
52
+ }, [agentUrlString, initialMessagesPromise]);
47
53
  async function aiFetch(request, options2 = {}) {
48
54
  const {
49
55
  method,
@@ -60,9 +66,15 @@ function useAgentChat(options) {
60
66
  window
61
67
  // dispatcher, duplex
62
68
  } = options2;
63
- const id = crypto.randomUUID();
69
+ const id = nanoid(8);
64
70
  const abortController = new AbortController();
65
71
  signal?.addEventListener("abort", () => {
72
+ agent.send(
73
+ JSON.stringify({
74
+ type: "cf_agent_chat_request_cancel",
75
+ id
76
+ })
77
+ );
66
78
  abortController.abort();
67
79
  });
68
80
  agent.addEventListener(
@@ -157,11 +169,7 @@ function useAgentChat(options) {
157
169
  agent.removeEventListener("message", onClearHistory);
158
170
  agent.removeEventListener("message", onMessages);
159
171
  };
160
- }, [
161
- agent.addEventListener,
162
- agent.removeEventListener,
163
- useChatHelpers.setMessages
164
- ]);
172
+ }, [agent, useChatHelpers.setMessages]);
165
173
  return {
166
174
  ...useChatHelpers,
167
175
  /**
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/ai-react.tsx"],"sourcesContent":["import { useChat } from \"@ai-sdk/react\";\nimport type { Message } from \"ai\";\nimport type { useAgent } from \"./react\";\nimport { useEffect, use } from \"react\";\nimport type { OutgoingMessage } from \"./ai-types\";\n\ntype GetInitialMessagesOptions = {\n agent: string;\n name: string;\n url: string;\n};\n\n/**\n * Options for the useAgentChat hook\n */\ntype UseAgentChatOptions<State> = Omit<\n Parameters<typeof useChat>[0] & {\n /** Agent connection from useAgent */\n agent: ReturnType<typeof useAgent<State>>;\n getInitialMessages?:\n | undefined\n | null\n // | (() => Message[])\n | ((options: GetInitialMessagesOptions) => Promise<Message[]>);\n },\n \"fetch\"\n>;\n\n// TODO: clear cache when the agent is unmounted?\nconst requestCache = new Map<string, Promise<Message[]>>();\n\n/**\n * React hook for building AI chat interfaces using an Agent\n * @param options Chat options including the agent connection\n * @returns Chat interface controls and state with added clearHistory method\n */\nexport function useAgentChat<State = unknown>(\n options: UseAgentChatOptions<State>\n) {\n const { agent, getInitialMessages, ...rest } = options;\n\n const agentUrl = new URL(\n `${// @ts-expect-error we're using a protected _url property that includes query params\n ((agent._url as string | null) || agent._pkurl)\n ?.replace(\"ws://\", \"http://\")\n .replace(\"wss://\", \"https://\")}`\n );\n\n // delete the _pk query param\n agentUrl.searchParams.delete(\"_pk\");\n const agentUrlString = agentUrl.toString();\n\n async function defaultGetInitialMessagesFetch({\n url,\n }: GetInitialMessagesOptions) {\n const getMessagesUrl = new URL(url);\n getMessagesUrl.pathname += \"/get-messages\";\n const response = await fetch(getMessagesUrl.toString(), {\n headers: options.headers,\n credentials: options.credentials,\n });\n return response.json<Message[]>();\n }\n\n const getInitialMessagesFetch =\n getInitialMessages || defaultGetInitialMessagesFetch;\n\n function doGetInitialMessages(\n getInitialMessagesOptions: GetInitialMessagesOptions\n ) {\n if (requestCache.has(agentUrlString)) {\n return requestCache.get(agentUrlString)!;\n }\n const promise = getInitialMessagesFetch(getInitialMessagesOptions);\n requestCache.set(agentUrlString, promise);\n return promise;\n }\n\n const initialMessages =\n getInitialMessages !== null\n ? use(\n doGetInitialMessages({\n agent: agent.agent,\n name: agent.name,\n url: agentUrlString,\n })\n )\n : rest.initialMessages;\n\n useEffect(() => {\n return () => {\n requestCache.delete(agentUrlString);\n };\n }, [agentUrlString]);\n\n async function aiFetch(\n request: RequestInfo | URL,\n options: RequestInit = {}\n ) {\n // we're going to use a websocket to do the actual \"fetching\"\n // but still satisfy the type signature of the fetch function\n // so we'll return a promise that resolves to a response\n\n const {\n method,\n keepalive,\n headers,\n body,\n redirect,\n integrity,\n signal,\n credentials,\n mode,\n referrer,\n referrerPolicy,\n window,\n // dispatcher, duplex\n } = options;\n const id = crypto.randomUUID();\n const abortController = new AbortController();\n\n signal?.addEventListener(\"abort\", () => {\n abortController.abort();\n });\n\n agent.addEventListener(\n \"message\",\n (event) => {\n let data: OutgoingMessage;\n try {\n data = JSON.parse(event.data) as OutgoingMessage;\n } catch (error) {\n // silently ignore invalid messages for now\n // TODO: log errors with log levels\n return;\n }\n if (data.type === \"cf_agent_use_chat_response\") {\n if (data.id === id) {\n controller.enqueue(new TextEncoder().encode(data.body));\n if (data.done) {\n controller.close();\n abortController.abort();\n }\n }\n }\n },\n { signal: abortController.signal }\n );\n\n let controller: ReadableStreamDefaultController;\n\n const stream = new ReadableStream({\n start(c) {\n controller = c;\n },\n });\n\n agent.send(\n JSON.stringify({\n type: \"cf_agent_use_chat_request\",\n id,\n url: request.toString(),\n init: {\n method,\n keepalive,\n headers,\n body,\n redirect,\n integrity,\n credentials,\n mode,\n referrer,\n referrerPolicy,\n window,\n // dispatcher,\n // duplex\n },\n })\n );\n\n return new Response(stream);\n }\n const useChatHelpers = useChat({\n initialMessages,\n sendExtraMessageFields: true,\n fetch: aiFetch,\n ...rest,\n });\n\n useEffect(() => {\n function onClearHistory(event: MessageEvent) {\n if (typeof event.data !== \"string\") {\n return;\n }\n let data: OutgoingMessage;\n try {\n data = JSON.parse(event.data) as OutgoingMessage;\n } catch (error) {\n // silently ignore invalid messages for now\n // TODO: log errors with log levels\n return;\n }\n if (data.type === \"cf_agent_chat_clear\") {\n useChatHelpers.setMessages([]);\n }\n }\n\n function onMessages(event: MessageEvent) {\n if (typeof event.data !== \"string\") {\n return;\n }\n let data: OutgoingMessage;\n try {\n data = JSON.parse(event.data) as OutgoingMessage;\n } catch (error) {\n // silently ignore invalid messages for now\n // TODO: log errors with log levels\n return;\n }\n if (data.type === \"cf_agent_chat_messages\") {\n useChatHelpers.setMessages(data.messages);\n }\n }\n\n agent.addEventListener(\"message\", onClearHistory);\n agent.addEventListener(\"message\", onMessages);\n\n return () => {\n agent.removeEventListener(\"message\", onClearHistory);\n agent.removeEventListener(\"message\", onMessages);\n };\n }, [\n agent.addEventListener,\n agent.removeEventListener,\n useChatHelpers.setMessages,\n ]);\n\n return {\n ...useChatHelpers,\n /**\n * Set the chat messages and synchronize with the Agent\n * @param messages New messages to set\n */\n setMessages: (messages: Message[]) => {\n useChatHelpers.setMessages(messages);\n agent.send(\n JSON.stringify({\n type: \"cf_agent_chat_messages\",\n messages,\n })\n );\n },\n /**\n * Clear chat history on both client and Agent\n */\n clearHistory: () => {\n useChatHelpers.setMessages([]);\n agent.send(\n JSON.stringify({\n type: \"cf_agent_chat_clear\",\n })\n );\n },\n };\n}\n"],"mappings":";;;AAAA,SAAS,eAAe;AAGxB,SAAS,WAAW,WAAW;AA0B/B,IAAM,eAAe,oBAAI,IAAgC;AAOlD,SAAS,aACd,SACA;AACA,QAAM,EAAE,OAAO,oBAAoB,GAAG,KAAK,IAAI;AAE/C,QAAM,WAAW,IAAI;AAAA,IACnB;AAAA,KACE,MAAM,QAA0B,MAAM,SACpC,QAAQ,SAAS,SAAS,EAC3B,QAAQ,UAAU,UAAU,CAAC;AAAA,EAClC;AAGA,WAAS,aAAa,OAAO,KAAK;AAClC,QAAM,iBAAiB,SAAS,SAAS;AAEzC,iBAAe,+BAA+B;AAAA,IAC5C;AA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9B,SAAS,OAAO;AAGd;AAAA,MACF;AACA,UAAI,KAAK,SAAS,0BAA0B;AAC1C,uBAAe,YAAY,KAAK,QAAQ;AAAA,MAC1C;AAAA,IACF;AAEA,UAAM,iBAAiB,WAAW,cAAc;AAChD,UAAM,iBAAiB,WAAW,UAAU;AAE5C,WAAO,MAAM;AACX,YAAM,oBAAoB,WAAW,cAAc;AACnD,YAAM,oBAAoB,WAAW,UAAU;AAAA,IACjD;AAAA,EACF,GAAG;AAAA,IACD,MAAM;AAAA,IACN,MAAM;AAAA,IACN,eAAe;AAAA,EACjB,CAAC;AAED,SAAO;AAAA,IACL,GAAG;AAAA;AAAA;AAAA;AAAA;AAAA,IAKH,aAAa,CAAC,aAAwB;AACpC,qBAAe,YAAY,QAAQ;AACnC,YAAM;AAAA,QACJ,KAAK,UAAU;AAAA,UACb,MAAM;AAAA,UACN;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF;AAAA;AAAA;AAAA;AAAA,IAIA,cAAc,MAAM;AAClB,qBAAe,YAAY,CAAC,CAAC;AAC7B,YAAM;AAAA,QACJ,KAAK,UAAU;AAAA,UACb,MAAM;AAAA,QACR,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AACF;","names":["options"]}
1
+ {"version":3,"sources":["../src/ai-react.tsx"],"sourcesContent":["import { useChat } from \"@ai-sdk/react\";\nimport type { Message } from \"ai\";\nimport { use, useEffect } from \"react\";\nimport type { OutgoingMessage } from \"./ai-types\";\nimport type { useAgent } from \"./react\";\nimport { nanoid } from \"nanoid\";\n\ntype GetInitialMessagesOptions = {\n agent: string;\n name: string;\n url: string;\n};\n\n/**\n * Options for the useAgentChat hook\n */\ntype UseAgentChatOptions<State> = Omit<\n Parameters<typeof useChat>[0] & {\n /** Agent connection from useAgent */\n agent: ReturnType<typeof useAgent<State>>;\n getInitialMessages?:\n | undefined\n | null\n // | (() => Message[])\n | ((options: GetInitialMessagesOptions) => Promise<Message[]>);\n },\n \"fetch\"\n>;\n\nconst requestCache = new Map<string, Promise<Message[]>>();\n\n/**\n * React hook for building AI chat interfaces using an Agent\n * @param options Chat options including the agent connection\n * @returns Chat interface controls and state with added clearHistory method\n */\nexport function useAgentChat<State = unknown>(\n options: UseAgentChatOptions<State>\n) {\n const { agent, getInitialMessages, ...rest } = options;\n\n const agentUrl = new URL(\n `${// @ts-expect-error we're using a protected _url property that includes query params\n ((agent._url as string | null) || agent._pkurl)\n ?.replace(\"ws://\", \"http://\")\n .replace(\"wss://\", \"https://\")}`\n );\n\n // delete the _pk query param\n agentUrl.searchParams.delete(\"_pk\");\n const agentUrlString = agentUrl.toString();\n\n async function defaultGetInitialMessagesFetch({\n url,\n }: GetInitialMessagesOptions) {\n const getMessagesUrl = new URL(url);\n getMessagesUrl.pathname += \"/get-messages\";\n const response = await fetch(getMessagesUrl.toString(), {\n headers: options.headers,\n credentials: options.credentials,\n });\n return response.json<Message[]>();\n }\n\n const getInitialMessagesFetch =\n getInitialMessages || defaultGetInitialMessagesFetch;\n\n function doGetInitialMessages(\n getInitialMessagesOptions: GetInitialMessagesOptions\n ) {\n if (requestCache.has(agentUrlString)) {\n return requestCache.get(agentUrlString)!;\n }\n const promise = getInitialMessagesFetch(getInitialMessagesOptions);\n // immediately cache the promise so that we don't\n // create multiple requests for the same agent during multiple\n // concurrent renders\n requestCache.set(agentUrlString, promise);\n return promise;\n }\n\n const initialMessagesPromise =\n getInitialMessages === null\n ? null\n : doGetInitialMessages({\n agent: agent.agent,\n name: agent.name,\n url: agentUrlString,\n });\n const initialMessages = initialMessagesPromise\n ? use(initialMessagesPromise)\n : (rest.initialMessages ?? []);\n\n // manages adding and removing the promise from the cache\n useEffect(() => {\n if (!initialMessagesPromise) {\n return;\n }\n // this effect is responsible for removing the promise from the cache\n // when the component unmounts or the promise changes,\n // but that means it also must add the promise to the cache\n // so that multiple arbitrary effect runs produce the expected state\n // when resolved.\n requestCache.set(agentUrlString, initialMessagesPromise!);\n return () => {\n if (requestCache.get(agentUrlString) === initialMessagesPromise) {\n requestCache.delete(agentUrlString);\n }\n };\n }, [agentUrlString, initialMessagesPromise]);\n\n async function aiFetch(\n request: RequestInfo | URL,\n options: RequestInit = {}\n ) {\n // we're going to use a websocket to do the actual \"fetching\"\n // but still satisfy the type signature of the fetch function\n // so we'll return a promise that resolves to a response\n\n const {\n method,\n keepalive,\n headers,\n body,\n redirect,\n integrity,\n signal,\n credentials,\n mode,\n referrer,\n referrerPolicy,\n window,\n // dispatcher, duplex\n } = options;\n const id = nanoid(8);\n const abortController = new AbortController();\n\n signal?.addEventListener(\"abort\", () => {\n // Propagate request cancellation to the Agent\n // We need to communciate cancellation as a websocket message, instead of a request signal\n agent.send(\n JSON.stringify({\n type: \"cf_agent_chat_request_cancel\",\n id,\n })\n );\n\n // NOTE - If we wanted to, we could preserve the \"interrupted\" message here, with the code below\n // However, I think it might be the responsibility of the library user to implement that behavior manually?\n // Reasoning: This code could be subject to collisions, as it \"force saves\" the messages we have locally\n //\n // agent.send(JSON.stringify({\n // type: \"cf_agent_chat_messages\",\n // messages: ... /* some way of getting current messages ref? */\n // }))\n\n abortController.abort();\n });\n\n agent.addEventListener(\n \"message\",\n (event) => {\n let data: OutgoingMessage;\n try {\n data = JSON.parse(event.data) as OutgoingMessage;\n } catch (error) {\n // silently ignore invalid messages for now\n // TODO: log errors with log levels\n return;\n }\n if (data.type === \"cf_agent_use_chat_response\") {\n if (data.id === id) {\n controller.enqueue(new TextEncoder().encode(data.body));\n if (data.done) {\n controller.close();\n abortController.abort();\n }\n }\n }\n },\n { signal: abortController.signal }\n );\n\n let controller: ReadableStreamDefaultController;\n\n const stream = new ReadableStream({\n start(c) {\n controller = c;\n },\n });\n\n agent.send(\n JSON.stringify({\n type: \"cf_agent_use_chat_request\",\n id,\n url: request.toString(),\n init: {\n method,\n keepalive,\n headers,\n body,\n redirect,\n integrity,\n credentials,\n mode,\n referrer,\n referrerPolicy,\n window,\n // dispatcher,\n // duplex\n },\n })\n );\n\n return new Response(stream);\n }\n const useChatHelpers = useChat({\n initialMessages,\n sendExtraMessageFields: true,\n fetch: aiFetch,\n ...rest,\n });\n\n useEffect(() => {\n function onClearHistory(event: MessageEvent) {\n if (typeof event.data !== \"string\") {\n return;\n }\n let data: OutgoingMessage;\n try {\n data = JSON.parse(event.data) as OutgoingMessage;\n } catch (error) {\n // silently ignore invalid messages for now\n // TODO: log errors with log levels\n return;\n }\n if (data.type === \"cf_agent_chat_clear\") {\n useChatHelpers.setMessages([]);\n }\n }\n\n function onMessages(event: MessageEvent) {\n if (typeof event.data !== \"string\") {\n return;\n }\n let data: OutgoingMessage;\n try {\n data = JSON.parse(event.data) as OutgoingMessage;\n } catch (error) {\n // silently ignore invalid messages for now\n // TODO: log errors with log levels\n return;\n }\n if (data.type === \"cf_agent_chat_messages\") {\n useChatHelpers.setMessages(data.messages);\n }\n }\n\n agent.addEventListener(\"message\", onClearHistory);\n agent.addEventListener(\"message\", onMessages);\n\n return () => {\n agent.removeEventListener(\"message\", onClearHistory);\n agent.removeEventListener(\"message\", onMessages);\n };\n }, [agent, useChatHelpers.setMessages]);\n\n return {\n ...useChatHelpers,\n /**\n * Set the chat messages and synchronize with the Agent\n * @param messages New messages to set\n */\n setMessages: (messages: Message[]) => {\n useChatHelpers.setMessages(messages);\n agent.send(\n JSON.stringify({\n type: \"cf_agent_chat_messages\",\n messages,\n })\n );\n },\n /**\n * Clear chat history on both client and Agent\n */\n clearHistory: () => {\n useChatHelpers.setMessages([]);\n agent.send(\n JSON.stringify({\n type: \"cf_agent_chat_clear\",\n })\n );\n },\n };\n}\n"],"mappings":";;;AAAA,SAAS,eAAe;AAExB,SAAS,KAAK,iBAAiB;AAG/B,SAAS,cAAc;AAwBvB,IAAM,eAAe,oBAAI,IAAgC;AAOlD,SAAS,aACd,SACA;AACA,QAAM,EAAE,OAAO,oBAAoB,GAAG,KAAK,IAAI;AAE/C,QAAM,WAAW,IAAI;AAAA,IACnB;AAAA,KACE,MAAM,QAA0B,MAAM,SACpC,QAAQ,SAAS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/** Array of chat messages */
66
66
  messages: Message[];
67
+ }
68
+ | {
69
+ /** Indicates the user wants to stop generation of this message */
70
+ type: "cf_agent_chat_request_cancel";
71
+ id: string;
67
72
  };
68
73
 
69
74
  export type { IncomingMessage, OutgoingMessage };
@@ -1,3 +1,6 @@
1
+ import {
2
+ MCPClientManager
3
+ } from "./chunk-Q5ZBHY4Z.js";
1
4
  import {
2
5
  __privateAdd,
3
6
  __privateGet,
@@ -14,7 +17,6 @@ import {
14
17
  import { parseCronExpression } from "cron-schedule";
15
18
  import { nanoid } from "nanoid";
16
19
  import { AsyncLocalStorage } from "node:async_hooks";
17
- import { WorkflowEntrypoint as CFWorkflowEntrypoint } from "cloudflare:workers";
18
20
  function isRPCRequest(msg) {
19
21
  return typeof msg === "object" && msg !== null && "type" in msg && msg.type === "rpc" && "id" in msg && typeof msg.id === "string" && "method" in msg && typeof msg.method === "string" && "args" in msg && Array.isArray(msg.args);
20
22
  }
@@ -30,8 +32,6 @@ function unstable_callable(metadata = {}) {
30
32
  return target;
31
33
  };
32
34
  }
33
- var WorkflowEntrypoint = class extends CFWorkflowEntrypoint {
34
- };
35
35
  function getNextCronTime(cron) {
36
36
  const interval = parseCronExpression(cron);
37
37
  return interval.getNextDate();
@@ -39,13 +39,24 @@ function getNextCronTime(cron) {
39
39
  var STATE_ROW_ID = "cf_state_row_id";
40
40
  var STATE_WAS_CHANGED = "cf_state_was_changed";
41
41
  var DEFAULT_STATE = {};
42
- var unstable_context = new AsyncLocalStorage();
43
- var _state, _Agent_instances, setStateInternal_fn, tryCatch_fn, scheduleNextAlarm_fn, isCallable_fn;
42
+ var agentContext = new AsyncLocalStorage();
43
+ function getCurrentAgent() {
44
+ const store = agentContext.getStore();
45
+ if (!store) {
46
+ throw new Error(
47
+ "No agent context found, this means you're trying to access the current agent when none of them are running."
48
+ );
49
+ }
50
+ return store;
51
+ }
52
+ var _state, _ParentClass, _Agent_instances, setStateInternal_fn, tryCatch_fn, scheduleNextAlarm_fn, isCallable_fn;
44
53
  var Agent = class extends Server {
45
54
  constructor(ctx, env) {
46
55
  super(ctx, env);
47
56
  __privateAdd(this, _Agent_instances);
48
57
  __privateAdd(this, _state, DEFAULT_STATE);
58
+ __privateAdd(this, _ParentClass, Object.getPrototypeOf(this).constructor);
59
+ this.mcp = new MCPClientManager(__privateGet(this, _ParentClass).name, "0.0.1");
49
60
  /**
50
61
  * Initial state for the Agent
51
62
  * Override to provide default state values
@@ -76,7 +87,7 @@ var Agent = class extends Server {
76
87
  });
77
88
  const _onMessage = this.onMessage.bind(this);
78
89
  this.onMessage = async (connection, message) => {
79
- return unstable_context.run(
90
+ return agentContext.run(
80
91
  { agent: this, connection, request: void 0 },
81
92
  async () => {
82
93
  if (typeof message !== "string") {
@@ -135,7 +146,7 @@ var Agent = class extends Server {
135
146
  };
136
147
  const _onConnect = this.onConnect.bind(this);
137
148
  this.onConnect = (connection, ctx2) => {
138
- return unstable_context.run(
149
+ return agentContext.run(
139
150
  { agent: this, connection, request: ctx2.request },
140
151
  async () => {
141
152
  setTimeout(() => {
@@ -217,7 +228,7 @@ var Agent = class extends Server {
217
228
  * @param email Email message to process
218
229
  */
219
230
  onEmail(email) {
220
- return unstable_context.run(
231
+ return agentContext.run(
221
232
  { agent: this, connection: void 0, request: void 0 },
222
233
  async () => {
223
234
  console.error("onEmail not implemented");
@@ -395,7 +406,7 @@ var Agent = class extends Server {
395
406
  console.error(`callback ${row.callback} not found`);
396
407
  continue;
397
408
  }
398
- await unstable_context.run(
409
+ await agentContext.run(
399
410
  { agent: this, connection: void 0, request: void 0 },
400
411
  async () => {
401
412
  try {
@@ -430,6 +441,7 @@ var Agent = class extends Server {
430
441
  }
431
442
  };
432
443
  _state = new WeakMap();
444
+ _ParentClass = new WeakMap();
433
445
  _Agent_instances = new WeakSet();
434
446
  setStateInternal_fn = function(state, source = "server") {
435
447
  __privateSet(this, _state, state);
@@ -449,8 +461,8 @@ setStateInternal_fn = function(state, source = "server") {
449
461
  source !== "server" ? [source.id] : []
450
462
  );
451
463
  return __privateMethod(this, _Agent_instances, tryCatch_fn).call(this, () => {
452
- const { connection, request } = unstable_context.getStore() || {};
453
- return unstable_context.run(
464
+ const { connection, request } = agentContext.getStore() || {};
465
+ return agentContext.run(
454
466
  { agent: this, connection, request },
455
467
  async () => {
456
468
  return this.onStateUpdate(state, source);
@@ -584,12 +596,11 @@ _closed = new WeakMap();
584
596
 
585
597
  export {
586
598
  unstable_callable,
587
- WorkflowEntrypoint,
588
- unstable_context,
599
+ getCurrentAgent,
589
600
  Agent,
590
601
  routeAgentRequest,
591
602
  routeAgentEmail,
592
603
  getAgentByName,
593
604
  StreamingResponse
594
605
  };
595
- //# sourceMappingURL=chunk-YMUU7QHV.js.map
606
+ //# sourceMappingURL=chunk-HN5JVKAZ.js.map