fix(opencode): show each OpenCode v2 model's real effort options, with one Default (#2439)

* fix(web): drop provider-default entry from dynamic reasoning effort options

* fix(cli): support OpenCode v2 /api/model variant lookup and Basic auth
This commit is contained in:
Junmo Kim
2026-11-15 08:00:10 +08:00
committed by GitHub
parent 9e85428913
commit 2d0c8a7695
4 changed files with 341 additions and 42 deletions
@@ -66,6 +66,99 @@ describe('parseOpencodeProviderVariants', () => {
expect(parseOpencodeProviderVariants('string')).toEqual({})
expect(parseOpencodeProviderVariants({})).toEqual({})
expect(parseOpencodeProviderVariants({ all: 'nope' })).toEqual({})
expect(parseOpencodeProviderVariants({ data: 'nope' })).toEqual({})
})
it('parses the OpenCode v2 /api/model payload shape captured from opencode serve', () => {
// Captured from `opencode serve` (v2.3.0) GET /api/model and stripped of keys/paths.
const v2Payload = {
location: {
directory: '/tmp/fixture-project'
},
data: [
{
id: 'iceman-free',
modelID: 'iceman-free',
providerID: 'opencode',
type: 'chat',
name: 'Iceman Free',
variants: [],
status: 'active',
enabled: true
},
{
id: 'flint-40b-free',
modelID: 'flint-40b-free',
providerID: 'opencode',
type: 'chat',
name: 'Flint 40B Free',
variants: [
{ id: 'low', settings: { reasoningEffort: 'low' } },
{ id: 'medium', settings: { reasoningEffort: 'medium' } },
{ id: 'high', settings: { reasoningEffort: 'high' } },
{ id: 'xhigh', settings: { reasoningEffort: 'xhigh' } },
{ id: 'max', settings: { reasoningEffort: 'max' } }
],
status: 'active',
enabled: true
},
{
id: 'minimaxai/minimax-m3.1-flash',
modelID: 'minimaxai/minimax-m3.1-flash',
providerID: 'nvidia',
type: 'chat',
name: 'MiniMax M3.1 Flash (agent)',
variants: [
{ id: 'none', body: { chat_template_kwargs: { thinking_mode: 'disabled' } } },
{ id: 'thinking', body: { chat_template_kwargs: { thinking_mode: 'enabled' } } }
],
status: 'active',
enabled: true
},
{
id: 'deepseek-v4.1-flash-free',
modelID: 'deepseek-v4.1-flash-free',
providerID: 'opencode',
type: 'chat',
name: 'DeepSeek V4.1 Flash Free',
variants: [
{ id: 'low', settings: { reasoningEffort: 'low' } },
{ id: 'high', settings: { reasoningEffort: 'high' } },
{ id: 'max', settings: { reasoningEffort: 'max' } }
],
status: 'active',
enabled: true
}
]
}
expect(parseOpencodeProviderVariants(v2Payload)).toEqual({
'opencode/flint-40b-free': ['low', 'medium', 'high', 'xhigh', 'max'],
'nvidia/minimaxai/minimax-m3.1-flash': ['none', 'thinking'],
'opencode/deepseek-v4.1-flash-free': ['low', 'high', 'max']
})
})
it('skips malformed v2 model entries and invalid variant items', () => {
const payload = {
location: { directory: '/tmp/fixture-project' },
data: [
'not-an-object',
{ id: 'missing-provider', variants: [{ id: 'low' }] },
{ providerID: 'p1', variants: [{ id: 'low' }] },
{ id: 'bad-variants', providerID: 'p1', variants: 'nope' },
{ id: 'empty-variants', providerID: 'p1', variants: [] },
{
id: 'partial',
providerID: 'p1',
variants: [null, { id: '' }, { id: ' ' }, { nope: true }, { id: 'high' }]
}
]
}
expect(parseOpencodeProviderVariants(payload)).toEqual({
'p1/partial': ['high']
})
})
})
@@ -254,6 +347,120 @@ describe('listOpencodeModelVariants resident lifecycle', () => {
expect(residentServe.kill).toHaveBeenCalled();
exitSpy.mockRestore();
});
it('authenticates against spawned v2 serve /api/model and ignores HTML /provider on initial spawn and resident reuse', async () => {
let capturedEnv: Record<string, string | undefined> | undefined;
vi.stubGlobal('Bun', {
spawn: vi.fn((argv: string[], opts?: { cwd?: string; env?: Record<string, string | undefined> }) => {
const proc = makeProc(argv);
if (argv[0] === 'ps') {
proc.stdout = new ReadableStream<Uint8Array>({
start(c) { c.enqueue(new TextEncoder().encode('')); c.close(); }
});
} else {
capturedEnv = opts?.env;
}
procs.push(proc);
return proc;
})
});
const v2Payload = {
location: { directory: '/tmp/fixture-project' },
data: [
{
id: 'deepseek-v4.1-flash-free',
modelID: 'deepseek-v4.1-flash-free',
providerID: 'opencode',
variants: [{ id: 'low' }, { id: 'high' }, { id: 'max' }]
}
]
};
vi.stubGlobal('fetch', vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
const url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
if (url.endsWith('/provider')) {
return new Response('<!doctype html><html><body>OpenCode</body></html>', {
status: 200,
headers: { 'content-type': 'text/html' }
});
}
if (url.endsWith('/api/model')) {
const password = capturedEnv?.OPENCODE_SERVER_PASSWORD ?? capturedEnv?.OPENCODE_PASSWORD;
const expectedAuth = password
? 'Basic ' + Buffer.from(`opencode:${password}`).toString('base64')
: null;
const headers = new Headers(init?.headers);
if (!expectedAuth || headers.get('authorization') !== expectedAuth) {
return new Response(JSON.stringify({ _tag: 'UnauthorizedError', message: 'Authentication required' }), {
status: 401,
headers: { 'content-type': 'application/json' }
});
}
return new Response(JSON.stringify(v2Payload), {
status: 200,
headers: { 'content-type': 'application/json' }
});
}
return new Response('Not Found', { status: 404 });
}));
mod = await import('./opencodeModelVariants');
const first = mod.listOpencodeModelVariants({ cwd: '/tmp/fixture-project' });
await vi.advanceTimersByTimeAsync(300);
const r1 = await first;
expect(r1).toEqual({
success: true,
variants: { 'opencode/deepseek-v4.1-flash-free': ['low', 'high', 'max'] }
});
expect(serveProcs().length).toBe(1);
// Resident reuse must also send the Basic auth header to /api/model.
const r2 = await mod.listOpencodeModelVariants({ cwd: '/tmp/fixture-project' });
expect(r2).toEqual({
success: true,
variants: { 'opencode/deepseek-v4.1-flash-free': ['low', 'high', 'max'] }
});
expect(serveProcs().length).toBe(1);
});
it('falls back to v1 /provider on a live acp server when /api/model returns 404', async () => {
vi.stubGlobal('Bun', {
spawn: vi.fn((argv: string[]) => {
const proc = makeProc(argv);
if (argv[0] === 'ps') {
proc.stdout = new ReadableStream<Uint8Array>({
start(c) {
c.enqueue(new TextEncoder().encode('opencode acp --cwd /a --port 42495 --hostname 127.0.0.1'));
c.close();
}
});
}
procs.push(proc);
return proc;
})
});
vi.stubGlobal('fetch', vi.fn(async (input: string | URL | Request) => {
const url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
if (url.endsWith('/api/model')) {
return new Response('Not Found', { status: 404 });
}
if (url.endsWith('/provider')) {
return new Response(JSON.stringify(providerPayload), { status: 200 });
}
return new Response('Not Found', { status: 404 });
}));
mod = await import('./opencodeModelVariants');
const r = await mod.listOpencodeModelVariants({ cwd: '/a' });
expect(r).toEqual({
success: true,
variants: { 'p/m': ['low', 'high'] }
});
expect(serveProcs().length).toBe(0);
});
});
describe('extractOpencodeAcpServers', () => {
+104 -40
View File
@@ -1,3 +1,4 @@
import { randomBytes } from 'node:crypto';
import { isObject } from '@hapi/protocol';
import type { OpencodeModelVariantsResponse } from '@hapi/protocol/apiTypes';
@@ -6,30 +7,66 @@ export type ListOpencodeModelVariantsResponse = OpencodeModelVariantsResponse;
const PROBE_TIMEOUT_MS = 10_000;
const POLL_INTERVAL_MS = 300;
function extractModelVariantIds(rawVariants: unknown): string[] {
if (Array.isArray(rawVariants)) {
const variants: string[] = [];
for (const entry of rawVariants) {
if (!isObject(entry)) continue;
const id = typeof entry.id === 'string' && entry.id.trim() ? entry.id : '';
if (id) {
variants.push(id);
}
}
return variants;
}
if (isObject(rawVariants)) {
return Object.keys(rawVariants).filter((key) => key.length > 0);
}
return [];
}
function addParsedModelVariants(out: Record<string, string[]>, rawModel: unknown): void {
if (!isObject(rawModel)) return;
const modelId = typeof rawModel.id === 'string' && rawModel.id.trim() ? rawModel.id : '';
const providerID = typeof rawModel.providerID === 'string' && rawModel.providerID.trim() ? rawModel.providerID : '';
if (!providerID || !modelId) return;
const variants = extractModelVariantIds(rawModel.variants);
if (variants.length === 0) return;
out[`${providerID}/${modelId}`] = variants;
}
function isOpencodeCatalogPayload(payload: unknown): boolean {
return isObject(payload) && (Array.isArray(payload.data) || Array.isArray(payload.all));
}
/**
* Extract a `{ 'providerId/modelId': string[] }` map of model variants from an
* OpenCode server `GET /provider` payload. Only models with at least one
* OpenCode server catalog payload — either v2 `GET /api/model` (`{ data: Model[] }`
* with `variants: Array<{ id }>`) or v1 `GET /provider` (`{ all: Provider[] }`
* with `variants: Record<string, unknown>`). Only models with at least one
* variant are collected; malformed entries are skipped defensively.
*/
export function parseOpencodeProviderVariants(payload: unknown): Record<string, string[]> {
if (!isObject(payload) || !Array.isArray(payload.all)) {
if (!isObject(payload)) {
return {};
}
const out: Record<string, string[]> = {};
for (const provider of payload.all) {
if (!isObject(provider) || !isObject(provider.models)) {
continue;
if (Array.isArray(payload.data)) {
for (const rawModel of payload.data) {
addParsedModelVariants(out, rawModel);
}
for (const [, rawModel] of Object.entries(provider.models)) {
if (!isObject(rawModel)) continue;
const modelId = typeof rawModel.id === 'string' && rawModel.id.trim() ? rawModel.id : '';
const providerID = typeof rawModel.providerID === 'string' ? rawModel.providerID : '';
if (!providerID || !modelId) continue;
if (!isObject(rawModel.variants)) continue;
const variants = Object.keys(rawModel.variants).filter((key) => key.length > 0);
if (variants.length === 0) continue;
out[`${providerID}/${modelId}`] = variants;
return out;
}
if (Array.isArray(payload.all)) {
for (const provider of payload.all) {
if (!isObject(provider) || !isObject(provider.models)) {
continue;
}
for (const rawModel of Object.values(provider.models)) {
addParsedModelVariants(out, rawModel);
}
}
}
return out;
@@ -73,14 +110,33 @@ async function listRunningOpencodeServers(): Promise<Array<{ port: number; cwd:
}
}
async function fetchVariantsFromServer(port: number): Promise<Record<string, string[]> | null> {
try {
const response = await fetch(`http://127.0.0.1:${port}/provider`, { signal: AbortSignal.timeout(1000) });
if (!response.ok) return null;
return parseOpencodeProviderVariants(await response.json());
} catch {
return null;
async function fetchVariantsFromServer(
port: number,
opts?: { timeoutMs?: number; authHeader?: string }
): Promise<Record<string, string[]> | null> {
const timeoutMs = opts?.timeoutMs ?? 1000;
const headers = opts?.authHeader ? { Authorization: opts.authHeader } : undefined;
for (const path of ['/api/model', '/provider'] as const) {
let response: Response;
try {
response = await fetch(`http://127.0.0.1:${port}${path}`, {
headers,
signal: AbortSignal.timeout(timeoutMs)
});
} catch {
// Transport failure / timeout means this server is unreachable.
return null;
}
if (!response.ok) {
continue;
}
const payload: unknown = await response.json().catch(() => null);
if (!isOpencodeCatalogPayload(payload)) {
continue;
}
return parseOpencodeProviderVariants(payload);
}
return null;
}
async function getEphemeralPort(): Promise<number> {
@@ -112,7 +168,7 @@ async function getEphemeralPort(): Promise<number> {
* the CLI exits gracefully. The reaper timer is unref'd so it never keeps the
* CLI process alive.
*/
type ResidentServe = { proc: ReturnType<typeof Bun.spawn>; port: number; cwd: string | null };
type ResidentServe = { proc: ReturnType<typeof Bun.spawn>; port: number; cwd: string | null; authHeader: string };
let resident: ResidentServe | null = null;
let idleReapTimer: ReturnType<typeof setTimeout> | null = null;
// In-flight coalescing is keyed by cwd: concurrent requests for different
@@ -169,10 +225,10 @@ export function listOpencodeModelVariants(request?: { cwd?: string | null }): Pr
}
async function listOpencodeModelVariantsUncached(cwd: string | null): Promise<ListOpencodeModelVariantsResponse> {
// Prefer a live OpenCode session's server — its /provider catalog is
// identical and answering from it skips any spawn entirely. Only servers
// running in the same cwd are used: project-level opencode config can
// override the provider catalog per directory.
// Prefer a live OpenCode session's server — its /api/model or /provider
// catalog is identical and answering from it skips any spawn entirely. Only
// servers running in the same cwd are used: project-level opencode config
// can override the provider catalog per directory.
for (const server of await listRunningOpencodeServers()) {
if (server.cwd !== cwd) continue;
const variants = await fetchVariantsFromServer(server.port);
@@ -189,7 +245,7 @@ async function listOpencodeModelVariantsUncached(cwd: string | null): Promise<Li
resident.proc.kill();
resident = null;
} else {
const variants = await fetchVariantsFromServer(resident.port);
const variants = await fetchVariantsFromServer(resident.port, { authHeader: resident.authHeader });
if (variants) {
scheduleIdleReap();
return { success: true, variants };
@@ -212,14 +268,15 @@ async function listOpencodeModelVariantsUncached(cwd: string | null): Promise<Li
if (previousResident) {
previousResident.proc.kill();
}
resident = { proc: spawned.proc, port: spawned.port, cwd };
resident = { proc: spawned.proc, port: spawned.port, cwd, authHeader: spawned.authHeader };
registerExitCleanup();
scheduleIdleReap();
return { success: true, variants: spawned.variants };
}
async function spawnServeWithCatalog(cwd: string | null): Promise<
{ success: false; error: string } | { success: true; proc: ReturnType<typeof Bun.spawn>; port: number; variants: Record<string, string[]> }
{ success: false; error: string }
| { success: true; proc: ReturnType<typeof Bun.spawn>; port: number; authHeader: string; variants: Record<string, string[]> }
> {
let port: number;
try {
@@ -228,16 +285,28 @@ async function spawnServeWithCatalog(cwd: string | null): Promise<
return { success: false, error: 'Failed to acquire ephemeral port' };
}
// OpenCode v2 `opencode serve` enforces HTTP Basic auth (`opencode:<pw>`)
// on `/api/model`. Generating a per-process secret lets us authenticate
// deterministically without parsing stdout.
const password = randomBytes(24).toString('hex');
const authHeader = 'Basic ' + Buffer.from(`opencode:${password}`).toString('base64');
let proc: ReturnType<typeof Bun.spawn> | null = null;
try {
proc = Bun.spawn(['opencode', 'serve', '--port', String(port)], {
stdout: 'ignore',
stderr: 'ignore',
env: {
...process.env,
OPENCODE_SERVER_PASSWORD: password,
OPENCODE_PASSWORD: password,
OPENCODE_SERVER_USERNAME: 'opencode'
},
...(cwd ? { cwd } : {}),
});
const deadline = Date.now() + PROBE_TIMEOUT_MS;
let payload: unknown = null;
let variants: Record<string, string[]> | null = null;
while (Date.now() < deadline) {
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
// Bail out early instead of burning the full deadline when serve
@@ -245,23 +314,18 @@ async function spawnServeWithCatalog(cwd: string | null): Promise<
if (proc.exitCode !== null) {
break;
}
try {
const response = await fetch(`http://127.0.0.1:${port}/provider`, { signal: AbortSignal.timeout(2000) });
if (response.ok) {
payload = await response.json();
break;
}
} catch {
// Server not ready yet — keep polling until the deadline.
variants = await fetchVariantsFromServer(port, { timeoutMs: 2000, authHeader });
if (variants !== null) {
break;
}
}
if (!payload || proc.exitCode !== null) {
if (variants === null || proc.exitCode !== null) {
proc.kill();
return { success: false, error: 'OpenCode provider catalog did not become available in time' };
}
return { success: true, proc, port, variants: parseOpencodeProviderVariants(payload) };
return { success: true, proc, port, authHeader, variants };
} catch (error) {
proc?.kill();
return {
@@ -72,6 +72,31 @@ describe('getCodexComposerReasoningEffortOptions', () => {
])
})
it('drops provider-default entries from dynamic options so Default is not duplicated', () => {
expect(getCodexComposerReasoningEffortOptions('default', 'opencode', [
{ value: 'low', name: 'Low' },
{ value: 'high', name: 'High' },
{ value: 'max', name: 'Max' },
{ value: 'default', name: 'Default' }
])).toEqual([
{ value: null, label: 'Default' },
{ value: 'low', label: 'Low' },
{ value: 'high', label: 'High' },
{ value: 'max', label: 'Max' }
])
expect(getCodexComposerReasoningEffortOptions('', 'dsh', [
{ value: '', name: 'Provider default' },
{ value: 'Default', name: 'Default' },
{ value: 'low', name: 'Low' },
{ value: 'high', name: 'High' }
])).toEqual([
{ value: null, label: 'Default' },
{ value: 'low', label: 'Low' },
{ value: 'high', label: 'High' }
])
})
it('preserves unsupported current OpenCode values in the dropdown', () => {
expect(getCodexComposerReasoningEffortOptions('high', 'opencode', [
{ value: 'low', name: 'Low' },
@@ -47,7 +47,10 @@ function buildDynamicReasoningEffortOptions(
currentEffort: string | null,
dynamicOptions: ComposerReasoningEffortSourceOption[]
): CodexComposerReasoningEffortOption[] {
const optionValues = new Set(dynamicOptions.map((option) => option.value))
const filteredOptions = dynamicOptions.filter(
(option) => normalizeCodexComposerReasoningEffort(option.value) !== null
)
const optionValues = new Set(filteredOptions.map((option) => option.value))
const options: CodexComposerReasoningEffortOption[] = [
{ value: null, label: 'Default' }
]
@@ -59,7 +62,7 @@ function buildDynamicReasoningEffortOptions(
})
}
options.push(...dynamicOptions.map((option) => ({
options.push(...filteredOptions.map((option) => ({
value: option.value,
label: option.name ?? formatCodexReasoningEffortLabel(option.value)
})))