agents 0.0.0-7f84d28 → 0.0.0-804e02e

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 (72) 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 +13 -9
  5. package/dist/ai-chat-agent.js +189 -59
  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 +70 -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-Y67CHZBI.js → chunk-QEPGNUG6.js} +231 -45
  26. package/dist/chunk-QEPGNUG6.js.map +1 -0
  27. package/dist/{chunk-QSGN3REV.js → chunk-QEVM4BVL.js} +10 -17
  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-BohGLma8.d.ts +5041 -0
  38. package/dist/client.d.ts +8 -2
  39. package/dist/client.js +3 -1
  40. package/dist/index.d.ts +196 -25
  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 +957 -637
  48. package/dist/mcp/index.js.map +1 -1
  49. package/dist/mcp/x402.d.ts +31 -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 +81 -11
  56. package/dist/react.js +22 -9
  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/dist/serializable.d.ts +32 -0
  64. package/dist/serializable.js +1 -0
  65. package/dist/serializable.js.map +1 -0
  66. package/package.json +91 -70
  67. package/src/index.ts +947 -203
  68. package/dist/chunk-BZXOAZUX.js.map +0 -1
  69. package/dist/chunk-QSGN3REV.js.map +0 -1
  70. package/dist/chunk-RIYR6FR6.js +0 -777
  71. package/dist/chunk-RIYR6FR6.js.map +0 -1
  72. package/dist/chunk-Y67CHZBI.js.map +0 -1
package/dist/client.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import {
2
2
  PartySocketOptions,
3
3
  PartyFetchOptions,
4
- PartySocket,
4
+ PartySocket
5
5
  } from "partysocket";
6
+ import { SerializableReturnValue, SerializableValue } from "./serializable.js";
6
7
 
7
8
  /**
8
9
  * Options for creating an AgentClient
@@ -65,6 +66,11 @@ declare class AgentClient<State = unknown> extends PartySocket {
65
66
  * @param streamOptions Options for handling streaming responses
66
67
  * @returns Promise that resolves with the method's return value
67
68
  */
