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