@ai2070/l0
Version:
L0: The Missing Reliability Substrate for AI
104 lines • 3.6 kB
JavaScript
import { normalizeForModel, dedent } from "../utils/normalize";
export function formatContext(content, options = {}) {
if (!content || content.trim().length === 0) {
return "";
}
const { label = "Context", dedent: shouldDedent = true, normalize = true, delimiter = "xml", customDelimiterStart, customDelimiterEnd, } = options;
let processed = content;
if (normalize) {
processed = normalizeForModel(processed);
}
if (shouldDedent) {
processed = dedent(processed);
}
if (customDelimiterStart && customDelimiterEnd) {
return `${customDelimiterStart}\n${processed}\n${customDelimiterEnd}`;
}
switch (delimiter) {
case "xml":
return formatXmlContext(processed, label);
case "markdown":
return formatMarkdownContext(processed, label);
case "brackets":
return formatBracketContext(processed, label);
case "none":
return processed;
default:
return formatXmlContext(processed, label);
}
}
function formatXmlContext(content, label) {
const tag = label.toLowerCase().replace(/\s+/g, "_");
return `<${tag}>\n${content}\n</${tag}>`;
}
function formatMarkdownContext(content, label) {
return `# ${label}\n\n${content}`;
}
function formatBracketContext(content, label) {
const delimiter = "=".repeat(Math.max(20, label.length + 10));
return `[${label.toUpperCase()}]\n${delimiter}\n${content}\n${delimiter}`;
}
export function formatMultipleContexts(items, options = {}) {
const formatted = items
.filter((item) => item.content && item.content.trim().length > 0)
.map((item) => formatContext(item.content, {
...options,
label: item.label || options.label,
}));
return formatted.join("\n\n");
}
export function formatDocument(content, metadata, options = {}) {
if (!content || content.trim().length === 0) {
return "";
}
let result = "";
if (metadata && Object.keys(metadata).length > 0) {
const metaLines = Object.entries(metadata)
.filter(([_, value]) => value && value.trim().length > 0)
.map(([key, value]) => `${key}: ${value}`);
if (metaLines.length > 0) {
result += metaLines.join("\n") + "\n\n";
}
}
result += content;
return formatContext(result, {
label: metadata?.title || "Document",
...options,
});
}
export function formatInstructions(instructions, options = {}) {
return formatContext(instructions, {
label: "Instructions",
delimiter: "xml",
...options,
});
}
export function escapeDelimiters(content, delimiter = "xml") {
if (!content)
return content;
switch (delimiter) {
case "xml":
return content.replace(/</g, "<").replace(/>/g, ">");
case "markdown":
return content.replace(/^(#{1,6})\s/gm, "\\$1 ");
case "brackets":
return content.replace(/\[/g, "\\[").replace(/\]/g, "\\]");
default:
return content;
}
}
export function unescapeDelimiters(content, delimiter = "xml") {
if (!content)
return content;
switch (delimiter) {
case "xml":
return content.replace(/</g, "<").replace(/>/g, ">");
case "markdown":
return content.replace(/\\(#{1,6})\s/g, "$1 ");
case "brackets":
return content.replace(/\\\[/g, "[").replace(/\\\]/g, "]");
default:
return content;
}
}
//# sourceMappingURL=context.js.map