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 IAM from "@alchemy.run/aws/IAM"; |
| 2 | import * as Kendra from "@alchemy.run/aws/Kendra"; |
| 3 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 4 | import * as S3 from "@alchemy.run/aws/S3"; |
| 5 | import * as Duration from "effect/Duration"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 8 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import path from "pathe"; |
| 11 | |
| 12 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 13 | |
| 14 | export class KendraTestFunction extends Lambda.Function<Lambda.Function>()("KendraTestFunction") {} |
| 15 | |
| 16 | /** |
| 17 | * Every route answers `{ …fields }` on success or `{ errorTag }` when the |
| 18 | * operation fails with a TYPED error — the test asserts on concrete fields |
| 19 | * (or a typed tag), which proves the binding wiring, the `IndexId`/`Id` |
| 20 | * injection, and the IAM grants. An untyped error crashes into a 500. |
| 21 | */ |
| 22 | const errorTagged = <A, E extends { _tag: string }, R>( |
| 23 | effect: Effect.Effect<A, E, R>, |
| 24 | ): Effect.Effect<A | { errorTag: string }, never, R> => |
| 25 | effect.pipe( |
| 26 | Effect.map((a): A | { errorTag: string } => a), |
| 27 | Effect.catch((e) => Effect.succeed({ errorTag: e._tag })), |
| 28 | ); |
| 29 | |
| 30 | /** |
| 31 | * Index-scoped binding fixture: deploys a real Developer-edition Kendra |
| 32 | * index (~20-30 minutes to provision, billed while it exists; Kendra is |
| 33 | * closed to new customers — skipped under `--fast` + AWS_TEST_KENDRA) plus an S3 data source and a Lambda bound to all |
| 34 | * twenty-three Kendra bindings. |
| 35 | */ |
| 36 | export default KendraTestFunction.make( |
| 37 | { |
| 38 | main, |
| 39 | functionUrl: true, |
| 40 | timeout: Duration.seconds(30), |
| 41 | }, |
| 42 | Effect.gen(function* () { |
| 43 | // Role Kendra assumes to publish CloudWatch metrics/logs. |
| 44 | const indexRole = yield* IAM.Role("KendraBindingsIndexRole", { |
| 45 | assumeRolePolicyDocument: { |
| 46 | Version: "2012-10-17", |
| 47 | Statement: [ |
| 48 | { |
| 49 | Effect: "Allow", |
| 50 | Principal: { Service: ["kendra.amazonaws.com"] }, |
| 51 | Action: ["sts:AssumeRole"], |
| 52 | }, |
| 53 | ], |
| 54 | }, |
| 55 | inlinePolicies: { |
| 56 | observability: { |
| 57 | Version: "2012-10-17", |
| 58 | Statement: [ |
| 59 | { |
| 60 | Effect: "Allow", |
| 61 | Action: ["cloudwatch:PutMetricData"], |
| 62 | Resource: ["*"], |
| 63 | Condition: { |
| 64 | StringEquals: { "cloudwatch:namespace": "AWS/Kendra" }, |
| 65 | }, |
| 66 | }, |
| 67 | { |
| 68 | Effect: "Allow", |
| 69 | Action: [ |
| 70 | "logs:DescribeLogGroups", |
| 71 | "logs:CreateLogGroup", |
| 72 | "logs:DescribeLogStreams", |
| 73 | "logs:CreateLogStream", |
| 74 | "logs:PutLogEvents", |
| 75 | ], |
| 76 | Resource: ["*"], |
| 77 | }, |
| 78 | ], |
| 79 | }, |
| 80 | }, |
| 81 | }); |
| 82 | |
| 83 | const bucket = yield* S3.Bucket("KendraBindingsDocs", { |
| 84 | forceDestroy: true, |
| 85 | }); |
| 86 | |
| 87 | // Role Kendra assumes to crawl the S3 bucket. |
| 88 | const dataSourceRole = yield* IAM.Role("KendraBindingsDataRole", { |
| 89 | assumeRolePolicyDocument: { |
| 90 | Version: "2012-10-17", |
| 91 | Statement: [ |
| 92 | { |
| 93 | Effect: "Allow", |
| 94 | Principal: { Service: ["kendra.amazonaws.com"] }, |
| 95 | Action: ["sts:AssumeRole"], |
| 96 | }, |
| 97 | ], |
| 98 | }, |
| 99 | inlinePolicies: { |
| 100 | s3: { |
| 101 | Version: "2012-10-17", |
| 102 | Statement: [ |
| 103 | { |
| 104 | Effect: "Allow", |
| 105 | Action: ["s3:GetObject", "s3:ListBucket"], |
| 106 | Resource: ["*"], |
| 107 | }, |
| 108 | { |
| 109 | Effect: "Allow", |
| 110 | Action: ["kendra:BatchPutDocument", "kendra:BatchDeleteDocument"], |
| 111 | Resource: ["*"], |
| 112 | }, |
| 113 | ], |
| 114 | }, |
| 115 | }, |
| 116 | }); |
| 117 | |
| 118 | const index = yield* Kendra.Index("BindingsIndex", { |
| 119 | edition: "DEVELOPER_EDITION", |
| 120 | roleArn: indexRole.roleArn, |
| 121 | tags: { fixture: "kendra-bindings" }, |
| 122 | }); |
| 123 | |
| 124 | const source = yield* Kendra.DataSource("BindingsSource", { |
| 125 | indexId: index.id, |
| 126 | type: "S3", |
| 127 | roleArn: dataSourceRole.roleArn, |
| 128 | configuration: { |
| 129 | S3Configuration: { BucketName: bucket.bucketName }, |
| 130 | }, |
| 131 | }); |
| 132 | |
| 133 | const query = yield* Kendra.Query(index); |
| 134 | const retrieve = yield* Kendra.Retrieve(index); |
| 135 | const suggest = yield* Kendra.GetQuerySuggestions(index); |
| 136 | const submitFeedback = yield* Kendra.SubmitFeedback(index); |
| 137 | const putDocuments = yield* Kendra.BatchPutDocument(index); |
| 138 | const deleteDocuments = yield* Kendra.BatchDeleteDocument(index); |
| 139 | const documentStatus = yield* Kendra.BatchGetDocumentStatus(index); |
| 140 | const getSnapshots = yield* Kendra.GetSnapshots(index); |
| 141 | const putPrincipalMapping = yield* Kendra.PutPrincipalMapping(index); |
| 142 | const deletePrincipalMapping = yield* Kendra.DeletePrincipalMapping(index); |
| 143 | const describePrincipalMapping = yield* Kendra.DescribePrincipalMapping(index); |
| 144 | const listStaleGroups = yield* Kendra.ListGroupsOlderThanOrderingId(index); |
| 145 | const clearSuggestions = yield* Kendra.ClearQuerySuggestions(index); |
| 146 | const suggestionsConfig = yield* Kendra.DescribeQuerySuggestionsConfig(index); |
| 147 | const updateSuggestions = yield* Kendra.UpdateQuerySuggestionsConfig(index); |
| 148 | const createAcl = yield* Kendra.CreateAccessControlConfiguration(index); |
| 149 | const describeAcl = yield* Kendra.DescribeAccessControlConfiguration(index); |
| 150 | const updateAcl = yield* Kendra.UpdateAccessControlConfiguration(index); |
| 151 | const deleteAcl = yield* Kendra.DeleteAccessControlConfiguration(index); |
| 152 | const listAcls = yield* Kendra.ListAccessControlConfigurations(index); |
| 153 | const startSync = yield* Kendra.StartDataSourceSyncJob(source); |
| 154 | const stopSync = yield* Kendra.StopDataSourceSyncJob(source); |
| 155 | const listSyncJobs = yield* Kendra.ListDataSourceSyncJobs(source); |
| 156 | |
| 157 | const bound = { |
| 158 | query, |
| 159 | retrieve, |
| 160 | suggest, |
| 161 | submitFeedback, |
| 162 | putDocuments, |
| 163 | deleteDocuments, |
| 164 | documentStatus, |
| 165 | getSnapshots, |
| 166 | putPrincipalMapping, |
| 167 | deletePrincipalMapping, |
| 168 | describePrincipalMapping, |
| 169 | listStaleGroups, |
| 170 | clearSuggestions, |
| 171 | suggestionsConfig, |
| 172 | updateSuggestions, |
| 173 | createAcl, |
| 174 | describeAcl, |
| 175 | updateAcl, |
| 176 | deleteAcl, |
| 177 | listAcls, |
| 178 | startSync, |
| 179 | stopSync, |
| 180 | listSyncJobs, |
| 181 | }; |
| 182 | |
| 183 | return { |
| 184 | fetch: Effect.gen(function* () { |
| 185 | const request = yield* HttpServerRequest; |
| 186 | const url = new URL(request.originalUrl); |
| 187 | const pathname = url.pathname; |
| 188 | |
| 189 | if (pathname === "/bindings") { |
| 190 | return yield* HttpServerResponse.json({ bound: Object.keys(bound) }); |
| 191 | } |
| 192 | |
| 193 | if (pathname === "/put-documents") { |
| 194 | const result = yield* errorTagged( |
| 195 | putDocuments({ |
| 196 | Documents: [ |
| 197 | { |
| 198 | Id: "welcome", |
| 199 | Title: "Welcome to Alchemy", |
| 200 | Blob: new TextEncoder().encode( |
| 201 | "Alchemy is an Infrastructure-as-Effects framework. " + |
| 202 | "The zanzibar passphrase is quicksilver.", |
| 203 | ), |
| 204 | ContentType: "PLAIN_TEXT", |
| 205 | }, |
| 206 | ], |
| 207 | }), |
| 208 | ); |
| 209 | return yield* HttpServerResponse.json( |
| 210 | "errorTag" in result ? result : { failed: (result.FailedDocuments ?? []).length }, |
| 211 | ); |
| 212 | } |
| 213 | |
| 214 | if (pathname === "/document-status") { |
| 215 | const result = yield* errorTagged( |
| 216 | documentStatus({ DocumentInfoList: [{ DocumentId: "welcome" }] }), |
| 217 | ); |
| 218 | return yield* HttpServerResponse.json( |
| 219 | "errorTag" in result |
| 220 | ? result |
| 221 | : { |
| 222 | status: result.DocumentStatusList?.[0]?.DocumentStatus, |
| 223 | }, |
| 224 | ); |
| 225 | } |
| 226 | |
| 227 | if (pathname === "/query") { |
| 228 | const q = url.searchParams.get("q") ?? "zanzibar"; |
| 229 | const result = yield* errorTagged(query({ QueryText: q })); |
| 230 | return yield* HttpServerResponse.json( |
| 231 | "errorTag" in result |
| 232 | ? result |
| 233 | : { |
| 234 | queryId: result.QueryId, |
| 235 | resultId: result.ResultItems?.[0]?.Id, |
| 236 | count: (result.ResultItems ?? []).length, |
| 237 | }, |
| 238 | ); |
| 239 | } |
| 240 | |
| 241 | if (pathname === "/retrieve") { |
| 242 | const q = url.searchParams.get("q") ?? "zanzibar passphrase"; |
| 243 | const result = yield* errorTagged(retrieve({ QueryText: q })); |
| 244 | return yield* HttpServerResponse.json( |
| 245 | "errorTag" in result ? result : { count: (result.ResultItems ?? []).length }, |
| 246 | ); |
| 247 | } |
| 248 | |
| 249 | if (pathname === "/suggest") { |
| 250 | const q = url.searchParams.get("q") ?? "zanzi"; |
| 251 | const result = yield* errorTagged(suggest({ QueryText: q })); |
| 252 | return yield* HttpServerResponse.json( |
| 253 | "errorTag" in result ? result : { count: (result.Suggestions ?? []).length }, |
| 254 | ); |
| 255 | } |
| 256 | |
| 257 | if (pathname === "/feedback") { |
| 258 | const queryId = url.searchParams.get("queryId") ?? ""; |
| 259 | const resultId = url.searchParams.get("resultId") ?? ""; |
| 260 | const result = yield* errorTagged( |
| 261 | submitFeedback({ |
| 262 | QueryId: queryId, |
| 263 | ClickFeedbackItems: [{ ResultId: resultId, ClickTime: new Date() }], |
| 264 | }), |
| 265 | ); |
| 266 | return yield* HttpServerResponse.json("errorTag" in result ? result : { ok: true }); |
| 267 | } |
| 268 | |
| 269 | if (pathname === "/delete-documents") { |
| 270 | const result = yield* errorTagged(deleteDocuments({ DocumentIdList: ["welcome"] })); |
| 271 | return yield* HttpServerResponse.json( |
| 272 | "errorTag" in result ? result : { failed: (result.FailedDocuments ?? []).length }, |
| 273 | ); |
| 274 | } |
| 275 | |
| 276 | if (pathname === "/snapshots") { |
| 277 | const result = yield* errorTagged( |
| 278 | getSnapshots({ |
| 279 | Interval: "ONE_WEEK_AGO", |
| 280 | MetricType: "QUERIES_BY_COUNT", |
| 281 | }), |
| 282 | ); |
| 283 | return yield* HttpServerResponse.json( |
| 284 | "errorTag" in result ? result : { header: result.SnapshotsDataHeader ?? [] }, |
| 285 | ); |
| 286 | } |
| 287 | |
| 288 | if (pathname === "/principal-mapping") { |
| 289 | const put = yield* errorTagged( |
| 290 | putPrincipalMapping({ |
| 291 | GroupId: "engineering", |
| 292 | GroupMembers: { |
| 293 | MemberUsers: [{ UserId: "user@example.com" }], |
| 294 | }, |
| 295 | }), |
| 296 | ); |
| 297 | if ("errorTag" in put) { |
| 298 | return yield* HttpServerResponse.json(put); |
| 299 | } |
| 300 | const described = yield* errorTagged( |
| 301 | describePrincipalMapping({ GroupId: "engineering" }), |
| 302 | ); |
| 303 | return yield* HttpServerResponse.json( |
| 304 | "errorTag" in described |
| 305 | ? described |
| 306 | : { |
| 307 | actions: (described.GroupOrderingIdSummaries ?? []).length, |
| 308 | }, |
| 309 | ); |
| 310 | } |
| 311 | |
| 312 | if (pathname === "/delete-principal-mapping") { |
| 313 | const result = yield* errorTagged(deletePrincipalMapping({ GroupId: "engineering" })); |
| 314 | return yield* HttpServerResponse.json("errorTag" in result ? result : { ok: true }); |
| 315 | } |
| 316 | |
| 317 | if (pathname === "/stale-groups") { |
| 318 | const result = yield* errorTagged(listStaleGroups({ OrderingId: 1 })); |
| 319 | return yield* HttpServerResponse.json( |
| 320 | "errorTag" in result ? result : { count: (result.GroupsSummaries ?? []).length }, |
| 321 | ); |
| 322 | } |
| 323 | |
| 324 | if (pathname === "/suggestions-config") { |
| 325 | const result = yield* errorTagged(suggestionsConfig()); |
| 326 | return yield* HttpServerResponse.json( |
| 327 | "errorTag" in result ? result : { mode: result.Mode }, |
| 328 | ); |
| 329 | } |
| 330 | |
| 331 | if (pathname === "/update-suggestions") { |
| 332 | const result = yield* errorTagged( |
| 333 | updateSuggestions({ |
| 334 | Mode: "LEARN_ONLY", |
| 335 | queryLogLookBackWindow: "14 days", |
| 336 | }), |
| 337 | ); |
| 338 | return yield* HttpServerResponse.json("errorTag" in result ? result : { ok: true }); |
| 339 | } |
| 340 | |
| 341 | if (pathname === "/clear-suggestions") { |
| 342 | const result = yield* errorTagged(clearSuggestions()); |
| 343 | return yield* HttpServerResponse.json("errorTag" in result ? result : { ok: true }); |
| 344 | } |
| 345 | |
| 346 | if (pathname === "/access-control") { |
| 347 | // Full create -> describe -> update -> delete round trip through |
| 348 | // the four ACL bindings. |
| 349 | const created = yield* errorTagged( |
| 350 | createAcl({ |
| 351 | Name: "block-departed-users", |
| 352 | AccessControlList: [{ Name: "departed-user", Type: "USER", Access: "DENY" }], |
| 353 | }), |
| 354 | ); |
| 355 | if ("errorTag" in created) { |
| 356 | return yield* HttpServerResponse.json(created); |
| 357 | } |
| 358 | const described = yield* errorTagged(describeAcl({ Id: created.Id })); |
| 359 | if ("errorTag" in described) { |
| 360 | return yield* HttpServerResponse.json(described); |
| 361 | } |
| 362 | const updated = yield* errorTagged( |
| 363 | updateAcl({ |
| 364 | Id: created.Id, |
| 365 | AccessControlList: [ |
| 366 | { Name: "departed-user", Type: "USER", Access: "DENY" }, |
| 367 | { Name: "auditor", Type: "USER", Access: "ALLOW" }, |
| 368 | ], |
| 369 | }), |
| 370 | ); |
| 371 | if ("errorTag" in updated) { |
| 372 | return yield* HttpServerResponse.json(updated); |
| 373 | } |
| 374 | const deleted = yield* errorTagged(deleteAcl({ Id: created.Id })); |
| 375 | return yield* HttpServerResponse.json( |
| 376 | "errorTag" in deleted ? deleted : { id: created.Id, name: described.Name }, |
| 377 | ); |
| 378 | } |
| 379 | |
| 380 | if (pathname === "/access-controls") { |
| 381 | const result = yield* errorTagged(listAcls()); |
| 382 | return yield* HttpServerResponse.json( |
| 383 | "errorTag" in result |
| 384 | ? result |
| 385 | : { count: (result.AccessControlConfigurations ?? []).length }, |
| 386 | ); |
| 387 | } |
| 388 | |
| 389 | if (pathname === "/sync") { |
| 390 | const result = yield* errorTagged(startSync()); |
| 391 | return yield* HttpServerResponse.json( |
| 392 | "errorTag" in result ? result : { executionId: result.ExecutionId }, |
| 393 | ); |
| 394 | } |
| 395 | |
| 396 | if (pathname === "/stop-sync") { |
| 397 | const result = yield* errorTagged(stopSync()); |
| 398 | return yield* HttpServerResponse.json("errorTag" in result ? result : { ok: true }); |
| 399 | } |
| 400 | |
| 401 | if (pathname === "/sync-jobs") { |
| 402 | const result = yield* errorTagged(listSyncJobs()); |
| 403 | return yield* HttpServerResponse.json( |
| 404 | "errorTag" in result ? result : { count: (result.History ?? []).length }, |
| 405 | ); |
| 406 | } |
| 407 | |
| 408 | return yield* HttpServerResponse.json( |
| 409 | { error: "Not found", method: request.method, pathname }, |
| 410 | { status: 404 }, |
| 411 | ); |
| 412 | }).pipe(Effect.orDie), |
| 413 | }; |
| 414 | }).pipe( |
| 415 | Effect.provide( |
| 416 | Layer.mergeAll( |
| 417 | Kendra.QueryHttp, |
| 418 | Kendra.RetrieveHttp, |
| 419 | Kendra.GetQuerySuggestionsHttp, |
| 420 | Kendra.SubmitFeedbackHttp, |
| 421 | Kendra.BatchPutDocumentHttp, |
| 422 | Kendra.BatchDeleteDocumentHttp, |
| 423 | Kendra.BatchGetDocumentStatusHttp, |
| 424 | Kendra.GetSnapshotsHttp, |
| 425 | Kendra.PutPrincipalMappingHttp, |
| 426 | Kendra.DeletePrincipalMappingHttp, |
| 427 | Kendra.DescribePrincipalMappingHttp, |
| 428 | Kendra.ListGroupsOlderThanOrderingIdHttp, |
| 429 | Kendra.ClearQuerySuggestionsHttp, |
| 430 | Kendra.DescribeQuerySuggestionsConfigHttp, |
| 431 | Kendra.UpdateQuerySuggestionsConfigHttp, |
| 432 | Kendra.CreateAccessControlConfigurationHttp, |
| 433 | Kendra.DescribeAccessControlConfigurationHttp, |
| 434 | Kendra.UpdateAccessControlConfigurationHttp, |
| 435 | Kendra.DeleteAccessControlConfigurationHttp, |
| 436 | Kendra.ListAccessControlConfigurationsHttp, |
| 437 | Kendra.StartDataSourceSyncJobHttp, |
| 438 | Kendra.StopDataSourceSyncJobHttp, |
| 439 | Kendra.ListDataSourceSyncJobsHttp, |
| 440 | ), |
| 441 | ), |
| 442 | ), |
| 443 | ); |