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

main
ts 141 lines 4.16 KB
Raw
1 import type * as lambda from "aws-lambda";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import * as Stream from "effect/Stream";
5 import {
6 EventSource as EventBridgeEventSource,
7 matchesEventPattern,
8 type EventPattern,
9 type EventRecord,
10 type EventRouteProps,
11 type EventSourceService,
12 } from "../EventBridge/EventSource.ts";
13 import { toLambda as createLambdaRoute } from "../EventBridge/ToLambda.ts";
14 import * as Lambda from "./Function.ts";
15
16 /**
17 * Narrow an arbitrary Lambda invocation payload to an EventBridge event.
18 */
19 export const isEventBridgeEvent = (event: any): event is lambda.EventBridgeEvent<string, any> =>
20 typeof event?.source === "string" && typeof event?.["detail-type"] === "string";
21
22 /**
23 * Lambda runtime implementation for `AWS.EventBridge.consumeBusEvents(...)`.
24 *
25 * This layer does two things:
26 *
27 * 1. It delegates to `EventSourcePolicy` so deployment creates an EventBridge
28 * rule targeting the current Lambda function.
29 * 2. At runtime it filters incoming Lambda events against the original event
30 * pattern and forwards matching events into the supplied `Stream`.
31 * ### Subscribing To The Default Bus
32 * **Example:** Match User Events On The Default Bus
33 * ```typescript
34 * yield* AWS.EventBridge.consumeBusEvents(
35 * {
36 * source: ["app.user"],
37 * "detail-type": ["UserCreated"],
38 * },
39 * (events) =>
40 * Stream.runForEach(events, (event) =>
41 * Effect.log(`new user: ${event.detail.userId}`),
42 * ),
43 * );
44 * ```
45 *
46 * ### Subscribing To A Custom Bus
47 * **Example:** Match Orders On A Named Bus
48 * ```typescript
49 * const bus = yield* AWS.EventBridge.EventBus("OrdersBus", {
50 * name: "orders",
51 * });
52 *
53 * yield* AWS.EventBridge.consumeBusEvents(
54 * bus,
55 * {
56 * source: ["app.orders"],
57 * "detail-type": ["OrderPaid"],
58 * },
59 * (events) =>
60 * Stream.runForEach(events, (event) =>
61 * Effect.log(`paid order: ${event.detail.orderId}`),
62 * ),
63 * );
64 * ```
65 *
66 * ### Explicit Route Names
67 * **Example:** Name The Backing Rule Deterministically
68 * ```typescript
69 * yield* AWS.EventBridge.consumeBusEvents(
70 * "InvoiceEvents",
71 * {
72 * source: ["app.billing"],
73 * "detail-type": ["InvoiceIssued"],
74 * },
75 * {
76 * description: "Deliver invoice events into this Lambda function",
77 * },
78 * (events) =>
79 * Stream.runForEach(events, (event) =>
80 * Effect.log(`invoice: ${event.detail.invoiceId}`),
81 * ),
82 * );
83 * ```
84 *
85 * ### Processing Typed Details
86 * **Example:** Narrow The Event Detail Payload
87 * ```typescript
88 * type UserCreated = {
89 * userId: string;
90 * email: string;
91 * };
92 *
93 * yield* AWS.EventBridge.consumeBusEvents(
94 * {
95 * source: ["app.user"],
96 * "detail-type": ["UserCreated"],
97 * },
98 * (events) =>
99 * Stream.runForEach(
100 * events as Stream.Stream<AWS.EventBridge.EventRecord<UserCreated>>,
101 * (event) => Effect.log(`welcome ${event.detail.email}`),
102 * ),
103 * );
104 * ```
105 *
106 * @binding
107 */
108 export const EventSource = Layer.effect(
109 EventBridgeEventSource,
110 Effect.gen(function* () {
111 const host = yield* Lambda.eventSourceHost("AWS.Lambda.EventBridgeEventSource");
112
113 return Effect.fn(function* <Detail = unknown, StreamReq = never, Req = never>(
114 descriptor: {
115 id?: string;
116 bus?: any;
117 pattern: EventPattern;
118 props?: EventRouteProps;
119 },
120 process: (
121 events: Stream.Stream<EventRecord<Detail>, never, StreamReq>,
122 ) => Effect.Effect<void, never, Req>,
123 ) {
124 // Deploy-time: create the backing EventBridge rule + Lambda permission
125 // targeting this function. Skipped once running inside the deployed
126 // Function (the global guard), where the only work is registering the
127 // runtime handler below.
128 if (!globalThis.__ALCHEMY_RUNTIME__) {
129 yield* createLambdaRoute(descriptor, host).pipe(Effect.asVoid);
130 }
131
132 yield* host.listen(
133 Effect.sync(() => (event: any) => {
134 if (isEventBridgeEvent(event) && matchesEventPattern(descriptor.pattern, event)) {
135 return process(Stream.succeed(event as EventRecord<Detail>)).pipe(Effect.orDie);
136 }
137 }),
138 );
139 }) as EventSourceService;
140 }),
141 );