mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
refactor: consolidate environment config and API handling
- Update database paths and port configuration - Remove backend config.ts and sessions route - Add mobile file preview and files API - Clean up shared constants and provider hooks
This commit is contained in:
1 parent
0db354048d
commit
8a56bc208d
32 files changed
+301
-512
No files matched your search
@@ -4,10 +4,11 @@ import * as db from '../db/queries'
|
||||
import * as repoService from '../services/repo'
|
||||
import { SettingsService } from '../services/settings'
|
||||
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 } from '../../../shared/src/constants'
|
||||
import { getReposPath, getWorkspacePath } from '../config'
|
||||
|
||||
export function createRepoRoutes(database: Database) {
|
||||
const app = new Hono()
|
||||
@@ -138,12 +139,25 @@ export function createRepoRoutes(database: Database) {
|
||||
}
|
||||
|
||||
const workingDir = path.join(getReposPath(), repo.localPath)
|
||||
const configPath = `${workingDir}/opencode.json`
|
||||
const workspaceConfigPath = `${getWorkspacePath()}/opencode.json`
|
||||
const repoConfigPath = `${workingDir}/opencode.json`
|
||||
|
||||
// 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(configPath, 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}`)
|
||||
|
||||
// Restart OpenCode server to pick up new workspace config
|
||||
logger.info('Restarting OpenCode server due to workspace config change')
|
||||
await opencodeServerManager.stop()
|
||||
await opencodeServerManager.start()
|
||||
|
||||
const updatedRepo = db.getRepoById(database, id)
|
||||
return c.json(updatedRepo)
|
||||
|
||||
@@ -1,154 +0,0 @@
|
||||
import { Hono } from 'hono'
|
||||
import { logger } from '../utils/logger'
|
||||
import { ENV } from '../config'
|
||||
|
||||
const OPENCODE_SERVER_PORT = ENV.OPENCODE_SERVER_PORT
|
||||
|
||||
export function createSessionRoutes() {
|
||||
const app = new Hono()
|
||||
|
||||
app.post('/', async (c) => {
|
||||
const body = await c.req.json()
|
||||
|
||||
try {
|
||||
const response = await fetch(`http://localhost:${OPENCODE_SERVER_PORT}/session`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
logger.error(`Session creation failed: ${response.status} ${response.statusText}`)
|
||||
return c.json({ error: 'Session creation failed', status: response.status }, response.status as any)
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
return c.json(data)
|
||||
} catch (error) {
|
||||
logger.error('Failed to create session:', error)
|
||||
return c.json({ error: 'Failed to create session' }, 500)
|
||||
}
|
||||
})
|
||||
|
||||
app.post('/:sessionID/command', async (c) => {
|
||||
const sessionID = c.req.param('sessionID')
|
||||
const body = await c.req.json()
|
||||
|
||||
try {
|
||||
const response = await fetch(`http://localhost:${OPENCODE_SERVER_PORT}/session/${sessionID}/command`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
logger.error(`Command failed: ${response.status} ${response.statusText}`)
|
||||
return c.json({ error: 'Command failed', status: response.status }, response.status as any)
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
return c.json(data)
|
||||
} catch (error) {
|
||||
logger.error('Failed to send command:', error)
|
||||
return c.json({ error: 'Failed to send command' }, 500)
|
||||
}
|
||||
})
|
||||
|
||||
app.post('/:sessionID/shell', async (c) => {
|
||||
const sessionID = c.req.param('sessionID')
|
||||
const body = await c.req.json()
|
||||
|
||||
try {
|
||||
const response = await fetch(`http://localhost:${OPENCODE_SERVER_PORT}/session/${sessionID}/shell`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
logger.error(`Shell command failed: ${response.status} ${response.statusText}`)
|
||||
return c.json({ error: 'Shell command failed', status: response.status }, response.status as any)
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
return c.json(data)
|
||||
} catch (error) {
|
||||
logger.error('Failed to send shell command:', error)
|
||||
return c.json({ error: 'Failed to send shell command' }, 500)
|
||||
}
|
||||
})
|
||||
|
||||
app.post('/:sessionID/abort', async (c) => {
|
||||
const sessionID = c.req.param('sessionID')
|
||||
|
||||
try {
|
||||
const response = await fetch(`http://localhost:${OPENCODE_SERVER_PORT}/session/${sessionID}/abort`, {
|
||||
method: 'POST',
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
logger.error(`Abort failed: ${response.status} ${response.statusText}`)
|
||||
return c.json({ error: 'Abort failed', status: response.status }, response.status as any)
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
return c.json(data)
|
||||
} catch (error) {
|
||||
logger.error('Failed to abort session:', error)
|
||||
return c.json({ error: 'Failed to abort session' }, 500)
|
||||
}
|
||||
})
|
||||
|
||||
app.get('/:sessionID/message', async (c) => {
|
||||
const sessionID = c.req.param('sessionID')
|
||||
|
||||
try {
|
||||
const response = await fetch(`http://localhost:${OPENCODE_SERVER_PORT}/session/${sessionID}/message`)
|
||||
|
||||
if (!response.ok) {
|
||||
logger.error(`Get messages failed: ${response.status} ${response.statusText}`)
|
||||
return c.json({ error: 'Get messages failed', status: response.status }, response.status as any)
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
return c.json(data)
|
||||
} catch (error) {
|
||||
logger.error('Failed to get messages:', error)
|
||||
return c.json({ error: 'Failed to get messages' }, 500)
|
||||
}
|
||||
})
|
||||
|
||||
app.post('/:sessionID/message', async (c) => {
|
||||
const sessionID = c.req.param('sessionID')
|
||||
const body = await c.req.json()
|
||||
|
||||
try {
|
||||
const response = await fetch(`http://localhost:${OPENCODE_SERVER_PORT}/session/${sessionID}/message`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
logger.error(`Send message failed: ${response.status} ${response.statusText}`)
|
||||
return c.json({ error: 'Send message failed', status: response.status }, response.status as any)
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
return c.json(data)
|
||||
} catch (error) {
|
||||
logger.error('Failed to send message:', error)
|
||||
return c.json({ error: 'Failed to send message' }, 500)
|
||||
}
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { Hono } from 'hono'
|
||||
import { z } from 'zod'
|
||||
import type { Database } from 'bun:sqlite'
|
||||
import { SettingsService } from '../services/settings'
|
||||
import { opencodeServerManager } from '../services/opencode-single-server'
|
||||
import {
|
||||
UserPreferencesSchema,
|
||||
OpenCodeConfigSchema,
|
||||
@@ -98,6 +99,14 @@ export function createSettingsRoutes(db: Database) {
|
||||
const validated = CreateOpenCodeConfigSchema.parse(body)
|
||||
|
||||
const config = settingsService.createOpenCodeConfig(validated, userId)
|
||||
|
||||
// Restart OpenCode server if this is the new default config
|
||||
if (validated.isDefault) {
|
||||
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 create OpenCode config:', error)
|
||||
@@ -120,6 +129,13 @@ 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) {
|
||||
logger.info('Restarting OpenCode server with updated default config')
|
||||
await opencodeServerManager.stop()
|
||||
await opencodeServerManager.start()
|
||||
}
|
||||
|
||||
return c.json(config)
|
||||
} catch (error) {
|
||||
logger.error('Failed to update OpenCode config:', error)
|
||||
|
||||
Reference in new issue
Block a user