agents 0.0.0-143ec31

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 ADDED
@@ -0,0 +1,428 @@
1
+ ### 🧠 `agents` - A Framework for Digital Intelligence
2
+
3
+ ![agents-header](https://github.com/user-attachments/assets/f6d99eeb-1803-4495-9c5e-3cf07a37b402)
4
+
5
+ Welcome to a new chapter in software development, where AI agents persist, think, and act with purpose. The `agents` framework creates an environment where artificial intelligence can flourish - maintaining state, engaging in meaningful interactions, and evolving over time.
6
+
7
+ _This project is in active development. Join us in shaping the future of intelligent agents._
8
+
9
+ #### The Nature of Agents
10
+
11
+ An AI agent transcends traditional software boundaries. It's an entity that:
12
+
13
+ - **Persistence**: Maintains its state and knowledge across time
14
+ - **Agency**: Acts autonomously within its defined purpose
15
+ - **Connection**: Communicates through multiple channels with both humans and other agents
16
+ - **Growth**: Learns and adapts through its interactions
17
+
18
+ Built on Cloudflare's global network, this framework provides agents with a reliable, distributed foundation where they can operate continuously and effectively.
19
+
20
+ #### 💫 Core Principles
21
+
22
+ 1. **Stateful Existence**: Each agent maintains its own persistent reality
23
+ 2. **Long-lived Presence**: Agents can run for extended periods, resting when idle
24
+ 3. **Natural Communication**: Interact through HTTP, WebSockets, or direct calls
25
+ 4. **Global Distribution**: Leverage Cloudflare's network for worldwide presence
26
+ 5. **Resource Harmony**: Efficient hibernation and awakening as needed
27
+
28
+ ---
29
+
30
+ ### 🌱 Beginning the Journey
31
+
32
+ Start with a complete environment:
33
+
34
+ ```sh
35
+ # Create a new project
36
+ npm create cloudflare@latest -- --template cloudflare/agents-starter
37
+
38
+ # Or enhance an existing one
39
+ npm install agents
40
+ ```
41
+
42
+ ### 📝 Your First Agent
43
+
44
+ Create an agent that bridges thought and action:
45
+
46
+ ```ts
47
+ import { Agent } from "agents";
48
+
49
+ export class IntelligentAgent extends Agent {
50
+ async onRequest(request) {
51
+ // Transform intention into response
52
+ return new Response("Ready to assist.");
53
+ }
54
+ }
55
+ ```
56
+
57
+ ### 🎭 Patterns of Intelligence
58
+
59
+ Agents can manifest various forms of understanding:
60
+
61
+ ```ts
62
+ import { Agent } from "agents";
63
+ import { OpenAI } from "openai";
64
+
65
+ export class AIAgent extends Agent {
66
+ async onRequest(request) {
67
+ // Connect with AI capabilities
68
+ const ai = new OpenAI({
69
+ apiKey: this.env.OPENAI_API_KEY,
70
+ });
71
+
72
+ // Process and understand
73
+ const response = await ai.chat.completions.create({
74
+ model: "gpt-4",
75
+ messages: [{ role: "user", content: await request.text() }],
76
+ });
77
+
78
+ return new Response(response.choices[0].message.content);
79
+ }
80
+
81
+ async processTask(task) {
82
+ await this.understand(task);
83
+ await this.act();
84
+ await this.reflect();
85
+ }
86
+ }
87
+ ```
88
+
89
+ ### 🏰 Creating Space
90
+
91
+ Define your agent's domain:
92
+
93
+ ```jsonc
94
+ {
95
+ "durable_objects": {
96
+ "bindings": [
97
+ {
98
+ "name": "AIAgent",
99
+ "class_name": "AIAgent",
100
+ },
101
+ ],
102
+ },
103
+ "migrations": [
104
+ {
105
+ "tag": "v1",
106
+ // Mandatory for the Agent to store state
107
+ "new_sqlite_classes": ["AIAgent"],
108
+ },
109
+ ],
110
+ }
111
+ ```
112
+
113
+ ### 🌐 Lifecycle
114
+
115
+ Bring your agent into being:
116
+
117
+ ```ts
118
+ // Create a new instance
119
+ const id = env.AIAgent.newUniqueId();
120
+ const agent = env.AIAgent.get(id);
121
+
122
+ // Initialize with purpose
123
+ await agent.processTask({
124
+ type: "analysis",
125
+ context: "incoming_data",
126
+ parameters: initialConfig,
127
+ });
128
+
129
+ // Or reconnect with an existing one
130
+ const existingAgent = await getAgentByName(env.AIAgent, "data-analyzer");
131
+ ```
132
+
133
+ ### 🔄 Paths of Communication
134
+
135
+ #### HTTP Understanding
136
+
137
+ Process and respond to direct requests:
138
+
139
+ ```ts
140
+ export class APIAgent extends Agent {
141
+ async onRequest(request) {
142
+ const data = await request.json();
143
+
144
+ return Response.json({
145
+ insight: await this.process(data),
146
+ moment: Date.now(),
147
+ });
148
+ }
149
+ }
150
+ ```
151
+
152
+ #### Persistent Connections
153
+
154
+ Maintain ongoing dialogues through WebSocket:
155
+
156
+ ```ts
157
+ export class DialogueAgent extends Agent {
158
+ async onConnect(connection) {
159
+ await this.initiate(connection);
160
+ }
161
+
162
+ async onMessage(connection, message) {
163
+ const understanding = await this.comprehend(message);
164
+ await this.respond(connection, understanding);
165
+ }
166
+ }
167
+ ```
168
+
169
+ #### Client Communion
170
+
171
+ For direct connection to your agent:
172
+
173
+ ```ts
174
+ import { AgentClient } from "agents/client";
175
+
176
+ const connection = new AgentClient({
177
+ agent: "dialogue-agent",
178
+ name: "insight-seeker",
179
+ });
180
+
181
+ connection.addEventListener("message", (event) => {
182
+ console.log("Received:", event.data);
183
+ });
184
+
185
+ connection.send(
186
+ JSON.stringify({
187
+ type: "inquiry",
188
+ content: "What patterns do you see?",
189
+ })
190
+ );
191
+ ```
192
+
193
+ #### React Integration
194
+
195
+ For harmonious integration with React:
196
+
197
+ ```tsx
198
+ import { useAgent } from "agents/react";
199
+
200
+ function AgentInterface() {
201
+ const connection = useAgent({
202
+ agent: "dialogue-agent",
203
+ name: "insight-seeker",
204
+ onMessage: (message) => {
205
+ console.log("Understanding received:", message.data);
206
+ },
207
+ onOpen: () => console.log("Connection established"),
208
+ onClose: () => console.log("Connection closed"),
209
+ });
210
+
211
+ const inquire = () => {
212
+ connection.send(
213
+ JSON.stringify({
214
+ type: "inquiry",
215
+ content: "What insights have you gathered?",
216
+ })
217
+ );
218
+ };
219
+
220
+ return (
221
+ <div className="agent-interface">
222
+ <button onClick={inquire}>Seek Understanding</button>
223
+ </div>
224
+ );
225
+ }
226
+ ```
227
+
228
+ ### 🌊 Flow of State
229
+
230
+ Maintain and evolve your agent's understanding:
231
+
232
+ ```ts
233
+ export class ThinkingAgent extends Agent {
234
+ async evolve(newInsight) {
235
+ this.setState({
236
+ ...this.state,
237
+ insights: [...(this.state.insights || []), newInsight],
238
+ understanding: this.state.understanding + 1,
239
+ });
240
+ }
241
+
242
+ onStateUpdate(state, source) {
243
+ console.log("Understanding deepened:", {
244
+ newState: state,
245
+ origin: source,
246
+ });
247
+ }
248
+ }
249
+ ```
250
+
251
+ Connect to your agent's state from React:
252
+
253
+ ```tsx
254
+ import { useState } from "react";
255
+ import { useAgent } from "agents/react";
256
+
257
+ function StateInterface() {
258
+ const [state, setState] = useState({ counter: 0 });
259
+
260
+ const agent = useAgent({
261
+ agent: "thinking-agent",
262
+ onStateUpdate: (newState) => setState(newState),
263
+ });
264
+
265
+ const increment = () => {
266
+ agent.setState({ counter: state.counter + 1 });
267
+ };
268
+
269
+ return (
270
+ <div>
271
+ <div>Count: {state.counter}</div>
272
+ <button onClick={increment}>Increment</button>
273
+ </div>
274
+ );
275
+ }
276
+ ```
277
+
278
+ This creates a synchronized state that automatically updates across all connected clients.
279
+
280
+ ### ⏳ Temporal Patterns
281
+
282
+ Schedule moments of action and reflection:
283
+
284
+ ```ts
285
+ export class TimeAwareAgent extends Agent {
286
+ async initialize() {
287
+ // Quick reflection
288
+ this.schedule(10, "quickInsight", { focus: "patterns" });
289
+
290
+ // Daily synthesis
291
+ this.schedule("0 0 * * *", "dailySynthesis", {
292
+ depth: "comprehensive",
293
+ });
294
+
295
+ // Milestone review
296
+ this.schedule(new Date("2024-12-31"), "yearlyAnalysis");
297
+ }
298
+
299
+ async quickInsight(data) {
300
+ await this.analyze(data.focus);
301
+ }
302
+
303
+ async dailySynthesis(data) {
304
+ await this.synthesize(data.depth);
305
+ }
306
+
307
+ async yearlyAnalysis() {
308
+ await this.analyze();
309
+ }
310
+ }
311
+ ```
312
+
313
+ ### 💬 AI Dialogue
314
+
315
+ Create meaningful conversations with intelligence:
316
+
317
+ ```ts
318
+ import { AIChatAgent } from "agents/ai-chat-agent";
319
+ import { openai } from "@ai-sdk/openai";
320
+
321
+ export class DialogueAgent extends AIChatAgent {
322
+ async onChatMessage(onFinish) {
323
+ return createDataStreamResponse({
324
+ execute: async (dataStream) => {
325
+ const stream = streamText({
326
+ model: openai("gpt-4o"),
327
+ messages: this.messages,
328
+ onFinish, // call onFinish so that messages get saved
329
+ });
330
+
331
+ stream.mergeIntoDataStream(dataStream);
332
+ },
333
+ });
334
+ }
335
+ }
336
+ ```
337
+
338
+ #### Creating the Interface
339
+
340
+ Connect with your agent through a React interface:
341
+
342
+ ```tsx
343
+ import { useAgent } from "agents/react";
344
+ import { useAgentChat } from "agents/ai-react";
345
+
346
+ function ChatInterface() {
347
+ // Connect to the agent
348
+ const agent = useAgent({
349
+ agent: "dialogue-agent",
350
+ });
351
+
352
+ // Set up the chat interaction
353
+ const { messages, input, handleInputChange, handleSubmit, clearHistory } =
354
+ useAgentChat({
355
+ agent,
356
+ maxSteps: 5,
357
+ });
358
+
359
+ return (
360
+ <div className="chat-interface">
361
+ {/* Message History */}
362
+ <div className="message-flow">
363
+ {messages.map((message) => (
364
+ <div key={message.id} className="message">
365
+ <div className="role">{message.role}</div>
366
+ <div className="content">{message.content}</div>
367
+ </div>
368
+ ))}
369
+ </div>
370
+
371
+ {/* Input Area */}
372
+ <form onSubmit={handleSubmit} className="input-area">
373
+ <input
374
+ value={input}
375
+ onChange={handleInputChange}
376
+ placeholder="Type your message..."
377
+ className="message-input"
378
+ />
379
+ </form>
380
+
381
+ <button onClick={clearHistory} className="clear-button">
382
+ Clear Chat
383
+ </button>
384
+ </div>
385
+ );
386
+ }
387
+ ```
388
+
389
+ This creates:
390
+
391
+ - Real-time message streaming
392
+ - Simple message history
393
+ - Intuitive input handling
394
+ - Easy conversation reset
395
+
396
+ ### 💬 The Path Forward
397
+
398
+ We're developing new dimensions of agent capability:
399
+
400
+ #### Enhanced Understanding
401
+
402
+ - **WebRTC Perception**: Audio and video communication channels
403
+ - **Email Discourse**: Automated email interaction and response
404
+ - **Deep Memory**: Long-term context and relationship understanding
405
+
406
+ #### Development Insights
407
+
408
+ - **Evaluation Framework**: Understanding agent effectiveness
409
+ - **Clear Sight**: Deep visibility into agent processes
410
+ - **Private Realms**: Complete self-hosting guide
411
+
412
+ These capabilities will expand your agents' potential while maintaining their reliability and purpose.
413
+
414
+ Welcome to the future of intelligent agents. Create something meaningful. 🌟
415
+
416
+ ### Contributing
417
+
418
+ Contributions are welcome, but are especially welcome when:
419
+
420
+ - You have opened an issue as a Request for Comment (RFC) to discuss your proposal, show your thinking, and iterate together.
421
+ - Is not "AI slop": LLMs are powerful tools, but contributions entirely authored by vibe coding are unlikely to meet the quality bar, and will be rejected.
422
+ - You're willing to accept feedback and make sure the changes fit the goals of the `agents` sdk. Not everything will, and that's OK.
423
+
424
+ Small fixes, type bugs, and documentation improvements can be raised directly as PRs.
425
+
426
+ ### License
427
+
428
+ MIT licensed. See the LICENSE file at the root of this repository for details.
@@ -0,0 +1,39 @@
1
+ import { Agent, AgentContext } from "./index.js";
2
+ import { Message, StreamTextOnFinishCallback, ToolSet } from "ai";
3
+ import { Connection, WSMessage } from "partyserver";
4
+ import "node:async_hooks";
5
+
6
+ /**
7
+ * Extension of Agent with built-in chat capabilities
8
+ * @template Env Environment type containing bindings
9
+ */
10
+ declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<
11
+ Env,
12
+ State
13
+ > {
14
+ #private;
15
+ /** Array of chat messages for the current conversation */
16
+ messages: Message[];
17
+ constructor(ctx: AgentContext, env: Env);
18
+ onMessage(connection: Connection, message: WSMessage): Promise<void>;
19
+ onRequest(request: Request): Promise<Response>;
20
+ /**
21
+ * Handle incoming chat messages and generate a response
22
+ * @param onFinish Callback to be called when the response is finished
23
+ * @returns Response to send to the client or undefined
24
+ */
25
+ onChatMessage(
26
+ onFinish: StreamTextOnFinishCallback<ToolSet>
27
+ ): Promise<Response | undefined>;
28
+ /**
29
+ * Save messages on the server side and trigger AI response
30
+ * @param messages Chat messages to save
31
+ */
32
+ saveMessages(messages: Message[]): Promise<void>;
33
+ persistMessages(
34
+ messages: Message[],
35
+ excludeBroadcastIds?: string[]
36
+ ): Promise<void>;
37
+ }
38
+
39
+ export { AIChatAgent };
@@ -0,0 +1,168 @@
1
+ import {
2
+ Agent
3
+ } from "./chunk-XG52S6YY.js";
4
+ import {
5
+ __privateAdd,
6
+ __privateMethod
7
+ } from "./chunk-HMLY7DHA.js";
8
+
9
+ // src/ai-chat-agent.ts
10
+ import { appendResponseMessages } from "ai";
11
+ var decoder = new TextDecoder();
12
+ var _AIChatAgent_instances, broadcastChatMessage_fn, tryCatch_fn, reply_fn;
13
+ var AIChatAgent = class extends Agent {
14
+ constructor(ctx, env) {
15
+ super(ctx, env);
16
+ __privateAdd(this, _AIChatAgent_instances);
17
+ this.sql`create table if not exists cf_ai_chat_agent_messages (
18
+ id text primary key,
19
+ message text not null,
20
+ created_at datetime default current_timestamp
21
+ )`;
22
+ this.messages = (this.sql`select * from cf_ai_chat_agent_messages` || []).map((row) => {
23
+ return JSON.parse(row.message);
24
+ });
25
+ }
26
+ async onMessage(connection, message) {
27
+ if (typeof message === "string") {
28
+ let data;
29
+ try {
30
+ data = JSON.parse(message);
31
+ } catch (error) {
32
+ return;
33
+ }
34
+ if (data.type === "cf_agent_use_chat_request" && data.init.method === "POST") {
35
+ const {
36
+ method,
37
+ keepalive,
38
+ headers,
39
+ body,
40
+ // we're reading this
41
+ redirect,
42
+ integrity,
43
+ credentials,
44
+ mode,
45
+ referrer,
46
+ referrerPolicy,
47
+ window
48
+ // dispatcher,
49
+ // duplex
50
+ } = data.init;
51
+ const { messages } = JSON.parse(body);
52
+ __privateMethod(this, _AIChatAgent_instances, broadcastChatMessage_fn).call(this, {
53
+ type: "cf_agent_chat_messages",
54
+ messages
55
+ }, [connection.id]);
56
+ await this.persistMessages(messages, [connection.id]);
57
+ 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 this.persistMessages(finalMessages, [connection.id]);
64
+ });
65
+ if (response) {
66
+ await __privateMethod(this, _AIChatAgent_instances, reply_fn).call(this, data.id, response);
67
+ }
68
+ });
69
+ }
70
+ if (data.type === "cf_agent_chat_clear") {
71
+ this.sql`delete from cf_ai_chat_agent_messages`;
72
+ this.messages = [];
73
+ __privateMethod(this, _AIChatAgent_instances, broadcastChatMessage_fn).call(this, {
74
+ type: "cf_agent_chat_clear"
75
+ }, [connection.id]);
76
+ } else if (data.type === "cf_agent_chat_messages") {
77
+ await this.persistMessages(data.messages, [connection.id]);
78
+ }
79
+ }
80
+ }
81
+ async onRequest(request) {
82
+ return __privateMethod(this, _AIChatAgent_instances, tryCatch_fn).call(this, () => {
83
+ const url = new URL(request.url);
84
+ if (url.pathname.endsWith("/get-messages")) {
85
+ const messages = (this.sql`select * from cf_ai_chat_agent_messages` || []).map((row) => {
86
+ return JSON.parse(row.message);
87
+ });
88
+ return Response.json(messages);
89
+ }
90
+ return super.onRequest(request);
91
+ });
92
+ }
93
+ /**
94
+ * Handle incoming chat messages and generate a response
95
+ * @param onFinish Callback to be called when the response is finished
96
+ * @returns Response to send to the client or undefined
97
+ */
98
+ async onChatMessage(onFinish) {
99
+ throw new Error(
100
+ "recieved a chat message, override onChatMessage and return a Response to send to the client"
101
+ );
102
+ }
103
+ /**
104
+ * Save messages on the server side and trigger AI response
105
+ * @param messages Chat messages to save
106
+ */
107
+ async saveMessages(messages) {
108
+ await this.persistMessages(messages);
109
+ const response = await this.onChatMessage(async ({ response: response2 }) => {
110
+ const finalMessages = appendResponseMessages({
111
+ messages,
112
+ responseMessages: response2.messages
113
+ });
114
+ await this.persistMessages(finalMessages, []);
115
+ });
116
+ if (response) {
117
+ for await (const chunk of response.body) {
118
+ decoder.decode(chunk);
119
+ }
120
+ response.body?.cancel();
121
+ }
122
+ }
123
+ async persistMessages(messages, excludeBroadcastIds = []) {
124
+ this.sql`delete from cf_ai_chat_agent_messages`;
125
+ for (const message of messages) {
126
+ this.sql`insert into cf_ai_chat_agent_messages (id, message) values (${message.id},${JSON.stringify(message)})`;
127
+ }
128
+ this.messages = messages;
129
+ __privateMethod(this, _AIChatAgent_instances, broadcastChatMessage_fn).call(this, {
130
+ type: "cf_agent_chat_messages",
131
+ messages
132
+ }, excludeBroadcastIds);
133
+ }
134
+ };
135
+ _AIChatAgent_instances = new WeakSet();
136
+ broadcastChatMessage_fn = function(message, exclude) {
137
+ this.broadcast(JSON.stringify(message), exclude);
138
+ };
139
+ tryCatch_fn = async function(fn) {
140
+ try {
141
+ return await fn();
142
+ } catch (e) {
143
+ throw this.onError(e);
144
+ }
145
+ };
146
+ reply_fn = async function(id, response) {
147
+ return __privateMethod(this, _AIChatAgent_instances, tryCatch_fn).call(this, async () => {
148
+ for await (const chunk of response.body) {
149
+ const body = decoder.decode(chunk);
150
+ __privateMethod(this, _AIChatAgent_instances, broadcastChatMessage_fn).call(this, {
151
+ id,
152
+ type: "cf_agent_use_chat_response",
153
+ body,
154
+ done: false
155
+ });
156
+ }
157
+ __privateMethod(this, _AIChatAgent_instances, broadcastChatMessage_fn).call(this, {
158
+ id,
159
+ type: "cf_agent_use_chat_response",
160
+ body: "",
161
+ done: true
162
+ });
163
+ });
164
+ };
165
+ export {
166
+ AIChatAgent
167
+ };
168
+ //# sourceMappingURL=ai-chat-agent.js.map
@@ -0,0 +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,I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