|
| 1 | +// Persistent command-default helpers for the OpenCode companion. |
| 2 | +// |
| 3 | +// `/opencode:setup` can persist defaults in `state.config.defaults`. |
| 4 | +// These helpers keep precedence rules centralized and testable: |
| 5 | +// explicit runtime flags win, otherwise saved defaults apply. |
| 6 | + |
| 7 | +import { parseModelString } from "./model.mjs"; |
| 8 | + |
| 9 | +const SUPPORTED_DEFAULT_AGENTS = new Set(["build", "plan"]); |
| 10 | + |
| 11 | +/** |
| 12 | + * @param {Record<string, unknown>|undefined|null} options |
| 13 | + * @param {string} key |
| 14 | + * @returns {boolean} |
| 15 | + */ |
| 16 | +export function hasOwnOption(options, key) { |
| 17 | + return Object.prototype.hasOwnProperty.call(options ?? {}, key); |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Normalize persisted defaults read from state. Invalid or missing values are |
| 22 | + * ignored so a hand-edited state file cannot break every command invocation. |
| 23 | + * @param {unknown} raw |
| 24 | + * @returns {{ model: string | null, agent: string | null }} |
| 25 | + */ |
| 26 | +export function normalizeDefaults(raw) { |
| 27 | + const defaults = raw && typeof raw === "object" ? raw : {}; |
| 28 | + |
| 29 | + const modelRaw = typeof defaults.model === "string" ? defaults.model.trim() : ""; |
| 30 | + const model = modelRaw && parseModelString(modelRaw) ? modelRaw : null; |
| 31 | + |
| 32 | + const agentRaw = typeof defaults.agent === "string" ? defaults.agent.trim() : ""; |
| 33 | + const agent = SUPPORTED_DEFAULT_AGENTS.has(agentRaw) ? agentRaw : null; |
| 34 | + |
| 35 | + return { model, agent }; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Parse a `/opencode:setup --default-model` value. Returns null for "off". |
| 40 | + * @param {unknown} value |
| 41 | + * @returns {string | null} |
| 42 | + */ |
| 43 | +export function parseDefaultModelSetting(value) { |
| 44 | + const raw = typeof value === "string" ? value.trim() : ""; |
| 45 | + if (raw === "off") return null; |
| 46 | + if (!raw || !parseModelString(raw)) { |
| 47 | + throw new Error( |
| 48 | + `--default-model must be "off" or a provider/model-id value ` + |
| 49 | + `(e.g. anthropic/claude-opus-4-6).` |
| 50 | + ); |
| 51 | + } |
| 52 | + return raw; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Parse a `/opencode:setup --default-agent` value. Returns null for "off". |
| 57 | + * @param {unknown} value |
| 58 | + * @returns {"build" | "plan" | null} |
| 59 | + */ |
| 60 | +export function parseDefaultAgentSetting(value) { |
| 61 | + const raw = typeof value === "string" ? value.trim() : ""; |
| 62 | + if (raw === "off") return null; |
| 63 | + if (!SUPPORTED_DEFAULT_AGENTS.has(raw)) { |
| 64 | + throw new Error(`--default-agent must be "build", "plan", or "off".`); |
| 65 | + } |
| 66 | + return raw; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Apply a persisted model default when the user did not explicitly supply |
| 71 | + * either `--model` or `--free`. |
| 72 | + * @param {Record<string, unknown>} options |
| 73 | + * @param {{ model?: string | null }} defaults |
| 74 | + * @returns {Record<string, unknown>} |
| 75 | + */ |
| 76 | +export function applyDefaultModelOptions(options, defaults) { |
| 77 | + if (hasOwnOption(options, "model") || options?.free) return options; |
| 78 | + if (!defaults?.model) return options; |
| 79 | + return { ...options, model: defaults.model }; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Resolve the task agent using explicit CLI args first, then persisted |
| 84 | + * defaults, then the existing write/read-only fallback. |
| 85 | + * @param {Record<string, unknown>} options |
| 86 | + * @param {{ agent?: string | null }} defaults |
| 87 | + * @param {boolean} isWrite |
| 88 | + * @returns {string} |
| 89 | + */ |
| 90 | +export function resolveTaskAgentName(options, defaults, isWrite) { |
| 91 | + if (hasOwnOption(options, "agent")) return options.agent; |
| 92 | + if (defaults?.agent) return defaults.agent; |
| 93 | + return isWrite ? "build" : "plan"; |
| 94 | +} |
0 commit comments