agents 0.20.0 → 0.21.0

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 (87) hide show
  1. package/README.md +50 -12
  2. package/dist/{agent-tool-types-Btk9ETS-.d.ts → agent-tool-types-CzGGB-20.d.ts} +403 -115
  3. package/dist/agent-tool-types.d.ts +1 -1
  4. package/dist/{agent-tools-UuScsJg3.d.ts → agent-tools-zR2d5uij.d.ts} +2 -2
  5. package/dist/agent-tools.d.ts +24 -8
  6. package/dist/agent-tools.js +14 -6
  7. package/dist/agent-tools.js.map +1 -1
  8. package/dist/browser/ai.d.ts +5 -3
  9. package/dist/browser/ai.js +86 -7
  10. package/dist/browser/ai.js.map +1 -1
  11. package/dist/browser/index.d.ts +1 -1
  12. package/dist/browser/index.js +1 -1
  13. package/dist/browser/tanstack-ai.js +13 -1
  14. package/dist/browser/tanstack-ai.js.map +1 -1
  15. package/dist/chat/index.d.ts +31 -2
  16. package/dist/chat/index.js +57 -2
  17. package/dist/chat/index.js.map +1 -1
  18. package/dist/chat/react.d.ts +5 -179
  19. package/dist/chat/react.js +18 -555
  20. package/dist/chat/react.js.map +1 -1
  21. package/dist/chat/transport.d.ts +10 -0
  22. package/dist/chat/transport.js +2 -0
  23. package/dist/chat-sdk/index.d.ts +1 -1
  24. package/dist/client.d.ts +1 -1
  25. package/dist/cloudflare-BduZwmYK.js +204 -0
  26. package/dist/cloudflare-BduZwmYK.js.map +1 -0
  27. package/dist/{connector-v2M1zlZp.d.ts → connector-CkQD4MK3.d.ts} +20 -3
  28. package/dist/{connector-KEJnl6e5.js → connector-CptFKzRh.js} +158 -40
  29. package/dist/connector-CptFKzRh.js.map +1 -0
  30. package/dist/{handler-stateless-8hQN_kC3.js → handler-stateless-CIkKPETH.js} +20 -69
  31. package/dist/handler-stateless-CIkKPETH.js.map +1 -0
  32. package/dist/index.d.ts +21 -11
  33. package/dist/index.js +31 -21
  34. package/dist/index.js.map +1 -1
  35. package/dist/mcp/client.d.ts +20 -20
  36. package/dist/mcp/index.d.ts +35 -35
  37. package/dist/mcp/index.js +32 -98
  38. package/dist/mcp/index.js.map +1 -1
  39. package/dist/mcp/server.js +1 -1
  40. package/dist/observability/ai/index.d.ts +10 -131
  41. package/dist/observability/ai/index.js +209 -444
  42. package/dist/observability/ai/index.js.map +1 -1
  43. package/dist/{wire-types-CU9rLoeS.js → protocol-Dqc2MQxo.js} +2 -46
  44. package/dist/protocol-Dqc2MQxo.js.map +1 -0
  45. package/dist/react.d.ts +1 -1
  46. package/dist/react.js +5 -8
  47. package/dist/react.js.map +1 -1
  48. package/dist/serializable.d.ts +1 -1
  49. package/dist/sub-routing.d.ts +18 -6
  50. package/dist/sub-routing.js +92 -2
  51. package/dist/sub-routing.js.map +1 -1
  52. package/dist/wire-types-CnMt6_HR.js +47 -0
  53. package/dist/wire-types-CnMt6_HR.js.map +1 -0
  54. package/dist/workflow-types.d.ts +25 -25
  55. package/dist/workflow-types.js.map +1 -1
  56. package/dist/workflows.d.ts +22 -22
  57. package/dist/ws-chat-transport-CIoOBbO7.js +561 -0
  58. package/dist/ws-chat-transport-CIoOBbO7.js.map +1 -0
  59. package/dist/ws-chat-transport-UNRIS2xl.d.ts +184 -0
  60. package/docs/adding-to-existing-project.md +4 -2
  61. package/docs/agent-class.md +1 -1
  62. package/docs/agent-tools.md +29 -0
  63. package/docs/browse-the-web.md +16 -1
  64. package/docs/chat-agents.md +3 -1
  65. package/docs/client-sdk.md +12 -8
  66. package/docs/configuration.md +7 -1
  67. package/docs/cross-domain-authentication.md +7 -35
  68. package/docs/email.md +2 -13
  69. package/docs/human-in-the-loop.md +15 -12
  70. package/docs/long-running-agents.md +11 -11
  71. package/docs/mcp-client.md +1 -1
  72. package/docs/mcp-servers.md +7 -1
  73. package/docs/mcp-transports.md +18 -12
  74. package/docs/migration-to-ai-sdk-v5.md +2 -2
  75. package/docs/migration-to-ai-sdk-v6.md +5 -1
  76. package/docs/observability.md +24 -67
  77. package/docs/routing.md +27 -0
  78. package/docs/sub-agents.md +47 -2
  79. package/docs/webhooks.md +109 -136
  80. package/docs/workflows.md +9 -1
  81. package/package.json +19 -10
  82. package/dist/cloudflare-BldFV0Pa.js +0 -117
  83. package/dist/cloudflare-BldFV0Pa.js.map +0 -1
  84. package/dist/connector-KEJnl6e5.js.map +0 -1
  85. package/dist/handler-stateless-8hQN_kC3.js.map +0 -1
  86. package/dist/wire-types-CU9rLoeS.js.map +0 -1
  87. package/dist/workflow-types-Baz_PO5v.d.ts +0 -280
