File size: 4,075 Bytes
d8c6e01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Copyright (c) 2026 CrewHub. All rights reserved.
# Proprietary and confidential. See LICENSE for details.
"""Webhook logs API — agent owners can view communication logs for their agents."""

from uuid import UUID

from fastapi import APIRouter, Depends, Query
from sqlalchemy import desc, func, select
from sqlalchemy.ext.asyncio import AsyncSession

from src.core.auth import resolve_db_user_id
from src.core.exceptions import ForbiddenError, NotFoundError
from src.database import get_db
from src.models.agent import Agent
from src.models.webhook_log import WebhookLog

router = APIRouter(prefix="/agents/{agent_id}/webhook-logs", tags=["webhook-logs"])


async def _verify_agent_owner(
    agent_id: UUID, owner_id: UUID, db: AsyncSession
) -> Agent:
    """Verify the requesting user owns the agent."""
    result = await db.execute(select(Agent).where(Agent.id == agent_id))
    agent = result.scalars().first()
    if not agent:
        raise NotFoundError(detail=f"Agent {agent_id} not found")
    if agent.owner_id != owner_id:
        raise ForbiddenError(detail="You do not own this agent")
    return agent


@router.get("/")
async def list_webhook_logs(
    agent_id: UUID,
    page: int = Query(1, ge=1),
    per_page: int = Query(20, ge=1, le=100),
    direction: str | None = Query(None, pattern="^(inbound|outbound)$"),
    method: str | None = Query(None),
    success: bool | None = Query(None),
    db: AsyncSession = Depends(get_db),
    owner_id: UUID = Depends(resolve_db_user_id),
) -> dict:
    """List webhook logs for an agent (owner only)."""
    await _verify_agent_owner(agent_id, owner_id, db)

    stmt = select(WebhookLog).where(WebhookLog.agent_id == agent_id)

    if direction:
        stmt = stmt.where(WebhookLog.direction == direction)
    if method:
        stmt = stmt.where(WebhookLog.method == method)
    if success is not None:
        stmt = stmt.where(WebhookLog.success == success)

    count_stmt = select(func.count()).select_from(stmt.subquery())
    total = (await db.execute(count_stmt)).scalar_one()

    offset = (page - 1) * per_page
    stmt = stmt.order_by(desc(WebhookLog.created_at)).offset(offset).limit(per_page)
    result = await db.execute(stmt)
    logs = result.scalars().all()

    return {
        "logs": [
            {
                "id": str(log.id),
                "agent_id": str(log.agent_id),
                "task_id": str(log.task_id) if log.task_id else None,
                "direction": log.direction,
                "method": log.method,
                "status_code": log.status_code,
                "success": log.success,
                "error_message": log.error_message,
                "latency_ms": log.latency_ms,
                "created_at": log.created_at.isoformat() if log.created_at else None,
            }
            for log in logs
        ],
        "total": total,
        "page": page,
        "per_page": per_page,
    }


@router.get("/{log_id}")
async def get_webhook_log(
    agent_id: UUID,
    log_id: UUID,
    db: AsyncSession = Depends(get_db),
    owner_id: UUID = Depends(resolve_db_user_id),
) -> dict:
    """Get a single webhook log with full request/response (owner only)."""
    await _verify_agent_owner(agent_id, owner_id, db)

    result = await db.execute(
        select(WebhookLog).where(
            WebhookLog.id == log_id,
            WebhookLog.agent_id == agent_id,
        )
    )
    log = result.scalars().first()
    if not log:
        raise NotFoundError(detail=f"Webhook log {log_id} not found")

    return {
        "id": str(log.id),
        "agent_id": str(log.agent_id),
        "task_id": str(log.task_id) if log.task_id else None,
        "direction": log.direction,
        "method": log.method,
        "request_body": log.request_body,
        "response_body": log.response_body,
        "status_code": log.status_code,
        "success": log.success,
        "error_message": log.error_message,
        "latency_ms": log.latency_ms,
        "created_at": log.created_at.isoformat() if log.created_at else None,
    }