agents 0.0.0-c3e8618 → 0.0.0-c41ebbc

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.
Files changed (76) hide show
  1. package/README.md +255 -27
  2. package/dist/_esm-LV5FJ3HK.js +3922 -0
  3. package/dist/_esm-LV5FJ3HK.js.map +1 -0
  4. package/dist/ai-chat-agent.d.ts +78 -23
  5. package/dist/ai-chat-agent.js +657 -0
  6. package/dist/ai-chat-agent.js.map +1 -0
  7. package/dist/ai-chat-v5-migration.d.ts +152 -0
  8. package/dist/ai-chat-v5-migration.js +20 -0
  9. package/dist/ai-chat-v5-migration.js.map +1 -0
  10. package/dist/ai-react.d.ts +86 -47
  11. package/dist/ai-react.js +356 -0
  12. package/dist/ai-react.js.map +1 -0
  13. package/dist/ai-types.d.ts +84 -41
  14. package/dist/ai-types.js +8 -0
  15. package/dist/ai-types.js.map +1 -0
  16. package/dist/ccip-CMBYN64O.js +15 -0
  17. package/dist/ccip-CMBYN64O.js.map +1 -0
  18. package/dist/chunk-5Y6BEZDY.js +276 -0
  19. package/dist/chunk-5Y6BEZDY.js.map +1 -0
  20. package/dist/chunk-BER7KXUJ.js +18 -0
  21. package/dist/chunk-BER7KXUJ.js.map +1 -0
  22. package/dist/chunk-C2OEBJZ2.js +113 -0
  23. package/dist/chunk-C2OEBJZ2.js.map +1 -0
  24. package/dist/chunk-JJBFIGUC.js +5202 -0
  25. package/dist/chunk-JJBFIGUC.js.map +1 -0
  26. package/dist/chunk-PR4QN5HX.js +43 -0
  27. package/dist/chunk-PR4QN5HX.js.map +1 -0
  28. package/dist/chunk-QEVM4BVL.js +116 -0
  29. package/dist/chunk-QEVM4BVL.js.map +1 -0
  30. package/dist/chunk-TYAY6AU6.js +159 -0
  31. package/dist/chunk-TYAY6AU6.js.map +1 -0
  32. package/dist/chunk-UJVEAURM.js +150 -0
  33. package/dist/chunk-UJVEAURM.js.map +1 -0
  34. package/dist/chunk-XGMKNUJA.js +1343 -0
  35. package/dist/chunk-XGMKNUJA.js.map +1 -0
  36. package/dist/chunk-ZMMHNOMZ.js +942 -0
  37. package/dist/chunk-ZMMHNOMZ.js.map +1 -0
  38. package/dist/client-D1Fa-xS_.d.ts +5120 -0
  39. package/dist/client.d.ts +71 -37
  40. package/dist/client.js +13 -0
  41. package/dist/client.js.map +1 -0
  42. package/dist/codemode/ai.d.ts +25 -0
  43. package/dist/codemode/ai.js +5200 -0
  44. package/dist/codemode/ai.js.map +1 -0
  45. package/dist/index.d.ts +479 -181
  46. package/dist/index.js +32 -0
  47. package/dist/index.js.map +1 -0
  48. package/dist/mcp/client.d.ts +12 -675
  49. package/dist/mcp/client.js +10 -0
  50. package/dist/mcp/client.js.map +1 -0
  51. package/dist/mcp/do-oauth-client-provider.d.ts +42 -0
  52. package/dist/mcp/do-oauth-client-provider.js +8 -0
  53. package/dist/mcp/do-oauth-client-provider.js.map +1 -0
  54. package/dist/mcp/index.d.ts +71 -33
  55. package/dist/mcp/index.js +1099 -0
  56. package/dist/mcp/index.js.map +1 -0
  57. package/dist/mcp/x402.d.ts +39 -0
  58. package/dist/mcp/x402.js +3195 -0
  59. package/dist/mcp/x402.js.map +1 -0
  60. package/dist/mcp-BH1fJeiU.d.ts +58 -0
  61. package/dist/observability/index.d.ts +34 -0
  62. package/dist/observability/index.js +12 -0
  63. package/dist/observability/index.js.map +1 -0
  64. package/dist/react.d.ts +115 -18
  65. package/dist/react.js +216 -0
  66. package/dist/react.js.map +1 -0
  67. package/dist/schedule.d.ts +106 -22
  68. package/dist/schedule.js +86 -0
  69. package/dist/schedule.js.map +1 -0
  70. package/dist/secp256k1-M22GZP2U.js +2193 -0
  71. package/dist/secp256k1-M22GZP2U.js.map +1 -0
  72. package/dist/serializable.d.ts +32 -0
  73. package/dist/serializable.js +1 -0
  74. package/dist/serializable.js.map +1 -0
  75. package/package.json +103 -52
  76. package/src/index.ts +1268 -156
