UNPKG

@ai2070/l0

Version:

L0: The Missing Reliability Substrate for AI

125 lines (123 loc) 3.4 kB
import { normalizeForModel, dedent } from "../utils/normalize"; 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} ${processed} ${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}> ${content} </${tag}>`; } function formatMarkdownContext(content, label) { return `# ${label} ${content}`; } function formatBracketContext(content, label) { const delimiter = "=".repeat(Math.max(20, label.length + 10)); return `[${label.toUpperCase()}] ${delimiter} ${content} ${delimiter}`; } 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"); } 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 }); } function formatInstructions(instructions, options = {}) { return formatContext(instructions, { label: "Instructions", delimiter: "xml", ...options }); } function escapeDelimiters(content, delimiter = "xml") { if (!content) return content; switch (delimiter) { case "xml": return content.replace(/</g, "&lt;").replace(/>/g, "&gt;"); case "markdown": return content.replace(/^(#{1,6})\s/gm, "\\$1 "); case "brackets": return content.replace(/\[/g, "\\[").replace(/\]/g, "\\]"); default: return content; } } function unescapeDelimiters(content, delimiter = "xml") { if (!content) return content; switch (delimiter) { case "xml": return content.replace(/&lt;/g, "<").replace(/&gt;/g, ">"); case "markdown": return content.replace(/\\(#{1,6})\s/g, "$1 "); case "brackets": return content.replace(/\\\[/g, "[").replace(/\\\]/g, "]"); default: return content; } } export { escapeDelimiters, formatContext, formatDocument, formatInstructions, formatMultipleContexts, unescapeDelimiters }; //# sourceMappingURL=context.js.map