forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.ts
More file actions
executable file
·76 lines (61 loc) · 2.61 KB
/
schema.ts
File metadata and controls
executable file
·76 lines (61 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env bun
import { Config } from "@/config/config"
import { Schema } from "effect"
import { TuiInfo } from "../src/cli/cmd/tui/config/tui-schema"
type JsonSchema = Record<string, unknown>
const MODEL_REF = "https://models.dev/model-schema.json#/$defs/Model"
function generateEffect(schema: Schema.Top) {
const document = Schema.toJsonSchemaDocument(schema)
const normalized = normalize({
$schema: "https://json-schema.org/draft/2020-12/schema",
...document.schema,
$defs: document.definitions,
})
if (!isRecord(normalized)) throw new Error("schema generator produced a non-object schema")
const restored = restoreModelRefs(normalized)
if (!isRecord(restored)) throw new Error("schema generator produced a non-object schema")
restored.allowComments = true
restored.allowTrailingCommas = true
return restored
}
function normalize(value: unknown): unknown {
if (Array.isArray(value)) return value.map(normalize)
if (!isRecord(value)) return value
const schema = Object.fromEntries(Object.entries(value).map(([key, item]) => [key, normalize(item)]))
if (Array.isArray(schema.anyOf)) {
const anyOf = schema.anyOf.filter((item) => !isRecord(item) || item.type !== "null")
if (anyOf.length !== schema.anyOf.length) {
const { anyOf: _, ...rest } = schema
if (anyOf.length === 1 && isRecord(anyOf[0])) return normalize({ ...anyOf[0], ...rest })
return { ...rest, anyOf }
}
}
if (Array.isArray(schema.allOf) && schema.allOf.length === 1 && isRecord(schema.allOf[0])) {
const { allOf: _, ...rest } = schema
return normalize({ ...schema.allOf[0], ...rest })
}
if (schema.type === "integer" && schema.maximum === undefined) {
return { ...schema, maximum: Number.MAX_SAFE_INTEGER }
}
return schema
}
function restoreModelRefs(value: unknown, key?: string): unknown {
if (Array.isArray(value)) return value.map((item) => restoreModelRefs(item))
if (!isRecord(value)) return value
const schema = Object.fromEntries(Object.entries(value).map(([name, item]) => [name, restoreModelRefs(item, name)]))
if ((key === "model" || key === "small_model") && schema.type === "string") {
return { ...schema, $ref: MODEL_REF }
}
return schema
}
function isRecord(value: unknown): value is JsonSchema {
return typeof value === "object" && value !== null && !Array.isArray(value)
}
const configFile = process.argv[2]
const tuiFile = process.argv[3]
console.log(configFile)
await Bun.write(configFile, JSON.stringify(generateEffect(Config.Info), null, 2))
if (tuiFile) {
console.log(tuiFile)
await Bun.write(tuiFile, JSON.stringify(generateEffect(TuiInfo), null, 2))
}