refactor: replace OpenCode server restarts with config reloads for faster updates (#70)

This commit is contained in:
Chris Scott authored and GitHub committed 2026-01-17 12:40:22 -05:00
1 parent 8be0bbd8a0
commit 2bd2b024b8
11 files changed
+183 -107

No files matched your search

@@ -1,8 +1,8 @@
import { useState, useRef, useEffect } from 'react'
import { Button } from '@/components/ui/button'
import { Label } from '@/components/ui/label'
import { Textarea } from '@/components/ui/textarea'
import { Loader2 } from 'lucide-react'
import { Dialog, DialogContent, DialogHeader, DialogFooter, DialogTitle } from '@/components/ui/dialog'
import type { OpenCodeConfig } from '@/api/types/settings'
import { parseJsonc } from '@/lib/jsonc'
@@ -66,60 +66,59 @@ export function OpenCodeConfigEditor({
}
}
if (!isOpen || !config) return null
if (!config) return null
return (
<div className="fixed inset-0 z-50 bg-background/80 backdrop-blur-sm">
<div className="fixed inset-0 z-50 flex items-center justify-center p-4">
<div className="relative w-full max-w-6xl h-[90vh] bg-background border rounded-lg shadow-lg">
<div className="flex flex-col h-full">
<div className="flex items-center justify-between p-6 border-b">
<div>
<h2 className="text-lg font-semibold">Edit Config: {config.name}</h2>
</div>
<div className="flex gap-2">
<Button variant="outline" onClick={onClose}>
Cancel
</Button>
<Button
onClick={updateConfig}
disabled={isUpdating || !editConfigContent.trim()}
>
{isUpdating && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
Update
</Button>
</div>
</div>
<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">
<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-6 overflow-hidden">
<div className="h-full flex flex-col">
<Label htmlFor="edit-config-content" className="mb-2">
Config Content (JSON/JSONC)
</Label>
<Textarea
id="edit-config-content"
ref={editTextareaRef}
value={editConfigContent}
onChange={(e) => {
setEditConfigContent(e.target.value)
setEditError('')
setEditErrorLine(null)
}}
className="flex-1 font-mono text-sm resize-none"
/>
{editError && (
<p className="text-sm text-red-500 mt-2">
{editError}
{editErrorLine && (
<span className="ml-2 text-xs">(Line {editErrorLine})</span>
)}
</p>
<div className="flex-1 p-0 sm:p-4 overflow-hidden relative">
<Textarea
id="edit-config-content"
ref={editTextareaRef}
value={editConfigContent}
onChange={(e) => {
setEditConfigContent(e.target.value)
setEditError('')
setEditErrorLine(null)
}}
className="flex-1 font-mono text-xs sm:text-sm resize-none h-full rounded-none sm:rounded-md"
/>
{editError && (
<div className="absolute bottom-0 left-0 right-0 bg-background/95 border-t p-2 sm:p-3">
<p className="text-xs sm:text-sm text-red-500">
{editError}
{editErrorLine && (
<span className="ml-2 text-xs">(Line {editErrorLine})</span>
)}
</div>
</p>
</div>
</div>
)}
</div>
</div>
</div>
<DialogFooter className="p-3 sm:p-4 border-t gap-2">
<Button
variant="outline"
onClick={onClose}
className="flex-1 sm:flex-none"
>
Cancel
</Button>
<Button
onClick={updateConfig}
disabled={isUpdating || !editConfigContent.trim()}
className="flex-1 sm:flex-none"
>
{isUpdating && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
Update
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}
}
@@ -79,6 +79,15 @@ export function OpenCodeConfigManager() {
}
}
const reloadConfigMutation = useMutation({
mutationFn: async () => {
return await settingsApi.reloadOpenCodeConfig()
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['opencode', 'agents'] })
},
})
const restartServerMutation = useMutation({
mutationFn: async () => {
return await settingsApi.restartOpenCodeServer()
@@ -160,10 +169,10 @@ export function OpenCodeConfigManager() {
const agentsChanged = JSON.stringify(previousContent?.agent) !== JSON.stringify(newContent.agent)
if (restartServer || agentsChanged) {
showToast.loading('Restarting server...', { id: 'update-restart' })
showToast.loading('Reloading server...', { id: 'update-restart' })
try {
await restartServerMutation.mutateAsync()
showToast.success('Configuration updated and server restarted', { id: 'update-restart' })
await reloadConfigMutation.mutateAsync()
showToast.success('Configuration updated and server reloaded', { id: 'update-restart' })
} catch (error) {
showToast.error(getRestartErrorMessage(error), { id: 'update-restart' })
throw error
@@ -214,10 +223,10 @@ export function OpenCodeConfigManager() {
await fetchConfigs()
if (isDefault) {
showToast.loading('Restarting server...', { id: 'create-config' })
showToast.loading('Reloading server...', { id: 'create-config' })
try {
await restartServerMutation.mutateAsync()
showToast.success('Configuration created and server restarted', { id: 'create-config' })
await reloadConfigMutation.mutateAsync()
showToast.success('Configuration created and server reloaded', { id: 'create-config' })
} catch (error) {
showToast.error(getRestartErrorMessage(error), { id: 'create-config' })
throw error
@@ -254,13 +263,13 @@ export function OpenCodeConfigManager() {
}
const setDefaultConfig = async (config: OpenCodeConfig) => {
showToast.loading('Setting default config and restarting server...', { id: 'set-default' })
showToast.loading('Setting default config and reloading server...', { id: 'set-default' })
try {
setIsUpdating(true)
await settingsApi.setDefaultOpenCodeConfig(config.name)
await fetchConfigs()
await restartServerMutation.mutateAsync()
showToast.success('Default config updated and server restarted', { id: 'set-default' })
await reloadConfigMutation.mutateAsync()
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
@@ -487,17 +496,12 @@ export function OpenCodeConfigManager() {
if (!editingConfig) return
showToast.loading('Saving configuration...', { id: 'edit-config' })
try {
const parsedContent = parseJsonc<Record<string, unknown>>(rawContent)
const agentsChanged = JSON.stringify(editingConfig.content.agent) !== JSON.stringify(parsedContent.agent)
await settingsApi.updateOpenCodeConfig(editingConfig.name, { content: rawContent })
await fetchConfigs()
if (agentsChanged) {
showToast.loading('Restarting server...', { id: 'edit-config' })
await restartServerMutation.mutateAsync()
showToast.success('Config updated and server restarted', { id: 'edit-config' })
} else {
showToast.success('Configuration saved', { id: 'edit-config' })
}
const successMsg = editingConfig.isDefault
? 'Configuration saved and server reloaded'
: 'Configuration saved'
showToast.success(successMsg, { id: 'edit-config' })
queryClient.invalidateQueries({ queryKey: ['opencode', 'agents'] })
} catch (error) {
if (error instanceof Error && error.message.includes('restart')) {