export const groupConsecutiveMessages = (agentMessages) => {
    const allMessages = agentMessages;
    const groups = [];
    let currentAssistantGroup = [];
    for (const message of allMessages) {
        if (message.role === "user") {
            if (currentAssistantGroup.length > 0) {
                groups.push({
                    type: "assistant-group",
                    messages: currentAssistantGroup,
                });
                currentAssistantGroup = [];
            }
            groups.push({ type: "user", messages: [message] });
        }
        else if (message.messageType === "heartbeat" ||
            message.role === "system") {
            if (currentAssistantGroup.length > 0) {
                groups.push({
                    type: "assistant-group",
                    messages: currentAssistantGroup,
                });
                currentAssistantGroup = [];
            }
            groups.push({ type: "heartbeat", messages: [message] });
        }
        else if (message.role === "assistant") {
            currentAssistantGroup.push(message);
        }
    }
    if (currentAssistantGroup.length > 0) {
        groups.push({ type: "assistant-group", messages: currentAssistantGroup });
    }
    return groups;
};
export const mergeMessagesById = (dbMessages, localMessages) => {
    const messageMap = new Map();
    const keyOf = (id) => (id ? id.toLowerCase() : "");
    dbMessages.forEach((msg) => {
        if (msg.id)
            messageMap.set(keyOf(msg.id), msg);
    });
    localMessages.forEach((localMsg) => {
        if (!localMsg.id)
            return;
        const key = keyOf(localMsg.id);
        const existingMsg = messageMap.get(key);
        if (!existingMsg) {
            messageMap.set(key, localMsg);
        }
        else if (!localMsg.isCompleted && localMsg.messageType === "streaming") {
            const localLen = (localMsg.content?.length || 0) + (localMsg.reasoning?.length || 0);
            const existingLen = (existingMsg.content?.length || 0) +
                (existingMsg.reasoning?.length || 0);
            if (localLen > existingLen) {
                messageMap.set(key, localMsg);
            }
        }
    });
    return Array.from(messageMap.values()).sort((a, b) => {
        const aIndex = a.messageIndex === -1 ? Infinity : a.messageIndex;
        const bIndex = b.messageIndex === -1 ? Infinity : b.messageIndex;
        if (aIndex !== bIndex) {
            return aIndex - bIndex;
        }
        return (new Date(a.createdDate || 0).getTime() -
            new Date(b.createdDate || 0).getTime());
    });
};
export const calculateTotalTokens = (messages) => {
    const totals = messages.reduce((acc, message) => {
        return {
            input: acc.input + (message.inputTokens || 0),
            output: acc.output + (message.outputTokens || 0),
            cached: acc.cached + (message.cachedInputTokens || 0),
            cacheWrite: acc.cacheWrite,
            inputCost: acc.inputCost + (message.inputTokenCost || 0),
            outputCost: acc.outputCost + (message.outputTokenCost || 0),
            cachedCost: acc.cachedCost + (message.cachedInputTokenCost || 0),
            cacheWriteCost: acc.cacheWriteCost,
            imageCost: acc.imageCost,
            totalCost: acc.totalCost + (message.totalCost || 0),
        };
    }, {
        input: 0,
        output: 0,
        cached: 0,
        cacheWrite: 0,
        inputCost: 0,
        outputCost: 0,
        cachedCost: 0,
        cacheWriteCost: 0,
        imageCost: 0,
        totalCost: 0,
    });
    return totals;
};
export const getOperationsForMessageGroup = (messages, agentOperations) => {
    const toolCallIds = new Set(messages.flatMap((m) => m.tool_calls?.map((tc) => tc.id) ?? []));
    const matched = agentOperations.filter((op) => op.toolCallId && toolCallIds.has(op.toolCallId));
    return matched;
};
//# sourceMappingURL=agentMessageGrouping.js.map