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

main
ts 83 lines 3.41 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as HttpClient from "effect/http/HttpClient";
3 import * as HttpClientResponse from "effect/http/HttpClientResponse";
4
5 interface RecordedResponse {
6 readonly status: number;
7 readonly contentType?: string;
8 readonly body: string;
9 }
10
11 // Unknown paths serve SPA HTML; content type alone rejects these truncated bodies.
12 const SPA_HTML =
13 '<!doctype html>\n<html lang="en" style="background-color: var(--v2-background-bg-deep, #fafafa)">\n';
14
15 export const OPENCODE_2_RESPONSES = {
16 "/api/info": {
17 status: 200,
18 contentType: "application/json",
19 body: '{"version":"2.0.18","pid":2393556,"urls":["http://127.0.0.1:48771"],"paths":{"tmp":"/tmp/opencode"}}',
20 },
21 "/global/health": { status: 200, contentType: "text/html", body: SPA_HTML },
22 unauthorized: {
23 status: 401,
24 contentType: "application/json",
25 body: '{"_tag":"UnauthorizedError","message":"Authentication required"}',
26 },
27 } satisfies Record<string, RecordedResponse>;
28
29 export const OPENCODE_2_WORKSPACE_RESPONSES = {
30 "/api/command": {
31 status: 200,
32 contentType: "application/json",
33 body: '{"location":{"directory":"/work"},"data":[{"name":"init","description":"guided AGENTS.md setup"},{"name":"review","description":"review changes [commit|branch|pr], defaults to uncommitted"},{"name":"bee","description":"B command"}]}',
34 },
35 "/api/skill": {
36 status: 200,
37 contentType: "application/json",
38 body: '{"location":{"directory":"/work"},"data":[{"id":"opencode","name":"OpenCode","description":"Use this skill for any question about OpenCode itself.","path":"/builtin/opencode.md","content":"# OpenCode"},{"id":"report","name":"Report","description":"Use when the user wants to report an opencode issue or bug.","path":"/builtin/report.md","content":"<!-- Built-in skill. -->"},{"id":"plum","name":"plum","description":"plum skill","path":"/work/.opencode/skills/plum/SKILL.md","content":"Say PLUM."}]}',
39 },
40 } satisfies Record<string, RecordedResponse>;
41
42 export const OPENCODE_1_RESPONSES = {
43 "/api/info": { status: 200, contentType: "text/html", body: SPA_HTML },
44 "/global/health": {
45 status: 200,
46 contentType: "application/json",
47 body: '{"healthy":true,"version":"1.18.32"}',
48 },
49 // 1.x also answers a wrong password with an empty 401 on every path.
50 unauthorized: { status: 401, body: "" },
51 } satisfies Record<string, RecordedResponse>;
52
53 export function replayOpenCodeServer(
54 responses:
55 | typeof OPENCODE_1_RESPONSES
56 | typeof OPENCODE_2_RESPONSES
57 | (typeof OPENCODE_2_RESPONSES & typeof OPENCODE_2_WORKSPACE_RESPONSES),
58 password: string,
59 requestedPaths: Array<string> = [],
60 ) {
61 const answers: Record<string, RecordedResponse | undefined> = responses;
62 // Both versions decode Basic credentials as UTF-8.
63 const expected = password
64 ? `Basic ${Buffer.from(`opencode:${password}`, "utf8").toString("base64")}`
65 : undefined;
66 return HttpClient.make((request) => {
67 const path = new URL(request.url).pathname;
68 requestedPaths.push(path);
69 const recorded: RecordedResponse =
70 request.headers.authorization === expected
71 ? (answers[path] ?? responses.unauthorized)
72 : responses.unauthorized;
73 return Effect.succeed(
74 HttpClientResponse.fromWeb(
75 request,
76 new Response(recorded.body, {
77 status: recorded.status,
78 headers: recorded.contentType ? { "content-type": recorded.contentType } : {},
79 }),
80 ),
81 );
82 });
83 }