agents 0.21.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 (258) hide show
  1. package/README.md +21 -1
  2. package/dist/{agent-tool-types-CzGGB-20.d.ts → agent-routing-DE5zmCQ8.d.ts} +1844 -1336
  3. package/dist/agent-routing.d.ts +14 -0
  4. package/dist/agent-routing.js +187 -0
  5. package/dist/agent-routing.js.map +1 -0
  6. package/dist/agent-tool-types.d.ts +26 -26
  7. package/dist/{agent-tools-zR2d5uij.d.ts → agent-tools-DtXMTDGM.d.ts} +7 -7
  8. package/dist/agent-tools.d.ts +21 -21
  9. package/dist/agent-tools.js +2 -1
  10. package/dist/agent-tools.js.map +1 -1
  11. package/dist/browser/ai.js +6 -2
  12. package/dist/browser/ai.js.map +1 -1
  13. package/dist/browser/tanstack-ai.js.map +1 -1
  14. package/dist/callable-decorator-DP__HhBA.d.ts +72 -0
  15. package/dist/callable-decorator.d.ts +18 -0
  16. package/dist/callable-decorator.js +71 -0
  17. package/dist/callable-decorator.js.map +1 -0
  18. package/dist/capability-BjSKYpzg.js +42 -0
  19. package/dist/capability-BjSKYpzg.js.map +1 -0
  20. package/dist/capability-runner-Be_-PLR1.d.ts +459 -0
  21. package/dist/channel-Bnm4S7T2.d.ts +491 -0
  22. package/dist/channels/ai-sdk.d.ts +46 -0
  23. package/dist/channels/ai-sdk.js +120 -0
  24. package/dist/channels/ai-sdk.js.map +1 -0
  25. package/dist/channels/email.d.ts +95 -0
  26. package/dist/channels/email.js +323 -0
  27. package/dist/channels/email.js.map +1 -0
  28. package/dist/channels/index.d.ts +233 -0
  29. package/dist/channels/index.js +608 -0
  30. package/dist/channels/index.js.map +1 -0
  31. package/dist/channels/slack.d.ts +140 -0
  32. package/dist/channels/slack.js +614 -0
  33. package/dist/channels/slack.js.map +1 -0
  34. package/dist/channels/tanstack-ai.d.ts +39 -0
  35. package/dist/channels/tanstack-ai.js +17 -0
  36. package/dist/channels/tanstack-ai.js.map +1 -0
  37. package/dist/channels/telegram.d.ts +106 -0
  38. package/dist/channels/telegram.js +427 -0
  39. package/dist/channels/telegram.js.map +1 -0
  40. package/dist/channels/voice.d.ts +45 -0
  41. package/dist/channels/voice.js +122 -0
  42. package/dist/channels/voice.js.map +1 -0
  43. package/dist/chat/index.d.ts +2328 -2015
  44. package/dist/chat/index.js +891 -521
  45. package/dist/chat/index.js.map +1 -1
  46. package/dist/chat/react.d.ts +14 -1
  47. package/dist/chat/react.js +82 -52
  48. package/dist/chat/react.js.map +1 -1
  49. package/dist/chat/transport.js +1 -1
  50. package/dist/chat-sdk/index.d.ts +7 -7
  51. package/dist/chat-sdk/index.js +1 -1
  52. package/dist/{client-zqKcsyFa.js → client-jagG8a9_.js} +129 -37
  53. package/dist/client-jagG8a9_.js.map +1 -0
  54. package/dist/client.d.ts +1 -1
  55. package/dist/client.js +1 -1
  56. package/dist/{cloudflare-BduZwmYK.js → cloudflare-Dzvc7V2N.js} +10 -3
  57. package/dist/{cloudflare-BduZwmYK.js.map → cloudflare-Dzvc7V2N.js.map} +1 -1
  58. package/dist/context/index.d.ts +216 -0
  59. package/dist/context/index.js +454 -0
  60. package/dist/context/index.js.map +1 -0
  61. package/dist/current-agent-Da_C9a3b.d.ts +266 -0
  62. package/dist/current-agent-DhoDkSnH.js +51 -0
  63. package/dist/current-agent-DhoDkSnH.js.map +1 -0
  64. package/dist/diagnostics-BzvaX2UT.js +45 -0
  65. package/dist/diagnostics-BzvaX2UT.js.map +1 -0
  66. package/dist/diagnostics-C4jcz3VK.js +360 -0
  67. package/dist/diagnostics-C4jcz3VK.js.map +1 -0
  68. package/dist/{do-oauth-client-provider-VTZj2VtM.d.ts → do-oauth-client-provider-Tmf1vgKz.d.ts} +2 -2
  69. package/dist/{email-CL27preh.d.ts → email-7TatiTnl.d.ts} +38 -9
  70. package/dist/email-send.d.ts +15 -0
  71. package/dist/email-send.js +32 -0
  72. package/dist/email-send.js.map +1 -0
  73. package/dist/email.d.ts +14 -10
  74. package/dist/email.js.map +1 -1
  75. package/dist/{handler-stateless-C_bo-Ytq.d.ts → handler-stateless-DxYpJ_XF.d.ts} +3 -3
  76. package/dist/{handler-stateless-CIkKPETH.js → handler-stateless-VvrWSAVA.js} +5 -5
  77. package/dist/handler-stateless-VvrWSAVA.js.map +1 -0
  78. package/dist/index-BB0kqhIz.d.ts +101 -0
  79. package/dist/index-XDkuQ7zm.d.ts +89 -0
  80. package/dist/{index-BRnybD6X.d.ts → index-YSKgfgg9.d.ts} +21 -31
  81. package/dist/index.d.ts +113 -101
  82. package/dist/index.js +11 -7234
  83. package/dist/ingress-BfetZbMO.js +83 -0
  84. package/dist/ingress-BfetZbMO.js.map +1 -0
  85. package/dist/internal-CYlgHl1l.js +59 -0
  86. package/dist/internal-CYlgHl1l.js.map +1 -0
  87. package/dist/internal_context-BlxFEWfn.d.ts +19 -0
  88. package/dist/internal_context.d.ts +10 -4
  89. package/dist/internal_context.js +1 -10
  90. package/dist/{client-invoker-BNSZxAkv.d.ts → invoker-CG0_p_Wq.d.ts} +2 -2
  91. package/dist/{client-invoker-VNZ7X0nn.js → invoker-CHMnoxIA.js} +2 -2
  92. package/dist/invoker-CHMnoxIA.js.map +1 -0
  93. package/dist/lifecycle/index.d.ts +66 -0
  94. package/dist/lifecycle/index.js +4 -0
  95. package/dist/lifecycle-CMRGjZdw.js +1299 -0
  96. package/dist/lifecycle-CMRGjZdw.js.map +1 -0
  97. package/dist/mcp/{do-oauth-client-provider.d.ts → client/do-oauth-client-provider.d.ts} +1 -1
  98. package/dist/mcp/{do-oauth-client-provider.js → client/do-oauth-client-provider.js} +1 -1
  99. package/dist/mcp/client/do-oauth-client-provider.js.map +1 -0
  100. package/dist/mcp/client/index.d.ts +42 -0
  101. package/dist/mcp/{client.js → client/index.js} +1 -1
  102. package/dist/mcp/{x402.d.ts → client/x402.d.ts} +2 -2
  103. package/dist/mcp/{x402.js → client/x402.js} +2 -2
  104. package/dist/mcp/client/x402.js.map +1 -0
  105. package/dist/mcp/index.d.ts +36 -36
  106. package/dist/mcp/index.js +14 -16
  107. package/dist/mcp/index.js.map +1 -1
  108. package/dist/mcp/{server.d.ts → server/index.d.ts} +1 -1
  109. package/dist/mcp/{server.js → server/index.js} +1 -1
  110. package/dist/observability/ai/index.js +50 -35
  111. package/dist/observability/ai/index.js.map +1 -1
  112. package/dist/observability/index.d.ts +4 -4
  113. package/dist/observability/index.js +3 -50
  114. package/dist/observability/index.js.map +1 -1
  115. package/dist/{protocol-Dqc2MQxo.js → protocol-B0nh6KNf.js} +19 -21
  116. package/dist/protocol-B0nh6KNf.js.map +1 -0
  117. package/dist/react.d.ts +4 -4
  118. package/dist/react.js +1 -1
  119. package/dist/{retries-CAvxtG9d.d.ts → retries-D9Ds-1lz.d.ts} +17 -6
  120. package/dist/retries.d.ts +8 -6
  121. package/dist/retries.js +13 -1
  122. package/dist/retries.js.map +1 -1
  123. package/dist/routing/index.d.ts +137 -0
  124. package/dist/routing/index.js +244 -0
  125. package/dist/routing/index.js.map +1 -0
  126. package/dist/sanitize-D9TujEK8.js +79 -0
  127. package/dist/sanitize-D9TujEK8.js.map +1 -0
  128. package/dist/schedule.d.ts +25 -94
  129. package/dist/schedule.js +1 -98
  130. package/dist/schedule.js.map +1 -1
  131. package/dist/scheduler-DD9NdYbF.js +665 -0
  132. package/dist/scheduler-DD9NdYbF.js.map +1 -0
  133. package/dist/scheduler-Dwh85ZGl.d.ts +223 -0
  134. package/dist/schedules/index.d.ts +22 -0
  135. package/dist/schedules/index.js +2 -0
  136. package/dist/schedules/parser.d.ts +79 -0
  137. package/dist/schedules/parser.js +103 -0
  138. package/dist/schedules/parser.js.map +1 -0
  139. package/dist/sentence-chunker-BAidJ4DA.d.ts +68 -0
  140. package/dist/serializable.d.ts +1 -1
  141. package/dist/sessions/index.d.ts +441 -0
  142. package/dist/sessions/index.js +2063 -0
  143. package/dist/sessions/index.js.map +1 -0
  144. package/dist/skills/index.d.ts +99 -0
  145. package/dist/skills/index.js +254 -5
  146. package/dist/skills/index.js.map +1 -1
  147. package/dist/sql-error-CPY-GXyI.d.ts +12 -0
  148. package/dist/sql-error.d.ts +2 -0
  149. package/dist/sql-error.js +16 -0
  150. package/dist/sql-error.js.map +1 -0
  151. package/dist/src-DlSHshb2.js +6963 -0
  152. package/dist/src-DlSHshb2.js.map +1 -0
  153. package/dist/streams/index.d.ts +120 -0
  154. package/dist/streams/index.js +107 -0
  155. package/dist/streams/index.js.map +1 -0
  156. package/dist/streams-D6tJ0NN9.d.ts +370 -0
  157. package/dist/streams-DZKgAj9b.js +709 -0
  158. package/dist/streams-DZKgAj9b.js.map +1 -0
  159. package/dist/sub-routing.d.ts +12 -12
  160. package/dist/surface-bZZJqBka.js +17 -0
  161. package/dist/surface-bZZJqBka.js.map +1 -0
  162. package/dist/tasks/index.d.ts +64 -0
  163. package/dist/tasks/index.js +2 -0
  164. package/dist/tasks-BRJ5zgya.d.ts +517 -0
  165. package/dist/tasks-ylZgBjhj.js +1656 -0
  166. package/dist/tasks-ylZgBjhj.js.map +1 -0
  167. package/dist/text-segment-joiner-BtAFQSA_.js +57 -0
  168. package/dist/text-segment-joiner-BtAFQSA_.js.map +1 -0
  169. package/dist/text-stream-CpdiKrJB.js +272 -0
  170. package/dist/text-stream-CpdiKrJB.js.map +1 -0
  171. package/dist/tokens-nHAKcN6M.js +52 -0
  172. package/dist/tokens-nHAKcN6M.js.map +1 -0
  173. package/dist/tool-schema-CBjGPrsQ.js +31 -0
  174. package/dist/tool-schema-CBjGPrsQ.js.map +1 -0
  175. package/dist/types-B7LojTe4.d.ts +202 -0
  176. package/dist/types-_Faxb570.d.ts +439 -0
  177. package/dist/voice/client.d.ts +226 -0
  178. package/dist/voice/client.js +932 -0
  179. package/dist/voice/client.js.map +1 -0
  180. package/dist/voice/errors.d.ts +43 -0
  181. package/dist/voice/errors.js +41 -0
  182. package/dist/voice/errors.js.map +1 -0
  183. package/dist/voice/index.d.ts +271 -0
  184. package/dist/voice/index.js +1812 -0
  185. package/dist/voice/index.js.map +1 -0
  186. package/dist/voice/react.d.ts +167 -0
  187. package/dist/voice/react.js +234 -0
  188. package/dist/voice/react.js.map +1 -0
  189. package/dist/voice/sfu.d.ts +71 -0
  190. package/dist/voice/sfu.js +157 -0
  191. package/dist/voice/sfu.js.map +1 -0
  192. package/dist/voice/text.d.ts +6 -0
  193. package/dist/voice/text.js +2 -0
  194. package/dist/voice/types.d.ts +58 -0
  195. package/dist/voice/types.js +18 -0
  196. package/dist/voice/types.js.map +1 -0
  197. package/dist/voice/workers-ai.d.ts +136 -0
  198. package/dist/voice/workers-ai.js +568 -0
  199. package/dist/voice/workers-ai.js.map +1 -0
  200. package/dist/websockets/index.d.ts +192 -0
  201. package/dist/websockets/index.js +2 -0
  202. package/dist/websockets-DUfRHPRq.js +502 -0
  203. package/dist/websockets-DUfRHPRq.js.map +1 -0
  204. package/dist/workflow-types.d.ts +25 -25
  205. package/dist/workflows.d.ts +22 -22
  206. package/dist/workflows.js +2 -1
  207. package/dist/workflows.js.map +1 -1
  208. package/dist/{ws-chat-transport-CIoOBbO7.js → ws-chat-transport-rWwta645.js} +152 -15
  209. package/dist/ws-chat-transport-rWwta645.js.map +1 -0
  210. package/docs/agent-class.md +29 -87
  211. package/docs/agent-tools.md +2 -1
  212. package/docs/channels.md +323 -0
  213. package/docs/chat-agents.md +19 -25
  214. package/docs/context.md +131 -0
  215. package/docs/durable-execution.md +1 -1
  216. package/docs/http-websockets.md +1 -11
  217. package/docs/human-in-the-loop.md +1 -1
  218. package/docs/index.md +16 -12
  219. package/docs/lifecycle.md +370 -0
  220. package/docs/long-running-agents.md +4 -6
  221. package/docs/mcp-client.md +55 -0
  222. package/docs/mcp-servers.md +5 -1
  223. package/docs/observability.md +11 -11
  224. package/docs/resumable-streaming.md +2 -2
  225. package/docs/routing.md +105 -0
  226. package/docs/scheduling.md +175 -15
  227. package/docs/server-driven-messages.md +1 -1
  228. package/docs/sessions.md +237 -871
  229. package/docs/streams.md +213 -0
  230. package/docs/sub-agents.md +185 -125
  231. package/docs/tasks.md +246 -0
  232. package/docs/voice.md +745 -0
  233. package/package.json +144 -33
  234. package/dist/cli/index.js +0 -26
  235. package/dist/cli/index.js.map +0 -1
  236. package/dist/client-invoker-VNZ7X0nn.js.map +0 -1
  237. package/dist/client-zqKcsyFa.js.map +0 -1
  238. package/dist/compaction-helpers-iiKMr2TQ.js +0 -340
  239. package/dist/compaction-helpers-iiKMr2TQ.js.map +0 -1
  240. package/dist/compaction-helpers-wUz6M3us.d.ts +0 -621
  241. package/dist/experimental/memory/session/index.d.ts +0 -670
  242. package/dist/experimental/memory/session/index.js +0 -2374
  243. package/dist/experimental/memory/session/index.js.map +0 -1
  244. package/dist/experimental/memory/utils/index.d.ts +0 -96
  245. package/dist/experimental/memory/utils/index.js +0 -79
  246. package/dist/experimental/memory/utils/index.js.map +0 -1
  247. package/dist/handler-stateless-CIkKPETH.js.map +0 -1
  248. package/dist/index.js.map +0 -1
  249. package/dist/internal_context-Dg4Cgjcu.d.ts +0 -37
  250. package/dist/internal_context.js.map +0 -1
  251. package/dist/mcp/client.d.ts +0 -42
  252. package/dist/mcp/do-oauth-client-provider.js.map +0 -1
  253. package/dist/mcp/x402.js.map +0 -1
  254. package/dist/protocol-Dqc2MQxo.js.map +0 -1
  255. package/dist/tool-output-truncation-CNnnGZQ3.js +0 -98
  256. package/dist/tool-output-truncation-CNnnGZQ3.js.map +0 -1
  257. package/dist/ws-chat-transport-CIoOBbO7.js.map +0 -1
  258. /package/dist/{cli/index.d.ts → index-BVVgDSdq.d.ts} +0 -0
