From 595c1e2da6c728fea1cba170c09e4a6ea5b9584d Mon Sep 17 00:00:00 2001 From: Chris Scott Date: Tue, 25 Nov 2025 10:30:40 -0500 Subject: [PATCH] fix: auto-create default OpenCode config on startup and sync to disk on update --- backend/src/index.ts | 71 ++++++++++++++++++++++++++++++++ backend/src/routes/settings.ts | 2 +- backend/src/services/settings.ts | 32 ++++++++++++-- 3 files changed, 101 insertions(+), 4 deletions(-) diff --git a/backend/src/index.ts b/backend/src/index.ts index 6766a1c7..180c3132 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -37,6 +37,76 @@ app.use('/*', cors({ const db = initializeDatabase(DB_PATH) +const DEFAULT_OPENCODE_CONFIG = { + $schema: 'https://opencode.ai/config.json', + theme: 'opencode', + autoupdate: true, + share: 'disabled', + keybinds: { + leader: 'ctrl+x', + app_exit: 'ctrl+c,ctrl+d,q', + editor_open: 'e', + theme_list: 't', + sidebar_toggle: 'b', + status_view: 's', + session_export: 'x', + session_new: 'n', + session_list: 'l', + session_timeline: 'g', + session_share: 'none', + session_unshare: 'none', + session_interrupt: 'escape', + session_compact: 'c', + messages_page_up: 'pageup', + messages_page_down: 'pagedown', + messages_half_page_up: 'ctrl+alt+u', + messages_half_page_down: 'ctrl+alt+d', + messages_first: 'ctrl+g,home', + messages_last: 'ctrl+alt+g,end', + messages_copy: 'y', + messages_undo: 'u', + messages_redo: 'r', + messages_toggle_conceal: 'h', + model_list: 'm', + model_cycle_recent: 'f2', + model_cycle_recent_reverse: 'shift+f2', + command_list: 'ctrl+p', + agent_list: 'a', + agent_cycle: 'tab', + agent_cycle_reverse: 'shift+tab', + input_clear: 'ctrl+c', + input_forward_delete: 'ctrl+d', + input_paste: 'ctrl+v', + input_submit: 'return', + input_newline: 'shift+return,ctrl+j', + history_previous: 'up', + history_next: 'down', + session_child_cycle: 'right', + session_child_cycle_reverse: 'left', + terminal_suspend: 'ctrl+z', + }, + permission: { + bash: { + '*': 'allow', + }, + }, +} + +async function ensureDefaultConfigExists(): Promise { + const settingsService = new SettingsService(db) + const configs = settingsService.getOpenCodeConfigs() + + if (configs.configs.length === 0) { + logger.info('No OpenCode configs found, creating default config') + settingsService.createOpenCodeConfig({ + name: 'default', + content: DEFAULT_OPENCODE_CONFIG, + isDefault: true, + }) + logger.info('Created default OpenCode config') + } +} + async function syncDefaultConfigToDisk(): Promise { const settingsService = new SettingsService(db) const defaultConfig = settingsService.getDefaultOpenCodeConfig() @@ -60,6 +130,7 @@ try { await cleanupOrphanedDirectories(db) logger.info('Orphaned directory cleanup completed') + await ensureDefaultConfigExists() await syncDefaultConfigToDisk() await opencodeServerManager.start() diff --git a/backend/src/routes/settings.ts b/backend/src/routes/settings.ts index a672c2d6..cb2735dd 100644 --- a/backend/src/routes/settings.ts +++ b/backend/src/routes/settings.ts @@ -133,7 +133,7 @@ export function createSettingsRoutes(db: Database) { return c.json({ error: 'Config not found' }, 404) } - if (validated.isDefault) { + if (config.isDefault) { const configPath = getOpenCodeConfigFilePath() const configContent = JSON.stringify(config.content, null, 2) await writeFileContent(configPath, configContent) diff --git a/backend/src/services/settings.ts b/backend/src/services/settings.ts index 47e98c84..0bffbb62 100644 --- a/backend/src/services/settings.ts +++ b/backend/src/services/settings.ts @@ -145,7 +145,13 @@ export class SettingsService { const contentValidated = OpenCodeConfigSchema.parse(request.content) const now = Date.now() - if (request.isDefault) { + const existingCount = this.db + .query('SELECT COUNT(*) as count FROM opencode_configs WHERE user_id = ?') + .get(userId) as { count: number } + + const shouldBeDefault = request.isDefault || existingCount.count === 0 + + if (shouldBeDefault) { this.db .query('UPDATE opencode_configs SET is_default = FALSE WHERE user_id = ?') .run(userId) @@ -160,7 +166,7 @@ export class SettingsService { userId, request.name, JSON.stringify(contentValidated), - request.isDefault || false, + shouldBeDefault, now, now ) @@ -169,7 +175,7 @@ export class SettingsService { id: result.lastInsertRowid as number, name: request.name, content: contentValidated, - isDefault: request.isDefault || false, + isDefault: shouldBeDefault, createdAt: now, updatedAt: now, } @@ -240,6 +246,7 @@ export class SettingsService { const deleted = result.changes > 0 if (deleted) { logger.info(`Deleted OpenCode config '${configName}' for user: ${userId}`) + this.ensureSingleConfigIsDefault(userId) } return deleted @@ -374,4 +381,23 @@ export class SettingsService { return null } } + + ensureSingleConfigIsDefault(userId: string = 'default'): void { + const hasDefault = this.db + .query('SELECT COUNT(*) as count FROM opencode_configs WHERE user_id = ? AND is_default = TRUE') + .get(userId) as { count: number } + + if (hasDefault.count === 0) { + const firstConfig = this.db + .query('SELECT config_name FROM opencode_configs WHERE user_id = ? ORDER BY created_at ASC LIMIT 1') + .get(userId) as { config_name: string } | undefined + + if (firstConfig) { + this.db + .query('UPDATE opencode_configs SET is_default = TRUE WHERE user_id = ? AND config_name = ?') + .run(userId, firstConfig.config_name) + logger.info(`Auto-set '${firstConfig.config_name}' as default (only config)`) + } + } + } }