feat(webui): show resolved Auto API in preset selector

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-10 01:33:02 +08:00
1 parent 994ce51502
commit 73a27e0726
28 files changed
+464 -259

No files matched your search

@@ -1,18 +1,11 @@
import { useId } from "react";
import { ChevronDown } from "lucide-react";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { ToggleButton } from "@/components/settings/ToggleButton";
import { SettingsRow } from "@/components/settings/shared/SettingsControls";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import type { ModelAPIConfig, SettingsPayload } from "@/lib/types";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { cn } from "@/lib/utils";
import type { ModelAPIConfig, ProviderRequestAPI, SettingsPayload } from "@/lib/types";
export function modelAPIConfigurable(provider: SettingsPayload["providers"][number] | undefined): boolean {
if (Array.isArray(provider?.request_apis)) {
@@ -32,115 +25,87 @@ export function modelAPISelection(api: ModelAPIConfig | null | undefined): strin
export function ModelAPIControl({
provider,
automaticAPI,
value,
onChange,
}: {
provider: SettingsPayload["providers"][number] | undefined;
automaticAPI?: ProviderRequestAPI;
value: ModelAPIConfig | null;
onChange: (api: ModelAPIConfig | null) => void;
}) {
const { t } = useTranslation();
const helpId = useId();
const fallbackHelpId = useId();
const [pointerFocus, setPointerFocus] = useState(false);
const requestAPIs = Array.isArray(provider?.request_apis) ? provider.request_apis : undefined;
const configurable = modelAPIConfigurable(provider);
const fixedAPI = requestAPIs?.length === 1 ? requestAPIs[0] : undefined;
const fixedLabels = {
const labels = {
responses: "Responses",
chat_completions: "Chat Completions",
anthropic_messages: "Anthropic Messages",
bedrock_converse: "Bedrock Converse",
transcription: t("settings.models.apiTranscription"),
};
const fixedLabel = fixedAPI ? fixedLabels[fixedAPI] : undefined;
const fixedLabel = fixedAPI ? labels[fixedAPI] : undefined;
const selection = modelAPISelection(value);
const selectedAPI = selection === "prefer_responses" ? "responses"
: selection === "prefer_chat" ? "chat_completions" : selection;
const allowChatFallback = selection === "prefer_responses";
const options = [
{
value: "auto", label: t("settings.models.apiAuto"),
help: t("settings.models.requestAPIHelp"),
},
{
value: "responses", label: "Responses",
help: t("settings.models.apiResponsesHelp"),
},
{
value: "chat_completions", label: "Chat Completions",
help: t("settings.models.apiChatHelp"),
value: "auto",
label: automaticAPI
? `${t("settings.models.apiAuto")} (${labels[automaticAPI]})`
: t("settings.models.apiAuto"),
},
{ value: "responses", label: "Responses" },
{ value: "chat_completions", label: "Chat Completions" },
];
const selected = options.find((option) => option.value === selectedAPI);
const help = !configurable
? t("settings.models.apiProviderManagedHelp", { provider: provider?.label })
: selectedAPI === "responses"
? t(allowChatFallback ? "settings.models.apiResponsesFallbackHelp" : "settings.models.apiResponsesOnlyHelp")
: selected?.help;
if (!configurable && !fixedLabel) return null;
return (
<div>
<SettingsRow title={t("settings.models.requestAPI")}>
{configurable ? (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
type="button"
variant="outline"
aria-label={t("settings.models.requestAPI")}
aria-describedby={helpId}
className="h-9 w-full justify-between gap-3 rounded-full px-3 text-[13px]"
>
<span className="truncate">{selected?.label}</span>
<ChevronDown className="h-3.5 w-3.5 shrink-0 text-muted-foreground" aria-hidden />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-[min(340px,calc(100vw-2rem))]">
<DropdownMenuRadioGroup value={selectedAPI} onValueChange={(api) => {
if (api === selectedAPI) return;
if (api === "auto") onChange(null);
else if (api === "responses" || api === "chat_completions") {
onChange({ supported_apis: [api], preferred_api: api });
}
}}>
{options.map((option) => (
<DropdownMenuRadioItem
key={option.value}
value={option.value}
aria-label={option.label}
className="items-start py-2.5"
>
<span>
<span className="block text-[13px]">{option.label}</span>
<span className="mt-0.5 block text-[12px] leading-5 text-muted-foreground">
{option.help}
</span>
</span>
</DropdownMenuRadioItem>
))}
</DropdownMenuRadioGroup>
</DropdownMenuContent>
</DropdownMenu>
<Select value={selectedAPI} onValueChange={(api) => {
if (api === selectedAPI) return;
if (api === "auto") onChange(null);
else if (api === "responses" || api === "chat_completions") {
onChange({ supported_apis: [api], preferred_api: api });
}
}}>
<SelectTrigger
aria-label={t("settings.models.requestAPI")}
className={cn("w-full rounded-full", pointerFocus && "focus-visible:ring-0")}
onPointerDown={() => setPointerFocus(true)}
onKeyDown={() => setPointerFocus(false)}
onBlur={() => setPointerFocus(false)}
>
<SelectValue />
</SelectTrigger>
<SelectContent
onPointerUpCapture={() => setPointerFocus(true)}
onPointerDownOutside={() => setPointerFocus(true)}
onKeyDownCapture={() => setPointerFocus(false)}
onEscapeKeyDown={() => setPointerFocus(false)}
>
{options.map((option) => (
<SelectItem key={option.value} value={option.value}>{option.label}</SelectItem>
))}
</SelectContent>
</Select>
) : (
<span className="block text-[13px] text-muted-foreground sm:text-right">{fixedLabel}</span>
)}
</SettingsRow>
<p id={helpId} className="-mt-1 px-4 pb-3 text-[12px] leading-5 text-muted-foreground sm:px-5">
{help}
</p>
{configurable && selectedAPI === "responses" ? (
<div className="mx-4 mb-3 flex items-start justify-between gap-4 rounded-xl bg-muted/30 px-3 py-3 sm:mx-5">
<div className="min-w-0">
<p className="text-[12px] font-medium leading-5 text-foreground">{t("settings.models.apiFallback")}</p>
<p id={fallbackHelpId} className="mt-0.5 text-[12px] leading-5 text-muted-foreground">
{t("settings.models.apiFallbackHelp")}
</p>
<p className="text-[13px] leading-5 text-foreground">{t("settings.models.apiFallback")}</p>
</div>
<div className="pt-0.5">
<ToggleButton
checked={allowChatFallback}
aria-describedby={fallbackHelpId}
label={t("settings.models.apiFallback")}
onChange={(checked) => onChange({
supported_apis: checked ? ["responses", "chat_completions"] : ["responses"],
@@ -43,7 +43,8 @@ import {
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { cn } from "@/lib/utils";
import type { ModelAPIConfig, SettingsPayload } from "@/lib/types";
import { fetchAutomaticModelAPI } from "@/lib/api";
import type { AutomaticModelAPIPayload, ModelAPIConfig, SettingsPayload } from "@/lib/types";
export interface AgentSettingsDraft {
model: string;
@@ -237,6 +238,29 @@ export function ModelsSettings({
const tx = (key: string, fallback: string, values?: Record<string, unknown>) =>
t(key, { defaultValue: fallback, ...(values ?? {}) });
const [editorOpen, setEditorOpen] = useState(false);
const [automaticAPI, setAutomaticAPI] = useState<{
scope: string;
result: AutomaticModelAPIPayload;
} | null>(null);
const automaticAPIScope = JSON.stringify([
token, form.provider, form.model, form.reasoningEffort, settings.providers,
]);
useEffect(() => {
if (!editorOpen || settings.model_api_resolution_supported !== true || !form.model.trim()) return;
let cancelled = false;
const timer = window.setTimeout(() => {
void fetchAutomaticModelAPI(token, form.provider, form.model, form.reasoningEffort)
.then((result) => {
if (!cancelled) setAutomaticAPI({ scope: automaticAPIScope, result });
})
.catch(() => {
if (!cancelled) setAutomaticAPI(null);
});
}, 150);
return () => { cancelled = true; window.clearTimeout(timer); };
}, [editorOpen, settings.model_api_resolution_supported, automaticAPIScope, token, form]);
const resolvedAutomaticAPI = settings.model_api_resolution_supported === true
&& automaticAPI?.scope === automaticAPIScope ? automaticAPI.result : null;
const editorTriggerRef = useRef<HTMLElement | null>(null);
const presetNameInputRef = useRef<HTMLInputElement>(null);
const suggestedPresetNameRef = useRef<string | null>(null);
@@ -509,8 +533,9 @@ export function ModelsSettings({
</SettingsRow>
<ModelAPIControl
provider={selectedProvider ?? settings.providers.find(
(provider) => provider.name === selectedPreset?.resolved_provider,
(provider) => provider.name === (resolvedAutomaticAPI?.provider ?? selectedPreset?.resolved_provider),
)}
automaticAPI={resolvedAutomaticAPI?.api}
value={form.api}
onChange={(api) => setForm((prev) => ({ ...prev, api }))}
/>