File size: 2,388 Bytes
23d418f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { ChevronRight, Loader2 } from 'lucide-react'
import { useState } from 'react'
import { EvidenceList } from './EvidenceList'
import { Progress } from './Progress'
import { STEPS } from '@/lib/content'
import { seconds } from '@/lib/format'
import { running, type RunState } from '@/lib/run'

/** How the answer was made, like Claude's "thinking": open while the system works (each
 * step ticks off with what it found), then folded to one line ("Worked 19 s · 6 steps")
 * that opens again on click. The passages the model read are inside it. */
export function Trace({ run, lastEventAt }: { run: RunState; lastEventAt: number }) {
  const [opened, setOpened] = useState<boolean | null>(null) // null: follow the run
  const working = run.phase === 'running'
  const open = opened ?? working
  const done = run.steps.filter((s) => s.status === 'done').length
  const step = running(run)
  const total = run.answer?.latency_ms

  let summary: string
  if (working) summary = step && run.live ? `${STEPS[step.name]?.title ?? step.name}…` : 'Searching and answering…'
  else if (run.live) summary = `Worked ${seconds(total)} · ${done} step${done === 1 ? '' : 's'}`
  else summary = `Worked ${seconds(total)} · one search and one model call`
  if (run.phase === 'error') summary = 'Stopped before an answer'

  return (
    <div className="text-sm">
      <button
        type="button"
        onClick={() => setOpened(!open)}
        aria-expanded={open}
        className="inline-flex items-center gap-1.5 rounded-md py-1 text-muted-foreground hover:text-foreground"
      >
        {working ? <Loader2 className="size-3.5 animate-spin text-primary" /> : <ChevronRight className={`size-3.5 transition-transform ${open ? 'rotate-90' : ''}`} />}
        <span>{summary}</span>
      </button>
      {open && (
        <div className="mt-1 ml-1.5 space-y-3 border-l pl-4">
          <Progress run={run} since={lastEventAt} />
          {run.evidence && (
            <details className="group">
              <summary className="cursor-pointer text-xs font-medium text-muted-foreground hover:text-foreground">
                Read {run.evidence.length} passages
              </summary>
              <div className="mt-1">
                <EvidenceList passages={run.evidence} />
              </div>
            </details>
          )}
        </div>
      )}
    </div>
  )
}