package/dist/index.d.ts CHANGED
@@ -1,202 +1,397 @@
1
- import { Server, Connection, PartyServerOptions } from 'partyserver';
2
- export { Connection, ConnectionContext, WSMessage } from 'partyserver';
3
- import { AsyncLocalStorage } from 'node:async_hooks';
4
- import { WorkflowEntrypoint as WorkflowEntrypoint$1 } from 'cloudflare:workers';
1
+ import { env } from "cloudflare:workers";
2
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
3
+ import {
4
+ ServerCapabilities,
5
+ Tool,
6
+ Prompt,
7
+ Resource
8
+ } from "@modelcontextprotocol/sdk/types.js";
9
+ import { Server, Connection, PartyServerOptions } from "partyserver";
10
+ export { Connection, ConnectionContext, WSMessage } from "partyserver";
11
+ import {
12
+ e as MCPClientManager,
13
+ T as TransportType,
14
+ f as MCPConnectionState
15
+ } from "./client-D1Fa-xS_.js";
16
+ import { Observability } from "./observability/index.js";
17
+ import { MessageType } from "./ai-types.js";
18
+ import "zod";
19
+ import "@modelcontextprotocol/sdk/shared/protocol.js";
20
+ import "ai";
21
+ import "./mcp-BH1fJeiU.js";
22
+ import "@modelcontextprotocol/sdk/client/sse.js";
23
+ import "@modelcontextprotocol/sdk/client/streamableHttp.js";
24
+ import "./mcp/do-oauth-client-provider.js";
25
+ import "@modelcontextprotocol/sdk/client/auth.js";
26
+ import "@modelcontextprotocol/sdk/shared/auth.js";
5
27
 
6
28
  /**
7
29
  * RPC request message from client
8
30
  */
9
31
  type RPCRequest = {
10
- type: "rpc";
11
- id: string;
12
- method: string;
13
- args: unknown[];
32
+ type: "rpc";
33
+ id: string;
34
+ method: string;
35
+ args: unknown[];
14
36
  };
15
37
  /**
16
38
  * State update message from client
17
39
  */
18
40
  type StateUpdateMessage = {
19
- type: "cf_agent_state";
20
- state: unknown;
41
+ type: MessageType.CF_AGENT_STATE;
42
+ state: unknown;
21
43
  };
22
44
  /**
23
45
  * RPC response message to client
24
46
  */
25
47
  type RPCResponse = {
26
- type: "rpc";
27
- id: string;
28
- } & ({
29
- success: true;
30
- result: unknown;
31
- done?: false;
32
- } | {
33
- success: true;
34
- result: unknown;
35
- done: true;
36
- } | {
37
- success: false;
38
- error: string;
39
- });
48
+ type: MessageType.RPC;
49
+ id: string;
50
+ } & (
51
+ | {
52
+ success: true;
53
+ result: unknown;
54
+ done?: false;
55
+ }
56
+ | {
57
+ success: true;
58
+ result: unknown;
59
+ done: true;
60
+ }
61
+ | {
62
+ success: false;
63
+ error: string;
64
+ }
65
+ );
40
66
  /**
41
67
  * Metadata for a callable method
42
68
  */
