-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend): support edit chat engine
- Loading branch information
Showing
9 changed files
with
365 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
frontend/app/src/components/chat-engine/edit-is-default-form.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use client'; | ||
|
||
import { type ChatEngine, updateChatEngine } from '@/api/chat-engines'; | ||
import { useManagedDialog } from '@/components/managed-dialog'; | ||
import { Button } from '@/components/ui/button'; | ||
import { DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog'; | ||
import { Form, FormControl, FormDescription, FormField, FormItem, FormMessage } from '@/components/ui/form'; | ||
import { Switch } from '@/components/ui/switch'; | ||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { useRouter } from 'next/navigation'; | ||
import { useTransition } from 'react'; | ||
import { useForm } from 'react-hook-form'; | ||
import { toast } from 'sonner'; | ||
import { z } from 'zod'; | ||
|
||
const schema = z.object({ | ||
is_default: z.literal(true), | ||
}); | ||
|
||
export interface EditIsDefaultFormProps { | ||
chatEngine: ChatEngine; | ||
} | ||
|
||
export function EditIsDefaultForm ({ chatEngine }: EditIsDefaultFormProps) { | ||
const router = useRouter(); | ||
const [transitioning, startTransition] = useTransition(); | ||
const { setOpen } = useManagedDialog(); | ||
|
||
const form = useForm<{ is_default: boolean }>({ | ||
resolver: zodResolver(schema), | ||
disabled: chatEngine.is_default, | ||
defaultValues: { | ||
is_default: chatEngine.is_default, | ||
}, | ||
}); | ||
|
||
const handleSubmit = form.handleSubmit(async (data) => { | ||
await updateChatEngine(chatEngine.id, data); | ||
startTransition(() => { | ||
router.refresh(); | ||
}); | ||
toast('ChatEngine\'s is_default successfully updated.'); | ||
setOpen(false); | ||
}); | ||
|
||
return ( | ||
<> | ||
<DialogHeader> | ||
<DialogTitle>Update chat engine's is_default</DialogTitle> | ||
</DialogHeader> | ||
<Form {...form}> | ||
<form id="update-form" className="space-y-4" onSubmit={handleSubmit}> | ||
<FormField | ||
name="is_default" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormControl> | ||
<Switch name={field.name} onBlur={field.onBlur} checked={field.value} onCheckedChange={field.onChange} disabled={field.disabled}></Switch> | ||
</FormControl> | ||
{chatEngine.is_default && <FormDescription> | ||
Cannot unset default chat engine. Please set another chat engine to default. | ||
</FormDescription>} | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
</form> | ||
</Form> | ||
<DialogFooter> | ||
<Button type="submit" form="update-form" disabled={form.formState.disabled || form.formState.isSubmitting || transitioning}> | ||
Update | ||
</Button> | ||
</DialogFooter> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
'use client'; | ||
|
||
import { type ChatEngine, updateChatEngine } from '@/api/chat-engines'; | ||
import { listLlms } from '@/api/llms'; | ||
import { LlmInfo } from '@/components/llm/LlmInfo'; | ||
import { useManagedDialog } from '@/components/managed-dialog'; | ||
import { Button } from '@/components/ui/button'; | ||
import { DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog'; | ||
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'; | ||
import { Select, SelectContent, SelectItem, SelectTrigger } from '@/components/ui/select'; | ||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { useRouter } from 'next/navigation'; | ||
import { useTransition } from 'react'; | ||
import { useForm } from 'react-hook-form'; | ||
import { toast } from 'sonner'; | ||
import useSWR from 'swr'; | ||
import { z } from 'zod'; | ||
|
||
const schema = z.object({ | ||
llm: z.number(), | ||
}); | ||
|
||
export interface EditLlmFormProps { | ||
chatEngine: ChatEngine; | ||
type: 'llm' | 'fast_llm'; | ||
} | ||
|
||
export function EditLlmForm ({ type, chatEngine }: EditLlmFormProps) { | ||
const router = useRouter(); | ||
const [transitioning, startTransition] = useTransition(); | ||
const { setOpen } = useManagedDialog(); | ||
const { data: llms } = useSWR('api.llms.list-all', () => listLlms({ size: 100 })); | ||
|
||
const form = useForm<{ llm: number }>({ | ||
resolver: zodResolver(schema), | ||
defaultValues: { | ||
llm: (type === 'llm' ? chatEngine.llm_id : chatEngine.fast_llm_id) ?? undefined, | ||
}, | ||
}); | ||
|
||
const handleSubmit = form.handleSubmit(async (data) => { | ||
await updateChatEngine(chatEngine.id, { | ||
[type === 'llm' ? 'llm_id' : 'fast_llm_id']: data.llm, | ||
}); | ||
startTransition(() => { | ||
router.refresh(); | ||
}); | ||
toast(`ChatEngine's ${type} successfully updated.`); | ||
setOpen(false); | ||
}); | ||
|
||
return ( | ||
<> | ||
<DialogHeader> | ||
<DialogTitle>Update chat engine's {type}</DialogTitle> | ||
</DialogHeader> | ||
<Form {...form}> | ||
<form id="update-form" className="space-y-4" onSubmit={handleSubmit}> | ||
<FormField | ||
name="llm" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>{type === 'llm' ? 'LLM' : 'Fast LLM'}</FormLabel> | ||
<FormControl> | ||
<Select | ||
name={field.name} | ||
disabled={!llms || field.disabled} | ||
value={String(field.value)} | ||
onValueChange={value => field.onChange(parseInt(value))} | ||
> | ||
<SelectTrigger> | ||
<span> | ||
<LlmInfo reverse id={field.value} /> | ||
</span> | ||
</SelectTrigger> | ||
<SelectContent> | ||
{llms?.items.map(llm => ( | ||
<SelectItem value={String(llm.id)} key={llm.id}> | ||
{llm.name} | ||
</SelectItem> | ||
))} | ||
</SelectContent> | ||
</Select> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
</form> | ||
</Form> | ||
<DialogFooter> | ||
<Button type="submit" form="update-form" disabled={form.formState.disabled || form.formState.isSubmitting || transitioning}> | ||
Update | ||
</Button> | ||
</DialogFooter> | ||
</> | ||
); | ||
} |
73 changes: 73 additions & 0 deletions
73
frontend/app/src/components/chat-engine/edit-name-form.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use client'; | ||
|
||
import { type ChatEngine, updateChatEngine } from '@/api/chat-engines'; | ||
import { useManagedDialog } from '@/components/managed-dialog'; | ||
import { Button } from '@/components/ui/button'; | ||
import { DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog'; | ||
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'; | ||
import { Input } from '@/components/ui/input'; | ||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { useRouter } from 'next/navigation'; | ||
import { useTransition } from 'react'; | ||
import { useForm } from 'react-hook-form'; | ||
import { toast } from 'sonner'; | ||
import { z } from 'zod'; | ||
|
||
const schema = z.object({ | ||
name: z.string().min(1), | ||
}); | ||
|
||
export interface EditNameFormProps { | ||
chatEngine: ChatEngine; | ||
} | ||
|
||
export function EditNameForm ({ chatEngine }: EditNameFormProps) { | ||
const router = useRouter(); | ||
const [transitioning, startTransition] = useTransition(); | ||
const { setOpen } = useManagedDialog(); | ||
|
||
const form = useForm<{ name: string }>({ | ||
resolver: zodResolver(schema), | ||
defaultValues: { | ||
name: chatEngine.name, | ||
}, | ||
}); | ||
|
||
const handleSubmit = form.handleSubmit(async (data) => { | ||
await updateChatEngine(chatEngine.id, data); | ||
startTransition(() => { | ||
router.refresh(); | ||
}); | ||
toast('ChatEngine\'s name successfully updated.'); | ||
setOpen(false); | ||
}); | ||
|
||
return ( | ||
<> | ||
<DialogHeader> | ||
<DialogTitle>Update chat engine's name</DialogTitle> | ||
</DialogHeader> | ||
<Form {...form}> | ||
<form id="update-form" className="space-y-4" onSubmit={handleSubmit}> | ||
<FormField | ||
name="name" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Name</FormLabel> | ||
<FormControl> | ||
<Input {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
</form> | ||
</Form> | ||
<DialogFooter> | ||
<Button type="submit" form='update-form' disabled={form.formState.disabled || form.formState.isSubmitting || transitioning}> | ||
Update | ||
</Button> | ||
</DialogFooter> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use client'; | ||
|
||
import { useLlm } from '@/components/llm/hooks'; | ||
import { Badge, badgeVariants } from '@/components/ui/badge'; | ||
import { cn } from '@/lib/utils'; | ||
import { Loader2Icon } from 'lucide-react'; | ||
import Link from 'next/link'; | ||
|
||
export function LlmInfo ({ reverse = false, id }: { reverse?: boolean, id: number | undefined | null }) { | ||
const { llm, isLoading } = useLlm(id); | ||
|
||
if (isLoading) { | ||
return <Loader2Icon className='size-4 animate-spin repeat-infinite'/> | ||
} | ||
|
||
if (!llm) { | ||
return <span>N/A</span>; | ||
} | ||
|
||
return ( | ||
<span className={cn('flex gap-1 items-center', reverse && 'flex-row-reverse')}> | ||
<Badge variant="secondary"><span className='font-bold'>{llm.provider}</span>:<span className='opacity-50'>{llm.model}</span></Badge> | ||
<Link className={badgeVariants()} href={`/llms/${llm.id}`}>{llm.name}</Link> | ||
</span> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { getLlm } from '@/api/llms'; | ||
import useSWR from 'swr'; | ||
|
||
export function useLlm (id: number | null | undefined) { | ||
const { data: llm, ...rest } = useSWR(id == null ? null : `api.llm.get?id=${id}`, () => getLlm(id as number)); | ||
return { llm, ...rest }; | ||
} |
Oops, something went wrong.