/** User message + following assistant replies until the next user message. */
export const AGENT_HISTORY_INITIAL_TURNS = 5;
export const AGENT_HISTORY_TURN_PAGE = 5;
export function splitChatTurns(messages) {
    if (!messages.length)
        return [];
    const turns = [];
    let current = [];
    for (const m of messages) {
        if (m.role === 'user') {
            if (current.length)
                turns.push(current);
            current = [m];
        }
        else {
            current.push(m);
        }
    }
    if (current.length)
        turns.push(current);
    return turns;
}
export function sliceMessagesByTurns(messages, opts) {
    const turns = splitChatTurns(messages);
    const totalTurns = turns.length;
    const limit = Math.max(1, Math.floor(opts.turnLimit));
    const offset = Math.max(0, Math.floor(opts.turnOffset ?? 0));
    const end = Math.max(0, totalTurns - offset);
    const start = Math.max(0, end - limit);
    return {
        messages: turns.slice(start, end).flat(),
        totalTurns,
        hasMoreOlder: start > 0,
        loadedTurns: end - start,
    };
}
