fix: consolidate OpenCode config file path and sync on startup

This commit is contained in:
Chris Scott committed 2025-11-25 10:09:28 -05:00
1 parent d9ee1a67b7
commit 4d7572a4fc
4 files changed
+53 -25

No files matched your search

+19 -1
View File
@@ -9,7 +9,8 @@ import { createHealthRoutes } from './routes/health'
import { createFileRoutes } from './routes/files'
import { createProvidersRoutes } from './routes/providers'
import { ensureDirectoryExists } from './services/file-operations'
import { ensureDirectoryExists, writeFileContent } from './services/file-operations'
import { SettingsService } from './services/settings'
import { opencodeServerManager } from './services/opencode-single-server'
import { cleanupOrphanedDirectories } from './services/repo'
import { proxyRequest } from './services/proxy'
@@ -18,6 +19,7 @@ import {
getWorkspacePath,
getReposPath,
getConfigPath,
getOpenCodeConfigFilePath,
getDatabasePath,
ENV
} from './config'
@@ -35,6 +37,20 @@ app.use('/*', cors({
const db = initializeDatabase(DB_PATH)
async function syncDefaultConfigToDisk(): Promise<void> {
const settingsService = new SettingsService(db)
const defaultConfig = settingsService.getDefaultOpenCodeConfig()
if (defaultConfig) {
const configPath = getOpenCodeConfigFilePath()
const configContent = JSON.stringify(defaultConfig.content, null, 2)
await writeFileContent(configPath, configContent)
logger.info(`Synced default config '${defaultConfig.name}' to: ${configPath}`)
} else {
logger.info('No default OpenCode config found in database')
}
}
try {
await ensureDirectoryExists(getWorkspacePath())
await ensureDirectoryExists(getReposPath())
@@ -44,6 +60,8 @@ try {
await cleanupOrphanedDirectories(db)
logger.info('Orphaned directory cleanup completed')
await syncDefaultConfigToDisk()
await opencodeServerManager.start()
logger.info(`OpenCode server running on port ${opencodeServerManager.getPort()}`)
} catch (error) {
+7 -15
View File
@@ -7,8 +7,7 @@ import { writeFileContent } from '../services/file-operations'
import { opencodeServerManager } from '../services/opencode-single-server'
import { logger } from '../utils/logger'
import { withTransactionAsync } from '../db/transactions'
import path from 'path'
import { getReposPath, getWorkspacePath } from '../config'
import { getOpenCodeConfigFilePath } from '../config'
export function createRepoRoutes(database: Database) {
const app = new Hono()
@@ -34,10 +33,10 @@ export function createRepoRoutes(database: Database) {
const configContent = settingsService.getOpenCodeConfigContent(openCodeConfigName)
if (configContent) {
const workingDir = path.join(getReposPath(), repo.localPath)
const configPath = `${workingDir}/opencode.json`
await writeFileContent(configPath, configContent)
const openCodeConfigPath = getOpenCodeConfigFilePath()
await writeFileContent(openCodeConfigPath, configContent)
db.updateRepoConfigName(database, repo.id, openCodeConfigName)
logger.info(`Applied config '${openCodeConfigName}' to: ${openCodeConfigPath}`)
}
}
@@ -138,21 +137,14 @@ export function createRepoRoutes(database: Database) {
return c.json({ error: `Config '${configName}' not found` }, 404)
}
const workingDir = path.join(getReposPath(), repo.localPath)
const workspaceConfigPath = `${getWorkspacePath()}/opencode.json`
const repoConfigPath = `${workingDir}/opencode.json`
const openCodeConfigPath = getOpenCodeConfigFilePath()
// Write to workspace as main config
await writeFileContent(workspaceConfigPath, configContent)
// Also write to repo directory for repo-specific usage
await writeFileContent(repoConfigPath, configContent)
await writeFileContent(openCodeConfigPath, configContent)
db.updateRepoConfigName(database, id, configName)
logger.info(`Switched config for repo ${id} to '${configName}'`)
logger.info(`Updated workspace config: ${workspaceConfigPath}`)
logger.info(`Updated repo config: ${repoConfigPath}`)
logger.info(`Updated OpenCode config: ${openCodeConfigPath}`)
// Restart OpenCode server to pick up new workspace config
logger.info('Restarting OpenCode server due to workspace config change')
+24 -7
View File
@@ -3,11 +3,11 @@ import { z } from 'zod'
import type { Database } from 'bun:sqlite'
import { SettingsService } from '../services/settings'
import { opencodeServerManager } from '../services/opencode-single-server'
import { writeFileContent } from '../services/file-operations'
import { getOpenCodeConfigFilePath } from '../config'
import {
UserPreferencesSchema,
OpenCodeConfigSchema,
OpenCodeConfigMetadataSchema,
CustomCommandSchema
} from '../types/settings'
import { logger } from '../utils/logger'
@@ -100,8 +100,12 @@ export function createSettingsRoutes(db: Database) {
const config = settingsService.createOpenCodeConfig(validated, userId)
// Restart OpenCode server if this is the new default config
if (validated.isDefault) {
const configPath = getOpenCodeConfigFilePath()
const configContent = JSON.stringify(config.content, null, 2)
await writeFileContent(configPath, configContent)
logger.info(`Wrote default config to: ${configPath}`)
logger.info('Restarting OpenCode server with new default config')
await opencodeServerManager.stop()
await opencodeServerManager.start()
@@ -129,8 +133,12 @@ export function createSettingsRoutes(db: Database) {
return c.json({ error: 'Config not found' }, 404)
}
// Restart OpenCode server if config was set as default
if (validated.isDefault) {
const configPath = getOpenCodeConfigFilePath()
const configContent = JSON.stringify(config.content, null, 2)
await writeFileContent(configPath, configContent)
logger.info(`Wrote default config to: ${configPath}`)
logger.info('Restarting OpenCode server with updated default config')
await opencodeServerManager.stop()
await opencodeServerManager.start()
@@ -173,6 +181,15 @@ export function createSettingsRoutes(db: Database) {
return c.json({ error: 'Config not found' }, 404)
}
const configPath = getOpenCodeConfigFilePath()
const configContent = JSON.stringify(config.content, null, 2)
await writeFileContent(configPath, configContent)
logger.info(`Wrote default config '${configName}' to: ${configPath}`)
logger.info('Restarting OpenCode server with new default config')
await opencodeServerManager.stop()
await opencodeServerManager.start()
return c.json(config)
} catch (error) {
logger.error('Failed to set default OpenCode config:', error)
@@ -220,7 +237,7 @@ export function createSettingsRoutes(db: Database) {
return c.json({ error: 'Command with this name already exists' }, 409)
}
const updatedSettings = settingsService.updateSettings({
settingsService.updateSettings({
customCommands: [...settings.preferences.customCommands, validated]
}, userId)
@@ -254,7 +271,7 @@ export function createSettingsRoutes(db: Database) {
promptTemplate: validated.promptTemplate
}
const updatedSettings = settingsService.updateSettings({
settingsService.updateSettings({
customCommands: updatedCommands
}, userId)
@@ -280,7 +297,7 @@ export function createSettingsRoutes(db: Database) {
}
const updatedCommands = settings.preferences.customCommands.filter(cmd => cmd.name !== commandName)
const updatedSettings = settingsService.updateSettings({
settingsService.updateSettings({
customCommands: updatedCommands
}, userId)
@@ -1,10 +1,11 @@
import { spawn, execSync } from 'child_process'
import path from 'path'
import { logger } from '../utils/logger'
import { getWorkspacePath, ENV } from '../config'
import { getWorkspacePath, getOpenCodeConfigFilePath, ENV } from '../config'
const OPENCODE_SERVER_PORT = ENV.OPENCODE.PORT
const OPENCODE_SERVER_DIRECTORY = getWorkspacePath()
const OPENCODE_CONFIG_PATH = getOpenCodeConfigFilePath()
class OpenCodeServerManager {
private static instance: OpenCodeServerManager
@@ -78,7 +79,7 @@ class OpenCodeServerManager {
env: {
...process.env,
XDG_DATA_HOME: path.join(OPENCODE_SERVER_DIRECTORY, '.opencode/state'),
OPENCODE_CONFIG: path.join(OPENCODE_SERVER_DIRECTORY, '.config/opencode/opencode.json')
OPENCODE_CONFIG: OPENCODE_CONFIG_PATH
}
}
)