File size: 989 Bytes
0df80b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from __future__ import annotations

from src.config import get_settings
from src.schemas import ChatHistoryMessage
from src.utils.agent_utils import redact_personal_info


class MessageBuilder:
    @staticmethod
    def trim_history(history: list[ChatHistoryMessage]) -> list[ChatHistoryMessage]:
        settings = get_settings()
        if len(history) <= settings.conversation_history_window:
            return history

        trimmed = history[-settings.conversation_history_window :]
        while trimmed and trimmed[0].role == "assistant":
            trimmed = trimmed[1:]
        return trimmed

    @staticmethod
    def build_input_items(history: list[ChatHistoryMessage], question: str) -> list[dict[str, str]]:
        trimmed = MessageBuilder.trim_history(history)
        items = [{"role": item.role, "content": redact_personal_info(item.content)} for item in trimmed]
        items.append({"role": "user", "content": redact_personal_info(question)})
        return items