Files

27 lines
1021 B
Python
Raw Permalink Normal View History

"""Log Tools API mutations to agent_events."""
from __future__ import annotations
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
from app.middleware import log_agent_event
class AgentLoggingMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
response = await call_next(request)
if request.method in ("POST", "PUT", "PATCH", "DELETE") and response.status_code < 400:
path = request.url.path
if path.startswith(("/research", "/recommendations", "/retail", "/events")):
try:
log_agent_event(
agent_name="tools_api",
event_type="api_call",
title=f"{request.method} {path}",
channel="api",
metadata={"status": response.status_code},
)
except Exception:
pass
return response