UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

109 lines (108 loc) • 4.3 kB
import { unescape as percentDecoded } from "node:querystring"; import { JSON_SCHEMA_SUBSCHEMA_SEATS } from "../core/json-schema-seats.js"; const isRecord = (v) => typeof v === "object" && v !== null && !Array.isArray(v); const REFERENCE_KEYWORDS = ["$ref", "$dynamicRef"]; const token = (t) => `/${String(t).replace(/~/g, "~0").replace(/\//g, "~1")}`; function typeWordOf(value, declared) { const own = typeof value === "string" || typeof value === "boolean" ? typeof value : typeof value === "number" && Number.isFinite(value) ? "number" : undefined; if (own === undefined) return undefined; if (declared === undefined || declared === own) return own; return declared === "integer" && own === "number" && Number.isInteger(value) ? "integer" : undefined; } function valueSetOf(node) { if (!isRecord(node)) return undefined; const keys = Object.keys(node).filter((k) => k !== "type"); if (keys.length !== 1 || (keys[0] !== "const" && keys[0] !== "enum")) return undefined; const values = keys[0] === "const" ? [node.const] : node.enum; if (!Array.isArray(values) || values.length === 0) return undefined; const words = new Set(values.map((v) => typeWordOf(v, node.type))); const [type] = words; return words.size === 1 && type !== undefined ? { type, values: values } : undefined; } function foldedUnion(node) { if (!Array.isArray(node.anyOf) || node.anyOf.length === 0 || Object.hasOwn(node, "enum")) return undefined; const sets = node.anyOf.map(valueSetOf); const type = sets[0]?.type; if (type === undefined || !sets.every((s) => s?.type === type)) return undefined; const values = [...new Set(sets.flatMap((s) => s.values))]; const out = {}; for (const [k, v] of Object.entries(node)) { if (k !== "anyOf") out[k] = v; else { if (!Object.hasOwn(node, "type")) out.type = type; out.enum = values; } } return out; } function* children(node, pointer) { for (const [key, value] of Object.entries(node)) { const seat = JSON_SCHEMA_SUBSCHEMA_SEATS.get(key); if (seat === undefined) continue; const at = pointer + token(key); if (seat === "schema" && !Array.isArray(value)) yield [key, undefined, value, at]; else if (Array.isArray(value)) for (const [i, sub] of value.entries()) yield [key, i, sub, at + token(i)]; else if (isRecord(value)) for (const [name, sub] of Object.entries(value)) yield [key, name, sub, at + token(name)]; } } function keptUnions(root) { const kept = new Set(); const seen = new Set(); const visit = (node) => { if (!isRecord(node) || seen.has(node)) return; seen.add(node); for (const k of REFERENCE_KEYWORDS) { const ref = node[k]; if (typeof ref !== "string" || !ref.startsWith("#/")) continue; const tokens = percentDecoded(ref.slice(1)).split("/").slice(1); tokens.forEach((t, i) => t === "anyOf" && kept.add(tokens.slice(0, i).map((x) => `/${x}`).join(""))); } for (const [, , sub] of children(node, "")) visit(sub); }; visit(root); return kept; } function project(node, pointer, walk) { if (!isRecord(node) || walk.ancestors.has(node)) return node; walk.ancestors.add(node); let out; for (const [key, name, sub, at] of children(node, pointer)) { const next = project(sub, at, walk); if (next === sub) continue; out ??= { ...node }; if (name === undefined) out[key] = next; else { const declared = node[key]; if (out[key] === declared) out[key] = Array.isArray(declared) ? [...declared] : { ...declared }; out[key][name] = next; } } walk.ancestors.delete(node); const current = out ?? node; return walk.kept.has(pointer) ? current : (foldedUnion(current) ?? current); } export function projectToolSchema(schema) { return project(schema, "", { ancestors: new Set(), kept: keptUnions(schema) }); }