@ai2070/l0
Version:
L0: The Missing Reliability Substrate for AI
166 lines (165 loc) • 4.54 kB
JavaScript
function formatJsonOutput(options = {}) {
const {
includeInstructions = true,
strict = true,
schema,
example
} = options;
const parts = [];
if (includeInstructions) {
if (strict) {
parts.push(
"Respond with valid JSON only. Do not include any text before or after the JSON object."
);
parts.push("Do not wrap the JSON in markdown code blocks or backticks.");
parts.push("Start your response with { and end with }.");
} else {
parts.push("Respond with valid JSON.");
}
}
if (schema) {
parts.push("");
parts.push("Expected JSON schema:");
parts.push(schema);
}
if (example) {
parts.push("");
parts.push("Example output:");
parts.push(example);
}
return parts.join("\n");
}
function formatStructuredOutput(format, options = {}) {
const { strict = true, schema, example } = options;
const parts = [];
switch (format) {
case "json":
return formatJsonOutput({
includeInstructions: true,
strict,
schema,
example
});
case "yaml":
if (strict) {
parts.push("Respond with valid YAML only.");
parts.push("Do not include any text before or after the YAML.");
} else {
parts.push("Respond with valid YAML.");
}
break;
case "xml":
if (strict) {
parts.push("Respond with valid XML only.");
parts.push("Start with an XML declaration or root element.");
parts.push("Do not include any text before or after the XML.");
} else {
parts.push("Respond with valid XML.");
}
break;
case "markdown":
parts.push("Respond with well-formatted Markdown.");
if (strict) {
parts.push("Use proper Markdown syntax for all formatting.");
}
break;
case "plain":
parts.push("Respond with plain text only.");
if (strict) {
parts.push(
"Do not use any formatting, markdown, or special characters."
);
}
break;
}
if (schema) {
parts.push("");
parts.push(`Expected ${format.toUpperCase()} schema:`);
parts.push(schema);
}
if (example) {
parts.push("");
parts.push("Example output:");
parts.push(example);
}
return parts.join("\n");
}
function formatOutputConstraints(constraints) {
const parts = [];
if (constraints.maxLength) {
parts.push(`Keep your response under ${constraints.maxLength} characters.`);
}
if (constraints.minLength) {
parts.push(
`Provide at least ${constraints.minLength} characters in your response.`
);
}
if (constraints.noCodeBlocks) {
parts.push("Do not use code blocks or backticks.");
}
if (constraints.noMarkdown) {
parts.push("Do not use Markdown formatting.");
}
if (constraints.language) {
parts.push(`Respond in ${constraints.language}.`);
}
if (constraints.tone) {
parts.push(`Use a ${constraints.tone} tone.`);
}
return parts.join("\n");
}
function wrapOutputInstruction(instruction) {
return `<output_format>
${instruction}
</output_format>`;
}
function createOutputFormatSection(format, options = {}) {
const { wrap = true, constraints } = options;
const parts = [];
parts.push(formatStructuredOutput(format, options));
if (constraints) {
parts.push("");
parts.push(formatOutputConstraints(constraints));
}
const instruction = parts.join("\n");
return wrap ? wrapOutputInstruction(instruction) : instruction;
}
function extractJsonFromOutput(output) {
if (!output) return null;
const codeBlockMatch = output.match(/```(?:json)?\s*\n?([\s\S]*?)\n?```/);
if (codeBlockMatch && codeBlockMatch[1]) {
return codeBlockMatch[1].trim();
}
const jsonMatch = output.match(/(\{[\s\S]*\}|\[[\s\S]*\])/);
if (jsonMatch && jsonMatch[1]) {
return jsonMatch[1].trim();
}
return null;
}
function cleanOutput(output) {
if (!output) return output;
let cleaned = output.trim();
const prefixes = [
/^here is the .+?:?\s*/i,
/^here's the .+?:?\s*/i,
/^sure,?\s*/i,
/^certainly,?\s*/i,
/^of course,?\s*/i
];
for (const prefix of prefixes) {
cleaned = cleaned.replace(prefix, "");
}
cleaned = cleaned.replace(/^```(?:\w+)?\s*\n?/, "");
cleaned = cleaned.replace(/\n?```\s*$/, "");
return cleaned.trim();
}
export {
cleanOutput,
createOutputFormatSection,
extractJsonFromOutput,
formatJsonOutput,
formatOutputConstraints,
formatStructuredOutput,
wrapOutputInstruction
};
//# sourceMappingURL=output.js.map