agents 0.22.0 → 0.23.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 (186) hide show
  1. package/README.md +20 -1
  2. package/dist/{agent-routing-CnkaHb-v.d.ts → agent-routing-DE5zmCQ8.d.ts} +1038 -394
  3. package/dist/agent-routing.d.ts +1 -1
  4. package/dist/agent-tool-types.d.ts +26 -26
  5. package/dist/{agent-tools-C0XJqWzB.d.ts → agent-tools-DtXMTDGM.d.ts} +7 -7
  6. package/dist/agent-tools.d.ts +21 -21
  7. package/dist/browser/ai.js +4 -1
  8. package/dist/browser/ai.js.map +1 -1
  9. package/dist/browser/tanstack-ai.js.map +1 -1
  10. package/dist/callable-decorator-DP__HhBA.d.ts +72 -0
  11. package/dist/callable-decorator.d.ts +18 -0
  12. package/dist/callable-decorator.js +71 -0
  13. package/dist/callable-decorator.js.map +1 -0
  14. package/dist/capability-BjSKYpzg.js.map +1 -1
  15. package/dist/capability-runner-Be_-PLR1.d.ts +459 -0
  16. package/dist/channel-Bnm4S7T2.d.ts +491 -0
  17. package/dist/channels/ai-sdk.d.ts +46 -0
  18. package/dist/channels/ai-sdk.js +120 -0
  19. package/dist/channels/ai-sdk.js.map +1 -0
  20. package/dist/channels/email.d.ts +95 -0
  21. package/dist/channels/email.js +323 -0
  22. package/dist/channels/email.js.map +1 -0
  23. package/dist/channels/index.d.ts +233 -0
  24. package/dist/channels/index.js +608 -0
  25. package/dist/channels/index.js.map +1 -0
  26. package/dist/channels/slack.d.ts +140 -0
  27. package/dist/channels/slack.js +614 -0
  28. package/dist/channels/slack.js.map +1 -0
  29. package/dist/channels/tanstack-ai.d.ts +39 -0
  30. package/dist/channels/tanstack-ai.js +17 -0
  31. package/dist/channels/tanstack-ai.js.map +1 -0
  32. package/dist/channels/telegram.d.ts +106 -0
  33. package/dist/channels/telegram.js +427 -0
  34. package/dist/channels/telegram.js.map +1 -0
  35. package/dist/channels/voice.d.ts +45 -0
  36. package/dist/channels/voice.js +122 -0
  37. package/dist/channels/voice.js.map +1 -0
  38. package/dist/chat/index.d.ts +2325 -2019
  39. package/dist/chat/index.js +888 -518
  40. package/dist/chat/index.js.map +1 -1
  41. package/dist/chat-sdk/index.d.ts +7 -7
  42. package/dist/chat-sdk/index.js +1 -1
  43. package/dist/client.d.ts +1 -1
  44. package/dist/context/index.d.ts +216 -0
  45. package/dist/context/index.js +454 -0
  46. package/dist/context/index.js.map +1 -0
  47. package/dist/{current-agent-CuMErtly.d.ts → current-agent-Da_C9a3b.d.ts} +90 -107
  48. package/dist/current-agent-DhoDkSnH.js.map +1 -1
  49. package/dist/{diagnostics-CaBjfz4J.js → diagnostics-BzvaX2UT.js} +5 -1
  50. package/dist/diagnostics-BzvaX2UT.js.map +1 -0
  51. package/dist/diagnostics-C4jcz3VK.js +360 -0
  52. package/dist/diagnostics-C4jcz3VK.js.map +1 -0
  53. package/dist/index-BB0kqhIz.d.ts +101 -0
  54. package/dist/index-BVVgDSdq.d.ts +1 -0
  55. package/dist/index-XDkuQ7zm.d.ts +89 -0
  56. package/dist/{index-DcSAZKsB.d.ts → index-YSKgfgg9.d.ts} +3 -1
  57. package/dist/index.d.ts +91 -82
  58. package/dist/index.js +3 -2
  59. package/dist/ingress-BfetZbMO.js +83 -0
  60. package/dist/ingress-BfetZbMO.js.map +1 -0
  61. package/dist/internal-CYlgHl1l.js +59 -0
  62. package/dist/internal-CYlgHl1l.js.map +1 -0
  63. package/dist/internal_context.d.ts +1 -1
  64. package/dist/lifecycle/index.d.ts +35 -17
  65. package/dist/lifecycle/index.js +1 -1
  66. package/dist/lifecycle-CMRGjZdw.js +1299 -0
  67. package/dist/lifecycle-CMRGjZdw.js.map +1 -0
  68. package/dist/mcp/client/index.d.ts +20 -20
  69. package/dist/mcp/index.d.ts +35 -35
  70. package/dist/mcp/index.js +1 -1
  71. package/dist/observability/index.d.ts +1 -1
  72. package/dist/observability/index.js +1 -1
  73. package/dist/react.d.ts +4 -4
  74. package/dist/{retries-CAvxtG9d.d.ts → retries-D9Ds-1lz.d.ts} +17 -6
  75. package/dist/retries.d.ts +8 -6
  76. package/dist/retries.js +13 -1
  77. package/dist/retries.js.map +1 -1
  78. package/dist/routing/index.d.ts +137 -0
  79. package/dist/routing/index.js +244 -0
  80. package/dist/routing/index.js.map +1 -0
  81. package/dist/sanitize-D9TujEK8.js +79 -0
  82. package/dist/sanitize-D9TujEK8.js.map +1 -0
  83. package/dist/scheduler-DD9NdYbF.js +665 -0
  84. package/dist/scheduler-DD9NdYbF.js.map +1 -0
  85. package/dist/{scheduler-DQoTGoAW.d.ts → scheduler-Dwh85ZGl.d.ts} +21 -22
  86. package/dist/schedules/index.d.ts +1 -1
  87. package/dist/schedules/index.js +1 -1
  88. package/dist/sentence-chunker-BAidJ4DA.d.ts +68 -0
  89. package/dist/serializable.d.ts +1 -1
  90. package/dist/sessions/index.d.ts +441 -0
  91. package/dist/sessions/index.js +2063 -0
  92. package/dist/sessions/index.js.map +1 -0
  93. package/dist/skills/index.d.ts +99 -0
  94. package/dist/skills/index.js +254 -5
  95. package/dist/skills/index.js.map +1 -1
  96. package/dist/{src-5W6JNKVb.js → src-DlSHshb2.js} +1460 -1110
  97. package/dist/src-DlSHshb2.js.map +1 -0
  98. package/dist/streams/index.d.ts +120 -0
  99. package/dist/streams/index.js +107 -0
  100. package/dist/streams/index.js.map +1 -0
  101. package/dist/streams-D6tJ0NN9.d.ts +370 -0
  102. package/dist/streams-DZKgAj9b.js +709 -0
  103. package/dist/streams-DZKgAj9b.js.map +1 -0
  104. package/dist/sub-routing.d.ts +12 -12
  105. package/dist/surface-bZZJqBka.js +17 -0
  106. package/dist/surface-bZZJqBka.js.map +1 -0
  107. package/dist/tasks/index.d.ts +64 -0
  108. package/dist/tasks/index.js +2 -0
  109. package/dist/tasks-BRJ5zgya.d.ts +517 -0
  110. package/dist/tasks-ylZgBjhj.js +1656 -0
  111. package/dist/tasks-ylZgBjhj.js.map +1 -0
  112. package/dist/text-segment-joiner-BtAFQSA_.js +57 -0
  113. package/dist/text-segment-joiner-BtAFQSA_.js.map +1 -0
  114. package/dist/text-stream-CpdiKrJB.js +272 -0
  115. package/dist/text-stream-CpdiKrJB.js.map +1 -0
  116. package/dist/tokens-nHAKcN6M.js +52 -0
  117. package/dist/tokens-nHAKcN6M.js.map +1 -0
  118. package/dist/tool-schema-CBjGPrsQ.js +31 -0
  119. package/dist/tool-schema-CBjGPrsQ.js.map +1 -0
  120. package/dist/types-B7LojTe4.d.ts +202 -0
  121. package/dist/types-_Faxb570.d.ts +439 -0
  122. package/dist/voice/client.d.ts +226 -0
  123. package/dist/voice/client.js +932 -0
  124. package/dist/voice/client.js.map +1 -0
  125. package/dist/voice/errors.d.ts +43 -0
  126. package/dist/voice/errors.js +41 -0
  127. package/dist/voice/errors.js.map +1 -0
  128. package/dist/voice/index.d.ts +271 -0
  129. package/dist/voice/index.js +1812 -0
  130. package/dist/voice/index.js.map +1 -0
  131. package/dist/voice/react.d.ts +167 -0
  132. package/dist/voice/react.js +234 -0
  133. package/dist/voice/react.js.map +1 -0
  134. package/dist/voice/sfu.d.ts +71 -0
  135. package/dist/voice/sfu.js +157 -0
  136. package/dist/voice/sfu.js.map +1 -0
  137. package/dist/voice/text.d.ts +6 -0
  138. package/dist/voice/text.js +2 -0
  139. package/dist/voice/types.d.ts +58 -0
  140. package/dist/voice/types.js +18 -0
  141. package/dist/voice/types.js.map +1 -0
  142. package/dist/voice/workers-ai.d.ts +136 -0
  143. package/dist/voice/workers-ai.js +568 -0
  144. package/dist/voice/workers-ai.js.map +1 -0
  145. package/dist/websockets/index.d.ts +192 -0
  146. package/dist/websockets/index.js +2 -0
  147. package/dist/websockets-DUfRHPRq.js +502 -0
  148. package/dist/websockets-DUfRHPRq.js.map +1 -0
  149. package/dist/workflow-types.d.ts +25 -25
  150. package/dist/workflows.d.ts +21 -21
  151. package/dist/workflows.js +1 -1
  152. package/docs/agent-class.md +2 -2
  153. package/docs/agent-tools.md +2 -1
  154. package/docs/channels.md +323 -0
  155. package/docs/chat-agents.md +6 -13
  156. package/docs/context.md +131 -0
  157. package/docs/index.md +15 -12
  158. package/docs/lifecycle.md +102 -55
  159. package/docs/long-running-agents.md +2 -2
  160. package/docs/mcp-servers.md +5 -1
  161. package/docs/resumable-streaming.md +1 -1
  162. package/docs/routing.md +105 -0
  163. package/docs/sessions.md +237 -871
  164. package/docs/streams.md +213 -0
  165. package/docs/sub-agents.md +184 -124
  166. package/docs/tasks.md +246 -0
  167. package/docs/voice.md +745 -0
  168. package/package.json +115 -13
  169. package/dist/capability-runner-CvHGZqUu.d.ts +0 -150
  170. package/dist/compaction-helpers-iiKMr2TQ.js +0 -340
  171. package/dist/compaction-helpers-iiKMr2TQ.js.map +0 -1
  172. package/dist/compaction-helpers-wUz6M3us.d.ts +0 -621
  173. package/dist/diagnostics-CaBjfz4J.js.map +0 -1
  174. package/dist/durable-object-lifecycle-D6nNQJJd.js +0 -862
  175. package/dist/durable-object-lifecycle-D6nNQJJd.js.map +0 -1
  176. package/dist/experimental/memory/session/index.d.ts +0 -671
  177. package/dist/experimental/memory/session/index.js +0 -2379
  178. package/dist/experimental/memory/session/index.js.map +0 -1
  179. package/dist/experimental/memory/utils/index.d.ts +0 -96
  180. package/dist/experimental/memory/utils/index.js +0 -79
  181. package/dist/experimental/memory/utils/index.js.map +0 -1
  182. package/dist/scheduler-CR9RHGos.js +0 -857
  183. package/dist/scheduler-CR9RHGos.js.map +0 -1
  184. package/dist/src-5W6JNKVb.js.map +0 -1
  185. package/dist/tool-output-truncation-CNnnGZQ3.js +0 -98
  186. package/dist/tool-output-truncation-CNnnGZQ3.js.map +0 -1