43
69
  type CallableMetadata = {
44
- /** Optional description of what the method does */
45
- description?: string;
46
- /** Whether the method supports streaming responses */
47
- streaming?: boolean;
70
+ /** Optional description of what the method does */
71
+ description?: string;
72
+ /** Whether the method supports streaming responses */
73
+ streaming?: boolean;
48
74
  };
49
75
  /**
50
76
  * Decorator that marks a method as callable by clients
51
77
  * @param metadata Optional metadata about the callable method
52
78
  */
53
- declare function unstable_callable(metadata?: CallableMetadata): <This, Args extends unknown[], Return>(target: (this: This, ...args: Args) => Return, context: ClassMethodDecoratorContext) => (this: This, ...args: Args) => Return;
79
+ declare function callable(
80
+ metadata?: CallableMetadata
81
+ ): <This, Args extends unknown[], Return>(
82
+ target: (this: This, ...args: Args) => Return,
83
+ context: ClassMethodDecoratorContext
84
+ ) => (this: This, ...args: Args) => Return;
54
85
  /**
55
- * A class for creating workflow entry points that can be used with Cloudflare Workers
86
+ * Decorator that marks a method as callable by clients
87
+ * @deprecated this has been renamed to callable, and unstable_callable will be removed in the next major version
88
+ * @param metadata Optional metadata about the callable method
56
89
  */
57
- declare class WorkflowEntrypoint extends WorkflowEntrypoint$1 {
58
- }
90
+ declare const unstable_callable: (metadata?: CallableMetadata) => void;
91
+ type QueueItem<T = string> = {
92
+ id: string;
93
+ payload: T;
94
+ callback: keyof Agent<unknown>;
95
+ created_at: number;
96
+ };
59
97
  /**
60
98
  * Represents a scheduled task within an Agent
61
99
  * @template T Type of the payload data
62
100
  */
63
101
  type Schedule<T = string> = {
64
- /** Unique identifier for the schedule */
65
- id: string;
66
- /** Name of the method to be called */
67
- callback: string;
68
- /** Data to be passed to the callback */
69
- payload: T;
70
- } & ({
71
- /** Type of schedule for one-time execution at a specific time */
72
- type: "scheduled";
73
- /** Timestamp when the task should execute */
74
- time: number;
75
- } | {
76
- /** Type of schedule for delayed execution */
77
- type: "delayed";
78
- /** Timestamp when the task should execute */
79
- time: number;
80
- /** Number of seconds to delay execution */
81
- delayInSeconds: number;
82
- } | {
83
- /** Type of schedule for recurring execution based on cron expression */
84
- type: "cron";
85
- /** Timestamp for the next execution */
86
- time: number;
87
- /** Cron expression defining the schedule */
88
- cron: string;
89
- });
90
- declare const unstable_context: AsyncLocalStorage<{
91
- agent: Agent<unknown>;
92
- connection: Connection | undefined;
93
- request: Request | undefined;
94
- }>;
102
+ /** Unique identifier for the schedule */
103
+ id: string;
104
+ /** Name of the method to be called */
105
+ callback: string;
106
+ /** Data to be passed to the callback */
107
+ payload: T;
108
+ } & (
109
+ | {
110
+ /** Type of schedule for one-time execution at a specific time */
111
+ type: "scheduled";
112
+ /** Timestamp when the task should execute */
113
+ time: number;
114
+ }
115
+ | {
116
+ /** Type of schedule for delayed execution */
117
+ type: "delayed";
118
+ /** Timestamp when the task should execute */
119
+ time: number;
120
+ /** Number of seconds to delay execution */
121
+ delayInSeconds: number;
122
+ }
123
+ | {
124
+ /** Type of schedule for recurring execution based on cron expression */
125
+ type: "cron";
126
+ /** Timestamp for the next execution */
127
+ time: number;
128
+ /** Cron expression defining the schedule */
129
+ cron: string;
130
+ }
131
+ );
132
+
133
+ /**
134
+ * MCP Server state update message from server -> Client
135
+ */
136
+ type MCPServerMessage = {
137
+ type: MessageType.CF_AGENT_MCP_SERVERS;
138
+ mcp: MCPServersState;
139
+ };
140
+ type MCPServersState = {
141
+ servers: {
142
+ [id: string]: MCPServer;
143
+ };
144
+ tools: Tool[];
145
+ prompts: Prompt[];
146
+ resources: Resource[];
147
+ };
148
+ type MCPServer = {
149
+ name: string;
150
+ server_url: string;
151
+ auth_url: string | null;
152
+ state: MCPConnectionState;
153
+ instructions: string | null;
154
+ capabilities: ServerCapabilities | null;
155
+ };
156
+ declare function getCurrentAgent<
157
+ T extends Agent<unknown, unknown> = Agent<unknown, unknown>
158
+ >(): {
159
+ agent: T | undefined;
160
+ connection: Connection | undefined;
161
+ request: Request | undefined;
162
+ email: AgentEmail | undefined;
163
+ };
95
164
  /**
96
165
  * Base class for creating Agent implementations
97
166
  * @template Env Environment type containing bindings
98
167
  * @template State State type to store within the Agent
99
168
  */
