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

main
ts 41 lines 1.73 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as DomainCloudflare from "@alchemy.run/cloudflare/Domain";
3 import * as Effect from "effect/Effect";
4 import { HttpServerRequest } from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import * as Layer from "effect/Layer";
7 import { Batches, GroupId, Ping, Pinger, Pongs, PongWriterLive } from "./batches.ts";
8
9 const BatchesLive = Batches.layer(PongWriterLive).pipe(
10 Layer.provide(DomainCloudflare.layerDurableObjects()),
11 );
12
13 /** `POST /ping/<group>/<id>[?slow=true]` sends a ping; `GET /pongs/<group>` lists the group's pongs. */
14 const ROUTE = /^\/(ping|pongs)\/([^/]+)(?:\/([^/]+))?$/;
15
16 export default class DomainBatchesWorker extends Cloudflare.Worker<DomainBatchesWorker>()(
17 "DomainBatchesWorker",
18 { main: import.meta.url },
19 Effect.gen(function* () {
20 const pingers = yield* Pinger;
21 const pongs = yield* Pongs;
22 return {
23 fetch: Effect.gen(function* () {
24 const request = yield* HttpServerRequest;
25 const url = new URL(request.url, "http://localhost");
26 const route = ROUTE.exec(url.pathname);
27 if (!route) return HttpServerResponse.text("Not Found", { status: 404 });
28 const group = GroupId.make(decodeURIComponent(route[2]!));
29 if (route[1] === "ping") {
30 yield* pingers.send(
31 decodeURIComponent(route[3]!),
32 new Ping({ group, slow: url.searchParams.get("slow") === "true" }),
33 );
34 return HttpServerResponse.text("ok");
35 }
36 const { entries } = yield* pongs.list(group);
37 return yield* HttpServerResponse.json(entries.map((entry) => entry.event.ping));
38 }),
39 };
40 }).pipe(Effect.provide(BatchesLive)),
41 ) {}