mlld
Version:
mlld: a modular prompt scripting language
150 lines (147 loc) • 5.15 kB
JavaScript
import { __name } from './chunk-OMKLS24H.mjs';
// interpreter/utils/ast-evaluation.ts
function createASTAwareJSONReplacer() {
const replacer = /* @__PURE__ */ __name((key, val) => {
if (val && typeof val === "object" && "content" in val && "filename" in val && "tokest" in val) {
const obj = val;
if (typeof obj.tokest === "number" || obj._extension !== void 0) {
return obj.content;
}
}
if (val && typeof val === "object" && "wrapperType" in val && "content" in val) {
const valWithContent = val;
if (Array.isArray(valWithContent.content)) {
if (valWithContent.content.length > 0) {
const firstItem = valWithContent.content[0];
if (firstItem && typeof firstItem === "object" && "type" in firstItem && "content" in firstItem) {
const textNode = firstItem;
if (textNode.type === "Text") {
return textNode.content;
}
}
}
if (valWithContent.content.length === 0) {
return "";
}
}
return "";
}
if (val && typeof val === "object" && "type" in val && "content" in val) {
const textNode = val;
if (textNode.type === "Text") {
return textNode.content;
}
}
if (val && typeof val === "object" && "type" in val && "identifier" in val) {
const varRef = val;
if (varRef.type === "VariableReference") {
return `@${varRef.identifier}`;
}
}
if (val && typeof val === "object" && "type" in val && "properties" in val) {
const dataObj = val;
if (dataObj.type === "object") {
return dataObj.properties;
}
}
if (val && typeof val === "object" && "type" in val && "items" in val) {
const dataArr = val;
if (dataArr.type === "array") {
return dataArr.items;
}
}
if (val && typeof val === "object" && "__executable" in val) {
const execVal = val;
const params = execVal.paramNames || [];
return `<function(${params.join(", ")})>`;
}
if (val && typeof val === "object" && "type" in val) {
const typeVal = val;
if (typeVal.type === "executable") {
const def = typeVal.value || typeVal.definition || {};
const params = def.paramNames || [];
return `<function(${params.join(", ")})>`;
}
}
if (val && typeof val === "object" && val.constructor === Object && !("type" in val)) {
const hasWrappedContent = Object.values(val).some((v) => v && typeof v === "object" && "wrapperType" in v && "content" in v);
if (hasWrappedContent) {
const processed = {};
for (const [k, v] of Object.entries(val)) {
processed[k] = replacer(k, v);
}
return processed;
}
}
return val;
}, "replacer");
return replacer;
}
__name(createASTAwareJSONReplacer, "createASTAwareJSONReplacer");
// interpreter/core/json-formatter.ts
var _JSONFormatter = class _JSONFormatter {
/**
* Single source of truth for JSON serialization in mlld
*/
static stringify(value, options = {}) {
const { pretty = false, indent = 2, handleExecutables = true, handleNamespaces = true } = options;
const replacer = createASTAwareJSONReplacer();
return JSON.stringify(value, replacer, pretty ? indent : void 0);
}
/**
* Special formatting for namespace objects
* Shows only frontmatter and exported variables, not internal structure
*/
static stringifyNamespace(namespaceObject) {
const cleaned = {
frontmatter: {},
exports: {
variables: {},
executables: {}
}
};
const fm = namespaceObject.fm || namespaceObject.frontmatter || namespaceObject.__meta__;
if (fm && Object.keys(fm).length > 0) {
cleaned.frontmatter = fm;
}
const internalFields = [
"fm",
"frontmatter",
"__meta__"
];
let hasExports = false;
for (const [key, value] of Object.entries(namespaceObject)) {
if (!internalFields.includes(key)) {
hasExports = true;
if (value && typeof value === "object" && value.__executable) {
const params = value.paramNames || [];
cleaned.exports.executables[key] = `<function(${params.join(", ")})>`;
} else if (value && typeof value === "object" && value.type === "executable") {
const def = value.value || value.definition || {};
const params = def.paramNames || [];
cleaned.exports.executables[key] = `<function(${params.join(", ")})>`;
} else {
cleaned.exports.variables[key] = value;
}
}
}
if (!hasExports) {
delete cleaned.exports;
}
if (Object.keys(cleaned.frontmatter).length === 0) {
delete cleaned.frontmatter;
}
if (Object.keys(cleaned).length === 0) {
return "{}";
}
return this.stringify(cleaned, {
pretty: true,
handleExecutables: false
});
}
};
__name(_JSONFormatter, "JSONFormatter");
var JSONFormatter = _JSONFormatter;
export { JSONFormatter };
//# sourceMappingURL=chunk-VFHGNRP2.mjs.map
//# sourceMappingURL=chunk-VFHGNRP2.mjs.map