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

main
ts 91 lines 3.23 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 { Collection } from "./Collection.ts";
5
6 /**
7 * Shared scaffolding for the OpenSearch Serverless runtime bindings.
8 *
9 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
10 * thin `Layer.effect(Cap, makeAoss…HttpBinding({ … }))` over one of the two
11 * builders below. Everything except the operation, the IAM action list, and
12 * (for collection-scoped operations) the injected collection `id` is
13 * boilerplate.
14 */
15
16 /**
17 * Build the impl Effect for a collection-scoped operation: the runtime
18 * callable injects the bound {@link Collection}'s ID as `id` and the
19 * deploy-time half grants `actions` on the collection's ARN.
20 */
21 export const makeAossCollectionHttpBinding = <I extends { id?: string }, A, E, R>(options: {
22 /** Fully-qualified binding tag, e.g. `AWS.OpenSearchServerless.CreateIndex`. */
23 tag: string;
24 /** The distilled operation; `id` is injected from the collection. */
25 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
26 /** IAM actions granted on the collection ARN. */
27 actions: readonly string[];
28 }) =>
29 Effect.gen(function* () {
30 const bind = yield* AWSBindings.Bind;
31 const op = yield* AWSBindings.withCredentials(options.operation);
32
33 return Effect.fn(function* (collection: Collection) {
34 const CollectionId = yield* collection.collectionId;
35 if (!globalThis.__ALCHEMY_RUNTIME__) {
36 yield* bind`${options.tag}(${collection})`({
37 policyStatements: [
38 {
39 Effect: "Allow",
40 Action: [...options.actions],
41 Resource: [Output.interpolate`${collection.collectionArn}`],
42 },
43 ],
44 });
45 }
46 return Effect.fn(`${options.tag}(${collection.LogicalId})`)(function* (
47 request: Omit<I, "id">,
48 ) {
49 return yield* op({
50 ...request,
51 id: yield* CollectionId,
52 } as I);
53 });
54 });
55 });
56
57 /**
58 * Build the impl Effect for an account-level operation (no collection
59 * argument): the deploy-time half grants `actions` on `*` — the account
60 * settings, policy stats, and lifecycle-policy read actions do not support
61 * resource-level scoping.
62 */
63 export const makeAossAccountHttpBinding = <I, A, E, R>(options: {
64 /** Fully-qualified binding tag, e.g. `AWS.OpenSearchServerless.GetAccountSettings`. */
65 tag: string;
66 /** The distilled operation, invoked with the caller's request as-is. */
67 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
68 /** IAM actions granted on `*`. */
69 actions: readonly string[];
70 }) =>
71 Effect.gen(function* () {
72 const bind = yield* AWSBindings.Bind;
73 const op = yield* AWSBindings.withCredentials(options.operation);
74
75 return Effect.fn(function* () {
76 if (!globalThis.__ALCHEMY_RUNTIME__) {
77 yield* bind`${options.tag}()`({
78 policyStatements: [
79 {
80 Effect: "Allow",
81 Action: [...options.actions],
82 Resource: ["*"],
83 },
84 ],
85 });
86 }
87 return Effect.fn(options.tag)(function* (request?: I) {
88 return yield* op((request ?? {}) as I);
89 });
90 });
91 });