From 3cbd1a4628865ac4adac27a266a1515696b3fe47 Mon Sep 17 00:00:00 2001 From: chihlasm Date: Mon, 16 Mar 2026 01:05:16 -0400 Subject: [PATCH] feat: inject PSA ticket context into copilot system prompt (Task 10) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a copilot conversation has an associated session with a linked PSA ticket, fetch the ticket context and append it to the system prompt. Failure is non-critical — errors are logged and the copilot proceeds without context rather than failing the request. Co-Authored-By: Claude Opus 4.6 (1M context) --- backend/app/services/copilot_service.py | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/backend/app/services/copilot_service.py b/backend/app/services/copilot_service.py index 4fc77a5c..175735ee 100644 --- a/backend/app/services/copilot_service.py +++ b/backend/app/services/copilot_service.py @@ -180,6 +180,40 @@ async def send_message( system_prompt += _build_flow_context(tree, conversation.current_node_id) system_prompt += build_rag_context(rag_results) + # Inject PSA ticket context if session has a linked ticket + if conversation.session_id: + try: + from app.models.session import Session as SessionModel + session_result = await db.execute( + select(SessionModel).where(SessionModel.id == conversation.session_id) + ) + session = session_result.scalar_one_or_none() + if session and session.psa_ticket_id: + try: + from app.services.psa.registry import get_provider_for_account + from app.services.psa.ticket_context import format_ticket_context_for_prompt + + provider = await get_provider_for_account(conversation.account_id, db) + connection_id = str(session.psa_connection_id) if session.psa_connection_id else None + ticket_ctx = await provider.get_ticket_context( + ticket_id=int(session.psa_ticket_id), + connection_id=connection_id, + ) + system_prompt += "\n\n" + format_ticket_context_for_prompt(ticket_ctx) + except Exception as psa_err: + logger.warning( + "Failed to fetch PSA ticket context for copilot (session=%s, ticket=%s): %s", + conversation.session_id, + session.psa_ticket_id, + psa_err, + ) + except Exception as session_err: + logger.warning( + "Failed to look up session for copilot PSA context (session_id=%s): %s", + conversation.session_id, + session_err, + ) + # Build messages for AI ai_messages = [] for msg in conversation.messages: