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
45 changes: 32 additions & 13 deletions packages/opencode/src/cli/cmd/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export const ModelsCommand = cmd({
describe: "refresh the models cache from models.dev",
type: "boolean",
})
.option("format", {
describe: "output format",
type: "string",
choices: ["text", "json"] as const,
default: "text",
})
},
handler: async (args) => {
if (args.refresh) {
Expand All @@ -36,6 +42,18 @@ export const ModelsCommand = cmd({
async fn() {
const providers = await Provider.list()

function collectModels(providerID: string) {
const provider = providers[providerID]
return Object.entries(provider.models)
.sort(([a], [b]) => a.localeCompare(b))
.map(([modelID, model]) => ({
...model,
id: `${providerID}/${modelID}`,
provider: providerID,
model: modelID,
}))
}

function printModels(providerID: string, verbose?: boolean) {
const provider = providers[providerID]
const sortedModels = Object.entries(provider.models).sort(([a], [b]) => a.localeCompare(b))
Expand All @@ -49,26 +67,27 @@ export const ModelsCommand = cmd({
}
}

if (args.provider) {
const provider = providers[args.provider]
if (!provider) {
UI.error(`Provider not found: ${args.provider}`)
return
}

printModels(args.provider, args.verbose)
return
}

const providerIDs = Object.keys(providers).sort((a, b) => {
const targetProviderIDs = args.provider ? [args.provider] : Object.keys(providers).sort((a, b) => {
const aIsOpencode = a.startsWith("opencode")
const bIsOpencode = b.startsWith("opencode")
if (aIsOpencode && !bIsOpencode) return -1
if (!aIsOpencode && bIsOpencode) return 1
return a.localeCompare(b)
})

for (const providerID of providerIDs) {
if (args.provider && !providers[args.provider]) {
UI.error(`Provider not found: ${args.provider}`)
return
}

if (args.format === "json") {
const models = targetProviderIDs.flatMap(collectModels)
process.stdout.write(JSON.stringify(models, null, 2))
process.stdout.write(EOL)
return
}

for (const providerID of targetProviderIDs) {
printModels(providerID, args.verbose)
}
},
Expand Down
Loading