File size: 1,631 Bytes
ab2012f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from config import CLOTHING_TYPES

conversation_memory = {}

def get_conversation_context(session_id: str) -> dict:
    if session_id not in conversation_memory:
        conversation_memory[session_id] = {
            "messages": [],
            "context": {}
        }
    return conversation_memory[session_id]

def update_context(session_id: str, message: str, response_data: dict):
    conv = conversation_memory[session_id]
    conv["messages"].append({"user": message, "assistant": response_data.get("response", "")})
    
    if len(conv["messages"]) > 10:
        conv["messages"] = conv["messages"][-10:]
    
    if "color" in response_data:
        conv["context"]["last_color"] = response_data["color"]
    if "item" in response_data:
        conv["context"]["last_item"] = response_data["item"]
    if "colors" in response_data:
        conv["context"]["last_colors"] = response_data["colors"]
    if "items" in response_data:
        conv["context"]["last_items"] = response_data["items"]

def enhance_message_with_context(message: str, context: dict) -> str:
    message_lower = message.lower()
    
    if "what about" in message_lower or "how about" in message_lower or "and" in message_lower:
        if context.get("last_color"):
            if not any(c in message_lower for c in ["color", "red", "blue", "green", "black", "white", "brown"]):
                message = message + f" with {context['last_color']}"
        if context.get("last_item"):
            if not any(item in message_lower for item in CLOTHING_TYPES):
                message = message + f" {context['last_item']}"
    
    return message