agents 0.0.0-fb503ba → 0.0.0-fb62d22

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 (69) 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 +12 -8
  5. package/dist/ai-chat-agent.js +482 -60
  6. package/dist/ai-chat-agent.js.map +1 -1
  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 +69 -72
  11. package/dist/ai-react.js +260 -107
  12. package/dist/ai-react.js.map +1 -1
  13. package/dist/ai-types.d.ts +37 -19
  14. package/dist/ai-types.js +7 -0
  15. package/dist/ccip-CMBYN64O.js +15 -0
  16. package/dist/ccip-CMBYN64O.js.map +1 -0
  17. package/dist/chunk-5Y6BEZDY.js +276 -0
  18. package/dist/chunk-5Y6BEZDY.js.map +1 -0
  19. package/dist/chunk-BER7KXUJ.js +18 -0
  20. package/dist/chunk-BER7KXUJ.js.map +1 -0
  21. package/dist/chunk-JJBFIGUC.js +5202 -0
  22. package/dist/chunk-JJBFIGUC.js.map +1 -0
  23. package/dist/chunk-PR4QN5HX.js +43 -0
  24. package/dist/chunk-PR4QN5HX.js.map +1 -0
  25. package/dist/{chunk-OYJXQRRH.js → chunk-QEPGNUG6.js} +230 -45
  26. package/dist/chunk-QEPGNUG6.js.map +1 -0
  27. package/dist/{chunk-VCSB47AK.js → chunk-QEVM4BVL.js} +10 -10
  28. package/dist/chunk-QEVM4BVL.js.map +1 -0
  29. package/dist/chunk-RS5OCNEQ.js +1323 -0
  30. package/dist/chunk-RS5OCNEQ.js.map +1 -0
  31. package/dist/chunk-TYAY6AU6.js +159 -0
  32. package/dist/chunk-TYAY6AU6.js.map +1 -0
  33. package/dist/chunk-UJVEAURM.js +150 -0
  34. package/dist/chunk-UJVEAURM.js.map +1 -0
  35. package/dist/{chunk-BZXOAZUX.js → chunk-XFS5ERG3.js} +27 -6
  36. package/dist/chunk-XFS5ERG3.js.map +1 -0
  37. package/dist/client-DNEb-lf3.d.ts +5041 -0
  38. package/dist/client.d.ts +2 -2
  39. package/dist/client.js +3 -1
  40. package/dist/index.d.ts +186 -26
  41. package/dist/index.js +14 -4
  42. package/dist/mcp/client.d.ts +9 -781
  43. package/dist/mcp/client.js +2 -1
  44. package/dist/mcp/do-oauth-client-provider.d.ts +9 -0
  45. package/dist/mcp/do-oauth-client-provider.js +2 -1
  46. package/dist/mcp/index.d.ts +58 -64
  47. package/dist/mcp/index.js +956 -639
  48. package/dist/mcp/index.js.map +1 -1
  49. package/dist/mcp/x402.d.ts +39 -0
  50. package/dist/mcp/x402.js +3195 -0
  51. package/dist/mcp/x402.js.map +1 -0
  52. package/dist/observability/index.d.ts +46 -0
  53. package/dist/observability/index.js +12 -0
  54. package/dist/observability/index.js.map +1 -0
  55. package/dist/react.d.ts +12 -8
  56. package/dist/react.js +13 -10
  57. package/dist/react.js.map +1 -1
  58. package/dist/schedule.d.ts +81 -7
  59. package/dist/schedule.js +21 -6
  60. package/dist/schedule.js.map +1 -1
  61. package/dist/secp256k1-M22GZP2U.js +2193 -0
  62. package/dist/secp256k1-M22GZP2U.js.map +1 -0
  63. package/package.json +91 -70
  64. package/src/index.ts +923 -190
  65. package/dist/chunk-BZXOAZUX.js.map +0 -1
  66. package/dist/chunk-MXJNY43J.js +0 -785
  67. package/dist/chunk-MXJNY43J.js.map +0 -1
  68. package/dist/chunk-OYJXQRRH.js.map +0 -1
  69. package/dist/chunk-VCSB47AK.js.map +0 -1
package/dist/client.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  PartySocketOptions,
3
3
  PartyFetchOptions,
4
- PartySocket,
4
+ PartySocket
5
5
  } from "partysocket";
6
6
  import { SerializableReturnValue, SerializableValue } from "./serializable.js";
7
7
 
