Spaces:
Sleeping
Sleeping
Manoj Thapa commited on
Commit ·
e59469c
1
Parent(s): 5609b13
Fix: Strip internal metadata tags (Language/Mode) before saving message to history
Browse files- backend/main.py +28 -27
backend/main.py
CHANGED
|
@@ -98,40 +98,41 @@ async def chat(request: ChatRequest):
|
|
| 98 |
for msg in history
|
| 99 |
]
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
async def generate():
|
| 105 |
"""Generate streaming response."""
|
| 106 |
full_response = ""
|
| 107 |
tool_calls = []
|
| 108 |
|
| 109 |
-
#
|
| 110 |
-
mode = "web"
|
| 111 |
-
clean_message = request.message
|
| 112 |
-
|
| 113 |
-
if "[Mode: Chat]" in request.message:
|
| 114 |
-
mode = "chat"
|
| 115 |
-
clean_message = request.message.replace("[Mode: Chat]", "").strip()
|
| 116 |
-
elif "[Mode: Web]" in request.message:
|
| 117 |
-
mode = "web"
|
| 118 |
-
clean_message = request.message.replace("[Mode: Web]", "").strip()
|
| 119 |
|
| 120 |
-
# Detect and extract language preference
|
| 121 |
-
# Format: [Language: Nepali]
|
| 122 |
-
language = "English"
|
| 123 |
-
import re
|
| 124 |
-
lang_match = re.search(r'\[Language: (.*?)\]', clean_message)
|
| 125 |
-
if lang_match:
|
| 126 |
-
language = lang_match.group(1)
|
| 127 |
-
clean_message = clean_message.replace(lang_match.group(0), "").strip()
|
| 128 |
-
|
| 129 |
-
# Also handle legacy format just in case
|
| 130 |
-
elif "[Focus Mode: Chat Only]" in request.message:
|
| 131 |
-
mode = "chat"
|
| 132 |
-
# Strip the heavy-handed legacy tag
|
| 133 |
-
clean_message = re.sub(r'\[Focus Mode:.*?\]', '', request.message).strip()
|
| 134 |
-
|
| 135 |
async for chunk in agent_runner.run(
|
| 136 |
user_message=clean_message,
|
| 137 |
conversation_history=conversation_history,
|
|
|
|
| 98 |
for msg in history
|
| 99 |
]
|
| 100 |
|
| 101 |
+
# --- PARSING LOGIC MOVED UP ---
|
| 102 |
+
mode = "web"
|
| 103 |
+
clean_message = request.message
|
| 104 |
+
language = "English"
|
| 105 |
+
|
| 106 |
+
# Detect mode
|
| 107 |
+
if "[Mode: Chat]" in request.message:
|
| 108 |
+
mode = "chat"
|
| 109 |
+
clean_message = request.message.replace("[Mode: Chat]", "").strip()
|
| 110 |
+
elif "[Mode: Web]" in request.message:
|
| 111 |
+
mode = "web"
|
| 112 |
+
clean_message = request.message.replace("[Mode: Web]", "").strip()
|
| 113 |
+
|
| 114 |
+
# Detect Language
|
| 115 |
+
import re
|
| 116 |
+
lang_match = re.search(r'\[Language: (.*?)\]', clean_message)
|
| 117 |
+
if lang_match:
|
| 118 |
+
language = lang_match.group(1)
|
| 119 |
+
clean_message = clean_message.replace(lang_match.group(0), "").strip()
|
| 120 |
+
|
| 121 |
+
# Handle legacy format
|
| 122 |
+
elif "[Focus Mode: Chat Only]" in request.message:
|
| 123 |
+
mode = "chat"
|
| 124 |
+
clean_message = re.sub(r'\[Focus Mode:.*?\]', '', request.message).strip()
|
| 125 |
+
|
| 126 |
+
# --- SAVE CLEAN MESSAGE ---
|
| 127 |
+
memory.add_message(conversation_id, "user", clean_message)
|
| 128 |
|
| 129 |
async def generate():
|
| 130 |
"""Generate streaming response."""
|
| 131 |
full_response = ""
|
| 132 |
tool_calls = []
|
| 133 |
|
| 134 |
+
# Use captured variables from outer scope: mode, clean_message, language
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
async for chunk in agent_runner.run(
|
| 137 |
user_message=clean_message,
|
| 138 |
conversation_history=conversation_history,
|