The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 2 | import * as Omics from "@alchemy.run/aws/Omics"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import path from "pathe"; |
| 8 | |
| 9 | const main = path.resolve(import.meta.dirname, "bindings-handler.ts"); |
| 10 | |
| 11 | // A syntactically-plausible but nonexistent read-set / run id. |
| 12 | const BOGUS_ID = "0000000000"; |
| 13 | |
| 14 | export class OmicsBindingsFunction extends Lambda.Function<Lambda.Function>()( |
| 15 | "OmicsBindingsFunction", |
| 16 | ) {} |
| 17 | |
| 18 | export default OmicsBindingsFunction.make( |
| 19 | { |
| 20 | main, |
| 21 | functionUrl: true, |
| 22 | }, |
| 23 | Effect.gen(function* () { |
| 24 | const sequenceStore = yield* Omics.SequenceStore("BindingsSequenceStore"); |
| 25 | const referenceStore = yield* Omics.ReferenceStore("BindingsReferenceStore"); |
| 26 | |
| 27 | // Resource-scoped bindings — the store id is injected automatically. |
| 28 | const listReadSets = yield* Omics.ListReadSets(sequenceStore); |
| 29 | const getReadSetMetadata = yield* Omics.GetReadSetMetadata(sequenceStore); |
| 30 | const startReadSetImportJob = yield* Omics.StartReadSetImportJob(sequenceStore); |
| 31 | const batchDeleteReadSet = yield* Omics.BatchDeleteReadSet(sequenceStore); |
| 32 | const listReferences = yield* Omics.ListReferences(referenceStore); |
| 33 | const getReferenceMetadata = yield* Omics.GetReferenceMetadata(referenceStore); |
| 34 | |
| 35 | // Account-level run-control bindings. |
| 36 | const listRuns = yield* Omics.ListRuns(); |
| 37 | const getRun = yield* Omics.GetRun(); |
| 38 | |
| 39 | const bound = { |
| 40 | listReadSets, |
| 41 | getReadSetMetadata, |
| 42 | startReadSetImportJob, |
| 43 | batchDeleteReadSet, |
| 44 | listReferences, |
| 45 | getReferenceMetadata, |
| 46 | listRuns, |
| 47 | getRun, |
| 48 | }; |
| 49 | |
| 50 | return { |
| 51 | fetch: Effect.gen(function* () { |
| 52 | const request = yield* HttpServerRequest; |
| 53 | const url = new URL(request.originalUrl); |
| 54 | const pathname = url.pathname; |
| 55 | |
| 56 | if (request.method === "GET" && pathname === "/bindings") { |
| 57 | return yield* HttpServerResponse.json({ bound: Object.keys(bound) }); |
| 58 | } |
| 59 | |
| 60 | if (request.method === "GET" && pathname === "/readsets") { |
| 61 | // sequenceStoreId injection scopes the list to the bound store. |
| 62 | const response = yield* listReadSets(); |
| 63 | return yield* HttpServerResponse.json({ |
| 64 | count: (response.readSets ?? []).length, |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | if (request.method === "GET" && pathname === "/references") { |
| 69 | const response = yield* listReferences(); |
| 70 | return yield* HttpServerResponse.json({ |
| 71 | count: (response.references ?? []).length, |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | if (request.method === "GET" && pathname === "/runs") { |
| 76 | const response = yield* listRuns(); |
| 77 | return yield* HttpServerResponse.json({ |
| 78 | count: (response.items ?? []).length, |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | // The typed-not-found routes prove the grant + injection end-to-end: |
| 83 | // an IAM gap would surface AccessDeniedException (a 500), while the |
| 84 | // typed ResourceNotFoundException proves the request reached the API. |
| 85 | if (request.method === "GET" && pathname === "/readset/typed-not-found") { |
| 86 | const typed = yield* getReadSetMetadata({ id: BOGUS_ID }).pipe( |
| 87 | Effect.map(() => false), |
| 88 | Effect.catchTag(["ResourceNotFoundException", "ValidationException"], () => |
| 89 | Effect.succeed(true), |
| 90 | ), |
| 91 | ); |
| 92 | return yield* HttpServerResponse.json({ typed }); |
| 93 | } |
| 94 | |
| 95 | if (request.method === "GET" && pathname === "/run/typed-not-found") { |
| 96 | const typed = yield* getRun({ id: BOGUS_ID }).pipe( |
| 97 | Effect.map(() => false), |
| 98 | Effect.catchTag(["ResourceNotFoundException", "ValidationException"], () => |
| 99 | Effect.succeed(true), |
| 100 | ), |
| 101 | ); |
| 102 | return yield* HttpServerResponse.json({ typed }); |
| 103 | } |
| 104 | |
| 105 | return yield* HttpServerResponse.json( |
| 106 | { error: "Not found", method: request.method, pathname }, |
| 107 | { status: 404 }, |
| 108 | ); |
| 109 | }).pipe(Effect.orDie), |
| 110 | }; |
| 111 | }).pipe( |
| 112 | Effect.provide( |
| 113 | Layer.mergeAll( |
| 114 | Omics.ListReadSetsHttp, |
| 115 | Omics.GetReadSetMetadataHttp, |
| 116 | Omics.StartReadSetImportJobHttp, |
| 117 | Omics.BatchDeleteReadSetHttp, |
| 118 | Omics.ListReferencesHttp, |
| 119 | Omics.GetReferenceMetadataHttp, |
| 120 | Omics.ListRunsHttp, |
| 121 | Omics.GetRunHttp, |
| 122 | ), |
| 123 | ), |
| 124 | ), |
| 125 | ); |