The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as AWSBindings from "../AWSBindings.ts"; |
| 3 | import type { Room } from "./Room.ts"; |
| 4 | |
| 5 | /** |
| 6 | * Shared scaffolding for Amazon IVS Chat HTTP bindings. |
| 7 | * |
| 8 | * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is |
| 9 | * a `Layer.effect(Cap, makeIvsChatRoomHttpBinding({ … }))` over the builder |
| 10 | * below. Everything except the operation, the IAM action list, and an |
| 11 | * optional request-shape mapper is boilerplate. |
| 12 | */ |
| 13 | |
| 14 | /** |
| 15 | * Build the impl Effect for an IVS Chat data-plane operation scoped to a |
| 16 | * {@link Room}: the deploy-time half grants `actions` on the bound room's |
| 17 | * ARN, and the runtime half injects the room's ARN as the `roomIdentifier` |
| 18 | * of every request. |
| 19 | * |
| 20 | * `prepare` (optional) maps a friendlier public request shape onto the wire |
| 21 | * request — e.g. `CreateChatToken` converts a `Duration.Input` into the wire |
| 22 | * `sessionDurationInMinutes`. It defaults to identity. |
| 23 | */ |
| 24 | export const makeIvsChatRoomHttpBinding = < |
| 25 | I extends { roomIdentifier: string }, |
| 26 | A, |
| 27 | E, |
| 28 | R, |
| 29 | Req = Omit<I, "roomIdentifier">, |
| 30 | >(options: { |
| 31 | /** Fully-qualified binding tag, e.g. `AWS.IVSChat.SendEvent`. */ |
| 32 | tag: string; |
| 33 | /** The distilled operation. */ |
| 34 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 35 | /** IAM actions granted on the room ARN. */ |
| 36 | actions: readonly string[]; |
| 37 | /** Map the public request shape to the wire request (defaults to identity). */ |
| 38 | prepare?: (request: Req) => Omit<I, "roomIdentifier">; |
| 39 | }) => |
| 40 | Effect.gen(function* () { |
| 41 | const bind = yield* AWSBindings.Bind; |
| 42 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 43 | |
| 44 | return Effect.fn(function* (room: Room) { |
| 45 | // Output yields a DEFERRED effect — resolve again per invocation below. |
| 46 | const RoomArn = yield* room.roomArn; |
| 47 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 48 | yield* bind`${options.tag}(${room})`({ |
| 49 | policyStatements: [ |
| 50 | { |
| 51 | Effect: "Allow", |
| 52 | Action: [...options.actions], |
| 53 | Resource: [room.roomArn], |
| 54 | }, |
| 55 | ], |
| 56 | }); |
| 57 | } |
| 58 | return Effect.fn(`${options.tag}(${room.LogicalId})`)(function* (request: Req) { |
| 59 | const roomIdentifier = yield* RoomArn; |
| 60 | const wire = options.prepare |
| 61 | ? options.prepare(request) |
| 62 | : (request as unknown as Omit<I, "roomIdentifier">); |
| 63 | return yield* op({ ...wire, roomIdentifier } as I); |
| 64 | }); |
| 65 | }); |
| 66 | }); |