Spaces:
Paused
Paused
File size: 2,602 Bytes
5eb4857 b7980f7 5eb4857 e58f87e 5eb4857 b7980f7 | 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 | from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, desc
from typing import List, Any
from uuid import UUID
from core.deps import get_db, get_current_user, get_arq_pool
from core.permissions import require_roles, WORKSPACE_ADMIN_ROLES, ALL_SC_ROLES
from models.user import User, UserRole
from models.ai_interaction import AIInteraction
from models.ticket import Ticket
from models.kb_article import KBArticle
from models.message import Message
from schemas.ai import AITraceResponse, AINodeInteraction, SummaryResponse, TicketSummary
from core.llm import get_llm
from langchain.schema import HumanMessage, SystemMessage
router = APIRouter()
@router.get("/trace/{ticket_id}/", response_model=AITraceResponse, dependencies=[Depends(require_roles(ALL_SC_ROLES))])
async def get_ticket_ai_trace(
ticket_id: UUID,
db: AsyncSession = Depends(get_db),
current_user: User = Depends(get_current_user)
) -> Any:
"""
Retrieve a chronological trace of all AI interactions (Triage, Remediation, Embedding)
related to a specific ticket.
"""
stmt = (
select(AIInteraction)
.where(
AIInteraction.ticket_id == ticket_id,
AIInteraction.workspace_id == current_user.workspace_id
)
.order_by(AIInteraction.created_at.asc())
)
result = await db.execute(stmt)
interactions = result.scalars().all()
if not interactions:
# Check if ticket exists to return 404 vs empty
from models.ticket import Ticket
t_stmt = select(Ticket).where(
Ticket.id == ticket_id,
Ticket.workspace_id == current_user.workspace_id,
Ticket.deleted_at.is_(None)
)
if not (await db.execute(t_stmt)).scalar_one_or_none():
raise HTTPException(status_code=404, detail="Ticket not found in your workspace")
return {
"ticket_id": ticket_id,
"interactions": [],
"total_steps": 0,
"total_duration_ms": 0.0
}
total_duration = sum([i.duration_ms.get("total", 0) for i in interactions if i.duration_ms])
return {
"ticket_id": ticket_id,
"thread_id": interactions[0].thread_id if interactions else None,
"interactions": interactions,
"total_steps": len(interactions),
"total_duration_ms": round(total_duration, 2)
}
# Manual summarization endpoints removed in V2.1.
# Summarization is now integrated directly into Ticket/KB lifecycles.
|