@@ -1,80 +1,42 @@
1
1
  import {
2
+ _ as LifecycleJobOutcome,
3
+ b as Connection,
4
+ g as LifecycleJobContext,
2
5
  i as DurableObjectCapability,
3
- l as LifecycleRouteAddress,
4
- t as AlarmContribution
5
- } from "./capability-runner-CvHGZqUu.js";
6
+ o as MemoryLimitContext,
7
+ u as LifecycleRouteAddress,
8
+ w as WSMessage,
9
+ y as LifecycleJobs
10
+ } from "./capability-runner-Be_-PLR1.js";
6
11
  import { AsyncLocalStorage } from "node:async_hooks";
7
12
  import { DurableObject } from "cloudflare:workers";
8
13
 
9
- //#region src/lifecycle/types.d.ts
10
- type ImmutablePrimitive = undefined | null | boolean | string | number;
11
- type Immutable<T> = T extends ImmutablePrimitive
12
- ? T
13
- : T extends Array<infer U>
14
- ? ImmutableArray<U>
15
- : T extends Map<infer K, infer V>
16
- ? ImmutableMap<K, V>
17
- : T extends Set<infer M>
18
- ? ImmutableSet<M>
19
- : ImmutableObject<T>;
20
- type ImmutableArray<T> = ReadonlyArray<Immutable<T>>;
21
- type ImmutableMap<K, V> = ReadonlyMap<Immutable<K>, Immutable<V>>;
22
- type ImmutableSet<T> = ReadonlySet<Immutable<T>>;
23
- type ImmutableObject<T> = { readonly [K in keyof T]: Immutable<T[K]> };
24
- /** Immutable state persisted in a hibernating WebSocket attachment. */
25
- type ConnectionState<T> = ImmutableObject<T> | null;
26
- /** Functional update applied to a connection's current state. */
27
- type ConnectionSetStateFn<T> = (prevState: ConnectionState<T>) => T;
28
- /** Context supplied when a lifecycle accepts a WebSocket connection. */
29
- type ConnectionContext = {
30
- /** Original WebSocket upgrade request. */ request: Request;
31
- };
32
- /** A WebSocket managed by a Durable Object lifecycle. */
33
- type Connection<TState = unknown> = WebSocket & {
34
- /** Connection identifier */ id: string;
35
- /**
36
- * The URL of the original WebSocket upgrade request.
37
- * Persisted in the WebSocket attachment so it survives hibernation.
38
- */
39
- uri: string | null;
40
- /**
41
- * Arbitrary state associated with this connection.
42
- * Read-only — use {@link Connection.setState} to update.
43
- *
44
- * This property is configurable, meaning it can be redefined via
45
- * `Object.defineProperty` by downstream consumers (e.g. the Cloudflare
46
- * Agents SDK) to namespace or wrap internal state storage.
47
- */
48
- state: ConnectionState<TState>;
49
- /**
50
- * Update the state associated with this connection.
51
- *
52
- * Accepts either a new state value or an updater function that receives
53
- * the previous state and returns the next state.
54
- *
55
- * This property is configurable, meaning it can be redefined via
56
- * `Object.defineProperty` by downstream consumers that provide their own
57
- * state projection.
58
- */
59
- setState(
60
- state: TState | ConnectionSetStateFn<TState> | null
61
- ): ConnectionState<TState>;
62
- /**
63
- * Tags returned by the owning Durable Object's `getConnectionTags` callback.
64
- * Always includes the connection id as the first tag.
65
- */
66
- tags: readonly string[];
67
- };
68
- //#endregion
69
14
  //#region src/lifecycle/durable-object-lifecycle.d.ts
