Debug Local¶
Executando o projeto localmente¶
1. Preparar ambiente¶
# Ativar virtualenv
source venv/bin/activate
# Verificar variáveis
cp .env.example .env
# Edite o .env com suas credenciais
2. Iniciar o servidor¶
python unified_bot.py
O servidor startup na porta definida em PORT (padrão: 8080).
3. Testar endpoint¶
# Health check
curl http://localhost:8080/health
# Testar agente
curl -X POST http://localhost:8080/trip/message \
-H "Content-Type: application/json" \
-H "Authorization: Bearer SEU_JWT" \
-d '{"message": {"text": "Olá"}}'
Debugging com VS Code¶
Launch configuration¶
{
"name": "Python: Debug Bot",
"type": "python",
"request": "launch",
"module": "uvicorn",
"args": ["unified_bot:app", "--reload", "--port", "8080"],
"envFile": "${workspaceFolder}/.env"
}
Breakpoints¶
Coloque breakpoints em:
- ifriend_agent/agent.py — Entry point do agente
- ifriend_agent/tools/* — Suas tools
- messaging/processor.py — Processador de mensagens
Variáveis importantes para debug¶
# Logging detalhado
LOG_LEVEL=DEBUG
# Modo debug (ativa stack traces)
DEBUG=true
# Desabilitar produção de blocos (cards)
SUPPRESS_BLOCKS=true
Erros comuns¶
"JWT validation failed"¶
- Verificar
JWT_SECRETno .env - Verificar se o token está no formato correto
- Usar script
generate_test_jwt.pypara gerar tokens de teste
"Service account key not found"¶
export GOOGLE_APPLICATION_CREDENTIALS=./path/to/key.json
"Connection refused" no Redis/CloudSQL¶
- Verificar variáveis de conexão
- Para desenvolvimento local, pode usar Docker Compose
Logs úteis¶
# No código
logger.debug(f"State: {tool_context.state}")
logger.info(f"User: {user_id}")
# No terminal, ver tudo
LOG_LEVEL=DEBUG python unified_bot.py
Testar tools isoladamente¶
import asyncio
from ifriend_agent.tools.busca_produtos import busca_produtos
async def test():
from unittest.mock import MagicMock
ctx = MagicMock()
ctx.state = {"user_id": "test"}
result = await busca_produtos(ctx, metadados_busca="São Paulo")
print(result)
asyncio.run(test())