@langchain/core
Version:
Core LangChain.js abstractions and schemas
57 lines (55 loc) • 2.19 kB
JavaScript
const require_Options = require('./Options.cjs');
const require_getRelativePath = require('./getRelativePath.cjs');
const require_any = require('./parsers/any.cjs');
const require_selectParser = require('./selectParser.cjs');
//#region src/utils/zod-to-json-schema/parseDef.ts
function parseDef(def, refs, forceResolution = false) {
const seenItem = refs.seen.get(def);
if (refs.override) {
const overrideResult = refs.override?.(def, refs, seenItem, forceResolution);
if (overrideResult !== require_Options.ignoreOverride) return overrideResult;
}
if (seenItem && !forceResolution) {
const seenSchema = get$ref(seenItem, refs);
if (seenSchema !== void 0) return seenSchema;
}
const newItem = {
def,
path: refs.currentPath,
jsonSchema: void 0
};
refs.seen.set(def, newItem);
const jsonSchemaOrGetter = require_selectParser.selectParser(def, def.typeName, refs);
const jsonSchema = typeof jsonSchemaOrGetter === "function" ? parseDef(jsonSchemaOrGetter(), refs) : jsonSchemaOrGetter;
if (jsonSchema) addMeta(def, refs, jsonSchema);
if (refs.postProcess) {
const postProcessResult = refs.postProcess(jsonSchema, def, refs);
newItem.jsonSchema = jsonSchema;
return postProcessResult;
}
newItem.jsonSchema = jsonSchema;
return jsonSchema;
}
const get$ref = (item, refs) => {
switch (refs.$refStrategy) {
case "root": return { $ref: item.path.join("/") };
case "relative": return { $ref: require_getRelativePath.getRelativePath(refs.currentPath, item.path) };
case "none":
case "seen":
if (item.path.length < refs.currentPath.length && item.path.every((value, index) => refs.currentPath[index] === value)) {
console.warn(`Recursive reference detected at ${refs.currentPath.join("/")}! Defaulting to any`);
return require_any.parseAnyDef(refs);
}
return refs.$refStrategy === "seen" ? require_any.parseAnyDef(refs) : void 0;
}
};
const addMeta = (def, refs, jsonSchema) => {
if (def.description) {
jsonSchema.description = def.description;
if (refs.markdownDescription) jsonSchema.markdownDescription = def.description;
}
return jsonSchema;
};
//#endregion
exports.parseDef = parseDef;
//# sourceMappingURL=parseDef.cjs.map