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.jsonloadSettings(): 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:
- If
options.modelsis empty, callloadSettings(). - If
settings.lastModelsis non-empty, use those. If empty, proceed with empty array (existing error from core function). - If models were loaded from settings and
--forceis not set, confirm:Use models [model1, model2]? (Y/n):usingpromptUserfrom utils. - If user rejects (n, no), print error and exit.
- Pass
saveToSettings: truetoopenMultiModelso successful runs persist the models.
5. Tests
utils.test.ts: TestloadSettingsreturns defaults for missing file, parses valid JSON, handles corrupt JSON gracefully. TestsaveSettingswrites correct content.open.test.ts: Test thatopenMultiModelcallssaveSettingson success (mockfs/ the save function).
Verification
- Run
bun run typecheck— no type errors - Run
bun run lint— no lint errors - Run
bun test— all tests pass - Manual CLI test:
opencode-multi-model open test1 -m model1 model2→ saves settingsopencode-multi-model open test2→ prompts with saved models, confirm with Enter → uses saved modelsopencode-multi-model open test3→ prompt, typen→ cancels