agents 0.0.0-cac66b8 → 0.0.0-ced3b22

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 (52) hide show
  1. package/README.md +157 -27
  2. package/dist/ai-chat-agent.d.ts +13 -9
  3. package/dist/ai-chat-agent.js +180 -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 +63 -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-IFXSRTKF.js → chunk-MH46VMM4.js} +182 -35
  18. package/dist/chunk-MH46VMM4.js.map +1 -0
  19. package/dist/{chunk-VCSB47AK.js → chunk-QEVM4BVL.js} +10 -10
  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 +2 -2
  27. package/dist/client.js +2 -1
  28. package/dist/index.d.ts +176 -23
  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 +67 -54
  35. package/dist/mcp/index.js +853 -606
  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 +12 -8
  41. package/dist/react.js +12 -10
  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/package.json +86 -70
  47. package/src/index.ts +894 -197
  48. package/dist/chunk-BZXOAZUX.js.map +0 -1
  49. package/dist/chunk-DVT5CDUP.js +0 -783
  50. package/dist/chunk-DVT5CDUP.js.map +0 -1
  51. package/dist/chunk-IFXSRTKF.js.map +0 -1
  52. 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,8 @@ import {
2
2
  AgentClient,
3
3
  agentFetch,
4
4
  camelCaseToKebabCase
5
- } from "./chunk-VCSB47AK.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,20 +1,24 @@
1
- import { Server, Connection, PartyServerOptions } from "partyserver";
2
- export { Connection, ConnectionContext, WSMessage } from "partyserver";
3
- import { MCPClientManager } from "./mcp/client.js";
1
+ import { env } from "cloudflare:workers";
2
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
4
3
  import {
5
4
  ServerCapabilities,
6
5
  Tool,
7
6
  Prompt,
8
- Resource,
7
+ Resource
9
8
  } from "@modelcontextprotocol/sdk/types.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 { M as MCPClientManager } from "./client-CvaJdLQA.js";
12
+ import { Observability } from "./observability/index.js";
13
+ import { MessageType } from "./ai-types.js";
11
14
  import "zod";
15
+ import "@modelcontextprotocol/sdk/shared/protocol.js";
16
+ import "ai";
12
17
  import "@modelcontextprotocol/sdk/client/sse.js";
18
+ import "@modelcontextprotocol/sdk/client/streamableHttp.js";
13
19
  import "./mcp/do-oauth-client-provider.js";
14
20
  import "@modelcontextprotocol/sdk/client/auth.js";
15
21
  import "@modelcontextprotocol/sdk/shared/auth.js";
16
- import "@modelcontextprotocol/sdk/shared/protocol.js";
17
- import "ai";
18
22
 
19
23
  /**
20
24
  * RPC request message from client
@@ -29,14 +33,14 @@ type RPCRequest = {
29
33
  * State update message from client
30
34
  */
31
35
  type StateUpdateMessage = {
32
- type: "cf_agent_state";
36
+ type: MessageType.CF_AGENT_STATE;
33
37
  state: unknown;
34
38
  };
35
39
  /**
36
40
  * RPC response message to client
37
41
  */
