|
|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
from typing import List, Dict, Any
|
|
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends
|
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
|
|
|
|
from app.api.schemas import (
|
|
|
|
|
@ -32,6 +33,22 @@ def get_db():
|
|
|
|
|
finally:
|
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _db_error_detail(exc: SQLAlchemyError) -> str:
|
|
|
|
|
text = str(exc).lower()
|
|
|
|
|
|
|
|
|
|
# Heuristica para identificar falhas no MySQL (base ficticia).
|
|
|
|
|
mysql_markers = ("mysql", "pymysql", "(2003", "mock", "3306")
|
|
|
|
|
if any(marker in text for marker in mysql_markers):
|
|
|
|
|
return "Servico temporariamente indisponivel: banco MySQL (dados ficticios) inacessivel."
|
|
|
|
|
|
|
|
|
|
# Heuristica para identificar falhas no PostgreSQL (tools).
|
|
|
|
|
pg_markers = ("postgres", "psycopg", "postgresql", "5432", "tools")
|
|
|
|
|
if any(marker in text for marker in pg_markers):
|
|
|
|
|
return "Servico temporariamente indisponivel: banco PostgreSQL (tools) inacessivel."
|
|
|
|
|
|
|
|
|
|
return "Servico temporariamente indisponivel: erro de acesso ao banco de dados."
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
# Removido momentaniamente para teste do Vertex IA
|
|
|
|
|
@router.post("/chat", response_model=ChatResponse)
|
|
|
|
|
@ -45,33 +62,48 @@ async def chat(request: ChatRequest, db: Session = Depends(get_db)):
|
|
|
|
|
'''
|
|
|
|
|
@router.post("/chat", response_model=ChatResponse)
|
|
|
|
|
async def chat(request: ChatRequest, db: Session = Depends(get_db)):
|
|
|
|
|
service = OrquestradorService(db)
|
|
|
|
|
|
|
|
|
|
result = await service.handle_message(
|
|
|
|
|
message=request.message
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return ChatResponse(response=result)
|
|
|
|
|
try:
|
|
|
|
|
service = OrquestradorService(db)
|
|
|
|
|
result = await service.handle_message(message=request.message)
|
|
|
|
|
return ChatResponse(response=result)
|
|
|
|
|
except SQLAlchemyError as exc:
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=503,
|
|
|
|
|
detail=_db_error_detail(exc),
|
|
|
|
|
)
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
# Erros de configuracao de Vertex (regiao/projeto/modelo)
|
|
|
|
|
raise HTTPException(status_code=500, detail=f"Configuracao invalida do Vertex AI: {exc}")
|
|
|
|
|
except RuntimeError as exc:
|
|
|
|
|
raise HTTPException(status_code=503, detail=f"Falha temporaria no LLM/Vertex AI: {exc}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/mock/consultar-estoque")
|
|
|
|
|
async def consultar_estoque_endpoint(
|
|
|
|
|
body: ConsultarEstoqueRequest,
|
|
|
|
|
) -> List[Dict[str, Any]]:
|
|
|
|
|
return await consultar_estoque(
|
|
|
|
|
preco_max=body.preco_max,
|
|
|
|
|
categoria=body.categoria,
|
|
|
|
|
)
|
|
|
|
|
try:
|
|
|
|
|
return await consultar_estoque(
|
|
|
|
|
preco_max=body.preco_max,
|
|
|
|
|
categoria=body.categoria,
|
|
|
|
|
ordenar_preco=body.ordenar_preco,
|
|
|
|
|
limite=body.limite,
|
|
|
|
|
)
|
|
|
|
|
except SQLAlchemyError as exc:
|
|
|
|
|
raise HTTPException(status_code=503, detail=_db_error_detail(exc))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/mock/validar-cliente-venda")
|
|
|
|
|
async def validar_cliente_venda_endpoint(
|
|
|
|
|
body: ValidarClienteVendaRequest,
|
|
|
|
|
) -> Dict[str, Any]:
|
|
|
|
|
return await validar_cliente_venda(
|
|
|
|
|
cpf=body.cpf,
|
|
|
|
|
valor_veiculo=body.valor_veiculo,
|
|
|
|
|
)
|
|
|
|
|
try:
|
|
|
|
|
return await validar_cliente_venda(
|
|
|
|
|
cpf=body.cpf,
|
|
|
|
|
valor_veiculo=body.valor_veiculo,
|
|
|
|
|
)
|
|
|
|
|
except SQLAlchemyError as exc:
|
|
|
|
|
raise HTTPException(status_code=503, detail=_db_error_detail(exc))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/mock/avaliar-veiculo-troca")
|
|
|
|
|
@ -89,17 +121,23 @@ async def avaliar_veiculo_troca_endpoint(
|
|
|
|
|
async def agendar_revisao_endpoint(
|
|
|
|
|
body: AgendarRevisaoRequest,
|
|
|
|
|
) -> Dict[str, Any]:
|
|
|
|
|
return await agendar_revisao(
|
|
|
|
|
placa=body.placa,
|
|
|
|
|
data_hora=body.data_hora,
|
|
|
|
|
)
|
|
|
|
|
try:
|
|
|
|
|
return await agendar_revisao(
|
|
|
|
|
placa=body.placa,
|
|
|
|
|
data_hora=body.data_hora,
|
|
|
|
|
)
|
|
|
|
|
except SQLAlchemyError as exc:
|
|
|
|
|
raise HTTPException(status_code=503, detail=_db_error_detail(exc))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/mock/cancelar-pedido")
|
|
|
|
|
async def cancelar_pedido_endpoint(
|
|
|
|
|
body: CancelarPedidoRequest,
|
|
|
|
|
) -> Dict[str, Any]:
|
|
|
|
|
return await cancelar_pedido(
|
|
|
|
|
numero_pedido=body.numero_pedido,
|
|
|
|
|
motivo=body.motivo,
|
|
|
|
|
)
|
|
|
|
|
try:
|
|
|
|
|
return await cancelar_pedido(
|
|
|
|
|
numero_pedido=body.numero_pedido,
|
|
|
|
|
motivo=body.motivo,
|
|
|
|
|
)
|
|
|
|
|
except SQLAlchemyError as exc:
|
|
|
|
|
raise HTTPException(status_code=503, detail=_db_error_detail(exc))
|
|
|
|
|
|