import { isTextAttachment, resolveAttachmentFileName, TEXT_ATTACHMENT_MIME_TYPES, } from "@superblocksteam/library-shared/types";
const TEXT_LIKE_EXTENSIONS = [
    ".txt",
    ".text",
    ".log",
    ".md",
    ".markdown",
    ".csv",
    ".tsv",
    ".json",
    ".jsonl",
    ".ndjson",
    ".xml",
    ".yaml",
    ".yml",
    ".toml",
    ".ini",
    ".cfg",
    ".conf",
    ".env",
    ".sh",
    ".bash",
    ".zsh",
    ".js",
    ".jsx",
    ".ts",
    ".tsx",
    ".mts",
    ".cts",
    ".py",
    ".go",
    ".java",
    ".rb",
    ".php",
    ".sql",
    ".css",
    ".scss",
    ".html",
    ".htm",
];
export function inferTextAttachmentType(params) {
    const attachmentType = typeof params.attachmentType === "string"
        ? params.attachmentType.toLowerCase()
        : "";
    const mimeType = typeof params.mimeType === "string" ? params.mimeType.toLowerCase() : "";
    const lowerFileName = (params.fileName ?? "").toLowerCase();
    if (attachmentType === "csv" || mimeType === "text/csv") {
        return "csv";
    }
    if (attachmentType === "json" || mimeType === "application/json") {
        return "json";
    }
    if (attachmentType === "yaml" ||
        mimeType === "text/yaml" ||
        mimeType === "application/yaml" ||
        mimeType === "application/x-yaml") {
        return "yaml";
    }
    if (attachmentType === "css" || mimeType === "text/css") {
        return "css";
    }
    if (attachmentType === "txt") {
        return "txt";
    }
    if (lowerFileName.endsWith(".csv")) {
        return "csv";
    }
    if (lowerFileName.endsWith(".json")) {
        return "json";
    }
    if (lowerFileName.endsWith(".yaml") || lowerFileName.endsWith(".yml")) {
        return "yaml";
    }
    if (lowerFileName.endsWith(".css")) {
        return "css";
    }
    if (mimeType.startsWith("text/") ||
        mimeType.includes("json") ||
        mimeType.includes("xml") ||
        mimeType.includes("yaml") ||
        mimeType.includes("toml") ||
        mimeType.includes("javascript") ||
        mimeType.includes("typescript") ||
        mimeType.includes("ecmascript") ||
        mimeType.includes("markdown") ||
        mimeType.includes("shellscript") ||
        mimeType.includes("python") ||
        mimeType.includes("ruby") ||
        mimeType.includes("php") ||
        mimeType.includes("sql") ||
        TEXT_LIKE_EXTENSIONS.some((extension) => lowerFileName.endsWith(extension))) {
        return "txt";
    }
    return null;
}
export function normalizeTextAttachmentForModel(attachment) {
    if (isTextAttachment(attachment)) {
        return {
            fileName: resolveAttachmentFileName({
                fileName: attachment.fileName,
                mimeType: attachment.mimeType,
                type: attachment.type,
            }),
            content: attachment.content,
            mimeType: attachment.mimeType,
            tagName: attachment.type,
        };
    }
    const candidate = attachment;
    if (typeof candidate.content !== "string") {
        return null;
    }
    const fileNameForInfer = typeof candidate.fileName === "string" ? candidate.fileName : "";
    const tagName = inferTextAttachmentType({
        attachmentType: candidate.type,
        mimeType: candidate.mimeType,
        fileName: fileNameForInfer,
    });
    if (!tagName) {
        return null;
    }
    return {
        fileName: resolveAttachmentFileName({
            fileName: typeof candidate.fileName === "string" ? candidate.fileName : undefined,
            mimeType: typeof candidate.mimeType === "string"
                ? candidate.mimeType
                : TEXT_ATTACHMENT_MIME_TYPES[tagName],
            type: tagName,
        }),
        content: candidate.content,
        mimeType: typeof candidate.mimeType === "string"
            ? candidate.mimeType
            : TEXT_ATTACHMENT_MIME_TYPES[tagName],
        tagName,
    };
}
export function sanitizeToolOutput(toolName, output) {
    if (toolName === "executeRequestToIntegration" || toolName === "testApi") {
        // do not record user/API result data in the DB
        return undefined;
    }
    return output;
}
const MAX_TITLE_LENGTH = 200;
const MAX_PARSE_SIZE = 50000;
function escapeHtml(text) {
    return text
        .replace(/&/g, "&amp;")
        .replace(/</g, "&lt;")
        .replace(/>/g, "&gt;")
        .replace(/"/g, "&quot;")
        .replace(/'/g, "&#x27;");
}
/**
 * Sanitizes message content that may contain HTML error responses from AI
 * providers. Extracts a human-readable title when possible, otherwise returns
 * a generic error string. Non-HTML content is returned unchanged.
 */
export function sanitizeAiProviderResponse(content) {
    if (!content || typeof content !== "string") {
        return content;
    }
    try {
        const trimmed = content.trim();
        const looksLikeHtml = trimmed.startsWith("<html") ||
            trimmed.startsWith("<HTML") ||
            trimmed.startsWith("<!DOCTYPE");
        if (!looksLikeHtml) {
            return content;
        }
        if (content.length > MAX_PARSE_SIZE) {
            return content;
        }
        if (content.includes("```html") || content.includes("```HTML")) {
            return content;
        }
        const titleMatch = content.match(/<title[^>]*>(.*?)<\/title>/is);
        if (titleMatch?.[1]) {
            const title = titleMatch[1].trim();
            const sanitizedTitle = escapeHtml(title.length > MAX_TITLE_LENGTH
                ? title.substring(0, MAX_TITLE_LENGTH) + "..."
                : title);
            return `⚠️ AI Provider Error: ${sanitizedTitle}`;
        }
        return `⚠️ AI Provider Error\n\nThe AI provider returned an error.`;
    }
    catch {
        return content;
    }
}
//# sourceMappingURL=utils.js.map