The best way to control Claude Code, Codex and more from any computer or phone

main
ts 234 lines 8.55 KB
Raw
1 import * as Bedrock from "@alchemy.run/aws/Bedrock";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as S3 from "@alchemy.run/aws/S3";
4 import * as Duration from "effect/Duration";
5 import * as Effect from "effect/Effect";
6 import { HttpServerRequest } from "effect/http/HttpServerRequest";
7 import * as HttpServerResponse from "effect/http/HttpServerResponse";
8 import * as Layer from "effect/Layer";
9 import * as Stream from "effect/Stream";
10 import path from "pathe";
11
12 const main = path.resolve(import.meta.dirname, "kb-handler.ts");
13
14 // Same env-gated vector-store prerequisites as KnowledgeBase.slow.test.ts —
15 // this fixture only ever deploys from that (skipIf-gated) suite.
16 const roleArn = process.env.BEDROCK_KB_ROLE_ARN ?? "";
17 const collectionArn = process.env.BEDROCK_KB_COLLECTION_ARN ?? "";
18 const indexName = process.env.BEDROCK_KB_INDEX_NAME ?? "bedrock-index";
19 const embeddingModelArn = process.env.BEDROCK_KB_EMBEDDING_MODEL_ARN ?? "";
20
21 // Nova Micro via the us cross-region inference profile — the cheapest
22 // on-demand generation model in the testing account.
23 const MODEL = "us.amazon.nova-micro-v1:0";
24
25 const DOC_ID = "welcome-doc";
26 const DOC_TEXT =
27 "Alchemy is an Infrastructure-as-Effects framework that deploys cloud resources from typed Effect programs.";
28
29 export class BedrockKbTestFunction extends Lambda.Function<Lambda.Function>()(
30 "BedrockKbTestFunction",
31 ) {}
32
33 export default BedrockKbTestFunction.make(
34 {
35 main,
36 functionUrl: true,
37 // Retrieval + generation regularly exceed Lambda's 3s default timeout.
38 timeout: Duration.seconds(60),
39 },
40 Effect.gen(function* () {
41 const bucket = yield* S3.Bucket("kb-docs", { forceDestroy: true });
42 const kb = yield* Bedrock.KnowledgeBase("BindingsKb", {
43 roleArn,
44 knowledgeBaseConfiguration: {
45 type: "VECTOR",
46 vectorKnowledgeBaseConfiguration: {
47 embeddingModelArn,
48 },
49 },
50 storageConfiguration: {
51 type: "OPENSEARCH_SERVERLESS",
52 opensearchServerlessConfiguration: {
53 collectionArn,
54 vectorIndexName: indexName,
55 fieldMapping: {
56 vectorField: "bedrock-vector",
57 textField: "bedrock-text",
58 metadataField: "bedrock-metadata",
59 },
60 },
61 },
62 });
63 // S3-backed source exercises the ingestion-job bindings; CUSTOM source
64 // exercises direct document ingestion.
65 const s3Source = yield* Bedrock.DataSource("BindingsS3Source", {
66 knowledgeBaseId: kb.knowledgeBaseId,
67 dataSourceConfiguration: {
68 type: "S3",
69 s3Configuration: { bucketArn: bucket.bucketArn },
70 },
71 dataDeletionPolicy: "DELETE",
72 });
73 const customSource = yield* Bedrock.DataSource("BindingsCustomSource", {
74 knowledgeBaseId: kb.knowledgeBaseId,
75 dataSourceConfiguration: { type: "CUSTOM" },
76 dataDeletionPolicy: "DELETE",
77 });
78
79 const knowledgeBaseId = yield* kb.knowledgeBaseId;
80
81 const ingestDocuments = yield* Bedrock.IngestKnowledgeBaseDocuments(customSource);
82 const getDocuments = yield* Bedrock.GetKnowledgeBaseDocuments(customSource);
83 const listDocuments = yield* Bedrock.ListKnowledgeBaseDocuments(customSource);
84 const deleteDocuments = yield* Bedrock.DeleteKnowledgeBaseDocuments(customSource);
85 const startIngestionJob = yield* Bedrock.StartIngestionJob(s3Source);
86 const getIngestionJob = yield* Bedrock.GetIngestionJob(s3Source);
87 const listIngestionJobs = yield* Bedrock.ListIngestionJobs(s3Source);
88 const retrieve = yield* Bedrock.Retrieve(kb);
89 const ragStream = yield* Bedrock.RetrieveAndGenerateStream(kb, MODEL);
90
91 return {
92 fetch: Effect.gen(function* () {
93 const request = yield* HttpServerRequest;
94 const url = new URL(request.originalUrl);
95 const pathname = url.pathname;
96
97 // Cheap readiness route — no Bedrock call.
98 if (request.method === "GET" && pathname === "/ping") {
99 return yield* HttpServerResponse.json({ ok: true });
100 }
101
102 if (request.method === "POST" && pathname === "/ingest") {
103 const { documentDetails } = yield* ingestDocuments({
104 documents: [
105 {
106 content: {
107 dataSourceType: "CUSTOM",
108 custom: {
109 customDocumentIdentifier: { id: DOC_ID },
110 sourceType: "IN_LINE",
111 inlineContent: {
112 type: "TEXT",
113 textContent: { data: DOC_TEXT },
114 },
115 },
116 },
117 },
118 ],
119 });
120 return yield* HttpServerResponse.json({
121 status: documentDetails?.[0]?.status,
122 });
123 }
124
125 if (request.method === "GET" && pathname === "/doc-status") {
126 const { documentDetails } = yield* getDocuments({
127 documentIdentifiers: [{ dataSourceType: "CUSTOM", custom: { id: DOC_ID } }],
128 });
129 return yield* HttpServerResponse.json({
130 status: documentDetails?.[0]?.status,
131 });
132 }
133
134 if (request.method === "GET" && pathname === "/docs") {
135 const { documentDetails } = yield* listDocuments({});
136 return yield* HttpServerResponse.json({
137 count: documentDetails?.length ?? 0,
138 });
139 }
140
141 if (request.method === "POST" && pathname === "/delete-doc") {
142 const { documentDetails } = yield* deleteDocuments({
143 documentIdentifiers: [{ dataSourceType: "CUSTOM", custom: { id: DOC_ID } }],
144 });
145 return yield* HttpServerResponse.json({
146 status: documentDetails?.[0]?.status,
147 });
148 }
149
150 if (request.method === "POST" && pathname === "/sync") {
151 const { ingestionJob } = yield* startIngestionJob({});
152 return yield* HttpServerResponse.json({
153 jobId: ingestionJob.ingestionJobId,
154 status: ingestionJob.status,
155 });
156 }
157
158 if (request.method === "GET" && pathname === "/job") {
159 const jobId = url.searchParams.get("id") ?? "";
160 const { ingestionJob } = yield* getIngestionJob({
161 ingestionJobId: jobId,
162 });
163 return yield* HttpServerResponse.json({
164 status: ingestionJob.status,
165 });
166 }
167
168 if (request.method === "GET" && pathname === "/jobs") {
169 const { ingestionJobSummaries } = yield* listIngestionJobs({});
170 return yield* HttpServerResponse.json({
171 count: ingestionJobSummaries.length,
172 });
173 }
174
175 if (request.method === "GET" && pathname === "/retrieve") {
176 const result = yield* retrieve({
177 retrievalQuery: { text: "What is Alchemy?" },
178 retrievalConfiguration: {
179 vectorSearchConfiguration: { numberOfResults: 3 },
180 },
181 });
182 return yield* HttpServerResponse.json({
183 count: result.retrievalResults.length,
184 passages: result.retrievalResults.map((r) => r.content?.text),
185 });
186 }
187
188 if (request.method === "GET" && pathname === "/rag-stream") {
189 // knowledgeBaseId is an Accessor<string> at init scope — yield it
190 // again at request time to resolve the concrete value.
191 const kbId = yield* knowledgeBaseId;
192 const result = yield* ragStream({
193 input: { text: "What is Alchemy?" },
194 retrieveAndGenerateConfiguration: {
195 type: "KNOWLEDGE_BASE",
196 knowledgeBaseConfiguration: {
197 knowledgeBaseId: kbId,
198 modelArn: MODEL,
199 },
200 },
201 });
202 const events = yield* Stream.runCollect(result.stream);
203 const text = Array.from(events)
204 .map((event) => event.output?.text ?? "")
205 .join("");
206 return yield* HttpServerResponse.json({
207 sessionId: result.sessionId,
208 events: events.length,
209 text,
210 });
211 }
212
213 return yield* HttpServerResponse.json(
214 { error: "Not found", method: request.method, pathname },
215 { status: 404 },
216 );
217 }).pipe(Effect.orDie),
218 };
219 }).pipe(
220 Effect.provide(
221 Layer.mergeAll(
222 Bedrock.DeleteKnowledgeBaseDocumentsHttp,
223 Bedrock.GetIngestionJobHttp,
224 Bedrock.GetKnowledgeBaseDocumentsHttp,
225 Bedrock.IngestKnowledgeBaseDocumentsHttp,
226 Bedrock.ListIngestionJobsHttp,
227 Bedrock.ListKnowledgeBaseDocumentsHttp,
228 Bedrock.RetrieveHttp,
229 Bedrock.RetrieveAndGenerateStreamHttp,
230 Bedrock.StartIngestionJobHttp,
231 ),
232 ),
233 ),
234 );