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

main
ts 194 lines 7.02 KB
Raw
1 import * as Effect from "effect/Effect";
2 /**
3 * Shared scaffolding for the Incident Manager Contacts (SSM Contacts) HTTP
4 * bindings.
5 *
6 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
7 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
8 * below. Everything except the operation and the IAM action list is
9 * boilerplate:
10 *
11 * - Contact-scoped operations (`ssm-contacts:StartEngagement`,
12 * `ssm-contacts:ListPagesByContact`) inject the bound {@link Contact}'s
13 * ARN as the request's `ContactId` and are granted on the contact ARN.
14 * - Channel-scoped operations (`ssm-contacts:ActivateContactChannel`, …)
15 * inject the bound {@link ContactChannel}'s ARN as `ContactChannelId` and
16 * are granted on the channel ARN.
17 * - Rotation-scoped operations (`ssm-contacts:ListRotationShifts`, rotation
18 * overrides) inject the bound {@link Rotation}'s ARN as `RotationId` and
19 * are granted on the rotation ARN.
20 * - Account-level operations (engagement / page reads and controls) take
21 * the caller's request as-is and are granted on `*` — engagement and page
22 * ARNs are minted at runtime and cannot be scoped statically.
23 */
24 import * as AWSBindings from "../AWSBindings.ts";
25 import type { Contact } from "./Contact.ts";
26 import type { ContactChannel } from "./ContactChannel.ts";
27 import type { Rotation } from "./Rotation.ts";
28
29 /**
30 * Build the impl Effect for an SSM Contacts operation scoped to a
31 * {@link Contact}: the deploy-time half grants `actions` on the bound
32 * contact's ARN, and the runtime half injects the contact's ARN as the
33 * request's `ContactId` field.
34 */
35 export const makeContactHttpBinding = <I extends { ContactId: string }, A, E, R>(options: {
36 /** Fully-qualified binding tag, e.g. `AWS.SSMContacts.StartEngagement`. */
37 tag: string;
38 /** The distilled operation; `ContactId` is injected from the contact. */
39 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
40 /** IAM actions granted on the contact 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* (contact: Contact) {
48 const ContactArn = yield* contact.contactArn;
49 if (!globalThis.__ALCHEMY_RUNTIME__) {
50 yield* bind`${options.tag}(${contact})`({
51 policyStatements: [
52 {
53 Effect: "Allow",
54 Action: [...options.actions],
55 Resource: [contact.contactArn],
56 },
57 ],
58 });
59 }
60 return Effect.fn(`${options.tag}(${contact.LogicalId})`)(function* (
61 request?: Omit<I, "ContactId">,
62 ) {
63 return yield* op({
64 ...request,
65 ContactId: yield* ContactArn,
66 } as I);
67 });
68 });
69 });
70
71 /**
72 * Build the impl Effect for an SSM Contacts operation scoped to a
73 * {@link ContactChannel}: the deploy-time half grants `actions` on the bound
74 * channel's ARN, and the runtime half injects the channel's ARN as the
75 * request's `ContactChannelId` field.
76 */
77 export const makeContactChannelHttpBinding = <
78 I extends { ContactChannelId: string },
79 A,
80 E,
81 R,
82 >(options: {
83 /** Fully-qualified binding tag, e.g. `AWS.SSMContacts.SendActivationCode`. */
84 tag: string;
85 /** The distilled operation; `ContactChannelId` is injected from the channel. */
86 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
87 /** IAM actions granted on the contact channel ARN. */
88 actions: readonly string[];
89 }) =>
90 Effect.gen(function* () {
91 const bind = yield* AWSBindings.Bind;
92 const op = yield* AWSBindings.withCredentials(options.operation);
93
94 return Effect.fn(function* (channel: ContactChannel) {
95 const ChannelArn = yield* channel.contactChannelArn;
96 if (!globalThis.__ALCHEMY_RUNTIME__) {
97 yield* bind`${options.tag}(${channel})`({
98 policyStatements: [
99 {
100 Effect: "Allow",
101 Action: [...options.actions],
102 Resource: [channel.contactChannelArn],
103 },
104 ],
105 });
106 }
107 return Effect.fn(`${options.tag}(${channel.LogicalId})`)(function* (
108 request?: Omit<I, "ContactChannelId">,
109 ) {
110 return yield* op({
111 ...request,
112 ContactChannelId: yield* ChannelArn,
113 } as I);
114 });
115 });
116 });
117
118 /**
119 * Build the impl Effect for an SSM Contacts operation scoped to a
120 * {@link Rotation}: the deploy-time half grants `actions` on the bound
121 * rotation's ARN, and the runtime half injects the rotation's ARN as the
122 * request's `RotationId` field.
123 */
124 export const makeRotationHttpBinding = <I extends { RotationId: string }, A, E, R>(options: {
125 /** Fully-qualified binding tag, e.g. `AWS.SSMContacts.ListRotationShifts`. */
126 tag: string;
127 /** The distilled operation; `RotationId` is injected from the rotation. */
128 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
129 /** IAM actions granted on the rotation ARN. */
130 actions: readonly string[];
131 }) =>
132 Effect.gen(function* () {
133 const bind = yield* AWSBindings.Bind;
134 const op = yield* AWSBindings.withCredentials(options.operation);
135
136 return Effect.fn(function* (rotation: Rotation) {
137 const RotationArn = yield* rotation.rotationArn;
138 if (!globalThis.__ALCHEMY_RUNTIME__) {
139 yield* bind`${options.tag}(${rotation})`({
140 policyStatements: [
141 {
142 Effect: "Allow",
143 Action: [...options.actions],
144 Resource: [rotation.rotationArn],
145 },
146 ],
147 });
148 }
149 return Effect.fn(`${options.tag}(${rotation.LogicalId})`)(function* (
150 request?: Omit<I, "RotationId">,
151 ) {
152 return yield* op({
153 ...request,
154 RotationId: yield* RotationArn,
155 } as I);
156 });
157 });
158 });
159
160 /**
161 * Build the impl Effect for an account-level SSM Contacts operation
162 * (engagement and page reads/controls). The deploy-time half grants
163 * `actions` on `*` — engagement and page ARNs embed runtime-generated ids
164 * and cannot be scoped to a deployed resource.
165 */
166 export const makeAccountHttpBinding = <I, A, E, R>(options: {
167 /** Fully-qualified binding tag, e.g. `AWS.SSMContacts.StopEngagement`. */
168 tag: string;
169 /** The distilled operation, invoked with the caller's request as-is. */
170 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
171 /** IAM actions granted on `*`. */
172 actions: readonly string[];
173 }) =>
174 Effect.gen(function* () {
175 const bind = yield* AWSBindings.Bind;
176 const op = yield* AWSBindings.withCredentials(options.operation);
177
178 return Effect.fn(function* () {
179 if (!globalThis.__ALCHEMY_RUNTIME__) {
180 yield* bind`${options.tag}()`({
181 policyStatements: [
182 {
183 Effect: "Allow",
184 Action: [...options.actions],
185 Resource: ["*"],
186 },
187 ],
188 });
189 }
190 return Effect.fn(options.tag)(function* (request?: I) {
191 return yield* op((request ?? {}) as I);
192 });
193 });
194 });