agents 0.0.0-8d8216c → 0.0.0-8dac62c
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 +131 -25
- package/dist/ai-chat-agent.d.ts +49 -12
- package/dist/ai-chat-agent.js +259 -91
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-chat-v5-migration.d.ts +152 -0
- package/dist/ai-chat-v5-migration.js +19 -0
- package/dist/ai-react.d.ts +71 -67
- package/dist/ai-react.js +166 -53
- package/dist/ai-react.js.map +1 -1
- package/dist/ai-types.d.ts +40 -18
- package/dist/ai-types.js +6 -0
- package/dist/chunk-AVYJQSLW.js +17 -0
- package/dist/chunk-AVYJQSLW.js.map +1 -0
- package/dist/chunk-PUJ7PRRT.js +1299 -0
- package/dist/chunk-PUJ7PRRT.js.map +1 -0
- package/dist/chunk-PVQZBKN7.js +106 -0
- package/dist/chunk-PVQZBKN7.js.map +1 -0
- package/dist/chunk-QEVM4BVL.js +116 -0
- package/dist/chunk-QEVM4BVL.js.map +1 -0
- package/dist/chunk-UJVEAURM.js +150 -0
- package/dist/chunk-UJVEAURM.js.map +1 -0
- package/dist/{chunk-YZNSS675.js → chunk-VYENMKFS.js} +230 -53
- package/dist/chunk-VYENMKFS.js.map +1 -0
- package/dist/client-B9tFv5gX.d.ts +4607 -0
- package/dist/client.d.ts +16 -2
- package/dist/client.js +7 -126
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +268 -26
- package/dist/index.js +17 -7
- package/dist/mcp/client.d.ts +9 -766
- package/dist/mcp/client.js +1 -2
- package/dist/mcp/do-oauth-client-provider.d.ts +3 -3
- package/dist/mcp/do-oauth-client-provider.js +3 -103
- package/dist/mcp/do-oauth-client-provider.js.map +1 -1
- package/dist/mcp/index.d.ts +54 -15
- package/dist/mcp/index.js +355 -185
- package/dist/mcp/index.js.map +1 -1
- package/dist/observability/index.d.ts +46 -0
- package/dist/observability/index.js +11 -0
- package/dist/observability/index.js.map +1 -0
- package/dist/react.d.ts +89 -5
- package/dist/react.js +23 -9
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +81 -7
- package/dist/schedule.js +19 -8
- 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 +83 -67
- package/src/index.ts +1173 -154
- package/dist/chunk-AV3OMRR4.js +0 -597
- package/dist/chunk-AV3OMRR4.js.map +0 -1
- package/dist/chunk-HMLY7DHA.js +0 -16
- package/dist/chunk-YZNSS675.js.map +0 -1
- /package/dist/{chunk-HMLY7DHA.js.map → ai-chat-v5-migration.js.map} +0 -0
package/src/index.ts
CHANGED
|
@@ -1,20 +1,34 @@
|
|
|
1
|
+
import type { env } from "cloudflare:workers";
|
|
2
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
3
|
+
import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
4
|
+
import type { SSEClientTransportOptions } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
5
|
+
|
|
6
|
+
import type {
|
|
7
|
+
Prompt,
|
|
8
|
+
Resource,
|
|
9
|
+
ServerCapabilities,
|
|
10
|
+
Tool
|
|
11
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
12
|
+
import { parseCronExpression } from "cron-schedule";
|
|
13
|
+
import { nanoid } from "nanoid";
|
|
14
|
+
import { EmailMessage } from "cloudflare:email";
|
|
1
15
|
import {
|
|
2
|
-
Server,
|
|
3
|
-
routePartykitRequest,
|
|
4
|
-
type PartyServerOptions,
|
|
5
|
-
getServerByName,
|
|
6
16
|
type Connection,
|
|
7
17
|
type ConnectionContext,
|
|
18
|
+
type PartyServerOptions,
|
|
19
|
+
Server,
|
|
8
20
|
type WSMessage,
|
|
21
|
+
getServerByName,
|
|
22
|
+
routePartykitRequest
|
|
9
23
|
} from "partyserver";
|
|
10
|
-
|
|
11
|
-
import { parseCronExpression } from "cron-schedule";
|
|
12
|
-
import { nanoid } from "nanoid";
|
|
13
|
-
|
|
14
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
24
|
+
import { camelCaseToKebabCase } from "./client";
|
|
15
25
|
import { MCPClientManager } from "./mcp/client";
|
|
26
|
+
// import type { MCPClientConnection } from "./mcp/client-connection";
|
|
27
|
+
import { DurableObjectOAuthClientProvider } from "./mcp/do-oauth-client-provider";
|
|
28
|
+
import { genericObservability, type Observability } from "./observability";
|
|
29
|
+
import { MessageType } from "./ai-types";
|
|
16
30
|
|
|
17
|
-
export type { Connection,
|
|
31
|
+
export type { Connection, ConnectionContext, WSMessage } from "partyserver";
|
|
18
32
|
|
|
19
33
|
/**
|
|
20
34
|
* RPC request message from client
|
|
@@ -30,7 +44,7 @@ export type RPCRequest = {
|
|
|
30
44
|
* State update message from client
|
|
31
45
|
*/
|
|
32
46
|
export type StateUpdateMessage = {
|
|
33
|
-
type:
|
|
47
|
+
type: MessageType.CF_AGENT_STATE;
|
|
34
48
|
state: unknown;
|
|
35
49
|
};
|
|
36
50
|
|
|
@@ -38,7 +52,7 @@ export type StateUpdateMessage = {
|
|
|
38
52
|
* RPC response message to client
|
|
39
53
|
*/
|
|
40
54
|
export type RPCResponse = {
|
|
41
|
-
type:
|
|
55
|
+
type: MessageType.RPC;
|
|
42
56
|
id: string;
|
|
43
57
|
} & (
|
|
44
58
|
| {
|
|
@@ -65,7 +79,7 @@ function isRPCRequest(msg: unknown): msg is RPCRequest {
|
|
|
65
79
|
typeof msg === "object" &&
|
|
66
80
|
msg !== null &&
|
|
67
81
|
"type" in msg &&
|
|
68
|
-
msg.type ===
|
|
82
|
+
msg.type === MessageType.RPC &&
|
|
69
83
|
"id" in msg &&
|
|
70
84
|
typeof msg.id === "string" &&
|
|
71
85
|
"method" in msg &&
|
|
@@ -83,7 +97,7 @@ function isStateUpdateMessage(msg: unknown): msg is StateUpdateMessage {
|
|
|
83
97
|
typeof msg === "object" &&
|
|
84
98
|
msg !== null &&
|
|
85
99
|
"type" in msg &&
|
|
86
|
-
msg.type ===
|
|
100
|
+
msg.type === MessageType.CF_AGENT_STATE &&
|
|
87
101
|
"state" in msg
|
|
88
102
|
);
|
|
89
103
|
}
|
|
@@ -98,16 +112,16 @@ export type CallableMetadata = {
|
|
|
98
112
|
streaming?: boolean;
|
|
99
113
|
};
|
|
100
114
|
|
|
101
|
-
// biome-ignore lint/complexity/noBannedTypes: <explanation>
|
|
102
115
|
const callableMetadata = new Map<Function, CallableMetadata>();
|
|
103
116
|
|
|
104
117
|
/**
|
|
105
118
|
* Decorator that marks a method as callable by clients
|
|
106
119
|
* @param metadata Optional metadata about the callable method
|
|
107
120
|
*/
|
|
108
|
-
export function
|
|
121
|
+
export function callable(metadata: CallableMetadata = {}) {
|
|
109
122
|
return function callableDecorator<This, Args extends unknown[], Return>(
|
|
110
123
|
target: (this: This, ...args: Args) => Return,
|
|
124
|
+
// biome-ignore lint/correctness/noUnusedFunctionParameters: later
|
|
111
125
|
context: ClassMethodDecoratorContext
|
|
112
126
|
) {
|
|
113
127
|
if (!callableMetadata.has(target)) {
|
|
@@ -118,6 +132,30 @@ export function unstable_callable(metadata: CallableMetadata = {}) {
|
|
|
118
132
|
};
|
|
119
133
|
}
|
|
120
134
|
|
|
135
|
+
let didWarnAboutUnstableCallable = false;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Decorator that marks a method as callable by clients
|
|
139
|
+
* @deprecated this has been renamed to callable, and unstable_callable will be removed in the next major version
|
|
140
|
+
* @param metadata Optional metadata about the callable method
|
|
141
|
+
*/
|
|
142
|
+
export const unstable_callable = (metadata: CallableMetadata = {}) => {
|
|
143
|
+
if (!didWarnAboutUnstableCallable) {
|
|
144
|
+
didWarnAboutUnstableCallable = true;
|
|
145
|
+
console.warn(
|
|
146
|
+
"unstable_callable is deprecated, use callable instead. unstable_callable will be removed in the next major version."
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
callable(metadata);
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export type QueueItem<T = string> = {
|
|
153
|
+
id: string;
|
|
154
|
+
payload: T;
|
|
155
|
+
callback: keyof Agent<unknown>;
|
|
156
|
+
created_at: number;
|
|
157
|
+
};
|
|
158
|
+
|
|
121
159
|
/**
|
|
122
160
|
* Represents a scheduled task within an Agent
|
|
123
161
|
* @template T Type of the payload data
|
|
@@ -159,29 +197,118 @@ function getNextCronTime(cron: string) {
|
|
|
159
197
|
return interval.getNextDate();
|
|
160
198
|
}
|
|
161
199
|
|
|
200
|
+
/**
|
|
201
|
+
* MCP Server state update message from server -> Client
|
|
202
|
+
*/
|
|
203
|
+
export type MCPServerMessage = {
|
|
204
|
+
type: MessageType.CF_AGENT_MCP_SERVERS;
|
|
205
|
+
mcp: MCPServersState;
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
export type MCPServersState = {
|
|
209
|
+
servers: {
|
|
210
|
+
[id: string]: MCPServer;
|
|
211
|
+
};
|
|
212
|
+
tools: Tool[];
|
|
213
|
+
prompts: Prompt[];
|
|
214
|
+
resources: Resource[];
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
export type MCPServer = {
|
|
218
|
+
name: string;
|
|
219
|
+
server_url: string;
|
|
220
|
+
auth_url: string | null;
|
|
221
|
+
// This state is specifically about the temporary process of getting a token (if needed).
|
|
222
|
+
// Scope outside of that can't be relied upon because when the DO sleeps, there's no way
|
|
223
|
+
// to communicate a change to a non-ready state.
|
|
224
|
+
state: "authenticating" | "connecting" | "ready" | "discovering" | "failed";
|
|
225
|
+
instructions: string | null;
|
|
226
|
+
capabilities: ServerCapabilities | null;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* MCP Server data stored in DO SQL for resuming MCP Server connections
|
|
231
|
+
*/
|
|
232
|
+
type MCPServerRow = {
|
|
233
|
+
id: string;
|
|
234
|
+
name: string;
|
|
235
|
+
server_url: string;
|
|
236
|
+
client_id: string | null;
|
|
237
|
+
auth_url: string | null;
|
|
238
|
+
callback_url: string;
|
|
239
|
+
server_options: string;
|
|
240
|
+
};
|
|
241
|
+
|
|
162
242
|
const STATE_ROW_ID = "cf_state_row_id";
|
|
163
243
|
const STATE_WAS_CHANGED = "cf_state_was_changed";
|
|
164
244
|
|
|
165
245
|
const DEFAULT_STATE = {} as unknown;
|
|
166
246
|
|
|
167
|
-
|
|
168
|
-
agent: Agent<unknown>;
|
|
247
|
+
const agentContext = new AsyncLocalStorage<{
|
|
248
|
+
agent: Agent<unknown, unknown>;
|
|
169
249
|
connection: Connection | undefined;
|
|
170
250
|
request: Request | undefined;
|
|
251
|
+
email: AgentEmail | undefined;
|
|
171
252
|
}>();
|
|
172
253
|
|
|
254
|
+
export function getCurrentAgent<
|
|
255
|
+
T extends Agent<unknown, unknown> = Agent<unknown, unknown>
|
|
256
|
+
>(): {
|
|
257
|
+
agent: T | undefined;
|
|
258
|
+
connection: Connection | undefined;
|
|
259
|
+
request: Request | undefined;
|
|
260
|
+
email: AgentEmail | undefined;
|
|
261
|
+
} {
|
|
262
|
+
const store = agentContext.getStore() as
|
|
263
|
+
| {
|
|
264
|
+
agent: T;
|
|
265
|
+
connection: Connection | undefined;
|
|
266
|
+
request: Request | undefined;
|
|
267
|
+
email: AgentEmail | undefined;
|
|
268
|
+
}
|
|
269
|
+
| undefined;
|
|
270
|
+
if (!store) {
|
|
271
|
+
return {
|
|
272
|
+
agent: undefined,
|
|
273
|
+
connection: undefined,
|
|
274
|
+
request: undefined,
|
|
275
|
+
email: undefined
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
return store;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Wraps a method to run within the agent context, ensuring getCurrentAgent() works properly
|
|
283
|
+
* @param agent The agent instance
|
|
284
|
+
* @param method The method to wrap
|
|
285
|
+
* @returns A wrapped method that runs within the agent context
|
|
286
|
+
*/
|
|
287
|
+
|
|
288
|
+
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
|
|
289
|
+
function withAgentContext<T extends (...args: any[]) => any>(
|
|
290
|
+
method: T
|
|
291
|
+
): (this: Agent<unknown, unknown>, ...args: Parameters<T>) => ReturnType<T> {
|
|
292
|
+
return function (...args: Parameters<T>): ReturnType<T> {
|
|
293
|
+
const { connection, request, email } = getCurrentAgent();
|
|
294
|
+
return agentContext.run({ agent: this, connection, request, email }, () => {
|
|
295
|
+
return method.apply(this, args);
|
|
296
|
+
});
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
173
300
|
/**
|
|
174
301
|
* Base class for creating Agent implementations
|
|
175
302
|
* @template Env Environment type containing bindings
|
|
176
303
|
* @template State State type to store within the Agent
|
|
177
304
|
*/
|
|
178
|
-
export class Agent<Env, State = unknown> extends Server<Env> {
|
|
179
|
-
|
|
305
|
+
export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
|
|
306
|
+
private _state = DEFAULT_STATE as State;
|
|
180
307
|
|
|
181
|
-
|
|
308
|
+
private _ParentClass: typeof Agent<Env, State> =
|
|
182
309
|
Object.getPrototypeOf(this).constructor;
|
|
183
310
|
|
|
184
|
-
mcp: MCPClientManager = new MCPClientManager(this
|
|
311
|
+
mcp: MCPClientManager = new MCPClientManager(this._ParentClass.name, "0.0.1");
|
|
185
312
|
|
|
186
313
|
/**
|
|
187
314
|
* Initial state for the Agent
|
|
@@ -193,9 +320,9 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
193
320
|
* Current state of the Agent
|
|
194
321
|
*/
|
|
195
322
|
get state(): State {
|
|
196
|
-
if (this
|
|
323
|
+
if (this._state !== DEFAULT_STATE) {
|
|
197
324
|
// state was previously set, and populated internal state
|
|
198
|
-
return this
|
|
325
|
+
return this._state;
|
|
199
326
|
}
|
|
200
327
|
// looks like this is the first time the state is being accessed
|
|
201
328
|
// check if the state was set in a previous life
|
|
@@ -215,8 +342,8 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
215
342
|
) {
|
|
216
343
|
const state = result[0]?.state as string; // could be null?
|
|
217
344
|
|
|
218
|
-
this
|
|
219
|
-
return this
|
|
345
|
+
this._state = JSON.parse(state);
|
|
346
|
+
return this._state;
|
|
220
347
|
}
|
|
221
348
|
|
|
222
349
|
// ok, this is the first time the state is being accessed
|
|
@@ -237,9 +364,14 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
237
364
|
*/
|
|
238
365
|
static options = {
|
|
239
366
|
/** Whether the Agent should hibernate when inactive */
|
|
240
|
-
hibernate: true
|
|
367
|
+
hibernate: true // default to hibernate
|
|
241
368
|
};
|
|
242
369
|
|
|
370
|
+
/**
|
|
371
|
+
* The observability implementation to use for the Agent
|
|
372
|
+
*/
|
|
373
|
+
observability?: Observability = genericObservability;
|
|
374
|
+
|
|
243
375
|
/**
|
|
244
376
|
* Execute SQL queries against the Agent's database
|
|
245
377
|
* @template T Type of the returned rows
|
|
@@ -269,6 +401,9 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
269
401
|
constructor(ctx: AgentContext, env: Env) {
|
|
270
402
|
super(ctx, env);
|
|
271
403
|
|
|
404
|
+
// Auto-wrap custom methods with agent context
|
|
405
|
+
this._autoWrapCustomMethods();
|
|
406
|
+
|
|
272
407
|
this.sql`
|
|
273
408
|
CREATE TABLE IF NOT EXISTS cf_agents_state (
|
|
274
409
|
id TEXT PRIMARY KEY NOT NULL,
|
|
@@ -276,8 +411,17 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
276
411
|
)
|
|
277
412
|
`;
|
|
278
413
|
|
|
414
|
+
this.sql`
|
|
415
|
+
CREATE TABLE IF NOT EXISTS cf_agents_queues (
|
|
416
|
+
id TEXT PRIMARY KEY NOT NULL,
|
|
417
|
+
payload TEXT,
|
|
418
|
+
callback TEXT,
|
|
419
|
+
created_at INTEGER DEFAULT (unixepoch())
|
|
420
|
+
)
|
|
421
|
+
`;
|
|
422
|
+
|
|
279
423
|
void this.ctx.blockConcurrencyWhile(async () => {
|
|
280
|
-
return this
|
|
424
|
+
return this._tryCatch(async () => {
|
|
281
425
|
// Create alarms table if it doesn't exist
|
|
282
426
|
this.sql`
|
|
283
427
|
CREATE TABLE IF NOT EXISTS cf_agents_schedules (
|
|
@@ -297,25 +441,65 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
297
441
|
});
|
|
298
442
|
});
|
|
299
443
|
|
|
444
|
+
this.sql`
|
|
445
|
+
CREATE TABLE IF NOT EXISTS cf_agents_mcp_servers (
|
|
446
|
+
id TEXT PRIMARY KEY NOT NULL,
|
|
447
|
+
name TEXT NOT NULL,
|
|
448
|
+
server_url TEXT NOT NULL,
|
|
449
|
+
callback_url TEXT NOT NULL,
|
|
450
|
+
client_id TEXT,
|
|
451
|
+
auth_url TEXT,
|
|
452
|
+
server_options TEXT
|
|
453
|
+
)
|
|
454
|
+
`;
|
|
455
|
+
|
|
456
|
+
const _onRequest = this.onRequest.bind(this);
|
|
457
|
+
this.onRequest = (request: Request) => {
|
|
458
|
+
return agentContext.run(
|
|
459
|
+
{ agent: this, connection: undefined, request, email: undefined },
|
|
460
|
+
async () => {
|
|
461
|
+
if (this.mcp.isCallbackRequest(request)) {
|
|
462
|
+
await this.mcp.handleCallbackRequest(request);
|
|
463
|
+
|
|
464
|
+
// after the MCP connection handshake, we can send updated mcp state
|
|
465
|
+
this.broadcast(
|
|
466
|
+
JSON.stringify({
|
|
467
|
+
mcp: this.getMcpServers(),
|
|
468
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
469
|
+
})
|
|
470
|
+
);
|
|
471
|
+
|
|
472
|
+
// We probably should let the user configure this response/redirect, but this is fine for now.
|
|
473
|
+
return new Response("<script>window.close();</script>", {
|
|
474
|
+
headers: { "content-type": "text/html" },
|
|
475
|
+
status: 200
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
return this._tryCatch(() => _onRequest(request));
|
|
480
|
+
}
|
|
481
|
+
);
|
|
482
|
+
};
|
|
483
|
+
|
|
300
484
|
const _onMessage = this.onMessage.bind(this);
|
|
301
485
|
this.onMessage = async (connection: Connection, message: WSMessage) => {
|
|
302
|
-
return
|
|
303
|
-
{ agent: this, connection, request: undefined },
|
|
486
|
+
return agentContext.run(
|
|
487
|
+
{ agent: this, connection, request: undefined, email: undefined },
|
|
304
488
|
async () => {
|
|
305
489
|
if (typeof message !== "string") {
|
|
306
|
-
return this
|
|
490
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
307
491
|
}
|
|
308
492
|
|
|
309
493
|
let parsed: unknown;
|
|
310
494
|
try {
|
|
311
495
|
parsed = JSON.parse(message);
|
|
312
|
-
} catch (
|
|
496
|
+
} catch (_e) {
|
|
313
497
|
// silently fail and let the onMessage handler handle it
|
|
314
|
-
return this
|
|
498
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
315
499
|
}
|
|
316
500
|
|
|
317
501
|
if (isStateUpdateMessage(parsed)) {
|
|
318
|
-
this
|
|
502
|
+
this._setStateInternal(parsed.state as State, connection);
|
|
319
503
|
return;
|
|
320
504
|
}
|
|
321
505
|
|
|
@@ -329,11 +513,10 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
329
513
|
throw new Error(`Method ${method} does not exist`);
|
|
330
514
|
}
|
|
331
515
|
|
|
332
|
-
if (!this
|
|
516
|
+
if (!this._isCallable(method)) {
|
|
333
517
|
throw new Error(`Method ${method} is not callable`);
|
|
334
518
|
}
|
|
335
519
|
|
|
336
|
-
// biome-ignore lint/complexity/noBannedTypes: <explanation>
|
|
337
520
|
const metadata = callableMetadata.get(methodFn as Function);
|
|
338
521
|
|
|
339
522
|
// For streaming methods, pass a StreamingResponse object
|
|
@@ -345,22 +528,37 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
345
528
|
|
|
346
529
|
// For regular methods, execute and send response
|
|
347
530
|
const result = await methodFn.apply(this, args);
|
|
531
|
+
|
|
532
|
+
this.observability?.emit(
|
|
533
|
+
{
|
|
534
|
+
displayMessage: `RPC call to ${method}`,
|
|
535
|
+
id: nanoid(),
|
|
536
|
+
payload: {
|
|
537
|
+
method,
|
|
538
|
+
streaming: metadata?.streaming
|
|
539
|
+
},
|
|
540
|
+
timestamp: Date.now(),
|
|
541
|
+
type: "rpc"
|
|
542
|
+
},
|
|
543
|
+
this.ctx
|
|
544
|
+
);
|
|
545
|
+
|
|
348
546
|
const response: RPCResponse = {
|
|
349
|
-
|
|
547
|
+
done: true,
|
|
350
548
|
id,
|
|
351
|
-
success: true,
|
|
352
549
|
result,
|
|
353
|
-
|
|
550
|
+
success: true,
|
|
551
|
+
type: MessageType.RPC
|
|
354
552
|
};
|
|
355
553
|
connection.send(JSON.stringify(response));
|
|
356
554
|
} catch (e) {
|
|
357
555
|
// Send error response
|
|
358
556
|
const response: RPCResponse = {
|
|
359
|
-
type: "rpc",
|
|
360
|
-
id: parsed.id,
|
|
361
|
-
success: false,
|
|
362
557
|
error:
|
|
363
558
|
e instanceof Error ? e.message : "Unknown error occurred",
|
|
559
|
+
id: parsed.id,
|
|
560
|
+
success: false,
|
|
561
|
+
type: MessageType.RPC
|
|
364
562
|
};
|
|
365
563
|
connection.send(JSON.stringify(response));
|
|
366
564
|
console.error("RPC error:", e);
|
|
@@ -368,7 +566,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
368
566
|
return;
|
|
369
567
|
}
|
|
370
568
|
|
|
371
|
-
return this
|
|
569
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
372
570
|
}
|
|
373
571
|
);
|
|
374
572
|
};
|
|
@@ -377,27 +575,115 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
377
575
|
this.onConnect = (connection: Connection, ctx: ConnectionContext) => {
|
|
378
576
|
// TODO: This is a hack to ensure the state is sent after the connection is established
|
|
379
577
|
// must fix this
|
|
380
|
-
return
|
|
381
|
-
{ agent: this, connection, request: ctx.request },
|
|
578
|
+
return agentContext.run(
|
|
579
|
+
{ agent: this, connection, request: ctx.request, email: undefined },
|
|
580
|
+
() => {
|
|
581
|
+
if (this.state) {
|
|
582
|
+
connection.send(
|
|
583
|
+
JSON.stringify({
|
|
584
|
+
state: this.state,
|
|
585
|
+
type: MessageType.CF_AGENT_STATE
|
|
586
|
+
})
|
|
587
|
+
);
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
connection.send(
|
|
591
|
+
JSON.stringify({
|
|
592
|
+
mcp: this.getMcpServers(),
|
|
593
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
594
|
+
})
|
|
595
|
+
);
|
|
596
|
+
|
|
597
|
+
this.observability?.emit(
|
|
598
|
+
{
|
|
599
|
+
displayMessage: "Connection established",
|
|
600
|
+
id: nanoid(),
|
|
601
|
+
payload: {
|
|
602
|
+
connectionId: connection.id
|
|
603
|
+
},
|
|
604
|
+
timestamp: Date.now(),
|
|
605
|
+
type: "connect"
|
|
606
|
+
},
|
|
607
|
+
this.ctx
|
|
608
|
+
);
|
|
609
|
+
return this._tryCatch(() => _onConnect(connection, ctx));
|
|
610
|
+
}
|
|
611
|
+
);
|
|
612
|
+
};
|
|
613
|
+
|
|
614
|
+
const _onStart = this.onStart.bind(this);
|
|
615
|
+
this.onStart = async () => {
|
|
616
|
+
return agentContext.run(
|
|
617
|
+
{
|
|
618
|
+
agent: this,
|
|
619
|
+
connection: undefined,
|
|
620
|
+
request: undefined,
|
|
621
|
+
email: undefined
|
|
622
|
+
},
|
|
382
623
|
async () => {
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
624
|
+
await this._tryCatch(() => {
|
|
625
|
+
const servers = this.sql<MCPServerRow>`
|
|
626
|
+
SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
|
|
627
|
+
`;
|
|
628
|
+
|
|
629
|
+
this.broadcast(
|
|
630
|
+
JSON.stringify({
|
|
631
|
+
mcp: this.getMcpServers(),
|
|
632
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
633
|
+
})
|
|
634
|
+
);
|
|
635
|
+
|
|
636
|
+
// from DO storage, reconnect to all servers not currently in the oauth flow using our saved auth information
|
|
637
|
+
if (servers && Array.isArray(servers) && servers.length > 0) {
|
|
638
|
+
servers.forEach((server) => {
|
|
639
|
+
this._connectToMcpServerInternal(
|
|
640
|
+
server.name,
|
|
641
|
+
server.server_url,
|
|
642
|
+
server.callback_url,
|
|
643
|
+
server.server_options
|
|
644
|
+
? JSON.parse(server.server_options)
|
|
645
|
+
: undefined,
|
|
646
|
+
{
|
|
647
|
+
id: server.id,
|
|
648
|
+
oauthClientId: server.client_id ?? undefined
|
|
649
|
+
}
|
|
650
|
+
)
|
|
651
|
+
.then(() => {
|
|
652
|
+
// Broadcast updated MCP servers state after each server connects
|
|
653
|
+
this.broadcast(
|
|
654
|
+
JSON.stringify({
|
|
655
|
+
mcp: this.getMcpServers(),
|
|
656
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
657
|
+
})
|
|
658
|
+
);
|
|
659
|
+
})
|
|
660
|
+
.catch((error) => {
|
|
661
|
+
console.error(
|
|
662
|
+
`Error connecting to MCP server: ${server.name} (${server.server_url})`,
|
|
663
|
+
error
|
|
664
|
+
);
|
|
665
|
+
// Still broadcast even if connection fails, so clients know about the failure
|
|
666
|
+
this.broadcast(
|
|
667
|
+
JSON.stringify({
|
|
668
|
+
mcp: this.getMcpServers(),
|
|
669
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
670
|
+
})
|
|
671
|
+
);
|
|
672
|
+
});
|
|
673
|
+
});
|
|
391
674
|
}
|
|
392
|
-
return
|
|
393
|
-
}
|
|
675
|
+
return _onStart();
|
|
676
|
+
});
|
|
394
677
|
}
|
|
395
678
|
);
|
|
396
679
|
};
|
|
397
680
|
}
|
|
398
681
|
|
|
399
|
-
|
|
400
|
-
|
|
682
|
+
private _setStateInternal(
|
|
683
|
+
state: State,
|
|
684
|
+
source: Connection | "server" = "server"
|
|
685
|
+
) {
|
|
686
|
+
this._state = state;
|
|
401
687
|
this.sql`
|
|
402
688
|
INSERT OR REPLACE INTO cf_agents_state (id, state)
|
|
403
689
|
VALUES (${STATE_ROW_ID}, ${JSON.stringify(state)})
|
|
@@ -408,16 +694,26 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
408
694
|
`;
|
|
409
695
|
this.broadcast(
|
|
410
696
|
JSON.stringify({
|
|
411
|
-
type: "cf_agent_state",
|
|
412
697
|
state: state,
|
|
698
|
+
type: MessageType.CF_AGENT_STATE
|
|
413
699
|
}),
|
|
414
700
|
source !== "server" ? [source.id] : []
|
|
415
701
|
);
|
|
416
|
-
return this
|
|
417
|
-
const { connection, request } =
|
|
418
|
-
return
|
|
419
|
-
{ agent: this, connection, request },
|
|
702
|
+
return this._tryCatch(() => {
|
|
703
|
+
const { connection, request, email } = agentContext.getStore() || {};
|
|
704
|
+
return agentContext.run(
|
|
705
|
+
{ agent: this, connection, request, email },
|
|
420
706
|
async () => {
|
|
707
|
+
this.observability?.emit(
|
|
708
|
+
{
|
|
709
|
+
displayMessage: "State updated",
|
|
710
|
+
id: nanoid(),
|
|
711
|
+
payload: {},
|
|
712
|
+
timestamp: Date.now(),
|
|
713
|
+
type: "state:update"
|
|
714
|
+
},
|
|
715
|
+
this.ctx
|
|
716
|
+
);
|
|
421
717
|
return this.onStateUpdate(state, source);
|
|
422
718
|
}
|
|
423
719
|
);
|
|
@@ -429,7 +725,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
429
725
|
* @param state New state to set
|
|
430
726
|
*/
|
|
431
727
|
setState(state: State) {
|
|
432
|
-
this
|
|
728
|
+
this._setStateInternal(state, "server");
|
|
433
729
|
}
|
|
434
730
|
|
|
435
731
|
/**
|
|
@@ -437,24 +733,90 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
437
733
|
* @param state Updated state
|
|
438
734
|
* @param source Source of the state update ("server" or a client connection)
|
|
439
735
|
*/
|
|
736
|
+
// biome-ignore lint/correctness/noUnusedFunctionParameters: overridden later
|
|
440
737
|
onStateUpdate(state: State | undefined, source: Connection | "server") {
|
|
441
738
|
// override this to handle state updates
|
|
442
739
|
}
|
|
443
740
|
|
|
444
741
|
/**
|
|
445
|
-
* Called when the Agent receives an email
|
|
742
|
+
* Called when the Agent receives an email via routeAgentEmail()
|
|
743
|
+
* Override this method to handle incoming emails
|
|
446
744
|
* @param email Email message to process
|
|
447
745
|
*/
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
746
|
+
async _onEmail(email: AgentEmail) {
|
|
747
|
+
// nb: we use this roundabout way of getting to onEmail
|
|
748
|
+
// because of https://github.com/cloudflare/workerd/issues/4499
|
|
749
|
+
return agentContext.run(
|
|
750
|
+
{ agent: this, connection: undefined, request: undefined, email: email },
|
|
451
751
|
async () => {
|
|
452
|
-
|
|
752
|
+
if ("onEmail" in this && typeof this.onEmail === "function") {
|
|
753
|
+
return this._tryCatch(() =>
|
|
754
|
+
(this.onEmail as (email: AgentEmail) => Promise<void>)(email)
|
|
755
|
+
);
|
|
756
|
+
} else {
|
|
757
|
+
console.log("Received email from:", email.from, "to:", email.to);
|
|
758
|
+
console.log("Subject:", email.headers.get("subject"));
|
|
759
|
+
console.log(
|
|
760
|
+
"Implement onEmail(email: AgentEmail): Promise<void> in your agent to process emails"
|
|
761
|
+
);
|
|
762
|
+
}
|
|
453
763
|
}
|
|
454
764
|
);
|
|
455
765
|
}
|
|
456
766
|
|
|
457
|
-
|
|
767
|
+
/**
|
|
768
|
+
* Reply to an email
|
|
769
|
+
* @param email The email to reply to
|
|
770
|
+
* @param options Options for the reply
|
|
771
|
+
* @returns void
|
|
772
|
+
*/
|
|
773
|
+
async replyToEmail(
|
|
774
|
+
email: AgentEmail,
|
|
775
|
+
options: {
|
|
776
|
+
fromName: string;
|
|
777
|
+
subject?: string | undefined;
|
|
778
|
+
body: string;
|
|
779
|
+
contentType?: string;
|
|
780
|
+
headers?: Record<string, string>;
|
|
781
|
+
}
|
|
782
|
+
): Promise<void> {
|
|
783
|
+
return this._tryCatch(async () => {
|
|
784
|
+
const agentName = camelCaseToKebabCase(this._ParentClass.name);
|
|
785
|
+
const agentId = this.name;
|
|
786
|
+
|
|
787
|
+
const { createMimeMessage } = await import("mimetext");
|
|
788
|
+
const msg = createMimeMessage();
|
|
789
|
+
msg.setSender({ addr: email.to, name: options.fromName });
|
|
790
|
+
msg.setRecipient(email.from);
|
|
791
|
+
msg.setSubject(
|
|
792
|
+
options.subject || `Re: ${email.headers.get("subject")}` || "No subject"
|
|
793
|
+
);
|
|
794
|
+
msg.addMessage({
|
|
795
|
+
contentType: options.contentType || "text/plain",
|
|
796
|
+
data: options.body
|
|
797
|
+
});
|
|
798
|
+
|
|
799
|
+
const domain = email.from.split("@")[1];
|
|
800
|
+
const messageId = `<${agentId}@${domain}>`;
|
|
801
|
+
msg.setHeader("In-Reply-To", email.headers.get("Message-ID")!);
|
|
802
|
+
msg.setHeader("Message-ID", messageId);
|
|
803
|
+
msg.setHeader("X-Agent-Name", agentName);
|
|
804
|
+
msg.setHeader("X-Agent-ID", agentId);
|
|
805
|
+
|
|
806
|
+
if (options.headers) {
|
|
807
|
+
for (const [key, value] of Object.entries(options.headers)) {
|
|
808
|
+
msg.setHeader(key, value);
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
await email.reply({
|
|
812
|
+
from: email.to,
|
|
813
|
+
raw: msg.asRaw(),
|
|
814
|
+
to: email.from
|
|
815
|
+
});
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
private async _tryCatch<T>(fn: () => T | Promise<T>) {
|
|
458
820
|
try {
|
|
459
821
|
return await fn();
|
|
460
822
|
} catch (e) {
|
|
@@ -462,6 +824,73 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
462
824
|
}
|
|
463
825
|
}
|
|
464
826
|
|
|
827
|
+
/**
|
|
828
|
+
* Automatically wrap custom methods with agent context
|
|
829
|
+
* This ensures getCurrentAgent() works in all custom methods without decorators
|
|
830
|
+
*/
|
|
831
|
+
private _autoWrapCustomMethods() {
|
|
832
|
+
// Collect all methods from base prototypes (Agent and Server)
|
|
833
|
+
const basePrototypes = [Agent.prototype, Server.prototype];
|
|
834
|
+
const baseMethods = new Set<string>();
|
|
835
|
+
for (const baseProto of basePrototypes) {
|
|
836
|
+
let proto = baseProto;
|
|
837
|
+
while (proto && proto !== Object.prototype) {
|
|
838
|
+
const methodNames = Object.getOwnPropertyNames(proto);
|
|
839
|
+
for (const methodName of methodNames) {
|
|
840
|
+
baseMethods.add(methodName);
|
|
841
|
+
}
|
|
842
|
+
proto = Object.getPrototypeOf(proto);
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
// Get all methods from the current instance's prototype chain
|
|
846
|
+
let proto = Object.getPrototypeOf(this);
|
|
847
|
+
let depth = 0;
|
|
848
|
+
while (proto && proto !== Object.prototype && depth < 10) {
|
|
849
|
+
const methodNames = Object.getOwnPropertyNames(proto);
|
|
850
|
+
for (const methodName of methodNames) {
|
|
851
|
+
// Skip if it's a private method or not a function or a getter
|
|
852
|
+
if (
|
|
853
|
+
baseMethods.has(methodName) ||
|
|
854
|
+
methodName.startsWith("_") ||
|
|
855
|
+
typeof this[methodName as keyof this] !== "function" ||
|
|
856
|
+
!!Object.getOwnPropertyDescriptor(proto, methodName)?.get
|
|
857
|
+
) {
|
|
858
|
+
continue;
|
|
859
|
+
}
|
|
860
|
+
// If the method doesn't exist in base prototypes, it's a custom method
|
|
861
|
+
if (!baseMethods.has(methodName)) {
|
|
862
|
+
const descriptor = Object.getOwnPropertyDescriptor(proto, methodName);
|
|
863
|
+
if (descriptor && typeof descriptor.value === "function") {
|
|
864
|
+
// Wrap the custom method with context
|
|
865
|
+
|
|
866
|
+
const wrappedFunction = withAgentContext(
|
|
867
|
+
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
|
|
868
|
+
this[methodName as keyof this] as (...args: any[]) => any
|
|
869
|
+
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
|
|
870
|
+
) as any;
|
|
871
|
+
|
|
872
|
+
// if the method is callable, copy the metadata from the original method
|
|
873
|
+
if (this._isCallable(methodName)) {
|
|
874
|
+
callableMetadata.set(
|
|
875
|
+
wrappedFunction,
|
|
876
|
+
callableMetadata.get(
|
|
877
|
+
this[methodName as keyof this] as Function
|
|
878
|
+
)!
|
|
879
|
+
);
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
// set the wrapped function on the prototype
|
|
883
|
+
this.constructor.prototype[methodName as keyof this] =
|
|
884
|
+
wrappedFunction;
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
proto = Object.getPrototypeOf(proto);
|
|
890
|
+
depth++;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
|
|
465
894
|
override onError(
|
|
466
895
|
connection: Connection,
|
|
467
896
|
error: unknown
|
|
@@ -496,6 +925,131 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
496
925
|
throw new Error("Not implemented");
|
|
497
926
|
}
|
|
498
927
|
|
|
928
|
+
/**
|
|
929
|
+
* Queue a task to be executed in the future
|
|
930
|
+
* @param payload Payload to pass to the callback
|
|
931
|
+
* @param callback Name of the method to call
|
|
932
|
+
* @returns The ID of the queued task
|
|
933
|
+
*/
|
|
934
|
+
async queue<T = unknown>(callback: keyof this, payload: T): Promise<string> {
|
|
935
|
+
const id = nanoid(9);
|
|
936
|
+
if (typeof callback !== "string") {
|
|
937
|
+
throw new Error("Callback must be a string");
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
if (typeof this[callback] !== "function") {
|
|
941
|
+
throw new Error(`this.${callback} is not a function`);
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
this.sql`
|
|
945
|
+
INSERT OR REPLACE INTO cf_agents_queues (id, payload, callback)
|
|
946
|
+
VALUES (${id}, ${JSON.stringify(payload)}, ${callback})
|
|
947
|
+
`;
|
|
948
|
+
|
|
949
|
+
void this._flushQueue().catch((e) => {
|
|
950
|
+
console.error("Error flushing queue:", e);
|
|
951
|
+
});
|
|
952
|
+
|
|
953
|
+
return id;
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
private _flushingQueue = false;
|
|
957
|
+
|
|
958
|
+
private async _flushQueue() {
|
|
959
|
+
if (this._flushingQueue) {
|
|
960
|
+
return;
|
|
961
|
+
}
|
|
962
|
+
this._flushingQueue = true;
|
|
963
|
+
while (true) {
|
|
964
|
+
const result = this.sql<QueueItem<string>>`
|
|
965
|
+
SELECT * FROM cf_agents_queues
|
|
966
|
+
ORDER BY created_at ASC
|
|
967
|
+
`;
|
|
968
|
+
|
|
969
|
+
if (!result || result.length === 0) {
|
|
970
|
+
break;
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
for (const row of result || []) {
|
|
974
|
+
const callback = this[row.callback as keyof Agent<Env>];
|
|
975
|
+
if (!callback) {
|
|
976
|
+
console.error(`callback ${row.callback} not found`);
|
|
977
|
+
continue;
|
|
978
|
+
}
|
|
979
|
+
const { connection, request, email } = agentContext.getStore() || {};
|
|
980
|
+
await agentContext.run(
|
|
981
|
+
{
|
|
982
|
+
agent: this,
|
|
983
|
+
connection,
|
|
984
|
+
request,
|
|
985
|
+
email
|
|
986
|
+
},
|
|
987
|
+
async () => {
|
|
988
|
+
// TODO: add retries and backoff
|
|
989
|
+
await (
|
|
990
|
+
callback as (
|
|
991
|
+
payload: unknown,
|
|
992
|
+
queueItem: QueueItem<string>
|
|
993
|
+
) => Promise<void>
|
|
994
|
+
).bind(this)(JSON.parse(row.payload as string), row);
|
|
995
|
+
await this.dequeue(row.id);
|
|
996
|
+
}
|
|
997
|
+
);
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
this._flushingQueue = false;
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
/**
|
|
1004
|
+
* Dequeue a task by ID
|
|
1005
|
+
* @param id ID of the task to dequeue
|
|
1006
|
+
*/
|
|
1007
|
+
async dequeue(id: string) {
|
|
1008
|
+
this.sql`DELETE FROM cf_agents_queues WHERE id = ${id}`;
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
/**
|
|
1012
|
+
* Dequeue all tasks
|
|
1013
|
+
*/
|
|
1014
|
+
async dequeueAll() {
|
|
1015
|
+
this.sql`DELETE FROM cf_agents_queues`;
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
/**
|
|
1019
|
+
* Dequeue all tasks by callback
|
|
1020
|
+
* @param callback Name of the callback to dequeue
|
|
1021
|
+
*/
|
|
1022
|
+
async dequeueAllByCallback(callback: string) {
|
|
1023
|
+
this.sql`DELETE FROM cf_agents_queues WHERE callback = ${callback}`;
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
/**
|
|
1027
|
+
* Get a queued task by ID
|
|
1028
|
+
* @param id ID of the task to get
|
|
1029
|
+
* @returns The task or undefined if not found
|
|
1030
|
+
*/
|
|
1031
|
+
async getQueue(id: string): Promise<QueueItem<string> | undefined> {
|
|
1032
|
+
const result = this.sql<QueueItem<string>>`
|
|
1033
|
+
SELECT * FROM cf_agents_queues WHERE id = ${id}
|
|
1034
|
+
`;
|
|
1035
|
+
return result
|
|
1036
|
+
? { ...result[0], payload: JSON.parse(result[0].payload) }
|
|
1037
|
+
: undefined;
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
/**
|
|
1041
|
+
* Get all queues by key and value
|
|
1042
|
+
* @param key Key to filter by
|
|
1043
|
+
* @param value Value to filter by
|
|
1044
|
+
* @returns Array of matching QueueItem objects
|
|
1045
|
+
*/
|
|
1046
|
+
async getQueues(key: string, value: string): Promise<QueueItem<string>[]> {
|
|
1047
|
+
const result = this.sql<QueueItem<string>>`
|
|
1048
|
+
SELECT * FROM cf_agents_queues
|
|
1049
|
+
`;
|
|
1050
|
+
return result.filter((row) => JSON.parse(row.payload)[key] === value);
|
|
1051
|
+
}
|
|
1052
|
+
|
|
499
1053
|
/**
|
|
500
1054
|
* Schedule a task to be executed in the future
|
|
501
1055
|
* @template T Type of the payload data
|
|
@@ -511,6 +1065,21 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
511
1065
|
): Promise<Schedule<T>> {
|
|
512
1066
|
const id = nanoid(9);
|
|
513
1067
|
|
|
1068
|
+
const emitScheduleCreate = (schedule: Schedule<T>) =>
|
|
1069
|
+
this.observability?.emit(
|
|
1070
|
+
{
|
|
1071
|
+
displayMessage: `Schedule ${schedule.id} created`,
|
|
1072
|
+
id: nanoid(),
|
|
1073
|
+
payload: {
|
|
1074
|
+
callback: callback as string,
|
|
1075
|
+
id: id
|
|
1076
|
+
},
|
|
1077
|
+
timestamp: Date.now(),
|
|
1078
|
+
type: "schedule:create"
|
|
1079
|
+
},
|
|
1080
|
+
this.ctx
|
|
1081
|
+
);
|
|
1082
|
+
|
|
514
1083
|
if (typeof callback !== "string") {
|
|
515
1084
|
throw new Error("Callback must be a string");
|
|
516
1085
|
}
|
|
@@ -528,15 +1097,19 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
528
1097
|
)}, 'scheduled', ${timestamp})
|
|
529
1098
|
`;
|
|
530
1099
|
|
|
531
|
-
await this
|
|
1100
|
+
await this._scheduleNextAlarm();
|
|
532
1101
|
|
|
533
|
-
|
|
534
|
-
id,
|
|
1102
|
+
const schedule: Schedule<T> = {
|
|
535
1103
|
callback: callback,
|
|
1104
|
+
id,
|
|
536
1105
|
payload: payload as T,
|
|
537
1106
|
time: timestamp,
|
|
538
|
-
type: "scheduled"
|
|
1107
|
+
type: "scheduled"
|
|
539
1108
|
};
|
|
1109
|
+
|
|
1110
|
+
emitScheduleCreate(schedule);
|
|
1111
|
+
|
|
1112
|
+
return schedule;
|
|
540
1113
|
}
|
|
541
1114
|
if (typeof when === "number") {
|
|
542
1115
|
const time = new Date(Date.now() + when * 1000);
|
|
@@ -549,16 +1122,20 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
549
1122
|
)}, 'delayed', ${when}, ${timestamp})
|
|
550
1123
|
`;
|
|
551
1124
|
|
|
552
|
-
await this
|
|
1125
|
+
await this._scheduleNextAlarm();
|
|
553
1126
|
|
|
554
|
-
|
|
555
|
-
id,
|
|
1127
|
+
const schedule: Schedule<T> = {
|
|
556
1128
|
callback: callback,
|
|
557
|
-
payload: payload as T,
|
|
558
1129
|
delayInSeconds: when,
|
|
1130
|
+
id,
|
|
1131
|
+
payload: payload as T,
|
|
559
1132
|
time: timestamp,
|
|
560
|
-
type: "delayed"
|
|
1133
|
+
type: "delayed"
|
|
561
1134
|
};
|
|
1135
|
+
|
|
1136
|
+
emitScheduleCreate(schedule);
|
|
1137
|
+
|
|
1138
|
+
return schedule;
|
|
562
1139
|
}
|
|
563
1140
|
if (typeof when === "string") {
|
|
564
1141
|
const nextExecutionTime = getNextCronTime(when);
|
|
@@ -571,16 +1148,20 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
571
1148
|
)}, 'cron', ${when}, ${timestamp})
|
|
572
1149
|
`;
|
|
573
1150
|
|
|
574
|
-
await this
|
|
1151
|
+
await this._scheduleNextAlarm();
|
|
575
1152
|
|
|
576
|
-
|
|
577
|
-
id,
|
|
1153
|
+
const schedule: Schedule<T> = {
|
|
578
1154
|
callback: callback,
|
|
579
|
-
payload: payload as T,
|
|
580
1155
|
cron: when,
|
|
1156
|
+
id,
|
|
1157
|
+
payload: payload as T,
|
|
581
1158
|
time: timestamp,
|
|
582
|
-
type: "cron"
|
|
1159
|
+
type: "cron"
|
|
583
1160
|
};
|
|
1161
|
+
|
|
1162
|
+
emitScheduleCreate(schedule);
|
|
1163
|
+
|
|
1164
|
+
return schedule;
|
|
584
1165
|
}
|
|
585
1166
|
throw new Error("Invalid schedule type");
|
|
586
1167
|
}
|
|
@@ -644,7 +1225,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
644
1225
|
.toArray()
|
|
645
1226
|
.map((row) => ({
|
|
646
1227
|
...row,
|
|
647
|
-
payload: JSON.parse(row.payload as string) as T
|
|
1228
|
+
payload: JSON.parse(row.payload as string) as T
|
|
648
1229
|
})) as Schedule<T>[];
|
|
649
1230
|
|
|
650
1231
|
return result;
|
|
@@ -656,18 +1237,34 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
656
1237
|
* @returns true if the task was cancelled, false otherwise
|
|
657
1238
|
*/
|
|
658
1239
|
async cancelSchedule(id: string): Promise<boolean> {
|
|
1240
|
+
const schedule = await this.getSchedule(id);
|
|
1241
|
+
if (schedule) {
|
|
1242
|
+
this.observability?.emit(
|
|
1243
|
+
{
|
|
1244
|
+
displayMessage: `Schedule ${id} cancelled`,
|
|
1245
|
+
id: nanoid(),
|
|
1246
|
+
payload: {
|
|
1247
|
+
callback: schedule.callback,
|
|
1248
|
+
id: schedule.id
|
|
1249
|
+
},
|
|
1250
|
+
timestamp: Date.now(),
|
|
1251
|
+
type: "schedule:cancel"
|
|
1252
|
+
},
|
|
1253
|
+
this.ctx
|
|
1254
|
+
);
|
|
1255
|
+
}
|
|
659
1256
|
this.sql`DELETE FROM cf_agents_schedules WHERE id = ${id}`;
|
|
660
1257
|
|
|
661
|
-
await this
|
|
1258
|
+
await this._scheduleNextAlarm();
|
|
662
1259
|
return true;
|
|
663
1260
|
}
|
|
664
1261
|
|
|
665
|
-
async
|
|
1262
|
+
private async _scheduleNextAlarm() {
|
|
666
1263
|
// Find the next schedule that needs to be executed
|
|
667
1264
|
const result = this.sql`
|
|
668
|
-
SELECT time FROM cf_agents_schedules
|
|
1265
|
+
SELECT time FROM cf_agents_schedules
|
|
669
1266
|
WHERE time > ${Math.floor(Date.now() / 1000)}
|
|
670
|
-
ORDER BY time ASC
|
|
1267
|
+
ORDER BY time ASC
|
|
671
1268
|
LIMIT 1
|
|
672
1269
|
`;
|
|
673
1270
|
if (!result) return;
|
|
@@ -679,10 +1276,14 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
679
1276
|
}
|
|
680
1277
|
|
|
681
1278
|
/**
|
|
682
|
-
* Method called when an alarm fires
|
|
683
|
-
* Executes any scheduled tasks that are due
|
|
1279
|
+
* Method called when an alarm fires.
|
|
1280
|
+
* Executes any scheduled tasks that are due.
|
|
1281
|
+
*
|
|
1282
|
+
* @remarks
|
|
1283
|
+
* To schedule a task, please use the `this.schedule` method instead.
|
|
1284
|
+
* See {@link https://developers.cloudflare.com/agents/api-reference/schedule-tasks/}
|
|
684
1285
|
*/
|
|
685
|
-
async
|
|
1286
|
+
public readonly alarm = async () => {
|
|
686
1287
|
const now = Math.floor(Date.now() / 1000);
|
|
687
1288
|
|
|
688
1289
|
// Get all schedules that should be executed now
|
|
@@ -690,46 +1291,67 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
690
1291
|
SELECT * FROM cf_agents_schedules WHERE time <= ${now}
|
|
691
1292
|
`;
|
|
692
1293
|
|
|
693
|
-
|
|
694
|
-
const
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
await unstable_context.run(
|
|
700
|
-
{ agent: this, connection: undefined, request: undefined },
|
|
701
|
-
async () => {
|
|
702
|
-
try {
|
|
703
|
-
await (
|
|
704
|
-
callback as (
|
|
705
|
-
payload: unknown,
|
|
706
|
-
schedule: Schedule<unknown>
|
|
707
|
-
) => Promise<void>
|
|
708
|
-
).bind(this)(JSON.parse(row.payload as string), row);
|
|
709
|
-
} catch (e) {
|
|
710
|
-
console.error(`error executing callback "${row.callback}"`, e);
|
|
711
|
-
}
|
|
1294
|
+
if (result && Array.isArray(result)) {
|
|
1295
|
+
for (const row of result) {
|
|
1296
|
+
const callback = this[row.callback as keyof Agent<Env>];
|
|
1297
|
+
if (!callback) {
|
|
1298
|
+
console.error(`callback ${row.callback} not found`);
|
|
1299
|
+
continue;
|
|
712
1300
|
}
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
1301
|
+
await agentContext.run(
|
|
1302
|
+
{
|
|
1303
|
+
agent: this,
|
|
1304
|
+
connection: undefined,
|
|
1305
|
+
request: undefined,
|
|
1306
|
+
email: undefined
|
|
1307
|
+
},
|
|
1308
|
+
async () => {
|
|
1309
|
+
try {
|
|
1310
|
+
this.observability?.emit(
|
|
1311
|
+
{
|
|
1312
|
+
displayMessage: `Schedule ${row.id} executed`,
|
|
1313
|
+
id: nanoid(),
|
|
1314
|
+
payload: {
|
|
1315
|
+
callback: row.callback,
|
|
1316
|
+
id: row.id
|
|
1317
|
+
},
|
|
1318
|
+
timestamp: Date.now(),
|
|
1319
|
+
type: "schedule:execute"
|
|
1320
|
+
},
|
|
1321
|
+
this.ctx
|
|
1322
|
+
);
|
|
718
1323
|
|
|
719
|
-
|
|
1324
|
+
await (
|
|
1325
|
+
callback as (
|
|
1326
|
+
payload: unknown,
|
|
1327
|
+
schedule: Schedule<unknown>
|
|
1328
|
+
) => Promise<void>
|
|
1329
|
+
).bind(this)(JSON.parse(row.payload as string), row);
|
|
1330
|
+
} catch (e) {
|
|
1331
|
+
console.error(`error executing callback "${row.callback}"`, e);
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
);
|
|
1335
|
+
if (row.type === "cron") {
|
|
1336
|
+
// Update next execution time for cron schedules
|
|
1337
|
+
const nextExecutionTime = getNextCronTime(row.cron);
|
|
1338
|
+
const nextTimestamp = Math.floor(nextExecutionTime.getTime() / 1000);
|
|
1339
|
+
|
|
1340
|
+
this.sql`
|
|
720
1341
|
UPDATE cf_agents_schedules SET time = ${nextTimestamp} WHERE id = ${row.id}
|
|
721
1342
|
`;
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
1343
|
+
} else {
|
|
1344
|
+
// Delete one-time schedules after execution
|
|
1345
|
+
this.sql`
|
|
725
1346
|
DELETE FROM cf_agents_schedules WHERE id = ${row.id}
|
|
726
1347
|
`;
|
|
1348
|
+
}
|
|
727
1349
|
}
|
|
728
1350
|
}
|
|
729
1351
|
|
|
730
1352
|
// Schedule the next alarm
|
|
731
|
-
await this
|
|
732
|
-
}
|
|
1353
|
+
await this._scheduleNextAlarm();
|
|
1354
|
+
};
|
|
733
1355
|
|
|
734
1356
|
/**
|
|
735
1357
|
* Destroy the Agent, removing all state and scheduled tasks
|
|
@@ -738,20 +1360,203 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
738
1360
|
// drop all tables
|
|
739
1361
|
this.sql`DROP TABLE IF EXISTS cf_agents_state`;
|
|
740
1362
|
this.sql`DROP TABLE IF EXISTS cf_agents_schedules`;
|
|
1363
|
+
this.sql`DROP TABLE IF EXISTS cf_agents_mcp_servers`;
|
|
1364
|
+
this.sql`DROP TABLE IF EXISTS cf_agents_queues`;
|
|
741
1365
|
|
|
742
1366
|
// delete all alarms
|
|
743
1367
|
await this.ctx.storage.deleteAlarm();
|
|
744
1368
|
await this.ctx.storage.deleteAll();
|
|
1369
|
+
this.ctx.abort("destroyed"); // enforce that the agent is evicted
|
|
1370
|
+
|
|
1371
|
+
this.observability?.emit(
|
|
1372
|
+
{
|
|
1373
|
+
displayMessage: "Agent destroyed",
|
|
1374
|
+
id: nanoid(),
|
|
1375
|
+
payload: {},
|
|
1376
|
+
timestamp: Date.now(),
|
|
1377
|
+
type: "destroy"
|
|
1378
|
+
},
|
|
1379
|
+
this.ctx
|
|
1380
|
+
);
|
|
745
1381
|
}
|
|
746
1382
|
|
|
747
1383
|
/**
|
|
748
1384
|
* Get all methods marked as callable on this Agent
|
|
749
1385
|
* @returns A map of method names to their metadata
|
|
750
1386
|
*/
|
|
751
|
-
|
|
752
|
-
// biome-ignore lint/complexity/noBannedTypes: <explanation>
|
|
1387
|
+
private _isCallable(method: string): boolean {
|
|
753
1388
|
return callableMetadata.has(this[method as keyof this] as Function);
|
|
754
1389
|
}
|
|
1390
|
+
|
|
1391
|
+
/**
|
|
1392
|
+
* Connect to a new MCP Server
|
|
1393
|
+
*
|
|
1394
|
+
* @param url MCP Server SSE URL
|
|
1395
|
+
* @param callbackHost Base host for the agent, used for the redirect URI.
|
|
1396
|
+
* @param agentsPrefix agents routing prefix if not using `agents`
|
|
1397
|
+
* @param options MCP client and transport (header) options
|
|
1398
|
+
* @returns authUrl
|
|
1399
|
+
*/
|
|
1400
|
+
async addMcpServer(
|
|
1401
|
+
serverName: string,
|
|
1402
|
+
url: string,
|
|
1403
|
+
callbackHost: string,
|
|
1404
|
+
agentsPrefix = "agents",
|
|
1405
|
+
options?: {
|
|
1406
|
+
client?: ConstructorParameters<typeof Client>[1];
|
|
1407
|
+
transport?: {
|
|
1408
|
+
headers: HeadersInit;
|
|
1409
|
+
};
|
|
1410
|
+
}
|
|
1411
|
+
): Promise<{ id: string; authUrl: string | undefined }> {
|
|
1412
|
+
const callbackUrl = `${callbackHost}/${agentsPrefix}/${camelCaseToKebabCase(this._ParentClass.name)}/${this.name}/callback`;
|
|
1413
|
+
|
|
1414
|
+
const result = await this._connectToMcpServerInternal(
|
|
1415
|
+
serverName,
|
|
1416
|
+
url,
|
|
1417
|
+
callbackUrl,
|
|
1418
|
+
options
|
|
1419
|
+
);
|
|
1420
|
+
this.sql`
|
|
1421
|
+
INSERT
|
|
1422
|
+
OR REPLACE INTO cf_agents_mcp_servers (id, name, server_url, client_id, auth_url, callback_url, server_options)
|
|
1423
|
+
VALUES (
|
|
1424
|
+
${result.id},
|
|
1425
|
+
${serverName},
|
|
1426
|
+
${url},
|
|
1427
|
+
${result.clientId ?? null},
|
|
1428
|
+
${result.authUrl ?? null},
|
|
1429
|
+
${callbackUrl},
|
|
1430
|
+
${options ? JSON.stringify(options) : null}
|
|
1431
|
+
);
|
|
1432
|
+
`;
|
|
1433
|
+
|
|
1434
|
+
this.broadcast(
|
|
1435
|
+
JSON.stringify({
|
|
1436
|
+
mcp: this.getMcpServers(),
|
|
1437
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
1438
|
+
})
|
|
1439
|
+
);
|
|
1440
|
+
|
|
1441
|
+
return result;
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
async _connectToMcpServerInternal(
|
|
1445
|
+
_serverName: string,
|
|
1446
|
+
url: string,
|
|
1447
|
+
callbackUrl: string,
|
|
1448
|
+
// it's important that any options here are serializable because we put them into our sqlite DB for reconnection purposes
|
|
1449
|
+
options?: {
|
|
1450
|
+
client?: ConstructorParameters<typeof Client>[1];
|
|
1451
|
+
/**
|
|
1452
|
+
* We don't expose the normal set of transport options because:
|
|
1453
|
+
* 1) we can't serialize things like the auth provider or a fetch function into the DB for reconnection purposes
|
|
1454
|
+
* 2) We probably want these options to be agnostic to the transport type (SSE vs Streamable)
|
|
1455
|
+
*
|
|
1456
|
+
* This has the limitation that you can't override fetch, but I think headers should handle nearly all cases needed (i.e. non-standard bearer auth).
|
|
1457
|
+
*/
|
|
1458
|
+
transport?: {
|
|
1459
|
+
headers?: HeadersInit;
|
|
1460
|
+
};
|
|
1461
|
+
},
|
|
1462
|
+
reconnect?: {
|
|
1463
|
+
id: string;
|
|
1464
|
+
oauthClientId?: string;
|
|
1465
|
+
}
|
|
1466
|
+
): Promise<{
|
|
1467
|
+
id: string;
|
|
1468
|
+
authUrl: string | undefined;
|
|
1469
|
+
clientId: string | undefined;
|
|
1470
|
+
}> {
|
|
1471
|
+
const authProvider = new DurableObjectOAuthClientProvider(
|
|
1472
|
+
this.ctx.storage,
|
|
1473
|
+
this.name,
|
|
1474
|
+
callbackUrl
|
|
1475
|
+
);
|
|
1476
|
+
|
|
1477
|
+
if (reconnect) {
|
|
1478
|
+
authProvider.serverId = reconnect.id;
|
|
1479
|
+
if (reconnect.oauthClientId) {
|
|
1480
|
+
authProvider.clientId = reconnect.oauthClientId;
|
|
1481
|
+
}
|
|
1482
|
+
}
|
|
1483
|
+
|
|
1484
|
+
// allows passing through transport headers if necessary
|
|
1485
|
+
// this handles some non-standard bearer auth setups (i.e. MCP server behind CF access instead of OAuth)
|
|
1486
|
+
let headerTransportOpts: SSEClientTransportOptions = {};
|
|
1487
|
+
if (options?.transport?.headers) {
|
|
1488
|
+
headerTransportOpts = {
|
|
1489
|
+
eventSourceInit: {
|
|
1490
|
+
fetch: (url, init) =>
|
|
1491
|
+
fetch(url, {
|
|
1492
|
+
...init,
|
|
1493
|
+
headers: options?.transport?.headers
|
|
1494
|
+
})
|
|
1495
|
+
},
|
|
1496
|
+
requestInit: {
|
|
1497
|
+
headers: options?.transport?.headers
|
|
1498
|
+
}
|
|
1499
|
+
};
|
|
1500
|
+
}
|
|
1501
|
+
|
|
1502
|
+
const { id, authUrl, clientId } = await this.mcp.connect(url, {
|
|
1503
|
+
client: options?.client,
|
|
1504
|
+
reconnect,
|
|
1505
|
+
transport: {
|
|
1506
|
+
...headerTransportOpts,
|
|
1507
|
+
authProvider
|
|
1508
|
+
}
|
|
1509
|
+
});
|
|
1510
|
+
|
|
1511
|
+
return {
|
|
1512
|
+
authUrl,
|
|
1513
|
+
clientId,
|
|
1514
|
+
id
|
|
1515
|
+
};
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
async removeMcpServer(id: string) {
|
|
1519
|
+
this.mcp.closeConnection(id);
|
|
1520
|
+
this.sql`
|
|
1521
|
+
DELETE FROM cf_agents_mcp_servers WHERE id = ${id};
|
|
1522
|
+
`;
|
|
1523
|
+
this.broadcast(
|
|
1524
|
+
JSON.stringify({
|
|
1525
|
+
mcp: this.getMcpServers(),
|
|
1526
|
+
type: MessageType.CF_AGENT_MCP_SERVERS
|
|
1527
|
+
})
|
|
1528
|
+
);
|
|
1529
|
+
}
|
|
1530
|
+
|
|
1531
|
+
getMcpServers(): MCPServersState {
|
|
1532
|
+
const mcpState: MCPServersState = {
|
|
1533
|
+
prompts: this.mcp.listPrompts(),
|
|
1534
|
+
resources: this.mcp.listResources(),
|
|
1535
|
+
servers: {},
|
|
1536
|
+
tools: this.mcp.listTools()
|
|
1537
|
+
};
|
|
1538
|
+
|
|
1539
|
+
const servers = this.sql<MCPServerRow>`
|
|
1540
|
+
SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
|
|
1541
|
+
`;
|
|
1542
|
+
|
|
1543
|
+
if (servers && Array.isArray(servers) && servers.length > 0) {
|
|
1544
|
+
for (const server of servers) {
|
|
1545
|
+
const serverConn = this.mcp.mcpConnections[server.id];
|
|
1546
|
+
mcpState.servers[server.id] = {
|
|
1547
|
+
auth_url: server.auth_url,
|
|
1548
|
+
capabilities: serverConn?.serverCapabilities ?? null,
|
|
1549
|
+
instructions: serverConn?.instructions ?? null,
|
|
1550
|
+
name: server.name,
|
|
1551
|
+
server_url: server.server_url,
|
|
1552
|
+
// mark as "authenticating" because the server isn't automatically connected, so it's pending authenticating
|
|
1553
|
+
state: serverConn?.connectionState ?? "authenticating"
|
|
1554
|
+
};
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
return mcpState;
|
|
1559
|
+
}
|
|
755
1560
|
}
|
|
756
1561
|
|
|
757
1562
|
/**
|
|
@@ -791,17 +1596,17 @@ export async function routeAgentRequest<Env>(
|
|
|
791
1596
|
const corsHeaders =
|
|
792
1597
|
options?.cors === true
|
|
793
1598
|
? {
|
|
794
|
-
"Access-Control-Allow-Origin": "*",
|
|
795
|
-
"Access-Control-Allow-Methods": "GET, POST, HEAD, OPTIONS",
|
|
796
1599
|
"Access-Control-Allow-Credentials": "true",
|
|
797
|
-
"Access-Control-
|
|
1600
|
+
"Access-Control-Allow-Methods": "GET, POST, HEAD, OPTIONS",
|
|
1601
|
+
"Access-Control-Allow-Origin": "*",
|
|
1602
|
+
"Access-Control-Max-Age": "86400"
|
|
798
1603
|
}
|
|
799
1604
|
: options?.cors;
|
|
800
1605
|
|
|
801
1606
|
if (request.method === "OPTIONS") {
|
|
802
1607
|
if (corsHeaders) {
|
|
803
1608
|
return new Response(null, {
|
|
804
|
-
headers: corsHeaders
|
|
1609
|
+
headers: corsHeaders
|
|
805
1610
|
});
|
|
806
1611
|
}
|
|
807
1612
|
console.warn(
|
|
@@ -814,7 +1619,7 @@ export async function routeAgentRequest<Env>(
|
|
|
814
1619
|
env as Record<string, unknown>,
|
|
815
1620
|
{
|
|
816
1621
|
prefix: "agents",
|
|
817
|
-
...(options as PartyServerOptions<Record<string, unknown>>)
|
|
1622
|
+
...(options as PartyServerOptions<Record<string, unknown>>)
|
|
818
1623
|
}
|
|
819
1624
|
);
|
|
820
1625
|
|
|
@@ -827,24 +1632,238 @@ export async function routeAgentRequest<Env>(
|
|
|
827
1632
|
response = new Response(response.body, {
|
|
828
1633
|
headers: {
|
|
829
1634
|
...response.headers,
|
|
830
|
-
...corsHeaders
|
|
831
|
-
}
|
|
1635
|
+
...corsHeaders
|
|
1636
|
+
}
|
|
832
1637
|
});
|
|
833
1638
|
}
|
|
834
1639
|
return response;
|
|
835
1640
|
}
|
|
836
1641
|
|
|
1642
|
+
export type EmailResolver<Env> = (
|
|
1643
|
+
email: ForwardableEmailMessage,
|
|
1644
|
+
env: Env
|
|
1645
|
+
) => Promise<{
|
|
1646
|
+
agentName: string;
|
|
1647
|
+
agentId: string;
|
|
1648
|
+
} | null>;
|
|
1649
|
+
|
|
1650
|
+
/**
|
|
1651
|
+
* Create a resolver that uses the message-id header to determine the agent to route the email to
|
|
1652
|
+
* @returns A function that resolves the agent to route the email to
|
|
1653
|
+
*/
|
|
1654
|
+
export function createHeaderBasedEmailResolver<Env>(): EmailResolver<Env> {
|
|
1655
|
+
return async (email: ForwardableEmailMessage, _env: Env) => {
|
|
1656
|
+
const messageId = email.headers.get("message-id");
|
|
1657
|
+
if (messageId) {
|
|
1658
|
+
const messageIdMatch = messageId.match(/<([^@]+)@([^>]+)>/);
|
|
1659
|
+
if (messageIdMatch) {
|
|
1660
|
+
const [, agentId, domain] = messageIdMatch;
|
|
1661
|
+
const agentName = domain.split(".")[0];
|
|
1662
|
+
return { agentName, agentId };
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
|
|
1666
|
+
const references = email.headers.get("references");
|
|
1667
|
+
if (references) {
|
|
1668
|
+
const referencesMatch = references.match(
|
|
1669
|
+
/<([A-Za-z0-9+/]{43}=)@([^>]+)>/
|
|
1670
|
+
);
|
|
1671
|
+
if (referencesMatch) {
|
|
1672
|
+
const [, base64Id, domain] = referencesMatch;
|
|
1673
|
+
const agentId = Buffer.from(base64Id, "base64").toString("hex");
|
|
1674
|
+
const agentName = domain.split(".")[0];
|
|
1675
|
+
return { agentName, agentId };
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1678
|
+
|
|
1679
|
+
const agentName = email.headers.get("x-agent-name");
|
|
1680
|
+
const agentId = email.headers.get("x-agent-id");
|
|
1681
|
+
if (agentName && agentId) {
|
|
1682
|
+
return { agentName, agentId };
|
|
1683
|
+
}
|
|
1684
|
+
|
|
1685
|
+
return null;
|
|
1686
|
+
};
|
|
1687
|
+
}
|
|
1688
|
+
|
|
1689
|
+
/**
|
|
1690
|
+
* Create a resolver that uses the email address to determine the agent to route the email to
|
|
1691
|
+
* @param defaultAgentName The default agent name to use if the email address does not contain a sub-address
|
|
1692
|
+
* @returns A function that resolves the agent to route the email to
|
|
1693
|
+
*/
|
|
1694
|
+
export function createAddressBasedEmailResolver<Env>(
|
|
1695
|
+
defaultAgentName: string
|
|
1696
|
+
): EmailResolver<Env> {
|
|
1697
|
+
return async (email: ForwardableEmailMessage, _env: Env) => {
|
|
1698
|
+
const emailMatch = email.to.match(/^([^+@]+)(?:\+([^@]+))?@(.+)$/);
|
|
1699
|
+
if (!emailMatch) {
|
|
1700
|
+
return null;
|
|
1701
|
+
}
|
|
1702
|
+
|
|
1703
|
+
const [, localPart, subAddress] = emailMatch;
|
|
1704
|
+
|
|
1705
|
+
if (subAddress) {
|
|
1706
|
+
return {
|
|
1707
|
+
agentName: localPart,
|
|
1708
|
+
agentId: subAddress
|
|
1709
|
+
};
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1712
|
+
// Option 2: Use defaultAgentName namespace, localPart as agentId
|
|
1713
|
+
// Common for catch-all email routing to a single EmailAgent namespace
|
|
1714
|
+
return {
|
|
1715
|
+
agentName: defaultAgentName,
|
|
1716
|
+
agentId: localPart
|
|
1717
|
+
};
|
|
1718
|
+
};
|
|
1719
|
+
}
|
|
1720
|
+
|
|
1721
|
+
/**
|
|
1722
|
+
* Create a resolver that uses the agentName and agentId to determine the agent to route the email to
|
|
1723
|
+
* @param agentName The name of the agent to route the email to
|
|
1724
|
+
* @param agentId The id of the agent to route the email to
|
|
1725
|
+
* @returns A function that resolves the agent to route the email to
|
|
1726
|
+
*/
|
|
1727
|
+
export function createCatchAllEmailResolver<Env>(
|
|
1728
|
+
agentName: string,
|
|
1729
|
+
agentId: string
|
|
1730
|
+
): EmailResolver<Env> {
|
|
1731
|
+
return async () => ({ agentName, agentId });
|
|
1732
|
+
}
|
|
1733
|
+
|
|
1734
|
+
export type EmailRoutingOptions<Env> = AgentOptions<Env> & {
|
|
1735
|
+
resolver: EmailResolver<Env>;
|
|
1736
|
+
};
|
|
1737
|
+
|
|
1738
|
+
// Cache the agent namespace map for email routing
|
|
1739
|
+
// This maps both kebab-case and original names to namespaces
|
|
1740
|
+
const agentMapCache = new WeakMap<
|
|
1741
|
+
Record<string, unknown>,
|
|
1742
|
+
Record<string, unknown>
|
|
1743
|
+
>();
|
|
1744
|
+
|
|
837
1745
|
/**
|
|
838
1746
|
* Route an email to the appropriate Agent
|
|
839
|
-
* @param email
|
|
840
|
-
* @param env
|
|
841
|
-
* @param options
|
|
1747
|
+
* @param email The email to route
|
|
1748
|
+
* @param env The environment containing the Agent bindings
|
|
1749
|
+
* @param options The options for routing the email
|
|
1750
|
+
* @returns A promise that resolves when the email has been routed
|
|
842
1751
|
*/
|
|
843
1752
|
export async function routeAgentEmail<Env>(
|
|
844
1753
|
email: ForwardableEmailMessage,
|
|
845
1754
|
env: Env,
|
|
846
|
-
options
|
|
847
|
-
): Promise<void> {
|
|
1755
|
+
options: EmailRoutingOptions<Env>
|
|
1756
|
+
): Promise<void> {
|
|
1757
|
+
const routingInfo = await options.resolver(email, env);
|
|
1758
|
+
|
|
1759
|
+
if (!routingInfo) {
|
|
1760
|
+
console.warn("No routing information found for email, dropping message");
|
|
1761
|
+
return;
|
|
1762
|
+
}
|
|
1763
|
+
|
|
1764
|
+
// Build a map that includes both original names and kebab-case versions
|
|
1765
|
+
if (!agentMapCache.has(env as Record<string, unknown>)) {
|
|
1766
|
+
const map: Record<string, unknown> = {};
|
|
1767
|
+
for (const [key, value] of Object.entries(env as Record<string, unknown>)) {
|
|
1768
|
+
if (
|
|
1769
|
+
value &&
|
|
1770
|
+
typeof value === "object" &&
|
|
1771
|
+
"idFromName" in value &&
|
|
1772
|
+
typeof value.idFromName === "function"
|
|
1773
|
+
) {
|
|
1774
|
+
// Add both the original name and kebab-case version
|
|
1775
|
+
map[key] = value;
|
|
1776
|
+
map[camelCaseToKebabCase(key)] = value;
|
|
1777
|
+
}
|
|
1778
|
+
}
|
|
1779
|
+
agentMapCache.set(env as Record<string, unknown>, map);
|
|
1780
|
+
}
|
|
1781
|
+
|
|
1782
|
+
const agentMap = agentMapCache.get(env as Record<string, unknown>)!;
|
|
1783
|
+
const namespace = agentMap[routingInfo.agentName];
|
|
1784
|
+
|
|
1785
|
+
if (!namespace) {
|
|
1786
|
+
// Provide helpful error message listing available agents
|
|
1787
|
+
const availableAgents = Object.keys(agentMap)
|
|
1788
|
+
.filter((key) => !key.includes("-")) // Show only original names, not kebab-case duplicates
|
|
1789
|
+
.join(", ");
|
|
1790
|
+
throw new Error(
|
|
1791
|
+
`Agent namespace '${routingInfo.agentName}' not found in environment. Available agents: ${availableAgents}`
|
|
1792
|
+
);
|
|
1793
|
+
}
|
|
1794
|
+
|
|
1795
|
+
const agent = await getAgentByName(
|
|
1796
|
+
namespace as unknown as AgentNamespace<Agent<Env>>,
|
|
1797
|
+
routingInfo.agentId
|
|
1798
|
+
);
|
|
1799
|
+
|
|
1800
|
+
// let's make a serialisable version of the email
|
|
1801
|
+
const serialisableEmail: AgentEmail = {
|
|
1802
|
+
getRaw: async () => {
|
|
1803
|
+
const reader = email.raw.getReader();
|
|
1804
|
+
const chunks: Uint8Array[] = [];
|
|
1805
|
+
|
|
1806
|
+
let done = false;
|
|
1807
|
+
while (!done) {
|
|
1808
|
+
const { value, done: readerDone } = await reader.read();
|
|
1809
|
+
done = readerDone;
|
|
1810
|
+
if (value) {
|
|
1811
|
+
chunks.push(value);
|
|
1812
|
+
}
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
|
|
1816
|
+
const combined = new Uint8Array(totalLength);
|
|
1817
|
+
let offset = 0;
|
|
1818
|
+
for (const chunk of chunks) {
|
|
1819
|
+
combined.set(chunk, offset);
|
|
1820
|
+
offset += chunk.length;
|
|
1821
|
+
}
|
|
1822
|
+
|
|
1823
|
+
return combined;
|
|
1824
|
+
},
|
|
1825
|
+
headers: email.headers,
|
|
1826
|
+
rawSize: email.rawSize,
|
|
1827
|
+
setReject: (reason: string) => {
|
|
1828
|
+
email.setReject(reason);
|
|
1829
|
+
},
|
|
1830
|
+
forward: (rcptTo: string, headers?: Headers) => {
|
|
1831
|
+
return email.forward(rcptTo, headers);
|
|
1832
|
+
},
|
|
1833
|
+
reply: (options: { from: string; to: string; raw: string }) => {
|
|
1834
|
+
return email.reply(
|
|
1835
|
+
new EmailMessage(options.from, options.to, options.raw)
|
|
1836
|
+
);
|
|
1837
|
+
},
|
|
1838
|
+
from: email.from,
|
|
1839
|
+
to: email.to
|
|
1840
|
+
};
|
|
1841
|
+
|
|
1842
|
+
await agent._onEmail(serialisableEmail);
|
|
1843
|
+
}
|
|
1844
|
+
|
|
1845
|
+
export type AgentEmail = {
|
|
1846
|
+
from: string;
|
|
1847
|
+
to: string;
|
|
1848
|
+
getRaw: () => Promise<Uint8Array>;
|
|
1849
|
+
headers: Headers;
|
|
1850
|
+
rawSize: number;
|
|
1851
|
+
setReject: (reason: string) => void;
|
|
1852
|
+
forward: (rcptTo: string, headers?: Headers) => Promise<void>;
|
|
1853
|
+
reply: (options: { from: string; to: string; raw: string }) => Promise<void>;
|
|
1854
|
+
};
|
|
1855
|
+
|
|
1856
|
+
export type EmailSendOptions = {
|
|
1857
|
+
to: string;
|
|
1858
|
+
subject: string;
|
|
1859
|
+
body: string;
|
|
1860
|
+
contentType?: string;
|
|
1861
|
+
headers?: Record<string, string>;
|
|
1862
|
+
includeRoutingHeaders?: boolean;
|
|
1863
|
+
agentName?: string;
|
|
1864
|
+
agentId?: string;
|
|
1865
|
+
domain?: string;
|
|
1866
|
+
};
|
|
848
1867
|
|
|
849
1868
|
/**
|
|
850
1869
|
* Get or create an Agent by name
|
|
@@ -855,7 +1874,7 @@ export async function routeAgentEmail<Env>(
|
|
|
855
1874
|
* @param options Options for Agent creation
|
|
856
1875
|
* @returns Promise resolving to an Agent instance stub
|
|
857
1876
|
*/
|
|
858
|
-
export function getAgentByName<Env, T extends Agent<Env>>(
|
|
1877
|
+
export async function getAgentByName<Env, T extends Agent<Env>>(
|
|
859
1878
|
namespace: AgentNamespace<T>,
|
|
860
1879
|
name: string,
|
|
861
1880
|
options?: {
|
|
@@ -870,13 +1889,13 @@ export function getAgentByName<Env, T extends Agent<Env>>(
|
|
|
870
1889
|
* A wrapper for streaming responses in callable methods
|
|
871
1890
|
*/
|
|
872
1891
|
export class StreamingResponse {
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
1892
|
+
private _connection: Connection;
|
|
1893
|
+
private _id: string;
|
|
1894
|
+
private _closed = false;
|
|
876
1895
|
|
|
877
1896
|
constructor(connection: Connection, id: string) {
|
|
878
|
-
this
|
|
879
|
-
this
|
|
1897
|
+
this._connection = connection;
|
|
1898
|
+
this._id = id;
|
|
880
1899
|
}
|
|
881
1900
|
|
|
882
1901
|
/**
|
|
@@ -884,17 +1903,17 @@ export class StreamingResponse {
|
|
|
884
1903
|
* @param chunk The data to send
|
|
885
1904
|
*/
|
|
886
1905
|
send(chunk: unknown) {
|
|
887
|
-
if (this
|
|
1906
|
+
if (this._closed) {
|
|
888
1907
|
throw new Error("StreamingResponse is already closed");
|
|
889
1908
|
}
|
|
890
1909
|
const response: RPCResponse = {
|
|
891
|
-
type: "rpc",
|
|
892
|
-
id: this.#id,
|
|
893
|
-
success: true,
|
|
894
|
-
result: chunk,
|
|
895
1910
|
done: false,
|
|
1911
|
+
id: this._id,
|
|
1912
|
+
result: chunk,
|
|
1913
|
+
success: true,
|
|
1914
|
+
type: MessageType.RPC
|
|
896
1915
|
};
|
|
897
|
-
this
|
|
1916
|
+
this._connection.send(JSON.stringify(response));
|
|
898
1917
|
}
|
|
899
1918
|
|
|
900
1919
|
/**
|
|
@@ -902,17 +1921,17 @@ export class StreamingResponse {
|
|
|
902
1921
|
* @param finalChunk Optional final chunk of data to send
|
|
903
1922
|
*/
|
|
904
1923
|
end(finalChunk?: unknown) {
|
|
905
|
-
if (this
|
|
1924
|
+
if (this._closed) {
|
|
906
1925
|
throw new Error("StreamingResponse is already closed");
|
|
907
1926
|
}
|
|
908
|
-
this
|
|
1927
|
+
this._closed = true;
|
|
909
1928
|
const response: RPCResponse = {
|
|
910
|
-
type: "rpc",
|
|
911
|
-
id: this.#id,
|
|
912
|
-
success: true,
|
|
913
|
-
result: finalChunk,
|
|
914
1929
|
done: true,
|
|
1930
|
+
id: this._id,
|
|
1931
|
+
result: finalChunk,
|
|
1932
|
+
success: true,
|
|
1933
|
+
type: MessageType.RPC
|
|
915
1934
|
};
|
|
916
|
-
this
|
|
1935
|
+
this._connection.send(JSON.stringify(response));
|
|
917
1936
|
}
|
|
918
1937
|
}
|