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

main
ts 73 lines 2.56 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { AppMonitor } from "./AppMonitor.ts";
4
5 /**
6 * Shared scaffolding for CloudWatch RUM HTTP bindings.
7 *
8 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
9 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over the builder below
10 * (or reuses {@link bindRumAppMonitorPolicy} when the runtime callable needs
11 * bespoke request shaping, like `PutRumEvents` injecting the monitor id and
12 * details). Everything except the operation and the IAM action list is
13 * boilerplate.
14 */
15
16 /**
17 * Deploy-time half shared by every app-monitor-scoped RUM binding: resolve
18 * the hosting Function and grant `actions` on the monitor's ARN. A no-op
19 * inside the deployed runtime.
20 */
21 export const BindRumAppMonitorPolicy = Effect.gen(function* () {
22 const bind = yield* AWSBindings.Bind;
23 return Effect.fn(function* (options: {
24 tag: string;
25 monitor: AppMonitor;
26 actions: readonly string[];
27 }) {
28 if (!globalThis.__ALCHEMY_RUNTIME__) {
29 yield* bind`${options.tag}(${options.monitor})`({
30 policyStatements: [
31 {
32 Effect: "Allow",
33 Action: [...options.actions],
34 Resource: [options.monitor.appMonitorArn],
35 },
36 ],
37 });
38 }
39 });
40 });
41
42 /**
43 * Build the impl Effect for an operation scoped to a single bound
44 * {@link AppMonitor} and addressed by monitor name. The runtime callable
45 * injects the monitor's name as the request's `Name`; the deploy-time half
46 * grants `actions` on the monitor ARN.
47 */
48 export const makeRumAppMonitorHttpBinding = <I extends { Name: string }, A, E, R>(options: {
49 /** Fully-qualified binding tag, e.g. `AWS.RUM.GetAppMonitorData`. */
50 tag: string;
51 /** The distilled operation; `Name` is injected from the monitor. */
52 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
53 /** IAM actions granted on the monitor ARN. */
54 actions: readonly string[];
55 }) =>
56 Effect.gen(function* () {
57 const bindRumAppMonitorPolicy = yield* BindRumAppMonitorPolicy;
58 const op = yield* AWSBindings.withCredentials(options.operation);
59
60 return Effect.fn(function* (monitor: AppMonitor) {
61 const Name = yield* monitor.appMonitorName;
62 yield* bindRumAppMonitorPolicy({
63 tag: options.tag,
64 monitor,
65 actions: options.actions,
66 });
67 return Effect.fn(`${options.tag}(${monitor.LogicalId})`)(function* (
68 request: Omit<I, "Name">,
69 ) {
70 return yield* op({ ...request, Name: yield* Name } as I);
71 });
72 });
73 });