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

main
ts 154 lines 5.77 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { OptOutList } from "./OptOutList.ts";
4 import type { PhoneNumber } from "./PhoneNumber.ts";
5
6 /**
7 * Shared scaffolding for AWS End User Messaging SMS (Pinpoint SMS Voice
8 * v2) HTTP bindings.
9 *
10 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service
11 * is a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the three
12 * builders below. Everything except the operation and the IAM action list
13 * is boilerplate: phone-number-scoped bindings inject the bound number's
14 * ARN as the request's `OriginationIdentity` and grant `actions` on that
15 * ARN; opt-out-list-scoped bindings inject the list's ARN as
16 * `OptOutListName`; account-scoped bindings pass the request through and
17 * grant `actions` on `*`.
18 */
19
20 /**
21 * Build the impl Effect for an End User Messaging SMS operation scoped to
22 * a {@link PhoneNumber} (`SendTextMessage`, `PutKeyword`, …): the
23 * deploy-time half grants `actions` on the bound number's ARN, and the
24 * runtime half injects the number's ARN into every request as
25 * `OriginationIdentity` (the API accepts a phone number ID or ARN there).
26 */
27 export const makeSmsVoicePhoneNumberHttpBinding = <
28 // Optional so the builder accepts SendTextMessage, whose wire
29 // `OriginationIdentity` is optional (account-default routing); the
30 // binding always injects the bound number's ARN regardless.
31 I extends { OriginationIdentity?: string },
32 A,
33 E,
34 R,
35 >(options: {
36 /** Fully-qualified binding tag, e.g. `AWS.PinpointSMSVoiceV2.SendTextMessage`. */
37 tag: string;
38 /** The distilled operation; `OriginationIdentity` is injected from the number. */
39 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
40 /** IAM actions granted on the phone number ARN. */
41 actions: readonly string[];
42 }) =>
43 Effect.gen(function* () {
44 const bind = yield* AWSBindings.Bind;
45 const op = yield* AWSBindings.withCredentials(options.operation);
46
47 return Effect.fn(function* (phoneNumber: PhoneNumber) {
48 // Output yields a DEFERRED effect — resolve again per invocation below.
49 const PhoneNumberArn = yield* phoneNumber.phoneNumberArn;
50 if (!globalThis.__ALCHEMY_RUNTIME__) {
51 yield* bind`${options.tag}(${phoneNumber})`({
52 policyStatements: [
53 {
54 Effect: "Allow",
55 Action: [...options.actions],
56 Resource: [phoneNumber.phoneNumberArn],
57 },
58 ],
59 });
60 }
61 return Effect.fn(`${options.tag}(${phoneNumber.LogicalId})`)(function* (
62 request?: Omit<I, "OriginationIdentity">,
63 ) {
64 const originationIdentity = yield* PhoneNumberArn;
65 return yield* op({
66 ...request,
67 OriginationIdentity: originationIdentity,
68 } as I);
69 });
70 });
71 });
72
73 /**
74 * Build the impl Effect for an End User Messaging SMS operation scoped to
75 * an {@link OptOutList} (`PutOptedOutNumber`, `DescribeOptedOutNumbers`,
76 * …): the deploy-time half grants `actions` on the bound list's ARN, and
77 * the runtime half injects the list's ARN into every request as
78 * `OptOutListName` (the API accepts a list name or ARN there).
79 */
80 export const makeSmsVoiceOptOutListHttpBinding = <
81 I extends { OptOutListName: string },
82 A,
83 E,
84 R,
85 >(options: {
86 /** Fully-qualified binding tag, e.g. `AWS.PinpointSMSVoiceV2.PutOptedOutNumber`. */
87 tag: string;
88 /** The distilled operation; `OptOutListName` is injected from the list. */
89 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
90 /** IAM actions granted on the opt-out list ARN. */
91 actions: readonly string[];
92 }) =>
93 Effect.gen(function* () {
94 const bind = yield* AWSBindings.Bind;
95 const op = yield* AWSBindings.withCredentials(options.operation);
96
97 return Effect.fn(function* (optOutList: OptOutList) {
98 // Output yields a DEFERRED effect — resolve again per invocation below.
99 const OptOutListArn = yield* optOutList.optOutListArn;
100 if (!globalThis.__ALCHEMY_RUNTIME__) {
101 yield* bind`${options.tag}(${optOutList})`({
102 policyStatements: [
103 {
104 Effect: "Allow",
105 Action: [...options.actions],
106 Resource: [optOutList.optOutListArn],
107 },
108 ],
109 });
110 }
111 return Effect.fn(`${options.tag}(${optOutList.LogicalId})`)(function* (
112 request?: Omit<I, "OptOutListName">,
113 ) {
114 const optOutListName = yield* OptOutListArn;
115 return yield* op({ ...request, OptOutListName: optOutListName } as I);
116 });
117 });
118 });
119
120 /**
121 * Build the impl Effect for an account-level End User Messaging SMS
122 * operation (`CarrierLookup`, `PutMessageFeedback`): the deploy-time half
123 * grants `actions` on `*` — these operations act on message IDs or raw
124 * phone numbers, not on a scopeable resource ARN.
125 */
126 export const makeSmsVoiceAccountHttpBinding = <I, A, E, R>(options: {
127 /** Fully-qualified binding tag, e.g. `AWS.PinpointSMSVoiceV2.CarrierLookup`. */
128 tag: string;
129 /** The distilled operation, invoked with the caller's request as-is. */
130 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
131 /** IAM actions granted on `*`. */
132 actions: readonly string[];
133 }) =>
134 Effect.gen(function* () {
135 const bind = yield* AWSBindings.Bind;
136 const op = yield* AWSBindings.withCredentials(options.operation);
137
138 return Effect.fn(function* () {
139 if (!globalThis.__ALCHEMY_RUNTIME__) {
140 yield* bind`${options.tag}()`({
141 policyStatements: [
142 {
143 Effect: "Allow",
144 Action: [...options.actions],
145 Resource: ["*"],
146 },
147 ],
148 });
149 }
150 return Effect.fn(options.tag)(function* (request: I) {
151 return yield* op(request);
152 });
153 });
154 });