fix(provider-cache): extract models from provider.list().all response
OpenCode SDK does not expose client.model.list API. This caused the
provider-models cache to always be empty (models: {}), which in turn
caused delegate-task categories with requiresModel (e.g., 'deep',
'artistry') to fail with misleading 'Unknown category' errors.
Changes:
- connected-providers-cache.ts: Extract models from provider.list()
response's .all array instead of calling non-existent client.model.list
- category-resolver.ts: Distinguish between 'unknown category' and
'model not available' errors with clearer error messages
- Add comprehensive tests for both fixes
Bug chain:
client.model?.list is undefined -> empty cache -> isModelAvailable
returns false for requiresModel categories -> null returned from
resolveCategoryConfig -> 'Unknown category' error (wrong message)
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
import { describe, test, expect, beforeEach, afterEach, spyOn } from "bun:test"
|
||||
import { existsSync, mkdirSync, rmSync } from "fs"
|
||||
import { join } from "path"
|
||||
import * as dataPath from "./data-path"
|
||||
import { updateConnectedProvidersCache, readProviderModelsCache } from "./connected-providers-cache"
|
||||
|
||||
const TEST_CACHE_DIR = join(import.meta.dir, "__test-cache__")
|
||||
|
||||
describe("updateConnectedProvidersCache", () => {
|
||||
let cacheDirSpy: ReturnType<typeof spyOn>
|
||||
|
||||
beforeEach(() => {
|
||||
cacheDirSpy = spyOn(dataPath, "getOmoOpenCodeCacheDir").mockReturnValue(TEST_CACHE_DIR)
|
||||
if (existsSync(TEST_CACHE_DIR)) {
|
||||
rmSync(TEST_CACHE_DIR, { recursive: true })
|
||||
}
|
||||
mkdirSync(TEST_CACHE_DIR, { recursive: true })
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
cacheDirSpy.mockRestore()
|
||||
if (existsSync(TEST_CACHE_DIR)) {
|
||||
rmSync(TEST_CACHE_DIR, { recursive: true })
|
||||
}
|
||||
})
|
||||
|
||||
test("extracts models from provider.list().all response", async () => {
|
||||
//#given
|
||||
const mockClient = {
|
||||
provider: {
|
||||
list: async () => ({
|
||||
data: {
|
||||
connected: ["openai", "anthropic"],
|
||||
all: [
|
||||
{
|
||||
id: "openai",
|
||||
name: "OpenAI",
|
||||
env: [],
|
||||
models: {
|
||||
"gpt-5.3-codex": { id: "gpt-5.3-codex", name: "GPT-5.3 Codex" },
|
||||
"gpt-5.2": { id: "gpt-5.2", name: "GPT-5.2" },
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "anthropic",
|
||||
name: "Anthropic",
|
||||
env: [],
|
||||
models: {
|
||||
"claude-opus-4-6": { id: "claude-opus-4-6", name: "Claude Opus 4.6" },
|
||||
"claude-sonnet-4-5": { id: "claude-sonnet-4-5", name: "Claude Sonnet 4.5" },
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
//#when
|
||||
await updateConnectedProvidersCache(mockClient)
|
||||
|
||||
//#then
|
||||
const cache = readProviderModelsCache()
|
||||
expect(cache).not.toBeNull()
|
||||
expect(cache!.connected).toEqual(["openai", "anthropic"])
|
||||
expect(cache!.models).toEqual({
|
||||
openai: ["gpt-5.3-codex", "gpt-5.2"],
|
||||
anthropic: ["claude-opus-4-6", "claude-sonnet-4-5"],
|
||||
})
|
||||
})
|
||||
|
||||
test("writes empty models when provider has no models", async () => {
|
||||
//#given
|
||||
const mockClient = {
|
||||
provider: {
|
||||
list: async () => ({
|
||||
data: {
|
||||
connected: ["empty-provider"],
|
||||
all: [
|
||||
{
|
||||
id: "empty-provider",
|
||||
name: "Empty",
|
||||
env: [],
|
||||
models: {},
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
//#when
|
||||
await updateConnectedProvidersCache(mockClient)
|
||||
|
||||
//#then
|
||||
const cache = readProviderModelsCache()
|
||||
expect(cache).not.toBeNull()
|
||||
expect(cache!.models).toEqual({})
|
||||
})
|
||||
|
||||
test("writes empty models when all field is missing", async () => {
|
||||
//#given
|
||||
const mockClient = {
|
||||
provider: {
|
||||
list: async () => ({
|
||||
data: {
|
||||
connected: ["openai"],
|
||||
},
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
//#when
|
||||
await updateConnectedProvidersCache(mockClient)
|
||||
|
||||
//#then
|
||||
const cache = readProviderModelsCache()
|
||||
expect(cache).not.toBeNull()
|
||||
expect(cache!.models).toEqual({})
|
||||
})
|
||||
|
||||
test("does nothing when client.provider.list is not available", async () => {
|
||||
//#given
|
||||
const mockClient = {}
|
||||
|
||||
//#when
|
||||
await updateConnectedProvidersCache(mockClient)
|
||||
|
||||
//#then
|
||||
const cache = readProviderModelsCache()
|
||||
expect(cache).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -149,10 +149,12 @@ export function writeProviderModelsCache(data: { models: Record<string, string[]
|
||||
*/
|
||||
export async function updateConnectedProvidersCache(client: {
|
||||
provider?: {
|
||||
list?: () => Promise<{ data?: { connected?: string[] } }>
|
||||
}
|
||||
model?: {
|
||||
list?: () => Promise<{ data?: Array<{ id: string; provider: string }> }>
|
||||
list?: () => Promise<{
|
||||
data?: {
|
||||
connected?: string[]
|
||||
all?: Array<{ id: string; models?: Record<string, unknown> }>
|
||||
}
|
||||
}>
|
||||
}
|
||||
}): Promise<void> {
|
||||
if (!client?.provider?.list) {
|
||||
@@ -167,31 +169,23 @@ export async function updateConnectedProvidersCache(client: {
|
||||
|
||||
writeConnectedProvidersCache(connected)
|
||||
|
||||
// Always update provider-models cache (overwrite with fresh data)
|
||||
let modelsByProvider: Record<string, string[]> = {}
|
||||
if (client.model?.list) {
|
||||
try {
|
||||
const modelsResult = await client.model.list()
|
||||
const models = modelsResult.data ?? []
|
||||
const modelsByProvider: Record<string, string[]> = {}
|
||||
const allProviders = result.data?.all ?? []
|
||||
|
||||
for (const model of models) {
|
||||
if (!modelsByProvider[model.provider]) {
|
||||
modelsByProvider[model.provider] = []
|
||||
}
|
||||
modelsByProvider[model.provider].push(model.id)
|
||||
for (const provider of allProviders) {
|
||||
if (provider.models) {
|
||||
const modelIds = Object.keys(provider.models)
|
||||
if (modelIds.length > 0) {
|
||||
modelsByProvider[provider.id] = modelIds
|
||||
}
|
||||
|
||||
log("[connected-providers-cache] Fetched models from API", {
|
||||
providerCount: Object.keys(modelsByProvider).length,
|
||||
totalModels: models.length,
|
||||
})
|
||||
} catch (modelErr) {
|
||||
log("[connected-providers-cache] Error fetching models, writing empty cache", { error: String(modelErr) })
|
||||
}
|
||||
} else {
|
||||
log("[connected-providers-cache] client.model.list not available, writing empty cache")
|
||||
}
|
||||
|
||||
log("[connected-providers-cache] Extracted models from provider list", {
|
||||
providerCount: Object.keys(modelsByProvider).length,
|
||||
totalModels: Object.values(modelsByProvider).reduce((sum, ids) => sum + ids.length, 0),
|
||||
})
|
||||
|
||||
writeProviderModelsCache({
|
||||
models: modelsByProvider,
|
||||
connected,
|
||||
|
||||
Reference in New Issue
Block a user