From ccd3aa787a0996307f329498de17efa991d7ab12 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Wed, 14 Oct 2026 18:09:31 -0500 Subject: [PATCH] fix(schema): publish config schema that accepts V1 and V2 shapes (#55524) --- packages/console/app/package.json | 2 +- packages/core/src/plugin/skill/opencode.md | 7 +- packages/schema/script/config-schema.ts | 86 +++++++++++++++++++ .../script/config-v1.json} | 2 +- packages/web/astro.config.mjs | 5 +- 5 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 packages/schema/script/config-schema.ts rename packages/{console/app/public/config.json => schema/script/config-v1.json} (99%) diff --git a/packages/console/app/package.json b/packages/console/app/package.json index 860aa9179b5..2147043fc4d 100644 --- a/packages/console/app/package.json +++ b/packages/console/app/package.json @@ -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": { diff --git a/packages/core/src/plugin/skill/opencode.md b/packages/core/src/plugin/skill/opencode.md index 50bcd2c09d4..87e27d7b6f1 100644 --- a/packages/core/src/plugin/skill/opencode.md +++ b/packages/core/src/plugin/skill/opencode.md @@ -23,10 +23,9 @@ Use only documentation as the source of truth for Do not use , 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 - 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. + 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 diff --git a/packages/schema/script/config-schema.ts b/packages/schema/script/config-schema.ts new file mode 100644 index 00000000000..1ec644ae3d9 --- /dev/null +++ b/packages/schema/script/config-schema.ts @@ -0,0 +1,86 @@ +import { Schema } from "effect" +import { Config } from "../src/config.js" + +interface JsonSchema { + readonly [key: string]: unknown + readonly properties?: Readonly> +} + +// 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 } = 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>) { + 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 + 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>, previous: Readonly>) { + 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") +} diff --git a/packages/console/app/public/config.json b/packages/schema/script/config-v1.json similarity index 99% rename from packages/console/app/public/config.json rename to packages/schema/script/config-v1.json index 29540479a29..0c597a6e91a 100644 --- a/packages/console/app/public/config.json +++ b/packages/schema/script/config-v1.json @@ -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", diff --git a/packages/web/astro.config.mjs b/packages/web/astro.config.mjs index 4d094e860ae..7bff7e79cb5 100644 --- a/packages/web/astro.config.mjs +++ b/packages/web/astro.config.mjs @@ -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") }, },