opencode icon indicating copy to clipboard operation
opencode copied to clipboard

[FEATURE]: Add a "Set as default" action to /models to persist the current model

Open BingWuJ opened this issue 2 months ago • 5 comments

Feature hasn't been suggested before.

  • [x] I have verified this feature I'm about to request hasn't been suggested before. (searched: "set default model", "persist model", "save selected model", "remember model selection"; the closest matches, #11209 and #28110, are related but different — see Additional context)

Summary

The /models selection is session-scoped by design — the models guide states "The selection applies to the current session and does not change your configuration" and "Switching a session's model does not rewrite the config file". So when the configured default is model A and I switch to model B in the TUI, there is no first-class way to make B the default for future sessions — the only option is to hand-edit opencode.json(c).

Describe the enhancement

Add a quick, discoverable way to persist the current model selection as the default, for example:

  • A "Set as default" action in the /models dialog, and/or
  • A command-palette command (e.g. model.set_default) that can be keybound, writing the selected provider/model to the user's global opencode.json (global rather than project-level, so personal model preferences don't get committed to shared repos)

Current behavior

  1. Set "model": "provider/a" in the global opencode.json
  2. Start the TUI, press /models, select provider/b
  3. Work, then quit OpenCode
  4. Start OpenCode again — new sessions fall back to provider/a again (occasionally a session instead resumes the last-used model, see Additional context), and there is no built-in shortcut to make provider/b the default

Expected behavior

A supported way to persist the current model as the default without manually editing the config file.

Environment

  • opencode version: 2.0.7 (channel: latest)
  • OS: Windows 11 Home (build 26200)
  • Terminal: mintty (xterm-256color, truecolor)
  • Shell: Git Bash (usr/bin/bash.exe)
  • Active plugins: none configured

Additional context

  • This is an explicit, opt-in "write it to config" action, not automatic remembering. #28110 (per-agent model remembering in the desktop app) and #11209 (per-agent defaults in opencode.json) both aim to change which model is picked automatically; this request is about letting the user commit their current selection as the default at a moment of their choosing.
  • Observed locally: some new sessions start with the last-used model (different from the configured default), which suggests the TUI keeps internal per-agent model state (also referenced in #28110). Explicit "Set as default" semantics would make the precedence between the config default and the remembered selection predictable.

BingWuJ avatar Sep 18 '26 08:09 BingWuJ

I'll take this.

snhsish avatar Sep 18 '26 09:09 snhsish

Backing this with the exact precedence chain from the TUI source — the "some new sessions start with the last-used model" observation in the description has a concrete cause.

packages/tui/src/context/local.tsx resolves the effective model in this order:

// currentModel() — :236-245
getFirstValidModel(
  () => a && modelStore.model[a.name],   // in-memory, per-agent, never persisted
  () => a && a.model,                    // agent's configured model
  fallbackModel,                         // ↓
)

// fallbackModel() — :197-234
if (args.model) ...                       // :198  --model CLI flag
if (sync.data.config.model) ...           // :208  root "model" from opencode.json
for (const item of modelStore.recent)     // :218  most-recently-used list
sync.data.provider_default[provider.id]   // :226  provider-wide default

Two things make the current behavior unpredictable:

  1. config.model outranks the last-used model. model.json's recent[0] is the most recently selected model — recentModels() prepends on every /models selection (local.tsx:35-48, written from dialog-model.tsx:143). But the config.model branch at :208 returns first, so recent[0] is never reached while a root model is set.

  2. The per-agent slots are never persisted. save() at local.tsx:169-180 writes only recent, favorite, and variant — the modelStore.model record is dropped. Confirmed on a real install: ~/.local/state/opencode/model.json has no model key.

Together: set root model to A, pick B via /models, cycle agents with Tab — and the model resolves to A whenever the current agent has no in-memory slot. Removing the root model makes recent[0] win, which is what people expect, but it's an emergent side effect rather than a supported contract.

An explicit "Set as default" would make this precedence intentional rather than emergent. Prior art that was auto-closed as stale before anyone looked: #21351, #23369, #18359, #28110.

lenucksi avatar Sep 27 '26 12:09 lenucksi

I'm the reporter. I dug through the source while preparing an implementation, and wanted to leave notes here in case they help whoever picks this up (I saw @snhsish claimed it — happy to hand these over, or to submit the PR myself if preferred).

The server side already has most of the plumbing, so this is a fairly contained change:

  1. Schema — Config.Patch (packages/schema/src/config.ts) currently only accepts shell. Adding an optional model: ConfigModel.Selection | null is straightforward; ConfigModel.Selection already accepts the short provider/model[#variant] string form used in config files.
  2. Core — Config.update (packages/core/src/config.ts) already patches the highest-precedence global config document, preserves JSONC comments via jsonc-parser's modify/applyEdits, and triggers a config reload afterwards. Extending it to also write model (encoding the decoded selection back to the short string; null deletes the key) is small and self-contained. This matches the "global rather than project-level" requirement from the original report.
  3. Client — regenerate the generated clients; the endpoint (PATCH /api/experimental/config) is already exposed.
  4. TUI — dialog-model.tsx already has the exact pattern to copy: the Favorite action (model.dialog.favorite, ctrl+f). A Set as default action (model.dialog.default, e.g. ctrl+s) next to it would call the endpoint with the highlighted model and show a success toast. One caveat: DialogSelect only renders footer actions that have a bound key, so the new command needs a default keybind.
  5. Docs/tests — a row in cli/keybinds.mdx, a sentence in models.mdx, and a server test alongside the existing "updates the global shell without replacing unrelated JSONC" case.

One design detail worth deciding: whether the action should also offer "clear default" (the endpoint can already express it by patching model: null).

BingWuJ avatar Oct 16 '26 16:10 BingWuJ

Thanks for the detailed request, @BingWuJ!

To restate it: you'd like an explicit, opt-in way to save the model you've picked in /models as your default. That could be a "Set as default" action in the dialog and/or a command you can bind to a key. It would write the provider/model to your global opencode.json, so you don't have to edit the file by hand.

In v2, switching models in /models only affects the current session. The TUI also remembers recent models, favourites and variants in a separate preferences file, but nothing writes the model key back to your config today. That is why the configured default comes back. Maintainers will consider this.

Related issues (not the same request, as you noted):

  • #28110: automatic per-agent model memory in the desktop app
  • #11209: per-agent default models in opencode.json

A plugin can't do this cleanly yet. A TUI plugin can register a command and read the current model, but there is no plugin or client API for updating the model setting in your config.

opencodejr[bot] avatar Oct 28 '26 11:10 opencodejr[bot]

yes please

pkieltyka avatar Nov 26 '26 16:11 pkieltyka