Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions packages/opencode/src/command/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export namespace Command {
description: z.string().optional(),
agent: z.string().optional(),
model: z.string().optional(),
variant: z.string().optional(),
mcp: z.boolean().optional(),
// workaround for zod not supporting async functions natively so we use getters
// https://zod.dev/v4/changelog?id=zfunction
Expand Down Expand Up @@ -83,6 +84,7 @@ export namespace Command {
name,
agent: command.agent,
model: command.model,
variant: command.variant,
description: command.description,
get template() {
return command.template
Expand Down
1 change: 1 addition & 0 deletions packages/opencode/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ export namespace Config {
description: z.string().optional(),
agent: z.string().optional(),
model: z.string().optional(),
variant: z.string().optional(),
subtask: z.boolean().optional(),
})
export type Command = z.infer<typeof Command>
Expand Down
47 changes: 38 additions & 9 deletions packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,16 @@ export namespace SessionPrompt {
template = template.trim()

const model = await (async () => {
if (!command.model && command.variant) {
const error = new NamedError.Unknown({
message: `Command "${input.command}" has a variant "${command.variant}" but no model defined.`,
})
Bus.publish(Session.Event.Error, {
sessionID: input.sessionID,
error: error.toObject(),
})
throw error
}
if (command.model) {
return Provider.parseModel(command.model)
}
Expand All @@ -1519,19 +1529,38 @@ export namespace SessionPrompt {
return await lastModel(input.sessionID)
})()

try {
await Provider.getModel(model.providerID, model.modelID)
} catch (e) {
if (Provider.ModelNotFoundError.isInstance(e)) {
const { providerID, modelID, suggestions } = e.data
const hint = suggestions?.length ? ` Did you mean: ${suggestions.join(", ")}?` : ""
const info = await (async () => {
try {
return await Provider.getModel(model.providerID, model.modelID)
} catch (e) {
if (Provider.ModelNotFoundError.isInstance(e)) {
const { providerID, modelID, suggestions } = e.data
const hint = suggestions?.length ? ` Did you mean: ${suggestions.join(", ")}?` : ""
Bus.publish(Session.Event.Error, {
sessionID: input.sessionID,
error: new NamedError.Unknown({ message: `Model not found: ${providerID}/${modelID}.${hint}` }).toObject(),
})
}
throw e
}
})()

if (command.variant) {
const variants = info.variants ?? {}
const available = Object.keys(variants)
if (!variants[command.variant]) {
const hint = available.length ? ` Available variants: ${available.join(", ")}` : ""
const error = new NamedError.Unknown({
message: `Variant not found: ${model.providerID}/${model.modelID} with variant "${command.variant}".${hint}`,
})
Bus.publish(Session.Event.Error, {
sessionID: input.sessionID,
error: new NamedError.Unknown({ message: `Model not found: ${providerID}/${modelID}.${hint}` }).toObject(),
error: error.toObject(),
})
throw error
}
throw e
}

const agent = await Agent.get(agentName)
if (!agent) {
const available = await Agent.list().then((agents) => agents.filter((a) => !a.hidden).map((a) => a.name))
Expand Down Expand Up @@ -1565,7 +1594,7 @@ export namespace SessionPrompt {
model,
agent: agentName,
parts,
variant: input.variant,
variant: command.variant ?? input.variant,
})) as MessageV2.WithParts

Bus.publish(Command.Event.Executed, {
Expand Down
4 changes: 4 additions & 0 deletions packages/opencode/test/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ test("handles command configuration", async () => {
template: "test template",
description: "test command",
agent: "test_agent",
model: "test_model",
variant: "test_variant",
},
},
}),
Expand All @@ -243,6 +245,8 @@ test("handles command configuration", async () => {
template: "test template",
description: "test command",
agent: "test_agent",
model: "test_model",
variant: "test_variant",
})
},
})
Expand Down
Loading