Nymbo commited on
Commit
a417fcf
·
verified ·
1 Parent(s): 9b8cfd6

Update chat_handler.py

Browse files
Files changed (1) hide show
  1. chat_handler.py +31 -29
chat_handler.py CHANGED
@@ -172,42 +172,45 @@ class ChatHandler:
172
  def _prepare_hf_messages(self, history: List, uploaded_file_urls: List[str] = None) -> List[Dict[str, Any]]:
173
  """Convert history (ChatMessage or dict) to HF OpenAI-compatible format with multimodal support"""
174
  messages: List[Dict[str, Any]] = []
175
-
176
  # Get optimal context settings for current model/provider
177
  if self.mcp_client.current_model and self.mcp_client.current_provider:
178
  context_settings = AppConfig.get_optimal_context_settings(
179
- self.mcp_client.current_model,
180
  self.mcp_client.current_provider,
181
  len(self.mcp_client.get_enabled_servers())
182
  )
183
- max_history = context_settings['recommended_history_limit']
184
  else:
185
  max_history = 20 # Fallback
186
-
187
- # Convert history to HF API format (text only for context)
188
  recent_history = history[-max_history:] if len(history) > max_history else history
189
-
190
- last_role = None
 
 
191
  for msg in recent_history:
192
- # Handle both ChatMessage objects and dictionary format for backward compatibility
193
- if hasattr(msg, 'role'): # ChatMessage object
194
  role = msg.role
195
  content = msg.content
196
- elif isinstance(msg, dict) and 'role' in msg: # Dictionary format
197
- role = msg.get('role')
198
- content = msg.get('content')
199
  else:
200
- continue # Skip invalid messages
201
-
202
  if role == "user":
203
- # Build multimodal user messages with parts
204
- part = None
205
  if isinstance(content, dict) and "path" in content:
206
  file_path = content.get("path", "")
207
  if isinstance(file_path, str) and file_path.startswith("http") and AppConfig.is_image_file(file_path):
208
- part = {"type": "image_url", "image_url": {"url": file_path}}
 
 
 
209
  else:
210
- # Non-image or non-URL: fallback to text description
211
  part = {"type": "text", "text": f"[File: {file_path}]"}
212
  elif isinstance(content, (list, tuple)):
213
  part = {"type": "text", "text": f"[List: {str(content)[:50]}...]"}
@@ -216,18 +219,17 @@ class ChatHandler:
216
  else:
217
  part = {"type": "text", "text": str(content)}
218
 
219
- if messages and last_role == "user" and isinstance(messages[-1].get("content"), list):
220
- messages[-1]["content"].append(part)
221
- elif messages and last_role == "user" and isinstance(messages[-1].get("content"), str):
222
- # Convert existing string content to parts and append
223
- existing_text = messages[-1]["content"]
224
- messages[-1]["content"] = [{"type": "text", "text": existing_text}, part]
225
- else:
226
- messages.append({"role": "user", "content": [part]})
227
- last_role = "user"
228
 
229
  elif role == "assistant":
230
- # Assistant content remains text for chat.completions API
231
  if isinstance(content, dict):
232
  text = f"[Object: {str(content)[:50]}...]"
233
  elif isinstance(content, (list, tuple)):
@@ -238,7 +240,7 @@ class ChatHandler:
238
  text = str(content)
239
  messages.append({"role": "assistant", "content": text})
240
  last_role = "assistant"
241
-
242
  return messages
243
 
244
  def _call_hf_api(self, messages: List[Dict[str, Any]], uploaded_file_urls: List[str] = None) -> List[ChatMessage]:
 
172
  def _prepare_hf_messages(self, history: List, uploaded_file_urls: List[str] = None) -> List[Dict[str, Any]]:
173
  """Convert history (ChatMessage or dict) to HF OpenAI-compatible format with multimodal support"""
174
  messages: List[Dict[str, Any]] = []
175
+
176
  # Get optimal context settings for current model/provider
177
  if self.mcp_client.current_model and self.mcp_client.current_provider:
178
  context_settings = AppConfig.get_optimal_context_settings(
179
+ self.mcp_client.current_model,
180
  self.mcp_client.current_provider,
181
  len(self.mcp_client.get_enabled_servers())
182
  )
183
+ max_history = context_settings["recommended_history_limit"]
184
  else:
185
  max_history = 20 # Fallback
186
+
187
+ # Limit history
188
  recent_history = history[-max_history:] if len(history) > max_history else history
189
+
190
+ vision_supported = AppConfig.supports_vision_model(self.mcp_client.current_model) if self.mcp_client.current_model else False
191
+ last_role: Optional[str] = None
192
+
193
  for msg in recent_history:
194
+ # Normalize message
195
+ if hasattr(msg, "role"):
196
  role = msg.role
197
  content = msg.content
198
+ elif isinstance(msg, dict) and "role" in msg:
199
+ role = msg.get("role")
200
+ content = msg.get("content")
201
  else:
202
+ continue
203
+
204
  if role == "user":
205
+ part: Optional[Dict[str, Any]] = None
 
206
  if isinstance(content, dict) and "path" in content:
207
  file_path = content.get("path", "")
208
  if isinstance(file_path, str) and file_path.startswith("http") and AppConfig.is_image_file(file_path):
209
+ if vision_supported:
210
+ part = {"type": "image_url", "image_url": {"url": file_path}}
211
+ else:
212
+ part = None # skip images for text-only models
213
  else:
 
214
  part = {"type": "text", "text": f"[File: {file_path}]"}
215
  elif isinstance(content, (list, tuple)):
216
  part = {"type": "text", "text": f"[List: {str(content)[:50]}...]"}
 
219
  else:
220
  part = {"type": "text", "text": str(content)}
221
 
222
+ if part is not None:
223
+ if messages and last_role == "user" and isinstance(messages[-1].get("content"), list):
224
+ messages[-1]["content"].append(part)
225
+ elif messages and last_role == "user" and isinstance(messages[-1].get("content"), str):
226
+ existing_text = messages[-1]["content"]
227
+ messages[-1]["content"] = [{"type": "text", "text": existing_text}, part]
228
+ else:
229
+ messages.append({"role": "user", "content": [part]})
230
+ last_role = "user"
231
 
232
  elif role == "assistant":
 
233
  if isinstance(content, dict):
234
  text = f"[Object: {str(content)[:50]}...]"
235
  elif isinstance(content, (list, tuple)):
 
240
  text = str(content)
241
  messages.append({"role": "assistant", "content": text})
242
  last_role = "assistant"
243
+
244
  return messages
245
 
246
  def _call_hf_api(self, messages: List[Dict[str, Any]], uploaded_file_urls: List[str] = None) -> List[ChatMessage]: