Open History to see the last commit
1import * as Effect from "effect/Effect";
2import * as HttpClient from "effect/http/HttpClient";
3import * as HttpClientResponse from "effect/http/HttpClientResponse";
4
5interface RecordedResponse {
6 readonly status: number;
7 readonly contentType?: string;
8 readonly body: string;
9}
10
11// Both versions serve their web UI's index page for unknown paths. Bodies are cut after the
12// first lines; the classifier rejects them on content type alone.
13const SPA_HTML =
14 '<!doctype html>\n<html lang="en" style="background-color: var(--v2-background-bg-deep, #fafafa)">\n';
15
16export const OPENCODE_2_RESPONSES = {
17 "/api/info": {
18 status: 200,
19 contentType: "application/json",
20 body: '{"version":"2.0.18","pid":2393556,"urls":["http://127.0.0.1:48771"],"paths":{"tmp":"/tmp/opencode"}}',
21 },
22 "/global/health": { status: 200, contentType: "text/html", body: SPA_HTML },
23 unauthorized: {
24 status: 401,
25 contentType: "application/json",
26 body: '{"_tag":"UnauthorizedError","message":"Authentication required"}',
27 },
28} satisfies Record<string, RecordedResponse>;
29
30export const OPENCODE_2_WORKSPACE_RESPONSES = {
31 "/api/command": {
32 status: 200,
33 contentType: "application/json",
34 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"}]}',
35 },
36 "/api/skill": {
37 status: 200,
38 contentType: "application/json",
39 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."}]}',
40 },
41} satisfies Record<string, RecordedResponse>;
42
43export const OPENCODE_1_RESPONSES = {
44 "/api/info": { status: 200, contentType: "text/html", body: SPA_HTML },
45 "/global/health": {
46 status: 200,
47 contentType: "application/json",
48 body: '{"healthy":true,"version":"1.18.32"}',
49 },
50 // 1.x also answers a wrong password with an empty 401 on every path.
51 unauthorized: { status: 401, body: "" },
52} satisfies Record<string, RecordedResponse>;
53
54export function replayOpenCodeServer(
55 responses:
56 | typeof OPENCODE_1_RESPONSES
57 | typeof OPENCODE_2_RESPONSES
58 | (typeof OPENCODE_2_RESPONSES & typeof OPENCODE_2_WORKSPACE_RESPONSES),
59 password: string,
60 requestedPaths: Array<string> = [],
61) {
62 const answers: Record<string, RecordedResponse | undefined> = responses;
63 // Both versions decode Basic credentials as UTF-8 (checked live with `pässwörd` and `pass€word`).
64 const expected = password
65 ? `Basic ${Buffer.from(`opencode:${password}`, "utf8").toString("base64")}`
66 : undefined;
67 return HttpClient.make((request) => {
68 const path = new URL(request.url).pathname;
69 requestedPaths.push(path);
70 const recorded: RecordedResponse =
71 request.headers.authorization === expected
72 ? (answers[path] ?? responses.unauthorized)
73 : responses.unauthorized;
74 return Effect.succeed(
75 HttpClientResponse.fromWeb(
76 request,
77 new Response(recorded.body, {
78 status: recorded.status,
79 headers: recorded.contentType ? { "content-type": recorded.contentType } : {},
80 }),
81 ),
82 );
83 });
84}