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

main
ts 94 lines 3.88 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { LinkedWhatsAppBusinessAccount } from "./LinkedWhatsAppBusinessAccount.ts";
4
5 /**
6 * Shared scaffolding for AWS End User Messaging Social (WhatsApp) HTTP
7 * bindings.
8 *
9 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service
10 * is a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two
11 * builders below. Everything except the operation and the IAM action list
12 * is boilerplate: WABA-scoped bindings inject the bound linked account's
13 * `id` (`waba-…`) into every request and grant `actions` on the WABA ARN;
14 * phone-plane bindings pass the request through (the caller addresses a
15 * phone number by its `phone-number-id-…`) and grant `actions` on `*`,
16 * because the WABA's phone numbers are provisioned by Meta at onboarding
17 * time and their ARNs are not derivable from the WABA ARN at bind time.
18 */
19
20 /**
21 * Build the impl Effect for an operation scoped to the bound
22 * {@link LinkedWhatsAppBusinessAccount} (message templates, WhatsApp
23 * Flows): the deploy-time half grants `actions` on the WABA ARN and the
24 * runtime half injects the linked account id into every request as `id`.
25 */
26 export const makeWabaScopedHttpBinding = <I extends { id: string }, A, E, R>(options: {
27 /** Fully-qualified binding tag, e.g. `AWS.SocialMessaging.ListWhatsAppMessageTemplates`. */
28 tag: string;
29 /** The distilled operation; `id` is injected from the linked account. */
30 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
31 /** IAM actions granted on the WABA ARN. */
32 actions: readonly string[];
33 }) =>
34 Effect.gen(function* () {
35 const bind = yield* AWSBindings.Bind;
36 const op = yield* AWSBindings.withCredentials(options.operation);
37
38 return Effect.fn(function* (account: LinkedWhatsAppBusinessAccount) {
39 // Output yields a DEFERRED effect — resolve again per invocation below.
40 const AccountId = yield* account.id;
41 if (!globalThis.__ALCHEMY_RUNTIME__) {
42 yield* bind`${options.tag}(${account})`({
43 policyStatements: [
44 {
45 Effect: "Allow",
46 Action: [...options.actions],
47 Resource: [account.arn],
48 },
49 ],
50 });
51 }
52 return Effect.fn(`${options.tag}(${account.LogicalId})`)(function* (request?: Omit<I, "id">) {
53 return yield* op({ ...request, id: yield* AccountId } as I);
54 });
55 });
56 });
57
58 /**
59 * Build the impl Effect for a phone-number-plane operation
60 * (`SendWhatsAppMessage`, message media): the caller addresses one of the
61 * bound WABA's phone numbers per request (`originationPhoneNumberId` /
62 * `id`), so the request passes through unchanged. The deploy-time half
63 * grants `actions` on `*` — phone numbers are provisioned by Meta under
64 * the WABA and their ARNs are not derivable from the WABA ARN.
65 */
66 export const makeWabaPhonePlaneHttpBinding = <I, A, E, R>(options: {
67 /** Fully-qualified binding tag, e.g. `AWS.SocialMessaging.SendWhatsAppMessage`. */
68 tag: string;
69 /** The distilled operation, invoked with the caller's request as-is. */
70 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
71 /** IAM actions granted on `*`. */
72 actions: readonly string[];
73 }) =>
74 Effect.gen(function* () {
75 const bind = yield* AWSBindings.Bind;
76 const op = yield* AWSBindings.withCredentials(options.operation);
77
78 return Effect.fn(function* (account: LinkedWhatsAppBusinessAccount) {
79 if (!globalThis.__ALCHEMY_RUNTIME__) {
80 yield* bind`${options.tag}(${account})`({
81 policyStatements: [
82 {
83 Effect: "Allow",
84 Action: [...options.actions],
85 Resource: ["*"],
86 },
87 ],
88 });
89 }
90 return Effect.fn(`${options.tag}(${account.LogicalId})`)(function* (request: I) {
91 return yield* op(request);
92 });
93 });
94 });