CoverageCheck / web /src /components /chat /Composer.tsx
TBM99's picture
Deploy coverage-check 3559250
23d418f verified
Raw History Blame Contribute Delete
6.48 kB
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>
)
}