100
- declare class Agent<Env, State = unknown> extends Server<Env> {
101
- #private;
102
- /**
103
- * Initial state for the Agent
104
- * Override to provide default state values
105
- */
106
- initialState: State;
107
- /**
108
- * Current state of the Agent
109
- */
110
- get state(): State;
111
- /**
112
- * Agent configuration options
113
- */
114
- static options: {
115
- /** Whether the Agent should hibernate when inactive */
116
- hibernate: boolean;
169
+ declare class Agent<
170
+ Env = typeof env,
171
+ State = unknown,
172
+ Props extends Record<string, unknown> = Record<string, unknown>
173
+ > extends Server<Env, Props> {
174
+ private _state;
175
+ private _disposables;
176
+ private _ParentClass;
177
+ readonly mcp: MCPClientManager;
178
+ /**
179
+ * Initial state for the Agent
180
+ * Override to provide default state values
181
+ */
182
+ initialState: State;
183
+ /**
184
+ * Current state of the Agent
185
+ */
186
+ get state(): State;
187
+ /**
188
+ * Agent configuration options
189
+ */
190
+ static options: {
191
+ /** Whether the Agent should hibernate when inactive */
192
+ hibernate: boolean;
193
+ };
194
+ /**
195
+ * The observability implementation to use for the Agent
196
+ */
197
+ observability?: Observability;
198
+ /**
199
+ * Execute SQL queries against the Agent's database
200
+ * @template T Type of the returned rows
201
+ * @param strings SQL query template strings
202
+ * @param values Values to be inserted into the query
203
+ * @returns Array of query results
204
+ */
205
+ sql<T = Record<string, string | number | boolean | null>>(
206
+ strings: TemplateStringsArray,
207
+ ...values: (string | number | boolean | null)[]
208
+ ): T[];
209
+ constructor(ctx: AgentContext, env: Env);
210
+ private _setStateInternal;
211
+ /**
212
+ * Update the Agent's state
213
+ * @param state New state to set
214
+ */
215
+ setState(state: State): void;
216
+ /**
217
+ * Called when the Agent's state is updated
218
+ * @param state Updated state
219
+ * @param source Source of the state update ("server" or a client connection)
220
+ */
221
+ onStateUpdate(state: State | undefined, source: Connection | "server"): void;
222
+ /**
223
+ * Called when the Agent receives an email via routeAgentEmail()
224
+ * Override this method to handle incoming emails
225
+ * @param email Email message to process
226
+ */
227
+ _onEmail(email: AgentEmail): Promise<void>;
228
+ /**
229
+ * Reply to an email
230
+ * @param email The email to reply to
231
+ * @param options Options for the reply
232
+ * @returns void
233
+ */
234
+ replyToEmail(
235
+ email: AgentEmail,
236
+ options: {
237
+ fromName: string;
238
+ subject?: string | undefined;
239
+ body: string;
240
+ contentType?: string;
241
+ headers?: Record<string, string>;
242
+ }
243
+ ): Promise<void>;
244
+ private _tryCatch;
245
+ /**
246
+ * Automatically wrap custom methods with agent context
247
+ * This ensures getCurrentAgent() works in all custom methods without decorators
248
+ */
249
+ private _autoWrapCustomMethods;
250
+ onError(connection: Connection, error: unknown): void | Promise<void>;
251
+ onError(error: unknown): void | Promise<void>;
252
+ /**
253
+ * Render content (not implemented in base class)
254
+ */
255
+ render(): void;
256
+ /**
257
+ * Queue a task to be executed in the future
258
+ * @param payload Payload to pass to the callback
259
+ * @param callback Name of the method to call
260
+ * @returns The ID of the queued task
261
+ */
262
+ queue<T = unknown>(callback: keyof this, payload: T): Promise<string>;
263
+ private _flushingQueue;
264
+ private _flushQueue;
265
+ /**
266
+ * Dequeue a task by ID
267
+ * @param id ID of the task to dequeue
268
+ */
269
+ dequeue(id: string): Promise<void>;
270
+ /**
271
+ * Dequeue all tasks
272
+ */
273
+ dequeueAll(): Promise<void>;
274
+ /**
275
+ * Dequeue all tasks by callback
276
+ * @param callback Name of the callback to dequeue
277
+ */
278
+ dequeueAllByCallback(callback: string): Promise<void>;
279
+ /**
280
+ * Get a queued task by ID
281
+ * @param id ID of the task to get
282
+ * @returns The task or undefined if not found
283
+ */
284
+ getQueue(id: string): Promise<QueueItem<string> | undefined>;
285
+ /**
286
+ * Get all queues by key and value
287
+ * @param key Key to filter by
288
+ * @param value Value to filter by
289
+ * @returns Array of matching QueueItem objects
290
+ */
291
+ getQueues(key: string, value: string): Promise<QueueItem<string>[]>;
292
+ /**
293
+ * Schedule a task to be executed in the future
294
+ * @template T Type of the payload data
295
+ * @param when When to execute the task (Date, seconds delay, or cron expression)
296
+ * @param callback Name of the method to call
297
+ * @param payload Data to pass to the callback
298
+ * @returns Schedule object representing the scheduled task
299
+ */
300
+ schedule<T = string>(
301
+ when: Date | string | number,
302
+ callback: keyof this,
303
+ payload?: T
304
+ ): Promise<Schedule<T>>;
305
+ /**
306
+ * Get a scheduled task by ID
307
+ * @template T Type of the payload data
308
+ * @param id ID of the scheduled task
309
+ * @returns The Schedule object or undefined if not found
310
+ */
311
+ getSchedule<T = string>(id: string): Promise<Schedule<T> | undefined>;
312
+ /**
313
+ * Get scheduled tasks matching the given criteria
314
+ * @template T Type of the payload data
315
+ * @param criteria Criteria to filter schedules
316
+ * @returns Array of matching Schedule objects
317
+ */
318
+ getSchedules<T = string>(criteria?: {
319
+ id?: string;
320
+ type?: "scheduled" | "delayed" | "cron";
321
+ timeRange?: {
322
+ start?: Date;
323
+ end?: Date;
117
324
  };
118
- /**
119
- * Execute SQL queries against the Agent's database
120
- * @template T Type of the returned rows
121
- * @param strings SQL query template strings
122
- * @param values Values to be inserted into the query
123
- * @returns Array of query results
124
- */
125
- sql<T = Record<string, string | number | boolean | null>>(strings: TemplateStringsArray, ...values: (string | number | boolean | null)[]): T[];
126
- constructor(ctx: AgentContext, env: Env);
127
- /**
128
- * Update the Agent's state
129
- * @param state New state to set
130
- */
131
- setState(state: State): void;
132
- /**
133
- * Called when the Agent's state is updated
134
- * @param state Updated state
135
- * @param source Source of the state update ("server" or a client connection)
136
- */
137
- onStateUpdate(state: State | undefined, source: Connection | "server"): void;
138
- /**
139
- * Called when the Agent receives an email
140
- * @param email Email message to process
141
- */
142
- onEmail(email: ForwardableEmailMessage): Promise<void>;
143
- onError(connection: Connection, error: unknown): void | Promise<void>;
144
- onError(error: unknown): void | Promise<void>;
145
- /**
146
- * Render content (not implemented in base class)
147
- */
148
- render(): void;
149
- /**
150
- * Schedule a task to be executed in the future
151
- * @template T Type of the payload data
152
- * @param when When to execute the task (Date, seconds delay, or cron expression)
153
- * @param callback Name of the method to call
154
- * @param payload Data to pass to the callback
155
- * @returns Schedule object representing the scheduled task
156
- */
157
- schedule<T = string>(when: Date | string | number, callback: keyof this, payload?: T): Promise<Schedule<T>>;
158
- /**
159
- * Get a scheduled task by ID
160
- * @template T Type of the payload data
161
- * @param id ID of the scheduled task
162
- * @returns The Schedule object or undefined if not found
163
- */
164
- getSchedule<T = string>(id: string): Promise<Schedule<T> | undefined>;
165
- /**
166
- * Get scheduled tasks matching the given criteria
167
- * @template T Type of the payload data
168
- * @param criteria Criteria to filter schedules
169
- * @returns Array of matching Schedule objects
170
- */
171
- getSchedules<T = string>(criteria?: {
172
- id?: string;
173
- type?: "scheduled" | "delayed" | "cron";
174
- timeRange?: {
175
- start?: Date;
176
- end?: Date;
177
- };
178
- }): Schedule<T>[];
179
- /**
180
- * Cancel a scheduled task
181
- * @param id ID of the task to cancel
182
- * @returns true if the task was cancelled, false otherwise
183
- */
184
- cancelSchedule(id: string): Promise<boolean>;
185
- /**
186
- * Method called when an alarm fires
187
- * Executes any scheduled tasks that are due
188
- */
189
- alarm(): Promise<void>;
190
- /**
191
- * Destroy the Agent, removing all state and scheduled tasks
192
- */
193
- destroy(): Promise<void>;
325
+ }): Schedule<T>[];
326
+ /**
327
+ * Cancel a scheduled task
328
+ * @param id ID of the task to cancel
329
+ * @returns true if the task was cancelled, false otherwise
330
+ */
331
+ cancelSchedule(id: string): Promise<boolean>;
332
+ private _scheduleNextAlarm;
333
+ /**
334
+ * Method called when an alarm fires.
335
+ * Executes any scheduled tasks that are due.
336
+ *
337
+ * @remarks
338
+ * To schedule a task, please use the `this.schedule` method instead.
339
+ * See {@link https://developers.cloudflare.com/agents/api-reference/schedule-tasks/}
340
+ */
341
+ readonly alarm: () => Promise<void>;
342
+ /**
343
+ * Destroy the Agent, removing all state and scheduled tasks
344
+ */
345
+ destroy(): Promise<void>;
346
+ /**
347
+ * Get all methods marked as callable on this Agent
348
+ * @returns A map of method names to their metadata
349
+ */
350
+ private _isCallable;
351
+ /**
352
+ * Connect to a new MCP Server
353
+ *
354
+ * @param serverName Name of the MCP server
355
+ * @param url MCP Server SSE URL
356
+ * @param callbackHost Base host for the agent, used for the redirect URI. If not provided, will be derived from the current request.
357
+ * @param agentsPrefix agents routing prefix if not using `agents`
358
+ * @param options MCP client and transport options
359
+ * @returns authUrl
360
+ */
361
+ addMcpServer(
362
+ serverName: string,
363
+ url: string,
364
+ callbackHost?: string,
365
+ agentsPrefix?: string,
366
+ options?: {
367
+ client?: ConstructorParameters<typeof Client>[1];
368
+ transport?: {
369
+ headers?: HeadersInit;
370
+ type?: TransportType;
371
+ };
372
+ }
373
+ ): Promise<{
374
+ id: string;
375
+ authUrl: string | undefined;
376
+ }>;
377
+ private _connectToMcpServerInternal;
378
+ removeMcpServer(id: string): Promise<void>;
379
+ getMcpServers(): MCPServersState;
380
+ private broadcastMcpServers;
381
+ /**
382
+ * Handle OAuth callback response using MCPClientManager configuration
383
+ * @param result OAuth callback result
384
+ * @param request The original request (needed for base URL)
385
+ * @returns Response for the OAuth callback
386
+ */
387
+ private handleOAuthCallbackResponse;
194
388
  }
195
389
  /**
196
390
  * Namespace for creating Agent instances
197
391
  * @template Agentic Type of the Agent class
198
392
  */
199
- type AgentNamespace<Agentic extends Agent<unknown>> = DurableObjectNamespace<Agentic>;
393
+ type AgentNamespace<Agentic extends Agent<unknown>> =
394
+ DurableObjectNamespace<Agentic>;
200
395
  /**
201
396
  * Agent's durable context
202
397
  */
@@ -205,10 +400,10 @@ type AgentContext = DurableObjectState;
205
400
  * Configuration options for Agent routing
206
401
  */
207
402
  type AgentOptions<Env> = PartyServerOptions<Env> & {
208
- /**
209
- * Whether to enable CORS for the Agent
210
- */
211
- cors?: boolean | HeadersInit | undefined;
403
+ /**
404
+ * Whether to enable CORS for the Agent
405
+ */
406
+ cors?: boolean | HeadersInit | undefined;
212
407
  };
213
408
  /**
214
409
  * Route a request to the appropriate Agent
@@ -217,14 +412,77 @@ type AgentOptions<Env> = PartyServerOptions<Env> & {
217
412
  * @param options Routing options
218
413
  * @returns Response from the Agent or undefined if no route matched
219
414
  */
220
- declare function routeAgentRequest<Env>(request: Request, env: Env, options?: AgentOptions<Env>): Promise<Response | null>;
415
+ declare function routeAgentRequest<Env>(
416
+ request: Request,
417
+ env: Env,
418
+ options?: AgentOptions<Env>
419
+ ): Promise<Response | null>;
420
+ type EmailResolver<Env> = (
421
+ email: ForwardableEmailMessage,
422
+ env: Env
423
+ ) => Promise<{
424
+ agentName: string;
425
+ agentId: string;
426
+ } | null>;
427
+ /**
428
+ * Create a resolver that uses the message-id header to determine the agent to route the email to
429
+ * @returns A function that resolves the agent to route the email to
430
+ */
431
+ declare function createHeaderBasedEmailResolver<Env>(): EmailResolver<Env>;
432
+ /**
433
+ * Create a resolver that uses the email address to determine the agent to route the email to
434
+ * @param defaultAgentName The default agent name to use if the email address does not contain a sub-address
435
+ * @returns A function that resolves the agent to route the email to
436
+ */
437
+ declare function createAddressBasedEmailResolver<Env>(
438
+ defaultAgentName: string
439
+ ): EmailResolver<Env>;
440
+ /**
441
+ * Create a resolver that uses the agentName and agentId to determine the agent to route the email to
442
+ * @param agentName The name of the agent to route the email to
443
+ * @param agentId The id of the agent to route the email to
444
+ * @returns A function that resolves the agent to route the email to
445
+ */
446
+ declare function createCatchAllEmailResolver<Env>(
447
+ agentName: string,
448
+ agentId: string
449
+ ): EmailResolver<Env>;
450
+ type EmailRoutingOptions<Env> = AgentOptions<Env> & {
451
+ resolver: EmailResolver<Env>;
452
+ };
221
453
  /**
222
454
  * Route an email to the appropriate Agent
223
- * @param email Email message to route
224
- * @param env Environment containing Agent bindings
225
- * @param options Routing options
455
+ * @param email The email to route
456
+ * @param env The environment containing the Agent bindings
457
+ * @param options The options for routing the email
458
+ * @returns A promise that resolves when the email has been routed
226
459
  */
227
- declare function routeAgentEmail<Env>(email: ForwardableEmailMessage, env: Env, options?: AgentOptions<Env>): Promise<void>;
460
+ declare function routeAgentEmail<Env>(
461
+ email: ForwardableEmailMessage,
462
+ env: Env,
463
+ options: EmailRoutingOptions<Env>
464
+ ): Promise<void>;
465
+ type AgentEmail = {
466
+ from: string;
467
+ to: string;
468
+ getRaw: () => Promise<Uint8Array>;
469
+ headers: Headers;
470
+ rawSize: number;
471
+ setReject: (reason: string) => void;
472
+ forward: (rcptTo: string, headers?: Headers) => Promise<void>;
473
+ reply: (options: { from: string; to: string; raw: string }) => Promise<void>;
474
+ };
475
+ type EmailSendOptions = {
476
+ to: string;
477
+ subject: string;
478
+ body: string;
479
+ contentType?: string;
480
+ headers?: Record<string, string>;
481
+ includeRoutingHeaders?: boolean;
482
+ agentName?: string;
483
+ agentId?: string;
484
+ domain?: string;
485
+ };
228
486
  /**
229
487
  * Get or create an Agent by name
230
488
  * @template Env Environment type containing bindings
@@ -234,26 +492,66 @@ declare function routeAgentEmail<Env>(email: ForwardableEmailMessage, env: Env,
234
492
  * @param options Options for Agent creation
235
493
  * @returns Promise resolving to an Agent instance stub
236
494
  */
237
- declare function getAgentByName<Env, T extends Agent<Env>>(namespace: AgentNamespace<T>, name: string, options?: {
495
+ declare function getAgentByName<
496
+ Env,
497
+ T extends Agent<Env>,
498
+ Props extends Record<string, unknown> = Record<string, unknown>
499
+ >(
500
+ namespace: AgentNamespace<T>,
501
+ name: string,
502
+ options?: {
238
503
  jurisdiction?: DurableObjectJurisdiction;
239
504
  locationHint?: DurableObjectLocationHint;
240
- }): Promise<DurableObjectStub<T>>;
505
+ props?: Props;
506
+ }
507
+ ): Promise<DurableObjectStub<T>>;
241
508
  /**
242
509
  * A wrapper for streaming responses in callable methods
243
510
  */
244
511
  declare class StreamingResponse {
245
- #private;
246
- constructor(connection: Connection, id: string);
247
- /**
248
- * Send a chunk of data to the client
249
- * @param chunk The data to send
250
- */
251
- send(chunk: unknown): void;
252
- /**
253
- * End the stream and send the final chunk (if any)
254
- * @param finalChunk Optional final chunk of data to send
255
- */
256
- end(finalChunk?: unknown): void;
512
+ private _connection;
513
+ private _id;
514
+ private _closed;
515
+ constructor(connection: Connection, id: string);
516
+ /**
517
+ * Send a chunk of data to the client
518
+ * @param chunk The data to send
519
+ */
520
+ send(chunk: unknown): void;
521
+ /**
522
+ * End the stream and send the final chunk (if any)
523
+ * @param finalChunk Optional final chunk of data to send
524
+ */
525
+ end(finalChunk?: unknown): void;
257
526
  }
258
527
 
259
- export { Agent, type AgentContext, type AgentNamespace, type AgentOptions, type CallableMetadata, type RPCRequest, type RPCResponse, type Schedule, type StateUpdateMessage, StreamingResponse, WorkflowEntrypoint, getAgentByName, routeAgentEmail, routeAgentRequest, unstable_callable, unstable_context };
528
+ export {
529
+ Agent,
530
+ type AgentContext,
531
+ type AgentEmail,
532
+ type AgentNamespace,
533
+ type AgentOptions,
534
+ type CallableMetadata,
535
+ type EmailResolver,
536
+ type EmailRoutingOptions,
537
+ type EmailSendOptions,
538
+ type MCPServer,
539
+ type MCPServerMessage,
540
+ type MCPServersState,
541
+ type QueueItem,
542
+ type RPCRequest,
543
+ type RPCResponse,
544
+ type Schedule,
545
+ type StateUpdateMessage,
546
+ StreamingResponse,
547
+ TransportType,
548
+ callable,
549
+ createAddressBasedEmailResolver,
550
+ createCatchAllEmailResolver,
551
+ createHeaderBasedEmailResolver,
552
+ getAgentByName,
553
+ getCurrentAgent,
554
+ routeAgentEmail,
555
+ routeAgentRequest,
556
+ unstable_callable
557
+ };