agents 0.0.0-25aeaf2 → 0.0.0-25b261e

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