|
| 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 | +} |
0 commit comments