mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 05:26:47 +03:00
fix(schema): publish config schema that accepts V1 and V2 shapes (#55524)
This commit is contained in:
committed by
GitHub
Unverified
parent
e05cc4a18d
commit
ccd3aa787a
@@ -8,7 +8,7 @@
|
||||
"dev": "vite dev --host 0.0.0.0",
|
||||
"dev:enterprise": "bun --watch script/dev-enterprise.ts",
|
||||
"dev:remote": "bun sst shell --stage=dev bun dev",
|
||||
"build": "bun ./script/generate-sitemap.ts && vite build",
|
||||
"build": "bun ./script/generate-sitemap.ts && vite build && bun ../../schema/script/config-schema.ts ./.output/public/config.json",
|
||||
"start": "vite start"
|
||||
},
|
||||
"dependencies": {
|
||||
|
||||
@@ -23,10 +23,9 @@ Use only <https://opencode.ai/v2/docs/> documentation as the source of truth for
|
||||
Do not use <https://opencode.ai/docs/>, which documents V1, and do not use
|
||||
general web search to resolve a V2 documentation question when the V2 docs or
|
||||
linked pages cover it. The schema served from
|
||||
<https://opencode.ai/config.json> may describe V1 even though V2 configuration
|
||||
files include that URL for editor integration. Never use it to infer V2 field
|
||||
names or shapes. If V2 documentation is missing or contradictory, state the
|
||||
uncertainty or ask for clarification instead of falling back to V1.
|
||||
<https://opencode.ai/config.json> accepts both V2 fields and deprecated V1
|
||||
fields. Never recommend a field it marks deprecated. If V2 documentation is
|
||||
missing or contradictory, state the uncertainty or ask for clarification instead of falling back to V1.
|
||||
|
||||
V1 documentation and syntax may be consulted only when the user explicitly
|
||||
asks about V1 or when needed as migration input. Outputs and recommendations
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import { Schema } from "effect"
|
||||
import { Config } from "../src/config.js"
|
||||
|
||||
interface JsonSchema {
|
||||
readonly [key: string]: unknown
|
||||
readonly properties?: Readonly<Record<string, JsonSchema>>
|
||||
}
|
||||
|
||||
// V1 and V2 read the same opencode.json, and V2 normalizes V1 fields at load time, so the published schema accepts
|
||||
// both. The V2 shape is generated from Config.Info; V1 is frozen in config-v1.json, the last schema the V1 runtime
|
||||
// published. Fields only V1 understands are marked deprecated.
|
||||
export async function generate() {
|
||||
const legacy: { $defs: Record<string, JsonSchema> } = await Bun.file(
|
||||
new URL("./config-v1.json", import.meta.url),
|
||||
).json()
|
||||
const { root: current, definitions } = document(Config.Info)
|
||||
const { Config: previous, ...legacyDefinitions } = legacy.$defs
|
||||
const shared = Object.keys(legacyDefinitions).filter((key) => key in definitions)
|
||||
if (shared.length) throw new Error(`V1 and V2 schema definitions share names: ${shared.join(", ")}`)
|
||||
|
||||
return {
|
||||
$schema: "https://json-schema.org/draft/2020-12/schema",
|
||||
$id: "https://opencode.ai/config.json",
|
||||
type: "object",
|
||||
properties: merge(describe(current.properties ?? {}), previous.properties ?? {}),
|
||||
additionalProperties: false,
|
||||
$defs: { ...legacyDefinitions, ...definitions },
|
||||
allowComments: true,
|
||||
allowTrailingCommas: true,
|
||||
}
|
||||
}
|
||||
|
||||
// Optional fields annotate their decoded side, which the encoded JSON Schema omits, so copy top-level descriptions
|
||||
// from the type-side document.
|
||||
function describe(properties: Readonly<Record<string, JsonSchema>>) {
|
||||
const type = document(Schema.toType(Config.Info)).root
|
||||
return Object.fromEntries(
|
||||
Object.entries(properties).map(([key, schema]) => [
|
||||
key,
|
||||
{ ...schema, description: type.properties?.[key]?.description ?? schema.description },
|
||||
]),
|
||||
)
|
||||
}
|
||||
|
||||
// Classes generate a root reference to their definition; plain structs generate the object inline.
|
||||
function document(schema: Schema.Top) {
|
||||
const result = Schema.toJsonSchemaDocument(schema, { onExcessProperty: "error" })
|
||||
const all = result.definitions as Record<string, JsonSchema>
|
||||
if (typeof result.schema.$ref !== "string") return { root: result.schema as JsonSchema, definitions: all }
|
||||
const { [result.schema.$ref.replace("#/$defs/", "")]: root, ...definitions } = all
|
||||
return { root, definitions }
|
||||
}
|
||||
|
||||
function merge(current: Readonly<Record<string, JsonSchema>>, previous: Readonly<Record<string, JsonSchema>>) {
|
||||
return Object.fromEntries([
|
||||
...Object.entries(current).map(([key, schema]) => [key, previous[key] ? combine(schema, previous[key]) : schema]),
|
||||
...Object.entries(previous)
|
||||
.filter(([key]) => !current[key])
|
||||
.map(([key, schema]) => [key, { ...schema, deprecated: true }]),
|
||||
])
|
||||
}
|
||||
|
||||
// Shared keys keep the V2 shape. Shared objects also accept their V1 properties, and keys whose type changed accept
|
||||
// either shape.
|
||||
function combine(current: JsonSchema, previous: JsonSchema): JsonSchema {
|
||||
const description = current.description ?? previous.description
|
||||
if (current.type === "object" && previous.type === "object")
|
||||
return {
|
||||
...current,
|
||||
description,
|
||||
properties: merge(current.properties ?? {}, previous.properties ?? {}),
|
||||
additionalProperties:
|
||||
typeof current.additionalProperties === "object" || typeof previous.additionalProperties !== "object"
|
||||
? current.additionalProperties
|
||||
: { ...previous.additionalProperties, deprecated: true },
|
||||
}
|
||||
if (current.type !== undefined && previous.type !== undefined && current.type !== previous.type)
|
||||
return { description, anyOf: [current, { ...previous, deprecated: true }] }
|
||||
return { ...current, description }
|
||||
}
|
||||
|
||||
if (import.meta.main) {
|
||||
const target = process.argv[2]
|
||||
if (!target) throw new Error("A schema output path is required")
|
||||
await Bun.write(target, JSON.stringify(await generate(), null, 2) + "\n")
|
||||
}
|
||||
@@ -954,7 +954,7 @@
|
||||
]
|
||||
}
|
||||
],
|
||||
"description": "Automatically update to the latest version. Set to true to auto-update, false to disable, or 'notify' to show update notifications"
|
||||
"description": "Check for new versions and show an update notice. Updates are never installed automatically; run /update or `opencode upgrade`. Set to false to disable update checks. true and 'notify' behave the same"
|
||||
},
|
||||
"disabled_providers": {
|
||||
"type": "array",
|
||||
@@ -8,6 +8,7 @@ import config from "./config.mjs"
|
||||
import { rehypeHeadingIds } from "@astrojs/markdown-remark"
|
||||
import rehypeAutolinkHeadings from "rehype-autolink-headings"
|
||||
import { copyFileSync } from "fs"
|
||||
import { execFileSync } from "child_process"
|
||||
|
||||
// https://astro.build/config
|
||||
export default defineConfig({
|
||||
@@ -327,8 +328,8 @@ function configSchema() {
|
||||
name: "configSchema",
|
||||
hooks: {
|
||||
"astro:build:done": async () => {
|
||||
// V1 schemas are frozen in the console app since packages/opencode lives on the v1 branch.
|
||||
copyFileSync("../console/app/public/config.json", "./dist/config.json")
|
||||
execFileSync("bun", ["../schema/script/config-schema.ts", "./dist/config.json"], { stdio: "inherit" })
|
||||
// The V1 TUI schema is frozen in the console app since packages/opencode lives on the v1 branch.
|
||||
copyFileSync("../console/app/public/tui.json", "./dist/tui.json")
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user