@actuate-media/cms-admin 0.33.0 → 0.34.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/dist/__tests__/views/ai-settings.render.test.d.ts +2 -0
  2. package/dist/__tests__/views/ai-settings.render.test.d.ts.map +1 -0
  3. package/dist/__tests__/views/ai-settings.render.test.js +182 -0
  4. package/dist/__tests__/views/ai-settings.render.test.js.map +1 -0
  5. package/dist/actuate-admin.css +1 -1
  6. package/dist/views/Settings.d.ts.map +1 -1
  7. package/dist/views/Settings.js +2 -46
  8. package/dist/views/Settings.js.map +1 -1
  9. package/dist/views/settings/AISettingsTab.d.ts +6 -0
  10. package/dist/views/settings/AISettingsTab.d.ts.map +1 -0
  11. package/dist/views/settings/AISettingsTab.js +138 -0
  12. package/dist/views/settings/AISettingsTab.js.map +1 -0
  13. package/dist/views/settings/AiFeaturesCard.d.ts +12 -0
  14. package/dist/views/settings/AiFeaturesCard.d.ts.map +1 -0
  15. package/dist/views/settings/AiFeaturesCard.js +14 -0
  16. package/dist/views/settings/AiFeaturesCard.js.map +1 -0
  17. package/dist/views/settings/CapabilityBadges.d.ts +5 -0
  18. package/dist/views/settings/CapabilityBadges.d.ts.map +1 -0
  19. package/dist/views/settings/CapabilityBadges.js +26 -0
  20. package/dist/views/settings/CapabilityBadges.js.map +1 -0
  21. package/dist/views/settings/ModelSelector.d.ts +12 -0
  22. package/dist/views/settings/ModelSelector.d.ts.map +1 -0
  23. package/dist/views/settings/ModelSelector.js +29 -0
  24. package/dist/views/settings/ModelSelector.js.map +1 -0
  25. package/dist/views/settings/MonthlyUsageCard.d.ts +14 -0
  26. package/dist/views/settings/MonthlyUsageCard.d.ts.map +1 -0
  27. package/dist/views/settings/MonthlyUsageCard.js +30 -0
  28. package/dist/views/settings/MonthlyUsageCard.js.map +1 -0
  29. package/dist/views/settings/ProviderConnectionCard.d.ts +13 -0
  30. package/dist/views/settings/ProviderConnectionCard.d.ts.map +1 -0
  31. package/dist/views/settings/ProviderConnectionCard.js +48 -0
  32. package/dist/views/settings/ProviderConnectionCard.js.map +1 -0
  33. package/dist/views/settings/useAiSettings.d.ts +143 -0
  34. package/dist/views/settings/useAiSettings.d.ts.map +1 -0
  35. package/dist/views/settings/useAiSettings.js +278 -0
  36. package/dist/views/settings/useAiSettings.js.map +1 -0
  37. package/package.json +2 -2
  38. package/src/__tests__/views/ai-settings.render.test.tsx +225 -0
  39. package/src/views/Settings.tsx +2 -139
  40. package/src/views/settings/AISettingsTab.tsx +411 -0
  41. package/src/views/settings/AiFeaturesCard.tsx +49 -0
  42. package/src/views/settings/CapabilityBadges.tsx +66 -0
  43. package/src/views/settings/ModelSelector.tsx +115 -0
  44. package/src/views/settings/MonthlyUsageCard.tsx +118 -0
  45. package/src/views/settings/ProviderConnectionCard.tsx +245 -0
  46. package/src/views/settings/useAiSettings.ts +448 -0
