mlld
Version:
mlld: llm scripting language
192 lines (189 loc) • 6.61 kB
JavaScript
import { __name } from './chunk-NJQT543K.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 && val.type === "object") {
const dataObj = val;
if (Array.isArray(dataObj.entries)) {
const plainObj = {};
for (const entry of dataObj.entries) {
if (entry.type === "pair") {
plainObj[entry.key] = entry.value;
}
}
return plainObj;
}
if (dataObj.properties) {
return dataObj.properties;
}
return val;
}
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();
let shouldPrettyPrint = pretty;
if (pretty) {
const compactJson = JSON.stringify(value, replacer);
shouldPrettyPrint = compactJson.length > 60 || this.hasNestedStructures(value);
}
const result = JSON.stringify(value, replacer, shouldPrettyPrint ? indent : void 0);
if (pretty && typeof value === "object" && value !== null && !Array.isArray(value)) {
const keys = Object.keys(value);
if (keys.length === 1 && shouldPrettyPrint) {
const val = value[keys[0]];
if (typeof val !== "object" || val === null) {
const lines = result.split("\n").map((l) => l.trim());
if (lines.length === 3 && lines[0] === "{" && lines[2] === "}") {
return `{${lines[1]}}`;
}
}
}
}
return result;
}
/**
* Check if a value has nested objects or arrays that warrant pretty-printing
*/
static hasNestedStructures(value, depth = 0) {
if (depth > 2) return true;
if (Array.isArray(value)) {
return value.length > 1 || value.some((item) => typeof item === "object" && item !== null);
}
if (typeof value === "object" && value !== null) {
const keys = Object.keys(value);
return keys.length > 0;
}
return false;
}
/**
* 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-VWPKQQNW.mjs.map
//# sourceMappingURL=chunk-VWPKQQNW.mjs.map