70
- /** Payload delivered to a lifecycle-managed WebSocket callback. */
71
- type WSMessage = ArrayBuffer | ArrayBufferView | string;
72
15
  /** Internal envelope transported between routed Lifecycle instances. */
73
16
  type LifecycleRouteEnvelope = {
74
17
  readonly capability: string;
75
18
  readonly source: LifecycleRouteAddress | undefined;
76
19
  readonly payload: unknown;
77
20
  };
21
+ /** Configuration accepted when constructing a {@link Lifecycle}. */
22
+ type LifecycleOptions = {
23
+ /**
24
+ * Consecutive alarm invocations that may end in a Durable Object
25
+ * memory-limit reset before the circuit breaker (#1825) seals recovery
26
+ * work instead of backing it off. Default: 3.
27
+ */
28
+ readonly maxAlarmMemoryLimitStrikes?: number;
29
+ };
30
+ /** Placement of a capability in the dispatch order. */
31
+ type LifecycleUseOptions = {
32
+ /**
33
+ * Dispatch after every non-fallback capability, whenever it was
34
+ * installed. For a host's catch-all, such as a WebSockets capability
35
+ * that claims every upgrade, so middleware installed later still runs
36
+ * first.
37
+ */
38
+ readonly fallback?: boolean;
39
+ };
78
40
  /**
79
41
  * Installs and coordinates the runtime lifecycle for a Durable Object.
80
42
  *
@@ -98,13 +60,17 @@ declare class Lifecycle<
98
60
  static install<
99
61
  Env extends object,
100
62
  Props extends Record<string, unknown> = Record<string, unknown>
101
- >(host: DurableObject<Env>): Lifecycle<Env, Props>;
63
+ >(
64
+ host: DurableObject<Env>,
65
+ options?: LifecycleOptions
66
+ ): Lifecycle<Env, Props>;
102
67
  /**
103
68
  * Bind a lifecycle to a Durable Object instance without mutating its handlers.
104
69
  *
105
70
  * @param host - The Durable Object whose runtime lifecycle this object owns.
71
+ * @param options - Policy configuration for this lifecycle.
106
72
  */
