mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
Add config rollback and startup error recovery
This commit is contained in:
1 parent
120d087910
commit
876b444599
10 files changed
+428
-69
No files matched your search
@@ -9,9 +9,9 @@ import { useSettingsDialog } from './hooks/useSettingsDialog'
|
||||
import { useTheme } from './hooks/useTheme'
|
||||
import { TTSProvider } from './contexts/TTSContext'
|
||||
import { PermissionProvider } from '@/contexts/PermissionContext'
|
||||
import { PermissionRequestDialog } from '@/components/session/PermissionRequestDialog'
|
||||
import { usePermissionContext } from '@/contexts/PermissionContext'
|
||||
import { GlobalPermissionNotification } from '@/components/permissions/GlobalPermissionNotification'
|
||||
import { PermissionRequestDialog } from './components/session/PermissionRequestDialog'
|
||||
import { usePermissionContext } from './contexts/PermissionContext'
|
||||
import { GlobalPermissionNotification } from './components/permissions/GlobalPermissionNotification'
|
||||
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
|
||||
@@ -98,11 +98,16 @@ export const settingsApi = {
|
||||
}
|
||||
},
|
||||
|
||||
restartOpenCodeServer: async (): Promise<{ success: boolean; message: string }> => {
|
||||
restartOpenCodeServer: async (): Promise<{ success: boolean; message: string; details?: string }> => {
|
||||
const { data } = await axios.post(`${API_BASE_URL}/api/settings/opencode-restart`)
|
||||
return data
|
||||
},
|
||||
|
||||
rollbackOpenCodeConfig: async (): Promise<{ success: boolean; message: string; configName?: string }> => {
|
||||
const { data } = await axios.post(`${API_BASE_URL}/api/settings/opencode-rollback`)
|
||||
return data
|
||||
},
|
||||
|
||||
getAgentsMd: async (): Promise<{ content: string }> => {
|
||||
const { data } = await axios.get(`${API_BASE_URL}/api/settings/agents-md`)
|
||||
return data
|
||||
|
||||
@@ -14,6 +14,7 @@ import { AgentsMdEditor } from './AgentsMdEditor'
|
||||
import { McpManager } from './McpManager'
|
||||
import { settingsApi } from '@/api/settings'
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { useServerHealth } from '@/hooks/useServerHealth'
|
||||
import { parseJsonc, hasJsoncComments } from '@/lib/jsonc'
|
||||
import { showToast } from '@/lib/toast'
|
||||
import type { OpenCodeConfig } from '@/api/types/settings'
|
||||
@@ -47,6 +48,7 @@ interface Agent {
|
||||
|
||||
export function OpenCodeConfigManager() {
|
||||
const queryClient = useQueryClient()
|
||||
const { data: health } = useServerHealth()
|
||||
const [configs, setConfigs] = useState<OpenCodeConfig[]>([])
|
||||
const [isLoading, setIsLoading] = useState(true)
|
||||
const [isUpdating, setIsUpdating] = useState(false)
|
||||
@@ -83,9 +85,30 @@ export function OpenCodeConfigManager() {
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['opencode', 'agents'] })
|
||||
showToast.success('Server restarted successfully', { id: 'update-restart' })
|
||||
},
|
||||
onError: (error: unknown) => {
|
||||
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 restart OpenCode server')
|
||||
: 'Failed to restart OpenCode server'
|
||||
showToast.error(errorMessage, { id: 'update-restart' })
|
||||
},
|
||||
})
|
||||
|
||||
const rollbackMutation = useMutation({
|
||||
mutationFn: async () => {
|
||||
return await settingsApi.rollbackOpenCodeConfig()
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['opencode', 'agents'] })
|
||||
queryClient.invalidateQueries({ queryKey: ['settings'] })
|
||||
showToast.success(data.message, { id: 'rollback-config' })
|
||||
fetchConfigs()
|
||||
},
|
||||
onError: () => {
|
||||
showToast.error('Failed to restart OpenCode server')
|
||||
showToast.error('Failed to rollback to previous config', { id: 'rollback-config' })
|
||||
},
|
||||
})
|
||||
|
||||
@@ -105,32 +128,27 @@ export function OpenCodeConfigManager() {
|
||||
try {
|
||||
setIsUpdating(true)
|
||||
const previousContent = configs.find(c => c.name === configName)?.content
|
||||
|
||||
|
||||
await settingsApi.updateOpenCodeConfig(configName, { content: newContent })
|
||||
|
||||
// Update the local state
|
||||
setConfigs(prev => prev.map(config =>
|
||||
config.name === configName
|
||||
|
||||
setConfigs(prev => prev.map(config =>
|
||||
config.name === configName
|
||||
? { ...config, content: newContent, updatedAt: Date.now() }
|
||||
: config
|
||||
))
|
||||
|
||||
// Update selected config if it's the one being edited
|
||||
|
||||
if (selectedConfig && selectedConfig.name === configName) {
|
||||
setSelectedConfig({ ...selectedConfig, content: newContent, updatedAt: Date.now() })
|
||||
}
|
||||
|
||||
// Auto-restart server if agents were modified
|
||||
|
||||
const agentsChanged = JSON.stringify(previousContent?.agent) !== JSON.stringify(newContent.agent)
|
||||
if (restartServer || agentsChanged) {
|
||||
showToast.loading('Restarting server...', { id: 'update-restart' })
|
||||
await restartServerMutation.mutateAsync()
|
||||
showToast.success('Config updated and server restarted', { id: 'update-restart' })
|
||||
} else {
|
||||
showToast.success('Configuration updated')
|
||||
}
|
||||
|
||||
// Invalidate agents cache so @ mentions get updated
|
||||
|
||||
queryClient.invalidateQueries({ queryKey: ['opencode', 'agents'] })
|
||||
} catch (error) {
|
||||
console.error('Failed to update config:', error)
|
||||
@@ -156,30 +174,29 @@ export function OpenCodeConfigManager() {
|
||||
try {
|
||||
setIsUpdating(true)
|
||||
const parsedContent = parseJsonc<Record<string, unknown>>(rawContent)
|
||||
|
||||
|
||||
const forbiddenFields = ['id', 'createdAt', 'updatedAt']
|
||||
const foundForbidden = forbiddenFields.filter(field => field in parsedContent)
|
||||
if (foundForbidden.length > 0) {
|
||||
throw new Error(`Invalid fields found: ${foundForbidden.join(', ')}. These fields are managed automatically.`)
|
||||
}
|
||||
|
||||
|
||||
await settingsApi.createOpenCodeConfig({
|
||||
name: name.trim(),
|
||||
content: rawContent,
|
||||
isDefault,
|
||||
})
|
||||
|
||||
|
||||
setIsCreateDialogOpen(false)
|
||||
await fetchConfigs()
|
||||
|
||||
|
||||
if (isDefault) {
|
||||
showToast.loading('Restarting server...', { id: 'create-config' })
|
||||
await restartServerMutation.mutateAsync()
|
||||
showToast.success('Configuration created and server restarted', { id: 'create-config' })
|
||||
} else {
|
||||
showToast.success('Configuration created', { id: 'create-config' })
|
||||
showToast.success('Configuration created')
|
||||
}
|
||||
|
||||
|
||||
queryClient.invalidateQueries({ queryKey: ['opencode', 'agents'] })
|
||||
} catch (error) {
|
||||
console.error('Failed to create config:', error)
|
||||
@@ -217,7 +234,12 @@ export function OpenCodeConfigManager() {
|
||||
showToast.success('Default config updated and server restarted', { id: 'set-default' })
|
||||
} catch (error) {
|
||||
console.error('Failed to set default config:', error)
|
||||
showToast.error('Failed to set default config', { id: 'set-default' })
|
||||
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' })
|
||||
} finally {
|
||||
setIsUpdating(false)
|
||||
}
|
||||
@@ -252,37 +274,81 @@ export function OpenCodeConfigManager() {
|
||||
)
|
||||
}
|
||||
|
||||
const isUnhealthy = health?.opencode !== 'healthy'
|
||||
|
||||
return (
|
||||
<div className="space-y-6 overflow-y-auto">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex gap-2 ">
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
showToast.loading('Restarting OpenCode server...', { id: 'manual-restart' })
|
||||
restartServerMutation.mutate()
|
||||
}}
|
||||
disabled={restartServerMutation.isPending}
|
||||
>
|
||||
{restartServerMutation.isPending ? (
|
||||
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
|
||||
) : (
|
||||
<RotateCcw className="h-4 w-4 mr-2" />
|
||||
)}
|
||||
Restart Server
|
||||
</Button>
|
||||
<Button type="button" onClick={() => setIsCreateDialogOpen(true)}>
|
||||
<Plus className="h-4 w-4 mr-2" />
|
||||
New Config
|
||||
</Button>
|
||||
<CreateConfigDialog
|
||||
isOpen={isCreateDialogOpen}
|
||||
onOpenChange={setIsCreateDialogOpen}
|
||||
onCreate={createConfig}
|
||||
isUpdating={isUpdating}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
{health && (
|
||||
<Card className={isUnhealthy ? 'border-destructive' : ''}>
|
||||
<CardContent className="p-3">
|
||||
<div className="flex flex-col sm:flex-row sm:items-center items-center gap-3">
|
||||
<div className="flex items-center gap-2 flex-wrap justify-center sm:justify-start">
|
||||
<div className={`h-3 w-3 rounded-full ${isUnhealthy ? 'bg-destructive animate-pulse' : 'bg-green-500'}`} />
|
||||
<p className="font-medium text-sm sm:text-base">
|
||||
Server Status: {isUnhealthy ? 'Unhealthy' : 'Healthy'}
|
||||
</p>
|
||||
{health.error && (
|
||||
<p className="text-xs text-destructive">
|
||||
{health.error}
|
||||
</p>
|
||||
)}
|
||||
{health.opencodeVersion && (
|
||||
<p className="text-xs text-muted-foreground">
|
||||
v{health.opencodeVersion}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex flex-wrap gap-2 justify-center sm:justify-end">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
showToast.loading('Restarting OpenCode server...', { id: 'manual-restart' })
|
||||
restartServerMutation.mutate()
|
||||
}}
|
||||
disabled={restartServerMutation.isPending}
|
||||
>
|
||||
{restartServerMutation.isPending ? (
|
||||
<Loader2 className="h-3 w-3 sm:h-4 sm:w-4 mr-1 animate-spin" />
|
||||
) : (
|
||||
<RotateCcw className="h-3 w-3 sm:h-4 sm:w-4 mr-1" />
|
||||
)}
|
||||
<span className="text-xs sm:text-sm">Restart</span>
|
||||
</Button>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
showToast.loading('Rolling back to previous config...', { id: 'rollback-config' })
|
||||
rollbackMutation.mutate()
|
||||
}}
|
||||
disabled={rollbackMutation.isPending}
|
||||
>
|
||||
{rollbackMutation.isPending ? (
|
||||
<Loader2 className="h-3 w-3 sm:h-4 sm:w-4 mr-1 animate-spin" />
|
||||
) : (
|
||||
<RotateCcw className="h-3 w-3 sm:h-4 sm:w-4 mr-1" />
|
||||
)}
|
||||
<span className="text-xs sm:text-sm">Rollback</span>
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => setIsCreateDialogOpen(true)}
|
||||
>
|
||||
<Plus className="h-3 w-3 sm:h-4 sm:w-4 mr-1" />
|
||||
<span className="text-xs sm:text-sm">New Config</span>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
<CreateConfigDialog
|
||||
isOpen={isCreateDialogOpen}
|
||||
onOpenChange={setIsCreateDialogOpen}
|
||||
onCreate={createConfig}
|
||||
isUpdating={isUpdating}
|
||||
/>
|
||||
|
||||
{configs.length === 0 ? (
|
||||
<Card>
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { toast } from 'sonner'
|
||||
import { settingsApi } from '@/api/settings'
|
||||
import { useMutation } from '@tanstack/react-query'
|
||||
import { useQueryClient } from '@tanstack/react-query'
|
||||
|
||||
interface HealthResponse {
|
||||
status: 'healthy' | 'degraded' | 'unhealthy'
|
||||
timestamp: string
|
||||
database: 'connected' | 'disconnected'
|
||||
opencode: 'healthy' | 'unhealthy'
|
||||
opencodePort: number
|
||||
opencodeVersion: string | null
|
||||
opencodeMinVersion: string
|
||||
opencodeVersionSupported: boolean
|
||||
error?: string
|
||||
}
|
||||
|
||||
async function fetchHealth(): Promise<HealthResponse> {
|
||||
const response = await fetch('/api/health')
|
||||
if (!response.ok) {
|
||||
throw new Error('Health check failed')
|
||||
}
|
||||
return response.json()
|
||||
}
|
||||
|
||||
let lastHealthStatus: 'healthy' | 'unhealthy' = 'healthy'
|
||||
|
||||
export function useServerHealth(enabled = true) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const restartMutation = useMutation({
|
||||
mutationFn: async () => {
|
||||
return await settingsApi.restartOpenCodeServer()
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['health'] })
|
||||
queryClient.invalidateQueries({ queryKey: ['opencode', 'agents'] })
|
||||
toast.success('Server restarted successfully')
|
||||
},
|
||||
onError: (error: unknown) => {
|
||||
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 restart server')
|
||||
: 'Failed to restart server'
|
||||
toast.error(errorMessage)
|
||||
},
|
||||
})
|
||||
|
||||
const rollbackMutation = useMutation({
|
||||
mutationFn: async () => {
|
||||
return await settingsApi.rollbackOpenCodeConfig()
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['health'] })
|
||||
queryClient.invalidateQueries({ queryKey: ['settings'] })
|
||||
queryClient.invalidateQueries({ queryKey: ['opencode', 'agents'] })
|
||||
toast.success(data.message)
|
||||
},
|
||||
onError: () => {
|
||||
toast.error('Failed to rollback to previous config')
|
||||
},
|
||||
})
|
||||
|
||||
const query = useQuery<HealthResponse>({
|
||||
queryKey: ['health'],
|
||||
queryFn: fetchHealth,
|
||||
refetchInterval: 30000,
|
||||
retry: false,
|
||||
enabled,
|
||||
staleTime: 10000,
|
||||
})
|
||||
|
||||
const { data: health } = query
|
||||
|
||||
if (health) {
|
||||
const isUnhealthy = health.opencode !== 'healthy'
|
||||
|
||||
if (isUnhealthy && lastHealthStatus === 'healthy') {
|
||||
toast.error(health.error || 'OpenCode server is currently unhealthy', {
|
||||
duration: Infinity,
|
||||
action: {
|
||||
label: 'Restart',
|
||||
onClick: () => restartMutation.mutate(),
|
||||
},
|
||||
})
|
||||
} else if (!isUnhealthy && lastHealthStatus === 'unhealthy') {
|
||||
toast.success('Server is back online')
|
||||
}
|
||||
|
||||
lastHealthStatus = isUnhealthy ? 'unhealthy' : 'healthy'
|
||||
}
|
||||
|
||||
return {
|
||||
...query,
|
||||
restartMutation,
|
||||
rollbackMutation,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user