-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrols.tsx
More file actions
393 lines (363 loc) · 15.4 KB
/
Copy pathcontrols.tsx
File metadata and controls
393 lines (363 loc) · 15.4 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/**
* Shared chat-shell control primitives — the LEAF that both the web-react barrel
* (`./index`) and the composer children (`./agent-session-controls`,
* `./seat-paywall`) import directly, so neither child has to reach back through
* the barrel (which would re-create an import cycle). The barrel re-exports the
* public names (`usePopover`, `usePending`, `ModelPicker`, `EffortPicker`, …)
* unchanged, so the published export surface is identical.
*
* Styling contract matches the rest of `web-react`: Tailwind classes against the
* shared design tokens; the glyphs are inline SVGs, no icon-library dependency.
*/
import { useEffect, useMemo, useRef, useState, type ReactNode } from 'react'
import { ProviderLogo } from './provider-logo'
import type { CatalogModel } from '../runtime/model-catalog'
// ── shared glyphs (no icon-library dependency) ────────────────────────────
export function ChevronDown({ className }: { className?: string }) {
return (
<svg className={className} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden>
<path d="m6 9 6 6 6-6" />
</svg>
)
}
export function SearchGlyph({ className }: { className?: string }) {
return (
<svg className={className} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden>
<circle cx="11" cy="11" r="8" />
<path d="m21 21-4.3-4.3" />
</svg>
)
}
export function SparkleGlyph({ className }: { className?: string }) {
return (
<svg className={className} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden>
<path d="M12 3v3m0 12v3M3 12h3m12 0h3M5.6 5.6l2.1 2.1m8.6 8.6 2.1 2.1m0-12.8-2.1 2.1M7.7 16.3l-2.1 2.1" />
</svg>
)
}
/**
* Keyboard + pointer model for a trigger-and-popover pair, dependency-free.
* Outside-mousedown and Escape both close; Escape also returns focus to the
* trigger so keyboard users aren't dropped at the top of the document. The
* returned `triggerProps` carry the ARIA contract (`aria-haspopup`/
* `aria-expanded`); spread them onto the trigger button.
*/
export function usePopover(open: boolean, setOpen: (open: boolean) => void) {
const containerRef = useRef<HTMLDivElement>(null)
const triggerRef = useRef<HTMLButtonElement>(null)
useEffect(() => {
if (!open) return
function onMouseDown(e: MouseEvent) {
if (containerRef.current && !containerRef.current.contains(e.target as Node)) setOpen(false)
}
function onKeyDown(e: KeyboardEvent) {
if (e.key === 'Escape') {
setOpen(false)
triggerRef.current?.focus()
}
}
document.addEventListener('mousedown', onMouseDown)
document.addEventListener('keydown', onKeyDown)
return () => {
document.removeEventListener('mousedown', onMouseDown)
document.removeEventListener('keydown', onKeyDown)
}
}, [open, setOpen])
return {
containerRef,
triggerRef,
triggerProps: {
ref: triggerRef,
'aria-haspopup': true as const,
'aria-expanded': open,
},
}
}
/** Tailwind utilities applied to every popover option so keyboard focus is
* visible (the prior buttons had no focus ring). */
export const POPOVER_OPTION_FOCUS =
'focus:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-card'
/**
* Guard an async action against double-submit. `run` ignores re-entrant calls
* while a promise is in flight and flips `pending` so the caller can disable
* the control — the fix for double-charge / double-approve on a slow network.
* Settles (success or throw) before clearing, and no-ops state updates after
* unmount.
*/
export function usePending(): { pending: boolean; run: (action: () => void | Promise<void>) => void } {
const [pending, setPending] = useState(false)
const inFlight = useRef(false)
const mounted = useRef(true)
useEffect(() => {
mounted.current = true
return () => {
mounted.current = false
}
}, [])
const run = (action: () => void | Promise<void>) => {
if (inFlight.current) return
let result: void | Promise<void>
try {
result = action()
} catch {
return
}
if (!(result instanceof Promise)) return
inFlight.current = true
setPending(true)
void result.finally(() => {
inFlight.current = false
if (mounted.current) setPending(false)
})
}
return { pending, run }
}
// ── ModelPicker ───────────────────────────────────────────────────────────
export interface ModelPickerProps {
value: string
onChange: (id: string) => void
/** Catalogue models — from `GET`ing the app's catalogue route (see
* `runtime/model-catalog`), plus any product-specific entries appended. */
models: CatalogModel[]
loading?: boolean
/** Render a provider logo/badge; default is a generic sparkle. */
renderProviderBadge?: (provider: string) => ReactNode
/** Section label for `featured` models. */
recommendedLabel?: string
}
function formatPrice(p?: string): string | undefined {
if (!p) return undefined
const n = Number(p)
if (isNaN(n) || n === 0) return undefined
const perM = n * 1_000_000
return perM >= 1 ? `$${perM.toFixed(0)}/M` : `$${perM.toFixed(2)}/M`
}
function formatContext(len?: number): string | undefined {
if (!len) return undefined
if (len >= 1_000_000) return `${(len / 1_000_000).toFixed(1)}M ctx`
if (len >= 1_000) return `${Math.round(len / 1_000)}K ctx`
return `${len} ctx`
}
function SectionHeader({ children }: { children: ReactNode }) {
return (
<div className="px-3 pb-1 pt-3 text-xs font-semibold uppercase tracking-wide text-muted-foreground">
{children}
</div>
)
}
function ModelRow({
model,
selected,
onSelect,
renderProviderBadge,
}: {
model: CatalogModel
selected: boolean
onSelect: () => void
renderProviderBadge?: (provider: string) => ReactNode
}) {
const price = formatPrice(model.pricing?.prompt)
const ctx = formatContext(model.contextLength)
return (
<button
type="button"
onClick={onSelect}
className={`flex w-full items-center gap-2.5 rounded-md px-3 py-2.5 text-left text-sm transition ${POPOVER_OPTION_FOCUS} ${
selected ? 'bg-primary/10 font-medium' : 'hover:bg-accent/30'
}`}
>
{renderProviderBadge ? renderProviderBadge(model.provider) : <ProviderLogo provider={model.provider} size={16} />}
<span className="truncate">{model.name}</span>
{!model.supportsTools && (
<span className="shrink-0 rounded bg-muted/60 px-1.5 py-0.5 text-[10px] font-medium text-muted-foreground">
no tools
</span>
)}
<span className="ml-auto flex shrink-0 items-center gap-2 text-xs text-muted-foreground">
{ctx && <span>{ctx}</span>}
{price && <span>{price}</span>}
</span>
</button>
)
}
/**
* Searchable model picker pill + popover: a featured/recommended section
* first, then per-provider groups in catalogue order (the server already
* sorts providers by tier).
*/
export function ModelPicker({ value, onChange, models, loading, renderProviderBadge, recommendedLabel = 'Recommended' }: ModelPickerProps) {
const [open, setOpen] = useState(false)
const [query, setQuery] = useState('')
const { containerRef, triggerProps } = usePopover(open, setOpen)
const inputRef = useRef<HTMLInputElement>(null)
useEffect(() => {
if (open) inputRef.current?.focus()
}, [open])
const selected = models.find((m) => m.id === value)
const filtered = useMemo(() => {
const q = query.trim().toLowerCase()
if (!q) return null
return models.filter(
(m) =>
m.id.toLowerCase().includes(q) ||
m.name.toLowerCase().includes(q) ||
(m.description?.toLowerCase() ?? '').includes(q) ||
m.provider.toLowerCase().includes(q),
)
}, [models, query])
const sections = useMemo(() => {
const recommended = models.filter((m) => m.featured)
const byProvider: Array<{ provider: string; items: CatalogModel[] }> = []
for (const m of models) {
if (m.featured) continue
const last = byProvider[byProvider.length - 1]
if (last && last.provider === m.provider) last.items.push(m)
else byProvider.push({ provider: m.provider, items: [m] })
}
return { recommended, byProvider }
}, [models])
const select = (id: string) => {
onChange(id)
setOpen(false)
setQuery('')
}
return (
<div ref={containerRef} className="relative inline-flex">
<button
type="button"
{...triggerProps}
onClick={() => setOpen(!open)}
className="inline-flex items-center gap-1.5 rounded-full border border-border bg-card px-3 py-1.5 text-sm font-medium text-foreground transition hover:bg-accent/30 focus:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background"
>
{selected ? (renderProviderBadge ? renderProviderBadge(selected.provider) : <ProviderLogo provider={selected.provider} size={16} />) : <SparkleGlyph className="h-3.5 w-3.5 text-muted-foreground" />}
<span className="max-w-[160px] truncate">{selected?.name ?? value}</span>
<ChevronDown className="h-3.5 w-3.5 text-muted-foreground" />
</button>
{open && (
<div className="absolute bottom-full left-0 z-50 mb-2 w-[420px] max-w-[calc(100vw-2rem)] overflow-hidden rounded-xl border border-border bg-card shadow-lg">
<div className="border-b border-border px-3 py-2">
<div className="flex items-center gap-2 rounded-lg border border-border bg-background px-3 py-2">
<SearchGlyph className="h-3.5 w-3.5 text-muted-foreground" />
<input
ref={inputRef}
type="text"
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder="Search models..."
className="flex-1 bg-transparent text-sm outline-none placeholder:text-muted-foreground"
/>
</div>
</div>
<div className="max-h-[400px] overflow-y-auto p-1 pb-2">
{loading && <div className="px-3 py-4 text-center text-sm text-muted-foreground">Loading models...</div>}
{!loading && filtered && (
<>
{filtered.length === 0 && (
<div className="px-3 py-4 text-center text-sm text-muted-foreground">No models match your search</div>
)}
{filtered.map((m) => (
<ModelRow key={m.id} model={m} selected={m.id === value} onSelect={() => select(m.id)} renderProviderBadge={renderProviderBadge} />
))}
</>
)}
{!loading && !filtered && (
<>
{sections.recommended.length > 0 && (
<>
<SectionHeader>{recommendedLabel}</SectionHeader>
{sections.recommended.map((m) => (
<ModelRow key={m.id} model={m} selected={m.id === value} onSelect={() => select(m.id)} renderProviderBadge={renderProviderBadge} />
))}
</>
)}
{sections.byProvider.map((g) => (
<div key={g.provider}>
<SectionHeader>{g.provider}</SectionHeader>
{g.items.map((m) => (
<ModelRow key={m.id} model={m} selected={m.id === value} onSelect={() => select(m.id)} renderProviderBadge={renderProviderBadge} />
))}
</div>
))}
</>
)}
</div>
</div>
)}
</div>
)
}
// ── EffortPicker ──────────────────────────────────────────────────────────
/** One reasoning-budget level: the engine `id` is unchanged (the value the
* product sends to the loop); only the user-facing `label` is renamed to the
* plainer "how hard should it think" vocabulary from docs/product-surfaces.md.
* `low`→Quick, `medium`→Standard, `high`→Extended. The mapping is overridable
* via `EffortPickerProps.levels`, so a product can relabel without losing the
* ids the runtime expects. */
export interface EffortLevel {
id: string
label: string
}
export const DEFAULT_EFFORT_LEVELS: readonly EffortLevel[] = [
{ id: 'off', label: 'Off' },
{ id: 'low', label: 'Quick' },
{ id: 'medium', label: 'Standard' },
{ id: 'high', label: 'Extended' },
]
export interface EffortPickerProps {
value: string
onChange: (id: string) => void
/** Selectable levels (engine id + user-facing label). Defaults to the plain
* "Thinking" vocabulary; override to relabel without changing the ids the
* runtime receives. */
levels?: readonly EffortLevel[]
/** Prefix shown before the active level on the pill — the "what is this"
* context the bare value lacked. Default "Thinking". Pass '' to hide it. */
label?: string
}
/** Thinking-budget selector pill, styled to match {@link ModelPicker}. Show
* it only when the selected model `supportsReasoning`. "Thinking" is the
* plain-English name for what was internally called "effort". */
export function EffortPicker({ value, onChange, levels = DEFAULT_EFFORT_LEVELS, label = 'Thinking' }: EffortPickerProps) {
const [open, setOpen] = useState(false)
const { containerRef, triggerProps } = usePopover(open, setOpen)
const selected = levels.find((l) => l.id === value) ?? levels[2] ?? levels[0]
return (
<div ref={containerRef} className="relative inline-flex">
<button
type="button"
{...triggerProps}
onClick={() => setOpen(!open)}
title={label ? `${label} — how hard the agent reasons before answering` : 'Reasoning effort'}
className="inline-flex min-h-[36px] items-center gap-1.5 rounded-full border border-border bg-card px-3 py-1.5 text-sm font-medium text-foreground transition hover:bg-accent/30 focus:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background"
>
<SparkleGlyph className="h-3.5 w-3.5 text-muted-foreground" />
<span>
{label ? <span className="text-muted-foreground">{label}: </span> : null}
{selected?.label}
</span>
<ChevronDown className="h-3.5 w-3.5 text-muted-foreground" />
</button>
{open && (
<div role="menu" className="absolute bottom-full left-0 z-50 mb-2 w-40 overflow-hidden rounded-xl border border-border bg-card p-1 shadow-lg">
{levels.map((l) => (
<button
key={l.id}
type="button"
role="menuitemradio"
aria-checked={l.id === value}
onClick={() => {
onChange(l.id)
setOpen(false)
}}
className={`flex min-h-[40px] w-full items-center rounded-md px-3 py-2 text-left text-sm transition ${POPOVER_OPTION_FOCUS} ${
l.id === value ? 'bg-primary/10 font-medium' : 'hover:bg-accent/30'
}`}
>
{l.label}
</button>
))}
</div>
)}
</div>
)
}