import { useEffect, useState } from 'react' import { useParams, useSearchParams } from 'react-router-dom' import { Sparkles, Loader2 } from 'lucide-react' import { useFlowPilotSession } from '@/hooks/useFlowPilotSession' import { FlowPilotIntake, FlowPilotSession, SessionBriefing } from '@/components/flowpilot' import { aiSessionsApi } from '@/api' import { toast } from '@/lib/toast' export default function FlowPilotSessionPage() { const { sessionId } = useParams<{ sessionId?: string }>() const [searchParams, setSearchParams] = useSearchParams() const isPickup = searchParams.get('pickup') === 'true' const fp = useFlowPilotSession() const [pickingUp, setPickingUp] = useState(false) // Load existing session if ID in URL useEffect(() => { if (sessionId && !fp.session) { fp.loadSession(sessionId) } }, [sessionId]) // eslint-disable-line react-hooks/exhaustive-deps const handlePickupContinue = async () => { if (!sessionId) return setPickingUp(true) try { await aiSessionsApi.pickupSession(sessionId, { resume_mode: 'continue' }) // Clear pickup param and reload the session as active setSearchParams({}) await fp.loadSession(sessionId) } catch (e: unknown) { const message = e instanceof Error ? e.message : 'Failed to pick up session' toast.error(message) } finally { setPickingUp(false) } } const handlePickupFresh = async (context: string) => { if (!sessionId) return setPickingUp(true) try { await aiSessionsApi.pickupSession(sessionId, { resume_mode: 'fresh', additional_context: context, }) setSearchParams({}) await fp.loadSession(sessionId) } catch (e: unknown) { const message = e instanceof Error ? e.message : 'Failed to pick up session' toast.error(message) } finally { setPickingUp(false) } } // Error state if (fp.error && !fp.session) { return (

{fp.error}

) } // Loading if (fp.isLoading && !fp.session) { return (
) } // Intake screen (no session yet) if (!fp.session) { return (
) } // Escalation pickup briefing if (isPickup && fp.session.status === 'requesting_escalation' && fp.session.escalation_reason) { // Build escalation package from session detail // The escalation_package is in the session but not directly on AISessionDetail — // we use what's available from the session fields const escalationPackage = { problem_summary: fp.session.problem_summary ?? undefined, escalation_reason: fp.session.escalation_reason ?? undefined, // Steps are available from the session detail steps_tried: fp.allSteps.map(step => ({ step_type: step.step_type, description: (step.content as Record)?.text || '', })), } return (
{/* Header */}

Escalation Pickup — {fp.session.problem_summary || 'FlowPilot Session'}

Awaiting pickup
{/* Briefing */}
) } // Active/completed session return (
{/* Header */}

{fp.session.problem_summary || 'FlowPilot Session'}

{fp.session.status}
{/* Session content */}
) }