Files
opencode-multi-model/.opencode/plans/archive/1774250210640-eager-orchid.md
T

3.6 KiB

model
model
mimo-v2-pro-free

Remember Last Used Models

Goal

When CLI open is called without -m models, read last used models from ~/.config/opencode-multi-model/settings.json and confirm with user (Y/n). Use --force to skip confirmation. After successful open from CLI, persist models to settings. Tool path is unchanged — always requires explicit values.

Files to Modify

File Change
src/core/utils.ts Add loadSettings(), saveSettings(), and move promptUser here
src/types.ts Add MultiModelSettings interface and saveToSettings to MultiModelOptions
src/core/open.ts Conditional save after success when saveToSettings is true
src/core/close.ts Remove local promptUser, import from utils
src/core/cleanup.ts Remove local promptUser, import from utils
src/cli.ts Load settings + confirmation (unless --force)
tests/utils.test.ts Add tests for loadSettings/saveSettings
tests/open.test.ts Add tests for settings save on success

Implementation Details

1. src/types.ts — Add settings interface and option flag

export interface MultiModelSettings {
  lastModels: string[];
}

Add saveToSettings?: boolean to MultiModelOptions.

2. src/core/utils.ts — Settings read/write + shared promptUser

Add three new exported functions:

  • getSettingsPath(): string — returns ~/.config/opencode-multi-model/settings.json
  • loadSettings(): Promise<MultiModelSettings> — reads and parses the JSON file. Returns { lastModels: [] } if file doesn't exist or is invalid.
  • saveSettings(settings: MultiModelSettings): Promise<void> — creates directory if needed, writes JSON with 2-space indent.

Also move the promptUser function (currently duplicated in close.ts:17-29 and cleanup.ts:9-21) into utils.ts as a shared export. Remove the local copies from both files and update imports.

3. src/core/open.ts — Conditional save after full success

Before the successful return at line 310, if options.saveToSettings is true, call saveSettings({ lastModels: models }). Only save on full success, not partial. CLI passes saveToSettings: true; tool passes nothing (defaults false).

4. src/cli.ts — Settings load + confirmation + --force

Add -f, --force option to the open command: .option("-f, --force", "Skip confirmation prompts", false) (matches close/cleanup pattern).

In the open command action, before calling openMultiModel:

  1. If options.models is empty, call loadSettings().
  2. If settings.lastModels is non-empty, use those. If empty, proceed with empty array (existing error from core function).
  3. If models were loaded from settings and --force is not set, confirm: Use models [model1, model2]? (Y/n): using promptUser from utils.
  4. If user rejects (n, no), print error and exit.
  5. Pass saveToSettings: true to openMultiModel so successful runs persist the models.

5. Tests

  • utils.test.ts: Test loadSettings returns defaults for missing file, parses valid JSON, handles corrupt JSON gracefully. Test saveSettings writes correct content.
  • open.test.ts: Test that openMultiModel calls saveSettings on success (mock fs / the save function).

Verification

  1. Run bun run typecheck — no type errors
  2. Run bun run lint — no lint errors
  3. Run bun test — all tests pass
  4. Manual CLI test:
    • opencode-multi-model open test1 -m model1 model2 → saves settings
    • opencode-multi-model open test2 → prompts with saved models, confirm with Enter → uses saved models
    • opencode-multi-model open test3 → prompt, type n → cancels