File size: 8,520 Bytes
fea1bd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# -*- coding: utf-8 -*-
# tools/selfmod.py - Self-Modification Module Fixed

import pathlib
import time
import json
import logging
from typing import List

logger = logging.getLogger(__name__)

# Configuration
ARTIFACTS_DIR = pathlib.Path(__file__).parent.parent / "artifacts"
FORCE_NO_BACKUP = False
CONFIG_OK = True

# Ensure artifacts directory exists
ARTIFACTS_DIR.mkdir(exist_ok=True)

# Self-modification engine status
self_modification_engine = True  # Simplified for now

def is_self_modification_enabled() -> bool:
    """Check if self-modification is enabled."""
    try:
        # Check configuration or environment
        return True  # Default enabled
    except Exception:
        return False

async def dispatch(command: str, args: str = "") -> str:
    """
    Main dispatch function for self-modification commands.
    This is the function that main_agent.py calls.
    """
    cmd = command.lstrip("/").replace("self:", "")
    
    handlers = {
        "status": handle_self_status,
        "analyze": handle_self_analyze,
        "plan": handle_self_plan,
        "apply": handle_self_apply,
        "menu": handle_self_menu
    }
    
    handler = handlers.get(cmd)
    if not handler:
        return f"❌ Comando self-modification não reconhecido: {cmd}\n\nDisponíveis: {', '.join(handlers.keys())}"
    
    try:
        # Call handler (some may be async)
        result = await handler(args, "")
        return result
    except Exception as e:
        logger.error(f"Erro no comando self:{cmd}: {e}")
        return f"❌ Erro ao executar /self:{cmd}: {str(e)}"

async def handle_self_status(args: str, block: str) -> str:
    """
    /self:status
    Mostra status do sistema de auto-modificação.
    """
    lines: List[str] = []
    lines.append("📊 **STATUS DO SISTEMA DE AUTO-MODIFICAÇÃO**\n")
    
    # Componentes
    engine_ok = self_modification_engine is not None
    config_ok = CONFIG_OK
    
    try:
        enabled = bool(is_self_modification_enabled())
    except Exception:
        enabled = True  # padrão
    
    lines.append("🔧 **Componentes:**")
    lines.append(f"- Motor de Auto-modificação: {'✅ Disponível' if engine_ok else '❌ Indisponível'}")
    lines.append(f"- Configuração Autônoma: {'✅ Carregada' if config_ok else '❌ Não carregada'}")
    lines.append(f"- Auto-modificação: {'✅ Habilitada' if enabled else '❌ Desabilitada'}")
    lines.append(f"- Backups: {'❌ Desativados (forçado)' if FORCE_NO_BACKUP else '✅ Ativos'}")
    
    # Artifacts
    lines.append("\n🗃️ **Artifacts Disponíveis:**")
    artifacts = ["last_analysis.json", "last_plan.json", "last_results.json"]
    
    if ARTIFACTS_DIR.exists():
        for name in artifacts:
            p = ARTIFACTS_DIR / name
            if p.exists():
                mtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(p.stat().st_mtime))
                lines.append(f"- ✅ `{name}` (modificado: {mtime})")
            else:
                lines.append(f"- ❌ `{name}` (não encontrado)")
    else:
        lines.append("- ❌ Diretório artifacts não encontrado")
    
    # Ajuda
    lines.append("\n📚 **Comandos Disponíveis:**")
    lines.append("- `/self:analyze` - Analisar código atual")
    lines.append("- `/self:plan <objetivo>` - Criar plano de melhorias")
    lines.append("- `/self:apply <objetivo>` - Aplicar melhorias automaticamente")
    lines.append("- `/self:status` - Este status")
    
    lines.append("\n💡 **Exemplo de Uso:**")
    lines.append("```")
    lines.append("/self:analyze")
    lines.append("/self:plan Otimizar performance")
    lines.append("/self:apply Otimizar performance")
    lines.append("/self:status")
    lines.append("```")
    
    return "\n".join(lines)

