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

main
ts 127 lines 4.54 KB
Raw
1 import * as AppConfig from "@alchemy.run/aws/AppConfig";
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, "events-handler.ts");
13
14 export class AppConfigEventsTestFunction extends Lambda.Function<Lambda.Function>()(
15 "AppConfigEventsTestFunction",
16 ) {}
17
18 export default AppConfigEventsTestFunction.make(
19 {
20 main,
21 functionUrl: true,
22 timeout: Duration.seconds(30),
23 },
24 Effect.gen(function* () {
25 const app = yield* AppConfig.Application("EventsApp", {});
26 const env = yield* AppConfig.Environment("EventsEnv", {
27 applicationId: app.applicationId,
28 });
29 const profile = yield* AppConfig.ConfigurationProfile("EventsProfile", {
30 applicationId: app.applicationId,
31 locationUri: "hosted",
32 });
33 yield* AppConfig.HostedConfigurationVersion("EventsV1", {
34 applicationId: app.applicationId,
35 configurationProfileId: profile.configurationProfileId,
36 content: JSON.stringify({ revision: 1 }),
37 contentType: "application/json",
38 });
39 const strategy = yield* AppConfig.DeploymentStrategy("EventsAllAtOnce", {
40 deploymentDuration: 0,
41 growthFactor: 100,
42 finalBakeTime: 0,
43 replicateTo: "NONE",
44 });
45
46 // Deployment notifications arrive as separate async invocations that may
47 // land on a DIFFERENT warm instance than the one serving the test's
48 // polls, so instance memory cannot capture them. Persist each event to
49 // S3 under a deterministic key and read it back by key.
50 const bucket = yield* S3.Bucket("EventsBucket", { forceDestroy: true });
51 const putObject = yield* S3.PutObject(bucket);
52 const getObject = yield* S3.GetObject(bucket);
53
54 // Deployments are triggered at runtime (POST /deploy) so the extension
55 // association below exists before the first deployment starts.
56 const startDeployment = yield* AppConfig.StartDeployment(app, env, profile, strategy);
57
58 yield* AppConfig.consumeDeploymentEvents(
59 env,
60 { events: ["ON_DEPLOYMENT_START", "ON_DEPLOYMENT_COMPLETE"] },
61 (events) =>
62 events.pipe(
63 Stream.runForEach((event) =>
64 putObject({
65 Key: `events/${event.DeploymentNumber}-${event.Type}.json`,
66 Body: JSON.stringify(event),
67 ContentType: "application/json",
68 }).pipe(Effect.orDie),
69 ),
70 ),
71 );
72
73 return {
74 fetch: Effect.gen(function* () {
75 const request = yield* HttpServerRequest;
76 const url = new URL(request.originalUrl);
77 const pathname = url.pathname;
78
79 if (request.method === "POST" && pathname === "/deploy") {
80 const body = (yield* request.json) as unknown as {
81 version: string;
82 };
83 const started = yield* startDeployment({
84 ConfigurationVersion: body.version,
85 });
86 return yield* HttpServerResponse.json({
87 deploymentNumber: started.DeploymentNumber,
88 state: started.State,
89 });
90 }
91
92 if (request.method === "GET" && pathname === "/event") {
93 const number = url.searchParams.get("number");
94 const type = url.searchParams.get("type");
95 return yield* getObject({
96 Key: `events/${number}-${type}.json`,
97 }).pipe(
98 Effect.flatMap((result) => Stream.mkString(Stream.decodeText(result.Body!))),
99 Effect.flatMap((text) => HttpServerResponse.json({ event: JSON.parse(text) })),
100 // Notification not delivered yet — the test polls until it is.
101 Effect.catchTag("NoSuchKey", () =>
102 HttpServerResponse.json({ event: null }, { status: 404 }),
103 ),
104 );
105 }
106
107 if (request.method === "GET" && pathname === "/ping") {
108 return yield* HttpServerResponse.json({ ok: true });
109 }
110
111 return yield* HttpServerResponse.json(
112 { error: "Not found", method: request.method, pathname },
113 { status: 404 },
114 );
115 }).pipe(Effect.orDie),
116 };
117 }).pipe(
118 Effect.provide(
119 Layer.mergeAll(
120 Lambda.AppConfigDeploymentEventSource,
121 AppConfig.StartDeploymentHttp,
122 S3.PutObjectHttp,
123 S3.GetObjectHttp,
124 ),
125 ),
126 ),
127 );