Compare commits
68
Commits
eda22811ce
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
442c91ee66 | ||
|
|
858b469def | ||
|
|
2e4175f26b | ||
|
|
e72905fd78 | ||
|
|
aec5736394 | ||
|
|
e69a289361 | ||
|
|
b6e8864829 | ||
|
|
39b9288773 | ||
|
|
20233db282 | ||
|
|
c76beb764c | ||
|
|
6cb514ec13 | ||
|
|
5d9bd48013 | ||
|
|
453156148c | ||
|
|
e75a75e16c | ||
|
|
f1d1248d4b | ||
|
|
3197dedd4c | ||
|
|
918b1f3a5b | ||
|
|
c8e7489c26 | ||
|
|
82b70f88d1 | ||
|
|
3a042355b8 | ||
|
|
9fa564e12b | ||
|
|
4a0342dcb7 | ||
|
|
32ed76e5be | ||
|
|
fc60fc1fd0 | ||
|
|
cce8709149 | ||
|
|
f847b92b0a | ||
|
|
1c7781a68f | ||
|
|
e5ce70c68e | ||
|
|
4fef38b740 | ||
|
|
f9def3e389 | ||
|
|
462f3d1785 | ||
|
|
57c43d4738 | ||
|
|
f678a6f20d | ||
|
|
34d6623cb2 | ||
|
|
f8ebfb65ab | ||
|
|
1e49eff703 | ||
|
|
9cccbabef1 | ||
|
|
cffac60211 | ||
|
|
d16102587d | ||
|
|
a0ce57132d | ||
|
|
fbc19aef23 | ||
|
|
e7cd792954 | ||
|
|
b22297a5d4 | ||
|
|
2253fbad13 | ||
|
|
658f7cd580 | ||
|
|
8528068995 | ||
|
|
7fd3bfd26b | ||
|
|
52925d80ce | ||
|
|
ecf4648393 | ||
|
|
7c645bff78 | ||
|
|
a2f933edee | ||
|
|
9970bba6c7 | ||
|
|
2fa06ec0af | ||
|
|
a309c7e27c | ||
|
|
2077618f52 | ||
|
|
0b5c6a874e | ||
|
|
8acbeb7dfe | ||
|
|
b25aa8fbad | ||
|
|
4de50a1712 | ||
|
|
122ad6930b | ||
|
|
05fc002cb4 | ||
|
|
d055204067 | ||
|
|
56f2819a92 | ||
|
|
5a8fcda993 | ||
|
|
d68417bbc8 | ||
|
|
6e7ae3a959 | ||
|
|
753c7d491e | ||
|
|
55a31278a8 |
No files matched your search
@@ -0,0 +1,60 @@
|
||||
{
|
||||
"conversations": [],
|
||||
"facts": [
|
||||
{
|
||||
"timestamp": "2026-03-22T20:18:14.550293",
|
||||
"fact": "LangCode Agent supports MCP integration",
|
||||
"category": "features"
|
||||
},
|
||||
{
|
||||
"timestamp": "2026-03-22T20:27:22.386697",
|
||||
"fact": "LangCode Agent supports MCP integration",
|
||||
"category": "features"
|
||||
},
|
||||
{
|
||||
"timestamp": "2026-03-22T21:16:16.644866",
|
||||
"fact": "\"LangCode Agent supports MCP integration\"",
|
||||
"category": "general"
|
||||
},
|
||||
{
|
||||
"timestamp": "2026-03-22T21:19:31.649087",
|
||||
"fact": "\"LangCode Agent supports MCP integration\"",
|
||||
"category": "general"
|
||||
},
|
||||
{
|
||||
"timestamp": "2026-03-22T21:25:33.013155",
|
||||
"fact": "\"LangCode Agent supports MCP integration\"",
|
||||
"category": "general"
|
||||
},
|
||||
{
|
||||
"timestamp": "2026-03-22T21:30:17.895357",
|
||||
"fact": "\"LangCode Agent supports MCP integration\"",
|
||||
"category": "general"
|
||||
},
|
||||
{
|
||||
"timestamp": "2026-03-22T22:34:46.997791",
|
||||
"fact": "\"LangCode Agent supports MCP integration\"",
|
||||
"category": "general"
|
||||
},
|
||||
{
|
||||
"timestamp": "2026-03-23T07:14:33.532609",
|
||||
"fact": "\"LangCode Agent supports MCP integration\"",
|
||||
"category": "general"
|
||||
},
|
||||
{
|
||||
"timestamp": "2026-03-23T09:14:30.538647",
|
||||
"fact": "\"Il mio colore preferito \u00e8 il verde\"",
|
||||
"category": "general"
|
||||
},
|
||||
{
|
||||
"timestamp": "2026-03-23T09:36:02.053792",
|
||||
"fact": "Questo \u00e8 un test senza virgolette",
|
||||
"category": "general"
|
||||
}
|
||||
],
|
||||
"preferences": {
|
||||
"model_provider": "ollama"
|
||||
},
|
||||
"skills_used": [],
|
||||
"last_updated": "2026-03-23T09:36:02.053800"
|
||||
}
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
# Stato Debug LangCode Agent - 23 Marzo 2026
|
||||
|
||||
## Situazione Attuale
|
||||
|
||||
L'agente **funziona** a livello di esecuzione tool, ma **non mostra il risultato** all'utente.
|
||||
|
||||
## Cosa Funziona
|
||||
|
||||
1. **Intent Detection**: Rileva correttamente "che SO sto usando" come richiesta di system_info
|
||||
2. **Tool Execution**: Il tool `system_info` viene eseguito e restituisce il risultato corretto
|
||||
3. **Response Generation**: `response_text` contiene i dati corretti
|
||||
|
||||
### Debug Output Osservato (CORRETTO):
|
||||
```
|
||||
OLLAMA_AGENT: user_content='che so sto usando'
|
||||
OLLAMA_AGENT: detected_tool=ToolCall(id='intent', name='system_info', arguments={})
|
||||
_execute_tool: name=system_info, args={}
|
||||
_execute_tool: got result type=<class 'str'>, value=OS: Linux 6.17.4-2-pve...
|
||||
OLLAMA_AGENT: result type=<class 'dict'>, value={'result': 'OS: Linux 6.17.4-2-pve...'}
|
||||
OLLAMA_AGENT: response_text=OS: Linux 6.17.4-2-pve
|
||||
Python: 3.12.3
|
||||
Current dir: /root/tryout/langcode_agent
|
||||
User: root
|
||||
```
|
||||
|
||||
## Problema
|
||||
|
||||
Nonostante `response_text` sia corretto, l'output mostra:
|
||||
```
|
||||
🤖 LangCode: che so sto usando
|
||||
```
|
||||
|
||||
Invece di:
|
||||
```
|
||||
🤖 LangCode: OS: Linux 6.17.4-2-pve
|
||||
Python: 3.12.3...
|
||||
```
|
||||
|
||||
## Ipotesi
|
||||
|
||||
1. **L'agente viene chiamato due volte**: Prima con SimpleAgent (fallback) e poi con OllamaToolAgent
|
||||
2. **L'eccezione viene mangiata**: Il blocco try/except in main.py potrebbe nascondere l'errore
|
||||
3. **Bufferizzazione output**: I print() non vengono visualizzati prima dell'eco
|
||||
|
||||
## Codicemain.py (main loop)
|
||||
|
||||
```python
|
||||
while True:
|
||||
user_input = input("\n👤 You: ").strip()
|
||||
|
||||
if not user_input:
|
||||
continue
|
||||
|
||||
# Check if it's a command (starts with /)
|
||||
if process_command(user_input):
|
||||
continue
|
||||
|
||||
# Otherwise, treat it as a chat message
|
||||
try:
|
||||
print("MAIN: before invoke", flush=True)
|
||||
result = agent.invoke({"messages": [HumanMessage(content=user_input)]})
|
||||
print(f"MAIN: after invoke, result type={type(result)}", flush=True)
|
||||
|
||||
# Extract the AI's response
|
||||
if result and "response" in result:
|
||||
response_text = result["response"]
|
||||
else:
|
||||
ai_message = result["messages"][-1]
|
||||
response_text = ai_message.content if hasattr(ai_message, 'content') else str(ai_message)
|
||||
|
||||
print(f"\n🤖 LangCode: {response_text}")
|
||||
except Exception as e:
|
||||
print(f"\n❌ Errore: {str(e)}")
|
||||
```
|
||||
|
||||
## Osservazioni Critiche
|
||||
|
||||
1. `MAIN: before invoke` NON viene mai visualizzato
|
||||
2. Questo significa che `agent.invoke()` potrebbe:
|
||||
- Essere chiamato ma stampare prima di return
|
||||
- Sollevare un'eccezione che viene catturata
|
||||
- Restituire qualcosa di inaspettato
|
||||
|
||||
3. L'eco `🤖 LangCode: che so sto usando` è identico all'input `👤 You: che so sto usando`
|
||||
- Questo suggerisce che l'agent sta restituendo l'input invece del risultato
|
||||
|
||||
## Prossimi Passi Suggeriti
|
||||
|
||||
1. **Verificare quale agente viene creato**: Aggiungere print in `create_langcode_agent()`
|
||||
2. **Verificare il tipo di agent restituito**: Stampare `type(agent)` dopo la creazione
|
||||
3. **Forzare output non bufferizzato**: Usare `sys.stdout.flush()` dopo ogni print
|
||||
4. **Verificare se l'eccezione viene mangiata**: Stampare SEMPRE l'eccezione, anche se vuota
|
||||
|
||||
## File Principali
|
||||
|
||||
- `main.py` - Entry point, main loop
|
||||
- `ollama_agent.py` - OllamaToolAgent con intent detection
|
||||
- `langcode.py` - CLI entry point
|
||||
|
||||
## Comandi di Test
|
||||
|
||||
```bash
|
||||
# Sul server
|
||||
cd ~/tryout/langcode_agent
|
||||
source venv/bin/activate
|
||||
python langcode.py
|
||||
|
||||
# Testare: "che SO sto usando"
|
||||
# Osservare: Il debug output dell'agente MA NON l'output MAIN
|
||||
```
|
||||
@@ -0,0 +1,199 @@
|
||||
# LangCode Agent Enhancement Implementation Plan
|
||||
Based on LangChain Deep Agents Architecture Analysis
|
||||
|
||||
## Overview
|
||||
|
||||
This plan outlines the integration of enhancements inspired by LangChain's Deep Agents architecture into the LangCode Agent. The enhancements focus on:
|
||||
|
||||
1. **Advanced Memory Systems** - Persistent, semantic, and summary-based memory
|
||||
2. **Enhanced Planning & Reasoning** - Sophisticated task planning and execution tracking
|
||||
3. **Production Observability** - Monitoring, tracing, and debugging capabilities
|
||||
4. **Agent Hierarchies** - Specialist agents and routing capabilities
|
||||
|
||||
## Phase 1: Foundation Enhancements (Immediate Integration)
|
||||
|
||||
### 1.1 Simple Persistent Memory System
|
||||
**File:** `enhancements/simple_memory.py`
|
||||
**Status:** Implemented and tested
|
||||
**Integration Points:**
|
||||
- Import in `main.py`: `from enhancements.simple_memory import simple_memory, create_simple_memory_tools`
|
||||
- Add to `get_custom_tools()`: Extend with memory tools
|
||||
- Update system prompt to mention memory capabilities
|
||||
- Add CLI commands: `/memory remember`, `/memory recall`, `/memory preference`, `/memory stats`
|
||||
|
||||
### 1.2 Enhanced Planning System
|
||||
**File:** `enhancements/planning_enhancement.py`
|
||||
**Status:** Implemented and tested
|
||||
**Integration Points:**
|
||||
- Import in `main.py`: `from enhancements.planning_enhancement import enhanced_planner`
|
||||
- Add to `get_custom_tools()`: Extend with planning tools
|
||||
- Update system prompt to mention planning capabilities
|
||||
- Enhance existing `/todo` commands or add `/plan` subcommands
|
||||
|
||||
## Phase 2: Advanced Features (Next Iteration)
|
||||
|
||||
### 2.1 Advanced Memory with Vector Storage
|
||||
**File:** `enhancements/memory_enhancement.py`
|
||||
**Dependencies:**
|
||||
- `langchain-community` for OllamaEmbeddings and FAISS
|
||||
- `sentence-transformers` for embeddings
|
||||
**Integration Points:**
|
||||
- Similar to simple memory but with semantic search capabilities
|
||||
- Add background processing for conversation summarization
|
||||
- Implement memory consolidation during idle periods
|
||||
|
||||
### 2.2 Agent Hierarchies and Routing
|
||||
**New File:** `enhancements/agent_hierarchy.py`
|
||||
**Features:**
|
||||
- Specialist agents for different domains (coding, research, system admin)
|
||||
- Intelligent routing based on task analysis
|
||||
- Agent-to-agent communication patterns
|
||||
- Load balancing and specialization
|
||||
|
||||
### 2.3 Production Observability
|
||||
**New File:** `enhancements/observability.py`
|
||||
**Features:**
|
||||
- LangChain-style callbacks for tracing
|
||||
- Tool usage monitoring and metrics
|
||||
- Performance profiling
|
||||
- Error tracking and alerting
|
||||
- Integration with LangSmith or custom monitoring
|
||||
|
||||
## Phase 3: Deep Agents Alignment (Future Work)
|
||||
|
||||
### 3.1 Context Management Enhancements
|
||||
- Implement automatic summarization at 85% context usage
|
||||
- Add file-based storage for large tool outputs
|
||||
- Implement pointer-based context management
|
||||
- Add context compression techniques
|
||||
|
||||
### 3.2 Subagent Spawning Improvements
|
||||
- Enhance the `task` tool with better isolation
|
||||
- Add per-subagent model configuration
|
||||
- Implement subagent pooling for efficiency
|
||||
- Add inter-subagent communication mechanisms
|
||||
|
||||
### 3.3 Advanced Planning Features
|
||||
- Implement plan-and-execute agent pattern
|
||||
- Add self-reflection and critique mechanisms
|
||||
- Enable dynamic replanning based on execution results
|
||||
- Add contingency planning and error recovery
|
||||
|
||||
## Integration Strategy
|
||||
|
||||
### Step-by-Step Integration:
|
||||
|
||||
1. **Start with Simple Enhancements** (Phase 1)
|
||||
- Integrate `simple_memory.py` and `planning_enhancement.py`
|
||||
- Test thoroughly with existing functionality
|
||||
- Gather user feedback
|
||||
|
||||
2. **Update Documentation**
|
||||
- Update `docs/plans/` with enhancement designs
|
||||
- Add usage examples to README
|
||||
- Create tutorial examples
|
||||
|
||||
3. **Gradual Rollout of Advanced Features** (Phase 2)
|
||||
- Add one enhancement at a time
|
||||
- Maintain backward compatibility
|
||||
- Provide migration paths
|
||||
|
||||
4. **Performance Optimization**
|
||||
- Profile and optimize memory usage
|
||||
- Implement caching where appropriate
|
||||
- Add configuration options to enable/disable features
|
||||
|
||||
## CLI Integration Examples
|
||||
|
||||
### Memory Commands:
|
||||
```
|
||||
/memory remember "LangCode uses Ollama for local models" --category architecture
|
||||
/memory recall "Ollama"
|
||||
/memory preference set model_provider ollama
|
||||
/memory preference get model_provider
|
||||
/memory stats
|
||||
```
|
||||
|
||||
### Planning Commands:
|
||||
```
|
||||
/plan create "Implement user authentication" --tasks "Design DB schema" "Create API endpoints" "Write frontend" "Add tests"
|
||||
/plan start auth-plan
|
||||
/plan list
|
||||
/plan progress auth-plan
|
||||
/task list
|
||||
/task start task_0000
|
||||
/task complete task_0000 "Authentication system implemented"
|
||||
```
|
||||
|
||||
## Configuration Options
|
||||
|
||||
Add to configuration system:
|
||||
```json
|
||||
{
|
||||
"enhancements": {
|
||||
"memory": {
|
||||
"type": "simple|advanced",
|
||||
"persist_directory": "./.langcode/memory",
|
||||
"max_conversations": 100,
|
||||
"max_facts": 50
|
||||
},
|
||||
"planning": {
|
||||
"enabled": true,
|
||||
"auto_create_todos": true,
|
||||
"dependency_tracking": true
|
||||
},
|
||||
"observability": {
|
||||
"enabled": false,
|
||||
"trace_level": "info",
|
||||
"metrics_endpoint": "http://localhost:9090/metrics"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Backward Compatibility Strategy
|
||||
|
||||
All enhancements are designed to be:
|
||||
- **Optional**: Can be enabled/disabled via configuration
|
||||
- **Additive**: Don't modify existing core functionality
|
||||
- **Testable**: Include comprehensive test suites
|
||||
- **Documented**: Clear usage instructions and examples
|
||||
|
||||
## Risk Mitigation
|
||||
|
||||
1. **Dependency Management**:
|
||||
- Enhancements with heavy dependencies are isolated
|
||||
- Fallback to simpler implementations when deps missing
|
||||
- Clear error messages when features unavailable
|
||||
|
||||
2. **Performance Impact**:
|
||||
- Lazy initialization of heavy components
|
||||
- Configurable feature flags
|
||||
- Performance monitoring built-in
|
||||
|
||||
3. **Data Safety**:
|
||||
- Memory data stored locally by default
|
||||
- No automatic data transmission
|
||||
- User control over what gets remembered
|
||||
|
||||
## Success Metrics
|
||||
|
||||
After implementation, measure:
|
||||
- User adoption of new features
|
||||
- Reduction in repeated context requests
|
||||
- Improvement in complex task completion rates
|
||||
- User satisfaction scores
|
||||
- System performance impact
|
||||
|
||||
## Next Steps
|
||||
|
||||
1. Review and approve this implementation plan
|
||||
2. Begin Phase 1 integration (simple memory and planning)
|
||||
3. Conduct internal testing
|
||||
4. Gather feedback from pilot users
|
||||
5. Iterate based on feedback
|
||||
6. Proceed to Phase 2 enhancements
|
||||
|
||||
---
|
||||
|
||||
*This plan brings LangCode Agent closer to the production-grade architecture patterns revealed in LangChain's Deep Agents while maintaining the project's core strengths in configurability and modularity.*
|
||||
+214
@@ -0,0 +1,214 @@
|
||||
# LangCode Agent - Guida Rapida
|
||||
|
||||
## Installazione
|
||||
|
||||
```bash
|
||||
# 1. Clona il repository
|
||||
git clone http://192.168.0.183:8085/truetype/langcode_agent.git
|
||||
cd langcode_agent
|
||||
|
||||
# 2. Crea e attiva virtual environment
|
||||
python3 -m venv venv
|
||||
source venv/bin/activate
|
||||
|
||||
# 3. Installa dipendenze
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Configurazione Rapida
|
||||
|
||||
```bash
|
||||
# Imposta variabili d'ambiente
|
||||
export MODEL_PROVIDER=ollama
|
||||
export MODEL_NAME=gemma2:9b
|
||||
export OLLAMA_BASE_URL=http://192.168.0.50:11434
|
||||
```
|
||||
|
||||
Oppure usa la CLI interattiva per salvare configurazioni persistenti:
|
||||
|
||||
```bash
|
||||
langcode config add -n ollama -p ollama -m gemma2:9b -u http://192.168.0.50:11434 --set-active
|
||||
langcode config test
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Modalità di Utilizzo
|
||||
|
||||
### 1. Chat Interattiva (EnhancedCLI)
|
||||
|
||||
```bash
|
||||
langcode chat
|
||||
```
|
||||
|
||||
Chatta con l'agent AI in tempo reale con UI moderna:
|
||||
- **Colors**: User (cyan), Agent (green), Error (red)
|
||||
- **History**: Cronologia comandi persistente (↑↓)
|
||||
- **Autocomplete**: Tab per completare comandi
|
||||
- **4 Temi**: default, dark, light, monokai
|
||||
- **Syntax Highlighting**: Code blocks colorati
|
||||
- **Keyboard shortcuts**: Ctrl+A/E, Ctrl+K/U, Ctrl+W
|
||||
|
||||
**Comandi CLI interni:**
|
||||
```
|
||||
/help - Mostra aiuto
|
||||
/status - Stato sistema
|
||||
/clear - Pulisci schermo (Ctrl+L)
|
||||
/theme-* - Cambia tema
|
||||
/timestamps - Toggle timestamps
|
||||
/syntax - Toggle syntax highlighting
|
||||
/history - Mostra cronologia
|
||||
/exit - Esci
|
||||
```
|
||||
|
||||
### 2. Server Web (API + Dashboard)
|
||||
|
||||
```bash
|
||||
langcode server
|
||||
langcode server --port 8080
|
||||
langcode server --host 0.0.0.0 --port 3000
|
||||
```
|
||||
|
||||
Accesso dopo l'avvio:
|
||||
- 🌐 **Web UI**: http://localhost:8000
|
||||
- 📊 **Dashboard**: http://localhost:8000/monitoring
|
||||
- 🛒 **Marketplace**: http://localhost:8000/marketplace
|
||||
- 📖 **API Docs**: http://localhost:8000/docs
|
||||
|
||||
### 3. CLI Configurazione
|
||||
|
||||
```bash
|
||||
langcode config list # Lista configurazioni
|
||||
langcode config add -n nome -p ollama -m gemma2:9b -u http://... # Aggiungi
|
||||
langcode config use nome # Attiva configurazione
|
||||
langcode config active # Mostra attiva
|
||||
langcode config test # Testa connessione
|
||||
langcode config search ollama # Cerca
|
||||
langcode config export -f json # Esporta
|
||||
langcode config import backup.json # Importa
|
||||
```
|
||||
|
||||
### 4. TUI Interattiva
|
||||
|
||||
```bash
|
||||
langcode tui
|
||||
```
|
||||
|
||||
Interfaccia testuale per gestire configurazioni.
|
||||
|
||||
### 5. Analytics
|
||||
|
||||
```bash
|
||||
langcode analytics # Riepilogo rapido
|
||||
langcode analytics --report # Report completo
|
||||
langcode dashboard # Dashboard performance
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Comandi Interni (nella Chat)
|
||||
|
||||
Durante la chat interattiva (`langcode chat`):
|
||||
|
||||
```
|
||||
/help # Aiuto
|
||||
/status # Stato sistema
|
||||
/config list # Lista configurazioni
|
||||
/config use <nome> # Cambia configurazione
|
||||
/models # Elenca modelli
|
||||
/clear # Pulisci schermo
|
||||
/exit # Esci
|
||||
```
|
||||
|
||||
### Memory (Memoria Persistente)
|
||||
|
||||
```
|
||||
/memory remember <testo> # Salva un fatto
|
||||
/memory recall <query> # Cerca fatti
|
||||
/memory preference set <key> <value> # Imposta preferenza
|
||||
/memory preference get <key> # Leggi preferenza
|
||||
/memory stats # Statistiche
|
||||
```
|
||||
|
||||
### Planning (Pianificazione)
|
||||
|
||||
```
|
||||
/plan create "<nome piano>" "<task1; task2; task3>" # Crea piano
|
||||
/plan list # Elenca piani
|
||||
/plan progress <plan_id> # Mostra progresso
|
||||
/plan start <task_id> # Inizia task
|
||||
/plan complete <task_id> # Completa task
|
||||
```
|
||||
|
||||
### Skills (Gestione Skills)
|
||||
|
||||
```
|
||||
/skills list # Skills installate
|
||||
/skills search <query> # Cerca skills
|
||||
/skills install <nome> # Installa skill
|
||||
/skills details <nome> # Dettagli skill
|
||||
/skills uninstall <nome> # Disinstalla
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Struttura
|
||||
|
||||
```
|
||||
langcode_agent/
|
||||
├── langcode.py # CLI unificato (langcode)
|
||||
├── main.py # Agent principale
|
||||
├── api_server.py # Server web + API
|
||||
├── config_manager.py # Gestione configurazioni
|
||||
├── config_cli.py # CLI configurazione
|
||||
├── config_tui.py # TUI configurazione
|
||||
├── skill_manager.py # Gestione skills
|
||||
├── mcp_client.py # Client MCP
|
||||
├── analytics.py # Analytics
|
||||
├── dashboard.py # Dashboard performance
|
||||
├── monitoring.py # Monitoraggio
|
||||
├── enhancements/ # Memory e Planning
|
||||
│ ├── simple_memory.py
|
||||
│ └── planning_enhancement.py
|
||||
├── web/ # Frontend web
|
||||
├── plugins/ # Plugin sistema
|
||||
└── venv/ # Virtual environment
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Provider Supportati
|
||||
|
||||
| Provider | Variabili necessarie |
|
||||
|-------------|---------------------|
|
||||
| Ollama | `OLLAMA_BASE_URL` |
|
||||
| OpenAI | `OPENAI_API_KEY` |
|
||||
| OpenRouter | `OPENROUTER_API_KEY` |
|
||||
| Anthropic | `ANTHROPIC_API_KEY` |
|
||||
| Google | `GOOGLE_API_KEY` |
|
||||
|
||||
---
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
**Agent non si avvia:**
|
||||
```bash
|
||||
source venv/bin/activate
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
**Ollama non risponde:**
|
||||
```bash
|
||||
curl http://192.168.0.50:11434/api/tags
|
||||
```
|
||||
|
||||
**Porta già in uso:**
|
||||
```bash
|
||||
langcode server --port 8080
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
**Tempo installazione**: ~5 minuti
|
||||
@@ -20,6 +20,10 @@ Un agente configurabile basato su DeepAgents che supporta più provider LLM.
|
||||
- **Marketplace plugin**: Scopri e installa plugin dal marketplace
|
||||
- **Analytics avanzate**: Analisi dettagliate dell'uso e raccomandazioni
|
||||
- **Comandi integrati**: Comandi speciali con prefisso "/" per gestione rapida
|
||||
- **MCP Integration**: Connect to MCP servers for external services and tools
|
||||
- **Skill Management**: Discover, install, and manage agent skills from skills.sh registry
|
||||
- **Memory Enhancement**: Sistema di memoria persistente per fatti, preferenze e cronologia conversazioni
|
||||
- **Planning Enhancement**: Pianificazione avanzata dei task con dipendenze, tracciamento progresso e monitoraggio stato
|
||||
- **Monitoraggio**: Sistema di logging e monitoraggio delle performance
|
||||
- **Dashboard performance**: Monitoraggio dettagliato delle performance
|
||||
- **Ottimizzazioni performance**: Caching, connection pooling, lazy loading
|
||||
@@ -235,6 +239,23 @@ langcode analytics # Riepilogo rapido
|
||||
langcode analytics --report # Report completo
|
||||
langcode dashboard # Dashboard performance
|
||||
|
||||
# Skills management
|
||||
langcode skills list # Lista skills installate
|
||||
langcode skills search "coding" # Cerca skills
|
||||
langcode skills install "skill-name" # Installa skill
|
||||
langcode skills details "skill-name" # Dettagli skill
|
||||
|
||||
# Memory
|
||||
langcode memory remember "fact" # Ricorda un fatto
|
||||
langcode memory recall "query" # Cerca fatti
|
||||
langcode memory preference set k v # Imposta preferenza
|
||||
langcode memory stats # Statistiche
|
||||
|
||||
# Planning
|
||||
langcode plan create "name" "t1;t2" # Crea piano
|
||||
langcode plan list # Lista piani
|
||||
langcode plan progress plan_id # Progresso piano
|
||||
|
||||
# Info
|
||||
langcode version
|
||||
langcode --help
|
||||
@@ -266,6 +287,19 @@ Quando interagisci con l'agente, puoi usare comandi speciali preceduti da "/" pe
|
||||
/language - Cambia la lingua dell'interfaccia
|
||||
/performance - Mostra il report delle performance
|
||||
/monitoring - Apre la dashboard di monitoraggio
|
||||
/skills list - Elenca skills installate
|
||||
/skills search <q> - Cerca skills nel registry
|
||||
/skills install <n> - Installa una skill
|
||||
/skills details <n> - Mostra dettagli di una skill
|
||||
/memory remember - Ricorda un fatto
|
||||
/memory recall - Cerca fatti nella memoria
|
||||
/memory preference - Gestisci preferenze
|
||||
/memory stats - Statistiche memoria
|
||||
/plan create <n> - Crea un piano
|
||||
/plan list - Lista tutti i piani
|
||||
/plan progress <id> - Mostra progresso piano
|
||||
/plan start <id> - Inizia un task
|
||||
/plan complete <id> - Completa un task
|
||||
/exit, /quit - Esci dall'agente
|
||||
```
|
||||
|
||||
@@ -768,6 +802,117 @@ Gli strumenti Ollama specifici sono ancora disponibili per retrocompatibilità:
|
||||
- `ollama_status`: Controlla se Ollama è in esecuzione
|
||||
- `ollama_running_models`: Mostra i modelli attualmente in esecuzione
|
||||
|
||||
## Ollama Agent con Tool Calling
|
||||
|
||||
LangCode Agent include `OllamaToolAgent` che supporta il tool calling per modelli Ollama locali senza bisogno di API keys esterne.
|
||||
|
||||
### Caratteristiche Principali
|
||||
|
||||
- **Tool calling locale**: Nessuna dipendenza da API esterne
|
||||
- **Intent detection**: Riconosce automaticamente le intenzioni dell'utente
|
||||
- **Strumenti built-in**: Sistema, filesystem, ricerca, esecuzione comandi
|
||||
- **Supporto multilingua**: Pattern in italiano e inglese
|
||||
|
||||
### Strumenti Disponibili
|
||||
|
||||
| Strumento | Descrizione | Esempio |
|
||||
|-----------|-------------|---------|
|
||||
| `system_info` | Informazioni di sistema (OS, Python, directory, utente) | "Che OS stai usando?" |
|
||||
| `ls` | Lista file in una directory | "Cosa c'è in questa cartella?" |
|
||||
| `read_file` | Leggi il contenuto di un file | "Leggi il file requirements.txt" |
|
||||
| `write_file` | Scrivi contenuto in un file | "Scrivi un file test.txt con dentro 'Hello'" |
|
||||
| `search` | Cerca file per pattern | "Cerca i file *.py" |
|
||||
| `execute` | Esegui comandi shell | "ls -lha", "git status" |
|
||||
|
||||
### Pattern di Intent Detection
|
||||
|
||||
L'agent riconosce automaticamente le intenzioni basandosi su parole chiave:
|
||||
|
||||
**System Info:**
|
||||
- "sistema operativo", "che OS", "che sistema"
|
||||
- "system info", "what os", "operating system"
|
||||
|
||||
**Lista File:**
|
||||
- "lista file", "mostrami", "cosa c'è"
|
||||
- "list files", "show me", "ls"
|
||||
|
||||
**Leggi File:**
|
||||
- "leggi", "leggere", "mostrami il file"
|
||||
- "read", "show me", "open"
|
||||
|
||||
**Scrivi File:**
|
||||
- "scrivi", "crea", "salva"
|
||||
- "write", "create", "save"
|
||||
|
||||
**Cerca File:**
|
||||
- "cerca", "trova"
|
||||
- "search", "find"
|
||||
|
||||
**Esegui Comandi:**
|
||||
- Comandi shell con argomenti: `ls -lha`, `git status`, `cat file.txt`
|
||||
- "esegui", "run", "comando"
|
||||
|
||||
### Esempi di Utilizzo
|
||||
|
||||
```
|
||||
👤 You: Che OS stai usando?
|
||||
🤖 LangCode: OS: Linux 5.15.0, Python: 3.12.0, Current dir: /home/user, User: user
|
||||
|
||||
👤 You: Cosa c'è in questa cartella?
|
||||
🤖 LangCode: FILE: main.py
|
||||
FILE: requirements.txt
|
||||
DIR: tests
|
||||
|
||||
👤 You: Leggi il file requirements.txt
|
||||
🤖 LangCode: langchain>=0.3.0
|
||||
requests>=2.31.0
|
||||
...
|
||||
|
||||
👤 You: Scrivi un file test.txt con dentro "Hello World"
|
||||
🤖 LangCode: Successfully wrote to test.txt
|
||||
|
||||
👤 You: Cerca i file *.py
|
||||
🤖 LangCode: Found 15 matches:
|
||||
main.py
|
||||
utils.py
|
||||
...
|
||||
|
||||
👤 You: ls -lha
|
||||
🤖 LangCode: STDOUT:
|
||||
-rw-r--r-- 1 user 11 Mar 23 test.txt
|
||||
```
|
||||
|
||||
### Configurazione
|
||||
|
||||
L'OllamaToolAgent viene caricato automaticamente quando usi Ollama come provider:
|
||||
|
||||
```bash
|
||||
# Configura Ollama
|
||||
langcode config add -n ollama -p ollama -m qwen3.5:4b -u http://192.168.0.50:11434 --set-active
|
||||
|
||||
# Avvia l'agent
|
||||
langcode
|
||||
|
||||
# Oppure direttamente
|
||||
python langcode.py
|
||||
```
|
||||
|
||||
### Struttura del Codice
|
||||
|
||||
```python
|
||||
# ollama_agent.py
|
||||
from langcode_agent.ollama_agent import create_ollama_tool_agent
|
||||
|
||||
# Crea l'agent con tool integrati
|
||||
agent = create_ollama_tool_agent(
|
||||
model=llm,
|
||||
include_builtin_tools=True # system_info, ls, read_file, etc.
|
||||
)
|
||||
|
||||
# Usa l'agent
|
||||
result = agent.invoke({"messages": [HumanMessage(content="Che OS usi?")]})
|
||||
```
|
||||
|
||||
## Sistema di Plugin
|
||||
|
||||
LangCode Agent include un sistema di plugin per estendere le funzionalità.
|
||||
@@ -989,6 +1134,174 @@ I dati di performance vengono salvati in `~/.langcode/performance/` e includono:
|
||||
- Utilizzo provider
|
||||
- Statistiche di sistema
|
||||
|
||||
## MCP Integration
|
||||
|
||||
LangCode Agent supports the Model Context Protocol (MCP) for connecting to external services and tools.
|
||||
|
||||
### Configurazione MCP
|
||||
|
||||
Per configurare un server MCP:
|
||||
|
||||
```bash
|
||||
langcode config add -n my-mcp -p mcp -u http://localhost:8080 --api-key your-key
|
||||
```
|
||||
|
||||
### Utilizzo MCP
|
||||
|
||||
Una volta configurato, gli strumenti MCP sono automaticamente disponibili nell'agente.
|
||||
|
||||
### Strumenti MCP Disponibili
|
||||
|
||||
- Gli strumenti variano in base al server MCP configurato
|
||||
- Consulta la documentazione del server MCP per gli strumenti specifici
|
||||
|
||||
## Skill Management
|
||||
|
||||
LangCode Agent può scoprire, installare e gestire skills dal registry skills.sh.
|
||||
|
||||
### Cercare Skills
|
||||
|
||||
```bash
|
||||
# Cerca skills per parola chiave
|
||||
langcode skills search "coding"
|
||||
/skills search "coding"
|
||||
```
|
||||
|
||||
### Installare Skills
|
||||
|
||||
```bash
|
||||
# Installa una skill
|
||||
langcode skills install "code-review-expert"
|
||||
/skills install "code-review-expert"
|
||||
```
|
||||
|
||||
### Elencare Skills Installate
|
||||
|
||||
```bash
|
||||
# Lista tutte le skills installate
|
||||
langcode skills list
|
||||
/skills list
|
||||
```
|
||||
|
||||
### Dettagli Skill
|
||||
|
||||
```bash
|
||||
# Mostra dettagli di una skill
|
||||
langcode skills details "code-review-expert"
|
||||
/skills details "code-review-expert"
|
||||
```
|
||||
|
||||
### Disinstallare Skills
|
||||
|
||||
```bash
|
||||
# Disinstalla una skill
|
||||
langcode skills uninstall "code-review-expert"
|
||||
/skills uninstall "code-review-expert"
|
||||
```
|
||||
|
||||
### Strumenti Skills nell'Agente
|
||||
|
||||
L'agente ha accesso a strumenti per:
|
||||
- `search_skills`: Cerca skills nel registry
|
||||
- `install_skill`: Installa una skill
|
||||
- `uninstall_skill`: Disinstalla una skill
|
||||
- `list_installed_skills`: Lista skills installate
|
||||
- `get_skill_details`: Ottieni dettagli di una skill
|
||||
|
||||
## Memory Enhancement
|
||||
|
||||
LangCode Agent include un sistema di memoria persistente per ricordare fatti, preferenze e cronologia delle conversazioni.
|
||||
|
||||
### Comandi Memory
|
||||
|
||||
```bash
|
||||
# Ricorda un fatto
|
||||
/memory remember Il mio colore preferito è il blu
|
||||
|
||||
# Cerca fatti nella memoria
|
||||
/memory recall colore
|
||||
|
||||
# Imposta una preferenza
|
||||
/memory preference set tema "dark mode"
|
||||
|
||||
/memory preference set lingua "italiano"
|
||||
|
||||
# Ottieni una preferenza
|
||||
/memory preference get tema
|
||||
|
||||
# Statistiche memoria
|
||||
/memory stats
|
||||
```
|
||||
|
||||
### Strumenti Memory nell'Agente
|
||||
|
||||
L'agente ha accesso a strumenti per:
|
||||
- `remember`: Ricorda un fatto
|
||||
- `recall`: Cerca fatti nella memoria
|
||||
- `preference`: Gestisci preferenze utente
|
||||
- `memory_stats`: Ottieni statistiche della memoria
|
||||
|
||||
### Come Funziona
|
||||
|
||||
La memoria persistente:
|
||||
- Salva automaticamente le conversazioni
|
||||
- Permette di aggiungere fatti categorizzati
|
||||
- Mantiene le preferenze dell'utente
|
||||
- Traccia le skills utilizzate
|
||||
- Persiste tra le sessioni
|
||||
|
||||
## Planning Enhancement
|
||||
|
||||
LangCode Agent include un sistema avanzato di pianificazione con dipendenze, tracciamento del progresso e monitoraggio dello stato.
|
||||
|
||||
### Creare un Piano
|
||||
|
||||
```bash
|
||||
# Crea un piano con più task
|
||||
/plan create "My Project" "Task 1; Task 2; Task 3"
|
||||
```
|
||||
|
||||
### Elencare Piani
|
||||
|
||||
```bash
|
||||
# Lista tutti i piani
|
||||
/plan list
|
||||
```
|
||||
|
||||
### Progresso del Piano
|
||||
|
||||
```bash
|
||||
# Mostra progresso di un piano
|
||||
/plan progress plan_0000
|
||||
```
|
||||
|
||||
### Gestire Task
|
||||
|
||||
```bash
|
||||
# Inizia un task
|
||||
/plan start task_0000
|
||||
|
||||
# Completa un task
|
||||
/plan complete task_0000
|
||||
```
|
||||
|
||||
### Strumenti Planning nell'Agente
|
||||
|
||||
L'agente ha accesso a strumenti per:
|
||||
- `create_task`: Crea un nuovo task
|
||||
- `start_task`: Inizia a lavorare su un task
|
||||
- `complete_task`: Marca un task come completato
|
||||
- `list_tasks`: Lista tutti i task
|
||||
- `get_ready_tasks`: Ottieni task pronti per essere iniziati
|
||||
- `get_plan_progress`: Ottieni progresso di un piano
|
||||
|
||||
### Caratteristiche Avanzate
|
||||
|
||||
- **Priorità**: low, medium, high, urgent
|
||||
- **Dipendenze**: I task possono dipendere da altri task
|
||||
- **Stati**: pending, in_progress, completed, failed, cancelled
|
||||
- **Progresso**: Calcolo automatico della percentuale di completamento
|
||||
|
||||
## Test
|
||||
|
||||
Esegui la suite di test:
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
+55
-13
@@ -1,15 +1,18 @@
|
||||
#!/bin/bash
|
||||
# Bash completion for MaxClaw Agent CLI
|
||||
# Bash completion for LangCode Agent CLI
|
||||
|
||||
_maxclaw_completions()
|
||||
_langcode_completions()
|
||||
{
|
||||
local cur prev commands
|
||||
local cur prev commands subcommands
|
||||
COMPREPLY=()
|
||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
||||
|
||||
# Main commands
|
||||
commands="list search add remove set-active active test export import help"
|
||||
commands="chat server tui config analytics dashboard version"
|
||||
|
||||
# Config subcommands
|
||||
config_subcommands="list add remove use active test export import search"
|
||||
|
||||
# Handle main command completion
|
||||
if [[ ${COMP_CWORD} -eq 1 ]]; then
|
||||
@@ -17,19 +20,40 @@ _maxclaw_completions()
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Handle config subcommand completion
|
||||
if [[ "${COMP_WORDS[1]}" == "config" && ${COMP_CWORD} -eq 2 ]]; then
|
||||
COMPREPLY=( $(compgen -W "${config_subcommands}" -- ${cur}) )
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Handle subcommand-specific completions
|
||||
case "${prev}" in
|
||||
server)
|
||||
local server_options="--port --host"
|
||||
COMPREPLY=( $(compgen -W "${server_options}" -- ${cur}) )
|
||||
return 0
|
||||
;;
|
||||
analytics)
|
||||
local analytics_options="--report --clear"
|
||||
COMPREPLY=( $(compgen -W "${analytics_options}" -- ${cur}) )
|
||||
return 0
|
||||
;;
|
||||
dashboard)
|
||||
local dashboard_options="--clear"
|
||||
COMPREPLY=( $(compgen -W "${dashboard_options}" -- ${cur}) )
|
||||
return 0
|
||||
;;
|
||||
search)
|
||||
# Search term - no completion
|
||||
return 0
|
||||
;;
|
||||
add)
|
||||
# Add command options
|
||||
local add_options="--non-interactive --name --provider --model --api-key --base-url --set-active"
|
||||
local add_options="--name --provider --model --api-key --base-url --activate"
|
||||
COMPREPLY=( $(compgen -W "${add_options}" -- ${cur}) )
|
||||
return 0
|
||||
;;
|
||||
remove|set-active|test)
|
||||
remove|use|test)
|
||||
# Configuration names - try to get from config list
|
||||
if command -v python3 &> /dev/null; then
|
||||
local configs=$(python3 -c "
|
||||
@@ -57,25 +81,43 @@ except:
|
||||
COMPREPLY=( $(compgen -f -- ${cur}) )
|
||||
return 0
|
||||
;;
|
||||
--provider)
|
||||
--provider|-p)
|
||||
# Provider options
|
||||
local providers="openai ollama openrouter"
|
||||
local providers="openai ollama openrouter anthropic google"
|
||||
COMPREPLY=( $(compgen -W "${providers}" -- ${cur}) )
|
||||
return 0
|
||||
;;
|
||||
--format)
|
||||
--format|-f)
|
||||
# Format options
|
||||
local formats="json yaml"
|
||||
COMPREPLY=( $(compgen -W "${formats}" -- ${cur}) )
|
||||
return 0
|
||||
;;
|
||||
--port|-p)
|
||||
# Port suggestions
|
||||
local ports="8000 3000 8080 5000"
|
||||
COMPREPLY=( $(compgen -W "${ports}" -- ${cur}) )
|
||||
return 0
|
||||
;;
|
||||
esac
|
||||
|
||||
# Handle option arguments
|
||||
case "${cur}" in
|
||||
-*)
|
||||
# Option completion
|
||||
local options="--non-interactive --name --provider --model --api-key --base-url --set-active --format --output"
|
||||
# Option completion based on context
|
||||
if [[ "${COMP_WORDS[1]}" == "server" ]]; then
|
||||
local options="--port --host"
|
||||
elif [[ "${COMP_WORDS[1]}" == "config" && "${COMP_WORDS[2]}" == "add" ]]; then
|
||||
local options="--name --provider --model --api-key --base-url --activate"
|
||||
elif [[ "${COMP_WORDS[1]}" == "config" && "${COMP_WORDS[2]}" == "export" ]]; then
|
||||
local options="--format --output"
|
||||
elif [[ "${COMP_WORDS[1]}" == "analytics" ]]; then
|
||||
local options="--report --clear"
|
||||
elif [[ "${COMP_WORDS[1]}" == "dashboard" ]]; then
|
||||
local options="--clear"
|
||||
else
|
||||
local options="--help --version"
|
||||
fi
|
||||
COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
|
||||
return 0
|
||||
;;
|
||||
@@ -84,5 +126,5 @@ except:
|
||||
return 0
|
||||
}
|
||||
|
||||
complete -F _maxclaw_completions python3 config_cli.py
|
||||
complete -F _maxclaw_completions python config_cli.py
|
||||
complete -F _langcode_completions langcode
|
||||
complete -F _langcode_completions langcode.py
|
||||
+73
-45
@@ -1,8 +1,8 @@
|
||||
#compdef config_cli.py
|
||||
# Zsh completion for MaxClaw Agent CLI
|
||||
#compdef langcode
|
||||
# Zsh completion for LangCode Agent CLI
|
||||
|
||||
_maxclaw() {
|
||||
local -a commands
|
||||
_langcode() {
|
||||
local -a commands config_subcommands
|
||||
local curcontext="$curcontext" state line
|
||||
typeset -A opt_args
|
||||
|
||||
@@ -13,39 +13,65 @@ _maxclaw() {
|
||||
case $state in
|
||||
command)
|
||||
commands=(
|
||||
'list:List all configurations'
|
||||
'search:Search configurations'
|
||||
'add:Add a new configuration'
|
||||
'remove:Remove a configuration'
|
||||
'set-active:Set active configuration'
|
||||
'active:Show active configuration'
|
||||
'test:Test connection to a configuration'
|
||||
'export:Export configurations to JSON/YAML'
|
||||
'import:Import configurations from JSON/YAML'
|
||||
'help:Show help'
|
||||
'chat:Avvia l'\''agent in modalità chat'
|
||||
'server:Avvia il web server'
|
||||
'tui:Apre TUI per gestione configurazioni'
|
||||
'config:Gestione configurazioni'
|
||||
'analytics:Mostra analytics'
|
||||
'dashboard:Mostra dashboard performance'
|
||||
'version:Mostra versione'
|
||||
)
|
||||
_describe -t commands 'MaxClaw commands' commands
|
||||
_describe -t commands 'LangCode commands' commands
|
||||
;;
|
||||
args)
|
||||
case ${line[1]} in
|
||||
search)
|
||||
_message 'search term'
|
||||
config)
|
||||
config_subcommands=(
|
||||
'list:Lista configurazioni'
|
||||
'add:Aggiungi configurazione'
|
||||
'remove:Rimuovi configurazione'
|
||||
'use:Attiva configurazione'
|
||||
'active:Mostra configurazione attiva'
|
||||
'test:Testa connessione configurazione'
|
||||
'export:Esporta configurazioni'
|
||||
'import:Importa configurazioni'
|
||||
'search:Cerca configurazioni'
|
||||
)
|
||||
_describe -t config_commands 'config commands' config_subcommands
|
||||
;;
|
||||
add)
|
||||
server)
|
||||
_arguments \
|
||||
'--non-interactive[Non-interactive mode]' \
|
||||
'--name[Configuration name]:name' \
|
||||
'--provider[Provider]:provider:(openai ollama openrouter)' \
|
||||
'--model[Model name]:model' \
|
||||
'--api-key[API key]:api_key' \
|
||||
'--base-url[Base URL]:base_url' \
|
||||
'--set-active[Set as active configuration]'
|
||||
'--port[Porta del server]:port:(8000 3000 8080 5000)' \
|
||||
'--host[Host del server]:host'
|
||||
;;
|
||||
remove|set-active|test)
|
||||
# Try to get configuration names
|
||||
local -a configs
|
||||
if (( $+commands[python3] )); then
|
||||
configs=(${(f)"$(python3 -c "
|
||||
analytics)
|
||||
_arguments \
|
||||
'--report[Genera report completo]' \
|
||||
'--clear[Cancella dati analytics]'
|
||||
;;
|
||||
dashboard)
|
||||
_arguments \
|
||||
'--clear[Cancella dati performance]'
|
||||
;;
|
||||
config)
|
||||
case ${line[2]} in
|
||||
search)
|
||||
_message 'termine di ricerca'
|
||||
;;
|
||||
add)
|
||||
_arguments \
|
||||
'--name[Nome configurazione]:name' \
|
||||
'--provider[Provider]:provider:(openai ollama openrouter anthropic google)' \
|
||||
'--model[Modello]:model' \
|
||||
'--api-key[API key]:api_key' \
|
||||
'--base-url[Base URL]:base_url' \
|
||||
'--activate[Attiva dopo la creazione]'
|
||||
;;
|
||||
remove|use|test)
|
||||
# Try to get configuration names
|
||||
local -a configs
|
||||
if (( $+commands[python3] )); then
|
||||
configs=(${(f)"$(python3 -c "
|
||||
import sys
|
||||
sys.path.insert(0, '.')
|
||||
try:
|
||||
@@ -56,24 +82,26 @@ try:
|
||||
except:
|
||||
pass
|
||||
" 2>/dev/null)"})
|
||||
fi
|
||||
if (( ${#configs} )); then
|
||||
_describe -t configurations 'configurations' configs
|
||||
else
|
||||
_message 'configuration name'
|
||||
fi
|
||||
;;
|
||||
export)
|
||||
_arguments \
|
||||
'--format[Export format]:format:(json yaml)' \
|
||||
'--output[Output filename]:filename'
|
||||
;;
|
||||
import)
|
||||
_files -g '*.json *.yaml *.yml'
|
||||
fi
|
||||
if (( ${#configs} )); then
|
||||
_describe -t configurations 'configurazioni' configs
|
||||
else
|
||||
_message 'nome configurazione'
|
||||
fi
|
||||
;;
|
||||
export)
|
||||
_arguments \
|
||||
'--format[Formato export]:format:(json yaml)' \
|
||||
'--output[File output]:filename'
|
||||
;;
|
||||
import)
|
||||
_files -g '*.json *.yaml *.yml'
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
_maxclaw "$@"
|
||||
_langcode "$@"
|
||||
+37
-2
@@ -42,6 +42,13 @@ def print_configurations(configs=None, title="LangCode Agent Configurations"):
|
||||
print(f" API Key: {'Set' if config.get('api_key') else 'Not set'}")
|
||||
base_url = config.get("base_url", "https://openrouter.ai/api/v1")
|
||||
print(f" Base URL: {base_url}")
|
||||
elif provider == "mcp":
|
||||
server_url = config.get("server_url", "Not set")
|
||||
print(f" Server URL: {server_url}")
|
||||
api_key_status = 'Set' if config.get('api_key') else 'Not set'
|
||||
print(f" API Key: {api_key_status}")
|
||||
timeout = config.get("timeout", 30)
|
||||
print(f" Timeout: {timeout}s")
|
||||
|
||||
print()
|
||||
|
||||
@@ -101,12 +108,14 @@ def add_configuration_interactive():
|
||||
print("1. OpenAI")
|
||||
print("2. Ollama")
|
||||
print("3. OpenRouter")
|
||||
print("4. MCP (Model Context Protocol)")
|
||||
|
||||
provider_choice = input("Enter choice (1-3): ").strip()
|
||||
provider_choice = input("Enter choice (1-4): ").strip()
|
||||
provider_map = {
|
||||
"1": "openai",
|
||||
"2": "ollama",
|
||||
"3": "openrouter"
|
||||
"3": "openrouter",
|
||||
"4": "mcp"
|
||||
}
|
||||
|
||||
if provider_choice not in provider_map:
|
||||
@@ -146,6 +155,32 @@ def add_configuration_interactive():
|
||||
if not base_url:
|
||||
base_url = "https://openrouter.ai/api/v1"
|
||||
config["base_url"] = base_url
|
||||
|
||||
elif provider == "mcp":
|
||||
server_url = input("MCP server URL (e.g., https://skills.sh/mcp): ").strip()
|
||||
if not server_url:
|
||||
print("Error: Server URL is required for MCP")
|
||||
return
|
||||
config["server_url"] = server_url
|
||||
|
||||
api_key = input("MCP API key (optional, press Enter to skip): ").strip()
|
||||
if api_key:
|
||||
config["api_key"] = api_key
|
||||
|
||||
timeout_input = input(f"MCP request timeout in seconds (default: 30): ").strip()
|
||||
if timeout_input:
|
||||
try:
|
||||
timeout = int(timeout_input)
|
||||
if timeout <= 0:
|
||||
print("Warning: Timeout must be positive, using default 30")
|
||||
config["timeout"] = "30"
|
||||
else:
|
||||
config["timeout"] = str(timeout)
|
||||
except ValueError:
|
||||
print("Warning: Invalid timeout value, using default 30")
|
||||
config["timeout"] = "30"
|
||||
else:
|
||||
config["timeout"] = "30"
|
||||
|
||||
# Ask if this should be the active configuration
|
||||
set_active = input("Set as active configuration? (y/N): ").strip().lower()
|
||||
|
||||
+31
-2
@@ -121,7 +121,7 @@ class ConfigManager:
|
||||
if isinstance(config_data, dict):
|
||||
# Decrypt API keys for relevant providers
|
||||
provider = config_data.get("provider", "").lower()
|
||||
if provider in ["openai", "openrouter"] and "api_key" in config_data:
|
||||
if provider in ["openai", "openrouter", "mcp"] and "api_key" in config_data:
|
||||
config_data["api_key"] = self._decrypt_data(config_data["api_key"])
|
||||
|
||||
return result
|
||||
@@ -207,7 +207,7 @@ class ConfigManager:
|
||||
|
||||
# Validate provider
|
||||
provider = config.get("provider", "").lower()
|
||||
valid_providers = ["openai", "ollama", "openrouter"]
|
||||
valid_providers = ["openai", "ollama", "openrouter", "mcp"]
|
||||
|
||||
if not provider:
|
||||
errors.append("Provider is required")
|
||||
@@ -262,6 +262,35 @@ class ConfigManager:
|
||||
if base_url.endswith("/"):
|
||||
config["base_url"] = base_url.rstrip("/")
|
||||
|
||||
elif provider == "mcp":
|
||||
server_url = config.get("server_url", "").strip()
|
||||
if not server_url:
|
||||
errors.append("Server URL is required for MCP provider")
|
||||
else:
|
||||
if not server_url.startswith(("http://", "https://")):
|
||||
errors.append("MCP server URL must start with http:// or https://")
|
||||
else:
|
||||
# Remove trailing slash for consistency
|
||||
if server_url.endswith("/"):
|
||||
config["server_url"] = server_url.rstrip("/")
|
||||
|
||||
# API key is optional for MCP (some servers might not require auth)
|
||||
api_key = config.get("api_key", "").strip()
|
||||
if api_key and len(api_key) < 10:
|
||||
errors.append("MCP API key appears too short if provided")
|
||||
|
||||
# Timeout is optional, but if provided must be positive integer
|
||||
timeout = config.get("timeout")
|
||||
if timeout is not None:
|
||||
try:
|
||||
timeout_int = int(timeout)
|
||||
if timeout_int <= 0:
|
||||
errors.append("MCP timeout must be a positive integer")
|
||||
else:
|
||||
config["timeout"] = timeout_int
|
||||
except ValueError:
|
||||
errors.append("MCP timeout must be a valid integer")
|
||||
|
||||
return len(errors) == 0, errors
|
||||
|
||||
def get_configurations(self) -> Dict[str, Any]:
|
||||
|
||||
+293
@@ -0,0 +1,293 @@
|
||||
"""
|
||||
Terminal UI con scrollback usando Curses
|
||||
Scrollback vero con frecce e PgUp/PgDn
|
||||
"""
|
||||
import sys
|
||||
import signal
|
||||
import curses
|
||||
from typing import Optional, Callable
|
||||
|
||||
try:
|
||||
import curses
|
||||
CURSES_AVAILABLE = True
|
||||
except ImportError:
|
||||
CURSES_AVAILABLE = False
|
||||
|
||||
|
||||
class CursesUI:
|
||||
"""
|
||||
Terminal UI con scrollback usando curses.
|
||||
|
||||
Features:
|
||||
- Scrollback completo con PgUp/PgDn
|
||||
- Frecce su/giù per navigare
|
||||
- Resize automatico
|
||||
- Colors (se supportati)
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
scrollback: int = 10000,
|
||||
prompt: str = "❯ "
|
||||
):
|
||||
"""
|
||||
Inizializza il terminale UI con curses.
|
||||
|
||||
Args:
|
||||
scrollback: Numero di righe nello scrollback buffer
|
||||
prompt: Prompt da mostrare
|
||||
"""
|
||||
if not CURSES_AVAILABLE:
|
||||
raise ImportError("curses non è disponibile su questo sistema")
|
||||
|
||||
self.scrollback = scrollback
|
||||
self.prompt = prompt
|
||||
|
||||
# Buffer per scrollback
|
||||
self.lines: list = []
|
||||
|
||||
# Callback per l'agent
|
||||
self.agent_callback: Optional[Callable[[str], str]] = None
|
||||
|
||||
def _init_colors(self, stdscr):
|
||||
"""Inizializza i colori"""
|
||||
if curses.has_colors():
|
||||
curses.start_color()
|
||||
curses.use_default_colors()
|
||||
# Colori: 1=cyan, 2=green, 3=yellow, 4=red, 5=magenta, 6=blue
|
||||
curses.init_pair(1, curses.COLOR_CYAN, -1) # Header
|
||||
curses.init_pair(2, curses.COLOR_GREEN, -1) # Agent output
|
||||
curses.init_pair(3, curses.COLOR_YELLOW, -1) # Thinking
|
||||
curses.init_pair(4, curses.COLOR_RED, -1) # Error
|
||||
curses.init_pair(5, curses.COLOR_MAGENTA, -1) # Prompt
|
||||
curses.init_pair(6, curses.COLOR_BLUE, -1) # User input
|
||||
|
||||
def _add_line(self, text: str, color: int = 2):
|
||||
"""Aggiunge una linea al buffer"""
|
||||
self.lines.append((text, color))
|
||||
# Limita la dimensione del buffer
|
||||
if len(self.lines) > self.scrollback:
|
||||
self.lines.pop(0)
|
||||
|
||||
def _draw(self, stdscr):
|
||||
"""Disegna lo schermo"""
|
||||
stdscr.clear()
|
||||
|
||||
# Ottieni dimensioni
|
||||
max_y, max_x = stdscr.getmaxyx()
|
||||
|
||||
# Header
|
||||
header = "═" * (max_x - 4)
|
||||
stdscr.addstr(0, 0, f"╔{header}╗", curses.color_pair(1) | curses.A_BOLD)
|
||||
title = " LangCode Agent - Curses UI "
|
||||
stdscr.addstr(1, (max_x - len(title)) // 2, title, curses.color_pair(1) | curses.A_BOLD)
|
||||
stdscr.addstr(2, 0, f"╚{header}╝", curses.color_pair(1) | curses.A_BOLD)
|
||||
|
||||
# Calcola area visibile
|
||||
start_row = 4
|
||||
visible_rows = max_y - start_row - 2 # -2 per status e prompt
|
||||
|
||||
# Mostra linee visibili
|
||||
total_lines = len(self.lines)
|
||||
|
||||
# Scroll offset (0 = showing latest)
|
||||
# La scrolling position va da 0 a max(0, total_lines - visible_rows)
|
||||
self.scroll_offset = max(0, total_lines - visible_rows)
|
||||
|
||||
# Disegna le linee visibili
|
||||
for i in range(min(visible_rows, total_lines)):
|
||||
line_idx = self.scroll_offset + i
|
||||
if line_idx < total_lines:
|
||||
text, color = self.lines[line_idx]
|
||||
# Tronca se troppo lunga
|
||||
display_text = text[:max_x - 2] if len(text) > max_x - 2 else text
|
||||
try:
|
||||
stdscr.addstr(start_row + i, 0, display_text, curses.color_pair(color))
|
||||
except curses.error:
|
||||
pass
|
||||
|
||||
# Status bar
|
||||
status_y = max_y - 2
|
||||
scroll_info = f"Linee: {total_lines} | Scroll: {self.scroll_offset} | PgUp/PgDn=Frecce=Scroll | q=quit | Ctrl+C=exit"
|
||||
stdscr.addstr(status_y, 0, "─" * (max_x - len(scroll_info) - 1), curses.A_DIM)
|
||||
stdscr.addstr(status_y, max_x - len(scroll_info) - 1, scroll_info, curses.A_DIM)
|
||||
|
||||
# Prompt
|
||||
stdscr.addstr(max_y - 1, 0, f"\033[1;35m{self.prompt}\033[0m ", curses.color_pair(5) | curses.A_BOLD)
|
||||
|
||||
stdscr.refresh()
|
||||
|
||||
def _handle_input(self, stdscr):
|
||||
"""Gestisce l'input"""
|
||||
curses.curs_set(1) # Mostra cursore
|
||||
|
||||
# Abilita arrow keys e PgUp/PgDn
|
||||
stdscr.keypad(True)
|
||||
|
||||
# Configura timeout per read non-blocking
|
||||
stdscr.timeout(100) # 100ms timeout
|
||||
|
||||
input_buffer = ""
|
||||
while True:
|
||||
self._draw(stdscr)
|
||||
|
||||
try:
|
||||
key = stdscr.getch()
|
||||
except curses.error:
|
||||
continue
|
||||
|
||||
if key == -1:
|
||||
# Timeout - continua
|
||||
continue
|
||||
|
||||
elif key in [curses.KEY_ENTER, 10, 13]:
|
||||
# Enter - processa comando
|
||||
if input_buffer.strip():
|
||||
return input_buffer.strip()
|
||||
input_buffer = ""
|
||||
|
||||
elif key in [curses.KEY_UP]:
|
||||
# Scroll up nel buffer
|
||||
if self.scroll_offset > 0:
|
||||
self.scroll_offset -= 1
|
||||
|
||||
elif key in [curses.KEY_DOWN]:
|
||||
# Scroll down nel buffer
|
||||
max_y, max_x = stdscr.getmaxyx()
|
||||
visible_rows = max_y - 6
|
||||
if self.scroll_offset < max(0, len(self.lines) - visible_rows):
|
||||
self.scroll_offset += 1
|
||||
|
||||
elif key in [curses.KEY_PPAGE]:
|
||||
# PgUp - scroll su di una pagina
|
||||
max_y, _ = stdscr.getmaxyx()
|
||||
self.scroll_offset = max(0, self.scroll_offset - (max_y - 6))
|
||||
|
||||
elif key in [curses.KEY_NPAGE]:
|
||||
# PgDn - scroll giu di una pagina
|
||||
max_y, max_x = stdscr.getmaxyx()
|
||||
visible_rows = max_y - 6
|
||||
self.scroll_offset = min(max(0, len(self.lines) - visible_rows),
|
||||
self.scroll_offset + visible_rows)
|
||||
|
||||
elif key == 27: # ESC
|
||||
return "/exit"
|
||||
|
||||
elif key in [3, 26]: # Ctrl+C
|
||||
return "/exit"
|
||||
|
||||
elif key in [curses.KEY_BACKSPACE, 127, 8]:
|
||||
# Backspace
|
||||
input_buffer = input_buffer[:-1]
|
||||
|
||||
elif 32 <= key <= 126:
|
||||
# Carattere stampabile
|
||||
input_buffer += chr(key)
|
||||
|
||||
# Mostra input corrente da qualche parte se necessario
|
||||
|
||||
def start(self, agent_callback: Callable[[str], str]):
|
||||
"""
|
||||
Avvia il terminale interattivo con curses.
|
||||
"""
|
||||
self.agent_callback = agent_callback
|
||||
|
||||
def handle_sigint(signum, frame):
|
||||
sys.exit(0)
|
||||
|
||||
old_handler = signal.signal(signal.SIGINT, handle_sigint)
|
||||
|
||||
try:
|
||||
curses.wrapper(self._main_loop)
|
||||
finally:
|
||||
signal.signal(signal.SIGINT, old_handler)
|
||||
|
||||
def _main_loop(self, stdscr):
|
||||
"""Loop principale curses"""
|
||||
self._init_colors(stdscr)
|
||||
curses.cbreak()
|
||||
curses.noecho()
|
||||
|
||||
# Aggiungi header
|
||||
self._add_line("╔═══════════════════════════════════════════════════════╗", 1)
|
||||
self._add_line("║ LangCode Agent - Curses UI con Scrollback ║", 1)
|
||||
self._add_line("╚═══════════════════════════════════════════════════════╝", 1)
|
||||
self._add_line("", 0)
|
||||
self._add_line("Scrivi un messaggio e premi Enter. Usa PgUp/PgDn per scrollare.", 6)
|
||||
self._add_line("Comandi: /help, /clear, /exit", 6)
|
||||
self._add_line("", 0)
|
||||
|
||||
while True:
|
||||
# Ottieni input
|
||||
stdscr.timeout(-1) # Bloccante per l'input
|
||||
command = self._handle_input(stdscr)
|
||||
|
||||
if command.lower() in ['/exit', '/quit']:
|
||||
break
|
||||
|
||||
if not command:
|
||||
continue
|
||||
|
||||
if command == '/help':
|
||||
self._add_line("╔═══════════════════════════════════════════════════════╗", 1)
|
||||
self._add_line("║ COMANDI ║", 1)
|
||||
self._add_line("╠═══════════════════════════════════════════════════════╣", 1)
|
||||
self._add_line("║ /help - Mostra questo messaggio ║", 1)
|
||||
self._add_line("║ /clear - Pulisci lo schermo ║", 1)
|
||||
self._add_line("║ /exit - Esci dall'agente ║", 1)
|
||||
self._add_line("║ PgUp - Scroll su ║", 1)
|
||||
self._add_line("║ PgDn - Scroll giu ║", 1)
|
||||
self._add_line("║ Frecce - Naviga nel scrollback ║", 1)
|
||||
self._add_line("╚═══════════════════════════════════════════════════════╝", 1)
|
||||
continue
|
||||
|
||||
if command == '/clear':
|
||||
self.lines.clear()
|
||||
continue
|
||||
|
||||
# Mostra comando utente
|
||||
self._add_line("", 0)
|
||||
self._add_line(f"👤 {command}", 6)
|
||||
|
||||
# Chiamata all'agent
|
||||
self._add_line("", 0)
|
||||
self._add_line("🤔 Pensando...", 3)
|
||||
|
||||
try:
|
||||
response = agent_callback(command)
|
||||
|
||||
# Rimuovi "thinking" e mostra risposta
|
||||
if self.lines and "🤔 Pensando..." in self.lines[-1][0]:
|
||||
self.lines.pop()
|
||||
|
||||
# Aggiungi risposta
|
||||
for line in response.split('\n'):
|
||||
self._add_line(f"🤖 {line}", 2)
|
||||
except Exception as e:
|
||||
self._add_line(f"❌ Errore: {str(e)}", 4)
|
||||
|
||||
self._add_line("", 0)
|
||||
|
||||
|
||||
def create_curses_ui(
|
||||
scrollback: int = 10000,
|
||||
prompt: str = "❯ "
|
||||
) -> CursesUI:
|
||||
"""Factory function per creare CursesUI"""
|
||||
return CursesUI(
|
||||
scrollback=scrollback,
|
||||
prompt=prompt
|
||||
)
|
||||
|
||||
|
||||
# Test standalone
|
||||
if __name__ == "__main__":
|
||||
def test_callback(text):
|
||||
return f"Ricevuto: {text}\n\nRisposta multi-linea:\n1. Prima linea\n2. Seconda\n3. Terza\n4. Quarta\n5. Quinta"
|
||||
|
||||
print("Avvio CursesUI di test...")
|
||||
ui = create_curses_ui()
|
||||
try:
|
||||
ui.start(test_callback)
|
||||
except KeyboardInterrupt:
|
||||
print("\nUscito")
|
||||
@@ -0,0 +1,271 @@
|
||||
# Da Chatbot ad Agente AI: Gap Analysis e Roadmap
|
||||
|
||||
## Stato Attuale del Progetto
|
||||
|
||||
**LangCode Agent è ora un Agente AI completo** con loop operativo, pianificazione, memoria persistente e UI moderna.
|
||||
|
||||
L'agente può:
|
||||
- Eseguire goal in autonomia attraverso il loop Pensa→Agisci→Osserva
|
||||
- Pianificare e tracciare task multipli
|
||||
- Salvare e recuperare fatti e preferenze
|
||||
- Interagire con l'utente attraverso una UI moderna a colori
|
||||
|
||||
---
|
||||
|
||||
## Chatbot vs Agente: Gap Analysis
|
||||
|
||||
| Caratteristica | Chatbot (Attuale) | Agente AI (Obiettivo) | Stato Attuale |
|
||||
|----------------|-------------------|----------------------|---------------|
|
||||
| **Input/Output** | Turn-by-turn | Goal-oriented | ✅ Goal-oriented |
|
||||
| **Planning** | Nessuno | Scomposizione in task | ✅ Integrato |
|
||||
| **Loop operativo** | Singola risposta | Pensa → Agisci → Osserva → Ripianifica | ✅ Funzionante |
|
||||
| **Memoria** | Solo cronologia sessione | A lungo termine, persistente | ✅ Integrato |
|
||||
| **Autonomia** | Dipende da input utente | Esegue autonomamente | ✅ Parziale |
|
||||
| **Tool use** | Arricchisci risposta | Agisci sull'ambiente | ✅ Funzionante |
|
||||
| **Goal tracking** | Nessuno | Tiene traccia obiettivi | ✅ Integrato nel planner |
|
||||
|
||||
---
|
||||
|
||||
## Componenti Implementati
|
||||
|
||||
### 1. Planning System (`enhancements/planning_enhancement.py`)
|
||||
- **Integrato in**: `ReActAgent`
|
||||
- **Funzionalità**: Task con priorità, dipendenze, progress tracking
|
||||
- **API**: `run()`, `run_plan()`, `get_plan_progress()`
|
||||
|
||||
### 2. Simple Memory (`enhancements/simple_memory.py`)
|
||||
- **Integrato in**: `ReActAgent`
|
||||
- **Funzionalità**: Memorizzazione fatti, preferenze, cronologia
|
||||
- **Tool**: `remember`, `recall`, `preference`, `memory_stats`
|
||||
|
||||
### 3. Enhanced Memory (`enhancements/memory_enhancement.py`)
|
||||
- **Esiste**: Vector store con FAISS, semantic search
|
||||
- **Dipendenza**: Richiede `nomic-embed-text` in Ollama
|
||||
- **Stato**: Parzialmente funzionante
|
||||
|
||||
---
|
||||
|
||||
## Cosa Manca per Diventare Agente
|
||||
|
||||
### ✅ 1. **Agent Loop (COMPLETATO)**
|
||||
Implementato in `enhancements/react_agent.py`:
|
||||
- Loop Pensa → Agisci → Osserva
|
||||
- Massimo 10 iterazioni configurabili
|
||||
- Supporto per tutti i tool built-in
|
||||
|
||||
### ✅ 2. **Goal Decomposition (COMPLETATO)**
|
||||
- `decompose_goal()` per scomposizione automatica
|
||||
- `run_plan()` per esecuzione sequenziale di goal multipli
|
||||
|
||||
### 3. **Self-Reflection**
|
||||
- L'agente deve valutare se il risultato è soddisfacente
|
||||
- Se un tool fallisce, provare approccio alternativo
|
||||
- **Stato**: Da implementare
|
||||
|
||||
### 4. **Enhanced Memory (Vector Store)**
|
||||
- Integrato ma richiede `nomic-embed-text` in Ollama
|
||||
- **Stato**: Dipendenza esterna necessaria
|
||||
|
||||
### 4. **Persistent Memory Integration**
|
||||
- Memoria deve essere доступна in ogni conversazione
|
||||
- L'agente deve poter salvare e recuperare informazioni
|
||||
|
||||
### 5. **Multi-Step Execution Context**
|
||||
- L'agente deve mantenere stato tra diverse chiamate tool
|
||||
- Non ripetere azioni già completate
|
||||
|
||||
---
|
||||
|
||||
## Roadmap di Implementazione
|
||||
|
||||
### ✅ Fase 1: Agent Loop Base (COMPLETATO)
|
||||
**Obiettivo**: Abilitare esecuzione multi-step
|
||||
|
||||
Implementato in `enhancements/react_agent.py`:
|
||||
- ThinkStep: LLM decide prossima azione (via intent detection)
|
||||
- ActStep: Esegui tool
|
||||
- ObserveStep: Leggi risultato
|
||||
- EvaluateStep: Goal raggiunto?
|
||||
- ReplanStep: Ripianifica se fallito
|
||||
|
||||
**Modifiche a `ollama_agent.py`**:
|
||||
- Aggiunto metodo `run()` con loop
|
||||
- Aggiunto parametro `max_iterations`
|
||||
- Integrato `enhanced_planner` per task tracking
|
||||
|
||||
### ✅ Fase 2: Planning Integration (COMPLETATO)
|
||||
**Obiettivo**: Planning system parte dell'agente
|
||||
|
||||
- `run()` crea e traccia task nel planner
|
||||
- `run_plan()` esegue sequenze di goal
|
||||
- `decompose_goal()` scompone goal complessi via LLM
|
||||
- Task tracciati con stato: pending->in_progress->completed/failed
|
||||
|
||||
### ✅ Fase 3: Memory Integration (COMPLETATO)
|
||||
**Obiettivo**: Rendere memoria parte dell'agente
|
||||
|
||||
- Integrato `simple_memory` in `ReActAgent`
|
||||
- Aggiunti tool: `remember`, `recall`, `preference`, `memory_stats`
|
||||
- Memoria persistente tra sessioni (file `.langcode/simple_memory.json`)
|
||||
- Metodi: `remember()`, `recall()`, `get_memory_stats()`
|
||||
- Totale: 10 tool disponibili (6 built-in + 4 memory)
|
||||
|
||||
### ✅ Fase 4: Enhanced CLI (COMPLETATO)
|
||||
**Obiettivo**: Migliorare l'esperienza utente con UI moderna
|
||||
|
||||
- `enhanced_cli.py`: UI moderna con prompt_toolkit
|
||||
- **Colors**: User (cyan), Agent (green), System (yellow), Error (red)
|
||||
- **History**: Cronologia persistente in `.langcode/.cli_history`
|
||||
- **Autocomplete**: Comandi `/help`, `/status`, `/clear`, `/theme`, `/exit`
|
||||
- **4 Temi**: default, dark, light, monokai
|
||||
- **Syntax Highlighting**: Code blocks con pygments
|
||||
- **Keyboard shortcuts**: Ctrl+A/E, Ctrl+K/U, Ctrl+W
|
||||
- **Toggle**: `/timestamps`, `/syntax`
|
||||
- Integrato in `langcode.py chat`
|
||||
|
||||
---
|
||||
|
||||
## Implementazione Proposta: Nuovo `ReActAgent`
|
||||
|
||||
```python
|
||||
# enhancements/react_agent.py (nuovo)
|
||||
|
||||
class ReActAgent:
|
||||
"""
|
||||
Agente con Loop Pensa→Agisci→Osserva→Ripianifica
|
||||
"""
|
||||
|
||||
def __init__(self, llm, tools, max_iterations=10):
|
||||
self.llm = llm
|
||||
self.tools = {t.name: t for t in tools}
|
||||
self.max_iterations = max_iterations
|
||||
self.planner = EnhancedPlanner() # Esistente
|
||||
self.memory = SimplePersistentMemory() # Esistente
|
||||
|
||||
def run(self, goal: str) -> Dict:
|
||||
"""
|
||||
Esegue un goal attraverso il loop ReAct.
|
||||
"""
|
||||
context = f"Goal: {goal}\n\n"
|
||||
|
||||
for i in range(self.max_iterations):
|
||||
# 1. THINK: LLM decide prossima azione
|
||||
thought = self._think(context, goal)
|
||||
|
||||
# 2. ACT: Esegui tool se necessario
|
||||
if thought.get("action"):
|
||||
result = self._act(thought["action"], thought["args"])
|
||||
context += f"\nAzione: {thought['action']}\nRisultato: {result}\n"
|
||||
|
||||
# Salva in memoria se rilevante
|
||||
if thought["action"] in ["read_file", "execute"]:
|
||||
self.memory.add_fact(f"Azione completata: {thought['action']}")
|
||||
|
||||
# 3. OBSERVE & EVALUATE
|
||||
if self._goal_achieved(context, goal):
|
||||
return {"success": True, "result": context}
|
||||
|
||||
return {"success": False, "result": context, "error": "Max iterations"}
|
||||
|
||||
def _think(self, context, goal) -> Dict:
|
||||
"""LLM decide prossima azione basata su contesto"""
|
||||
# Prompt ingegnerizzato per decision-making
|
||||
...
|
||||
|
||||
def _act(self, action_name, args) -> str:
|
||||
"""Esegue azione tramite tool"""
|
||||
if action_name in self.tools:
|
||||
return self.tools[action_name].invoke(args)
|
||||
return f"Tool {action_name} non trovato"
|
||||
|
||||
def _goal_achieved(self, context, goal) -> bool:
|
||||
"""Valuta se goal è stato raggiunto"""
|
||||
...
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Utilizzo Attuale
|
||||
|
||||
### Test Semplice:
|
||||
```bash
|
||||
source venv/bin/activate
|
||||
./venv/bin/python -c "
|
||||
from enhancements.react_agent import ReActAgent
|
||||
from ollama_agent import get_builtin_tools
|
||||
from langchain_ollama import ChatOllama
|
||||
|
||||
llm = ChatOllama(model='qwen3.5:4b', base_url='http://192.168.0.50:11434')
|
||||
agent = ReActAgent(llm=llm, tools=get_builtin_tools())
|
||||
|
||||
result = agent.run('Che OS stai usando?')
|
||||
print(result['result'])
|
||||
"
|
||||
```
|
||||
|
||||
### Test con Piano (multi-goal):
|
||||
```python
|
||||
goals = [
|
||||
'Che OS stai usando?',
|
||||
'Elenca i file',
|
||||
'Leggi requirements.txt'
|
||||
]
|
||||
result = agent.run_plan(goals, 'test_plan')
|
||||
print(f\"Completati: {result['completed']}/{result['total_goals']}\")
|
||||
```
|
||||
|
||||
### Verifica Task nel Planner:
|
||||
```python
|
||||
print(agent.get_plan_progress())
|
||||
# {'total_tasks': 3, 'plans': 1}
|
||||
```
|
||||
|
||||
### Test Memory:
|
||||
```python
|
||||
# Ricorda un fatto
|
||||
agent.remember("L'utente preferisce il tema scuro", "preferenza")
|
||||
|
||||
# Cerca fatti
|
||||
print(agent.recall("tema"))
|
||||
# Trovati 1 ricordi:
|
||||
# - L'utente preferisce il tema scuro (preferenza)
|
||||
|
||||
# Statistiche memoria
|
||||
print(agent.get_memory_stats())
|
||||
# {'total_conversations': 0, 'total_facts': 11, 'total_preferences': 1, ...}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Commit Log
|
||||
|
||||
| Data | Commit | Descrizione |
|
||||
|------|--------|-------------|
|
||||
| 24 Mar 2026 | `e5ce70c` | fix: migliora intent detection e parsing message dict |
|
||||
| 24 Mar 2026 | `1c778a` | feat: aggiungi ReAct Agent Loop |
|
||||
| 24 Mar 2026 | `f847b92` | feat: integra Planning system nel ReActAgent |
|
||||
| 24 Mar 2026 | `fc60fc1` | feat: integra Simple Memory nel ReActAgent |
|
||||
| 24 Mar 2026 | `4a0342d` | feat: aggiungi EnhancedCLI con prompt_toolkit |
|
||||
| 24 Mar 2026 | `9fa564e` | feat: potenzia EnhancedCLI con funzionalità avanzate |
|
||||
|
||||
---
|
||||
|
||||
## Conclusione
|
||||
|
||||
**Il progetto è progredito da chatbot ad agente con UI moderna.**
|
||||
|
||||
Tutte le fasi della roadmap sono completate:
|
||||
- ✅ Agent Loop (Pensa→Agisci→Osserva)
|
||||
- ✅ Planning Integration
|
||||
- ✅ Memory Integration
|
||||
- ✅ Enhanced CLI (UI moderna con colors, history, autocomplete, temi)
|
||||
|
||||
Il progetto è ora un **Agente AI completo** con:
|
||||
- Loop operativo autonomo
|
||||
- Pianificazione task
|
||||
- Memoria persistente
|
||||
- UI moderna e user-friendly
|
||||
|
||||
---
|
||||
|
||||
*Documento aggiornato: 24 Marzo 2026*
|
||||
@@ -0,0 +1,50 @@
|
||||
# MCP Integration Design for LangCode Agent
|
||||
|
||||
## Section 1: MCP Client and Tool Conversion (Previously discussed and validated)
|
||||
|
||||
## Section 2: Integration with LangCode Agent Architecture (Previously discussed and validated)
|
||||
|
||||
## Section 3: Specific Implementation Details, Data Flow, and Error Handling
|
||||
|
||||
### Implementation Files to Create/Modify:
|
||||
1. **New file**: `mcp_client.py` - Contains the MCPClient class and tool conversion functions
|
||||
2. **Modified file**: `main.py` - Add `get_mcp_tools()` function and integrate MCP tools into `get_custom_tools()`
|
||||
3. **Modified file**: `config_manager.py` - Add methods to manage MCP server configurations (if not already present)
|
||||
4. **Modified file**: `config_cli.py` - Add CLI commands for MCP server management (list, add, remove, use, etc.)
|
||||
|
||||
### Data Flow:
|
||||
1. **Startup**: When the agent initializes, `get_custom_tools()` is called
|
||||
2. **MCP Tool Discovery**: `get_mcp_tools()` reads all configured MCP servers from config_manager
|
||||
3. **Server Connection**: For each MCP server, an MCPClient instance connects to the server URL
|
||||
4. **Tool Enumeration**: The client calls the MCP server's `tools/list` endpoint to get available tools
|
||||
5. **Tool Conversion**: Each tool specification is converted to a LangChain Tool with:
|
||||
- Same name and description as the MCP tool
|
||||
- A function that serializes arguments and calls the MCP server's `tools/call` endpoint
|
||||
- Proper error handling for network issues, server errors, or invalid responses
|
||||
6. **Tool Aggregation**: All MCP-derived tools are combined with existing Proxmox and provider tools
|
||||
7. **Agent Initialization**: The DeepAgent is created with the complete toolset
|
||||
|
||||
### Error Handling Strategy:
|
||||
- **Connection Failures**: If an MCP server is unreachable during startup, log a warning and continue (don't fail agent initialization)
|
||||
- **Tool Call Failures**: If a tool call to an MCP server fails, return a descriptive error message as the tool result
|
||||
- **Schema Validation**: Validate tool arguments against the MCP tool's input schema before making the call
|
||||
- **Timeouts**: Implement reasonable timeouts for MCP server connections and calls
|
||||
- **Retry Logic**: Consider implementing retry logic for transient network failures
|
||||
|
||||
### Testing Approach:
|
||||
1. **Unit Tests**:
|
||||
- Test MCPClient connection and tool listing with mock servers
|
||||
- Test tool conversion function with various tool specifications
|
||||
- Test error handling scenarios (timeouts, invalid responses, etc.)
|
||||
|
||||
2. **Integration Tests**:
|
||||
- Test with a real MCP server (could be a local test server or skills.sh if accessible)
|
||||
- Verify that tools appear in the agent's tool list and can be invoked successfully
|
||||
- Test end-to-end flows: user asks agent to use an MCP-derived tool, agent executes it correctly
|
||||
|
||||
3. **Manual Testing**:
|
||||
- Add an MCP server configuration pointing to skills.sh (if it offers MCP endpoints)
|
||||
- Verify that skills from skills.sh appear as available tools in the agent
|
||||
- Test invoking a few different skills to ensure they work correctly
|
||||
|
||||
Are these implementation details, data flow, error handling, and testing approaches correct and complete? Do you see any gaps or areas that need adjustment?
|
||||
@@ -0,0 +1,300 @@
|
||||
# Skill Management Design for LangCode Agent
|
||||
|
||||
## Overview
|
||||
|
||||
The Skill Management feature enables LangCode Agent to discover, install, uninstall, and manage agent skills from skill registries like skills.sh. This functionality allows users to extend the agent's capabilities by leveraging community-developed skills that encapsulate domain expertise, specialized tools, and reusable workflows.
|
||||
|
||||
## Architecture
|
||||
|
||||
The skill management system consists of three main components:
|
||||
|
||||
1. **Skill Manager (`skill_manager.py`)**:
|
||||
- Handles all interactions with skill registries (primarily skills.sh CLI)
|
||||
- Provides functions for searching, installing, uninstalling, listing, and getting details about skills
|
||||
- Includes fallback mechanisms when the skills.sh CLI is not available
|
||||
- Manages the local skills directory (`~/.langcode/skills/`)
|
||||
|
||||
2. **Skill Tools (`main.py`)**:
|
||||
- Converts skill management functions into LangChain Tool objects
|
||||
- Makes skill management capabilities available to the LLM agent
|
||||
- Includes tools for: search_skills, install_skill, uninstall_skill, list_installed_skills, get_skill_details
|
||||
|
||||
3. **CLI Integration (`main.py` and `config_cli.py`)**:
|
||||
- Adds `/skills` subcommand to the main LangCode CLI
|
||||
- Provides direct access to skill management functions via terminal commands
|
||||
- Integrates with the existing configuration and command processing systems
|
||||
|
||||
## CLI Usage
|
||||
|
||||
The skill management functionality is accessible through the `/skills` subcommand:
|
||||
|
||||
```
|
||||
langcode skills search <query> [--limit <n>]
|
||||
langcode skills install <skill_name> [--version <version>]
|
||||
langcode skills uninstall <skill_name>
|
||||
langcode skills list
|
||||
langcode skills details <skill_name>
|
||||
```
|
||||
|
||||
### Command Details
|
||||
|
||||
#### Search for Skills
|
||||
```
|
||||
langcode skills search "web scraping" --limit 5
|
||||
```
|
||||
Searches for skills matching the query and displays results with name, description, version, and author information.
|
||||
|
||||
#### Install a Skill
|
||||
```
|
||||
langcode skills install brave-search
|
||||
```
|
||||
Installs the specified skill from the skill registry. Optionally specify a version with `--version`.
|
||||
|
||||
#### Uninstall a Skill
|
||||
```
|
||||
langcode skills uninstall brave-search
|
||||
```
|
||||
Removes the specified skill from the local skills installation.
|
||||
|
||||
#### List Installed Skills
|
||||
```
|
||||
langcode skills list
|
||||
```
|
||||
Displays all currently installed skills with their names, descriptions, versions, authors, and installation paths.
|
||||
|
||||
#### Get Skill Details
|
||||
```
|
||||
langcode skills details brave-search
|
||||
```
|
||||
Shows detailed information about a specific skill including description, version, author, category, repository, and homepage.
|
||||
|
||||
## Natural Language Usage
|
||||
|
||||
Once the LangCode Agent is running, users can interact with skill management features using natural language:
|
||||
|
||||
### Examples:
|
||||
- "Search for skills related to web scraping"
|
||||
- "Install the brave-search skill"
|
||||
- "What skills do I have installed?"
|
||||
- "Show me details about the mcp-builder skill"
|
||||
- "Uninstall the github-api skill"
|
||||
|
||||
The agent will use its skill management tools to perform these operations and provide feedback on the results.
|
||||
|
||||
## Skill Management Tools Available to the Agent
|
||||
|
||||
When the agent initializes with skill management enabled, it gains access to these LangChain Tools:
|
||||
|
||||
1. **search_skills**: Search for agent skills in the skills.sh registry
|
||||
- Input: query (string), limit (integer, optional, default 10)
|
||||
- Output: Formatted list of matching skills with details
|
||||
|
||||
2. **install_skill**: Install a skill from the skills.sh registry
|
||||
- Input: skill_name (string)
|
||||
- Output: Success or error message
|
||||
|
||||
3. **uninstall_skill**: Uninstall an installed skill
|
||||
- Input: skill_name (string)
|
||||
- Output: Success or error message
|
||||
|
||||
4. **list_installed_skills**: List all currently installed skills
|
||||
- Input: None
|
||||
- Output: Formatted list of installed skills with details
|
||||
|
||||
5. **get_skill_details**: Get detailed information about a specific skill
|
||||
- Input: skill_name (string)
|
||||
- Output: Detailed skill information
|
||||
|
||||
## Workflow Examples
|
||||
|
||||
### Discovering and Installing a New Skill
|
||||
1. User: "Search for skills about GitHub integration"
|
||||
2. Agent: Uses search_skills tool to find relevant skills
|
||||
3. Agent: Returns list of GitHub-related skills (e.g., github-api, github-actions)
|
||||
4. User: "Install the github-api skill"
|
||||
5. Agent: Uses install_skill tool to install the skill
|
||||
6. Agent: Confirms successful installation
|
||||
7. User: Now the agent can use GitHub API tools through the newly installed skill
|
||||
|
||||
### Managing Existing Skills
|
||||
1. User: "What skills do I have installed?"
|
||||
2. Agent: Uses list_installed_skills tool to show current skills
|
||||
3. User: "Show me details about the brave-search skill"
|
||||
4. Agent: Uses get_skill_details tool to display comprehensive information
|
||||
5. User: "Uninstall the brave-search skill"
|
||||
6. Agent: Uses uninstall_skill tool to remove the skill
|
||||
7. Agent: Confirms successful removal
|
||||
|
||||
## Technical Implementation Details
|
||||
|
||||
### Skill Manager Class
|
||||
The `SkillManager` class in `skill_manager.py` provides the core functionality:
|
||||
|
||||
- **search_skills(query, limit)**: Uses `skills search` CLI command with JSON output
|
||||
- **get_skill_details(skill_name)**: Uses `skills show` CLI command with JSON output
|
||||
- **install_skill(skill_name, version)**: Uses `skills add` CLI command
|
||||
- **uninstall_skill(skill_name)**: Uses `skills rm` CLI command
|
||||
- **list_installed_skills()**: Uses `skills list` CLI command
|
||||
- **skill_is_installed(skill_name)**: Checks if a skill is in the installed list
|
||||
- **get_skill_path(skill_name)**: Returns filesystem path to installed skill
|
||||
|
||||
### Fallback Mechanisms
|
||||
When the skills.sh CLI is not available:
|
||||
- Search functions return empty results
|
||||
- Installation/uninstallation functions return error messages
|
||||
- Local skills directory checking provides basic installed skill information
|
||||
|
||||
### Integration with Agent System
|
||||
- Skill tools are added to the agent's toolset in `get_custom_tools()` alongside MCP, Proxmox, and provider tools
|
||||
- The agent's system prompt is updated to inform users about skill management capabilities
|
||||
- CLI commands are integrated into the main command processing flow in `process_command()`
|
||||
|
||||
## Configuration
|
||||
|
||||
Skill management uses the local skills directory by default:
|
||||
- Location: `~/.langcode/skills/`
|
||||
- Automatically created when first needed
|
||||
- Each skill is installed in its own subdirectory
|
||||
- Skills consist of a SKILL.md file and any supporting files/scripts
|
||||
|
||||
No additional configuration is required to use skill management - it works out of the box with the skills.sh CLI when available.
|
||||
|
||||
## Dependencies
|
||||
|
||||
The skill management feature requires:
|
||||
1. **skills.sh CLI** (primary method): For full functionality including search, install, uninstall
|
||||
- Install via: `npm install -g @openai/codex` (includes skills.sh)
|
||||
- Or follow skills.sh installation instructions
|
||||
|
||||
2. **Python subprocess module**: For executing skills.sh commands
|
||||
- Included in standard Python library
|
||||
|
||||
When skills.sh is not available:
|
||||
- Basic functionality is limited to checking locally installed skills
|
||||
- Search, install, and uninstall operations will return appropriate error messages
|
||||
- The system gracefully degrades rather than failing completely
|
||||
|
||||
## Security Considerations
|
||||
|
||||
- Skill installation executes code from external sources - users should review skills before installing
|
||||
- The skill manager does not automatically execute skill code - it only manages installation
|
||||
- Skills are installed to a local directory under user control
|
||||
- Users should only install skills from trusted sources
|
||||
- The system does not grant additional permissions to installed skills beyond what the agent already has
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### "skills.sh command not found"
|
||||
- Install skills.sh using: `npm install -g @openai/codex`
|
||||
- Ensure the npm global bin directory is in your PATH
|
||||
- Verify installation with: `skills --version`
|
||||
|
||||
### Skill installation fails
|
||||
- Check your internet connection
|
||||
- Verify the skill name is correct
|
||||
- Some skills may have specific dependencies or requirements
|
||||
- Try installing without specifying a version to get the latest compatible version
|
||||
|
||||
### No skills found in search
|
||||
- Try different or more general search terms
|
||||
- Check that skills.sh is properly installed and authenticated
|
||||
- Some skills may not be publicly available or may require specific access
|
||||
|
||||
### Skill not found after installation
|
||||
- Verify installation completed successfully
|
||||
- Check the local skills directory (`~/.langcode/skills/`)
|
||||
- Use `/skills list` to confirm the skill appears in the installed list
|
||||
- Restart the agent if necessary to reload available tools
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Search before installing**: Use search to find the most relevant skill for your needs
|
||||
2. **Check skill details**: Review description, version, and author before installing
|
||||
3. **Start with popular skills**: Well-maintained skills with good documentation are safer choices
|
||||
4. **Keep skills updated**: Periodically check for updates to installed skills
|
||||
5. **Remove unused skills**: Keep your skills directory clean by uninstalling skills you no longer need
|
||||
6. **Review skill content**: For security, examine SKILL.md and supporting files before relying on a skill
|
||||
7. **Combine with MCP**: Many skills work best when combined with MCP servers for external service access
|
||||
|
||||
## Future Enhancements
|
||||
|
||||
Potential improvements to the skill management system include:
|
||||
- Integration with multiple skill registries beyond skills.sh
|
||||
- Skill version management and upgrading
|
||||
- Skill dependency resolution
|
||||
- Skill categorization and tagging systems
|
||||
- Offline skill distribution and sharing
|
||||
- Skill validation and safety scanning
|
||||
- Integration with agent configuration for skill-specific settings
|
||||
- Web interface for skill management
|
||||
|
||||
## Example Session
|
||||
|
||||
```
|
||||
$ langcode
|
||||
🚀 Initializing LangCode Agent...
|
||||
✅ Agent initialized successfully!
|
||||
|
||||
🔧 Available tools:
|
||||
• Provider tools: list models, test connections, set defaults
|
||||
• Proxmox tools: manage VMs, snapshots, resources
|
||||
• MCP tools: accessing external services and skills registries (like skills.sh)
|
||||
• Skill management tools: discovering, installing, and managing agent skills
|
||||
• Standard tools: planning, filesystem, shell, subagents
|
||||
|
||||
💬 You can now interact with the agent.
|
||||
📌 Comandi speciali (prefissati con /):
|
||||
/help - Mostra i comandi disponibili
|
||||
/status - Mostra lo stato del sistema
|
||||
/config - Gestisci le configurazioni
|
||||
/skills - Gestione skill agent
|
||||
/exit - Esci dall'agente
|
||||
|
||||
💡 Per chattare, scrivi semplicemente il tuo messaggio.
|
||||
|
||||
👤 You: Search for skills about web scraping
|
||||
🤖 LangCode:
|
||||
Found 3 skills:
|
||||
1. brave-search
|
||||
Description: Helps you structured brave web search queries and summarizer workflows
|
||||
Version: 1.0.0
|
||||
Author: skillmd.ai
|
||||
|
||||
2. scrape-web
|
||||
Description: Skill for scraping web content with various options
|
||||
Version: 0.5.0
|
||||
Author: webtools.dev
|
||||
|
||||
3. html-parser
|
||||
Description: Parse and extract data from HTML documents
|
||||
Version: 2.1.0
|
||||
Author: dataparsers.org
|
||||
|
||||
👤 You: Install the brave-search skill
|
||||
🤖 LangCode: ✅ Skill 'brave-search' installed successfully
|
||||
|
||||
👤 You: What skills do I have installed?
|
||||
🤖 LangCode:
|
||||
Installed skills (1):
|
||||
1. brave-search
|
||||
Description: Helps you structured brave web search queries and summarizer workflows
|
||||
Version: 1.0.0
|
||||
Author: skillmd.ai
|
||||
Path: /home/user/.langcode/skills/brave-search
|
||||
|
||||
👤 You: Show me details about the brave-search skill
|
||||
🤖 LangCode:
|
||||
Skill Details: brave-search
|
||||
==================================================
|
||||
Description: Helps you structured brave web search queries and summarizer workflows
|
||||
Version: 1.0.0
|
||||
Author: skillmd.ai
|
||||
Category: development
|
||||
Repository: https://github.com/skillmd/brave-search
|
||||
Homepage: https://skillmd.ai/how-to-build/brave-search-13/
|
||||
|
||||
👤 You: Now I want to scrape a website and summarize the content
|
||||
🤖 LangCode: I can help you with that! First, let me use the brave-search skill to search for information about the website you're interested in. What specific website or topic would you like me to research?
|
||||
```
|
||||
|
||||
This demonstrates the complete workflow from discovering skills to installing them and using their capabilities through natural language interaction with the LangCode Agent.
|
||||
+734
@@ -0,0 +1,734 @@
|
||||
"""
|
||||
Enhanced CLI for LangCode Agent
|
||||
Provides modern terminal UI with colors, history, autocomplete, and more.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
from typing import Optional, List, Dict, Any, Callable
|
||||
from datetime import datetime
|
||||
|
||||
# prompt_toolkit imports
|
||||
from prompt_toolkit import PromptSession, HTML
|
||||
from prompt_toolkit.key_binding import KeyBindings
|
||||
from prompt_toolkit.styles import Style
|
||||
from prompt_toolkit.completion import WordCompleter, NestedCompleter, PathCompleter
|
||||
from prompt_toolkit.history import FileHistory
|
||||
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
|
||||
from prompt_toolkit.formatted_text import FormattedText
|
||||
from prompt_toolkit.cursor_shapes import CursorShape
|
||||
|
||||
# For syntax highlighting
|
||||
try:
|
||||
from pygments.lexers import get_lexer_by_name, get_lexer_for_filename
|
||||
from pygments.token import Token
|
||||
from pygments import highlight as pygments_highlight
|
||||
PYGMENTS_AVAILABLE = True
|
||||
except ImportError:
|
||||
PYGMENTS_AVAILABLE = False
|
||||
get_lexer_by_name = None
|
||||
get_lexer_for_filename = None
|
||||
pygments_highlight = None
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# STYLING
|
||||
# =============================================================================
|
||||
|
||||
class Colors:
|
||||
"""ANSI color codes for terminal output"""
|
||||
RESET = "\033[0m"
|
||||
BOLD = "\033[1m"
|
||||
DIM = "\033[2m"
|
||||
ITALIC = "\033[3m"
|
||||
|
||||
BLACK = "\033[30m"
|
||||
RED = "\033[31m"
|
||||
GREEN = "\033[32m"
|
||||
YELLOW = "\033[33m"
|
||||
BLUE = "\033[34m"
|
||||
MAGENTA = "\033[35m"
|
||||
CYAN = "\033[36m"
|
||||
WHITE = "\033[37m"
|
||||
|
||||
BRIGHT_BLACK = "\033[90m"
|
||||
BRIGHT_RED = "\033[91m"
|
||||
BRIGHT_GREEN = "\033[92m"
|
||||
BRIGHT_YELLOW = "\033[93m"
|
||||
BRIGHT_BLUE = "\033[94m"
|
||||
BRIGHT_MAGENTA = "\033[95m"
|
||||
BRIGHT_CYAN = "\033[96m"
|
||||
BRIGHT_WHITE = "\033[97m"
|
||||
|
||||
|
||||
class Theme:
|
||||
"""Color themes for the CLI"""
|
||||
|
||||
@staticmethod
|
||||
def get_style(theme_name: str) -> Style:
|
||||
"""Get prompt_toolkit style from theme name"""
|
||||
themes = {
|
||||
"default": {
|
||||
"user": "ansicyan bold",
|
||||
"agent": "ansigreen",
|
||||
"system": "ansiyellow",
|
||||
"error": "ansired bold",
|
||||
"info": "ansiblue",
|
||||
"muted": "ansiblack",
|
||||
"prompt": "ansimagenta bold",
|
||||
"code": "ansiyellow",
|
||||
"url": "ansiblue underline",
|
||||
},
|
||||
"dark": {
|
||||
"user": "cyan bold",
|
||||
"agent": "lightgreen",
|
||||
"system": "yellow",
|
||||
"error": "lightred bold",
|
||||
"info": "lightblue",
|
||||
"muted": "gray",
|
||||
"prompt": "lightmagenta bold",
|
||||
"code": "yellow",
|
||||
"url": "lightblue underline",
|
||||
},
|
||||
"light": {
|
||||
"user": "blue bold",
|
||||
"agent": "darkgreen",
|
||||
"system": "darkyellow",
|
||||
"error": "darkred bold",
|
||||
"info": "darkblue",
|
||||
"muted": "gray",
|
||||
"prompt": "darkmagenta bold",
|
||||
"code": "darkyellow",
|
||||
"url": "darkblue underline",
|
||||
},
|
||||
"monokai": {
|
||||
"user": "cyan bold",
|
||||
"agent": "green",
|
||||
"system": "yellow",
|
||||
"error": "red bold",
|
||||
"info": "blue",
|
||||
"muted": "gray",
|
||||
"prompt": "magenta bold",
|
||||
"code": "yellow",
|
||||
"url": "cyan underline",
|
||||
},
|
||||
}
|
||||
theme_dict = themes.get(theme_name, themes["default"])
|
||||
return Style.from_dict(theme_dict)
|
||||
|
||||
@staticmethod
|
||||
def get_ansi_theme(theme_name: str) -> Dict[str, str]:
|
||||
"""Get ANSI color theme for non-prompt_toolkit output"""
|
||||
themes = {
|
||||
"default": {
|
||||
"user": f"{Colors.CYAN}{Colors.BOLD}",
|
||||
"agent": Colors.GREEN,
|
||||
"system": Colors.YELLOW,
|
||||
"error": f"{Colors.RED}{Colors.BOLD}",
|
||||
"info": Colors.BLUE,
|
||||
"muted": Colors.BRIGHT_BLACK,
|
||||
"prompt": f"{Colors.MAGENTA}{Colors.BOLD}",
|
||||
"code": Colors.YELLOW,
|
||||
"reset": Colors.RESET,
|
||||
},
|
||||
"dark": {
|
||||
"user": Colors.BRIGHT_CYAN,
|
||||
"agent": Colors.BRIGHT_GREEN,
|
||||
"system": Colors.BRIGHT_YELLOW,
|
||||
"error": f"{Colors.BRIGHT_RED}{Colors.BOLD}",
|
||||
"info": Colors.BRIGHT_BLUE,
|
||||
"muted": Colors.BRIGHT_BLACK,
|
||||
"prompt": Colors.BRIGHT_MAGENTA,
|
||||
"code": Colors.YELLOW,
|
||||
"reset": Colors.RESET,
|
||||
},
|
||||
"light": {
|
||||
"user": Colors.BLUE,
|
||||
"agent": "\033[32m", # darkgreen
|
||||
"system": "\033[33m", # darkyellow
|
||||
"error": "\033[31m", # darkred
|
||||
"info": "\033[34m", # darkblue
|
||||
"muted": "\033[90m", # brightblack/gray
|
||||
"prompt": "\033[35m", # darkmagenta
|
||||
"code": "\033[33m",
|
||||
"reset": Colors.RESET,
|
||||
},
|
||||
"monokai": {
|
||||
"user": Colors.CYAN,
|
||||
"agent": Colors.GREEN,
|
||||
"system": Colors.YELLOW,
|
||||
"error": Colors.RED,
|
||||
"info": Colors.BLUE,
|
||||
"muted": Colors.BRIGHT_BLACK,
|
||||
"prompt": Colors.MAGENTA,
|
||||
"code": Colors.YELLOW,
|
||||
"reset": Colors.RESET,
|
||||
},
|
||||
}
|
||||
return themes.get(theme_name, themes["default"])
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# SYNTAX HIGHLIGHTING
|
||||
# =============================================================================
|
||||
|
||||
class SyntaxHighlighter:
|
||||
"""Simple syntax highlighter for code blocks"""
|
||||
|
||||
# Language aliases
|
||||
LANG_ALIASES = {
|
||||
"py": "python",
|
||||
"js": "javascript",
|
||||
"ts": "typescript",
|
||||
"rb": "ruby",
|
||||
"sh": "bash",
|
||||
"zsh": "bash",
|
||||
"ps": "powershell",
|
||||
"yml": "yaml",
|
||||
"md": "markdown",
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get_lexer(cls, lang: str):
|
||||
"""Get pygments lexer for language"""
|
||||
if not PYGMENTS_AVAILABLE:
|
||||
return None
|
||||
|
||||
lang = cls.LANG_ALIASES.get(lang.lower(), lang.lower())
|
||||
try:
|
||||
return get_lexer_by_name(lang)
|
||||
except:
|
||||
try:
|
||||
return get_lexer_for_filename(f"file.{lang}")
|
||||
except:
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def highlight(cls, code: str, lang: str = "text") -> str:
|
||||
"""Apply syntax highlighting to code"""
|
||||
if not PYGMENTS_AVAILABLE:
|
||||
return code
|
||||
|
||||
lexer = cls.get_lexer(lang)
|
||||
if not lexer:
|
||||
return code
|
||||
|
||||
from pygments import highlight as pygments_highlight
|
||||
from pygments.formatters import Terminal256Formatter
|
||||
formatter = Terminal256Formatter()
|
||||
|
||||
try:
|
||||
highlighted = pygments_highlight(code, lexer, formatter)
|
||||
return highlighted.rstrip()
|
||||
except:
|
||||
return code
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# MAIN ENHANCED CLI CLASS
|
||||
# =============================================================================
|
||||
|
||||
class EnhancedCLI:
|
||||
"""
|
||||
Enhanced Command Line Interface for LangCode Agent.
|
||||
|
||||
Features:
|
||||
- Colored input/output with themes
|
||||
- Command history (persistent)
|
||||
- Autocomplete for commands and paths
|
||||
- Multi-line input support
|
||||
- Code syntax highlighting
|
||||
- Mouse support for scrolling
|
||||
- Copy/paste support
|
||||
- Signal handling (Ctrl+C, Ctrl+L)
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
history_file: str = "./.langcode/.cli_history",
|
||||
theme: str = "default",
|
||||
welcome_message: Optional[str] = None,
|
||||
show_timestamps: bool = True,
|
||||
enable_mouse: bool = True,
|
||||
enable_syntax_highlight: bool = True,
|
||||
terminal_width: Optional[int] = None,
|
||||
):
|
||||
"""
|
||||
Initialize Enhanced CLI.
|
||||
|
||||
Args:
|
||||
history_file: Path to persist command history
|
||||
theme: Color theme (default, dark, light, monokai)
|
||||
welcome_message: Initial message to show
|
||||
show_timestamps: Show timestamps in output
|
||||
enable_mouse: Enable mouse for scrolling
|
||||
enable_syntax_highlight: Highlight code blocks
|
||||
terminal_width: Force terminal width
|
||||
"""
|
||||
self.history_file = history_file
|
||||
self.theme = theme
|
||||
self.show_timestamps = show_timestamps
|
||||
self.enable_mouse = enable_mouse
|
||||
self.enable_syntax_highlight = enable_syntax_highlight and PYGMENTS_AVAILABLE
|
||||
|
||||
# Get terminal size
|
||||
self.terminal_width = terminal_width or shutil.get_terminal_size().columns
|
||||
|
||||
# ANSI theme for non-prompt output
|
||||
self.ansi_theme = Theme.get_ansi_theme(theme)
|
||||
|
||||
# Welcome message
|
||||
self.welcome_message = welcome_message or self._default_welcome()
|
||||
|
||||
# Ensure history directory exists
|
||||
os.makedirs(os.path.dirname(history_file) or ".", exist_ok=True)
|
||||
|
||||
# Command definitions
|
||||
self.commands = {
|
||||
"/help": "Mostra aiuto",
|
||||
"/status": "Stato sistema",
|
||||
"/clear": "Pulisci schermo",
|
||||
"/theme": "Cicla tra i temi",
|
||||
"/history": "Mostra cronologia",
|
||||
"/theme-default": "Tema default",
|
||||
"/theme-dark": "Tema dark",
|
||||
"/theme-light": "Tema light",
|
||||
"/theme-monokai": "Tema monokai",
|
||||
"/timestamps": "Toggle timestamps",
|
||||
"/syntax": "Toggle syntax highlighting",
|
||||
"/width": "Show terminal width",
|
||||
"/exit": "Esci",
|
||||
"/quit": "Esci",
|
||||
}
|
||||
|
||||
# Path completer for commands that need paths
|
||||
self.path_completer = PathCompleter()
|
||||
|
||||
# Create key bindings
|
||||
self.key_bindings = self._create_key_bindings()
|
||||
|
||||
# Create prompt session
|
||||
self.session = self._create_session()
|
||||
|
||||
# Current theme name
|
||||
self.current_theme = theme
|
||||
|
||||
# Statistics
|
||||
self.stats = {
|
||||
"commands_executed": 0,
|
||||
"session_start": datetime.now(),
|
||||
}
|
||||
|
||||
# Callback for Ctrl+L (clear)
|
||||
self._on_clear: Optional[Callable] = None
|
||||
|
||||
def _default_welcome(self) -> str:
|
||||
"""Generate default welcome message"""
|
||||
theme = self.ansi_theme
|
||||
w = self.terminal_width
|
||||
|
||||
# Box drawing
|
||||
top = f"{theme['prompt']}╔{'═' * (w - 4)}╗{theme['reset']}"
|
||||
bottom = f"{theme['prompt']}╚{'═' * (w - 4)}╝{theme['reset']}"
|
||||
|
||||
lines = [
|
||||
f"{theme['prompt']}║{theme['reset']} {theme['agent']}{Colors.BOLD}LangCode Agent - Enhanced CLI{theme['reset']}" + " " * (w - 43) + f"{theme['prompt']}║{theme['reset']}",
|
||||
f"{theme['prompt']}║{theme['reset']}" + " " * (w - 2) + f"{theme['prompt']}║{theme['reset']}",
|
||||
f"{theme['prompt']}║{theme['reset']} {theme['system']}Comandi:{theme['reset']}" + " " * (w - 17) + f"{theme['prompt']}║{theme['reset']}",
|
||||
f"{theme['prompt']}║{theme['reset']} {theme['user']}/help{theme['reset']} - Mostra aiuto {theme['user']}/clear{theme['reset']} - Pulisci {theme['prompt']}║{theme['reset']}",
|
||||
f"{theme['prompt']}║{theme['reset']} {theme['user']}/status{theme['reset']} - Stato sistema {theme['user']}/theme{theme['reset']} - Cambia tema {theme['prompt']}║{theme['reset']}",
|
||||
f"{theme['prompt']}║{theme['reset']} {theme['user']}/exit{theme['reset']} - Esci {theme['prompt']}║{theme['reset']}",
|
||||
f"{theme['prompt']}║{theme['reset']}" + " " * (w - 2) + f"{theme['prompt']}║{theme['reset']}",
|
||||
f"{theme['prompt']}║{theme['reset']} {theme['info']}Funzionalità:{theme['reset']}" + " " * (w - 22) + f"{theme['prompt']}║{theme['reset']}",
|
||||
f"{theme['prompt']}║{theme['reset']} • History (↑↓) • Autocomplete (Tab) • Colors {theme['prompt']}║{theme['reset']}",
|
||||
f"{theme['prompt']}║{theme['reset']} • Syntax highlighting • Mouse scroll • Multiline {theme['prompt']}║{theme['reset']}",
|
||||
f"{theme['prompt']}║{theme['reset']}" + " " * (w - 2) + f"{theme['prompt']}║{theme['reset']}",
|
||||
]
|
||||
|
||||
return "\n".join([top] + lines + [bottom])
|
||||
|
||||
def _create_key_bindings(self) -> KeyBindings:
|
||||
"""Create custom key bindings"""
|
||||
kb = KeyBindings()
|
||||
|
||||
@kb.add("c-c")
|
||||
def _(event):
|
||||
"""Handle Ctrl+C - interrupt or exit"""
|
||||
# If there's text, clear it; otherwise exit
|
||||
if event.app.current_buffer.text:
|
||||
event.app.current_buffer.text = ""
|
||||
else:
|
||||
event.app.exit(exception=KeyboardInterrupt)
|
||||
|
||||
@kb.add("c-l")
|
||||
def _(event):
|
||||
"""Handle Ctrl+L - clear screen"""
|
||||
os.system("clear" if os.name != "nt" else "cls")
|
||||
if self._on_clear:
|
||||
self._on_clear()
|
||||
|
||||
@kb.add("c-a")
|
||||
def _(event):
|
||||
"""Handle Ctrl+A - go to start of line"""
|
||||
buffer = event.app.current_buffer
|
||||
buffer.cursor_position = 0
|
||||
|
||||
@kb.add("c-e")
|
||||
def _(event):
|
||||
"""Handle Ctrl+E - go to end of line"""
|
||||
buffer = event.app.current_buffer
|
||||
buffer.cursor_position = len(buffer.text)
|
||||
|
||||
@kb.add("c-k")
|
||||
def _(event):
|
||||
"""Handle Ctrl+K - kill line to end"""
|
||||
buffer = event.app.current_buffer
|
||||
buffer.text = buffer.text[:buffer.cursor_position]
|
||||
|
||||
@kb.add("c-u")
|
||||
def _(event):
|
||||
"""Handle Ctrl+U - kill line from start"""
|
||||
buffer = event.app.current_buffer
|
||||
buffer.text = buffer.text[buffer.cursor_position:]
|
||||
buffer.cursor_position = 0
|
||||
|
||||
@kb.add("c-w")
|
||||
def _(event):
|
||||
"""Handle Ctrl+W - kill word before cursor"""
|
||||
buffer = event.app.current_buffer
|
||||
text = buffer.text[:buffer.cursor_position]
|
||||
if text and text[-1] == " ":
|
||||
buffer.text = text[:-1] + buffer.text[buffer.cursor_position:]
|
||||
buffer.cursor_position = len(buffer.text) - (len(buffer.text) - buffer.cursor_position + 1)
|
||||
else:
|
||||
words = text.split()
|
||||
if words:
|
||||
remaining = " ".join(words[:-1]) + (" " if len(words) > 1 else "")
|
||||
buffer.text = remaining + buffer.text[buffer.cursor_position:]
|
||||
buffer.cursor_position = len(remaining)
|
||||
|
||||
# Note: Mouse support is enabled via mouse_support parameter in PromptSession
|
||||
# Scroll events are handled automatically by prompt_toolkit
|
||||
|
||||
return kb
|
||||
|
||||
def _create_session(self) -> PromptSession:
|
||||
"""Create prompt session with history and autocomplete"""
|
||||
|
||||
# Create nested completer for commands
|
||||
command_completer = NestedCompleter.from_nested_dict({
|
||||
cmd: None for cmd in self.commands.keys()
|
||||
})
|
||||
|
||||
return PromptSession(
|
||||
history=FileHistory(self.history_file),
|
||||
completer=command_completer,
|
||||
key_bindings=self.key_bindings,
|
||||
auto_suggest=AutoSuggestFromHistory(),
|
||||
multiline=True,
|
||||
mouse_support=self.enable_mouse,
|
||||
)
|
||||
|
||||
def _get_prompt_tokens(self) -> FormattedText:
|
||||
"""Create prompt tokens with colors and optional info"""
|
||||
theme = self.ansi_theme
|
||||
timestamp = datetime.now().strftime("%H:%M") if self.show_timestamps else None
|
||||
|
||||
tokens = []
|
||||
|
||||
# Prompt symbol with theme
|
||||
tokens.append(("class:prompt", f"{theme['prompt']}❯ {theme['reset']}"))
|
||||
tokens.append(("class:user", f"{theme['user']}TUO{theme['reset']}"))
|
||||
|
||||
if timestamp:
|
||||
tokens.append(("class:muted", f" [{timestamp}]"))
|
||||
|
||||
tokens.append(("class:prompt", f" {theme['prompt']}»{theme['reset']} "))
|
||||
|
||||
return FormattedText(tokens)
|
||||
|
||||
def _print_with_timestamp(self, message: str, prefix: str = "") -> None:
|
||||
"""Print message with optional timestamp"""
|
||||
if self.show_timestamps:
|
||||
ts = datetime.now().strftime("%H:%M:%S")
|
||||
print(f"{self.ansi_theme['muted']}[{ts}]{self.ansi_theme['reset']} {prefix}{message}")
|
||||
else:
|
||||
print(f"{prefix}{message}")
|
||||
|
||||
def _print_formatted(self, message: str, level: str = "info") -> None:
|
||||
"""Print formatted message with color and optional timestamp"""
|
||||
theme = self.ansi_theme
|
||||
colors = {
|
||||
"user": theme["user"],
|
||||
"agent": theme["agent"],
|
||||
"system": theme["system"],
|
||||
"error": theme["error"],
|
||||
"info": theme["info"],
|
||||
}
|
||||
color = colors.get(level, theme["reset"])
|
||||
prefix_map = {
|
||||
"user": f"{color}👤{theme['reset']} ",
|
||||
"agent": f"{color}🤖{theme['reset']} ",
|
||||
"system": f"{color}🔧{theme['reset']} ",
|
||||
"error": f"{color}❌{theme['reset']} ",
|
||||
"info": f"{color}ℹ️{theme['reset']} ",
|
||||
}
|
||||
prefix = prefix_map.get(level, "")
|
||||
self._print_with_timestamp(message, prefix)
|
||||
|
||||
def _print_user(self, message: str) -> None:
|
||||
"""Print user message"""
|
||||
self._print_formatted(message, "user")
|
||||
|
||||
def _print_agent(self, message: str) -> None:
|
||||
"""Print agent response with optional code highlighting"""
|
||||
if self.enable_syntax_highlight:
|
||||
message = self._highlight_code_blocks(message)
|
||||
self._print_formatted(message, "agent")
|
||||
|
||||
def _print_system(self, message: str) -> None:
|
||||
"""Print system message"""
|
||||
self._print_formatted(message, "system")
|
||||
|
||||
def _print_error(self, message: str) -> None:
|
||||
"""Print error message"""
|
||||
self._print_formatted(message, "error")
|
||||
|
||||
def _print_info(self, message: str) -> None:
|
||||
"""Print info message"""
|
||||
self._print_formatted(message, "info")
|
||||
|
||||
def _highlight_code_blocks(self, text: str) -> str:
|
||||
"""Apply syntax highlighting to code blocks in text"""
|
||||
if not self.enable_syntax_highlight:
|
||||
return text
|
||||
|
||||
# Pattern: ```language\ncode\n```
|
||||
code_block_pattern = r"```(\w+)?\n(.*?)```"
|
||||
|
||||
def replace_block(match):
|
||||
lang = match.group(1) or "text"
|
||||
code = match.group(2)
|
||||
|
||||
if PYGMENTS_AVAILABLE and lang.lower() not in ["text", "txt", "ansi"]:
|
||||
try:
|
||||
lexer = SyntaxHighlighter.get_lexer(lang)
|
||||
if lexer:
|
||||
from pygments import highlight as pygments_highlight
|
||||
from pygments.formatters import Terminal256Formatter
|
||||
highlighted = pygments_highlight(code, lexer, Terminal256Formatter())
|
||||
return highlighted.rstrip()
|
||||
except:
|
||||
pass
|
||||
|
||||
# No highlighting available, just return code with a header
|
||||
return f"{self.ansi_theme['code']}--- {lang or 'code'} ---\n{code}{self.ansi_theme['reset']}"
|
||||
|
||||
return re.sub(code_block_pattern, replace_block, text, flags=re.DOTALL)
|
||||
|
||||
def _handle_command(self, command: str) -> Optional[bool]:
|
||||
"""Handle special commands (/command)."""
|
||||
cmd_lower = command.lower().strip()
|
||||
|
||||
if cmd_lower == "/help":
|
||||
self._print_system("""
|
||||
╔══════════════════════════════════════════════════════════════════╗
|
||||
║ COMANDI DISPONIBILI ║
|
||||
╠══════════════════════════════════════════════════════════════════╣
|
||||
║ /help - Mostra questo messaggio ║
|
||||
║ /status - Mostra stato del sistema ║
|
||||
║ /clear, Ctrl+L - Pulisci lo schermo ║
|
||||
║ /theme - Cicla tra i temi ║
|
||||
║ /theme-default - Tema default ║
|
||||
║ /theme-dark - Tema scuro ║
|
||||
║ /theme-light - Tema chiaro ║
|
||||
║ /theme-monokai - Tema monokai ║
|
||||
║ /timestamps - Toggle timestamps ║
|
||||
║ /syntax - Toggle syntax highlighting ║
|
||||
║ /width - Mostra larghezza terminale ║
|
||||
║ /history - Mostra cronologia comandi ║
|
||||
║ /exit, Ctrl+C - Esci dall'agente ║
|
||||
╠══════════════════════════════════════════════════════════════════╣
|
||||
║ SHORTCUTS: ║
|
||||
║ Ctrl+A/E - Inizio/fine riga ║
|
||||
║ Ctrl+K/U - Kill riga da cursore/fino a cursore ║
|
||||
║ Ctrl+W - Kill parola precedente ║
|
||||
║ ↑/↓ - History navigation ║
|
||||
║ Tab - Autocomplete ║
|
||||
║ Shift+Tab - Show suggestions ║
|
||||
╚══════════════════════════════════════════════════════════════════╝
|
||||
""")
|
||||
return None
|
||||
|
||||
elif cmd_lower == "/status":
|
||||
uptime = datetime.now() - self.stats["session_start"]
|
||||
self._print_info(f"Theme: {self.current_theme}")
|
||||
self._print_info(f"Timestamps: {'On' if self.show_timestamps else 'Off'}")
|
||||
self._print_info(f"Syntax highlighting: {'On' if self.enable_syntax_highlight else 'Off'}")
|
||||
self._print_info(f"Commands executed: {self.stats['commands_executed']}")
|
||||
self._print_info(f"Session uptime: {uptime}")
|
||||
self._print_info(f"Terminal width: {self.terminal_width}")
|
||||
self._print_info(f"History file: {self.history_file}")
|
||||
return None
|
||||
|
||||
elif cmd_lower == "/clear":
|
||||
os.system("clear" if os.name != "nt" else "cls")
|
||||
return None
|
||||
|
||||
elif cmd_lower in ["/theme", "/theme-default"]:
|
||||
self.current_theme = "default"
|
||||
self.ansi_theme = Theme.get_ansi_theme("default")
|
||||
self._print_info("Tema cambiato a: default")
|
||||
return None
|
||||
|
||||
elif cmd_lower == "/theme-dark":
|
||||
self.current_theme = "dark"
|
||||
self.ansi_theme = Theme.get_ansi_theme("dark")
|
||||
self._print_info("Tema cambiato a: dark")
|
||||
return None
|
||||
|
||||
elif cmd_lower == "/theme-light":
|
||||
self.current_theme = "light"
|
||||
self.ansi_theme = Theme.get_ansi_theme("light")
|
||||
self._print_info("Tema cambiato a: light")
|
||||
return None
|
||||
|
||||
elif cmd_lower == "/theme-monokai":
|
||||
self.current_theme = "monokai"
|
||||
self.ansi_theme = Theme.get_ansi_theme("monokai")
|
||||
self._print_info("Tema cambiato a: monokai")
|
||||
return None
|
||||
|
||||
elif cmd_lower == "/timestamps":
|
||||
self.show_timestamps = not self.show_timestamps
|
||||
self._print_info(f"Timestamps: {'On' if self.show_timestamps else 'Off'}")
|
||||
return None
|
||||
|
||||
elif cmd_lower == "/syntax":
|
||||
self.enable_syntax_highlight = not self.enable_syntax_highlight
|
||||
self._print_info(f"Syntax highlighting: {'On' if self.enable_syntax_highlight else 'Off'}")
|
||||
return None
|
||||
|
||||
elif cmd_lower == "/width":
|
||||
self.terminal_width = shutil.get_terminal_size().columns
|
||||
self._print_info(f"Terminal width: {self.terminal_width}")
|
||||
return None
|
||||
|
||||
elif cmd_lower in ["/exit", "/quit"]:
|
||||
return True
|
||||
|
||||
elif cmd_lower == "/history":
|
||||
try:
|
||||
with open(self.history_file, "r") as f:
|
||||
lines = f.readlines()
|
||||
if lines:
|
||||
self._print_system(f"Ultimi {min(20, len(lines))} comandi:")
|
||||
for i, line in enumerate(lines[-20:], 1):
|
||||
print(f" {i:3d}. {line.strip()}")
|
||||
else:
|
||||
self._print_info("Nessuna cronologia")
|
||||
except FileNotFoundError:
|
||||
self._print_info("Nessuna cronologia")
|
||||
return None
|
||||
|
||||
return None # Command not recognized
|
||||
|
||||
def prompt(self) -> str:
|
||||
"""Get input from user with enhanced UI"""
|
||||
try:
|
||||
text = self.session.prompt(
|
||||
self._get_prompt_tokens(),
|
||||
style=Theme.get_style(self.current_theme),
|
||||
)
|
||||
return text
|
||||
except KeyboardInterrupt:
|
||||
return ""
|
||||
except EOFError:
|
||||
return "/exit"
|
||||
|
||||
def run(self, agent_callback: Callable[[str], str]):
|
||||
"""
|
||||
Run the interactive CLI loop.
|
||||
|
||||
Args:
|
||||
agent_callback: Function that takes user input and returns agent response
|
||||
"""
|
||||
import signal
|
||||
import sys
|
||||
|
||||
# Register signal handler for Ctrl+C
|
||||
def handle_sigint(signum, frame):
|
||||
print("\n\n👋 Arrivederci!")
|
||||
sys.exit(0)
|
||||
|
||||
signal.signal(signal.SIGINT, handle_sigint)
|
||||
|
||||
# Print welcome
|
||||
print(self.welcome_message)
|
||||
|
||||
while True:
|
||||
try:
|
||||
# Get user input
|
||||
user_input = self.prompt().strip()
|
||||
|
||||
if not user_input:
|
||||
continue
|
||||
|
||||
# Show what user typed
|
||||
self._print_user(user_input)
|
||||
self.stats["commands_executed"] += 1
|
||||
|
||||
# Check for commands
|
||||
if user_input.startswith("/"):
|
||||
should_exit = self._handle_command(user_input)
|
||||
if should_exit:
|
||||
break
|
||||
continue
|
||||
|
||||
# Call agent and show response
|
||||
try:
|
||||
response = agent_callback(user_input)
|
||||
self._print_agent(response)
|
||||
except KeyboardInterrupt:
|
||||
self._print_system("\nInterruzione... Riprova.")
|
||||
except Exception as e:
|
||||
self._print_error(f"Errore: {str(e)}")
|
||||
|
||||
except KeyboardInterrupt:
|
||||
self._print_system("\n👋 Arrivederci!")
|
||||
break
|
||||
except EOFError:
|
||||
break
|
||||
|
||||
self._print_system("Sessione terminata.")
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# FACTORY FUNCTION
|
||||
# =============================================================================
|
||||
|
||||
def create_enhanced_cli(
|
||||
history_file: str = "./.langcode/.cli_history",
|
||||
theme: str = "default",
|
||||
**kwargs
|
||||
) -> EnhancedCLI:
|
||||
"""Create an EnhancedCLI instance"""
|
||||
return EnhancedCLI(
|
||||
history_file=history_file,
|
||||
theme=theme,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# STANDALONE TEST
|
||||
# =============================================================================
|
||||
|
||||
if __name__ == "__main__":
|
||||
def test_callback(text):
|
||||
return f"Hai detto: {text}\n\n```python\nprint('Hello!')\n```"
|
||||
|
||||
cli = create_enhanced_cli(theme="monokai")
|
||||
cli.run(test_callback)
|
||||
@@ -0,0 +1,40 @@
|
||||
# Enhancements for LangCode Agent based on LangChain Deep Agents analysis
|
||||
|
||||
This directory contains planned enhancements to bring LangCode Agent closer to the architecture patterns revealed in LangChain's Deep Agents framework.
|
||||
|
||||
## Planned Enhancements
|
||||
|
||||
1. **Enhanced Memory Systems**
|
||||
- Add VectorStoreRetrieverMemory for semantic recall
|
||||
- Implement SummaryBufferMemory for conversation summarization
|
||||
- Add persistent memory storage for cross-session learning
|
||||
|
||||
2. **Advanced Planning & Reasoning**
|
||||
- Implement plan-and-execute agent pattern
|
||||
- Add self-reflection capabilities
|
||||
- Improve task decomposition and tracking
|
||||
|
||||
3. **Production Observability**
|
||||
- Add LangChain-style callbacks for tracing
|
||||
- Implement tool usage monitoring
|
||||
- Add performance metrics collection
|
||||
|
||||
4. **Advanced Tool Composition**
|
||||
- Implement tool chaining capabilities
|
||||
- Add async tool execution for parallelism
|
||||
- Enhance input validation and error handling
|
||||
|
||||
5. **Agent Hierarchies**
|
||||
- Develop specialist subagents for specific domains
|
||||
- Implement agent routing based on task analysis
|
||||
- Add agent-to-agent communication patterns
|
||||
|
||||
## Implementation Approach
|
||||
|
||||
Each enhancement will be implemented as a separate module that can be optionally integrated into the main agent, maintaining backward compatibility while extending capabilities.
|
||||
|
||||
The enhancements will follow the same patterns already established in the codebase:
|
||||
- Modular design
|
||||
- Configuration-driven behavior
|
||||
- Clear separation of concerns
|
||||
- Comprehensive error handling
|
||||
@@ -0,0 +1,317 @@
|
||||
"""
|
||||
Enhanced memory systems for LangCode Agent
|
||||
Implements advanced memory patterns from LangChain Deep Agents
|
||||
"""
|
||||
import os
|
||||
import json
|
||||
from typing import Dict, Any, List, Optional
|
||||
|
||||
# Try to import from langchain_core, fallback to community if needed
|
||||
try:
|
||||
from langchain_core.memory import BaseMemory
|
||||
from langchain_core.vectorstores import VectorStore
|
||||
from langchain_core.embeddings import Embeddings
|
||||
from langchain_core.documents import Document
|
||||
from langchain_core.storage import BaseStore
|
||||
from langchain_core.retrievers import BaseRetriever
|
||||
LANGCHAIN_CORE_AVAILABLE = True
|
||||
except ImportError:
|
||||
LANGCHAIN_CORE_AVAILABLE = False
|
||||
# We'll handle this gracefully in the implementation
|
||||
|
||||
# Try to import OllamaEmbeddings
|
||||
try:
|
||||
from langchain_community.embeddings import OllamaEmbeddings
|
||||
OLLAMA_AVAILABLE = True
|
||||
except ImportError:
|
||||
OLLAMA_AVAILABLE = False
|
||||
OllamaEmbeddings = None # type: ignore
|
||||
|
||||
# Try to import FAISS for vector store
|
||||
try:
|
||||
from langchain_community.vectorstores import FAISS
|
||||
FAISS_AVAILABLE = True
|
||||
except ImportError:
|
||||
FAISS_AVAILABLE = False
|
||||
FAISS = None # type: ignore
|
||||
|
||||
# Try to import memory components
|
||||
try:
|
||||
from langchain_core.memory import VectorStoreRetrieverMemory, SummaryBufferMemory
|
||||
MEMORY_COMPONENTS_AVAILABLE = True
|
||||
except ImportError:
|
||||
MEMORY_COMPONENTS_AVAILABLE = False
|
||||
VectorStoreRetrieverMemory = None # type: ignore
|
||||
SummaryBufferMemory = None # type: ignore
|
||||
|
||||
from langchain_core.tools import Tool
|
||||
|
||||
class EnhancedMemoryManager:
|
||||
"""Manages enhanced memory systems for the LangCode Agent"""
|
||||
|
||||
def __init__(self,
|
||||
embedding_model: str = "nomic-embed-text",
|
||||
ollama_base_url: str = "http://localhost:11434",
|
||||
persist_directory: str = "./.langcode/memory"):
|
||||
"""
|
||||
Initialize the enhanced memory manager
|
||||
|
||||
Args:
|
||||
embedding_model: Name of the embedding model to use
|
||||
ollama_base_url: URL for Ollama service
|
||||
persist_directory: Directory to persist memory data
|
||||
"""
|
||||
self.embedding_model = embedding_model
|
||||
self.ollama_base_url = ollama_base_url
|
||||
self.persist_directory = persist_directory
|
||||
|
||||
# Ensure persist directory exists
|
||||
os.makedirs(persist_directory, exist_ok=True)
|
||||
|
||||
# Initialize components if available
|
||||
self.embeddings = None
|
||||
self.vectorstore = None
|
||||
self.summary_memory = None
|
||||
self.vector_memory = None
|
||||
self.docstore = None
|
||||
|
||||
self._initialize_memory_systems()
|
||||
|
||||
def _initialize_memory_systems(self):
|
||||
"""Initialize all memory systems if dependencies are available"""
|
||||
if not (OLLAMA_AVAILABLE and FAISS_AVAILABLE and MEMORY_COMPONENTS_AVAILABLE):
|
||||
missing = []
|
||||
if not OLLAMA_AVAILABLE:
|
||||
missing.append("Ollama embeddings")
|
||||
if not FAISS_AVAILABLE:
|
||||
missing.append("FAISS vector store")
|
||||
if not MEMORY_COMPONENTS_AVAILABLE:
|
||||
missing.append("Memory components")
|
||||
print(f"Warning: Enhanced memory features limited due to missing: {', '.join(missing)}")
|
||||
return
|
||||
|
||||
try:
|
||||
# Initialize embeddings
|
||||
self.embeddings = OllamaEmbeddings(
|
||||
model=self.embedding_model,
|
||||
base_url=self.ollama_base_url
|
||||
)
|
||||
|
||||
# Initialize docstore for storing documents
|
||||
from langchain.storage import InMemoryStore
|
||||
self.docstore = InMemoryStore()
|
||||
|
||||
# Initialize vector store for semantic memory
|
||||
self.vectorstore = FAISS(
|
||||
embedding_function=self.embeddings,
|
||||
index=None,
|
||||
docstore=self.docstore,
|
||||
index_to_docstore_id={}
|
||||
)
|
||||
|
||||
# Initialize vector store memory
|
||||
self.vector_memory = VectorStoreRetrieverMemory(
|
||||
retriever=self.vectorstore.as_retriever(search_kwargs={"k": 3})
|
||||
)
|
||||
|
||||
# Initialize summary memory for conversation compression
|
||||
self.summary_memory = SummaryBufferMemory(
|
||||
max_token_limit=1000
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Warning: Failed to initialize enhanced memory systems: {e}")
|
||||
# Reset to None if initialization failed
|
||||
self.embeddings = None
|
||||
self.vectorstore = None
|
||||
self.summary_memory = None
|
||||
self.vector_memory = None
|
||||
self.docstore = None
|
||||
|
||||
def set_llm_for_summary_memory(self, llm):
|
||||
"""Set the LLM for summary memory"""
|
||||
if self.summary_memory and hasattr(self.summary_memory, 'llm'):
|
||||
self.summary_memory.llm = llm
|
||||
|
||||
def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, Any]) -> None:
|
||||
"""
|
||||
Save context to all memory systems
|
||||
|
||||
Args:
|
||||
inputs: Input values from the conversation
|
||||
outputs: Output values from the conversation
|
||||
"""
|
||||
try:
|
||||
# Save to vector memory
|
||||
if self.vector_memory:
|
||||
self.vector_memory.save_context(inputs, outputs)
|
||||
|
||||
# Save to summary memory
|
||||
if self.summary_memory:
|
||||
self.summary_memory.save_context(inputs, outputs)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Warning: Failed to save context to memory: {e}")
|
||||
|
||||
def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Load memory variables from all memory systems
|
||||
|
||||
Args:
|
||||
inputs: Input values for context retrieval
|
||||
|
||||
Returns:
|
||||
Dictionary containing memory variables
|
||||
"""
|
||||
result = {}
|
||||
|
||||
try:
|
||||
# Load from vector memory
|
||||
if self.vector_memory:
|
||||
vector_result = self.vector_memory.load_memory_variables(inputs)
|
||||
result.update(vector_result)
|
||||
|
||||
# Load from summary memory
|
||||
if self.summary_memory:
|
||||
summary_result = self.summary_memory.load_memory_variables(inputs)
|
||||
result.update(summary_result)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Warning: Failed to load memory variables: {e}")
|
||||
|
||||
return result
|
||||
|
||||
def add_document(self, content: str, metadata: Optional[Dict[str, Any]] = None) -> None:
|
||||
"""
|
||||
Add a document to the vector store for long-term memory
|
||||
|
||||
Args:
|
||||
content: Document content to store
|
||||
metadata: Optional metadata for the document
|
||||
"""
|
||||
if not self.vectorstore:
|
||||
return
|
||||
|
||||
try:
|
||||
doc = Document(page_content=content, metadata=metadata or {})
|
||||
self.vectorstore.add_documents([doc])
|
||||
except Exception as e:
|
||||
print(f"Warning: Failed to add document to vector store: {e}")
|
||||
|
||||
def search_similar(self, query: str, k: int = 3) -> List[Document]:
|
||||
"""
|
||||
Search for similar documents in vector store
|
||||
|
||||
Args:
|
||||
query: Search query
|
||||
k: Number of results to return
|
||||
|
||||
Returns:
|
||||
List of similar documents
|
||||
"""
|
||||
if not self.vectorstore:
|
||||
return []
|
||||
|
||||
try:
|
||||
return self.vectorstore.similarity_search(query, k=k)
|
||||
except Exception as e:
|
||||
print(f"Warning: Failed to search vector store: {e}")
|
||||
return []
|
||||
|
||||
def clear(self) -> None:
|
||||
"""Clear all memory systems"""
|
||||
try:
|
||||
if self.vector_memory:
|
||||
self.vector_memory.clear()
|
||||
if self.summary_memory:
|
||||
self.summary_memory.clear()
|
||||
# Reinitialize vector store
|
||||
self._initialize_memory_systems()
|
||||
except Exception as e:
|
||||
print(f"Warning: Failed to clear memory systems: {e}")
|
||||
|
||||
def create_memory_tools(memory_manager: EnhancedMemoryManager) -> List[Tool]:
|
||||
"""
|
||||
Create LangChain tools for the enhanced memory system
|
||||
|
||||
Args:
|
||||
memory_manager: Instance of EnhancedMemoryManager
|
||||
|
||||
Returns:
|
||||
List of memory tools
|
||||
"""
|
||||
|
||||
def add_memory_func(content: str, metadata: str = "{}") -> str:
|
||||
try:
|
||||
metadata_dict = json.loads(metadata) if metadata.strip() else {}
|
||||
memory_manager.add_document(content, metadata_dict)
|
||||
return f"Added content to memory: {content[:50]}{'...' if len(content) > 50 else ''}"
|
||||
except Exception as e:
|
||||
return f"Error adding to memory: {str(e)}"
|
||||
|
||||
def search_memory_func(query: str, k: str = "3") -> str:
|
||||
try:
|
||||
k_int = int(k)
|
||||
results = memory_manager.search_similar(query, k_int)
|
||||
if not results:
|
||||
return "No similar memories found."
|
||||
|
||||
result = f"Found {len(results)} similar memories:\n"
|
||||
for i, doc in enumerate(results, 1):
|
||||
result += f"{i}. {doc.page_content[:100]}{'...' if len(doc.page_content) > 100 else ''}\n"
|
||||
if doc.metadata:
|
||||
result += f" Metadata: {doc.metadata}\n"
|
||||
return result
|
||||
except Exception as e:
|
||||
return f"Error searching memory: {str(e)}"
|
||||
|
||||
def clear_memory_func() -> str:
|
||||
try:
|
||||
memory_manager.clear()
|
||||
return "Memory cleared successfully."
|
||||
except Exception as e:
|
||||
return f"Error clearing memory: {str(e)}"
|
||||
|
||||
def get_memory_stats_func() -> str:
|
||||
try:
|
||||
stats = []
|
||||
if memory_manager.vectorstore:
|
||||
# Try to get vector count
|
||||
try:
|
||||
count = memory_manager.vectorstore.index.ntotal if hasattr(memory_manager.vectorstore.index, 'ntotal') else 'unknown'
|
||||
stats.append(f"Vector store: {count} vectors")
|
||||
except:
|
||||
stats.append("Vector store: active")
|
||||
if memory_manager.summary_memory:
|
||||
stats.append("Summary memory: active")
|
||||
if memory_manager.vector_memory:
|
||||
stats.append("Vector memory: active")
|
||||
return "Memory Systems:\n" + "\n".join(f" - {s}" for s in stats)
|
||||
except Exception as e:
|
||||
return f"Error getting memory stats: {str(e)}"
|
||||
|
||||
return [
|
||||
Tool(
|
||||
name="add_to_memory",
|
||||
description="Add content to long-term memory. Provide content and optional metadata as JSON string.",
|
||||
func=add_memory_func
|
||||
),
|
||||
Tool(
|
||||
name="search_memory",
|
||||
description="Search long-term memory for similar content. Provide query and optional k (number of results, default 3).",
|
||||
func=search_memory_func
|
||||
),
|
||||
Tool(
|
||||
name="clear_memory",
|
||||
description="Clear all long-term memory.",
|
||||
func=clear_memory_func
|
||||
),
|
||||
Tool(
|
||||
name="memory_stats",
|
||||
description="Get statistics about the memory systems.",
|
||||
func=get_memory_stats_func
|
||||
)
|
||||
]
|
||||
|
||||
# Global instance for easy access
|
||||
enhanced_memory_manager = EnhancedMemoryManager()
|
||||
@@ -0,0 +1,364 @@
|
||||
"""
|
||||
Enhanced planning systems for LangCode Agent
|
||||
Implements advanced planning patterns from LangChain Deep Agents
|
||||
"""
|
||||
import json
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from typing import Dict, List, Any, Optional, Tuple
|
||||
from enum import Enum
|
||||
from dataclasses import dataclass, asdict, field
|
||||
from langchain_core.tools import Tool
|
||||
|
||||
class TaskStatus(Enum):
|
||||
PENDING = "pending"
|
||||
IN_PROGRESS = "in_progress"
|
||||
COMPLETED = "completed"
|
||||
CANCELLED = "cancelled"
|
||||
FAILED = "failed"
|
||||
|
||||
class TaskPriority(Enum):
|
||||
LOW = "low"
|
||||
MEDIUM = "medium"
|
||||
HIGH = "high"
|
||||
URGENT = "urgent"
|
||||
|
||||
@dataclass
|
||||
class Task:
|
||||
"""Represents a task in the planning system"""
|
||||
id: str
|
||||
description: str
|
||||
status: TaskStatus
|
||||
priority: TaskPriority
|
||||
created_at: datetime
|
||||
dependencies: List[str] = field(default_factory=list) # List of task IDs that must be completed first
|
||||
started_at: Optional[datetime] = None
|
||||
completed_at: Optional[datetime] = None
|
||||
result: Optional[Any] = None
|
||||
error: Optional[str] = None
|
||||
subtasks: List[str] = field(default_factory=list) # List of subtask IDs
|
||||
|
||||
class EnhancedPlanner:
|
||||
"""Enhanced planning system for LangCode Agent"""
|
||||
|
||||
def __init__(self):
|
||||
self.tasks: Dict[str, Task] = {}
|
||||
self.task_counter = 0
|
||||
self.plans: Dict[str, List[str]] = {} # plan_id -> list of task IDs
|
||||
|
||||
def create_task(self,
|
||||
description: str,
|
||||
priority: TaskPriority = TaskPriority.MEDIUM,
|
||||
dependencies: Optional[List[str]] = None) -> str:
|
||||
"""
|
||||
Create a new task
|
||||
|
||||
Args:
|
||||
description: Task description
|
||||
priority: Task priority level
|
||||
dependencies: List of task IDs that must be completed first
|
||||
|
||||
Returns:
|
||||
Task ID
|
||||
"""
|
||||
if dependencies is None:
|
||||
dependencies = []
|
||||
|
||||
task_id = f"task_{self.task_counter:04d}"
|
||||
self.task_counter += 1
|
||||
|
||||
task = Task(
|
||||
id=task_id,
|
||||
description=description,
|
||||
status=TaskStatus.PENDING,
|
||||
priority=priority,
|
||||
dependencies=dependencies,
|
||||
created_at=datetime.now()
|
||||
)
|
||||
|
||||
self.tasks[task_id] = task
|
||||
return task_id
|
||||
|
||||
def create_plan(self, name: str, task_descriptions: List[str]) -> str:
|
||||
"""
|
||||
Create a plan with multiple tasks
|
||||
|
||||
Args:
|
||||
name: Plan name
|
||||
task_descriptions: List of task descriptions
|
||||
|
||||
Returns:
|
||||
Plan ID
|
||||
"""
|
||||
plan_id = f"plan_{len(self.plans):04d}"
|
||||
task_ids = []
|
||||
|
||||
for desc in task_descriptions:
|
||||
task_id = self.create_task(desc)
|
||||
task_ids.append(task_id)
|
||||
|
||||
self.plans[plan_id] = task_ids
|
||||
return plan_id
|
||||
|
||||
def start_task(self, task_id: str) -> bool:
|
||||
"""
|
||||
Start working on a task
|
||||
|
||||
Args:
|
||||
task_id: ID of the task to start
|
||||
|
||||
Returns:
|
||||
True if task started successfully, False otherwise
|
||||
"""
|
||||
if task_id not in self.tasks:
|
||||
return False
|
||||
|
||||
task = self.tasks[task_id]
|
||||
|
||||
# Check if dependencies are completed
|
||||
for dep_id in task.dependencies:
|
||||
if dep_id not in self.tasks or self.tasks[dep_id].status != TaskStatus.COMPLETED:
|
||||
return False # Dependencies not met
|
||||
|
||||
task.status = TaskStatus.IN_PROGRESS
|
||||
task.started_at = datetime.now()
|
||||
return True
|
||||
|
||||
def complete_task(self, task_id: str, result: Any = None) -> bool:
|
||||
"""
|
||||
Mark a task as completed
|
||||
|
||||
Args:
|
||||
task_id: ID of the task to complete
|
||||
result: Result of the task execution
|
||||
|
||||
Returns:
|
||||
True if task completed successfully, False otherwise
|
||||
"""
|
||||
if task_id not in self.tasks:
|
||||
return False
|
||||
|
||||
task = self.tasks[task_id]
|
||||
if task.status != TaskStatus.IN_PROGRESS:
|
||||
return False
|
||||
|
||||
task.status = TaskStatus.COMPLETED
|
||||
task.completed_at = datetime.now()
|
||||
task.result = result
|
||||
return True
|
||||
|
||||
def fail_task(self, task_id: str, error: str) -> bool:
|
||||
"""
|
||||
Mark a task as failed
|
||||
|
||||
Args:
|
||||
task_id: ID of the task to fail
|
||||
error: Error message
|
||||
|
||||
Returns:
|
||||
True if task marked as failed, False otherwise
|
||||
"""
|
||||
if task_id not in self.tasks:
|
||||
return False
|
||||
|
||||
task = self.tasks[task_id]
|
||||
if task.status != TaskStatus.IN_PROGRESS:
|
||||
return False
|
||||
|
||||
task.status = TaskStatus.FAILED
|
||||
task.completed_at = datetime.now()
|
||||
task.error = error
|
||||
return True
|
||||
|
||||
def get_ready_tasks(self) -> List[Task]:
|
||||
"""
|
||||
Get tasks that are ready to be started (dependencies met)
|
||||
|
||||
Returns:
|
||||
List of ready tasks
|
||||
"""
|
||||
ready_tasks = []
|
||||
for task in self.tasks.values():
|
||||
if task.status == TaskStatus.PENDING:
|
||||
# Check if all dependencies are completed
|
||||
deps_met = True
|
||||
for dep_id in task.dependencies:
|
||||
if dep_id not in self.tasks or self.tasks[dep_id].status != TaskStatus.COMPLETED:
|
||||
deps_met = False
|
||||
break
|
||||
if deps_met:
|
||||
ready_tasks.append(task)
|
||||
return ready_tasks
|
||||
|
||||
def get_task_status(self, task_id: str) -> Optional[TaskStatus]:
|
||||
"""
|
||||
Get the status of a task
|
||||
|
||||
Args:
|
||||
task_id: ID of the task
|
||||
|
||||
Returns:
|
||||
Task status or None if task not found
|
||||
"""
|
||||
if task_id in self.tasks:
|
||||
return self.tasks[task_id].status
|
||||
return None
|
||||
|
||||
def get_plan_progress(self, plan_id: str) -> Dict[str, Any]:
|
||||
"""
|
||||
Get progress information for a plan
|
||||
|
||||
Args:
|
||||
plan_id: ID of the plan
|
||||
|
||||
Returns:
|
||||
Dictionary with progress information
|
||||
"""
|
||||
if plan_id not in self.plans:
|
||||
return {"error": "Plan not found"}
|
||||
|
||||
task_ids = self.plans[plan_id]
|
||||
total_tasks = len(task_ids)
|
||||
completed_tasks = 0
|
||||
failed_tasks = 0
|
||||
in_progress_tasks = 0
|
||||
|
||||
for task_id in task_ids:
|
||||
if task_id in self.tasks:
|
||||
task = self.tasks[task_id]
|
||||
if task.status == TaskStatus.COMPLETED:
|
||||
completed_tasks += 1
|
||||
elif task.status == TaskStatus.FAILED:
|
||||
failed_tasks += 1
|
||||
elif task.status == TaskStatus.IN_PROGRESS:
|
||||
in_progress_tasks += 1
|
||||
|
||||
return {
|
||||
"plan_id": plan_id,
|
||||
"total_tasks": total_tasks,
|
||||
"completed_tasks": completed_tasks,
|
||||
"failed_tasks": failed_tasks,
|
||||
"in_progress_tasks": in_progress_tasks,
|
||||
"pending_tasks": total_tasks - completed_tasks - failed_tasks - in_progress_tasks,
|
||||
"progress_percentage": (completed_tasks / total_tasks * 100) if total_tasks > 0 else 0
|
||||
}
|
||||
|
||||
def create_planning_tools(self) -> List[Tool]:
|
||||
"""
|
||||
Create LangChain tools for the planning system
|
||||
|
||||
Returns:
|
||||
List of planning tools
|
||||
"""
|
||||
def create_task_func(description: str, priority: str = "medium", dependencies: str = "") -> str:
|
||||
try:
|
||||
priority_enum = TaskPriority(priority.lower())
|
||||
dep_list = [d.strip() for d in dependencies.split(",") if d.strip()] if dependencies else []
|
||||
task_id = self.create_task(description, priority_enum, dep_list)
|
||||
return f"Created task '{description}' with ID: {task_id}"
|
||||
except Exception as e:
|
||||
return f"Error creating task: {str(e)}"
|
||||
|
||||
def start_task_func(task_id: str) -> str:
|
||||
try:
|
||||
if self.start_task(task_id):
|
||||
return f"Started task {task_id}"
|
||||
else:
|
||||
return f"Failed to start task {task_id} - check dependencies or task ID"
|
||||
except Exception as e:
|
||||
return f"Error starting task: {str(e)}"
|
||||
|
||||
def complete_task_func(task_id: str, result: str = "") -> str:
|
||||
try:
|
||||
if self.complete_task(task_id, result if result else None):
|
||||
return f"Completed task {task_id}"
|
||||
else:
|
||||
return f"Failed to complete task {task_id} - check task ID or status"
|
||||
except Exception as e:
|
||||
return f"Error completing task: {str(e)}"
|
||||
|
||||
def list_tasks_func(status: str = "") -> str:
|
||||
try:
|
||||
if status:
|
||||
status_enum = TaskStatus(status.lower())
|
||||
filtered_tasks = [t for t in self.tasks.values() if t.status == status_enum]
|
||||
else:
|
||||
filtered_tasks = list(self.tasks.values())
|
||||
|
||||
if not filtered_tasks:
|
||||
return "No tasks found"
|
||||
|
||||
result = f"Tasks ({len(filtered_tasks)}):\n"
|
||||
for task in sorted(filtered_tasks, key=lambda t: t.created_at):
|
||||
result += f"- [{task.status.value}] {task.id}: {task.description}\n"
|
||||
if task.dependencies:
|
||||
result += f" Dependencies: {', '.join(task.dependencies)}\n"
|
||||
return result
|
||||
except Exception as e:
|
||||
return f"Error listing tasks: {str(e)}"
|
||||
|
||||
def get_ready_tasks_func() -> str:
|
||||
try:
|
||||
ready_tasks = self.get_ready_tasks()
|
||||
if not ready_tasks:
|
||||
return "No tasks ready to start"
|
||||
|
||||
result = f"Ready tasks ({len(ready_tasks)}):\n"
|
||||
for task in ready_tasks:
|
||||
result += f"- {task.id}: {task.description}\n"
|
||||
return result
|
||||
except Exception as e:
|
||||
return f"Error getting ready tasks: {str(e)}"
|
||||
|
||||
def get_plan_progress_func(plan_id: str) -> str:
|
||||
try:
|
||||
progress = self.get_plan_progress(plan_id)
|
||||
if "error" in progress:
|
||||
return progress["error"]
|
||||
|
||||
result = f"Plan {plan_id} Progress:\n"
|
||||
result += f" Total: {progress['total_tasks']}\n"
|
||||
result += f" Completed: {progress['completed_tasks']}\n"
|
||||
result += f" Failed: {progress['failed_tasks']}\n"
|
||||
result += f" In Progress: {progress['in_progress_tasks']}\n"
|
||||
result += f" Pending: {progress['pending_tasks']}\n"
|
||||
result += f" Progress: {progress['progress_percentage']:.1f}%\n"
|
||||
return result
|
||||
except Exception as e:
|
||||
return f"Error getting plan progress: {str(e)}"
|
||||
|
||||
return [
|
||||
Tool(
|
||||
name="create_task",
|
||||
description="Create a new task. Provide description, priority (low/medium/high/urgent), and optional dependencies (comma-separated task IDs)",
|
||||
func=create_task_func
|
||||
),
|
||||
Tool(
|
||||
name="start_task",
|
||||
description="Start working on a task. Provide task ID",
|
||||
func=start_task_func
|
||||
),
|
||||
Tool(
|
||||
name="complete_task",
|
||||
description="Mark a task as completed. Provide task ID and optional result",
|
||||
func=complete_task_func
|
||||
),
|
||||
Tool(
|
||||
name="list_tasks",
|
||||
description="List all tasks. Optionally filter by status (pending, in_progress, completed, failed)",
|
||||
func=list_tasks_func
|
||||
),
|
||||
Tool(
|
||||
name="get_ready_tasks",
|
||||
description="Get tasks that are ready to be started (dependencies met)",
|
||||
func=get_ready_tasks_func
|
||||
),
|
||||
Tool(
|
||||
name="get_plan_progress",
|
||||
description="Get progress information for a plan. Provide plan ID",
|
||||
func=get_plan_progress_func
|
||||
)
|
||||
]
|
||||
|
||||
# Global planner instance
|
||||
enhanced_planner = EnhancedPlanner()
|
||||
@@ -0,0 +1,638 @@
|
||||
"""
|
||||
ReAct Agent - Agent Loop Implementation
|
||||
|
||||
Questo modulo implementa un agente con loop Pensa→Agisci→Osserva→Ripianifica.
|
||||
Ispirato al pattern ReAct (Reasoning + Acting) di Yao et al.
|
||||
|
||||
L'agente:
|
||||
1. THINK: Analizza il goal e decide la prossima azione
|
||||
2. ACT: Esegue l'azione tramite tool
|
||||
3. OBSERVE: Legge il risultato
|
||||
4. EVALUATE: Verifica se il goal è raggiunto
|
||||
5. REPLAN: Se fallito, ripianifica
|
||||
"""
|
||||
|
||||
import json
|
||||
import re
|
||||
from typing import List, Dict, Any, Optional, Callable
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
|
||||
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage, BaseMessage
|
||||
from langchain_core.tools import Tool
|
||||
|
||||
from enhancements.planning_enhancement import EnhancedPlanner, TaskPriority
|
||||
from enhancements.simple_memory import SimplePersistentMemory, create_simple_memory_tools
|
||||
|
||||
|
||||
class AgentState(Enum):
|
||||
"""Stati dell'agente nel loop"""
|
||||
THINKING = "thinking"
|
||||
ACTING = "acting"
|
||||
OBSERVING = "observing"
|
||||
EVALUATING = "evaluating"
|
||||
REPLANNING = "replanning"
|
||||
DONE = "done"
|
||||
FAILED = "failed"
|
||||
|
||||
|
||||
@dataclass
|
||||
class ActionResult:
|
||||
"""Risultato di un'azione"""
|
||||
action_name: str
|
||||
args: Dict[str, Any]
|
||||
result: str
|
||||
success: bool
|
||||
error: Optional[str] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class Step:
|
||||
"""Singolo step nel loop dell'agente"""
|
||||
step_number: int
|
||||
state: AgentState
|
||||
thought: str
|
||||
action: Optional[ActionResult] = None
|
||||
observation: Optional[str] = None
|
||||
timestamp: datetime = field(default_factory=datetime.now)
|
||||
|
||||
|
||||
class ReActAgent:
|
||||
"""
|
||||
Agente con loop ReAct (Reasoning + Acting).
|
||||
|
||||
Esegue un goal attraverso iterazioni di:
|
||||
- THINK: Decide cosa fare
|
||||
- ACT: Esegue l'azione
|
||||
- OBSERVE: Legge il risultato
|
||||
- EVALUATE: Verifica se goal raggiunto
|
||||
- REPLAN: Ripianifica se necessario
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
llm,
|
||||
tools: List[Tool],
|
||||
max_iterations: int = 10,
|
||||
planner: Optional[EnhancedPlanner] = None,
|
||||
memory: Optional[SimplePersistentMemory] = None,
|
||||
verbose: bool = False,
|
||||
include_memory_tools: bool = True
|
||||
):
|
||||
"""
|
||||
Inizializza il ReActAgent.
|
||||
|
||||
Args:
|
||||
llm: Modello LLM da usare
|
||||
tools: Lista di tool disponibili
|
||||
max_iterations: Massimo numero di iterazioni
|
||||
planner: Istanza EnhancedPlanner (creato se None)
|
||||
memory: Istanza SimplePersistentMemory (creato se None)
|
||||
verbose: Se True, stampa debug info
|
||||
include_memory_tools: Se True, aggiunge tool memory (remember, recall, preference)
|
||||
"""
|
||||
self.llm = llm
|
||||
self.max_iterations = max_iterations
|
||||
self.verbose = verbose
|
||||
|
||||
# Planning e memory esistenti
|
||||
self.planner = planner or EnhancedPlanner()
|
||||
self.memory = memory or SimplePersistentMemory()
|
||||
|
||||
# Aggiungi memory tools se richiesto
|
||||
if include_memory_tools:
|
||||
memory_tools = create_simple_memory_tools(self.memory)
|
||||
tools = tools + memory_tools
|
||||
|
||||
self.tools = {tool.name: tool for tool in tools}
|
||||
|
||||
# Stato interno
|
||||
self.steps: List[Step] = []
|
||||
self.current_goal: Optional[str] = None
|
||||
self.context: str = ""
|
||||
self.conversation_history: List[Dict[str, str]] = []
|
||||
|
||||
# System prompt per il loop
|
||||
self._system_prompt = self._build_system_prompt()
|
||||
|
||||
def _build_system_prompt(self) -> str:
|
||||
"""Costruisce il system prompt per il reasoning"""
|
||||
tool_descriptions = []
|
||||
for name, tool in self.tools.items():
|
||||
desc = tool.description or "No description"
|
||||
tool_descriptions.append(f"- {name}: {desc}")
|
||||
|
||||
tools_list = "\n".join(tool_descriptions)
|
||||
|
||||
return f"""Sei un AI Agent che ragiona e agisce per raggiungere obiettivi.
|
||||
|
||||
TOOLS DISPONIBILI:
|
||||
{tools_list}
|
||||
|
||||
MODO DI OPERARE:
|
||||
1. ANALIZZA il goal e il contesto attuale
|
||||
2. DECIDI la prossima azione (usa un tool se necessario)
|
||||
3. Se il goal è raggiunto, rispondi con SUCCESS
|
||||
4. Se serve un'azione, rispondi con: TOOL:<nome_tool>|arg1=val1|arg2=val2
|
||||
5. Se il goal non può essere raggiunto, rispondi con: FAIL:<reason>
|
||||
|
||||
ESEMPI:
|
||||
Goal: Elenca i file nella directory
|
||||
→ TOOL:ls|path=.
|
||||
|
||||
Goal: Leggi il file config.txt
|
||||
→ TOOL:read_file|path=config.txt
|
||||
|
||||
Goal: Mostra info sistema
|
||||
→ TOOL:system_info
|
||||
|
||||
Goal: Crea un file prova.txt con "hello"
|
||||
→ TOOL:write_file|path=prova.txt|content=hello
|
||||
|
||||
Goal: Qual è il contenuto del file?
|
||||
(analizza il contesto - il file è già stato letto)
|
||||
→ SUCCESS: Il file contiene XYZ
|
||||
|
||||
RISPOSTA:"""
|
||||
|
||||
def run(self, goal: str, track_in_planner: bool = True) -> Dict[str, Any]:
|
||||
"""
|
||||
Esegue un goal attraverso il loop ReAct.
|
||||
|
||||
Args:
|
||||
goal: Goal da raggiungere
|
||||
track_in_planner: Se True, traccia il task nel planner
|
||||
|
||||
Returns:
|
||||
Dict con success, result, steps, iterations
|
||||
"""
|
||||
self.current_goal = goal
|
||||
self.context = f"GOAL: {goal}\n"
|
||||
self.steps = []
|
||||
|
||||
# Traccia nel planner se richiesto
|
||||
task_id = None
|
||||
if track_in_planner and self.planner:
|
||||
task_id = self.planner.create_task(
|
||||
description=goal,
|
||||
priority=TaskPriority.MEDIUM
|
||||
)
|
||||
self.planner.start_task(task_id)
|
||||
if self.verbose:
|
||||
print(f"📋 Task creato: {task_id}")
|
||||
|
||||
if self.verbose:
|
||||
print(f"\n🎯 GOAL: {goal}")
|
||||
print("="*60)
|
||||
|
||||
# Esegui prima azione
|
||||
current_input = goal
|
||||
success = False
|
||||
final_result = ""
|
||||
|
||||
for iteration in range(1, self.max_iterations + 1):
|
||||
# 1. THINK - analizza input corrente
|
||||
thought = self._think(current_input)
|
||||
|
||||
if self.verbose:
|
||||
print(f"\n📝 Step {iteration} - THINK:")
|
||||
print(f" {thought[:200]}...")
|
||||
|
||||
# Check per terminazione
|
||||
if thought.strip().upper().startswith("SUCCESS"):
|
||||
result = thought[len("SUCCESS"):].strip()
|
||||
self._add_step(AgentState.DONE, thought)
|
||||
success = True
|
||||
final_result = result
|
||||
break
|
||||
|
||||
if thought.strip().upper().startswith("FAIL"):
|
||||
reason = thought[len("FAIL:"):].strip()
|
||||
self._add_step(AgentState.FAILED, thought)
|
||||
final_result = self.context
|
||||
if task_id:
|
||||
self.planner.fail_task(task_id, reason)
|
||||
return self._build_result(False, self.context, reason)
|
||||
|
||||
# 2. ACT - parse e esegui tool
|
||||
action_result = self._act(thought)
|
||||
self._add_step(AgentState.ACTING, thought, action_result)
|
||||
|
||||
if self.verbose:
|
||||
print(f" 🔧 ACTION: {action_result.action_name}")
|
||||
print(f" 📤 RESULT: {action_result.result[:100]}...")
|
||||
|
||||
# 3. OBSERVE
|
||||
observation = f"[{action_result.action_name}] {action_result.result}"
|
||||
self.context += f"\n{observation}"
|
||||
self._add_step(AgentState.OBSERVING, thought, action_result, observation)
|
||||
|
||||
# Se l'azione era un tool, goal semplice raggiunto
|
||||
if action_result.action_name != "respond":
|
||||
success = True
|
||||
final_result = action_result.result
|
||||
break
|
||||
|
||||
# 4. EVALUATE - solo per risposte dirette
|
||||
self._add_step(AgentState.EVALUATING, thought, action_result, observation)
|
||||
current_input = f"Risultato: {action_result.result}\n\nGoal: {goal}\nGoal raggiunto?"
|
||||
|
||||
# Aggiorna planner
|
||||
if task_id:
|
||||
if success:
|
||||
self.planner.complete_task(task_id, final_result)
|
||||
if self.verbose:
|
||||
print(f"✅ Task {task_id} completato")
|
||||
else:
|
||||
self.planner.fail_task(task_id, "Max iterations reached")
|
||||
|
||||
if success:
|
||||
return self._build_result(
|
||||
True,
|
||||
final_result,
|
||||
f"Goal raggiunto in {iteration} step"
|
||||
)
|
||||
else:
|
||||
return self._build_result(
|
||||
False,
|
||||
self.context,
|
||||
f"Raggiunto massimo numero di iterazioni ({self.max_iterations})"
|
||||
)
|
||||
|
||||
def _think(self, goal: str) -> str:
|
||||
"""LLM decide la prossima azione basata su contesto e goal"""
|
||||
|
||||
# Usa intent detection esistente (già testata e funzionante)
|
||||
from ollama_agent import OllamaToolAgent
|
||||
|
||||
# Crea un agent temporaneo per detect
|
||||
temp_agent = OllamaToolAgent(llm=self.llm, tools=list(self.tools.values()))
|
||||
detected = temp_agent._detect_intent(goal)
|
||||
|
||||
if detected:
|
||||
# Formatta come TOOL:...|
|
||||
args_str = "|".join(f"{k}={v}" for k, v in detected.arguments.items())
|
||||
result = f"TOOL:{detected.name}"
|
||||
if args_str:
|
||||
result += f"|{args_str}"
|
||||
if self.verbose:
|
||||
print(f" [DEBUG] Detected intent: {detected.name}, args: {detected.arguments}")
|
||||
return result
|
||||
|
||||
# Se non c'è intent detection, chiedi al LLM
|
||||
tool_names = ", ".join(self.tools.keys())
|
||||
user_prompt = f"Goal: {goal}\nTool: {tool_names}\nRispondi con TOOL:nome|arg=val oppure SUCCESS:risposta"
|
||||
|
||||
try:
|
||||
messages = [
|
||||
SystemMessage(content=self._system_prompt),
|
||||
HumanMessage(content=user_prompt)
|
||||
]
|
||||
response = self.llm.invoke(messages)
|
||||
if hasattr(response, 'content'):
|
||||
return response.content.strip()
|
||||
return str(response).strip()
|
||||
except Exception as e:
|
||||
return f"FAIL: Errore LLM: {str(e)}"
|
||||
|
||||
def _act(self, thought: str) -> ActionResult:
|
||||
"""Esegue l'azione decisa da _think"""
|
||||
# Parse TOOL:toolname|arg1=val1|...
|
||||
match = re.match(r'^TOOL:(\w+)(?:\|(.+))?$', thought.strip())
|
||||
|
||||
if not match:
|
||||
# Non è un'azione tool valida, ritorna come thought
|
||||
return ActionResult(
|
||||
action_name="respond",
|
||||
args={},
|
||||
result=thought,
|
||||
success=True
|
||||
)
|
||||
|
||||
tool_name = match.group(1)
|
||||
args_str = match.group(2) or "" if match.group(2) is not None else ""
|
||||
|
||||
# Parse args
|
||||
args = {}
|
||||
if args_str:
|
||||
for arg_part in args_str.split("|"):
|
||||
if '=' in arg_part:
|
||||
key, val = arg_part.split('=', 1)
|
||||
args[key.strip()] = val.strip()
|
||||
|
||||
# Esegui tool
|
||||
if tool_name not in self.tools:
|
||||
return ActionResult(
|
||||
action_name=tool_name,
|
||||
args=args,
|
||||
result=f"Tool '{tool_name}' non trovato",
|
||||
success=False,
|
||||
error=f"Unknown tool: {tool_name}"
|
||||
)
|
||||
|
||||
try:
|
||||
tool = self.tools[tool_name]
|
||||
if args:
|
||||
result = tool.invoke(args)
|
||||
else:
|
||||
result = tool.invoke({})
|
||||
|
||||
result_str = result if isinstance(result, str) else json.dumps(result, ensure_ascii=False)
|
||||
|
||||
return ActionResult(
|
||||
action_name=tool_name,
|
||||
args=args,
|
||||
result=result_str,
|
||||
success=True
|
||||
)
|
||||
except Exception as e:
|
||||
return ActionResult(
|
||||
action_name=tool_name,
|
||||
args=args,
|
||||
result=f"Errore nell'esecuzione: {str(e)}",
|
||||
success=False,
|
||||
error=str(e)
|
||||
)
|
||||
|
||||
def _add_step(
|
||||
self,
|
||||
state: AgentState,
|
||||
thought: str,
|
||||
action: Optional[ActionResult] = None,
|
||||
observation: Optional[str] = None
|
||||
):
|
||||
"""Aggiunge uno step alla cronologia"""
|
||||
step = Step(
|
||||
step_number=len(self.steps) + 1,
|
||||
state=state,
|
||||
thought=thought,
|
||||
action=action,
|
||||
observation=observation
|
||||
)
|
||||
self.steps.append(step)
|
||||
|
||||
def _build_result(self, success: bool, result: str, error: Optional[str] = None) -> Dict[str, Any]:
|
||||
"""Costruisce il risultato finale"""
|
||||
return {
|
||||
"success": success,
|
||||
"result": result,
|
||||
"error": error,
|
||||
"iterations": len(self.steps),
|
||||
"steps": [
|
||||
{
|
||||
"step": s.step_number,
|
||||
"state": s.state.value,
|
||||
"thought": s.thought[:200] if s.thought else None,
|
||||
"action": s.action.action_name if s.action else None,
|
||||
"action_result": s.action.result[:100] if s.action and s.action.result else None,
|
||||
"observation": s.observation[:200] if s.observation else None
|
||||
}
|
||||
for s in self.steps
|
||||
]
|
||||
}
|
||||
|
||||
def get_plan_progress(self) -> Dict[str, Any]:
|
||||
"""Restituisce statistiche sullo stato del planner"""
|
||||
return {
|
||||
"total_tasks": len(self.planner.tasks),
|
||||
"plans": len(self.planner.plans)
|
||||
}
|
||||
|
||||
def run_plan(self, goals: List[str], plan_name: str = "multi_step_plan") -> Dict[str, Any]:
|
||||
"""
|
||||
Esegue un piano con più goal in sequenza.
|
||||
|
||||
Args:
|
||||
goals: Lista di goal da eseguire in ordine
|
||||
plan_name: Nome del piano
|
||||
|
||||
Returns:
|
||||
Dict con risultati per ogni goal e progresso complessivo
|
||||
"""
|
||||
# Crea piano nel planner
|
||||
plan_id = self.planner.create_plan(plan_name, goals)
|
||||
|
||||
if self.verbose:
|
||||
print(f"\n📋 PIANO: {plan_name}")
|
||||
print(f" Goal: {len(goals)}")
|
||||
print("="*60)
|
||||
|
||||
results = []
|
||||
completed = 0
|
||||
failed = 0
|
||||
|
||||
for i, goal in enumerate(goals):
|
||||
if self.verbose:
|
||||
print(f"\n--- Goal {i+1}/{len(goals)} ---")
|
||||
|
||||
result = self.run(goal, track_in_planner=True)
|
||||
results.append({
|
||||
"goal": goal,
|
||||
"success": result["success"],
|
||||
"result": result["result"][:200] if result["result"] else None,
|
||||
"error": result.get("error")
|
||||
})
|
||||
|
||||
if result["success"]:
|
||||
completed += 1
|
||||
else:
|
||||
failed += 1
|
||||
|
||||
# Risultato complessivo
|
||||
overall_success = failed == 0
|
||||
|
||||
if self.verbose:
|
||||
print(f"\n{'='*60}")
|
||||
print(f"📊 PIANO COMPLETATO")
|
||||
print(f" Totale: {len(goals)}")
|
||||
print(f" Completati: {completed}")
|
||||
print(f" Falliti: {failed}")
|
||||
|
||||
return {
|
||||
"success": overall_success,
|
||||
"plan_id": plan_id,
|
||||
"total_goals": len(goals),
|
||||
"completed": completed,
|
||||
"failed": failed,
|
||||
"results": results,
|
||||
"plan_progress": self.planner.get_plan_progress(plan_id)
|
||||
}
|
||||
|
||||
def decompose_goal(self, complex_goal: str) -> List[str]:
|
||||
"""
|
||||
Decompone un goal complesso in sotto-goal usando LLM.
|
||||
|
||||
Args:
|
||||
complex_goal: Goal complesso da decomporre
|
||||
|
||||
Returns:
|
||||
Lista di sotto-goal
|
||||
"""
|
||||
prompt = f"""Scomponi questo goal in passi sequenziali semplici.
|
||||
|
||||
Goal complesso: {complex_goal}
|
||||
|
||||
Elenca i passi necessari, uno per riga. Ogni passo deve essere un'azione concreta.
|
||||
Non usare numeri o bullet, solo testo puro.
|
||||
|
||||
ESEMPIO:
|
||||
Goal: Prepara report vendite mensile
|
||||
Output:
|
||||
Leggi i dati di vendita dal database
|
||||
Analizza i dati e calcola totali
|
||||
Genera il report in formato markdown
|
||||
Salva il report come report_mese.md"""
|
||||
|
||||
try:
|
||||
response = self.llm.invoke([HumanMessage(content=prompt)])
|
||||
content = response.content if hasattr(response, 'content') else str(response)
|
||||
|
||||
# Parse output - una linea per passo
|
||||
lines = [l.strip() for l in content.split('\n') if l.strip()]
|
||||
# Filtra linee vuote e possibili header
|
||||
steps = [l for l in lines if len(l) > 10 and not l.startswith('#')]
|
||||
|
||||
if self.verbose:
|
||||
print(f"\n🔬 Goal decomposed into {len(steps)} steps")
|
||||
|
||||
return steps
|
||||
except Exception as e:
|
||||
if self.verbose:
|
||||
print(f"⚠️ Decomposition failed: {e}")
|
||||
# Fallback: ritorna goal originale
|
||||
return [complex_goal]
|
||||
|
||||
def get_relevant_facts(self, query: str) -> List[str]:
|
||||
"""
|
||||
Cerca fatti rilevanti nella memoria.
|
||||
|
||||
Args:
|
||||
query: Query di ricerca
|
||||
|
||||
Returns:
|
||||
Lista di fatti rilevanti
|
||||
"""
|
||||
facts = self.memory.search_facts(query)
|
||||
return [f"{f['fact']} ({f['category']})" for f in facts]
|
||||
|
||||
def save_conversation(self, user_input: str, agent_response: str) -> None:
|
||||
"""
|
||||
Salva una conversazione nella memoria.
|
||||
|
||||
Args:
|
||||
user_input: Input dell'utente
|
||||
agent_response: Risposta dell'agente
|
||||
"""
|
||||
self.memory.add_conversation(user_input, agent_response)
|
||||
|
||||
def remember(self, fact: str, category: str = "general") -> str:
|
||||
"""
|
||||
Ricorda un fatto.
|
||||
|
||||
Args:
|
||||
fact: Il fatto da ricordare
|
||||
category: Categoria del fatto
|
||||
|
||||
Returns:
|
||||
Messaggio di conferma
|
||||
"""
|
||||
self.memory.add_fact(fact, category)
|
||||
return f"Ricordato: {fact}"
|
||||
|
||||
def recall(self, query: str) -> str:
|
||||
"""
|
||||
Cerca fatti nella memoria.
|
||||
|
||||
Args:
|
||||
query: Query di ricerca
|
||||
|
||||
Returns:
|
||||
Risultati della ricerca
|
||||
"""
|
||||
facts = self.memory.search_facts(query)
|
||||
if not facts:
|
||||
return f"Nessun ricordo trovato per: {query}"
|
||||
|
||||
result = f"Trovati {len(facts)} ricordi:\n"
|
||||
for f in facts[:5]:
|
||||
result += f"- {f['fact']} ({f['category']})\n"
|
||||
return result
|
||||
|
||||
def get_memory_stats(self) -> Dict[str, Any]:
|
||||
"""
|
||||
Restituisce statistiche della memoria.
|
||||
|
||||
Returns:
|
||||
Statistiche memoria
|
||||
"""
|
||||
return self.memory.get_memory_stats()
|
||||
|
||||
def add_preference(self, key: str, value: Any) -> str:
|
||||
"""
|
||||
Imposta una preferenza.
|
||||
|
||||
Args:
|
||||
key: Chiave preferenza
|
||||
value: Valore preferenza
|
||||
|
||||
Returns:
|
||||
Messaggio di conferma
|
||||
"""
|
||||
self.memory.set_preference(key, value)
|
||||
return f"Preferenza impostata: {key} = {value}"
|
||||
|
||||
def get_preference(self, key: str) -> str:
|
||||
"""
|
||||
Recupera una preferenza.
|
||||
|
||||
Args:
|
||||
key: Chiave preferenza
|
||||
|
||||
Returns:
|
||||
Valore della preferenza
|
||||
"""
|
||||
val = self.memory.get_preference(key)
|
||||
if val is None:
|
||||
return f"Preferenza '{key}' non trovata"
|
||||
return f"{key} = {val}"
|
||||
|
||||
|
||||
def create_react_agent(
|
||||
llm,
|
||||
tools: Optional[List[Tool]] = None,
|
||||
include_builtin_tools: bool = True,
|
||||
include_memory_tools: bool = True,
|
||||
max_iterations: int = 10,
|
||||
verbose: bool = False
|
||||
) -> ReActAgent:
|
||||
"""
|
||||
Factory function per creare un ReActAgent.
|
||||
|
||||
Args:
|
||||
llm: Modello LLM
|
||||
tools: Lista tool personalizzati
|
||||
include_builtin_tools: Se True, aggiunge tool built-in
|
||||
include_memory_tools: Se True, aggiunge tool memory
|
||||
max_iterations: Massimo iterazioni
|
||||
verbose: Debug output
|
||||
|
||||
Returns:
|
||||
ReActAgent instance
|
||||
"""
|
||||
if tools is None:
|
||||
tools = []
|
||||
|
||||
# Import built-in tools
|
||||
if include_builtin_tools:
|
||||
from ollama_agent import get_builtin_tools
|
||||
builtin = get_builtin_tools()
|
||||
tools = builtin + tools
|
||||
|
||||
return ReActAgent(
|
||||
llm=llm,
|
||||
tools=tools,
|
||||
max_iterations=max_iterations,
|
||||
verbose=verbose,
|
||||
include_memory_tools=include_memory_tools
|
||||
)
|
||||
@@ -0,0 +1,266 @@
|
||||
"""
|
||||
Simple memory enhancement for LangCode Agent
|
||||
Implements basic persistent memory without complex dependencies
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
from typing import Dict, Any, List, Optional
|
||||
from datetime import datetime
|
||||
|
||||
class SimplePersistentMemory:
|
||||
"""Simple persistent memory system for LangCode Agent"""
|
||||
|
||||
def __init__(self, memory_file: str = "./.langcode/simple_memory.json"):
|
||||
"""
|
||||
Initialize the simple persistent memory
|
||||
|
||||
Args:
|
||||
memory_file: Path to the memory file
|
||||
"""
|
||||
self.memory_file = memory_file
|
||||
self.memory_data = {
|
||||
"conversations": [],
|
||||
"facts": [],
|
||||
"preferences": {},
|
||||
"skills_used": [],
|
||||
"last_updated": None
|
||||
}
|
||||
|
||||
# Ensure directory exists
|
||||
os.makedirs(os.path.dirname(memory_file), exist_ok=True)
|
||||
|
||||
# Load existing memory
|
||||
self.load()
|
||||
|
||||
def load(self) -> None:
|
||||
"""Load memory from file"""
|
||||
try:
|
||||
if os.path.exists(self.memory_file):
|
||||
with open(self.memory_file, 'r') as f:
|
||||
self.memory_data = json.load(f)
|
||||
except Exception as e:
|
||||
print(f"Warning: Failed to load memory: {e}")
|
||||
# Reset to default if load fails
|
||||
self.memory_data = {
|
||||
"conversations": [],
|
||||
"facts": [],
|
||||
"preferences": {},
|
||||
"skills_used": [],
|
||||
"last_updated": None
|
||||
}
|
||||
|
||||
def save(self) -> None:
|
||||
"""Save memory to file"""
|
||||
try:
|
||||
self.memory_data["last_updated"] = datetime.now().isoformat()
|
||||
with open(self.memory_file, 'w') as f:
|
||||
json.dump(self.memory_data, f, indent=2)
|
||||
except Exception as e:
|
||||
print(f"Warning: Failed to save memory: {e}")
|
||||
|
||||
def add_conversation(self, user_input: str, agent_response: str) -> None:
|
||||
"""
|
||||
Add a conversation exchange to memory
|
||||
|
||||
Args:
|
||||
user_input: User's input
|
||||
agent_response: Agent's response
|
||||
"""
|
||||
self.memory_data["conversations"].append({
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
"user": user_input,
|
||||
"agent": agent_response
|
||||
})
|
||||
|
||||
# Keep only last 100 conversations to prevent file from growing too large
|
||||
if len(self.memory_data["conversations"]) > 100:
|
||||
self.memory_data["conversations"] = self.memory_data["conversations"][-100:]
|
||||
|
||||
self.save()
|
||||
|
||||
def add_fact(self, fact: str, category: str = "general") -> None:
|
||||
"""
|
||||
Add a fact to memory
|
||||
|
||||
Args:
|
||||
fact: The fact to remember
|
||||
category: Category for the fact
|
||||
"""
|
||||
self.memory_data["facts"].append({
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
"fact": fact,
|
||||
"category": category
|
||||
})
|
||||
|
||||
# Keep only last 50 facts
|
||||
if len(self.memory_data["facts"]) > 50:
|
||||
self.memory_data["facts"] = self.memory_data["facts"][-50:]
|
||||
|
||||
self.save()
|
||||
|
||||
def set_preference(self, key: str, value: Any) -> None:
|
||||
"""
|
||||
Set a user preference
|
||||
|
||||
Args:
|
||||
key: Preference key
|
||||
value: Preference value
|
||||
"""
|
||||
self.memory_data["preferences"][key] = value
|
||||
self.save()
|
||||
|
||||
def get_preference(self, key: str, default: Any = None) -> Any:
|
||||
"""
|
||||
Get a user preference
|
||||
|
||||
Args:
|
||||
key: Preference key
|
||||
default: Default value if key not found
|
||||
|
||||
Returns:
|
||||
Preference value or default
|
||||
"""
|
||||
return self.memory_data["preferences"].get(key, default)
|
||||
|
||||
def add_skill_used(self, skill_name: str) -> None:
|
||||
"""
|
||||
Record that a skill was used
|
||||
|
||||
Args:
|
||||
skill_name: Name of the skill used
|
||||
"""
|
||||
if skill_name not in self.memory_data["skills_used"]:
|
||||
self.memory_data["skills_used"].append(skill_name)
|
||||
self.save()
|
||||
|
||||
def get_recent_conversations(self, limit: int = 5) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Get recent conversations
|
||||
|
||||
Args:
|
||||
limit: Maximum number of conversations to return
|
||||
|
||||
Returns:
|
||||
List of recent conversations
|
||||
"""
|
||||
return self.memory_data["conversations"][-limit:] if self.memory_data["conversations"] else []
|
||||
|
||||
def search_facts(self, query: str) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Search facts by query
|
||||
|
||||
Args:
|
||||
query: Search query
|
||||
|
||||
Returns:
|
||||
List of matching facts
|
||||
"""
|
||||
query_lower = query.lower()
|
||||
return [
|
||||
fact for fact in self.memory_data["facts"]
|
||||
if query_lower in fact["fact"].lower()
|
||||
]
|
||||
|
||||
def get_memory_stats(self) -> Dict[str, Any]:
|
||||
"""
|
||||
Get memory statistics
|
||||
|
||||
Returns:
|
||||
Dictionary with memory statistics
|
||||
"""
|
||||
return {
|
||||
"total_conversations": len(self.memory_data["conversations"]),
|
||||
"total_facts": len(self.memory_data["facts"]),
|
||||
"total_preferences": len(self.memory_data["preferences"]),
|
||||
"total_skills_used": len(self.memory_data["skills_used"]),
|
||||
"last_updated": self.memory_data["last_updated"]
|
||||
}
|
||||
|
||||
def create_simple_memory_tools(memory: SimplePersistentMemory) -> List:
|
||||
"""
|
||||
Create simple tools for the persistent memory system
|
||||
|
||||
Args:
|
||||
memory: Instance of SimplePersistentMemory
|
||||
|
||||
Returns:
|
||||
List of memory tools
|
||||
"""
|
||||
from langchain_core.tools import Tool
|
||||
|
||||
def remember_func(fact: str, category: str = "general") -> str:
|
||||
try:
|
||||
memory.add_fact(fact, category)
|
||||
return f"Remembered: {fact}"
|
||||
except Exception as e:
|
||||
return f"Error remembering: {str(e)}"
|
||||
|
||||
def recall_func(query: str) -> str:
|
||||
try:
|
||||
results = memory.search_facts(query)
|
||||
if not results:
|
||||
return f"No memories found for: {query}"
|
||||
|
||||
result = f"Found {len(results)} memories:\n"
|
||||
for i, fact in enumerate(results[:5], 1): # Limit to 5 results
|
||||
result += f"{i}. {fact['fact']} ({fact['category']})\n"
|
||||
return result
|
||||
except Exception as e:
|
||||
return f"Error recalling: {str(e)}"
|
||||
|
||||
def preference_func(action: str, key: str = "", value: str = "") -> str:
|
||||
try:
|
||||
if action.lower() == "set" and key and value:
|
||||
# Try to parse value as JSON for complex types
|
||||
try:
|
||||
parsed_value = json.loads(value)
|
||||
except:
|
||||
parsed_value = value
|
||||
memory.set_preference(key, parsed_value)
|
||||
return f"Set preference '{key}' = {parsed_value}"
|
||||
elif action.lower() == "get" and key:
|
||||
val = memory.get_preference(key)
|
||||
return f"Preference '{key}': {val}"
|
||||
else:
|
||||
return "Usage: preference set <key> <value> or preference get <key>"
|
||||
except Exception as e:
|
||||
return f"Error with preference: {str(e)}"
|
||||
|
||||
def stats_func() -> str:
|
||||
try:
|
||||
stats = memory.get_memory_stats()
|
||||
result = "Memory Statistics:\n"
|
||||
result += f" Conversations: {stats['total_conversations']}\n"
|
||||
result += f" Facts: {stats['total_facts']}\n"
|
||||
result += f" Preferences: {stats['total_preferences']}\n"
|
||||
result += f" Skills Used: {stats['total_skills_used']}\n"
|
||||
result += f" Last Updated: {stats['last_updated'] or 'Never'}\n"
|
||||
return result
|
||||
except Exception as e:
|
||||
return f"Error getting memory stats: {str(e)}"
|
||||
|
||||
return [
|
||||
Tool(
|
||||
name="remember",
|
||||
description="Remember a fact for future reference. Provide fact and optional category.",
|
||||
func=remember_func
|
||||
),
|
||||
Tool(
|
||||
name="recall",
|
||||
description="Recall facts by searching memory. Provide search query.",
|
||||
func=recall_func
|
||||
),
|
||||
Tool(
|
||||
name="preference",
|
||||
description="Get or set user preferences. Usage: preference set <key> <value> or preference get <key>",
|
||||
func=preference_func
|
||||
),
|
||||
Tool(
|
||||
name="memory_stats",
|
||||
description="Get statistics about the memory system.",
|
||||
func=stats_func
|
||||
)
|
||||
]
|
||||
|
||||
# Global instance
|
||||
simple_memory = SimplePersistentMemory()
|
||||
@@ -0,0 +1,193 @@
|
||||
"""
|
||||
Test integration of enhancements with LangCode Agent
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, '.')
|
||||
|
||||
def test_enhancements_import():
|
||||
"""Test that our enhancements can be imported"""
|
||||
print("Testing enhancement imports...")
|
||||
|
||||
try:
|
||||
from enhancements.simple_memory import simple_memory, create_simple_memory_tools
|
||||
print("✓ Simple memory enhancement imported successfully")
|
||||
except Exception as e:
|
||||
print(f"✗ Failed to import simple memory enhancement: {e}")
|
||||
return False
|
||||
|
||||
try:
|
||||
from enhancements.planning_enhancement import enhanced_planner
|
||||
print("✓ Planning enhancement imported successfully")
|
||||
except Exception as e:
|
||||
print(f"✗ Failed to import planning enhancement: {e}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def test_simple_memory():
|
||||
"""Test simple memory functionality"""
|
||||
print("\nTesting simple memory...")
|
||||
|
||||
try:
|
||||
from enhancements.simple_memory import simple_memory
|
||||
|
||||
# Test adding a fact
|
||||
simple_memory.add_fact("LangCode Agent supports MCP integration", "features")
|
||||
print("✓ Added fact to memory")
|
||||
|
||||
# Test recalling
|
||||
results = simple_memory.search_facts("MCP")
|
||||
if results:
|
||||
print(f"✓ Recalled {len(results)} facts about MCP")
|
||||
else:
|
||||
print("✗ Failed to recall facts about MCP")
|
||||
return False
|
||||
|
||||
# Test preference
|
||||
simple_memory.set_preference("model_provider", "ollama")
|
||||
provider = simple_memory.get_preference("model_provider")
|
||||
if provider == "ollama":
|
||||
print("✓ Preference system works")
|
||||
else:
|
||||
print(f"✗ Preference system failed: got {provider}")
|
||||
return False
|
||||
|
||||
# Test stats
|
||||
stats = simple_memory.get_memory_stats()
|
||||
if isinstance(stats, dict) and "total_facts" in stats:
|
||||
print(f"✓ Memory stats work: {stats['total_facts']} facts")
|
||||
else:
|
||||
print("✗ Memory stats failed")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ Simple memory test failed: {e}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def test_planning():
|
||||
"""Test planning functionality"""
|
||||
print("\nTesting planning...")
|
||||
|
||||
try:
|
||||
from enhancements.planning_enhancement import enhanced_planner, TaskPriority
|
||||
|
||||
# Test creating a task
|
||||
task_id = enhanced_planner.create_task("Test task", TaskPriority.HIGH)
|
||||
if task_id:
|
||||
print(f"✓ Created task: {task_id}")
|
||||
else:
|
||||
print("✗ Failed to create task")
|
||||
return False
|
||||
|
||||
# Test starting task
|
||||
if enhanced_planner.start_task(task_id):
|
||||
print("✓ Started task successfully")
|
||||
else:
|
||||
print("✗ Failed to start task")
|
||||
return False
|
||||
|
||||
# Test completing task
|
||||
if enhanced_planner.complete_task(task_id, "Task completed successfully"):
|
||||
print("✓ Completed task successfully")
|
||||
else:
|
||||
print("✗ Failed to complete task")
|
||||
return False
|
||||
|
||||
# Test listing tasks by getting the tool and calling it
|
||||
planning_tools = enhanced_planner.create_planning_tools()
|
||||
list_tasks_tool = None
|
||||
for tool in planning_tools:
|
||||
if tool.name == "list_tasks":
|
||||
list_tasks_tool = tool
|
||||
break
|
||||
|
||||
if list_tasks_tool and list_tasks_tool.func:
|
||||
tasks_output = list_tasks_tool.func("")
|
||||
if "Test task" in tasks_output and "completed" in tasks_output:
|
||||
print("✓ Task listing works")
|
||||
else:
|
||||
print("✗ Task listing failed")
|
||||
print(f" Output: {tasks_output}")
|
||||
return False
|
||||
else:
|
||||
print("✗ Could not find or call list_tasks tool")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ Planning test failed: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def test_tool_creation():
|
||||
"""Test that our enhancements create valid LangChain tools"""
|
||||
print("\nTesting tool creation...")
|
||||
|
||||
try:
|
||||
from enhancements.simple_memory import create_simple_memory_tools, simple_memory
|
||||
from enhancements.planning_enhancement import enhanced_planner
|
||||
|
||||
# Test simple memory tools
|
||||
memory_tools = create_simple_memory_tools(simple_memory)
|
||||
if len(memory_tools) == 4:
|
||||
print(f"✓ Simple memory tools created: {len(memory_tools)} tools")
|
||||
tool_names = [tool.name for tool in memory_tools]
|
||||
expected = ["remember", "recall", "preference", "memory_stats"]
|
||||
if all(name in tool_names for name in expected):
|
||||
print("✓ All expected memory tools present")
|
||||
else:
|
||||
print(f"✗ Missing some memory tools. Got: {tool_names}")
|
||||
return False
|
||||
else:
|
||||
print(f"✗ Expected 4 memory tools, got {len(memory_tools)}")
|
||||
return False
|
||||
|
||||
# Test planning tools
|
||||
planning_tools = enhanced_planner.create_planning_tools()
|
||||
if len(planning_tools) == 6:
|
||||
print(f"✓ Planning tools created: {len(planning_tools)} tools")
|
||||
tool_names = [tool.name for tool in planning_tools]
|
||||
expected = ["create_task", "start_task", "complete_task", "list_tasks", "get_ready_tasks", "get_plan_progress"]
|
||||
if all(name in tool_names for name in expected):
|
||||
print("✓ All expected planning tools present")
|
||||
else:
|
||||
print(f"✗ Missing some planning tools. Got: {tool_names}")
|
||||
return False
|
||||
else:
|
||||
print(f"✗ Expected 6 planning tools, got {len(planning_tools)}")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ Tool creation test failed: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def main():
|
||||
"""Run all tests"""
|
||||
print("=== LangCode Agent Enhancements Integration Test ===\n")
|
||||
|
||||
all_passed = True
|
||||
|
||||
all_passed &= test_enhancements_import()
|
||||
all_passed &= test_simple_memory()
|
||||
all_passed &= test_planning()
|
||||
all_passed &= test_tool_creation()
|
||||
|
||||
print("\n=== Test Results ===")
|
||||
if all_passed:
|
||||
print("✓ All tests passed! Enhancements are ready for integration.")
|
||||
return 0
|
||||
else:
|
||||
print("✗ Some tests failed. Please review the implementation.")
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
+194
-15
@@ -29,11 +29,44 @@ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
VERSION = "1.0.0"
|
||||
|
||||
|
||||
def strip_ansi(text: str) -> str:
|
||||
"""Rimuove codici ANSI escape sequences per terminali semplici"""
|
||||
import re
|
||||
ansi_pattern = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
|
||||
return ansi_pattern.sub('', text)
|
||||
|
||||
|
||||
def cmd_chat(args):
|
||||
"""Avvia l'agent in modalità chat interattiva"""
|
||||
import signal
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from main import create_langcode_agent, process_command
|
||||
from langchain_core.messages import HumanMessage
|
||||
|
||||
# Signal handler for Ctrl+C
|
||||
def handle_sigint(signum, frame):
|
||||
print("\n\n👋 Arrivederci!")
|
||||
sys.exit(0)
|
||||
|
||||
# Register signal handler
|
||||
signal.signal(signal.SIGINT, handle_sigint)
|
||||
|
||||
# Thread-safe flag for thinking indicator
|
||||
thinking_active = threading.Event()
|
||||
thinking_active.set()
|
||||
|
||||
def thinking_indicator():
|
||||
"""Mostra indicatore di pensiero mentre aspetta"""
|
||||
dots = [" ", ". ", ".. ", "..."]
|
||||
idx = 0
|
||||
while thinking_active.is_set():
|
||||
print(f"\r🤖 Pensando{dots[idx % 4]} ", end="", flush=True)
|
||||
idx += 1
|
||||
time.sleep(0.5)
|
||||
print("\r" + " " * 20 + "\r", end="") # Clear line
|
||||
|
||||
print("🚀 Inizializzo LangCode Agent...")
|
||||
|
||||
try:
|
||||
@@ -53,27 +86,171 @@ def cmd_chat(args):
|
||||
print(" /exit - Esci dall'agente")
|
||||
print("\n💡 Per chattare, scrivi semplicemente il tuo messaggio.")
|
||||
|
||||
while True:
|
||||
try:
|
||||
user_input = input("\n👤 You: ").strip()
|
||||
except (EOFError, KeyboardInterrupt):
|
||||
print("\n👋 Arrivederci!")
|
||||
break
|
||||
|
||||
if not user_input:
|
||||
continue
|
||||
|
||||
# Callback per processare i messaggi con l'agente
|
||||
def agent_callback(user_input: str) -> str:
|
||||
# Check if it's a command (starts with /)
|
||||
if process_command(user_input):
|
||||
continue
|
||||
|
||||
return "Comando eseguito."
|
||||
|
||||
# Otherwise, treat it as a chat message
|
||||
try:
|
||||
result = agent.invoke({"messages": [HumanMessage(content=user_input)]})
|
||||
ai_message = result["messages"][-1]
|
||||
print(f"\n🤖 LangCode: {ai_message.content}")
|
||||
|
||||
# Extract response - support both simple agent (messages[-1]) and tool agent (response)
|
||||
if "response" in result:
|
||||
return result["response"]
|
||||
else:
|
||||
ai_message = result["messages"][-1]
|
||||
return ai_message.content if hasattr(ai_message, 'content') else str(ai_message)
|
||||
except Exception as e:
|
||||
print(f"\n❌ Errore: {str(e)}")
|
||||
return f"Errore: {str(e)}"
|
||||
|
||||
# Default: CLI semplice, EnhancedCLI/TerminalUI con flag
|
||||
use_enhanced = getattr(args, 'enhanced', False)
|
||||
use_terminal = getattr(args, 'terminal', False)
|
||||
|
||||
if use_terminal:
|
||||
try:
|
||||
from terminal_ui import create_terminal_ui
|
||||
term = create_terminal_ui(
|
||||
scrollback=10000,
|
||||
prompt="❯ "
|
||||
)
|
||||
term.start(agent_callback)
|
||||
return
|
||||
except ImportError:
|
||||
print("⚠️ pyte non installato. Esegui: pip install pyte")
|
||||
print("📝 Uso CLI semplice...")
|
||||
except Exception as e:
|
||||
print(f"⚠️ TerminalUI errore: {e}")
|
||||
print("📝 Uso CLI semplice...")
|
||||
|
||||
if use_enhanced:
|
||||
import errno
|
||||
import fcntl
|
||||
import select
|
||||
import threading
|
||||
|
||||
def timeout_handler():
|
||||
print("\n⚠️ EnhancedCLI timeout - uso CLI semplice")
|
||||
|
||||
try:
|
||||
from enhanced_cli import create_enhanced_cli
|
||||
cli = create_enhanced_cli(
|
||||
history_file="./.langcode/.cli_history",
|
||||
theme="default"
|
||||
)
|
||||
|
||||
# Run with timeout detection
|
||||
timeout_seconds = 30
|
||||
timer = threading.Timer(timeout_seconds, timeout_handler)
|
||||
timer.daemon = True
|
||||
timer.start()
|
||||
|
||||
cli.run(agent_callback)
|
||||
timer.cancel()
|
||||
return
|
||||
except (KeyboardInterrupt, EOFError, OSError) as e:
|
||||
# EnhancedCLI hang - fall back to simple
|
||||
print(f"⚠️ EnhancedCLI bloccato: {e}")
|
||||
print("📝 Uso CLI semplice...")
|
||||
except Exception as e:
|
||||
print(f"⚠️ EnhancedCLI errore: {e}")
|
||||
print("📝 Uso CLI semplice...")
|
||||
|
||||
# CLI semplice con input()
|
||||
# Reset terminal state dopo eventuale EnhancedCLI fallito
|
||||
import subprocess
|
||||
try:
|
||||
subprocess.run(["stty", "sane"], check=False, capture_output=True)
|
||||
except:
|
||||
pass
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print(" CLI SEMPLICE - digita /exit per uscire")
|
||||
print("=" * 60 + "\n")
|
||||
|
||||
while True:
|
||||
try:
|
||||
sys.stdout.flush()
|
||||
sys.stdout.write("❯ ")
|
||||
sys.stdout.flush()
|
||||
user_input = sys.stdin.readline().strip()
|
||||
|
||||
if not user_input:
|
||||
continue
|
||||
|
||||
if user_input.lower() in ["/exit", "/quit"]:
|
||||
print("👋 Arrivederci!")
|
||||
break
|
||||
|
||||
if user_input.startswith("/"):
|
||||
if user_input == "/help":
|
||||
print("""
|
||||
Comandi disponibili:
|
||||
/help - Mostra questo messaggio
|
||||
/status - Mostra stato del sistema
|
||||
/exit - Esci
|
||||
""")
|
||||
continue
|
||||
elif user_input == "/status":
|
||||
print("Agent: in esecuzione")
|
||||
continue
|
||||
else:
|
||||
print(f"Comando sconosciuto: {user_input}")
|
||||
continue
|
||||
|
||||
# Mostra input utente
|
||||
print(f"👤 {user_input}\n")
|
||||
|
||||
# Chiamata all'agent CON indicatore di pensiero
|
||||
thinking_active.set()
|
||||
thinker = threading.Thread(target=thinking_indicator, daemon=True)
|
||||
thinker.start()
|
||||
|
||||
try:
|
||||
response = agent_callback(user_input)
|
||||
finally:
|
||||
thinking_active.clear()
|
||||
thinker.join(timeout=1)
|
||||
|
||||
# Strip ANSI codes per terminali semplici
|
||||
clean_response = strip_ansi(response)
|
||||
|
||||
# Output con scrollback via less
|
||||
lines = clean_response.split('\n')
|
||||
if len(lines) > 10:
|
||||
import tempfile
|
||||
from datetime import datetime
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
output_file = f".langcode/output_{timestamp}.txt"
|
||||
|
||||
# Save to file
|
||||
os.makedirs(".langcode", exist_ok=True)
|
||||
with open(output_file, "w") as f:
|
||||
f.write(clean_response)
|
||||
|
||||
print(f"🤖 Output lungo salvato in: {output_file}")
|
||||
print("💡 Visualizza con: less {0} (usa frecce per scrollare, q per uscire)\n".format(output_file))
|
||||
|
||||
# Try to show with less
|
||||
import subprocess
|
||||
try:
|
||||
subprocess.run(["less", "-R", output_file], check=False)
|
||||
except FileNotFoundError:
|
||||
# less not available, just cat first part
|
||||
print("🤖 " + "\n🤖 ".join(lines[:20]))
|
||||
if len(lines) > 20:
|
||||
print(f"\n... (altre {len(lines)-20} righe nel file {output_file})")
|
||||
print()
|
||||
else:
|
||||
print(f"🤖 {clean_response}\n")
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("\n👋 Arrivederci!")
|
||||
break
|
||||
except EOFError:
|
||||
break
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Inizializzazione fallita: {str(e)}")
|
||||
@@ -450,6 +627,8 @@ Esempi:
|
||||
|
||||
# === CHAT (default) ===
|
||||
chat_parser = subparsers.add_parser("chat", help="Avvia l'agent in modalità chat")
|
||||
chat_parser.add_argument("--enhanced", "-e", action="store_true", help="Usa EnhancedCLI (sperimentale)")
|
||||
chat_parser.add_argument("--terminal", "-t", action="store_true", help="Usa TerminalUI con scrollback (pyte)")
|
||||
chat_parser.set_defaults(func=cmd_chat)
|
||||
|
||||
# === SERVER ===
|
||||
|
||||
@@ -6,11 +6,12 @@ Enhanced with unified provider tools and custom tools
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
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
|
||||
from langchain.tools import Tool
|
||||
from langchain_core.tools import Tool
|
||||
|
||||
# Import custom tools
|
||||
try:
|
||||
@@ -29,22 +30,73 @@ except ImportError:
|
||||
CONFIG_AVAILABLE = False
|
||||
print("Warning: Configuration manager not available.")
|
||||
|
||||
# Import skill manager
|
||||
try:
|
||||
from skill_manager import skill_manager
|
||||
SKILL_MANAGER_AVAILABLE = True
|
||||
except ImportError:
|
||||
SKILL_MANAGER_AVAILABLE = False
|
||||
print("Warning: Skill manager not available.")
|
||||
|
||||
# Import enhancement modules
|
||||
try:
|
||||
from enhancements.simple_memory import simple_memory, create_simple_memory_tools
|
||||
from enhancements.planning_enhancement import enhanced_planner
|
||||
ENHANCEMENTS_AVAILABLE = True
|
||||
except ImportError:
|
||||
ENHANCEMENTS_AVAILABLE = False
|
||||
print("Warning: Enhancement modules not available.")
|
||||
|
||||
# Import json for memory preference handling
|
||||
import json
|
||||
|
||||
|
||||
def load_model_from_config():
|
||||
"""
|
||||
Load the LLM model based on configuration.
|
||||
Configuration can come from environment variables or a config file.
|
||||
For simplicity, we'll use environment variables.
|
||||
Priority: 1) config_manager active config, 2) environment variables
|
||||
|
||||
Expected environment variables:
|
||||
Expected environment variables (fallback):
|
||||
- OPENAI_API_KEY: For OpenAI models
|
||||
- OLLAMA_BASE_URL: For Ollama (e.g., https://ollama.solarpi.net)
|
||||
- MODEL_PROVIDER: One of "openai", "ollama", "openrouter", etc.
|
||||
- MODEL_NAME: The specific model name (e.g., "gpt-4o", "gemma2:9b")
|
||||
"""
|
||||
# Try to use config_manager first
|
||||
if CONFIG_AVAILABLE:
|
||||
active_name = config_manager.get_active_configuration()
|
||||
if active_name:
|
||||
config = config_manager.get_configuration(active_name)
|
||||
if config:
|
||||
provider = config.get("provider", "ollama").lower()
|
||||
model_name = config.get("model_name", "gemma2:9b")
|
||||
base_url = config.get("base_url", "http://localhost:11434")
|
||||
api_key = config.get("api_key")
|
||||
|
||||
print(f"🔗 Caricamento modello da config_manager: {active_name}")
|
||||
print(f" Provider: {provider}")
|
||||
print(f" Modello: {model_name}")
|
||||
print(f" URL: {base_url}")
|
||||
|
||||
if provider == "ollama":
|
||||
return init_chat_model(f"ollama:{model_name}", base_url=base_url)
|
||||
elif provider == "openai":
|
||||
return init_chat_model(f"openai:{model_name}", api_key=api_key)
|
||||
elif provider == "openrouter":
|
||||
return init_chat_model(
|
||||
f"openai:{model_name}",
|
||||
base_url=base_url,
|
||||
api_key=api_key
|
||||
)
|
||||
|
||||
# Fallback to environment variables
|
||||
provider = os.getenv("MODEL_PROVIDER", "openai").lower()
|
||||
model_name = os.getenv("MODEL_NAME", "gpt-4o")
|
||||
|
||||
print(f"⚠️ Usando variabili d'ambiente (nessuna config attiva)")
|
||||
print(f" Provider: {provider}")
|
||||
print(f" Modello: {model_name}")
|
||||
|
||||
# Configure based on provider
|
||||
if provider == "openai":
|
||||
# Uses OPENAI_API_KEY from environment
|
||||
@@ -72,6 +124,217 @@ def load_model_from_config():
|
||||
return init_chat_model(f"openai:{model_name}")
|
||||
|
||||
|
||||
def get_mcp_tools() -> List[Tool]:
|
||||
"""
|
||||
Get MCP tools from configured MCP servers
|
||||
|
||||
Returns:
|
||||
List of MCP-derived LangChain Tool objects
|
||||
"""
|
||||
from mcp_client import MCPClient, get_mcp_tools_from_client
|
||||
|
||||
mcp_tools = []
|
||||
|
||||
if not CONFIG_AVAILABLE:
|
||||
return mcp_tools
|
||||
|
||||
try:
|
||||
# Get all MCP server configurations
|
||||
# We'll look for configurations with provider="mcp"
|
||||
all_configs = config_manager.get_configurations()
|
||||
mcp_configs = {
|
||||
name: config for name, config in all_configs.items()
|
||||
if config.get("provider") == "mcp"
|
||||
}
|
||||
|
||||
for name, config in mcp_configs.items():
|
||||
try:
|
||||
server_url = config.get("server_url")
|
||||
if not server_url:
|
||||
print(f"⚠️ MCP configuration '{name}' missing server_url")
|
||||
continue
|
||||
|
||||
headers = {}
|
||||
api_key = config.get("api_key")
|
||||
if api_key:
|
||||
headers["Authorization"] = f"Bearer {api_key}"
|
||||
|
||||
mcp_client = MCPClient(
|
||||
server_url=server_url,
|
||||
headers=headers,
|
||||
timeout=config.get("timeout", 30)
|
||||
)
|
||||
|
||||
tools = get_mcp_tools_from_client(mcp_client)
|
||||
mcp_tools.extend(tools)
|
||||
print(f"✅ Added {len(tools)} tools from MCP server '{name}'")
|
||||
except Exception as e:
|
||||
print(f"⚠️ Could not load tools from MCP server '{name}': {e}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"⚠️ Error loading MCP tools: {e}")
|
||||
|
||||
return mcp_tools
|
||||
|
||||
|
||||
def get_skill_tools() -> List[Tool]:
|
||||
"""
|
||||
Get skill management tools for the LangCode agent
|
||||
|
||||
Returns:
|
||||
List of skill management LangChain Tool objects
|
||||
"""
|
||||
from skill_manager import skill_manager
|
||||
|
||||
skill_tools = []
|
||||
|
||||
# Search for skills
|
||||
def search_skills_func(query: str, limit: int = 10) -> str:
|
||||
try:
|
||||
skills = skill_manager.search_skills(query, limit)
|
||||
if not skills:
|
||||
return f"No skills found for query: '{query}'"
|
||||
|
||||
result = f"Found {len(skills)} skills:\n"
|
||||
for i, skill in enumerate(skills, 1):
|
||||
result += f"{i}. {skill.get('name', 'Unknown')}: {skill.get('description', 'No description')}\n"
|
||||
return result
|
||||
except Exception as e:
|
||||
return f"Error searching skills: {str(e)}"
|
||||
|
||||
# Install a skill
|
||||
def install_skill_func(skill_name: str) -> str:
|
||||
try:
|
||||
success, message = skill_manager.install_skill(skill_name)
|
||||
return message
|
||||
except Exception as e:
|
||||
return f"Error installing skill: {str(e)}"
|
||||
|
||||
# Uninstall a skill
|
||||
def uninstall_skill_func(skill_name: str) -> str:
|
||||
try:
|
||||
success, message = skill_manager.uninstall_skill(skill_name)
|
||||
return message
|
||||
except Exception as e:
|
||||
return f"Error uninstalling skill: {str(e)}"
|
||||
|
||||
# List installed skills
|
||||
def list_installed_skills_func() -> str:
|
||||
try:
|
||||
skills = skill_manager.list_installed_skills()
|
||||
if not skills:
|
||||
return "No skills installed"
|
||||
|
||||
result = f"Installed skills ({len(skills)}):\n"
|
||||
for i, skill in enumerate(skills, 1):
|
||||
result += f"{i}. {skill.get('name', 'Unknown')}: {skill.get('description', 'No description')}\n"
|
||||
return result
|
||||
except Exception as e:
|
||||
return f"Error listing installed skills: {str(e)}"
|
||||
|
||||
# Get skill details
|
||||
def get_skill_details_func(skill_name: str) -> str:
|
||||
try:
|
||||
details = skill_manager.get_skill_details(skill_name)
|
||||
if not details:
|
||||
return f"Skill '{skill_name}' not found or no details available"
|
||||
|
||||
# Format the details nicely
|
||||
result = f"Skill: {details.get('name', 'Unknown')}\n"
|
||||
result += f"Description: {details.get('description', 'No description')}\n"
|
||||
|
||||
# Add other relevant details if available
|
||||
if 'version' in details:
|
||||
result += f"Version: {details['version']}\n"
|
||||
if 'author' in details:
|
||||
result += f"Author: {details['author']}\n"
|
||||
if 'category' in details:
|
||||
result += f"Category: {details['category']}\n"
|
||||
|
||||
return result
|
||||
except Exception as e:
|
||||
return f"Error getting skill details: {str(e)}"
|
||||
|
||||
# Create skill management tools
|
||||
skill_tools.extend([
|
||||
Tool(
|
||||
name="search_skills",
|
||||
description="Search for agent skills in the skills.sh registry. Use this to find skills by keyword or description.",
|
||||
func=search_skills_func
|
||||
),
|
||||
Tool(
|
||||
name="install_skill",
|
||||
description="Install a skill from the skills.sh registry. Provide the skill name to install.",
|
||||
func=install_skill_func
|
||||
),
|
||||
Tool(
|
||||
name="uninstall_skill",
|
||||
description="Uninstall an installed skill. Provide the skill name to remove.",
|
||||
func=uninstall_skill_func
|
||||
),
|
||||
Tool(
|
||||
name="list_installed_skills",
|
||||
description="List all currently installed skills in the local skills directory.",
|
||||
func=list_installed_skills_func
|
||||
),
|
||||
Tool(
|
||||
name="get_skill_details",
|
||||
description="Get detailed information about a specific skill. Provide the skill name to get details.",
|
||||
func=get_skill_details_func
|
||||
)
|
||||
])
|
||||
|
||||
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_core.messages 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: List = []
|
||||
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
|
||||
@@ -111,13 +374,102 @@ def get_custom_tools() -> List[Tool]:
|
||||
except Exception as e:
|
||||
print(f"⚠️ Could not load provider tools: {e}")
|
||||
|
||||
# Add MCP tools from configured servers
|
||||
try:
|
||||
mcp_tools = get_mcp_tools()
|
||||
custom_tools.extend(mcp_tools)
|
||||
print(f"✅ Added {len(mcp_tools)} MCP tools")
|
||||
except Exception as e:
|
||||
print(f"⚠️ Could not load MCP tools: {e}")
|
||||
|
||||
# Add skill management tools
|
||||
try:
|
||||
skill_tools = get_skill_tools()
|
||||
custom_tools.extend(skill_tools)
|
||||
print(f"✅ Added {len(skill_tools)} skill management tools")
|
||||
except Exception as e:
|
||||
print(f"⚠️ Could not load skill tools: {e}")
|
||||
|
||||
# Add enhancement tools
|
||||
try:
|
||||
if ENHANCEMENTS_AVAILABLE:
|
||||
# Add simple memory tools
|
||||
memory_tools = create_simple_memory_tools(simple_memory)
|
||||
custom_tools.extend(memory_tools)
|
||||
print(f"✅ Added {len(memory_tools)} simple memory tools")
|
||||
|
||||
# Add planning tools
|
||||
planning_tools = enhanced_planner.create_planning_tools()
|
||||
custom_tools.extend(planning_tools)
|
||||
print(f"✅ Added {len(planning_tools)} planning tools")
|
||||
else:
|
||||
print("⚠️ Enhancement modules not available, skipping enhancement tools")
|
||||
except Exception as e:
|
||||
print(f"⚠️ Could not load enhancement tools: {e}")
|
||||
|
||||
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 uses custom OllamaToolAgent with tool calling support.
|
||||
For other providers with full tool support uses deepagents.
|
||||
"""
|
||||
# Try to get config info for Ollama detection
|
||||
is_ollama = False
|
||||
if 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":
|
||||
is_ollama = True
|
||||
|
||||
# Use OllamaToolAgent for Ollama with tool calling
|
||||
if is_ollama and not use_simple_agent:
|
||||
try:
|
||||
# Force reload the module
|
||||
import importlib
|
||||
import ollama_agent
|
||||
importlib.reload(ollama_agent)
|
||||
from ollama_agent import create_ollama_tool_agent
|
||||
|
||||
model = load_model_from_config()
|
||||
custom_tools = get_custom_tools()
|
||||
|
||||
# Enhance with memory and planning tools if available
|
||||
if ENHANCEMENTS_AVAILABLE:
|
||||
try:
|
||||
memory_tools = create_simple_memory_tools(simple_memory)
|
||||
custom_tools.extend(memory_tools)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
planning_tools = enhanced_planner.create_planning_tools()
|
||||
custom_tools.extend(planning_tools)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
print("🔧 Using OllamaToolAgent with tool calling")
|
||||
return create_ollama_tool_agent(
|
||||
model=model,
|
||||
tools=custom_tools,
|
||||
system_prompt=system_prompt,
|
||||
include_builtin_tools=True
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"⚠️ Could not load OllamaToolAgent: {e}")
|
||||
print("⚠️ Falling back to simple agent")
|
||||
use_simple_agent = True
|
||||
elif is_ollama:
|
||||
print("⚠️ Using simple chat agent (Ollama detected)")
|
||||
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
|
||||
@@ -126,7 +478,10 @@ def create_langcode_agent(system_prompt: Optional[str] = None):
|
||||
You have access to:
|
||||
1. Unified provider tools for managing LLM providers (list models, test connections, etc.)
|
||||
2. Custom tools for managing Proxmox virtual machines
|
||||
3. Standard DeepAgents tools (planning, filesystem, shell, subagents)
|
||||
3. MCP tools for accessing external services and skills registries (like skills.sh)
|
||||
4. Skill management tools for discovering, installing, and managing agent skills
|
||||
5. Enhancement tools for persistent memory and advanced planning
|
||||
6. Standard DeepAgents tools (planning, filesystem, shell, subagents)
|
||||
|
||||
Use these tools when asked about:
|
||||
- LLM provider management (listing models, testing connections, setting defaults)
|
||||
@@ -320,6 +675,287 @@ def clear_screen():
|
||||
os.system('cls' if os.name == 'nt' else 'clear')
|
||||
|
||||
|
||||
def skills_search(query: str):
|
||||
"""Search for skills"""
|
||||
from skill_manager import skill_manager
|
||||
|
||||
try:
|
||||
skills = skill_manager.search_skills(query)
|
||||
if not skills:
|
||||
print(f"No skills found for query: '{query}'")
|
||||
return
|
||||
|
||||
print(f"\nFound {len(skills)} skills:")
|
||||
print("=" * 50)
|
||||
for i, skill in enumerate(skills, 1):
|
||||
print(f"{i}. {skill.get('name', 'Unknown')}")
|
||||
print(f" Description: {skill.get('description', 'No description')}")
|
||||
if 'version' in skill:
|
||||
print(f" Version: {skill['version']}")
|
||||
if 'author' in skill:
|
||||
print(f" Author: {skill['author']}")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"Error searching skills: {str(e)}")
|
||||
|
||||
|
||||
def skills_install(skill_name: str):
|
||||
"""Install a skill"""
|
||||
from skill_manager import skill_manager
|
||||
|
||||
try:
|
||||
success, message = skill_manager.install_skill(skill_name)
|
||||
if success:
|
||||
print(f"✅ {message}")
|
||||
else:
|
||||
print(f"❌ {message}")
|
||||
except Exception as e:
|
||||
print(f"Error installing skill: {str(e)}")
|
||||
|
||||
|
||||
def skills_uninstall(skill_name: str):
|
||||
"""Uninstall a skill"""
|
||||
from skill_manager import skill_manager
|
||||
|
||||
try:
|
||||
success, message = skill_manager.uninstall_skill(skill_name)
|
||||
if success:
|
||||
print(f"✅ {message}")
|
||||
else:
|
||||
print(f"❌ {message}")
|
||||
except Exception as e:
|
||||
print(f"Error uninstalling skill: {str(e)}")
|
||||
|
||||
|
||||
def skills_list():
|
||||
"""List installed skills"""
|
||||
from skill_manager import skill_manager
|
||||
|
||||
try:
|
||||
skills = skill_manager.list_installed_skills()
|
||||
if not skills:
|
||||
print("No skills installed")
|
||||
return
|
||||
|
||||
print(f"\nInstalled skills ({len(skills)}):")
|
||||
print("=" * 50)
|
||||
for i, skill in enumerate(skills, 1):
|
||||
print(f"{i}. {skill.get('name', 'Unknown')}")
|
||||
print(f" Description: {skill.get('description', 'No description')}")
|
||||
if 'version' in skill:
|
||||
print(f" Version: {skill['version']}")
|
||||
if 'author' in skill:
|
||||
print(f" Author: {skill['author']}")
|
||||
print(f" Path: {skill.get('path', 'Unknown')}")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"Error listing installed skills: {str(e)}")
|
||||
|
||||
|
||||
def skills_details(skill_name: str):
|
||||
"""Show skill details"""
|
||||
from skill_manager import skill_manager
|
||||
|
||||
try:
|
||||
details = skill_manager.get_skill_details(skill_name)
|
||||
if not details:
|
||||
print(f"Skill '{skill_name}' not found or no details available")
|
||||
return
|
||||
|
||||
print(f"\nSkill Details: {details.get('name', 'Unknown')}")
|
||||
print("=" * 50)
|
||||
print(f"Description: {details.get('description', 'No description')}")
|
||||
if 'version' in details:
|
||||
print(f"Version: {details['version']}")
|
||||
if 'author' in details:
|
||||
print(f"Author: {details['author']}")
|
||||
if 'category' in details:
|
||||
print(f"Category: {details['category']}")
|
||||
if 'repository' in details:
|
||||
print(f"Repository: {details['repository']}")
|
||||
if 'homepage' in details:
|
||||
print(f"Homepage: {details['homepage']}")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"Error getting skill details: {str(e)}")
|
||||
|
||||
|
||||
def memory_remember(text: str):
|
||||
"""Remember a fact for future reference"""
|
||||
from enhancements.simple_memory import simple_memory
|
||||
try:
|
||||
# Clean the text - remove any quotes
|
||||
clean_text = _clean_text(text)
|
||||
if not clean_text:
|
||||
print("❌ Errore: il testo non può essere vuoto")
|
||||
return
|
||||
# Default category is "general"
|
||||
simple_memory.add_fact(clean_text, "general")
|
||||
print(f"✅ Remembered: {clean_text[:100]}{'...' if len(clean_text) > 100 else ''}")
|
||||
except Exception as e:
|
||||
print(f"❌ Error remembering: {e}")
|
||||
|
||||
def memory_recall(query: str):
|
||||
"""Recall facts by searching memory"""
|
||||
from enhancements.simple_memory import simple_memory
|
||||
try:
|
||||
results = simple_memory.search_facts(query)
|
||||
if not results:
|
||||
print(f"No memories found for: {query}")
|
||||
return
|
||||
|
||||
print(f"\nFound {len(results)} memories:")
|
||||
print("=" * 50)
|
||||
for i, fact in enumerate(results[:10], 1): # Limit to 10 results
|
||||
print(f"{i}. {fact['fact']}")
|
||||
print(f" Category: {fact['category']}")
|
||||
print(f" Time: {fact['timestamp']}")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"Error recalling: {e}")
|
||||
|
||||
def memory_set_preference(key: str, value: str):
|
||||
"""Set a user preference"""
|
||||
from enhancements.simple_memory import simple_memory
|
||||
try:
|
||||
# Try to parse value as JSON for complex types
|
||||
try:
|
||||
parsed_val = json.loads(value)
|
||||
except:
|
||||
parsed_val = value
|
||||
simple_memory.set_preference(key, parsed_val)
|
||||
print(f"✅ Set preference '{key}' = {parsed_val}")
|
||||
except Exception as e:
|
||||
print(f"Error setting preference: {e}")
|
||||
|
||||
def memory_preference_get(key: str):
|
||||
"""Get a user preference"""
|
||||
from enhancements.simple_memory import simple_memory
|
||||
try:
|
||||
val = simple_memory.get_preference(key)
|
||||
if val is not None:
|
||||
print(f"Preference '{key}': {val}")
|
||||
else:
|
||||
print(f"Preference '{key}' not found")
|
||||
except Exception as e:
|
||||
print(f"Error getting preference: {e}")
|
||||
|
||||
def memory_stats():
|
||||
"""Get memory statistics"""
|
||||
from enhancements.simple_memory import simple_memory
|
||||
try:
|
||||
stats = simple_memory.get_memory_stats()
|
||||
print("\nMemory Statistics:")
|
||||
print("=" * 30)
|
||||
print(f"Total Conversations: {stats['total_conversations']}")
|
||||
print(f"Total Facts: {stats['total_facts']}")
|
||||
print(f"Total Preferences: {stats['total_preferences']}")
|
||||
print(f"Total Skills Used: {stats['total_skills_used']}")
|
||||
print(f"Last Updated: {stats['last_updated'] or 'Never'}")
|
||||
except Exception as e:
|
||||
print(f"Error getting memory stats: {e}")
|
||||
|
||||
def _strip_quotes(s: str) -> str:
|
||||
"""Strip surrounding quotes from a string"""
|
||||
s = s.strip()
|
||||
if len(s) >= 2:
|
||||
if (s.startswith('"') and s.endswith('"')) or (s.startswith("'") and s.endswith("'")):
|
||||
return s[1:-1]
|
||||
return s
|
||||
|
||||
def _clean_text(s: str) -> str:
|
||||
"""Remove all quotes (single and double) from a string"""
|
||||
return s.replace('"', '').replace("'", '').strip()
|
||||
|
||||
def plan_create(name: str, tasks_string: str):
|
||||
"""Create a plan with multiple tasks"""
|
||||
from enhancements.planning_enhancement import enhanced_planner
|
||||
try:
|
||||
# Strip quotes from name
|
||||
name = _strip_quotes(name)
|
||||
# Split tasks by semicolon and clean up
|
||||
task_descriptions = [_strip_quotes(t.strip()) for t in tasks_string.split(';') if t.strip()]
|
||||
if not task_descriptions:
|
||||
print("❌ No tasks provided")
|
||||
return
|
||||
|
||||
plan_id = enhanced_planner.create_plan(name, task_descriptions)
|
||||
print(f"✅ Created plan '{name}' with ID: {plan_id}")
|
||||
print(f" Tasks: {len(task_descriptions)}")
|
||||
for i, desc in enumerate(task_descriptions, 1):
|
||||
print(f" {i}. {desc}")
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating plan: {e}")
|
||||
|
||||
def plan_list():
|
||||
"""List all plans"""
|
||||
from enhancements.planning_enhancement import enhanced_planner
|
||||
try:
|
||||
if not enhanced_planner.plans:
|
||||
print("No plans created yet")
|
||||
return
|
||||
|
||||
print(f"\nPlans ({len(enhanced_planner.plans)}):")
|
||||
print("=" * 50)
|
||||
for plan_id, task_ids in enhanced_planner.plans.items():
|
||||
task_count = len(task_ids)
|
||||
print(f"Plan ID: {plan_id}")
|
||||
print(f" Tasks: {task_count}")
|
||||
# Show first few tasks
|
||||
for i, task_id in enumerate(task_ids[:3], 1):
|
||||
if task_id in enhanced_planner.tasks:
|
||||
task = enhanced_planner.tasks[task_id]
|
||||
print(f" {i}. [{task.status.value}] {task.description}")
|
||||
else:
|
||||
print(f" {i}. [unknown] Task {task_id} not found")
|
||||
if len(task_ids) > 3:
|
||||
print(f" ... and {len(task_ids) - 3} more tasks")
|
||||
print()
|
||||
except Exception as e:
|
||||
print(f"Error listing plans: {e}")
|
||||
|
||||
def plan_progress(plan_id: str):
|
||||
"""Get progress information for a plan"""
|
||||
from enhancements.planning_enhancement import enhanced_planner
|
||||
try:
|
||||
progress = enhanced_planner.get_plan_progress(plan_id)
|
||||
if "error" in progress:
|
||||
print(f"❌ {progress['error']}")
|
||||
return
|
||||
|
||||
print(f"\nPlan {plan_id} Progress:")
|
||||
print("=" * 40)
|
||||
print(f"Total Tasks: {progress['total_tasks']}")
|
||||
print(f"Completed: {progress['completed_tasks']}")
|
||||
print(f"Failed: {progress['failed_tasks']}")
|
||||
print(f"In Progress: {progress['in_progress_tasks']}")
|
||||
print(f"Pending: {progress['pending_tasks']}")
|
||||
print(f"Progress: {progress['progress_percentage']:.1f}%")
|
||||
except Exception as e:
|
||||
print(f"Error getting plan progress: {e}")
|
||||
|
||||
def plan_start(task_id: str):
|
||||
"""Start working on a task"""
|
||||
from enhancements.planning_enhancement import enhanced_planner
|
||||
try:
|
||||
if enhanced_planner.start_task(task_id):
|
||||
print(f"✅ Started task {task_id}")
|
||||
else:
|
||||
print(f"❌ Failed to start task {task_id} - check dependencies or task ID")
|
||||
except Exception as e:
|
||||
print(f"Error starting task: {e}")
|
||||
|
||||
def plan_complete(task_id: str):
|
||||
"""Mark a task as completed"""
|
||||
from enhancements.planning_enhancement import enhanced_planner
|
||||
try:
|
||||
if enhanced_planner.complete_task(task_id):
|
||||
print(f"✅ Completed task {task_id}")
|
||||
else:
|
||||
print(f"❌ Failed to complete task {task_id} - check task ID or status")
|
||||
except Exception as e:
|
||||
print(f"Error completing task: {e}")
|
||||
|
||||
def process_command(user_input: str) -> bool:
|
||||
"""
|
||||
Process a command starting with "/"
|
||||
@@ -355,10 +991,105 @@ def process_command(user_input: str) -> bool:
|
||||
else:
|
||||
print(f"❌ Sottocomando sconosciuto: {args[0]}")
|
||||
print("Sottocomandi disponibili: list, active, test, use <name>")
|
||||
elif command == 'skills':
|
||||
if not args:
|
||||
print("❌ Specifica un sottocomando: search, install, uninstall, list, details")
|
||||
elif args[0] == 'search' and len(args) > 1:
|
||||
skills_search(' '.join(args[1:]))
|
||||
elif args[0] == 'install' and len(args) > 1:
|
||||
skills_install(args[1])
|
||||
elif args[0] == 'uninstall' and len(args) > 1:
|
||||
skills_uninstall(args[1])
|
||||
elif args[0] == 'list':
|
||||
skills_list()
|
||||
elif args[0] == 'details' and len(args) > 1:
|
||||
skills_details(args[1])
|
||||
else:
|
||||
print("❌ Sottocomando sconosciuto o argomenti mancanti")
|
||||
print("Sottocomandi disponibili: search <query>, install <name>, uninstall <name>, list, details <name>")
|
||||
elif command == 'memory':
|
||||
if not args:
|
||||
print("❌ Specifica un sottocomando: remember, recall, preference, stats")
|
||||
elif args[0] == 'remember' and len(args) > 1:
|
||||
memory_remember(' '.join(args[1:]))
|
||||
elif args[0] == 'recall' and len(args) > 1:
|
||||
memory_recall(' '.join(args[1:]))
|
||||
elif args[0] == 'preference':
|
||||
if len(args) >= 4 and args[1] == 'set':
|
||||
# Join everything after 'set' as the value (key is args[2], value is args[3:])
|
||||
key = args[2]
|
||||
value = ' '.join(args[3:])
|
||||
memory_set_preference(key, value)
|
||||
elif len(args) == 3 and args[1] == 'get':
|
||||
key = args[2]
|
||||
memory_preference_get(key)
|
||||
else:
|
||||
print("❌ Usage: preference set <key> <value> or preference get <key>")
|
||||
elif args[0] == 'stats':
|
||||
memory_stats()
|
||||
else:
|
||||
print("❌ Sottocomando sconosciuto o argomenti mancanti")
|
||||
print("Sottocomandi disponibili: remember <text>, recall <query>, preference set <key> <value>, preference get <key>, stats")
|
||||
elif command == 'plan':
|
||||
if not args:
|
||||
print("❌ Specifica un sottocomando: create, list, progress, start, complete")
|
||||
elif args[0] == 'create':
|
||||
# Handle plan creation with special parsing for quoted strings
|
||||
# Format: /plan create "Plan Name" "Task 1; Task 2; Task 3"
|
||||
# Or: /plan create PlanName "Task1; Task2; Task3"
|
||||
import re
|
||||
# Get the raw part after "plan create"
|
||||
raw_part = command_line[len("plan create "):]
|
||||
|
||||
# Find all quoted strings (handles both " and ' quotes)
|
||||
quoted_pattern = r'"([^"]*)"|\'([^\']*)\''
|
||||
matches = re.findall(quoted_pattern, raw_part)
|
||||
|
||||
if len(matches) >= 2:
|
||||
# Both name and tasks are quoted
|
||||
name = matches[0][0] or matches[0][1]
|
||||
tasks = matches[1][0] or matches[1][1]
|
||||
plan_create(name, tasks)
|
||||
elif len(matches) == 1:
|
||||
# Only one quoted string - it's the tasks, name is the first unquoted word
|
||||
tasks = matches[0][0] or matches[0][1]
|
||||
# Find the name as the first word before the quoted part
|
||||
unquoted_start = raw_part.find('"') if '"' in raw_part else raw_part.find("'")
|
||||
name = raw_part[:unquoted_start].strip()
|
||||
if name and tasks:
|
||||
plan_create(name, tasks)
|
||||
else:
|
||||
print("❌ Usage: /plan create <nome> \"<task1; task2; ...>\"")
|
||||
else:
|
||||
# No quotes - use simple split
|
||||
if len(args) >= 3:
|
||||
name = args[1]
|
||||
tasks = ' '.join(args[2:])
|
||||
plan_create(name, tasks)
|
||||
else:
|
||||
print("❌ Usage: /plan create <nome> \"<task1; task2; ...>\"")
|
||||
elif args[0] == 'list':
|
||||
plan_list()
|
||||
elif args[0] == 'progress' and len(args) > 1:
|
||||
plan_progress(args[1])
|
||||
elif args[0] == 'start' and len(args) > 1:
|
||||
plan_start(args[1])
|
||||
elif args[0] == 'complete' and len(args) > 1:
|
||||
plan_complete(args[1])
|
||||
else:
|
||||
print("❌ Sottocomando sconosciuto o argomenti mancanti")
|
||||
print("Sottocomandi disponibili: create <name> <task1> <task2> ..., list, progress <plan_id>, start <task_id>, complete <task_id>")
|
||||
elif command == 'models':
|
||||
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)
|
||||
@@ -405,12 +1136,38 @@ def main():
|
||||
|
||||
# Otherwise, treat it as a chat message
|
||||
try:
|
||||
print("MAIN: before invoke", flush=True)
|
||||
result = agent.invoke({"messages": [HumanMessage(content=user_input)]})
|
||||
# Extract the AI's response
|
||||
ai_message = result["messages"][-1]
|
||||
print(f"\n🤖 LangCode: {ai_message.content}")
|
||||
print(f"MAIN: after invoke, result type={type(result)}", flush=True)
|
||||
|
||||
# Extract the AI's response (works for both simple and tool agent)
|
||||
print(f"MAIN: result keys={result.keys() if hasattr(result, 'keys') else 'NO KEYS'}")
|
||||
if result and "response" in result:
|
||||
# OllamaToolAgent returns a separate response field
|
||||
response_text = result["response"]
|
||||
else:
|
||||
# Simple agent returns the last message
|
||||
ai_message = result["messages"][-1]
|
||||
response_text = ai_message.content if hasattr(ai_message, 'content') else str(ai_message)
|
||||
print(f"MAIN: response_text={response_text[:100] if response_text else 'EMPTY'}...")
|
||||
|
||||
print(f"\n🤖 LangCode: {response_text}")
|
||||
except Exception as e:
|
||||
print(f"\n❌ Error: {str(e)}")
|
||||
error_msg = str(e)
|
||||
if not error_msg:
|
||||
error_msg = f"{type(e).__name__} (nessun messaggio)"
|
||||
print(f"\n❌ Errore: {error_msg}")
|
||||
|
||||
# Suggerimenti basati sul tipo di errore
|
||||
exc_type = type(e).__name__
|
||||
if "Connection" in exc_type or "Connect" in str(e):
|
||||
print("💡 Suggerimento: Verifica che il server Ollama sia raggiungibile.")
|
||||
print(f" URL: {os.getenv('OLLAMA_BASE_URL', 'http://localhost:11434')}")
|
||||
elif "Timeout" in exc_type or "timeout" in str(e).lower():
|
||||
print("💡 Suggerimento: Il server è lento. Prova con un modello più piccolo.")
|
||||
elif "model" in str(e).lower() or "Model" in exc_type:
|
||||
print("💡 Suggerimento: Il modello specificato potrebbe non esistere.")
|
||||
print(f" Modello: {os.getenv('MODEL_NAME', 'non impostato')}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to initialize agent: {str(e)}")
|
||||
|
||||
+195
@@ -0,0 +1,195 @@
|
||||
"""
|
||||
MCP (Model Context Protocol) client for LangCode Agent
|
||||
Enables integration with MCP servers like skills.sh to discover and use tools
|
||||
"""
|
||||
import json
|
||||
import requests
|
||||
from typing import Dict, List, Any, Optional
|
||||
from langchain_core.tools import Tool
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class MCPClient:
|
||||
"""Client for communicating with MCP (Model Context Protocol) servers"""
|
||||
|
||||
def __init__(self, server_url: str, headers: Optional[Dict[str, str]] = None, timeout: int = 30):
|
||||
"""
|
||||
Initialize MCP client
|
||||
|
||||
Args:
|
||||
server_url: URL of the MCP server
|
||||
headers: Optional headers for authentication
|
||||
timeout: Request timeout in seconds
|
||||
"""
|
||||
self.server_url = server_url.rstrip('/')
|
||||
self.headers = headers or {}
|
||||
self.timeout = timeout
|
||||
self._available_tools: Optional[List[Dict[str, Any]]] = None
|
||||
|
||||
def _make_request(self, method: str, endpoint: str, data: Optional[Dict] = None) -> Dict[str, Any]:
|
||||
"""
|
||||
Make a JSON-RPC request to the MCP server
|
||||
|
||||
Args:
|
||||
method: HTTP method (GET, POST, etc.)
|
||||
endpoint: API endpoint
|
||||
data: Optional data to send
|
||||
|
||||
Returns:
|
||||
Response data as dictionary
|
||||
|
||||
Raises:
|
||||
Exception: If request fails
|
||||
"""
|
||||
url = f"{self.server_url}{endpoint}"
|
||||
|
||||
# For MCP, we typically use JSON-RPC over HTTP POST
|
||||
if method.upper() == "POST":
|
||||
# JSON-RPC 2.0 format
|
||||
payload = {
|
||||
"jsonrpc": "2.0",
|
||||
"method": endpoint.lstrip('/'), # Remove leading slash for method name
|
||||
"params": data or {},
|
||||
"id": 1
|
||||
}
|
||||
response = requests.post(
|
||||
url,
|
||||
json=payload,
|
||||
headers={**self.headers, "Content-Type": "application/json"},
|
||||
timeout=self.timeout
|
||||
)
|
||||
else:
|
||||
response = requests.request(
|
||||
method,
|
||||
url,
|
||||
headers=self.headers,
|
||||
params=data,
|
||||
timeout=self.timeout
|
||||
)
|
||||
|
||||
response.raise_for_status()
|
||||
|
||||
# Handle JSON-RPC response
|
||||
if method.upper() == "POST":
|
||||
result = response.json()
|
||||
if "error" in result:
|
||||
raise Exception(f"MCP error: {result['error']}")
|
||||
return result.get("result", {})
|
||||
else:
|
||||
return response.json()
|
||||
|
||||
def list_tools(self) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
List available tools from the MCP server
|
||||
|
||||
Returns:
|
||||
List of tool specifications
|
||||
"""
|
||||
if self._available_tools is not None:
|
||||
return self._available_tools
|
||||
|
||||
try:
|
||||
# MCP standard method for listing tools
|
||||
result = self._make_request("POST", "/tools/list")
|
||||
self._available_tools = result.get("tools", [])
|
||||
logger.info(f"Discovered {len(self._available_tools)} tools from MCP server {self.server_url}")
|
||||
return self._available_tools
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to list tools from MCP server {self.server_url}: {e}")
|
||||
self._available_tools = []
|
||||
return []
|
||||
|
||||
def call_tool(self, tool_name: str, arguments: Dict[str, Any]) -> Any:
|
||||
"""
|
||||
Call a specific tool on the MCP server
|
||||
|
||||
Args:
|
||||
tool_name: Name of the tool to call
|
||||
arguments: Arguments to pass to the tool
|
||||
|
||||
Returns:
|
||||
Tool execution result
|
||||
"""
|
||||
try:
|
||||
result = self._make_request("POST", "/tools/call", {
|
||||
"name": tool_name,
|
||||
"arguments": arguments
|
||||
})
|
||||
return result
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to call tool {tool_name} on MCP server {self.server_url}: {e}")
|
||||
raise Exception(f"MCP tool call failed: {e}")
|
||||
|
||||
def refresh_tools(self):
|
||||
"""Refresh the cached list of available tools"""
|
||||
self._available_tools = None
|
||||
self.list_tools()
|
||||
|
||||
|
||||
def mcp_to_langchain_tool(mcp_client: MCPClient, tool_spec: Dict[str, Any]) -> Tool:
|
||||
"""
|
||||
Convert an MCP tool specification to a LangChain Tool
|
||||
|
||||
Args:
|
||||
mcp_client: MCP client instance
|
||||
tool_spec: Tool specification from MCP server
|
||||
|
||||
Returns:
|
||||
LangChain Tool object
|
||||
"""
|
||||
def tool_function(*args, **kwargs):
|
||||
# Handle both positional and keyword arguments
|
||||
if args and len(args) == 1 and isinstance(args[0], dict):
|
||||
# Single dict argument passed positionally
|
||||
tool_args = args[0]
|
||||
elif kwargs:
|
||||
# Keyword arguments
|
||||
tool_args = kwargs
|
||||
else:
|
||||
# No arguments or multiple positional args (shouldn't happen for MCP tools)
|
||||
tool_args = {}
|
||||
|
||||
try:
|
||||
result = mcp_client.call_tool(tool_spec["name"], tool_args)
|
||||
# Convert result to string if it isn't already
|
||||
if isinstance(result, str):
|
||||
return result
|
||||
else:
|
||||
return json.dumps(result, indent=2)
|
||||
except Exception as e:
|
||||
return f"Error calling MCP tool {tool_spec['name']}: {str(e)}"
|
||||
|
||||
# Extract description, default to empty string if not provided
|
||||
description = tool_spec.get("description", "")
|
||||
if not description:
|
||||
description = f"MCP tool: {tool_spec['name']}"
|
||||
|
||||
return Tool(
|
||||
name=tool_spec["name"],
|
||||
description=description,
|
||||
func=tool_function
|
||||
)
|
||||
|
||||
|
||||
def get_mcp_tools_from_client(mcp_client: MCPClient) -> List[Tool]:
|
||||
"""
|
||||
Get all available tools from an MCP client as LangChain Tools
|
||||
|
||||
Args:
|
||||
mcp_client: Configured MCP client
|
||||
|
||||
Returns:
|
||||
List of LangChain Tool objects
|
||||
"""
|
||||
tools = []
|
||||
try:
|
||||
tool_specs = mcp_client.list_tools()
|
||||
for tool_spec in tool_specs:
|
||||
langchain_tool = mcp_to_langchain_tool(mcp_client, tool_spec)
|
||||
tools.append(langchain_tool)
|
||||
logger.info(f"Converted {len(tools)} MCP tools to LangChain Tools")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get tools from MCP client: {e}")
|
||||
|
||||
return tools
|
||||
+528
@@ -0,0 +1,528 @@
|
||||
"""
|
||||
Ollama Agent with Tool Calling Support
|
||||
|
||||
Questo modulo implementa un agente che supporta tool calling per Ollama
|
||||
usando un approccio di parsing delle risposte invece di bind_tools.
|
||||
"""
|
||||
import json
|
||||
import re
|
||||
import uuid
|
||||
import subprocess
|
||||
from typing import List, Dict, Any, Optional, Callable
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage, BaseMessage
|
||||
from langchain_core.tools import tool, Tool
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# TOOL CALL PATTERNS
|
||||
# =============================================================================
|
||||
|
||||
# Old format: [TOOL_CALL]...[/TOOL_CALL]
|
||||
TOOL_CALL_PATTERN_OLD = r'\[TOOL_CALL\]\s*(.*?)\s*\[\/TOOL_CALL\]'
|
||||
|
||||
# New format: TOOL:toolname|arg1=val1|arg2=val2
|
||||
TOOL_CALL_PATTERN_NEW = r'^TOOL:(\w+)(?:\|(.*))?$'
|
||||
|
||||
TOOL_RESULT_PATTERN = r'\[TOOL_RESULT\]\s*(.*?)\s*\[\/TOOL_RESULT\]'
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# BASIC BUILT-IN TOOLS
|
||||
# =============================================================================
|
||||
|
||||
def get_builtin_tools() -> List[Tool]:
|
||||
"""
|
||||
Restituisce i tool built-in disponibili per l'agente.
|
||||
"""
|
||||
tools = []
|
||||
|
||||
# Tool: Info sistema - usa @tool decorator
|
||||
@tool
|
||||
def system_info() -> str:
|
||||
"""Get system information (OS, Python version, current directory)."""
|
||||
import platform
|
||||
import os
|
||||
info = []
|
||||
info.append(f"OS: {platform.system()} {platform.release()}")
|
||||
info.append(f"Python: {platform.python_version()}")
|
||||
info.append(f"Current dir: {os.getcwd()}")
|
||||
info.append(f"User: {os.getenv('USER', 'unknown')}")
|
||||
return "\n".join(info)
|
||||
|
||||
tools.append(system_info)
|
||||
|
||||
# Tool: Lista file
|
||||
@tool
|
||||
def ls(path: str) -> str:
|
||||
"""List files in a directory."""
|
||||
try:
|
||||
dir_path = Path(path).expanduser()
|
||||
if not dir_path.exists():
|
||||
return f"Error: Directory '{path}' not found"
|
||||
if not dir_path.is_dir():
|
||||
return f"Error: '{path}' is not a directory"
|
||||
|
||||
items = []
|
||||
for item in sorted(dir_path.iterdir()):
|
||||
item_type = "DIR" if item.is_dir() else "FILE"
|
||||
items.append(f"{item_type}: {item.name}")
|
||||
return "\n".join(items) if items else "Directory is empty"
|
||||
except Exception as e:
|
||||
return f"Error listing directory: {str(e)}"
|
||||
|
||||
tools.append(ls)
|
||||
|
||||
# Tool: Esegui comando shell
|
||||
@tool
|
||||
def execute(command: str) -> str:
|
||||
"""Execute a shell command. Use this for file operations, running scripts, git commands, etc."""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
command,
|
||||
shell=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=30
|
||||
)
|
||||
output = []
|
||||
if result.stdout:
|
||||
output.append(f"STDOUT:\n{result.stdout}")
|
||||
if result.stderr:
|
||||
output.append(f"STDERR:\n{result.stderr}")
|
||||
if result.returncode != 0:
|
||||
output.append(f"Exit code: {result.returncode}")
|
||||
return "\n".join(output) if output else "Command executed with no output"
|
||||
except subprocess.TimeoutExpired:
|
||||
return "Error: Command timed out after 30 seconds"
|
||||
except Exception as e:
|
||||
return f"Error: {str(e)}"
|
||||
|
||||
tools.append(execute)
|
||||
|
||||
# Tool: Leggi file
|
||||
@tool
|
||||
def read_file(path: str) -> str:
|
||||
"""Read the contents of a file."""
|
||||
try:
|
||||
file_path = Path(path).expanduser()
|
||||
if not file_path.exists():
|
||||
return f"Error: File '{path}' not found"
|
||||
if file_path.is_dir():
|
||||
return f"Error: '{path}' is a directory, not a file"
|
||||
return file_path.read_text(encoding='utf-8')
|
||||
except Exception as e:
|
||||
return f"Error reading file: {str(e)}"
|
||||
|
||||
tools.append(read_file)
|
||||
|
||||
# Tool: Scrivi file
|
||||
@tool
|
||||
def write_file(path: str, content: str) -> str:
|
||||
"""Write content to a file. WARNING: overwrites existing files!"""
|
||||
try:
|
||||
file_path = Path(path).expanduser()
|
||||
file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
file_path.write_text(content, encoding='utf-8')
|
||||
return f"Successfully wrote to {path}"
|
||||
except Exception as e:
|
||||
return f"Error writing file: {str(e)}"
|
||||
|
||||
tools.append(write_file)
|
||||
|
||||
# Tool: Cerca file
|
||||
@tool
|
||||
def search(directory: str, pattern: str) -> str:
|
||||
"""Search for files by name pattern."""
|
||||
try:
|
||||
dir_path = Path(directory).expanduser()
|
||||
if not dir_path.exists():
|
||||
return f"Error: Directory '{directory}' not found"
|
||||
|
||||
matches = list(dir_path.rglob(pattern))
|
||||
if not matches:
|
||||
return f"No files matching '{pattern}' found in {directory}"
|
||||
|
||||
results = []
|
||||
for match in matches[:50]:
|
||||
results.append(str(match))
|
||||
|
||||
return f"Found {len(matches)} matches (showing first 50):\n" + "\n".join(results)
|
||||
except Exception as e:
|
||||
return f"Error searching files: {str(e)}"
|
||||
|
||||
tools.append(search)
|
||||
|
||||
return tools
|
||||
|
||||
|
||||
@dataclass
|
||||
class ToolCall:
|
||||
"""Rappresenta una chiamata a tool"""
|
||||
id: str
|
||||
name: str
|
||||
arguments: Dict[str, Any]
|
||||
|
||||
|
||||
class OllamaToolAgent:
|
||||
"""
|
||||
Agente che supporta tool calling per Ollama e provider simili.
|
||||
|
||||
Strategia: Il modello risponde con marcatori speciali per le chiamate tool.
|
||||
Noi parsiamo, eseguiamo, e riportiamo i risultati.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
llm,
|
||||
tools: List[Tool],
|
||||
system_prompt: Optional[str] = None,
|
||||
max_iterations: int = 10
|
||||
):
|
||||
self.llm = llm
|
||||
self.tools = {tool.name: tool for tool in tools}
|
||||
self.max_iterations = max_iterations
|
||||
|
||||
# Costruisci il system prompt con la lista dei tool
|
||||
if system_prompt is None:
|
||||
system_prompt = self._default_system_prompt()
|
||||
else:
|
||||
system_prompt = system_prompt + "\n\n" + self._tool_instructions()
|
||||
|
||||
self.messages: List[BaseMessage] = [SystemMessage(content=system_prompt)]
|
||||
|
||||
# Tool per la memoria
|
||||
self.short_term_memory: List[Dict] = []
|
||||
|
||||
def _default_system_prompt(self) -> str:
|
||||
"""System prompt di default"""
|
||||
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=.
|
||||
|
||||
Example: User asks "what OS am I using?" -> You respond: TOOL:system_info
|
||||
Example: User asks "list files" -> You respond: TOOL:ls|path=.
|
||||
|
||||
For other questions, just answer normally without tools."""
|
||||
|
||||
def _tool_instructions(self) -> str:
|
||||
"""Genera le istruzioni per i tool disponibili"""
|
||||
instructions = []
|
||||
instructions.append("\n\n# AVAILABLE TOOLS\n")
|
||||
instructions.append("You have access to the following tools:\n\n")
|
||||
|
||||
for name, tool in self.tools.items():
|
||||
# Estrai la descrizione dal docstring del tool
|
||||
desc = tool.description or "No description"
|
||||
# Pulisci la descrizione da caratteri speciali
|
||||
desc = desc.replace('"', '\\"').replace('\n', ' ')
|
||||
instructions.append(f"## {name}\n")
|
||||
instructions.append(f"Description: {desc}\n")
|
||||
|
||||
# Parsa i parametri se presenti
|
||||
if hasattr(tool, 'args_schema') and tool.args_schema:
|
||||
instructions.append("Parameters:\n")
|
||||
schema = tool.args_schema
|
||||
# Handle both dict and BaseModel schemas
|
||||
try:
|
||||
model_json_schema = getattr(schema, 'model_json_schema', None) # type: ignore
|
||||
if callable(model_json_schema):
|
||||
schema = model_json_schema()
|
||||
except Exception:
|
||||
pass
|
||||
if isinstance(schema, dict) and 'properties' in schema:
|
||||
for param_name, param_info in schema['properties'].items():
|
||||
param_type = param_info.get('type', 'any')
|
||||
param_desc = param_info.get('description', '')
|
||||
required = param_name in schema.get('required', [])
|
||||
req_marker = "(required)" if required else "(optional)"
|
||||
instructions.append(f" - {param_name}: {param_type} {req_marker} - {param_desc}\n")
|
||||
|
||||
instructions.append("\n")
|
||||
|
||||
instructions.append("\n# IMPORTANT RULES:\n")
|
||||
instructions.append("1. When you need to use a tool, respond ONLY with [TOOL_CALL] blocks\n")
|
||||
instructions.append("2. After receiving the result, continue your response\n")
|
||||
instructions.append("3. For simple questions, respond directly without tools\n")
|
||||
instructions.append("4. Use valid JSON in tool calls\n")
|
||||
instructions.append("5. If a tool fails, explain the error to the user\n")
|
||||
|
||||
return ''.join(instructions)
|
||||
|
||||
def _parse_tool_calls(self, text: str) -> List[ToolCall]:
|
||||
"""Parse le chiamate tool dalla risposta del modello"""
|
||||
tool_calls = []
|
||||
|
||||
# Try new format first: TOOL:toolname|arg1=val1
|
||||
lines = text.strip().split('\n')
|
||||
for line in lines:
|
||||
line = line.strip()
|
||||
if line.startswith('TOOL:'):
|
||||
# Parse: TOOL:toolname|arg1=val1|arg2=val2
|
||||
match = re.match(r'^TOOL:(\w+)(?:\|(.+))?$', line)
|
||||
if match:
|
||||
tool_name = match.group(1)
|
||||
if tool_name in self.tools:
|
||||
args = {}
|
||||
if match.group(2):
|
||||
# Parse arguments: arg1=val1|arg2=val2
|
||||
for arg_part in match.group(2).split('|'):
|
||||
if '=' in arg_part:
|
||||
key, val = arg_part.split('=', 1)
|
||||
args[key] = val
|
||||
tool_calls.append(ToolCall(
|
||||
id=str(uuid.uuid4())[:8],
|
||||
name=tool_name,
|
||||
arguments=args
|
||||
))
|
||||
# If we found a valid tool call, return only that one
|
||||
return tool_calls
|
||||
|
||||
# Try old format: [TOOL_CALL]{...}[/TOOL_CALL]
|
||||
matches = re.finditer(TOOL_CALL_PATTERN_OLD, text, re.DOTALL)
|
||||
for match in matches:
|
||||
try:
|
||||
data = json.loads(match.group(1))
|
||||
tool_name = data.get('tool')
|
||||
if tool_name and tool_name in self.tools:
|
||||
tool_calls.append(ToolCall(
|
||||
id=str(uuid.uuid4())[:8],
|
||||
name=tool_name,
|
||||
arguments=data.get('arguments', {})
|
||||
))
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
continue
|
||||
|
||||
return tool_calls
|
||||
|
||||
def _execute_tool(self, tool_call: ToolCall) -> Any:
|
||||
"""Esegui un singolo tool"""
|
||||
if tool_call.name not in self.tools:
|
||||
available = ", ".join(self.tools.keys())
|
||||
return {
|
||||
"error": f"Tool '{tool_call.name}' not found",
|
||||
"available_tools": available
|
||||
}
|
||||
|
||||
try:
|
||||
tool = self.tools[tool_call.name]
|
||||
|
||||
# Per tool senza argomenti, non passare arguments
|
||||
if tool_call.arguments:
|
||||
result = tool.invoke(tool_call.arguments)
|
||||
else:
|
||||
result = tool.invoke({})
|
||||
|
||||
# Converti result in stringa per il modello
|
||||
if result is None:
|
||||
return {"result": "Tool executed successfully"}
|
||||
elif isinstance(result, (dict, list)):
|
||||
return {"result": json.dumps(result, ensure_ascii=False, indent=2)}
|
||||
else:
|
||||
return {"result": str(result)}
|
||||
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
def _format_tool_result(self, tool_call: ToolCall, result: Any) -> str:
|
||||
"""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 intents using both patterns and LLM interpretation"""
|
||||
import re
|
||||
|
||||
text_lower = text.lower().strip()
|
||||
|
||||
# Extract potential file paths (case insensitive)
|
||||
path_match = re.search(r'([/\w.-]+\.\w+)', text, re.IGNORECASE)
|
||||
file_path = path_match.group(1) if path_match else None
|
||||
|
||||
# Execute command patterns - check FIRST for shell commands with flags
|
||||
# Shell commands like "ls -lha", "git status", "pwd", "echo" should use execute
|
||||
shell_commands = ['ls', 'git', 'cat', 'grep', 'find', 'pwd', 'echo', 'cd',
|
||||
'mkdir', 'rm', 'cp', 'mv', 'chmod', 'head', 'tail', 'sort', 'uniq']
|
||||
# Check for command alone or with arguments
|
||||
for cmd in shell_commands:
|
||||
if text_lower.strip() == cmd or text_lower.startswith(cmd + ' '):
|
||||
return ToolCall(id='intent', name='execute', arguments={'command': text.strip()})
|
||||
|
||||
# System info patterns (more comprehensive)
|
||||
system_patterns = [
|
||||
'sistema operativo', 'so sto', 'che os', 'che sistema', 'che so',
|
||||
'sto usando', 'che computer', 'che tipo di sistema',
|
||||
'versione python', 'che versione', 'dettagli sistema',
|
||||
'operating system', 'what os', 'which system', 'what system',
|
||||
'which computer', 'system info', 'python version'
|
||||
]
|
||||
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', 'mostrami',
|
||||
'file nella cartella', 'file del', 'cartella corrente', 'mostra i file',
|
||||
'cosa c\'è', 'cosa c\'e', 'cosa contiene', 'cosa trovo',
|
||||
'list files', 'list directory', 'ls', 'show files', 'show me',
|
||||
'what\'s in', 'what is in', 'files here', 'directory contents'
|
||||
]
|
||||
if any(p in text_lower for p in list_patterns):
|
||||
return ToolCall(id='intent', name='ls', arguments={'path': '.'})
|
||||
|
||||
# Search patterns
|
||||
search_patterns = [
|
||||
'cerca', 'cercare', 'trova', 'trovare', 'dove si trova',
|
||||
'search', 'find', 'file', '*.py', '*.md', '*.txt'
|
||||
]
|
||||
# If the message mentions file patterns, use search tool
|
||||
if 'cerca' in text_lower or 'search' in text_lower or 'find' in text_lower:
|
||||
# Extract directory and pattern
|
||||
dir_match = re.search(r'(?:in|alla|nella)\s+([/\w.-]+)', text)
|
||||
pattern_match = re.search(r'\*\.(\w+)', text)
|
||||
directory = dir_match.group(1) if dir_match else '.'
|
||||
pattern = f"*.{pattern_match.group(1)}" if pattern_match else "*"
|
||||
return ToolCall(id='intent', name='search', arguments={'directory': directory, 'pattern': pattern})
|
||||
|
||||
# Read/show file patterns - check for ANY read keyword AND a file path
|
||||
read_keywords = ['leggi', 'leggere', 'mostrami', 'apri', 'aprimi', 'visualizza',
|
||||
'vediamo', 'vedi', 'show me', 'show the', 'read', 'open',
|
||||
'display', 'view', 'contenuto', 'che dice']
|
||||
has_read_keyword = any(k in text_lower for k in read_keywords)
|
||||
|
||||
if has_read_keyword and file_path:
|
||||
return ToolCall(id='intent', name='read_file', arguments={'path': file_path})
|
||||
|
||||
# Write file patterns
|
||||
write_keywords = ['scrivi', 'scrivere', 'crea', 'creare', 'salva', 'salvare',
|
||||
'write', 'create', 'save', 'make file']
|
||||
has_write_keyword = any(k in text_lower for k in write_keywords)
|
||||
|
||||
if has_write_keyword and file_path:
|
||||
# Extract content - try multiple patterns
|
||||
content = None
|
||||
|
||||
# Pattern 1: "con dentro 'testo'" o "con 'testo'"
|
||||
content_match = re.search(r'con\s+(?:dentro\s+)?["\']([^"\']+)["\']', text, re.IGNORECASE)
|
||||
if content_match:
|
||||
content = content_match.group(1)
|
||||
|
||||
# Pattern 2: testo tra virgolette anywhere
|
||||
if not content:
|
||||
content_match = re.search(r'["\']([^"\']+)["\']', text)
|
||||
if content_match:
|
||||
content = content_match.group(1)
|
||||
|
||||
# Pattern 3: contenuto dopo "con" fino alla fine (senza virgolette)
|
||||
if not content:
|
||||
# Match: con <resto della frase senza virgolette iniziali/finali>
|
||||
after_con = re.search(r'con\s+["\']?(.+?)["\']?\s*$', text, re.IGNORECASE | re.DOTALL)
|
||||
if after_con:
|
||||
content = after_con.group(1).strip()
|
||||
|
||||
if not content:
|
||||
content = "Content not specified"
|
||||
|
||||
return ToolCall(id='intent', name='write_file', arguments={'path': file_path, 'content': content})
|
||||
|
||||
exec_patterns = [
|
||||
'esegui', 'eseguire', 'run', 'run command', 'lanciare',
|
||||
'avvia', 'comando', 'terminale', 'bash', 'shell'
|
||||
]
|
||||
if any(p in text_lower for p in exec_patterns):
|
||||
cmd_match = re.search(r'[`"]?([a-zA-Z0-9_.\s-]+)[`"]?\s*$', text)
|
||||
if cmd_match:
|
||||
return ToolCall(id='intent', name='execute', arguments={'command': cmd_match.group(1).strip()})
|
||||
|
||||
return None
|
||||
|
||||
def invoke(self, input_dict: Dict) -> Dict:
|
||||
"""
|
||||
Invoca l'agente con un input.
|
||||
|
||||
L'agente può fare multiple iterazioni tool-call prima di rispondere.
|
||||
"""
|
||||
# Estrai il messaggio utente
|
||||
user_message = input_dict.get("messages", [])[-1]
|
||||
|
||||
# Gestisci diversi formati di messaggio
|
||||
if isinstance(user_message, dict):
|
||||
user_content = user_message.get("content", str(user_message))
|
||||
elif hasattr(user_message, 'content'):
|
||||
user_content = user_message.content
|
||||
else:
|
||||
user_content = str(user_message)
|
||||
|
||||
# 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)
|
||||
|
||||
# Format result as a clean response
|
||||
response_text = ""
|
||||
try:
|
||||
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}
|
||||
|
||||
# Se nessun intent detectato, usa il modello per rispondere normalmente
|
||||
# (senza tool calling per ora - i modelli locali non lo supportano bene)
|
||||
response = self.llm.invoke(self.messages)
|
||||
self.messages.append(response)
|
||||
|
||||
response_text = response.content if hasattr(response, 'content') else str(response)
|
||||
return {"messages": self.messages, "response": response_text}
|
||||
|
||||
def clear_history(self):
|
||||
"""Pulisci la cronologia ma mantieni il 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 []
|
||||
|
||||
|
||||
def create_ollama_tool_agent(
|
||||
model,
|
||||
tools: Optional[List[Tool]] = None,
|
||||
system_prompt: Optional[str] = None,
|
||||
include_builtin_tools: bool = True
|
||||
) -> OllamaToolAgent:
|
||||
"""
|
||||
Factory function per creare un OllamaToolAgent.
|
||||
|
||||
Args:
|
||||
model: Il modello LLM da usare
|
||||
tools: Lista di tool personalizzati (opzionale)
|
||||
system_prompt: System prompt personalizzato (opzionale)
|
||||
include_builtin_tools: Se True, include i tool built-in (execute, read_file, etc.)
|
||||
"""
|
||||
if tools is None:
|
||||
tools = []
|
||||
|
||||
# Aggiungi i tool built-in
|
||||
if include_builtin_tools:
|
||||
builtin = get_builtin_tools()
|
||||
tools = builtin + tools
|
||||
|
||||
return OllamaToolAgent(
|
||||
llm=model,
|
||||
tools=tools,
|
||||
system_prompt=system_prompt
|
||||
)
|
||||
@@ -0,0 +1,48 @@
|
||||
[build-system]
|
||||
requires = ["setuptools>=61.0", "wheel"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "langcode-agent"
|
||||
version = "1.0.0"
|
||||
description = "AI Agent harness based on LangChain Deep Agents with memory, planning and MCP support"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
license = {text = "MIT"}
|
||||
authors = [
|
||||
{name = "LangCode Team"}
|
||||
]
|
||||
keywords = ["ai", "agent", "langchain", "ollama", "mcp"]
|
||||
classifiers = [
|
||||
"Development Status :: 4 - Beta",
|
||||
"Intended Audience :: Developers",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Programming Language :: Python :: 3",
|
||||
"Programming Language :: Python :: 3.10",
|
||||
"Programming Language :: Python :: 3.11",
|
||||
"Programming Language :: Python :: 3.12",
|
||||
]
|
||||
|
||||
dependencies = [
|
||||
"deepagents>=0.0.34",
|
||||
"langchain>=0.3.0",
|
||||
"langchain-community>=0.3.0",
|
||||
"langchain-openai>=0.1.0",
|
||||
"cryptography>=41.0.0",
|
||||
"requests>=2.31.0",
|
||||
"pyyaml>=6.0",
|
||||
"fastapi>=0.104.0",
|
||||
"uvicorn>=0.24.0",
|
||||
"pydantic>=2.5.0",
|
||||
"pytest>=7.4.0",
|
||||
]
|
||||
|
||||
[project.scripts]
|
||||
langcode = "langcode:main"
|
||||
|
||||
[project.urls]
|
||||
Homepage = "http://192.168.0.183:8085/truetype/langcode_agent"
|
||||
|
||||
[tool.setuptools.packages.find]
|
||||
where = ["."]
|
||||
include = ["*"]
|
||||
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
LangCode Agent - Setup
|
||||
"""
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
setup(
|
||||
name="langcode-agent",
|
||||
version="1.0.0",
|
||||
description="AI Agent harness based on LangChain Deep Agents",
|
||||
author="LangCode Team",
|
||||
packages=find_packages(),
|
||||
python_requires=">=3.10",
|
||||
install_requires=[
|
||||
"deepagents>=0.0.34",
|
||||
"langchain>=0.3.0",
|
||||
"langchain-community>=0.3.0",
|
||||
"langchain-openai>=0.1.0",
|
||||
"cryptography>=41.0.0",
|
||||
"requests>=2.31.0",
|
||||
"pyyaml>=6.0",
|
||||
"fastapi>=0.104.0",
|
||||
"uvicorn>=0.24.0",
|
||||
"pydantic>=2.5.0",
|
||||
"pytest>=7.4.0",
|
||||
],
|
||||
entry_points={
|
||||
"console_scripts": [
|
||||
"langcode=langcode:main",
|
||||
],
|
||||
},
|
||||
)
|
||||
@@ -0,0 +1,275 @@
|
||||
"""
|
||||
Skill manager for LangCode Agent
|
||||
Handles discovery, installation, and management of agent skills from skill registries like skills.sh
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, Any, Optional, Tuple
|
||||
import requests
|
||||
|
||||
from config_manager import config_manager
|
||||
|
||||
class SkillManager:
|
||||
"""Manages agent skills discovery and installation"""
|
||||
|
||||
def __init__(self, skills_dir: Optional[str] = None):
|
||||
"""
|
||||
Initialize the skill manager
|
||||
|
||||
Args:
|
||||
skills_dir: Directory to store skills. If None, uses ~/.langcode/skills
|
||||
"""
|
||||
if skills_dir is None:
|
||||
self.skills_dir = Path.home() / ".langcode" / "skills"
|
||||
else:
|
||||
self.skills_dir = Path(skills_dir)
|
||||
|
||||
# Ensure skills directory exists
|
||||
self.skills_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def search_skills(self, query: str, limit: int = 10) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Search for skills using skills.sh CLI or fallback to manual search
|
||||
|
||||
Args:
|
||||
query: Search query
|
||||
limit: Maximum number of results
|
||||
|
||||
Returns:
|
||||
List of skill information dictionaries
|
||||
"""
|
||||
# Try using skills.sh CLI first
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["skills", "search", query, "--limit", str(limit), "--json"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=30
|
||||
)
|
||||
|
||||
if result.returncode == 0:
|
||||
skills_data = json.loads(result.stdout)
|
||||
return skills_data.get("skills", [])
|
||||
except (FileNotFoundError, subprocess.TimeoutExpired, json.JSONDecodeError):
|
||||
# Fall back to manual search if skills.sh is not available
|
||||
pass
|
||||
|
||||
# Fallback: try to use known skill registries or return empty
|
||||
return self._fallback_skill_search(query, limit)
|
||||
|
||||
def _fallback_skill_search(self, query: str, limit: int) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Fallback skill search when skills.sh CLI is not available
|
||||
|
||||
Args:
|
||||
query: Search query
|
||||
limit: Maximum number of results
|
||||
|
||||
Returns:
|
||||
List of skill information dictionaries
|
||||
"""
|
||||
# For now, return empty list - in a full implementation, this would
|
||||
# query known skill registries via their APIs
|
||||
return []
|
||||
|
||||
def get_skill_details(self, skill_name: str) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
Get detailed information about a specific skill
|
||||
|
||||
Args:
|
||||
skill_name: Name of the skill to get details for
|
||||
|
||||
Returns:
|
||||
Skill details dictionary or None if not found
|
||||
"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["skills", "show", skill_name, "--json"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=30
|
||||
)
|
||||
|
||||
if result.returncode == 0:
|
||||
return json.loads(result.stdout)
|
||||
except (FileNotFoundError, subprocess.TimeoutExpired, json.JSONDecodeError):
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
def install_skill(self, skill_name: str, version: Optional[str] = None) -> Tuple[bool, str]:
|
||||
"""
|
||||
Install a skill from the skill registry
|
||||
|
||||
Args:
|
||||
skill_name: Name of the skill to install
|
||||
version: Specific version to install (optional)
|
||||
|
||||
Returns:
|
||||
Tuple of (success, message)
|
||||
"""
|
||||
try:
|
||||
cmd = ["skills", "add", skill_name]
|
||||
if version:
|
||||
cmd.extend(["--version", version])
|
||||
|
||||
result = subprocess.run(
|
||||
cmd,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=60 # Skills might take time to install
|
||||
)
|
||||
|
||||
if result.returncode == 0:
|
||||
# Refresh installed skills list
|
||||
self._refresh_installed_skills()
|
||||
return True, f"Skill '{skill_name}' installed successfully"
|
||||
else:
|
||||
return False, f"Failed to install skill: {result.stderr}"
|
||||
except (FileNotFoundError, subprocess.TimeoutExpired) as e:
|
||||
return False, f"Error installing skill: {str(e)}"
|
||||
|
||||
def uninstall_skill(self, skill_name: str) -> Tuple[bool, str]:
|
||||
"""
|
||||
Uninstall a skill
|
||||
|
||||
Args:
|
||||
skill_name: Name of the skill to uninstall
|
||||
|
||||
Returns:
|
||||
Tuple of (success, message)
|
||||
"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["skills", "rm", skill_name],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=30
|
||||
)
|
||||
|
||||
if result.returncode == 0:
|
||||
# Refresh installed skills list
|
||||
self._refresh_installed_skills()
|
||||
return True, f"Skill '{skill_name}' uninstalled successfully"
|
||||
else:
|
||||
return False, f"Failed to uninstall skill: {result.stderr}"
|
||||
except (FileNotFoundError, subprocess.TimeoutExpired) as e:
|
||||
return False, f"Error uninstalling skill: {str(e)}"
|
||||
|
||||
def list_installed_skills(self) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
List all installed skills
|
||||
|
||||
Returns:
|
||||
List of installed skill information dictionaries
|
||||
"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["skills", "list", "--json"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=30
|
||||
)
|
||||
|
||||
if result.returncode == 0:
|
||||
skills_data = json.loads(result.stdout)
|
||||
return skills_data.get("skills", [])
|
||||
except (FileNotFoundError, subprocess.TimeoutExpired, json.JSONDecodeError):
|
||||
pass
|
||||
|
||||
# Fallback: check local skills directory
|
||||
return self._list_local_skills()
|
||||
|
||||
def _list_local_skills(self) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
List skills installed in the local skills directory
|
||||
|
||||
Returns:
|
||||
List of skill information dictionaries
|
||||
"""
|
||||
skills = []
|
||||
|
||||
if not self.skills_dir.exists():
|
||||
return skills
|
||||
|
||||
for skill_dir in self.skills_dir.iterdir():
|
||||
if skill_dir.is_dir():
|
||||
skill_file = skill_dir / "SKILL.md"
|
||||
if skill_file.exists():
|
||||
# Try to read basic info from SKILL.md
|
||||
try:
|
||||
content = skill_file.read_text(encoding='utf-8')
|
||||
# Extract name from first line if it's a markdown header
|
||||
name = skill_dir.name
|
||||
description = "Skill installed locally"
|
||||
|
||||
# Try to extract more info from SKILL.md frontmatter
|
||||
if content.startswith('---'):
|
||||
end_frontmatter = content.find('---', 3)
|
||||
if end_frontmatter != -1:
|
||||
frontmatter = content[3:end_frontmatter]
|
||||
# Simple parsing for name and description
|
||||
for line in frontmatter.split('\n'):
|
||||
if line.startswith('name:'):
|
||||
name = line.split(':', 1)[1].strip()
|
||||
elif line.startswith('description:'):
|
||||
description = line.split(':', 1)[1].strip()
|
||||
|
||||
skills.append({
|
||||
"name": name,
|
||||
"description": description,
|
||||
"path": str(skill_dir),
|
||||
"installed": True
|
||||
})
|
||||
except Exception:
|
||||
# If we can't read the skill properly, still list it
|
||||
skills.append({
|
||||
"name": skill_dir.name,
|
||||
"description": "Skill installed locally",
|
||||
"path": str(skill_dir),
|
||||
"installed": True
|
||||
})
|
||||
|
||||
return skills
|
||||
|
||||
def _refresh_installed_skills(self):
|
||||
"""Refresh the cache of installed skills"""
|
||||
# This would typically update some internal cache
|
||||
# For now, we rely on calling list_installed_skills() which checks fresh
|
||||
pass
|
||||
|
||||
def skill_is_installed(self, skill_name: str) -> bool:
|
||||
"""
|
||||
Check if a skill is installed
|
||||
|
||||
Args:
|
||||
skill_name: Name of the skill to check
|
||||
|
||||
Returns:
|
||||
True if skill is installed, False otherwise
|
||||
"""
|
||||
installed_skills = self.list_installed_skills()
|
||||
return any(skill.get("name") == skill_name for skill in installed_skills)
|
||||
|
||||
def get_skill_path(self, skill_name: str) -> Optional[Path]:
|
||||
"""
|
||||
Get the filesystem path where a skill is installed
|
||||
|
||||
Args:
|
||||
skill_name: Name of the skill
|
||||
|
||||
Returns:
|
||||
Path to the skill directory or None if not installed
|
||||
"""
|
||||
installed_skills = self.list_installed_skills()
|
||||
for skill in installed_skills:
|
||||
if skill.get("name") == skill_name:
|
||||
return Path(skill.get("path", ""))
|
||||
return None
|
||||
|
||||
|
||||
# Global skill manager instance
|
||||
skill_manager = SkillManager()
|
||||
+224
@@ -0,0 +1,224 @@
|
||||
"""
|
||||
Terminal UI con scrollback semplice
|
||||
Usa input() standard + visualizzazione scrollback
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import signal
|
||||
from typing import Optional, Callable
|
||||
|
||||
|
||||
class TerminalUI:
|
||||
"""
|
||||
Terminal UI con scrollback.
|
||||
Uso semplice: input() per input, history buffer per scrollback.
|
||||
"""
|
||||
|
||||
def __init__(self, scrollback: int = 10000, prompt: str = "❯ "):
|
||||
self.scrollback = scrollback
|
||||
self.prompt = prompt
|
||||
|
||||
# History buffer
|
||||
self.history: list = []
|
||||
|
||||
# Callback
|
||||
self.agent_callback: Optional[Callable[[str], str]] = None
|
||||
|
||||
def _print(self, text: str):
|
||||
"""Stampa con colori base"""
|
||||
# Semplice - solo reset e bold
|
||||
if text.startswith("👤"):
|
||||
print(f"\033[1;36m{text}\033[0m")
|
||||
elif text.startswith("🤖"):
|
||||
print(f"\033[1;32m{text}\033[0m")
|
||||
elif text.startswith("🤔"):
|
||||
print(f"\033[1;33m{text}\033[0m")
|
||||
elif text.startswith("❌"):
|
||||
print(f"\033[1;31m{text}\033[0m")
|
||||
else:
|
||||
print(text)
|
||||
|
||||
def _wrap_line(self, text: str, width: int = 80) -> list:
|
||||
"""Wrap linea a width"""
|
||||
lines = []
|
||||
while len(text) > width:
|
||||
lines.append(text[:width])
|
||||
text = text[width:]
|
||||
if text:
|
||||
lines.append(text)
|
||||
return lines
|
||||
|
||||
def _add_history(self, text: str):
|
||||
"""Aggiunge a history"""
|
||||
self.history.append(text)
|
||||
while len(self.history) > self.scrollback:
|
||||
self.history.pop(0)
|
||||
|
||||
def _show_history(self, lines: int = None):
|
||||
"""Mostra ultime N linee di history"""
|
||||
if lines is None:
|
||||
# Determina righe visibili
|
||||
try:
|
||||
size = os.get_terminal_size()
|
||||
lines = size.lines - 5 # spazio per header, status, prompt
|
||||
except:
|
||||
lines = 20
|
||||
|
||||
# Mostra ultime righe
|
||||
start = max(0, len(self.history) - lines)
|
||||
for line in self.history[start:]:
|
||||
self._print(line)
|
||||
|
||||
def _process(self, cmd: str):
|
||||
"""Processa comando"""
|
||||
if not cmd.strip():
|
||||
return
|
||||
|
||||
self._add_history(f"👤 {cmd}")
|
||||
|
||||
# Thinking
|
||||
print("\033[1;33m🤔 Pensando...\033[0m")
|
||||
|
||||
try:
|
||||
resp = self.agent_callback(cmd)
|
||||
|
||||
# Rimuovi thinking (print sopra con risposta)
|
||||
print("\033[2A\033[K", end="") # move up 2, clear line
|
||||
|
||||
# Stampa risposta
|
||||
for line in resp.split('\n'):
|
||||
if line.strip():
|
||||
self._add_history(f"🤖 {line}")
|
||||
self._print(f"🤖 {line}")
|
||||
|
||||
except Exception as e:
|
||||
print("\033[2A\033[K", end="")
|
||||
err_msg = f"❌ {str(e)}"
|
||||
self._add_history(err_msg)
|
||||
self._print(err_msg)
|
||||
|
||||
def _scroll_help(self):
|
||||
"""Mostra aiuto scroll"""
|
||||
print("\n" + "=" * 60)
|
||||
print("SCROLLBACK - Premi q per uscire, frecce per navigare")
|
||||
print("=" * 60)
|
||||
print()
|
||||
|
||||
# Paginator semplice
|
||||
page_size = 30
|
||||
start = 0
|
||||
total = len(self.history)
|
||||
|
||||
while start < total:
|
||||
# Mostra pagina
|
||||
for i in range(start, min(start + page_size, total)):
|
||||
self._print(self.history[i])
|
||||
|
||||
start += page_size
|
||||
|
||||
if start < total:
|
||||
print()
|
||||
print(f"[{start}/{total} righe] - Premi ENTER per continuare, q per uscire")
|
||||
try:
|
||||
choice = input().strip()
|
||||
if choice.lower() == 'q':
|
||||
break
|
||||
except:
|
||||
break
|
||||
|
||||
print()
|
||||
print("=" * 60)
|
||||
print("FINE SCROLLBACK")
|
||||
print("=" * 60)
|
||||
|
||||
def start(self, agent_callback: Callable[[str], str]):
|
||||
"""Avvia UI"""
|
||||
self.agent_callback = agent_callback
|
||||
|
||||
# Signal handler
|
||||
def sigint_handler(signum, frame):
|
||||
print("\n\033[1;33m👋 Arrivederci!\033[0m\n")
|
||||
sys.exit(0)
|
||||
|
||||
signal.signal(signal.SIGINT, sigint_handler)
|
||||
|
||||
# Header
|
||||
print()
|
||||
print("\033[1;36m" + "=" * 50 + "\033[0m")
|
||||
print("\033[1;36m=\033[0m" + " " * 15 + "\033[1;33mLangCode Agent\033[0m" + " " * 15 + "\033[1;36m=\033[0m")
|
||||
print("\033[1;36m" + "=" * 50 + "\033[0m")
|
||||
print()
|
||||
print("digita /help per aiuto, /exit per uscire")
|
||||
print()
|
||||
|
||||
while True:
|
||||
try:
|
||||
# Input
|
||||
user_input = input(f"\033[1;35m{self.prompt}\033[0m ").strip()
|
||||
except EOFError:
|
||||
break
|
||||
except KeyboardInterrupt:
|
||||
print("\n\033[1;33m👋 Arrivederci!\033[0m")
|
||||
break
|
||||
|
||||
if not user_input:
|
||||
continue
|
||||
|
||||
# Comandi speciali
|
||||
if user_input.lower() == '/exit':
|
||||
print("\033[1;33m👋 Arrivederci!\033[0m")
|
||||
break
|
||||
|
||||
elif user_input.lower() == '/quit':
|
||||
print("\033[1;33m👋 Arrivederci!\033[0m")
|
||||
break
|
||||
|
||||
elif user_input.lower() == '/help':
|
||||
print()
|
||||
print("\033[1;36m" + "=" * 40 + "\033[0m")
|
||||
print("COMANDI:")
|
||||
print(" /help - Questo messaggio")
|
||||
print(" /clear - Pulisci schermo")
|
||||
print(" /history - Mostra cronologia")
|
||||
print(" /scroll - Entra in scrollback")
|
||||
print(" /exit, /quit - Esci")
|
||||
print()
|
||||
print("SCROLLBACK:")
|
||||
print(" /scroll - Mostra tutto lo scrollback")
|
||||
print(" Frecce su/giù - Naviga cronologia (in some terminals)")
|
||||
print()
|
||||
print("\033[1;36m" + "=" * 40 + "\033[0m")
|
||||
print()
|
||||
continue
|
||||
|
||||
elif user_input.lower() == '/clear':
|
||||
os.system('clear' if os.name != 'nt' else 'cls')
|
||||
continue
|
||||
|
||||
elif user_input.lower() == '/history':
|
||||
print()
|
||||
self._show_history(50)
|
||||
print()
|
||||
continue
|
||||
|
||||
elif user_input.lower() == '/scroll':
|
||||
self._scroll_help()
|
||||
continue
|
||||
|
||||
# Processa comando normale
|
||||
self._process(user_input)
|
||||
print()
|
||||
|
||||
|
||||
def create_terminal_ui(scrollback: int = 10000, prompt: str = "❯ ") -> TerminalUI:
|
||||
return TerminalUI(scrollback=scrollback, prompt=prompt)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
def test(text):
|
||||
lines = [f"Ricevuto: {text}"]
|
||||
lines += [f"Linea {i}: contenuto di esempio" for i in range(1, 31)]
|
||||
return "\n".join(lines)
|
||||
|
||||
ui = create_terminal_ui()
|
||||
ui.start(test)
|
||||
@@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script per debuggare langcode agent
|
||||
"""
|
||||
import sys
|
||||
sys.path.insert(0, '/home/codex/langChainAgent/langcode_agent')
|
||||
|
||||
print("1. Importing modules...", flush=True)
|
||||
|
||||
from langchain_core.messages import HumanMessage
|
||||
|
||||
print("2. Importing main...", flush=True)
|
||||
from main import create_langcode_agent
|
||||
|
||||
print("3. Creating agent...", flush=True)
|
||||
agent = create_langcode_agent()
|
||||
|
||||
print(f"4. Agent type: {type(agent)}", flush=True)
|
||||
|
||||
print("5. Invoking with test message...", flush=True)
|
||||
try:
|
||||
result = agent.invoke({"messages": [HumanMessage(content="che SO sto usando")]})
|
||||
print(f"6. Result type: {type(result)}", flush=True)
|
||||
print(f"7. Result keys: {result.keys() if hasattr(result, 'keys') else 'NO KEYS'}", flush=True)
|
||||
|
||||
if "response" in result:
|
||||
print(f"8. Response: {result['response'][:200]}...", flush=True)
|
||||
else:
|
||||
print("8. No 'response' key in result!", flush=True)
|
||||
if result.get("messages"):
|
||||
last_msg = result["messages"][-1]
|
||||
print(f" Last message: {last_msg.content if hasattr(last_msg, 'content') else str(last_msg)[:100]}", flush=True)
|
||||
|
||||
except Exception as e:
|
||||
print(f"ERROR: {type(e).__name__}: {e}", flush=True)
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
Reference in new issue
Block a user