Spaces:
Sleeping
Sleeping
Update chat_handler.py
Browse files- chat_handler.py +57 -30
chat_handler.py
CHANGED
|
@@ -230,27 +230,41 @@ class ChatHandler:
|
|
| 230 |
current_tool_id = None
|
| 231 |
current_server_name = None
|
| 232 |
tool_start_time = None
|
| 233 |
-
|
| 234 |
|
| 235 |
# Process Claude's response
|
| 236 |
for content in response.content:
|
| 237 |
if content.type == "text":
|
| 238 |
-
|
|
|
|
| 239 |
# Check if Claude indicated media was generated
|
| 240 |
-
if "MEDIA_GENERATED:" in
|
| 241 |
-
media_match = re.search(r"MEDIA_GENERATED:\s*([^\s]+)",
|
| 242 |
if media_match:
|
| 243 |
media_url = media_match.group(1)
|
| 244 |
# Clean up the response text
|
| 245 |
-
|
| 246 |
logger.info(f"π― Claude indicated media generated: {media_url}")
|
| 247 |
# Add media as separate message
|
| 248 |
chat_messages.append(ChatMessage(
|
| 249 |
role="assistant",
|
| 250 |
content={"path": media_url}
|
| 251 |
))
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
elif hasattr(content, 'type') and content.type == "mcp_tool_use":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
tool_name = content.name
|
| 255 |
server_name = content.server_name
|
| 256 |
current_tool_id = getattr(content, 'id', 'unknown')
|
|
@@ -287,27 +301,14 @@ class ChatHandler:
|
|
| 287 |
msg.metadata["duration"] = round(duration, 2)
|
| 288 |
break
|
| 289 |
|
|
|
|
| 290 |
if content.content:
|
| 291 |
result_content = content.content[0]
|
| 292 |
result_text = result_content.text if hasattr(result_content, 'text') else str(result_content)
|
| 293 |
|
| 294 |
logger.info(f"π MCP tool result: {result_text[:200]}...")
|
| 295 |
|
| 296 |
-
#
|
| 297 |
-
result_msg = ChatMessage(
|
| 298 |
-
role="assistant",
|
| 299 |
-
content=result_text,
|
| 300 |
-
metadata={
|
| 301 |
-
"title": "π Tool Result",
|
| 302 |
-
"parent_id": current_tool_id,
|
| 303 |
-
"status": "done"
|
| 304 |
-
}
|
| 305 |
-
)
|
| 306 |
-
|
| 307 |
-
chat_messages.append(result_msg)
|
| 308 |
-
|
| 309 |
-
# Try to extract media from the result
|
| 310 |
-
media_url = None
|
| 311 |
if current_server_name and current_server_name in self.mcp_client.servers:
|
| 312 |
config = self.mcp_client.servers[current_server_name]
|
| 313 |
extracted_media = self.mcp_client._extract_media_from_mcp_response(result_text, config)
|
|
@@ -323,8 +324,31 @@ class ChatHandler:
|
|
| 323 |
logger.info(f"π― Extracted media from MCP result (fallback): {media_url}")
|
| 324 |
break
|
| 325 |
|
| 326 |
-
#
|
| 327 |
-
if media_url:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
chat_messages.append(ChatMessage(
|
| 329 |
role="assistant",
|
| 330 |
content={"path": media_url}
|
|
@@ -342,14 +366,17 @@ class ChatHandler:
|
|
| 342 |
}
|
| 343 |
))
|
| 344 |
|
| 345 |
-
# Add
|
| 346 |
-
if
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
|
|
|
|
|
|
|
|
|
| 353 |
chat_messages.append(ChatMessage(
|
| 354 |
role="assistant",
|
| 355 |
content="I understand your request and I'm here to help."
|
|
|
|
| 230 |
current_tool_id = None
|
| 231 |
current_server_name = None
|
| 232 |
tool_start_time = None
|
| 233 |
+
text_segments = [] # Collect text segments separately
|
| 234 |
|
| 235 |
# Process Claude's response
|
| 236 |
for content in response.content:
|
| 237 |
if content.type == "text":
|
| 238 |
+
# Collect text segments but don't combine them yet
|
| 239 |
+
text_content = content.text
|
| 240 |
# Check if Claude indicated media was generated
|
| 241 |
+
if "MEDIA_GENERATED:" in text_content:
|
| 242 |
+
media_match = re.search(r"MEDIA_GENERATED:\s*([^\s]+)", text_content)
|
| 243 |
if media_match:
|
| 244 |
media_url = media_match.group(1)
|
| 245 |
# Clean up the response text
|
| 246 |
+
text_content = re.sub(r"MEDIA_GENERATED:\s*[^\s]+", "", text_content).strip()
|
| 247 |
logger.info(f"π― Claude indicated media generated: {media_url}")
|
| 248 |
# Add media as separate message
|
| 249 |
chat_messages.append(ChatMessage(
|
| 250 |
role="assistant",
|
| 251 |
content={"path": media_url}
|
| 252 |
))
|
| 253 |
+
|
| 254 |
+
if text_content.strip():
|
| 255 |
+
text_segments.append(text_content.strip())
|
| 256 |
|
| 257 |
elif hasattr(content, 'type') and content.type == "mcp_tool_use":
|
| 258 |
+
# Add any accumulated text before tool use
|
| 259 |
+
if text_segments:
|
| 260 |
+
combined_text = " ".join(text_segments)
|
| 261 |
+
if combined_text.strip():
|
| 262 |
+
chat_messages.append(ChatMessage(
|
| 263 |
+
role="assistant",
|
| 264 |
+
content=combined_text.strip()
|
| 265 |
+
))
|
| 266 |
+
text_segments = [] # Reset
|
| 267 |
+
|
| 268 |
tool_name = content.name
|
| 269 |
server_name = content.server_name
|
| 270 |
current_tool_id = getattr(content, 'id', 'unknown')
|
|
|
|
| 301 |
msg.metadata["duration"] = round(duration, 2)
|
| 302 |
break
|
| 303 |
|
| 304 |
+
media_url = None
|
| 305 |
if content.content:
|
| 306 |
result_content = content.content[0]
|
| 307 |
result_text = result_content.text if hasattr(result_content, 'text') else str(result_content)
|
| 308 |
|
| 309 |
logger.info(f"π MCP tool result: {result_text[:200]}...")
|
| 310 |
|
| 311 |
+
# Try to extract media from the result first
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
if current_server_name and current_server_name in self.mcp_client.servers:
|
| 313 |
config = self.mcp_client.servers[current_server_name]
|
| 314 |
extracted_media = self.mcp_client._extract_media_from_mcp_response(result_text, config)
|
|
|
|
| 324 |
logger.info(f"π― Extracted media from MCP result (fallback): {media_url}")
|
| 325 |
break
|
| 326 |
|
| 327 |
+
# Only show tool result if no media was found or if it's not just file data
|
| 328 |
+
if not media_url:
|
| 329 |
+
# No media found, show the result
|
| 330 |
+
chat_messages.append(ChatMessage(
|
| 331 |
+
role="assistant",
|
| 332 |
+
content=result_text,
|
| 333 |
+
metadata={
|
| 334 |
+
"title": "π Tool Result",
|
| 335 |
+
"parent_id": current_tool_id,
|
| 336 |
+
"status": "done"
|
| 337 |
+
}
|
| 338 |
+
))
|
| 339 |
+
else:
|
| 340 |
+
# Media found, show success message instead of raw result
|
| 341 |
+
chat_messages.append(ChatMessage(
|
| 342 |
+
role="assistant",
|
| 343 |
+
content="β
Successfully generated content",
|
| 344 |
+
metadata={
|
| 345 |
+
"title": "π Tool Result",
|
| 346 |
+
"parent_id": current_tool_id,
|
| 347 |
+
"status": "done"
|
| 348 |
+
}
|
| 349 |
+
))
|
| 350 |
+
|
| 351 |
+
# Add media as separate message
|
| 352 |
chat_messages.append(ChatMessage(
|
| 353 |
role="assistant",
|
| 354 |
content={"path": media_url}
|
|
|
|
| 366 |
}
|
| 367 |
))
|
| 368 |
|
| 369 |
+
# Add any remaining text segments after all processing
|
| 370 |
+
if text_segments:
|
| 371 |
+
combined_text = " ".join(text_segments)
|
| 372 |
+
if combined_text.strip():
|
| 373 |
+
chat_messages.append(ChatMessage(
|
| 374 |
+
role="assistant",
|
| 375 |
+
content=combined_text.strip()
|
| 376 |
+
))
|
| 377 |
+
|
| 378 |
+
# Fallback if no content was processed
|
| 379 |
+
if not chat_messages:
|
| 380 |
chat_messages.append(ChatMessage(
|
| 381 |
role="assistant",
|
| 382 |
content="I understand your request and I'm here to help."
|