agents 0.0.0-d6a4eda → 0.0.0-d72c6a2

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 (55) hide show
  1. package/README.md +255 -27
  2. package/dist/ai-chat-agent.d.ts +13 -9
  3. package/dist/ai-chat-agent.js +188 -59
  4. package/dist/ai-chat-agent.js.map +1 -1
  5. package/dist/ai-chat-v5-migration.d.ts +152 -0
  6. package/dist/ai-chat-v5-migration.js +19 -0
  7. package/dist/ai-chat-v5-migration.js.map +1 -0
  8. package/dist/ai-react.d.ts +64 -72
  9. package/dist/ai-react.js +161 -54
  10. package/dist/ai-react.js.map +1 -1
  11. package/dist/ai-types.d.ts +36 -19
  12. package/dist/ai-types.js +6 -0
  13. package/dist/chunk-AVYJQSLW.js +17 -0
  14. package/dist/chunk-AVYJQSLW.js.map +1 -0
  15. package/dist/{chunk-BZXOAZUX.js → chunk-LL2AFX7V.js} +8 -5
  16. package/dist/chunk-LL2AFX7V.js.map +1 -0
  17. package/dist/{chunk-Y67CHZBI.js → chunk-MH46VMM4.js} +183 -35
  18. package/dist/chunk-MH46VMM4.js.map +1 -0
  19. package/dist/{chunk-QSGN3REV.js → chunk-QEVM4BVL.js} +10 -17
  20. package/dist/chunk-QEVM4BVL.js.map +1 -0
  21. package/dist/chunk-UJVEAURM.js +150 -0
  22. package/dist/chunk-UJVEAURM.js.map +1 -0
  23. package/dist/chunk-YDUDMOL6.js +1296 -0
  24. package/dist/chunk-YDUDMOL6.js.map +1 -0
  25. package/dist/client-CvaJdLQA.d.ts +5015 -0
  26. package/dist/client.d.ts +8 -2
  27. package/dist/client.js +2 -1
  28. package/dist/index.d.ts +186 -22
  29. package/dist/index.js +13 -4
  30. package/dist/mcp/client.d.ts +9 -781
  31. package/dist/mcp/client.js +1 -1
  32. package/dist/mcp/do-oauth-client-provider.d.ts +1 -0
  33. package/dist/mcp/do-oauth-client-provider.js +1 -1
  34. package/dist/mcp/index.d.ts +69 -56
  35. package/dist/mcp/index.js +858 -608
  36. package/dist/mcp/index.js.map +1 -1
  37. package/dist/observability/index.d.ts +46 -0
  38. package/dist/observability/index.js +11 -0
  39. package/dist/observability/index.js.map +1 -0
  40. package/dist/react.d.ts +81 -11
  41. package/dist/react.js +21 -9
  42. package/dist/react.js.map +1 -1
  43. package/dist/schedule.d.ts +81 -7
  44. package/dist/schedule.js +19 -6
  45. package/dist/schedule.js.map +1 -1
  46. package/dist/serializable.d.ts +32 -0
  47. package/dist/serializable.js +1 -0
  48. package/dist/serializable.js.map +1 -0
  49. package/package.json +85 -70
  50. package/src/index.ts +904 -200
  51. package/dist/chunk-BZXOAZUX.js.map +0 -1
  52. package/dist/chunk-QSGN3REV.js.map +0 -1
  53. package/dist/chunk-RIYR6FR6.js +0 -777
  54. package/dist/chunk-RIYR6FR6.js.map +0 -1
  55. 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,8 @@ import {
2
2
  AgentClient,
3
3
  agentFetch,
4
4
  camelCaseToKebabCase
5
- } from "./chunk-QSGN3REV.js";
5
+ } from "./chunk-QEVM4BVL.js";
6
+ import "./chunk-AVYJQSLW.js";
6
7
  export {
7
8
  AgentClient,
8
9
  agentFetch,
package/dist/index.d.ts CHANGED
@@ -1,15 +1,24 @@
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 { M as MCPClientManager } from "./client-CvaJdLQA.js";
12
+ import { Observability } from "./observability/index.js";
13
+ import { MessageType } from "./ai-types.js";
6
14
  import "zod";
15
+ import "@modelcontextprotocol/sdk/shared/protocol.js";
16
+ import "ai";
7
17
  import "@modelcontextprotocol/sdk/client/sse.js";
18
+ import "@modelcontextprotocol/sdk/client/streamableHttp.js";
8
19
  import "./mcp/do-oauth-client-provider.js";
9
20
  import "@modelcontextprotocol/sdk/client/auth.js";
10
21
  import "@modelcontextprotocol/sdk/shared/auth.js";
11
- import "@modelcontextprotocol/sdk/shared/protocol.js";
12
- import "ai";
13
22
 
14
23
  /**
15
24
  * RPC request message from client
@@ -24,14 +33,14 @@ type RPCRequest = {
24
33
  * State update message from client
25
34
  */
26
35
  type StateUpdateMessage = {
27
- type: "cf_agent_state";
36
+ type: MessageType.CF_AGENT_STATE;
28
37
  state: unknown;
29
38
  };
30
39
  /**
31
40
  * RPC response message to client
32
41
  */
33
42
  type RPCResponse = {
34
- type: "rpc";
43
+ type: MessageType.RPC;
35
44
  id: string;
36
45
  } & (
37
46
  | {
@@ -62,12 +71,24 @@ type CallableMetadata = {
62
71
  * Decorator that marks a method as callable by clients
63
72
  * @param metadata Optional metadata about the callable method
64
73
  */
65
- declare function unstable_callable(
74
+ declare function callable(
66
75
  metadata?: CallableMetadata
67
76
  ): <This, Args extends unknown[], Return>(
68
77
  target: (this: This, ...args: Args) => Return,
69
78
  context: ClassMethodDecoratorContext
70
79
  ) => (this: This, ...args: Args) => Return;
80
+ /**
81
+ * Decorator that marks a method as callable by clients
82
+ * @deprecated this has been renamed to callable, and unstable_callable will be removed in the next major version
83
+ * @param metadata Optional metadata about the callable method
84
+ */
85
+ declare const unstable_callable: (metadata?: CallableMetadata) => void;
86
+ type QueueItem<T = string> = {
87
+ id: string;
88
+ payload: T;
89
+ callback: keyof Agent<unknown>;
90
+ created_at: number;
91
+ };
71
92
  /**
72
93
  * Represents a scheduled task within an Agent
73
94
  * @template T Type of the payload data
@@ -107,7 +128,7 @@ type Schedule<T = string> = {
107
128
  * MCP Server state update message from server -> Client
108
129
  */
109
130
  type MCPServerMessage = {
110
- type: "cf_agent_mcp_servers";
131
+ type: MessageType.CF_AGENT_MCP_SERVERS;
111
132
  mcp: MCPServersState;
112
133
  };
113
134
  type MCPServersState = {
@@ -123,20 +144,27 @@ type MCPServer = {
123
144
  server_url: string;
124
145
  auth_url: string | null;
125
146
  state: "authenticating" | "connecting" | "ready" | "discovering" | "failed";
147
+ instructions: string | null;
148
+ capabilities: ServerCapabilities | null;
126
149
  };
127
150
  declare function getCurrentAgent<
128
- T extends Agent<unknown, unknown> = Agent<unknown, unknown>,
151
+ T extends Agent<unknown, unknown> = Agent<unknown, unknown>
129
152
  >(): {
130
153
  agent: T | undefined;
131
154
  connection: Connection | undefined;
132
- request: Request<unknown, CfProperties<unknown>> | undefined;
155
+ request: Request | undefined;
156
+ email: AgentEmail | undefined;
133
157
  };
134
158
  /**
135
159
  * Base class for creating Agent implementations
136
160
  * @template Env Environment type containing bindings
137
161
  * @template State State type to store within the Agent
138
162
  */
139
- declare class Agent<Env, State = unknown> extends Server<Env> {
163
+ declare class Agent<
164
+ Env = typeof env,
165
+ State = unknown,
166
+ Props extends Record<string, unknown> = Record<string, unknown>
167
+ > extends Server<Env, Props> {
140
168
  private _state;
141
169
  private _ParentClass;
142
170
  mcp: MCPClientManager;
@@ -156,6 +184,10 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
156
184
  /** Whether the Agent should hibernate when inactive */
157
185
  hibernate: boolean;
158
186
  };
187
+ /**
188
+ * The observability implementation to use for the Agent
189
+ */
190
+ observability?: Observability;
159
191
  /**
160
192
  * Execute SQL queries against the Agent's database
161
193
  * @template T Type of the returned rows
@@ -181,17 +213,75 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
181
213
  */
182
214
  onStateUpdate(state: State | undefined, source: Connection | "server"): void;
183
215
  /**
184
- * Called when the Agent receives an email
216
+ * Called when the Agent receives an email via routeAgentEmail()
217
+ * Override this method to handle incoming emails
185
218
  * @param email Email message to process
186
219
  */
187
- onEmail(email: ForwardableEmailMessage): Promise<void>;
220
+ _onEmail(email: AgentEmail): Promise<void>;
221
+ /**
222
+ * Reply to an email
223
+ * @param email The email to reply to
224
+ * @param options Options for the reply
225
+ * @returns void
226
+ */
227
+ replyToEmail(
228
+ email: AgentEmail,
229
+ options: {
230
+ fromName: string;
231
+ subject?: string | undefined;
232
+ body: string;
233
+ contentType?: string;
234
+ headers?: Record<string, string>;
235
+ }
236
+ ): Promise<void>;
188
237
  private _tryCatch;
238
+ /**
239
+ * Automatically wrap custom methods with agent context
240
+ * This ensures getCurrentAgent() works in all custom methods without decorators
241
+ */
242
+ private _autoWrapCustomMethods;
189
243
  onError(connection: Connection, error: unknown): void | Promise<void>;
190
244
  onError(error: unknown): void | Promise<void>;
191
245
  /**
192
246
  * Render content (not implemented in base class)
193
247
  */
194
248
  render(): void;
249
+ /**
250
+ * Queue a task to be executed in the future
251
+ * @param payload Payload to pass to the callback
252
+ * @param callback Name of the method to call
253
+ * @returns The ID of the queued task
254
+ */
255
+ queue<T = unknown>(callback: keyof this, payload: T): Promise<string>;
256
+ private _flushingQueue;
257
+ private _flushQueue;
258
+ /**
259
+ * Dequeue a task by ID
260
+ * @param id ID of the task to dequeue
261
+ */
262
+ dequeue(id: string): Promise<void>;
263
+ /**
264
+ * Dequeue all tasks
265
+ */
266
+ dequeueAll(): Promise<void>;
267
+ /**
268
+ * Dequeue all tasks by callback
269
+ * @param callback Name of the callback to dequeue
270
+ */
271
+ dequeueAllByCallback(callback: string): Promise<void>;
272
+ /**
273
+ * Get a queued task by ID
274
+ * @param id ID of the task to get
275
+ * @returns The task or undefined if not found
276
+ */
277
+ getQueue(id: string): Promise<QueueItem<string> | undefined>;
278
+ /**
279
+ * Get all queues by key and value
280
+ * @param key Key to filter by
281
+ * @param value Value to filter by
282
+ * @returns Array of matching QueueItem objects
283
+ */
284
+ getQueues(key: string, value: string): Promise<QueueItem<string>[]>;
195
285
  /**
196
286
  * Schedule a task to be executed in the future
197
287
  * @template T Type of the payload data
@@ -246,6 +336,10 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
246
336
  * Destroy the Agent, removing all state and scheduled tasks
247
337
  */
248
338
  destroy(): Promise<void>;
339
+ /**
340
+ * Get all methods marked as callable on this Agent
341
+ * @returns A map of method names to their metadata
342
+ */
249
343
  private _isCallable;
250
344
  /**
251
345
  * Connect to a new MCP Server
@@ -272,7 +366,7 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
272
366
  authUrl: string | undefined;
273
367
  }>;
274
368
  _connectToMcpServerInternal(
275
- serverName: string,
369
+ _serverName: string,
276
370
  url: string,
277
371
  callbackUrl: string,
278
372
  options?: {
@@ -295,9 +389,10 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
295
389
  ): Promise<{
296
390
  id: string;
297
391
  authUrl: string | undefined;
392
+ clientId: string | undefined;
298
393
  }>;
299
394
  removeMcpServer(id: string): Promise<void>;
300
- private _getMcpServerStateInternal;
395
+ getMcpServers(): MCPServersState;
301
396
  }
302
397
  /**
303
398
  * Namespace for creating Agent instances
@@ -330,17 +425,72 @@ declare function routeAgentRequest<Env>(
330
425
  env: Env,
331
426
  options?: AgentOptions<Env>
332
427
  ): Promise<Response | null>;
428
+ type EmailResolver<Env> = (
429
+ email: ForwardableEmailMessage,
430
+ env: Env
431
+ ) => Promise<{
432
+ agentName: string;
433
+ agentId: string;
434
+ } | null>;
435
+ /**
436
+ * Create a resolver that uses the message-id header to determine the agent to route the email to
437
+ * @returns A function that resolves the agent to route the email to
438
+ */
439
+ declare function createHeaderBasedEmailResolver<Env>(): EmailResolver<Env>;
440
+ /**
441
+ * Create a resolver that uses the email address to determine the agent to route the email to
442
+ * @param defaultAgentName The default agent name to use if the email address does not contain a sub-address
443
+ * @returns A function that resolves the agent to route the email to
444
+ */
445
+ declare function createAddressBasedEmailResolver<Env>(
446
+ defaultAgentName: string
447
+ ): EmailResolver<Env>;
448
+ /**
449
+ * Create a resolver that uses the agentName and agentId to determine the agent to route the email to
450
+ * @param agentName The name of the agent to route the email to
451
+ * @param agentId The id of the agent to route the email to
452
+ * @returns A function that resolves the agent to route the email to
453
+ */
454
+ declare function createCatchAllEmailResolver<Env>(
455
+ agentName: string,
456
+ agentId: string
457
+ ): EmailResolver<Env>;
458
+ type EmailRoutingOptions<Env> = AgentOptions<Env> & {
459
+ resolver: EmailResolver<Env>;
460
+ };
333
461
  /**
334
462
  * 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
463
+ * @param email The email to route
464
+ * @param env The environment containing the Agent bindings
465
+ * @param options The options for routing the email
466
+ * @returns A promise that resolves when the email has been routed
338
467
  */
339
468
  declare function routeAgentEmail<Env>(
340
469
  email: ForwardableEmailMessage,
341
470
  env: Env,
342
- options?: AgentOptions<Env>
471
+ options: EmailRoutingOptions<Env>
343
472
  ): Promise<void>;
473
+ type AgentEmail = {
474
+ from: string;
475
+ to: string;
476
+ getRaw: () => Promise<Uint8Array>;
477
+ headers: Headers;
478
+ rawSize: number;
479
+ setReject: (reason: string) => void;
480
+ forward: (rcptTo: string, headers?: Headers) => Promise<void>;
481
+ reply: (options: { from: string; to: string; raw: string }) => Promise<void>;
482
+ };
483
+ type EmailSendOptions = {
484
+ to: string;
485
+ subject: string;
486
+ body: string;
487
+ contentType?: string;
488
+ headers?: Record<string, string>;
489
+ includeRoutingHeaders?: boolean;
490
+ agentName?: string;
491
+ agentId?: string;
492
+ domain?: string;
493
+ };
344
494
  /**
345
495
  * Get or create an Agent by name
346
496
  * @template Env Environment type containing bindings
@@ -350,12 +500,17 @@ declare function routeAgentEmail<Env>(
350
500
  * @param options Options for Agent creation
351
501
  * @returns Promise resolving to an Agent instance stub
352
502
  */
353
- declare function getAgentByName<Env, T extends Agent<Env>>(
503
+ declare function getAgentByName<
504
+ Env,
505
+ T extends Agent<Env>,
506
+ Props extends Record<string, unknown> = Record<string, unknown>
507
+ >(
354
508
  namespace: AgentNamespace<T>,
355
509
  name: string,
356
510
  options?: {
357
511
  jurisdiction?: DurableObjectJurisdiction;
358
512
  locationHint?: DurableObjectLocationHint;
513
+ props?: Props;
359
514
  }
360
515
  ): Promise<DurableObjectStub<T>>;
361
516
  /**
@@ -381,20 +536,29 @@ declare class StreamingResponse {
381
536
  export {
382
537
  Agent,
383
538
  type AgentContext,
539
+ type AgentEmail,
384
540
  type AgentNamespace,
385
541
  type AgentOptions,
386
542
  type CallableMetadata,
543
+ type EmailResolver,
544
+ type EmailRoutingOptions,
545
+ type EmailSendOptions,
387
546
  type MCPServer,
388
547
  type MCPServerMessage,
389
548
  type MCPServersState,
549
+ type QueueItem,
390
550
  type RPCRequest,
391
551
  type RPCResponse,
392
552
  type Schedule,
393
553
  type StateUpdateMessage,
394
554
  StreamingResponse,
555
+ callable,
556
+ createAddressBasedEmailResolver,
557
+ createCatchAllEmailResolver,
558
+ createHeaderBasedEmailResolver,
395
559
  getAgentByName,
396
560
  getCurrentAgent,
397
561
  routeAgentEmail,
398
562
  routeAgentRequest,
399
- unstable_callable,
563
+ unstable_callable
400
564
  };
package/dist/index.js CHANGED
@@ -1,18 +1,27 @@
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-YDUDMOL6.js";
14
+ import "./chunk-MH46VMM4.js";
15
+ import "./chunk-LL2AFX7V.js";
16
+ import "./chunk-QEVM4BVL.js";
17
+ import "./chunk-AVYJQSLW.js";
13
18
  export {
14
19
  Agent,
15
20
  StreamingResponse,
21
+ callable,
22
+ createAddressBasedEmailResolver,
23
+ createCatchAllEmailResolver,
24
+ createHeaderBasedEmailResolver,
16
25
  getAgentByName,
17
26
  getCurrentAgent,
18
27
  routeAgentEmail,