UNPKG

mlld

Version:

mlld: a modular prompt scripting language

110 lines (108 loc) 3.52 kB
import { __name } from './chunk-OMKLS24H.mjs'; // interpreter/core/ast-evaluator.ts var _ASTEvaluator = class _ASTEvaluator { /** * Normalize an array value to have consistent type information */ static normalizeArray(value) { if (value && typeof value === "object" && value.type === "array") { return value; } if (Array.isArray(value)) { return { type: "array", items: value.map((item) => this.normalizeValue(item)), location: { synthetic: true } }; } throw new Error(`Expected array, got ${typeof value}`); } /** * Normalize an object value to have consistent type information */ static normalizeObject(value) { if (value && typeof value === "object" && value.type === "object") { return value; } if (typeof value === "object" && value !== null && value.constructor === Object) { const properties = {}; for (const [key, val] of Object.entries(value)) { if (key === "wrapperType" || key === "nodeId" || key === "location") { continue; } properties[key] = this.normalizeValue(val); } return { type: "object", properties, location: { synthetic: true } }; } throw new Error(`Expected object, got ${typeof value}`); } /** * Normalize any value - Phase 2: Extended for objects */ static normalizeValue(value) { if (value === null || value === void 0) { return value; } if (value && typeof value === "object" && "wrapperType" in value && "content" in value && Array.isArray(value.content)) { if (value.content.length > 0 && value.content[0].type === "Text") { return value.content[0].content; } if (value.content.length === 0) { return ""; } return value; } if (value && typeof value === "object" && value.type === "Text" && "content" in value) { return value.content; } if (Array.isArray(value)) { return this.normalizeArray(value); } if (typeof value === "object" && value !== null && value.constructor === Object && !value.type) { return this.normalizeObject(value); } return value; } /** * Evaluate a value to runtime representation * Phase 2: Extended for objects and arrays */ static async evaluateToRuntime(value, env) { if (value === null || value === void 0) { return value; } const normalized = this.normalizeValue(value); if (normalized && normalized.type === "array") { const evaluatedItems = []; for (const item of normalized.items) { evaluatedItems.push(await this.evaluateToRuntime(item, env)); } return evaluatedItems; } if (normalized && normalized.type === "object") { const evaluatedObject = {}; for (const [key, val] of Object.entries(normalized.properties)) { evaluatedObject[key] = await this.evaluateToRuntime(val, env); } return evaluatedObject; } if (normalized && typeof normalized === "object" && "wrapperType" in normalized && "content" in normalized) { const { interpolate } = await import('./interpreter-W2C2T4AU.mjs'); return await interpolate(normalized.content, env); } return normalized; } }; __name(_ASTEvaluator, "ASTEvaluator"); var ASTEvaluator = _ASTEvaluator; export { ASTEvaluator }; //# sourceMappingURL=ast-evaluator-WZNEUUA2.mjs.map //# sourceMappingURL=ast-evaluator-WZNEUUA2.mjs.map