107
- constructor(host: DurableObject<Env>);
73
+ constructor(host: DurableObject<Env>, options?: LifecycleOptions);
108
74
  /**
109
75
  * Install platform fetch, alarm, and hibernating WebSocket handlers.
110
76
  *
@@ -116,10 +82,17 @@ declare class Lifecycle<
116
82
  /**
117
83
  * Add a reusable capability before this lifecycle starts.
118
84
  *
119
- * @param capability - The capability to add in dispatch order.
85
+ * Capabilities dispatch in registration order, except that fallbacks
86
+ * always come after non-fallbacks.
87
+ *
88
+ * @param capability - The capability to add.
89
+ * @param options - Dispatch placement.
120
90
  * @returns This lifecycle.
121
91
  */
122
- use(capability: DurableObjectCapability<Props>): this;
92
+ use(
93
+ capability: DurableObjectCapability<Props>,
94
+ options?: LifecycleUseOptions
95
+ ): this;
123
96
  /** @internal Deliver a generic capability envelope to this Lifecycle. */
124
97
  route(envelope: LifecycleRouteEnvelope): Promise<unknown>;
125
98
  /**
@@ -135,11 +108,14 @@ declare class Lifecycle<
135
108
  ): T[];
136
109
  /**
137
110
  * Handle an incoming request for the owning Durable Object.
111
+ *
112
+ * Non-upgrade requests run through the capability middleware chain first,
113
+ * then fall through to the host's `onRequest`.
138
114
  */
139
115
  fetch(request: Request): Promise<Response>;
140
116
  /** @internal Dispatch a hibernating WebSocket message. */
141
117
  webSocketMessage(ws: WebSocket, message: WSMessage): Promise<void>;
142
- /** @internal Dispatch and reciprocate a hibernating WebSocket close. */
118
+ /** @internal Dispatch a hibernating WebSocket close. */
143
119
  webSocketClose(
144
120
  ws: WebSocket,
145
121
  code: number,
@@ -165,27 +141,39 @@ declare class Lifecycle<
165
141
  * name writes.
166
142
  */
167
143
  get name(): string;
168
- /** Send a message to all connected clients, except connection ids listed in `without` */
169
- broadcast(
170
- msg: string | ArrayBuffer | ArrayBufferView,
171
- without?: string[] | undefined
172
- ): void;
173
- /** Get a connection by connection id */
174
- getConnection<TState = unknown>(id: string): Connection<TState> | undefined;
175
- /** Get all managed connections, optionally filtered by tag. */
176
- getConnections<TState = unknown>(tag?: string): Iterable<Connection<TState>>;
177
144
  /**
178
- * Recompute the physical Durable Object alarm from every capability.
145
+ * The host's scoped access to the Lifecycle work queue. Items pushed here
146
+ * are dispatched to the host's `onJob` inside the host invocation
147
+ * boundary.
148
+ */
149
+ get jobs(): LifecycleJobs;
150
+ /**
151
+ * Recompute the physical Durable Object alarm from job-queue state.
179
152
  *
180
153
  * Concurrent requests are serialized so a later durable-state change cannot
181
- * be overwritten by an earlier alarm calculation.
154
+ * be overwritten by an earlier alarm calculation. Queue mutations call this
155
+ * automatically; it stays public for composition roots and tests.
182
156
  */
183
157
  rearmAlarm(): Promise<void>;
158
+ /**
159
+ * Keep work a job handed off at a bounded return inside the current
160
+ * alarm's memory-limit breaker domain (#1825). Hosts call this where a
161
+ * queue-driven callback detaches long work and returns.
162
+ *
163
+ * @returns True when called during an alarm invocation; false otherwise.
164
+ */
165
+ trackAlarmWork(work: Promise<unknown>): boolean;
184
166
  /** Dispose installed capabilities in reverse registration order. */
185
167
  dispose(): Promise<void>;
186
168
  /** Permanently disable and clear alarms during explicit object teardown. */
187
169
  disableAlarms(): Promise<void>;
188
- /** Dispatch lifecycle and host alarm callbacks after startup. */
170
+ /**
171
+ * Run one alarm invocation. The job driver owns the event loop — deadman
172
+ * pre-arm, due-job dispatch with retry and deferral policy, the alarm
173
+ * memory-limit circuit breaker (#1825) — and re-arms the physical alarm
174
+ * from queue state. The host's `onAlarm()` runs after due jobs, inside
175
+ * the host invocation boundary.
176
+ */
189
177
  alarm(): Promise<void>;
190
178
  }
