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

main
ts 130 lines 4.91 KB
Raw
1 import type { Output } from "alchemy/Output";
2 import type { ResourceLike } from "alchemy/Resource";
3 import * as Effect from "effect/Effect";
4 import * as AWSBindings from "../AWSBindings.ts";
5 import type { PolicyStatement } from "../IAM/Policy.ts";
6
7 /**
8 * Shared scaffolding for the Amazon HealthOmics HTTP bindings.
9 *
10 * Every Omics data-plane operation is either addressed by a store/workflow id
11 * (read-set jobs by `sequenceStoreId`, reference jobs by `referenceStoreId`,
12 * runs by `workflowId`) or account-level (run control by `runId`, a runtime
13 * value). Two builders cover both shapes; every thin `{Op}Http.ts` in this
14 * service is a `Layer.effect(Cap, make…HttpBinding({ … }))`.
15 *
16 * NOT exported from `index.ts` — leaking the generic helper names into the
17 * flat `AWS` namespace would collide across services.
18 */
19
20 /**
21 * Build the impl Effect for a resource-scoped operation: the runtime callable
22 * injects the bound resource's id under `key` (`sequenceStoreId`,
23 * `referenceStoreId`, or `workflowId`) and the deploy-time half grants
24 * `actions` on the resource ARN. Operations that hand the service a role to
25 * assume (`Start*ImportJob`, `Start*ExportJob`, `StartRun`) set `passRole`.
26 */
27 export const makeOmicsResourceHttpBinding = <
28 Res extends ResourceLike,
29 K extends string,
30 I extends { [P in K]?: string },
31 A,
32 E,
33 R,
34 >(options: {
35 /** Fully-qualified binding tag, e.g. `AWS.Omics.GetReadSet`. */
36 tag: string;
37 /** The distilled operation; the resource id is injected under `key`. */
38 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
39 /** IAM actions granted on the resource ARN. */
40 actions: readonly string[];
41 /** The request field the resource id is injected under. */
42 key: K;
43 /**
44 * Resolve the injected id from the bound resource. `Req` is pinned to
45 * `never` (resource attributes are `Output<string, never>`): the default
46 * `Output<string>` widens `Req` to `any`, which leaks into the per-resource
47 * Effect's requirements and breaks the binding contract's `never`.
48 */
49 id: (res: Res) => Output<string, never>;
50 /** Resolve the ARN the IAM grant targets from the bound resource. */
51 arn: (res: Res) => Output<string> | string;
52 /** Grant `iam:PassRole` (conditioned to `omics.amazonaws.com`). */
53 passRole?: boolean;
54 }) =>
55 Effect.gen(function* () {
56 const bind = yield* AWSBindings.Bind;
57 const op = yield* AWSBindings.withCredentials(options.operation);
58
59 return Effect.fn(function* (res: Res) {
60 // Outputs yield a DEFERRED effect — resolve again per invocation below.
61 const Id = yield* options.id(res);
62 if (!globalThis.__ALCHEMY_RUNTIME__) {
63 // Inline (not a `PolicyStatement[]`-typed local) so the bind data's
64 // `Input<…>` contextual typing accepts the `Output<string>` ARN.
65 yield* bind`${options.tag}(${res})`({
66 policyStatements: [
67 {
68 Effect: "Allow",
69 Action: [...options.actions],
70 Resource: [options.arn(res)],
71 },
72 ...(options.passRole
73 ? ([
74 {
75 Effect: "Allow",
76 Action: ["iam:PassRole"],
77 Resource: ["*"],
78 Condition: {
79 StringEquals: {
80 "iam:PassedToService": "omics.amazonaws.com",
81 },
82 },
83 },
84 ] satisfies PolicyStatement[])
85 : []),
86 ],
87 });
88 }
89 return Effect.fn(`${options.tag}(${res.LogicalId})`)(function* (request?: Omit<I, K>) {
90 return yield* op({ ...request, [options.key]: yield* Id } as I);
91 });
92 });
93 });
94
95 /**
96 * Build the impl Effect for an account-level run-control operation
97 * (`GetRun`, `ListRuns`, `CancelRun`, `DeleteRun`, `GetRunTask`,
98 * `ListRunTasks`): the runtime callable passes the caller's request through
99 * unchanged (the `runId` is a runtime value) and the deploy-time half grants
100 * `actions` on `*`.
101 */
102 export const makeOmicsAccountHttpBinding = <I, A, E, R>(options: {
103 /** Fully-qualified binding tag, e.g. `AWS.Omics.GetRun`. */
104 tag: string;
105 /** The distilled operation, invoked with the caller's request as-is. */
106 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
107 /** IAM actions granted on `*`. */
108 actions: readonly string[];
109 }) =>
110 Effect.gen(function* () {
111 const bind = yield* AWSBindings.Bind;
112 const op = yield* AWSBindings.withCredentials(options.operation);
113
114 return Effect.fn(function* () {
115 if (!globalThis.__ALCHEMY_RUNTIME__) {
116 yield* bind`${options.tag}()`({
117 policyStatements: [
118 {
119 Effect: "Allow",
120 Action: [...options.actions],
121 Resource: ["*"],
122 },
123 ],
124 });
125 }
126 return Effect.fn(options.tag)(function* (request?: I) {
127 return yield* op((request ?? {}) as I);
128 });
129 });
130 });