Spaces:
Runtime error
Runtime error
File size: 9,271 Bytes
077865a | 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 | import { useState } from 'react'
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { ArrowDown, ArrowUp } from 'lucide-react'
import { apiFetch } from '@/lib/api'
import { Button } from '@/components/ui/button'
import { Switch } from '@/components/ui/switch'
import { PageHeader } from '@/components/page-header'
import { FloatingBar } from '@/components/floating-bar'
import { ModelsTabs } from '@/components/models-tabs'
interface ProviderEntry {
id: number
platform: string
modelId: string
displayName: string
priority: number
enabled: boolean
quotaLabel: string
keyCount: number
}
interface Family {
family: string
dimensions: number
maxInputTokens: number | null
isDefault: boolean
providers: ProviderEntry[]
}
interface EmbeddingsData {
defaultFamily: string
families: Family[]
}
interface UsageData {
families: { family: string; requestsToday: number; tokensMonth: number }[]
}
function formatTokens(n: number): string {
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`
if (n >= 1_000) return `${(n / 1_000).toFixed(1)}K`
return String(n)
}
export default function EmbeddingsPage() {
const queryClient = useQueryClient()
// Local unsaved edits, same pattern as the chat fallback page.
const [localFamilies, setLocalFamilies] = useState<Family[] | null>(null)
const [localDefault, setLocalDefault] = useState<string | null>(null)
const { data, isLoading } = useQuery<EmbeddingsData>({
queryKey: ['embeddings'],
queryFn: () => apiFetch('/api/embeddings'),
})
const { data: usage } = useQuery<UsageData>({
queryKey: ['embeddings', 'usage'],
queryFn: () => apiFetch('/api/embeddings/usage'),
refetchInterval: 30_000,
})
const usageByFamily = new Map((usage?.families ?? []).map(u => [u.family, u]))
const saveMutation = useMutation({
mutationFn: (body: { defaultFamily?: string; providers?: { id: number; priority: number; enabled: boolean }[] }) =>
apiFetch('/api/embeddings', { method: 'PUT', body: JSON.stringify(body) }),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['embeddings'] })
setLocalFamilies(null)
setLocalDefault(null)
},
})
const families = localFamilies ?? data?.families ?? []
const defaultFamily = localDefault ?? data?.defaultFamily ?? ''
const hasChanges = localFamilies !== null || localDefault !== null
function updateProvider(familyName: string, id: number, patch: Partial<ProviderEntry>) {
setLocalFamilies(families.map(f =>
f.family === familyName
? { ...f, providers: f.providers.map(p => (p.id === id ? { ...p, ...patch } : p)) }
: f,
))
}
function moveProvider(familyName: string, index: number, dir: -1 | 1) {
setLocalFamilies(families.map(f => {
if (f.family !== familyName) return f
const list = [...f.providers]
const j = index + dir
if (j < 0 || j >= list.length) return f
;[list[index], list[j]] = [list[j], list[index]]
return { ...f, providers: list.map((p, i) => ({ ...p, priority: i + 1 })) }
}))
}
function handleSave() {
saveMutation.mutate({
...(localDefault !== null ? { defaultFamily: localDefault } : {}),
...(localFamilies !== null
? { providers: families.flatMap(f => f.providers.map(p => ({ id: p.id, priority: p.priority, enabled: p.enabled }))) }
: {}),
})
}
function discard() {
setLocalFamilies(null)
setLocalDefault(null)
}
return (
<div>
<PageHeader
title="Models"
description="Embeddings fail over within a family only: the same model served by another provider. Vectors from different models are incompatible, so the router never swaps models on you."
divider={false}
actions={<ModelsTabs />}
/>
<div className="space-y-6">
<p className="text-xs text-muted-foreground">
<code className="rounded-md bg-muted px-1.5 py-0.5 font-mono">model: "auto"</code> on{' '}
<code className="rounded-md bg-muted px-1.5 py-0.5 font-mono">POST /v1/embeddings</code> routes to the
default family. Naming a family (or a provider model id) pins that family; providers inside it are tried in order.
</p>
{isLoading ? (
<p className="text-sm text-muted-foreground">Loading…</p>
) : (
families.map(f => {
const u = usageByFamily.get(f.family)
const noKeys = f.providers.every(p => p.keyCount === 0)
return (
<section key={f.family} className={`rounded-3xl border bg-card p-5 ${noKeys ? 'opacity-60' : ''}`}>
<div className="flex items-baseline justify-between gap-4 mb-3 flex-wrap">
<div className="flex items-baseline gap-2.5 min-w-0">
<h2 className="text-sm font-medium font-mono truncate">{f.family}</h2>
<span className="text-[10px] rounded-full px-1.5 py-0.5 bg-muted text-muted-foreground tabular-nums">
{f.dimensions}d
</span>
{f.maxInputTokens && (
<span className="text-[11px] text-muted-foreground/70 tabular-nums">
{formatTokens(f.maxInputTokens)} tok max
</span>
)}
{f.family === defaultFamily ? (
<span className="text-[10px] rounded-full px-1.5 py-0.5 bg-foreground text-background font-medium">
Default · auto
</span>
) : (
<button
onClick={() => setLocalDefault(f.family)}
className="text-[11px] text-muted-foreground hover:text-foreground underline decoration-dotted underline-offset-2 transition-colors"
>
Make default
</button>
)}
</div>
<span className="text-xs text-muted-foreground tabular-nums">
{u ? <>{u.requestsToday} req today · {formatTokens(u.tokensMonth)} tok this month</> : '—'}
</span>
</div>
<div className="divide-y">
{f.providers.map((p, i) => (
<div key={p.id} className={`flex items-center gap-3 py-2 ${p.enabled ? '' : 'opacity-50'}`}>
<span className="w-5 text-center font-mono text-xs text-muted-foreground tabular-nums">{i + 1}</span>
<div className="min-w-0 flex-1">
<div className="flex items-center gap-2">
<span className="text-sm font-medium">{p.platform}</span>
<span className="truncate font-mono text-[11px] text-muted-foreground">{p.modelId}</span>
{p.keyCount === 0 && (
<span className="text-[10px] rounded-full px-1.5 py-0.5 bg-amber-600/15 text-amber-700 dark:bg-amber-400/15 dark:text-amber-400">
no key
</span>
)}
</div>
<div className="text-[11px] text-muted-foreground/70">{p.quotaLabel}</div>
</div>
{f.providers.length > 1 && (
<div className="flex gap-0.5">
<button
onClick={() => moveProvider(f.family, i, -1)}
disabled={i === 0}
aria-label="Move up"
className="rounded-md p-1 text-muted-foreground/60 hover:text-foreground disabled:opacity-25 transition-colors"
>
<ArrowUp className="size-3.5" />
</button>
<button
onClick={() => moveProvider(f.family, i, 1)}
disabled={i === f.providers.length - 1}
aria-label="Move down"
className="rounded-md p-1 text-muted-foreground/60 hover:text-foreground disabled:opacity-25 transition-colors"
>
<ArrowDown className="size-3.5" />
</button>
</div>
)}
<Switch
checked={p.enabled}
onCheckedChange={(c) => updateProvider(f.family, p.id, { enabled: c })}
/>
</div>
))}
</div>
</section>
)
})
)}
<FloatingBar show={hasChanges}>
<span className="text-xs text-muted-foreground">Unsaved changes</span>
<Button variant="outline" size="sm" onClick={discard}>Discard</Button>
<Button size="sm" onClick={handleSave} disabled={saveMutation.isPending}>
{saveMutation.isPending ? 'Saving…' : 'Save changes'}
</Button>
</FloatingBar>
</div>
</div>
)
}
|