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

main
ts 60 lines 1.98 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import * as Effect from "effect/Effect";
3 import * as HttpServerResponse from "effect/http/HttpServerResponse";
4
5 /** Bucket whose uploads Eventarc routes to the service. */
6 export const Drops = GCP.Storage.Bucket("EventarcDrops", {
7 location: "US-CENTRAL1",
8 forceDestroy: true,
9 });
10 /** Bucket the service records each event in (unwatched, so no loops). */
11 export const Markers = GCP.Storage.Bucket("EventarcMarkers", {
12 location: "US-CENTRAL1",
13 forceDestroy: true,
14 });
15
16 export const markerFor = (object: string) => `${object.replaceAll("/", "_")}.json`;
17
18 /**
19 * Effect-native Cloud Run service (default private ingress) receiving
20 * Storage finalize events through an Eventarc trigger.
21 * Deployed from {@link ../EventSource.test.ts}.
22 */
23 export default class EventarcService extends GCP.Function<EventarcService>()(
24 "EventarcService",
25 { main: import.meta.url, location: "us-central1" },
26 Effect.gen(function* () {
27 const drops = yield* Drops;
28 const markers = yield* Markers;
29 const putMarker = yield* GCP.Storage.PutObject(markers);
30
31 yield* GCP.Eventarc.consumeEvents(
32 "Drops",
33 {
34 eventFilters: [
35 {
36 attribute: "type",
37 value: "google.cloud.storage.object.v1.finalized",
38 },
39 { attribute: "bucket", value: drops.bucketName },
40 ],
41 },
42 (event) =>
43 Effect.gen(function* () {
44 const data = event.data as { name?: string; bucket?: string };
45 yield* putMarker({
46 name: markerFor(data.name ?? "unknown"),
47 body: JSON.stringify({
48 id: event.id,
49 type: event.type,
50 subject: event.subject,
51 bucket: data.bucket,
52 name: data.name,
53 }),
54 }).pipe(Effect.orDie);
55 }),
56 );
57
58 return { fetch: Effect.succeed(HttpServerResponse.text("ok")) };
59 }).pipe(Effect.provide(GCP.Storage.PutObjectHttp), Effect.provide(GCP.Run.EventarcEventSource)),
60 ) {}