feat(providers): declare supported APIs for custom connections
This commit is contained in:
1 parent
73a27e0726
commit
34fd4ed75a
29 files changed
+799
-108
No files matched your search
@@ -9,13 +9,16 @@ import type { ModelAPIConfig, ProviderRequestAPI, SettingsPayload } from "@/lib/
|
||||
|
||||
export function modelAPIConfigurable(provider: SettingsPayload["providers"][number] | undefined): boolean {
|
||||
if (Array.isArray(provider?.request_apis)) {
|
||||
return provider.request_apis.includes("chat_completions") && provider.request_apis.includes("responses");
|
||||
const supported = provider.request_apis;
|
||||
return (["chat_completions", "responses", "anthropic_messages"] as const)
|
||||
.filter((api) => supported.includes(api)).length > 1;
|
||||
}
|
||||
return provider?.model_api_configurable === true;
|
||||
}
|
||||
|
||||
export function modelAPISelection(api: ModelAPIConfig | null | undefined): string {
|
||||
if (!api) return "auto";
|
||||
if (api.supported_apis.includes("anthropic_messages")) return "anthropic_messages";
|
||||
if (!api.supported_apis.includes("responses")) return "chat_completions";
|
||||
if (!api.supported_apis.includes("chat_completions")) return "responses";
|
||||
return (api.preferred_api ?? api.supported_apis[0]) === "responses"
|
||||
@@ -58,8 +61,9 @@ export function ModelAPIControl({
|
||||
? `${t("settings.models.apiAuto")} (${labels[automaticAPI]})`
|
||||
: t("settings.models.apiAuto"),
|
||||
},
|
||||
{ value: "responses", label: "Responses" },
|
||||
{ value: "chat_completions", label: "Chat Completions" },
|
||||
...(["responses", "chat_completions", "anthropic_messages"] as const)
|
||||
.filter((api) => requestAPIs ? requestAPIs.includes(api) : api !== "anthropic_messages")
|
||||
.map((api) => ({ value: api, label: labels[api] })),
|
||||
];
|
||||
if (!configurable && !fixedLabel) return null;
|
||||
|
||||
@@ -70,7 +74,7 @@ export function ModelAPIControl({
|
||||
<Select value={selectedAPI} onValueChange={(api) => {
|
||||
if (api === selectedAPI) return;
|
||||
if (api === "auto") onChange(null);
|
||||
else if (api === "responses" || api === "chat_completions") {
|
||||
else if (api === "responses" || api === "chat_completions" || api === "anthropic_messages") {
|
||||
onChange({ supported_apis: [api], preferred_api: api });
|
||||
}
|
||||
}}>
|
||||
@@ -98,7 +102,7 @@ export function ModelAPIControl({
|
||||
<span className="block text-[13px] text-muted-foreground sm:text-right">{fixedLabel}</span>
|
||||
)}
|
||||
</SettingsRow>
|
||||
{configurable && selectedAPI === "responses" ? (
|
||||
{configurable && selectedAPI === "responses" && (!requestAPIs || requestAPIs.includes("chat_completions")) ? (
|
||||
<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-[13px] leading-5 text-foreground">{t("settings.models.apiFallback")}</p>
|
||||
|
||||
@@ -242,14 +242,15 @@ export function ModelsSettings({
|
||||
scope: string;
|
||||
result: AutomaticModelAPIPayload;
|
||||
} | null>(null);
|
||||
const { provider: apiProvider, model: apiModel, reasoningEffort: apiReasoningEffort } = form;
|
||||
const automaticAPIScope = JSON.stringify([
|
||||
token, form.provider, form.model, form.reasoningEffort, settings.providers,
|
||||
token, apiProvider, apiModel, apiReasoningEffort, settings.providers,
|
||||
]);
|
||||
useEffect(() => {
|
||||
if (!editorOpen || settings.model_api_resolution_supported !== true || !form.model.trim()) return;
|
||||
if (!editorOpen || settings.model_api_resolution_supported !== true || !apiModel.trim()) return;
|
||||
let cancelled = false;
|
||||
const timer = window.setTimeout(() => {
|
||||
void fetchAutomaticModelAPI(token, form.provider, form.model, form.reasoningEffort)
|
||||
void fetchAutomaticModelAPI(token, apiProvider, apiModel, apiReasoningEffort)
|
||||
.then((result) => {
|
||||
if (!cancelled) setAutomaticAPI({ scope: automaticAPIScope, result });
|
||||
})
|
||||
@@ -258,7 +259,7 @@ export function ModelsSettings({
|
||||
});
|
||||
}, 150);
|
||||
return () => { cancelled = true; window.clearTimeout(timer); };
|
||||
}, [editorOpen, settings.model_api_resolution_supported, automaticAPIScope, token, form]);
|
||||
}, [editorOpen, settings.model_api_resolution_supported, automaticAPIScope, token, apiProvider, apiModel, apiReasoningEffort]);
|
||||
const resolvedAutomaticAPI = settings.model_api_resolution_supported === true
|
||||
&& automaticAPI?.scope === automaticAPIScope ? automaticAPI.result : null;
|
||||
const editorTriggerRef = useRef<HTMLElement | null>(null);
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { ToggleButton } from "@/components/settings/ToggleButton";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import type { ModelAPIConfig, ModelRequestAPI } from "@/lib/types";
|
||||
|
||||
const APIS: Array<{ value: ModelRequestAPI; label: string }> = [
|
||||
{ value: "chat_completions", label: "Chat Completions" },
|
||||
{ value: "responses", label: "Responses" },
|
||||
{ value: "anthropic_messages", label: "Anthropic Messages" },
|
||||
];
|
||||
|
||||
export function ProviderAPIControl({ value, onChange }: {
|
||||
value: ModelAPIConfig | null;
|
||||
onChange: (api: ModelAPIConfig) => void;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
if (value === null) {
|
||||
return (
|
||||
<Button variant="outline" className="rounded-full" onClick={() => onChange({
|
||||
supported_apis: ["chat_completions"], preferred_api: "chat_completions",
|
||||
})}>
|
||||
{t("settings.providers.declareAPIs")}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
const supported = value.supported_apis;
|
||||
const preferred = value.preferred_api ?? supported[0];
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
<fieldset className="space-y-3 rounded-xl border border-border/60 p-3">
|
||||
<legend className="px-1 text-[12px] font-medium text-muted-foreground">
|
||||
{t("settings.providers.supportedAPIs")}
|
||||
</legend>
|
||||
{APIS.map((api) => {
|
||||
const checked = supported.includes(api.value);
|
||||
return (
|
||||
<div key={api.value} className="flex items-center justify-between gap-3 text-[13px]">
|
||||
<span>{api.label}</span>
|
||||
<ToggleButton
|
||||
label={api.label}
|
||||
checked={checked}
|
||||
disabled={checked && supported.length === 1}
|
||||
onChange={(enabled) => {
|
||||
const next = enabled ? [...supported, api.value] : supported.filter((item) => item !== api.value);
|
||||
onChange({ supported_apis: next, preferred_api: next.includes(preferred) ? preferred : next[0] });
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</fieldset>
|
||||
<div className="space-y-1.5">
|
||||
<span className="text-[12px] font-medium text-muted-foreground">
|
||||
{t("settings.providers.defaultAPI")}
|
||||
</span>
|
||||
<Select value={preferred} onValueChange={(api) => onChange({
|
||||
supported_apis: supported, preferred_api: api as ModelRequestAPI,
|
||||
})}>
|
||||
<SelectTrigger aria-label={t("settings.providers.defaultAPI")} className="w-full rounded-full">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{APIS.filter((api) => supported.includes(api.value)).map((api) => (
|
||||
<SelectItem key={api.value} value={api.value}>{api.label}</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import {
|
||||
SettingsSectionTitle,
|
||||
} from "@/components/settings/shared/SettingsControls";
|
||||
import { ToggleButton } from "@/components/settings/ToggleButton";
|
||||
import { ProviderAPIControl } from "@/components/settings/models/ProviderAPIControl";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
@@ -54,6 +55,7 @@ import { cn } from "@/lib/utils";
|
||||
import { copyTextToClipboard } from "@/lib/clipboard";
|
||||
import type {
|
||||
NanobotFeaturesPayload,
|
||||
ModelAPIConfig,
|
||||
ProviderOAuthAuthorizationRequired,
|
||||
SettingsPayload,
|
||||
} from "@/lib/types";
|
||||
@@ -67,6 +69,7 @@ export type ProviderForm = {
|
||||
apiKey: string;
|
||||
apiBase: string;
|
||||
apiType: ProviderApiType;
|
||||
api: ModelAPIConfig | null;
|
||||
proxy: string;
|
||||
extraHeaders: string;
|
||||
extraBody: string;
|
||||
@@ -208,6 +211,7 @@ export function providerFormFromRow(
|
||||
apiKey: "",
|
||||
apiBase: provider.api_base ?? provider.default_api_base ?? "",
|
||||
apiType: provider.api_type ?? "auto",
|
||||
api: provider.api ?? null,
|
||||
proxy: provider.proxy ?? "",
|
||||
extraHeaders: providerJsonValue(provider.extra_headers),
|
||||
extraBody: providerJsonValue(provider.extra_body),
|
||||
@@ -225,6 +229,7 @@ function emptyCustomProviderDraft(): CustomProviderDraft {
|
||||
apiKey: "",
|
||||
apiBase: "",
|
||||
apiType: "auto",
|
||||
api: { supported_apis: ["chat_completions"], preferred_api: "chat_completions" },
|
||||
proxy: "",
|
||||
extraHeaders: "",
|
||||
extraBody: "",
|
||||
@@ -1038,6 +1043,12 @@ export function ProvidersSettings({
|
||||
form={form}
|
||||
onChange={(value) => onChangeProviderForm(provider.name, value)}
|
||||
/>
|
||||
{provider.provider_api_configurable && (
|
||||
<ProviderAPIControl
|
||||
value={form.api}
|
||||
onChange={(api) => onChangeProviderForm(provider.name, { api })}
|
||||
/>
|
||||
)}
|
||||
<ProviderAdvancedOptions
|
||||
fields={advancedFields}
|
||||
form={form}
|
||||
@@ -1178,6 +1189,12 @@ export function ProvidersSettings({
|
||||
</Button>
|
||||
</div>
|
||||
</label>
|
||||
{settings.provider_api_configuration_supported && (
|
||||
<ProviderAPIControl
|
||||
value={customProviderDraft.api}
|
||||
onChange={(api) => setCustomProviderDraft((current) => ({ ...current, api }))}
|
||||
/>
|
||||
)}
|
||||
<ProviderAdvancedOptions
|
||||
fields={CUSTOM_PROVIDER_ADVANCED_FIELDS}
|
||||
form={customProviderDraft}
|
||||
|
||||
@@ -7,7 +7,7 @@ import type {
|
||||
PendingRestartSections,
|
||||
} from "@/components/settings/contracts";
|
||||
import { agentDraftFromPayload } from "@/components/settings/models/ModelsSettings";
|
||||
import { modelAPIConfigurable, modelAPISelection } from "@/components/settings/models/ModelAPIControl";
|
||||
import { modelAPISelection } from "@/components/settings/models/ModelAPIControl";
|
||||
import {
|
||||
CUSTOM_PROVIDER_CREATION_KEY,
|
||||
providerFormFromRow,
|
||||
@@ -196,8 +196,7 @@ export function useModelSettingsActions({
|
||||
contextWindowTokens: form.contextWindowTokens,
|
||||
temperature: form.temperature,
|
||||
reasoningEffort: form.reasoningEffort || null,
|
||||
api: modelAPIConfigurable(settings.providers.find((row) => row.name === provider))
|
||||
? form.api : undefined,
|
||||
api: form.api ?? undefined,
|
||||
});
|
||||
const createdPreset = payload.created_model_preset;
|
||||
const nextOrder = createdPreset ? [...modelCallOrder, createdPreset] : null;
|
||||
@@ -260,10 +259,7 @@ export function useModelSettingsActions({
|
||||
form.temperature !== selectedPreset.temperature ? form.temperature : undefined,
|
||||
reasoningEffort:
|
||||
reasoningEffort !== selectedPreset.reasoning_effort ? reasoningEffort : undefined,
|
||||
api: modelAPIConfigurable(settings.providers.find((row) => row.name === (
|
||||
form.provider === "auto" ? selectedPreset.resolved_provider : form.provider
|
||||
))) &&
|
||||
modelAPISelection(form.api) !== modelAPISelection(selectedPreset.api)
|
||||
api: modelAPISelection(form.api) !== modelAPISelection(selectedPreset.api)
|
||||
? form.api : undefined,
|
||||
});
|
||||
applyPayload(payload);
|
||||
@@ -411,6 +407,7 @@ export function useModelSettingsActions({
|
||||
update.apiKey = apiKey || undefined;
|
||||
update.apiBase = providerForm.apiBase.trim();
|
||||
if (provider.is_custom) update.displayName = providerForm.displayName.trim();
|
||||
if (provider.provider_api_configurable) update.api = providerForm.api;
|
||||
}
|
||||
for (const field of provider.advanced_fields ?? []) {
|
||||
if (field === "api_type") update.apiType = providerForm.apiType;
|
||||
@@ -464,6 +461,7 @@ export function useModelSettingsActions({
|
||||
name: draft.name.trim(),
|
||||
apiKey: draft.apiKey.trim() || undefined,
|
||||
apiBase: draft.apiBase.trim(),
|
||||
...(settings?.provider_api_configuration_supported ? { api: draft.api } : {}),
|
||||
proxy: draft.proxy.trim(),
|
||||
extraHeaders: draft.extraHeaders.trim(),
|
||||
extraBody: draft.extraBody.trim(),
|
||||
|
||||
Reference in new issue
Block a user