@@ -94,5 +94,5 @@ export {
94
94
  type AgentClientOptions,
95
95
  type StreamOptions,
96
96
  agentFetch,
97
- camelCaseToKebabCase,
97
+ camelCaseToKebabCase
98
98
  };
package/dist/client.js CHANGED
@@ -2,7 +2,9 @@ import {
2
2
  AgentClient,
3
3
  agentFetch,
4
4
  camelCaseToKebabCase
5
- } from "./chunk-VCSB47AK.js";
5
+ } from "./chunk-QEVM4BVL.js";
6
+ import "./chunk-BER7KXUJ.js";
7
+ import "./chunk-PR4QN5HX.js";
6
8
  export {
7
9
  AgentClient,
8
10
  agentFetch,
package/dist/index.d.ts CHANGED
@@ -1,20 +1,27 @@
1
- import { Server, Connection, PartyServerOptions } from "partyserver";
2
- export { Connection, ConnectionContext, WSMessage } from "partyserver";
1
+ import { env } from "cloudflare:workers";
2
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
3
3
  import {
4
4
  ServerCapabilities,
5
5
  Tool,
6
6
  Prompt,
7
- Resource,
7
+ Resource
8
8
  } from "@modelcontextprotocol/sdk/types.js";
9
- import { MCPClientManager } from "./mcp/client.js";
10
- import { Client } from "@modelcontextprotocol/sdk/client/index.js";
9
+ import { Server, Connection, PartyServerOptions } from "partyserver";
10
+ export { Connection, ConnectionContext, WSMessage } from "partyserver";
11
+ import {
12
+ c as MCPClientManager,
13
+ T as TransportType
14
+ } from "./client-DNEb-lf3.js";
15
+ import { Observability } from "./observability/index.js";
16
+ import { MessageType } from "./ai-types.js";
11
17
  import "zod";
18
+ import "@modelcontextprotocol/sdk/shared/protocol.js";
19
+ import "ai";
12
20
  import "@modelcontextprotocol/sdk/client/sse.js";
21
+ import "@modelcontextprotocol/sdk/client/streamableHttp.js";
13
22
  import "./mcp/do-oauth-client-provider.js";
14
23
  import "@modelcontextprotocol/sdk/client/auth.js";
15
24
  import "@modelcontextprotocol/sdk/shared/auth.js";
16
- import "@modelcontextprotocol/sdk/shared/protocol.js";
17
- import "ai";
18
25
 
19
26
  /**
20
27
  * RPC request message from client
@@ -29,14 +36,14 @@ type RPCRequest = {
29
36
  * State update message from client
30
37
  */
31
38
  type StateUpdateMessage = {
32
- type: "cf_agent_state";
39
+ type: MessageType.CF_AGENT_STATE;
33
40
  state: unknown;
34
41
  };
35
42
  /**
36
43
  * RPC response message to client
37
44
  */
38
45
  type RPCResponse = {
39
- type: "rpc";
46
+ type: MessageType.RPC;
40
47
  id: string;
41
48
  } & (
42
49
  | {
@@ -67,12 +74,24 @@ type CallableMetadata = {
67
74
  * Decorator that marks a method as callable by clients
68
75
  * @param metadata Optional metadata about the callable method
69
76
  */
70
- declare function unstable_callable(
77
+ declare function callable(
71
78
  metadata?: CallableMetadata
72
79
  ): <This, Args extends unknown[], Return>(
73
80
  target: (this: This, ...args: Args) => Return,
74
81
  context: ClassMethodDecoratorContext
75
82
  ) => (this: This, ...args: Args) => Return;
83
+ /**
84
+ * Decorator that marks a method as callable by clients
85
+ * @deprecated this has been renamed to callable, and unstable_callable will be removed in the next major version
86
+ * @param metadata Optional metadata about the callable method
87
+ */
88
+ declare const unstable_callable: (metadata?: CallableMetadata) => void;
89
+ type QueueItem<T = string> = {
90
+ id: string;
91
+ payload: T;
92
+ callback: keyof Agent<unknown>;
93
+ created_at: number;
94
+ };
76
95
  /**
77
96
  * Represents a scheduled task within an Agent
78
97
  * @template T Type of the payload data
@@ -108,11 +127,12 @@ type Schedule<T = string> = {
108
127
  cron: string;
109
128
  }
110
129
  );
130
+
111
131
  /**
112
132
  * MCP Server state update message from server -> Client
113
133
  */
114
134
  type MCPServerMessage = {
115
- type: "cf_agent_mcp_servers";
135
+ type: MessageType.CF_AGENT_MCP_SERVERS;
116
136
  mcp: MCPServersState;
117
137
  };
118
138
  type MCPServersState = {
@@ -132,21 +152,26 @@ type MCPServer = {
132
152
  capabilities: ServerCapabilities | null;
133
153
  };
134
154
  declare function getCurrentAgent<
135
- T extends Agent<unknown, unknown> = Agent<unknown, unknown>,
155
+ T extends Agent<unknown, unknown> = Agent<unknown, unknown>
136
156
  >(): {
137
157
  agent: T | undefined;
138
158
  connection: Connection | undefined;
139
- request: Request<unknown, CfProperties<unknown>> | undefined;
159
+ request: Request | undefined;
160
+ email: AgentEmail | undefined;
140
161
  };
141
162
  /**
142
163
  * Base class for creating Agent implementations
143
164
  * @template Env Environment type containing bindings
144
165
  * @template State State type to store within the Agent
145
166
  */
146
- declare class Agent<Env, State = unknown> extends Server<Env> {
167
+ declare class Agent<
168
+ Env = typeof env,
169
+ State = unknown,
170
+ Props extends Record<string, unknown> = Record<string, unknown>
171
+ > extends Server<Env, Props> {
147
172
  private _state;
148
173
  private _ParentClass;
149
- mcp: MCPClientManager;
174
+ readonly mcp: MCPClientManager;
150
175
  /**
151
176
  * Initial state for the Agent
152
177
  * Override to provide default state values
@@ -163,6 +188,10 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
163
188
  /** Whether the Agent should hibernate when inactive */
164
189
  hibernate: boolean;
165
190
  };
191
+ /**
192
+ * The observability implementation to use for the Agent
193
+ */
194
+ observability?: Observability;
166
195
  /**
167
196
  * Execute SQL queries against the Agent's database
168
197
  * @template T Type of the returned rows
@@ -188,17 +217,75 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
188
217
  */
189
218
  onStateUpdate(state: State | undefined, source: Connection | "server"): void;
190
219
  /**
191
- * Called when the Agent receives an email
220
+ * Called when the Agent receives an email via routeAgentEmail()
221
+ * Override this method to handle incoming emails
192
222
  * @param email Email message to process
193
223
  */
194
- onEmail(email: ForwardableEmailMessage): Promise<void>;
224
+ _onEmail(email: AgentEmail): Promise<void>;
225
+ /**
226
+ * Reply to an email
227
+ * @param email The email to reply to
228
+ * @param options Options for the reply
229
+ * @returns void
230
+ */
231
+ replyToEmail(
232
+ email: AgentEmail,
233
+ options: {
234
+ fromName: string;
235
+ subject?: string | undefined;
236
+ body: string;
237
+ contentType?: string;
238
+ headers?: Record<string, string>;
239
+ }
240
+ ): Promise<void>;
195
241
  private _tryCatch;
242
+ /**
243
+ * Automatically wrap custom methods with agent context
244
+ * This ensures getCurrentAgent() works in all custom methods without decorators
245
+ */
246
+ private _autoWrapCustomMethods;
196
247
  onError(connection: Connection, error: unknown): void | Promise<void>;
197
248
  onError(error: unknown): void | Promise<void>;
198
249
  /**
199
250
  * Render content (not implemented in base class)
200
251
  */
201
252
  render(): void;
253
+ /**
254
+ * Queue a task to be executed in the future
255
+ * @param payload Payload to pass to the callback
256
+ * @param callback Name of the method to call
257
+ * @returns The ID of the queued task
258
+ */
259
+ queue<T = unknown>(callback: keyof this, payload: T): Promise<string>;
260
+ private _flushingQueue;
261
+ private _flushQueue;
262
+ /**
263
+ * Dequeue a task by ID
264
+ * @param id ID of the task to dequeue
265
+ */
266
+ dequeue(id: string): Promise<void>;
267
+ /**
268
+ * Dequeue all tasks
269
+ */
270
+ dequeueAll(): Promise<void>;
271
+ /**
272
+ * Dequeue all tasks by callback
273
+ * @param callback Name of the callback to dequeue
274
+ */
275
+ dequeueAllByCallback(callback: string): Promise<void>;
276
+ /**
277
+ * Get a queued task by ID
278
+ * @param id ID of the task to get
279
+ * @returns The task or undefined if not found
280
+ */
281
+ getQueue(id: string): Promise<QueueItem<string> | undefined>;
282
+ /**
283
+ * Get all queues by key and value
284
+ * @param key Key to filter by
285
+ * @param value Value to filter by
286
+ * @returns Array of matching QueueItem objects
287
+ */
288
+ getQueues(key: string, value: string): Promise<QueueItem<string>[]>;
202
289
  /**
203
290
  * Schedule a task to be executed in the future
204
291
  * @template T Type of the payload data
@@ -261,8 +348,9 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
261
348
  /**
262
349
  * Connect to a new MCP Server
263
350
  *
351
+ * @param serverName Name of the MCP server
264
352
  * @param url MCP Server SSE URL
265
- * @param callbackHost Base host for the agent, used for the redirect URI.
353
+ * @param callbackHost Base host for the agent, used for the redirect URI. If not provided, will be derived from the current request.
266
354
  * @param agentsPrefix agents routing prefix if not using `agents`
267
355
  * @param options MCP client and transport (header) options
268
356
  * @returns authUrl
@@ -270,7 +358,7 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
270
358
  addMcpServer(
271
359
  serverName: string,
272
360
  url: string,
273
- callbackHost: string,
361
+ callbackHost?: string,
274
362
  agentsPrefix?: string,
275
363
  options?: {
276
364
  client?: ConstructorParameters<typeof Client>[1];
@@ -283,7 +371,7 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
283
371
  authUrl: string | undefined;
284
372
  }>;
285
373
  _connectToMcpServerInternal(
286
- serverName: string,
374
+ _serverName: string,
287
375
  url: string,
288
376
  callbackUrl: string,
289
377
  options?: {
@@ -297,6 +385,7 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
297
385
  */
298
386
  transport?: {
299
387
  headers?: HeadersInit;
388
+ type?: TransportType;
300
389
  };
301
390
  },
302
391
  reconnect?: {
@@ -306,6 +395,7 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
306
395
  ): Promise<{
307
396
  id: string;
308
397
  authUrl: string | undefined;
398
+ clientId: string | undefined;
309
399
  }>;
310
400
  removeMcpServer(id: string): Promise<void>;
311
401
  getMcpServers(): MCPServersState;
@@ -341,17 +431,72 @@ declare function routeAgentRequest<Env>(
341
431
  env: Env,
342
432
  options?: AgentOptions<Env>
343
433
  ): Promise<Response | null>;
434
+ type EmailResolver<Env> = (
435
+ email: ForwardableEmailMessage,
436
+ env: Env
437
+ ) => Promise<{
438
+ agentName: string;
439
+ agentId: string;
440
+ } | null>;
441
+ /**
442
+ * Create a resolver that uses the message-id header to determine the agent to route the email to
443
+ * @returns A function that resolves the agent to route the email to
444
+ */
445
+ declare function createHeaderBasedEmailResolver<Env>(): EmailResolver<Env>;
446
+ /**
447
+ * Create a resolver that uses the email address to determine the agent to route the email to
448
+ * @param defaultAgentName The default agent name to use if the email address does not contain a sub-address
449
+ * @returns A function that resolves the agent to route the email to
450
+ */
451
+ declare function createAddressBasedEmailResolver<Env>(
452
+ defaultAgentName: string
453
+ ): EmailResolver<Env>;
454
+ /**
455
+ * Create a resolver that uses the agentName and agentId to determine the agent to route the email to
456
+ * @param agentName The name of the agent to route the email to
457
+ * @param agentId The id of the agent to route the email to
458
+ * @returns A function that resolves the agent to route the email to
459
+ */
460
+ declare function createCatchAllEmailResolver<Env>(
461
+ agentName: string,
462
+ agentId: string
463
+ ): EmailResolver<Env>;
464
+ type EmailRoutingOptions<Env> = AgentOptions<Env> & {
465
+ resolver: EmailResolver<Env>;
466
+ };
344
467
  /**
345
468
  * Route an email to the appropriate Agent
346
- * @param email Email message to route
347
- * @param env Environment containing Agent bindings
348
- * @param options Routing options
469
+ * @param email The email to route
470
+ * @param env The environment containing the Agent bindings
471
+ * @param options The options for routing the email
472
+ * @returns A promise that resolves when the email has been routed
349
473
  */
350
474
  declare function routeAgentEmail<Env>(
351
475
  email: ForwardableEmailMessage,
352
476
  env: Env,
353
- options?: AgentOptions<Env>
477
+ options: EmailRoutingOptions<Env>
354
478
  ): Promise<void>;
479
+ type AgentEmail = {
480
+ from: string;
481
+ to: string;
482
+ getRaw: () => Promise<Uint8Array>;
483
+ headers: Headers;
484
+ rawSize: number;
485
+ setReject: (reason: string) => void;
486
+ forward: (rcptTo: string, headers?: Headers) => Promise<void>;
487
+ reply: (options: { from: string; to: string; raw: string }) => Promise<void>;
488
+ };
489
+ type EmailSendOptions = {
490
+ to: string;
491
+ subject: string;
492
+ body: string;
493
+ contentType?: string;
494
+ headers?: Record<string, string>;
495
+ includeRoutingHeaders?: boolean;
496
+ agentName?: string;
497
+ agentId?: string;
498
+ domain?: string;
499
+ };
355
500
  /**
356
501
  * Get or create an Agent by name
357
502
  * @template Env Environment type containing bindings
@@ -361,12 +506,17 @@ declare function routeAgentEmail<Env>(
361
506
  * @param options Options for Agent creation
362
507
  * @returns Promise resolving to an Agent instance stub
363
508
  */
364
- declare function getAgentByName<Env, T extends Agent<Env>>(
509
+ declare function getAgentByName<
510
+ Env,
511
+ T extends Agent<Env>,
512
+ Props extends Record<string, unknown> = Record<string, unknown>
513
+ >(
365
514
  namespace: AgentNamespace<T>,
366
515
  name: string,
367
516
  options?: {
368
517
  jurisdiction?: DurableObjectJurisdiction;
369
518
  locationHint?: DurableObjectLocationHint;
519
+ props?: Props;
370
520
  }
371
521
  ): Promise<DurableObjectStub<T>>;
372
522
  /**
@@ -392,20 +542,30 @@ declare class StreamingResponse {
392
542
  export {
393
543
  Agent,
394
544
  type AgentContext,
545
+ type AgentEmail,
395
546
  type AgentNamespace,
396
547
  type AgentOptions,
397
548
  type CallableMetadata,
549
+ type EmailResolver,
550
+ type EmailRoutingOptions,
551
+ type EmailSendOptions,
398
552
  type MCPServer,
399
553
  type MCPServerMessage,
400
554
  type MCPServersState,
555
+ type QueueItem,
401
556
  type RPCRequest,
402
557
  type RPCResponse,
403
558
  type Schedule,
404
559
  type StateUpdateMessage,
405
560
  StreamingResponse,
561
+ TransportType,
562
+ callable,
563
+ createAddressBasedEmailResolver,
564
+ createCatchAllEmailResolver,
565
+ createHeaderBasedEmailResolver,
406
566
  getAgentByName,
407
567
  getCurrentAgent,
408
568
  routeAgentEmail,
409
569
  routeAgentRequest,
410
- unstable_callable,
570
+ unstable_callable
411
571
  };
package/dist/index.js CHANGED
@@ -1,18 +1,28 @@
1
1
  import {
2
2
  Agent,
3
3
  StreamingResponse,
4
+ callable,
5
+ createAddressBasedEmailResolver,
6
+ createCatchAllEmailResolver,
7
+ createHeaderBasedEmailResolver,
4
8
  getAgentByName,
5
9
  getCurrentAgent,
6
10
  routeAgentEmail,
7
11
  routeAgentRequest,
8
12
  unstable_callable
9
- } from "./chunk-MXJNY43J.js";
10
- import "./chunk-OYJXQRRH.js";
11
- import "./chunk-BZXOAZUX.js";
12
- import "./chunk-VCSB47AK.js";
13
+ } from "./chunk-RS5OCNEQ.js";
14
+ import "./chunk-QEPGNUG6.js";
15
+ import "./chunk-XFS5ERG3.js";
16
+ import "./chunk-QEVM4BVL.js";
17
+ import "./chunk-BER7KXUJ.js";
18
+ import "./chunk-PR4QN5HX.js";
13
19
  export {
14
20
  Agent,
15
21
  StreamingResponse,
22
+ callable,
23
+ createAddressBasedEmailResolver,
24
+ createCatchAllEmailResolver,
25
+ createHeaderBasedEmailResolver,
16
26
  getAgentByName,
17
27
  getCurrentAgent,
18
28
  routeAgentEmail,