File size: 4,510 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { Check, Circle, Loader2, MinusCircle } from 'lucide-react'
import { useEffect, useState } from 'react'
import { STEPS } from '@/lib/content'
import { seconds } from '@/lib/format'
import type { RunState, StepView } from '@/lib/run'
import type { StepDetail } from '@/lib/types'
import { cn } from '@/lib/utils'

/** The run as it happens: C's graph node by node (with what each found), or A/B's steps
 * as one call. The running step shows a live timer from when the last event came. */
export function Progress({ run, since }: { run: RunState; since: number }) {
  const now = useNow(run.phase === 'running')
  return (
    <div className="space-y-1">
      {!run.live && run.steps.length > 0 && (
        <p className="mb-2 text-xs text-muted-foreground">
          System {run.system?.toUpperCase()} runs these steps in one call: no progress until the answer.
        </p>
      )}
      <ol className="space-y-1">
        {run.steps.map((s) => (
          <li key={s.name} className={cn('flex gap-2 rounded-md px-2 py-1.5', s.status === 'running' && 'bg-muted')}>
            <StatusIcon status={s.status} />
            <div className="min-w-0 flex-1 break-words">
              <div className="flex items-baseline justify-between gap-2 text-sm">
                <span className={cn('font-medium', s.status === 'skipped' && 'text-muted-foreground line-through')}>
                  {STEPS[s.name]?.title ?? s.name}
                </span>
                <span className="shrink-0 text-xs tabular-nums text-muted-foreground">
                  {s.status === 'done' ? seconds(s.ms) : s.status === 'running' ? seconds(now - since) : ''}
                  {s.status === 'skipped' ? 'not needed' : ''}
                </span>
              </div>
              <p className="text-xs text-muted-foreground">{STEPS[s.name]?.what}</p>
              {s.status === 'done' && s.detail && <Detail step={s} detail={s.detail} />}
            </div>
          </li>
        ))}
      </ol>
    </div>
  )
}

function StatusIcon({ status }: { status: StepView['status'] }) {
  const cls = 'mt-0.5 size-4 shrink-0'
  if (status === 'done') return <Check role="img" className={cn(cls, 'text-emerald-600')} aria-label="done" />
  if (status === 'running') return <Loader2 role="img" className={cn(cls, 'animate-spin text-primary')} aria-label="running" />
  if (status === 'skipped') return <MinusCircle role="img" className={cn(cls, 'text-muted-foreground')} aria-label="not needed" />
  return <Circle role="img" className={cn(cls, 'text-muted-foreground/50')} aria-label="waiting" />
}

/** What a finished step found, in a line or a short list. */
function Detail({ step, detail }: { step: StepView; detail: StepDetail }) {
  const line = 'mt-1 text-xs'
  switch (step.name) {
    case 'retrieve':
      return <p className={line}>{detail.candidates} candidate passages</p>
    case 'judge':
    case 'judge_new':
      return (
        <ul className={cn(line, 'space-y-0.5')}>
          {(detail.requirements ?? []).map((r, i) => (
            <li key={i} className="flex gap-1.5">
              <span className={r.found ? 'text-emerald-700 dark:text-emerald-400' : 'text-amber-700 dark:text-amber-400'}>
                {r.found ? 'found' : 'missing'}
              </span>
              <span>{r.need}</span>
            </li>
          ))}
        </ul>
      )
    case 'research':
      return (
        <p className={line}>
          {(detail.queries ?? []).map((q) => `"${q}"`).join(', ')} → {detail.new_candidates} new passages
        </p>
      )
    case 'select':
      return (
        <p className={line}>
          {detail.kept} passages kept, {detail.expansions} cited sections added
          {detail.fallback ? ' (the judge kept none: the top search results are used)' : ''}
        </p>
      )
    case 'verify':
      return (
        <p className={line}>
          {detail.checked} citations checked
          {detail.retried ? `, ${detail.failed_first?.length ?? 0} failed the first time: the answer was retried` : ', all passed'}
          {detail.dropped ? `, ${detail.dropped} still failing and dropped` : ''}
        </p>
      )
    default:
      return null
  }
}

/** The current time, ticking every 100 ms while `on`. */
function useNow(on: boolean): number {
  const [now, setNow] = useState(() => Date.now())
  useEffect(() => {
    if (!on) return
    const timer = setInterval(() => setNow(Date.now()), 100)
    return () => clearInterval(timer)
  }, [on])
  return now
}