This commit is contained in:
2026-06-13 09:45:18 -03:00
parent 89c23fb0ed
commit 471c0e21a9
18 changed files with 837 additions and 91 deletions

View File

@@ -1,11 +1,10 @@
import time
import logging
import json
import re
from src.agent.state.agent_state import AgentState, update_state_metadata
from src.agent.state.steps import GraphStep
from src.agent.state.step_helpers import set_current_step
from src.components.clients.speech_analytics_client import SpeechAnalyticsClient
from src.components.clients.exceptions.speech_exceptions import SpeechClientError
from src.utils.observer import trace_node, score_current_trace
from src.utils.ics_collector import build_ic_payload, build_noc_api_metadata, build_noc_llm_metadata
from src.compat.framework_observer import event
@@ -13,6 +12,7 @@ from src.providers.llm_provider import classification_llm, chat_llm_with_usage,
from src.core.config import settings
from src.core.prompt_manager import get_prompt
from src.agent.local_prompts.speech_history_analysis import speech_history_similarity_pt
from src.compat.framework_services import call_mcp_tool
logger = logging.getLogger(__name__)
@@ -42,9 +42,7 @@ _NULL_SPEECH_DATA = {
def _map_prediction_response(response: dict) -> dict:
"""
Map the raw Prediction API response to the internal speech_analytics schema.
"""
"""Map the raw Prediction API response to the internal speech_analytics schema."""
variables = response.get("variables", {})
return {
"reclamacao_resumo": response.get("resume") or variables.get("resume"),
@@ -57,18 +55,8 @@ def _map_prediction_response(response: dict) -> dict:
}
def _build_related_history(
raw_history: list,
current_complaint_id: str | None,
llm_scores: list,
threshold: int,
) -> list:
"""
Merge LLM similarity scores with raw history items and filter by threshold.
Each returned item has: protocolo, data_reclamacao, similaridade_pct,
reasoning, plus the 7 detail keys (same as reclamacao_atual).
"""
def _build_related_history(raw_history: list, current_complaint_id: str | None, llm_scores: list, threshold: int) -> list:
"""Merge LLM similarity scores with raw history items and filter by threshold."""
if not raw_history or not llm_scores:
return []
@@ -105,18 +93,13 @@ def _build_related_history(
def _build_analise_agente(related_items: list) -> str:
"""
Build the analise_agente string from related items: one line per item with
'Protocolo X (Y%): <reasoning>'. Empty list -> negative spec message.
"""
"""Build the analise_agente text from related items."""
if not related_items:
return _NO_SIMILAR_MESSAGE
lines = []
for item in related_items:
reasoning = (item.get("reasoning") or "").strip()
if not reasoning:
reasoning = ""
reasoning = (item.get("reasoning") or "").strip() or ""
lines.append(f"Protocolo {item.get('protocolo')} ({item.get('similaridade_pct')}%): {reasoning}")
return "\n".join(lines)
@@ -128,20 +111,31 @@ async def _score_history_with_llm(
session_id: str = "",
state: dict | None = None,
) -> list:
"""
Calls the LLM to score similarity between the current complaint description
and each historical item. Returns the LLM-parsed JSON list. Items below
SPEECH_SIMILARITY_THRESHOLD will be dropped later by _build_related_history.
"""
"""Score similarity between current complaint and historical complaints using the LLM."""
if not current_complaint_description or not history_list:
event("AGA.015", {
"status": "Houve acionamento do Agente Especializado (LLM): não — sem resumo atual ou histórico vazio",
"type": "INFO",
"session_id": session_id,
"tag": "AGA.015",
"call_id": session_id,
"origin": "LLM",
**build_ic_payload({"session_id": session_id}, "AGA.015"),
})
return []
clean_history = [
item for item in history_list
if str(item.get("protocolo")) != str(current_id or "")
]
clean_history = [item for item in history_list if str(item.get("protocolo")) != str(current_id or "")]
if not clean_history:
logger.info("No historical items left after deduplication.")
event("AGA.015", {
"status": "Houve acionamento do Agente Especializado (LLM): não — histórico vazio após deduplicação",
"type": "INFO",
"session_id": session_id,
"tag": "AGA.015",
"call_id": session_id,
"origin": "LLM",
**build_ic_payload({"session_id": session_id}, "AGA.015"),
})
return []
llm = classification_llm
@@ -163,6 +157,305 @@ async def _score_history_with_llm(
history_json=json.dumps(history_payload, ensure_ascii=False),
)
logger.info(f"Calling LLM for history similarity filtering. Items: {len(clean_history)}")
logger.info("Calling LLM for history similarity filtering. Items: %s", len(clean_history))
event("AGA.014", {
"status": "Houve acionamento do Agente Especializado (LLM): sim — filtragem de histórico Speech",
"type": "INFO",
"session_id": session_id,
"tag": "AGA.014",
"call_id": session_id,
"origin": "LLM",
**build_ic_payload({"session_id": session_id}, "AGA.014"),
})
# LLM generation telemetry is recorded by agent_framework.llm.providers.
try:
_t0 = time.perf_counter()
llm_resp = chat_llm_with_usage(llm, message)
content = llm_resp.content
json_match = re.search(r"```(?:json)?\s*(\[.*?\])\s*```", content, re.DOTALL)
if json_match:
content = json_match.group(1)
else:
json_match = re.search(r"(\[.*?\])", content, re.DOTALL)
if json_match:
content = json_match.group(1)
scores = json.loads(content)
if not isinstance(scores, list):
raise ValueError(f"LLM returned non-list: {type(scores).__name__}")
score_current_trace(name="speech_history_similarity_valid", value=1.0)
return scores
except Exception as e:
logger.error("Error during LLM history scoring: %s", e, exc_info=True)
score_current_trace(name="speech_history_similarity_valid", value=0.0, comment=str(e))
if state is not None:
event("NOC.004", {
"status": f"LLM error during history similarity analysis: {e}",
"type": "FAILURE",
**build_noc_llm_metadata(
state,
"NOC.004",
latency_ms=int((time.perf_counter() - _t0) * 1000) if "_t0" in locals() else 0,
llm_endpoint=LLM_ENDPOINT,
model_name=str(getattr(llm, "eligibleModel_name", "unknown")),
),
}, metadata={"noc": True})
return []
@trace_node
async def enrich_with_speech(state: AgentState) -> AgentState:
"""Enrich the ticket with Speech Analytics NLP insights and historical similarity."""
logger.info("Running speech enrichment node.")
await set_current_step(state, GraphStep.SPEECH_ENRICHMENT)
session_id = state.get("session_id", "")
context = state.get("metadata", {}).get("request_context", {})
if "speech_analytics" in context:
logger.info("Speech analytics data already exists in context (cached). Skipping API call.")
return state
complaint = context.get("complaint", {})
reclamacao_id: str | None = complaint.get("complaintProtocol")
raw_text: str | None = complaint.get("description")
customer = context.get("customer", {})
cpf_cnpj: str | None = customer.get("cpfCnpj")
msisdn: str | None = customer.get("msisdn")
# Framework-native path: call Speech through MCP/fallback instead of direct legacy clients.
mcp_response = await call_mcp_tool(
"consultar_speech_analytics",
{
"protocol_id": reclamacao_id,
"customer_key": cpf_cnpj or msisdn,
"interaction_key": reclamacao_id,
"document": cpf_cnpj,
},
business_context={"customer_key": cpf_cnpj or msisdn, "interaction_key": reclamacao_id},
original_context=context,
)
mcp_result = mcp_response.get("result") if isinstance(mcp_response.get("result"), dict) else mcp_response
if mcp_response.get("ok") or str(mcp_result.get("source", "")).startswith("mock_"):
summary = mcp_result.get("summary") or (raw_text or "Reclamação recebida para análise.")
speech_data = {
"reclamacao_resumo": summary,
"causa_raiz": mcp_result.get("root_cause") or "Cobrança/contato indevido",
"descortesia_cliente": mcp_result.get("rude_customer") or "False",
"motivo_reclamacao": mcp_result.get("reason") or complaint.get("motive"),
"submotivo_reclamacao": mcp_result.get("subreason") or complaint.get("modality"),
"sentimento_cliente": mcp_result.get("sentiment") or "Negativo",
"solucao_proposta_cliente": mcp_result.get("proposed_solution") or "Analisar cobrança e cessar contatos indevidos",
"historico_bruto": mcp_result.get("events") or [],
"historico_relacionado": mcp_result.get("related_history") or [],
"analise_agente": mcp_result.get("agent_analysis") or (mcp_result.get("message") or _NO_SIMILAR_MESSAGE),
"source": mcp_result.get("source", "framework_mcp"),
"mcp_tool": "consultar_speech_analytics",
}
context = dict(context)
context["speech_analytics"] = speech_data
state = update_state_metadata(state, request_context=context)
event("AGA.034", {
"status": "Resultado Speech: sucesso via MCP/framework",
"type": "SUCCESS",
"session_id": session_id,
"tag": "AGA.034",
"call_id": session_id,
"origin": "MCP",
**build_ic_payload(state, "AGA.034"),
}, metadata={"noc": True})
return state
if not getattr(settings, "BACKOFFICE_ALLOW_LEGACY_CLIENTS", False):
speech_data = dict(_NULL_SPEECH_DATA)
speech_data.update({
"reclamacao_resumo": raw_text or "Indisponível SPEECH para Reclamação Atual - realizar consulta manualmente",
"historico_bruto": [],
"historico_relacionado": [],
"analise_agente": _HISTORY_UNAVAILABLE_MESSAGE,
"source": "framework_local_fallback",
})
context = dict(context)
context["speech_analytics"] = speech_data
state = update_state_metadata(state, request_context=context)
await set_current_step(state, GraphStep.SPEECH_ENRICHMENT_UNAVAILABLE)
return state
from src.components.clients.speech_analytics_client import SpeechAnalyticsClient
from src.components.clients.exceptions.speech_exceptions import SpeechClientError
speech_data: dict = dict(_NULL_SPEECH_DATA)
history_unavailable = False
if not reclamacao_id or not raw_text:
logger.warning(
"Speech enrichment skipped prediction: missing complaintProtocol or description. "
"reclamacao_id=%s, raw_text_present=%s",
bool(reclamacao_id), bool(raw_text)
)
event("AGA.010", {
"status": "Resultado consulta Speech: ignorada — complaintProtocol ou description ausente",
"type": "INFO",
"session_id": session_id,
"tag": "AGA.010",
"call_id": session_id,
"origin": "AGENT",
**build_ic_payload(state, "AGA.010"),
}, metadata={"noc": True})
else:
try:
client = SpeechAnalyticsClient()
response, http_meta = await client.get_prediction(
reclamacao_id=reclamacao_id,
raw_text=raw_text,
customer_segment="",
)
speech_data = _map_prediction_response(response)
if not speech_data.get("reclamacao_resumo"):
speech_data["reclamacao_resumo"] = "Reclamação Atual não foi encontrada no SPEECH"
event("NOC.002", {
"status": "Invalid API response",
"type": "WARNING",
**build_noc_api_metadata(
state,
"NOC.002",
retry_count=http_meta.get("retry_count", 0),
latency_ms=http_meta["latency_ms"],
api_url=http_meta["url"],
status_code=http_meta["status_code"],
),
}, metadata={"noc": True})
logger.info("Speech Analytics prediction retrieved successfully. reclamacao_id=%s", reclamacao_id)
event("AGA.010", {
"status": "Resultado consulta Speech: sucesso",
"type": "SUCCESS",
"session_id": session_id,
"tag": "AGA.010",
"call_id": session_id,
"origin": "AGENT",
**build_ic_payload(state, "AGA.010", {
"apiUrl": http_meta["url"],
"apiStatusCode": http_meta["status_code"],
"apiResponsePayload": http_meta["response_text"],
"latencyMs": http_meta["latency_ms"],
}),
}, metadata={"noc": True})
except SpeechClientError as client_exc:
logger.warning("Speech Analytics unavailable — continuing without prediction. Error: %s", client_exc)
speech_data["reclamacao_resumo"] = "Indisponível SPEECH para Reclamação Atual - realizar consulta manualmente"
await set_current_step(state, GraphStep.SPEECH_ENRICHMENT_UNAVAILABLE)
api_fields = {
"apiUrl": getattr(client_exc, "url", None) or "N/A",
"apiStatusCode": getattr(client_exc, "status_code", None) if getattr(client_exc, "status_code", None) is not None else "N/A",
"apiResponsePayload": getattr(client_exc, "response_text", None) if getattr(client_exc, "response_text", None) is not None else "N/A",
"latencyMs": getattr(client_exc, "latency_ms", None) if getattr(client_exc, "latency_ms", None) is not None else "N/A",
}
event("AGA.035", {
"status": f"Erro na consulta Speech (Reclamação Atual): SpeechClientError: {str(client_exc)}",
"type": "FAILURE",
"session_id": session_id,
"tag": "AGA.035",
"call_id": session_id,
"origin": "AGENT",
**build_ic_payload(state, "AGA.035", api_fields),
}, metadata={"noc": True})
historico: list = []
if not cpf_cnpj:
logger.warning("Speech enrichment skipped history fetch: cpfCnpj absent.", extra={"session_id": session_id})
event("AGA.034", {
"status": "Resultado consulta histórico Speech: ignorada — cpfCnpj ausente",
"type": "INFO",
"session_id": session_id,
"tag": "AGA.034",
"call_id": session_id,
"origin": "AGENT",
**build_ic_payload(state, "AGA.034"),
}, metadata={"noc": True})
else:
try:
client = SpeechAnalyticsClient()
historico, http_meta = await client.get_history(cpf_cnpj, msisdn)
logger.info("Speech Analytics history retrieved successfully. %s items.", len(historico), extra={"session_id": session_id})
event("AGA.034", {
"status": f"Resultado consulta histórico Speech: sucesso — {len(historico)} item(ns)",
"type": "SUCCESS",
"session_id": session_id,
"tag": "AGA.034",
"call_id": session_id,
"origin": "AGENT",
**build_ic_payload(state, "AGA.034", {
"apiUrl": http_meta["url"],
"apiStatusCode": http_meta["status_code"],
"apiResponsePayload": http_meta["response_text"],
"latencyMs": http_meta["latency_ms"],
}),
}, metadata={"noc": True})
if not historico:
event("NOC.002", {
"status": "Invalid API response",
"type": "WARNING",
**build_noc_api_metadata(
state,
"NOC.002",
retry_count=http_meta.get("retry_count", 0),
latency_ms=http_meta["latency_ms"],
api_url=http_meta["url"],
status_code=http_meta["status_code"],
),
}, metadata={"noc": True})
except Exception as exc:
logger.warning("Failed to fetch speech history: %s", exc)
await set_current_step(state, GraphStep.SPEECH_ENRICHMENT_UNAVAILABLE)
api_fields = {}
if isinstance(exc, SpeechClientError):
api_fields = {
"apiUrl": getattr(exc, "url", None) or "N/A",
"apiStatusCode": getattr(exc, "status_code", None) if getattr(exc, "status_code", None) is not None else "N/A",
"apiResponsePayload": getattr(exc, "response_text", None) if getattr(exc, "response_text", None) is not None else "N/A",
"latencyMs": getattr(exc, "latency_ms", None) if getattr(exc, "latency_ms", None) is not None else "N/A",
}
event("AGA.035", {
"status": f"Falha ao buscar histórico Speech: {str(exc)}",
"type": "FAILURE",
"session_id": session_id,
"tag": "AGA.035",
"call_id": session_id,
"origin": "AGENT",
**build_ic_payload(state, "AGA.035", api_fields),
}, metadata={"noc": True})
history_unavailable = True
historico_relacionado: list = []
if historico and raw_text:
scores = await _score_history_with_llm(
current_id=reclamacao_id,
current_complaint_description=raw_text,
history_list=historico,
session_id=session_id,
state=state,
)
historico_relacionado = _build_related_history(
raw_history=historico,
current_complaint_id=reclamacao_id,
llm_scores=scores,
threshold=settings.SPEECH_SIMILARITY_THRESHOLD,
)
def _strip_to_detail_keys(h_list: list) -> list:
return [{k: item.get(k) for k in _HISTORY_DETAIL_KEYS} for item in h_list]
speech_data["historico_bruto"] = _strip_to_detail_keys(historico)
speech_data["historico_relacionado"] = historico_relacionado
speech_data["analise_agente"] = _HISTORY_UNAVAILABLE_MESSAGE if history_unavailable else _build_analise_agente(historico_relacionado)
context = dict(context)
context["speech_analytics"] = speech_data
state = update_state_metadata(state, request_context=context)
logger.info("Speech enrichment node completed successfully. State metadata and current step updated: %s", state.get("current_step"))
return state