File size: 1,805 Bytes
7fc61e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
import json

def load_character_data():
    current_dir = os.path.dirname(os.path.abspath(__file__))
    json_path = os.path.join(current_dir, 'prompts/trump.character.json')
    
    with open(json_path, 'r') as file:
        return json.load(file)

def load_chat_history():
    current_dir = os.path.dirname(os.path.abspath(__file__))
    history_path = os.path.join(current_dir, 'chat_history.json')
    
    try:
        with open(history_path, 'r') as file:
            return json.load(file)
    except FileNotFoundError:
        return []

def update_chat_history(chat_history, user_message=None, character_response=None):
    # If this is a new interaction, create a new interaction number
    interaction_number = len(chat_history) + 1
    
    # If we're starting a new interaction with a user message
    if user_message and not character_response:
        interaction_key = f"interaction_{interaction_number}"
        new_interaction = {
            interaction_key: {
                "user": {"role": "user", "message": user_message},
                "trump": None
            }
        }
        chat_history.append(new_interaction)
    
    # If we're adding Trump's response to an existing interaction
    elif character_response:
        # Get the last interaction number (current one)
        interaction_key = f"interaction_{len(chat_history)}"
        current_interaction = chat_history[-1][interaction_key]
        current_interaction["trump"] = {"role": "Trump", "message": character_response}
    
    return chat_history

def save_chat_history(history):
    current_dir = os.path.dirname(os.path.abspath(__file__))
    history_path = os.path.join(current_dir, 'chat_history.json')
    
    with open(history_path, 'w') as file:
        json.dump(history, file, indent=2)