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:
Chris Scott committed 2025-12-12 15:29:53 -05:00
1 parent 7a647cb316
commit 4eec4e5c27
12 files changed
+138 -66

No files matched your search

+34 -9
View File
@@ -36,6 +36,7 @@
"rehype-raw": "^7.0.0",
"remark-gfm": "^4.0.1",
"sonner": "^2.0.7",
"strip-json-comments": "^5.0.3",
"tailwind-merge": "^3.3.1",
"zod": "^4.1.12",
"zustand": "^5.0.8"
@@ -90,6 +91,7 @@
"integrity": "sha512-e7jT4DxYvIDLk1ZHmU/m/mB19rex9sv0c2ftBtjSBv+kVM/902eh0fINUzD7UwLLNR+jU585GxUJ8/EBfAM5fw==",
"dev": true,
"license": "MIT",
"peer": true,
"dependencies": {
"@babel/code-frame": "^7.27.1",
"@babel/generator": "^7.28.5",
@@ -877,6 +879,19 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/@eslint/eslintrc/node_modules/strip-json-comments": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz",
"integrity": "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=8"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/@eslint/js": {
"version": "9.39.1",
"resolved": "https://registry.npmjs.org/@eslint/js/-/js-9.39.1.tgz",
@@ -2762,6 +2777,7 @@
"integrity": "sha512-GNWcUTRBgIRJD5zj+Tq0fKOJ5XZajIiBroOF0yvj2bSU1WvNdYS/dn9UxwsujGW4JX06dnHyjV2y9rRaybH0iQ==",
"devOptional": true,
"license": "MIT",
"peer": true,
"dependencies": {
"undici-types": "~7.16.0"
}
@@ -2771,6 +2787,7 @@
"resolved": "https://registry.npmjs.org/@types/react/-/react-19.2.7.tgz",
"integrity": "sha512-MWtvHrGZLFttgeEj28VXHxpmwYbor/ATPYbBfSFZEIRK0ecCFLl2Qo55z52Hss+UV9CRN7trSeq1zbgx7YDWWg==",
"license": "MIT",
"peer": true,
"dependencies": {
"csstype": "^3.2.2"
}
@@ -2781,6 +2798,7 @@
"integrity": "sha512-jp2L/eY6fn+KgVVQAOqYItbF0VY/YApe5Mz2F0aykSO8gx31bYCZyvSeYxCHKvzHG5eZjc+zyaS5BrBWya2+kQ==",
"devOptional": true,
"license": "MIT",
"peer": true,
"peerDependencies": {
"@types/react": "^19.2.0"
}
@@ -2790,8 +2808,7 @@
"resolved": "https://registry.npmjs.org/@types/trusted-types/-/trusted-types-2.0.7.tgz",
"integrity": "sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==",
"license": "MIT",
"optional": true,
"peer": true
"optional": true
},
"node_modules/@types/unist": {
"version": "3.0.3",
@@ -2845,6 +2862,7 @@
"integrity": "sha512-jCzKdm/QK0Kg4V4IK/oMlRZlY+QOcdjv89U2NgKHZk1CYTj82/RVSx1mV/0gqCVMJ/DA+Zf/S4NBWNF8GQ+eqQ==",
"dev": true,
"license": "MIT",
"peer": true,
"dependencies": {
"@typescript-eslint/scope-manager": "8.48.0",
"@typescript-eslint/types": "8.48.0",
@@ -3102,6 +3120,7 @@
"integrity": "sha512-NZyJarBfL7nWwIq+FDL6Zp/yHEhePMNnnJ0y3qfieCrmNvYct8uvtiV41UvlSe6apAfk0fY1FbWx+NwfmpvtTg==",
"dev": true,
"license": "MIT",
"peer": true,
"bin": {
"acorn": "bin/acorn"
},
@@ -3304,6 +3323,7 @@
}
],
"license": "MIT",
"peer": true,
"dependencies": {
"baseline-browser-mapping": "^2.8.25",
"caniuse-lite": "^1.0.30001754",
@@ -3677,7 +3697,6 @@
"resolved": "https://registry.npmjs.org/dompurify/-/dompurify-3.2.7.tgz",
"integrity": "sha512-WhL/YuveyGXJaerVlMYGWhvQswa7myDG17P7Vu65EWC05o8vfeNbvNf4d/BOvH99+ZW+LlQsc1GDKMa1vNK6dw==",
"license": "(MPL-2.0 OR Apache-2.0)",
"peer": true,
"optionalDependencies": {
"@types/trusted-types": "^2.0.7"
}
@@ -3843,6 +3862,7 @@
"integrity": "sha512-BhHmn2yNOFA9H9JmmIVKJmd288g9hrVRDkdoIgRCRuSySRUHH7r/DI6aAXW9T1WwUuY3DFgrcaqB+deURBLR5g==",
"dev": true,
"license": "MIT",
"peer": true,
"dependencies": {
"@eslint-community/eslint-utils": "^4.8.0",
"@eslint-community/regexpp": "^4.12.1",
@@ -5173,7 +5193,6 @@
"resolved": "https://registry.npmjs.org/marked/-/marked-14.0.0.tgz",
"integrity": "sha512-uIj4+faQ+MgHgwUW1l2PsPglZLOLOT1uErt06dAPtx2kjteLAkbsd/0FiYg/MGS+i7ZKLb7w2WClxHkzOOuryQ==",
"license": "MIT",
"peer": true,
"bin": {
"marked": "bin/marked.js"
},
@@ -6311,6 +6330,7 @@
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.3.tgz",
"integrity": "sha512-5gTmgEY/sqK6gFXLIsQNH19lWb4ebPDLA4SdLP7dsWkIXHWlG66oPuVvXSGFPppYZz8ZDZq0dYYrbHfBCVUb1Q==",
"license": "MIT",
"peer": true,
"engines": {
"node": ">=12"
},
@@ -6347,6 +6367,7 @@
}
],
"license": "MIT",
"peer": true,
"dependencies": {
"nanoid": "^3.3.11",
"picocolors": "^1.1.1",
@@ -6404,6 +6425,7 @@
"resolved": "https://registry.npmjs.org/react/-/react-19.2.0.tgz",
"integrity": "sha512-tmbWg6W31tQLeB5cdIBOicJDJRR2KzXsV7uSK9iNfLWQ5bIZfxuPEHp7M8wiHyHnn0DD1i7w3Zmin0FtkrwoCQ==",
"license": "MIT",
"peer": true,
"engines": {
"node": ">=0.10.0"
}
@@ -6413,6 +6435,7 @@
"resolved": "https://registry.npmjs.org/react-dom/-/react-dom-19.2.0.tgz",
"integrity": "sha512-UlbRu4cAiGaIewkPyiRGJk0imDN2T3JjieT6spoL2UeSf5od4n5LB/mQ4ejmxhCFT1tYe8IvaFulzynWovsEFQ==",
"license": "MIT",
"peer": true,
"dependencies": {
"scheduler": "^0.27.0"
},
@@ -6425,6 +6448,7 @@
"resolved": "https://registry.npmjs.org/react-hook-form/-/react-hook-form-7.67.0.tgz",
"integrity": "sha512-E55EOwKJHHIT/I6J9DmQbCWToAYSw9nN5R57MZw9rMtjh+YQreMDxRLfdjfxQbiJ3/qbg3Z02wGzBX4M+5fMtQ==",
"license": "MIT",
"peer": true,
"engines": {
"node": ">=18.0.0"
},
@@ -6834,13 +6858,12 @@
}
},
"node_modules/strip-json-comments": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz",
"integrity": "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==",
"dev": true,
"version": "5.0.3",
"resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-5.0.3.tgz",
"integrity": "sha512-1tB5mhVo7U+ETBKNf92xT4hrQa3pm0MZ0PQvuDnWgAAGHDsfp4lPSpiS6psrSiet87wyGPh9ft6wmhOMQ0hDiw==",
"license": "MIT",
"engines": {
"node": ">=8"
"node": ">=14.16"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
@@ -6993,6 +7016,7 @@
"integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
"dev": true,
"license": "Apache-2.0",
"peer": true,
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
@@ -7264,6 +7288,7 @@
"resolved": "https://registry.npmjs.org/vite/-/vite-7.2.4.tgz",
"integrity": "sha512-NL8jTlbo0Tn4dUEXEsUg8KeyG/Lkmc4Fnzb8JXN/Ykm9G4HNImjtABMJgkQoVjOBN/j2WAwDTRytdqJbZsah7w==",
"license": "MIT",
"peer": true,
"dependencies": {
"esbuild": "^0.25.0",
"fdir": "^6.5.0",
+1
View File
@@ -39,6 +39,7 @@
"rehype-raw": "^7.0.0",
"remark-gfm": "^4.0.1",
"sonner": "^2.0.7",
"strip-json-comments": "^5.0.3",
"tailwind-merge": "^3.3.1",
"zod": "^4.1.12",
"zustand": "^5.0.8"
+3 -2
View File
@@ -57,6 +57,7 @@ export interface OpenCodeConfig {
id: number
name: string
content: Record<string, unknown>
rawContent?: string
isDefault: boolean
createdAt: number
updatedAt: number
@@ -64,12 +65,12 @@ export interface OpenCodeConfig {
export interface CreateOpenCodeConfigRequest {
name: string
content: Record<string, unknown>
content: Record<string, unknown> | string
isDefault?: boolean
}
export interface UpdateOpenCodeConfigRequest {
content: Record<string, unknown>
content: Record<string, unknown> | string
isDefault?: boolean
}
@@ -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}