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

main
ts 56 lines 2.22 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import { RuntimeContext } from "alchemy/RuntimeContext";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import path from "pathe";
6
7 const main = path.resolve(import.meta.dirname, "durable-handler.ts");
8
9 /**
10 * A three-operation durable orchestration: two checkpointed steps around a
11 * durable sleep. The sleep suspends the execution (the invocation returns
12 * PENDING) and Lambda re-invokes the function to resume, so a SUCCEEDED
13 * result proves the full checkpoint/replay round-trip:
14 * invoke #1 — step "reserve" checkpoints, wait checkpoints, suspend
15 * invoke #2 — replay memoizes "reserve", runs "price", completes
16 *
17 * A DurableFunction IS the Lambda function (wrapper of AWS.Lambda.Function):
18 * the wrapper sets `DurableConfig` at create time, disables the Function URL,
19 * self-binds the checkpoint-protocol IAM, and vendors
20 * `@aws/durable-execution-sdk-js` into the artifact automatically.
21 */
22 export class DurableFlow extends Lambda.DurableFunction<DurableFlow>()("DurableFlow") {}
23
24 export default DurableFlow.make(
25 {
26 main,
27 executionTimeout: Duration.hours(1),
28 retentionPeriod: "1 day",
29 // Per-invocation budget (each resume is its own invocation).
30 timeout: Duration.seconds(60),
31 },
32 Effect.gen(function* () {
33 return Effect.fn(function* (input: { orderId: string }) {
34 const reserved = yield* Lambda.Durable.step(
35 "reserve",
36 Effect.succeed({ orderId: input.orderId, reserved: true }),
37 );
38 yield* Lambda.Durable.sleep("cooldown", "5 seconds");
39 const total = yield* Lambda.Durable.step(
40 "price",
41 // Non-deterministic work belongs inside a step: the checkpointed
42 // result replays identically on the resume invocation.
43 Effect.sync(() => 40 + 2),
44 );
45 // Runtime-only code (binding clients, services colored with
46 // `RuntimeContext`) must be callable from the durable body.
47 const runtime = yield* Lambda.Durable.step(
48 "runtime",
49 Effect.gen(function* () {
50 return (yield* RuntimeContext).Type;
51 }),
52 );
53 return { orderId: reserved.orderId, reserved: reserved.reserved, total, runtime };
54 });
55 }),
56 );