/**
 * Shared helpers for the LangChain callback integration.
 */
export function nanoTimestamp() {
    return (BigInt(Date.now()) * 1000000n).toString();
}
export function extractModelName(serialized, extraParams) {
    const serKwargs = serialized["kwargs"];
    if (serKwargs != null && typeof serKwargs === "object") {
        const kw = serKwargs;
        if (typeof kw["model_name"] === "string")
            return kw["model_name"];
        if (typeof kw["model"] === "string")
            return kw["model"];
    }
    if (typeof serialized["name"] === "string")
        return serialized["name"];
    const inv = extraParams?.["invocation_params"];
    if (inv != null && typeof inv === "object") {
        const ip = inv;
        if (typeof ip["model_name"] === "string")
            return ip["model_name"];
        if (typeof ip["model"] === "string")
            return ip["model"];
    }
    return null;
}
export function extractModelParameters(extraParams) {
    const inv = extraParams?.["invocation_params"];
    if (inv == null || typeof inv !== "object")
        return {};
    const ip = inv;
    const params = {};
    const keys = [
        "temperature",
        "max_tokens",
        "top_p",
        "top_k",
        "frequency_penalty",
        "presence_penalty",
        "seed",
    ];
    for (const key of keys) {
        const value = ip[key];
        if (value != null) {
            params[key] = value;
        }
    }
    return params;
}
export function normalizeMessages(messages) {
    const result = [];
    for (const group of messages) {
        for (const msg of group) {
            const type = msg._getType();
            switch (type) {
                case "human":
                    result.push({ role: "user", content: msg.content });
                    break;
                case "system":
                    result.push({ role: "system", content: msg.content });
                    break;
                case "ai": {
                    const entry = {
                        role: "assistant",
                        content: msg.content,
                    };
                    const toolCalls = msg["tool_calls"] ??
                        msg.additional_kwargs?.["tool_calls"];
                    if (toolCalls) {
                        entry["tool_calls"] = toolCalls;
                    }
                    result.push(entry);
                    break;
                }
                case "tool":
                    result.push({
                        role: "tool",
                        content: msg.content,
                        tool_call_id: msg["tool_call_id"],
                    });
                    break;
                case "function":
                    result.push({
                        role: "function",
                        content: msg.content,
                        name: msg["name"],
                    });
                    break;
                case "chat":
                    result.push({
                        role: msg["role"],
                        content: msg.content,
                    });
                    break;
                default:
                    result.push({ role: "unknown", content: String(msg.content) });
            }
        }
    }
    return result;
}
export function extractAssistantToolCalls(message) {
    if (message == null || typeof message !== "object")
        return null;
    const msg = message;
    const additionalKwargs = msg["additional_kwargs"];
    if (additionalKwargs != null && typeof additionalKwargs === "object") {
        const fromKwargs = additionalKwargs["tool_calls"];
        if (Array.isArray(fromKwargs) && fromKwargs.length > 0) {
            return fromKwargs;
        }
    }
    const fromMessage = msg["tool_calls"];
    if (Array.isArray(fromMessage) && fromMessage.length > 0) {
        return fromMessage;
    }
    return null;
}
function readUsage(usage) {
    if (usage == null)
        return null;
    const promptTokens = usage["prompt_tokens"] ??
        usage["promptTokens"] ??
        usage["input_tokens"] ??
        usage["inputTokens"] ??
        0;
    const completionTokens = usage["completion_tokens"] ??
        usage["completionTokens"] ??
        usage["output_tokens"] ??
        usage["outputTokens"] ??
        0;
    const totalTokens = usage["total_tokens"] ??
        usage["totalTokens"] ??
        promptTokens + completionTokens;
    if (!promptTokens && !completionTokens && !totalTokens)
        return null;
    const result = {
        prompt_tokens: promptTokens,
        completion_tokens: completionTokens,
        total_tokens: totalTokens,
    };
    const promptDetails = usage["prompt_tokens_details"] ?? usage["input_token_details"];
    if (promptDetails != null && typeof promptDetails === "object") {
        const cached = promptDetails["cached_tokens"];
        if (cached != null) {
            result["cached_tokens"] = cached;
        }
    }
    const completionDetails = usage["completion_tokens_details"] ?? usage["output_token_details"];
    if (completionDetails != null && typeof completionDetails === "object") {
        const reasoning = completionDetails["reasoning_tokens"];
        if (reasoning != null) {
            result["reasoning_tokens"] = reasoning;
        }
    }
    return result;
}
export function extractTokenUsage(response) {
    const resp = response;
    if (resp == null)
        return null;
    const llmOutput = (resp["llmOutput"] ?? resp["llm_output"]);
    if (llmOutput != null && typeof llmOutput === "object") {
        const usage = (llmOutput["tokenUsage"] ??
            llmOutput["token_usage"] ??
            llmOutput["usage"] ??
            llmOutput["estimatedTokenUsage"]);
        const fromLlmOutput = readUsage(usage);
        if (fromLlmOutput)
            return fromLlmOutput;
    }
    const generations = resp["generations"];
    if (Array.isArray(generations)) {
        for (const group of generations) {
            if (!Array.isArray(group))
                continue;
            for (const gen of group) {
                const g = gen;
                if (g == null)
                    continue;
                const message = g["message"];
                const usageMeta = message?.["usage_metadata"] ??
                    message?.["usageMetadata"] ??
                    message?.["response_metadata"]?.["tokenUsage"] ??
                    message?.["response_metadata"]?.["usage"];
                const fromMessage = readUsage(usageMeta);
                if (fromMessage)
                    return fromMessage;
            }
        }
    }
    return null;
}
export function extractToolName(serialized) {
    if (!serialized)
        return null;
    const kwargs = serialized["kwargs"];
    if (kwargs != null && typeof kwargs === "object") {
        const kw = kwargs;
        if (typeof kw["name"] === "string" && kw["name"])
            return kw["name"];
    }
    if (typeof serialized["name"] === "string" && serialized["name"]) {
        return serialized["name"];
    }
    const idList = serialized["id"];
    if (Array.isArray(idList) && idList.length > 0) {
        const last = idList[idList.length - 1];
        if (typeof last === "string")
            return last;
    }
    return null;
}
export function stringifyToolOutput(output) {
    if (output == null)
        return "";
    if (typeof output === "string")
        return output;
    if (typeof output === "object") {
        const obj = output;
        if (typeof obj["content"] === "string")
            return obj["content"];
        if (obj["content"] != null) {
            try {
                return JSON.stringify(obj["content"]);
            }
            catch {
                return String(obj["content"]);
            }
        }
        try {
            return JSON.stringify(output);
        }
        catch {
            return String(output);
        }
    }
    return String(output);
}
export function resolveSpanName(name, metadata, serialized, fallback = "LangGraph") {
    if (name)
        return name;
    if (serialized) {
        if (typeof serialized["name"] === "string")
            return serialized["name"];
        const idList = serialized["id"];
        if (idList && idList.length > 0) {
            const last = idList[idList.length - 1];
            if (last != null)
                return last;
        }
    }
    if (metadata && typeof metadata["langgraph_node"] === "string") {
        return metadata["langgraph_node"];
    }
    return fallback;
}
export function formatError(error) {
    return {
        message: error.message,
        traceback: error.stack ?? "",
    };
}
//# sourceMappingURL=utils.mjs.map