191
179
  //#endregion
@@ -206,23 +194,21 @@ interface LifecycleObject<
206
194
  onStart?(props?: Props): void | Promise<void>;
207
195
  onRequest?(request: Request): Response | Promise<Response>;
208
196
  onAlarm?(): void | Promise<void>;
209
- getNextAlarm?(): AlarmContribution | Promise<AlarmContribution>;
210
- onConnect?(
211
- connection: Connection,
212
- context: ConnectionContext
213
- ): void | Promise<void>;
214
- onMessage?(connection: Connection, message: WSMessage): void | Promise<void>;
215
- onClose?(
216
- connection: Connection,
217
- code: number,
218
- reason: string,
219
- wasClean: boolean
220
- ): void | Promise<void>;
221
- onError?(connection: Connection, error: unknown): void | Promise<void>;
222
- getConnectionTags?(
223
- connection: Connection,
224
- context: ConnectionContext
225
- ): string[] | Promise<string[]>;
197
+ onJob?(
198
+ context: LifecycleJobContext
199
+ ): LifecycleJobOutcome | void | Promise<LifecycleJobOutcome | void>;
200
+ /**
201
+ * Host domain policy applied when the alarm memory-limit circuit breaker
202
+ * records a strike (#1825), after every capability's `onMemoryLimit` hook.
203
+ *
204
+ * Lifecycle dispatches host hooks structurally through its internal host
205
+ * cast, so TS visibility is the host's to choose: a framework host whose
206
+ * implementation is internal machinery declares the hook `protected` (the
207
+ * pattern — see `AIChatAgent`/`Think`) and keeps the real work in a
208
+ * `private _cf_`-prefixed method, without falling out of this contract at
209
+ * runtime.
210
+ */
211
+ onAlarmMemoryLimit?(context: MemoryLimitContext): void | Promise<void>;
226
212
  }
227
213
  /** Values associated with the currently executing Lifecycle host. */
