@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 65 lines 2.38 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as DomainCloudflare from "@alchemy.run/cloudflare/Domain";
3 import * as DateTime from "effect/DateTime";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
8 import {
9 EXPIRY,
10 ExpireHold,
11 HOLD_WINDOW,
12 Hold,
13 HoldDeadlineLive,
14 Holds,
15 PlaceHold,
16 ReleaseHold,
17 } from "./holds.ts";
18
19 const HoldsLive = Holds.layer(HoldDeadlineLive).pipe(
20 Layer.provide(DomainCloudflare.layerDurableObjects()),
21 );
22
23 /**
24 * `POST /place/<id>`, `POST /release/<id>`, `GET /state/<id>` on the Holds
25 * model; `POST /schedule/<id>` and `POST /cancel/<id>` schedule and cancel
26 * the hold's expiry directly.
27 */
28 const ROUTE = /^\/(place|release|state|schedule|cancel)\/([^/]+)$/;
29
30 export default class DomainScheduleWorker extends Cloudflare.Worker<DomainScheduleWorker>()(
31 "DomainScheduleWorker",
32 { main: import.meta.url },
33 Effect.gen(function* () {
34 const holds = yield* Hold;
35 return {
36 fetch: Effect.gen(function* () {
37 const request = yield* HttpServerRequest;
38 const route = ROUTE.exec(new URL(request.url, "http://localhost").pathname);
39 if (!route) return HttpServerResponse.text("Not Found", { status: 404 });
40 const id = decodeURIComponent(route[2]!);
41 if (route[1] === "schedule") {
42 const receipt = yield* holds
43 .schedule(id, new ExpireHold(), { after: HOLD_WINDOW, commandId: EXPIRY })
44 .pipe(Effect.orDie);
45 return yield* HttpServerResponse.json({
46 commandId: receipt.commandId,
47 at: DateTime.toEpochMillis(receipt.at),
48 });
49 }
50 if (route[1] === "cancel")
51 return yield* HttpServerResponse.json({
52 cancel: yield* holds.unschedule(id, EXPIRY).pipe(
53 Effect.as("Cancelled"),
54 Effect.catchTag(["AlreadyExecuted", "AlreadyCancelled", "NotScheduled"], (e) =>
55 Effect.succeed(e._tag),
56 ),
57 ),
58 });
59 if (route[1] === "place") yield* holds.send(id, new PlaceHold());
60 if (route[1] === "release") yield* holds.send(id, new ReleaseHold());
61 return yield* HttpServerResponse.json(yield* holds.state(id));
62 }),
63 };
64 }).pipe(Effect.provide(HoldsLive)),
65 ) {}