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

main
ts 116 lines 4.26 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import * as Effect from "effect/Effect";
3 import { HttpServerRequest } from "effect/http/HttpServerRequest";
4 import * as HttpServerResponse from "effect/http/HttpServerResponse";
5 import Drain from "./Drain.ts";
6 import { Events, EventsTable, type EventRow } from "./resources.ts";
7
8 /**
9 * The front door of an analytics pipeline.
10 *
11 * Producers post events; the service publishes them to Pub/Sub and
12 * returns. Nothing touches BigQuery on the request path, so a slow
13 * warehouse or a schema change cannot take the ingest endpoint down —
14 * the messages just queue up until {@link Drain} runs.
15 *
16 * - `POST /events` — accept an event and publish it.
17 * - `POST /drain` — start a drain now, instead of waiting for a schedule.
18 * - `GET /events/count` — count what has landed in BigQuery.
19 */
20 export default class Ingest extends GCP.Function<Ingest>()(
21 "Ingest",
22 {
23 main: import.meta.url,
24 invokerIamDisabled: true,
25 },
26 Effect.gen(function* () {
27 const topic = yield* Events;
28 const table = yield* EventsTable;
29 const drain = yield* Drain;
30
31 // pubsub.publisher on the topic only.
32 const publisher = yield* GCP.PubSub.WriteTopic(topic);
33 // bigquery.dataViewer on the table, plus bigquery.jobUser on the
34 // project — BigQuery only grants running query jobs there.
35 const warehouse = yield* GCP.BigQuery.ReadTable(table);
36 // Binding a Job to a Service grants run.jobsExecutorWithOverrides on
37 // the job — this is how one host triggers another.
38 const runDrain = yield* GCP.Run.RunJob(drain);
39
40 // An accessor: the table id is bound at deploy time and read back
41 // inside the handler.
42 const tableId = yield* table.tableId;
43
44 return {
45 fetch: Effect.gen(function* () {
46 const request = yield* HttpServerRequest;
47 const url = new URL(request.originalUrl);
48
49 if (request.method === "GET" && url.pathname === "/") {
50 return HttpServerResponse.text("ok");
51 }
52
53 if (request.method === "POST" && url.pathname === "/events") {
54 const body = (yield* request.json) as {
55 type?: string;
56 payload?: unknown;
57 };
58 if (!body.type) {
59 return yield* HttpServerResponse.json({ error: "type is required" }, { status: 400 });
60 }
61
62 const event: EventRow = {
63 id: crypto.randomUUID(),
64 type: body.type,
65 occurredAt: new Date().toISOString(),
66 payload: JSON.stringify(body.payload ?? {}),
67 };
68
69 yield* publisher
70 .publish({
71 data: JSON.stringify(event),
72 // Attributes are queryable without decoding the body,
73 // which lets a filtered subscription fan out by type.
74 attributes: { type: event.type },
75 })
76 .pipe(Effect.orDie);
77
78 return yield* HttpServerResponse.json({ id: event.id }, { status: 202 });
79 }
80
81 // Cloud Run Jobs are asynchronous: this returns as soon as the
82 // execution is created, not when it finishes.
83 if (request.method === "POST" && url.pathname === "/drain") {
84 const operation = yield* runDrain().pipe(Effect.orDie);
85 return yield* HttpServerResponse.json(
86 { execution: operation.name ?? null },
87 { status: 202 },
88 );
89 }
90
91 if (request.method === "GET" && url.pathname === "/events/count") {
92 const type = url.searchParams.get("type");
93 const events = yield* tableId;
94 // Unqualified table names resolve against the bound table's
95 // dataset; `params` become named `@type` parameters.
96 const rows = yield* warehouse
97 .query(
98 type
99 ? `SELECT COUNT(*) AS n FROM \`${events}\` WHERE type = @type`
100 : `SELECT COUNT(*) AS n FROM \`${events}\``,
101 type ? { type } : undefined,
102 )
103 .pipe(Effect.orDie);
104
105 return yield* HttpServerResponse.json({
106 count: Number(rows[0]?.n ?? 0),
107 });
108 }
109
110 return yield* HttpServerResponse.json({ error: "not found" }, { status: 404 });
111 }),
112 };
113 }).pipe(
114 Effect.provide([GCP.PubSub.WriteTopicHttp, GCP.BigQuery.ReadTableHttp, GCP.Run.RunJobHttp]),
115 ),
116 ) {}