diff --git a/ollama_agent.py b/ollama_agent.py index 556c5c3..6987aab 100644 --- a/ollama_agent.py +++ b/ollama_agent.py @@ -361,13 +361,15 @@ For other questions, just answer normally without tools.""" def _detect_intent(self, text: str) -> Optional[ToolCall]: """Detect common intents directly without relying on model""" - text_lower = text.lower() + text_lower = text.lower().strip() # System info patterns system_patterns = [ 'sistema operativo', 'so sto', 'che os', 'what os', 'che sistema', 'which system', 'operating system', - 'che versione', 'python version', 'versione python' + 'che versione', 'python version', 'versione python', + 'sto usando', 'sto usand', 'che so', 'which computer', + 'che tipo di sistema', 'che computer', 'what system' ] if any(p in text_lower for p in system_patterns): return ToolCall(id='intent', name='system_info', arguments={}) @@ -375,7 +377,8 @@ For other questions, just answer normally without tools.""" # List files patterns list_patterns = [ 'lista', 'listare', 'listami', 'elenco', 'mostrami i file', - 'list files', 'list directory', 'ls', 'file nella cartella' + 'list files', 'list directory', 'ls', 'file nella cartella', + 'file del', 'cartella corrente', 'mostra i file' ] if any(p in text_lower for p in list_patterns): return ToolCall(id='intent', name='ls', arguments={'path': '.'}) @@ -404,16 +407,25 @@ For other questions, just answer normally without tools.""" result = self._execute_tool(detected_tool) # Format result as a clean response + response_text = "" try: - result_data = json.loads(result) if isinstance(result, str) else result - if isinstance(result_data, dict): - if "result" in result_data: - return {"messages": self.messages, "response": result_data["result"]} - elif "error" in result_data: - return {"messages": self.messages, "response": f"Errore: {result_data['error']}"} - except: - pass - return {"messages": self.messages, "response": str(result)} + if isinstance(result, str): + try: + result = json.loads(result) + except: + response_text = result + + if isinstance(result, dict): + if "result" in result: + response_text = result["result"] + elif "error" in result: + response_text = f"Errore: {result['error']}" + else: + response_text = json.dumps(result, indent=2) + except Exception as e: + response_text = str(result) if not response_text else response_text + + return {"messages": self.messages, "response": response_text} # Loop principale: risposta -> parse tools -> esegui -> ripeti iteration = 0