@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, 04:45 UTC

main
ts 74 lines 3.09 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 * as Schedule from "effect/Schedule";
5 import { OpencodeContainer } from "./opencode-container.ts";
6
7 // Matches OPENCODE_SERVER_PASSWORD=bench in contexts/opencode/Dockerfile
8 // (username defaults to `opencode`): base64("opencode:bench").
9 const AUTH = "Basic b3BlbmNvZGU6YmVuY2g=";
10
11 /**
12 * Durable Object backing one opencode container instance. `boot()` blocks
13 * until opencode's `/global/health` endpoint answers `{"healthy":true}` AND a
14 * session can be created — the same two-request readiness probe the MicroVM
15 * hosts use, so the platforms share one definition of "usable coding agent".
16 * NOTE: the authoritative cold-start clock runs in the Worker AROUND the whole
17 * DO call, so it also covers Durable Object construction.
18 */
19 export class OpencodeObject extends Cloudflare.DurableObject<OpencodeObject>()(
20 "BenchOpencodeObject",
21 Effect.gen(function* () {
22 const container = yield* OpencodeContainer;
23
24 return Effect.gen(function* () {
25 const port = yield* container.getTcpPort(8080);
26
27 return {
28 boot: () =>
29 Effect.gen(function* () {
30 const start = yield* Effect.sync(() => Date.now());
31 yield* container.start({ enableInternet: true });
32 yield* Effect.gen(function* () {
33 const health = yield* port.fetch(
34 HttpClientRequest.get("http://container/global/health").pipe(
35 HttpClientRequest.setHeader("authorization", AUTH),
36 ),
37 );
38 const healthBody = yield* health.text;
39 if (health.status !== 200 || !healthBody.includes('"healthy":true')) {
40 return yield* Effect.fail(
41 new Error(`opencode health ${health.status}: ${healthBody.slice(0, 120)}`),
42 );
43 }
44 // A real write through the app — proves the server is
45 // functional, not merely listening.
46 const session = yield* port.fetch(
47 HttpClientRequest.post("http://container/session").pipe(
48 HttpClientRequest.setHeader("authorization", AUTH),
49 HttpClientRequest.bodyJsonUnsafe({}),
50 ),
51 );
52 const sessionBody = yield* session.text;
53 if (session.status !== 200 || !sessionBody.includes('"id"')) {
54 return yield* Effect.fail(
55 new Error(`opencode session ${session.status}: ${sessionBody.slice(0, 120)}`),
56 );
57 }
58 }).pipe(
59 Effect.retry({
60 schedule: Schedule.min([
61 Schedule.exponential("1 second"),
62 Schedule.spaced("5 seconds"),
63 ]),
64 times: 40,
65 }),
66 );
67 const readyMs = (yield* Effect.sync(() => Date.now())) - start;
68 return { readyMs };
69 }),
70 shutdown: () => container.destroy().pipe(Effect.ignore),
71 };
72 });
73 }),
74 ) {}