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

main
ts 129 lines 4.59 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
9 /**
10 * The `detail` payload Amazon IVS delivers to EventBridge. Stream
11 * state-change events describe the transition (`event_name` of
12 * `Stream Start`, `Stream End`, or `Session Created` plus the
13 * `channel_name`/`stream_id`); limit-breach events carry the breached
14 * `limit_name` and `limit_value`; recording state changes carry the
15 * `recording_status` and S3 key prefix. Fields not shared by every event
16 * kind are optional (the schema grows over time).
17 */
18 export interface IvsStreamEventDetail {
19 /**
20 * Stream state-change events: `Stream Start`, `Stream End`, or
21 * `Session Created`. Limit-breach events: the name of the breached
22 * limit.
23 */
24 event_name?: string;
25 /** The name of the channel the event is about. */
26 channel_name?: string;
27 /** Stream state-change events: the id of the stream session. */
28 stream_id?: string;
29 /** Limit-breach events: the name of the breached limit. */
30 limit_name?: string;
31 /** Limit-breach events: the value of the breached limit. */
32 limit_value?: number;
33 /** Recording state-change events: the recording session id. */
34 recording_session_id?: string;
35 /**
36 * Recording state-change events: `Recording Start`, `Recording End`,
37 * or `Recording Start Failure`.
38 */
39 recording_status?: string;
40 /** Additional event fields (the schema grows over time). */
41 [key: string]: unknown;
42 }
43
44 /** An Amazon IVS EventBridge event delivered to the handler. */
45 export type IvsStreamEvent = EventRecord<IvsStreamEventDetail>;
46
47 /** Which Amazon IVS notifications to subscribe to. */
48 export type IvsStreamEventKind = "stream-state-change" | "recording-state-change" | "limit-breach";
49
50 const DETAIL_TYPES: Record<IvsStreamEventKind, string> = {
51 "stream-state-change": "IVS Stream State Change",
52 "recording-state-change": "IVS Recording State Change",
53 "limit-breach": "IVS Limit Breach",
54 };
55
56 export interface StreamEventSourceProps extends EventRouteProps {
57 /**
58 * Logical id for the backing EventBridge rule.
59 * @default "IVSStreamEvents"
60 */
61 id?: string;
62 /**
63 * Which notifications to subscribe to: stream state changes (stream
64 * start/end, session created), recording state changes (recording
65 * start/end/failure), and/or limit breaches.
66 * @default ["stream-state-change"]
67 */
68 kinds?: readonly IvsStreamEventKind[];
69 /**
70 * Restrict to events about specific channels (matched against the
71 * event's top-level `resources`, which contains the channel's ARN).
72 */
73 channelArns?: readonly string[];
74 }
75
76 /**
77 * Event source connecting Amazon IVS stream notifications to the hosting
78 * compute. IVS publishes stream state changes (a broadcast starting or
79 * ending), recording state changes (an S3 recording starting, completing,
80 * or failing), and account limit breaches to the account's default
81 * EventBridge bus (source `aws.ivs`); this subscribes the host Function
82 * to those events so it can react to broadcasts going live or recordings
83 * landing in S3.
84 *
85 * IVS publishes to EventBridge automatically — no additional resource is
86 * created besides the EventBridge rule targeting the host. Provide the
87 * host-specific implementation layer (e.g. `AWS.Lambda.EventSource`) on
88 * the Function effect.
89 *
90 * ### Consuming Stream Events
91 * **Example:** React When a Broadcast Starts
92 * ```typescript
93 * import * as AWS from "@alchemy.run/aws";
94 *
95 * export default NotifyFunction.make(
96 * { main: import.meta.url },
97 * Effect.gen(function* () {
98 * yield* AWS.IVS.consumeStreamEvents(
99 * { kinds: ["stream-state-change"] },
100 * (events) =>
101 * Stream.runForEach(events, (event) =>
102 * event.detail.event_name === "Stream Start"
103 * ? Effect.log(`${event.detail.channel_name} went live`)
104 * : Effect.void,
105 * ),
106 * );
107 * return {};
108 * }).pipe(Effect.provide(AWS.Lambda.EventSource)),
109 * );
110 * ```
111 */
112 export const consumeStreamEvents = <StreamReq = never, Req = never>(
113 props: StreamEventSourceProps,
114 process: (
115 events: Stream.Stream<IvsStreamEvent, never, StreamReq>,
116 ) => Effect.Effect<void, never, Req>,
117 ) =>
118 consumeBusEvents(
119 props.id ?? "IVSStreamEvents",
120 {
121 source: ["aws.ivs"],
122 "detail-type": (props.kinds ?? (["stream-state-change"] as const)).map(
123 (kind) => DETAIL_TYPES[kind],
124 ),
125 ...(props.channelArns !== undefined ? { resources: [...props.channelArns] } : {}),
126 },
127 { description: props.description, state: props.state },
128 process,
129 );