import { generateTextWithModelId } from "./generate-text.js";
const MAX_HTML_INPUT_CHARACTERS = 200_000;
function buildHtmlToMarkdownPrompt({ url, title, siteName, html, }) {
    const system = `You convert HTML into clean GitHub-Flavored Markdown.

Rules:
- Output ONLY Markdown (no JSON, no explanations, no code fences).
- Keep headings, lists, code blocks, blockquotes.
- Preserve links as Markdown links when possible.
- Remove navigation, cookie banners, footers, and unrelated page chrome.
- Do not invent content.`;
    const prompt = `URL: ${url}
Site: ${siteName ?? "unknown"}
Title: ${title ?? "unknown"}

HTML:
"""
${html}
"""
`;
    return { system, prompt };
}
export function createHtmlToMarkdownConverter({ modelId, forceOpenRouter, xaiApiKey, googleApiKey, openaiApiKey, openaiBaseUrlOverride, ollamaBaseUrlOverride, anthropicBaseUrlOverride, googleBaseUrlOverride, xaiBaseUrlOverride, anthropicApiKey, openrouterApiKey, fetchImpl, forceChatCompletions, requestOptions, retries = 0, onRetry, onUsage, }) {
    return async ({ url, html, title, siteName, timeoutMs }) => {
        const trimmedHtml = html.length > MAX_HTML_INPUT_CHARACTERS ? html.slice(0, MAX_HTML_INPUT_CHARACTERS) : html;
        const { system, prompt } = buildHtmlToMarkdownPrompt({
            url,
            title,
            siteName,
            html: trimmedHtml,
        });
        const result = await generateTextWithModelId({
            modelId,
            apiKeys: { xaiApiKey, googleApiKey, openaiApiKey, anthropicApiKey, openrouterApiKey },
            forceOpenRouter,
            openaiBaseUrlOverride,
            ollamaBaseUrlOverride,
            anthropicBaseUrlOverride,
            googleBaseUrlOverride,
            xaiBaseUrlOverride,
            forceChatCompletions,
            requestOptions,
            prompt: { system, userText: prompt },
            timeoutMs,
            fetchImpl,
            retries,
            onRetry,
        });
        onUsage?.({ model: result.canonicalModelId, provider: result.provider, usage: result.usage });
        return result.text;
    };
}
//# sourceMappingURL=html-to-markdown.js.map