File size: 6,480 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { ArrowUp, CalendarDays, Check, ChevronDown, Square } from 'lucide-react'
import { RadioGroup } from 'radix-ui'
import { useState } from 'react'
import { Button } from '@/components/ui/button'
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
import { SYSTEMS } from '@/lib/content'
import { percent, seconds } from '@/lib/format'
import type { SystemId, SystemsFile } from '@/lib/types'
import { cn } from '@/lib/utils'

/** The question box: text, then a row with the policy date, the system and send/stop.
 * Enter sends, Shift+Enter is a new line (as in Claude and ChatGPT). */
export function Composer({
  system,
  onSystem,
  day,
  onDay,
  numbers,
  busy,
  live,
  onSend,
  onStop,
}: {
  system: SystemId
  onSystem: (s: SystemId) => void
  day: string | null
  onDay: (d: string | null) => void
  numbers: SystemsFile | null
  busy: boolean
  live: boolean // the backend answers questions
  onSend: (question: string) => void
  onStop: () => void
}) {
  const [text, setText] = useState('')
  const ready = live && !busy && text.trim().length >= 3

  function send() {
    if (!ready) return
    onSend(text.trim())
    setText('')
  }

  return (
    <div className="rounded-2xl border bg-background shadow-sm focus-within:border-primary/50 focus-within:ring-2 focus-within:ring-primary/15">
      <textarea
        value={text}
        onChange={(e) => setText(e.target.value)}
        onKeyDown={(e) => {
          if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing) {
            e.preventDefault()
            send()
          }
        }}
        rows={1}
        maxLength={1000}
        placeholder="Ask about Ontario auto insurance policies…"
        aria-label="Your question"
        className="field-sizing-content max-h-48 min-h-12 w-full resize-none bg-transparent px-4 pt-3 text-[15px] outline-none placeholder:text-muted-foreground"
      />
      <div className="flex items-center gap-1.5 px-2 pb-2">
        <DateButton day={day} onDay={onDay} />
        <SystemButton system={system} onSystem={onSystem} numbers={numbers} />
        <div className="ml-auto">
          {busy ? (
            <Button size="icon" variant="secondary" className="rounded-full" onClick={onStop} aria-label="Stop">
              <Square className="fill-current" />
            </Button>
          ) : (
            <Button size="icon" className="rounded-full" onClick={send} disabled={!ready} aria-label="Send">
              <ArrowUp />
            </Button>
          )}
        </div>
      </div>
    </div>
  )
}

const chip =
  'inline-flex h-8 items-center gap-1.5 rounded-full border px-3 text-xs font-medium text-muted-foreground hover:bg-accent hover:text-accent-foreground data-[state=open]:bg-accent data-[state=open]:text-accent-foreground'

/** The policy date: which versions of each document apply (B and C filter by it; A is
 * only told it). Empty = today. */
function DateButton({ day, onDay }: { day: string | null; onDay: (d: string | null) => void }) {
  return (
    <Popover>
      <PopoverTrigger className={chip}>
        <CalendarDays className="size-3.5" />
        {day ? `Policy date ${day}` : 'Policy date: today'}
      </PopoverTrigger>
      <PopoverContent align="start" className="w-72 space-y-2 text-sm">
        <p className="font-medium">Policy effective date</p>
        <p className="text-xs text-muted-foreground">
          Answers use the versions of each form and law in force on this date.
        </p>
        <input
          type="date"
          value={day ?? ''}
          onChange={(e) => onDay(e.target.value || null)}
          className="w-full rounded-md border px-2 py-1.5"
          aria-label="Policy effective date"
        />
        {day && (
          <button type="button" className="text-xs text-primary underline" onClick={() => onDay(null)}>
            Use today
          </button>
        )}
      </PopoverContent>
    </Popover>
  )
}

/** Which system answers: what each does, and its held-out accuracy and median time
 * (graded by an LLM), so the choice is a visible trade-off. */
function SystemButton({
  system,
  onSystem,
  numbers,
}: {
  system: SystemId
  onSystem: (s: SystemId) => void
  numbers: SystemsFile | null
}) {
  const [open, setOpen] = useState(false)
  return (
    <Popover open={open} onOpenChange={setOpen}>
      <PopoverTrigger className={chip}>
        System {system.toUpperCase()}
        {numbers ? ` · ${numbers.systems[system].name}` : ''}
        <ChevronDown className="size-3.5" />
      </PopoverTrigger>
      <PopoverContent align="start" className="w-96 p-2">
        <RadioGroup.Root
          value={system}
          onValueChange={(v) => {
            onSystem(v as SystemId)
            setOpen(false)
          }}
          aria-label="System"
          className="space-y-1"
        >
          {(['a', 'b', 'c'] as SystemId[]).map((id) => {
            const n = numbers?.systems[id]
            return (
              <RadioGroup.Item
                key={id}
                value={id}
                className={cn(
                  'flex w-full gap-2 rounded-lg p-2.5 text-left outline-none hover:bg-accent focus-visible:bg-accent',
                )}
              >
                <span className="mt-0.5 size-4 shrink-0">
                  <RadioGroup.Indicator>
                    <Check className="size-4 text-primary" />
                  </RadioGroup.Indicator>
                </span>
                <span className="min-w-0">
                  <span className="block text-sm font-medium">
                    System {id.toUpperCase()} · {n?.name ?? ''}
                  </span>
                  <span className="block text-xs leading-snug text-muted-foreground">{SYSTEMS[id].how}</span>
                  {n && (
                    <span className="mt-1 block text-xs tabular-nums text-foreground/80">
                      {percent(n.accuracy)} correct on the held-out test · {seconds(n.latency_p50_ms)} median
                    </span>
                  )}
                </span>
              </RadioGroup.Item>
            )
          })}
        </RadioGroup.Root>
        {numbers && (
          <p className="px-2.5 pt-1 pb-1 text-[11px] text-muted-foreground">
            Accuracy on {numbers.systems.c.n} held-out questions, written and graded by an LLM. Details on the Evals page.
          </p>
        )}
      </PopoverContent>
    </Popover>
  )
}