mlld
Version:
mlld: llm scripting language
282 lines (279 loc) • 9.36 kB
JavaScript
import { hasVariableMetadata, getVariableMetadata } from './chunk-DFBYCVE5.mjs';
import { MlldError } from './chunk-V5XE5YB5.mjs';
import { makeSecurityDescriptor, mergeDescriptors, isStructuredValue, wrapStructured } from './chunk-RKGZ44GZ.mjs';
import { isLoadContentResult } from './chunk-TZUIAYSF.mjs';
import { __name } from './chunk-NJQT543K.mjs';
import fs from 'fs';
import path from 'path';
function normalizeDirPath(dirPath) {
const trimmed = dirPath.replace(/[\\/]+$/, "");
const normalized = path.normalize(trimmed || dirPath);
const withForwardSlashes = normalized.replace(/\\/g, "/");
if (process.platform === "win32") {
return withForwardSlashes.toLowerCase();
}
return withForwardSlashes;
}
__name(normalizeDirPath, "normalizeDirPath");
function resolveRealPath(targetPath) {
const resolved = path.resolve(targetPath);
try {
return fs.realpathSync.native ? fs.realpathSync.native(resolved) : fs.realpathSync(resolved);
} catch {
return resolved;
}
}
__name(resolveRealPath, "resolveRealPath");
function getAllDirsInPath(filePath) {
const realPath = resolveRealPath(filePath);
const normalizedPath = normalizeDirPath(realPath);
const parsed = path.parse(normalizedPath);
const root = normalizeDirPath(parsed.root || path.sep);
const dirs = [];
let currentDir = normalizeDirPath(path.dirname(normalizedPath));
while (currentDir && currentDir !== root) {
dirs.push(currentDir);
const nextDir = normalizeDirPath(path.dirname(currentDir));
if (nextDir === currentDir) {
break;
}
currentDir = nextDir;
}
return dirs;
}
__name(getAllDirsInPath, "getAllDirsInPath");
function labelsForPath(filePath) {
return getAllDirsInPath(filePath).map((dir) => `dir:${dir}`);
}
__name(labelsForPath, "labelsForPath");
// interpreter/utils/load-content-structured.ts
function detectStructuredType(value) {
if (Array.isArray(value)) {
return "array";
}
if (value !== null && typeof value === "object") {
return "object";
}
return "json";
}
__name(detectStructuredType, "detectStructuredType");
function tryParseJson(text) {
if (typeof text !== "string") {
return {
success: false
};
}
const trimmed = text.trim();
if (!trimmed) {
return {
success: false
};
}
try {
return {
success: true,
value: JSON.parse(trimmed)
};
} catch {
return {
success: false
};
}
}
__name(tryParseJson, "tryParseJson");
function parseJsonWithContext(text, sourceLabel) {
try {
return JSON.parse(text);
} catch (error) {
const message = error?.message ? ` (${error.message})` : "";
throw new MlldError(`Failed to parse JSON from ${sourceLabel}${message}`);
}
}
__name(parseJsonWithContext, "parseJsonWithContext");
function parseJsonLines(text, sourceLabel) {
const lines = text.split(/\r?\n/);
const results = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if (!line) continue;
try {
results.push(JSON.parse(line));
} catch (error) {
const message = error?.message ? ` (${error.message})` : "";
throw new MlldError(`Failed to parse JSONL from ${sourceLabel} at line ${i + 1}${message}`, {
line: i + 1,
offendingLine: line
});
}
}
return results;
}
__name(parseJsonLines, "parseJsonLines");
function isProbablyURL(input) {
return /^https?:\/\//i.test(input);
}
__name(isProbablyURL, "isProbablyURL");
function buildLoadSecurityDescriptor(result) {
if (!result.absolute || isProbablyURL(result.absolute)) {
return void 0;
}
const dirLabels = labelsForPath(result.absolute);
return makeSecurityDescriptor({
taint: [
"src:file",
...dirLabels
],
sources: [
result.absolute
]
});
}
__name(buildLoadSecurityDescriptor, "buildLoadSecurityDescriptor");
function buildMetadata(base, extra) {
if (!base && !extra) {
return void 0;
}
return {
...base || {},
...extra || {}
};
}
__name(buildMetadata, "buildMetadata");
function extractLoadContentMetadata(result) {
const metadata = {
source: "load-content",
filename: result.filename,
relative: result.relative,
absolute: result.absolute,
tokest: result.tokest,
tokens: result.tokens,
fm: result.fm,
json: result.json,
length: typeof result.content === "string" ? result.content.length : void 0,
metrics: {
tokens: result.tokens,
length: typeof result.content === "string" ? result.content.length : void 0
}
};
if ("url" in result && result.url) {
const urlResult = result;
metadata.url = urlResult.url;
metadata.domain = urlResult.domain;
if (urlResult.title) metadata.title = urlResult.title;
if (urlResult.description) metadata.description = urlResult.description;
if (urlResult.status !== void 0) metadata.status = urlResult.status;
if (urlResult.headers) metadata.headers = urlResult.headers;
}
if ("html" in result && !result.url) {
const htmlResult = result;
metadata.html = htmlResult.html;
if (htmlResult.title) metadata.title = htmlResult.title;
if (htmlResult.description) metadata.description = htmlResult.description;
}
const security = buildLoadSecurityDescriptor(result);
if (security) {
metadata.security = metadata.security ? mergeDescriptors(metadata.security, security) : security;
}
return metadata;
}
__name(extractLoadContentMetadata, "extractLoadContentMetadata");
function deriveArrayText(value) {
if (typeof value.toString === "function" && value.toString !== Array.prototype.toString) {
return value.toString();
}
if (value.length > 0 && isLoadContentResult(value[0])) {
return value.map((item) => item.content ?? "").join("\n\n");
}
try {
return JSON.stringify(value);
} catch {
return value.map((item) => String(item)).join("\n");
}
}
__name(deriveArrayText, "deriveArrayText");
function wrapLoadContentValue(value) {
if (isStructuredValue(value)) {
return value;
}
if (typeof value === "string") {
return wrapStructured(value, "text", value, {
source: "load-content"
});
}
if (isLoadContentResult(value)) {
const baseMetadata = extractLoadContentMetadata(value);
const contentText = typeof value.content === "string" ? value.content : String(value.content ?? "");
const filenameLower = (value.filename || "").toLowerCase();
if (filenameLower.endsWith(".jsonl") && typeof value.content === "string") {
const data = parseJsonLines(contentText, value.filename || "content");
const metadata = buildMetadata(baseMetadata, {
type: "jsonl"
});
return wrapStructured(data, "array", contentText, metadata);
}
if (filenameLower.endsWith(".json") && typeof value.content === "string") {
const data = parseJsonWithContext(contentText, value.filename || "content");
const metadata = buildMetadata(baseMetadata, {
type: "json"
});
return wrapStructured(data, detectStructuredType(data), contentText, metadata);
}
const parsedFromContent = tryParseJson(contentText);
if (parsedFromContent.success) {
const data = parsedFromContent.value;
return wrapStructured(data, detectStructuredType(data), contentText, baseMetadata);
}
const parsed = value.json;
if (parsed !== void 0) {
return wrapStructured(parsed, detectStructuredType(parsed), contentText, baseMetadata);
}
return wrapStructured(contentText, "text", contentText, baseMetadata);
}
if (Array.isArray(value)) {
const baseMetadata = {
source: "load-content",
length: value.length
};
const variableMetadata = hasVariableMetadata(value) ? getVariableMetadata(value) : void 0;
const aggregatedSecurity = value.length > 0 && isLoadContentResult(value[0]) ? value.map((item) => buildLoadSecurityDescriptor(item)).filter((descriptor) => Boolean(descriptor)) : [];
const mergedSecurity = aggregatedSecurity.length > 0 ? mergeDescriptors(...aggregatedSecurity) : void 0;
const metadata = buildMetadata(baseMetadata, variableMetadata?.mx ? {
variableMetadata: variableMetadata.mx
} : void 0);
const finalMetadata = mergedSecurity ? buildMetadata(metadata, {
security: mergedSecurity
}) : metadata;
return wrapStructured(value, "array", deriveArrayText(value), finalMetadata);
}
const fallbackText = typeof value === "string" ? value : (() => {
try {
return JSON.stringify(value);
} catch {
return String(value ?? "");
}
})();
return wrapStructured(value, "object", fallbackText, {
source: "load-content"
});
}
__name(wrapLoadContentValue, "wrapLoadContentValue");
function isFileLoadedValue(value) {
if (isStructuredValue(value)) {
return Boolean(value.mx?.filename || value.mx?.url);
}
return isLoadContentResult(value);
}
__name(isFileLoadedValue, "isFileLoadedValue");
function isURLLoadedValue(value) {
if (isStructuredValue(value)) {
return Boolean(value.mx?.url);
}
if (isLoadContentResult(value)) {
return Boolean(value.url);
}
return false;
}
__name(isURLLoadedValue, "isURLLoadedValue");
export { isFileLoadedValue, isURLLoadedValue, labelsForPath, wrapLoadContentValue };
//# sourceMappingURL=chunk-GLVJZ2CA.mjs.map
//# sourceMappingURL=chunk-GLVJZ2CA.mjs.map