You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.0 KiB
Python
66 lines
2.0 KiB
Python
from typing import Callable, Dict, List
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.models.tool_model import ToolDefinition
|
|
from app.repositories.tool_repository import ToolRepository
|
|
from app.services.handlers import (
|
|
agendar_revisao,
|
|
avaliar_veiculo_troca,
|
|
cancelar_pedido,
|
|
consultar_estoque,
|
|
validar_cliente_venda,
|
|
)
|
|
|
|
|
|
HANDLERS: Dict[str, Callable] = {
|
|
"consultar_estoque": consultar_estoque,
|
|
"validar_cliente_venda": validar_cliente_venda,
|
|
"avaliar_veiculo_troca": avaliar_veiculo_troca,
|
|
"agendar_revisao": agendar_revisao,
|
|
"cancelar_pedido": cancelar_pedido,
|
|
}
|
|
|
|
|
|
class ToolRegistry:
|
|
|
|
def __init__(self, db: Session):
|
|
"""Carrega tools do banco e registra apenas as que possuem handler conhecido."""
|
|
self._tools = []
|
|
repo = ToolRepository(db)
|
|
db_tools = repo.get_all()
|
|
for db_tool in db_tools:
|
|
handler = HANDLERS.get(db_tool.name)
|
|
if not handler:
|
|
continue
|
|
self.register_tool(
|
|
name=db_tool.name,
|
|
description=db_tool.description,
|
|
parameters=db_tool.parameters,
|
|
handler=handler,
|
|
)
|
|
|
|
def register_tool(self, name, description, parameters, handler):
|
|
"""Registra uma tool em memoria para uso pelo orquestrador."""
|
|
self._tools.append(
|
|
ToolDefinition(
|
|
name=name,
|
|
description=description,
|
|
parameters=parameters,
|
|
handler=handler,
|
|
)
|
|
)
|
|
|
|
def get_tools(self) -> List[ToolDefinition]:
|
|
"""Retorna a lista atual de tools registradas."""
|
|
return self._tools
|
|
|
|
async def execute(self, name: str, arguments: dict):
|
|
"""Executa a tool solicitada pelo modelo com os argumentos extraidos."""
|
|
tool = next((t for t in self._tools if t.name == name), None)
|
|
|
|
if not tool:
|
|
raise Exception(f"Tool {name} nao encontrada.")
|
|
|
|
return await tool.handler(**arguments)
|