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 AWS from "@alchemy.run/aws"; |
| 2 | import { DataSource, KnowledgeBase } from "@alchemy.run/aws/Bedrock"; |
| 3 | import * as S3 from "@alchemy.run/aws/S3"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as bedrock from "@distilled.cloud/aws/bedrock-agent"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import BedrockKbTestFunctionLive, { BedrockKbTestFunction } from "./kb-handler"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: AWS.providers() }); |
| 14 | |
| 15 | // The KnowledgeBase + DataSource live lifecycle needs a pre-existing vector |
| 16 | // store. Provisioning an OpenSearch Serverless collection is cheap, but the |
| 17 | // vector INDEX must be created through the collection's data-plane API |
| 18 | // (SigV4-signed HTTP to the collection endpoint) — there is no AWS control |
| 19 | // -plane call for it — which is out of scope for this suite. Supply a ready |
| 20 | // collection + index (and a KB service role that can read the embedding model, |
| 21 | // the collection, and S3) via env vars to run this against real infra. |
| 22 | // |
| 23 | // BEDROCK_KB_ROLE_ARN=arn:aws:iam::...:role/... |
| 24 | // BEDROCK_KB_COLLECTION_ARN=arn:aws:aoss:...:collection/... |
| 25 | // BEDROCK_KB_INDEX_NAME=bedrock-index |
| 26 | // BEDROCK_KB_EMBEDDING_MODEL_ARN=arn:aws:bedrock:us-west-2::foundation-model/amazon.titan-embed-text-v2:0 |
| 27 | const roleArn = process.env.BEDROCK_KB_ROLE_ARN; |
| 28 | const collectionArn = process.env.BEDROCK_KB_COLLECTION_ARN; |
| 29 | const indexName = process.env.BEDROCK_KB_INDEX_NAME ?? "bedrock-index"; |
| 30 | const embeddingModelArn = process.env.BEDROCK_KB_EMBEDDING_MODEL_ARN; |
| 31 | |
| 32 | const gated = !!process.env.FAST || !roleArn || !collectionArn || !embeddingModelArn; |
| 33 | |
| 34 | const findKb = (knowledgeBaseId: string) => |
| 35 | bedrock.getKnowledgeBase({ knowledgeBaseId }).pipe( |
| 36 | Effect.map((r) => r.knowledgeBase), |
| 37 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)), |
| 38 | ); |
| 39 | |
| 40 | class StillExists extends Data.TaggedError("StillExists")<{ |
| 41 | readonly id: string; |
| 42 | }> {} |
| 43 | |
| 44 | const assertKbGone = (knowledgeBaseId: string) => |
| 45 | findKb(knowledgeBaseId).pipe( |
| 46 | Effect.flatMap((kb) => |
| 47 | kb === undefined || kb.status === "DELETING" |
| 48 | ? Effect.void |
| 49 | : Effect.fail(new StillExists({ id: knowledgeBaseId })), |
| 50 | ), |
| 51 | Effect.retry({ |
| 52 | while: (e) => e._tag === "StillExists", |
| 53 | schedule: Schedule.max([Schedule.exponential(2000), Schedule.recurs(10)]), |
| 54 | }), |
| 55 | ); |
| 56 | |
| 57 | test.provider.skipIf(gated)( |
| 58 | "create knowledge base + S3 data source, then delete", |
| 59 | (stack) => |
| 60 | Effect.gen(function* () { |
| 61 | yield* stack.destroy(); |
| 62 | |
| 63 | const result = yield* stack.deploy( |
| 64 | Effect.gen(function* () { |
| 65 | const bucket = yield* S3.Bucket("kb-docs", { forceDestroy: true }); |
| 66 | const kb = yield* KnowledgeBase("TestKb", { |
| 67 | roleArn: roleArn!, |
| 68 | knowledgeBaseConfiguration: { |
| 69 | type: "VECTOR", |
| 70 | vectorKnowledgeBaseConfiguration: { |
| 71 | embeddingModelArn: embeddingModelArn!, |
| 72 | }, |
| 73 | }, |
| 74 | storageConfiguration: { |
| 75 | type: "OPENSEARCH_SERVERLESS", |
| 76 | opensearchServerlessConfiguration: { |
| 77 | collectionArn: collectionArn!, |
| 78 | vectorIndexName: indexName, |
| 79 | fieldMapping: { |
| 80 | vectorField: "bedrock-vector", |
| 81 | textField: "bedrock-text", |
| 82 | metadataField: "bedrock-metadata", |
| 83 | }, |
| 84 | }, |
| 85 | }, |
| 86 | tags: { Environment: "test" }, |
| 87 | }); |
| 88 | const source = yield* DataSource("TestSource", { |
| 89 | knowledgeBaseId: kb.knowledgeBaseId, |
| 90 | dataSourceConfiguration: { |
| 91 | type: "S3", |
| 92 | s3Configuration: { bucketArn: bucket.bucketArn }, |
| 93 | }, |
| 94 | dataDeletionPolicy: "DELETE", |
| 95 | }); |
| 96 | return { |
| 97 | knowledgeBaseId: kb.knowledgeBaseId, |
| 98 | knowledgeBaseArn: kb.knowledgeBaseArn, |
| 99 | dataSourceId: source.dataSourceId, |
| 100 | }; |
| 101 | }), |
| 102 | ); |
| 103 | |
| 104 | const kb = yield* findKb(result.knowledgeBaseId); |
| 105 | expect(kb?.status).toBe("ACTIVE"); |
| 106 | |
| 107 | const ds = yield* bedrock |
| 108 | .getDataSource({ |
| 109 | knowledgeBaseId: result.knowledgeBaseId, |
| 110 | dataSourceId: result.dataSourceId, |
| 111 | }) |
| 112 | .pipe(Effect.map((r) => r.dataSource)); |
| 113 | expect(ds.status).toBe("AVAILABLE"); |
| 114 | |
| 115 | yield* stack.destroy(); |
| 116 | yield* assertKbGone(result.knowledgeBaseId); |
| 117 | }), |
| 118 | { |
| 119 | tags: ["provider:aws", "provider:aws:bedrock", "provider:aws:s3", "live"], |
| 120 | timeout: 600_000, |
| 121 | }, |
| 122 | ); |
| 123 | |
| 124 | // Runtime coverage for the KB-scoped bindings (document ingestion, ingestion |
| 125 | // jobs, retrieve, streaming RAG) through a deployed Lambda — gated behind the |
| 126 | // same vector-store env prerequisites as the lifecycle test above. |
| 127 | test.provider.skipIf(gated)( |
| 128 | "KB-scoped bindings work from a deployed Lambda", |
| 129 | (stack) => |
| 130 | Effect.gen(function* () { |
| 131 | yield* stack.destroy(); |
| 132 | |
| 133 | const { functionUrl } = yield* stack.deploy( |
| 134 | Effect.gen(function* () { |
| 135 | return yield* BedrockKbTestFunction; |
| 136 | }).pipe(Effect.provide(BedrockKbTestFunctionLive)), |
| 137 | ); |
| 138 | const baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 139 | |
| 140 | // Fresh function URLs take a few seconds to start serving 200s. |
| 141 | yield* HttpClient.get(`${baseUrl}/ping`).pipe( |
| 142 | Effect.flatMap((r) => |
| 143 | r.status === 200 ? Effect.succeed(r) : Effect.fail(new Error(`not ready: ${r.status}`)), |
| 144 | ), |
| 145 | Effect.retry({ |
| 146 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]), |
| 147 | }), |
| 148 | ); |
| 149 | |
| 150 | const getJson = (path: string) => |
| 151 | HttpClient.get(`${baseUrl}${path}`).pipe(Effect.flatMap((r) => r.json)); |
| 152 | const postJson = (path: string) => |
| 153 | HttpClient.post(`${baseUrl}${path}`).pipe(Effect.flatMap((r) => r.json)); |
| 154 | |
| 155 | // 1. Direct document ingestion into the CUSTOM data source. |
| 156 | const ingested = (yield* postJson("/ingest")) as { status?: string }; |
| 157 | expect(ingested.status).toBeDefined(); |
| 158 | |
| 159 | // 2. Poll the document to INDEXED. |
| 160 | const docStatus = (yield* getJson("/doc-status").pipe( |
| 161 | Effect.repeat({ |
| 162 | schedule: Schedule.spaced("5 seconds"), |
| 163 | until: (r): boolean => |
| 164 | (r as { status?: string }).status === "INDEXED" || |
| 165 | (r as { status?: string }).status === "FAILED", |
| 166 | times: 36, |
| 167 | }), |
| 168 | )) as { status?: string }; |
| 169 | expect(docStatus.status).toBe("INDEXED"); |
| 170 | |
| 171 | // 3. The document is tracked. |
| 172 | const docs = (yield* getJson("/docs")) as { count: number }; |
| 173 | expect(docs.count).toBeGreaterThan(0); |
| 174 | |
| 175 | // 4. Semantic retrieval finds the ingested passage. |
| 176 | const retrieved = (yield* getJson("/retrieve").pipe( |
| 177 | Effect.repeat({ |
| 178 | schedule: Schedule.spaced("5 seconds"), |
| 179 | until: (r): boolean => (r as { count: number }).count > 0, |
| 180 | times: 12, |
| 181 | }), |
| 182 | )) as { count: number; passages: string[] }; |
| 183 | expect(retrieved.count).toBeGreaterThan(0); |
| 184 | |
| 185 | // 5. Streaming RAG produces a grounded, non-empty answer. |
| 186 | const rag = (yield* getJson("/rag-stream")) as { |
| 187 | events: number; |
| 188 | text: string; |
| 189 | }; |
| 190 | expect(rag.events).toBeGreaterThan(0); |
| 191 | expect(rag.text.trim().length).toBeGreaterThan(0); |
| 192 | |
| 193 | // 6. Ingestion-job lifecycle on the S3 data source. |
| 194 | const sync = (yield* postJson("/sync")) as { jobId: string }; |
| 195 | expect(sync.jobId).toBeDefined(); |
| 196 | const job = (yield* getJson(`/job?id=${sync.jobId}`).pipe( |
| 197 | Effect.repeat({ |
| 198 | schedule: Schedule.spaced("5 seconds"), |
| 199 | until: (r): boolean => |
| 200 | (r as { status: string }).status === "COMPLETE" || |
| 201 | (r as { status: string }).status === "FAILED", |
| 202 | times: 36, |
| 203 | }), |
| 204 | )) as { status: string }; |
| 205 | expect(job.status).toBe("COMPLETE"); |
| 206 | const jobs = (yield* getJson("/jobs")) as { count: number }; |
| 207 | expect(jobs.count).toBeGreaterThan(0); |
| 208 | |
| 209 | // 7. Delete the document again. |
| 210 | const deleted = (yield* postJson("/delete-doc")) as { status?: string }; |
| 211 | expect(deleted.status).toBeDefined(); |
| 212 | |
| 213 | yield* stack.destroy(); |
| 214 | }), |
| 215 | { |
| 216 | tags: [ |
| 217 | "provider:aws", |
| 218 | "provider:aws:bedrock", |
| 219 | "provider:aws:lambda", |
| 220 | "provider:aws:s3", |
| 221 | "live", |
| 222 | ], |
| 223 | timeout: 600_000, |
| 224 | }, |
| 225 | ); |