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

main
ts 53 lines 2.31 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 { MyContainer } from "./container.ts";
5 import { Storage } from "./storage.ts";
6
7 export class Object extends Cloudflare.DurableObject<Object>()(
8 "Object",
9 Effect.gen(function* () {
10 // plan time
11 const bucket = yield* Cloudflare.R2.ReadWriteBucket(Storage);
12 const state = yield* Cloudflare.DurableObjectState;
13 const container = yield* MyContainer;
14
15 return Effect.gen(function* () {
16 // runtime
17 yield* state.storage.sql.exec(
18 "CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, name TEXT)",
19 );
20
21 // Starting is idempotent: a no-op once the container is running.
22 const start = container.start({ enableInternet: true });
23 const conn = yield* container.getTcpPort(3000);
24
25 return {
26 // Seed R2 through the DO's NATIVE binding so the container (which reads
27 // over its HTTP token) sees a value written by a different binding.
28 put: (key: string, value: string) => bucket.put(key, value).pipe(Effect.asVoid),
29 get: (key: string) => bucket.get(key),
30 ping: () => start.pipe(Effect.andThen(container.ping())),
31 // The env var a `Binding.Service` injected into the container.
32 boundEnv: () => start.pipe(Effect.andThen(container.boundEnv())),
33 // Read the object from inside the container over RPC.
34 readObjectRpc: (key: string) => start.pipe(Effect.andThen(container.readObject(key))),
35 // Read the object from inside the container over its TCP port (fetch).
36 readObjectFetch: (key: string) =>
37 Effect.gen(function* () {
38 yield* start;
39 const response = yield* conn.fetch(
40 HttpClientRequest.get(`http://container/object?key=${encodeURIComponent(key)}`),
41 );
42 return (yield* response.json) as { value: string | null };
43 }).pipe(Effect.orDie),
44 hello: () =>
45 Effect.gen(function* () {
46 yield* start;
47 const response = yield* conn.fetch(HttpClientRequest.get("http://container/"));
48 return yield* response.text;
49 }).pipe(Effect.orDie),
50 };
51 });
52 }).pipe(Effect.provide(Cloudflare.R2.ReadWriteBucketBinding)),
53 ) {}