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

main
ts 49 lines 1.67 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as Effect from "effect/Effect";
3 import * as HttpClientRequest from "effect/http/HttpClientRequest";
4 import { PrismaHostConnection } from "./db.ts";
5
6 class PrismaHostContainer extends Cloudflare.Container<PrismaHostContainer>()(
7 "PrismaHostContainer",
8 Effect.gen(function* () {
9 const connection = yield* PrismaHostConnection;
10 return {
11 // Template string, not `path.join(import.meta.dirname, …)`: this module is
12 // bundled into the Worker and `import.meta.dirname` is undefined there.
13 context: `${import.meta.dirname}/../sqlreach/context`,
14 env: {
15 DATABASE_URL: connection.directConnectionString,
16 },
17 observability: { logs: { enabled: true } },
18 };
19 }),
20 ) {}
21
22 /**
23 * Durable Object that binds the {@link PrismaHostContainer} and exposes the
24 * probe server's routes to the Worker.
25 */
26 export class PrismaHostContainerObject extends Cloudflare.DurableObject<PrismaHostContainerObject>()(
27 "PrismaHostContainerObject",
28 Effect.gen(function* () {
29 const container = yield* PrismaHostContainer;
30
31 return Effect.gen(function* () {
32 // Starting is idempotent: a no-op once the container is running.
33 const start = container.start({ enableInternet: true });
34 const { fetch } = yield* container.getTcpPort(8080);
35
36 const get = (path: string) =>
37 Effect.gen(function* () {
38 yield* start;
39 const response = yield* fetch(HttpClientRequest.get(`http://container${path}`));
40 return yield* response.text;
41 });
42
43 return {
44 getEnv: () => get("/env"),
45 getProbe: () => get("/probe"),
46 };
47 });
48 }),
49 ) {}