69
+ call<T extends SerializableReturnValue>(
70
+ method: string,
71
+ args?: SerializableValue[],
72
+ streamOptions?: StreamOptions
73
+ ): Promise<T>;
68
74
  call<T = unknown>(
69
75
  method: string,
70
76
  args?: unknown[],
@@ -88,5 +94,5 @@ export {
88
94
  type AgentClientOptions,
89
95
  type StreamOptions,
90
96
  agentFetch,
91
- camelCaseToKebabCase,
97
+ camelCaseToKebabCase
92
98
  };
package/dist/client.js CHANGED
@@ -2,7 +2,9 @@ import {
2
2
  AgentClient,
3
3
  agentFetch,
4
4
  camelCaseToKebabCase
5
- } from "./chunk-QSGN3REV.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,15 +1,27 @@
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";
1
9
  import { Server, Connection, PartyServerOptions } from "partyserver";
2
10
  export { Connection, ConnectionContext, WSMessage } from "partyserver";
3
- import { MCPClientManager } from "./mcp/client.js";
4
- import { Tool, Prompt, Resource } from "@modelcontextprotocol/sdk/types.js";
5
- import { Client } from "@modelcontextprotocol/sdk/client/index.js";
11
+ import {
12
+ c as MCPClientManager,
13
+ T as TransportType
14
+ } from "./client-BohGLma8.js";
15
+ import { Observability } from "./observability/index.js";
16
+ import { MessageType } from "./ai-types.js";
6
17
  import "zod";
18
+ import "@modelcontextprotocol/sdk/shared/protocol.js";
19
+ import "ai";
7
20
  import "@modelcontextprotocol/sdk/client/sse.js";
21
+ import "@modelcontextprotocol/sdk/client/streamableHttp.js";
8
22
  import "./mcp/do-oauth-client-provider.js";
9
23
  import "@modelcontextprotocol/sdk/client/auth.js";
10
24
  import "@modelcontextprotocol/sdk/shared/auth.js";
11
- import "@modelcontextprotocol/sdk/shared/protocol.js";
12
- import "ai";
13
25
 
14
26
  /**
15
27
  * RPC request message from client
@@ -24,14 +36,14 @@ type RPCRequest = {
24
36
  * State update message from client
25
37
  */
26
38
  type StateUpdateMessage = {
27
- type: "cf_agent_state";
39
+ type: MessageType.CF_AGENT_STATE;
28
40
  state: unknown;
29
41
  };
30
42
  /**
31
43
  * RPC response message to client
32
44
  */
33
45
  type RPCResponse = {
34
- type: "rpc";
46
+ type: MessageType.RPC;
35
47
  id: string;
36
48
  } & (
37
49
  | {
@@ -62,12 +74,24 @@ type CallableMetadata = {
62
74
  * Decorator that marks a method as callable by clients
63
75
  * @param metadata Optional metadata about the callable method
64
76
  */
65
- declare function unstable_callable(
77
+ declare function callable(
66
78
  metadata?: CallableMetadata
67
79
  ): <This, Args extends unknown[], Return>(
68
80
  target: (this: This, ...args: Args) => Return,
69
81
  context: ClassMethodDecoratorContext
70
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
+ };
71
95
  /**
72
96
  * Represents a scheduled task within an Agent
73
97
  * @template T Type of the payload data
@@ -103,11 +127,12 @@ type Schedule<T = string> = {
103
127
  cron: string;
104
128
  }
105
129
  );
130
+
106
131
  /**
107
132
  * MCP Server state update message from server -> Client
108
133
  */
109
134
  type MCPServerMessage = {
110
- type: "cf_agent_mcp_servers";
135
+ type: MessageType.CF_AGENT_MCP_SERVERS;
111
136
  mcp: MCPServersState;
112
137
  };
113
138
  type MCPServersState = {
@@ -123,23 +148,30 @@ type MCPServer = {
123
148
  server_url: string;
124
149
  auth_url: string | null;
125
150
  state: "authenticating" | "connecting" | "ready" | "discovering" | "failed";
151
+ instructions: string | null;
152
+ capabilities: ServerCapabilities | null;
126
153
  };
127
154
  declare function getCurrentAgent<
128
- T extends Agent<unknown, unknown> = Agent<unknown, unknown>,
155
+ T extends Agent<unknown, unknown> = Agent<unknown, unknown>
129
156
  >(): {
130
157
  agent: T | undefined;
131
158
  connection: Connection | undefined;
132
- request: Request<unknown, CfProperties<unknown>> | undefined;
159
+ request: Request | undefined;
160
+ email: AgentEmail | undefined;
133
161
  };
134
162
  /**
135
163
  * Base class for creating Agent implementations
136
164
  * @template Env Environment type containing bindings
137
165
  * @template State State type to store within the Agent
138
166
  */
139
- 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> {
140
172
  private _state;
141
173
  private _ParentClass;
142
- mcp: MCPClientManager;
174
+ readonly mcp: MCPClientManager;
143
175
  /**
144
176
  * Initial state for the Agent
145
177
  * Override to provide default state values
@@ -156,6 +188,10 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
156
188
  /** Whether the Agent should hibernate when inactive */
157
189
  hibernate: boolean;
158
190
  };
191
+ /**
192
+ * The observability implementation to use for the Agent
193
+ */
194
+ observability?: Observability;
159
195
  /**
160
196
  * Execute SQL queries against the Agent's database
161
197
  * @template T Type of the returned rows
@@ -181,17 +217,75 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
181
217
  */
182
218
  onStateUpdate(state: State | undefined, source: Connection | "server"): void;
183
219
  /**
184
- * 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
185
222
  * @param email Email message to process
186
223
  */
187
- 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>;
188
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;
189
247
  onError(connection: Connection, error: unknown): void | Promise<void>;
190
248
  onError(error: unknown): void | Promise<void>;
191
249
  /**
192
250
  * Render content (not implemented in base class)
193
251
  */
194
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>[]>;
195
289
  /**
196
290
  * Schedule a task to be executed in the future
197
291
  * @template T Type of the payload data
@@ -246,12 +340,17 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
246
340
  * Destroy the Agent, removing all state and scheduled tasks
247
341
  */
248
342
  destroy(): Promise<void>;
343
+ /**
344
+ * Get all methods marked as callable on this Agent
345
+ * @returns A map of method names to their metadata
346
+ */
249
347
  private _isCallable;
250
348
  /**
251
349
  * Connect to a new MCP Server
252
350
  *
351
+ * @param serverName Name of the MCP server
253
352
  * @param url MCP Server SSE URL
254
- * @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.
255
354
  * @param agentsPrefix agents routing prefix if not using `agents`
256
355
  * @param options MCP client and transport (header) options
257
356
  * @returns authUrl
@@ -259,7 +358,7 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
259
358
  addMcpServer(
260
359
  serverName: string,
261
360
  url: string,
262
- callbackHost: string,
361
+ callbackHost?: string,
263
362
  agentsPrefix?: string,
264
363
  options?: {
265
364
  client?: ConstructorParameters<typeof Client>[1];
@@ -272,7 +371,7 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
272
371
  authUrl: string | undefined;
273
372
  }>;
274
373
  _connectToMcpServerInternal(
275
- serverName: string,
374
+ _serverName: string,
276
375
  url: string,
277
376
  callbackUrl: string,
278
377
  options?: {
@@ -286,6 +385,7 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
286
385
  */
287
386
  transport?: {
288
387
  headers?: HeadersInit;
388
+ type?: TransportType;
289
389
  };
290
390
  },
291
391
  reconnect?: {
@@ -295,9 +395,10 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
295
395
  ): Promise<{
296
396
  id: string;
297
397
  authUrl: string | undefined;
398
+ clientId: string | undefined;
298
399
  }>;
299
400
  removeMcpServer(id: string): Promise<void>;
300
- private _getMcpServerStateInternal;
401
+ getMcpServers(): MCPServersState;
301
402
  }
302
403
  /**
303
404
  * Namespace for creating Agent instances
@@ -330,17 +431,72 @@ declare function routeAgentRequest<Env>(
330
431
  env: Env,
331
432
  options?: AgentOptions<Env>
332
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
+ };
333
467
  /**
334
468
  * Route an email to the appropriate Agent
335
- * @param email Email message to route
336
- * @param env Environment containing Agent bindings
337
- * @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
338
473
  */
339
474
  declare function routeAgentEmail<Env>(
340
475
  email: ForwardableEmailMessage,
341
476
  env: Env,
342
- options?: AgentOptions<Env>
477
+ options: EmailRoutingOptions<Env>
343
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
+ };
344
500
  /**
345
501
  * Get or create an Agent by name
346
502
  * @template Env Environment type containing bindings
@@ -350,12 +506,17 @@ declare function routeAgentEmail<Env>(
350
506
  * @param options Options for Agent creation
351
507
  * @returns Promise resolving to an Agent instance stub
352
508
  */
353
- 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
+ >(
354
514
  namespace: AgentNamespace<T>,
355
515
  name: string,
356
516
  options?: {
357
517
  jurisdiction?: DurableObjectJurisdiction;
358
518
  locationHint?: DurableObjectLocationHint;
519
+ props?: Props;
359
520
  }
360
521
  ): Promise<DurableObjectStub<T>>;
361
522
  /**
@@ -381,20 +542,30 @@ declare class StreamingResponse {
381
542
  export {
382
543
  Agent,
383
544
  type AgentContext,
545
+ type AgentEmail,
384
546
  type AgentNamespace,
385
547
  type AgentOptions,
386
548
  type CallableMetadata,
549
+ type EmailResolver,
550
+ type EmailRoutingOptions,
551
+ type EmailSendOptions,
387
552
  type MCPServer,
388
553
  type MCPServerMessage,
389
554
  type MCPServersState,
555
+ type QueueItem,
390
556
  type RPCRequest,
391
557
  type RPCResponse,
392
558
  type Schedule,
393
559
  type StateUpdateMessage,
394
560
  StreamingResponse,
561
+ TransportType,
562
+ callable,
563
+ createAddressBasedEmailResolver,
564
+ createCatchAllEmailResolver,
565
+ createHeaderBasedEmailResolver,
395
566
  getAgentByName,
396
567
  getCurrentAgent,
397
568
  routeAgentEmail,
398
569
  routeAgentRequest,
399
- unstable_callable,
570
+ unstable_callable
400
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-RIYR6FR6.js";
10
- import "./chunk-BZXOAZUX.js";
11
- import "./chunk-QSGN3REV.js";
12
- import "./chunk-Y67CHZBI.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,