agents 0.22.0 → 0.24.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 (215) hide show
  1. package/README.md +20 -1
  2. package/dist/{agent-routing-CnkaHb-v.d.ts → agent-routing-B2XLNMxq.d.ts} +1094 -443
  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-Dj5Mzlw7.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 → capability-B4WbF81e.js} +2 -1
  15. package/dist/capability-B4WbF81e.js.map +1 -0
  16. package/dist/capability-runner-BUBa6Ake.d.ts +484 -0
  17. package/dist/channel-Bnm4S7T2.d.ts +491 -0
  18. package/dist/channels/ai-sdk.d.ts +46 -0
  19. package/dist/channels/ai-sdk.js +120 -0
  20. package/dist/channels/ai-sdk.js.map +1 -0
  21. package/dist/channels/email.d.ts +95 -0
  22. package/dist/channels/email.js +323 -0
  23. package/dist/channels/email.js.map +1 -0
  24. package/dist/channels/index.d.ts +233 -0
  25. package/dist/channels/index.js +608 -0
  26. package/dist/channels/index.js.map +1 -0
  27. package/dist/channels/slack.d.ts +140 -0
  28. package/dist/channels/slack.js +614 -0
  29. package/dist/channels/slack.js.map +1 -0
  30. package/dist/channels/tanstack-ai.d.ts +39 -0
  31. package/dist/channels/tanstack-ai.js +17 -0
  32. package/dist/channels/tanstack-ai.js.map +1 -0
  33. package/dist/channels/telegram.d.ts +106 -0
  34. package/dist/channels/telegram.js +427 -0
  35. package/dist/channels/telegram.js.map +1 -0
  36. package/dist/channels/voice.d.ts +45 -0
  37. package/dist/channels/voice.js +122 -0
  38. package/dist/channels/voice.js.map +1 -0
  39. package/dist/chat/index.d.ts +2319 -2010
  40. package/dist/chat/index.js +892 -518
  41. package/dist/chat/index.js.map +1 -1
  42. package/dist/chat-sdk/index.d.ts +7 -7
  43. package/dist/chat-sdk/index.js +1 -1
  44. package/dist/client-BxdkCuVy.d.ts +411 -0
  45. package/dist/client-C9DAjZIY.js +518 -0
  46. package/dist/client-C9DAjZIY.js.map +1 -0
  47. package/dist/{client-jagG8a9_.js → client-fyQrLGkD.js} +2 -2
  48. package/dist/{client-jagG8a9_.js.map → client-fyQrLGkD.js.map} +1 -1
  49. package/dist/client.d.ts +27 -283
  50. package/dist/client.js +2 -274
  51. package/dist/context/index.d.ts +216 -0
  52. package/dist/context/index.js +454 -0
  53. package/dist/context/index.js.map +1 -0
  54. package/dist/current-agent-C4w86kcN.d.ts +93 -0
  55. package/dist/current-agent-DhoDkSnH.js.map +1 -1
  56. package/dist/{diagnostics-CaBjfz4J.js → diagnostics-BzvaX2UT.js} +5 -1
  57. package/dist/diagnostics-BzvaX2UT.js.map +1 -0
  58. package/dist/diagnostics-C4jcz3VK.js +360 -0
  59. package/dist/diagnostics-C4jcz3VK.js.map +1 -0
  60. package/dist/durable-object-lifecycle-ei7M7lqB.d.ts +157 -0
  61. package/dist/index-BB0kqhIz.d.ts +101 -0
  62. package/dist/index-BVVgDSdq.d.ts +1 -0
  63. package/dist/index-Dlgtd3Mj.d.ts +78 -0
  64. package/dist/index-XDkuQ7zm.d.ts +89 -0
  65. package/dist/{index-DcSAZKsB.d.ts → index-YSKgfgg9.d.ts} +3 -1
  66. package/dist/index.d.ts +92 -83
  67. package/dist/index.js +4 -3
  68. package/dist/ingress-BfetZbMO.js +83 -0
  69. package/dist/ingress-BfetZbMO.js.map +1 -0
  70. package/dist/internal-CYlgHl1l.js +59 -0
  71. package/dist/internal-CYlgHl1l.js.map +1 -0
  72. package/dist/internal_context.d.ts +1 -1
  73. package/dist/lifecycle/index.d.ts +39 -19
  74. package/dist/lifecycle/index.js +2 -2
  75. package/dist/lifecycle-Mm_jQh7r.js +1314 -0
  76. package/dist/lifecycle-Mm_jQh7r.js.map +1 -0
  77. package/dist/mcp/client/index.d.ts +20 -20
  78. package/dist/mcp/client/index.js +1 -1
  79. package/dist/mcp/index.d.ts +35 -35
  80. package/dist/mcp/index.js +2 -2
  81. package/dist/observability/index.d.ts +1 -1
  82. package/dist/observability/index.js +1 -1
  83. package/dist/queue/index.d.ts +123 -0
  84. package/dist/queue/index.js +2 -0
  85. package/dist/queue-BC9F5cUo.js +406 -0
  86. package/dist/queue-BC9F5cUo.js.map +1 -0
  87. package/dist/react.d.ts +34 -16
  88. package/dist/react.js +31 -8
  89. package/dist/react.js.map +1 -1
  90. package/dist/{retries-CAvxtG9d.d.ts → retries-D9B2UCq3.d.ts} +23 -6
  91. package/dist/retries.d.ts +10 -6
  92. package/dist/retries.js +21 -1
  93. package/dist/retries.js.map +1 -1
  94. package/dist/routing/index.d.ts +137 -0
  95. package/dist/routing/index.js +244 -0
  96. package/dist/routing/index.js.map +1 -0
  97. package/dist/sanitize-D9TujEK8.js +79 -0
  98. package/dist/sanitize-D9TujEK8.js.map +1 -0
  99. package/dist/{scheduler-DQoTGoAW.d.ts → scheduler-BGq6M5Kd.d.ts} +20 -21
  100. package/dist/scheduler-D_KHqxBa.js +658 -0
  101. package/dist/scheduler-D_KHqxBa.js.map +1 -0
  102. package/dist/schedules/index.d.ts +1 -1
  103. package/dist/schedules/index.js +1 -1
  104. package/dist/sentence-chunker-BAidJ4DA.d.ts +68 -0
  105. package/dist/serializable.d.ts +1 -1
  106. package/dist/sessions/index.d.ts +441 -0
  107. package/dist/sessions/index.js +2063 -0
  108. package/dist/sessions/index.js.map +1 -0
  109. package/dist/skills/index.d.ts +99 -0
  110. package/dist/skills/index.js +254 -5
  111. package/dist/skills/index.js.map +1 -1
  112. package/dist/{src-5W6JNKVb.js → src-BNU3ZiJM.js} +1524 -1441
  113. package/dist/src-BNU3ZiJM.js.map +1 -0
  114. package/dist/state/index.d.ts +6 -0
  115. package/dist/state/index.js +137 -0
  116. package/dist/state/index.js.map +1 -0
  117. package/dist/streams/index.d.ts +120 -0
  118. package/dist/streams/index.js +107 -0
  119. package/dist/streams/index.js.map +1 -0
  120. package/dist/streams-BcE8JP2X.d.ts +370 -0
  121. package/dist/streams-CCPRV6dt.js +709 -0
  122. package/dist/streams-CCPRV6dt.js.map +1 -0
  123. package/dist/sub-routing.d.ts +12 -12
  124. package/dist/surface-bZZJqBka.js +17 -0
  125. package/dist/surface-bZZJqBka.js.map +1 -0
  126. package/dist/tasks/index.d.ts +64 -0
  127. package/dist/tasks/index.js +2 -0
  128. package/dist/tasks-D4nLqVSI.js +1656 -0
  129. package/dist/tasks-D4nLqVSI.js.map +1 -0
  130. package/dist/tasks-V2lCT8ZV.d.ts +517 -0
  131. package/dist/text-segment-joiner-BtAFQSA_.js +57 -0
  132. package/dist/text-segment-joiner-BtAFQSA_.js.map +1 -0
  133. package/dist/text-stream-CpdiKrJB.js +272 -0
  134. package/dist/text-stream-CpdiKrJB.js.map +1 -0
  135. package/dist/tokens-nHAKcN6M.js +52 -0
  136. package/dist/tokens-nHAKcN6M.js.map +1 -0
  137. package/dist/tool-schema-CBjGPrsQ.js +31 -0
  138. package/dist/tool-schema-CBjGPrsQ.js.map +1 -0
  139. package/dist/transport-protocol-BxyngQ_R.js +25 -0
  140. package/dist/transport-protocol-BxyngQ_R.js.map +1 -0
  141. package/dist/transport-protocol-ZTew5Wso.d.ts +27 -0
  142. package/dist/types-B7LojTe4.d.ts +202 -0
  143. package/dist/types-DCD6BNZH.d.ts +70 -0
  144. package/dist/types-_Faxb570.d.ts +439 -0
  145. package/dist/voice/client.d.ts +226 -0
  146. package/dist/voice/client.js +932 -0
  147. package/dist/voice/client.js.map +1 -0
  148. package/dist/voice/errors.d.ts +43 -0
  149. package/dist/voice/errors.js +41 -0
  150. package/dist/voice/errors.js.map +1 -0
  151. package/dist/voice/index.d.ts +271 -0
  152. package/dist/voice/index.js +1812 -0
  153. package/dist/voice/index.js.map +1 -0
  154. package/dist/voice/react.d.ts +167 -0
  155. package/dist/voice/react.js +234 -0
  156. package/dist/voice/react.js.map +1 -0
  157. package/dist/voice/sfu.d.ts +71 -0
  158. package/dist/voice/sfu.js +157 -0
  159. package/dist/voice/sfu.js.map +1 -0
  160. package/dist/voice/text.d.ts +6 -0
  161. package/dist/voice/text.js +2 -0
  162. package/dist/voice/types.d.ts +58 -0
  163. package/dist/voice/types.js +18 -0
  164. package/dist/voice/types.js.map +1 -0
  165. package/dist/voice/workers-ai.d.ts +136 -0
  166. package/dist/voice/workers-ai.js +568 -0
  167. package/dist/voice/workers-ai.js.map +1 -0
  168. package/dist/websockets/index.d.ts +301 -0
  169. package/dist/websockets/index.js +3 -0
  170. package/dist/websockets-D7IzWbZb.js +1005 -0
  171. package/dist/websockets-D7IzWbZb.js.map +1 -0
  172. package/dist/workflow-types.d.ts +25 -25
  173. package/dist/workflows.d.ts +21 -21
  174. package/dist/workflows.js +1 -1
  175. package/docs/agent-class.md +4 -4
  176. package/docs/agent-tools.md +2 -1
  177. package/docs/channels.md +323 -0
  178. package/docs/chat-agents.md +6 -13
  179. package/docs/client-sdk.md +30 -0
  180. package/docs/context.md +131 -0
  181. package/docs/index.md +16 -13
  182. package/docs/lifecycle.md +179 -57
  183. package/docs/long-running-agents.md +2 -2
  184. package/docs/mcp-servers.md +5 -1
  185. package/docs/queue.md +135 -241
  186. package/docs/resumable-streaming.md +1 -1
  187. package/docs/retries.md +1 -1
  188. package/docs/routing.md +105 -0
  189. package/docs/sessions.md +237 -871
  190. package/docs/streams.md +213 -0
  191. package/docs/sub-agents.md +184 -124
  192. package/docs/tasks.md +246 -0
  193. package/docs/voice.md +745 -0
  194. package/package.json +126 -14
  195. package/dist/capability-BjSKYpzg.js.map +0 -1
  196. package/dist/capability-runner-CvHGZqUu.d.ts +0 -150
  197. package/dist/client.js.map +0 -1
  198. package/dist/compaction-helpers-iiKMr2TQ.js +0 -340
  199. package/dist/compaction-helpers-iiKMr2TQ.js.map +0 -1
  200. package/dist/compaction-helpers-wUz6M3us.d.ts +0 -621
  201. package/dist/current-agent-CuMErtly.d.ts +0 -283
  202. package/dist/diagnostics-CaBjfz4J.js.map +0 -1
  203. package/dist/durable-object-lifecycle-D6nNQJJd.js +0 -862
  204. package/dist/durable-object-lifecycle-D6nNQJJd.js.map +0 -1
  205. package/dist/experimental/memory/session/index.d.ts +0 -671
  206. package/dist/experimental/memory/session/index.js +0 -2379
  207. package/dist/experimental/memory/session/index.js.map +0 -1
  208. package/dist/experimental/memory/utils/index.d.ts +0 -96
  209. package/dist/experimental/memory/utils/index.js +0 -79
  210. package/dist/experimental/memory/utils/index.js.map +0 -1
  211. package/dist/scheduler-CR9RHGos.js +0 -857
  212. package/dist/scheduler-CR9RHGos.js.map +0 -1
  213. package/dist/src-5W6JNKVb.js.map +0 -1
  214. package/dist/tool-output-truncation-CNnnGZQ3.js +0 -98
  215. package/dist/tool-output-truncation-CNnnGZQ3.js.map +0 -1
