The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Output from "alchemy/Output"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as AWSBindings from "../AWSBindings.ts"; |
| 4 | import type { Application } from "./Application.ts"; |
| 5 | import type { DataSource } from "./DataSource.ts"; |
| 6 | import type { Index } from "./SearchIndex.ts"; |
| 7 | import type { WebExperience } from "./WebExperience.ts"; |
| 8 | |
| 9 | /** |
| 10 | * Shared scaffolding for AWS QBusiness HTTP bindings. |
| 11 | * |
| 12 | * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a |
| 13 | * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the four |
| 14 | * builders below. Everything except the operation and the IAM action list |
| 15 | * is boilerplate: every Amazon Q Business operation is scoped to one |
| 16 | * application (whose id is injected as `applicationId`), and the |
| 17 | * index/data-source/web-experience operations additionally inject their |
| 18 | * sub-resource ids and receive grants on the sub-resource ARN plus its |
| 19 | * parents (Q Business authorizes most actions against both the application |
| 20 | * and the sub-resource). |
| 21 | */ |
| 22 | |
| 23 | /** |
| 24 | * Build the impl Effect for an application-scoped Q Business operation |
| 25 | * (chat, conversations, users, subscriptions, chat controls, policy): the |
| 26 | * runtime callable injects the bound {@link Application}'s id as |
| 27 | * `applicationId` and the deploy-time half grants `actions` on the |
| 28 | * application ARN (plus any `subResources` suffix patterns, e.g. |
| 29 | * `retriever/*` for `SearchRelevantContent`, which Q Business additionally |
| 30 | * authorizes against the retriever it searches). |
| 31 | */ |
| 32 | export const makeQBusinessApplicationHttpBinding = < |
| 33 | I extends { applicationId: string }, |
| 34 | A, |
| 35 | E, |
| 36 | R, |
| 37 | >(options: { |
| 38 | /** Fully-qualified binding tag, e.g. `AWS.QBusiness.ChatSync`. */ |
| 39 | tag: string; |
| 40 | /** The distilled operation; `applicationId` is injected from the application. */ |
| 41 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 42 | /** IAM actions granted on the application ARN. */ |
| 43 | actions: readonly string[]; |
| 44 | /** |
| 45 | * Extra ARN suffix patterns (relative to the application ARN) the |
| 46 | * actions are also granted on, e.g. `retriever/*`. |
| 47 | */ |
| 48 | subResources?: readonly string[]; |
| 49 | }) => |
| 50 | Effect.gen(function* () { |
| 51 | const bind = yield* AWSBindings.Bind; |
| 52 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 53 | |
| 54 | return Effect.fn(function* (application: Application) { |
| 55 | const applicationId = yield* application.applicationId; |
| 56 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 57 | yield* bind`${options.tag}(${application})`({ |
| 58 | policyStatements: [ |
| 59 | { |
| 60 | Effect: "Allow", |
| 61 | Action: [...options.actions], |
| 62 | Resource: [ |
| 63 | application.applicationArn, |
| 64 | ...(options.subResources ?? []).map( |
| 65 | (suffix) => Output.interpolate`${application.applicationArn}/${suffix}`, |
| 66 | ), |
| 67 | ], |
| 68 | }, |
| 69 | ], |
| 70 | }); |
| 71 | } |
| 72 | return Effect.fn(`${options.tag}(${application.LogicalId})`)(function* ( |
| 73 | request?: Omit<I, "applicationId">, |
| 74 | ) { |
| 75 | return yield* op({ |
| 76 | ...request, |
| 77 | applicationId: yield* applicationId, |
| 78 | } as I); |
| 79 | }); |
| 80 | }); |
| 81 | }); |
| 82 | |
| 83 | /** |
| 84 | * Build the impl Effect for an index-scoped Q Business operation (document |
| 85 | * batches, groups, document reads): the runtime callable injects the bound |
| 86 | * {@link Index}'s `applicationId` + `indexId` and the deploy-time half |
| 87 | * grants `actions` on the index ARN **and** its parent application ARN — |
| 88 | * Q Business authorizes index actions against both. |
| 89 | */ |
| 90 | export const makeQBusinessIndexHttpBinding = < |
| 91 | I extends { applicationId: string; indexId: string }, |
| 92 | A, |
| 93 | E, |
| 94 | R, |
| 95 | >(options: { |
| 96 | /** Fully-qualified binding tag, e.g. `AWS.QBusiness.BatchPutDocument`. */ |
| 97 | tag: string; |
| 98 | /** |
| 99 | * The distilled operation; `applicationId` and `indexId` are injected |
| 100 | * from the index. |
| 101 | */ |
| 102 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 103 | /** IAM actions granted on the index ARN + its parent application ARN. */ |
| 104 | actions: readonly string[]; |
| 105 | /** |
| 106 | * Extra ARN suffix patterns (relative to the index ARN) the actions are |
| 107 | * also granted on, e.g. `data-source/*` for the principal-group |
| 108 | * operations that also act on data-source-scoped groups. |
| 109 | */ |
| 110 | subResources?: readonly string[]; |
| 111 | }) => |
| 112 | Effect.gen(function* () { |
| 113 | const bind = yield* AWSBindings.Bind; |
| 114 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 115 | |
| 116 | return Effect.fn(function* (index: Index) { |
| 117 | const applicationId = yield* index.applicationId; |
| 118 | const indexId = yield* index.indexId; |
| 119 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 120 | yield* bind`${options.tag}(${index})`({ |
| 121 | policyStatements: [ |
| 122 | { |
| 123 | Effect: "Allow", |
| 124 | Action: [...options.actions], |
| 125 | Resource: [ |
| 126 | index.indexArn, |
| 127 | // The index ARN is `…:application/{appId}/index/{indexId}` |
| 128 | // — the parent application ARN is its prefix. |
| 129 | index.indexArn.pipe(Output.map((arn) => arn.split("/index/")[0]!)), |
| 130 | ...(options.subResources ?? []).map( |
| 131 | (suffix) => Output.interpolate`${index.indexArn}/${suffix}`, |
| 132 | ), |
| 133 | ], |
| 134 | }, |
| 135 | ], |
| 136 | }); |
| 137 | } |
| 138 | return Effect.fn(`${options.tag}(${index.LogicalId})`)(function* ( |
| 139 | request?: Omit<I, "applicationId" | "indexId">, |
| 140 | ) { |
| 141 | return yield* op({ |
| 142 | ...request, |
| 143 | applicationId: yield* applicationId, |
| 144 | indexId: yield* indexId, |
| 145 | } as I); |
| 146 | }); |
| 147 | }); |
| 148 | }); |
| 149 | |
| 150 | /** |
| 151 | * Build the impl Effect for a data-source-scoped Q Business operation (the |
| 152 | * sync job start/stop/list trio): the runtime callable injects the bound |
| 153 | * {@link DataSource}'s `applicationId` + `indexId` + `dataSourceId`; the |
| 154 | * deploy-time half grants `actions` on the data source ARN **and** its |
| 155 | * parent index + application ARNs. |
| 156 | */ |
| 157 | export const makeQBusinessDataSourceHttpBinding = < |
| 158 | I extends { applicationId: string; indexId: string; dataSourceId: string }, |
| 159 | A, |
| 160 | E, |
| 161 | R, |
| 162 | >(options: { |
| 163 | /** Fully-qualified binding tag, e.g. `AWS.QBusiness.StartDataSourceSyncJob`. */ |
| 164 | tag: string; |
| 165 | /** |
| 166 | * The distilled operation; `applicationId`, `indexId`, and |
| 167 | * `dataSourceId` are injected from the data source. |
| 168 | */ |
| 169 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 170 | /** |
| 171 | * IAM actions granted on the data source ARN + its parent index and |
| 172 | * application ARNs. |
| 173 | */ |
| 174 | actions: readonly string[]; |
| 175 | }) => |
| 176 | Effect.gen(function* () { |
| 177 | const bind = yield* AWSBindings.Bind; |
| 178 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 179 | |
| 180 | return Effect.fn(function* (dataSource: DataSource) { |
| 181 | const applicationId = yield* dataSource.applicationId; |
| 182 | const indexId = yield* dataSource.indexId; |
| 183 | const dataSourceId = yield* dataSource.dataSourceId; |
| 184 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 185 | yield* bind`${options.tag}(${dataSource})`({ |
| 186 | policyStatements: [ |
| 187 | { |
| 188 | Effect: "Allow", |
| 189 | Action: [...options.actions], |
| 190 | Resource: [ |
| 191 | dataSource.dataSourceArn, |
| 192 | // The data source ARN is |
| 193 | // `…:application/{appId}/index/{indexId}/data-source/{dsId}` |
| 194 | // — the parent index and application ARNs are prefixes. |
| 195 | dataSource.dataSourceArn.pipe(Output.map((arn) => arn.split("/data-source/")[0]!)), |
| 196 | dataSource.dataSourceArn.pipe(Output.map((arn) => arn.split("/index/")[0]!)), |
| 197 | ], |
| 198 | }, |
| 199 | ], |
| 200 | }); |
| 201 | } |
| 202 | return Effect.fn(`${options.tag}(${dataSource.LogicalId})`)(function* ( |
| 203 | request?: Omit<I, "applicationId" | "indexId" | "dataSourceId">, |
| 204 | ) { |
| 205 | return yield* op({ |
| 206 | ...request, |
| 207 | applicationId: yield* applicationId, |
| 208 | indexId: yield* indexId, |
| 209 | dataSourceId: yield* dataSourceId, |
| 210 | } as I); |
| 211 | }); |
| 212 | }); |
| 213 | }); |
| 214 | |
| 215 | /** |
| 216 | * Build the impl Effect for a web-experience-scoped Q Business operation |
| 217 | * (anonymous URL minting): the runtime callable injects the bound |
| 218 | * {@link WebExperience}'s `applicationId` + `webExperienceId`; the |
| 219 | * deploy-time half grants `actions` on the web experience ARN **and** its |
| 220 | * parent application ARN. |
| 221 | */ |
| 222 | export const makeQBusinessWebExperienceHttpBinding = < |
| 223 | I extends { applicationId: string; webExperienceId: string }, |
| 224 | A, |
| 225 | E, |
| 226 | R, |
| 227 | Req = Omit<I, "applicationId" | "webExperienceId">, |
| 228 | >(options: { |
| 229 | /** Fully-qualified binding tag, e.g. `AWS.QBusiness.CreateAnonymousWebExperienceUrl`. */ |
| 230 | tag: string; |
| 231 | /** |
| 232 | * The distilled operation; `applicationId` and `webExperienceId` are |
| 233 | * injected from the web experience. |
| 234 | */ |
| 235 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 236 | /** |
| 237 | * IAM actions granted on the web experience ARN + its parent application |
| 238 | * ARN. |
| 239 | */ |
| 240 | actions: readonly string[]; |
| 241 | /** |
| 242 | * Map the public request shape to the wire request (defaults to |
| 243 | * identity) — e.g. `CreateAnonymousWebExperienceUrl` converts a |
| 244 | * `Duration.Input` into the wire `sessionDurationInMinutes`. |
| 245 | */ |
| 246 | prepare?: (request: Req | undefined) => Omit<I, "applicationId" | "webExperienceId">; |
| 247 | }) => |
| 248 | Effect.gen(function* () { |
| 249 | const bind = yield* AWSBindings.Bind; |
| 250 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 251 | |
| 252 | return Effect.fn(function* (webExperience: WebExperience) { |
| 253 | const applicationId = yield* webExperience.applicationId; |
| 254 | const webExperienceId = yield* webExperience.webExperienceId; |
| 255 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 256 | yield* bind`${options.tag}(${webExperience})`({ |
| 257 | policyStatements: [ |
| 258 | { |
| 259 | Effect: "Allow", |
| 260 | Action: [...options.actions], |
| 261 | Resource: [ |
| 262 | webExperience.webExperienceArn, |
| 263 | // The web experience ARN is |
| 264 | // `…:application/{appId}/web-experience/{weId}` — the |
| 265 | // parent application ARN is its prefix. |
| 266 | webExperience.webExperienceArn.pipe( |
| 267 | Output.map((arn) => arn.split("/web-experience/")[0]!), |
| 268 | ), |
| 269 | ], |
| 270 | }, |
| 271 | ], |
| 272 | }); |
| 273 | } |
| 274 | return Effect.fn(`${options.tag}(${webExperience.LogicalId})`)(function* (request?: Req) { |
| 275 | const wire = options.prepare |
| 276 | ? options.prepare(request) |
| 277 | : (request as unknown as Omit<I, "applicationId" | "webExperienceId"> | undefined); |
| 278 | return yield* op({ |
| 279 | ...wire, |
| 280 | applicationId: yield* applicationId, |
| 281 | webExperienceId: yield* webExperienceId, |
| 282 | } as I); |
| 283 | }); |
| 284 | }); |
| 285 | }); |