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

main
ts 131 lines 4.73 KB
Raw
1 import type * as Effect from "effect/Effect";
2 import type * as Stream from "effect/Stream";
3 import {
4 consumeBusEvents,
5 type EventRecord,
6 type EventRouteProps,
7 } from "../EventBridge/EventSource.ts";
8 import type { StateMachine } from "./StateMachine.ts";
9
10 /**
11 * The `detail` payload Step Functions delivers to EventBridge when an
12 * execution changes status (`Step Functions Execution Status Change`).
13 * Fields not shared by every status are optional (the schema grows over
14 * time).
15 */
16 export interface ExecutionEventDetail {
17 /** The execution's ARN (`arn:…:execution:{machine}:{name}`). */
18 executionArn?: string;
19 /** ARN of the state machine the execution belongs to. */
20 stateMachineArn?: string;
21 /** The execution name. */
22 name?: string;
23 /** The new status: `RUNNING`, `SUCCEEDED`, `FAILED`, `TIMED_OUT`, `ABORTED`. */
24 status?: string;
25 /** Epoch millis the execution started. */
26 startDate?: number;
27 /** Epoch millis the execution stopped (terminal statuses only). */
28 stopDate?: number | null;
29 /** The execution input (JSON string), when execution data is included. */
30 input?: string | null;
31 /** The execution output (JSON string) on `SUCCEEDED`, else `null`. */
32 output?: string | null;
33 /** The `Error` name on `FAILED`/`TIMED_OUT`, when present. */
34 error?: string | null;
35 /** The `Cause` string on `FAILED`/`TIMED_OUT`, when present. */
36 cause?: string | null;
37 /** Additional event fields (the schema grows over time). */
38 [key: string]: unknown;
39 }
40
41 /** A Step Functions EventBridge event delivered to the handler. */
42 export type ExecutionEvent = EventRecord<ExecutionEventDetail>;
43
44 /** An execution status a subscription can filter on. */
45 export type ExecutionEventStatus = "RUNNING" | "SUCCEEDED" | "FAILED" | "TIMED_OUT" | "ABORTED";
46
47 export interface ExecutionEventSourceProps extends EventRouteProps {
48 /**
49 * Logical id for the backing EventBridge rule.
50 * @default "SfnExecutionEvents"
51 */
52 id?: string;
53 /**
54 * Restrict the subscription to executions of specific state machines.
55 * Omit to receive status changes for every state machine in the
56 * account/region.
57 */
58 stateMachines?: readonly StateMachine[];
59 /**
60 * Which execution statuses to subscribe to.
61 * @default all statuses
62 */
63 statuses?: readonly ExecutionEventStatus[];
64 }
65
66 /**
67 * Event source connecting Step Functions execution status changes to the
68 * hosting compute. Step Functions publishes every STANDARD execution status
69 * transition to the account's default EventBridge bus (source `aws.states`,
70 * detail-type `Step Functions Execution Status Change`); this subscribes
71 * the host Function to those events so it can alert on `FAILED` executions
72 * or chain post-completion automation.
73 *
74 * Step Functions publishes to EventBridge automatically — no additional
75 * resource is created besides the EventBridge rule targeting the host.
76 * Provide the host-specific implementation layer (e.g.
77 * `AWS.Lambda.EventSource`) on the Function effect.
78 *
79 * `EXPRESS` workflows do not emit execution status change events.
80 *
81 * ### Consuming Execution Events
82 * **Example:** Alert On Failed Executions
83 * ```typescript
84 * import * as AWS from "@alchemy.run/aws";
85 *
86 * export default AlertFunction.make(
87 * { main: import.meta.url },
88 * Effect.gen(function* () {
89 * yield* AWS.StepFunctions.consumeExecutionEvents(
90 * { stateMachines: [orderWorkflow], statuses: ["FAILED", "TIMED_OUT"] },
91 * (events) =>
92 * Stream.runForEach(events, (event) =>
93 * Effect.log(`execution ${event.detail.executionArn} failed`),
94 * ),
95 * );
96 * return {};
97 * }).pipe(Effect.provide(AWS.Lambda.EventSource)),
98 * );
99 * ```
100 */
101 export const consumeExecutionEvents = <StreamReq = never, Req = never>(
102 props: ExecutionEventSourceProps,
103 process: (
104 events: Stream.Stream<ExecutionEvent, never, StreamReq>,
105 ) => Effect.Effect<void, never, Req>,
106 ) =>
107 consumeBusEvents(
108 props.id ?? "SfnExecutionEvents",
109 {
110 source: ["aws.states"],
111 "detail-type": ["Step Functions Execution Status Change"],
112 ...(props.stateMachines !== undefined && props.stateMachines.length > 0
113 ? props.statuses !== undefined && props.statuses.length > 0
114 ? {
115 detail: {
116 stateMachineArn: props.stateMachines.map((machine) => machine.stateMachineArn),
117 status: [...props.statuses],
118 },
119 }
120 : {
121 detail: {
122 stateMachineArn: props.stateMachines.map((machine) => machine.stateMachineArn),
123 },
124 }
125 : props.statuses !== undefined && props.statuses.length > 0
126 ? { detail: { status: [...props.statuses] } }
127 : {}),
128 },
129 { description: props.description, state: props.state },
130 process,
131 );