UNPKG

@genkit-ai/dotprompt

Version:

Genkit AI framework `.prompt` file format and management library.

144 lines 4.83 kB
"use strict"; var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropNames = Object.getOwnPropertyNames; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var picoschema_exports = {}; __export(picoschema_exports, { picoschema: () => picoschema }); module.exports = __toCommonJS(picoschema_exports); const JSON_SCHEMA_SCALAR_TYPES = [ "string", "boolean", "null", "number", "integer", "any" ]; const WILDCARD_PROPERTY_NAME = "(*)"; function picoschema(schema) { if (!schema) return null; if ([...JSON_SCHEMA_SCALAR_TYPES, "object", "array"].includes( schema == null ? void 0 : schema.type )) { return schema; } if (typeof (schema == null ? void 0 : schema.properties) === "object") { return __spreadProps(__spreadValues({}, schema), { type: "object" }); } return parsePico(schema); } function extractDescription(input) { if (!input.includes(",")) return [input, null]; const match = input.match(/(.*?), *(.*)$/); return [match[1], match[2]]; } function parsePico(obj, path = []) { if (typeof obj === "string") { const [type, description] = extractDescription(obj); if (!JSON_SCHEMA_SCALAR_TYPES.includes(type)) { throw new Error(`Picoschema: Unsupported scalar type '${type}'.`); } if (type === "any") { return description ? { description } : {}; } return description ? { type, description } : { type }; } else if (typeof obj !== "object") { throw new Error( "Picoschema: only consists of objects and strings. Got: " + JSON.stringify(obj) ); } const schema = { type: "object", properties: {}, required: [], additionalProperties: false }; for (const key in obj) { if (key === WILDCARD_PROPERTY_NAME) { schema.additionalProperties = parsePico(obj[key], [...path, key]); continue; } const [name, typeInfo] = key.split("("); const isOptional = name.endsWith("?"); const propertyName = isOptional ? name.slice(0, -1) : name; if (!isOptional) { schema.required.push(propertyName); } if (!typeInfo) { const prop = parsePico(obj[key], [...path, key]); if (isOptional && typeof prop.type === "string") { prop.type = [prop.type, "null"]; } schema.properties[propertyName] = prop; continue; } const [type, description] = extractDescription( typeInfo.substring(0, typeInfo.length - 1) ); if (type === "array") { schema.properties[propertyName] = { type: isOptional ? ["array", "null"] : "array", items: parsePico(obj[key], [...path, key]) }; } else if (type === "object") { const prop = parsePico(obj[key], [...path, key]); if (isOptional) prop.type = [prop.type, "null"]; schema.properties[propertyName] = prop; } else if (type === "enum") { const prop = { enum: obj[key] }; if (isOptional) prop.enum.push(null); schema.properties[propertyName] = prop; } else { throw new Error( "Picoschema: parenthetical types must be 'object' or 'array', got: " + type ); } if (description) { schema.properties[propertyName].description = description; } } if (!schema.required.length) delete schema.required; return schema; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { picoschema }); //# sourceMappingURL=picoschema.js.map