export function nonEmptyString(value) {
    return typeof value === "string" && value.length > 0 ? value : undefined;
}
const CODEX_SHELL_ENVELOPE_HEADER_LINES = new Set([
    "chunk id:",
    "wall time:",
    "process exited with code",
    "original token count:",
]);
function isCodexShellEnvelopeHeaderLine(line) {
    const normalized = line.trim().toLowerCase();
    for (const prefix of CODEX_SHELL_ENVELOPE_HEADER_LINES) {
        if (normalized.startsWith(prefix)) {
            return true;
        }
    }
    return false;
}
function looksLikeCodexShellEnvelope(lines) {
    if (lines.length === 0) {
        return false;
    }
    const first = lines[0]?.trim().toLowerCase() ?? "";
    if (!first.startsWith("chunk id:")) {
        return false;
    }
    const headerWindow = lines.slice(0, 8).map((line) => line.trim().toLowerCase());
    const hasWallTime = headerWindow.some((line) => line.startsWith("wall time:"));
    const hasExitCode = headerWindow.some((line) => line.startsWith("process exited with code"));
    return hasWallTime && hasExitCode;
}
export function extractCodexShellOutput(value) {
    const text = nonEmptyString(value);
    if (!text) {
        return undefined;
    }
    const normalized = text.replace(/\r\n/g, "\n");
    const lines = normalized.split("\n");
    if (!looksLikeCodexShellEnvelope(lines)) {
        return text;
    }
    const outputLineIndex = lines.findIndex((line) => line.trim() === "Output:");
    if (outputLineIndex >= 0) {
        return nonEmptyString(lines.slice(outputLineIndex + 1).join("\n"));
    }
    let firstBodyLineIndex = -1;
    for (let index = 1; index < lines.length; index += 1) {
        const line = lines[index] ?? "";
        if (!isCodexShellEnvelopeHeaderLine(line)) {
            firstBodyLineIndex = index;
            break;
        }
    }
    if (firstBodyLineIndex === -1) {
        return undefined;
    }
    return nonEmptyString(lines.slice(firstBodyLineIndex).join("\n"));
}
export function extractCodexTerminalSessionId(value) {
    const text = nonEmptyString(value);
    if (!text) {
        return undefined;
    }
    const match = text.match(/process running with session id\s+([A-Za-z0-9._:-]+)/i);
    return nonEmptyString(match?.[1]);
}
export function flattenReadContent(value) {
    if (typeof value === "string") {
        return nonEmptyString(value);
    }
    if (!value) {
        return undefined;
    }
    if (Array.isArray(value)) {
        const parts = value
            .map((chunk) => nonEmptyString(chunk.text) ??
            nonEmptyString(chunk.content) ??
            nonEmptyString(chunk.output))
            .filter((part) => typeof part === "string");
        return parts.length > 0 ? parts.join("\n") : undefined;
    }
    return (nonEmptyString(value.text) ?? nonEmptyString(value.content) ?? nonEmptyString(value.output));
}
export function truncateDiffText(text, maxChars = 12000) {
    if (typeof text !== "string") {
        return undefined;
    }
    if (text.length <= maxChars) {
        return text;
    }
    const truncatedCount = text.length - maxChars;
    return `${text.slice(0, maxChars)}\n...[truncated ${truncatedCount} chars]`;
}
function hashText(value) {
    let hash = 0;
    for (let i = 0; i < value.length; i += 1) {
        hash = (hash << 5) - hash + value.charCodeAt(i);
        hash |= 0;
    }
    return Math.abs(hash).toString(36);
}
export function coerceToolCallId(params) {
    if (typeof params.rawCallId === "string" && params.rawCallId.trim().length > 0) {
        return params.rawCallId;
    }
    let serialized = "";
    try {
        serialized = JSON.stringify(params.input) ?? "";
    }
    catch {
        serialized = String(params.input);
    }
    return `${params.providerPrefix}-${hashText(`${params.toolName}:${serialized}`)}`;
}
//# sourceMappingURL=tool-call-mapper-utils.js.map