agents 0.0.0-33ff003 → 0.0.0-352d62c

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 (50) hide show
  1. package/README.md +131 -25
  2. package/dist/ai-chat-agent.d.ts +10 -5
  3. package/dist/ai-chat-agent.js +75 -42
  4. package/dist/ai-chat-agent.js.map +1 -1
  5. package/dist/ai-react.d.ts +14 -9
  6. package/dist/ai-react.js +31 -31
  7. package/dist/ai-react.js.map +1 -1
  8. package/dist/ai-types.d.ts +27 -9
  9. package/dist/ai-types.js +6 -0
  10. package/dist/chunk-EEKLJYON.js +17 -0
  11. package/dist/chunk-EEKLJYON.js.map +1 -0
  12. package/dist/{chunk-Y67CHZBI.js → chunk-EM3J4KV7.js} +168 -34
  13. package/dist/chunk-EM3J4KV7.js.map +1 -0
  14. package/dist/chunk-ID62XSAS.js +1290 -0
  15. package/dist/chunk-ID62XSAS.js.map +1 -0
  16. package/dist/{chunk-BZXOAZUX.js → chunk-PVQZBKN7.js} +5 -5
  17. package/dist/chunk-PVQZBKN7.js.map +1 -0
  18. package/dist/{chunk-QSGN3REV.js → chunk-QEVM4BVL.js} +10 -17
  19. package/dist/chunk-QEVM4BVL.js.map +1 -0
  20. package/dist/client-DgyzBU_8.d.ts +4601 -0
  21. package/dist/client.d.ts +8 -2
  22. package/dist/client.js +2 -2
  23. package/dist/index.d.ts +192 -19
  24. package/dist/index.js +11 -5
  25. package/dist/mcp/client.d.ts +9 -781
  26. package/dist/mcp/client.js +1 -2
  27. package/dist/mcp/do-oauth-client-provider.js +1 -2
  28. package/dist/mcp/index.d.ts +38 -10
  29. package/dist/mcp/index.js +238 -62
  30. package/dist/mcp/index.js.map +1 -1
  31. package/dist/observability/index.d.ts +46 -0
  32. package/dist/observability/index.js +11 -0
  33. package/dist/react.d.ts +81 -11
  34. package/dist/react.js +20 -10
  35. package/dist/react.js.map +1 -1
  36. package/dist/schedule.d.ts +6 -6
  37. package/dist/schedule.js +4 -6
  38. package/dist/schedule.js.map +1 -1
  39. package/dist/serializable.d.ts +32 -0
  40. package/dist/serializable.js +1 -0
  41. package/dist/serializable.js.map +1 -0
  42. package/package.json +79 -71
  43. package/src/index.ts +865 -187
  44. package/dist/chunk-BZXOAZUX.js.map +0 -1
  45. package/dist/chunk-NOUFNU2O.js +0 -12
  46. package/dist/chunk-NPGUKHFR.js +0 -773
  47. package/dist/chunk-NPGUKHFR.js.map +0 -1
  48. package/dist/chunk-QSGN3REV.js.map +0 -1
  49. package/dist/chunk-Y67CHZBI.js.map +0 -1
  50. /package/dist/{chunk-NOUFNU2O.js.map → observability/index.js.map} +0 -0
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,8 +2,8 @@ import {
2
2
  AgentClient,
3
3
  agentFetch,
4
4
  camelCaseToKebabCase
5
- } from "./chunk-QSGN3REV.js";
6
- import "./chunk-NOUFNU2O.js";
5
+ } from "./chunk-QEVM4BVL.js";
6
+ import "./chunk-EEKLJYON.js";
7
7
  export {
8
8
  AgentClient,
9
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-DgyzBU_8.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
  | {
@@ -68,6 +77,12 @@ declare function unstable_callable(
68
77
  target: (this: This, ...args: Args) => Return,
69
78
  context: ClassMethodDecoratorContext
70
79
  ) => (this: This, ...args: Args) => Return;
80
+ type QueueItem<T = string> = {
81
+ id: string;
82
+ payload: T;
83
+ callback: keyof Agent<unknown>;
84
+ created_at: number;
85
+ };
71
86
  /**
72
87
  * Represents a scheduled task within an Agent
73
88
  * @template T Type of the payload data
@@ -107,7 +122,7 @@ type Schedule<T = string> = {
107
122
  * MCP Server state update message from server -> Client
108
123
  */
109
124
  type MCPServerMessage = {
110
- type: "cf_agent_mcp_servers";
125
+ type: MessageType.CF_AGENT_MCP_SERVERS;
111
126
  mcp: MCPServersState;
112
127
  };
113
128
  type MCPServersState = {
@@ -123,21 +138,23 @@ type MCPServer = {
123
138
  server_url: string;
124
139
  auth_url: string | null;
125
140
  state: "authenticating" | "connecting" | "ready" | "discovering" | "failed";
141
+ instructions: string | null;
142
+ capabilities: ServerCapabilities | null;
126
143
  };
127
144
  declare function getCurrentAgent<
128
- T extends Agent<unknown, unknown> = Agent<unknown, unknown>,
145
+ T extends Agent<unknown, unknown> = Agent<unknown, unknown>
129
146
  >(): {
130
147
  agent: T | undefined;
131
148
  connection: Connection | undefined;
132
- request: Request<unknown, CfProperties<unknown>> | undefined;
149
+ request: Request | undefined;
150
+ email: AgentEmail | undefined;
133
151
  };
134
152
  /**
135
153
  * Base class for creating Agent implementations
136
154
  * @template Env Environment type containing bindings
137
155
  * @template State State type to store within the Agent
138
156
  */
139
- declare class Agent<Env, State = unknown> extends Server<Env> {
140
- #private;
157
+ declare class Agent<Env = typeof env, State = unknown> extends Server<Env> {
141
158
  private _state;
142
159
  private _ParentClass;
143
160
  mcp: MCPClientManager;
@@ -157,6 +174,10 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
157
174
  /** Whether the Agent should hibernate when inactive */
158
175
  hibernate: boolean;
159
176
  };
177
+ /**
178
+ * The observability implementation to use for the Agent
179
+ */
180
+ observability?: Observability;
160
181
  /**
161
182
  * Execute SQL queries against the Agent's database
162
183
  * @template T Type of the returned rows
@@ -182,17 +203,75 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
182
203
  */
183
204
  onStateUpdate(state: State | undefined, source: Connection | "server"): void;
184
205
  /**
185
- * Called when the Agent receives an email
206
+ * Called when the Agent receives an email via routeAgentEmail()
207
+ * Override this method to handle incoming emails
186
208
  * @param email Email message to process
187
209
  */
188
- onEmail(email: ForwardableEmailMessage): Promise<void>;
210
+ _onEmail(email: AgentEmail): Promise<void>;
211
+ /**
212
+ * Reply to an email
213
+ * @param email The email to reply to
214
+ * @param options Options for the reply
215
+ * @returns void
216
+ */
217
+ replyToEmail(
218
+ email: AgentEmail,
219
+ options: {
220
+ fromName: string;
221
+ subject?: string | undefined;
222
+ body: string;
223
+ contentType?: string;
224
+ headers?: Record<string, string>;
225
+ }
226
+ ): Promise<void>;
189
227
  private _tryCatch;
228
+ /**
229
+ * Automatically wrap custom methods with agent context
230
+ * This ensures getCurrentAgent() works in all custom methods without decorators
231
+ */
232
+ private _autoWrapCustomMethods;
190
233
  onError(connection: Connection, error: unknown): void | Promise<void>;
191
234
  onError(error: unknown): void | Promise<void>;
192
235
  /**
193
236
  * Render content (not implemented in base class)
194
237
  */
195
238
  render(): void;
239
+ /**
240
+ * Queue a task to be executed in the future
241
+ * @param payload Payload to pass to the callback
242
+ * @param callback Name of the method to call
243
+ * @returns The ID of the queued task
244
+ */
245
+ queue<T = unknown>(callback: keyof this, payload: T): Promise<string>;
246
+ private _flushingQueue;
247
+ private _flushQueue;
248
+ /**
249
+ * Dequeue a task by ID
250
+ * @param id ID of the task to dequeue
251
+ */
252
+ dequeue(id: string): Promise<void>;
253
+ /**
254
+ * Dequeue all tasks
255
+ */
256
+ dequeueAll(): Promise<void>;
257
+ /**
258
+ * Dequeue all tasks by callback
259
+ * @param callback Name of the callback to dequeue
260
+ */
261
+ dequeueAllByCallback(callback: string): Promise<void>;
262
+ /**
263
+ * Get a queued task by ID
264
+ * @param id ID of the task to get
265
+ * @returns The task or undefined if not found
266
+ */
267
+ getQueue(id: string): Promise<QueueItem<string> | undefined>;
268
+ /**
269
+ * Get all queues by key and value
270
+ * @param key Key to filter by
271
+ * @param value Value to filter by
272
+ * @returns Array of matching QueueItem objects
273
+ */
274
+ getQueues(key: string, value: string): Promise<QueueItem<string>[]>;
196
275
  /**
197
276
  * Schedule a task to be executed in the future
198
277
  * @template T Type of the payload data
@@ -247,6 +326,10 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
247
326
  * Destroy the Agent, removing all state and scheduled tasks
248
327
  */
249
328
  destroy(): Promise<void>;
329
+ /**
330
+ * Get all methods marked as callable on this Agent
331
+ * @returns A map of method names to their metadata
332
+ */
250
333
  private _isCallable;
251
334
  /**
252
335
  * Connect to a new MCP Server
@@ -272,7 +355,34 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
272
355
  id: string;
273
356
  authUrl: string | undefined;
274
357
  }>;
358
+ _connectToMcpServerInternal(
359
+ _serverName: string,
360
+ url: string,
361
+ callbackUrl: string,
362
+ options?: {
363
+ client?: ConstructorParameters<typeof Client>[1];
364
+ /**
365
+ * We don't expose the normal set of transport options because:
366
+ * 1) we can't serialize things like the auth provider or a fetch function into the DB for reconnection purposes
367
+ * 2) We probably want these options to be agnostic to the transport type (SSE vs Streamable)
368
+ *
369
+ * This has the limitation that you can't override fetch, but I think headers should handle nearly all cases needed (i.e. non-standard bearer auth).
370
+ */
371
+ transport?: {
372
+ headers?: HeadersInit;
373
+ };
374
+ },
375
+ reconnect?: {
376
+ id: string;
377
+ oauthClientId?: string;
378
+ }
379
+ ): Promise<{
380
+ id: string;
381
+ authUrl: string | undefined;
382
+ clientId: string | undefined;
383
+ }>;
275
384
  removeMcpServer(id: string): Promise<void>;
385
+ getMcpServers(): MCPServersState;
276
386
  }
277
387
  /**
278
388
  * Namespace for creating Agent instances
@@ -305,17 +415,72 @@ declare function routeAgentRequest<Env>(
305
415
  env: Env,
306
416
  options?: AgentOptions<Env>
307
417
  ): Promise<Response | null>;
418
+ type EmailResolver<Env> = (
419
+ email: ForwardableEmailMessage,
420
+ env: Env
421
+ ) => Promise<{
422
+ agentName: string;
423
+ agentId: string;
424
+ } | null>;
425
+ /**
426
+ * Create a resolver that uses the message-id header to determine the agent to route the email to
427
+ * @returns A function that resolves the agent to route the email to
428
+ */
429
+ declare function createHeaderBasedEmailResolver<Env>(): EmailResolver<Env>;
430
+ /**
431
+ * Create a resolver that uses the email address to determine the agent to route the email to
432
+ * @param defaultAgentName The default agent name to use if the email address does not contain a sub-address
433
+ * @returns A function that resolves the agent to route the email to
434
+ */
435
+ declare function createAddressBasedEmailResolver<Env>(
436
+ defaultAgentName: string
437
+ ): EmailResolver<Env>;
438
+ /**
439
+ * Create a resolver that uses the agentName and agentId to determine the agent to route the email to
440
+ * @param agentName The name of the agent to route the email to
441
+ * @param agentId The id of the agent to route the email to
442
+ * @returns A function that resolves the agent to route the email to
443
+ */
444
+ declare function createCatchAllEmailResolver<Env>(
445
+ agentName: string,
446
+ agentId: string
447
+ ): EmailResolver<Env>;
448
+ type EmailRoutingOptions<Env> = AgentOptions<Env> & {
449
+ resolver: EmailResolver<Env>;
450
+ };
308
451
  /**
309
452
  * Route an email to the appropriate Agent
310
- * @param email Email message to route
311
- * @param env Environment containing Agent bindings
312
- * @param options Routing options
453
+ * @param email The email to route
454
+ * @param env The environment containing the Agent bindings
455
+ * @param options The options for routing the email
456
+ * @returns A promise that resolves when the email has been routed
313
457
  */
314
458
  declare function routeAgentEmail<Env>(
315
459
  email: ForwardableEmailMessage,
316
460
  env: Env,
317
- options?: AgentOptions<Env>
461
+ options: EmailRoutingOptions<Env>
318
462
  ): Promise<void>;
463
+ type AgentEmail = {
464
+ from: string;
465
+ to: string;
466
+ getRaw: () => Promise<Uint8Array>;
467
+ headers: Headers;
468
+ rawSize: number;
469
+ setReject: (reason: string) => void;
470
+ forward: (rcptTo: string, headers?: Headers) => Promise<void>;
471
+ reply: (options: { from: string; to: string; raw: string }) => Promise<void>;
472
+ };
473
+ type EmailSendOptions = {
474
+ to: string;
475
+ subject: string;
476
+ body: string;
477
+ contentType?: string;
478
+ headers?: Record<string, string>;
479
+ includeRoutingHeaders?: boolean;
480
+ agentName?: string;
481
+ agentId?: string;
482
+ domain?: string;
483
+ };
319
484
  /**
320
485
  * Get or create an Agent by name
321
486
  * @template Env Environment type containing bindings
@@ -356,20 +521,28 @@ declare class StreamingResponse {
356
521
  export {
357
522
  Agent,
358
523
  type AgentContext,
524
+ type AgentEmail,
359
525
  type AgentNamespace,
360
526
  type AgentOptions,
361
527
  type CallableMetadata,
528
+ type EmailResolver,
529
+ type EmailRoutingOptions,
530
+ type EmailSendOptions,
362
531
  type MCPServer,
363
532
  type MCPServerMessage,
364
533
  type MCPServersState,
534
+ type QueueItem,
365
535
  type RPCRequest,
366
536
  type RPCResponse,
367
537
  type Schedule,
368
538
  type StateUpdateMessage,
369
539
  StreamingResponse,
540
+ createAddressBasedEmailResolver,
541
+ createCatchAllEmailResolver,
542
+ createHeaderBasedEmailResolver,
370
543
  getAgentByName,
371
544
  getCurrentAgent,
372
545
  routeAgentEmail,
373
546
  routeAgentRequest,
374
- unstable_callable,
547
+ unstable_callable
375
548
  };
package/dist/index.js CHANGED
@@ -1,19 +1,25 @@
1
1
  import {
2
2
  Agent,
3
3
  StreamingResponse,
4
+ createAddressBasedEmailResolver,
5
+ createCatchAllEmailResolver,
6
+ createHeaderBasedEmailResolver,
4
7
  getAgentByName,
5
8
  getCurrentAgent,
6
9
  routeAgentEmail,
7
10
  routeAgentRequest,
8
11
  unstable_callable
9
- } from "./chunk-NPGUKHFR.js";
10
- import "./chunk-BZXOAZUX.js";
11
- import "./chunk-QSGN3REV.js";
12
- import "./chunk-Y67CHZBI.js";
13
- import "./chunk-NOUFNU2O.js";
12
+ } from "./chunk-ID62XSAS.js";
13
+ import "./chunk-EM3J4KV7.js";
14
+ import "./chunk-PVQZBKN7.js";
15
+ import "./chunk-QEVM4BVL.js";
16
+ import "./chunk-EEKLJYON.js";
14
17
  export {
15
18
  Agent,
16
19
  StreamingResponse,
20
+ createAddressBasedEmailResolver,
21
+ createCatchAllEmailResolver,
22
+ createHeaderBasedEmailResolver,
17
23
  getAgentByName,
18
24
  getCurrentAgent,
19
25
  routeAgentEmail,