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

main
ts 267 lines 9.65 KB
Raw
1 import { EventBus } from "@alchemy.run/aws/EventBridge";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as Schemas from "@alchemy.run/aws/Schemas";
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, "handler.ts");
13
14 export class SchemasTestFunction extends Lambda.Function<Lambda.Function>()(
15 "SchemasTestFunction",
16 ) {}
17
18 /** The OpenAPI 3 document registered as the fixture schema. */
19 const ORDER_CREATED_CONTENT = JSON.stringify({
20 openapi: "3.0.0",
21 info: { version: "1.0.0", title: "OrderCreated" },
22 paths: {},
23 components: {
24 schemas: {
25 OrderCreated: {
26 type: "object",
27 properties: {
28 orderId: { type: "string" },
29 amount: { type: "number" },
30 },
31 },
32 },
33 },
34 });
35
36 /** A full AWS event envelope for GetDiscoveredSchema inference. */
37 const SAMPLE_EVENT = JSON.stringify({
38 version: "0",
39 id: "12345678-1234-1234-1234-123456789012",
40 "detail-type": "OrderCreated",
41 source: "alchemy.test",
42 account: "123456789012",
43 time: "2026-01-01T00:00:00Z",
44 region: "us-east-1",
45 resources: [],
46 detail: { orderId: "abc", amount: 42 },
47 });
48
49 const CODE_BINDING_LANGUAGE = "Python36";
50
51 export default SchemasTestFunction.make(
52 {
53 main,
54 functionUrl: true,
55 timeout: Duration.seconds(30),
56 // The default 128 MB sits at ~114 MB used; the last routes in a test run
57 // OOM the instance (platform-level 500 with no handler log).
58 memorySize: 512,
59 },
60 Effect.gen(function* () {
61 const registry = yield* Schemas.Registry("BindingsRegistry", {
62 description: "alchemy schemas bindings fixture registry",
63 });
64
65 const schema = yield* Schemas.Schema("BindingsSchema", {
66 registryName: registry.registryName,
67 type: "OpenApi3",
68 content: ORDER_CREATED_CONTENT,
69 description: "alchemy schemas bindings fixture schema",
70 });
71
72 // The discoverer the start/stop bindings are bound to — on a dedicated
73 // bus that carries no traffic, so it never publishes junk schemas.
74 const bus = yield* EventBus("BindingsBus", {});
75 const discoverer = yield* Schemas.Discoverer("BindingsDiscoverer", {
76 sourceArn: bus.eventBusArn,
77 description: "alchemy schemas bindings fixture discoverer",
78 });
79
80 // Event source: subscribe the host to schema-registry notifications.
81 // The deploy proves the EventBridge rule + invoke permission wiring.
82 yield* Schemas.consumeSchemaEvents(
83 { kinds: ["schema-created", "schema-version-created"] },
84 (events) =>
85 Stream.runForEach(events, (event) =>
86 Effect.log(
87 `schema event: ${event.detail.RegistryName}/${event.detail.SchemaName} v${event.detail.SchemaVersion}`,
88 ),
89 ),
90 );
91
92 const describeSchema = yield* Schemas.DescribeSchema(schema);
93 const exportSchema = yield* Schemas.ExportSchema(schema);
94 const listSchemaVersions = yield* Schemas.ListSchemaVersions(schema);
95 const searchSchemas = yield* Schemas.SearchSchemas(registry);
96 const getDiscoveredSchema = yield* Schemas.GetDiscoveredSchema();
97 const putCodeBinding = yield* Schemas.PutCodeBinding(schema);
98 const describeCodeBinding = yield* Schemas.DescribeCodeBinding(schema);
99 const getCodeBindingSource = yield* Schemas.GetCodeBindingSource(schema);
100 const startDiscoverer = yield* Schemas.StartDiscoverer(discoverer);
101 const stopDiscoverer = yield* Schemas.StopDiscoverer(discoverer);
102
103 const bound = {
104 describeSchema,
105 exportSchema,
106 listSchemaVersions,
107 searchSchemas,
108 getDiscoveredSchema,
109 putCodeBinding,
110 describeCodeBinding,
111 getCodeBindingSource,
112 startDiscoverer,
113 stopDiscoverer,
114 };
115
116 return {
117 fetch: Effect.gen(function* () {
118 const request = yield* HttpServerRequest;
119 const url = new URL(request.originalUrl);
120 const pathname = url.pathname;
121
122 if (request.method === "GET" && pathname === "/bindings") {
123 return yield* HttpServerResponse.json({
124 bound: Object.keys(bound),
125 });
126 }
127
128 // Schema-scoped read: registry + schema names are injected.
129 if (request.method === "GET" && pathname === "/schema") {
130 const response = yield* describeSchema();
131 return yield* HttpServerResponse.json({
132 name: response.SchemaName,
133 type: response.Type,
134 version: response.SchemaVersion,
135 hasContent: !!response.Content,
136 });
137 }
138
139 // Export the schema as JSONSchemaDraft4. AWS only allows exporting
140 // discovered / AWS-managed schemas, so on this custom-registry
141 // schema the call surfaces the typed ForbiddenException — the route
142 // reports which outcome occurred so the test can assert the typed
143 // error (proving the binding + IAM wiring end-to-end).
144 if (request.method === "GET" && pathname === "/export") {
145 const response = yield* exportSchema({
146 Type: "JSONSchemaDraft4",
147 }).pipe(
148 Effect.map((r) => ({
149 outcome: "exported" as const,
150 hasContent: !!r.Content,
151 })),
152 Effect.catchTag("ForbiddenException", (e) =>
153 Effect.succeed({
154 outcome: "forbidden" as const,
155 message: e.message ?? "",
156 }),
157 ),
158 );
159 return yield* HttpServerResponse.json(response);
160 }
161
162 if (request.method === "GET" && pathname === "/versions") {
163 const response = yield* listSchemaVersions();
164 return yield* HttpServerResponse.json({
165 versions: (response.SchemaVersions ?? []).map((v) => v.SchemaVersion),
166 });
167 }
168
169 // Registry-scoped search: the registry name is injected.
170 if (request.method === "GET" && pathname === "/search") {
171 const response = yield* searchSchemas({ Keywords: "order" });
172 return yield* HttpServerResponse.json({
173 names: (response.Schemas ?? []).map((s) => s.SchemaName),
174 });
175 }
176
177 // Account-level inference from sample events.
178 if (request.method === "POST" && pathname === "/discover") {
179 const response = yield* getDiscoveredSchema({
180 Type: "OpenApi3",
181 Events: [SAMPLE_EVENT],
182 });
183 return yield* HttpServerResponse.json({
184 hasContent: !!response.Content,
185 });
186 }
187
188 // Kick off async code-binding generation. Re-running against an
189 // already-generated binding returns the typed ConflictException —
190 // treat it as "generation already complete" so the route is
191 // idempotent across test re-runs.
192 if (request.method === "POST" && pathname === "/codebinding") {
193 const response = yield* putCodeBinding({
194 Language: CODE_BINDING_LANGUAGE,
195 }).pipe(
196 Effect.catchTag("ConflictException", () =>
197 Effect.succeed({ Status: "CREATE_COMPLETE" as const }),
198 ),
199 );
200 return yield* HttpServerResponse.json({ status: response.Status });
201 }
202
203 // Poll generation status (NOT_FOUND until PutCodeBinding ran).
204 if (request.method === "GET" && pathname === "/codebinding") {
205 const response = yield* describeCodeBinding({
206 Language: CODE_BINDING_LANGUAGE,
207 }).pipe(
208 Effect.catchTag("NotFoundException", () =>
209 Effect.succeed({ Status: "NOT_FOUND" as const }),
210 ),
211 );
212 return yield* HttpServerResponse.json({ status: response.Status });
213 }
214
215 // Download the generated package (a zip streamed as Body).
216 if (request.method === "GET" && pathname === "/codebinding/source") {
217 const bytes = yield* getCodeBindingSource({
218 Language: CODE_BINDING_LANGUAGE,
219 }).pipe(
220 Effect.flatMap((response) =>
221 response.Body === undefined
222 ? Effect.succeed(0)
223 : Stream.runFold(
224 response.Body,
225 () => 0,
226 (n, chunk) => n + chunk.length,
227 ),
228 ),
229 Effect.catchTag("NotFoundException", () => Effect.succeed(-1)),
230 );
231 return yield* HttpServerResponse.json({ bytes });
232 }
233
234 // Discoverer-scoped control plane: the discoverer id is injected.
235 if (request.method === "POST" && pathname === "/discoverer/stop") {
236 const response = yield* stopDiscoverer();
237 return yield* HttpServerResponse.json({ state: response.State });
238 }
239 if (request.method === "POST" && pathname === "/discoverer/start") {
240 const response = yield* startDiscoverer();
241 return yield* HttpServerResponse.json({ state: response.State });
242 }
243
244 return yield* HttpServerResponse.json(
245 { error: "Not found", method: request.method, pathname },
246 { status: 404 },
247 );
248 }).pipe(Effect.orDie),
249 };
250 }).pipe(
251 Effect.provide(
252 Layer.mergeAll(
253 Lambda.EventSource,
254 Schemas.DescribeSchemaHttp,
255 Schemas.ExportSchemaHttp,
256 Schemas.ListSchemaVersionsHttp,
257 Schemas.SearchSchemasHttp,
258 Schemas.GetDiscoveredSchemaHttp,
259 Schemas.PutCodeBindingHttp,
260 Schemas.DescribeCodeBindingHttp,
261 Schemas.GetCodeBindingSourceHttp,
262 Schemas.StartDiscovererHttp,
263 Schemas.StopDiscovererHttp,
264 ),
265 ),
266 ),
267 );