- Reformat PSA resolution/escalation notes: clean single-line header, steps with engineer responses inline, remove duplicate timing blocks, remove AI confidence section, add follow-up recommendations - Standardize time display to decimal hours (e.g. 0.25 hrs) across all note formatters and status update context - Add follow_up_recommendations to SessionDocumentation schema and surface in SessionDocView; extracted from resolution suggestion steps - Add _build_what_we_know() helper: uses session.evidence_items when cockpit branch merges, falls back to deriving findings from steps - Fix option label lookup in generate_status_update (was passing raw machine values to AI instead of human-readable labels) - Add 'What We Know' section to status update ticket notes prompt - Improve _build_session_context in resolution_output_generator to include intake text and full step details instead of truncated chat - Add request_info audience type: client-facing information request that skips the length step and generates a numbered question list - Improve client_update and email_draft prompts with per-context guidance (status/resolution/escalation) and fix escalation subject line from 'Specialist Review' to 'Specialist Assistance' Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
193 lines
7.3 KiB
Python
193 lines
7.3 KiB
Python
"""Resolution output generator — three deliverables on session resolve."""
|
|
import logging
|
|
from typing import Any
|
|
from uuid import UUID
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.models.ai_session import AISession
|
|
from app.models.session_resolution_output import SessionResolutionOutput
|
|
from app.services.assistant_chat_service import _call_ai
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
RESOLUTION_MODEL = "claude-sonnet-4-6"
|
|
|
|
|
|
class ResolutionOutputGenerator:
|
|
def __init__(self, db: AsyncSession):
|
|
self.db = db
|
|
|
|
async def generate_all(
|
|
self,
|
|
session_id: UUID,
|
|
root_cause: str | None = None,
|
|
steps_taken: list[str] | None = None,
|
|
recommendations: str | None = None,
|
|
) -> list[SessionResolutionOutput]:
|
|
result = await self.db.execute(
|
|
select(AISession).where(AISession.id == session_id)
|
|
)
|
|
session = result.scalar_one_or_none()
|
|
if not session:
|
|
raise ValueError(f"Session {session_id} not found")
|
|
|
|
context = self._build_session_context(
|
|
session,
|
|
root_cause=root_cause,
|
|
steps_taken=steps_taken,
|
|
recommendations=recommendations,
|
|
)
|
|
|
|
outputs = []
|
|
for output_type, prompt in [
|
|
("psa_ticket_notes", self._psa_notes_prompt(context)),
|
|
("knowledge_base", self._kb_article_prompt(context)),
|
|
("client_summary", self._client_summary_prompt(context)),
|
|
]:
|
|
content, _, _ = await _call_ai(
|
|
system_base="You are a technical documentation assistant for MSP teams.",
|
|
rag_context="",
|
|
history=[],
|
|
new_message=prompt,
|
|
max_tokens=2048,
|
|
)
|
|
|
|
output = SessionResolutionOutput(
|
|
session_id=session_id,
|
|
output_type=output_type,
|
|
generated_content=content,
|
|
status="draft",
|
|
generated_by_model=RESOLUTION_MODEL,
|
|
)
|
|
self.db.add(output)
|
|
outputs.append(output)
|
|
|
|
await self.db.flush()
|
|
return outputs
|
|
|
|
async def edit_output(self, output_id: UUID, edited_content: str) -> SessionResolutionOutput:
|
|
result = await self.db.execute(
|
|
select(SessionResolutionOutput).where(SessionResolutionOutput.id == output_id)
|
|
)
|
|
output = result.scalar_one_or_none()
|
|
if not output:
|
|
raise ValueError(f"Output {output_id} not found")
|
|
output.edited_content = edited_content
|
|
await self.db.flush()
|
|
return output
|
|
|
|
async def push_output(self, output_id: UUID, destination: str) -> SessionResolutionOutput:
|
|
result = await self.db.execute(
|
|
select(SessionResolutionOutput).where(SessionResolutionOutput.id == output_id)
|
|
)
|
|
output = result.scalar_one_or_none()
|
|
if not output:
|
|
raise ValueError(f"Output {output_id} not found")
|
|
|
|
from datetime import datetime, timezone
|
|
output.status = "pushed"
|
|
output.pushed_to = destination
|
|
output.pushed_at = datetime.now(timezone.utc)
|
|
await self.db.flush()
|
|
return output
|
|
|
|
def _build_session_context(
|
|
self,
|
|
session: AISession,
|
|
root_cause: str | None = None,
|
|
steps_taken: list[str] | None = None,
|
|
recommendations: str | None = None,
|
|
) -> str:
|
|
intake = session.intake_content or {}
|
|
intake_text = intake.get("text", "") or str(intake)
|
|
parts = [
|
|
f"Problem: {session.problem_summary or 'Unknown'}",
|
|
f"Domain: {session.problem_domain or 'Unknown'}",
|
|
f"Original intake: {intake_text[:300]}",
|
|
f"Resolution: {session.resolution_summary or 'Not specified'}",
|
|
]
|
|
|
|
# Structured handoff fields from cockpit conclude modal
|
|
if root_cause:
|
|
parts.append(f"Root cause: {root_cause}")
|
|
if steps_taken:
|
|
parts.append("Steps performed:")
|
|
for step in steps_taken:
|
|
parts.append(f" - {step}")
|
|
if recommendations:
|
|
parts.append(f"Recommendations: {recommendations}")
|
|
|
|
# Triage metadata (cockpit branch)
|
|
if getattr(session, 'client_name', None):
|
|
parts.append(f"Client: {session.client_name}")
|
|
if getattr(session, 'triage_hypothesis', None):
|
|
parts.append(f"Hypothesis: {session.triage_hypothesis}")
|
|
if getattr(session, 'evidence_items', None):
|
|
parts.append("Evidence collected:")
|
|
for item in session.evidence_items:
|
|
icon = {"confirmed": "✓", "ruled_out": "✗", "pending": "?"}.get(item.get("status", ""), "?")
|
|
parts.append(f" {icon} {item.get('text', '')}")
|
|
|
|
# Diagnostic steps from FlowPilot session steps
|
|
diagnostic = []
|
|
follow_ups: list[str] = []
|
|
for step in sorted(session.steps or [], key=lambda s: s.step_order):
|
|
content = step.content or {}
|
|
step_type = content.get("type", "")
|
|
if step_type == "resolution_suggestion":
|
|
recs = content.get("follow_up_recommendations", [])
|
|
if isinstance(recs, list):
|
|
follow_ups.extend(recs)
|
|
continue
|
|
description = content.get("text", "").strip()
|
|
if not description:
|
|
continue
|
|
response = None
|
|
if step.was_skipped:
|
|
response = "skipped"
|
|
elif step.selected_option and step.options_presented:
|
|
for opt in step.options_presented:
|
|
if opt.get("value") == step.selected_option:
|
|
response = opt.get("label", step.selected_option)
|
|
break
|
|
else:
|
|
response = step.selected_option
|
|
elif step.selected_option:
|
|
response = step.selected_option
|
|
elif step.free_text_input:
|
|
response = step.free_text_input
|
|
entry = f" {step.step_order + 1}. {description}"
|
|
if response and response != "skipped":
|
|
entry += f" — {response}"
|
|
diagnostic.append(entry)
|
|
|
|
if diagnostic:
|
|
parts.append("\nDiagnostic steps:")
|
|
parts.extend(diagnostic)
|
|
if follow_ups:
|
|
parts.append("\nRecommended follow-up:")
|
|
parts.extend(f" - {r}" for r in follow_ups)
|
|
|
|
return "\n".join(parts)
|
|
|
|
def _psa_notes_prompt(self, context: str) -> str:
|
|
return (
|
|
f"Generate professional PSA ticket notes for this resolved troubleshooting session.\n"
|
|
f"Format as structured markdown with: Problem, Diagnostic Steps, Resolution, Recommendations.\n\n{context}"
|
|
)
|
|
|
|
def _kb_article_prompt(self, context: str) -> str:
|
|
return (
|
|
f"Generate a knowledge base article draft from this resolved session.\n"
|
|
f"Include: Symptoms, Root Cause, Resolution Steps, Things to Rule Out First.\n\n{context}"
|
|
)
|
|
|
|
def _client_summary_prompt(self, context: str) -> str:
|
|
return (
|
|
f"Generate a non-technical summary for the end user/client.\n"
|
|
f"Explain what was wrong and what was done to fix it in plain language.\n"
|
|
f"No jargon. 2-3 paragraphs max.\n\n{context}"
|
|
)
|