diff --git a/packages/opencode/src/cli/cmd/tui/context/local.tsx b/packages/opencode/src/cli/cmd/tui/context/local.tsx index d93079f12a42..dda5b36dbfa9 100644 --- a/packages/opencode/src/cli/cmd/tui/context/local.tsx +++ b/packages/opencode/src/cli/cmd/tui/context/local.tsx @@ -21,16 +21,26 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({ const sdk = useSDK() const toast = useToast() - function isModelValid(model: { providerID: string; modelID: string }) { + function isModelValid(model: { providerID: string; modelID: string }, allowUncached = false) { const provider = sync.data.provider.find((x) => x.id === model.providerID) - return !!provider?.models[model.modelID] + if (!provider) return false + // If model exists in cache, it's valid + if (provider.models[model.modelID]) return true + // If allowUncached is true (for CLI-provided models), accept it even if not in cache + return allowUncached } - function getFirstValidModel(...modelFns: (() => { providerID: string; modelID: string } | undefined)[]) { + function getFirstValidModel( + ...modelFns: (() => { providerID: string; modelID: string; allowUncached?: boolean } | undefined)[] + ) { for (const modelFn of modelFns) { const model = modelFn() if (!model) continue - if (isModelValid(model)) return model + if (isModelValid(model, model.allowUncached ?? false)) { + // Strip allowUncached from returned model to match expected type + const { providerID, modelID } = model + return { providerID, modelID } + } } } @@ -192,8 +202,24 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({ const currentModel = createMemo(() => { const a = agent.current() + const args = useArgs() + + // FIX: Include CLI -m flag in model resolution chain + // This allows custom providers like openrouter/* to work even if not in cache + const cliModel = args.model + ? (() => { + const { providerID, modelID } = Provider.parseModel(args.model) + if (providerID && modelID) { + // Mark CLI-provided models to skip strict cache validation + return { providerID, modelID, allowUncached: true } + } + return undefined + })() + : undefined + return ( getFirstValidModel( + () => cliModel, // CLI -m flag takes precedence () => modelStore.model[a.name], () => a.model, fallbackModel,