refactor(schema): dedupe custom agent override with ref

This commit is contained in:
edxeth
2026-02-26 21:39:04 +01:00
parent 818fdc490c
commit d7ab5c4d7b
3 changed files with 267 additions and 222 deletions

View File

@@ -1,17 +1,53 @@
import * as z from "zod"
import { OhMyOpenCodeConfigSchema } from "../src/config/schema"
function asRecord(value: unknown): Record<string, unknown> | undefined {
return typeof value === "object" && value !== null ? (value as Record<string, unknown>) : undefined
}
function dedupeCustomAgentOverrideSchema(schema: Record<string, unknown>): Record<string, unknown> {
const rootProperties = asRecord(schema.properties)
const agentsSchema = asRecord(rootProperties?.agents)
const builtInAgentProps = asRecord(agentsSchema?.properties)
const customAgentsSchema = asRecord(rootProperties?.custom_agents)
const customAdditionalProperties = asRecord(customAgentsSchema?.additionalProperties)
if (!builtInAgentProps || !customAgentsSchema || !customAdditionalProperties) {
return schema
}
const referenceAgentSchema = asRecord(
builtInAgentProps.build
?? builtInAgentProps.oracle
?? builtInAgentProps.explore,
)
if (!referenceAgentSchema) {
return schema
}
const defs = asRecord(schema.$defs) ?? {}
defs.agentOverrideConfig = referenceAgentSchema
schema.$defs = defs
customAgentsSchema.additionalProperties = { $ref: "#/$defs/agentOverrideConfig" }
return schema
}
export function createOhMyOpenCodeJsonSchema(): Record<string, unknown> {
const jsonSchema = z.toJSONSchema(OhMyOpenCodeConfigSchema, {
target: "draft-7",
unrepresentable: "any",
})
return {
const schema = {
$schema: "http://json-schema.org/draft-07/schema#",
$id: "https://raw.githubusercontent.com/code-yeongyu/oh-my-opencode/master/assets/oh-my-opencode.schema.json",
title: "Oh My OpenCode Configuration",
description: "Configuration schema for oh-my-opencode plugin",
...jsonSchema,
}
return dedupeCustomAgentOverrideSchema(schema)
}