import { useState, useEffect, Fragment } from 'react' import { trpc } from '@/lib/trpc' import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { Skeleton } from '@/components/ui/skeleton' import { useSubscriptionWithErrorHandling } from '@/hooks' import type { DrilldownDialogProps } from './types' const RELEVANT_EVENTS = ['agent:waiting'] function formatTimestamp(ts: string): string { const date = new Date(ts) const now = new Date() const diffMs = now.getTime() - date.getTime() const diffSecs = Math.floor(diffMs / 1000) const diffMins = Math.floor(diffSecs / 60) const diffHours = Math.floor(diffMins / 60) const diffDays = Math.floor(diffHours / 24) let relative: string if (diffSecs < 60) { relative = `${diffSecs}s ago` } else if (diffMins < 60) { relative = `${diffMins}m ago` } else if (diffHours < 24) { relative = `${diffHours}h ago` } else { relative = `${diffDays}d ago` } const absolute = date.toLocaleString('en-US', { month: 'short', day: 'numeric', year: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false, }) return `${relative} · ${absolute}` } function truncate(text: string, max: number): string { return text.length > max ? text.slice(0, max) + '…' : text } export function QuestionsAskedDialog({ open, onOpenChange, agentId, agentName, isAgentRunning, }: DrilldownDialogProps) { const [expandedIndex, setExpandedIndex] = useState(null) const { data, isLoading, refetch } = trpc.agent.getQuestionsAsked.useQuery( { agentId }, { enabled: open } ) const [lastRefreshedAt, setLastRefreshedAt] = useState(null) const [secondsAgo, setSecondsAgo] = useState(0) useSubscriptionWithErrorHandling( () => trpc.onEvent.useSubscription(undefined), { enabled: open && !!isAgentRunning, onData: (event: any) => { const eventType: string = event?.data?.type ?? event?.type ?? '' const eventAgentId: string = event?.data?.agentId ?? event?.agentId ?? '' if (RELEVANT_EVENTS.some(e => eventType.startsWith(e)) && eventAgentId === agentId) { void refetch().then(() => { setLastRefreshedAt(new Date()) setSecondsAgo(0) }) } }, } ) useEffect(() => { if (!open || !isAgentRunning || !lastRefreshedAt) return const interval = setInterval(() => { setSecondsAgo(Math.floor((Date.now() - lastRefreshedAt.getTime()) / 1000)) }, 1000) return () => clearInterval(interval) }, [open, isAgentRunning, lastRefreshedAt]) useEffect(() => { if (!open) { setExpandedIndex(null) setLastRefreshedAt(null) setSecondsAgo(0) } }, [open]) return ( {`Questions Asked — ${agentName}`} Each row is a question this agent sent to the user via the AskUserQuestion tool.
{isLoading ? (
{[0, 1, 2].map((i) => (
{i < 2 &&
}
))}
) : !data || data.length === 0 ? (

No data found

) : ( <> {data.length >= 200 && (

Showing first 200 instances.

)} {data.map((row, i) => { const n = row.questions.length const countLabel = `${n} question${n !== 1 ? 's' : ''}` const firstHeader = truncate(row.questions[0]?.header ?? '', 40) return ( setExpandedIndex(i === expandedIndex ? null : i)} > {expandedIndex === i && ( )} ) })}
Timestamp # Questions First Question Header
{formatTimestamp(row.timestamp)} {countLabel} {firstHeader}
    {row.questions.map((q, qi) => (
  1. {q.header} {q.question}
      {q.options.map((opt, oi) => (
    • {`• ${opt.label} — ${opt.description}`}
    • ))}
  2. ))}
)}
{isAgentRunning && lastRefreshedAt && (

Last refreshed: {secondsAgo === 0 ? 'just now' : `${secondsAgo}s ago`}

)}
) }