@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 186 lines 5.95 KB
Raw
1 import * as Context from "effect/Context";
2 import * as Duration from "effect/Duration";
3 import * as Effect from "effect/Effect";
4
5 /**
6 * Retry policy for a durable step. Maps onto the Durable Execution SDK's
7 * `retryStrategy` — the delay grows per the configured backoff between
8 * attempts, and the step fails the execution once `limit` retries are
9 * exhausted.
10 */
11 export interface DurableRetryPolicy {
12 /**
13 * Maximum number of RETRIES after the first attempt (a step with
14 * `limit: 2` runs at most 3 times).
15 */
16 limit: number;
17 /**
18 * Delay before the first retry.
19 * @default 1 second
20 */
21 delay?: Duration.Input;
22 /**
23 * Backoff shape applied to `delay` across attempts.
24 * @default "exponential"
25 */
26 backoff?: "constant" | "linear" | "exponential";
27 /**
28 * Upper bound on the computed delay.
29 */
30 maxDelay?: Duration.Input;
31 }
32
33 /**
34 * Configuration for a durable {@link step}.
35 */
36 export interface DurableStepConfig {
37 /**
38 * Retry policy applied when the step's effect dies. Omit for the SDK's
39 * default behavior.
40 */
41 retry?: DurableRetryPolicy;
42 /**
43 * Checkpoint semantics for the step body.
44 *
45 * - `"at-least-once"` (default) — the body may re-run if the invocation is
46 * interrupted before the checkpoint lands; the body must be idempotent.
47 * - `"at-most-once"` — the step is checkpointed as started before the body
48 * runs; an interruption surfaces as a `StepInterruptedError` instead of a
49 * silent re-run.
50 */
51 semantics?: "at-least-once" | "at-most-once";
52 }
53
54 /**
55 * Configuration for {@link waitForCallback}.
56 */
57 export interface DurableCallbackConfig {
58 /**
59 * How long to wait for the external system to complete the callback before
60 * the operation fails.
61 */
62 timeout?: Duration.Input;
63 /**
64 * Maximum silence between `SendDurableExecutionCallbackHeartbeat` calls
65 * before the callback is considered lost.
66 */
67 heartbeatTimeout?: Duration.Input;
68 }
69
70 /**
71 * Internal descriptor passed from {@link step} to the bridge. Bundles the
72 * step name and (fully-provided) Effect together with the step config.
73 */
74 export interface DurableStepOptions<Output = unknown> extends DurableStepConfig {
75 name: string;
76 effect: Effect.Effect<Output>;
77 }
78
79 /**
80 * Internal descriptor passed from {@link waitForCallback} to the bridge.
81 */
82 export interface DurableCallbackOptions extends DurableCallbackConfig {
83 name: string;
84 /**
85 * Runs exactly once (checkpointed) with the callback id; hand the id to the
86 * external system that will complete the callback via
87 * `SendDurableExecutionCallbackSuccess`/`Failure`.
88 */
89 submitter: (callbackId: string) => Effect.Effect<void>;
90 }
91
92 /**
93 * Internal service that wraps the AWS Durable Execution SDK's
94 * `DurableContext`. Not accessed directly by users — use {@link step},
95 * {@link sleep}, and {@link waitForCallback} instead.
96 */
97 export class DurableStep extends Context.Service<
98 DurableStep,
99 {
100 step<T>(options: DurableStepOptions<T>): Effect.Effect<T>;
101 wait(name: string, duration: Duration.Input): Effect.Effect<void>;
102 waitForCallback<T>(options: DurableCallbackOptions): Effect.Effect<T>;
103 }
104 >()("AWS.Lambda.DurableStep") {}
105
106 /**
107 * Runtime information about the current durable execution.
108 * `yield* DurableExecutionContext` inside a durable function body.
109 */
110 export class DurableExecutionContext extends Context.Service<
111 DurableExecutionContext,
112 {
113 /**
114 * ARN uniquely identifying this durable execution instance. Stable across
115 * every suspend/resume invocation of the same execution.
116 */
117 executionArn: string;
118 }
119 >()("AWS.Lambda.DurableExecutionContext") {}
120
121 /**
122 * Execute a named, durable step. The effect runs inside the AWS Durable
123 * Execution checkpoint protocol: its result is persisted after first
124 * completion and replayed (without re-executing) on every subsequent
125 * invocation of the execution.
126 *
127 * Any services the inner effect requires (e.g. binding clients resolved in
128 * the durable function's init phase) are threaded through automatically by
129 * capturing the surrounding body's context and providing it to the inner
130 * effect before it is handed to the SDK.
131 *
132 * Determinism law: all effectful/non-deterministic work (time, randomness,
133 * I/O, SDK calls) must live INSIDE a step — code between steps re-runs on
134 * every replay and must be a pure function of the input and prior step
135 * results.
136 */
137 export function step<T, R = never>(
138 name: string,
139 effect: Effect.Effect<T, never, R>,
140 config?: DurableStepConfig,
141 ): Effect.Effect<T, never, DurableStep | R> {
142 return Effect.gen(function* () {
143 const durable = yield* DurableStep;
144 const context = yield* Effect.context<R>();
145 return yield* durable.step({
146 ...config,
147 name,
148 effect: effect.pipe(Effect.provide(context)),
149 });
150 });
151 }
152
153 /**
154 * Pause the durable execution for the given duration. The invocation is
155 * checkpointed and terminated — no compute is billed while suspended — and
156 * Lambda re-invokes the function when the timer fires.
157 */
158 export const sleep = (
159 name: string,
160 duration: Duration.Input,
161 ): Effect.Effect<void, never, DurableStep> =>
162 Effect.gen(function* () {
163 const durable = yield* DurableStep;
164 yield* durable.wait(name, duration);
165 });
166
167 /**
168 * Suspend the durable execution until an external system completes the
169 * callback via `SendDurableExecutionCallbackSuccess` (or fails it via
170 * `...Failure`). The `submitter` effect runs exactly once (checkpointed) and
171 * receives the callback id to hand to the external system.
172 */
173 export const waitForCallback = <T = unknown, R = never>(
174 name: string,
175 submitter: (callbackId: string) => Effect.Effect<void, never, R>,
176 config?: DurableCallbackConfig,
177 ): Effect.Effect<T, never, DurableStep | R> =>
178 Effect.gen(function* () {
179 const durable = yield* DurableStep;
180 const context = yield* Effect.context<R>();
181 return yield* durable.waitForCallback<T>({
182 ...config,
183 name,
184 submitter: (callbackId) => submitter(callbackId).pipe(Effect.provide(context)),
185 });
186 });