fix: Provider.models is a Record keyed by modelID, not an array
Per the OpenCode server docs (GET /config/providers), each Provider's
'models' is { [modelID: string]: Model } (a Record), so model IDs can be
referenced as strings (matching the 'default' map of providerID ->
modelID). Our code treated it as Model[], which:
1. crashed with 'p.models is not iterable' on real responses, and
2. after the previous Array.isArray guard, skipped EVERY provider
(a Record is never an array), so the inline picker showed nothing.
Update types.ts (OpenCodeProvider.models -> Record, add OpenCodeModel,
add optional 'default' map to the response) and rewrite
flattenProviders to iterate Object.keys(p.models). Update test fixture
to the Record shape.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
f7099be060
commit
e7579d3779
@@ -184,8 +184,13 @@ describe('OpenCodeCellActions', () => {
|
|||||||
it('renders a model selector with the first option selected when providers are cached', () => {
|
it('renders a model selector with the first option selected when providers are cached', () => {
|
||||||
setOpenCodeProviders({
|
setOpenCodeProviders({
|
||||||
providers: [
|
providers: [
|
||||||
{ id: 'anthropic', models: [{ id: 'claude-sonnet-4-20250514' }] },
|
{
|
||||||
{ id: 'openai', models: [{ id: 'gpt-5' }] }
|
id: 'anthropic',
|
||||||
|
models: {
|
||||||
|
'claude-sonnet-4-20250514': { id: 'claude-sonnet-4-20250514' }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ id: 'openai', models: { 'gpt-5': { id: 'gpt-5' } } }
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
const cell = makeFakeCell('x = 1');
|
const cell = makeFakeCell('x = 1');
|
||||||
|
|||||||
@@ -114,14 +114,13 @@ function flattenProviders(
|
|||||||
}
|
}
|
||||||
const out: { providerId: string; modelId: string }[] = [];
|
const out: { providerId: string; modelId: string }[] = [];
|
||||||
for (const p of providers.providers) {
|
for (const p of providers.providers) {
|
||||||
// Some OpenCode Serve responses include providers without a `models`
|
// Per the OpenCode server docs, Provider.models is a Record keyed by
|
||||||
// field (or with a non-array value). Guard to avoid the "p.models is
|
// modelID ({ [modelID: string]: Model }), NOT an array.
|
||||||
// not iterable" crash and simply skip such providers in the picker.
|
if (!p.models || typeof p.models !== "object") {
|
||||||
if (!Array.isArray(p.models)) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
for (const m of p.models) {
|
for (const modelId of Object.keys(p.models)) {
|
||||||
out.push({ providerId: p.id, modelId: m.id });
|
out.push({ providerId: p.id, modelId });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
|
|||||||
+17
-2
@@ -47,12 +47,27 @@ export interface OpenCodeFailure {
|
|||||||
|
|
||||||
export type OpenCodeResponse = OpenCodeSuccess | OpenCodeFailure;
|
export type OpenCodeResponse = OpenCodeSuccess | OpenCodeFailure;
|
||||||
|
|
||||||
/** Response from GET /opencode-bridge/providers. */
|
/** Response from GET /opencode-bridge/providers (proxies OpenCode /config/providers).
|
||||||
|
*
|
||||||
|
* Per the OpenCode server docs, `/config/providers` returns
|
||||||
|
* `{ providers: Provider[], default: { [providerID]: modelID } }`. Each
|
||||||
|
* `Provider.models` is a Record keyed by modelID (NOT an array), so we
|
||||||
|
* can reference a model by its string ID (also matching `default[providerID]`).
|
||||||
|
*/
|
||||||
|
export interface OpenCodeModel {
|
||||||
|
id: string;
|
||||||
|
name?: string;
|
||||||
|
[k: string]: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
export interface OpenCodeProvider {
|
export interface OpenCodeProvider {
|
||||||
id: string;
|
id: string;
|
||||||
models: { id: string; [k: string]: unknown }[];
|
name?: string;
|
||||||
|
source?: string;
|
||||||
|
models: { [modelID: string]: OpenCodeModel };
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface OpenCodeProvidersResponse {
|
export interface OpenCodeProvidersResponse {
|
||||||
providers: OpenCodeProvider[];
|
providers: OpenCodeProvider[];
|
||||||
|
default?: { [providerID: string]: string };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user