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

main
ts 126 lines 4.08 KB
Raw
1 import type { Credentials } from "@distilled.cloud/aws/Credentials";
2 import type { Region } from "@distilled.cloud/aws/Region";
3 import * as Output from "alchemy/Output";
4 import * as Effect from "effect/Effect";
5 import type * as HttpClient from "effect/http/HttpClient";
6 import * as AWSBindings from "../AWSBindings.ts";
7 import type { Broker } from "./Broker.ts";
8
9 /**
10 * Shared HTTP scaffolding for the Amazon MQ runtime bindings.
11 *
12 * Every MQ capability follows the same shape — resolve the distilled
13 * operation, register an IAM policy statement on the binding host, and return
14 * a runtime callable that injects the broker id. The only variation is the
15 * operation, the IAM action(s), and whether the binding is scoped to one
16 * broker or to the whole account, so those are the only inputs.
17 *
18 * @internal — not exported from `index.ts`.
19 */
20
21 type MqRequirements = Credentials | Region | HttpClient.HttpClient;
22
23 export interface MqBrokerHttpBindingConfig<Req extends object, Out, Err> {
24 /**
25 * Short capability name used in the binding sid and runtime span, e.g.
26 * `"DescribeBroker"`.
27 */
28 capability: string;
29 /**
30 * IAM actions granted to the binding host on the broker ARN, e.g.
31 * `["mq:DescribeBroker"]`.
32 */
33 iamActions: readonly string[];
34 /**
35 * The distilled MQ operation implementing the capability.
36 */
37 operation: Effect.Effect<
38 (input: Req & { BrokerId: string }) => Effect.Effect<Out, Err>,
39 never,
40 MqRequirements
41 >;
42 }
43
44 /**
45 * Build the implementation effect for a broker-scoped MQ capability:
46 * `Layer.effect(Cap, makeMqBrokerHttpBinding({ ... }))`.
47 *
48 * The runtime callable injects the bound broker's id, so `Req` is the
49 * operation's request type without `BrokerId`.
50 */
51 export const makeMqBrokerHttpBinding = <Req extends object, Out, Err>(
52 config: MqBrokerHttpBindingConfig<Req, Out, Err>,
53 ) =>
54 Effect.gen(function* () {
55 const bind = yield* AWSBindings.Bind;
56 const op = yield* AWSBindings.withCredentials(config.operation);
57
58 return Effect.fn(function* (broker: Broker) {
59 const BrokerId = yield* broker.brokerId;
60 if (!globalThis.__ALCHEMY_RUNTIME__) {
61 yield* bind`AWS.MQ.${config.capability}(${broker})`({
62 policyStatements: [
63 {
64 Effect: "Allow",
65 Action: [...config.iamActions],
66 Resource: [Output.interpolate`${broker.brokerArn}`],
67 },
68 ],
69 });
70 }
71 return Effect.fn(`AWS.MQ.${config.capability}(${broker.LogicalId})`)(function* (
72 request?: Req,
73 ) {
74 return yield* op({
75 ...(request ?? ({} as Req)),
76 BrokerId: yield* BrokerId,
77 });
78 });
79 });
80 });
81
82 export interface MqAccountHttpBindingConfig<Req extends object, Out, Err> {
83 /**
84 * Short capability name used in the binding sid and runtime span, e.g.
85 * `"ListBrokers"`.
86 */
87 capability: string;
88 /**
89 * IAM actions granted to the binding host on `Resource: ["*"]` (MQ
90 * account-level operations are not resource-scoped).
91 */
92 iamActions: readonly string[];
93 /**
94 * The distilled MQ operation implementing the capability.
95 */
96 operation: Effect.Effect<(input: Req) => Effect.Effect<Out, Err>, never, MqRequirements>;
97 }
98
99 /**
100 * Build the implementation effect for an account-level MQ capability (no
101 * broker argument): `Layer.effect(Cap, makeMqAccountHttpBinding({ ... }))`.
102 */
103 export const makeMqAccountHttpBinding = <Req extends object, Out, Err>(
104 config: MqAccountHttpBindingConfig<Req, Out, Err>,
105 ) =>
106 Effect.gen(function* () {
107 const bind = yield* AWSBindings.Bind;
108 const op = yield* AWSBindings.withCredentials(config.operation);
109
110 return Effect.fn(function* () {
111 if (!globalThis.__ALCHEMY_RUNTIME__) {
112 yield* bind`AWS.MQ.${config.capability}()`({
113 policyStatements: [
114 {
115 Effect: "Allow",
116 Action: [...config.iamActions],
117 Resource: ["*"],
118 },
119 ],
120 });
121 }
122 return Effect.fn(`AWS.MQ.${config.capability}`)(function* (request?: Req) {
123 return yield* op(request ?? ({} as Req));
124 });
125 });
126 });