Files
AI-upflux-docprocessor/code/services/authorization.py

61 lines
1.8 KiB
Python

import json
import time
from utils.langgraph_agent import RULES, run_agent
async def evaluate_servico(servico: dict, guia: dict, file_content: str) -> dict:
codigo_servico_raw = str(servico.get("codigoServico", ""))
code = "".join(c for c in codigo_servico_raw if c.isdigit())
if code not in RULES:
return {
"codigoServico": codigo_servico_raw,
"resultado": "SKIPPED",
"motivo": f"Codigo '{code}' nao encontrado nas regras",
"agentOutput": "",
"tempoAgentSegundos": 0,
}
query_data = {
"atendimento": guia.get("atendimento", {}),
"guia": guia.get("guia", {}),
"servico": servico,
"historico": guia.get("historico", {}),
}
query = json.dumps(query_data, indent=2, ensure_ascii=False)
t0 = time.time()
agent_output = ""
input_tokens = 0
output_tokens = 0
resultado = "Reprovado"
try:
result = await run_agent(query, code, file_content)
agent_output = result["response"]
input_tokens = result["input_tokens"]
output_tokens = result["output_tokens"]
except Exception as e:
agent_output = f"ERROR: {str(e)}"
try:
clean = agent_output.strip()
if clean.startswith("```"):
clean = clean.split("```", 2)[1]
if clean.startswith("json"):
clean = clean[4:]
parsed = json.loads(clean.strip())
resultado = parsed.get("status", "Reprovado")
except Exception:
pass
return {
"codigoServico": codigo_servico_raw,
"resultado": resultado,
"agentOutput": agent_output,
"input_tokens": input_tokens,
"output_tokens": output_tokens,
"tempoAgentSegundos": round(time.time() - t0, 2),
}