Spaces:
Paused
Paused
Download api/v1/ai.py from TeamError404/nexus: direct link, hf CLI and curl.
- Browser
- Download file 2.6 kB
-
https://huggingface.co/spaces/TeamError404/nexus/resolve/main/api/v1/ai.py
- Command line
-
hf download hf://spaces/TeamError404/nexus/api/v1/ai.py
-
curl -L -o ai.py https://huggingface.co/spaces/TeamError404/nexus/resolve/main/api/v1/ai.py
2.6 kB
| 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() | |
| 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. | |