Skip to content

Commit ccd3aa7

Browse files
authored
fix(schema): publish config schema that accepts V1 and V2 shapes (anomalyco#55524)
1 parent e05cc4a commit ccd3aa7

5 files changed

Lines changed: 94 additions & 8 deletions

File tree

‎packages/console/app/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"dev": "vite dev --host 0.0.0.0",
99
"dev:enterprise": "bun --watch script/dev-enterprise.ts",
1010
"dev:remote": "bun sst shell --stage=dev bun dev",
11-
"build": "bun ./script/generate-sitemap.ts && vite build",
11+
"build": "bun ./script/generate-sitemap.ts && vite build && bun ../../schema/script/config-schema.ts ./.output/public/config.json",
1212
"start": "vite start"
1313
},
1414
"dependencies": {

‎packages/core/src/plugin/skill/opencode.md‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ Use only <https://opencode.ai/v2/docs/> documentation as the source of truth for
2323
Do not use <https://opencode.ai/docs/>, which documents V1, and do not use
2424
general web search to resolve a V2 documentation question when the V2 docs or
2525
linked pages cover it. The schema served from
26-
<https://opencode.ai/config.json> may describe V1 even though V2 configuration
27-
files include that URL for editor integration. Never use it to infer V2 field
28-
names or shapes. If V2 documentation is missing or contradictory, state the
29-
uncertainty or ask for clarification instead of falling back to V1.
26+
<https://opencode.ai/config.json> accepts both V2 fields and deprecated V1
27+
fields. Never recommend a field it marks deprecated. If V2 documentation is
28+
missing or contradictory, state the uncertainty or ask for clarification instead of falling back to V1.
3029

3130
V1 documentation and syntax may be consulted only when the user explicitly
3231
asks about V1 or when needed as migration input. Outputs and recommendations
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { Schema } from "effect"
2+
import { Config } from "../src/config.js"
3+
4+
interface JsonSchema {
5+
readonly [key: string]: unknown
6+
readonly properties?: Readonly<Record<string, JsonSchema>>
7+
}
8+
9+
// V1 and V2 read the same opencode.json, and V2 normalizes V1 fields at load time, so the published schema accepts
10+
// both. The V2 shape is generated from Config.Info; V1 is frozen in config-v1.json, the last schema the V1 runtime
11+
// published. Fields only V1 understands are marked deprecated.
12+
export async function generate() {
13+
const legacy: { $defs: Record<string, JsonSchema> } = await Bun.file(
14+
new URL("./config-v1.json", import.meta.url),
15+
).json()
16+
const { root: current, definitions } = document(Config.Info)
17+
const { Config: previous, ...legacyDefinitions } = legacy.$defs
18+
const shared = Object.keys(legacyDefinitions).filter((key) => key in definitions)
19+
if (shared.length) throw new Error(`V1 and V2 schema definitions share names: ${shared.join(", ")}`)
20+
21+
return {
22+
$schema: "https://json-schema.org/draft/2020-12/schema",
23+
$id: "https://opencode.ai/config.json",
24+
type: "object",
25+
properties: merge(describe(current.properties ?? {}), previous.properties ?? {}),
26+
additionalProperties: false,
27+
$defs: { ...legacyDefinitions, ...definitions },
28+
allowComments: true,
29+
allowTrailingCommas: true,
30+
}
31+
}
32+
33+
// Optional fields annotate their decoded side, which the encoded JSON Schema omits, so copy top-level descriptions
34+
// from the type-side document.
35+
function describe(properties: Readonly<Record<string, JsonSchema>>) {
36+
const type = document(Schema.toType(Config.Info)).root
37+
return Object.fromEntries(
38+
Object.entries(properties).map(([key, schema]) => [
39+
key,
40+
{ ...schema, description: type.properties?.[key]?.description ?? schema.description },
41+
]),
42+
)
43+
}
44+
45+
// Classes generate a root reference to their definition; plain structs generate the object inline.
46+
function document(schema: Schema.Top) {
47+
const result = Schema.toJsonSchemaDocument(schema, { onExcessProperty: "error" })
48+
const all = result.definitions as Record<string, JsonSchema>
49+
if (typeof result.schema.$ref !== "string") return { root: result.schema as JsonSchema, definitions: all }
50+
const { [result.schema.$ref.replace("#/$defs/", "")]: root, ...definitions } = all
51+
return { root, definitions }
52+
}
53+
54+
function merge(current: Readonly<Record<string, JsonSchema>>, previous: Readonly<Record<string, JsonSchema>>) {
55+
return Object.fromEntries([
56+
...Object.entries(current).map(([key, schema]) => [key, previous[key] ? combine(schema, previous[key]) : schema]),
57+
...Object.entries(previous)
58+
.filter(([key]) => !current[key])
59+
.map(([key, schema]) => [key, { ...schema, deprecated: true }]),
60+
])
61+
}
62+
63+
// Shared keys keep the V2 shape. Shared objects also accept their V1 properties, and keys whose type changed accept
64+
// either shape.
65+
function combine(current: JsonSchema, previous: JsonSchema): JsonSchema {
66+
const description = current.description ?? previous.description
67+
if (current.type === "object" && previous.type === "object")
68+
return {
69+
...current,
70+
description,
71+
properties: merge(current.properties ?? {}, previous.properties ?? {}),
72+
additionalProperties:
73+
typeof current.additionalProperties === "object" || typeof previous.additionalProperties !== "object"
74+
? current.additionalProperties
75+
: { ...previous.additionalProperties, deprecated: true },
76+
}
77+
if (current.type !== undefined && previous.type !== undefined && current.type !== previous.type)
78+
return { description, anyOf: [current, { ...previous, deprecated: true }] }
79+
return { ...current, description }
80+
}
81+
82+
if (import.meta.main) {
83+
const target = process.argv[2]
84+
if (!target) throw new Error("A schema output path is required")
85+
await Bun.write(target, JSON.stringify(await generate(), null, 2) + "\n")
86+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@
954954
]
955955
}
956956
],
957-
"description": "Automatically update to the latest version. Set to true to auto-update, false to disable, or 'notify' to show update notifications"
957+
"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"
958958
},
959959
"disabled_providers": {
960960
"type": "array",

‎packages/web/astro.config.mjs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import config from "./config.mjs"
88
import { rehypeHeadingIds } from "@astrojs/markdown-remark"
99
import rehypeAutolinkHeadings from "rehype-autolink-headings"
1010
import { copyFileSync } from "fs"
11+
import { execFileSync } from "child_process"
1112

1213
// https://astro.build/config
1314
export default defineConfig({
@@ -327,8 +328,8 @@ function configSchema() {
327328
name: "configSchema",
328329
hooks: {
329330
"astro:build:done": async () => {
330-
// V1 schemas are frozen in the console app since packages/opencode lives on the v1 branch.
331-
copyFileSync("../console/app/public/config.json", "./dist/config.json")
331+
execFileSync("bun", ["../schema/script/config-schema.ts", "./dist/config.json"], { stdio: "inherit" })
332+
// The V1 TUI schema is frozen in the console app since packages/opencode lives on the v1 branch.
332333
copyFileSync("../console/app/public/tui.json", "./dist/tui.json")
333334
},
334335
},

0 commit comments

Comments
 (0)