Skip to content

feat(plugin): add request-scoped chat.model hook - #40188

Open
millsydotdev wants to merge 1 commit into
anomalyco:devfrom
millsydotdev:feat/chat-model-hook
Open

feat(plugin): add request-scoped chat.model hook#40188
millsydotdev wants to merge 1 commit into
anomalyco:devfrom
millsydotdev:feat/chat-model-hook

Conversation

@millsydotdev

@millsydotdev millsydotdev commented Aug 2, 2026

Copy link
Copy Markdown

Issue for this PR

Closes:

Addresses / partially addresses:

Type of change

  • New feature

What does this PR do?

Adds a request-scoped chat.model plugin hook that fires before provider/model/auth resolution. A plugin can replace the model for a single request without changing the session's selected model. Enables per-task routing (local vs cloud, fast vs heavy) without session mutation.

This is a rework of the earlier #30457 implementation against the current dev API (provider.getModel with ProviderV2.ID / ModelV2.ID, SessionV1.User, current llm.ts structure). The chat.model hook type is added to @opencode-ai/plugin; the LLM run path triggers it and resolves the routed model via the provider service; chat.headers and downstream hooks see the routed model; logs include routedFrom + reason for observability.

No models are hardcoded anywhere. The hook output must reference models from the user.s provider configuration.

How did you verify your code works?

  • 3 new integration tests in packages/opencode/test/session/llm.test.ts: noop route keeps the original model, routing replaces the model for the request, and the hook receives session/agent/message context. Full file: 31 pass / 0 fail.
  • bun run typecheck: 30/30 packages pass.
  • bun run lint: no new issues in changed files (the repo's single pre-existing lint error is in packages/app/test-browser/prompt-persistence.test.ts, untouched).
  • Docs: chat.model example added to the plugins docs (config/env-driven, no hardcoded models).

Screenshots / recordings

n/a

Checklist

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

@millsydotdev

Copy link
Copy Markdown
Author

This is a first-contributor PR from a fork, so the typecheck/test/nix-eval workflows are waiting for maintainer approval before they run (status: action_required). Everything they check has been verified locally:

  • bun run typecheck: 30/30 packages pass
  • GITHUB_ACTIONS=false bun turbo test: all packages pass (packages/opencode: 3231 pass / 0 fail, 247 files, includes the 3 new chat.model tests)
  • bun run check:generated: clean
  • bun run test:httpapi: 208/208 pass (coverage + auth modes; effect mode re-running now)
  • lint: clean on changed files (the repo's pre-existing packages/app failures are untouched by this diff)

Could someone approve the workflow runs? Happy to adjust anything on review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants