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, 06:00 UTC
| 1 | import * as Output from "alchemy/Output"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as AWSBindings from "../AWSBindings.ts"; |
| 4 | import { AWSEnvironment } from "../Environment.ts"; |
| 5 | import type { AgentAlias } from "./AgentAlias.ts"; |
| 6 | import type { DataSource } from "./DataSource.ts"; |
| 7 | import type { KnowledgeBase } from "./KnowledgeBase.ts"; |
| 8 | import { bedrockModelArns } from "./ModelArns.ts"; |
| 9 | |
| 10 | /** |
| 11 | * Shared scaffolding for Bedrock HTTP bindings. |
| 12 | * |
| 13 | * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is |
| 14 | * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders |
| 15 | * below. Everything except the operation, the IAM action list, and the |
| 16 | * injected identifier(s) is boilerplate. Genuinely-different bindings |
| 17 | * (e.g. `Rerank`, which grants a wildcard `bedrock:Rerank` alongside |
| 18 | * model-scoped `bedrock:InvokeModel`) stay bespoke. |
| 19 | */ |
| 20 | |
| 21 | const currentEnv = AWSEnvironment.current as unknown as Effect.Effect<{ |
| 22 | accountId: string; |
| 23 | region: string; |
| 24 | }>; |
| 25 | |
| 26 | /** |
| 27 | * Build the impl Effect for a model-scoped `bedrock-runtime` operation whose |
| 28 | * request carries a top-level `modelId`. The binding accepts one or more |
| 29 | * model references (foundation-model id, cross-region inference profile id, |
| 30 | * or full ARN), grants `actions` on exactly the resolved model ARNs, and the |
| 31 | * runtime callable defaults `modelId` to the first bound model. |
| 32 | */ |
| 33 | export const makeModelScopedHttpBinding = <I extends { modelId: string }, A, E, R>(options: { |
| 34 | /** Fully-qualified binding tag, e.g. `AWS.Bedrock.Converse`. */ |
| 35 | tag: string; |
| 36 | /** The distilled operation; `modelId` defaults to the first bound model. */ |
| 37 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 38 | /** IAM actions granted on the bound model ARNs. */ |
| 39 | actions: readonly string[]; |
| 40 | }) => |
| 41 | Effect.gen(function* () { |
| 42 | const bind = yield* AWSBindings.Bind; |
| 43 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 44 | |
| 45 | return Effect.fn(function* (model: string, ...additionalModels: string[]) { |
| 46 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 47 | const { accountId, region } = yield* currentEnv; |
| 48 | // Sort so the binding identity (SID + ARN list) is deterministic |
| 49 | // regardless of argument order. |
| 50 | const sorted = [...new Set([model, ...additionalModels])].sort(); |
| 51 | yield* bind`${options.tag}(${sorted.join(",")})`({ |
| 52 | policyStatements: [ |
| 53 | { |
| 54 | Effect: "Allow", |
| 55 | Action: [...options.actions], |
| 56 | Resource: [ |
| 57 | ...new Set(sorted.flatMap((id) => bedrockModelArns(region, accountId, id))), |
| 58 | ], |
| 59 | }, |
| 60 | ], |
| 61 | }); |
| 62 | } |
| 63 | return Effect.fn(`${options.tag}(${model})`)(function* ( |
| 64 | request: Omit<I, "modelId"> & { modelId?: string }, |
| 65 | ) { |
| 66 | return yield* op({ |
| 67 | ...request, |
| 68 | modelId: request.modelId ?? model, |
| 69 | } as unknown as I); |
| 70 | }); |
| 71 | }); |
| 72 | }); |
| 73 | |
| 74 | /** |
| 75 | * Build the impl Effect for a knowledge-base-scoped `bedrock-agent-runtime` |
| 76 | * operation whose request carries a top-level `knowledgeBaseId`. The binding |
| 77 | * grants `actions` on the bound {@link KnowledgeBase}'s ARN and the runtime |
| 78 | * callable injects its `knowledgeBaseId`. |
| 79 | */ |
| 80 | export const makeKnowledgeBaseScopedHttpBinding = < |
| 81 | I extends { knowledgeBaseId: string }, |
| 82 | A, |
| 83 | E, |
| 84 | R, |
| 85 | >(options: { |
| 86 | /** Fully-qualified binding tag, e.g. `AWS.Bedrock.Retrieve`. */ |
| 87 | tag: string; |
| 88 | /** The distilled operation; `knowledgeBaseId` is injected. */ |
| 89 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 90 | /** IAM actions granted on the knowledge-base ARN. */ |
| 91 | actions: readonly string[]; |
| 92 | }) => |
| 93 | Effect.gen(function* () { |
| 94 | const bind = yield* AWSBindings.Bind; |
| 95 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 96 | |
| 97 | return Effect.fn(function* <K extends KnowledgeBase>(knowledgeBase: K) { |
| 98 | const KnowledgeBaseId = yield* knowledgeBase.knowledgeBaseId; |
| 99 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 100 | yield* bind`${options.tag}(${knowledgeBase})`({ |
| 101 | policyStatements: [ |
| 102 | { |
| 103 | Effect: "Allow", |
| 104 | Action: [...options.actions], |
| 105 | Resource: [knowledgeBase.knowledgeBaseArn], |
| 106 | }, |
| 107 | ], |
| 108 | }); |
| 109 | } |
| 110 | return Effect.fn(`${options.tag}(${knowledgeBase.LogicalId})`)(function* ( |
| 111 | request: Omit<I, "knowledgeBaseId">, |
| 112 | ) { |
| 113 | return yield* op({ |
| 114 | ...request, |
| 115 | knowledgeBaseId: yield* KnowledgeBaseId, |
| 116 | } as unknown as I); |
| 117 | }); |
| 118 | }); |
| 119 | }); |
| 120 | |
| 121 | /** |
| 122 | * Build the impl Effect for a data-source-scoped `bedrock-agent` operation |
| 123 | * whose request carries top-level `knowledgeBaseId` + `dataSourceId` |
| 124 | * (ingestion jobs, direct document ingestion). Bedrock authorizes these |
| 125 | * actions against the parent knowledge-base ARN, so the deploy-time half |
| 126 | * grants `actions` on the bound {@link DataSource}'s knowledge base; the |
| 127 | * runtime callable injects both identifiers. |
| 128 | */ |
| 129 | export const makeDataSourceScopedHttpBinding = < |
| 130 | I extends { knowledgeBaseId: string; dataSourceId: string }, |
| 131 | A, |
| 132 | E, |
| 133 | R, |
| 134 | >(options: { |
| 135 | /** Fully-qualified binding tag, e.g. `AWS.Bedrock.StartIngestionJob`. */ |
| 136 | tag: string; |
| 137 | /** The distilled operation; both identifiers are injected. */ |
| 138 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 139 | /** IAM actions granted on the parent knowledge-base ARN. */ |
| 140 | actions: readonly string[]; |
| 141 | }) => |
| 142 | Effect.gen(function* () { |
| 143 | const bind = yield* AWSBindings.Bind; |
| 144 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 145 | |
| 146 | return Effect.fn(function* <D extends DataSource>(dataSource: D) { |
| 147 | const KnowledgeBaseId = yield* dataSource.knowledgeBaseId; |
| 148 | const DataSourceId = yield* dataSource.dataSourceId; |
| 149 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 150 | const { accountId, region } = yield* currentEnv; |
| 151 | yield* bind`${options.tag}(${dataSource})`({ |
| 152 | policyStatements: [ |
| 153 | { |
| 154 | Effect: "Allow", |
| 155 | Action: [...options.actions], |
| 156 | Resource: [ |
| 157 | dataSource.knowledgeBaseId.pipe( |
| 158 | Output.map((id) => `arn:aws:bedrock:${region}:${accountId}:knowledge-base/${id}`), |
| 159 | ), |
| 160 | ], |
| 161 | }, |
| 162 | ], |
| 163 | }); |
| 164 | } |
| 165 | return Effect.fn(`${options.tag}(${dataSource.LogicalId})`)(function* ( |
| 166 | request: Omit<I, "knowledgeBaseId" | "dataSourceId">, |
| 167 | ) { |
| 168 | return yield* op({ |
| 169 | ...request, |
| 170 | knowledgeBaseId: yield* KnowledgeBaseId, |
| 171 | dataSourceId: yield* DataSourceId, |
| 172 | } as unknown as I); |
| 173 | }); |
| 174 | }); |
| 175 | }); |
| 176 | |
| 177 | /** |
| 178 | * Build the impl Effect for an agent-alias-scoped `bedrock-agent-runtime` |
| 179 | * operation whose request carries top-level `agentId` + `agentAliasId`. The |
| 180 | * binding grants `actions` on the bound {@link AgentAlias}'s ARN and the |
| 181 | * runtime callable injects both identifiers. |
| 182 | */ |
| 183 | export const makeAgentAliasScopedHttpBinding = < |
| 184 | I extends { agentId: string; agentAliasId: string }, |
| 185 | A, |
| 186 | E, |
| 187 | R, |
| 188 | >(options: { |
| 189 | /** Fully-qualified binding tag, e.g. `AWS.Bedrock.InvokeAgent`. */ |
| 190 | tag: string; |
| 191 | /** The distilled operation; both identifiers are injected. */ |
| 192 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 193 | /** IAM actions granted on the agent-alias ARN. */ |
| 194 | actions: readonly string[]; |
| 195 | }) => |
| 196 | Effect.gen(function* () { |
| 197 | const bind = yield* AWSBindings.Bind; |
| 198 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 199 | |
| 200 | return Effect.fn(function* <AA extends AgentAlias>(alias: AA) { |
| 201 | const AgentId = yield* alias.agentId; |
| 202 | const AgentAliasId = yield* alias.agentAliasId; |
| 203 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 204 | yield* bind`${options.tag}(${alias})`({ |
| 205 | policyStatements: [ |
| 206 | { |
| 207 | Effect: "Allow", |
| 208 | Action: [...options.actions], |
| 209 | Resource: [alias.agentAliasArn], |
| 210 | }, |
| 211 | ], |
| 212 | }); |
| 213 | } |
| 214 | return Effect.fn(`${options.tag}(${alias.LogicalId})`)(function* ( |
| 215 | request: Omit<I, "agentId" | "agentAliasId">, |
| 216 | ) { |
| 217 | return yield* op({ |
| 218 | ...request, |
| 219 | agentId: yield* AgentId, |
| 220 | agentAliasId: yield* AgentAliasId, |
| 221 | } as unknown as I); |
| 222 | }); |
| 223 | }); |
| 224 | }); |
| 225 | |
| 226 | /** |
| 227 | * Build the impl Effect for a managed-RAG operation (`RetrieveAndGenerate` / |
| 228 | * `RetrieveAndGenerateStream`). The binding grants `bedrock:Retrieve` + |
| 229 | * `bedrock:RetrieveAndGenerate` on the bound {@link KnowledgeBase} plus |
| 230 | * `bedrock:InvokeModel` scoped to the named generation models (or all |
| 231 | * foundation models + cross-region inference profiles when none are named). |
| 232 | * The request passes through unchanged — the knowledge base is referenced |
| 233 | * inside `retrieveAndGenerateConfiguration`. |
| 234 | */ |
| 235 | export const makeRagHttpBinding = <I, A, E, R>(options: { |
| 236 | /** Fully-qualified binding tag, e.g. `AWS.Bedrock.RetrieveAndGenerate`. */ |
| 237 | tag: string; |
| 238 | /** The distilled operation; the request passes through unchanged. */ |
| 239 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 240 | }) => |
| 241 | Effect.gen(function* () { |
| 242 | const bind = yield* AWSBindings.Bind; |
| 243 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 244 | |
| 245 | return Effect.fn(function* <K extends KnowledgeBase>(knowledgeBase: K, ...models: string[]) { |
| 246 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 247 | const { accountId, region } = yield* currentEnv; |
| 248 | // Scope InvokeModel to the named models, or all foundation models + |
| 249 | // cross-region inference profiles when the caller names none. |
| 250 | const modelResources = |
| 251 | models.length > 0 |
| 252 | ? [...new Set(models.flatMap((id) => bedrockModelArns(region, accountId, id)))] |
| 253 | : [ |
| 254 | `arn:aws:bedrock:${region}::foundation-model/*`, |
| 255 | `arn:aws:bedrock:${region}:${accountId}:inference-profile/*`, |
| 256 | ]; |
| 257 | yield* bind`${options.tag}(${knowledgeBase})`({ |
| 258 | policyStatements: [ |
| 259 | { |
| 260 | Effect: "Allow", |
| 261 | Action: ["bedrock:Retrieve", "bedrock:RetrieveAndGenerate"], |
| 262 | Resource: [knowledgeBase.knowledgeBaseArn], |
| 263 | }, |
| 264 | { |
| 265 | Effect: "Allow", |
| 266 | Action: ["bedrock:InvokeModel"], |
| 267 | Resource: modelResources, |
| 268 | }, |
| 269 | ], |
| 270 | }); |
| 271 | } |
| 272 | return Effect.fn(`${options.tag}(${knowledgeBase.LogicalId})`)(function* (request: I) { |
| 273 | return yield* op(request); |
| 274 | }); |
| 275 | }); |
| 276 | }); |