Phase 7: Performance optimizations, monitoring, and command system with / prefix

This commit is contained in:
truetype committed 2026-03-20 18:36:32 +01:00
1 parent 6eace3abc0
commit c44a4ae92a
4 files changed
+849 -13

No files matched your search

+243 -12
View File
@@ -6,7 +6,7 @@ Enhanced with unified provider tools and custom tools
"""
import os
import sys
from typing import Optional, List
from typing import Optional, List, Dict, Any
from deepagents import create_deep_agent
from langchain.chat_models import init_chat_model
from langchain_core.messages import HumanMessage, SystemMessage
@@ -21,6 +21,14 @@ except ImportError:
TOOLS_AVAILABLE = False
print("Warning: Custom tools not available. Install required dependencies.")
# Import configuration manager
try:
from config_manager import config_manager
CONFIG_AVAILABLE = True
except ImportError:
CONFIG_AVAILABLE = False
print("Warning: Configuration manager not available.")
def load_model_from_config():
"""
@@ -140,6 +148,227 @@ def create_maxclaw_agent(system_prompt: Optional[str] = None):
return agent
def show_help():
"""Show available commands"""
print("""
📖 Comandi disponibili (prefissati con /):
/help, /? - Mostra questo messaggio di aiuto
/status - Mostra lo stato del sistema
/config list - Elenca tutte le configurazioni
/config active - Mostra la configurazione attiva
/config test - Testa la connessione alla configurazione attiva
/config use <name> - Cambia la configurazione attiva
/models - Elenca i modelli disponibili per il provider attivo
/clear - Pulisce lo schermo
/exit, /quit - Esci dall'agente
💡 Per chattare con l'agente, scrivi semplicemente il tuo messaggio senza "/".
Esempio: "Ciao, chi sei?" oppure "Elenca i file nella directory corrente"
""")
def show_status():
"""Show system status"""
print("\n📊 Stato del Sistema:")
print("=" * 40)
# Show provider info
provider = os.getenv("MODEL_PROVIDER", "non configurato")
model = os.getenv("MODEL_NAME", "non configurato")
print(f"Provider: {provider}")
print(f"Modello: {model}")
# Show Ollama URL if configured
if provider == "ollama":
ollama_url = os.getenv("OLLAMA_BASE_URL", "http://localhost:11434")
print(f"Ollama URL: {ollama_url}")
# Show configuration status
if CONFIG_AVAILABLE:
active_config = config_manager.get_active_configuration()
if active_config:
print(f"Configurazione attiva: {active_config}")
else:
print("Configurazione attiva: Nessuna")
configs = config_manager.get_configurations()
print(f"Configurazioni totali: {len(configs)}")
# Show tools status
if TOOLS_AVAILABLE:
print("Strumenti personalizzati: Disponibili")
else:
print("Strumenti personalizzati: Non disponibili")
def list_configurations():
"""List all configurations"""
if not CONFIG_AVAILABLE:
print("❌ Gestore configurazioni non disponibile.")
return
configs = config_manager.get_configurations()
active = config_manager.get_active_configuration()
if not configs:
print("📭 Nessuna configurazione trovata.")
return
print("\n📋 Configurazioni disponibili:")
print("=" * 50)
for name, config in configs.items():
marker = "✅" if name == active else " "
provider = config.get("provider", "unknown")
model = config.get("model_name", "unknown")
print(f"{marker} {name}: {provider} - {model}")
def show_active_configuration():
"""Show active configuration"""
if not CONFIG_AVAILABLE:
print("❌ Gestore configurazioni non disponibile.")
return
active_name = config_manager.get_active_configuration()
if not active_name:
print("📭 Nessuna configurazione attiva.")
return
config = config_manager.get_configuration(active_name)
if not config:
print(f"❌ Configurazione '{active_name}' non trovata.")
return
print(f"\n✅ Configurazione attiva: {active_name}")
print(f" Provider: {config.get('provider', 'unknown')}")
print(f" Modello: {config.get('model_name', 'unknown')}")
if config.get('base_url'):
print(f" URL: {config.get('base_url')}")
def test_active_configuration():
"""Test connection to active configuration"""
if not CONFIG_AVAILABLE:
print("❌ Gestore configurazioni non disponibile.")
return
active_name = config_manager.get_active_configuration()
if not active_name:
print("📭 Nessuna configurazione attiva da testare.")
return
print(f"\n🔍 Test connessione a '{active_name}'...")
success, message = config_manager.test_configuration_connection(active_name)
if success:
print(f"✅ {message}")
else:
print(f"❌ {message}")
def use_configuration(name: str):
"""Set active configuration"""
if not CONFIG_AVAILABLE:
print("❌ Gestore configurazioni non disponibile.")
return
if not config_manager.configuration_exists(name):
print(f"❌ Configurazione '{name}' non trovata.")
print("Usa '/config list' per vedere le configurazioni disponibili.")
return
if config_manager.set_active_configuration(name):
print(f"✅ Configurazione '{name}' impostata come attiva.")
else:
print(f"❌ Errore nell'impostare la configurazione '{name}'.")
def list_models():
"""List available models for current provider"""
provider = os.getenv("MODEL_PROVIDER", "").lower()
if not provider:
print("❌ Nessun provider configurato.")
return
print(f"\n📋 Modelli disponibili per {provider}:")
print("=" * 40)
# This is a simplified version - in a real implementation,
# you would call the provider's API to list models
if provider == "ollama":
print("Per elencare i modelli Ollama, chiedi all'agente:")
print(" 'Elenca i modelli disponibili su Ollama'")
elif provider == "openai":
print("Modelli OpenAI comuni:")
print(" - gpt-4o")
print(" - gpt-4o-mini")
print(" - gpt-4-turbo")
print(" - gpt-3.5-turbo")
elif provider == "openrouter":
print("Per elencare i modelli OpenRouter, chiedi all'agente:")
print(" 'Elenca i modelli disponibili su OpenRouter'")
else:
print(f"Provider '{provider}' non riconosciuto.")
def clear_screen():
"""Clear the screen"""
os.system('cls' if os.name == 'nt' else 'clear')
def process_command(user_input: str) -> bool:
"""
Process a command starting with "/"
Returns:
True if it was a command, False otherwise
"""
if not user_input.startswith('/'):
return False
# Remove the "/" and split into command and arguments
command_line = user_input[1:].strip()
parts = command_line.split()
command = parts[0].lower() if parts else ""
args = parts[1:] if len(parts) > 1 else []
# Process commands
if command in ['help', '?']:
show_help()
elif command == 'status':
show_status()
elif command == 'config':
if not args:
print("❌ Specifica un sottocomando: list, active, test, use <name>")
elif args[0] == 'list':
list_configurations()
elif args[0] == 'active':
show_active_configuration()
elif args[0] == 'test':
test_active_configuration()
elif args[0] == 'use' and len(args) > 1:
use_configuration(args[1])
else:
print(f"❌ Sottocomando sconosciuto: {args[0]}")
print("Sottocomandi disponibili: list, active, test, use <name>")
elif command == 'models':
list_models()
elif command == 'clear':
clear_screen()
elif command in ['exit', 'quit']:
print("👋 Goodbye!")
sys.exit(0)
else:
print(f"❌ Comando sconosciuto: /{command}")
print("Digita /help per vedere i comandi disponibili.")
return True
def main():
"""
Main entry point for the MaxClaw agent.
@@ -156,23 +385,25 @@ def main():
print(" • Proxmox tools: manage VMs, snapshots, resources")
print(" • Standard tools: planning, filesystem, shell, subagents")
print("\n💬 You can now interact with the agent. Type 'exit' to quit.")
print("💡 Try asking:")
print(" - 'List available models for my provider'")
print(" - 'Test connection to Ollama'")
print(" - 'Show me Proxmox VMs'")
print(" - 'What's the default model for OpenAI?'")
print("\n💬 You can now interact with the agent.")
print("📌 Comandi speciali (prefissati con /):")
print(" /help - Mostra i comandi disponibili")
print(" /status - Mostra lo stato del sistema")
print(" /config - Gestisci le configurazioni")
print(" /exit - Esci dall'agente")
print("\n💡 Per chattare, scrivi semplicemente il tuo messaggio.")
while True:
user_input = input("\n👤 You: ").strip()
if user_input.lower() in ['exit', 'quit']:
print("👋 Goodbye!")
break
if not user_input:
continue
# Invoke the agent
# Check if it's a command (starts with /)
if process_command(user_input):
continue
# Otherwise, treat it as a chat message
try:
result = agent.invoke({"messages": [HumanMessage(content=user_input)]})
# Extract the AI's response