1- import type { ModeSwitcherConfig , ModePreset , AgentPreset } from './types.ts'
2- import { DEFAULT_ECONOMY_MODEL } from './types.ts'
31import {
4- loadOpencodeConfig ,
52 loadOhMyOpencodeConfig ,
3+ loadOpencodeConfig ,
64 loadPluginConfig ,
7- savePluginConfig ,
85 pluginConfigExists ,
6+ savePluginConfig ,
97} from './loader.ts'
8+ import type { AgentPreset , ModePreset , ModeSwitcherConfig } from './types.ts'
9+ import { DEFAULT_ECONOMY_MODEL } from './types.ts'
1010
1111/**
12- * Default opencode agent names
12+ * Default opencode agent names.
13+ *
14+ * These are the standard agents used by OpenCode for various tasks.
15+ *
16+ * @constant
1317 */
1418const OPENCODE_AGENTS = [
1519 'build' ,
@@ -22,7 +26,19 @@ const OPENCODE_AGENTS = [
2226] as const
2327
2428/**
25- * Build a preset from existing configurations
29+ * Builds a performance preset from existing OpenCode configurations.
30+ *
31+ * This function scans the current `opencode.json` and `oh-my-opencode.json`
32+ * files to extract the currently configured models for each agent. These
33+ * models are saved as the "performance" preset, preserving the user's
34+ * high-performance model choices.
35+ *
36+ * @returns Promise resolving to a ModePreset with performance-oriented models
37+ * @example
38+ * ```typescript
39+ * const preset = await buildPerformancePreset();
40+ * console.log(preset.opencode.build.model); // "anthropic/claude-sonnet-4"
41+ * ```
2642 */
2743async function buildPerformancePreset ( ) : Promise < ModePreset > {
2844 const opencodeConfig = await loadOpencodeConfig ( )
@@ -64,7 +80,19 @@ async function buildPerformancePreset(): Promise<ModePreset> {
6480}
6581
6682/**
67- * Build economy preset with free model
83+ * Builds an economy preset using the default free model.
84+ *
85+ * This function creates a cost-efficient preset where all agents
86+ * (both OpenCode and oh-my-opencode) are configured to use the
87+ * `opencode/glm-4.7-free` model. This provides a budget-friendly
88+ * alternative to performance mode for routine tasks.
89+ *
90+ * @returns Promise resolving to a ModePreset with economy-oriented models
91+ * @example
92+ * ```typescript
93+ * const preset = await buildEconomyPreset();
94+ * console.log(preset.model); // "opencode/glm-4.7-free"
95+ * ```
6896 */
6997async function buildEconomyPreset ( ) : Promise < ModePreset > {
7098 const opencodeConfig = await loadOpencodeConfig ( )
@@ -101,8 +129,27 @@ async function buildEconomyPreset(): Promise<ModePreset> {
101129}
102130
103131/**
104- * Initialize the plugin configuration if it doesn't exist
105- * @returns The configuration (existing or newly created)
132+ * Initializes the plugin configuration if it doesn't exist.
133+ *
134+ * This function performs the following steps:
135+ * 1. Checks if a configuration file already exists
136+ * 2. If exists, loads and returns it
137+ * 3. If not, creates a new configuration by:
138+ * - Building a performance preset from current settings
139+ * - Building an economy preset with free models
140+ * - Setting default mode to "performance"
141+ * - Saving the configuration to disk
142+ *
143+ * This is called on plugin startup to ensure a valid configuration
144+ * is always available.
145+ *
146+ * @returns Promise resolving to the configuration (existing or newly created)
147+ * @throws {Error } If configuration creation or file I/O fails
148+ * @example
149+ * ```typescript
150+ * const config = await initializeConfig();
151+ * console.log(config.currentMode); // "performance"
152+ * ```
106153 */
107154export async function initializeConfig ( ) : Promise < ModeSwitcherConfig > {
108155 const exists = await pluginConfigExists ( )
@@ -134,7 +181,24 @@ export async function initializeConfig(): Promise<ModeSwitcherConfig> {
134181}
135182
136183/**
137- * Ensure configuration is valid and has required presets
184+ * Validates that a configuration object is well-formed and has required presets.
185+ *
186+ * This function performs the following checks:
187+ * - `currentMode` field is present and non-empty
188+ * - `presets` object exists and contains at least one preset
189+ * - A preset exists for the current mode
190+ *
191+ * @param config - The configuration object to validate
192+ * @returns True if configuration is valid, false otherwise
193+ * @example
194+ * ```typescript
195+ * const config = await loadPluginConfig();
196+ * if (validateConfig(config)) {
197+ * console.log('Configuration is valid');
198+ * } else {
199+ * console.error('Invalid configuration detected');
200+ * }
201+ * ```
138202 */
139203export function validateConfig ( config : ModeSwitcherConfig ) : boolean {
140204 if ( ! config . currentMode ) {
@@ -143,8 +207,5 @@ export function validateConfig(config: ModeSwitcherConfig): boolean {
143207 if ( ! config . presets || Object . keys ( config . presets ) . length === 0 ) {
144208 return false
145209 }
146- if ( ! config . presets [ config . currentMode ] ) {
147- return false
148- }
149- return true
210+ return Boolean ( config . presets [ config . currentMode ] )
150211}
0 commit comments