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// Unknown paths serve SPA HTML; content type alone rejects these truncated bodies.
12const SPA_HTML =
13 '<!doctype html>\n<html lang="en" style="background-color: var(--v2-background-bg-deep, #fafafa)">\n';
14
15export 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
29export 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// OpenCode 2.4.2 `/api/event` data for a project: its scan ends, then a SKILL.md is added to it.
43export const OPENCODE_2_SKILL_EVENTS = {
44 scanCommands:
45 '{"id":"evt_2166d464c001SsOdwlKFp696kN","created":1795672589900,"type":"command.updated","location":{"directory":"/work"},"data":{}}',
46 scanSkills:
47 '{"id":"evt_2166d464e001YEzhFl7mAlBitq","created":1795672589902,"type":"skill.updated","location":{"directory":"/work"},"data":{}}',
48 skillAdded:
49 '{"id":"evt_2166d5160001YtAz1aBpkG7u8o","created":1795672592736,"type":"skill.updated","location":{"directory":"/work"},"data":{}}',
50};
51
52export const OPENCODE_1_RESPONSES = {
53 "/api/info": { status: 200, contentType: "text/html", body: SPA_HTML },
54 "/global/health": {
55 status: 200,
56 contentType: "application/json",
57 body: '{"healthy":true,"version":"1.18.32"}',
58 },
59 // 1.x also answers a wrong password with an empty 401 on every path.
60 unauthorized: { status: 401, body: "" },
61} satisfies Record<string, RecordedResponse>;
62
63export function replayOpenCodeServer(
64 responses:
65 | typeof OPENCODE_1_RESPONSES
66 | typeof OPENCODE_2_RESPONSES
67 | (typeof OPENCODE_2_RESPONSES & typeof OPENCODE_2_WORKSPACE_RESPONSES),
68 password: string,
69 requestedPaths: Array<string> = [],
70) {
71 const answers: Record<string, RecordedResponse | undefined> = responses;
72 // Both versions decode Basic credentials as UTF-8.
73 const expected = password
74 ? `Basic ${Buffer.from(`opencode:${password}`, "utf8").toString("base64")}`
75 : undefined;
76 return HttpClient.make((request) => {
77 const path = new URL(request.url).pathname;
78 requestedPaths.push(path);
79 const recorded: RecordedResponse =
80 request.headers.authorization === expected
81 ? (answers[path] ?? responses.unauthorized)
82 : responses.unauthorized;
83 return Effect.succeed(
84 HttpClientResponse.fromWeb(
85 request,
86 new Response(recorded.body, {
87 status: recorded.status,
88 headers: recorded.contentType ? { "content-type": recorded.contentType } : {},
89 }),
90 ),
91 );
92 });
93}