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

main
ts 167 lines 5.38 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as AOSS from "@alchemy.run/aws/OpenSearchServerless";
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 * as Schedule from "effect/Schedule";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "index-bindings-handler.ts");
11
12 // Deterministic names, distinct from Collection.test.ts so the suites never
13 // collide. ≤32 chars, lowercase.
14 export const COLLECTION_NAME = "alchemy-aossb";
15 export const ENC_POLICY = "alchemy-aossb-enc";
16 export const NET_POLICY = "alchemy-aossb-net";
17
18 const INDEX_NAME = "alchemy-roundtrip";
19
20 export class AossIndexFunction extends Lambda.Function<Lambda.Function>()("AossIndexFunction") {}
21
22 export default AossIndexFunction.make(
23 {
24 main,
25 functionUrl: true,
26 },
27 Effect.gen(function* () {
28 // The collection (and its prerequisite encryption/network policies) is
29 // deployed as part of the fixture; the data access policy granting THIS
30 // function's role lives in the test (it needs the function's roleArn).
31 const enc = yield* AOSS.SecurityPolicy("Enc", {
32 policyName: ENC_POLICY,
33 type: "encryption",
34 policy: {
35 Rules: [
36 {
37 ResourceType: "collection",
38 Resource: [`collection/${COLLECTION_NAME}`],
39 },
40 ],
41 AWSOwnedKey: true,
42 },
43 });
44 const net = yield* AOSS.SecurityPolicy("Net", {
45 policyName: NET_POLICY,
46 type: "network",
47 policy: [
48 {
49 Rules: [
50 {
51 ResourceType: "collection",
52 Resource: [`collection/${COLLECTION_NAME}`],
53 },
54 ],
55 AllowFromPublic: true,
56 },
57 ],
58 });
59 const collection = yield* AOSS.Collection("Collection", {
60 collectionName: COLLECTION_NAME,
61 type: "SEARCH",
62 standbyReplicas: "DISABLED",
63 // Reference the policies so the engine orders them before the
64 // collection (the encryption policy is a hard create prerequisite).
65 tags: { encPolicy: enc.policyName, netPolicy: net.policyName },
66 });
67
68 const getCollection = yield* AOSS.GetCollection(collection);
69 const createIndex = yield* AOSS.CreateIndex(collection);
70 const getIndex = yield* AOSS.GetIndex(collection);
71 const updateIndex = yield* AOSS.UpdateIndex(collection);
72 const deleteIndex = yield* AOSS.DeleteIndex(collection);
73
74 const bound = {
75 getCollection,
76 createIndex,
77 getIndex,
78 updateIndex,
79 deleteIndex,
80 };
81
82 // Create → read → update → delete: exercises the runtime multi-tenant
83 // index pattern end-to-end and leaves no orphan.
84 const roundtrip = Effect.gen(function* () {
85 yield* createIndex({
86 indexName: INDEX_NAME,
87 indexSchema: {
88 mappings: { properties: { title: { type: "text" } } },
89 },
90 }).pipe(Effect.catchTag("ConflictException", () => Effect.void));
91
92 // Index visibility is eventually consistent — retry the read
93 // through the typed not-found window (bounded, ~30s).
94 const read = yield* Effect.retry(getIndex({ indexName: INDEX_NAME }), {
95 while: (e): boolean => e._tag === "ResourceNotFoundException",
96 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(10)]),
97 });
98
99 yield* updateIndex({
100 indexName: INDEX_NAME,
101 indexSchema: {
102 mappings: {
103 properties: { body: { type: "text" } },
104 },
105 },
106 });
107
108 yield* deleteIndex({ indexName: INDEX_NAME }).pipe(
109 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
110 );
111
112 return yield* HttpServerResponse.json({
113 created: true,
114 hadSchema: read.indexSchema !== undefined,
115 deleted: true,
116 });
117 });
118
119 return {
120 fetch: Effect.gen(function* () {
121 const request = yield* HttpServerRequest;
122 const url = new URL(request.originalUrl);
123 const pathname = url.pathname;
124
125 if (request.method === "GET" && pathname === "/bindings") {
126 return yield* HttpServerResponse.json({
127 bound: Object.keys(bound),
128 });
129 }
130
131 if (request.method === "GET" && pathname === "/collection") {
132 const detail = yield* getCollection();
133 return yield* HttpServerResponse.json({
134 id: detail?.id,
135 status: detail?.status,
136 endpoint: detail?.collectionEndpoint,
137 });
138 }
139
140 if (request.method === "POST" && pathname === "/index/roundtrip") {
141 return yield* roundtrip.pipe(
142 // Surface the typed failure (AccessDenied while the data access
143 // policy propagates, …) so the test's retry log names it.
144 Effect.catch((e) =>
145 HttpServerResponse.json({ error: e._tag, message: e.message }, { status: 500 }),
146 ),
147 );
148 }
149
150 return yield* HttpServerResponse.json(
151 { error: "Not found", method: request.method, pathname },
152 { status: 404 },
153 );
154 }).pipe(Effect.orDie),
155 };
156 }).pipe(
157 Effect.provide(
158 Layer.mergeAll(
159 AOSS.GetCollectionHttp,
160 AOSS.CreateIndexHttp,
161 AOSS.GetIndexHttp,
162 AOSS.UpdateIndexHttp,
163 AOSS.DeleteIndexHttp,
164 ),
165 ),
166 ),
167 );