@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 81 lines 3.03 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 { Jobs, Results, resultPath, type JobMessage } from "./resources.ts";
6
7 /**
8 * The public front of the job queue.
9 *
10 * - `POST /jobs` — enqueue `{ payload }` and return `202 { id }`.
11 * - `GET /jobs/:id` — `200` with the result once the worker is done,
12 * `202 { status: "pending" }` until then.
13 *
14 * The API never processes a job itself, so a slow or crashed worker only
15 * delays results; submissions keep succeeding.
16 */
17 export default class Api extends GCP.Function<Api>()(
18 "Api",
19 {
20 main: import.meta.url,
21 invokerIamDisabled: true,
22 },
23 Effect.gen(function* () {
24 const jobs = yield* GCP.PubSub.WriteTopic(Jobs);
25 const results = yield* GCP.Firestore.ReadDatabase(Results);
26
27 return {
28 fetch: Effect.gen(function* () {
29 const request = yield* HttpServerRequest;
30 const url = new URL(request.originalUrl);
31 const segments = url.pathname.split("/").filter(Boolean);
32
33 if (request.method === "GET" && segments.length === 0) {
34 return HttpServerResponse.text("ok");
35 }
36
37 if (request.method === "POST" && segments.length === 1 && segments[0] === "jobs") {
38 const body = (yield* request.json.pipe(Effect.orElseSucceed(() => ({})))) as {
39 payload?: unknown;
40 };
41 if (typeof body.payload !== "string") {
42 return yield* HttpServerResponse.json(
43 { error: "payload must be a string" },
44 { status: 400 },
45 );
46 }
47
48 const job: JobMessage = {
49 id: crypto.randomUUID(),
50 payload: body.payload,
51 submittedAt: new Date().toISOString(),
52 };
53 yield* jobs
54 .publish({
55 data: JSON.stringify(job),
56 // Attributes are readable without decoding the body, e.g. by
57 // a subscription filter.
58 attributes: { jobId: job.id },
59 })
60 .pipe(Effect.orDie);
61
62 return yield* HttpServerResponse.json({ id: job.id, status: "pending" }, { status: 202 });
63 }
64
65 if (request.method === "GET" && segments.length === 2 && segments[0] === "jobs") {
66 const id = segments[1]!;
67 const result = yield* results.get(resultPath(id)).pipe(Effect.orDie);
68 // No document yet means the job is queued or in flight. The API
69 // keeps no record of submissions, so an unknown id also reads
70 // as pending.
71 if (result === undefined) {
72 return yield* HttpServerResponse.json({ id, status: "pending" }, { status: 202 });
73 }
74 return yield* HttpServerResponse.json({ id, ...result.fields });
75 }
76
77 return yield* HttpServerResponse.json({ error: "not found" }, { status: 404 });
78 }),
79 };
80 }).pipe(Effect.provide([GCP.PubSub.WriteTopicHttp, GCP.Firestore.ReadDatabaseHttp])),
81 ) {}