fix: aggiunge intent detection per fallback automatico

- Rileva automaticamente richieste di system info e list file
- Non dipende dal modello per usare i tool base
- System prompt semplificato
This commit is contained in:
opencode committed 2026-03-23 17:02:30 +01:00
1 parent 2fa06ec0af
commit 9970bba6c7
1 file changed
+36 -21
+36 -21
View File
@@ -232,29 +232,14 @@ class OllamaToolAgent:
def _default_system_prompt(self) -> str:
"""System prompt di default"""
return """You are LangCode AI. You have access to these tools:
return """You are a helpful AI assistant. When users ask about:
- system info, OS, operating system, what computer: respond with TOOL:system_info
- list files, directory contents, ls: respond with TOOL:ls|path=.
1. system_info - Get system info (OS, Python version, etc). No arguments needed.
2. ls(path=".") - List directory contents. Use path="." for current directory.
3. execute(command) - Run shell commands. Example: execute("ls -la")
4. read_file(path) - Read file contents. Example: read_file("/path/to/file")
5. write_file(path, content) - Write to file.
6. search(directory, pattern) - Search for files. Example: search(".", "*.py")
Example: User asks "what OS am I using?" -> You respond: TOOL:system_info
Example: User asks "list files" -> You respond: TOOL:ls|path=.
IMPORTANT RULES:
- ONLY use these exact tool names: system_info, ls, execute, read_file, write_file, search
- NEVER invent tool names
- When you need a tool, output ONLY this format (no other text):
TOOL:system_info
TOOL:ls|path=.
TOOL:execute|command=ls -la
TOOL:read_file|path=/path/to/file
TOOL:write_file|path=/file.txt|content=hello
TOOL:search|directory=.|pattern=*.py
- After the tool result, you can respond normally.
- If no tool is needed, just respond normally."""
For other questions, just answer normally without tools."""
def _tool_instructions(self) -> str:
"""Genera le istruzioni per i tool disponibili"""
@@ -374,6 +359,29 @@ TOOL:search|directory=.|pattern=*.py
"""Formatta il risultato di un tool per il modello"""
return f"\n[TOOL_RESULT]\nTool: {tool_call.name}\nResult: {json.dumps(result, ensure_ascii=False)}\n[/TOOL_RESULT]\n"
def _detect_intent(self, text: str) -> Optional[ToolCall]:
"""Detect common intents directly without relying on model"""
text_lower = text.lower()
# 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'
]
if any(p in text_lower for p in system_patterns):
return ToolCall(id='intent', name='system_info', arguments={})
# List files patterns
list_patterns = [
'lista', 'listare', 'listami', 'elenco', 'mostrami i file',
'list files', 'list directory', 'ls', 'file nella cartella'
]
if any(p in text_lower for p in list_patterns):
return ToolCall(id='intent', name='ls', arguments={'path': '.'})
return None
def invoke(self, input_dict: Dict) -> Dict:
"""
Invoca l'agente con un input.
@@ -390,6 +398,13 @@ TOOL:search|directory=.|pattern=*.py
# Aggiungi il messaggio utente alla cronologia
self.messages.append(HumanMessage(content=user_content))
# Try to detect intent directly first
detected_tool = self._detect_intent(user_content)
if detected_tool:
result = self._execute_tool(detected_tool)
tool_result_text = self._format_tool_result(detected_tool, result)
self.messages.append(HumanMessage(content=tool_result_text))
# Loop principale: risposta -> parse tools -> esegui -> ripeti
iteration = 0
final_response = None