agents 0.0.0-f641104 → 0.0.0-f6c26e4
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 +24 -28
- package/dist/ai-chat-agent.d.ts +51 -4
- package/dist/ai-chat-agent.js +179 -78
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +18 -5
- package/dist/ai-react.js +62 -48
- package/dist/ai-react.js.map +1 -1
- package/dist/ai-types.d.ts +5 -0
- package/dist/chunk-5YIRLLUX.js +1264 -0
- package/dist/chunk-5YIRLLUX.js.map +1 -0
- package/dist/chunk-KUH345EY.js +116 -0
- package/dist/chunk-KUH345EY.js.map +1 -0
- package/dist/chunk-MW5BQ2FW.js +469 -0
- package/dist/chunk-MW5BQ2FW.js.map +1 -0
- package/dist/chunk-PVQZBKN7.js +106 -0
- package/dist/chunk-PVQZBKN7.js.map +1 -0
- package/dist/client.d.ts +16 -2
- package/dist/client.js +6 -133
- package/dist/client.js.map +1 -1
- package/dist/index-BIJvkfYt.d.ts +614 -0
- package/dist/index.d.ts +37 -300
- package/dist/index.js +12 -4
- package/dist/mcp/client.d.ts +1055 -0
- package/dist/mcp/client.js +9 -0
- package/dist/mcp/do-oauth-client-provider.d.ts +41 -0
- package/dist/mcp/do-oauth-client-provider.js +7 -0
- package/dist/mcp/do-oauth-client-provider.js.map +1 -0
- package/dist/mcp/index.d.ts +84 -0
- package/dist/mcp/index.js +783 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/observability/index.d.ts +12 -0
- package/dist/observability/index.js +10 -0
- package/dist/observability/index.js.map +1 -0
- package/dist/react.d.ts +85 -5
- package/dist/react.js +50 -31
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +6 -6
- package/dist/schedule.js +4 -6
- package/dist/schedule.js.map +1 -1
- package/dist/serializable.d.ts +32 -0
- package/dist/serializable.js +1 -0
- package/dist/serializable.js.map +1 -0
- package/package.json +84 -52
- package/src/index.ts +1172 -182
- package/dist/chunk-HMLY7DHA.js +0 -16
- package/dist/chunk-X6BBKLSC.js +0 -568
- package/dist/chunk-X6BBKLSC.js.map +0 -1
- package/dist/mcp.d.ts +0 -58
- package/dist/mcp.js +0 -945
- package/dist/mcp.js.map +0 -1
- /package/dist/{chunk-HMLY7DHA.js.map → mcp/client.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
|
|
@@ -316,24 +316,20 @@ Create meaningful conversations with intelligence:
|
|
|
316
316
|
|
|
317
317
|
```ts
|
|
318
318
|
import { AIChatAgent } from "agents/ai-chat-agent";
|
|
319
|
-
import {
|
|
319
|
+
import { openai } from "@ai-sdk/openai";
|
|
320
320
|
|
|
321
321
|
export class DialogueAgent extends AIChatAgent {
|
|
322
322
|
async onChatMessage(onFinish) {
|
|
323
323
|
return createDataStreamResponse({
|
|
324
324
|
execute: async (dataStream) => {
|
|
325
|
-
const ai = createOpenAI({
|
|
326
|
-
apiKey: this.env.OPENAI_API_KEY,
|
|
327
|
-
});
|
|
328
|
-
|
|
329
325
|
const stream = streamText({
|
|
330
|
-
model:
|
|
326
|
+
model: openai("gpt-4o"),
|
|
331
327
|
messages: this.messages,
|
|
332
|
-
onFinish
|
|
328
|
+
onFinish // call onFinish so that messages get saved
|
|
333
329
|
});
|
|
334
330
|
|
|
335
331
|
stream.mergeIntoDataStream(dataStream);
|
|
336
|
-
}
|
|
332
|
+
}
|
|
337
333
|
});
|
|
338
334
|
}
|
|
339
335
|
}
|
|
@@ -350,14 +346,14 @@ import { useAgentChat } from "agents/ai-react";
|
|
|
350
346
|
function ChatInterface() {
|
|
351
347
|
// Connect to the agent
|
|
352
348
|
const agent = useAgent({
|
|
353
|
-
agent: "dialogue-agent"
|
|
349
|
+
agent: "dialogue-agent"
|
|
354
350
|
});
|
|
355
351
|
|
|
356
352
|
// Set up the chat interaction
|
|
357
353
|
const { messages, input, handleInputChange, handleSubmit, clearHistory } =
|
|
358
354
|
useAgentChat({
|
|
359
355
|
agent,
|
|
360
|
-
maxSteps: 5
|
|
356
|
+
maxSteps: 5
|
|
361
357
|
});
|
|
362
358
|
|
|
363
359
|
return (
|
package/dist/ai-chat-agent.d.ts
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
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-BIJvkfYt.js";
|
|
3
3
|
import { Connection, WSMessage } from "partyserver";
|
|
4
|
-
import "
|
|
4
|
+
import "@modelcontextprotocol/sdk/client/index.js";
|
|
5
|
+
import "@modelcontextprotocol/sdk/types.js";
|
|
6
|
+
import "./mcp/client.js";
|
|
7
|
+
import "zod";
|
|
8
|
+
import "@modelcontextprotocol/sdk/client/sse.js";
|
|
9
|
+
import "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
10
|
+
import "./mcp/do-oauth-client-provider.js";
|
|
11
|
+
import "@modelcontextprotocol/sdk/client/auth.js";
|
|
12
|
+
import "@modelcontextprotocol/sdk/shared/auth.js";
|
|
5
13
|
|
|
6
14
|
/**
|
|
7
15
|
* Extension of Agent with built-in chat capabilities
|
|
@@ -11,25 +19,64 @@ declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<
|
|
|
11
19
|
Env,
|
|
12
20
|
State
|
|
13
21
|
> {
|
|
14
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Map of message `id`s to `AbortController`s
|
|
24
|
+
* useful to propagate request cancellation signals for any external calls made by the agent
|
|
25
|
+
*/
|
|
26
|
+
private _chatMessageAbortControllers;
|
|
15
27
|
/** Array of chat messages for the current conversation */
|
|
16
28
|
messages: Message[];
|
|
17
29
|
constructor(ctx: AgentContext, env: Env);
|
|
30
|
+
private _broadcastChatMessage;
|
|
18
31
|
onMessage(connection: Connection, message: WSMessage): Promise<void>;
|
|
19
32
|
onRequest(request: Request): Promise<Response>;
|
|
33
|
+
private _tryCatchChat;
|
|
20
34
|
/**
|
|
21
35
|
* Handle incoming chat messages and generate a response
|
|
22
36
|
* @param onFinish Callback to be called when the response is finished
|
|
37
|
+
* @param options.signal A signal to pass to any child requests which can be used to cancel them
|
|
23
38
|
* @returns Response to send to the client or undefined
|
|
24
39
|
*/
|
|
25
40
|
onChatMessage(
|
|
26
|
-
onFinish: StreamTextOnFinishCallback<ToolSet
|
|
41
|
+
onFinish: StreamTextOnFinishCallback<ToolSet>,
|
|
42
|
+
options?: {
|
|
43
|
+
abortSignal: AbortSignal | undefined;
|
|
44
|
+
}
|
|
27
45
|
): Promise<Response | undefined>;
|
|
28
46
|
/**
|
|
29
47
|
* Save messages on the server side and trigger AI response
|
|
30
48
|
* @param messages Chat messages to save
|
|
31
49
|
*/
|
|
32
50
|
saveMessages(messages: Message[]): Promise<void>;
|
|
51
|
+
persistMessages(
|
|
52
|
+
messages: Message[],
|
|
53
|
+
excludeBroadcastIds?: string[]
|
|
54
|
+
): Promise<void>;
|
|
55
|
+
private _messagesNotAlreadyInAgent;
|
|
56
|
+
private _reply;
|
|
57
|
+
/**
|
|
58
|
+
* For the given message id, look up its associated AbortController
|
|
59
|
+
* If the AbortController does not exist, create and store one in memory
|
|
60
|
+
*
|
|
61
|
+
* returns the AbortSignal associated with the AbortController
|
|
62
|
+
*/
|
|
63
|
+
private _getAbortSignal;
|
|
64
|
+
/**
|
|
65
|
+
* Remove an abort controller from the cache of pending message responses
|
|
66
|
+
*/
|
|
67
|
+
private _removeAbortController;
|
|
68
|
+
/**
|
|
69
|
+
* Propagate an abort signal for any requests associated with the given message id
|
|
70
|
+
*/
|
|
71
|
+
private _cancelChatRequest;
|
|
72
|
+
/**
|
|
73
|
+
* Abort all pending requests and clear the cache of AbortControllers
|
|
74
|
+
*/
|
|
75
|
+
private _destroyAbortControllers;
|
|
76
|
+
/**
|
|
77
|
+
* When the DO is destroyed, cancel all pending requests
|
|
78
|
+
*/
|
|
79
|
+
destroy(): Promise<void>;
|
|
33
80
|
}
|
|
34
81
|
|
|
35
82
|
export { AIChatAgent };
|
package/dist/ai-chat-agent.js
CHANGED
|
@@ -1,19 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Agent
|
|
3
|
-
} from "./chunk-
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
} from "./chunk-HMLY7DHA.js";
|
|
3
|
+
} from "./chunk-5YIRLLUX.js";
|
|
4
|
+
import "./chunk-MW5BQ2FW.js";
|
|
5
|
+
import "./chunk-PVQZBKN7.js";
|
|
6
|
+
import "./chunk-KUH345EY.js";
|
|
8
7
|
|
|
9
8
|
// src/ai-chat-agent.ts
|
|
10
9
|
import { appendResponseMessages } from "ai";
|
|
11
10
|
var decoder = new TextDecoder();
|
|
12
|
-
var _AIChatAgent_instances, broadcastChatMessage_fn, tryCatch_fn, persistMessages_fn, reply_fn;
|
|
13
11
|
var AIChatAgent = class extends Agent {
|
|
14
12
|
constructor(ctx, env) {
|
|
15
13
|
super(ctx, env);
|
|
16
|
-
__privateAdd(this, _AIChatAgent_instances);
|
|
17
14
|
this.sql`create table if not exists cf_ai_chat_agent_messages (
|
|
18
15
|
id text primary key,
|
|
19
16
|
message text not null,
|
|
@@ -22,65 +19,119 @@ var AIChatAgent = class extends Agent {
|
|
|
22
19
|
this.messages = (this.sql`select * from cf_ai_chat_agent_messages` || []).map((row) => {
|
|
23
20
|
return JSON.parse(row.message);
|
|
24
21
|
});
|
|
22
|
+
this._chatMessageAbortControllers = /* @__PURE__ */ new Map();
|
|
23
|
+
}
|
|
24
|
+
_broadcastChatMessage(message, exclude) {
|
|
25
|
+
this.broadcast(JSON.stringify(message), exclude);
|
|
25
26
|
}
|
|
26
27
|
async onMessage(connection, message) {
|
|
27
28
|
if (typeof message === "string") {
|
|
28
29
|
let data;
|
|
29
30
|
try {
|
|
30
31
|
data = JSON.parse(message);
|
|
31
|
-
} catch (
|
|
32
|
+
} catch (_error) {
|
|
32
33
|
return;
|
|
33
34
|
}
|
|
34
35
|
if (data.type === "cf_agent_use_chat_request" && data.init.method === "POST") {
|
|
35
36
|
const {
|
|
36
|
-
method,
|
|
37
|
-
keepalive,
|
|
38
|
-
headers,
|
|
39
|
-
body
|
|
37
|
+
// method,
|
|
38
|
+
// keepalive,
|
|
39
|
+
// headers,
|
|
40
|
+
body
|
|
40
41
|
// we're reading this
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
credentials,
|
|
44
|
-
mode,
|
|
45
|
-
referrer,
|
|
46
|
-
referrerPolicy,
|
|
47
|
-
window
|
|
42
|
+
//
|
|
43
|
+
// // these might not exist?
|
|
48
44
|
// dispatcher,
|
|
49
45
|
// duplex
|
|
50
46
|
} = data.init;
|
|
51
47
|
const { messages } = JSON.parse(body);
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
48
|
+
this._broadcastChatMessage(
|
|
49
|
+
{
|
|
50
|
+
messages,
|
|
51
|
+
type: "cf_agent_chat_messages"
|
|
52
|
+
},
|
|
53
|
+
[connection.id]
|
|
54
|
+
);
|
|
55
|
+
const incomingMessages = this._messagesNotAlreadyInAgent(messages);
|
|
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
|
+
);
|
|
69
|
+
const chatMessageId = data.id;
|
|
70
|
+
const abortSignal = this._getAbortSignal(chatMessageId);
|
|
71
|
+
return this._tryCatchChat(async () => {
|
|
72
|
+
const response = await this.onChatMessage(
|
|
73
|
+
async ({ response: response2 }) => {
|
|
74
|
+
const finalMessages = appendResponseMessages({
|
|
75
|
+
messages,
|
|
76
|
+
responseMessages: response2.messages
|
|
77
|
+
});
|
|
78
|
+
const outgoingMessages = this._messagesNotAlreadyInAgent(finalMessages);
|
|
79
|
+
await this.persistMessages(finalMessages, [connection.id]);
|
|
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
|
+
);
|
|
93
|
+
},
|
|
94
|
+
abortSignal ? { abortSignal } : void 0
|
|
95
|
+
);
|
|
65
96
|
if (response) {
|
|
66
|
-
await
|
|
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
|
+
);
|
|
67
111
|
}
|
|
68
112
|
});
|
|
69
113
|
}
|
|
70
114
|
if (data.type === "cf_agent_chat_clear") {
|
|
115
|
+
this._destroyAbortControllers();
|
|
71
116
|
this.sql`delete from cf_ai_chat_agent_messages`;
|
|
72
117
|
this.messages = [];
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
118
|
+
this._broadcastChatMessage(
|
|
119
|
+
{
|
|
120
|
+
type: "cf_agent_chat_clear"
|
|
121
|
+
},
|
|
122
|
+
[connection.id]
|
|
123
|
+
);
|
|
76
124
|
} else if (data.type === "cf_agent_chat_messages") {
|
|
77
|
-
await
|
|
125
|
+
await this.persistMessages(data.messages, [connection.id]);
|
|
126
|
+
} else if (data.type === "cf_agent_chat_request_cancel") {
|
|
127
|
+
this._cancelChatRequest(data.id);
|
|
78
128
|
}
|
|
79
129
|
}
|
|
80
130
|
}
|
|
81
131
|
async onRequest(request) {
|
|
82
|
-
return
|
|
83
|
-
|
|
132
|
+
return this._tryCatchChat(() => {
|
|
133
|
+
const url = new URL(request.url);
|
|
134
|
+
if (url.pathname.endsWith("/get-messages")) {
|
|
84
135
|
const messages = (this.sql`select * from cf_ai_chat_agent_messages` || []).map((row) => {
|
|
85
136
|
return JSON.parse(row.message);
|
|
86
137
|
});
|
|
@@ -89,12 +140,20 @@ var AIChatAgent = class extends Agent {
|
|
|
89
140
|
return super.onRequest(request);
|
|
90
141
|
});
|
|
91
142
|
}
|
|
143
|
+
async _tryCatchChat(fn) {
|
|
144
|
+
try {
|
|
145
|
+
return await fn();
|
|
146
|
+
} catch (e) {
|
|
147
|
+
throw this.onError(e);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
92
150
|
/**
|
|
93
151
|
* Handle incoming chat messages and generate a response
|
|
94
152
|
* @param onFinish Callback to be called when the response is finished
|
|
153
|
+
* @param options.signal A signal to pass to any child requests which can be used to cancel them
|
|
95
154
|
* @returns Response to send to the client or undefined
|
|
96
155
|
*/
|
|
97
|
-
async onChatMessage(onFinish) {
|
|
156
|
+
async onChatMessage(onFinish, options) {
|
|
98
157
|
throw new Error(
|
|
99
158
|
"recieved a chat message, override onChatMessage and return a Response to send to the client"
|
|
100
159
|
);
|
|
@@ -104,13 +163,13 @@ var AIChatAgent = class extends Agent {
|
|
|
104
163
|
* @param messages Chat messages to save
|
|
105
164
|
*/
|
|
106
165
|
async saveMessages(messages) {
|
|
107
|
-
await
|
|
166
|
+
await this.persistMessages(messages);
|
|
108
167
|
const response = await this.onChatMessage(async ({ response: response2 }) => {
|
|
109
168
|
const finalMessages = appendResponseMessages({
|
|
110
169
|
messages,
|
|
111
170
|
responseMessages: response2.messages
|
|
112
171
|
});
|
|
113
|
-
await
|
|
172
|
+
await this.persistMessages(finalMessages, []);
|
|
114
173
|
});
|
|
115
174
|
if (response) {
|
|
116
175
|
for await (const chunk of response.body) {
|
|
@@ -119,47 +178,89 @@ var AIChatAgent = class extends Agent {
|
|
|
119
178
|
response.body?.cancel();
|
|
120
179
|
}
|
|
121
180
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
181
|
+
async persistMessages(messages, excludeBroadcastIds = []) {
|
|
182
|
+
this.sql`delete from cf_ai_chat_agent_messages`;
|
|
183
|
+
for (const message of messages) {
|
|
184
|
+
this.sql`insert into cf_ai_chat_agent_messages (id, message) values (${message.id},${JSON.stringify(message)})`;
|
|
185
|
+
}
|
|
186
|
+
this.messages = messages;
|
|
187
|
+
this._broadcastChatMessage(
|
|
188
|
+
{
|
|
189
|
+
messages,
|
|
190
|
+
type: "cf_agent_chat_messages"
|
|
191
|
+
},
|
|
192
|
+
excludeBroadcastIds
|
|
193
|
+
);
|
|
132
194
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
for (const message of messages) {
|
|
137
|
-
this.sql`insert into cf_ai_chat_agent_messages (id, message) values (${message.id},${JSON.stringify(message)})`;
|
|
195
|
+
_messagesNotAlreadyInAgent(messages) {
|
|
196
|
+
const existingIds = new Set(this.messages.map((message) => message.id));
|
|
197
|
+
return messages.filter((message) => !existingIds.has(message.id));
|
|
138
198
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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,
|
|
150
213
|
id,
|
|
151
|
-
type: "cf_agent_use_chat_response"
|
|
152
|
-
body,
|
|
153
|
-
done: false
|
|
214
|
+
type: "cf_agent_use_chat_response"
|
|
154
215
|
});
|
|
155
|
-
}
|
|
156
|
-
__privateMethod(this, _AIChatAgent_instances, broadcastChatMessage_fn).call(this, {
|
|
157
|
-
id,
|
|
158
|
-
type: "cf_agent_use_chat_response",
|
|
159
|
-
body: "",
|
|
160
|
-
done: true
|
|
161
216
|
});
|
|
162
|
-
}
|
|
217
|
+
}
|
|
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;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Remove an abort controller from the cache of pending message responses
|
|
235
|
+
*/
|
|
236
|
+
_removeAbortController(id) {
|
|
237
|
+
this._chatMessageAbortControllers.delete(id);
|
|
238
|
+
}
|
|
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();
|
|
263
|
+
}
|
|
163
264
|
};
|
|
164
265
|
export {
|
|
165
266
|
AIChatAgent
|
|
@@ -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 if (request.url.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,WAAsB,UAAU,CAAC,WAAW,EAAE;AACpD,eAAO,sBAAK,qCAAL,WAAe,YAAY;AAChC,gBAAM,WAAW,MAAM,KAAK,cAAc,OAAO,EAAE,UAAAA,UAAS,MAAM;AAChE,kBAAM,gBAAgB,uBAAuB;AAAA,cAC3C;AAAA,cACA,kBAAkB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AC;AACD,QAAI,UAAU;AAGZ,uBAAiB,SAAS,SAAS,MAAO;AACxC,gBAAQ,OAAO,KAAK;AAAA,MACtB;AACA,eAAS,MAAM,OAAO;AAAA,IACxB;AAAA,EACF;AA6CF;AAlMO;AAoBL,0BAAqB,SAAC,SAA0B,SAAoB;AAClE,OAAK,UAAU,KAAK,UAAU,OAAO,GAAG,OAAO;AACjD;AAoFM,cAAY,eAAC,IAA0B;AAC3C,MAAI;AACF,WAAO,MAAM,GAAG;AAAA,EAClB,SAAS,GAAG;AACV,UAAM,KAAK,QAAQ,CAAC;AAAA,EACtB;AACF;AAuCM,qBAAgB,eACpB,UACA,sBAAgC,CAAC,GACjC;AACA,OAAK;AACL,aAAW,WAAW,UAAU;AAC9B,SAAK,kEACH,QAAQ,EACV,IAAI,KAAK,UAAU,OAAO,CAAC;AAAA,EAC7B;AACA,OAAK,WAAW;AAChB,wBAAK,iDAAL,WACE;AAAA,IACE,MAAM;AAAA,IACN;AAAA,EACF,GACA;AAEJ;AAEM,WAAM,eAAC,IAAY,UAAoB;AAE3C,SAAO,sBAAK,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;","names":["response"]}
|
|
1
|
+
{"version":3,"sources":["../src/ai-chat-agent.ts"],"sourcesContent":["import type {\n Message as ChatMessage,\n StreamTextOnFinishCallback,\n ToolSet\n} from \"ai\";\nimport { appendResponseMessages } from \"ai\";\nimport { Agent, type AgentContext, type Connection, type WSMessage } from \"./\";\nimport type { IncomingMessage, OutgoingMessage } 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 private _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 private _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 //\n // // these might not exist?\n // dispatcher,\n // duplex\n } = data.init;\n const { messages } = JSON.parse(body as string);\n this._broadcastChatMessage(\n {\n messages,\n type: \"cf_agent_chat_messages\"\n },\n [connection.id]\n );\n\n const incomingMessages = this._messagesNotAlreadyInAgent(messages);\n await this.persistMessages(messages, [connection.id]);\n\n this.observability?.emit(\n {\n displayMessage: \"Chat message request\",\n id: data.id,\n payload: {\n message: incomingMessages\n },\n timestamp: Date.now(),\n type: \"message:request\"\n },\n this.ctx\n );\n\n const chatMessageId = data.id;\n const abortSignal = this._getAbortSignal(chatMessageId);\n\n return this._tryCatchChat(async () => {\n const response = await this.onChatMessage(\n async ({ response }) => {\n const finalMessages = appendResponseMessages({\n messages,\n responseMessages: response.messages\n });\n\n const outgoingMessages =\n this._messagesNotAlreadyInAgent(finalMessages);\n await this.persistMessages(finalMessages, [connection.id]);\n this._removeAbortController(chatMessageId);\n\n this.observability?.emit(\n {\n displayMessage: \"Chat message response\",\n id: data.id,\n payload: {\n message: outgoingMessages\n },\n timestamp: Date.now(),\n type: \"message:response\"\n },\n this.ctx\n );\n },\n abortSignal ? { abortSignal } : undefined\n );\n\n if (response) {\n await this._reply(data.id, response);\n } else {\n // Log a warning for observability\n console.warn(\n `[AIChatAgent] onChatMessage returned no response for chatMessageId: ${chatMessageId}`\n );\n // Send a fallback message to the client\n this._broadcastChatMessage(\n {\n body: \"No response was generated by the agent.\",\n done: true,\n id: data.id,\n type: \"cf_agent_use_chat_response\"\n },\n [connection.id]\n );\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._tryCatchChat(() => {\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 private async _tryCatchChat<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 // biome-ignore lint/correctness/noUnusedFunctionParameters: overridden later\n onFinish: StreamTextOnFinishCallback<ToolSet>,\n // biome-ignore lint/correctness/noUnusedFunctionParameters: overridden later\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 messages: messages,\n type: \"cf_agent_chat_messages\"\n },\n excludeBroadcastIds\n );\n }\n\n private _messagesNotAlreadyInAgent(messages: ChatMessage[]) {\n const existingIds = new Set(this.messages.map((message) => message.id));\n return messages.filter((message) => !existingIds.has(message.id));\n }\n\n private async _reply(id: string, response: Response) {\n // now take chunks out from dataStreamResponse and send them to the client\n return this._tryCatchChat(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 body,\n done: false,\n id,\n type: \"cf_agent_use_chat_response\"\n });\n }\n\n this._broadcastChatMessage({\n body: \"\",\n done: true,\n id,\n type: \"cf_agent_use_chat_response\"\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 private _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 private _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 private _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 private _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":";;;;;;;;AAKA,SAAS,8BAA8B;AAIvC,IAAM,UAAU,IAAI,YAAY;AAMzB,IAAM,cAAN,cAA0D,MAG/D;AAAA,EAQA,YAAY,KAAmB,KAAU;AACvC,UAAM,KAAK,GAAG;AACd,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,SAAK,+BAA+B,oBAAI,IAAI;AAAA,EAC9C;AAAA,EAEQ,sBAAsB,SAA0B,SAAoB;AAC1E,SAAK,UAAU,KAAK,UAAU,OAAO,GAAG,OAAO;AAAA,EACjD;AAAA,EAEA,MAAe,UAAU,YAAwB,SAAoB;AACnE,QAAI,OAAO,YAAY,UAAU;AAC/B,UAAI;AACJ,UAAI;AACF,eAAO,KAAK,MAAM,OAAO;AAAA,MAC3B,SA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