228
214
  type AgentContextStore = {
@@ -268,16 +254,13 @@ declare function getCurrentAgent<
268
254
  //#endregion
269
255
  export {
270
256
  getCurrentAgent as a,
271
- WSMessage as c,
272
- ConnectionSetStateFn as d,
273
- ConnectionState as f,
257
+ LifecycleRouteEnvelope as c,
274
258
  __DO_NOT_USE_WILL_BREAK__agentContext as i,
275
- Connection as l,
259
+ LifecycleUseOptions as l,
276
260
  CurrentAgentContext as n,
277
261
  Lifecycle as o,
278
262
  LifecycleObject as r,
279
- LifecycleRouteEnvelope as s,
280
- AgentContextStore as t,
281
- ConnectionContext as u
263
+ LifecycleOptions as s,
264
+ AgentContextStore as t
282
265
  };
283
- //# sourceMappingURL=current-agent-CuMErtly.d.ts.map
266
+ //# sourceMappingURL=current-agent-Da_C9a3b.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"current-agent-DhoDkSnH.js","names":[],"sources":["../src/lifecycle/current-agent.ts"],"sourcesContent":["import { AsyncLocalStorage } from \"node:async_hooks\";\nimport type { DurableObject } from \"cloudflare:workers\";\n\nimport type { AlarmContribution } from \"./capability-runner\";\nimport type { Lifecycle, WSMessage } from \"./durable-object-lifecycle\";\nimport type { Connection, ConnectionContext } from \"./types\";\n\n/**\n * A Durable Object that has installed the Agents SDK Lifecycle.\n *\n * Pass a more specific Lifecycle Object type to {@link getCurrentAgent} when\n * shared host code needs APIs implemented by a particular object.\n *\n * @experimental The API surface may change before stabilizing.\n */\nexport interface LifecycleObject<\n Env extends object = Cloudflare.Env,\n Props extends Record<string, unknown> = Record<string, unknown>\n> extends DurableObject<Env> {\n readonly lifecycle: Lifecycle<Env, Props>;\n onStart?(props?: Props): void | Promise<void>;\n onRequest?(request: Request): Response | Promise<Response>;\n onAlarm?(): void | Promise<void>;\n getNextAlarm?(): AlarmContribution | Promise<AlarmContribution>;\n onConnect?(\n connection: Connection,\n context: ConnectionContext\n ): void | Promise<void>;\n onMessage?(connection: Connection, message: WSMessage): void | Promise<void>;\n onClose?(\n connection: Connection,\n code: number,\n reason: string,\n wasClean: boolean\n ): void | Promise<void>;\n onError?(connection: Connection, error: unknown): void | Promise<void>;\n getConnectionTags?(\n connection: Connection,\n context: ConnectionContext\n ): string[] | Promise<string[]>;\n}\n\n/** Values associated with the currently executing Lifecycle host. */\nexport type AgentContextStore = {\n /** Lifecycle host selected for this invocation. */\n agent: unknown;\n /** WebSocket connection selected for this invocation, when applicable. */\n connection: Connection | undefined;\n /** HTTP request selected for this invocation, when applicable. */\n request: Request | undefined;\n /** Extension-owned value selected for this invocation, when applicable. */\n email: unknown;\n};\n\n/** Values returned by {@link getCurrentAgent}. */\nexport type CurrentAgentContext<\n Host extends DurableObject = LifecycleObject,\n Email = unknown\n> = {\n agent: Host | undefined;\n connection: Connection | undefined;\n request: Request | undefined;\n email: Email | undefined;\n};\n\n/**\n * Shared invocation context for Lifecycle, Agent, AIChatAgent, and Think.\n *\n * @internal Importing or relying on this symbol will break your code in a\n * future release. Use {@link getCurrentAgent} to read the public context.\n */\nexport const __DO_NOT_USE_WILL_BREAK__agentContext =\n new AsyncLocalStorage<AgentContextStore>();\n\n/**\n * Return the current Agent or Lifecycle Object and invocation-specific values.\n *\n * Lifecycle host startup and alarm hooks receive the current object with no\n * request. Request hooks additionally receive the request being handled.\n * Lifecycle-managed WebSocket hooks receive their connection and, during\n * connect, its upgrade request. Agent extensions may also establish context\n * for email, chat turns, callable methods, and detached work. Capability hooks\n * do not run in this ambient context.\n */\nexport function getCurrentAgent<\n Host extends DurableObject = LifecycleObject\n>(): CurrentAgentContext<Host> {\n const store = __DO_NOT_USE_WILL_BREAK__agentContext.getStore();\n if (!store) {\n return {\n agent: undefined,\n connection: undefined,\n request: undefined,\n email: undefined\n };\n }\n\n return {\n agent: store.agent as Host,\n connection: store.connection,\n request: store.request,\n email: store.email\n };\n}\n\ntype LifecycleHostContext<\n Env extends object,\n Props extends Record<string, unknown>\n> = {\n readonly host: LifecycleObject<Env, Props>;\n readonly connection?: Connection;\n readonly request?: Request;\n};\n\n/** Run one host hook in the context of its Lifecycle Object. */\nexport function runInLifecycleHostContext<\n Env extends object,\n Props extends Record<string, unknown>,\n T\n>(context: LifecycleHostContext<Env, Props>, operation: () => T): T {\n return __DO_NOT_USE_WILL_BREAK__agentContext.run(\n {\n agent: context.host,\n connection: context.connection,\n request: context.request,\n email: undefined\n },\n operation\n );\n}\n\n/** Run a capability hook without inheriting a current Agent or host context. */\nexport function runWithoutCurrentAgent<T>(operation: () => T): T {\n return __DO_NOT_USE_WILL_BREAK__agentContext.exit(operation);\n}\n"],"mappings":";;;;;;;;AAuEA,MAAa,wCACX,IAAI,kBAAqC;;;;;;;;;;;AAY3C,SAAgB,kBAEe;CAC7B,MAAM,QAAQ,sCAAsC,SAAS;CAC7D,IAAI,CAAC,OACH,OAAO;EACL,OAAO,KAAA;EACP,YAAY,KAAA;EACZ,SAAS,KAAA;EACT,OAAO,KAAA;CACT;CAGF,OAAO;EACL,OAAO,MAAM;EACb,YAAY,MAAM;EAClB,SAAS,MAAM;EACf,OAAO,MAAM;CACf;AACF;;AAYA,SAAgB,0BAId,SAA2C,WAAuB;CAClE,OAAO,sCAAsC,IAC3C;EACE,OAAO,QAAQ;EACf,YAAY,QAAQ;EACpB,SAAS,QAAQ;EACjB,OAAO,KAAA;CACT,GACA,SACF;AACF;;AAGA,SAAgB,uBAA0B,WAAuB;CAC/D,OAAO,sCAAsC,KAAK,SAAS;AAC7D"}
1
+ {"version":3,"file":"current-agent-DhoDkSnH.js","names":[],"sources":["../src/lifecycle/current-agent.ts"],"sourcesContent":["import { AsyncLocalStorage } from \"node:async_hooks\";\nimport type { DurableObject } from \"cloudflare:workers\";\n\nimport type { MemoryLimitContext } from \"./capability-runner\";\nimport type { Lifecycle } from \"./durable-object-lifecycle\";\nimport type { Connection } from \"./types\";\nimport type { LifecycleJobContext, LifecycleJobOutcome } from \"./job-queue\";\n\n/**\n * A Durable Object that has installed the Agents SDK Lifecycle.\n *\n * Pass a more specific Lifecycle Object type to {@link getCurrentAgent} when\n * shared host code needs APIs implemented by a particular object.\n *\n * @experimental The API surface may change before stabilizing.\n */\nexport interface LifecycleObject<\n Env extends object = Cloudflare.Env,\n Props extends Record<string, unknown> = Record<string, unknown>\n> extends DurableObject<Env> {\n readonly lifecycle: Lifecycle<Env, Props>;\n onStart?(props?: Props): void | Promise<void>;\n onRequest?(request: Request): Response | Promise<Response>;\n onAlarm?(): void | Promise<void>;\n onJob?(\n context: LifecycleJobContext\n ): LifecycleJobOutcome | void | Promise<LifecycleJobOutcome | void>;\n /**\n * Host domain policy applied when the alarm memory-limit circuit breaker\n * records a strike (#1825), after every capability's `onMemoryLimit` hook.\n *\n * Lifecycle dispatches host hooks structurally through its internal host\n * cast, so TS visibility is the host's to choose: a framework host whose\n * implementation is internal machinery declares the hook `protected` (the\n * pattern — see `AIChatAgent`/`Think`) and keeps the real work in a\n * `private _cf_`-prefixed method, without falling out of this contract at\n * runtime.\n */\n onAlarmMemoryLimit?(context: MemoryLimitContext): void | Promise<void>;\n}\n\n/** Values associated with the currently executing Lifecycle host. */\nexport type AgentContextStore = {\n /** Lifecycle host selected for this invocation. */\n agent: unknown;\n /** WebSocket connection selected for this invocation, when applicable. */\n connection: Connection | undefined;\n /** HTTP request selected for this invocation, when applicable. */\n request: Request | undefined;\n /** Extension-owned value selected for this invocation, when applicable. */\n email: unknown;\n};\n\n/** Values returned by {@link getCurrentAgent}. */\nexport type CurrentAgentContext<\n Host extends DurableObject = LifecycleObject,\n Email = unknown\n> = {\n agent: Host | undefined;\n connection: Connection | undefined;\n request: Request | undefined;\n email: Email | undefined;\n};\n\n/**\n * Shared invocation context for Lifecycle, Agent, AIChatAgent, and Think.\n *\n * @internal Importing or relying on this symbol will break your code in a\n * future release. Use {@link getCurrentAgent} to read the public context.\n */\nexport const __DO_NOT_USE_WILL_BREAK__agentContext =\n new AsyncLocalStorage<AgentContextStore>();\n\n/**\n * Return the current Agent or Lifecycle Object and invocation-specific values.\n *\n * Lifecycle host startup and alarm hooks receive the current object with no\n * request. Request hooks additionally receive the request being handled.\n * Lifecycle-managed WebSocket hooks receive their connection and, during\n * connect, its upgrade request. Agent extensions may also establish context\n * for email, chat turns, callable methods, and detached work. Capability hooks\n * do not run in this ambient context.\n */\nexport function getCurrentAgent<\n Host extends DurableObject = LifecycleObject\n>(): CurrentAgentContext<Host> {\n const store = __DO_NOT_USE_WILL_BREAK__agentContext.getStore();\n if (!store) {\n return {\n agent: undefined,\n connection: undefined,\n request: undefined,\n email: undefined\n };\n }\n\n return {\n agent: store.agent as Host,\n connection: store.connection,\n request: store.request,\n email: store.email\n };\n}\n\ntype LifecycleHostContext<\n Env extends object,\n Props extends Record<string, unknown>\n> = {\n readonly host: LifecycleObject<Env, Props>;\n readonly connection?: Connection;\n readonly request?: Request;\n};\n\n/** Run one host hook in the context of its Lifecycle Object. */\nexport function runInLifecycleHostContext<\n Env extends object,\n Props extends Record<string, unknown>,\n T\n>(context: LifecycleHostContext<Env, Props>, operation: () => T): T {\n return __DO_NOT_USE_WILL_BREAK__agentContext.run(\n {\n agent: context.host,\n connection: context.connection,\n request: context.request,\n email: undefined\n },\n operation\n );\n}\n\n/** Run a capability hook without inheriting a current Agent or host context. */\nexport function runWithoutCurrentAgent<T>(operation: () => T): T {\n return __DO_NOT_USE_WILL_BREAK__agentContext.exit(operation);\n}\n"],"mappings":";;;;;;;;AAsEA,MAAa,wCACX,IAAI,kBAAqC;;;;;;;;;;;AAY3C,SAAgB,kBAEe;CAC7B,MAAM,QAAQ,sCAAsC,SAAS;CAC7D,IAAI,CAAC,OACH,OAAO;EACL,OAAO,KAAA;EACP,YAAY,KAAA;EACZ,SAAS,KAAA;EACT,OAAO,KAAA;CACT;CAGF,OAAO;EACL,OAAO,MAAM;EACb,YAAY,MAAM;EAClB,SAAS,MAAM;EACf,OAAO,MAAM;CACf;AACF;;AAYA,SAAgB,0BAId,SAA2C,WAAuB;CAClE,OAAO,sCAAsC,IAC3C;EACE,OAAO,QAAQ;EACf,YAAY,QAAQ;EACpB,SAAS,QAAQ;EACjB,OAAO,KAAA;CACT,GACA,SACF;AACF;;AAGA,SAAgB,uBAA0B,WAAuB;CAC/D,OAAO,sCAAsC,KAAK,SAAS;AAC7D"}
@@ -8,6 +8,8 @@ const channels = {
8
8
  chat: channel("agents:chat"),
9
9
  transcript: channel("agents:transcript"),
10
10
  fiber: channel("agents:fiber"),
11
+ task: channel("agents:task"),
12
+ stream: channel("agents:stream"),
11
13
  agentTool: channel("agents:agent_tool"),
12
14
  schedule: channel("agents:schedule"),
13
15
  lifecycle: channel("agents:lifecycle"),
@@ -20,6 +22,8 @@ function getChannel(type) {
20
22
  if (type.startsWith("mcp:")) return channels.mcp;
21
23
  if (type.startsWith("workflow:")) return channels.workflow;
22
24
  if (type.startsWith("fiber:")) return channels.fiber;
25
+ if (type.startsWith("task:")) return channels.task;
26
+ if (type.startsWith("stream:")) return channels.stream;
23
27
  if (type.startsWith("transcript:") || type.startsWith("chat:transcript:")) return channels.transcript;
24
28
  if (type.startsWith("chat:")) return channels.chat;
25
29
  if (type.startsWith("agent_tool:")) return channels.agentTool;
@@ -38,4 +42,4 @@ function publishDiagnosticsEvent(event) {
38
42
  //#endregion
39
43
  export { publishDiagnosticsEvent as n, channels as t };
40
44
 
41
- //# sourceMappingURL=diagnostics-CaBjfz4J.js.map
45
+ //# sourceMappingURL=diagnostics-BzvaX2UT.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"diagnostics-BzvaX2UT.js","names":[],"sources":["../src/observability/diagnostics.ts"],"sourcesContent":["import { channel, type Channel } from \"node:diagnostics_channel\";\n\n/** Event shape accepted by the diagnostics-channel publisher. */\nexport type DiagnosticsEvent = {\n readonly type: string;\n readonly source?: string;\n readonly agent?: string;\n readonly name?: string;\n readonly payload: unknown;\n readonly timestamp: number;\n};\n\n/** Diagnostics channels for Agent and Lifecycle telemetry. */\nexport const channels = {\n state: channel(\"agents:state\"),\n rpc: channel(\"agents:rpc\"),\n message: channel(\"agents:message\"),\n chat: channel(\"agents:chat\"),\n transcript: channel(\"agents:transcript\"),\n fiber: channel(\"agents:fiber\"),\n task: channel(\"agents:task\"),\n stream: channel(\"agents:stream\"),\n agentTool: channel(\"agents:agent_tool\"),\n schedule: channel(\"agents:schedule\"),\n lifecycle: channel(\"agents:lifecycle\"),\n workflow: channel(\"agents:workflow\"),\n mcp: channel(\"agents:mcp\"),\n email: channel(\"agents:email\"),\n channel: channel(\"agents:channel\")\n} as const;\n\nfunction getChannel(type: string): Channel {\n if (type.startsWith(\"mcp:\")) return channels.mcp;\n if (type.startsWith(\"workflow:\")) return channels.workflow;\n if (type.startsWith(\"fiber:\")) return channels.fiber;\n if (type.startsWith(\"task:\")) return channels.task;\n if (type.startsWith(\"stream:\")) return channels.stream;\n if (type.startsWith(\"transcript:\") || type.startsWith(\"chat:transcript:\"))\n return channels.transcript;\n if (type.startsWith(\"chat:\")) return channels.chat;\n if (type.startsWith(\"agent_tool:\")) return channels.agentTool;\n if (type.startsWith(\"schedule:\") || type.startsWith(\"queue:\"))\n return channels.schedule;\n if (\n type.startsWith(\"message:\") ||\n type.startsWith(\"tool:\") ||\n type.startsWith(\"submission:\") ||\n type.startsWith(\"action:\")\n )\n return channels.message;\n if (type === \"rpc\" || type.startsWith(\"rpc:\")) return channels.rpc;\n if (type.startsWith(\"state:\")) return channels.state;\n if (type.startsWith(\"email:\")) return channels.email;\n if (type.startsWith(\"channel:\") || type.startsWith(\"notice:\"))\n return channels.channel;\n return channels.lifecycle;\n}\n\n/** Publish one event to its existing diagnostics channel. */\nexport function publishDiagnosticsEvent(event: DiagnosticsEvent): void {\n getChannel(event.type).publish(event);\n}\n"],"mappings":";;;AAaA,MAAa,WAAW;CACtB,OAAO,QAAQ,cAAc;CAC7B,KAAK,QAAQ,YAAY;CACzB,SAAS,QAAQ,gBAAgB;CACjC,MAAM,QAAQ,aAAa;CAC3B,YAAY,QAAQ,mBAAmB;CACvC,OAAO,QAAQ,cAAc;CAC7B,MAAM,QAAQ,aAAa;CAC3B,QAAQ,QAAQ,eAAe;CAC/B,WAAW,QAAQ,mBAAmB;CACtC,UAAU,QAAQ,iBAAiB;CACnC,WAAW,QAAQ,kBAAkB;CACrC,UAAU,QAAQ,iBAAiB;CACnC,KAAK,QAAQ,YAAY;CACzB,OAAO,QAAQ,cAAc;CAC7B,SAAS,QAAQ,gBAAgB;AACnC;AAEA,SAAS,WAAW,MAAuB;CACzC,IAAI,KAAK,WAAW,MAAM,GAAG,OAAO,SAAS;CAC7C,IAAI,KAAK,WAAW,WAAW,GAAG,OAAO,SAAS;CAClD,IAAI,KAAK,WAAW,QAAQ,GAAG,OAAO,SAAS;CAC/C,IAAI,KAAK,WAAW,OAAO,GAAG,OAAO,SAAS;CAC9C,IAAI,KAAK,WAAW,SAAS,GAAG,OAAO,SAAS;CAChD,IAAI,KAAK,WAAW,aAAa,KAAK,KAAK,WAAW,kBAAkB,GACtE,OAAO,SAAS;CAClB,IAAI,KAAK,WAAW,OAAO,GAAG,OAAO,SAAS;CAC9C,IAAI,KAAK,WAAW,aAAa,GAAG,OAAO,SAAS;CACpD,IAAI,KAAK,WAAW,WAAW,KAAK,KAAK,WAAW,QAAQ,GAC1D,OAAO,SAAS;CAClB,IACE,KAAK,WAAW,UAAU,KAC1B,KAAK,WAAW,OAAO,KACvB,KAAK,WAAW,aAAa,KAC7B,KAAK,WAAW,SAAS,GAEzB,OAAO,SAAS;CAClB,IAAI,SAAS,SAAS,KAAK,WAAW,MAAM,GAAG,OAAO,SAAS;CAC/D,IAAI,KAAK,WAAW,QAAQ,GAAG,OAAO,SAAS;CAC/C,IAAI,KAAK,WAAW,QAAQ,GAAG,OAAO,SAAS;CAC/C,IAAI,KAAK,WAAW,UAAU,KAAK,KAAK,WAAW,SAAS,GAC1D,OAAO,SAAS;CAClB,OAAO,SAAS;AAClB;;AAGA,SAAgB,wBAAwB,OAA+B;CACrE,WAAW,MAAM,IAAI,CAAC,CAAC,QAAQ,KAAK;AACtC"}