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

main
ts 87 lines 3.58 KB
Raw
1 import * as Output from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { LogGroup } from "./LogGroup.ts";
5
6 /**
7 * Shared scaffolding for CloudWatch Logs HTTP bindings.
8 *
9 * NOT exported from `index.ts` — every single-operation `{Op}Http.ts` in this
10 * service is a thin `Layer.effect(Cap, makeLogGroupHttpBinding({ … }))` over
11 * this builder. Everything except the operation, the IAM action(s), and the
12 * IAM resource shape is boilerplate:
13 *
14 * - the deploy-time half registers `Allow(host, tag(logGroup))` with the
15 * requested actions on the bound group's ARN (and/or its `:*` log-stream
16 * wildcard, per `iamResources`);
17 * - the runtime callable injects the resolved `logGroupName` into the request
18 * (unless `injectLogGroupName: false` — for query-id / record-pointer
19 * scoped operations like `GetQueryResults`, `StopQuery`, `GetLogRecord`).
20 *
21 * Genuinely-different bindings stay bespoke: `LogEventSink` (a batching sink
22 * over the `PutLogEvents` capability).
23 */
24 export const makeLogGroupHttpBinding = <I, A, E, R>(options: {
25 /** Fully-qualified binding tag, e.g. `AWS.Logs.FilterLogEvents`. */
26 tag: string;
27 /**
28 * The distilled operation the runtime callable invokes.
29 *
30 * Typed as distilled's `OperationMethod` intersection (yieldable Effect +
31 * direct call signature) so `I` is inferred from the call signature — the
32 * Effect half alone defeats inference for requests without `logGroupName`
33 * (e.g. `GetLogRecord`, `GetQueryResults`, `StopQuery`).
34 */
35 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R> &
36 ((input: I) => Effect.Effect<A, E, R>);
37 /** IAM actions granted on the bound log group. */
38 actions: readonly string[];
39 /**
40 * Which ARNs the actions are granted on:
41 * - `"group-and-streams"` (default): the log-group ARN plus `${arn}:*`;
42 * - `"streams"`: only `${arn}:*` — for actions that authorize against the
43 * log-stream resource (`PutLogEvents`, `DeleteLogStream`);
44 * - `"all"`: `*` — for actions that do not support resource-level
45 * permissions (`StopQuery`).
46 */
47 iamResources?: "group-and-streams" | "streams" | "all";
48 /**
49 * Inject the bound group's name under `logGroupName` (default `true`).
50 * Disable for operations scoped by a query id or record pointer.
51 */
52 injectLogGroupName?: boolean;
53 }) =>
54 Effect.gen(function* () {
55 const bind = yield* AWSBindings.Bind;
56 const op = yield* AWSBindings.withCredentials(options.operation);
57 const inject = options.injectLogGroupName ?? true;
58
59 return Effect.fn(function* <G extends LogGroup>(logGroup: G) {
60 const LogGroupName = yield* logGroup.logGroupName;
61 if (!globalThis.__ALCHEMY_RUNTIME__) {
62 yield* bind`${options.tag}(${logGroup})`({
63 policyStatements: [
64 {
65 Effect: "Allow",
66 Action: [...options.actions],
67 Resource:
68 options.iamResources === "all"
69 ? ["*"]
70 : options.iamResources === "streams"
71 ? [Output.interpolate`${logGroup.logGroupArn}:*`]
72 : [logGroup.logGroupArn, Output.interpolate`${logGroup.logGroupArn}:*`],
73 },
74 ],
75 });
76 }
77 return Effect.fn(`${options.tag}(${logGroup.LogicalId})`)(function* (
78 request?: Omit<I, "logGroupName">,
79 ) {
80 const input: Record<string, unknown> = { ...request };
81 if (inject) {
82 input.logGroupName = yield* LogGroupName;
83 }
84 return yield* op(input as unknown as I);
85 });
86 });
87 });