Skip to content

Commit 22300b9

Browse files
committed
feat: add subagent_models config option to optionally let the main llm run subagents with a chosen model, good for pseudo-headless tooling
1 parent d59357c commit 22300b9

3 files changed

Lines changed: 58 additions & 18 deletions

File tree

‎packages/opencode/src/config/config.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,11 @@ export namespace Config {
171171
{
172172
cwd: dir,
173173
},
174-
).catch(() => {})
174+
).catch(() => { })
175175

176176
// Install any additional dependencies defined in the package.json
177177
// This allows local plugins and custom tools to use external packages
178-
await BunProc.run(["install"], { cwd: dir }).catch(() => {})
178+
await BunProc.run(["install"], { cwd: dir }).catch(() => { })
179179
}
180180

181181
const COMMAND_GLOB = new Bun.Glob("{command,commands}/**/*.md")
@@ -809,6 +809,12 @@ export namespace Config {
809809
prune: z.boolean().optional().describe("Enable pruning of old tool outputs (default: true)"),
810810
})
811811
.optional(),
812+
subagent_models: z
813+
.record(z.string(), z.string())
814+
.optional()
815+
.describe(
816+
"Named models for subagents. Keys become enum values the LLM can choose from, values are provider/model strings (e.g., { fast: 'anthropic/claude-3-5-haiku-latest', smart: 'anthropic/claude-sonnet-4-20250514' })",
817+
),
812818
experimental: z
813819
.object({
814820
hook: z
@@ -882,7 +888,7 @@ export namespace Config {
882888
await Bun.write(path.join(Global.Path.config, "config.json"), JSON.stringify(result, null, 2))
883889
await fs.unlink(path.join(Global.Path.config, "config"))
884890
})
885-
.catch(() => {})
891+
.catch(() => { })
886892

887893
return result
888894
})
@@ -977,7 +983,7 @@ export namespace Config {
977983
const plugin = data.plugin[i]
978984
try {
979985
data.plugin[i] = import.meta.resolve!(plugin, configFilepath)
980-
} catch (err) {}
986+
} catch (err) { }
981987
}
982988
}
983989
return data

‎packages/opencode/src/tool/task.ts‎

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,57 @@ import { SessionPrompt } from "../session/prompt"
1010
import { iife } from "@/util/iife"
1111
import { defer } from "@/util/defer"
1212
import { Config } from "../config/config"
13+
import { Provider } from "../provider/provider"
1314

1415
export const TaskTool = Tool.define("task", async () => {
1516
const agents = await Agent.list().then((x) => x.filter((a) => a.mode !== "primary"))
16-
const description = DESCRIPTION.replace(
17+
const config = await Config.get()
18+
19+
// Build model enum from config's subagent_models keys
20+
const subagentModels = config.subagent_models ?? {}
21+
const modelKeys = Object.keys(subagentModels)
22+
23+
// Build description with model options if available
24+
let description = DESCRIPTION.replace(
1725
"{agents}",
1826
agents
1927
.map((a) => `- ${a.name}: ${a.description ?? "This subagent should only be called manually by the user."}`)
2028
.join("\n"),
2129
)
30+
if (modelKeys.length > 0) {
31+
description += `\n\nAvailable model options for the 'model' parameter: ${modelKeys.join(", ")}`
32+
}
33+
34+
// Build parameters schema with optional model enum
35+
const baseParams = {
36+
description: z.string().describe("A short (3-5 words) description of the task"),
37+
prompt: z.string().describe("The task for the agent to perform"),
38+
subagent_type: z.string().describe("The type of specialized agent to use for this task"),
39+
session_id: z.string().describe("Existing Task session to continue").optional(),
40+
command: z.string().describe("The command that triggered this task").optional(),
41+
}
42+
43+
const parameters =
44+
modelKeys.length > 0
45+
? z.object({
46+
...baseParams,
47+
model: z
48+
.enum(modelKeys as [string, ...string[]])
49+
.optional()
50+
.describe("Model to use for this subagent (from configured subagent_models options)"),
51+
})
52+
: z.object(baseParams)
53+
2254
return {
2355
description,
24-
parameters: z.object({
25-
description: z.string().describe("A short (3-5 words) description of the task"),
26-
prompt: z.string().describe("The task for the agent to perform"),
27-
subagent_type: z.string().describe("The type of specialized agent to use for this task"),
28-
session_id: z.string().describe("Existing Task session to continue").optional(),
29-
command: z.string().describe("The command that triggered this task").optional(),
30-
}),
56+
parameters,
57+
3158
async execute(params, ctx) {
3259
const agent = await Agent.get(params.subagent_type)
3360
if (!agent) throw new Error(`Unknown agent type: ${params.subagent_type} is not a valid agent type`)
3461
const session = await iife(async () => {
3562
if (params.session_id) {
36-
const found = await Session.get(params.session_id).catch(() => {})
63+
const found = await Session.get(params.session_id).catch(() => { })
3764
if (found) return found
3865
}
3966

@@ -76,10 +103,16 @@ export const TaskTool = Tool.define("task", async () => {
76103
})
77104
})
78105

79-
const model = agent.model ?? {
80-
modelID: msg.info.modelID,
81-
providerID: msg.info.providerID,
82-
}
106+
// Resolve model: use params.model alias if provided, otherwise agent's model, otherwise inherit from parent
107+
const model = iife(() => {
108+
if ((params as any).model && subagentModels[(params as any).model]) {
109+
return Provider.parseModel(subagentModels[(params as any).model])
110+
}
111+
return agent.model ?? {
112+
modelID: msg.info.modelID,
113+
providerID: msg.info.providerID,
114+
}
115+
})
83116

84117
function cancel() {
85118
SessionPrompt.cancel(session.id)
@@ -88,7 +121,6 @@ export const TaskTool = Tool.define("task", async () => {
88121
using _ = defer(() => ctx.abort.removeEventListener("abort", cancel))
89122
const promptParts = await SessionPrompt.resolvePromptParts(params.prompt)
90123

91-
const config = await Config.get()
92124
const result = await SessionPrompt.prompt({
93125
messageID,
94126
sessionID: session.id,

‎packages/opencode/src/tool/task.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Available agent types and the tools they have access to:
55

66
When using the Task tool, you must specify a subagent_type parameter to select which agent type to use.
77

8+
If subagent_models are configured in your opencode.json config, you can optionally specify a model parameter to override which model the subagent uses. The model parameter accepts one of the named aliases defined in your config's subagent_models section.
9+
810
When to use the Task tool:
911
- When you are instructed to execute custom slash commands. Use the Task tool with the slash command invocation as the entire prompt. The slash command can take arguments. For example: Task(description="Check the file", prompt="/check-file path/to/file.py")
1012

0 commit comments

Comments
 (0)