🚀 Auto-deploy: BotVPS atualizado em 29/04/2026 10:42:36

This commit is contained in:
2026-04-29 10:42:36 +00:00
parent 63d78a53f8
commit 4158e39106
2 changed files with 52 additions and 31 deletions

View File

@@ -78,32 +78,40 @@ DIRETRIZES:
current_history = history_str
max_iterations = 6
total_in = 0
total_out = 0
final_model = current_model
for i in range(max_iterations):
print(f"[AGENT] Iteração {i+1} - Enviando para {provider} (modelo padrão)...")
try:
response = await get_llm_response_async(system_prompt + current_history, provider, cfg)
res_dict = await call_llm(provider, current_model, system_prompt + current_history)
# Lógica de FALLBACK: Se o Qwen falhar ou retornar erro de API, tenta o Ling-2.6-flash
if response.startswith("Erro OpenRouter") and provider == "openrouter" and current_model == "qwen/qwen-2.5-72b-instruct":
if res_dict["content"].startswith("Erro OpenRouter") and provider == "openrouter" and current_model == "qwen/qwen-2.5-72b-instruct":
backup_model = "inclusionai/ling-2.6-flash:free"
print(f"⚠️ [FALLBACK CHAT] Falha no Qwen. Tentando {backup_model}...")
response = await call_llm("openrouter", backup_model, system_prompt + current_history)
res_dict = await call_llm("openrouter", backup_model, system_prompt + current_history)
except Exception as e:
if provider == "openrouter" and current_model == "qwen/qwen-2.5-72b-instruct":
backup_model = "inclusionai/ling-2.6-flash:free"
print(f"⚠️ [FALLBACK CHAT] Exceção no Qwen ({str(e)}). Tentando {backup_model}...")
response = await call_llm("openrouter", backup_model, system_prompt + current_history)
res_dict = await call_llm("openrouter", backup_model, system_prompt + current_history)
else:
return f"Erro Crítico no Agente: {str(e)}"
response = res_dict["content"]
usage = res_dict.get("usage", {})
total_in += usage.get("prompt_tokens", 0)
total_out += usage.get("completion_tokens", 0)
final_model = res_dict.get("model", final_model)
print(f"[LLM RESPONSE]: {response}")
# Regex mais flexível: tenta casar [TOOL:nome] e extrair o conteúdo até [/TOOL] ou final da string
# Regex robusto: captura [TOOL:nome] ou TOOL:nome (sem colchetes como fallback)
match = re.search(r"(?:\[?TOOL:([\w_]+)\]?|\[TOOL:([\w_]+)\])", response, re.I)
if match:
t_name = (match.group(1) or match.group(2)).strip().lower()
# Mapeamento de conveniência/atalho
if t_name == "run": t_name = "run_bash_command"
content_after = response[match.end():]
@@ -117,13 +125,11 @@ DIRETRIZES:
func = tool_info["func"]
print(f"[AGENT] Executando {t_name} com argumento: {arg[:50]}...")
# Execução (suporta async se necessário, embora tools.py seja sync)
if asyncio.iscoroutinefunction(func):
obs = await func(arg) if arg else await func()
else:
obs = func(arg) if arg else func()
# Se o resultado for um dicionário (comum em tools_v2), extrai o output ou converte para string
if isinstance(obs, dict):
obs = obs.get("output") or obs.get("message") or str(obs)
@@ -136,10 +142,13 @@ DIRETRIZES:
print(f"[AGENT] Erro: Ferramenta '{t_name}' não encontrada.")
current_history += f"\nAgente: {response}\nSISTEMA: Erro: Ferramenta '{t_name}' inexistente no sistema.\n"
else:
# Se não há ferramenta, terminou o pensamento.
return response
# Terminou o pensamento. Adiciona rodapé de tokens.
footer = f"\n\n---\n⚙️ **Modelo:** `{final_model}`\n📊 **Tokens:** `{total_in} IN` / `{total_out} OUT`"
if "RESUMO:" in response:
return response + footer
return response + footer
# Ao atingir o limite, tenta ao menos limpar a resposta final
final_reply = response if 'response' in locals() else 'Nenhuma'
final_reply = re.sub(r'[<\[]/?REFINED[>\]]', '', final_reply, flags=re.IGNORECASE).strip()
return f"Limite de iterações atingido. RESUMO: {final_reply}"
footer = f"\n\n---\n⚠️ *Limite de iterações atingido*\n⚙️ **Modelo:** `{final_model}`\n📊 **Tokens:** `{total_in} IN` / `{total_out} OUT`"
return f"RESUMO: {final_reply}" + footer