@@ -0,0 +1,411 @@
1
+ 'use client'
2
+
3
+ import { AlertTriangle, Bot, Info, Loader2, Plus } from 'lucide-react'
4
+ import { useId, useMemo, useState } from 'react'
5
+ import { toast } from 'sonner'
6
+ import { AiFeaturesCard } from './AiFeaturesCard.js'
7
+ import { ModelSelector } from './ModelSelector.js'
8
+ import { MonthlyUsageCard } from './MonthlyUsageCard.js'
9
+ import { ProviderConnectionCard } from './ProviderConnectionCard.js'
10
+ import { SettingsSaveBar } from './components.js'
11
+ import {
12
+ useAiSettings,
13
+ type AiProviderKey,
14
+ type ProviderInput,
15
+ type UseAiSettings,
16
+ } from './useAiSettings.js'
17
+
18
+ const INPUT_CLASS =
19
+ 'w-full rounded-md border border-border bg-input-background px-3 py-2 text-base text-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none disabled:cursor-not-allowed disabled:opacity-60'
20
+ const LABEL_CLASS = 'mb-1 flex items-center gap-2 text-sm font-medium text-foreground'
21
+
22
+ const PROVIDER_OPTIONS: Array<{ value: AiProviderKey; label: string }> = [
23
+ { value: 'anthropic', label: 'Anthropic (Claude)' },
24
+ { value: 'openai', label: 'OpenAI (GPT)' },
25
+ { value: 'xai', label: 'xAI (Grok)' },
26
+ { value: 'custom', label: 'Custom (OpenAI-compatible)' },
27
+ ]
28
+
29
+ export interface AISettingsTabProps {
30
+ /** Current user's role. Only ADMIN can persist changes. */
31
+ role?: string
32
+ }
33
+
34
+ export function AISettingsTab({ role }: AISettingsTabProps) {
35
+ const canEdit = role === 'ADMIN'
36
+ const ai = useAiSettings(canEdit)
37
+
38
+ if (ai.loading) {
39
+ return (
40
+ <div className="space-y-4" aria-busy="true" aria-live="polite">
41
+ <span className="sr-only">Loading AI settings…</span>
42
+ {[0, 1, 2].map((i) => (
43
+ <div key={i} className="border-border bg-card rounded-lg border p-4">
44
+ <div className="bg-muted mb-4 h-4 w-40 animate-pulse rounded" />
45
+ <div className="space-y-3">
46
+ <div className="bg-muted h-9 w-full animate-pulse rounded" />
47
+ <div className="bg-muted h-9 w-full animate-pulse rounded" />
48
+ </div>
49
+ </div>
50
+ ))}
51
+ </div>
52
+ )
53
+ }
54
+
55
+ return (
56
+ <div className="space-y-4">
57
+ {ai.loadError && (
58
+ <div
59
+ role="alert"
60
+ className="border-destructive/30 bg-destructive/10 text-destructive flex items-center gap-3 rounded-lg border p-3 text-sm"
61
+ >
62
+ <AlertTriangle size={16} className="shrink-0" aria-hidden="true" />
63
+ <span className="flex-1">{ai.loadError}</span>
64
+ <button
65
+ type="button"
66
+ onClick={ai.refetch}
67
+ className="border-destructive/40 rounded-md border px-3 py-1 transition-colors hover:opacity-80"
68
+ >
69
+ Retry
70
+ </button>
71
+ </div>
72
+ )}
73
+
74
+ {!canEdit && (
75
+ <div className="border-border bg-muted text-muted-foreground flex items-center gap-2 rounded-lg border p-3 text-sm">
76
+ <Info size={16} className="shrink-0" aria-hidden="true" />
77
+ You have read-only access to AI settings. Only administrators can make changes.
78
+ </div>
79
+ )}
80
+
81
+ <AssistantCard ai={ai} canEdit={canEdit} />
82
+ <ProviderConnectionsSection ai={ai} canEdit={canEdit} />
83
+ <AiFeaturesCard features={ai.form.features} canEdit={canEdit} onToggle={ai.toggleFeature} />
84
+ <MonthlyUsageCard
85
+ usage={ai.usage}
86
+ monthlyTokenLimit={ai.form.monthlyTokenLimit}
87
+ onChangeLimit={ai.setMonthlyTokenLimit}
88
+ budgetError={ai.budgetError}
89
+ canEdit={canEdit}
90
+ />
91
+
92
+ {ai.saveState === 'error' && ai.saveError && (
93
+ <div
94
+ role="alert"
95
+ className="border-destructive/30 bg-destructive/10 text-destructive flex items-center gap-3 rounded-lg border p-3 text-sm"
96
+ >
97
+ <AlertTriangle size={16} className="shrink-0" aria-hidden="true" />
98
+ <span className="flex-1">{ai.saveError}</span>
99
+ </div>
100
+ )}
101
+
102
+ <SettingsSaveBar
103
+ dirty={ai.dirty}
104
+ canEdit={canEdit}
105
+ hasErrors={Boolean(ai.budgetError)}
106
+ saveState={ai.saveState}
107
+ onSave={ai.save}
108
+ onReset={ai.reset}
109
+ />
110
+ </div>
111
+ )
112
+ }
113
+
114
+ // ---------------------------------------------------------------------------
115
+ // AI Assistant — default provider + default model
116
+ // ---------------------------------------------------------------------------
117
+
118
+ function AssistantCard({ ai, canEdit }: { ai: UseAiSettings; canEdit: boolean }) {
119
+ const headingId = useId()
120
+ const providerSelectId = useId()
121
+ const [refreshing, setRefreshing] = useState(false)
122
+
123
+ const defaultProvider = ai.providers.find((p) => p.id === ai.form.defaultProviderId)
124
+ const modelsForDefault = useMemo(() => {
125
+ if (!defaultProvider) return ai.models
126
+ return ai.models.filter((m) => m.provider === defaultProvider.provider)
127
+ }, [ai.models, defaultProvider])
128
+
129
+ async function handleRefresh() {
130
+ if (!defaultProvider) return
131
+ setRefreshing(true)
132
+ try {
133
+ const res = await ai.syncModels(defaultProvider.id)
134
+ if (!res.ok) toast.error(res.error ?? 'Failed to refresh models.')
135
+ else if (res.fromFallback)
136
+ toast.warning(res.warning ?? 'Showing the curated model list (live listing unavailable).')
137
+ else toast.success('Model list refreshed.')
138
+ } finally {
139
+ setRefreshing(false)
140
+ }
141
+ }
142
+
143
+ return (
144
+ <section aria-labelledby={headingId} className="border-border bg-card rounded-lg border p-4">
145
+ <div className="mb-4 flex items-center gap-2">
146
+ <span className="bg-brand/10 text-brand flex h-9 w-9 items-center justify-center rounded-lg">
147
+ <Bot size={18} aria-hidden="true" />
148
+ </span>
149
+ <div>
150
+ <h3 id={headingId} className="text-foreground text-base font-medium">
151
+ AI Assistant
152
+ </h3>
153
+ <p className="text-muted-foreground text-sm">
154
+ The default provider and model used across AI features.
155
+ </p>
156
+ </div>
157
+ </div>
158
+
159
+ {ai.providers.length === 0 ? (
160
+ <div className="border-border bg-muted/40 text-muted-foreground rounded-md border border-dashed p-4 text-sm">
161
+ No AI providers are connected yet. Add one below to enable AI features.
162
+ </div>
163
+ ) : (
164
+ <div className="space-y-4">
165
+ <div>
166
+ <label htmlFor={providerSelectId} className={LABEL_CLASS}>
167
+ Default provider
168
+ </label>
169
+ <select
170
+ id={providerSelectId}
171
+ value={ai.form.defaultProviderId}
172
+ disabled={!canEdit}
173
+ onChange={(e) => ai.setDefaultProvider(e.target.value)}
174
+ className={INPUT_CLASS}
175
+ >
176
+ <option value="">Select a provider…</option>
177
+ {ai.providers.map((p) => (
178
+ <option key={p.id} value={p.id}>
179
+ {p.displayName}
180
+ </option>
181
+ ))}
182
+ </select>
183
+ </div>
184
+
185
+ <ModelSelector
186
+ models={modelsForDefault}
187
+ value={ai.form.defaultModelId}
188
+ onChange={ai.setDefaultModel}
189
+ onRefresh={handleRefresh}
190
+ refreshing={refreshing}
191
+ disabled={!canEdit || !defaultProvider}
192
+ noModelsHint={
193
+ defaultProvider
194
+ ? 'No models cached yet. Use “Refresh models” to load them.'
195
+ : 'Select a default provider to choose a model.'
196
+ }
197
+ />
198
+ </div>
199
+ )}
200
+ </section>
201
+ )
202
+ }
203
+
204
+ // ---------------------------------------------------------------------------
205
+ // Provider connections (manage multiple)
206
+ // ---------------------------------------------------------------------------
207
+
208
+ function ProviderConnectionsSection({ ai, canEdit }: { ai: UseAiSettings; canEdit: boolean }) {
209
+ const headingId = useId()
210
+ const [adding, setAdding] = useState(false)
211
+
212
+ return (
213
+ <section aria-labelledby={headingId} className="space-y-3">
214
+ <div className="flex items-center justify-between">
215
+ <h3 id={headingId} className="text-foreground text-base font-medium">
216
+ Provider connections
217
+ </h3>
218
+ {canEdit && !adding && (
219
+ <button
220
+ type="button"
221
+ onClick={() => setAdding(true)}
222
+ className="border-border text-foreground hover:bg-accent inline-flex items-center gap-1.5 rounded-md border px-3 py-1.5 text-sm transition-colors"
223
+ >
224
+ <Plus size={14} aria-hidden="true" />
225
+ Add provider
226
+ </button>
227
+ )}
228
+ </div>
229
+
230
+ {adding && (
231
+ <AddProviderForm
232
+ ai={ai}
233
+ onDone={() => setAdding(false)}
234
+ onCancel={() => setAdding(false)}
235
+ />
236
+ )}
237
+
238
+ {ai.providers.map((p) => (
239
+ <ProviderConnectionCard
240
+ key={p.id}
241
+ provider={p}
242
+ canEdit={canEdit}
243
+ busy={ai.busyProviderId === p.id}
244
+ onTest={async () => {
245
+ const res = await ai.testProvider(p.id)
246
+ if (!res.ok) toast.error(res.error ?? res.message ?? 'Connection test failed.')
247
+ else toast.success(res.message ?? 'Connection successful.')
248
+ }}
249
+ onSyncModels={async () => {
250
+ const res = await ai.syncModels(p.id)
251
+ if (!res.ok) toast.error(res.error ?? 'Failed to refresh models.')
252
+ else if (res.fromFallback)
253
+ toast.warning(res.warning ?? 'Showing the curated model list.')
254
+ else toast.success('Model list refreshed.')
255
+ }}
256
+ onUpdateKey={async (apiKey) => {
257
+ const res = await ai.updateProvider(p.id, { apiKey })
258
+ if (!res.ok) toast.error(res.error ?? 'Failed to update key.')
259
+ else toast.success('API key updated.')
260
+ }}
261
+ onUpdateBaseUrl={async (baseUrl) => {
262
+ const res = await ai.updateProvider(p.id, { baseUrl })
263
+ if (!res.ok) toast.error(res.error ?? 'Failed to update base URL.')
264
+ else toast.success('Base URL updated.')
265
+ }}
266
+ onDisconnect={async () => {
267
+ const res = await ai.deleteProvider(p.id)
268
+ if (!res.ok) toast.error(res.error ?? 'Failed to disconnect.')
269
+ else toast.success(`${p.displayName} disconnected.`)
270
+ }}
271
+ />
272
+ ))}
273
+
274
+ {ai.providers.length === 0 && !adding && (
275
+ <p className="text-muted-foreground text-sm">No providers connected.</p>
276
+ )}
277
+ </section>
278
+ )
279
+ }
280
+
281
+ function AddProviderForm({
282
+ ai,
283
+ onDone,
284
+ onCancel,
285
+ }: {
286
+ ai: UseAiSettings
287
+ onDone: () => void
288
+ onCancel: () => void
289
+ }) {
290
+ const providerId = useId()
291
+ const nameId = useId()
292
+ const keyId = useId()
293
+ const baseUrlId = useId()
294
+ const [provider, setProvider] = useState<AiProviderKey>('anthropic')
295
+ const [displayName, setDisplayName] = useState('')
296
+ const [apiKey, setApiKey] = useState('')
297
+ const [baseUrl, setBaseUrl] = useState('')
298
+ const [submitting, setSubmitting] = useState(false)
299
+
300
+ const needsBaseUrl = provider === 'custom'
301
+ const canSubmit = !submitting && (!needsBaseUrl || baseUrl.trim() !== '')
302
+
303
+ async function submit() {
304
+ setSubmitting(true)
305
+ try {
306
+ const input: ProviderInput = {
307
+ provider,
308
+ displayName: displayName.trim() || undefined,
309
+ apiKey: apiKey.trim() || undefined,
310
+ baseUrl: needsBaseUrl ? baseUrl.trim() : undefined,
311
+ test: true,
312
+ }
313
+ const res = await ai.createProvider(input)
314
+ if (!res.ok) {
315
+ toast.error(res.error ?? 'Failed to add provider.')
316
+ return
317
+ }
318
+ toast.success('Provider connected.')
319
+ onDone()
320
+ } finally {
321
+ setSubmitting(false)
322
+ }
323
+ }
324
+
325
+ return (
326
+ <div className="border-border bg-card rounded-lg border p-4">
327
+ <h4 className="text-foreground mb-3 text-base font-medium">Add a provider</h4>
328
+ <div className="space-y-3">
329
+ <div>
330
+ <label htmlFor={providerId} className={LABEL_CLASS}>
331
+ Provider
332
+ </label>
333
+ <select
334
+ id={providerId}
335
+ value={provider}
336
+ onChange={(e) => setProvider(e.target.value as AiProviderKey)}
337
+ className={INPUT_CLASS}
338
+ >
339
+ {PROVIDER_OPTIONS.map((o) => (
340
+ <option key={o.value} value={o.value}>
341
+ {o.label}
342
+ </option>
343
+ ))}
344
+ </select>
345
+ </div>
346
+ <div>
347
+ <label htmlFor={nameId} className={LABEL_CLASS}>
348
+ Display name (optional)
349
+ </label>
350
+ <input
351
+ id={nameId}
352
+ type="text"
353
+ value={displayName}
354
+ placeholder="e.g. Production Anthropic"
355
+ onChange={(e) => setDisplayName(e.target.value)}
356
+ className={INPUT_CLASS}
357
+ />
358
+ </div>
359
+ {needsBaseUrl && (
360
+ <div>
361
+ <label htmlFor={baseUrlId} className={LABEL_CLASS}>
362
+ Base URL
363
+ </label>
364
+ <input
365
+ id={baseUrlId}
366
+ type="url"
367
+ value={baseUrl}
368
+ placeholder="https://api.example.com/v1"
369
+ onChange={(e) => setBaseUrl(e.target.value)}
370
+ className={INPUT_CLASS}
371
+ />
372
+ </div>
373
+ )}
374
+ <div>
375
+ <label htmlFor={keyId} className={LABEL_CLASS}>
376
+ API key
377
+ </label>
378
+ <input
379
+ id={keyId}
380
+ type="password"
381
+ autoComplete="off"
382
+ spellCheck={false}
383
+ value={apiKey}
384
+ placeholder="Paste API key (or leave blank if set via environment)"
385
+ onChange={(e) => setApiKey(e.target.value)}
386
+ className={INPUT_CLASS}
387
+ />
388
+ </div>
389
+ <div className="flex items-center justify-end gap-3 pt-1">
390
+ <button
391
+ type="button"
392
+ onClick={onCancel}
393
+ disabled={submitting}
394
+ className="text-muted-foreground hover:text-foreground rounded-md px-3 py-2 text-sm transition-colors disabled:opacity-50"
395
+ >
396
+ Cancel
397
+ </button>
398
+ <button
399
+ type="button"
400
+ onClick={submit}
401
+ disabled={!canSubmit}
402
+ className="bg-brand text-brand-foreground inline-flex items-center gap-2 rounded-md px-4 py-2 text-sm font-medium transition-opacity hover:opacity-90 disabled:cursor-not-allowed disabled:opacity-50"
403
+ >
404
+ {submitting && <Loader2 size={14} className="animate-spin" aria-hidden="true" />}
405
+ Connect &amp; test
406
+ </button>
407
+ </div>
408
+ </div>
409
+ </div>
410
+ )
411
+ }
@@ -0,0 +1,49 @@
1
+ 'use client'
2
+
3
+ import { useId } from 'react'
4
+ import { SettingsToggleRow } from './components.js'
5
+ import type { AiFeatureView } from './useAiSettings.js'
6
+
7
+ /**
8
+ * Global AI feature toggles. Each switch persists (via the Save bar) to the
9
+ * shared `ai.features.*` flags. Enforcement across modules lands in a later
10
+ * phase; here we surface and store the operator's intent.
11
+ */
12
+ export function AiFeaturesCard({
13
+ features,
14
+ canEdit,
15
+ onToggle,
16
+ }: {
17
+ features: AiFeatureView[]
18
+ canEdit: boolean
19
+ onToggle: (key: string, enabled: boolean) => void
20
+ }) {
21
+ const headingId = useId()
22
+ return (
23
+ <section aria-labelledby={headingId} className="border-border bg-card rounded-lg border p-4">
24
+ <h3 id={headingId} className="text-foreground mb-1 text-base font-medium">
25
+ AI Features
26
+ </h3>
27
+ <p className="text-muted-foreground mb-4 text-sm">
28
+ Turn individual AI-powered features on or off across the CMS.
29
+ </p>
30
+
31
+ {features.length === 0 ? (
32
+ <p className="text-muted-foreground text-sm">No AI features are available.</p>
33
+ ) : (
34
+ <div className="space-y-4">
35
+ {features.map((f) => (
36
+ <SettingsToggleRow
37
+ key={f.key}
38
+ label={f.label}
39
+ description={f.description}
40
+ checked={f.enabled}
41
+ disabled={!canEdit}
42
+ onChange={(v) => onToggle(f.key, v)}
43
+ />
44
+ ))}
45
+ </div>
46
+ )}
47
+ </section>
48
+ )
49
+ }
@@ -0,0 +1,66 @@
1
+ 'use client'
2
+
3
+ import {
4
+ Binary,
5
+ Braces,
6
+ Brain,
7
+ Eye,
8
+ Image as ImageIcon,
9
+ type LucideIcon,
10
+ Type,
11
+ Wrench,
12
+ Zap,
13
+ } from 'lucide-react'
14
+
15
+ /**
16
+ * Renders a model's capabilities as labelled badges. Text + icon (never
17
+ * color-only) so the information is accessible to color-blind users and screen
18
+ * readers.
19
+ */
20
+
21
+ interface CapabilityMeta {
22
+ label: string
23
+ icon: LucideIcon
24
+ }
25
+
26
+ // Ordered for stable rendering. Keys match `AIModelCapabilities`.
27
+ const CAPABILITY_META: Record<string, CapabilityMeta> = {
28
+ text: { label: 'Text', icon: Type },
29
+ vision: { label: 'Vision', icon: Eye },
30
+ imageGeneration: { label: 'Image gen', icon: ImageIcon },
31
+ embeddings: { label: 'Embeddings', icon: Binary },
32
+ reasoning: { label: 'Reasoning', icon: Brain },
33
+ toolCalling: { label: 'Tools', icon: Wrench },
34
+ structuredOutput: { label: 'Structured', icon: Braces },
35
+ longContext: { label: 'Long context', icon: Zap },
36
+ }
37
+
38
+ export function CapabilityBadges({
39
+ capabilities,
40
+ className,
41
+ }: {
42
+ capabilities: Record<string, boolean>
43
+ className?: string
44
+ }) {
45
+ const active = Object.keys(CAPABILITY_META).filter((k) => capabilities?.[k])
46
+ if (active.length === 0) {
47
+ return <span className="text-muted-foreground text-sm">No capability data</span>
48
+ }
49
+ return (
50
+ <ul className={`flex flex-wrap gap-1.5 ${className ?? ''}`} aria-label="Model capabilities">
51
+ {active.map((key) => {
52
+ const meta = CAPABILITY_META[key]!
53
+ const Icon = meta.icon
54
+ return (
55
+ <li
56
+ key={key}
57
+ className="border-border bg-muted text-muted-foreground inline-flex items-center gap-1 rounded-md border px-1.5 py-0.5 text-xs"
58
+ >
59
+ <Icon size={11} aria-hidden="true" />
60
+ {meta.label}
61
+ </li>
62
+ )
63
+ })}
64
+ </ul>
65
+ )
66
+ }
@@ -0,0 +1,115 @@
1
+ 'use client'
2
+
3
+ import { AlertTriangle, Loader2, RefreshCw } from 'lucide-react'
4
+ import { useId } from 'react'
5
+ import { CapabilityBadges } from './CapabilityBadges.js'
6
+ import type { AiModelView } from './useAiSettings.js'
7
+
8
+ const INPUT_CLASS =
9
+ 'w-full rounded-md border border-border bg-input-background px-3 py-2 text-base text-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none disabled:cursor-not-allowed disabled:opacity-60'
10
+ const LABEL_CLASS = 'mb-1 flex items-center gap-2 text-sm font-medium text-foreground'
11
+
12
+ function priceLabel(model: AiModelView): string | null {
13
+ const p = model.pricing
14
+ if (!p) return null
15
+ if (typeof p.inputPerMillion === 'number' && typeof p.outputPerMillion === 'number') {
16
+ return `$${p.inputPerMillion}/$${p.outputPerMillion} per 1M tokens`
17
+ }
18
+ if (typeof p.inputPerMillion === 'number') return `$${p.inputPerMillion} per 1M tokens`
19
+ if (typeof p.image === 'number') return `$${p.image} per image`
20
+ return null
21
+ }
22
+
23
+ export function ModelSelector({
24
+ models,
25
+ value,
26
+ onChange,
27
+ onRefresh,
28
+ refreshing,
29
+ disabled,
30
+ noModelsHint,
31
+ }: {
32
+ models: AiModelView[]
33
+ value: string
34
+ onChange: (id: string) => void
35
+ onRefresh: () => void
36
+ refreshing: boolean
37
+ disabled: boolean
38
+ /** Shown when there are no models to choose from. */
39
+ noModelsHint?: string
40
+ }) {
41
+ const selectId = useId()
42
+ const selected = models.find((m) => m.id === value || m.providerModelId === value)
43
+ // The configured model is no longer in the catalog — keep the choice, warn.
44
+ const selectedUnavailable = Boolean(value) && !selected
45
+ const price = selected ? priceLabel(selected) : null
46
+
47
+ return (
48
+ <div>
49
+ <label htmlFor={selectId} className={LABEL_CLASS}>
50
+ Default model
51
+ </label>
52
+ <div className="flex items-center gap-2">
53
+ <select
54
+ id={selectId}
55
+ value={value}
56
+ disabled={disabled}
57
+ onChange={(e) => onChange(e.target.value)}
58
+ className={INPUT_CLASS}
59
+ >
60
+ <option value="">Select a model…</option>
61
+ {selectedUnavailable && <option value={value}>{value} (unavailable)</option>}
62
+ {models.map((m) => (
63
+ <option key={m.id} value={m.id} disabled={m.status === 'unavailable'}>
64
+ {m.displayName}
65
+ {m.status === 'deprecated' ? ' (deprecated)' : ''}
66
+ {m.status === 'unavailable' ? ' (unavailable)' : ''}
67
+ </option>
68
+ ))}
69
+ </select>
70
+ <button
71
+ type="button"
72
+ onClick={onRefresh}
73
+ disabled={disabled || refreshing}
74
+ aria-label="Refresh models"
75
+ className="border-border text-foreground hover:bg-accent inline-flex shrink-0 items-center gap-2 rounded-md border px-3 py-2 text-sm transition-colors disabled:cursor-not-allowed disabled:opacity-60"
76
+ >
77
+ {refreshing ? (
78
+ <Loader2 size={16} className="animate-spin" aria-hidden="true" />
79
+ ) : (
80
+ <RefreshCw size={16} aria-hidden="true" />
81
+ )}
82
+ <span className="sr-only sm:not-sr-only">Refresh models</span>
83
+ </button>
84
+ </div>
85
+
86
+ {models.length === 0 && (
87
+ <p className="text-muted-foreground mt-1 text-sm">
88
+ {noModelsHint ?? 'No models available yet. Connect a provider and refresh.'}
89
+ </p>
90
+ )}
91
+
92
+ {selectedUnavailable && (
93
+ <p className="text-warning mt-2 flex items-start gap-1.5 text-sm" role="status">
94
+ <AlertTriangle size={14} className="mt-0.5 shrink-0" aria-hidden="true" />
95
+ The selected model is no longer offered by the provider. Pick another or refresh the list.
96
+ </p>
97
+ )}
98
+
99
+ {selected && (
100
+ <div className="mt-2 space-y-1.5">
101
+ <CapabilityBadges capabilities={selected.capabilities} />
102
+ <div className="text-muted-foreground flex flex-wrap gap-x-4 gap-y-1 text-sm">
103
+ {typeof selected.contextWindow === 'number' && (
104
+ <span>{selected.contextWindow.toLocaleString()} token context</span>
105
+ )}
106
+ {price && <span>{price}</span>}
107
+ {selected.status === 'deprecated' && (
108
+ <span className="text-warning">Deprecated by provider</span>
109
+ )}
110
+ </div>
111
+ </div>
112
+ )}
113
+ </div>
114
+ )
115
+ }