mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Improve OpenCode config error handling and UI responsiveness
This commit is contained in:
1 parent
0a592e7715
commit
0be6bd16b7
5 files changed
+65
-32
No files matched your search
@@ -153,7 +153,10 @@ export function createSettingsRoutes(db: Database) {
|
||||
await writeFileContent(configPath, config.rawContent)
|
||||
logger.info(`Wrote default config to: ${configPath}`)
|
||||
|
||||
await patchOpenCodeConfig(config.content)
|
||||
const patchResult = await patchOpenCodeConfig(config.content)
|
||||
if (!patchResult.success) {
|
||||
return c.json({ error: 'Config saved but failed to apply', details: patchResult.error }, 500)
|
||||
}
|
||||
}
|
||||
|
||||
return c.json(config)
|
||||
@@ -186,7 +189,10 @@ export function createSettingsRoutes(db: Database) {
|
||||
await writeFileContent(configPath, config.rawContent)
|
||||
logger.info(`Wrote default config to: ${configPath}`)
|
||||
|
||||
await patchOpenCodeConfig(config.content)
|
||||
const patchResult = await patchOpenCodeConfig(config.content)
|
||||
if (!patchResult.success) {
|
||||
return c.json({ error: 'Config saved but failed to apply', details: patchResult.error }, 500)
|
||||
}
|
||||
}
|
||||
|
||||
return c.json(config)
|
||||
@@ -232,7 +238,10 @@ export function createSettingsRoutes(db: Database) {
|
||||
await writeFileContent(configPath, config.rawContent)
|
||||
logger.info(`Wrote default config '${configName}' to: ${configPath}`)
|
||||
|
||||
await patchOpenCodeConfig(config.content)
|
||||
const patchResult = await patchOpenCodeConfig(config.content)
|
||||
if (!patchResult.success) {
|
||||
return c.json({ error: 'Config saved but failed to apply', details: patchResult.error }, 500)
|
||||
}
|
||||
|
||||
return c.json(config)
|
||||
} catch (error) {
|
||||
@@ -276,7 +285,7 @@ export function createSettingsRoutes(db: Database) {
|
||||
app.post('/opencode-reload', async (c) => {
|
||||
try {
|
||||
logger.info('OpenCode configuration reload requested')
|
||||
await fetch(`http://${ENV.OPENCODE.HOST}:${ENV.OPENCODE.PORT}/config/`, {
|
||||
await fetch(`http://${ENV.OPENCODE.HOST}:${ENV.OPENCODE.PORT}/config`, {
|
||||
method: 'GET'
|
||||
})
|
||||
await opencodeServerManager.reloadConfig()
|
||||
|
||||
@@ -210,7 +210,7 @@ class OpenCodeServerManager {
|
||||
async reloadConfig(): Promise<void> {
|
||||
logger.info('Reloading OpenCode configuration (via API)')
|
||||
try {
|
||||
const response = await fetch(`http://${OPENCODE_SERVER_HOST}:${OPENCODE_SERVER_PORT}/config/`, {
|
||||
const response = await fetch(`http://${OPENCODE_SERVER_HOST}:${OPENCODE_SERVER_PORT}/config`, {
|
||||
method: 'GET'
|
||||
})
|
||||
|
||||
@@ -220,7 +220,7 @@ class OpenCodeServerManager {
|
||||
|
||||
const currentConfig = await response.json()
|
||||
logger.info('Triggering OpenCode config reload via PATCH')
|
||||
const patchResponse = await fetch(`http://${OPENCODE_SERVER_HOST}:${OPENCODE_SERVER_PORT}/config/`, {
|
||||
const patchResponse = await fetch(`http://${OPENCODE_SERVER_HOST}:${OPENCODE_SERVER_PORT}/config`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(currentConfig)
|
||||
|
||||
@@ -43,7 +43,12 @@ export async function deleteOpenCodeAuth(providerId: string): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
export async function patchOpenCodeConfig(config: Record<string, unknown>): Promise<boolean> {
|
||||
export type PatchConfigResult = {
|
||||
success: boolean
|
||||
error?: string
|
||||
}
|
||||
|
||||
export async function patchOpenCodeConfig(config: Record<string, unknown>): Promise<PatchConfigResult> {
|
||||
try {
|
||||
const response = await fetch(`${OPENCODE_SERVER_URL}/config`, {
|
||||
method: 'PATCH',
|
||||
@@ -53,14 +58,37 @@ export async function patchOpenCodeConfig(config: Record<string, unknown>): Prom
|
||||
|
||||
if (response.ok) {
|
||||
logger.info('Patched OpenCode config via API')
|
||||
return true
|
||||
return { success: true }
|
||||
}
|
||||
|
||||
logger.error(`Failed to patch OpenCode config: ${response.status} ${response.statusText}`)
|
||||
return false
|
||||
let errorMessage = `${response.status} ${response.statusText}`
|
||||
try {
|
||||
const errorBody = await response.json() as Record<string, unknown>
|
||||
if (errorBody?.name === 'ConfigInvalidError' && errorBody?.data) {
|
||||
const data = errorBody.data as { issues?: Array<{ message: string; path?: string[] }> }
|
||||
if (data.issues) {
|
||||
const issues = data.issues
|
||||
.map((issue) =>
|
||||
issue.path ? `${issue.path.join('.')}: ${issue.message}` : issue.message
|
||||
)
|
||||
.join('; ')
|
||||
errorMessage = `Invalid config: ${issues}`
|
||||
}
|
||||
} else if (typeof errorBody?.error === 'string') {
|
||||
errorMessage = errorBody.error
|
||||
} else if (typeof errorBody?.message === 'string') {
|
||||
errorMessage = errorBody.message
|
||||
}
|
||||
} catch {
|
||||
// Use default error message if we can't parse response body
|
||||
}
|
||||
|
||||
logger.error(`Failed to patch OpenCode config: ${errorMessage}`)
|
||||
return { success: false, error: errorMessage }
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
|
||||
logger.error('Failed to patch OpenCode config:', error)
|
||||
return false
|
||||
return { success: false, error: errorMessage }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ export function OpenCodeConfigEditor({
|
||||
setEditErrorLine(line)
|
||||
setEditError('Invalid JSON/JSONC format')
|
||||
} else {
|
||||
setEditError(error instanceof Error ? error.message : 'Failed to update config')
|
||||
setEditError('Failed to save. Please check your changes and try again.')
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -70,14 +70,14 @@ export function OpenCodeConfigEditor({
|
||||
|
||||
return (
|
||||
<Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
|
||||
<DialogContent mobileFullscreen className="h-[90vh] max-w-6xl gap-0 flex flex-col p-0 sm:p-6">
|
||||
<DialogContent mobileFullscreen className="gap-0 flex flex-col p-0 md:p-6 w-full min-w-0 sm:max-w-4xl">
|
||||
<DialogHeader className="p-4 sm:p-6 border-b flex flex-row items-center justify-between space-y-0">
|
||||
<DialogTitle className="text-lg sm:text-xl font-semibold">
|
||||
{`Edit Config: ${config.name}`}
|
||||
</DialogTitle>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="flex-1 p-0 sm:p-4 overflow-hidden relative">
|
||||
<div className="flex-1 p-0 sm:p-4 overflow-hidden relative w-full">
|
||||
<Textarea
|
||||
id="edit-config-content"
|
||||
ref={editTextareaRef}
|
||||
|
||||
@@ -115,12 +115,16 @@ export function OpenCodeConfigManager() {
|
||||
},
|
||||
})
|
||||
|
||||
const getApiErrorMessage = (error: unknown, fallback: string): string => {
|
||||
if (error && typeof error === 'object' && 'response' in error) {
|
||||
const response = (error as { response?: { data?: { details?: string; error?: string } } }).response
|
||||
return response?.data?.details || response?.data?.error || fallback
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
const getRestartErrorMessage = (error: unknown): string => {
|
||||
return error && typeof error === 'object' && 'response' in error
|
||||
? ((error as { response?: { data?: { details?: string; error?: string } } }).response?.data?.details
|
||||
|| (error as { response?: { data?: { details?: string; error?: string } } }).response?.data?.error
|
||||
|| 'Failed to restart OpenCode server')
|
||||
: 'Failed to restart OpenCode server'
|
||||
return getApiErrorMessage(error, 'Failed to restart OpenCode server')
|
||||
}
|
||||
|
||||
const rollbackMutation = useMutation({
|
||||
@@ -184,7 +188,7 @@ export function OpenCodeConfigManager() {
|
||||
queryClient.invalidateQueries({ queryKey: ['opencode', 'agents'] })
|
||||
} catch (error) {
|
||||
console.error('Failed to update config:', error)
|
||||
showToast.error('Failed to update config', { id: 'update-restart' })
|
||||
showToast.error(getApiErrorMessage(error, 'Failed to update config'), { id: 'update-restart' })
|
||||
} finally {
|
||||
setIsUpdating(false)
|
||||
}
|
||||
@@ -238,7 +242,7 @@ export function OpenCodeConfigManager() {
|
||||
queryClient.invalidateQueries({ queryKey: ['opencode', 'agents'] })
|
||||
} catch (error) {
|
||||
console.error('Failed to create config:', error)
|
||||
showToast.error('Failed to create configuration', { id: 'create-config' })
|
||||
showToast.error(getApiErrorMessage(error, 'Failed to create configuration'), { id: 'create-config' })
|
||||
throw error
|
||||
} finally {
|
||||
setIsUpdating(false)
|
||||
@@ -272,12 +276,7 @@ export function OpenCodeConfigManager() {
|
||||
showToast.success('Default config updated and server reloaded', { id: 'set-default' })
|
||||
} catch (error) {
|
||||
console.error('Failed to set default config:', error)
|
||||
const errorMessage = error && typeof error === 'object' && 'response' in error
|
||||
? ((error as { response?: { data?: { details?: string; error?: string } } }).response?.data?.details
|
||||
|| (error as { response?: { data?: { details?: string; error?: string } } }).response?.data?.error
|
||||
|| 'Failed to set default config')
|
||||
: 'Failed to set default config'
|
||||
showToast.error(errorMessage, { id: 'set-default' })
|
||||
showToast.error(getApiErrorMessage(error, 'Failed to set default config'), { id: 'set-default' })
|
||||
} finally {
|
||||
setIsUpdating(false)
|
||||
}
|
||||
@@ -504,11 +503,8 @@ export function OpenCodeConfigManager() {
|
||||
showToast.success(successMsg, { id: 'edit-config' })
|
||||
queryClient.invalidateQueries({ queryKey: ['opencode', 'agents'] })
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message.includes('restart')) {
|
||||
showToast.error(getRestartErrorMessage(error), { id: 'edit-config' })
|
||||
} else {
|
||||
showToast.error('Failed to save configuration', { id: 'edit-config' })
|
||||
}
|
||||
showToast.error(getApiErrorMessage(error, 'Failed to save configuration'), { id: 'edit-config' })
|
||||
throw error
|
||||
}
|
||||
}}
|
||||
isUpdating={isUpdating}
|
||||
|
||||
Reference in new issue
Block a user