import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" import { useEffect, useState } from "react" import { type AIConfigPublic, AiService } from "@/client" import { InfoTip } from "@/components/Flow/SidePanel" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { LoadingButton } from "@/components/ui/loading-button" import { PasswordInput } from "@/components/ui/password-input" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import useCustomToast from "@/hooks/useCustomToast" import { handleError } from "@/utils" /** Where the usual providers answer. Anything OpenAI-compatible will do. */ const PROVIDERS = [ ["https://api.openai.com/v1", "OpenAI"], ["https://api.anthropic.com/v1", "Anthropic"], ["https://openrouter.ai/api/v1", "OpenRouter"], ["http://localhost:11434/v1", "Ollama"], ] as const /** * The assistant's provider: one for the whole instance, set by an operator, * with the key kept encrypted here and never shown again. */ export function Assistant() { const queryClient = useQueryClient() const { showSuccessToast, showErrorToast } = useCustomToast() const [apiBase, setApiBase] = useState("") const [apiKey, setApiKey] = useState("") const [model, setModel] = useState("") const [models, setModels] = useState([]) const config = useQuery({ queryKey: ["ai", "config"], queryFn: () => AiService.readAiConfig(), }) useEffect(() => { if (!config.data) return setApiBase(config.data.api_base) setModel(config.data.model) }, [config.data]) const invalidate = () => queryClient.invalidateQueries({ queryKey: ["ai"] }) const fetchModels = useMutation({ mutationFn: () => AiService.listAiModels({ requestBody: { api_base: apiBase.trim(), api_key: apiKey.trim() || null, }, }), onSuccess: (list) => { setModels(list) if (list.length && !list.includes(model)) setModel(list[0]) }, onError: handleError.bind(showErrorToast), }) const save = useMutation({ mutationFn: () => AiService.updateAiConfig({ requestBody: { api_base: apiBase.trim(), api_key: apiKey.trim() || null, model: model.trim(), }, }), onSuccess: () => { setApiKey("") showSuccessToast("Assistant ready") invalidate() }, onError: handleError.bind(showErrorToast), }) const remove = useMutation({ mutationFn: () => AiService.deleteAiConfig(), onSuccess: () => { setApiKey("") setModels([]) showSuccessToast("Assistant turned off") invalidate() }, onError: handleError.bind(showErrorToast), }) if (!config.data) return null const keySet = config.data.api_key_set const preset = PROVIDERS.find(([url]) => url === apiBase)?.[0] ?? "custom" return ( Assistant Builds and edits flows from a chat, through the same API as agents. Needs a model provider; everyone on this instance shares it.
An OpenAI-compatible base, ending in /v1. A server on your own network works too.
setApiBase(event.target.value)} />
Stored encrypted on this instance and never shown again. Leave it empty to keep the one saved.
setApiKey(event.target.value)} />
{models.length ? ( ) : ( setModel(event.target.value)} /> )} fetchModels.mutate()} > Fetch models
save.mutate()} data-testid="save-assistant" > Save {keySet ? ( ) : null}
) } export default Assistant