@@ -0,0 +1,484 @@
1
+ import { t as RetryOptions } from "./retries-D9B2UCq3.js";
2
+
3
+ //#region src/lifecycle/types.d.ts
4
+ type ImmutablePrimitive = undefined | null | boolean | string | number;
5
+ type Immutable<T> = T extends ImmutablePrimitive
6
+ ? T
7
+ : T extends Array<infer U>
8
+ ? ImmutableArray<U>
9
+ : T extends Map<infer K, infer V>
10
+ ? ImmutableMap<K, V>
11
+ : T extends Set<infer M>
12
+ ? ImmutableSet<M>
13
+ : ImmutableObject<T>;
14
+ type ImmutableArray<T> = ReadonlyArray<Immutable<T>>;
15
+ type ImmutableMap<K, V> = ReadonlyMap<Immutable<K>, Immutable<V>>;
16
+ type ImmutableSet<T> = ReadonlySet<Immutable<T>>;
17
+ type ImmutableObject<T> = { readonly [K in keyof T]: Immutable<T[K]> };
18
+ /** A payload delivered on a WebSocket connection. */
19
+ type WSMessage = ArrayBuffer | ArrayBufferView | string;
20
+ /** Immutable state persisted in a hibernating WebSocket attachment. */
21
+ type ConnectionState<T> = ImmutableObject<T> | null;
22
+ /** Functional update applied to a connection's current state. */
23
+ type ConnectionSetStateFn<T> = (prevState: ConnectionState<T>) => T;
24
+ /** Context supplied when a lifecycle accepts a WebSocket connection. */
25
+ type ConnectionContext = {
26
+ /** Original WebSocket upgrade request. */ request: Request;
27
+ };
28
+ /** A WebSocket managed by a Durable Object lifecycle. */
29
+ type Connection<TState = unknown> = WebSocket & {
30
+ /** Connection identifier */ id: string;
31
+ /**
32
+ * The URL of the original WebSocket upgrade request.
33
+ * Persisted in the WebSocket attachment so it survives hibernation.
34
+ */
35
+ uri: string | null;
36
+ /**
37
+ * Arbitrary state associated with this connection.
38
+ * Read-only — use {@link Connection.setState} to update.
39
+ *
40
+ * This property is configurable, meaning it can be redefined via
41
+ * `Object.defineProperty` by downstream consumers (e.g. the Cloudflare
42
+ * Agents SDK) to namespace or wrap internal state storage.
43
+ */
44
+ state: ConnectionState<TState>;
45
+ /**
46
+ * Update the state associated with this connection.
47
+ *
48
+ * Accepts either a new state value or an updater function that receives
49
+ * the previous state and returns the next state.
50
+ *
51
+ * This property is configurable, meaning it can be redefined via
52
+ * `Object.defineProperty` by downstream consumers that provide their own
53
+ * state projection.
54
+ */
55
+ setState(
56
+ state: TState | ConnectionSetStateFn<TState> | null
57
+ ): ConnectionState<TState>;
58
+ /**
59
+ * Tags returned by the owning Durable Object's `getConnectionTags` callback.
60
+ * Always includes the connection id as the first tag.
61
+ */
62
+ tags: readonly string[];
63
+ };
64
+ //#endregion
65
+ //#region src/lifecycle/job-queue.d.ts
66
+ /** One durable job in the Lifecycle queue. */
67
+ type LifecycleJob = {
68
+ /** Unique job id. Stable across reschedules. */ readonly id: string /** Owning capability id, or `"host"`. */;
69
+ readonly capability: string /** Serialisable function name the owner dispatches on. */;
70
+ readonly fn: string /** Due time in epoch milliseconds. */;
71
+ readonly time: number /** Owner-defined payload, JSON round-tripped. */;
72
+ readonly payload: unknown /** Retry policy for dispatch, when the pusher supplied one. */;
73
+ readonly retry:
74
+ | RetryOptions
75
+ | undefined /** Whether the job is skipped while a previous run is in flight. */;
76
+ readonly singleflight: boolean /** Whether the job suppresses ordinary alarm candidates while pending. */;
77
+ readonly exclusive: boolean /** Whether the alarm memory-limit breaker governs this pending job (#1825). */;
78
+ readonly recoveryLoop: boolean /** Creation time in epoch seconds. */;
79
+ readonly createdAt: number;
80
+ };
81
+ /** Options accepted when pushing one job. */
82
+ type LifecycleJobPushOptions = {
83
+ /** Serialisable function name the owner dispatches on. */ readonly fn: string /** Due time in epoch milliseconds. */;
84
+ readonly time: number /** Owner-defined payload. Must be JSON-serializable. */;
85
+ readonly payload?: unknown;
86
+ /**
87
+ * Stable job id. A push with an existing id replaces that job.
88
+ * Omitted ids are generated.
89
+ */
90
+ readonly id?: string /** Retry policy for dispatch, overriding the queue default. */;
91
+ readonly retry?: RetryOptions /** Skip this job while a previous run of it is still in flight. */;
92
+ readonly singleflight?: boolean;
93
+ /**
94
+ * Seconds before an in-flight single-flight run is treated as hung, and
95
+ * before any long dispatch triggers the slow-dispatch warning. Default: 30.
96
+ */
97
+ readonly hungTimeoutSeconds?: number /** Suppress ordinary alarm candidates while this job is pending. */;
98
+ readonly exclusive?: boolean;
99
+ /**
100
+ * Mark this job as part of a recovery loop that can deterministically
101
+ * exhaust memory. On an alarm memory-limit strike the circuit breaker
102
+ * (#1825) backs off every pending flagged job to the strike's backoff
103
+ * time, and purges them all when it seals at the strike budget — so a
104
+ * doomed loop cannot re-trigger through a sibling row while unrelated
105
+ * jobs stay untouched.
106
+ */
107
+ readonly recoveryLoop?: boolean;
108
+ };
109
+ /**
110
+ * What the owner tells Lifecycle after one job ran — the drive result.
111
+ *
112
+ * `undefined` (or no return) completes the job and deletes it.
113
+ * `{ rescheduleAt }` suspends the job until a future time.
114
+ * `"yield"` leaves the job due, waking again immediately.
115
+ *
116
+ * A same-id `push()` or `reschedule()` made while the job is dispatching
117
+ * supersedes the drive result: the newer durable intent wins, and the
118
+ * result is quietly discarded. Owners that both push and return outcomes
119
+ * for the same job should derive both from the same durable state so the
120
+ * two always agree.
121
+ */
122
+ type LifecycleJobOutcome =
123
+ | undefined
124
+ | {
125
+ readonly rescheduleAt: number;
126
+ }
127
+ | "yield";
128
+ /** Context supplied when a job is dispatched to its owner. */
129
+ type LifecycleJobContext = {
130
+ /** The due job being executed. */ readonly job: LifecycleJob /** 1-indexed dispatch attempt within the current alarm invocation. */;
131
+ readonly attempt: number;
132
+ };
133
+ /** Job-queue access scoped to one owning capability. */
134
+ type LifecycleJobs = {
135
+ /**
136
+ * Push one job. A push with an existing id replaces that job — ids are
137
+ * scoped to their owner, so replacing (or colliding with) another
138
+ * owner's job is impossible; a cross-owner id collision throws instead.
139
+ */
140
+ readonly push: (
141
+ options: LifecycleJobPushOptions
142
+ ) => Promise<LifecycleJob> /** Cancel one owned job. Returns false when no job matched. */;
143
+ readonly cancel: (
144
+ id: string
145
+ ) => Promise<boolean> /** Re-time one owned job. Returns false when no job matched. */;
146
+ readonly reschedule: (
147
+ id: string,
148
+ time: number
149
+ ) => Promise<boolean> /** Read one owned job. */;
150
+ readonly get: (
151
+ id: string
152
+ ) =>
153
+ | LifecycleJob
154
+ | undefined /** List every owned job, ordered by due time. */;
155
+ readonly list: () => LifecycleJob[];
156
+ /**
157
+ * Recompute the physical alarm from queue state without mutating it.
158
+ * Mutations re-arm automatically; use this only to recover a lost alarm
159
+ * for existing jobs (e.g. an idempotent push that deduplicated).
160
+ */
161
+ readonly rearm: () => Promise<void>;
162
+ };
163
+ //#endregion
164
+ //#region src/lifecycle/capability.d.ts
165
+ /** Opaque address understood by a Lifecycle routing transport. */
166
+ type LifecycleRouteAddress = {
167
+ /** Stable equality and storage key. */ readonly key: string /** Transport-owned serialized address. */;
168
+ readonly data: string;
169
+ };
170
+ /** Context supplied with a routed capability message. */
171
+ type LifecycleRouteContext = {
172
+ /** Address of the sending Lifecycle, or undefined for an unrouted root. */ readonly source:
173
+ | LifecycleRouteAddress
174
+ | undefined /** Capability-owned message payload. */;
175
+ readonly payload: unknown;
176
+ };
177
+ /** Startup state of a Lifecycle. */
178
+ type LifecycleStatus = "zero" | "starting" | "started";
179
+ /** Best-effort telemetry available to every Lifecycle capability. */
180
+ type LifecycleEvents = {
181
+ /** Publish an event under this capability's stable identity. */ readonly emit: (
182
+ type: string,
183
+ payload: unknown
184
+ ) => void;
185
+ };
186
+ /** Routing available to every Lifecycle capability. */
187
+ type LifecycleRoutes = {
188
+ /** This Lifecycle's transport address, or undefined at the route root. */ readonly source:
189
+ | LifecycleRouteAddress
190
+ | undefined /** Route a capability-owned message to the root Lifecycle. */;
191
+ readonly toRoot: (
192
+ payload: unknown
193
+ ) => Promise<unknown> /** Route a capability-owned message to another Lifecycle. */;
194
+ readonly to: (
195
+ target: LifecycleRouteAddress,
196
+ payload: unknown
197
+ ) => Promise<unknown>;
198
+ };
199
+ /**
200
+ * Ambient scope a capability supplies when entering host context on
201
+ * behalf of a live connection or request.
202
+ */
203
+ type LifecycleHostContextScope = {
204
+ /** The connection the callback runs on behalf of, when there is one. */ readonly connection?: Connection /** The request the callback runs on behalf of, when there is one. */;
205
+ readonly request?: Request;
206
+ };
207
+ /**
208
+ * The platform's hibernatable-socket surface, exposed narrowly so a
209
+ * capability that owns connections (e.g. WebSockets) can accept and
210
+ * enumerate them without holding the whole `DurableObjectState`.
211
+ * These are workerd API names, not Lifecycle modeling sockets.
212
+ */
213
+ type LifecycleSockets = {
214
+ /** Accept a socket into hibernation under the given tags. */ readonly accept: (
215
+ ws: WebSocket,
216
+ tags: string[]
217
+ ) => void /** Every hibernated socket on the object, optionally by tag. */;
218
+ readonly get: (tag?: string) => WebSocket[];
219
+ };
220
+ /**
221
+ * Standard services granted to every installed Lifecycle capability.
222
+ *
223
+ * @experimental The API surface may change before stabilizing.
224
+ */
225
+ type LifecycleServices = {
226
+ /** The host's Durable Object name; see `Lifecycle.name`. */ readonly name: string /** The host class name, as exported from the Worker. */;
227
+ readonly className: string;
228
+ readonly storage: DurableObjectStorage;
229
+ readonly sockets: LifecycleSockets;
230
+ readonly ready: () => Promise<void>;
231
+ /**
232
+ * Startup state: `"zero"` before startup, `"starting"` while capability
233
+ * and host startup hooks run, `"started"` once `ready()` resolves without
234
+ * waiting.
235
+ */
236
+ readonly status: () => LifecycleStatus;
237
+ /**
238
+ * This capability's scoped access to the Lifecycle-owned work queue.
239
+ * Pushed items are dispatched to `onJob` when due; every queue mutation
240
+ * re-arms the physical alarm automatically.
241
+ */
242
+ readonly jobs: LifecycleJobs;
243
+ /**
244
+ * Keep work this capability hands off at a bounded `onJob` return inside
245
+ * the current alarm's memory-limit breaker domain (#1825). Returns false,
246
+ * tracking nothing, outside an alarm invocation.
247
+ */
248
+ readonly trackAlarmWork: (work: Promise<unknown>) => boolean;
249
+ /**
250
+ * Run a capability-held user callback inside the host invocation context.
251
+ * Capability hooks run outside host context; this is the one boundary for
252
+ * entering it, and a host composition root may substitute its own wrapper
253
+ * (Agent adds tracing span scope). Pass `scope` to make a live
254
+ * connection or request ambient for the callback.
255
+ */
256
+ readonly runInHostContext: (
257
+ fn: () => unknown,
258
+ scope?: LifecycleHostContextScope
259
+ ) => Promise<unknown>;
260
+ readonly events: LifecycleEvents;
261
+ readonly routes: LifecycleRoutes;
262
+ };
263
+ /**
264
+ * Base class for capabilities that consume standard Lifecycle services.
265
+ *
266
+ * @experimental The API surface may change before stabilizing.
267
+ */
268
+ declare abstract class LifecycleCapability<Props extends object = object> {
269
+ readonly capabilityId: string;
270
+ /** How this capability claims traffic; see {@link DurableObjectCapability.claims}. */
271
+ readonly claims: "selective" | "catch-all";
272
+ protected constructor(capabilityId: string);
273
+ /** Default startup hook; capabilities override when they own startup work. */
274
+ onStart(_context: CapabilityStartContext<Props>): void;
275
+ /** Standard services when installed, or undefined in isolated unit tests. */
276
+ protected get lifecycleServices(): LifecycleServices | undefined;
277
+ /** Standard services supplied when Lifecycle installs this capability. */
278
+ protected get lifecycle(): LifecycleServices;
279
+ }
280
+ //#endregion
281
+ //#region src/lifecycle/capability-runner.d.ts
282
+ type MaybePromise<T> = T | Promise<T>;
283
+ /** One best-effort event published by a Lifecycle capability. */
284
+ type LifecycleEvent = {
285
+ /** Stable capability or subsystem name. */ readonly source: string /** Stable event name within that source. */;
286
+ readonly type: string /** Event-specific data. */;
287
+ readonly payload: unknown;
288
+ };
289
+ /** Context supplied when durable capabilities start. */
290
+ type CapabilityStartContext<Props extends object = object> = {
291
+ /** Properties supplied while resolving the Durable Object. */ readonly props:
292
+ | Props
293
+ | undefined;
294
+ };
295
+ /** Context supplied when durable capabilities inspect an HTTP request. */
296
+ type CapabilityRequestContext = {
297
+ /** The request entering the Durable Object. */ readonly request: Request;
298
+ };
299
+ /** Context supplied when a capability inspects a WebSocket upgrade. */
300
+ type CapabilityWebSocketUpgradeContext = {
301
+ /** The WebSocket upgrade request entering the Durable Object. */ readonly request: Request;
302
+ };
303
+ /**
304
+ * Context supplied when the alarm memory-limit circuit breaker records a
305
+ * strike (#1825), to capabilities and the host alike.
306
+ */
307
+ type MemoryLimitContext = {
308
+ /** Whether the breaker hit its strike budget and sealed recovery work. */ readonly sealed: boolean /** The backoff wake time (epoch ms) armed for an unsealed strike. */;
309
+ readonly nextTime?: number;
310
+ /**
311
+ * The queue job that was executing when the strike landed. Absent when the
312
+ * reset happened during startup or host alarm work. Lifecycle has already
313
+ * backed off or purged its row before invoking memory-limit policy.
314
+ *
315
+ * Capabilities whose durable state outlives their queue row use this to
316
+ * apply the same policy to the underlying work — Tasks routes a struck
317
+ * routed job's owner this way (see `setTaskRoutedMemoryLimitHandler`).
318
+ */
319
+ readonly executing?: LifecycleJob;
320
+ /**
321
+ * Recovery-loop jobs removed when this strike sealed the breaker. This is
322
+ * a pre-purge snapshot because the durable rows no longer exist when
323
+ * policy hooks run. No current capability reads this — Scheduler's own
324
+ * former use of it (routing a sealed strike to every purged row's owning
325
+ * dynamic agent) was retired once Tasks took over routed chat-recovery
326
+ * wakes — but it stays available for a capability whose routed rows can
327
+ * be purged as a pack, unlike Tasks' one-run-at-a-time model.
328
+ */
329
+ readonly purgedRecoveryLoopJobs?: ReadonlyArray<LifecycleJob>;
330
+ };
331
+ /**
332
+ * A capability installed into a Durable Object lifecycle.
333
+ *
334
+ * Capabilities extending `LifecycleCapability` receive the standard storage,
335
+ * readiness, alarm, event, and routing surface. Host-specific bindings and
336
+ * protocol adapters remain explicit constructor dependencies. Hook parameters
337
+ * carry only phase data; hooks do not run in ambient host context — a
338
+ * capability-held user callback re-enters host context exactly once,
339
+ * through `LifecycleServices.runInHostContext(fn, scope)`.
340
+ *
341
+ * A capability interacts with Lifecycle through exactly three channels:
342
+ * these declared hooks, the `LifecycleServices` surface, and
343
+ * composition-root `set*()` apertures. Any other direct reach in either
344
+ * direction is a design smell.
345
+ *
346
+ * Dispatch contract, hook by hook:
347
+ * - `onRequest` and `onWebSocketUpgrade` are offered in declaration
348
+ * order, the catch-all last; the first capability to return a `Response`
349
+ * claims the request, and a claimed upgrade's socket belongs to that
350
+ * capability for its whole lifetime.
351
+ * - `onWebSocketMessage`/`onWebSocketClose`/`onWebSocketError` are
352
+ * platform wakes, offered in declaration order; return `true` to
353
+ * consume one. Socket ownership is the capability's to determine —
354
+ * keep a private hibernation-attachment namespace and recognize your
355
+ * own sockets by it.
356
+ * - `onRoute` is addressed to one capability by its id; `onJob` is
357
+ * addressed by the due job's owning capability, with Lifecycle owning
358
+ * the queue and the one physical alarm.
359
+ *
360
+ * @experimental The API surface may change before stabilizing.
361
+ */
362
+ interface DurableObjectCapability<Props extends object = object> {
363
+ /**
364
+ * How this capability claims traffic. Default: `"selective"`.
365
+ *
366
+ * A `"selective"` capability answers only requests and upgrades it
367
+ * recognizes and declines the rest. A `"catch-all"` claims everything
368
+ * offered to the hooks it implements, so Lifecycle always places it
369
+ * last, whenever it was installed, and refuses a second catch-all for
370
+ * the same hook. Catch-alls for disjoint hooks coexist: `WebSockets`
371
+ * claims every upgrade and implements no `onRequest`, so an HTTP
372
+ * catch-all installs beside it. Selective HTTP capabilities target
373
+ * their own routes and run before either.
374
+ */
375
+ readonly claims?: "selective" | "catch-all";
376
+ /** Initialize or recover the capability before the host handles work. */
377
+ onStart?(context: CapabilityStartContext<Props>): MaybePromise<void>;
378
+ /**
379
+ * Act as middleware over HTTP requests, ahead of the host's request handler.
380
+ *
381
+ * Return a response to handle the request, or `undefined` to pass it to the
382
+ * next capability and finally the host.
383
+ */
384
+ onRequest?(
385
+ context: CapabilityRequestContext
386
+ ): MaybePromise<Response | undefined | void>;
387
+ /**
388
+ * Claim a WebSocket upgrade before the host's legacy connection path.
389
+ *
390
+ * A capability that returns a response owns that socket and its lifetime,
391
+ * including any hibernation attachment it needs to recognize the socket
392
+ * later. Return `undefined` to decline.
393
+ */
394
+ onWebSocketUpgrade?(
395
+ context: CapabilityWebSocketUpgradeContext
396
+ ): MaybePromise<Response | undefined | void>;
397
+ /**
398
+ * Handle a platform `webSocketMessage` wake for a socket this capability
399
+ * owns. Return `true` to consume the event; anything else offers it to
400
+ * the next capability and finally the host's legacy path. Ownership is
401
+ * the capability's to determine — typically via its own hibernation
402
+ * attachment namespace.
403
+ */
404
+ onWebSocketMessage?(
405
+ ws: WebSocket,
406
+ message: WSMessage
407
+ ): MaybePromise<boolean | void>;
408
+ /** Handle a platform `webSocketClose` wake for an owned socket. */
409
+ onWebSocketClose?(
410
+ ws: WebSocket,
411
+ code: number,
412
+ reason: string,
413
+ wasClean: boolean
414
+ ): MaybePromise<boolean | void>;
415
+ /** Handle a platform `webSocketError` wake for an owned socket. */
416
+ onWebSocketError?(
417
+ ws: WebSocket,
418
+ error: unknown
419
+ ): MaybePromise<boolean | void>;
420
+ /**
421
+ * Drive one due job this capability pushed into the Lifecycle queue.
422
+ * Return an outcome to reschedule or retain the job; returning nothing
423
+ * completes it.
424
+ *
425
+ * Dispatch must be bounded: the event loop awaits each job inline, so a
426
+ * long-running `onJob` delays every other job on this object. Detach
427
+ * unbounded work (start it, keep durable evidence, return) instead of
428
+ * awaiting it here; a dispatch that outlives the job's hung timeout
429
+ * logs a warning and emits `job:slow_dispatch`.
430
+ */
431
+ onJob?(
432
+ context: LifecycleJobContext
433
+ ): MaybePromise<LifecycleJobOutcome | void>;
434
+ /**
435
+ * Observe one job's terminal application failure after retry exhaustion.
436
+ * The returned outcome decides advancement; returning nothing completes
437
+ * the job.
438
+ */
439
+ onJobError?(
440
+ context: LifecycleJobContext,
441
+ error: unknown
442
+ ): MaybePromise<LifecycleJobOutcome | void>;
443
+ /**
444
+ * Apply domain policy after the alarm memory-limit circuit breaker
445
+ * records a strike (#1825). Lifecycle has already handled the queue: the
446
+ * executing job and every `recoveryLoop`-flagged job are backed off (or
447
+ * purged when `sealed`). Runs at the outermost alarm frame, post-unwind,
448
+ * best-effort — the isolate resets right after, so keep writes small.
449
+ */
450
+ onMemoryLimit?(context: MemoryLimitContext): MaybePromise<void>;
451
+ /** Handle one message routed to this capability identity. */
452
+ onRoute?(context: LifecycleRouteContext): MaybePromise<unknown>;
453
+ /** Release live or in-memory resources during explicit host destruction. */
454
+ dispose?(): MaybePromise<void>;
455
+ }
456
+ //#endregion
457
+ export {
458
+ ConnectionSetStateFn as C,
459
+ ConnectionContext as S,
460
+ WSMessage as T,
461
+ LifecycleJobContext as _,
462
+ LifecycleEvent as a,
463
+ LifecycleJobs as b,
464
+ LifecycleEvents as c,
465
+ LifecycleRouteContext as d,
466
+ LifecycleRoutes as f,
467
+ LifecycleJob as g,
468
+ LifecycleStatus as h,
469
+ DurableObjectCapability as i,
470
+ LifecycleHostContextScope as l,
471
+ LifecycleSockets as m,
472
+ CapabilityStartContext as n,
473
+ MemoryLimitContext as o,
474
+ LifecycleServices as p,
475
+ CapabilityWebSocketUpgradeContext as r,
476
+ LifecycleCapability as s,
477
+ CapabilityRequestContext as t,
478
+ LifecycleRouteAddress as u,
479
+ LifecycleJobOutcome as v,
480
+ ConnectionState as w,
481
+ Connection as x,
482
+ LifecycleJobPushOptions as y
483
+ };
484
+ //# sourceMappingURL=capability-runner-BUBa6Ake.d.ts.map