MCPcopy Create free account
hub / github.com/anomalyco/opencode / fromSchema

Function fromSchema

packages/opencode/src/tool/json-schema.ts:8–22  ·  view source on GitHub ↗
(schema: Schema.Top)

Source from the content-addressed store, hash-verified

6const cache = new WeakMap<Schema.Top, JSONSchema7>()
7
8export function fromSchema(schema: Schema.Top): JSONSchema7 {
9 const cached = cache.get(schema)
10 if (cached) return cached
11
12 const document = Schema.toJsonSchemaDocument(schema, { additionalProperties: true })
13 const result = normalize({
14 $schema: JsonSchema.META_SCHEMA_URI_DRAFT_2020_12,
15 ...document.schema,
16 ...(Object.keys(document.definitions).length > 0 ? { $defs: document.definitions } : {}),
17 })
18 const inlined = dropDefinitionsIfResolved(inlineLocalReferences(result))
19 if (!isJsonSchema(inlined)) throw new Error("tool JSON Schema helper produced a non-schema value")
20 cache.set(schema, inlined)
21 return inlined
22}
23
24export function fromTool(tool: Tool.Def): JSONSchema7 {
25 return tool.jsonSchema ?? fromSchema(tool.parameters as Schema.Top)

Callers 1

fromToolFunction · 0.85

Calls 6

inlineLocalReferencesFunction · 0.85
isJsonSchemaFunction · 0.85
normalizeFunction · 0.70
getMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected