mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Add comprehensive JSONC support for opencode.jsonc files
- Store raw JSONC content in database, parse on-the-fly for validation - Return rawContent field in API response for editing with comments preserved - Update frontend to use rawContent for config editing - Accept string or object content in create/update requests - Download configs as .jsonc when they contain comments - Add strip-json-comments dependency for JSONC parsing - Add .jsonc MIME type support in backend file service This preserves comments in opencode.jsonc files when editing through the UI
This commit is contained in:
1 parent
7a647cb316
commit
4eec4e5c27
12 files changed
+138
-66
No files matched your search
@@ -6,6 +6,7 @@ import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import stripJsonComments from 'strip-json-comments'
|
||||
|
||||
interface CreateConfigDialogProps {
|
||||
isOpen: boolean
|
||||
@@ -62,11 +63,11 @@ export function CreateConfigDialog({ isOpen, onOpenChange, onCreate, isUpdating
|
||||
reader.onload = (e) => {
|
||||
const fileContent = e.target?.result as string
|
||||
try {
|
||||
JSON.parse(fileContent)
|
||||
JSON.parse(stripJsonComments(fileContent))
|
||||
setContent(fileContent)
|
||||
setName(file.name.replace('.json', '').replace('.jsonc', ''))
|
||||
} catch {
|
||||
window.alert('Invalid JSON file')
|
||||
window.alert('Invalid JSON/JSONC file')
|
||||
}
|
||||
}
|
||||
reader.readAsText(file)
|
||||
|
||||
@@ -4,12 +4,13 @@ import { Label } from '@/components/ui/label'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Loader2 } from 'lucide-react'
|
||||
import type { OpenCodeConfig } from '@/api/types/settings'
|
||||
import stripJsonComments from 'strip-json-comments'
|
||||
|
||||
interface OpenCodeConfigEditorProps {
|
||||
config: OpenCodeConfig | null
|
||||
isOpen: boolean
|
||||
onClose: () => void
|
||||
onUpdate: (content: Record<string, unknown>) => Promise<void>
|
||||
onUpdate: (content: string) => Promise<void>
|
||||
isUpdating: boolean
|
||||
}
|
||||
|
||||
@@ -27,7 +28,7 @@ export function OpenCodeConfigEditor({
|
||||
|
||||
useEffect(() => {
|
||||
if (config && isOpen) {
|
||||
setEditConfigContent(JSON.stringify(config.content, null, 2))
|
||||
setEditConfigContent(config.rawContent || JSON.stringify(config.content, null, 2))
|
||||
setEditError('')
|
||||
setEditErrorLine(null)
|
||||
}
|
||||
@@ -43,7 +44,7 @@ export function OpenCodeConfigEditor({
|
||||
if (!config) return
|
||||
|
||||
try {
|
||||
const parsedContent = JSON.parse(editConfigContent)
|
||||
const parsedContent = JSON.parse(stripJsonComments(editConfigContent))
|
||||
|
||||
const forbiddenFields = ['id', 'createdAt', 'updatedAt']
|
||||
const foundForbidden = forbiddenFields.filter(field => field in parsedContent)
|
||||
@@ -51,14 +52,14 @@ export function OpenCodeConfigEditor({
|
||||
throw new Error(`Invalid fields found: ${foundForbidden.join(', ')}. These fields are managed automatically.`)
|
||||
}
|
||||
|
||||
await onUpdate(parsedContent)
|
||||
await onUpdate(editConfigContent)
|
||||
onClose()
|
||||
} catch (error) {
|
||||
if (error instanceof SyntaxError) {
|
||||
const match = error.message.match(/line (\d+)/i)
|
||||
const line = match ? parseInt(match[1]) : null
|
||||
setEditErrorLine(line)
|
||||
setEditError('Invalid JSON format')
|
||||
setEditError('Invalid JSON/JSONC format')
|
||||
} else {
|
||||
setEditError(error instanceof Error ? error.message : 'Failed to update config')
|
||||
}
|
||||
@@ -93,7 +94,7 @@ export function OpenCodeConfigEditor({
|
||||
<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)
|
||||
Config Content (JSON/JSONC)
|
||||
</Label>
|
||||
<Textarea
|
||||
id="edit-config-content"
|
||||
|
||||
@@ -13,11 +13,13 @@ import { AgentsEditor } from './AgentsEditor'
|
||||
import { McpManager } from './McpManager'
|
||||
import { settingsApi } from '@/api/settings'
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import stripJsonComments from 'strip-json-comments'
|
||||
|
||||
interface OpenCodeConfig {
|
||||
id: number
|
||||
name: string
|
||||
content: Record<string, unknown>
|
||||
rawContent?: string
|
||||
isDefault: boolean
|
||||
createdAt: number
|
||||
updatedAt: number
|
||||
@@ -110,10 +112,10 @@ export function OpenCodeConfigManager() {
|
||||
}
|
||||
}, [configs, selectedConfig])
|
||||
|
||||
const createConfig = async (name: string, content: string, isDefault: boolean) => {
|
||||
const createConfig = async (name: string, rawContent: string, isDefault: boolean) => {
|
||||
try {
|
||||
setIsUpdating(true)
|
||||
const parsedContent = JSON.parse(content)
|
||||
const parsedContent = JSON.parse(stripJsonComments(rawContent))
|
||||
|
||||
const forbiddenFields = ['id', 'createdAt', 'updatedAt']
|
||||
const foundForbidden = forbiddenFields.filter(field => field in parsedContent)
|
||||
@@ -123,13 +125,12 @@ export function OpenCodeConfigManager() {
|
||||
|
||||
await settingsApi.createOpenCodeConfig({
|
||||
name: name.trim(),
|
||||
content: parsedContent,
|
||||
content: rawContent,
|
||||
isDefault,
|
||||
})
|
||||
|
||||
setIsCreateDialogOpen(false)
|
||||
fetchConfigs()
|
||||
// Invalidate agents cache in case new config has agents
|
||||
queryClient.invalidateQueries({ queryKey: ['opencode', 'agents'] })
|
||||
} catch (error) {
|
||||
console.error('Failed to create config:', error)
|
||||
@@ -171,11 +172,14 @@ export function OpenCodeConfigManager() {
|
||||
|
||||
|
||||
const downloadConfig = (config: OpenCodeConfig) => {
|
||||
const blob = new Blob([JSON.stringify(config.content, null, 2)], { type: 'application/json' })
|
||||
const content = config.rawContent || JSON.stringify(config.content, null, 2)
|
||||
const hasComments = config.rawContent && config.rawContent.includes('//')
|
||||
const extension = hasComments ? 'jsonc' : 'json'
|
||||
const blob = new Blob([content], { type: 'application/json' })
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = `${config.name}.json`
|
||||
a.download = `${config.name}.${extension}`
|
||||
a.click()
|
||||
URL.revokeObjectURL(url)
|
||||
}
|
||||
@@ -298,16 +302,15 @@ export function OpenCodeConfigManager() {
|
||||
config={editingConfig}
|
||||
isOpen={isEditDialogOpen}
|
||||
onClose={() => setIsEditDialogOpen(false)}
|
||||
onUpdate={async (content) => {
|
||||
onUpdate={async (rawContent) => {
|
||||
if (!editingConfig) return
|
||||
const agentsChanged = JSON.stringify(editingConfig.content.agent) !== JSON.stringify(content.agent)
|
||||
await settingsApi.updateOpenCodeConfig(editingConfig.name, { content })
|
||||
const parsedContent = JSON.parse(stripJsonComments(rawContent)) as Record<string, unknown>
|
||||
const agentsChanged = JSON.stringify(editingConfig.content.agent) !== JSON.stringify(parsedContent.agent)
|
||||
await settingsApi.updateOpenCodeConfig(editingConfig.name, { content: rawContent })
|
||||
await fetchConfigs()
|
||||
// Auto-restart server if agents were modified
|
||||
if (agentsChanged) {
|
||||
restartServerMutation.mutate()
|
||||
}
|
||||
// Invalidate agents cache so @ mentions get updated
|
||||
queryClient.invalidateQueries({ queryKey: ['opencode', 'agents'] })
|
||||
}}
|
||||
isUpdating={isUpdating}
|
||||
|
||||
Reference in new issue
Block a user