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
33export const OPENCODE_1_RESPONSES = {
34 "/api/info": { status: 200, contentType: "text/html", body: SPA_HTML },
35 "/global/health": {
36 status: 200,
37 contentType: "application/json",
38 body: '{"healthy":true,"version":"1.18.32"}',
39 },
40 // 1.x also answers a wrong password with an empty 401 on every path.
41 unauthorized: { status: 401, body: "" },
42} satisfies Record<string, RecordedResponse>;
43
44/** Replays a recorded server; credentials other than `password` (none when empty) get its 401. */
45export function replayOpenCodeServer(
46 responses: typeof OPENCODE_1_RESPONSES | typeof OPENCODE_2_RESPONSES,
47 password: string,
48 requestedPaths: Array<string> = [],
49) {
50 // Both versions decode Basic credentials as UTF-8 (checked live with `pässwörd` and `pass€word`).
51 const expected = password
52 ? `Basic ${Buffer.from(`opencode:${password}`, "utf8").toString("base64")}`
53 : undefined;
54 return HttpClient.make((request) => {
55 const path = new URL(request.url).pathname;
56 requestedPaths.push(path);
57 const recorded: RecordedResponse =
58 (path === "/api/info" || path === "/global/health") &&
59 request.headers.authorization === expected
60 ? responses[path]
61 : responses.unauthorized;
62 return Effect.succeed(
63 HttpClientResponse.fromWeb(
64 request,
65 new Response(recorded.body, {
66 status: recorded.status,
67 headers: recorded.contentType ? { "content-type": recorded.contentType } : {},
68 }),
69 ),
70 );
71 });
72}