Spaces:
Paused
Paused
dushyantsingh0 Claude Sonnet 4.6 commited on
Commit ·
76bd96a
1
Parent(s): 1181145
fix: resolve creator/assignee names and expose all ticket fields in tools
Browse filesget_ticket_details_tool:
- JOIN User table 3x (creator, assignee, resolver) — now answers 'who created/assigned this'
- JOIN User on messages — shows sender names instead of raw UUIDs
- JOIN User on audit events — shows actor names in activity history
- Include: created_at, sla_deadline, expected_resolution_at, estimated_effort_hours,
category, is_escalated, resolved_by — all fields an agent needs to answer basic questions
- Staff sees creator email too; customers see only name
search_tickets_tool:
- JOIN assignee + creator — shows names in list results
- Include created_at, sla_deadline, category per ticket row
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- agents/tools/nexus_tools.py +121 -54
agents/tools/nexus_tools.py
CHANGED
|
@@ -435,27 +435,48 @@ async def search_tickets_tool(workspace_id: UUID, query: Optional[str] = None, s
|
|
| 435 |
stmt = stmt.where(Ticket.status.in_(target_statuses))
|
| 436 |
|
| 437 |
stmt = stmt.limit(limit)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 438 |
result = await db.execute(stmt)
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
if not
|
| 442 |
return "No tickets found matching your search."
|
| 443 |
|
|
|
|
|
|
|
| 444 |
res = []
|
| 445 |
-
for
|
| 446 |
-
|
| 447 |
-
# Safe extraction without SA internal state
|
| 448 |
data = {c.name: getattr(t, c.name) for c in t.__table__.columns}
|
| 449 |
t_data = mask_for_role(data, role)
|
| 450 |
meta = t_data.get("ai_metadata", {})
|
| 451 |
-
summary = meta.get("summaries", {}).get("customer_summary", "
|
| 452 |
-
|
| 453 |
-
priority_label = t.priority.value
|
| 454 |
short_ref = str(t.id)[:8].upper()
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
|
| 460 |
final_text = "\n".join(res)
|
| 461 |
|
|
@@ -474,73 +495,119 @@ async def search_tickets_tool(workspace_id: UUID, query: Optional[str] = None, s
|
|
| 474 |
|
| 475 |
async def get_ticket_details_tool(ticket_id: str, workspace_id: UUID, role: str = "customer_user") -> str:
|
| 476 |
"""
|
| 477 |
-
Retrieve full context,
|
| 478 |
"""
|
| 479 |
async with async_session_factory() as db:
|
| 480 |
res_obj = await resolve_ticket_id(db, ticket_id, workspace_id, role)
|
| 481 |
-
if not res_obj:
|
|
|
|
| 482 |
t_id, actual_ws_id = res_obj
|
| 483 |
|
| 484 |
-
# 1. Fetch
|
| 485 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 486 |
res = await db.execute(stmt)
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 493 |
msg_res = await db.execute(msg_stmt)
|
| 494 |
-
|
| 495 |
-
for m in msg_res.scalars().all():
|
| 496 |
-
m_dict = {c.name: getattr(m, c.name) for c in m.__table__.columns}
|
| 497 |
-
messages.append(m_dict)
|
| 498 |
|
| 499 |
-
# 3. Fetch
|
| 500 |
audit_stmt = (
|
| 501 |
-
select(AuditLog)
|
|
|
|
| 502 |
.where(AuditLog.entity_type == "ticket", AuditLog.entity_id == t_id)
|
| 503 |
.order_by(AuditLog.created_at.asc())
|
| 504 |
-
.limit(
|
| 505 |
)
|
| 506 |
audit_res = await db.execute(audit_stmt)
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
t_data = {c.name: getattr(t, c.name) for c in t.__table__.columns}
|
| 510 |
-
t_data = mask_for_role(t_data, role)
|
| 511 |
|
| 512 |
-
#
|
| 513 |
if not is_staff_role(role):
|
| 514 |
-
|
| 515 |
|
| 516 |
-
|
|
|
|
|
|
|
| 517 |
|
| 518 |
-
|
| 519 |
-
|
|
|
|
|
|
|
|
|
|
| 520 |
summaries = meta.get("summaries", {})
|
| 521 |
-
|
|
|
|
|
|
|
|
|
|
| 522 |
|
| 523 |
-
|
|
|
|
|
|
|
| 524 |
|
|
|
|
| 525 |
activity_lines = []
|
| 526 |
-
for ev in
|
| 527 |
-
ts =
|
| 528 |
-
actor = f"user:{str(ev.user_id)[:8]}"
|
| 529 |
-
old_v = json.dumps(ev.old_value) if ev.old_value else "
|
| 530 |
-
new_v = json.dumps(ev.new_value) if ev.new_value else "
|
| 531 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 532 |
activity_section = "\n".join(activity_lines) if activity_lines else "No recorded activity events."
|
| 533 |
|
| 534 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
|
| 536 |
return (
|
| 537 |
-
f"### TICKET
|
| 538 |
-
f"**ID:**
|
| 539 |
-
f"**Priority:** {priority_label}\n"
|
| 540 |
-
f"**
|
| 541 |
-
f"**
|
| 542 |
-
f"**
|
| 543 |
-
f"**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 544 |
)
|
| 545 |
|
| 546 |
async def nexus_analytics_tool(workspace_id: UUID, role: str = "customer_user", user_id: Optional[UUID] = None) -> str:
|
|
|
|
| 435 |
stmt = stmt.where(Ticket.status.in_(target_statuses))
|
| 436 |
|
| 437 |
stmt = stmt.limit(limit)
|
| 438 |
+
|
| 439 |
+
# Join assignee for name resolution
|
| 440 |
+
AssigneeUser = aliased(User)
|
| 441 |
+
CreatorUser = aliased(User)
|
| 442 |
+
stmt = (
|
| 443 |
+
stmt.outerjoin(AssigneeUser, Ticket.assigned_to == AssigneeUser.id)
|
| 444 |
+
.outerjoin(CreatorUser, Ticket.created_by == CreatorUser.id)
|
| 445 |
+
.add_columns(AssigneeUser.full_name, CreatorUser.full_name)
|
| 446 |
+
)
|
| 447 |
result = await db.execute(stmt)
|
| 448 |
+
rows = result.all()
|
| 449 |
+
|
| 450 |
+
if not rows:
|
| 451 |
return "No tickets found matching your search."
|
| 452 |
|
| 453 |
+
priority_map = {"P0": "P0 · Critical", "P1": "P1 · High", "P2": "P2 · Medium", "P3": "P3 · Low"}
|
| 454 |
+
|
| 455 |
res = []
|
| 456 |
+
for row in rows:
|
| 457 |
+
t, assignee_name, creator_name = row[0], row[1], row[2]
|
|
|
|
| 458 |
data = {c.name: getattr(t, c.name) for c in t.__table__.columns}
|
| 459 |
t_data = mask_for_role(data, role)
|
| 460 |
meta = t_data.get("ai_metadata", {})
|
| 461 |
+
summary = meta.get("summaries", {}).get("customer_summary", "")
|
| 462 |
+
|
| 463 |
+
priority_label = priority_map.get(t.priority.value, t.priority.value) if t.priority else "Unknown"
|
| 464 |
short_ref = str(t.id)[:8].upper()
|
| 465 |
+
created = t.created_at.strftime("%d %b %Y") if t.created_at else "?"
|
| 466 |
+
sla = t.sla_deadline.strftime("%d %b %Y, %H:%M") if t.sla_deadline else "—"
|
| 467 |
+
assignee_str = assignee_name or "Unassigned"
|
| 468 |
+
creator_str = creator_name or "Unknown"
|
| 469 |
+
|
| 470 |
+
entry = (
|
| 471 |
+
f"**{t.title}** (REF: {short_ref})\n"
|
| 472 |
+
f"Status: {t.status.value.upper()} | Priority: {priority_label} | Category: {t.category or 'General'}\n"
|
| 473 |
+
f"Created by: {creator_str} on {created} | Assigned to: {assignee_str}\n"
|
| 474 |
+
f"SLA Deadline: {sla}"
|
| 475 |
)
|
| 476 |
+
if summary:
|
| 477 |
+
entry += f"\nSummary: {summary}"
|
| 478 |
+
entry += "\n---"
|
| 479 |
+
res.append(entry)
|
| 480 |
|
| 481 |
final_text = "\n".join(res)
|
| 482 |
|
|
|
|
| 495 |
|
| 496 |
async def get_ticket_details_tool(ticket_id: str, workspace_id: UUID, role: str = "customer_user") -> str:
|
| 497 |
"""
|
| 498 |
+
Retrieve full ticket context: metadata, people, dates, SLA, activity history, and conversation.
|
| 499 |
"""
|
| 500 |
async with async_session_factory() as db:
|
| 501 |
res_obj = await resolve_ticket_id(db, ticket_id, workspace_id, role)
|
| 502 |
+
if not res_obj:
|
| 503 |
+
return f"Error: Ticket `{ticket_id}` not found or ID is ambiguous."
|
| 504 |
t_id, actual_ws_id = res_obj
|
| 505 |
|
| 506 |
+
# 1. Fetch ticket + creator + assignee + resolver in one query
|
| 507 |
+
Creator = aliased(User)
|
| 508 |
+
Assignee = aliased(User)
|
| 509 |
+
Resolver = aliased(User)
|
| 510 |
+
stmt = (
|
| 511 |
+
select(Ticket, Creator.full_name, Creator.email, Assignee.full_name, Resolver.full_name)
|
| 512 |
+
.outerjoin(Creator, Ticket.created_by == Creator.id)
|
| 513 |
+
.outerjoin(Assignee, Ticket.assigned_to == Assignee.id)
|
| 514 |
+
.outerjoin(Resolver, Ticket.resolved_by == Resolver.id)
|
| 515 |
+
.where(Ticket.id == t_id, Ticket.workspace_id == actual_ws_id)
|
| 516 |
+
)
|
| 517 |
res = await db.execute(stmt)
|
| 518 |
+
row = res.one_or_none()
|
| 519 |
+
if not row:
|
| 520 |
+
return f"Error: Ticket `{ticket_id}` not found."
|
| 521 |
+
t, creator_name, creator_email, assignee_name, resolver_name = row
|
| 522 |
+
|
| 523 |
+
# 2. Fetch messages with sender names
|
| 524 |
+
msg_stmt = (
|
| 525 |
+
select(Message, User.full_name)
|
| 526 |
+
.outerjoin(User, Message.sender_id == User.id)
|
| 527 |
+
.where(Message.ticket_id == t_id)
|
| 528 |
+
.order_by(Message.created_at.asc())
|
| 529 |
+
.limit(25)
|
| 530 |
+
)
|
| 531 |
msg_res = await db.execute(msg_stmt)
|
| 532 |
+
raw_messages = msg_res.all()
|
|
|
|
|
|
|
|
|
|
| 533 |
|
| 534 |
+
# 3. Fetch audit / activity history with actor names
|
| 535 |
audit_stmt = (
|
| 536 |
+
select(AuditLog, User.full_name)
|
| 537 |
+
.outerjoin(User, AuditLog.user_id == User.id)
|
| 538 |
.where(AuditLog.entity_type == "ticket", AuditLog.entity_id == t_id)
|
| 539 |
.order_by(AuditLog.created_at.asc())
|
| 540 |
+
.limit(40)
|
| 541 |
)
|
| 542 |
audit_res = await db.execute(audit_stmt)
|
| 543 |
+
audit_rows = audit_res.all()
|
|
|
|
|
|
|
|
|
|
| 544 |
|
| 545 |
+
# --- Role-based filtering ---
|
| 546 |
if not is_staff_role(role):
|
| 547 |
+
raw_messages = [(m, name) for m, name in raw_messages if not m.is_internal]
|
| 548 |
|
| 549 |
+
# --- Format helpers ---
|
| 550 |
+
def fmt_dt(dt):
|
| 551 |
+
return dt.strftime("%d %b %Y, %H:%M") if dt else "—"
|
| 552 |
|
| 553 |
+
priority_map = {"P0": "P0 · Critical", "P1": "P1 · High", "P2": "P2 · Medium", "P3": "P3 · Low"}
|
| 554 |
+
priority_label = priority_map.get(t.priority.value, t.priority.value) if t.priority else "Unknown"
|
| 555 |
+
|
| 556 |
+
# --- AI Summary ---
|
| 557 |
+
meta = t.ai_metadata or {}
|
| 558 |
summaries = meta.get("summaries", {})
|
| 559 |
+
if is_staff_role(role):
|
| 560 |
+
ai_summary = summaries.get("internal_summary") or summaries.get("customer_summary") or "No summary generated yet."
|
| 561 |
+
else:
|
| 562 |
+
ai_summary = summaries.get("customer_summary") or "No summary available."
|
| 563 |
|
| 564 |
+
# --- SLA / Effort ---
|
| 565 |
+
effort_data = t.estimated_effort_hours or {}
|
| 566 |
+
effort_str = f"{effort_data.get('value', '?')}h ({effort_data.get('complexity', '?')})" if effort_data else "Not estimated"
|
| 567 |
|
| 568 |
+
# --- Activity history ---
|
| 569 |
activity_lines = []
|
| 570 |
+
for ev, actor_name in audit_rows:
|
| 571 |
+
ts = fmt_dt(ev.created_at)
|
| 572 |
+
actor = actor_name or ("System/AI" if not ev.user_id else f"user:{str(ev.user_id)[:8]}")
|
| 573 |
+
old_v = json.dumps(ev.old_value) if ev.old_value else "—"
|
| 574 |
+
new_v = json.dumps(ev.new_value) if ev.new_value else "—"
|
| 575 |
+
meta_note = ""
|
| 576 |
+
if ev.metadata_json:
|
| 577 |
+
note = ev.metadata_json.get("note") or ev.metadata_json.get("reason") or ev.metadata_json.get("label")
|
| 578 |
+
if note:
|
| 579 |
+
meta_note = f" · {note}"
|
| 580 |
+
activity_lines.append(f"• [{ts}] **{ev.action.upper()}** by {actor}{meta_note} | {old_v} → {new_v}")
|
| 581 |
activity_section = "\n".join(activity_lines) if activity_lines else "No recorded activity events."
|
| 582 |
|
| 583 |
+
# --- Conversation ---
|
| 584 |
+
convo_lines = []
|
| 585 |
+
for m, sender_name in raw_messages:
|
| 586 |
+
ts = fmt_dt(m.created_at)
|
| 587 |
+
name = sender_name or "Unknown"
|
| 588 |
+
tag = " [INTERNAL]" if m.is_internal else ""
|
| 589 |
+
convo_lines.append(f"• [{ts}] **{name}**{tag}: {m.content}")
|
| 590 |
+
convo_section = "\n".join(convo_lines) if convo_lines else "No messages yet."
|
| 591 |
+
|
| 592 |
+
# --- Assemble output ---
|
| 593 |
+
assignee_str = assignee_name or "Unassigned"
|
| 594 |
+
resolver_str = resolver_name or "—"
|
| 595 |
+
creator_display = creator_name or "Unknown"
|
| 596 |
+
if is_staff_role(role) and creator_email:
|
| 597 |
+
creator_display += f" ({creator_email})"
|
| 598 |
|
| 599 |
return (
|
| 600 |
+
f"### TICKET: {t.title}\n"
|
| 601 |
+
f"**Short ID:** {str(t.id)[:8].upper()} | **Full ID:** `{t.id}`\n"
|
| 602 |
+
f"**Status:** {t.status.value.upper()} | **Priority:** {priority_label} | **Category:** {t.category or 'General'}\n\n"
|
| 603 |
+
f"**Created by:** {creator_display} | **Created at:** {fmt_dt(t.created_at)}\n"
|
| 604 |
+
f"**Assigned to:** {assignee_str} | **Resolved by:** {resolver_str}\n"
|
| 605 |
+
f"**SLA Deadline:** {fmt_dt(t.sla_deadline)} | **Expected Resolution:** {fmt_dt(t.expected_resolution_at)}\n"
|
| 606 |
+
f"**Estimated Effort:** {effort_str} | **Escalated:** {'Yes' if t.is_escalated else 'No'}\n\n"
|
| 607 |
+
f"**Description:**\n{t.description}\n\n"
|
| 608 |
+
f"**AI Summary:**\n{ai_summary}\n\n"
|
| 609 |
+
f"**Activity History ({len(audit_rows)} events):**\n{activity_section}\n\n"
|
| 610 |
+
f"**Conversation ({len(convo_lines)} messages):**\n{convo_section}"
|
| 611 |
)
|
| 612 |
|
| 613 |
async def nexus_analytics_tool(workspace_id: UUID, role: str = "customer_user", user_id: Optional[UUID] = None) -> str:
|