mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 15:14:10 +00:00
feat: configure embedding servers and transcript agents
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
import { useState } from 'react'
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { updateEmbeddingServerSettingsRequestSchema } from '@shumai/dtos'
|
||||
import { Loader2 } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
import { client } from '@/ui/api/client'
|
||||
import { Badge } from '@/ui/components/ui/badge'
|
||||
import { Button } from '@/ui/components/ui/button'
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardFooter,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/ui/components/ui/card'
|
||||
import { Input } from '@/ui/components/ui/input'
|
||||
import { Label } from '@/ui/components/ui/label'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
|
||||
export function EmbeddingServerSettingsCard({ teamId }: { teamId: string }) {
|
||||
const queryClient = useQueryClient()
|
||||
const queryKey = ['teams', teamId, 'embedding-server-settings']
|
||||
const endpoint = client.api.teams[':teamId']['embedding-server-settings']
|
||||
const [draft, setDraft] = useState<string | null>(null)
|
||||
|
||||
const {
|
||||
data: settings,
|
||||
isLoading,
|
||||
isError,
|
||||
} = useQuery({
|
||||
queryKey,
|
||||
queryFn: async () => {
|
||||
const response = await endpoint.$get({ param: { teamId } })
|
||||
if (!response.ok) throw new Error(m.failed_load_settings())
|
||||
return response.json()
|
||||
},
|
||||
})
|
||||
|
||||
const isReadOnly = Boolean(settings?.isEnvConfigured)
|
||||
const url = isReadOnly ? (settings?.url ?? '') : (draft ?? settings?.url ?? '')
|
||||
const validUrl =
|
||||
isReadOnly || updateEmbeddingServerSettingsRequestSchema.safeParse({ url }).success
|
||||
|
||||
const save = useMutation({
|
||||
mutationFn: async () => {
|
||||
const response = await endpoint.$put({ param: { teamId }, json: { url: url.trim() } })
|
||||
if (!response.ok) throw new Error(m.failed_update_settings())
|
||||
return response.json()
|
||||
},
|
||||
onSuccess: (updated) => {
|
||||
queryClient.setQueryData(queryKey, updated)
|
||||
setDraft(null)
|
||||
test.reset()
|
||||
void queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'settings'] })
|
||||
void queryClient.invalidateQueries({ queryKey: ['signup-info'] })
|
||||
void queryClient.invalidateQueries({ queryKey: ['transcription-languages'] })
|
||||
toast.success(m.settings_updated())
|
||||
},
|
||||
onError: () => toast.error(m.failed_update_settings()),
|
||||
})
|
||||
|
||||
const test = useMutation({
|
||||
mutationFn: async () => {
|
||||
const response = await endpoint.test.$post({
|
||||
param: { teamId },
|
||||
json: isReadOnly ? {} : { url: url.trim() },
|
||||
})
|
||||
if (!response.ok) throw new Error(m.embedding_server_connection_failed())
|
||||
return response.json()
|
||||
},
|
||||
})
|
||||
const isBusy = save.isPending || test.isPending
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex flex-wrap items-center justify-between gap-2">
|
||||
<CardTitle>{m.embedding_server_settings()}</CardTitle>
|
||||
{isReadOnly && <Badge variant="secondary">{m.embedding_server_env_configured()}</Badge>}
|
||||
</div>
|
||||
<CardDescription>{m.embedding_server_description()}</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-2">
|
||||
{isLoading ? (
|
||||
<Loader2 className="mx-auto my-6 size-6 animate-spin text-muted-foreground" />
|
||||
) : isError ? (
|
||||
<p role="alert" className="text-sm text-destructive">
|
||||
{m.failed_load_settings()}
|
||||
</p>
|
||||
) : (
|
||||
<>
|
||||
<Label htmlFor="embedding-server-url">{m.embedding_server_url()}</Label>
|
||||
<Input
|
||||
id="embedding-server-url"
|
||||
type="url"
|
||||
value={url}
|
||||
placeholder="http://embedding-server:9379/v1"
|
||||
readOnly={isReadOnly}
|
||||
disabled={isBusy}
|
||||
aria-invalid={!validUrl}
|
||||
aria-describedby={validUrl ? undefined : 'embedding-server-url-error'}
|
||||
onChange={(event) => {
|
||||
setDraft(event.target.value)
|
||||
test.reset()
|
||||
}}
|
||||
/>
|
||||
{!validUrl && (
|
||||
<p id="embedding-server-url-error" className="text-sm text-destructive">
|
||||
{m.embedding_server_invalid_url()}
|
||||
</p>
|
||||
)}
|
||||
{test.data?.ok && (
|
||||
<p role="status" className="text-sm">
|
||||
{m.embedding_server_connection_success()}
|
||||
</p>
|
||||
)}
|
||||
{(test.data?.ok === false || test.isError) && (
|
||||
<p role="alert" className="text-sm text-destructive">
|
||||
{m.embedding_server_connection_failed()}
|
||||
{test.data?.error && <> — {test.data.error}</>}
|
||||
</p>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</CardContent>
|
||||
<CardFooter className="flex justify-between border-t border-border pt-4">
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => test.mutate()}
|
||||
disabled={!settings || isBusy || !validUrl || !url.trim()}
|
||||
>
|
||||
{test.isPending && <Loader2 className="size-4 animate-spin" />}
|
||||
{m.embedding_server_test_connection()}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
onClick={() => save.mutate()}
|
||||
disabled={!settings || isReadOnly || isBusy || !validUrl || url === (settings.url ?? '')}
|
||||
>
|
||||
{save.isPending && <Loader2 className="size-4 animate-spin" />}
|
||||
{m.save()}
|
||||
</Button>
|
||||
</CardFooter>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user