Skip to content

Commit f7b1cf8

Browse files
committed
feat(plugin): add chat.model hook for dynamic model routing for token savings
Adds a new 'chat.model' plugin hook that allows plugins to dynamically route messages to different models based on content/complexity. When a plugin sets output.model, the routed model is persisted in session state so future turns and subagents (via Task tool) inherit it. Changes: - packages/plugin/src/index.ts: Add chat.model hook type definition - packages/opencode/src/session/prompt.ts: Fire chat.model hook before model resolution, persist routed model for subagent inheritance - packages/opencode/src/tool/task.ts: Check for routed model when resolving subagent model (fixes subagent model inheritance) Related: anomalyco#18644, anomalyco#17870, anomalyco#6928
1 parent 9a006d8 commit f7b1cf8

3 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ const STRUCTURED_OUTPUT_SYSTEM_PROMPT = `IMPORTANT: The user has requested struc
6666
export namespace SessionPrompt {
6767
const log = Log.create({ service: "session.prompt" })
6868

69+
// Tracks plugin-routed models per session for subagent inheritance
70+
const routedModels: Record<string, { providerID: string; modelID: string }> = {}
71+
72+
export function getRoutedModel(sessionID: string) {
73+
return routedModels[sessionID]
74+
}
75+
6976
const state = Instance.state(
7077
() => {
7178
const data: Record<
@@ -966,7 +973,29 @@ export namespace SessionPrompt {
966973
async function createUserMessage(input: PromptInput) {
967974
const agent = await Agent.get(input.agent ?? (await Agent.defaultAgent()))
968975

969-
const model = input.model ?? agent.model ?? (await lastModel(input.sessionID))
976+
// Resolve the proposed model before plugin can override
977+
const proposedModel = input.model ?? agent.model ?? (await lastModel(input.sessionID))
978+
979+
// Fire chat.model hook to allow plugins to dynamically route to different models
980+
const modelOverride = await Plugin.trigger(
981+
"chat.model",
982+
{
983+
sessionID: input.sessionID,
984+
agent: agent.name,
985+
proposedModel,
986+
},
987+
{ model: undefined as { providerID: string; modelID: string } | undefined },
988+
)
989+
990+
// If plugin set a model, persist it for subagent inheritance
991+
const model = modelOverride.model ?? proposedModel
992+
if (modelOverride.model) {
993+
routedModels[input.sessionID] = modelOverride.model
994+
log.info("plugin routed model", {
995+
sessionID: input.sessionID,
996+
model: modelOverride.model,
997+
})
998+
}
970999
const full =
9711000
!input.variant && agent.variant
9721001
? await Provider.getModel(model.providerID, model.modelID).catch(() => undefined)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ export const TaskTool = Tool.define("task", async (ctx) => {
105105
const msg = await MessageV2.get({ sessionID: ctx.sessionID, messageID: ctx.messageID })
106106
if (msg.info.role !== "assistant") throw new Error("Not an assistant message")
107107

108-
const model = agent.model ?? {
108+
const routedModel = SessionPrompt.getRoutedModel(ctx.sessionID)
109+
const model = agent.model ?? routedModel ?? {
109110
modelID: msg.info.modelID,
110111
providerID: msg.info.providerID,
111112
}

‎packages/plugin/src/index.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,21 @@ export interface Hooks {
179179
},
180180
output: { message: UserMessage; parts: Part[] },
181181
) => Promise<void>
182+
/**
183+
* Called to dynamically route messages to different models.
184+
* When a plugin sets output.model, OpenCode updates the session's
185+
* active model so future turns and subagents inherit the routed model.
186+
*/
187+
"chat.model"?: (
188+
input: {
189+
sessionID: string
190+
agent: string
191+
proposedModel: { providerID: string; modelID: string }
192+
},
193+
output: {
194+
model?: { providerID: string; modelID: string }
195+
},
196+
) => Promise<void>
182197
/**
183198
* Modify parameters sent to LLM
184199
*/

0 commit comments

Comments
 (0)