* feat: add paletteIntent utility for command palette query classification
Detects query intent ('question' | 'keyword' | 'page' | 'empty') to drive
smart result ordering in the enhanced command palette.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: add recentFlows localStorage utility for command palette empty state
Tracks recently visited flows (capped at 10) with deduplication by id,
surfaced in command palette when query is empty.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: rewrite CommandPalette with categorized results and smart ranking
- Adds FlowPilot AI result (always present when query is non-empty)
- Intent-aware ordering: question → FlowPilot prominent; page → pages first;
keyword → FlowPilot at top with flows/sessions/tags below
- Pages section with admin-gated items (uses useAuthStore)
- Tags extracted from flow search results with ?tag= navigation
- Quick Actions for create/import/scripts
- Empty state shows recent flows + quick actions
- Grouped rendering with section labels per design system
- Keyboard nav flattened across groups
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: add FlowPilot prefill handoff from command palette to AssistantChatPage
When navigated to /assistant with location.state.prefill, automatically
creates a new chat and sends the prefill message without user interaction.
Clears location state after handling to prevent re-trigger on back navigation.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: track recently visited flows for command palette empty state
Calls addRecentFlow after tree data loads in both TreeNavigationPage and
ProceduralNavigationPage so the command palette can surface recent flows
when the query is empty.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* refactor: use useMemo instead of useCallback for groups builder in CommandPalette
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: add PSA ticket context Pydantic schemas (Task 6)
Add TicketDetails, CompanyInfo, ContactInfo, ConfigItem, TicketNote,
RelatedTicket, and TicketContext models in schemas/psa_context.py for
structured ticket context enrichment used by AI prompt injection.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: add ticket context prompt formatter (Task 7)
format_ticket_context_for_prompt() in services/psa/ticket_context.py
serializes TicketContext into structured text for AI system prompts,
with 10-note limit, 200-char text previews, and human-readable timestamps.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: add get_ticket_context() to ConnectWise provider (Task 8)
Fetches ticket details, company, contact, configurations, notes, and
related open tickets in parallel via asyncio.gather with partial failure
tolerance. Results are cached with a 5-minute TTL per ticket/connection.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: add GET /integrations/psa/tickets/{id}/context endpoint (Task 9)
Returns rich TicketContext for a ticket ID. Handles PSA auth failures
(returns structured error), ticket-not-found (404), and general PSA
errors (502). Requires active PSA connection for the user's account.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: inject PSA ticket context into copilot system prompt (Task 10)
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) <noreply@anthropic.com>
* feat: add PSA context API client with TypeScript interfaces
Defines TicketDetails, CompanyInfo, ContactInfo, ConfigItemInfo,
TicketNote, RelatedTicket, and TicketContext interfaces matching backend
psa_context.py schemas. Exports psaContextApi with getTicketContext().
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: add useTicketContext hook for PSA ticket context fetching
Accepts psaTicketId and psaConnectionId, fetches context on mount
when both IDs are present, and exposes refresh() for manual re-fetch.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: add TicketContextPanel component with accordion sections
Glass-card panel showing ticket summary, status/priority/SLA, and
accordion sections for Client, Contact, Devices, Notes, and Related
tickets. Matches design system with font-label labels and ice-cyan accents.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: mount TicketContextPanel in session runners when ticket is linked
ProceduralNavigationPage renders panel in left sidebar below step checklist.
TreeNavigationPage renders panel above breadcrumb trail. Both use
useTicketContext hook and show panel only when psa_ticket_id is set.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: add fallback_steps to TypeScript types (Task 15)
Add optional fallback_steps field to ProceduralStep interface.
Add FallbackStepRecord interface and fallback_decisions field to Session.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: add backend validation for fallback steps (Task 16)
Validate fallback_steps in procedural flow validation: required fields,
no nested fallback_steps, no duplicate IDs. Add FallbackStepRecord schema
and fallback_decisions field to SessionResponse.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: create FallbackSteps UI component (Task 17)
Collapsible component supporting edit and execute modes. Edit mode
provides title/description inputs with add/remove controls. Execute
mode shows "This worked" / "Didn't help" action buttons with emerald/
rose styling. Amber accent styling throughout.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: integrate FallbackSteps into editor and session runner (Task 18)
Wire FallbackSteps edit mode into StepEditor for procedure_step type
with add/remove/update handlers using crypto.randomUUID(). Add execute
mode rendering in ProceduralNavigationPage with fallbackDecisions state
tracking per parent step.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: add session-to-flow request/response schemas (Task 19)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: add session-to-flow AI generation service (Task 20)
Converts completed troubleshooting sessions into reusable procedural flows
with fallback branches. Includes PSA ticket context integration and
AI-generated step validation.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: add POST /ai/session-to-flow endpoint (Task 21)
Converts a completed session into a reusable procedural flow using AI.
Includes quota checking, usage recording, and proper error handling.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* feat: add Create Flow from Session button to session detail page (Task 22)
Adds sessionToFlow API client, exports from api/index.ts, and integrates
a prominent "Create Flow from Session" button on SessionDetailPage for
completed sessions. Generates a procedural flow via AI then navigates
to the procedural editor.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: cast tree_type to TreeType in session-to-flow creation
Fixes build error where string was not assignable to TreeType.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: update Playwright test selectors to match actual UI
- Use Control+k instead of Meta+k (Linux/CI compatibility)
- Use 'AI Assistant' group label instead of 'FlowPilot AI'
- Match actual FlowPilot chat page elements (Start a Conversation, New Chat, textarea)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: update Playwright test selectors to match actual UI
- Use specific command palette placeholder to avoid ambiguous matches
- Fix 'Quick Actions' scoping (two elements with same text)
- Fix 'Resolved' exact match on session detail page
- Fix tree editor to use getByText instead of getByDisplayValue
- Fix 'Add Step' strict mode by using .first()
- Fix fallback description placeholder text
- Update playwright.config.ts to use port 5433 and resolutionflow DB
- Update FlowPilot chat selectors to match actual page layout
11/17 new tests now passing. Remaining 6 need procedural session
navigation investigation.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* fix: resolve all Playwright test failures — 16/16 passing
- Fix procedural session tests: sessions auto-start, no Start button
- Fix strict mode violations: use getByRole('heading') for step titles
- Fix FlowPilot chat: use button role selector for New Chat
- Fix command palette page nav: scope Analytics click to palette modal
- Fix fallback runner: remove non-existent Start button click
- Update playwright.config to port 5433 for local Docker
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
255 lines
9.3 KiB
Python
255 lines
9.3 KiB
Python
"""Session-to-Flow AI generation service.
|
|
|
|
Converts a completed troubleshooting session into a reusable procedural
|
|
flow with fallback branches, powered by AI.
|
|
"""
|
|
import json
|
|
import logging
|
|
import re
|
|
import uuid
|
|
from typing import Any, Optional
|
|
from uuid import UUID
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.ai_provider import get_ai_provider
|
|
from app.core.config import settings
|
|
from app.core.ai_tree_validator import validate_generated_procedural_steps
|
|
from app.models.session import Session
|
|
from app.models.tree import Tree
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# AI system prompt for session-to-flow conversion
|
|
SESSION_TO_FLOW_SYSTEM_PROMPT = """You are an expert MSP engineer and IT process documentation specialist.
|
|
|
|
Your task is to convert a completed IT troubleshooting session into a reusable procedural flow with optional fallback branches.
|
|
|
|
You will receive:
|
|
- The session outcome and engineer notes
|
|
- An ordered list of decisions the engineer made (questions/answers, actions, notes, command output)
|
|
- The original troubleshooting tree structure (for context on alternative paths)
|
|
- Optional PSA ticket context
|
|
|
|
Generate a procedural flow that can be replicated for similar issues in the future. Each step should:
|
|
1. Be concrete and actionable — include exact commands, paths, or config values
|
|
2. Have a clear verification criterion
|
|
3. Include 1-3 fallback_steps per step (alternatives to try if the primary action fails)
|
|
4. End with a procedure_end step summarizing the resolution
|
|
|
|
Return ONLY a valid JSON object with this exact structure:
|
|
{
|
|
"name": "Short descriptive title (5-10 words)",
|
|
"description": "2-3 sentence description of what this flow resolves",
|
|
"tags": ["tag1", "tag2"],
|
|
"steps": [
|
|
{
|
|
"id": "step-1",
|
|
"type": "procedure_step",
|
|
"title": "Step title",
|
|
"description": "Detailed instructions with exact commands/paths",
|
|
"content_type": "text",
|
|
"fallback_steps": [
|
|
{
|
|
"id": "step-1-fb-1",
|
|
"type": "procedure_step",
|
|
"title": "Alternative: ...",
|
|
"description": "Alternative approach if primary step fails",
|
|
"content_type": "text"
|
|
}
|
|
]
|
|
},
|
|
{
|
|
"id": "step-end",
|
|
"type": "procedure_end",
|
|
"title": "Resolution Complete",
|
|
"description": "Summary of what was resolved and any follow-up actions"
|
|
}
|
|
]
|
|
}
|
|
|
|
Rules:
|
|
- Use unique string IDs for all steps (e.g. "step-1", "step-2", "step-1-fb-1")
|
|
- Include 3-10 procedure_step entries before the procedure_end
|
|
- Each step should be 1 concrete action, not a vague suggestion
|
|
- Fallback steps use the same schema as procedure_steps but represent alternative approaches
|
|
- Tags should be 2-5 relevant keywords (technology, vendor, symptom)
|
|
- Do NOT wrap JSON in markdown code fences
|
|
- Return only valid JSON, nothing else
|
|
"""
|
|
|
|
|
|
def _strip_markdown_fences(text: str) -> str:
|
|
"""Strip markdown code fences if the model wrapped its JSON response."""
|
|
text = text.strip()
|
|
match = re.match(r"^```(?:json)?\s*([\s\S]*?)```$", text)
|
|
if match:
|
|
return match.group(1).strip()
|
|
return text
|
|
|
|
|
|
def _build_session_context(session: Session, tree: Optional[Tree]) -> str:
|
|
"""Build a context string from session data for the AI prompt."""
|
|
parts: list[str] = []
|
|
|
|
# Flow info
|
|
tree_name = session.tree_snapshot.get("name", "Unknown Flow") if session.tree_snapshot else "Unknown Flow"
|
|
parts.append(f"Flow: {tree_name}")
|
|
parts.append(f"Outcome: {session.outcome or 'Unknown'}")
|
|
|
|
if session.outcome_notes:
|
|
parts.append(f"Outcome Notes: {session.outcome_notes}")
|
|
|
|
# Session decisions (the troubleshooting path taken)
|
|
if session.decisions:
|
|
parts.append("\n--- TROUBLESHOOTING PATH ---")
|
|
for i, decision in enumerate(session.decisions):
|
|
step_parts: list[str] = [f"\nStep {i + 1}:"]
|
|
if decision.get("question"):
|
|
step_parts.append(f" Question: {decision['question']}")
|
|
if decision.get("answer"):
|
|
step_parts.append(f" Answer: {decision['answer']}")
|
|
if decision.get("action_performed"):
|
|
step_parts.append(f" Action: {decision['action_performed']}")
|
|
if decision.get("notes"):
|
|
step_parts.append(f" Notes: {decision['notes']}")
|
|
if decision.get("command_output"):
|
|
# Truncate long command output
|
|
output = decision["command_output"]
|
|
if len(output) > 500:
|
|
output = output[:500] + "... [truncated]"
|
|
step_parts.append(f" Command Output: {output}")
|
|
parts.append("\n".join(step_parts))
|
|
|
|
# Scratchpad
|
|
if session.scratchpad and session.scratchpad.strip():
|
|
parts.append(f"\n--- ENGINEER SCRATCHPAD ---\n{session.scratchpad[:1000]}")
|
|
|
|
# Original tree structure (for branch context, truncated)
|
|
if tree and tree.tree_structure:
|
|
tree_json = json.dumps(tree.tree_structure, indent=None)
|
|
if len(tree_json) > 3000:
|
|
tree_json = tree_json[:3000] + "... [truncated]"
|
|
parts.append(f"\n--- ORIGINAL TREE STRUCTURE (for alternative paths) ---\n{tree_json}")
|
|
elif session.tree_snapshot:
|
|
snapshot_json = json.dumps(session.tree_snapshot, indent=None)
|
|
if len(snapshot_json) > 3000:
|
|
snapshot_json = snapshot_json[:3000] + "... [truncated]"
|
|
parts.append(f"\n--- TREE SNAPSHOT (for alternative paths) ---\n{snapshot_json}")
|
|
|
|
return "\n".join(parts)
|
|
|
|
|
|
async def generate_flow_from_session(
|
|
session_id: str,
|
|
user_id: UUID,
|
|
account_id: UUID,
|
|
db: AsyncSession,
|
|
) -> dict[str, Any]:
|
|
"""Generate a procedural flow from a completed session.
|
|
|
|
Returns a dict with keys: name, description, tree_type, tags, tree_structure.
|
|
Raises ValueError on validation failures, Exception on AI/DB errors.
|
|
"""
|
|
# Load the session
|
|
session_uuid = UUID(session_id) if isinstance(session_id, str) else session_id
|
|
result = await db.execute(
|
|
select(Session).where(
|
|
Session.id == session_uuid,
|
|
Session.user_id == user_id,
|
|
)
|
|
)
|
|
session = result.scalar_one_or_none()
|
|
if not session:
|
|
raise ValueError(f"Session '{session_id}' not found or access denied")
|
|
|
|
# Load the original tree for branch context
|
|
tree: Optional[Tree] = None
|
|
if session.tree_id:
|
|
tree_result = await db.execute(
|
|
select(Tree).where(Tree.id == session.tree_id)
|
|
)
|
|
tree = tree_result.scalar_one_or_none()
|
|
|
|
# Build session context
|
|
session_context = _build_session_context(session, tree)
|
|
|
|
# Optionally fetch PSA ticket context
|
|
psa_context = ""
|
|
if session.psa_ticket_id and session.psa_connection_id:
|
|
try:
|
|
from app.services.psa.registry import get_provider_for_account
|
|
from app.services.psa.ticket_context import format_ticket_context_for_prompt
|
|
|
|
psa_provider = await get_provider_for_account(account_id, db)
|
|
connection_id = str(session.psa_connection_id)
|
|
ticket_ctx = await psa_provider.get_ticket_context(
|
|
ticket_id=int(session.psa_ticket_id),
|
|
connection_id=connection_id,
|
|
)
|
|
psa_context = "\n\n--- PSA TICKET CONTEXT ---\n" + format_ticket_context_for_prompt(ticket_ctx)
|
|
except Exception as psa_err:
|
|
logger.warning(
|
|
"Failed to fetch PSA ticket context for session-to-flow (session=%s, ticket=%s): %s",
|
|
session_id,
|
|
session.psa_ticket_id,
|
|
psa_err,
|
|
)
|
|
|
|
# Build user message
|
|
user_message = (
|
|
"Please convert the following completed troubleshooting session into a reusable procedural flow:\n\n"
|
|
f"{session_context}"
|
|
f"{psa_context}"
|
|
)
|
|
|
|
# Call AI
|
|
model = settings.get_model_for_action("generate_steps")
|
|
provider = get_ai_provider(model=model)
|
|
|
|
raw_text, input_tokens, output_tokens = await provider.generate_json(
|
|
system_prompt=SESSION_TO_FLOW_SYSTEM_PROMPT,
|
|
messages=[{"role": "user", "content": user_message}],
|
|
max_tokens=4096,
|
|
)
|
|
|
|
logger.info(
|
|
"session_to_flow AI response (tokens in=%d out=%d, session=%s)",
|
|
input_tokens,
|
|
output_tokens,
|
|
session_id,
|
|
)
|
|
|
|
# Strip markdown fences and parse JSON
|
|
raw_text = _strip_markdown_fences(raw_text)
|
|
try:
|
|
generated = json.loads(raw_text)
|
|
except json.JSONDecodeError as e:
|
|
raise ValueError(f"AI returned invalid JSON: {e}") from e
|
|
|
|
# Validate the generated steps
|
|
val_errors = validate_generated_procedural_steps(generated)
|
|
if val_errors:
|
|
raise ValueError(f"Generated flow failed validation: {'; '.join(val_errors)}")
|
|
|
|
# Ensure procedure_end exists; add if missing
|
|
steps = generated.get("steps", [])
|
|
has_end = any(s.get("type") == "procedure_end" for s in steps)
|
|
if not has_end:
|
|
steps.append({
|
|
"id": f"step-end-{uuid.uuid4().hex[:8]}",
|
|
"type": "procedure_end",
|
|
"title": "Procedure Complete",
|
|
"description": "All steps completed successfully.",
|
|
})
|
|
generated["steps"] = steps
|
|
|
|
return {
|
|
"name": generated.get("name", "AI-Generated Flow"),
|
|
"description": generated.get("description", ""),
|
|
"tree_type": "procedural",
|
|
"tags": generated.get("tags", []),
|
|
"tree_structure": {"steps": steps},
|
|
}
|