agents 0.0.0-25aeaf2 → 0.0.0-263787c
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 +128 -22
- package/dist/ai-chat-agent.d.ts +35 -6
- package/dist/ai-chat-agent.js +149 -117
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +13 -10
- package/dist/ai-react.js +27 -29
- package/dist/ai-react.js.map +1 -1
- package/dist/{chunk-25YDMV4H.js → chunk-HY7ZLHJB.js} +178 -44
- package/dist/chunk-HY7ZLHJB.js.map +1 -0
- package/dist/chunk-JXN5WZFQ.js +1287 -0
- package/dist/chunk-JXN5WZFQ.js.map +1 -0
- package/dist/{chunk-RN4SNE73.js → chunk-KUH345EY.js} +17 -34
- package/dist/chunk-KUH345EY.js.map +1 -0
- package/dist/{chunk-D6UOOELW.js → chunk-PVQZBKN7.js} +15 -15
- package/dist/chunk-PVQZBKN7.js.map +1 -0
- package/dist/client-DgyzBU_8.d.ts +4601 -0
- package/dist/client.d.ts +10 -3
- package/dist/client.js +1 -2
- package/dist/index-BCJclX6q.d.ts +615 -0
- package/dist/index.d.ts +35 -361
- package/dist/index.js +10 -5
- package/dist/mcp/client.d.ts +9 -781
- 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 +1 -2
- package/dist/mcp/index.d.ts +48 -11
- package/dist/mcp/index.js +317 -180
- package/dist/mcp/index.js.map +1 -1
- package/dist/observability/index.d.ts +14 -0
- package/dist/observability/index.js +10 -0
- package/dist/react.d.ts +79 -11
- package/dist/react.js +16 -8
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +10 -10
- 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 +76 -71
- package/src/index.ts +895 -222
- package/dist/chunk-25YDMV4H.js.map +0 -1
- package/dist/chunk-D6UOOELW.js.map +0 -1
- package/dist/chunk-HMLY7DHA.js +0 -16
- package/dist/chunk-RN4SNE73.js.map +0 -1
- package/dist/chunk-YFPCCSZO.js +0 -787
- package/dist/chunk-YFPCCSZO.js.map +0 -1
- /package/dist/{chunk-HMLY7DHA.js.map → observability/index.js.map} +0 -0
package/src/index.ts
CHANGED
|
@@ -1,34 +1,33 @@
|
|
|
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";
|
|
15
|
-
import { MCPClientManager } from "./mcp/client";
|
|
16
|
-
import {
|
|
17
|
-
DurableObjectOAuthClientProvider,
|
|
18
|
-
type AgentsOAuthProvider,
|
|
19
|
-
} from "./mcp/do-oauth-client-provider";
|
|
20
|
-
import type {
|
|
21
|
-
Tool,
|
|
22
|
-
Resource,
|
|
23
|
-
Prompt,
|
|
24
|
-
} from "@modelcontextprotocol/sdk/types.js";
|
|
25
|
-
|
|
26
|
-
import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
27
|
-
import type { SSEClientTransportOptions } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
28
|
-
|
|
29
24
|
import { camelCaseToKebabCase } from "./client";
|
|
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";
|
|
30
29
|
|
|
31
|
-
export type { Connection,
|
|
30
|
+
export type { Connection, ConnectionContext, WSMessage } from "partyserver";
|
|
32
31
|
|
|
33
32
|
/**
|
|
34
33
|
* RPC request message from client
|
|
@@ -112,7 +111,6 @@ export type CallableMetadata = {
|
|
|
112
111
|
streaming?: boolean;
|
|
113
112
|
};
|
|
114
113
|
|
|
115
|
-
// biome-ignore lint/complexity/noBannedTypes: <explanation>
|
|
116
114
|
const callableMetadata = new Map<Function, CallableMetadata>();
|
|
117
115
|
|
|
118
116
|
/**
|
|
@@ -122,6 +120,7 @@ const callableMetadata = new Map<Function, CallableMetadata>();
|
|
|
122
120
|
export function unstable_callable(metadata: CallableMetadata = {}) {
|
|
123
121
|
return function callableDecorator<This, Args extends unknown[], Return>(
|
|
124
122
|
target: (this: This, ...args: Args) => Return,
|
|
123
|
+
// biome-ignore lint/correctness/noUnusedFunctionParameters: later
|
|
125
124
|
context: ClassMethodDecoratorContext
|
|
126
125
|
) {
|
|
127
126
|
if (!callableMetadata.has(target)) {
|
|
@@ -132,6 +131,13 @@ export function unstable_callable(metadata: CallableMetadata = {}) {
|
|
|
132
131
|
};
|
|
133
132
|
}
|
|
134
133
|
|
|
134
|
+
export type QueueItem<T = string> = {
|
|
135
|
+
id: string;
|
|
136
|
+
payload: T;
|
|
137
|
+
callback: keyof Agent<unknown>;
|
|
138
|
+
created_at: number;
|
|
139
|
+
};
|
|
140
|
+
|
|
135
141
|
/**
|
|
136
142
|
* Represents a scheduled task within an Agent
|
|
137
143
|
* @template T Type of the payload data
|
|
@@ -194,7 +200,12 @@ export type MCPServer = {
|
|
|
194
200
|
name: string;
|
|
195
201
|
server_url: string;
|
|
196
202
|
auth_url: string | null;
|
|
203
|
+
// This state is specifically about the temporary process of getting a token (if needed).
|
|
204
|
+
// Scope outside of that can't be relied upon because when the DO sleeps, there's no way
|
|
205
|
+
// to communicate a change to a non-ready state.
|
|
197
206
|
state: "authenticating" | "connecting" | "ready" | "discovering" | "failed";
|
|
207
|
+
instructions: string | null;
|
|
208
|
+
capabilities: ServerCapabilities | null;
|
|
198
209
|
};
|
|
199
210
|
|
|
200
211
|
/**
|
|
@@ -216,23 +227,26 @@ const STATE_WAS_CHANGED = "cf_state_was_changed";
|
|
|
216
227
|
const DEFAULT_STATE = {} as unknown;
|
|
217
228
|
|
|
218
229
|
const agentContext = new AsyncLocalStorage<{
|
|
219
|
-
agent: Agent<unknown>;
|
|
230
|
+
agent: Agent<unknown, unknown>;
|
|
220
231
|
connection: Connection | undefined;
|
|
221
232
|
request: Request | undefined;
|
|
233
|
+
email: AgentEmail | undefined;
|
|
222
234
|
}>();
|
|
223
235
|
|
|
224
236
|
export function getCurrentAgent<
|
|
225
|
-
T extends Agent<unknown, unknown> = Agent<unknown, unknown
|
|
237
|
+
T extends Agent<unknown, unknown> = Agent<unknown, unknown>
|
|
226
238
|
>(): {
|
|
227
239
|
agent: T | undefined;
|
|
228
240
|
connection: Connection | undefined;
|
|
229
|
-
request: Request
|
|
241
|
+
request: Request | undefined;
|
|
242
|
+
email: AgentEmail | undefined;
|
|
230
243
|
} {
|
|
231
244
|
const store = agentContext.getStore() as
|
|
232
245
|
| {
|
|
233
246
|
agent: T;
|
|
234
247
|
connection: Connection | undefined;
|
|
235
|
-
request: Request
|
|
248
|
+
request: Request | undefined;
|
|
249
|
+
email: AgentEmail | undefined;
|
|
236
250
|
}
|
|
237
251
|
| undefined;
|
|
238
252
|
if (!store) {
|
|
@@ -240,23 +254,43 @@ export function getCurrentAgent<
|
|
|
240
254
|
agent: undefined,
|
|
241
255
|
connection: undefined,
|
|
242
256
|
request: undefined,
|
|
257
|
+
email: undefined
|
|
243
258
|
};
|
|
244
259
|
}
|
|
245
260
|
return store;
|
|
246
261
|
}
|
|
247
262
|
|
|
263
|
+
/**
|
|
264
|
+
* Wraps a method to run within the agent context, ensuring getCurrentAgent() works properly
|
|
265
|
+
* @param agent The agent instance
|
|
266
|
+
* @param method The method to wrap
|
|
267
|
+
* @returns A wrapped method that runs within the agent context
|
|
268
|
+
*/
|
|
269
|
+
|
|
270
|
+
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
|
|
271
|
+
function withAgentContext<T extends (...args: any[]) => any>(
|
|
272
|
+
method: T
|
|
273
|
+
): (this: Agent<unknown, unknown>, ...args: Parameters<T>) => ReturnType<T> {
|
|
274
|
+
return function (...args: Parameters<T>): ReturnType<T> {
|
|
275
|
+
const { connection, request, email } = getCurrentAgent();
|
|
276
|
+
return agentContext.run({ agent: this, connection, request, email }, () => {
|
|
277
|
+
return method.apply(this, args);
|
|
278
|
+
});
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
|
|
248
282
|
/**
|
|
249
283
|
* Base class for creating Agent implementations
|
|
250
284
|
* @template Env Environment type containing bindings
|
|
251
285
|
* @template State State type to store within the Agent
|
|
252
286
|
*/
|
|
253
|
-
export class Agent<Env, State = unknown> extends Server<Env> {
|
|
254
|
-
|
|
287
|
+
export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
|
|
288
|
+
private _state = DEFAULT_STATE as State;
|
|
255
289
|
|
|
256
|
-
|
|
290
|
+
private _ParentClass: typeof Agent<Env, State> =
|
|
257
291
|
Object.getPrototypeOf(this).constructor;
|
|
258
292
|
|
|
259
|
-
mcp: MCPClientManager = new MCPClientManager(this
|
|
293
|
+
mcp: MCPClientManager = new MCPClientManager(this._ParentClass.name, "0.0.1");
|
|
260
294
|
|
|
261
295
|
/**
|
|
262
296
|
* Initial state for the Agent
|
|
@@ -268,9 +302,9 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
268
302
|
* Current state of the Agent
|
|
269
303
|
*/
|
|
270
304
|
get state(): State {
|
|
271
|
-
if (this
|
|
305
|
+
if (this._state !== DEFAULT_STATE) {
|
|
272
306
|
// state was previously set, and populated internal state
|
|
273
|
-
return this
|
|
307
|
+
return this._state;
|
|
274
308
|
}
|
|
275
309
|
// looks like this is the first time the state is being accessed
|
|
276
310
|
// check if the state was set in a previous life
|
|
@@ -290,8 +324,8 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
290
324
|
) {
|
|
291
325
|
const state = result[0]?.state as string; // could be null?
|
|
292
326
|
|
|
293
|
-
this
|
|
294
|
-
return this
|
|
327
|
+
this._state = JSON.parse(state);
|
|
328
|
+
return this._state;
|
|
295
329
|
}
|
|
296
330
|
|
|
297
331
|
// ok, this is the first time the state is being accessed
|
|
@@ -312,9 +346,14 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
312
346
|
*/
|
|
313
347
|
static options = {
|
|
314
348
|
/** Whether the Agent should hibernate when inactive */
|
|
315
|
-
hibernate: true
|
|
349
|
+
hibernate: true // default to hibernate
|
|
316
350
|
};
|
|
317
351
|
|
|
352
|
+
/**
|
|
353
|
+
* The observability implementation to use for the Agent
|
|
354
|
+
*/
|
|
355
|
+
observability?: Observability = genericObservability;
|
|
356
|
+
|
|
318
357
|
/**
|
|
319
358
|
* Execute SQL queries against the Agent's database
|
|
320
359
|
* @template T Type of the returned rows
|
|
@@ -344,6 +383,9 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
344
383
|
constructor(ctx: AgentContext, env: Env) {
|
|
345
384
|
super(ctx, env);
|
|
346
385
|
|
|
386
|
+
// Auto-wrap custom methods with agent context
|
|
387
|
+
this._autoWrapCustomMethods();
|
|
388
|
+
|
|
347
389
|
this.sql`
|
|
348
390
|
CREATE TABLE IF NOT EXISTS cf_agents_state (
|
|
349
391
|
id TEXT PRIMARY KEY NOT NULL,
|
|
@@ -351,8 +393,17 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
351
393
|
)
|
|
352
394
|
`;
|
|
353
395
|
|
|
396
|
+
this.sql`
|
|
397
|
+
CREATE TABLE IF NOT EXISTS cf_agents_queues (
|
|
398
|
+
id TEXT PRIMARY KEY NOT NULL,
|
|
399
|
+
payload TEXT,
|
|
400
|
+
callback TEXT,
|
|
401
|
+
created_at INTEGER DEFAULT (unixepoch())
|
|
402
|
+
)
|
|
403
|
+
`;
|
|
404
|
+
|
|
354
405
|
void this.ctx.blockConcurrencyWhile(async () => {
|
|
355
|
-
return this
|
|
406
|
+
return this._tryCatch(async () => {
|
|
356
407
|
// Create alarms table if it doesn't exist
|
|
357
408
|
this.sql`
|
|
358
409
|
CREATE TABLE IF NOT EXISTS cf_agents_schedules (
|
|
@@ -387,7 +438,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
387
438
|
const _onRequest = this.onRequest.bind(this);
|
|
388
439
|
this.onRequest = (request: Request) => {
|
|
389
440
|
return agentContext.run(
|
|
390
|
-
{ agent: this, connection: undefined, request },
|
|
441
|
+
{ agent: this, connection: undefined, request, email: undefined },
|
|
391
442
|
async () => {
|
|
392
443
|
if (this.mcp.isCallbackRequest(request)) {
|
|
393
444
|
await this.mcp.handleCallbackRequest(request);
|
|
@@ -395,19 +446,19 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
395
446
|
// after the MCP connection handshake, we can send updated mcp state
|
|
396
447
|
this.broadcast(
|
|
397
448
|
JSON.stringify({
|
|
398
|
-
|
|
399
|
-
|
|
449
|
+
mcp: this.getMcpServers(),
|
|
450
|
+
type: "cf_agent_mcp_servers"
|
|
400
451
|
})
|
|
401
452
|
);
|
|
402
453
|
|
|
403
454
|
// We probably should let the user configure this response/redirect, but this is fine for now.
|
|
404
455
|
return new Response("<script>window.close();</script>", {
|
|
405
|
-
status: 200,
|
|
406
456
|
headers: { "content-type": "text/html" },
|
|
457
|
+
status: 200
|
|
407
458
|
});
|
|
408
459
|
}
|
|
409
460
|
|
|
410
|
-
return this
|
|
461
|
+
return this._tryCatch(() => _onRequest(request));
|
|
411
462
|
}
|
|
412
463
|
);
|
|
413
464
|
};
|
|
@@ -415,22 +466,22 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
415
466
|
const _onMessage = this.onMessage.bind(this);
|
|
416
467
|
this.onMessage = async (connection: Connection, message: WSMessage) => {
|
|
417
468
|
return agentContext.run(
|
|
418
|
-
{ agent: this, connection, request: undefined },
|
|
469
|
+
{ agent: this, connection, request: undefined, email: undefined },
|
|
419
470
|
async () => {
|
|
420
471
|
if (typeof message !== "string") {
|
|
421
|
-
return this
|
|
472
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
422
473
|
}
|
|
423
474
|
|
|
424
475
|
let parsed: unknown;
|
|
425
476
|
try {
|
|
426
477
|
parsed = JSON.parse(message);
|
|
427
|
-
} catch (
|
|
478
|
+
} catch (_e) {
|
|
428
479
|
// silently fail and let the onMessage handler handle it
|
|
429
|
-
return this
|
|
480
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
430
481
|
}
|
|
431
482
|
|
|
432
483
|
if (isStateUpdateMessage(parsed)) {
|
|
433
|
-
this
|
|
484
|
+
this._setStateInternal(parsed.state as State, connection);
|
|
434
485
|
return;
|
|
435
486
|
}
|
|
436
487
|
|
|
@@ -444,11 +495,10 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
444
495
|
throw new Error(`Method ${method} does not exist`);
|
|
445
496
|
}
|
|
446
497
|
|
|
447
|
-
if (!this
|
|
498
|
+
if (!this._isCallable(method)) {
|
|
448
499
|
throw new Error(`Method ${method} is not callable`);
|
|
449
500
|
}
|
|
450
501
|
|
|
451
|
-
// biome-ignore lint/complexity/noBannedTypes: <explanation>
|
|
452
502
|
const metadata = callableMetadata.get(methodFn as Function);
|
|
453
503
|
|
|
454
504
|
// For streaming methods, pass a StreamingResponse object
|
|
@@ -460,22 +510,39 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
460
510
|
|
|
461
511
|
// For regular methods, execute and send response
|
|
462
512
|
const result = await methodFn.apply(this, args);
|
|
513
|
+
|
|
514
|
+
this.observability?.emit(
|
|
515
|
+
{
|
|
516
|
+
displayMessage: `RPC call to ${method}`,
|
|
517
|
+
id: nanoid(),
|
|
518
|
+
payload: {
|
|
519
|
+
args,
|
|
520
|
+
method,
|
|
521
|
+
streaming: metadata?.streaming,
|
|
522
|
+
success: true
|
|
523
|
+
},
|
|
524
|
+
timestamp: Date.now(),
|
|
525
|
+
type: "rpc"
|
|
526
|
+
},
|
|
527
|
+
this.ctx
|
|
528
|
+
);
|
|
529
|
+
|
|
463
530
|
const response: RPCResponse = {
|
|
464
|
-
|
|
531
|
+
done: true,
|
|
465
532
|
id,
|
|
466
|
-
success: true,
|
|
467
533
|
result,
|
|
468
|
-
|
|
534
|
+
success: true,
|
|
535
|
+
type: "rpc"
|
|
469
536
|
};
|
|
470
537
|
connection.send(JSON.stringify(response));
|
|
471
538
|
} catch (e) {
|
|
472
539
|
// Send error response
|
|
473
540
|
const response: RPCResponse = {
|
|
474
|
-
type: "rpc",
|
|
475
|
-
id: parsed.id,
|
|
476
|
-
success: false,
|
|
477
541
|
error:
|
|
478
542
|
e instanceof Error ? e.message : "Unknown error occurred",
|
|
543
|
+
id: parsed.id,
|
|
544
|
+
success: false,
|
|
545
|
+
type: "rpc"
|
|
479
546
|
};
|
|
480
547
|
connection.send(JSON.stringify(response));
|
|
481
548
|
console.error("RPC error:", e);
|
|
@@ -483,7 +550,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
483
550
|
return;
|
|
484
551
|
}
|
|
485
552
|
|
|
486
|
-
return this
|
|
553
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
487
554
|
}
|
|
488
555
|
);
|
|
489
556
|
};
|
|
@@ -493,26 +560,38 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
493
560
|
// TODO: This is a hack to ensure the state is sent after the connection is established
|
|
494
561
|
// must fix this
|
|
495
562
|
return agentContext.run(
|
|
496
|
-
{ agent: this, connection, request: ctx.request },
|
|
563
|
+
{ agent: this, connection, request: ctx.request, email: undefined },
|
|
497
564
|
async () => {
|
|
498
565
|
setTimeout(() => {
|
|
499
566
|
if (this.state) {
|
|
500
567
|
connection.send(
|
|
501
568
|
JSON.stringify({
|
|
502
|
-
type: "cf_agent_state",
|
|
503
569
|
state: this.state,
|
|
570
|
+
type: "cf_agent_state"
|
|
504
571
|
})
|
|
505
572
|
);
|
|
506
573
|
}
|
|
507
574
|
|
|
508
575
|
connection.send(
|
|
509
576
|
JSON.stringify({
|
|
510
|
-
|
|
511
|
-
|
|
577
|
+
mcp: this.getMcpServers(),
|
|
578
|
+
type: "cf_agent_mcp_servers"
|
|
512
579
|
})
|
|
513
580
|
);
|
|
514
581
|
|
|
515
|
-
|
|
582
|
+
this.observability?.emit(
|
|
583
|
+
{
|
|
584
|
+
displayMessage: "Connection established",
|
|
585
|
+
id: nanoid(),
|
|
586
|
+
payload: {
|
|
587
|
+
connectionId: connection.id
|
|
588
|
+
},
|
|
589
|
+
timestamp: Date.now(),
|
|
590
|
+
type: "connect"
|
|
591
|
+
},
|
|
592
|
+
this.ctx
|
|
593
|
+
);
|
|
594
|
+
return this._tryCatch(() => _onConnect(connection, ctx));
|
|
516
595
|
}, 20);
|
|
517
596
|
}
|
|
518
597
|
);
|
|
@@ -521,45 +600,77 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
521
600
|
const _onStart = this.onStart.bind(this);
|
|
522
601
|
this.onStart = async () => {
|
|
523
602
|
return agentContext.run(
|
|
524
|
-
{
|
|
603
|
+
{
|
|
604
|
+
agent: this,
|
|
605
|
+
connection: undefined,
|
|
606
|
+
request: undefined,
|
|
607
|
+
email: undefined
|
|
608
|
+
},
|
|
525
609
|
async () => {
|
|
526
|
-
|
|
610
|
+
await this._tryCatch(() => {
|
|
611
|
+
const servers = this.sql<MCPServerRow>`
|
|
527
612
|
SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
|
|
528
613
|
`;
|
|
529
614
|
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
server.callback_url,
|
|
537
|
-
server.server_options
|
|
538
|
-
? JSON.parse(server.server_options)
|
|
539
|
-
: undefined,
|
|
540
|
-
{
|
|
541
|
-
id: server.id,
|
|
542
|
-
oauthClientId: server.client_id ?? undefined,
|
|
543
|
-
}
|
|
544
|
-
);
|
|
545
|
-
})
|
|
546
|
-
);
|
|
547
|
-
|
|
548
|
-
this.broadcast(
|
|
549
|
-
JSON.stringify({
|
|
550
|
-
type: "cf_agent_mcp_servers",
|
|
551
|
-
mcp: this.#getMcpServerStateInternal(),
|
|
552
|
-
})
|
|
553
|
-
);
|
|
615
|
+
this.broadcast(
|
|
616
|
+
JSON.stringify({
|
|
617
|
+
mcp: this.getMcpServers(),
|
|
618
|
+
type: "cf_agent_mcp_servers"
|
|
619
|
+
})
|
|
620
|
+
);
|
|
554
621
|
|
|
555
|
-
|
|
622
|
+
// from DO storage, reconnect to all servers not currently in the oauth flow using our saved auth information
|
|
623
|
+
if (servers && Array.isArray(servers) && servers.length > 0) {
|
|
624
|
+
servers.forEach((server) => {
|
|
625
|
+
this._connectToMcpServerInternal(
|
|
626
|
+
server.name,
|
|
627
|
+
server.server_url,
|
|
628
|
+
server.callback_url,
|
|
629
|
+
server.server_options
|
|
630
|
+
? JSON.parse(server.server_options)
|
|
631
|
+
: undefined,
|
|
632
|
+
{
|
|
633
|
+
id: server.id,
|
|
634
|
+
oauthClientId: server.client_id ?? undefined
|
|
635
|
+
}
|
|
636
|
+
)
|
|
637
|
+
.then(() => {
|
|
638
|
+
// Broadcast updated MCP servers state after each server connects
|
|
639
|
+
this.broadcast(
|
|
640
|
+
JSON.stringify({
|
|
641
|
+
mcp: this.getMcpServers(),
|
|
642
|
+
type: "cf_agent_mcp_servers"
|
|
643
|
+
})
|
|
644
|
+
);
|
|
645
|
+
})
|
|
646
|
+
.catch((error) => {
|
|
647
|
+
console.error(
|
|
648
|
+
`Error connecting to MCP server: ${server.name} (${server.server_url})`,
|
|
649
|
+
error
|
|
650
|
+
);
|
|
651
|
+
// Still broadcast even if connection fails, so clients know about the failure
|
|
652
|
+
this.broadcast(
|
|
653
|
+
JSON.stringify({
|
|
654
|
+
mcp: this.getMcpServers(),
|
|
655
|
+
type: "cf_agent_mcp_servers"
|
|
656
|
+
})
|
|
657
|
+
);
|
|
658
|
+
});
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
return _onStart();
|
|
662
|
+
});
|
|
556
663
|
}
|
|
557
664
|
);
|
|
558
665
|
};
|
|
559
666
|
}
|
|
560
667
|
|
|
561
|
-
|
|
562
|
-
|
|
668
|
+
private _setStateInternal(
|
|
669
|
+
state: State,
|
|
670
|
+
source: Connection | "server" = "server"
|
|
671
|
+
) {
|
|
672
|
+
const previousState = this._state;
|
|
673
|
+
this._state = state;
|
|
563
674
|
this.sql`
|
|
564
675
|
INSERT OR REPLACE INTO cf_agents_state (id, state)
|
|
565
676
|
VALUES (${STATE_ROW_ID}, ${JSON.stringify(state)})
|
|
@@ -570,16 +681,29 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
570
681
|
`;
|
|
571
682
|
this.broadcast(
|
|
572
683
|
JSON.stringify({
|
|
573
|
-
type: "cf_agent_state",
|
|
574
684
|
state: state,
|
|
685
|
+
type: "cf_agent_state"
|
|
575
686
|
}),
|
|
576
687
|
source !== "server" ? [source.id] : []
|
|
577
688
|
);
|
|
578
|
-
return this
|
|
579
|
-
const { connection, request } = agentContext.getStore() || {};
|
|
689
|
+
return this._tryCatch(() => {
|
|
690
|
+
const { connection, request, email } = agentContext.getStore() || {};
|
|
580
691
|
return agentContext.run(
|
|
581
|
-
{ agent: this, connection, request },
|
|
692
|
+
{ agent: this, connection, request, email },
|
|
582
693
|
async () => {
|
|
694
|
+
this.observability?.emit(
|
|
695
|
+
{
|
|
696
|
+
displayMessage: "State updated",
|
|
697
|
+
id: nanoid(),
|
|
698
|
+
payload: {
|
|
699
|
+
previousState,
|
|
700
|
+
state
|
|
701
|
+
},
|
|
702
|
+
timestamp: Date.now(),
|
|
703
|
+
type: "state:update"
|
|
704
|
+
},
|
|
705
|
+
this.ctx
|
|
706
|
+
);
|
|
583
707
|
return this.onStateUpdate(state, source);
|
|
584
708
|
}
|
|
585
709
|
);
|
|
@@ -591,7 +715,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
591
715
|
* @param state New state to set
|
|
592
716
|
*/
|
|
593
717
|
setState(state: State) {
|
|
594
|
-
this
|
|
718
|
+
this._setStateInternal(state, "server");
|
|
595
719
|
}
|
|
596
720
|
|
|
597
721
|
/**
|
|
@@ -599,24 +723,90 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
599
723
|
* @param state Updated state
|
|
600
724
|
* @param source Source of the state update ("server" or a client connection)
|
|
601
725
|
*/
|
|
726
|
+
// biome-ignore lint/correctness/noUnusedFunctionParameters: overridden later
|
|
602
727
|
onStateUpdate(state: State | undefined, source: Connection | "server") {
|
|
603
728
|
// override this to handle state updates
|
|
604
729
|
}
|
|
605
730
|
|
|
606
731
|
/**
|
|
607
|
-
* Called when the Agent receives an email
|
|
732
|
+
* Called when the Agent receives an email via routeAgentEmail()
|
|
733
|
+
* Override this method to handle incoming emails
|
|
608
734
|
* @param email Email message to process
|
|
609
735
|
*/
|
|
610
|
-
|
|
736
|
+
async _onEmail(email: AgentEmail) {
|
|
737
|
+
// nb: we use this roundabout way of getting to onEmail
|
|
738
|
+
// because of https://github.com/cloudflare/workerd/issues/4499
|
|
611
739
|
return agentContext.run(
|
|
612
|
-
{ agent: this, connection: undefined, request: undefined },
|
|
740
|
+
{ agent: this, connection: undefined, request: undefined, email: email },
|
|
613
741
|
async () => {
|
|
614
|
-
|
|
742
|
+
if ("onEmail" in this && typeof this.onEmail === "function") {
|
|
743
|
+
return this._tryCatch(() =>
|
|
744
|
+
(this.onEmail as (email: AgentEmail) => Promise<void>)(email)
|
|
745
|
+
);
|
|
746
|
+
} else {
|
|
747
|
+
console.log("Received email from:", email.from, "to:", email.to);
|
|
748
|
+
console.log("Subject:", email.headers.get("subject"));
|
|
749
|
+
console.log(
|
|
750
|
+
"Implement onEmail(email: AgentEmail): Promise<void> in your agent to process emails"
|
|
751
|
+
);
|
|
752
|
+
}
|
|
615
753
|
}
|
|
616
754
|
);
|
|
617
755
|
}
|
|
618
756
|
|
|
619
|
-
|
|
757
|
+
/**
|
|
758
|
+
* Reply to an email
|
|
759
|
+
* @param email The email to reply to
|
|
760
|
+
* @param options Options for the reply
|
|
761
|
+
* @returns void
|
|
762
|
+
*/
|
|
763
|
+
async replyToEmail(
|
|
764
|
+
email: AgentEmail,
|
|
765
|
+
options: {
|
|
766
|
+
fromName: string;
|
|
767
|
+
subject?: string | undefined;
|
|
768
|
+
body: string;
|
|
769
|
+
contentType?: string;
|
|
770
|
+
headers?: Record<string, string>;
|
|
771
|
+
}
|
|
772
|
+
): Promise<void> {
|
|
773
|
+
return this._tryCatch(async () => {
|
|
774
|
+
const agentName = camelCaseToKebabCase(this._ParentClass.name);
|
|
775
|
+
const agentId = this.name;
|
|
776
|
+
|
|
777
|
+
const { createMimeMessage } = await import("mimetext");
|
|
778
|
+
const msg = createMimeMessage();
|
|
779
|
+
msg.setSender({ addr: email.to, name: options.fromName });
|
|
780
|
+
msg.setRecipient(email.from);
|
|
781
|
+
msg.setSubject(
|
|
782
|
+
options.subject || `Re: ${email.headers.get("subject")}` || "No subject"
|
|
783
|
+
);
|
|
784
|
+
msg.addMessage({
|
|
785
|
+
contentType: options.contentType || "text/plain",
|
|
786
|
+
data: options.body
|
|
787
|
+
});
|
|
788
|
+
|
|
789
|
+
const domain = email.from.split("@")[1];
|
|
790
|
+
const messageId = `<${agentId}@${domain}>`;
|
|
791
|
+
msg.setHeader("In-Reply-To", email.headers.get("Message-ID")!);
|
|
792
|
+
msg.setHeader("Message-ID", messageId);
|
|
793
|
+
msg.setHeader("X-Agent-Name", agentName);
|
|
794
|
+
msg.setHeader("X-Agent-ID", agentId);
|
|
795
|
+
|
|
796
|
+
if (options.headers) {
|
|
797
|
+
for (const [key, value] of Object.entries(options.headers)) {
|
|
798
|
+
msg.setHeader(key, value);
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
await email.reply({
|
|
802
|
+
from: email.to,
|
|
803
|
+
raw: msg.asRaw(),
|
|
804
|
+
to: email.from
|
|
805
|
+
});
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
private async _tryCatch<T>(fn: () => T | Promise<T>) {
|
|
620
810
|
try {
|
|
621
811
|
return await fn();
|
|
622
812
|
} catch (e) {
|
|
@@ -624,6 +814,72 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
624
814
|
}
|
|
625
815
|
}
|
|
626
816
|
|
|
817
|
+
/**
|
|
818
|
+
* Automatically wrap custom methods with agent context
|
|
819
|
+
* This ensures getCurrentAgent() works in all custom methods without decorators
|
|
820
|
+
*/
|
|
821
|
+
private _autoWrapCustomMethods() {
|
|
822
|
+
// Collect all methods from base prototypes (Agent and Server)
|
|
823
|
+
const basePrototypes = [Agent.prototype, Server.prototype];
|
|
824
|
+
const baseMethods = new Set<string>();
|
|
825
|
+
for (const baseProto of basePrototypes) {
|
|
826
|
+
let proto = baseProto;
|
|
827
|
+
while (proto && proto !== Object.prototype) {
|
|
828
|
+
const methodNames = Object.getOwnPropertyNames(proto);
|
|
829
|
+
for (const methodName of methodNames) {
|
|
830
|
+
baseMethods.add(methodName);
|
|
831
|
+
}
|
|
832
|
+
proto = Object.getPrototypeOf(proto);
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
// Get all methods from the current instance's prototype chain
|
|
836
|
+
let proto = Object.getPrototypeOf(this);
|
|
837
|
+
let depth = 0;
|
|
838
|
+
while (proto && proto !== Object.prototype && depth < 10) {
|
|
839
|
+
const methodNames = Object.getOwnPropertyNames(proto);
|
|
840
|
+
for (const methodName of methodNames) {
|
|
841
|
+
// Skip if it's a private method or not a function
|
|
842
|
+
if (
|
|
843
|
+
baseMethods.has(methodName) ||
|
|
844
|
+
methodName.startsWith("_") ||
|
|
845
|
+
typeof this[methodName as keyof this] !== "function"
|
|
846
|
+
) {
|
|
847
|
+
continue;
|
|
848
|
+
}
|
|
849
|
+
// If the method doesn't exist in base prototypes, it's a custom method
|
|
850
|
+
if (!baseMethods.has(methodName)) {
|
|
851
|
+
const descriptor = Object.getOwnPropertyDescriptor(proto, methodName);
|
|
852
|
+
if (descriptor && typeof descriptor.value === "function") {
|
|
853
|
+
// Wrap the custom method with context
|
|
854
|
+
|
|
855
|
+
const wrappedFunction = withAgentContext(
|
|
856
|
+
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
|
|
857
|
+
this[methodName as keyof this] as (...args: any[]) => any
|
|
858
|
+
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
|
|
859
|
+
) as any;
|
|
860
|
+
|
|
861
|
+
// if the method is callable, copy the metadata from the original method
|
|
862
|
+
if (this._isCallable(methodName)) {
|
|
863
|
+
callableMetadata.set(
|
|
864
|
+
wrappedFunction,
|
|
865
|
+
callableMetadata.get(
|
|
866
|
+
this[methodName as keyof this] as Function
|
|
867
|
+
)!
|
|
868
|
+
);
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
// set the wrapped function on the prototype
|
|
872
|
+
this.constructor.prototype[methodName as keyof this] =
|
|
873
|
+
wrappedFunction;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
proto = Object.getPrototypeOf(proto);
|
|
879
|
+
depth++;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
|
|
627
883
|
override onError(
|
|
628
884
|
connection: Connection,
|
|
629
885
|
error: unknown
|
|
@@ -658,6 +914,131 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
658
914
|
throw new Error("Not implemented");
|
|
659
915
|
}
|
|
660
916
|
|
|
917
|
+
/**
|
|
918
|
+
* Queue a task to be executed in the future
|
|
919
|
+
* @param payload Payload to pass to the callback
|
|
920
|
+
* @param callback Name of the method to call
|
|
921
|
+
* @returns The ID of the queued task
|
|
922
|
+
*/
|
|
923
|
+
async queue<T = unknown>(callback: keyof this, payload: T): Promise<string> {
|
|
924
|
+
const id = nanoid(9);
|
|
925
|
+
if (typeof callback !== "string") {
|
|
926
|
+
throw new Error("Callback must be a string");
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
if (typeof this[callback] !== "function") {
|
|
930
|
+
throw new Error(`this.${callback} is not a function`);
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
this.sql`
|
|
934
|
+
INSERT OR REPLACE INTO cf_agents_queues (id, payload, callback)
|
|
935
|
+
VALUES (${id}, ${JSON.stringify(payload)}, ${callback})
|
|
936
|
+
`;
|
|
937
|
+
|
|
938
|
+
void this._flushQueue().catch((e) => {
|
|
939
|
+
console.error("Error flushing queue:", e);
|
|
940
|
+
});
|
|
941
|
+
|
|
942
|
+
return id;
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
private _flushingQueue = false;
|
|
946
|
+
|
|
947
|
+
private async _flushQueue() {
|
|
948
|
+
if (this._flushingQueue) {
|
|
949
|
+
return;
|
|
950
|
+
}
|
|
951
|
+
this._flushingQueue = true;
|
|
952
|
+
while (true) {
|
|
953
|
+
const result = this.sql<QueueItem<string>>`
|
|
954
|
+
SELECT * FROM cf_agents_queues
|
|
955
|
+
ORDER BY created_at ASC
|
|
956
|
+
`;
|
|
957
|
+
|
|
958
|
+
if (!result || result.length === 0) {
|
|
959
|
+
break;
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
for (const row of result || []) {
|
|
963
|
+
const callback = this[row.callback as keyof Agent<Env>];
|
|
964
|
+
if (!callback) {
|
|
965
|
+
console.error(`callback ${row.callback} not found`);
|
|
966
|
+
continue;
|
|
967
|
+
}
|
|
968
|
+
const { connection, request, email } = agentContext.getStore() || {};
|
|
969
|
+
await agentContext.run(
|
|
970
|
+
{
|
|
971
|
+
agent: this,
|
|
972
|
+
connection,
|
|
973
|
+
request,
|
|
974
|
+
email
|
|
975
|
+
},
|
|
976
|
+
async () => {
|
|
977
|
+
// TODO: add retries and backoff
|
|
978
|
+
await (
|
|
979
|
+
callback as (
|
|
980
|
+
payload: unknown,
|
|
981
|
+
queueItem: QueueItem<string>
|
|
982
|
+
) => Promise<void>
|
|
983
|
+
).bind(this)(JSON.parse(row.payload as string), row);
|
|
984
|
+
await this.dequeue(row.id);
|
|
985
|
+
}
|
|
986
|
+
);
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
this._flushingQueue = false;
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
/**
|
|
993
|
+
* Dequeue a task by ID
|
|
994
|
+
* @param id ID of the task to dequeue
|
|
995
|
+
*/
|
|
996
|
+
async dequeue(id: string) {
|
|
997
|
+
this.sql`DELETE FROM cf_agents_queues WHERE id = ${id}`;
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
/**
|
|
1001
|
+
* Dequeue all tasks
|
|
1002
|
+
*/
|
|
1003
|
+
async dequeueAll() {
|
|
1004
|
+
this.sql`DELETE FROM cf_agents_queues`;
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
/**
|
|
1008
|
+
* Dequeue all tasks by callback
|
|
1009
|
+
* @param callback Name of the callback to dequeue
|
|
1010
|
+
*/
|
|
1011
|
+
async dequeueAllByCallback(callback: string) {
|
|
1012
|
+
this.sql`DELETE FROM cf_agents_queues WHERE callback = ${callback}`;
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
/**
|
|
1016
|
+
* Get a queued task by ID
|
|
1017
|
+
* @param id ID of the task to get
|
|
1018
|
+
* @returns The task or undefined if not found
|
|
1019
|
+
*/
|
|
1020
|
+
async getQueue(id: string): Promise<QueueItem<string> | undefined> {
|
|
1021
|
+
const result = this.sql<QueueItem<string>>`
|
|
1022
|
+
SELECT * FROM cf_agents_queues WHERE id = ${id}
|
|
1023
|
+
`;
|
|
1024
|
+
return result
|
|
1025
|
+
? { ...result[0], payload: JSON.parse(result[0].payload) }
|
|
1026
|
+
: undefined;
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
/**
|
|
1030
|
+
* Get all queues by key and value
|
|
1031
|
+
* @param key Key to filter by
|
|
1032
|
+
* @param value Value to filter by
|
|
1033
|
+
* @returns Array of matching QueueItem objects
|
|
1034
|
+
*/
|
|
1035
|
+
async getQueues(key: string, value: string): Promise<QueueItem<string>[]> {
|
|
1036
|
+
const result = this.sql<QueueItem<string>>`
|
|
1037
|
+
SELECT * FROM cf_agents_queues
|
|
1038
|
+
`;
|
|
1039
|
+
return result.filter((row) => JSON.parse(row.payload)[key] === value);
|
|
1040
|
+
}
|
|
1041
|
+
|
|
661
1042
|
/**
|
|
662
1043
|
* Schedule a task to be executed in the future
|
|
663
1044
|
* @template T Type of the payload data
|
|
@@ -673,6 +1054,18 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
673
1054
|
): Promise<Schedule<T>> {
|
|
674
1055
|
const id = nanoid(9);
|
|
675
1056
|
|
|
1057
|
+
const emitScheduleCreate = (schedule: Schedule<T>) =>
|
|
1058
|
+
this.observability?.emit(
|
|
1059
|
+
{
|
|
1060
|
+
displayMessage: `Schedule ${schedule.id} created`,
|
|
1061
|
+
id: nanoid(),
|
|
1062
|
+
payload: schedule,
|
|
1063
|
+
timestamp: Date.now(),
|
|
1064
|
+
type: "schedule:create"
|
|
1065
|
+
},
|
|
1066
|
+
this.ctx
|
|
1067
|
+
);
|
|
1068
|
+
|
|
676
1069
|
if (typeof callback !== "string") {
|
|
677
1070
|
throw new Error("Callback must be a string");
|
|
678
1071
|
}
|
|
@@ -690,15 +1083,19 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
690
1083
|
)}, 'scheduled', ${timestamp})
|
|
691
1084
|
`;
|
|
692
1085
|
|
|
693
|
-
await this
|
|
1086
|
+
await this._scheduleNextAlarm();
|
|
694
1087
|
|
|
695
|
-
|
|
696
|
-
id,
|
|
1088
|
+
const schedule: Schedule<T> = {
|
|
697
1089
|
callback: callback,
|
|
1090
|
+
id,
|
|
698
1091
|
payload: payload as T,
|
|
699
1092
|
time: timestamp,
|
|
700
|
-
type: "scheduled"
|
|
1093
|
+
type: "scheduled"
|
|
701
1094
|
};
|
|
1095
|
+
|
|
1096
|
+
emitScheduleCreate(schedule);
|
|
1097
|
+
|
|
1098
|
+
return schedule;
|
|
702
1099
|
}
|
|
703
1100
|
if (typeof when === "number") {
|
|
704
1101
|
const time = new Date(Date.now() + when * 1000);
|
|
@@ -711,16 +1108,20 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
711
1108
|
)}, 'delayed', ${when}, ${timestamp})
|
|
712
1109
|
`;
|
|
713
1110
|
|
|
714
|
-
await this
|
|
1111
|
+
await this._scheduleNextAlarm();
|
|
715
1112
|
|
|
716
|
-
|
|
717
|
-
id,
|
|
1113
|
+
const schedule: Schedule<T> = {
|
|
718
1114
|
callback: callback,
|
|
719
|
-
payload: payload as T,
|
|
720
1115
|
delayInSeconds: when,
|
|
1116
|
+
id,
|
|
1117
|
+
payload: payload as T,
|
|
721
1118
|
time: timestamp,
|
|
722
|
-
type: "delayed"
|
|
1119
|
+
type: "delayed"
|
|
723
1120
|
};
|
|
1121
|
+
|
|
1122
|
+
emitScheduleCreate(schedule);
|
|
1123
|
+
|
|
1124
|
+
return schedule;
|
|
724
1125
|
}
|
|
725
1126
|
if (typeof when === "string") {
|
|
726
1127
|
const nextExecutionTime = getNextCronTime(when);
|
|
@@ -733,16 +1134,20 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
733
1134
|
)}, 'cron', ${when}, ${timestamp})
|
|
734
1135
|
`;
|
|
735
1136
|
|
|
736
|
-
await this
|
|
1137
|
+
await this._scheduleNextAlarm();
|
|
737
1138
|
|
|
738
|
-
|
|
739
|
-
id,
|
|
1139
|
+
const schedule: Schedule<T> = {
|
|
740
1140
|
callback: callback,
|
|
741
|
-
payload: payload as T,
|
|
742
1141
|
cron: when,
|
|
1142
|
+
id,
|
|
1143
|
+
payload: payload as T,
|
|
743
1144
|
time: timestamp,
|
|
744
|
-
type: "cron"
|
|
1145
|
+
type: "cron"
|
|
745
1146
|
};
|
|
1147
|
+
|
|
1148
|
+
emitScheduleCreate(schedule);
|
|
1149
|
+
|
|
1150
|
+
return schedule;
|
|
746
1151
|
}
|
|
747
1152
|
throw new Error("Invalid schedule type");
|
|
748
1153
|
}
|
|
@@ -806,7 +1211,7 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
806
1211
|
.toArray()
|
|
807
1212
|
.map((row) => ({
|
|
808
1213
|
...row,
|
|
809
|
-
payload: JSON.parse(row.payload as string) as T
|
|
1214
|
+
payload: JSON.parse(row.payload as string) as T
|
|
810
1215
|
})) as Schedule<T>[];
|
|
811
1216
|
|
|
812
1217
|
return result;
|
|
@@ -818,18 +1223,31 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
818
1223
|
* @returns true if the task was cancelled, false otherwise
|
|
819
1224
|
*/
|
|
820
1225
|
async cancelSchedule(id: string): Promise<boolean> {
|
|
1226
|
+
const schedule = await this.getSchedule(id);
|
|
1227
|
+
if (schedule) {
|
|
1228
|
+
this.observability?.emit(
|
|
1229
|
+
{
|
|
1230
|
+
displayMessage: `Schedule ${id} cancelled`,
|
|
1231
|
+
id: nanoid(),
|
|
1232
|
+
payload: schedule,
|
|
1233
|
+
timestamp: Date.now(),
|
|
1234
|
+
type: "schedule:cancel"
|
|
1235
|
+
},
|
|
1236
|
+
this.ctx
|
|
1237
|
+
);
|
|
1238
|
+
}
|
|
821
1239
|
this.sql`DELETE FROM cf_agents_schedules WHERE id = ${id}`;
|
|
822
1240
|
|
|
823
|
-
await this
|
|
1241
|
+
await this._scheduleNextAlarm();
|
|
824
1242
|
return true;
|
|
825
1243
|
}
|
|
826
1244
|
|
|
827
|
-
async
|
|
1245
|
+
private async _scheduleNextAlarm() {
|
|
828
1246
|
// Find the next schedule that needs to be executed
|
|
829
1247
|
const result = this.sql`
|
|
830
|
-
SELECT time FROM cf_agents_schedules
|
|
1248
|
+
SELECT time FROM cf_agents_schedules
|
|
831
1249
|
WHERE time > ${Math.floor(Date.now() / 1000)}
|
|
832
|
-
ORDER BY time ASC
|
|
1250
|
+
ORDER BY time ASC
|
|
833
1251
|
LIMIT 1
|
|
834
1252
|
`;
|
|
835
1253
|
if (!result) return;
|
|
@@ -856,45 +1274,63 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
856
1274
|
SELECT * FROM cf_agents_schedules WHERE time <= ${now}
|
|
857
1275
|
`;
|
|
858
1276
|
|
|
859
|
-
|
|
860
|
-
const
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
await agentContext.run(
|
|
866
|
-
{ agent: this, connection: undefined, request: undefined },
|
|
867
|
-
async () => {
|
|
868
|
-
try {
|
|
869
|
-
await (
|
|
870
|
-
callback as (
|
|
871
|
-
payload: unknown,
|
|
872
|
-
schedule: Schedule<unknown>
|
|
873
|
-
) => Promise<void>
|
|
874
|
-
).bind(this)(JSON.parse(row.payload as string), row);
|
|
875
|
-
} catch (e) {
|
|
876
|
-
console.error(`error executing callback "${row.callback}"`, e);
|
|
877
|
-
}
|
|
1277
|
+
if (result && Array.isArray(result)) {
|
|
1278
|
+
for (const row of result) {
|
|
1279
|
+
const callback = this[row.callback as keyof Agent<Env>];
|
|
1280
|
+
if (!callback) {
|
|
1281
|
+
console.error(`callback ${row.callback} not found`);
|
|
1282
|
+
continue;
|
|
878
1283
|
}
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
1284
|
+
await agentContext.run(
|
|
1285
|
+
{
|
|
1286
|
+
agent: this,
|
|
1287
|
+
connection: undefined,
|
|
1288
|
+
request: undefined,
|
|
1289
|
+
email: undefined
|
|
1290
|
+
},
|
|
1291
|
+
async () => {
|
|
1292
|
+
try {
|
|
1293
|
+
this.observability?.emit(
|
|
1294
|
+
{
|
|
1295
|
+
displayMessage: `Schedule ${row.id} executed`,
|
|
1296
|
+
id: nanoid(),
|
|
1297
|
+
payload: row,
|
|
1298
|
+
timestamp: Date.now(),
|
|
1299
|
+
type: "schedule:execute"
|
|
1300
|
+
},
|
|
1301
|
+
this.ctx
|
|
1302
|
+
);
|
|
884
1303
|
|
|
885
|
-
|
|
1304
|
+
await (
|
|
1305
|
+
callback as (
|
|
1306
|
+
payload: unknown,
|
|
1307
|
+
schedule: Schedule<unknown>
|
|
1308
|
+
) => Promise<void>
|
|
1309
|
+
).bind(this)(JSON.parse(row.payload as string), row);
|
|
1310
|
+
} catch (e) {
|
|
1311
|
+
console.error(`error executing callback "${row.callback}"`, e);
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
);
|
|
1315
|
+
if (row.type === "cron") {
|
|
1316
|
+
// Update next execution time for cron schedules
|
|
1317
|
+
const nextExecutionTime = getNextCronTime(row.cron);
|
|
1318
|
+
const nextTimestamp = Math.floor(nextExecutionTime.getTime() / 1000);
|
|
1319
|
+
|
|
1320
|
+
this.sql`
|
|
886
1321
|
UPDATE cf_agents_schedules SET time = ${nextTimestamp} WHERE id = ${row.id}
|
|
887
1322
|
`;
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
1323
|
+
} else {
|
|
1324
|
+
// Delete one-time schedules after execution
|
|
1325
|
+
this.sql`
|
|
891
1326
|
DELETE FROM cf_agents_schedules WHERE id = ${row.id}
|
|
892
1327
|
`;
|
|
1328
|
+
}
|
|
893
1329
|
}
|
|
894
1330
|
}
|
|
895
1331
|
|
|
896
1332
|
// Schedule the next alarm
|
|
897
|
-
await this
|
|
1333
|
+
await this._scheduleNextAlarm();
|
|
898
1334
|
};
|
|
899
1335
|
|
|
900
1336
|
/**
|
|
@@ -905,18 +1341,30 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
905
1341
|
this.sql`DROP TABLE IF EXISTS cf_agents_state`;
|
|
906
1342
|
this.sql`DROP TABLE IF EXISTS cf_agents_schedules`;
|
|
907
1343
|
this.sql`DROP TABLE IF EXISTS cf_agents_mcp_servers`;
|
|
1344
|
+
this.sql`DROP TABLE IF EXISTS cf_agents_queues`;
|
|
908
1345
|
|
|
909
1346
|
// delete all alarms
|
|
910
1347
|
await this.ctx.storage.deleteAlarm();
|
|
911
1348
|
await this.ctx.storage.deleteAll();
|
|
1349
|
+
this.ctx.abort("destroyed"); // enforce that the agent is evicted
|
|
1350
|
+
|
|
1351
|
+
this.observability?.emit(
|
|
1352
|
+
{
|
|
1353
|
+
displayMessage: "Agent destroyed",
|
|
1354
|
+
id: nanoid(),
|
|
1355
|
+
payload: {},
|
|
1356
|
+
timestamp: Date.now(),
|
|
1357
|
+
type: "destroy"
|
|
1358
|
+
},
|
|
1359
|
+
this.ctx
|
|
1360
|
+
);
|
|
912
1361
|
}
|
|
913
1362
|
|
|
914
1363
|
/**
|
|
915
1364
|
* Get all methods marked as callable on this Agent
|
|
916
1365
|
* @returns A map of method names to their metadata
|
|
917
1366
|
*/
|
|
918
|
-
|
|
919
|
-
// biome-ignore lint/complexity/noBannedTypes: <explanation>
|
|
1367
|
+
private _isCallable(method: string): boolean {
|
|
920
1368
|
return callableMetadata.has(this[method as keyof this] as Function);
|
|
921
1369
|
}
|
|
922
1370
|
|
|
@@ -941,27 +1389,40 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
941
1389
|
};
|
|
942
1390
|
}
|
|
943
1391
|
): Promise<{ id: string; authUrl: string | undefined }> {
|
|
944
|
-
const callbackUrl = `${callbackHost}/${agentsPrefix}/${camelCaseToKebabCase(this
|
|
1392
|
+
const callbackUrl = `${callbackHost}/${agentsPrefix}/${camelCaseToKebabCase(this._ParentClass.name)}/${this.name}/callback`;
|
|
945
1393
|
|
|
946
|
-
const result = await this
|
|
1394
|
+
const result = await this._connectToMcpServerInternal(
|
|
947
1395
|
serverName,
|
|
948
1396
|
url,
|
|
949
1397
|
callbackUrl,
|
|
950
1398
|
options
|
|
951
1399
|
);
|
|
1400
|
+
this.sql`
|
|
1401
|
+
INSERT
|
|
1402
|
+
OR REPLACE INTO cf_agents_mcp_servers (id, name, server_url, client_id, auth_url, callback_url, server_options)
|
|
1403
|
+
VALUES (
|
|
1404
|
+
${result.id},
|
|
1405
|
+
${serverName},
|
|
1406
|
+
${url},
|
|
1407
|
+
${result.clientId ?? null},
|
|
1408
|
+
${result.authUrl ?? null},
|
|
1409
|
+
${callbackUrl},
|
|
1410
|
+
${options ? JSON.stringify(options) : null}
|
|
1411
|
+
);
|
|
1412
|
+
`;
|
|
952
1413
|
|
|
953
1414
|
this.broadcast(
|
|
954
1415
|
JSON.stringify({
|
|
955
|
-
|
|
956
|
-
|
|
1416
|
+
mcp: this.getMcpServers(),
|
|
1417
|
+
type: "cf_agent_mcp_servers"
|
|
957
1418
|
})
|
|
958
1419
|
);
|
|
959
1420
|
|
|
960
1421
|
return result;
|
|
961
1422
|
}
|
|
962
1423
|
|
|
963
|
-
async
|
|
964
|
-
|
|
1424
|
+
async _connectToMcpServerInternal(
|
|
1425
|
+
_serverName: string,
|
|
965
1426
|
url: string,
|
|
966
1427
|
callbackUrl: string,
|
|
967
1428
|
// it's important that any options here are serializable because we put them into our sqlite DB for reconnection purposes
|
|
@@ -982,7 +1443,11 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
982
1443
|
id: string;
|
|
983
1444
|
oauthClientId?: string;
|
|
984
1445
|
}
|
|
985
|
-
): Promise<{
|
|
1446
|
+
): Promise<{
|
|
1447
|
+
id: string;
|
|
1448
|
+
authUrl: string | undefined;
|
|
1449
|
+
clientId: string | undefined;
|
|
1450
|
+
}> {
|
|
986
1451
|
const authProvider = new DurableObjectOAuthClientProvider(
|
|
987
1452
|
this.ctx.storage,
|
|
988
1453
|
this.name,
|
|
@@ -1005,40 +1470,28 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
1005
1470
|
fetch: (url, init) =>
|
|
1006
1471
|
fetch(url, {
|
|
1007
1472
|
...init,
|
|
1008
|
-
headers: options?.transport?.headers
|
|
1009
|
-
})
|
|
1473
|
+
headers: options?.transport?.headers
|
|
1474
|
+
})
|
|
1010
1475
|
},
|
|
1011
1476
|
requestInit: {
|
|
1012
|
-
headers: options?.transport?.headers
|
|
1013
|
-
}
|
|
1477
|
+
headers: options?.transport?.headers
|
|
1478
|
+
}
|
|
1014
1479
|
};
|
|
1015
1480
|
}
|
|
1016
1481
|
|
|
1017
1482
|
const { id, authUrl, clientId } = await this.mcp.connect(url, {
|
|
1483
|
+
client: options?.client,
|
|
1018
1484
|
reconnect,
|
|
1019
1485
|
transport: {
|
|
1020
1486
|
...headerTransportOpts,
|
|
1021
|
-
authProvider
|
|
1022
|
-
}
|
|
1023
|
-
client: options?.client,
|
|
1487
|
+
authProvider
|
|
1488
|
+
}
|
|
1024
1489
|
});
|
|
1025
1490
|
|
|
1026
|
-
this.sql`
|
|
1027
|
-
INSERT OR REPLACE INTO cf_agents_mcp_servers (id, name, server_url, client_id, auth_url, callback_url, server_options)
|
|
1028
|
-
VALUES (
|
|
1029
|
-
${id},
|
|
1030
|
-
${serverName},
|
|
1031
|
-
${url},
|
|
1032
|
-
${clientId ?? null},
|
|
1033
|
-
${authUrl ?? null},
|
|
1034
|
-
${callbackUrl},
|
|
1035
|
-
${options ? JSON.stringify(options) : null}
|
|
1036
|
-
);
|
|
1037
|
-
`;
|
|
1038
|
-
|
|
1039
1491
|
return {
|
|
1040
|
-
id,
|
|
1041
1492
|
authUrl,
|
|
1493
|
+
clientId,
|
|
1494
|
+
id
|
|
1042
1495
|
};
|
|
1043
1496
|
}
|
|
1044
1497
|
|
|
@@ -1049,31 +1502,37 @@ export class Agent<Env, State = unknown> extends Server<Env> {
|
|
|
1049
1502
|
`;
|
|
1050
1503
|
this.broadcast(
|
|
1051
1504
|
JSON.stringify({
|
|
1052
|
-
|
|
1053
|
-
|
|
1505
|
+
mcp: this.getMcpServers(),
|
|
1506
|
+
type: "cf_agent_mcp_servers"
|
|
1054
1507
|
})
|
|
1055
1508
|
);
|
|
1056
1509
|
}
|
|
1057
1510
|
|
|
1058
|
-
|
|
1511
|
+
getMcpServers(): MCPServersState {
|
|
1059
1512
|
const mcpState: MCPServersState = {
|
|
1060
|
-
servers: {},
|
|
1061
|
-
tools: this.mcp.listTools(),
|
|
1062
1513
|
prompts: this.mcp.listPrompts(),
|
|
1063
1514
|
resources: this.mcp.listResources(),
|
|
1515
|
+
servers: {},
|
|
1516
|
+
tools: this.mcp.listTools()
|
|
1064
1517
|
};
|
|
1065
1518
|
|
|
1066
1519
|
const servers = this.sql<MCPServerRow>`
|
|
1067
1520
|
SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
|
|
1068
1521
|
`;
|
|
1069
1522
|
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1523
|
+
if (servers && Array.isArray(servers) && servers.length > 0) {
|
|
1524
|
+
for (const server of servers) {
|
|
1525
|
+
const serverConn = this.mcp.mcpConnections[server.id];
|
|
1526
|
+
mcpState.servers[server.id] = {
|
|
1527
|
+
auth_url: server.auth_url,
|
|
1528
|
+
capabilities: serverConn?.serverCapabilities ?? null,
|
|
1529
|
+
instructions: serverConn?.instructions ?? null,
|
|
1530
|
+
name: server.name,
|
|
1531
|
+
server_url: server.server_url,
|
|
1532
|
+
// mark as "authenticating" because the server isn't automatically connected, so it's pending authenticating
|
|
1533
|
+
state: serverConn?.connectionState ?? "authenticating"
|
|
1534
|
+
};
|
|
1535
|
+
}
|
|
1077
1536
|
}
|
|
1078
1537
|
|
|
1079
1538
|
return mcpState;
|
|
@@ -1117,17 +1576,17 @@ export async function routeAgentRequest<Env>(
|
|
|
1117
1576
|
const corsHeaders =
|
|
1118
1577
|
options?.cors === true
|
|
1119
1578
|
? {
|
|
1120
|
-
"Access-Control-Allow-Origin": "*",
|
|
1121
|
-
"Access-Control-Allow-Methods": "GET, POST, HEAD, OPTIONS",
|
|
1122
1579
|
"Access-Control-Allow-Credentials": "true",
|
|
1123
|
-
"Access-Control-
|
|
1580
|
+
"Access-Control-Allow-Methods": "GET, POST, HEAD, OPTIONS",
|
|
1581
|
+
"Access-Control-Allow-Origin": "*",
|
|
1582
|
+
"Access-Control-Max-Age": "86400"
|
|
1124
1583
|
}
|
|
1125
1584
|
: options?.cors;
|
|
1126
1585
|
|
|
1127
1586
|
if (request.method === "OPTIONS") {
|
|
1128
1587
|
if (corsHeaders) {
|
|
1129
1588
|
return new Response(null, {
|
|
1130
|
-
headers: corsHeaders
|
|
1589
|
+
headers: corsHeaders
|
|
1131
1590
|
});
|
|
1132
1591
|
}
|
|
1133
1592
|
console.warn(
|
|
@@ -1140,7 +1599,7 @@ export async function routeAgentRequest<Env>(
|
|
|
1140
1599
|
env as Record<string, unknown>,
|
|
1141
1600
|
{
|
|
1142
1601
|
prefix: "agents",
|
|
1143
|
-
...(options as PartyServerOptions<Record<string, unknown>>)
|
|
1602
|
+
...(options as PartyServerOptions<Record<string, unknown>>)
|
|
1144
1603
|
}
|
|
1145
1604
|
);
|
|
1146
1605
|
|
|
@@ -1153,24 +1612,238 @@ export async function routeAgentRequest<Env>(
|
|
|
1153
1612
|
response = new Response(response.body, {
|
|
1154
1613
|
headers: {
|
|
1155
1614
|
...response.headers,
|
|
1156
|
-
...corsHeaders
|
|
1157
|
-
}
|
|
1615
|
+
...corsHeaders
|
|
1616
|
+
}
|
|
1158
1617
|
});
|
|
1159
1618
|
}
|
|
1160
1619
|
return response;
|
|
1161
1620
|
}
|
|
1162
1621
|
|
|
1622
|
+
export type EmailResolver<Env> = (
|
|
1623
|
+
email: ForwardableEmailMessage,
|
|
1624
|
+
env: Env
|
|
1625
|
+
) => Promise<{
|
|
1626
|
+
agentName: string;
|
|
1627
|
+
agentId: string;
|
|
1628
|
+
} | null>;
|
|
1629
|
+
|
|
1630
|
+
/**
|
|
1631
|
+
* Create a resolver that uses the message-id header to determine the agent to route the email to
|
|
1632
|
+
* @returns A function that resolves the agent to route the email to
|
|
1633
|
+
*/
|
|
1634
|
+
export function createHeaderBasedEmailResolver<Env>(): EmailResolver<Env> {
|
|
1635
|
+
return async (email: ForwardableEmailMessage, _env: Env) => {
|
|
1636
|
+
const messageId = email.headers.get("message-id");
|
|
1637
|
+
if (messageId) {
|
|
1638
|
+
const messageIdMatch = messageId.match(/<([^@]+)@([^>]+)>/);
|
|
1639
|
+
if (messageIdMatch) {
|
|
1640
|
+
const [, agentId, domain] = messageIdMatch;
|
|
1641
|
+
const agentName = domain.split(".")[0];
|
|
1642
|
+
return { agentName, agentId };
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
|
|
1646
|
+
const references = email.headers.get("references");
|
|
1647
|
+
if (references) {
|
|
1648
|
+
const referencesMatch = references.match(
|
|
1649
|
+
/<([A-Za-z0-9+/]{43}=)@([^>]+)>/
|
|
1650
|
+
);
|
|
1651
|
+
if (referencesMatch) {
|
|
1652
|
+
const [, base64Id, domain] = referencesMatch;
|
|
1653
|
+
const agentId = Buffer.from(base64Id, "base64").toString("hex");
|
|
1654
|
+
const agentName = domain.split(".")[0];
|
|
1655
|
+
return { agentName, agentId };
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
const agentName = email.headers.get("x-agent-name");
|
|
1660
|
+
const agentId = email.headers.get("x-agent-id");
|
|
1661
|
+
if (agentName && agentId) {
|
|
1662
|
+
return { agentName, agentId };
|
|
1663
|
+
}
|
|
1664
|
+
|
|
1665
|
+
return null;
|
|
1666
|
+
};
|
|
1667
|
+
}
|
|
1668
|
+
|
|
1669
|
+
/**
|
|
1670
|
+
* Create a resolver that uses the email address to determine the agent to route the email to
|
|
1671
|
+
* @param defaultAgentName The default agent name to use if the email address does not contain a sub-address
|
|
1672
|
+
* @returns A function that resolves the agent to route the email to
|
|
1673
|
+
*/
|
|
1674
|
+
export function createAddressBasedEmailResolver<Env>(
|
|
1675
|
+
defaultAgentName: string
|
|
1676
|
+
): EmailResolver<Env> {
|
|
1677
|
+
return async (email: ForwardableEmailMessage, _env: Env) => {
|
|
1678
|
+
const emailMatch = email.to.match(/^([^+@]+)(?:\+([^@]+))?@(.+)$/);
|
|
1679
|
+
if (!emailMatch) {
|
|
1680
|
+
return null;
|
|
1681
|
+
}
|
|
1682
|
+
|
|
1683
|
+
const [, localPart, subAddress] = emailMatch;
|
|
1684
|
+
|
|
1685
|
+
if (subAddress) {
|
|
1686
|
+
return {
|
|
1687
|
+
agentName: localPart,
|
|
1688
|
+
agentId: subAddress
|
|
1689
|
+
};
|
|
1690
|
+
}
|
|
1691
|
+
|
|
1692
|
+
// Option 2: Use defaultAgentName namespace, localPart as agentId
|
|
1693
|
+
// Common for catch-all email routing to a single EmailAgent namespace
|
|
1694
|
+
return {
|
|
1695
|
+
agentName: defaultAgentName,
|
|
1696
|
+
agentId: localPart
|
|
1697
|
+
};
|
|
1698
|
+
};
|
|
1699
|
+
}
|
|
1700
|
+
|
|
1701
|
+
/**
|
|
1702
|
+
* Create a resolver that uses the agentName and agentId to determine the agent to route the email to
|
|
1703
|
+
* @param agentName The name of the agent to route the email to
|
|
1704
|
+
* @param agentId The id of the agent to route the email to
|
|
1705
|
+
* @returns A function that resolves the agent to route the email to
|
|
1706
|
+
*/
|
|
1707
|
+
export function createCatchAllEmailResolver<Env>(
|
|
1708
|
+
agentName: string,
|
|
1709
|
+
agentId: string
|
|
1710
|
+
): EmailResolver<Env> {
|
|
1711
|
+
return async () => ({ agentName, agentId });
|
|
1712
|
+
}
|
|
1713
|
+
|
|
1714
|
+
export type EmailRoutingOptions<Env> = AgentOptions<Env> & {
|
|
1715
|
+
resolver: EmailResolver<Env>;
|
|
1716
|
+
};
|
|
1717
|
+
|
|
1718
|
+
// Cache the agent namespace map for email routing
|
|
1719
|
+
// This maps both kebab-case and original names to namespaces
|
|
1720
|
+
const agentMapCache = new WeakMap<
|
|
1721
|
+
Record<string, unknown>,
|
|
1722
|
+
Record<string, unknown>
|
|
1723
|
+
>();
|
|
1724
|
+
|
|
1163
1725
|
/**
|
|
1164
1726
|
* Route an email to the appropriate Agent
|
|
1165
|
-
* @param email
|
|
1166
|
-
* @param env
|
|
1167
|
-
* @param options
|
|
1727
|
+
* @param email The email to route
|
|
1728
|
+
* @param env The environment containing the Agent bindings
|
|
1729
|
+
* @param options The options for routing the email
|
|
1730
|
+
* @returns A promise that resolves when the email has been routed
|
|
1168
1731
|
*/
|
|
1169
1732
|
export async function routeAgentEmail<Env>(
|
|
1170
1733
|
email: ForwardableEmailMessage,
|
|
1171
1734
|
env: Env,
|
|
1172
|
-
options
|
|
1173
|
-
): Promise<void> {
|
|
1735
|
+
options: EmailRoutingOptions<Env>
|
|
1736
|
+
): Promise<void> {
|
|
1737
|
+
const routingInfo = await options.resolver(email, env);
|
|
1738
|
+
|
|
1739
|
+
if (!routingInfo) {
|
|
1740
|
+
console.warn("No routing information found for email, dropping message");
|
|
1741
|
+
return;
|
|
1742
|
+
}
|
|
1743
|
+
|
|
1744
|
+
// Build a map that includes both original names and kebab-case versions
|
|
1745
|
+
if (!agentMapCache.has(env as Record<string, unknown>)) {
|
|
1746
|
+
const map: Record<string, unknown> = {};
|
|
1747
|
+
for (const [key, value] of Object.entries(env as Record<string, unknown>)) {
|
|
1748
|
+
if (
|
|
1749
|
+
value &&
|
|
1750
|
+
typeof value === "object" &&
|
|
1751
|
+
"idFromName" in value &&
|
|
1752
|
+
typeof value.idFromName === "function"
|
|
1753
|
+
) {
|
|
1754
|
+
// Add both the original name and kebab-case version
|
|
1755
|
+
map[key] = value;
|
|
1756
|
+
map[camelCaseToKebabCase(key)] = value;
|
|
1757
|
+
}
|
|
1758
|
+
}
|
|
1759
|
+
agentMapCache.set(env as Record<string, unknown>, map);
|
|
1760
|
+
}
|
|
1761
|
+
|
|
1762
|
+
const agentMap = agentMapCache.get(env as Record<string, unknown>)!;
|
|
1763
|
+
const namespace = agentMap[routingInfo.agentName];
|
|
1764
|
+
|
|
1765
|
+
if (!namespace) {
|
|
1766
|
+
// Provide helpful error message listing available agents
|
|
1767
|
+
const availableAgents = Object.keys(agentMap)
|
|
1768
|
+
.filter((key) => !key.includes("-")) // Show only original names, not kebab-case duplicates
|
|
1769
|
+
.join(", ");
|
|
1770
|
+
throw new Error(
|
|
1771
|
+
`Agent namespace '${routingInfo.agentName}' not found in environment. Available agents: ${availableAgents}`
|
|
1772
|
+
);
|
|
1773
|
+
}
|
|
1774
|
+
|
|
1775
|
+
const agent = await getAgentByName(
|
|
1776
|
+
namespace as unknown as AgentNamespace<Agent<Env>>,
|
|
1777
|
+
routingInfo.agentId
|
|
1778
|
+
);
|
|
1779
|
+
|
|
1780
|
+
// let's make a serialisable version of the email
|
|
1781
|
+
const serialisableEmail: AgentEmail = {
|
|
1782
|
+
getRaw: async () => {
|
|
1783
|
+
const reader = email.raw.getReader();
|
|
1784
|
+
const chunks: Uint8Array[] = [];
|
|
1785
|
+
|
|
1786
|
+
let done = false;
|
|
1787
|
+
while (!done) {
|
|
1788
|
+
const { value, done: readerDone } = await reader.read();
|
|
1789
|
+
done = readerDone;
|
|
1790
|
+
if (value) {
|
|
1791
|
+
chunks.push(value);
|
|
1792
|
+
}
|
|
1793
|
+
}
|
|
1794
|
+
|
|
1795
|
+
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
|
|
1796
|
+
const combined = new Uint8Array(totalLength);
|
|
1797
|
+
let offset = 0;
|
|
1798
|
+
for (const chunk of chunks) {
|
|
1799
|
+
combined.set(chunk, offset);
|
|
1800
|
+
offset += chunk.length;
|
|
1801
|
+
}
|
|
1802
|
+
|
|
1803
|
+
return combined;
|
|
1804
|
+
},
|
|
1805
|
+
headers: email.headers,
|
|
1806
|
+
rawSize: email.rawSize,
|
|
1807
|
+
setReject: (reason: string) => {
|
|
1808
|
+
email.setReject(reason);
|
|
1809
|
+
},
|
|
1810
|
+
forward: (rcptTo: string, headers?: Headers) => {
|
|
1811
|
+
return email.forward(rcptTo, headers);
|
|
1812
|
+
},
|
|
1813
|
+
reply: (options: { from: string; to: string; raw: string }) => {
|
|
1814
|
+
return email.reply(
|
|
1815
|
+
new EmailMessage(options.from, options.to, options.raw)
|
|
1816
|
+
);
|
|
1817
|
+
},
|
|
1818
|
+
from: email.from,
|
|
1819
|
+
to: email.to
|
|
1820
|
+
};
|
|
1821
|
+
|
|
1822
|
+
await agent._onEmail(serialisableEmail);
|
|
1823
|
+
}
|
|
1824
|
+
|
|
1825
|
+
export type AgentEmail = {
|
|
1826
|
+
from: string;
|
|
1827
|
+
to: string;
|
|
1828
|
+
getRaw: () => Promise<Uint8Array>;
|
|
1829
|
+
headers: Headers;
|
|
1830
|
+
rawSize: number;
|
|
1831
|
+
setReject: (reason: string) => void;
|
|
1832
|
+
forward: (rcptTo: string, headers?: Headers) => Promise<void>;
|
|
1833
|
+
reply: (options: { from: string; to: string; raw: string }) => Promise<void>;
|
|
1834
|
+
};
|
|
1835
|
+
|
|
1836
|
+
export type EmailSendOptions = {
|
|
1837
|
+
to: string;
|
|
1838
|
+
subject: string;
|
|
1839
|
+
body: string;
|
|
1840
|
+
contentType?: string;
|
|
1841
|
+
headers?: Record<string, string>;
|
|
1842
|
+
includeRoutingHeaders?: boolean;
|
|
1843
|
+
agentName?: string;
|
|
1844
|
+
agentId?: string;
|
|
1845
|
+
domain?: string;
|
|
1846
|
+
};
|
|
1174
1847
|
|
|
1175
1848
|
/**
|
|
1176
1849
|
* Get or create an Agent by name
|
|
@@ -1196,13 +1869,13 @@ export async function getAgentByName<Env, T extends Agent<Env>>(
|
|
|
1196
1869
|
* A wrapper for streaming responses in callable methods
|
|
1197
1870
|
*/
|
|
1198
1871
|
export class StreamingResponse {
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1872
|
+
private _connection: Connection;
|
|
1873
|
+
private _id: string;
|
|
1874
|
+
private _closed = false;
|
|
1202
1875
|
|
|
1203
1876
|
constructor(connection: Connection, id: string) {
|
|
1204
|
-
this
|
|
1205
|
-
this
|
|
1877
|
+
this._connection = connection;
|
|
1878
|
+
this._id = id;
|
|
1206
1879
|
}
|
|
1207
1880
|
|
|
1208
1881
|
/**
|
|
@@ -1210,17 +1883,17 @@ export class StreamingResponse {
|
|
|
1210
1883
|
* @param chunk The data to send
|
|
1211
1884
|
*/
|
|
1212
1885
|
send(chunk: unknown) {
|
|
1213
|
-
if (this
|
|
1886
|
+
if (this._closed) {
|
|
1214
1887
|
throw new Error("StreamingResponse is already closed");
|
|
1215
1888
|
}
|
|
1216
1889
|
const response: RPCResponse = {
|
|
1217
|
-
type: "rpc",
|
|
1218
|
-
id: this.#id,
|
|
1219
|
-
success: true,
|
|
1220
|
-
result: chunk,
|
|
1221
1890
|
done: false,
|
|
1891
|
+
id: this._id,
|
|
1892
|
+
result: chunk,
|
|
1893
|
+
success: true,
|
|
1894
|
+
type: "rpc"
|
|
1222
1895
|
};
|
|
1223
|
-
this
|
|
1896
|
+
this._connection.send(JSON.stringify(response));
|
|
1224
1897
|
}
|
|
1225
1898
|
|
|
1226
1899
|
/**
|
|
@@ -1228,17 +1901,17 @@ export class StreamingResponse {
|
|
|
1228
1901
|
* @param finalChunk Optional final chunk of data to send
|
|
1229
1902
|
*/
|
|
1230
1903
|
end(finalChunk?: unknown) {
|
|
1231
|
-
if (this
|
|
1904
|
+
if (this._closed) {
|
|
1232
1905
|
throw new Error("StreamingResponse is already closed");
|
|
1233
1906
|
}
|
|
1234
|
-
this
|
|
1907
|
+
this._closed = true;
|
|
1235
1908
|
const response: RPCResponse = {
|
|
1236
|
-
type: "rpc",
|
|
1237
|
-
id: this.#id,
|
|
1238
|
-
success: true,
|
|
1239
|
-
result: finalChunk,
|
|
1240
1909
|
done: true,
|
|
1910
|
+
id: this._id,
|
|
1911
|
+
result: finalChunk,
|
|
1912
|
+
success: true,
|
|
1913
|
+
type: "rpc"
|
|
1241
1914
|
};
|
|
1242
|
-
this
|
|
1915
|
+
this._connection.send(JSON.stringify(response));
|
|
1243
1916
|
}
|
|
1244
1917
|
}
|