File size: 971 Bytes
23b8b85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# chat_manager.py
from typing import Dict, List, Optional
from chat_state import ChatState


class ChatManager:
    def __init__(self, system_prompt: str):
        self.system_prompt = system_prompt

    def create_new_chat(self) -> List[Dict[str, str]]:
        return [{"role": "system", "content": self.system_prompt}]

    def save_chat(
        self,
        temp_chat: List[Dict[str, str]],
        chat_history: Dict[str, List[Dict[str, str]]],
    ) -> str:
        if temp_chat and len(temp_chat) > 1:
            new_chat_id = str(len(chat_history) + 1)
            chat_history[new_chat_id] = temp_chat
            return new_chat_id
        return None

    def delete_chat(self, chat_id: str, chat_state: ChatState) -> None:
        if chat_id in chat_state.chat_history:
            del chat_state.chat_history[chat_id]
        if chat_state.current_chat_id == chat_id:
            chat_state.current_chat_id = None
        chat_state.delete_chat_id = None