Spaces:
Paused
Paused
dushyantsingh0 commited on
Commit ·
b04413b
1
Parent(s): 9dfdacd
backend stabilisation
Browse files- api/v1/ai.py +9 -6
- tasks/ai.py +16 -0
api/v1/ai.py
CHANGED
|
@@ -24,13 +24,14 @@ async def get_ticket_ai_trace(
|
|
| 24 |
"""
|
| 25 |
stmt = (
|
| 26 |
select(AIInteraction)
|
| 27 |
-
.where(
|
| 28 |
-
AIInteraction.ticket_id == ticket_id,
|
| 29 |
-
AIInteraction.workspace_id == current_user.workspace_id
|
| 30 |
-
)
|
| 31 |
-
.order_by(AIInteraction.created_at.asc())
|
| 32 |
)
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
result = await db.execute(stmt)
|
| 35 |
interactions = result.scalars().all()
|
| 36 |
|
|
@@ -39,9 +40,11 @@ async def get_ticket_ai_trace(
|
|
| 39 |
from models.ticket import Ticket
|
| 40 |
t_stmt = select(Ticket).where(
|
| 41 |
Ticket.id == ticket_id,
|
| 42 |
-
Ticket.workspace_id == current_user.workspace_id,
|
| 43 |
Ticket.deleted_at.is_(None)
|
| 44 |
)
|
|
|
|
|
|
|
|
|
|
| 45 |
if not (await db.execute(t_stmt)).scalar_one_or_none():
|
| 46 |
raise HTTPException(status_code=404, detail="Ticket not found in your workspace")
|
| 47 |
|
|
|
|
| 24 |
"""
|
| 25 |
stmt = (
|
| 26 |
select(AIInteraction)
|
| 27 |
+
.where(AIInteraction.ticket_id == ticket_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
+
if not current_user.is_platform_admin:
|
| 31 |
+
stmt = stmt.where(AIInteraction.workspace_id == current_user.workspace_id)
|
| 32 |
+
|
| 33 |
+
stmt = stmt.order_by(AIInteraction.created_at.asc())
|
| 34 |
+
|
| 35 |
result = await db.execute(stmt)
|
| 36 |
interactions = result.scalars().all()
|
| 37 |
|
|
|
|
| 40 |
from models.ticket import Ticket
|
| 41 |
t_stmt = select(Ticket).where(
|
| 42 |
Ticket.id == ticket_id,
|
|
|
|
| 43 |
Ticket.deleted_at.is_(None)
|
| 44 |
)
|
| 45 |
+
if not current_user.is_platform_admin:
|
| 46 |
+
t_stmt = t_stmt.where(Ticket.workspace_id == current_user.workspace_id)
|
| 47 |
+
|
| 48 |
if not (await db.execute(t_stmt)).scalar_one_or_none():
|
| 49 |
raise HTTPException(status_code=404, detail="Ticket not found in your workspace")
|
| 50 |
|
tasks/ai.py
CHANGED
|
@@ -86,6 +86,22 @@ async def auto_summarize_ticket(ctx, ticket_id: UUID, **kwargs):
|
|
| 86 |
|
| 87 |
meta = dict(ticket.ai_metadata or {})
|
| 88 |
meta["summaries"] = summary_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
ticket.ai_metadata = meta
|
| 90 |
db.add(ticket)
|
| 91 |
await db.commit()
|
|
|
|
| 86 |
|
| 87 |
meta = dict(ticket.ai_metadata or {})
|
| 88 |
meta["summaries"] = summary_data
|
| 89 |
+
|
| 90 |
+
# --- FORENSIC TRAIL BACKFILL (ISSUE-7 FIX) ---
|
| 91 |
+
# On manual regeneration, we attempt to backfill the processed_steps
|
| 92 |
+
# from the raw ai_interactions table so they persist in ai_metadata.
|
| 93 |
+
from models.ai_interaction import AIInteraction
|
| 94 |
+
trace_stmt = select(AIInteraction).where(AIInteraction.ticket_id == ticket_id).order_by(AIInteraction.created_at.asc())
|
| 95 |
+
traces = (await db.execute(trace_stmt)).scalars().all()
|
| 96 |
+
if traces:
|
| 97 |
+
meta["processed_steps"] = [
|
| 98 |
+
{
|
| 99 |
+
"name": t.agent_name.replace("_node", "").upper(),
|
| 100 |
+
"status": t.status,
|
| 101 |
+
"timestamp": t.created_at.isoformat()
|
| 102 |
+
} for t in traces
|
| 103 |
+
]
|
| 104 |
+
|
| 105 |
ticket.ai_metadata = meta
|
| 106 |
db.add(ticket)
|
| 107 |
await db.commit()
|