@upstart.gg/sdk
Version:
You can test the CLI without recompiling by running:
59 lines (57 loc) • 1.87 kB
JavaScript
import { Kind } from "@sinclair/typebox";
//#region src/shared/utils/llm.ts
/**
* Clean all properties from custom metadata recursively. Custom metadata are key that:
* - Either with the name "metadata"
* - Or starting with "ui:"
* Also removes properties that have "ai:hidden" set to true
*/
function toLLMSchema(schema) {
return cleanSchemaRecursive(schema);
}
/**
* Recursively removes custom metadata from schema and filters out properties with ai:hidden = true
*/
function cleanSchemaRecursive(schema) {
const cleaned = { [Kind]: schema[Kind] };
for (const [key, value] of Object.entries(schema)) {
if (key === "metadata" || key.startsWith("ui:")) continue;
if (value && typeof value === "object") if (Array.isArray(value)) cleaned[key] = value.map((item) => {
if (item && typeof item === "object" && isSchemaLike(item)) return cleanSchemaRecursive(item);
return item;
});
else if (isSchemaLike(value)) cleaned[key] = cleanSchemaRecursive(value);
else if (key === "properties" || key === "definitions" || key === "$defs" || key === "patternProperties") {
const cleanedProps = {};
for (const [propKey, propValue] of Object.entries(value)) if (propValue && typeof propValue === "object") {
if (propValue["ai:hidden"] === true) continue;
cleanedProps[propKey] = cleanSchemaRecursive(propValue);
} else cleanedProps[propKey] = propValue;
cleaned[key] = cleanedProps;
} else cleaned[key] = value;
else cleaned[key] = value;
}
return cleaned;
}
/**
* Helper function to determine if an object looks like a schema
*/
function isSchemaLike(obj) {
if (!obj || typeof obj !== "object") return false;
return [
"type",
"$ref",
"properties",
"items",
"anyOf",
"oneOf",
"allOf",
"not",
"if",
"then",
"else"
].some((key) => key in obj);
}
//#endregion
export { toLLMSchema };
//# sourceMappingURL=llm.js.map