File size: 570 Bytes
f39ba75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# agents/agent_helpers.py

def format_history_for_prompt(chat_history: list = None) -> str:
    """Formats the chat history list into a string for the AI prompt."""
    if not chat_history:
        return ""
    
    history_for_prompt = ""
    for turn in chat_history:
        #  Ensure 'parts' is a list and not empty before accessing
        if turn.get('parts') and isinstance(turn.get('parts'), list):
            role = "User" if turn['role'] == 'user' else "AI"
            history_for_prompt += f"{role}: {turn['parts'][0]}\n"
    
    return history_for_prompt