Open History to see the last commit
1/**
2 * Responses recorded from `opencode serve` 1.18.32 and 2.0.18 on 2026-09-29, replayed by an
3 * `HttpClient` so the version probe runs against real bytes.
4 */
5import * as Effect from "effect/Effect";
6import { HttpClient, HttpClientResponse } from "effect/unstable/http";
7
8interface RecordedResponse {
9 readonly status: number;
10 readonly contentType?: string;
11 readonly body: string;
12}
13
14// Both versions serve their web UI's index page for unknown paths. Bodies are cut after the
15// first lines; the classifier rejects them on content type alone.
16const SPA_HTML =
17 '<!doctype html>\n<html lang="en" style="background-color: var(--v2-background-bg-deep, #fafafa)">\n';
18
19export const OPENCODE_2_RESPONSES = {
20 "/api/info": {
21 status: 200,
22 contentType: "application/json",
23 body: '{"version":"2.0.18","pid":2393556,"urls":["http://127.0.0.1:48771"],"paths":{"tmp":"/tmp/opencode"}}',
24 },
25 "/global/health": { status: 200, contentType: "text/html", body: SPA_HTML },
26 unauthorized: {
27 status: 401,
28 contentType: "application/json",
29 body: '{"_tag":"UnauthorizedError","message":"Authentication required"}',
30 },
31} satisfies Record<string, RecordedResponse>;
32
33/**
34 * A 2.0.18 server's command and skill lists for a directory with one command
35 * (`.opencode/command/bee.md`) and one skill (`.opencode/skills/plum`), live on
36 * 2026-09-29. Skill contents are cut; machine-wide skills are dropped.
37 */
38export const OPENCODE_2_WORKSPACE_RESPONSES = {
39 "/api/command": {
40 status: 200,
41 contentType: "application/json",
42 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"}]}',
43 },
44 "/api/skill": {
45 status: 200,
46 contentType: "application/json",
47 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."}]}',
48 },
49} satisfies Record<string, RecordedResponse>;
50
51export const OPENCODE_1_RESPONSES = {
52 "/api/info": { status: 200, contentType: "text/html", body: SPA_HTML },
53 "/global/health": {
54 status: 200,
55 contentType: "application/json",
56 body: '{"healthy":true,"version":"1.18.32"}',
57 },
58 // 1.x also answers a wrong password with an empty 401 on every path.
59 unauthorized: { status: 401, body: "" },
60} satisfies Record<string, RecordedResponse>;
61
62/** Replays a recorded server; credentials other than `password` (none when empty) get its 401. */
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 (checked live with `pässwörd` and `pass€word`).
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}