@@ -0,0 +1,459 @@
1
+ import { t as RetryOptions } from "./retries-D9Ds-1lz.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
+ /** Best-effort telemetry available to every Lifecycle capability. */
178
+ type LifecycleEvents = {
179
+ /** Publish an event under this capability's stable identity. */ readonly emit: (
180
+ type: string,
181
+ payload: unknown
182
+ ) => void;
183
+ };
184
+ /** Routing available to every Lifecycle capability. */
185
+ type LifecycleRoutes = {
186
+ /** This Lifecycle's transport address, or undefined at the route root. */ readonly source:
187
+ | LifecycleRouteAddress
188
+ | undefined /** Route a capability-owned message to the root Lifecycle. */;
189
+ readonly toRoot: (
190
+ payload: unknown
191
+ ) => Promise<unknown> /** Route a capability-owned message to another Lifecycle. */;
192
+ readonly to: (
193
+ target: LifecycleRouteAddress,
194
+ payload: unknown
195
+ ) => Promise<unknown>;
196
+ };
197
+ /**
198
+ * Ambient scope a capability supplies when entering host context on
199
+ * behalf of a live connection or request.
200
+ */
201
+ type LifecycleHostContextScope = {
202
+ /** 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. */;
203
+ readonly request?: Request;
204
+ };
205
+ /**
206
+ * The platform's hibernatable-socket surface, exposed narrowly so a
207
+ * capability that owns connections (e.g. WebSockets) can accept and
208
+ * enumerate them without holding the whole `DurableObjectState`.
209
+ * These are workerd API names, not Lifecycle modeling sockets.
210
+ */
211
+ type LifecycleSockets = {
212
+ /** Accept a socket into hibernation under the given tags. */ readonly accept: (
213
+ ws: WebSocket,
214
+ tags: string[]
215
+ ) => void /** Every hibernated socket on the object, optionally by tag. */;
216
+ readonly get: (tag?: string) => WebSocket[];
217
+ };
218
+ /**
219
+ * Standard services granted to every installed Lifecycle capability.
220
+ *
221
+ * @experimental The API surface may change before stabilizing.
222
+ */
223
+ type LifecycleServices = {
224
+ readonly storage: DurableObjectStorage;
225
+ readonly sockets: LifecycleSockets;
226
+ readonly ready: () => Promise<void> /** True while capability and host startup hooks are still running. */;
227
+ readonly starting: () => boolean;
228
+ /**
229
+ * This capability's scoped access to the Lifecycle-owned work queue.
230
+ * Pushed items are dispatched to `onJob` when due; every queue mutation
231
+ * re-arms the physical alarm automatically.
232
+ */
233
+ readonly jobs: LifecycleJobs;
234
+ /**
235
+ * Keep work this capability hands off at a bounded `onJob` return inside
236
+ * the current alarm's memory-limit breaker domain (#1825). Returns false,
237
+ * tracking nothing, outside an alarm invocation.
238
+ */
239
+ readonly trackAlarmWork: (work: Promise<unknown>) => boolean;
240
+ /**
241
+ * Run a capability-held user callback inside the host invocation context.
242
+ * Capability hooks run outside host context; this is the one boundary for
243
+ * entering it, and a host composition root may substitute its own wrapper
244
+ * (Agent adds tracing span scope). Pass `scope` to make a live
245
+ * connection or request ambient for the callback.
246
+ */
247
+ readonly runInHostContext: (
248
+ fn: () => unknown,
249
+ scope?: LifecycleHostContextScope
250
+ ) => Promise<unknown>;
251
+ readonly events: LifecycleEvents;
252
+ readonly routes: LifecycleRoutes;
253
+ };
254
+ /**
255
+ * Base class for capabilities that consume standard Lifecycle services.
256
+ *
257
+ * @experimental The API surface may change before stabilizing.
258
+ */
259
+ declare abstract class LifecycleCapability<Props extends object = object> {
260
+ readonly capabilityId: string;
261
+ protected constructor(capabilityId: string);
262
+ /** Default startup hook; capabilities override when they own startup work. */
263
+ onStart(_context: CapabilityStartContext<Props>): void;
264
+ /** Standard services when installed, or undefined in isolated unit tests. */
265
+ protected get lifecycleServices(): LifecycleServices | undefined;
266
+ /** Standard services supplied when Lifecycle installs this capability. */
267
+ protected get lifecycle(): LifecycleServices;
268
+ }
269
+ //#endregion
270
+ //#region src/lifecycle/capability-runner.d.ts
271
+ type MaybePromise<T> = T | Promise<T>;
272
+ /** One best-effort event published by a Lifecycle capability. */
273
+ type LifecycleEvent = {
274
+ /** Stable capability or subsystem name. */ readonly source: string /** Stable event name within that source. */;
275
+ readonly type: string /** Event-specific data. */;
276
+ readonly payload: unknown;
277
+ };
278
+ /** Context supplied when durable capabilities start. */
279
+ type CapabilityStartContext<Props extends object = object> = {
280
+ /** Properties supplied while resolving the Durable Object. */ readonly props:
281
+ | Props
282
+ | undefined;
283
+ };
284
+ /** Context supplied when durable capabilities inspect an HTTP request. */
285
+ type CapabilityRequestContext = {
286
+ /** The request entering the Durable Object. */ readonly request: Request;
287
+ };
288
+ /** Context supplied when a capability inspects a WebSocket upgrade. */
289
+ type CapabilityWebSocketUpgradeContext = {
290
+ /** The WebSocket upgrade request entering the Durable Object. */ readonly request: Request;
291
+ };
292
+ /**
293
+ * Context supplied when the alarm memory-limit circuit breaker records a
294
+ * strike (#1825), to capabilities and the host alike.
295
+ */
296
+ type MemoryLimitContext = {
297
+ /** 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. */;
298
+ readonly nextTime?: number;
299
+ /**
300
+ * The queue job that was executing when the strike landed. Absent when the
301
+ * reset happened during startup or host alarm work. Lifecycle has already
302
+ * backed off or purged its row before invoking memory-limit policy.
303
+ *
304
+ * Capabilities whose durable state outlives their queue row use this to
305
+ * apply the same policy to the underlying work — Tasks routes a struck
306
+ * routed job's owner this way (see `setTaskRoutedMemoryLimitHandler`).
307
+ */
308
+ readonly executing?: LifecycleJob;
309
+ /**
310
+ * Recovery-loop jobs removed when this strike sealed the breaker. This is
311
+ * a pre-purge snapshot because the durable rows no longer exist when
312
+ * policy hooks run. No current capability reads this — Scheduler's own
313
+ * former use of it (routing a sealed strike to every purged row's owning
314
+ * dynamic agent) was retired once Tasks took over routed chat-recovery
315
+ * wakes — but it stays available for a capability whose routed rows can
316
+ * be purged as a pack, unlike Tasks' one-run-at-a-time model.
317
+ */
318
+ readonly purgedRecoveryLoopJobs?: ReadonlyArray<LifecycleJob>;
319
+ };
320
+ /**
321
+ * A capability installed into a Durable Object lifecycle.
322
+ *
323
+ * Capabilities extending `LifecycleCapability` receive the standard storage,
324
+ * readiness, alarm, event, and routing surface. Host-specific bindings and
325
+ * protocol adapters remain explicit constructor dependencies. Hook parameters
326
+ * carry only phase data; hooks do not run in ambient host context — a
327
+ * capability-held user callback re-enters host context exactly once,
328
+ * through `LifecycleServices.runInHostContext(fn, scope)`.
329
+ *
330
+ * A capability interacts with Lifecycle through exactly three channels:
331
+ * these declared hooks, the `LifecycleServices` surface, and
332
+ * composition-root `set*()` apertures. Any other direct reach in either
333
+ * direction is a design smell.
334
+ *
335
+ * Dispatch contract, hook by hook:
336
+ * - `onRequest` and `onWebSocketUpgrade` are offered in declaration
337
+ * order, fallbacks last; the first capability to return a `Response`
338
+ * claims the request, and a claimed upgrade's socket belongs to that
339
+ * capability for its whole lifetime.
340
+ * - `onWebSocketMessage`/`onWebSocketClose`/`onWebSocketError` are
341
+ * platform wakes, offered in declaration order; return `true` to
342
+ * consume one. Socket ownership is the capability's to determine —
343
+ * keep a private hibernation-attachment namespace and recognize your
344
+ * own sockets by it.
345
+ * - `onRoute` is addressed to one capability by its id; `onJob` is
346
+ * addressed by the due job's owning capability, with Lifecycle owning
347
+ * the queue and the one physical alarm.
348
+ *
349
+ * @experimental The API surface may change before stabilizing.
350
+ */
351
+ interface DurableObjectCapability<Props extends object = object> {
352
+ /** Initialize or recover the capability before the host handles work. */
353
+ onStart?(context: CapabilityStartContext<Props>): MaybePromise<void>;
354
+ /**
355
+ * Act as middleware over HTTP requests, ahead of the host's request handler.
356
+ *
357
+ * Return a response to handle the request, or `undefined` to pass it to the
358
+ * next capability and finally the host.
359
+ */
360
+ onRequest?(
361
+ context: CapabilityRequestContext
362
+ ): MaybePromise<Response | undefined | void>;
363
+ /**
364
+ * Claim a WebSocket upgrade before the host's legacy connection path.
365
+ *
366
+ * A capability that returns a response owns that socket and its lifetime,
367
+ * including any hibernation attachment it needs to recognize the socket
368
+ * later. Return `undefined` to decline.
369
+ */
370
+ onWebSocketUpgrade?(
371
+ context: CapabilityWebSocketUpgradeContext
372
+ ): MaybePromise<Response | undefined | void>;
373
+ /**
374
+ * Handle a platform `webSocketMessage` wake for a socket this capability
375
+ * owns. Return `true` to consume the event; anything else offers it to
376
+ * the next capability and finally the host's legacy path. Ownership is
377
+ * the capability's to determine — typically via its own hibernation
378
+ * attachment namespace.
379
+ */
380
+ onWebSocketMessage?(
381
+ ws: WebSocket,
382
+ message: WSMessage
383
+ ): MaybePromise<boolean | void>;
384
+ /** Handle a platform `webSocketClose` wake for an owned socket. */
385
+ onWebSocketClose?(
386
+ ws: WebSocket,
387
+ code: number,
388
+ reason: string,
389
+ wasClean: boolean
390
+ ): MaybePromise<boolean | void>;
391
+ /** Handle a platform `webSocketError` wake for an owned socket. */
392
+ onWebSocketError?(
393
+ ws: WebSocket,
394
+ error: unknown
395
+ ): MaybePromise<boolean | void>;
396
+ /**
397
+ * Drive one due job this capability pushed into the Lifecycle queue.
398
+ * Return an outcome to reschedule or retain the job; returning nothing
399
+ * completes it.
400
+ *
401
+ * Dispatch must be bounded: the event loop awaits each job inline, so a
402
+ * long-running `onJob` delays every other job on this object. Detach
403
+ * unbounded work (start it, keep durable evidence, return) instead of
404
+ * awaiting it here; a dispatch that outlives the job's hung timeout
405
+ * logs a warning and emits `job:slow_dispatch`.
406
+ */
407
+ onJob?(
408
+ context: LifecycleJobContext
409
+ ): MaybePromise<LifecycleJobOutcome | void>;
410
+ /**
411
+ * Observe one job's terminal application failure after retry exhaustion.
412
+ * The returned outcome decides advancement; returning nothing completes
413
+ * the job.
414
+ */
415
+ onJobError?(
416
+ context: LifecycleJobContext,
417
+ error: unknown
418
+ ): MaybePromise<LifecycleJobOutcome | void>;
419
+ /**
420
+ * Apply domain policy after the alarm memory-limit circuit breaker
421
+ * records a strike (#1825). Lifecycle has already handled the queue: the
422
+ * executing job and every `recoveryLoop`-flagged job are backed off (or
423
+ * purged when `sealed`). Runs at the outermost alarm frame, post-unwind,
424
+ * best-effort — the isolate resets right after, so keep writes small.
425
+ */
426
+ onMemoryLimit?(context: MemoryLimitContext): MaybePromise<void>;
427
+ /** Handle one message routed to this capability identity. */
428
+ onRoute?(context: LifecycleRouteContext): MaybePromise<unknown>;
429
+ /** Release live or in-memory resources during explicit host destruction. */
430
+ dispose?(): MaybePromise<void>;
431
+ }
432
+ //#endregion
433
+ export {
434
+ ConnectionState as C,
435
+ ConnectionSetStateFn as S,
436
+ LifecycleJobOutcome as _,
437
+ LifecycleEvent as a,
438
+ Connection as b,
439
+ LifecycleEvents as c,
440
+ LifecycleRouteContext as d,
441
+ LifecycleRoutes as f,
442
+ LifecycleJobContext as g,
443
+ LifecycleJob as h,
444
+ DurableObjectCapability as i,
445
+ LifecycleHostContextScope as l,
446
+ LifecycleSockets as m,
447
+ CapabilityStartContext as n,
448
+ MemoryLimitContext as o,
449
+ LifecycleServices as p,
450
+ CapabilityWebSocketUpgradeContext as r,
451
+ LifecycleCapability as s,
452
+ CapabilityRequestContext as t,
453
+ LifecycleRouteAddress as u,
454
+ LifecycleJobPushOptions as v,
455
+ WSMessage as w,
456
+ ConnectionContext as x,
457
+ LifecycleJobs as y
458
+ };
459
+ //# sourceMappingURL=capability-runner-Be_-PLR1.d.ts.map