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

main
ts 64 lines 2.52 KB
Raw
1 import * as eventbridge from "@distilled.cloud/aws/eventbridge";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import * as AWSBindings from "../AWSBindings.ts";
5 import { AWSEnvironment } from "../Environment.ts";
6 import type { Archive } from "./Archive.ts";
7 import { StartReplay, type StartReplayRequest } from "./StartReplay.ts";
8
9 /**
10 * HTTP implementation of {@link StartReplay}. At deploy time it grants
11 * `events:StartReplay` on the account's replays (replay names are chosen at
12 * runtime, so the grant is the `replay/*` wildcard); at runtime it calls the
13 * EventBridge API with the host Function's credentials, injecting the bound
14 * archive's ARN as the `EventSourceArn`. Provide this layer on the Function
15 * using the binding.
16 */
17 export const StartReplayHttp = Layer.effect(
18 StartReplay,
19 Effect.gen(function* () {
20 const bind = yield* AWSBindings.Bind;
21 const startReplay = yield* AWSBindings.withCredentials(eventbridge.startReplay);
22
23 return Effect.fn(function* (archive: Archive) {
24 const ArchiveArn = yield* archive.archiveArn;
25 const EventSourceArn = yield* archive.eventSourceArn;
26 if (!globalThis.__ALCHEMY_RUNTIME__) {
27 const { accountId, region } = yield* AWSEnvironment.current as unknown as Effect.Effect<{
28 accountId: string;
29 region: string;
30 }>;
31 yield* bind`AWS.EventBridge.StartReplay(${archive})`({
32 policyStatements: [
33 {
34 Effect: "Allow",
35 Action: ["events:StartReplay"],
36 Resource: [
37 `arn:aws:events:${region}:${accountId}:replay/*`,
38 // StartReplay also authorizes against the source archive.
39 archive.archiveArn,
40 // ...and against the DESTINATION event bus. EventBridge
41 // only allows replaying to the archive's source bus, so
42 // that bus ARN covers every legal destination.
43 archive.eventSourceArn,
44 ],
45 },
46 ],
47 });
48 }
49 return Effect.fn(`AWS.EventBridge.StartReplay(${archive.LogicalId})`)(function* (
50 request: StartReplayRequest,
51 ) {
52 return yield* startReplay({
53 ...request,
54 EventSourceArn: yield* ArchiveArn,
55 // Default the destination to the archive's source bus — the only
56 // destination EventBridge accepts for a replay.
57 Destination: request.Destination ?? {
58 Arn: yield* EventSourceArn,
59 },
60 });
61 });
62 });
63 }),
64 );