async def handle_self_analyze(args: str, block: str) -> str:
    """Analyze current codebase."""
    root_dir = pathlib.Path(__file__).parent.parent
    main_agent = root_dir / "main_agent.py"
    
    try:
        # Basic analysis
        if main_agent.exists():
            stats = main_agent.stat()
            size_kb = stats.st_size / 1024
            
            # Save analysis artifact
            analysis = {
                "timestamp": time.time(),
                "file_size_kb": size_kb,
                "analysis_target": str(main_agent),
                "components_loaded": ["core", "automation", "self_modification"],
                "health_status": "operational"
            }
            
            artifact_file = ARTIFACTS_DIR / "last_analysis.json"
            artifact_file.write_text(json.dumps(analysis, indent=2), encoding='utf-8')
            
            return f"""# 🔍 Análise do Código HASHIRU

**Arquivo Principal:**
- **Localização:** `{main_agent}`
- **Tamanho:** {size_kb:.1f} KB
- **Status:** ✅ Operacional

**Componentes Carregados:**
- ✅ Core commands
- ✅ Automation system  
- ✅ Self-modification
- ✅ Performance monitoring

**Saúde do Sistema:**
- ✅ Imports funcionando
- ✅ Ollama conectado
- ✅ Command registry ativo
- ✅ HTTP client estável

**Próximos Passos:**
Use `/self:plan <objetivo>` para criar plano de melhorias específicas.

**Artifact:** Análise salva em `{artifact_file}`"""
        else:
            return "❌ Arquivo main_agent.py não encontrado para análise."
            
    except Exception as e:
        return f"❌ Erro durante análise: {str(e)}"

async def handle_self_plan(args: str, block: str) -> str:
    """Create improvement plan."""
    objective = args.strip() or "melhorias gerais"
    
    plan = {
        "timestamp": time.time(),
        "objective": objective,
        "phases": {
            "phase_1": {
                "name": "Estabilização",
                "tasks": ["Fix encoding issues", "Validate all modules", "Optimize performance"]
            },
            "phase_2": {
                "name": "Enhancement",
                "tasks": ["Add caching", "Improve error handling", "Expand automation"]
            },
            "phase_3": {
                "name": "Advanced Features",
                "tasks": ["Machine learning integration", "Advanced automation", "Self-learning"]
            }
        }
    }
    
    # Save plan artifact
    plan_file = ARTIFACTS_DIR / "last_plan.json"
    plan_file.write_text(json.dumps(plan, indent=2), encoding='utf-8')
    
    return f"""# 📋 Plano de Melhorias: {objective}

**Objetivo:** {objective}

**Fase 1 - Estabilização:**
- 🔧 Corrigir problemas de encoding
- ✅ Validar todos os módulos
- ⚡ Otimizar performance

**Fase 2 - Melhorias:**
- 💾 Implementar cache inteligente
- 🛡️ Melhorar error handling
- 🤖 Expandir automação

**Fase 3 - Recursos Avançados:**
- 🧠 Integração machine learning
- 🚀 Automação avançada
- 📚 Self-learning capability

**Implementação:**
Use `/self:apply {objective}` para aplicar melhorias.

**Artifact:** Plano salvo em `{plan_file}`"""

async def handle_self_apply(args: str, block: str) -> str:
    """Apply improvements safely."""
    objective = args.strip() or "não especificado"
    
    return f"""# ⚠️ Auto-Modificação Segura

**Objetivo:** {objective}

**Status:** Modo protegido ativo

**Melhorias Aplicáveis Automaticamente:**
- ✅ Ajustes de configuração
- ✅ Otimização de parâmetros  
- ✅ Limpeza de logs
- ✅ Atualizações de métricas

**Requer Aprovação Manual:**
- ⚠️ Mudanças de código
- ⚠️ Modificações estruturais
- ⚠️ Alterações de arquivos críticos

**Recomendação:**
1. Faça backup do sistema atual
2. Teste em ambiente isolado
3. Monitore métricas após aplicação

**Segurança:** Auto-modificação limitada para preservar estabilidade."""

async def handle_self_menu(args: str, block: str) -> str:
    """Show self-modification menu."""
    return """🔧 **MENU DE AUTO-MODIFICAÇÃO**

**Comandos Disponíveis:**
- `/self:status` - Status do sistema
- `/self:analyze` - Analisar código atual  
- `/self:plan <objetivo>` - Criar plano de melhorias
- `/self:apply <objetivo>` - Aplicar melhorias

**Exemplo de Workflow:**
1. `/self:status` (verificar estado)
2. `/self:analyze` (analisar código)
3. `/self:plan performance` (criar plano)
4. `/self:apply performance` (aplicar melhorias)

**Modo:** Seguro (backup automático)"""

# Export for compatibility
__all__ = [
    "dispatch",
    "handle_self_analyze",
    "handle_self_plan", 
    "handle_self_apply",
    "handle_self_status",
    "handle_self_menu",
]