This commit is contained in:
2026-03-28 23:23:26 +00:00
parent 01c48c2cb0
commit 8fc8ed419f
3 changed files with 87 additions and 25 deletions

View File

@@ -25,11 +25,14 @@ logging.basicConfig(
)
logger = logging.getLogger(__name__)
# Dicionário global para manter o histórico (Em um sistema de produção, usar Redis ou DB)
chat_histories = {}
async def call_antigravity_api(endpoint: str, payload: dict) -> str:
"""Faz a chamada para a API interna do BotVPS."""
async with httpx.AsyncClient(timeout=120.0) as client:
try:
logger.info(f"Enviando payload para {endpoint}: {payload}")
logger.info(f"Enviando payload para {endpoint}: {payload.get('text', payload.get('task'))}")
response = await client.post(f"{API_BASE_URL}{endpoint}", json=payload)
response.raise_for_status()
data = response.json()
@@ -46,6 +49,7 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not update.message or not update.message.text:
return
chat_id = update.effective_chat.id
user_id = update.effective_user.id
# Filtro de Segurança
@@ -56,9 +60,13 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
text = update.message.text
logger.info(f"Mensagem recebida de {user_id}: {text}")
# Inicializa histórico se não existir
if chat_id not in chat_histories:
chat_histories[chat_id] = []
# Diferenciação entre comandos de sistema e conhecimento geral
if text.startswith(('/bash', '/vps', '/cmd')):
# Remove prefixos para enviar a tarefa limpa para o orquestrador
# Comandos de sistema geralmente não precisam de histórico de chat natural
task = text.replace('/bash', '').replace('/vps', '').replace('/cmd', '').strip()
if not task:
@@ -68,13 +76,24 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("⚙️ *Processando tarefa no Claw System...*", parse_mode='Markdown')
reply = await call_antigravity_api("/api/orchestrate", {"task": task})
else:
# Chat Natural
# Nota: O bot local mantém o histórico se enviarmos, mas aqui simplificamos para 1-to-1
# A API /api/chat já lida com o prompt do sistema CLAW
reply = await call_antigravity_api("/api/chat", {"text": text})
# Chat Natural com contexto
await context.bot.send_chat_action(chat_id=chat_id, action="typing")
# Prepara payload com histórico
payload = {
"text": text,
"history": chat_histories[chat_id][-10:] # Envia os últimos 10 turnos
}
reply = await call_antigravity_api("/api/chat", payload)
# Atualiza histórico Local
chat_histories[chat_id].append({"user": text, "bot": reply})
# Mantém apenas os últimos 15 para não crescer infinito no middleware
if len(chat_histories[chat_id]) > 15:
chat_histories[chat_id].pop(0)
# Envia a resposta de volta para o usuário
# Se a resposta for muito longa, o Telegram pode cortar (máx 4096 chars)
if len(reply) > 4000:
reply = reply[:3900] + "... [Texto truncado]"