File size: 32,472 Bytes
1cdb77b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
"""
Chat handling logic for Universal MCP Client - Fixed Version with File Upload Support
"""
import re
import logging
import traceback
from datetime import datetime
from typing import Dict, Any, List, Tuple, Optional
import gradio as gr
from gradio import ChatMessage
from gradio_client import Client
import time
import json
import httpx

from config import AppConfig
from mcp_client import UniversalMCPClient

logger = logging.getLogger(__name__)

class ChatHandler:
    """Handles chat interactions with HF Inference Providers and MCP servers using ChatMessage dataclass"""
    
    def __init__(self, mcp_client: UniversalMCPClient):
        self.mcp_client = mcp_client
        # Initialize the file uploader client for converting local files to public URLs
        try:
            self.uploader_client = Client("abidlabs/file-uploader")
            logger.info("βœ… File uploader client initialized")
        except Exception as e:
            logger.error(f"Failed to initialize file uploader: {e}")
            self.uploader_client = None
    
    def _upload_file_to_gradio_server(self, file_path: str) -> str:
        """Upload a file to the Gradio server and get a public URL"""
        if not self.uploader_client:
            logger.error("File uploader client not initialized")
            return file_path
        
        try:
            # Open file in binary mode as your peer discovered
            with open(file_path, "rb") as f_:
                files = [("files", (file_path.split("/")[-1], f_))]
                r = httpx.post(
                    self.uploader_client.upload_url,
                    files=files,
                )
            r.raise_for_status()
            result = r.json()
            uploaded_path = result[0]
            # Construct the full public URL
            public_url = f"{self.uploader_client.src}/gradio_api/file={uploaded_path}"
            logger.info(f"βœ… Uploaded {file_path} -> {public_url}")
            return public_url
        except Exception as e:
            logger.error(f"Failed to upload file {file_path}: {e}")
            return file_path  # Return original path as fallback
    
    def process_multimodal_message(self, message: Dict[str, Any], history: List) -> Tuple[List[ChatMessage], Dict[str, Any]]:
        """Enhanced MCP chat function with multimodal input support and ChatMessage formatting"""
        
        if not self.mcp_client.hf_client:
            error_msg = "❌ HuggingFace token not configured. Please set HF_TOKEN environment variable or login."
            history.append(ChatMessage(role="user", content=error_msg))
            history.append(ChatMessage(role="assistant", content=error_msg))
            return history, gr.MultimodalTextbox(value=None, interactive=False)
        
        if not self.mcp_client.current_provider or not self.mcp_client.current_model:
            error_msg = "❌ Please select an inference provider and model first."
            history.append(ChatMessage(role="user", content=error_msg))
            history.append(ChatMessage(role="assistant", content=error_msg))
            return history, gr.MultimodalTextbox(value=None, interactive=False)
        
        # Initialize variables for error handling
        user_text = ""
        user_files = []
        uploaded_file_urls = []  # Store uploaded file URLs
        self.file_url_mapping = {}  # Add this: Map local paths to uploaded URLs

        try:
            # Handle multimodal input - message is a dict with 'text' and 'files'
            user_text = message.get("text", "") if message else ""
            user_files = message.get("files", []) if message else []
            
            # Handle case where message might be a string (backward compatibility)
            if isinstance(message, str):
                user_text = message
                user_files = []
            
            logger.info(f"πŸ’¬ Processing multimodal message:")
            logger.info(f"  πŸ“ Text: {user_text}")
            logger.info(f"  πŸ“ Files: {len(user_files)} files uploaded")
            logger.info(f"  πŸ“‹ History type: {type(history)}, length: {len(history)}")
            
            # Convert history to ChatMessage objects if needed
            converted_history = []
            for i, msg in enumerate(history):
                try:
                    if isinstance(msg, dict):
                        # Convert dict to ChatMessage for internal processing
                        logger.info(f"  πŸ“ Converting dict message {i}: {msg.get('role', 'unknown')}")
                        converted_history.append(ChatMessage(
                            role=msg.get('role', 'assistant'),
                            content=msg.get('content', ''),
                            metadata=msg.get('metadata', None)
                        ))
                    else:
                        # Already a ChatMessage
                        logger.info(f"  βœ… ChatMessage {i}: {getattr(msg, 'role', 'unknown')}")
                        converted_history.append(msg)
                except Exception as conv_error:
                    logger.error(f"Error converting message {i}: {conv_error}")
                    logger.error(f"Message content: {msg}")
                    # Skip problematic messages
                    continue
            
            history = converted_history
            
            # Upload files and get public URLs
            for file_path in user_files:
                logger.info(f"  πŸ“„ Local File: {file_path}")
                try:
                    # Upload file to get public URL
                    uploaded_url = self._upload_file_to_gradio_server(file_path)
                    # Store the mapping
                    self.file_url_mapping[file_path] = uploaded_url
                    logger.info(f"  βœ… Uploaded File URL: {uploaded_url}")
                    
                    # Add to history with public URL
                    history.append(ChatMessage(role="user", content={"path": uploaded_url}))
                except Exception as upload_error:
                    logger.error(f"Failed to upload file {file_path}: {upload_error}")
                    # Fallback to local path with warning
                    history.append(ChatMessage(role="user", content={"path": file_path}))
                    logger.warning(f"⚠️ Using local path for {file_path} - MCP servers may not be able to access it")
            
            # Add text message if provided
            if user_text and user_text.strip():
                history.append(ChatMessage(role="user", content=user_text))
            
            # If no text and no files, return early
            if not user_text.strip() and not user_files:
                return history, gr.MultimodalTextbox(value=None, interactive=False)
            
            # Create messages for HF Inference API
            messages = self._prepare_hf_messages(history, uploaded_file_urls)
            
            # Process the chat and get structured responses
            response_messages = self._call_hf_api(messages, uploaded_file_urls)
            
            # Add all response messages to history
            history.extend(response_messages)
            
            return history, gr.MultimodalTextbox(value=None, interactive=False)
            
        except Exception as e:
            error_msg = f"❌ Error: {str(e)}"
            logger.error(f"Chat error: {e}")
            logger.error(traceback.format_exc())
            
            # Add user input to history if it exists
            if user_text and user_text.strip():
                history.append(ChatMessage(role="user", content=user_text))
            if user_files:
                for file_path in user_files:
                    history.append(ChatMessage(role="user", content={"path": file_path}))
                    
            history.append(ChatMessage(role="assistant", content=error_msg))
            return history, gr.MultimodalTextbox(value=None, interactive=False)
    
    def _prepare_hf_messages(self, history: List, uploaded_file_urls: List[str] = None) -> List[Dict[str, Any]]:
        """Convert history (ChatMessage or dict) to HuggingFace Inference API format"""
        messages = []
        
        # Get optimal context settings for current model/provider
        if self.mcp_client.current_model and self.mcp_client.current_provider:
            context_settings = AppConfig.get_optimal_context_settings(
                self.mcp_client.current_model, 
                self.mcp_client.current_provider,
                len(self.mcp_client.get_enabled_servers())
            )
            max_history = context_settings['recommended_history_limit']
        else:
            max_history = 20  # Fallback
        
        # Convert history to HF API format (text only for context)
        recent_history = history[-max_history:] if len(history) > max_history else history
        
        for msg in recent_history:
            # Handle both ChatMessage objects and dictionary format for backward compatibility
            if hasattr(msg, 'role'):  # ChatMessage object
                role = msg.role
                content = msg.content
            elif isinstance(msg, dict) and 'role' in msg:  # Dictionary format
                role = msg.get('role')
                content = msg.get('content')
            else:
                continue  # Skip invalid messages
                
            if role in ["user", "assistant"]:
                
                # Convert any non-string content to string description for context
                if isinstance(content, dict):
                    if "path" in content:
                        file_path = content.get('path', 'unknown')
                        # Check if it's a public URL or local path
                        if file_path.startswith('http'):
                            # It's already a public URL
                            if AppConfig.is_image_file(file_path):
                                content = f"[User uploaded an image: {file_path}]"
                            elif AppConfig.is_audio_file(file_path):
                                content = f"[User uploaded an audio file: {file_path}]"
                            elif AppConfig.is_video_file(file_path):
                                content = f"[User uploaded a video file: {file_path}]"
                            else:
                                content = f"[User uploaded a file: {file_path}]"
                        else:
                            # Local path - mention it's not accessible to remote servers
                            content = f"[User uploaded a file (local path, not accessible to remote servers): {file_path}]"
                    else:
                        content = f"[Object: {str(content)[:50]}...]"
                elif isinstance(content, (list, tuple)):
                    content = f"[List: {str(content)[:50]}...]"
                elif content is None:
                    content = "[Empty]"
                else:
                    content = str(content)
                
                messages.append({
                    "role": role, 
                    "content": content
                })
        
        return messages
    
    def _call_hf_api(self, messages: List[Dict[str, Any]], uploaded_file_urls: List[str] = None) -> List[ChatMessage]:
        """Call HuggingFace Inference API and return structured ChatMessage responses"""
        
        # Check if we have enabled MCP servers to use
        enabled_servers = self.mcp_client.get_enabled_servers()
        if not enabled_servers:
            return self._call_hf_without_mcp(messages)
        else:
            return self._call_hf_with_mcp(messages, uploaded_file_urls)
    
    def _call_hf_without_mcp(self, messages: List[Dict[str, Any]]) -> List[ChatMessage]:
        """Call HF Inference API without MCP servers"""
        logger.info("πŸ’¬ No MCP servers available, using regular HF Inference chat")
        
        system_prompt = self._get_native_system_prompt()
        
        # Add system prompt to messages
        if messages and messages[0].get("role") == "system":
            messages[0]["content"] = system_prompt + "\n\n" + messages[0]["content"]
        else:
            messages.insert(0, {"role": "system", "content": system_prompt})
        
        # Get optimal token settings
        if self.mcp_client.current_model and self.mcp_client.current_provider:
            context_settings = AppConfig.get_optimal_context_settings(
                self.mcp_client.current_model, 
                self.mcp_client.current_provider,
                0  # No MCP servers
            )
            max_tokens = context_settings['max_response_tokens']
        else:
            max_tokens = 8192
        
        # Use HF Inference API
        try:
            response = self.mcp_client.generate_chat_completion(messages, **{"max_tokens": max_tokens})
            response_text = response.choices[0].message.content
            
            if not response_text:
                response_text = "I understand your request and I'm here to help."
            
            return [ChatMessage(role="assistant", content=response_text)]
        except Exception as e:
            logger.error(f"HF Inference API call failed: {e}")
            return [ChatMessage(role="assistant", content=f"❌ API call failed: {str(e)}")]
    
    def _call_hf_with_mcp(self, messages: List[Dict[str, Any]], uploaded_file_urls: List[str] = None) -> List[ChatMessage]:
        """Call HF Inference API with MCP servers and return structured responses"""
        
        # Enhanced system prompt with multimodal and MCP instructions
        system_prompt = self._get_mcp_system_prompt(uploaded_file_urls)
        
        # Add system prompt to messages
        if messages and messages[0].get("role") == "system":
            messages[0]["content"] = system_prompt + "\n\n" + messages[0]["content"]
        else:
            messages.insert(0, {"role": "system", "content": system_prompt})
        
        # Get optimal token settings
        enabled_servers = self.mcp_client.get_enabled_servers()
        if self.mcp_client.current_model and self.mcp_client.current_provider:
            context_settings = AppConfig.get_optimal_context_settings(
                self.mcp_client.current_model, 
                self.mcp_client.current_provider,
                len(enabled_servers)
            )
            max_tokens = context_settings['max_response_tokens']
        else:
            max_tokens = 8192
        
        # Debug logging
        logger.info(f"πŸ“€ Sending {len(messages)} messages to HF Inference API")
        logger.info(f"πŸ”§ Using {len(self.mcp_client.servers)} MCP servers")
        logger.info(f"πŸ€– Model: {self.mcp_client.current_model} via {self.mcp_client.current_provider}")
        logger.info(f"πŸ“ Max tokens: {max_tokens}")
        
        start_time = time.time()
        
        try:
            # Pass file mapping to MCP client
            if hasattr(self, 'file_url_mapping'):
                self.mcp_client.chat_handler_file_mapping = self.file_url_mapping
                
            # Call HF Inference with MCP tool support - using optimal max_tokens
            response = self.mcp_client.generate_chat_completion_with_mcp_tools(messages, **{"max_tokens": max_tokens})
            
            return self._process_hf_response(response, start_time)
        except Exception as e:
            logger.error(f"HF Inference API call with MCP failed: {e}")
            return [ChatMessage(role="assistant", content=f"❌ API call failed: {str(e)}")]
    
    def _process_hf_response(self, response, start_time: float) -> List[ChatMessage]:
        """Process HF Inference response with simplified media handling and nested errors"""
        chat_messages = []
        
        try:
            response_text = response.choices[0].message.content
            
            if not response_text:
                response_text = "I understand your request and I'm here to help."
            
            # Check if this response includes tool execution info
            if hasattr(response, '_tool_execution'):
                tool_info = response._tool_execution
                logger.info(f"πŸ”§ Processing response with tool execution: {tool_info}")
                
                duration = round(time.time() - start_time, 2)
                tool_id = f"tool_{tool_info['tool']}_{int(time.time())}"
                
                if tool_info['success']:
                    tool_result = str(tool_info['result'])
                    
                    # Extract media URL if present
                    media_url = self._extract_media_url(tool_result, tool_info.get('server', ''))
                    
                    # Create tool usage metadata message
                    chat_messages.append(ChatMessage(
                        role="assistant",
                        content="",
                        metadata={
                            "title": f"πŸ”§ Used {tool_info['tool']}",
                            "status": "done",
                            "duration": duration,
                            "id": tool_id
                        }
                    ))
                    
                    # Add nested success message with the raw result
                    if media_url:
                        result_preview = f"βœ… Successfully generated media\nURL: {media_url[:100]}..."
                    else:
                        result_preview = f"βœ… Tool executed successfully\nResult: {tool_result[:200]}..."
                    
                    chat_messages.append(ChatMessage(
                        role="assistant",
                        content=result_preview,
                        metadata={
                            "title": "πŸ“Š Server Response",
                            "parent_id": tool_id,
                            "status": "done"
                        }
                    ))
                    
                    # Add LLM's descriptive text if present (before media)
                    if response_text and not response_text.startswith('{"use_tool"'):
                        # Clean the response text by removing URLs and tool JSON
                        clean_response = response_text
                        if media_url and media_url in clean_response:
                            clean_response = clean_response.replace(media_url, "").strip()
                        
                        # Remove any remaining JSON tool call patterns
                        clean_response = re.sub(r'\{"use_tool"[^}]+\}', '', clean_response).strip()
                        
                        # Remove all markdown link/image syntax completely
                        clean_response = re.sub(r'!\[([^\]]*)\]\([^)]*\)', '', clean_response)  # Remove image markdown
                        clean_response = re.sub(r'\[([^\]]*)\]\([^)]*\)', '', clean_response)   # Remove link markdown  
                        clean_response = re.sub(r'!\[([^\]]*)\]', '', clean_response)           # Remove broken image refs
                        clean_response = re.sub(r'\[([^\]]*)\]', '', clean_response)            # Remove broken link refs
                        clean_response = re.sub(r'\(\s*\)', '', clean_response)                 # Remove empty parentheses
                        clean_response = clean_response.strip()                                 # Final strip

                        # Only add if there's meaningful text left after cleaning
                        if clean_response and len(clean_response) > 10:
                            chat_messages.append(ChatMessage(
                                role="assistant",
                                content=clean_response
                            ))
                    # Handle media content if present
                    if media_url:
                        # Add media as a separate message - Gradio will auto-detect type
                        chat_messages.append(ChatMessage(
                            role="assistant",
                            content={"path": media_url}
                        ))
                    else:
                        # No media URL found, check if we need to show non-media result
                        if not response_text or response_text.startswith('{"use_tool"'):
                            # Only show result if there wasn't descriptive text from LLM
                            if len(tool_result) > 500:
                                result_preview = f"Operation completed successfully. Result preview: {tool_result[:500]}..."
                            else:
                                result_preview = f"Operation completed successfully. Result: {tool_result}"
                            
                            chat_messages.append(ChatMessage(
                                role="assistant",
                                content=result_preview
                            ))
                            
                else:
                    # Tool execution failed
                    error_details = tool_info['result']
                    
                    # Create main tool message with error status
                    chat_messages.append(ChatMessage(
                        role="assistant",
                        content="",
                        metadata={
                            "title": f"❌ Used {tool_info['tool']}",
                            "status": "error",
                            "duration": duration,
                            "id": tool_id
                        }
                    ))
                    
                    # Add nested error response from server
                    chat_messages.append(ChatMessage(
                        role="assistant",
                        content=f"❌ Tool execution failed\n```\n{error_details}\n```",
                        metadata={
                            "title": "πŸ“Š Server Response",
                            "parent_id": tool_id,
                            "status": "error"
                        }
                    ))
                    
                    # Add suggestions as another nested message
                    chat_messages.append(ChatMessage(
                        role="assistant",
                        content="**Suggestions:**\nβ€’ Try modifying your request slightly\nβ€’ Wait a moment and try again\nβ€’ Use a different MCP server if available",
                        metadata={
                            "title": "πŸ’‘ Possible Solutions",
                            "parent_id": tool_id,
                            "status": "info"
                        }
                    ))
            else:
                # No tool usage, just return the response
                chat_messages.append(ChatMessage(
                    role="assistant",
                    content=response_text
                ))
            
        except Exception as e:
            logger.error(f"Error processing HF response: {e}")
            logger.error(traceback.format_exc())
            chat_messages.append(ChatMessage(
                role="assistant",
                content="I understand your request and I'm here to help."
            ))
        
        return chat_messages
    
    def _extract_media_url(self, result_text: str, server_name: str) -> Optional[str]:
        """Extract media URL from MCP response with improved pattern matching"""
        if not isinstance(result_text, str):
            return None
        
        logger.info(f"πŸ” Extracting media from result: {result_text[:500]}...")
        
        # Try JSON parsing first
        try:
            if result_text.strip().startswith('[') or result_text.strip().startswith('{'):
                data = json.loads(result_text.strip())
                
                # Handle array format
                if isinstance(data, list) and len(data) > 0:
                    item = data[0]
                    if isinstance(item, dict):
                        # Check for nested media structure
                        for media_type in ['audio', 'video', 'image']:
                            if media_type in item and isinstance(item[media_type], dict):
                                if 'url' in item[media_type]:
                                    url = item[media_type]['url'].strip('\'"')
                                    logger.info(f"🎯 Found {media_type} URL in JSON: {url}")
                                    return url
                        # Check for direct URL
                        if 'url' in item:
                            url = item['url'].strip('\'"')
                            logger.info(f"🎯 Found direct URL in JSON: {url}")
                            return url
                
                # Handle object format
                elif isinstance(data, dict):
                    # Check for nested media structure
                    for media_type in ['audio', 'video', 'image']:
                        if media_type in data and isinstance(data[media_type], dict):
                            if 'url' in data[media_type]:
                                url = data[media_type]['url'].strip('\'"')
                                logger.info(f"🎯 Found {media_type} URL in JSON: {url}")
                                return url
                    # Check for direct URL
                    if 'url' in data:
                        url = data['url'].strip('\'"')
                        logger.info(f"🎯 Found direct URL in JSON: {url}")
                        return url
                        
        except json.JSONDecodeError:
            pass
        
        # Check for Gradio file URLs (common pattern)
        gradio_patterns = [
            r'https://[^/]+\.hf\.space/gradio_api/file=/[^/]+/[^/]+/[^\s"\'<>,]+',
            r'https://[^/]+\.hf\.space/file=[^\s"\'<>,]+',
            r'/gradio_api/file=/[^\s"\'<>,]+'
        ]
        
        for pattern in gradio_patterns:
            match = re.search(pattern, result_text)
            if match:
                url = match.group(0).rstrip('\'",:;')
                logger.info(f"🎯 Found Gradio file URL: {url}")
                return url
        
        # Check for any HTTP URLs with media extensions
        url_pattern = r'https?://[^\s"\'<>]+\.(?:mp3|wav|ogg|m4a|flac|aac|opus|wma|mp4|webm|avi|mov|mkv|m4v|wmv|png|jpg|jpeg|gif|webp|bmp|svg)'
        match = re.search(url_pattern, result_text, re.IGNORECASE)
        if match:
            url = match.group(0)
            logger.info(f"🎯 Found media URL by extension: {url}")
            return url
        
        # Check for data URLs
        if result_text.startswith('data:'):
            logger.info("🎯 Found data URL")
            return result_text
        
        logger.info("❌ No media URL found in result")
        return None
    
    def _get_native_system_prompt(self) -> str:
        """Get system prompt for HF Inference without MCP servers"""
        model_info = AppConfig.AVAILABLE_MODELS.get(self.mcp_client.current_model, {})
        context_length = model_info.get("context_length", 128000)
        
        return f"""You are an AI assistant powered by {self.mcp_client.current_model} via {self.mcp_client.current_provider}. You have native capabilities for:
- **Text Processing**: You can analyze, summarize, translate, and process text directly
- **General Knowledge**: You can answer questions, explain concepts, and have conversations
- **Code Analysis**: You can read, analyze, and explain code
- **Reasoning**: You can perform step-by-step reasoning and problem-solving
- **Context Window**: You have access to {context_length:,} tokens of context
Current time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
Please provide helpful, accurate, and engaging responses to user queries."""
    
    def _get_mcp_system_prompt(self, uploaded_file_urls: List[str] = None) -> str:
        """Get enhanced system prompt for HF Inference with MCP servers"""
        model_info = AppConfig.AVAILABLE_MODELS.get(self.mcp_client.current_model, {})
        context_length = model_info.get("context_length", 128000)
        
        uploaded_files_context = ""
        if uploaded_file_urls:
            uploaded_files_context = f"\n\nFILES UPLOADED BY USER (Public URLs accessible to MCP servers):\n"
            for i, file_url in enumerate(uploaded_file_urls, 1):
                file_name = file_url.split('/')[-1] if '/' in file_url else file_url
                if AppConfig.is_image_file(file_url):
                    file_type = "Image"
                elif AppConfig.is_audio_file(file_url):
                    file_type = "Audio"
                elif AppConfig.is_video_file(file_url):
                    file_type = "Video"
                else:
                    file_type = "File"
                uploaded_files_context += f"{i}. {file_type}: {file_name}\n   URL: {file_url}\n"
        
        # Get available tools with correct names from enabled servers only
        enabled_servers = self.mcp_client.get_enabled_servers()
        tools_info = []
        for server_name, config in enabled_servers.items():
            tools_info.append(f"- **{server_name}**: {config.description}")
        
        return f"""You are an AI assistant powered by {self.mcp_client.current_model} via {self.mcp_client.current_provider}, with access to various MCP tools.
YOUR NATIVE CAPABILITIES:
- **Text Processing**: You can analyze, summarize, translate, and process text directly
- **General Knowledge**: You can answer questions, explain concepts, and have conversations
- **Code Analysis**: You can read, analyze, and explain code
- **Reasoning**: You can perform step-by-step reasoning and problem-solving
- **Context Window**: You have access to {context_length:,} tokens of context
AVAILABLE MCP TOOLS:
You have access to the following MCP servers:
{chr(10).join(tools_info)}
WHEN TO USE MCP TOOLS:
- **Image Generation**: Creating new images from text prompts
- **Image Editing**: Modifying, enhancing, or transforming existing images  
- **Audio Processing**: Transcribing audio, generating speech, audio enhancement
- **Video Processing**: Creating or editing videos
- **Text to Speech**: Converting text to audio
- **Specialized Analysis**: Tasks requiring specific models or APIs
TOOL USAGE FORMAT:
When you need to use an MCP tool, respond with JSON in this exact format:
{{"use_tool": true, "server": "exact_server_name", "tool": "exact_tool_name", "arguments": {{"param": "value"}}}}
IMPORTANT: Always describe what you're going to do BEFORE the JSON tool call. For example:
"I'll generate speech for your text using the TTS tool."
{{"use_tool": true, "server": "text to speech", "tool": "Kokoro_TTS_mcp_test_generate_first", "arguments": {{"text": "hello"}}}}
IMPORTANT TOOL NAME MAPPING:
- For TTS server: use tool name "Kokoro_TTS_mcp_test_generate_first"
- For image generation: use tool name "dalle_3_xl_lora_v2_generate"  
- For video generation: use tool name "ysharma_ltx_video_distilledtext_to_video"
- For letter counting: use tool name "gradio_app_dummy1_letter_counter"
EXACT SERVER NAMES TO USE:
{', '.join([f'"{name}"' for name in enabled_servers.keys()])}
FILE HANDLING FOR MCP TOOLS:
When using MCP tools with uploaded files, always use the public URLs provided above.
These URLs are accessible to remote MCP servers.
{uploaded_files_context}
MEDIA HANDLING:
When tool results contain media URLs (images, audio, videos), the system will automatically embed them as playable media.
IMPORTANT NOTES:
- Always use the EXACT server names and tool names as specified above
- Use proper JSON format for tool calls
- Include all required parameters in arguments
- For file inputs to MCP tools, use the public URLs provided, not local paths
- ALWAYS provide a descriptive message before the JSON tool call
- After tool execution, you can provide additional context or ask if the user needs anything else
Current time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
Current model: {self.mcp_client.current_model} via {self.mcp_client.current_provider}"""