mlld
Version:
mlld: llm scripting language
250 lines (247 loc) • 7.68 kB
JavaScript
import { STRUCTURED_VALUE_SYMBOL } from './chunk-CQPPOI5P.mjs';
import { MlldInterpreterError } from './chunk-YVSXROKS.mjs';
import { __name } from './chunk-NJQWMXLH.mjs';
// interpreter/utils/json-to-xml.ts
function toScreamingSnakeCase(str) {
return str.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/[\s\-\.]+/g, "_").replace(/[^A-Z0-9_]/gi, "").toUpperCase().replace(/^_+|_+$/g, "");
}
__name(toScreamingSnakeCase, "toScreamingSnakeCase");
function escapeXml(str) {
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
}
__name(escapeXml, "escapeXml");
function jsonToXml(data, rootTag) {
const lines = [];
function convertValue(value, tagName, indent = "") {
if (value === null || value === void 0) {
return;
}
const tag = toScreamingSnakeCase(tagName);
if (Array.isArray(value)) {
lines.push(`${indent}<${tag}>`);
value.forEach((item, index) => {
convertValue(item, `item`, indent + " ");
});
lines.push(`${indent}</${tag}>`);
} else if (typeof value === "object" && value !== null) {
const entries = Object.entries(value);
if (entries.length === 0) {
lines.push(`${indent}<${tag} />`);
} else if (rootTag && indent === "") {
entries.forEach(([key, val]) => {
convertValue(val, key, indent);
});
} else {
lines.push(`${indent}<${tag}>`);
entries.forEach(([key, val]) => {
convertValue(val, key, indent + " ");
});
lines.push(`${indent}</${tag}>`);
}
} else {
const content = escapeXml(String(value));
lines.push(`${indent}<${tag}>${content}</${tag}>`);
}
}
__name(convertValue, "convertValue");
if (Array.isArray(data)) {
convertValue(data, rootTag || "ROOT");
} else if (typeof data === "object" && data !== null) {
Object.entries(data).forEach(([key, value]) => {
convertValue(value, key, "");
});
} else {
return `<DOCUMENT>${escapeXml(String(data))}</DOCUMENT>`;
}
return lines.join("\n");
}
__name(jsonToXml, "jsonToXml");
// interpreter/utils/pipeline-input.ts
function parseCSV(text) {
const lines = text.trim().split("\n");
return lines.map((line) => {
const result = [];
let current = "";
let inQuotes = false;
for (let i = 0; i < line.length; i++) {
const char = line[i];
if (char === '"') {
if (inQuotes && line[i + 1] === '"') {
current += '"';
i++;
} else {
inQuotes = !inQuotes;
}
} else if (char === "," && !inQuotes) {
result.push(current);
current = "";
} else {
current += char;
}
}
if (current || line.endsWith(",")) {
result.push(current);
}
return result;
});
}
__name(parseCSV, "parseCSV");
function parseXML(text) {
try {
const parsed = JSON.parse(text);
return jsonToXml(parsed);
} catch {
return `<DOCUMENT>
${text}
</DOCUMENT>`;
}
}
__name(parseXML, "parseXML");
function createPipelineInput(text, format = "json") {
if (process.env.MLLD_DEBUG === "true") {
console.error("createPipelineInput called with:", {
textType: typeof text,
textLength: typeof text === "string" ? text.length : "N/A",
format,
textPreview: typeof text === "string" ? text.substring(0, 50) : String(text)
});
}
const input = {
text: String(text),
// Ensure it's a string
type: format,
_parsed: void 0
};
Object.defineProperty(input, STRUCTURED_VALUE_SYMBOL, {
value: true,
enumerable: false,
configurable: false,
writable: false
});
switch (format.toLowerCase()) {
case "json":
default:
Object.defineProperty(input, "data", {
get() {
if (this._parsed === void 0) {
try {
if (process.env.MLLD_DEBUG === "true") {
console.error("JSON getter called");
console.error("this:", this);
console.error("this.text exists:", "text" in this);
console.error("this.text value:", this.text);
console.error("Text type:", typeof this.text);
if (this.text) {
console.error("Text length:", this.text.length);
console.error("First 200 chars:", this.text.substring(0, 200));
}
}
if (!this.text) {
throw new Error("PipelineInput.text is undefined or null");
}
this._parsed = JSON.parse(this.text);
} catch (e) {
if (process.env.MLLD_DEBUG === "true") {
console.error("Failed to parse JSON. Text was:", this.text);
console.error("First 100 chars:", this.text.substring(0, 100));
}
throw new MlldInterpreterError(`Failed to parse JSON: ${e.message}`);
}
}
return this._parsed;
},
enumerable: true,
configurable: true
});
break;
case "csv":
Object.defineProperty(input, "csv", {
get() {
if (this._parsed === void 0) {
try {
this._parsed = parseCSV(this.text);
} catch (e) {
throw new MlldInterpreterError(`Failed to parse CSV: ${e.message}`);
}
}
return this._parsed;
},
enumerable: true,
configurable: true
});
Object.defineProperty(input, "data", {
get() {
return this.csv;
},
enumerable: true,
configurable: true
});
break;
case "xml":
Object.defineProperty(input, "xml", {
get() {
if (this._parsed === void 0) {
try {
this._parsed = parseXML(this.text);
} catch (e) {
throw new MlldInterpreterError(`Failed to parse XML: ${e.message}`);
}
}
return this._parsed;
},
enumerable: true,
configurable: true
});
Object.defineProperty(input, "data", {
get() {
return this.xml;
},
enumerable: true,
configurable: true
});
break;
case "text":
Object.defineProperty(input, "data", {
get() {
return this.text;
},
enumerable: true,
configurable: true
});
break;
}
Object.defineProperty(input, "toString", {
value: /* @__PURE__ */ __name(function() {
if (process.env.MLLD_DEBUG === "true") {
console.log("PipelineInput.toString() called - returning text property");
console.trace("toString call stack");
}
return this.text;
}, "value"),
enumerable: false,
configurable: true
});
Object.defineProperty(input, "valueOf", {
value: /* @__PURE__ */ __name(function() {
return this.text;
}, "value"),
enumerable: false,
configurable: true
});
Object.defineProperty(input, Symbol.toPrimitive, {
value: /* @__PURE__ */ __name(function() {
return this.text;
}, "value"),
enumerable: false,
configurable: true
});
return input;
}
__name(createPipelineInput, "createPipelineInput");
function isPipelineInput(value) {
return value && typeof value === "object" && "text" in value && "type" in value && typeof value.text === "string" && typeof value.type === "string";
}
__name(isPipelineInput, "isPipelineInput");
export { createPipelineInput, isPipelineInput, jsonToXml };
//# sourceMappingURL=chunk-7YZGUDWA.mjs.map
//# sourceMappingURL=chunk-7YZGUDWA.mjs.map