Skip to content

Adding a chat model plugin hook for model routing based on complexity#18845

Closed
rjmorales13 wants to merge 5 commits intoanomalyco:devfrom
rjmorales13:feat/plugin-chat-model-hook
Closed

Adding a chat model plugin hook for model routing based on complexity#18845
rjmorales13 wants to merge 5 commits intoanomalyco:devfrom
rjmorales13:feat/plugin-chat-model-hook

Conversation

@rjmorales13
Copy link
Copy Markdown

@rjmorales13 rjmorales13 commented Mar 23, 2026

Issue for this PR: #18844

Closes #

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Please provide a description of the issue, the changes you made to fix it, and why they work. It is expected that you understand why your changes work and if you do not understand why at least say as much so a maintainer knows how much to value the PR.

If you paste a large clearly AI generated description here your PR may be IGNORED or CLOSED!

How did you verify your code works?

Screenshots / recordings

image

If this is a UI change, please include a screenshot or recording.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

If you do not follow this template your PR will be automatically rejected.

… 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
Convert plain strings from plugin hook to branded types using
ProviderID.make() and ModelID.make(). Cast branded types to plain
strings when sending to the hook interface. Fixes TS2345/TS2322
typecheck errors.
Allows plugins to make content-based routing decisions by receiving
the user's message text in the chat.model hook. Previously plugins
could only route by agent name. Now plugins can analyze the actual
prompt to route simple tasks to free models and complex tasks to
premium models.
@github-actions
Copy link
Copy Markdown
Contributor

Hey! Your PR title Feat/plugin chat model hook doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

@github-actions github-actions bot added the needs:compliance This means the issue will auto-close after 2 hours. label Mar 23, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 23, 2026

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • No issue referenced. Please add Closes #<number> linking to the relevant issue.
  • "How did you verify your code works?" section is empty. Please explain how you tested.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions
Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Based on my search, the only PR that appears in all results is PR #18845 itself (the current PR). The searches returned various feature and provider-related PRs, but none of them appear to be duplicates of the "Feat/plugin chat model hook" PR.

No duplicate PRs found

@rjmorales13 rjmorales13 changed the title Feat/plugin chat model hook Adding a chat model plugin hook for model routing based on complexity Mar 23, 2026
@rjmorales13 rjmorales13 marked this pull request as draft March 23, 2026 21:54
@rjmorales13 rjmorales13 marked this pull request as ready for review March 23, 2026 21:55
@rjmorales13 rjmorales13 marked this pull request as draft March 23, 2026 21:56
Core changes (3 files, 55 lines):
- packages/plugin/src/index.ts: chat.model hook type with message field
- packages/opencode/src/session/prompt.ts: fire hook, persist routed model
- packages/opencode/src/tool/task.ts: subagent model inheritance

Reference plugin (1 file):
- packages/opencode/examples/plugins/token-optimizer.ts: cascading confidence router

Plugin features:
- Stage 1: Zero-cost heuristics (catches 60% - greetings, short queries)
- Stage 2: Complexity estimation with caching (catches 35%)
- Stage 3: Config-based routing with threshold (remaining 5%)
- Per-prompt overrides: !premium, !free, !think
- De-escalation: simple tasks on premium → route to free
- Escalation: complex tasks on free → route to premium
- Heartbeat fallback: Ollama slow → Zen/OpenRouter free
- Config: TOKEN_OPTIMIZER_FREE_MODEL, TOKEN_OPTIMIZER_PREMIUM_MODEL

Tested:
- De-escalation: hello on Claude → routed to Ollama
- Escalation: implement JWT auth → routed to Claude via OpenRouter
- Override: !premium skips routing
- Subagent inheritance: Task tool inherits routed model

Related: anomalyco#18844, anomalyco#18667, anomalyco#18666, anomalyco#18644, anomalyco#17870, anomalyco#6928
Documents implementation status, testing results, and pending items.
Testing for new changes is pending — will continue tomorrow.
@github-actions
Copy link
Copy Markdown
Contributor

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

@github-actions github-actions bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Mar 24, 2026
@github-actions github-actions bot closed this Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant