The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as Output from "alchemy/Output"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as AWSBindings from "../AWSBindings.ts"; |
| 4 | import type { DataSource } from "./DataSource.ts"; |
| 5 | import type { Index } from "./SearchIndex.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Shared scaffolding for AWS Kendra HTTP bindings. |
| 9 | * |
| 10 | * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a |
| 11 | * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two |
| 12 | * builders below. Everything except the operation and the IAM action list is |
| 13 | * boilerplate: every Kendra data-plane operation is scoped to one index |
| 14 | * (whose id is injected as `IndexId` and whose ARN receives the grant), and |
| 15 | * the sync-job operations are additionally scoped to one data source (whose |
| 16 | * id is injected as `Id` and whose ARN receives the grant alongside its |
| 17 | * parent index's). |
| 18 | */ |
| 19 | |
| 20 | /** |
| 21 | * Build the impl Effect for an index-scoped Kendra operation (query, |
| 22 | * retrieve, document batches, principal mapping, suggestions config, …): the |
| 23 | * runtime callable injects the bound {@link Index}'s id as `IndexId` and the |
| 24 | * deploy-time half grants `actions` on the index ARN (plus any |
| 25 | * `subResources` suffix patterns, e.g. `data-source/*` for the |
| 26 | * principal-mapping operations that also act on data-source-scoped groups). |
| 27 | * |
| 28 | * `prepare` (optional) maps a friendlier public request shape onto the wire |
| 29 | * request — e.g. `UpdateQuerySuggestionsConfig` converts a `Duration.Input` |
| 30 | * into the wire `QueryLogLookBackWindowInDays`. It defaults to identity. |
| 31 | */ |
| 32 | export const makeKendraIndexHttpBinding = < |
| 33 | I extends { IndexId: string }, |
| 34 | A, |
| 35 | E, |
| 36 | R, |
| 37 | Req = Omit<I, "IndexId">, |
| 38 | >(options: { |
| 39 | /** Fully-qualified binding tag, e.g. `AWS.Kendra.Query`. */ |
| 40 | tag: string; |
| 41 | /** The distilled operation; `IndexId` is injected from the index. */ |
| 42 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 43 | /** IAM actions granted on the index ARN. */ |
| 44 | actions: readonly string[]; |
| 45 | /** |
| 46 | * Extra ARN suffix patterns (relative to the index ARN) the actions are |
| 47 | * also granted on, e.g. `data-source/*` or |
| 48 | * `access-control-configuration/*`. |
| 49 | */ |
| 50 | subResources?: readonly string[]; |
| 51 | /** Map the public request shape to the wire request (defaults to identity). */ |
| 52 | prepare?: (request: Req) => Omit<I, "IndexId">; |
| 53 | }) => |
| 54 | Effect.gen(function* () { |
| 55 | const bind = yield* AWSBindings.Bind; |
| 56 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 57 | |
| 58 | return Effect.fn(function* (index: Index) { |
| 59 | const IndexId = yield* index.id; |
| 60 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 61 | yield* bind`${options.tag}(${index})`({ |
| 62 | policyStatements: [ |
| 63 | { |
| 64 | Effect: "Allow", |
| 65 | Action: [...options.actions], |
| 66 | Resource: [ |
| 67 | index.arn, |
| 68 | ...(options.subResources ?? []).map( |
| 69 | (suffix) => Output.interpolate`${index.arn}/${suffix}`, |
| 70 | ), |
| 71 | ], |
| 72 | }, |
| 73 | ], |
| 74 | }); |
| 75 | } |
| 76 | return Effect.fn(`${options.tag}(${index.LogicalId})`)(function* (request?: Req) { |
| 77 | const wire = options.prepare |
| 78 | ? options.prepare(request as Req) |
| 79 | : (request as unknown as Omit<I, "IndexId"> | undefined); |
| 80 | return yield* op({ |
| 81 | ...wire, |
| 82 | IndexId: yield* IndexId, |
| 83 | } as I); |
| 84 | }); |
| 85 | }); |
| 86 | }); |
| 87 | |
| 88 | /** |
| 89 | * Build the impl Effect for a data-source-scoped Kendra operation (the sync |
| 90 | * job start/stop/list trio): the runtime callable injects the bound |
| 91 | * {@link DataSource}'s id as `Id` and its parent index's id as `IndexId`; |
| 92 | * the deploy-time half grants `actions` on the data source ARN **and** the |
| 93 | * parent index ARN — Kendra authorizes sync-job actions against both. |
| 94 | */ |
| 95 | export const makeKendraDataSourceHttpBinding = < |
| 96 | I extends { IndexId: string; Id: string }, |
| 97 | A, |
| 98 | E, |
| 99 | R, |
| 100 | >(options: { |
| 101 | /** Fully-qualified binding tag, e.g. `AWS.Kendra.StartDataSourceSyncJob`. */ |
| 102 | tag: string; |
| 103 | /** |
| 104 | * The distilled operation; `Id` and `IndexId` are injected from the data |
| 105 | * source. |
| 106 | */ |
| 107 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 108 | /** IAM actions granted on the data source ARN + its parent index ARN. */ |
| 109 | actions: readonly string[]; |
| 110 | }) => |
| 111 | Effect.gen(function* () { |
| 112 | const bind = yield* AWSBindings.Bind; |
| 113 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 114 | |
| 115 | return Effect.fn(function* (dataSource: DataSource) { |
| 116 | const IndexId = yield* dataSource.indexId; |
| 117 | const Id = yield* dataSource.id; |
| 118 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 119 | yield* bind`${options.tag}(${dataSource})`({ |
| 120 | policyStatements: [ |
| 121 | { |
| 122 | Effect: "Allow", |
| 123 | Action: [...options.actions], |
| 124 | Resource: [ |
| 125 | dataSource.arn, |
| 126 | // The data source ARN is |
| 127 | // `…:index/{indexId}/data-source/{id}` — the parent index |
| 128 | // ARN is its prefix. |
| 129 | dataSource.arn.pipe(Output.map((arn) => arn.split("/data-source/")[0]!)), |
| 130 | ], |
| 131 | }, |
| 132 | ], |
| 133 | }); |
| 134 | } |
| 135 | return Effect.fn(`${options.tag}(${dataSource.LogicalId})`)(function* ( |
| 136 | request?: Omit<I, "IndexId" | "Id">, |
| 137 | ) { |
| 138 | return yield* op({ |
| 139 | ...request, |
| 140 | IndexId: yield* IndexId, |
| 141 | Id: yield* Id, |
| 142 | } as I); |
| 143 | }); |
| 144 | }); |
| 145 | }); |