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
| 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 { Sandbox } from "./Sandbox.ts"; |
| 5 | |
| 6 | export default class Agent extends Cloudflare.DurableObject<Agent>()( |
| 7 | "Agents", |
| 8 | Effect.gen(function* () { |
| 9 | const container = yield* Sandbox; |
| 10 | const state = yield* Cloudflare.DurableObjectState; |
| 11 | |
| 12 | return Effect.gen(function* () { |
| 13 | // Starting is idempotent: a no-op once the container is running. |
| 14 | const start = container.start({ enableInternet: true }); |
| 15 | const sessions = new Map<string, Cloudflare.WebSocket>(); |
| 16 | |
| 17 | for (const socket of yield* state.getWebSockets()) { |
| 18 | const session = socket.deserializeAttachment<{ id: string }>(); |
| 19 | if (session) { |
| 20 | sessions.set(session.id, socket); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | return { |
| 25 | exec: (command: string) => |
| 26 | start.pipe(Effect.andThen(container.shell(command)), Effect.orDie), |
| 27 | hello: () => |
| 28 | Effect.gen(function* () { |
| 29 | yield* start; |
| 30 | const { fetch } = yield* container.getTcpPort(3000); |
| 31 | const response = yield* fetch(HttpClientRequest.get("http://container/")); |
| 32 | return yield* response.text; |
| 33 | }).pipe(Effect.orDie), |
| 34 | increment: () => |
| 35 | Effect.gen(function* () { |
| 36 | yield* start; |
| 37 | const { fetch } = yield* container.getTcpPort(3000); |
| 38 | const response = yield* fetch(HttpClientRequest.post("http://container/increment")); |
| 39 | return yield* response.text; |
| 40 | }).pipe(Effect.orDie), |
| 41 | fetch: Effect.gen(function* () { |
| 42 | const [response, socket] = yield* Cloudflare.upgrade(); |
| 43 | const id = "TODO"; |
| 44 | socket.serializeAttachment({ id }); |
| 45 | sessions.set(id, socket); |
| 46 | return response; |
| 47 | }).pipe(Effect.orDie), |
| 48 | webSocketMessage: Effect.fn(function* ( |
| 49 | socket: Cloudflare.WebSocket, |
| 50 | message: string | Uint8Array, |
| 51 | ) { |
| 52 | const session = socket.deserializeAttachment<{ id: string }>(); |
| 53 | if (!session) return; |
| 54 | const text = typeof message === "string" ? message : new TextDecoder().decode(message); |
| 55 | for (const peer of sessions.values()) { |
| 56 | yield* peer.send(`[${session.id}] ${text}`); |
| 57 | } |
| 58 | }), |
| 59 | webSocketClose: Effect.fn(function* ( |
| 60 | ws: Cloudflare.WebSocket, |
| 61 | code: number, |
| 62 | reason: string, |
| 63 | _wasClean: boolean, |
| 64 | ) { |
| 65 | const session = ws.deserializeAttachment<{ id: string }>(); |
| 66 | if (session) { |
| 67 | sessions.delete(session.id); |
| 68 | } |
| 69 | yield* ws.close(code, reason); |
| 70 | }), |
| 71 | }; |
| 72 | }); |
| 73 | }), |
| 74 | ) {} |