package/dist/index.d.ts CHANGED
@@ -4,42 +4,48 @@ import {
4
4
  $ as RoutingRetryOptions,
5
5
  A as AgentGetOptions,
6
6
  B as EmailSendBinding,
7
+ Bn as parseSubAgentPath,
7
8
  C as DetachedRunAgentToolResult,
8
9
  D as AddRpcMcpServerOptions,
9
10
  E as AddMcpServerOptions,
10
11
  F as Connection,
11
- Ft as normalizeServerId,
12
+ Fn as buildAgentPath,
12
13
  G as FiberStatus,
13
14
  H as FiberInspection,
15
+ Hn as withInvocationScope,
14
16
  I as ConnectionContext,
17
+ In as buildAgentUrl,
15
18
  J as MCPServerMessage,
16
19
  K as ListFibersOptions,
17
20
  L as DEFAULT_AGENT_STATIC_OPTIONS,
18
21
  M as AgentOptions,
19
- Mt as MCP_SERVER_ID_MAX_LENGTH,
22
+ Mn as BuildAgentPathOptions,
20
23
  N as AgentStaticOptions,
24
+ Nn as SUB_PREFIX,
21
25
  O as Agent,
22
26
  P as CallableMetadata,
27
+ Pn as SubAgentPathMatch,
23
28
  Q as RPCResponse,
24
29
  R as DeleteFibersOptions,
25
30
  S as DetachedAgentToolConfig,
26
31
  T as RunAgentToolResult,
27
32
  U as FiberRecoveryContext,
33
+ Ut as MCPAITool,
28
34
  V as FiberContext,
35
+ Vn as routeSubAgentRequest,
29
36
  W as FiberRecoveryResult,
37
+ Wt as MCPAIToolSet,
30
38
  X as QueueItem,
31
39
  Y as MCPServersState,
32
40
  Z as RPCRequest,
33
41
  _ as AgentToolRunState,
34
42
  a as AgentToolEvent,
43
+ an as normalizeServerId,
35
44
  at as StartFiberResult,
36
45
  b as AgentToolTerminalStatus,
37
- bt as MCPAIToolSet,
38
46
  c as AgentToolFailure,
39
- cn as SubAgentPathMatch,
40
47
  ct as SubAgentClass,
41
48
  d as AgentToolMilestone,
42
- dn as routeSubAgentRequest,
43
49
  dt as callable,
44
50
  et as Schedule,
45
51
  f as AgentToolProgress,
@@ -51,13 +57,14 @@ import {
51
57
  i as AgentToolDisplayMetadata,
52
58
  it as StartFiberOptions,
53
59
  j as AgentNamespace,
60
+ jn as AgentPathStep,
54
61
  k as AgentContext,
55
62
  l as AgentToolInterruptedReason,
56
- ln as getSubAgentByName,
57
63
  lt as SubAgentStub,
58
64
  m as AgentToolRunInfo,
59
65
  mt as routeAgentEmail,
60
66
  n as AGENT_TOOL_PROGRESS_PART,
67
+ nn as MCP_SERVER_ID_MAX_LENGTH,
61
68
  nt as SendEmailOptions,
62
69
  o as AgentToolEventMessage,
63
70
  ot as StateUpdateMessage,
@@ -67,20 +74,18 @@ import {
67
74
  r as AgentToolChildAdapter,
68
75
  rt as SqlError,
69
76
  s as AgentToolEventState,
70
- sn as SUB_PREFIX,
71
77
  st as StreamingResponse,
72
78
  t as AGENT_TOOL_MILESTONE_PART,
73
79
  tt as ScheduleCriteria,
74
80
  u as AgentToolLifecycleResult,
75
- un as parseSubAgentPath,
76
81
  ut as WSMessage,
77
82
  v as AgentToolRunStatus,
78
83
  w as RunAgentToolOptions,
79
84
  x as ChatCapableAgentClass,
80
85
  y as AgentToolStoredChunk,
81
- yt as MCPAITool,
82
- z as EmailRoutingOptions
83
- } from "./agent-tool-types-Btk9ETS-.js";
86
+ z as EmailRoutingOptions,
87
+ zn as getSubAgentByName
88
+ } from "./agent-tool-types-CzGGB-20.js";
84
89
  import { n as camelCaseToKebabCase } from "./utils-CGtGDSgA.js";
85
90
  import {
86
91
  i as isDurableObjectStorageReset,
@@ -108,6 +113,7 @@ export {
108
113
  type AgentMcpOAuthProvider,
109
114
  AgentNamespace,
110
115
  AgentOptions,
116
+ type AgentPathStep,
111
117
  AgentStaticOptions,
112
118
  type AgentToolChildAdapter,
113
119
  type AgentToolDisplayMetadata,
@@ -128,6 +134,7 @@ export {
128
134
  type AgentToolStoredChunk,
129
135
  type AgentToolTerminalStatus,
130
136
  type AgentsOAuthProvider,
137
+ type BuildAgentPathOptions,
131
138
  CallableMetadata,
132
139
  type ChatCapableAgentClass,
133
140
  type Connection,
@@ -174,6 +181,9 @@ export {
174
181
  type TransportType,
175
182
  type WSMessage,
176
183
  __DO_NOT_USE_WILL_BREAK__agentContext,
184
+ withInvocationScope as __DO_NOT_USE_WILL_BREAK__withInvocationScope,
185
+ buildAgentPath,
186
+ buildAgentUrl,
177
187
  callable,
178
188
  camelCaseToKebabCase,
179
189
  createHeaderBasedEmailResolver,
package/dist/index.js CHANGED
@@ -4,12 +4,12 @@ import { MessageType } from "./types.js";
4
4
  import { camelCaseToKebabCase, isInternalJsStubProp } from "./utils.js";
5
5
  import { createHeaderBasedEmailResolver, signAgentHeaders } from "./email.js";
6
6
  import { i as _classPrivateFieldInitSpec, n as _classPrivateFieldSet2, t as _classPrivateFieldGet2 } from "./classPrivateFieldGet2-DZBYAB34.js";
7
- import { SUB_PREFIX, getSubAgentByName, parseSubAgentPath, routeSubAgentRequest } from "./sub-routing.js";
7
+ import { n as withInvocationScope, r as writeSpanAttributes, t as tracer } from "./cloudflare-BduZwmYK.js";
8
+ import { SUB_PREFIX, buildAgentPath, buildAgentUrl, getSubAgentByName, parseSubAgentPath, routeSubAgentRequest } from "./sub-routing.js";
8
9
  import { isDurableObjectCodeUpdateReset, isDurableObjectMemoryLimitReset, isDurableObjectStorageReset, isErrorRetryable, isPlatformTransientError, tryN, validateRetryOptions } from "./retries.js";
9
10
  import { a as MCPConnectionState, c as RPC_DO_PREFIX, i as normalizeServerId, l as DisposableStore, n as MCP_SERVER_ID_MAX_LENGTH, t as MCPClientManager } from "./client-zqKcsyFa.js";
10
11
  import { DurableObjectOAuthClientProvider } from "./mcp/do-oauth-client-provider.js";
11
12
  import { genericObservability } from "./observability/index.js";
12
- import { n as writeSpanAttributes, t as tracer } from "./cloudflare-BldFV0Pa.js";
13
13
  import { AsyncLocalStorage } from "node:async_hooks";
14
14
  import { parseCronExpression } from "cron-schedule";
15
15
  import { nanoid } from "nanoid";
@@ -18,6 +18,15 @@ import { RpcTarget, exports } from "cloudflare:workers";
18
18
  import { Server, getServerByName, routePartykitRequest } from "partyserver";
19
19
  //#region src/index.ts
20
20
  let _Symbol$dispose;
21
+ /**
22
+ * Enters an agent invocation: the context every handler reads, plus the span
23
+ * scope that stops invocation-bounded spans from outliving it. Scopes do not
24
+ * nest, so the outermost live entry point owns the boundary — pass
25
+ * `detached` for work that deliberately runs on past its caller.
26
+ */
27
+ function runInInvocation(store, body, options) {
28
+ return __DO_NOT_USE_WILL_BREAK__agentContext.run(store, () => withInvocationScope(body, options));
29
+ }
21
30
  function isClosedWebSocketSendError(error) {
22
31
  return error instanceof TypeError && error.message.includes("WebSocket send() after close");
23
32
  }
@@ -380,7 +389,7 @@ function withAgentContext(method) {
380
389
  return function(...args) {
381
390
  const { agent } = getCurrentAgent();
382
391
  if (agent === this) return method.apply(this, args);
383
- return __DO_NOT_USE_WILL_BREAK__agentContext.run({
392
+ return runInInvocation({
384
393
  agent: this,
385
394
  connection: void 0,
386
395
  request: void 0,
@@ -497,7 +506,7 @@ var Agent = class Agent extends Server {
497
506
  "cloudflare.agents.storage.system": "durable_object",
498
507
  "cloudflare.agents.storage.phase": storagePhase,
499
508
  ...attributes
500
- }, (span) => run((finishAttributes) => writeSpanAttributes(span, finishAttributes)));
509
+ }, (span) => run((finishAttributes) => writeSpanAttributes(span, finishAttributes)), __DO_NOT_USE_WILL_BREAK__agentContext.getStore()?.connection === void 0 ? void 0 : { boundToInvocation: true });
501
510
  }
502
511
  /**
503
512
  * Execute SQL queries against the Agent's database
@@ -824,7 +833,7 @@ var Agent = class Agent extends Server {
824
833
  }
825
834
  const _onRequest = this.onRequest.bind(this);
826
835
  this.onRequest = (request) => {
827
- return __DO_NOT_USE_WILL_BREAK__agentContext.run({
836
+ return runInInvocation({
828
837
  agent: this,
829
838
  connection: void 0,
830
839
  request,
@@ -839,7 +848,7 @@ var Agent = class Agent extends Server {
839
848
  this.onMessage = async (connection, message) => {
840
849
  if (await this._cf_forwardSubAgentWebSocketMessage(connection, message)) return;
841
850
  this._ensureConnectionWrapped(connection);
842
- return __DO_NOT_USE_WILL_BREAK__agentContext.run({
851
+ return runInInvocation({
843
852
  agent: this,
844
853
  connection,
845
854
  request: void 0,
@@ -932,7 +941,7 @@ var Agent = class Agent extends Server {
932
941
  const subAgentOuterUrl = ctx.request.headers.get(SUB_AGENT_OUTER_URL_HEADER);
933
942
  if (subAgentOuterUrl) this._unsafe_setConnectionFlag(connection, CF_SUB_AGENT_OUTER_URL_KEY, subAgentOuterUrl);
934
943
  if (await this._cf_forwardSubAgentWebSocketConnect(connection, ctx.request, { gate: false })) return;
935
- return __DO_NOT_USE_WILL_BREAK__agentContext.run({
944
+ return runInInvocation({
936
945
  agent: this,
937
946
  connection,
938
947
  request: ctx.request,
@@ -979,7 +988,7 @@ var Agent = class Agent extends Server {
979
988
  const _onClose = this.onClose.bind(this);
980
989
  this.onClose = async (connection, code, reason, wasClean) => {
981
990
  if (await this._cf_forwardSubAgentWebSocketClose(connection, code, reason, wasClean)) return;
982
- return __DO_NOT_USE_WILL_BREAK__agentContext.run({
991
+ return runInInvocation({
983
992
  agent: this,
984
993
  connection,
985
994
  request: void 0,
@@ -995,7 +1004,7 @@ var Agent = class Agent extends Server {
995
1004
  };
996
1005
  const _onStart = this.onStart.bind(this);
997
1006
  const startAgent = async (props, update) => {
998
- return __DO_NOT_USE_WILL_BREAK__agentContext.run({
1007
+ return runInInvocation({
999
1008
  agent: this,
1000
1009
  connection: void 0,
1001
1010
  request: void 0,
@@ -1098,7 +1107,7 @@ var Agent = class Agent extends Server {
1098
1107
  const { connection, request, email } = __DO_NOT_USE_WILL_BREAK__agentContext.getStore() || {};
1099
1108
  this.ctx.waitUntil((async () => {
1100
1109
  try {
1101
- await __DO_NOT_USE_WILL_BREAK__agentContext.run({
1110
+ await runInInvocation({
1102
1111
  agent: this,
1103
1112
  connection,
1104
1113
  request,
@@ -1106,7 +1115,7 @@ var Agent = class Agent extends Server {
1106
1115
  }, async () => {
1107
1116
  this._emit("state:update");
1108
1117
  await this._callStatePersistenceHook(nextState, source);
1109
- });
1118
+ }, { detached: true });
1110
1119
  } catch (e) {
1111
1120
  try {
1112
1121
  await this.onError(e);
@@ -1371,7 +1380,7 @@ var Agent = class Agent extends Server {
1371
1380
  forward: (rcptTo, headers) => payload._bridge.forward(rcptTo, headers),
1372
1381
  reply: (options) => payload._bridge.reply(options)
1373
1382
  };
1374
- return __DO_NOT_USE_WILL_BREAK__agentContext.run({
1383
+ return runInInvocation({
1375
1384
  agent: this,
1376
1385
  connection: void 0,
1377
1386
  request: void 0,
@@ -1620,7 +1629,7 @@ var Agent = class Agent extends Server {
1620
1629
  continue;
1621
1630
  }
1622
1631
  const { connection, request, email } = __DO_NOT_USE_WILL_BREAK__agentContext.getStore() || {};
1623
- await __DO_NOT_USE_WILL_BREAK__agentContext.run({
1632
+ await runInInvocation({
1624
1633
  agent: this,
1625
1634
  connection,
1626
1635
  request,
@@ -1655,7 +1664,7 @@ var Agent = class Agent extends Server {
1655
1664
  } finally {
1656
1665
  this.dequeue(row.id);
1657
1666
  }
1658
- });
1667
+ }, { detached: true });
1659
1668
  }
1660
1669
  }
1661
1670
  } finally {
@@ -3295,7 +3304,7 @@ var Agent = class Agent extends Server {
3295
3304
  console.error(`callback ${row.callback} not found`);
3296
3305
  return;
3297
3306
  }
3298
- await __DO_NOT_USE_WILL_BREAK__agentContext.run({
3307
+ await runInInvocation({
3299
3308
  agent: this,
3300
3309
  connection: void 0,
3301
3310
  request: void 0,
@@ -4091,7 +4100,7 @@ var Agent = class Agent extends Server {
4091
4100
  headers: forwardedHeaders
4092
4101
  };
4093
4102
  if (req.headers.get("Upgrade")?.toLowerCase() === "websocket") forwardedHeaders.set(SUB_AGENT_OUTER_URL_HEADER, req.url);
4094
- if (req.body && req.method !== "GET" && req.method !== "HEAD") forwardedInit.body = await req.arrayBuffer();
4103
+ if (req.body && req.method !== "GET" && req.method !== "HEAD") forwardedInit.body = req.body;
4095
4104
  const forwarded = new Request(rewritten, forwardedInit);
4096
4105
  return fetcher.fetch(forwarded);
4097
4106
  }
@@ -4766,12 +4775,12 @@ var Agent = class Agent extends Server {
4766
4775
  await invoke();
4767
4776
  return;
4768
4777
  }
4769
- await __DO_NOT_USE_WILL_BREAK__agentContext.run({
4778
+ await runInInvocation({
4770
4779
  agent: this,
4771
4780
  connection: void 0,
4772
4781
  request: void 0,
4773
4782
  email: void 0
4774
- }, invoke);
4783
+ }, invoke, { detached: true });
4775
4784
  }
4776
4785
  /**
4777
4786
  * Arm the self-scheduling detached reconcile backbone. Existing schedules are
@@ -5727,7 +5736,7 @@ var Agent = class Agent extends Server {
5727
5736
  }));
5728
5737
  this._recordSubAgent(className, name, identity);
5729
5738
  try {
5730
- await __DO_NOT_USE_WILL_BREAK__agentContext.run({
5739
+ await runInInvocation({
5731
5740
  agent: this,
5732
5741
  connection: void 0,
5733
5742
  request: void 0,
@@ -6046,7 +6055,8 @@ var Agent = class Agent extends Server {
6046
6055
  };
6047
6056
  const instance = await workflow.create({
6048
6057
  id: workflowId,
6049
- params: augmentedParams
6058
+ params: augmentedParams,
6059
+ retention: options?.retention
6050
6060
  });
6051
6061
  const id = nanoid();
6052
6062
  const metadataJson = options?.metadata ? JSON.stringify(options.metadata) : null;
@@ -7223,6 +7233,6 @@ var StreamingResponse = class {
7223
7233
  }
7224
7234
  };
7225
7235
  //#endregion
7226
- export { AGENT_TOOL_MILESTONE_PART, AGENT_TOOL_PROGRESS_PART, Agent, DEFAULT_AGENT_STATIC_OPTIONS, DurableObjectOAuthClientProvider, MCP_SERVER_ID_MAX_LENGTH, MessageType, SUB_PREFIX, SqlError, StreamingResponse, __DO_NOT_USE_WILL_BREAK__agentContext, callable, camelCaseToKebabCase, createHeaderBasedEmailResolver, getAgentByName, getCurrentAgent, getSubAgentByName, isDurableObjectCodeUpdateReset, isDurableObjectMemoryLimitReset, isDurableObjectStorageReset, isPlatformTransientError, normalizeServerId, parseSubAgentPath, routeAgentEmail, routeAgentRequest, routeSubAgentRequest, unstable_callable };
7236
+ export { AGENT_TOOL_MILESTONE_PART, AGENT_TOOL_PROGRESS_PART, Agent, DEFAULT_AGENT_STATIC_OPTIONS, DurableObjectOAuthClientProvider, MCP_SERVER_ID_MAX_LENGTH, MessageType, SUB_PREFIX, SqlError, StreamingResponse, __DO_NOT_USE_WILL_BREAK__agentContext, withInvocationScope as __DO_NOT_USE_WILL_BREAK__withInvocationScope, buildAgentPath, buildAgentUrl, callable, camelCaseToKebabCase, createHeaderBasedEmailResolver, getAgentByName, getCurrentAgent, getSubAgentByName, isDurableObjectCodeUpdateReset, isDurableObjectMemoryLimitReset, isDurableObjectStorageReset, isPlatformTransientError, normalizeServerId, parseSubAgentPath, routeAgentEmail, routeAgentRequest, routeSubAgentRequest, unstable_callable };
7227
7237
 
7228
7238
  //# sourceMappingURL=index.js.map