38
42
  type RPCResponse = {
39
- type: "rpc";
43
+ type: MessageType.RPC;
40
44
  id: string;
41
45
  } & (
42
46
  | {
@@ -67,12 +71,24 @@ type CallableMetadata = {
67
71
  * Decorator that marks a method as callable by clients
68
72
  * @param metadata Optional metadata about the callable method
69
73
  */
70
- declare function unstable_callable(
74
+ declare function callable(
71
75
  metadata?: CallableMetadata
72
76
  ): <This, Args extends unknown[], Return>(
73
77
  target: (this: This, ...args: Args) => Return,
74
78
  context: ClassMethodDecoratorContext
75
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
+ };
76
92
  /**
77
93
  * Represents a scheduled task within an Agent
78
94
  * @template T Type of the payload data
@@ -112,7 +128,7 @@ type Schedule<T = string> = {
112
128
  * MCP Server state update message from server -> Client
113
129
  */
114
130
  type MCPServerMessage = {
115
- type: "cf_agent_mcp_servers";
131
+ type: MessageType.CF_AGENT_MCP_SERVERS;
116
132
  mcp: MCPServersState;
117
133
  };
118
134
  type MCPServersState = {
@@ -132,18 +148,23 @@ type MCPServer = {
132
148
  capabilities: ServerCapabilities | null;
133
149
  };
134
150
  declare function getCurrentAgent<
135
- T extends Agent<unknown, unknown> = Agent<unknown, unknown>,
151
+ T extends Agent<unknown, unknown> = Agent<unknown, unknown>
136
152
  >(): {
137
153
  agent: T | undefined;
138
154
  connection: Connection | undefined;
139
- request: Request<unknown, CfProperties<unknown>> | undefined;
155
+ request: Request | undefined;
156
+ email: AgentEmail | undefined;
140
157
  };
141
158
  /**
142
159
  * Base class for creating Agent implementations
143
160
  * @template Env Environment type containing bindings
144
161
  * @template State State type to store within the Agent
145
162
  */
146
- 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> {
147
168
  private _state;
148
169
  private _ParentClass;
149
170
  mcp: MCPClientManager;
@@ -163,6 +184,10 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
163
184
  /** Whether the Agent should hibernate when inactive */
164
185
  hibernate: boolean;
165
186
  };
187
+ /**
188
+ * The observability implementation to use for the Agent
189
+ */
190
+ observability?: Observability;
166
191
  /**
167
192
  * Execute SQL queries against the Agent's database
168
193
  * @template T Type of the returned rows
@@ -188,17 +213,75 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
188
213
  */
189
214
  onStateUpdate(state: State | undefined, source: Connection | "server"): void;
190
215
  /**
191
- * 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
192
218
  * @param email Email message to process
193
219
  */
194
- 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>;
195
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;
196
243
  onError(connection: Connection, error: unknown): void | Promise<void>;
197
244
  onError(error: unknown): void | Promise<void>;
198
245
  /**
199
246
  * Render content (not implemented in base class)
200
247
  */
201
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>[]>;
202
285
  /**
203
286
  * Schedule a task to be executed in the future
204
287
  * @template T Type of the payload data
@@ -283,7 +366,7 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
283
366
  authUrl: string | undefined;
284
367
  }>;
285
368
  _connectToMcpServerInternal(
286
- serverName: string,
369
+ _serverName: string,
287
370
  url: string,
288
371
  callbackUrl: string,
289
372
  options?: {
@@ -306,6 +389,7 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
306
389
  ): Promise<{
307
390
  id: string;
308
391
  authUrl: string | undefined;
392
+ clientId: string | undefined;
309
393
  }>;
310
394
  removeMcpServer(id: string): Promise<void>;
311
395
  getMcpServers(): MCPServersState;
@@ -341,17 +425,72 @@ declare function routeAgentRequest<Env>(
341
425
  env: Env,
342
426
  options?: AgentOptions<Env>
343
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
+ };
344
461
  /**
345
462
  * 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
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
349
467
  */
350
468
  declare function routeAgentEmail<Env>(
351
469
  email: ForwardableEmailMessage,
352
470
  env: Env,
353
- options?: AgentOptions<Env>
471
+ options: EmailRoutingOptions<Env>
354
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
+ };
355
494
  /**
356
495
  * Get or create an Agent by name
357
496
  * @template Env Environment type containing bindings
@@ -361,12 +500,17 @@ declare function routeAgentEmail<Env>(
361
500
  * @param options Options for Agent creation
362
501
  * @returns Promise resolving to an Agent instance stub
363
502
  */
364
- 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
+ >(
365
508
  namespace: AgentNamespace<T>,
366
509
  name: string,
367
510
  options?: {
368
511
  jurisdiction?: DurableObjectJurisdiction;
369
512
  locationHint?: DurableObjectLocationHint;
513
+ props?: Props;
370
514
  }
371
515
  ): Promise<DurableObjectStub<T>>;
372
516
  /**
@@ -392,20 +536,29 @@ declare class StreamingResponse {
392
536
  export {
393
537
  Agent,
394
538
  type AgentContext,
539
+ type AgentEmail,
395
540
  type AgentNamespace,
396
541
  type AgentOptions,
397
542
  type CallableMetadata,
543
+ type EmailResolver,
544
+ type EmailRoutingOptions,
545
+ type EmailSendOptions,
398
546
  type MCPServer,
399
547
  type MCPServerMessage,
400
548
  type MCPServersState,
549
+ type QueueItem,
401
550
  type RPCRequest,
402
551
  type RPCResponse,
403
552
  type Schedule,
404
553
  type StateUpdateMessage,
405
554
  StreamingResponse,
555
+ callable,
556
+ createAddressBasedEmailResolver,
557
+ createCatchAllEmailResolver,
558
+ createHeaderBasedEmailResolver,
406
559
  getAgentByName,
407
560
  getCurrentAgent,
408
561
  routeAgentEmail,
409
562
  routeAgentRequest,
410
- unstable_callable,
563
+ unstable_callable
411
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-DVT5CDUP.js";
10
- import "./chunk-IFXSRTKF.js";
11
- import "./chunk-BZXOAZUX.js";
12
- import "./chunk-VCSB47AK.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,