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.
- package/README.md +20 -1
- package/dist/{agent-routing-CnkaHb-v.d.ts → agent-routing-DE5zmCQ8.d.ts} +1038 -394
- package/dist/agent-routing.d.ts +1 -1
- package/dist/agent-tool-types.d.ts +26 -26
- package/dist/{agent-tools-C0XJqWzB.d.ts → agent-tools-DtXMTDGM.d.ts} +7 -7
- package/dist/agent-tools.d.ts +21 -21
- package/dist/browser/ai.js +4 -1
- package/dist/browser/ai.js.map +1 -1
- package/dist/browser/tanstack-ai.js.map +1 -1
- package/dist/callable-decorator-DP__HhBA.d.ts +72 -0
- package/dist/callable-decorator.d.ts +18 -0
- package/dist/callable-decorator.js +71 -0
- package/dist/callable-decorator.js.map +1 -0
- package/dist/capability-BjSKYpzg.js.map +1 -1
- package/dist/capability-runner-Be_-PLR1.d.ts +459 -0
- package/dist/channel-Bnm4S7T2.d.ts +491 -0
- package/dist/channels/ai-sdk.d.ts +46 -0
- package/dist/channels/ai-sdk.js +120 -0
- package/dist/channels/ai-sdk.js.map +1 -0
- package/dist/channels/email.d.ts +95 -0
- package/dist/channels/email.js +323 -0
- package/dist/channels/email.js.map +1 -0
- package/dist/channels/index.d.ts +233 -0
- package/dist/channels/index.js +608 -0
- package/dist/channels/index.js.map +1 -0
- package/dist/channels/slack.d.ts +140 -0
- package/dist/channels/slack.js +614 -0
- package/dist/channels/slack.js.map +1 -0
- package/dist/channels/tanstack-ai.d.ts +39 -0
- package/dist/channels/tanstack-ai.js +17 -0
- package/dist/channels/tanstack-ai.js.map +1 -0
- package/dist/channels/telegram.d.ts +106 -0
- package/dist/channels/telegram.js +427 -0
- package/dist/channels/telegram.js.map +1 -0
- package/dist/channels/voice.d.ts +45 -0
- package/dist/channels/voice.js +122 -0
- package/dist/channels/voice.js.map +1 -0
- package/dist/chat/index.d.ts +2325 -2019
- package/dist/chat/index.js +888 -518
- package/dist/chat/index.js.map +1 -1
- package/dist/chat-sdk/index.d.ts +7 -7
- package/dist/chat-sdk/index.js +1 -1
- package/dist/client.d.ts +1 -1
- package/dist/context/index.d.ts +216 -0
- package/dist/context/index.js +454 -0
- package/dist/context/index.js.map +1 -0
- package/dist/{current-agent-CuMErtly.d.ts → current-agent-Da_C9a3b.d.ts} +90 -107
- package/dist/current-agent-DhoDkSnH.js.map +1 -1
- package/dist/{diagnostics-CaBjfz4J.js → diagnostics-BzvaX2UT.js} +5 -1
- package/dist/diagnostics-BzvaX2UT.js.map +1 -0
- package/dist/diagnostics-C4jcz3VK.js +360 -0
- package/dist/diagnostics-C4jcz3VK.js.map +1 -0
- package/dist/index-BB0kqhIz.d.ts +101 -0
- package/dist/index-BVVgDSdq.d.ts +1 -0
- package/dist/index-XDkuQ7zm.d.ts +89 -0
- package/dist/{index-DcSAZKsB.d.ts → index-YSKgfgg9.d.ts} +3 -1
- package/dist/index.d.ts +91 -82
- package/dist/index.js +3 -2
- package/dist/ingress-BfetZbMO.js +83 -0
- package/dist/ingress-BfetZbMO.js.map +1 -0
- package/dist/internal-CYlgHl1l.js +59 -0
- package/dist/internal-CYlgHl1l.js.map +1 -0
- package/dist/internal_context.d.ts +1 -1
- package/dist/lifecycle/index.d.ts +35 -17
- package/dist/lifecycle/index.js +1 -1
- package/dist/lifecycle-CMRGjZdw.js +1299 -0
- package/dist/lifecycle-CMRGjZdw.js.map +1 -0
- package/dist/mcp/client/index.d.ts +20 -20
- package/dist/mcp/index.d.ts +35 -35
- package/dist/mcp/index.js +1 -1
- package/dist/observability/index.d.ts +1 -1
- package/dist/observability/index.js +1 -1
- package/dist/react.d.ts +4 -4
- package/dist/{retries-CAvxtG9d.d.ts → retries-D9Ds-1lz.d.ts} +17 -6
- package/dist/retries.d.ts +8 -6
- package/dist/retries.js +13 -1
- package/dist/retries.js.map +1 -1
- package/dist/routing/index.d.ts +137 -0
- package/dist/routing/index.js +244 -0
- package/dist/routing/index.js.map +1 -0
- package/dist/sanitize-D9TujEK8.js +79 -0
- package/dist/sanitize-D9TujEK8.js.map +1 -0
- package/dist/scheduler-DD9NdYbF.js +665 -0
- package/dist/scheduler-DD9NdYbF.js.map +1 -0
- package/dist/{scheduler-DQoTGoAW.d.ts → scheduler-Dwh85ZGl.d.ts} +21 -22
- package/dist/schedules/index.d.ts +1 -1
- package/dist/schedules/index.js +1 -1
- package/dist/sentence-chunker-BAidJ4DA.d.ts +68 -0
- package/dist/serializable.d.ts +1 -1
- package/dist/sessions/index.d.ts +441 -0
- package/dist/sessions/index.js +2063 -0
- package/dist/sessions/index.js.map +1 -0
- package/dist/skills/index.d.ts +99 -0
- package/dist/skills/index.js +254 -5
- package/dist/skills/index.js.map +1 -1
- package/dist/{src-5W6JNKVb.js → src-DlSHshb2.js} +1460 -1110
- package/dist/src-DlSHshb2.js.map +1 -0
- package/dist/streams/index.d.ts +120 -0
- package/dist/streams/index.js +107 -0
- package/dist/streams/index.js.map +1 -0
- package/dist/streams-D6tJ0NN9.d.ts +370 -0
- package/dist/streams-DZKgAj9b.js +709 -0
- package/dist/streams-DZKgAj9b.js.map +1 -0
- package/dist/sub-routing.d.ts +12 -12
- package/dist/surface-bZZJqBka.js +17 -0
- package/dist/surface-bZZJqBka.js.map +1 -0
- package/dist/tasks/index.d.ts +64 -0
- package/dist/tasks/index.js +2 -0
- package/dist/tasks-BRJ5zgya.d.ts +517 -0
- package/dist/tasks-ylZgBjhj.js +1656 -0
- package/dist/tasks-ylZgBjhj.js.map +1 -0
- package/dist/text-segment-joiner-BtAFQSA_.js +57 -0
- package/dist/text-segment-joiner-BtAFQSA_.js.map +1 -0
- package/dist/text-stream-CpdiKrJB.js +272 -0
- package/dist/text-stream-CpdiKrJB.js.map +1 -0
- package/dist/tokens-nHAKcN6M.js +52 -0
- package/dist/tokens-nHAKcN6M.js.map +1 -0
- package/dist/tool-schema-CBjGPrsQ.js +31 -0
- package/dist/tool-schema-CBjGPrsQ.js.map +1 -0
- package/dist/types-B7LojTe4.d.ts +202 -0
- package/dist/types-_Faxb570.d.ts +439 -0
- package/dist/voice/client.d.ts +226 -0
- package/dist/voice/client.js +932 -0
- package/dist/voice/client.js.map +1 -0
- package/dist/voice/errors.d.ts +43 -0
- package/dist/voice/errors.js +41 -0
- package/dist/voice/errors.js.map +1 -0
- package/dist/voice/index.d.ts +271 -0
- package/dist/voice/index.js +1812 -0
- package/dist/voice/index.js.map +1 -0
- package/dist/voice/react.d.ts +167 -0
- package/dist/voice/react.js +234 -0
- package/dist/voice/react.js.map +1 -0
- package/dist/voice/sfu.d.ts +71 -0
- package/dist/voice/sfu.js +157 -0
- package/dist/voice/sfu.js.map +1 -0
- package/dist/voice/text.d.ts +6 -0
- package/dist/voice/text.js +2 -0
- package/dist/voice/types.d.ts +58 -0
- package/dist/voice/types.js +18 -0
- package/dist/voice/types.js.map +1 -0
- package/dist/voice/workers-ai.d.ts +136 -0
- package/dist/voice/workers-ai.js +568 -0
- package/dist/voice/workers-ai.js.map +1 -0
- package/dist/websockets/index.d.ts +192 -0
- package/dist/websockets/index.js +2 -0
- package/dist/websockets-DUfRHPRq.js +502 -0
- package/dist/websockets-DUfRHPRq.js.map +1 -0
- package/dist/workflow-types.d.ts +25 -25
- package/dist/workflows.d.ts +21 -21
- package/dist/workflows.js +1 -1
- package/docs/agent-class.md +2 -2
- package/docs/agent-tools.md +2 -1
- package/docs/channels.md +323 -0
- package/docs/chat-agents.md +6 -13
- package/docs/context.md +131 -0
- package/docs/index.md +15 -12
- package/docs/lifecycle.md +102 -55
- package/docs/long-running-agents.md +2 -2
- package/docs/mcp-servers.md +5 -1
- package/docs/resumable-streaming.md +1 -1
- package/docs/routing.md +105 -0
- package/docs/sessions.md +237 -871
- package/docs/streams.md +213 -0
- package/docs/sub-agents.md +184 -124
- package/docs/tasks.md +246 -0
- package/docs/voice.md +745 -0
- package/package.json +115 -13
- package/dist/capability-runner-CvHGZqUu.d.ts +0 -150
- package/dist/compaction-helpers-iiKMr2TQ.js +0 -340
- package/dist/compaction-helpers-iiKMr2TQ.js.map +0 -1
- package/dist/compaction-helpers-wUz6M3us.d.ts +0 -621
- package/dist/diagnostics-CaBjfz4J.js.map +0 -1
- package/dist/durable-object-lifecycle-D6nNQJJd.js +0 -862
- package/dist/durable-object-lifecycle-D6nNQJJd.js.map +0 -1
- package/dist/experimental/memory/session/index.d.ts +0 -671
- package/dist/experimental/memory/session/index.js +0 -2379
- package/dist/experimental/memory/session/index.js.map +0 -1
- package/dist/experimental/memory/utils/index.d.ts +0 -96
- package/dist/experimental/memory/utils/index.js +0 -79
- package/dist/experimental/memory/utils/index.js.map +0 -1
- package/dist/scheduler-CR9RHGos.js +0 -857
- package/dist/scheduler-CR9RHGos.js.map +0 -1
- package/dist/src-5W6JNKVb.js.map +0 -1
- package/dist/tool-output-truncation-CNnnGZQ3.js +0 -98
- package/dist/tool-output-truncation-CNnnGZQ3.js.map +0 -1
|
@@ -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
|