mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
fix: auto-create default OpenCode config on startup and sync to disk on update
This commit is contained in:
1 parent
4d7572a4fc
commit
595c1e2da6
3 files changed
+101
-4
No files matched your search
@@ -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,<leader>q',
|
||||
editor_open: '<leader>e',
|
||||
theme_list: '<leader>t',
|
||||
sidebar_toggle: '<leader>b',
|
||||
status_view: '<leader>s',
|
||||
session_export: '<leader>x',
|
||||
session_new: '<leader>n',
|
||||
session_list: '<leader>l',
|
||||
session_timeline: '<leader>g',
|
||||
session_share: 'none',
|
||||
session_unshare: 'none',
|
||||
session_interrupt: 'escape',
|
||||
session_compact: '<leader>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: '<leader>y',
|
||||
messages_undo: '<leader>u',
|
||||
messages_redo: '<leader>r',
|
||||
messages_toggle_conceal: '<leader>h',
|
||||
model_list: '<leader>m',
|
||||
model_cycle_recent: 'f2',
|
||||
model_cycle_recent_reverse: 'shift+f2',
|
||||
command_list: 'ctrl+p',
|
||||
agent_list: '<leader>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: '<leader>right',
|
||||
session_child_cycle_reverse: '<leader>left',
|
||||
terminal_suspend: 'ctrl+z',
|
||||
},
|
||||
permission: {
|
||||
bash: {
|
||||
'*': 'allow',
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
async function ensureDefaultConfigExists(): Promise<void> {
|
||||
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<void> {
|
||||
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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)`)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user