agents 0.0.0-2662748 → 0.0.0-2684ade

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 +12 -8
  3. package/dist/ai-chat-agent.js +166 -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-OYJXQRRH.js → chunk-DS7BJNPH.js} +167 -34
  16. package/dist/chunk-DS7BJNPH.js.map +1 -0
  17. package/dist/{chunk-P3RZJ72N.js → chunk-EGCWEPQL.js} +639 -132
  18. package/dist/chunk-EGCWEPQL.js.map +1 -0
  19. package/dist/{chunk-BZXOAZUX.js → chunk-PVQZBKN7.js} +5 -5
  20. package/dist/chunk-PVQZBKN7.js.map +1 -0
  21. package/dist/{chunk-VCSB47AK.js → chunk-QEVM4BVL.js} +10 -10
  22. package/dist/chunk-QEVM4BVL.js.map +1 -0
  23. package/dist/chunk-UJVEAURM.js +150 -0
  24. package/dist/chunk-UJVEAURM.js.map +1 -0
  25. package/dist/client-DgyzBU_8.d.ts +4601 -0
  26. package/dist/client.d.ts +2 -2
  27. package/dist/client.js +2 -1
  28. package/dist/index.d.ts +158 -21
  29. package/dist/index.js +11 -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.js +1 -1
  33. package/dist/mcp/index.d.ts +38 -10
  34. package/dist/mcp/index.js +233 -59
  35. package/dist/mcp/index.js.map +1 -1
  36. package/dist/observability/index.d.ts +46 -0
  37. package/dist/observability/index.js +11 -0
  38. package/dist/observability/index.js.map +1 -0
  39. package/dist/react.d.ts +12 -8
  40. package/dist/react.js +12 -10
  41. package/dist/react.js.map +1 -1
  42. package/dist/schedule.d.ts +6 -6
  43. package/dist/schedule.js +4 -4
  44. package/dist/schedule.js.map +1 -1
  45. package/package.json +83 -70
  46. package/src/index.ts +838 -169
  47. package/dist/chunk-BZXOAZUX.js.map +0 -1
  48. package/dist/chunk-OYJXQRRH.js.map +0 -1
  49. package/dist/chunk-P3RZJ72N.js.map +0 -1
  50. 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";
1
+ import { env } from "cloudflare:workers";
2
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
3
3
  import {
4
4
  ServerCapabilities,
5
5
  Tool,
6
6
  Prompt,
7
- Resource,
7
+ Resource
8
8
  } from "@modelcontextprotocol/sdk/types.js";
