Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions packages/opencode/src/cli/cmd/tui/context/local.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}
}

Expand Down Expand Up @@ -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,
Expand Down
Loading