From 8acbeb7dfe615b03a127bfcec7f9c0238a4fa965 Mon Sep 17 00:00:00 2001 From: opencode Date: Mon, 23 Mar 2026 15:59:11 +0100 Subject: [PATCH] feat: aggiunge supporto agente semplice per Ollama - Crea create_simple_agent() per provider senza tool-calling - Detecta automaticamente Ollama e usa agente semplice - Aggiunge comando /reset per pulire cronologia --- main.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index fadf75e..7b0c902 100644 --- a/main.py +++ b/main.py @@ -287,6 +287,54 @@ def get_skill_tools() -> List[Tool]: return skill_tools +def create_simple_agent(system_prompt: Optional[str] = None): + """ + Create a simple chat agent for providers without full tool-calling support (e.g., Ollama). + This agent just uses direct chat without advanced tool calling features. + """ + from langchain.schema import HumanMessage, AIMessage, SystemMessage + from langchain.chat_models import init_chat_model + + model = load_model_from_config() + + class SimpleAgent: + """Simple agent wrapper for Ollama and similar providers.""" + + def __init__(self, llm, prompt): + self.llm = llm + self.messages = [] + if prompt: + self.messages.append(SystemMessage(content=prompt)) + + def invoke(self, input_dict): + user_message = input_dict.get("messages", [])[-1] + if hasattr(user_message, 'content'): + user_content = user_message.content + else: + user_content = str(user_message) + + # Add to history + self.messages.append(HumanMessage(content=user_content)) + + # Get response + response = self.llm.invoke(self.messages) + self.messages.append(response) + + return {"messages": self.messages} + + def clear_history(self): + """Clear conversation history but keep system prompt.""" + system_msg = self.messages[0] if self.messages and isinstance(self.messages[0], SystemMessage) else None + self.messages = [system_msg] if system_msg else [] + + if system_prompt is None: + system_prompt = """You are LangCode, a helpful AI assistant. +You can answer questions, write code, explain concepts, and help with programming tasks. +Be concise and helpful. Respond in Italian when the user writes in Italian.""" + + return SimpleAgent(model, system_prompt) + + def get_custom_tools() -> List[Tool]: """ Get custom tools for the LangCode agent @@ -362,10 +410,30 @@ def get_custom_tools() -> List[Tool]: return custom_tools -def create_langcode_agent(system_prompt: Optional[str] = None): +def create_langcode_agent(system_prompt: Optional[str] = None, use_simple_agent: bool = False): """ - Create and return a DeepAgent instance with configurable model. + Create and return an agent instance with configurable model. + + For Ollama and other providers without full tool-calling support, + uses a simple chat agent instead of deepagents. """ + # Detect if we need simple agent (for Ollama without tool support) + if not use_simple_agent and CONFIG_AVAILABLE: + active_name = config_manager.get_active_configuration() + if active_name: + config = config_manager.get_configuration(active_name) + if config and config.get("provider") == "ollama": + # Ollama doesn't fully support bind_tools in deepagents + # Check if we have tools - if so, use simple agent + custom_tools = get_custom_tools() + if len(custom_tools) > 0: + print("⚠️ Using simple chat agent (Ollama detected without full tool support)") + use_simple_agent = True + + if use_simple_agent: + return create_simple_agent(system_prompt) + + # Use deepagents for providers with full tool-calling support if system_prompt is None: system_prompt = """You are LangCode, a helpful AI agent that can plan, use tools, manage context, and delegate to subagents. You are designed to be configurable @@ -979,6 +1047,13 @@ def process_command(user_input: str) -> bool: list_models() elif command == 'clear': clear_screen() + elif command == 'reset': + # Reset conversation history (for simple agent) + if hasattr(agent, 'clear_history'): + agent.clear_history() + print("✅ Cronologia conversazione pulita!") + else: + print("ℹ️ Reset non disponibile per questo tipo di agent.") elif command in ['exit', 'quit']: print("👋 Goodbye!") sys.exit(0)