9
- import { MCPClientManager } from "./mcp/client.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-DgyzBU_8.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
  | {
@@ -73,6 +77,12 @@ declare function unstable_callable(
73
77
  target: (this: This, ...args: Args) => Return,
74
78
  context: ClassMethodDecoratorContext
75
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
+ };
76
86
  /**
77
87
  * Represents a scheduled task within an Agent
78
88
  * @template T Type of the payload data
@@ -112,7 +122,7 @@ type Schedule<T = string> = {
112
122
  * MCP Server state update message from server -> Client
113
123
  */
114
124
  type MCPServerMessage = {
115
- type: "cf_agent_mcp_servers";
125
+ type: MessageType.CF_AGENT_MCP_SERVERS;
116
126
  mcp: MCPServersState;
117
127
  };
118
128
  type MCPServersState = {
@@ -132,18 +142,19 @@ type MCPServer = {
132
142
  capabilities: ServerCapabilities | null;
133
143
  };
134
144
  declare function getCurrentAgent<
135
- T extends Agent<unknown, unknown> = Agent<unknown, unknown>,
145
+ T extends Agent<unknown, unknown> = Agent<unknown, unknown>
136
146
  >(): {
137
147
  agent: T | undefined;
138
148
  connection: Connection | undefined;
139
- request: Request<unknown, CfProperties<unknown>> | undefined;
149
+ request: Request | undefined;
150
+ email: AgentEmail | undefined;
140
151
  };
141
152
  /**
142
153
  * Base class for creating Agent implementations
143
154
  * @template Env Environment type containing bindings
144
155
  * @template State State type to store within the Agent
145
156
  */
146
- declare class Agent<Env, State = unknown> extends Server<Env> {
157
+ declare class Agent<Env = typeof env, State = unknown> extends Server<Env> {
147
158
  private _state;
148
159
  private _ParentClass;
149
160
  mcp: MCPClientManager;
@@ -163,6 +174,10 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
163
174
  /** Whether the Agent should hibernate when inactive */
164
175
  hibernate: boolean;
165
176
  };
177
+ /**
178
+ * The observability implementation to use for the Agent
179
+ */
180
+ observability?: Observability;
166
181
  /**
167
182
  * Execute SQL queries against the Agent's database
168
183
  * @template T Type of the returned rows
@@ -188,17 +203,75 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
188
203
  */
189
204
  onStateUpdate(state: State | undefined, source: Connection | "server"): void;
190
205
  /**
191
- * 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
192
208
  * @param email Email message to process
193
209
  */
194
- 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>;
195
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;
196
233
  onError(connection: Connection, error: unknown): void | Promise<void>;
197
234
  onError(error: unknown): void | Promise<void>;
198
235
  /**
199
236
  * Render content (not implemented in base class)
200
237
  */
201
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>[]>;
202
275
  /**
203
276
  * Schedule a task to be executed in the future
204
277
  * @template T Type of the payload data
@@ -283,7 +356,7 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
283
356
  authUrl: string | undefined;
284
357
  }>;
285
358
  _connectToMcpServerInternal(
286
- serverName: string,
359
+ _serverName: string,
287
360
  url: string,
288
361
  callbackUrl: string,
289
362
  options?: {
@@ -306,6 +379,7 @@ declare class Agent<Env, State = unknown> extends Server<Env> {
306
379
  ): Promise<{
307
380
  id: string;
308
381
  authUrl: string | undefined;
382
+ clientId: string | undefined;
309
383
  }>;
310
384
  removeMcpServer(id: string): Promise<void>;
311
385
  getMcpServers(): MCPServersState;
@@ -341,17 +415,72 @@ declare function routeAgentRequest<Env>(
341
415
  env: Env,
342
416
  options?: AgentOptions<Env>
343
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
+ };
344
451
  /**
345
452
  * 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
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
349
457
  */
350
458
  declare function routeAgentEmail<Env>(
351
459
  email: ForwardableEmailMessage,
352
460
  env: Env,
353
- options?: AgentOptions<Env>
461
+ options: EmailRoutingOptions<Env>
354
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
+ };
355
484
  /**
356
485
  * Get or create an Agent by name
357
486
  * @template Env Environment type containing bindings
@@ -392,20 +521,28 @@ declare class StreamingResponse {
392
521
  export {
393
522
  Agent,
394
523
  type AgentContext,
524
+ type AgentEmail,
395
525
  type AgentNamespace,
396
526
  type AgentOptions,
397
527
  type CallableMetadata,
528
+ type EmailResolver,
529
+ type EmailRoutingOptions,
530
+ type EmailSendOptions,
398
531
  type MCPServer,
399
532
  type MCPServerMessage,
400
533
  type MCPServersState,
534
+ type QueueItem,
401
535
  type RPCRequest,
402
536
  type RPCResponse,
403
537
  type Schedule,
404
538
  type StateUpdateMessage,
405
539
  StreamingResponse,
540
+ createAddressBasedEmailResolver,
541
+ createCatchAllEmailResolver,
542
+ createHeaderBasedEmailResolver,
406
543
  getAgentByName,
407
544
  getCurrentAgent,
408
545
  routeAgentEmail,
409
546
  routeAgentRequest,
410
- unstable_callable,
547
+ unstable_callable
411
548
  };
package/dist/index.js CHANGED
@@ -1,18 +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-P3RZJ72N.js";
10
- import "./chunk-OYJXQRRH.js";
11
- import "./chunk-BZXOAZUX.js";
12
- import "./chunk-VCSB47AK.js";
12
+ } from "./chunk-EGCWEPQL.js";
13
+ import "./chunk-DS7BJNPH.js";
14
+ import "./chunk-PVQZBKN7.js";
15
+ import "./chunk-QEVM4BVL.js";
16
+ import "./chunk-AVYJQSLW.js";
13
17
  export {
14
18
  Agent,
15
19
  StreamingResponse,
20
+ createAddressBasedEmailResolver,
21
+ createCatchAllEmailResolver,
22
+ createHeaderBasedEmailResolver,
16
23
  getAgentByName,
17
24
  getCurrentAgent,
18
25
  routeAgentEmail,