2026-09-19 14:46:31 +08:00
|
|
|
import { useEffect, useId, useMemo, useRef, useState, type ReactNode } from "react";
|
2026-09-11 19:10:15 +08:00
|
|
|
import { DisclosureContent } from "@/components/ui/disclosure";
|
2026-08-10 19:33:52 +08:00
|
|
|
import {
|
|
|
|
|
ChevronDown,
|
2026-09-19 14:46:31 +08:00
|
|
|
ChevronLeft,
|
|
|
|
|
ChevronRight,
|
2026-08-10 19:33:52 +08:00
|
|
|
Clipboard,
|
|
|
|
|
Eye,
|
|
|
|
|
EyeOff,
|
|
|
|
|
ExternalLink,
|
|
|
|
|
Globe2,
|
|
|
|
|
Hexagon,
|
|
|
|
|
Loader2,
|
|
|
|
|
Pencil,
|
|
|
|
|
Plus,
|
2026-09-19 14:46:31 +08:00
|
|
|
Search,
|
2026-08-10 19:33:52 +08:00
|
|
|
Zap,
|
|
|
|
|
} from "lucide-react";
|
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
|
|
|
|
|
import { PROVIDER_ICONS } from "@/components/settings/shared/ModelControls";
|
|
|
|
|
import {
|
|
|
|
|
CapabilityInstallNotice,
|
|
|
|
|
SettingsGroup,
|
|
|
|
|
SettingsSectionTitle,
|
|
|
|
|
} from "@/components/settings/shared/SettingsControls";
|
|
|
|
|
import { ToggleButton } from "@/components/settings/ToggleButton";
|
2026-10-09 13:59:13 +08:00
|
|
|
import { ProviderAPIControl } from "@/components/settings/models/ProviderAPIControl";
|
2026-08-10 19:33:52 +08:00
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import {
|
|
|
|
|
Dialog,
|
|
|
|
|
DialogContent,
|
|
|
|
|
DialogDescription,
|
|
|
|
|
DialogFooter,
|
|
|
|
|
DialogHeader,
|
|
|
|
|
DialogTitle,
|
2026-09-10 11:10:51 +08:00
|
|
|
DialogTrigger,
|
2026-08-10 19:33:52 +08:00
|
|
|
} from "@/components/ui/dialog";
|
|
|
|
|
import {
|
|
|
|
|
DropdownMenu,
|
|
|
|
|
DropdownMenuContent,
|
|
|
|
|
DropdownMenuItem,
|
|
|
|
|
DropdownMenuTrigger,
|
|
|
|
|
} from "@/components/ui/dropdown-menu";
|
|
|
|
|
import { Input } from "@/components/ui/input";
|
2026-09-19 14:46:31 +08:00
|
|
|
import { ComboboxOption, useComboboxNavigation } from "@/components/ui/combobox";
|
|
|
|
|
import { SheetContent } from "@/components/ui/sheet";
|
|
|
|
|
import { FloatingPortalContext } from "@/components/ui/floating-portal";
|
2026-08-10 19:33:52 +08:00
|
|
|
import { Textarea } from "@/components/ui/textarea";
|
2026-09-10 11:10:51 +08:00
|
|
|
import { SettingsTextEditor } from "@/components/settings/shared/SettingsTextEditor";
|
2026-08-10 19:33:52 +08:00
|
|
|
import { useLogoFallback } from "@/hooks/useLogoFallback";
|
2026-09-19 14:46:31 +08:00
|
|
|
import { useMediaQuery } from "@/hooks/useMediaQuery";
|
2026-08-10 19:33:52 +08:00
|
|
|
import { providerBrand } from "@/lib/provider-brand";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
2026-09-20 23:41:50 +08:00
|
|
|
import { copyTextToClipboard } from "@/lib/clipboard";
|
2026-08-10 19:33:52 +08:00
|
|
|
import type {
|
|
|
|
|
NanobotFeaturesPayload,
|
2026-10-09 13:59:13 +08:00
|
|
|
ModelAPIConfig,
|
2026-08-10 19:33:52 +08:00
|
|
|
ProviderOAuthAuthorizationRequired,
|
|
|
|
|
SettingsPayload,
|
|
|
|
|
} from "@/lib/types";
|
|
|
|
|
|
|
|
|
|
type ProviderApiType = "auto" | "chat_completions" | "responses";
|
|
|
|
|
type ProviderAdvancedField = NonNullable<
|
|
|
|
|
SettingsPayload["providers"][number]["advanced_fields"]
|
|
|
|
|
>[number];
|
|
|
|
|
export type ProviderForm = {
|
|
|
|
|
displayName: string;
|
|
|
|
|
apiKey: string;
|
|
|
|
|
apiBase: string;
|
|
|
|
|
apiType: ProviderApiType;
|
2026-10-09 13:59:13 +08:00
|
|
|
api: ModelAPIConfig | null;
|
2026-08-10 19:33:52 +08:00
|
|
|
proxy: string;
|
|
|
|
|
extraHeaders: string;
|
|
|
|
|
extraBody: string;
|
|
|
|
|
extraQuery: string;
|
|
|
|
|
thinkingStyle: string;
|
|
|
|
|
region: string;
|
|
|
|
|
profile: string;
|
|
|
|
|
};
|
|
|
|
|
export type CustomProviderDraft = ProviderForm & { name: string };
|
|
|
|
|
const OAUTH_PROXY_PROVIDERS = new Set(["openai_codex", "xai_grok"]);
|
|
|
|
|
type ProviderRequestOption = {
|
|
|
|
|
kind: "priority" | "hosted_tool";
|
|
|
|
|
titleKey: string;
|
|
|
|
|
title: string;
|
|
|
|
|
helpKey: string;
|
|
|
|
|
help: string;
|
|
|
|
|
toolType?: "web_search" | "x_search";
|
|
|
|
|
defaultEnabled?: boolean;
|
|
|
|
|
forceResponses?: boolean;
|
|
|
|
|
};
|
|
|
|
|
const PROVIDER_REQUEST_OPTIONS: Partial<Record<string, ProviderRequestOption[]>> = {
|
|
|
|
|
openai_codex: [{
|
|
|
|
|
kind: "priority",
|
|
|
|
|
titleKey: "settings.providers.capabilityFastMode",
|
|
|
|
|
title: "Fast mode",
|
|
|
|
|
helpKey: "settings.providers.capabilityFastModeHelp",
|
|
|
|
|
help: "Use OpenAI's priority service tier for faster responses. This consumes credits faster.",
|
|
|
|
|
}],
|
|
|
|
|
openai: [{
|
|
|
|
|
kind: "hosted_tool",
|
|
|
|
|
titleKey: "settings.providers.capabilityOpenAISearch",
|
|
|
|
|
title: "OpenAI web search",
|
|
|
|
|
helpKey: "settings.providers.capabilityOpenAISearchHelp",
|
|
|
|
|
help: "Allow compatible Responses API models to search the web. Search activity appears in chat.",
|
|
|
|
|
toolType: "web_search",
|
|
|
|
|
forceResponses: true,
|
|
|
|
|
}],
|
|
|
|
|
deepseek: [{
|
|
|
|
|
kind: "hosted_tool",
|
|
|
|
|
titleKey: "settings.providers.capabilityDeepSeekSearch",
|
|
|
|
|
title: "DeepSeek web search",
|
|
|
|
|
helpKey: "settings.providers.capabilityDeepSeekSearchHelp",
|
|
|
|
|
help: "Let DeepSeek V4 Flash search the web through its Responses API. Search activity appears in chat.",
|
|
|
|
|
toolType: "web_search",
|
|
|
|
|
defaultEnabled: true,
|
|
|
|
|
}],
|
|
|
|
|
xai_grok: [{
|
|
|
|
|
kind: "hosted_tool",
|
|
|
|
|
titleKey: "settings.providers.capabilityXSearch",
|
|
|
|
|
title: "X Search",
|
|
|
|
|
helpKey: "settings.providers.capabilityXSearchHelp",
|
|
|
|
|
help: "Allow supported Grok models to use xAI-hosted X Search. Search activity appears in chat.",
|
|
|
|
|
toolType: "x_search",
|
|
|
|
|
defaultEnabled: true,
|
|
|
|
|
}],
|
|
|
|
|
};
|
|
|
|
|
export const CUSTOM_PROVIDER_CREATION_KEY = "__custom_provider__";
|
|
|
|
|
const CUSTOM_PROVIDER_ADVANCED_FIELDS: ProviderAdvancedField[] = [
|
|
|
|
|
"extra_headers",
|
|
|
|
|
"extra_body",
|
|
|
|
|
"extra_query",
|
|
|
|
|
"proxy",
|
|
|
|
|
"thinking_style",
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
function providerJsonValue(value: Record<string, unknown> | null | undefined): string {
|
|
|
|
|
return value && Object.keys(value).length > 0 ? JSON.stringify(value, null, 2) : "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function parseProviderExtraBody(value: string): Record<string, unknown> | null {
|
|
|
|
|
if (!value.trim()) return {};
|
|
|
|
|
try {
|
|
|
|
|
const parsed: unknown = JSON.parse(value);
|
|
|
|
|
return parsed && typeof parsed === "object" && !Array.isArray(parsed)
|
|
|
|
|
? parsed as Record<string, unknown>
|
|
|
|
|
: null;
|
|
|
|
|
} catch {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isHostedSearchTool(tool: unknown, toolType: "web_search" | "x_search"): boolean {
|
|
|
|
|
if (!tool || typeof tool !== "object" || Array.isArray(tool)) return false;
|
|
|
|
|
const configuredType = (tool as Record<string, unknown>).type;
|
|
|
|
|
if (typeof configuredType !== "string") return false;
|
|
|
|
|
return configuredType === toolType
|
|
|
|
|
|| (toolType === "web_search" && configuredType.startsWith("web_search_"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function hasHostedSearchTool(value: unknown, toolType: "web_search" | "x_search"): boolean {
|
|
|
|
|
return Array.isArray(value) && value.some((tool) => isHostedSearchTool(tool, toolType));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function providerRequestOptionEnabled(
|
|
|
|
|
option: ProviderRequestOption,
|
|
|
|
|
extraBody: Record<string, unknown>,
|
|
|
|
|
): boolean {
|
|
|
|
|
if (option.kind === "priority") return extraBody.service_tier === "priority";
|
|
|
|
|
if (Object.prototype.hasOwnProperty.call(extraBody, "tools")) {
|
|
|
|
|
return hasHostedSearchTool(extraBody.tools, option.toolType!);
|
|
|
|
|
}
|
|
|
|
|
return option.defaultEnabled === true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateProviderRequestOption(
|
|
|
|
|
option: ProviderRequestOption,
|
|
|
|
|
enabled: boolean,
|
|
|
|
|
form: ProviderForm,
|
|
|
|
|
): Partial<ProviderForm> {
|
|
|
|
|
const extraBody = { ...(parseProviderExtraBody(form.extraBody) ?? {}) };
|
|
|
|
|
if (option.kind === "priority") {
|
|
|
|
|
if (enabled) extraBody.service_tier = "priority";
|
|
|
|
|
else if (extraBody.service_tier === "priority") delete extraBody.service_tier;
|
|
|
|
|
} else {
|
|
|
|
|
const toolType = option.toolType!;
|
|
|
|
|
const tools = Array.isArray(extraBody.tools)
|
|
|
|
|
? extraBody.tools.filter((tool) => !isHostedSearchTool(tool, toolType))
|
|
|
|
|
: [];
|
|
|
|
|
if (enabled) tools.push({ type: toolType });
|
|
|
|
|
if (tools.length || option.defaultEnabled) {
|
|
|
|
|
extraBody.tools = tools;
|
|
|
|
|
} else {
|
|
|
|
|
delete extraBody.tools;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
extraBody: providerJsonValue(extraBody),
|
|
|
|
|
...(option.forceResponses && enabled
|
|
|
|
|
? { apiType: "responses" as const }
|
|
|
|
|
: {}),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function providerFormFromRow(
|
|
|
|
|
provider: SettingsPayload["providers"][number],
|
|
|
|
|
): ProviderForm {
|
|
|
|
|
return {
|
|
|
|
|
displayName: provider.is_custom ? provider.label : "",
|
|
|
|
|
apiKey: "",
|
|
|
|
|
apiBase: provider.api_base ?? provider.default_api_base ?? "",
|
|
|
|
|
apiType: provider.api_type ?? "auto",
|
2026-10-09 13:59:13 +08:00
|
|
|
api: provider.api ?? null,
|
2026-08-10 19:33:52 +08:00
|
|
|
proxy: provider.proxy ?? "",
|
|
|
|
|
extraHeaders: providerJsonValue(provider.extra_headers),
|
|
|
|
|
extraBody: providerJsonValue(provider.extra_body),
|
|
|
|
|
extraQuery: providerJsonValue(provider.extra_query),
|
|
|
|
|
thinkingStyle: provider.thinking_style ?? "",
|
|
|
|
|
region: provider.region ?? "",
|
|
|
|
|
profile: provider.profile ?? "",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function emptyCustomProviderDraft(): CustomProviderDraft {
|
|
|
|
|
return {
|
|
|
|
|
name: "",
|
|
|
|
|
displayName: "",
|
|
|
|
|
apiKey: "",
|
|
|
|
|
apiBase: "",
|
|
|
|
|
apiType: "auto",
|
2026-10-09 13:59:13 +08:00
|
|
|
api: { supported_apis: ["chat_completions"], preferred_api: "chat_completions" },
|
2026-08-10 19:33:52 +08:00
|
|
|
proxy: "",
|
|
|
|
|
extraHeaders: "",
|
|
|
|
|
extraBody: "",
|
|
|
|
|
extraQuery: "",
|
|
|
|
|
thinkingStyle: "",
|
|
|
|
|
region: "",
|
|
|
|
|
profile: "",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const OPENAI_API_TYPE_OPTIONS: Array<{ value: ProviderApiType; label: string }> = [
|
|
|
|
|
{ value: "auto", label: "Auto" },
|
|
|
|
|
{ value: "chat_completions", label: "Chat Completions" },
|
|
|
|
|
{ value: "responses", label: "Responses" },
|
|
|
|
|
];
|
|
|
|
|
|
2026-09-02 04:19:12 +08:00
|
|
|
const LOCAL_UNCONFIGURED_PROVIDER_ORDER = new Map(
|
2026-08-10 19:33:52 +08:00
|
|
|
["vllm", "ollama", "lm_studio", "atomic_chat", "ovms"].map((name, index) => [
|
|
|
|
|
name,
|
|
|
|
|
index,
|
|
|
|
|
]),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export function ProviderOAuthLoginDialog({
|
|
|
|
|
flow,
|
|
|
|
|
providerLabel,
|
|
|
|
|
authorizationResponse,
|
|
|
|
|
completing,
|
|
|
|
|
error,
|
|
|
|
|
remoteBrowserAccess,
|
|
|
|
|
onAuthorizationResponseChange,
|
|
|
|
|
onOpenAuthorization,
|
|
|
|
|
onComplete,
|
|
|
|
|
onClose,
|
|
|
|
|
}: {
|
|
|
|
|
flow: ProviderOAuthAuthorizationRequired | null;
|
|
|
|
|
providerLabel: string;
|
|
|
|
|
authorizationResponse: string;
|
|
|
|
|
completing: boolean;
|
|
|
|
|
error: string | null;
|
|
|
|
|
remoteBrowserAccess: boolean;
|
|
|
|
|
onAuthorizationResponseChange: (value: string) => void;
|
|
|
|
|
onOpenAuthorization: () => void;
|
|
|
|
|
onComplete: () => void;
|
|
|
|
|
onClose: () => void;
|
|
|
|
|
}) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const expectsCallbackUrl = flow?.completion_input === "callback_url";
|
2026-09-20 23:41:50 +08:00
|
|
|
const isDeviceCode = flow?.completion_input === "device_code";
|
|
|
|
|
const [copiedCode, setCopiedCode] = useState<string | null>(null);
|
2026-08-10 19:33:52 +08:00
|
|
|
const inputId = expectsCallbackUrl ? "provider-oauth-callback" : "provider-oauth-code";
|
|
|
|
|
const inputLabel = expectsCallbackUrl
|
|
|
|
|
? t("settings.oauth.callbackUrl")
|
|
|
|
|
: t("settings.oauth.authorizationCode");
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Dialog
|
|
|
|
|
open={Boolean(flow)}
|
|
|
|
|
onOpenChange={(open) => {
|
|
|
|
|
if (!open) onClose();
|
|
|
|
|
}}
|
|
|
|
|
>
|
2026-08-14 07:07:09 +09:00
|
|
|
<DialogContent className="w-[min(calc(100vw-2rem),28rem)]">
|
2026-08-10 19:33:52 +08:00
|
|
|
<form
|
|
|
|
|
className="space-y-4"
|
|
|
|
|
onSubmit={(event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
onComplete();
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<DialogHeader>
|
|
|
|
|
<DialogTitle>{providerLabel}</DialogTitle>
|
|
|
|
|
<DialogDescription>
|
2026-09-20 23:41:50 +08:00
|
|
|
{isDeviceCode
|
|
|
|
|
? t("settings.oauth.deviceCodeHelp")
|
|
|
|
|
: expectsCallbackUrl
|
2026-08-10 19:33:52 +08:00
|
|
|
? remoteBrowserAccess
|
|
|
|
|
? t("settings.oauth.remoteCallbackHelp")
|
|
|
|
|
: t("settings.oauth.localCallbackHelp")
|
|
|
|
|
: remoteBrowserAccess
|
|
|
|
|
? t("settings.oauth.remoteCodeHelp")
|
|
|
|
|
: t("settings.oauth.localCodeHelp")}
|
|
|
|
|
</DialogDescription>
|
|
|
|
|
</DialogHeader>
|
2026-08-14 07:07:09 +09:00
|
|
|
<div className="flex items-center gap-2 rounded-control border border-border/45 bg-muted/35 px-3 py-2.5 text-[12px] text-muted-foreground">
|
2026-08-10 19:33:52 +08:00
|
|
|
{expectsCallbackUrl && remoteBrowserAccess ? (
|
|
|
|
|
<Clipboard className="h-3.5 w-3.5 shrink-0" aria-hidden />
|
|
|
|
|
) : (
|
|
|
|
|
<Loader2 className="h-3.5 w-3.5 shrink-0 animate-spin" aria-hidden />
|
|
|
|
|
)}
|
|
|
|
|
<span>
|
2026-09-20 23:41:50 +08:00
|
|
|
{isDeviceCode
|
|
|
|
|
? t("settings.oauth.waitingForDeviceApproval")
|
|
|
|
|
: expectsCallbackUrl && remoteBrowserAccess
|
2026-08-10 19:33:52 +08:00
|
|
|
? t("settings.oauth.pasteCallbackToContinue")
|
|
|
|
|
: t("settings.oauth.waitingForCallback")}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
2026-09-20 23:41:50 +08:00
|
|
|
{isDeviceCode ? (
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<label htmlFor="provider-device-code" className="block text-xs font-medium text-foreground">
|
|
|
|
|
{t("settings.oauth.deviceCode")}
|
|
|
|
|
</label>
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<Input
|
|
|
|
|
id="provider-device-code"
|
|
|
|
|
value={flow?.user_code ?? ""}
|
|
|
|
|
readOnly
|
|
|
|
|
onFocus={(event) => event.target.select()}
|
|
|
|
|
className="h-12 text-center font-mono text-xl tracking-widest"
|
|
|
|
|
/>
|
|
|
|
|
<Button type="button" variant="outline" onClick={async () => {
|
|
|
|
|
if (flow?.user_code && await copyTextToClipboard(flow.user_code)) {
|
|
|
|
|
setCopiedCode(flow.flow_id);
|
|
|
|
|
}
|
|
|
|
|
}}>
|
|
|
|
|
<Clipboard className="mr-2 h-4 w-4" aria-hidden />
|
|
|
|
|
{copiedCode === flow?.flow_id ? t("code.copied") : t("code.copy")}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
) : <div className="space-y-2">
|
2026-08-10 19:33:52 +08:00
|
|
|
<label
|
|
|
|
|
htmlFor={inputId}
|
|
|
|
|
className="block text-xs font-medium text-foreground"
|
|
|
|
|
>
|
|
|
|
|
{inputLabel}
|
|
|
|
|
</label>
|
|
|
|
|
{expectsCallbackUrl ? (
|
|
|
|
|
<Textarea
|
|
|
|
|
id={inputId}
|
|
|
|
|
value={authorizationResponse}
|
|
|
|
|
onChange={(event) => onAuthorizationResponseChange(event.target.value)}
|
|
|
|
|
placeholder={t("settings.oauth.callbackUrlPlaceholder")}
|
|
|
|
|
aria-label={inputLabel}
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
spellCheck={false}
|
|
|
|
|
className="min-h-[88px] resize-none break-all font-mono text-[12px] leading-5"
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<Input
|
|
|
|
|
id={inputId}
|
|
|
|
|
value={authorizationResponse}
|
|
|
|
|
onChange={(event) => onAuthorizationResponseChange(event.target.value)}
|
|
|
|
|
placeholder={inputLabel}
|
|
|
|
|
aria-label={inputLabel}
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
spellCheck={false}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2026-09-20 23:41:50 +08:00
|
|
|
</div>}
|
2026-08-10 19:33:52 +08:00
|
|
|
{error ? (
|
|
|
|
|
<p
|
|
|
|
|
role="alert"
|
2026-08-14 07:07:09 +09:00
|
|
|
className="rounded-control border border-destructive/20 bg-destructive/5 px-3 py-2.5 text-[12px] text-destructive"
|
2026-08-10 19:33:52 +08:00
|
|
|
>
|
|
|
|
|
{error}
|
|
|
|
|
</p>
|
|
|
|
|
) : null}
|
2026-08-14 07:07:09 +09:00
|
|
|
<DialogFooter>
|
2026-08-10 19:33:52 +08:00
|
|
|
<Button type="button" variant="outline" onClick={onOpenAuthorization}>
|
|
|
|
|
<ExternalLink className="mr-2 h-4 w-4" aria-hidden />
|
2026-09-20 23:41:50 +08:00
|
|
|
{isDeviceCode
|
|
|
|
|
? t("settings.oauth.openGitHub")
|
|
|
|
|
: expectsCallbackUrl
|
2026-08-10 19:33:52 +08:00
|
|
|
? t("settings.oauth.openChatGPT")
|
|
|
|
|
: t("settings.oauth.signIn")}
|
|
|
|
|
</Button>
|
2026-09-20 23:41:50 +08:00
|
|
|
{!isDeviceCode ? <Button type="submit" disabled={!authorizationResponse.trim() || completing}>
|
2026-08-10 19:33:52 +08:00
|
|
|
{completing ? t("settings.oauth.signingIn") : t("settings.oauth.finishSignIn")}
|
2026-09-20 23:41:50 +08:00
|
|
|
</Button> : null}
|
2026-08-10 19:33:52 +08:00
|
|
|
</DialogFooter>
|
|
|
|
|
</form>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ProviderRequestOptions({
|
|
|
|
|
providerName,
|
|
|
|
|
form,
|
|
|
|
|
onChange,
|
|
|
|
|
}: {
|
|
|
|
|
providerName: string;
|
|
|
|
|
form: ProviderForm;
|
|
|
|
|
onChange: (value: Partial<ProviderForm>) => void;
|
|
|
|
|
}) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const options = PROVIDER_REQUEST_OPTIONS[providerName] ?? [];
|
|
|
|
|
if (options.length === 0) return null;
|
|
|
|
|
const extraBody = parseProviderExtraBody(form.extraBody) ?? {};
|
|
|
|
|
|
|
|
|
|
const tx = (key: string, fallback: string) => t(key, { defaultValue: fallback });
|
|
|
|
|
|
|
|
|
|
return (
|
2026-08-14 07:07:09 +09:00
|
|
|
<div className="overflow-hidden rounded-floating border border-border/45 bg-background/75">
|
2026-09-10 11:10:51 +08:00
|
|
|
{options.map((option) => {
|
2026-08-10 19:33:52 +08:00
|
|
|
const title = tx(option.titleKey, option.title);
|
|
|
|
|
const Icon = option.kind === "priority" ? Zap : Globe2;
|
|
|
|
|
const checked = providerRequestOptionEnabled(option, extraBody);
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
key={option.titleKey}
|
2026-09-10 11:10:51 +08:00
|
|
|
className="flex items-center justify-between gap-4 rounded-xl px-4 py-3 transition-colors settings-hover focus-within:bg-sidebar-accent/60"
|
2026-08-10 19:33:52 +08:00
|
|
|
>
|
|
|
|
|
<div className="flex min-w-0 items-start gap-3">
|
|
|
|
|
<span className="mt-0.5 flex h-8 w-8 shrink-0 items-center justify-center rounded-full bg-muted/70 text-muted-foreground">
|
|
|
|
|
<Icon className="h-4 w-4" aria-hidden />
|
|
|
|
|
</span>
|
|
|
|
|
<div className="min-w-0">
|
|
|
|
|
<p className="text-[13px] font-semibold text-foreground">{title}</p>
|
|
|
|
|
<p className="mt-0.5 text-[12px] leading-5 text-muted-foreground">
|
|
|
|
|
{tx(option.helpKey, option.help)}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<ToggleButton
|
|
|
|
|
checked={checked}
|
|
|
|
|
onChange={(enabled) => onChange(
|
|
|
|
|
updateProviderRequestOption(option, enabled, form),
|
|
|
|
|
)}
|
|
|
|
|
ariaLabel={title}
|
|
|
|
|
label={checked ? tx("settings.values.on", "On") : tx("settings.values.off", "Off")}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ProviderAdvancedOptions({
|
|
|
|
|
fields,
|
|
|
|
|
form,
|
|
|
|
|
onChange,
|
|
|
|
|
footer,
|
|
|
|
|
}: {
|
|
|
|
|
fields: ProviderAdvancedField[];
|
|
|
|
|
form: ProviderForm;
|
|
|
|
|
onChange: (value: Partial<ProviderForm>) => void;
|
|
|
|
|
footer?: ReactNode;
|
|
|
|
|
}) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
|
const enabled = new Set(fields);
|
2026-09-11 19:10:15 +08:00
|
|
|
const contentId = useId();
|
2026-08-10 19:33:52 +08:00
|
|
|
if (enabled.size === 0) return null;
|
|
|
|
|
|
|
|
|
|
const tx = (key: string, fallback: string) => t(key, { defaultValue: fallback });
|
|
|
|
|
const thinkingStyleOptions = [
|
|
|
|
|
{ value: "", label: tx("settings.values.default", "Default") },
|
|
|
|
|
{ value: "thinking_type", label: "thinking_type" },
|
|
|
|
|
{ value: "enable_thinking", label: "enable_thinking" },
|
|
|
|
|
{ value: "reasoning_split", label: "reasoning_split" },
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return (
|
2026-09-10 11:10:51 +08:00
|
|
|
<div className="space-y-1">
|
2026-08-10 19:33:52 +08:00
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
aria-expanded={open}
|
2026-09-11 19:10:15 +08:00
|
|
|
aria-controls={contentId}
|
2026-08-10 19:33:52 +08:00
|
|
|
onClick={() => setOpen((value) => !value)}
|
|
|
|
|
className="flex min-h-[48px] w-full items-center justify-between gap-4 px-1 py-2.5 text-left transition-colors hover:text-foreground"
|
|
|
|
|
>
|
|
|
|
|
<span className="text-[13px] font-medium text-foreground">
|
|
|
|
|
{tx("settings.providers.advancedOptions", "Advanced options")}
|
|
|
|
|
</span>
|
|
|
|
|
<ChevronDown
|
|
|
|
|
className={cn(
|
2026-09-11 19:10:15 +08:00
|
|
|
"h-4 w-4 shrink-0 text-muted-foreground transition-transform duration-200 motion-reduce:transition-none",
|
2026-08-10 19:33:52 +08:00
|
|
|
open && "rotate-180",
|
|
|
|
|
)}
|
|
|
|
|
aria-hidden
|
|
|
|
|
/>
|
|
|
|
|
</button>
|
2026-09-11 19:10:15 +08:00
|
|
|
<DisclosureContent id={contentId} open={open}>
|
2026-09-10 11:10:51 +08:00
|
|
|
<div className="py-3">
|
2026-08-10 19:33:52 +08:00
|
|
|
<div className="grid gap-3 md:grid-cols-2">
|
|
|
|
|
{enabled.has("api_type") ? (
|
|
|
|
|
<label className="block space-y-1.5">
|
|
|
|
|
<span className="text-[12px] font-medium text-muted-foreground">
|
|
|
|
|
{tx("settings.providers.apiType", "API type")}
|
|
|
|
|
</span>
|
|
|
|
|
<DropdownMenu>
|
|
|
|
|
<DropdownMenuTrigger asChild>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="outline"
|
|
|
|
|
className="h-9 w-full justify-between rounded-full px-3 text-[13px]"
|
|
|
|
|
>
|
|
|
|
|
<span>
|
|
|
|
|
{OPENAI_API_TYPE_OPTIONS.find(
|
|
|
|
|
(option) => option.value === form.apiType,
|
|
|
|
|
)?.label ?? form.apiType}
|
|
|
|
|
</span>
|
|
|
|
|
<ChevronDown
|
|
|
|
|
className="h-3.5 w-3.5 text-muted-foreground"
|
|
|
|
|
aria-hidden
|
|
|
|
|
/>
|
|
|
|
|
</Button>
|
|
|
|
|
</DropdownMenuTrigger>
|
|
|
|
|
<DropdownMenuContent align="start" className="min-w-[220px]">
|
|
|
|
|
{OPENAI_API_TYPE_OPTIONS.map((option) => (
|
|
|
|
|
<DropdownMenuItem
|
|
|
|
|
key={option.value}
|
|
|
|
|
onSelect={() => onChange({ apiType: option.value })}
|
|
|
|
|
>
|
|
|
|
|
{option.label}
|
|
|
|
|
</DropdownMenuItem>
|
|
|
|
|
))}
|
|
|
|
|
</DropdownMenuContent>
|
|
|
|
|
</DropdownMenu>
|
|
|
|
|
</label>
|
|
|
|
|
) : null}
|
|
|
|
|
{enabled.has("thinking_style") ? (
|
|
|
|
|
<label className="block space-y-1.5">
|
|
|
|
|
<span className="text-[12px] font-medium text-muted-foreground">
|
2026-09-10 11:10:51 +08:00
|
|
|
{tx("settings.providers.thinkingStyle", "Reasoning parameter format")}
|
2026-08-10 19:33:52 +08:00
|
|
|
</span>
|
|
|
|
|
<DropdownMenu>
|
|
|
|
|
<DropdownMenuTrigger asChild>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="outline"
|
|
|
|
|
className="h-9 w-full justify-between rounded-full px-3 text-[13px]"
|
|
|
|
|
>
|
|
|
|
|
<span className="font-mono text-[12px]">
|
|
|
|
|
{thinkingStyleOptions.find(
|
|
|
|
|
(option) => option.value === form.thinkingStyle,
|
|
|
|
|
)?.label ?? form.thinkingStyle}
|
|
|
|
|
</span>
|
|
|
|
|
<ChevronDown
|
|
|
|
|
className="h-3.5 w-3.5 text-muted-foreground"
|
|
|
|
|
aria-hidden
|
|
|
|
|
/>
|
|
|
|
|
</Button>
|
|
|
|
|
</DropdownMenuTrigger>
|
|
|
|
|
<DropdownMenuContent align="start" className="min-w-[220px]">
|
|
|
|
|
{thinkingStyleOptions.map((option) => (
|
|
|
|
|
<DropdownMenuItem
|
|
|
|
|
key={option.value || "default"}
|
|
|
|
|
onSelect={() => onChange({ thinkingStyle: option.value })}
|
|
|
|
|
className="font-mono text-[12px]"
|
|
|
|
|
>
|
|
|
|
|
{option.label}
|
|
|
|
|
</DropdownMenuItem>
|
|
|
|
|
))}
|
|
|
|
|
</DropdownMenuContent>
|
|
|
|
|
</DropdownMenu>
|
|
|
|
|
</label>
|
|
|
|
|
) : null}
|
|
|
|
|
{enabled.has("proxy") ? (
|
|
|
|
|
<label className="block space-y-1.5 md:col-span-2">
|
|
|
|
|
<span className="text-[12px] font-medium text-muted-foreground">
|
|
|
|
|
{tx("settings.providers.proxy", "Network proxy")}
|
|
|
|
|
</span>
|
|
|
|
|
<Input
|
|
|
|
|
value={form.proxy}
|
|
|
|
|
onChange={(event) => onChange({ proxy: event.target.value })}
|
|
|
|
|
placeholder="http://127.0.0.1:7890"
|
|
|
|
|
autoCapitalize="none"
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
autoCorrect="off"
|
|
|
|
|
spellCheck={false}
|
|
|
|
|
className="h-9 rounded-full font-mono text-[12px]"
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
) : null}
|
|
|
|
|
{enabled.has("region") ? (
|
|
|
|
|
<label className="block space-y-1.5">
|
|
|
|
|
<span className="text-[12px] font-medium text-muted-foreground">
|
|
|
|
|
{tx("settings.providers.region", "Region")}
|
|
|
|
|
</span>
|
|
|
|
|
<Input
|
|
|
|
|
value={form.region}
|
|
|
|
|
onChange={(event) => onChange({ region: event.target.value })}
|
|
|
|
|
placeholder="us-east-1"
|
|
|
|
|
autoCapitalize="none"
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
autoCorrect="off"
|
|
|
|
|
spellCheck={false}
|
|
|
|
|
className="h-9 rounded-full font-mono text-[12px]"
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
) : null}
|
|
|
|
|
{enabled.has("profile") ? (
|
|
|
|
|
<label className="block space-y-1.5">
|
|
|
|
|
<span className="text-[12px] font-medium text-muted-foreground">
|
2026-09-10 11:10:51 +08:00
|
|
|
{tx("settings.providers.profile", "AWS profile")}
|
2026-08-10 19:33:52 +08:00
|
|
|
</span>
|
|
|
|
|
<Input
|
|
|
|
|
value={form.profile}
|
|
|
|
|
onChange={(event) => onChange({ profile: event.target.value })}
|
|
|
|
|
placeholder="default"
|
|
|
|
|
autoCapitalize="none"
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
autoCorrect="off"
|
|
|
|
|
spellCheck={false}
|
|
|
|
|
className="h-9 rounded-full font-mono text-[12px]"
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
) : null}
|
|
|
|
|
{enabled.has("extra_headers") ? (
|
|
|
|
|
<label className="block min-w-0 space-y-1.5">
|
|
|
|
|
<span className="text-[12px] font-medium text-muted-foreground">
|
|
|
|
|
{tx("settings.providers.extraHeaders", "Extra headers")}
|
|
|
|
|
</span>
|
2026-09-10 11:10:51 +08:00
|
|
|
<SettingsTextEditor title={tx("settings.providers.extraHeaders", "Extra headers")}
|
|
|
|
|
value={form.extraHeaders} onSave={(value) => onChange({ extraHeaders: value })}
|
2026-08-10 19:33:52 +08:00
|
|
|
placeholder={'{"X-Header":"value"}'}
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
) : null}
|
|
|
|
|
{enabled.has("extra_query") ? (
|
|
|
|
|
<label className="block min-w-0 space-y-1.5">
|
|
|
|
|
<span className="text-[12px] font-medium text-muted-foreground">
|
2026-09-10 11:10:51 +08:00
|
|
|
{tx("settings.providers.extraQuery", "Additional query parameters")}
|
2026-08-10 19:33:52 +08:00
|
|
|
</span>
|
2026-09-10 11:10:51 +08:00
|
|
|
<SettingsTextEditor title={tx("settings.providers.extraQuery", "Additional query parameters")}
|
|
|
|
|
value={form.extraQuery} onSave={(value) => onChange({ extraQuery: value })}
|
2026-08-10 19:33:52 +08:00
|
|
|
placeholder={'{"api-version":"2024-02-01"}'}
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
) : null}
|
|
|
|
|
{enabled.has("extra_body") ? (
|
|
|
|
|
<label className="block min-w-0 space-y-1.5 md:col-span-2">
|
|
|
|
|
<span className="text-[12px] font-medium text-muted-foreground">
|
2026-09-10 11:10:51 +08:00
|
|
|
{tx("settings.providers.extraBody", "Additional body parameters")}
|
2026-08-10 19:33:52 +08:00
|
|
|
</span>
|
2026-09-10 11:10:51 +08:00
|
|
|
<SettingsTextEditor title={tx("settings.providers.extraBody", "Additional body parameters")}
|
|
|
|
|
value={form.extraBody} onSave={(value) => onChange({ extraBody: value })}
|
2026-08-10 19:33:52 +08:00
|
|
|
placeholder={'{"service_tier":"priority"}'}
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-09-11 19:10:15 +08:00
|
|
|
</DisclosureContent>
|
2026-08-10 19:33:52 +08:00
|
|
|
{footer ? (
|
2026-09-10 11:10:51 +08:00
|
|
|
<div className="flex items-center justify-end gap-2 py-3">
|
2026-08-10 19:33:52 +08:00
|
|
|
{footer}
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function ProvidersSettings({
|
|
|
|
|
settings,
|
|
|
|
|
nanobotFeatures,
|
|
|
|
|
featureAction,
|
|
|
|
|
capabilityError,
|
|
|
|
|
expandedProvider,
|
|
|
|
|
providerForms,
|
|
|
|
|
visibleProviderKeys,
|
|
|
|
|
editingProviderKeys,
|
|
|
|
|
providerSaving,
|
|
|
|
|
showBrandLogos,
|
|
|
|
|
remoteBrowserAccess,
|
|
|
|
|
onToggleProvider,
|
|
|
|
|
onToggleProviderKey,
|
|
|
|
|
onToggleProviderKeyEditing,
|
|
|
|
|
onChangeProviderForm,
|
|
|
|
|
onSaveProvider,
|
|
|
|
|
onCreateCustomProvider,
|
|
|
|
|
onProviderOAuthLogin,
|
|
|
|
|
onProviderOAuthLogout,
|
|
|
|
|
}: {
|
|
|
|
|
settings: SettingsPayload;
|
|
|
|
|
nanobotFeatures: NanobotFeaturesPayload | null;
|
|
|
|
|
featureAction: string | null;
|
|
|
|
|
capabilityError: string | null;
|
|
|
|
|
expandedProvider: string | null;
|
|
|
|
|
providerForms: Record<string, ProviderForm>;
|
|
|
|
|
visibleProviderKeys: Record<string, boolean>;
|
|
|
|
|
editingProviderKeys: Record<string, boolean>;
|
|
|
|
|
providerSaving: string | null;
|
|
|
|
|
showBrandLogos: boolean;
|
|
|
|
|
remoteBrowserAccess: boolean;
|
|
|
|
|
onToggleProvider: (provider: string) => void;
|
|
|
|
|
onToggleProviderKey: (provider: string) => void;
|
|
|
|
|
onToggleProviderKeyEditing: (provider: string) => void;
|
|
|
|
|
onChangeProviderForm: (provider: string, value: Partial<ProviderForm>) => void;
|
|
|
|
|
onSaveProvider: (provider: string) => void;
|
|
|
|
|
onCreateCustomProvider: (draft: CustomProviderDraft) => Promise<boolean>;
|
|
|
|
|
onProviderOAuthLogin: (provider: string) => void;
|
|
|
|
|
onProviderOAuthLogout: (provider: string) => void;
|
|
|
|
|
}) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const tx = (key: string, fallback: string) => t(key, { defaultValue: fallback });
|
|
|
|
|
const [creatingCustomProvider, setCreatingCustomProvider] = useState(false);
|
2026-09-19 14:46:31 +08:00
|
|
|
const [addingProvider, setAddingProvider] = useState(false);
|
|
|
|
|
const [providerToAdd, setProviderToAdd] = useState<string | null>(null);
|
2026-09-19 14:48:42 +08:00
|
|
|
const [providerSearch, setProviderSearch] = useState("");
|
2026-08-10 19:33:52 +08:00
|
|
|
const [customProviderKeyVisible, setCustomProviderKeyVisible] = useState(false);
|
|
|
|
|
const [customProviderDraft, setCustomProviderDraft] = useState<CustomProviderDraft>(
|
|
|
|
|
emptyCustomProviderDraft,
|
|
|
|
|
);
|
|
|
|
|
const configuredProviders = settings.providers.filter((provider) => provider.configured);
|
|
|
|
|
const unconfiguredProviders = useMemo(
|
|
|
|
|
() =>
|
2026-09-02 04:19:12 +08:00
|
|
|
orderUnconfiguredProviders(
|
2026-08-10 19:33:52 +08:00
|
|
|
settings.providers.filter(
|
|
|
|
|
(provider) => !provider.configured && provider.name !== "custom",
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
[settings.providers],
|
|
|
|
|
);
|
|
|
|
|
const selectedUnconfiguredProvider =
|
|
|
|
|
unconfiguredProviders.find((provider) => provider.name === expandedProvider) ?? null;
|
|
|
|
|
const customProviderSaving = providerSaving === CUSTOM_PROVIDER_CREATION_KEY;
|
2026-09-19 14:46:31 +08:00
|
|
|
const selectedProviderToAdd = settings.providers.find((provider) => provider.name === providerToAdd);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
// Existing save/cancel actions own expandedProvider; close the add flow when they finish.
|
|
|
|
|
if (addingProvider && providerToAdd && expandedProvider !== providerToAdd) {
|
|
|
|
|
setAddingProvider(false);
|
|
|
|
|
setProviderToAdd(null);
|
|
|
|
|
}
|
|
|
|
|
}, [addingProvider, providerToAdd, expandedProvider]);
|
|
|
|
|
const closeAddProvider = () => {
|
|
|
|
|
setAddingProvider(false);
|
|
|
|
|
setProviderToAdd(null);
|
|
|
|
|
setCreatingCustomProvider(false);
|
|
|
|
|
setCustomProviderDraft(emptyCustomProviderDraft());
|
|
|
|
|
setCustomProviderKeyVisible(false);
|
|
|
|
|
if (providerToAdd && expandedProvider === providerToAdd) onToggleProvider(providerToAdd);
|
|
|
|
|
};
|
|
|
|
|
const backToProviderPicker = () => {
|
|
|
|
|
setProviderToAdd(null);
|
|
|
|
|
setCreatingCustomProvider(false);
|
|
|
|
|
if (providerToAdd && expandedProvider === providerToAdd) onToggleProvider(providerToAdd);
|
|
|
|
|
};
|
2026-08-10 19:33:52 +08:00
|
|
|
const toggleProvider = (providerName: string) => {
|
|
|
|
|
setCreatingCustomProvider(false);
|
|
|
|
|
onToggleProvider(providerName);
|
|
|
|
|
};
|
|
|
|
|
const beginCustomProviderCreation = () => {
|
|
|
|
|
if (expandedProvider) onToggleProvider(expandedProvider);
|
|
|
|
|
setCustomProviderDraft(emptyCustomProviderDraft());
|
|
|
|
|
setCustomProviderKeyVisible(false);
|
|
|
|
|
setCreatingCustomProvider(true);
|
|
|
|
|
};
|
|
|
|
|
const cancelCustomProviderCreation = () => {
|
|
|
|
|
setCreatingCustomProvider(false);
|
|
|
|
|
setCustomProviderDraft(emptyCustomProviderDraft());
|
|
|
|
|
setCustomProviderKeyVisible(false);
|
2026-09-19 14:46:31 +08:00
|
|
|
setAddingProvider(false);
|
2026-08-10 19:33:52 +08:00
|
|
|
};
|
|
|
|
|
const saveCustomProvider = async () => {
|
|
|
|
|
if (customProviderSaving) return;
|
|
|
|
|
if (await onCreateCustomProvider(customProviderDraft)) {
|
|
|
|
|
cancelCustomProviderCreation();
|
|
|
|
|
}
|
|
|
|
|
};
|
2026-09-19 14:46:31 +08:00
|
|
|
const renderProviderRow = (provider: SettingsPayload["providers"][number], contentOnly = false) => {
|
|
|
|
|
const expanded = expandedProvider === provider.name && !addingProvider;
|
2026-08-10 19:33:52 +08:00
|
|
|
const form = providerForms[provider.name] ?? providerFormFromRow(provider);
|
|
|
|
|
const saving = providerSaving === provider.name;
|
|
|
|
|
const isOauthProvider = provider.auth_type === "oauth";
|
|
|
|
|
const supportsOauthAdvancedSettings =
|
|
|
|
|
isOauthProvider && OAUTH_PROXY_PROVIDERS.has(provider.name);
|
|
|
|
|
const keyVisible = !!visibleProviderKeys[provider.name];
|
|
|
|
|
const editingKey = !provider.configured || !!editingProviderKeys[provider.name];
|
|
|
|
|
const apiKeyRequired = provider.api_key_required ?? true;
|
|
|
|
|
const apiKey = form.apiKey.trim();
|
|
|
|
|
const apiBase = form.apiBase.trim();
|
|
|
|
|
const advancedFields = provider.advanced_fields ?? [];
|
|
|
|
|
const oauthSettingsDirty = isOauthProvider && (
|
|
|
|
|
form.proxy.trim() !== (provider.proxy ?? "").trim()
|
|
|
|
|
|| form.extraBody.trim() !== providerJsonValue(provider.extra_body).trim()
|
|
|
|
|
);
|
|
|
|
|
const oauthSettingsSaving = saving && oauthSettingsDirty;
|
|
|
|
|
const oauthActionBusy = saving && !oauthSettingsSaving;
|
|
|
|
|
const missingRequiredApiKey = !isOauthProvider && apiKeyRequired && !provider.configured && !apiKey;
|
|
|
|
|
const hasOptionalProviderSetting = Boolean(
|
|
|
|
|
apiKey
|
|
|
|
|
|| apiBase
|
|
|
|
|
|| form.proxy.trim()
|
|
|
|
|
|| form.extraHeaders.trim()
|
|
|
|
|
|| form.extraBody.trim()
|
|
|
|
|
|| form.extraQuery.trim()
|
|
|
|
|
|| form.thinkingStyle.trim()
|
|
|
|
|
|| form.region.trim()
|
|
|
|
|
|| form.profile.trim(),
|
|
|
|
|
);
|
|
|
|
|
const missingOptionalCredential =
|
|
|
|
|
!isOauthProvider
|
|
|
|
|
&& !apiKeyRequired
|
|
|
|
|
&& !provider.configured
|
|
|
|
|
&& !hasOptionalProviderSetting;
|
|
|
|
|
const supportName = provider.name === "bedrock"
|
|
|
|
|
? "bedrock"
|
|
|
|
|
: provider.name === "azure_openai"
|
|
|
|
|
? "azure"
|
|
|
|
|
: null;
|
|
|
|
|
const supportFeature = supportName
|
|
|
|
|
? (nanobotFeatures?.features ?? []).find((feature) => feature.name === supportName)
|
|
|
|
|
: null;
|
2026-09-19 14:46:31 +08:00
|
|
|
const content = (
|
|
|
|
|
<>
|
2026-08-10 19:33:52 +08:00
|
|
|
{supportFeature && !supportFeature.installed ? (
|
|
|
|
|
<CapabilityInstallNotice
|
2026-09-10 11:10:51 +08:00
|
|
|
title={tx("settings.capabilities.providerSupport", "Provider dependencies")}
|
2026-08-10 19:33:52 +08:00
|
|
|
description={tx(
|
|
|
|
|
"settings.capabilities.providerInstallOnSave",
|
2026-09-10 11:10:51 +08:00
|
|
|
"Required packages will be installed automatically when you save this provider.",
|
2026-08-10 19:33:52 +08:00
|
|
|
)}
|
|
|
|
|
installing={featureAction === `enable:${supportName}`}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
{supportName && capabilityError ? (
|
|
|
|
|
<p className="text-[12px] text-destructive">{capabilityError}</p>
|
|
|
|
|
) : null}
|
|
|
|
|
{isOauthProvider ? (
|
|
|
|
|
<>
|
2026-08-14 07:07:09 +09:00
|
|
|
<div className="flex flex-col gap-3 rounded-floating border border-border/45 bg-background/75 px-4 py-3 sm:flex-row sm:items-center sm:justify-between">
|
2026-08-10 19:33:52 +08:00
|
|
|
<div className="min-w-0">
|
|
|
|
|
<p className="text-[13px] font-semibold text-foreground">
|
|
|
|
|
{tx("settings.oauth.authentication", "OAuth authentication")}
|
|
|
|
|
</p>
|
|
|
|
|
<p className="mt-1 text-[12px] text-muted-foreground">
|
|
|
|
|
{provider.configured
|
|
|
|
|
? t("settings.oauth.signedInAs", {
|
|
|
|
|
account: provider.oauth_account || provider.label,
|
|
|
|
|
defaultValue: "Signed in as {{account}}",
|
|
|
|
|
})
|
|
|
|
|
: provider.name === "openai_codex" && remoteBrowserAccess
|
|
|
|
|
? tx(
|
|
|
|
|
"settings.oauth.codexRemoteSignInHelp",
|
|
|
|
|
"Sign in through this browser, then paste the full localhost callback URL back into nanobot.",
|
|
|
|
|
)
|
|
|
|
|
: provider.name === "xai_grok" && remoteBrowserAccess
|
|
|
|
|
? tx(
|
|
|
|
|
"settings.oauth.remoteSignInHelp",
|
|
|
|
|
"Select Sign in to open xAI on your computer, then paste the authorization code shown after login.",
|
|
|
|
|
)
|
|
|
|
|
: tx("settings.oauth.signInHelp", "Sign in from this device; no API key is stored in config.")}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex shrink-0 justify-end gap-2">
|
|
|
|
|
{provider.configured ? (
|
|
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
onClick={() => onProviderOAuthLogout(provider.name)}
|
|
|
|
|
disabled={saving}
|
|
|
|
|
className="rounded-full"
|
|
|
|
|
>
|
|
|
|
|
{tx("settings.oauth.signOut", "Sign out")}
|
|
|
|
|
</Button>
|
|
|
|
|
) : null}
|
|
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="outline"
|
|
|
|
|
onClick={() => onProviderOAuthLogin(provider.name)}
|
|
|
|
|
disabled={saving || oauthSettingsDirty || !provider.oauth_login_supported}
|
|
|
|
|
title={
|
|
|
|
|
oauthSettingsDirty
|
|
|
|
|
? tx(
|
|
|
|
|
"settings.providers.saveAdvancedBeforeSignIn",
|
|
|
|
|
"Save advanced changes before signing in.",
|
|
|
|
|
)
|
|
|
|
|
: undefined
|
|
|
|
|
}
|
|
|
|
|
className="rounded-full"
|
|
|
|
|
>
|
|
|
|
|
{oauthActionBusy ? (
|
|
|
|
|
<Loader2 className="mr-1.5 h-3.5 w-3.5 animate-spin" aria-hidden />
|
|
|
|
|
) : null}
|
|
|
|
|
{oauthActionBusy
|
|
|
|
|
? tx("settings.oauth.signingIn", "Signing in...")
|
|
|
|
|
: provider.configured
|
|
|
|
|
? tx("settings.oauth.signInAgain", "Sign in again")
|
|
|
|
|
: tx("settings.oauth.signIn", "Sign in")}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<ProviderRequestOptions
|
|
|
|
|
providerName={provider.name}
|
|
|
|
|
form={form}
|
|
|
|
|
onChange={(value) => onChangeProviderForm(provider.name, value)}
|
|
|
|
|
/>
|
|
|
|
|
{supportsOauthAdvancedSettings ? (
|
|
|
|
|
<ProviderAdvancedOptions
|
|
|
|
|
fields={advancedFields}
|
|
|
|
|
form={form}
|
|
|
|
|
onChange={(value) => onChangeProviderForm(provider.name, value)}
|
|
|
|
|
footer={
|
|
|
|
|
<>
|
|
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
onClick={() => toggleProvider(provider.name)}
|
|
|
|
|
disabled={saving}
|
|
|
|
|
className="rounded-full"
|
|
|
|
|
>
|
|
|
|
|
{t("settings.actions.cancel")}
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="outline"
|
|
|
|
|
onClick={() => onSaveProvider(provider.name)}
|
|
|
|
|
disabled={saving || !oauthSettingsDirty}
|
|
|
|
|
className="rounded-full"
|
|
|
|
|
>
|
|
|
|
|
{oauthSettingsSaving ? (
|
|
|
|
|
<Loader2
|
|
|
|
|
className="mr-1.5 h-3.5 w-3.5 animate-spin"
|
|
|
|
|
aria-hidden
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
{oauthSettingsSaving
|
|
|
|
|
? t("settings.actions.saving")
|
|
|
|
|
: tx("settings.providers.saveProvider", "Save provider")}
|
|
|
|
|
</Button>
|
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
{provider.is_custom ? (
|
|
|
|
|
<label className="block space-y-1.5">
|
|
|
|
|
<span className="text-[12px] font-medium text-muted-foreground">
|
|
|
|
|
{tx("settings.providers.customProviderName", "Provider name")}
|
|
|
|
|
</span>
|
|
|
|
|
<Input
|
|
|
|
|
value={form.displayName}
|
|
|
|
|
onChange={(event) =>
|
|
|
|
|
onChangeProviderForm(provider.name, { displayName: event.target.value })
|
|
|
|
|
}
|
|
|
|
|
className="h-9 rounded-full text-[13px]"
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
) : null}
|
|
|
|
|
<label className="block space-y-1.5">
|
|
|
|
|
<span className="text-[12px] font-medium text-muted-foreground">
|
|
|
|
|
{t("settings.byok.apiKey")}
|
|
|
|
|
</span>
|
|
|
|
|
<div className="relative">
|
|
|
|
|
{editingKey ? (
|
|
|
|
|
<>
|
|
|
|
|
<Input
|
|
|
|
|
type={keyVisible ? "text" : "password"}
|
|
|
|
|
value={form.apiKey}
|
|
|
|
|
onChange={(event) =>
|
|
|
|
|
onChangeProviderForm(provider.name, { apiKey: event.target.value })
|
|
|
|
|
}
|
|
|
|
|
placeholder={
|
|
|
|
|
provider.configured
|
|
|
|
|
? t("settings.byok.apiKeyConfiguredPlaceholder")
|
|
|
|
|
: t("settings.byok.apiKeyPlaceholder")
|
|
|
|
|
}
|
|
|
|
|
className="h-9 rounded-full pr-11 text-[13px]"
|
|
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
|
|
|
|
onClick={() => onToggleProviderKey(provider.name)}
|
|
|
|
|
aria-label={
|
|
|
|
|
keyVisible
|
|
|
|
|
? t("settings.byok.hideApiKey")
|
|
|
|
|
: t("settings.byok.showApiKey")
|
|
|
|
|
}
|
2026-10-06 00:46:38 +08:00
|
|
|
className="absolute right-1 top-1/2 h-7 w-7 -translate-y-1/2 rounded-full text-muted-foreground hover:text-foreground"
|
2026-08-10 19:33:52 +08:00
|
|
|
>
|
|
|
|
|
{keyVisible ? (
|
|
|
|
|
<EyeOff className="h-3.5 w-3.5" aria-hidden />
|
|
|
|
|
) : (
|
|
|
|
|
<Eye className="h-3.5 w-3.5" aria-hidden />
|
|
|
|
|
)}
|
|
|
|
|
</Button>
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<div className="flex h-9 items-center rounded-full border border-input bg-background px-3 pr-11 text-[13px] text-muted-foreground">
|
|
|
|
|
{provider.api_key_hint ?? t("settings.byok.configuredKeyHint")}
|
|
|
|
|
</div>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
|
|
|
|
onClick={() => onToggleProviderKeyEditing(provider.name)}
|
|
|
|
|
aria-label={t("settings.actions.edit")}
|
2026-10-06 00:46:38 +08:00
|
|
|
className="absolute right-1 top-1/2 h-7 w-7 -translate-y-1/2 rounded-full text-muted-foreground hover:text-foreground"
|
2026-08-10 19:33:52 +08:00
|
|
|
>
|
|
|
|
|
<Pencil className="h-3.5 w-3.5" aria-hidden />
|
|
|
|
|
</Button>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</label>
|
|
|
|
|
<label className="block space-y-1.5">
|
|
|
|
|
<span className="text-[12px] font-medium text-muted-foreground">
|
|
|
|
|
{t("settings.byok.apiBase")}
|
|
|
|
|
</span>
|
|
|
|
|
<Input
|
|
|
|
|
value={form.apiBase}
|
|
|
|
|
onChange={(event) =>
|
|
|
|
|
onChangeProviderForm(provider.name, { apiBase: event.target.value })
|
|
|
|
|
}
|
|
|
|
|
placeholder={provider.default_api_base ?? t("settings.byok.apiBasePlaceholder")}
|
|
|
|
|
className="h-9 rounded-full text-[13px]"
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
<ProviderRequestOptions
|
|
|
|
|
providerName={provider.name}
|
|
|
|
|
form={form}
|
|
|
|
|
onChange={(value) => onChangeProviderForm(provider.name, value)}
|
|
|
|
|
/>
|
2026-10-09 13:59:13 +08:00
|
|
|
{provider.provider_api_configurable && (
|
|
|
|
|
<ProviderAPIControl
|
|
|
|
|
value={form.api}
|
|
|
|
|
onChange={(api) => onChangeProviderForm(provider.name, { api })}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2026-08-10 19:33:52 +08:00
|
|
|
<ProviderAdvancedOptions
|
|
|
|
|
fields={advancedFields}
|
|
|
|
|
form={form}
|
|
|
|
|
onChange={(value) => onChangeProviderForm(provider.name, value)}
|
|
|
|
|
/>
|
|
|
|
|
<div className="flex items-center justify-end gap-2">
|
|
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
onClick={() => toggleProvider(provider.name)}
|
|
|
|
|
className="rounded-full"
|
|
|
|
|
>
|
|
|
|
|
{t("settings.actions.cancel")}
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="outline"
|
|
|
|
|
onClick={() => onSaveProvider(provider.name)}
|
|
|
|
|
disabled={
|
|
|
|
|
saving
|
|
|
|
|
|| missingRequiredApiKey
|
|
|
|
|
|| missingOptionalCredential
|
|
|
|
|
|| (provider.is_custom && !form.displayName.trim())
|
|
|
|
|
}
|
|
|
|
|
className="rounded-full"
|
|
|
|
|
>
|
|
|
|
|
{saving
|
|
|
|
|
? t("settings.actions.saving")
|
|
|
|
|
: tx("settings.providers.saveProvider", "Save provider")}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
2026-09-19 14:46:31 +08:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
if (contentOnly) return content;
|
|
|
|
|
return (
|
|
|
|
|
<Dialog key={provider.name} open={expanded} onOpenChange={(open) => {
|
|
|
|
|
if (open !== expanded) toggleProvider(provider.name);
|
|
|
|
|
}}>
|
|
|
|
|
<DialogTrigger asChild>
|
|
|
|
|
<button type="button" aria-label={provider.label}
|
|
|
|
|
className="settings-list-row flex w-full items-center justify-between gap-4 py-2.5 text-left transition-colors settings-hover">
|
|
|
|
|
<span className="flex min-w-0 items-center gap-3">
|
|
|
|
|
<ProviderIcon provider={provider.name} showBrandLogos={showBrandLogos} />
|
|
|
|
|
<span className="truncate text-[14px] font-medium leading-5 text-foreground">{provider.label}</span>
|
|
|
|
|
</span>
|
|
|
|
|
<span className="shrink-0 px-2 py-1 text-[13px] font-normal leading-5 text-muted-foreground">{t("settings.configure")}</span>
|
|
|
|
|
</button>
|
|
|
|
|
</DialogTrigger>
|
|
|
|
|
{expanded ? <DialogContent aria-describedby={undefined} className="max-h-[85dvh] w-[min(calc(100vw-2rem),40rem)] max-w-none overflow-y-auto">
|
|
|
|
|
<DialogHeader><DialogTitle>{provider.label}</DialogTitle></DialogHeader>
|
|
|
|
|
{content}
|
|
|
|
|
</DialogContent> : null}
|
2026-09-10 11:10:51 +08:00
|
|
|
</Dialog>
|
2026-08-10 19:33:52 +08:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
const customProviderForm = creatingCustomProvider ? (
|
2026-09-10 11:10:51 +08:00
|
|
|
<div className="space-y-3">
|
2026-08-10 19:33:52 +08:00
|
|
|
<label className="block space-y-1.5">
|
|
|
|
|
<span className="text-[12px] font-medium text-muted-foreground">
|
|
|
|
|
{tx("settings.providers.customProviderName", "Provider name")}
|
|
|
|
|
</span>
|
|
|
|
|
<Input
|
|
|
|
|
autoFocus
|
|
|
|
|
value={customProviderDraft.name}
|
|
|
|
|
onChange={(event) =>
|
|
|
|
|
setCustomProviderDraft((current) => ({
|
|
|
|
|
...current,
|
|
|
|
|
name: event.target.value,
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
placeholder={tx(
|
|
|
|
|
"settings.providers.customProviderNamePlaceholder",
|
|
|
|
|
"My model provider",
|
|
|
|
|
)}
|
|
|
|
|
className="h-9 rounded-full text-[13px]"
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
<label className="block space-y-1.5">
|
|
|
|
|
<span className="text-[12px] font-medium text-muted-foreground">
|
|
|
|
|
{t("settings.byok.apiBase")}
|
|
|
|
|
</span>
|
|
|
|
|
<Input
|
|
|
|
|
value={customProviderDraft.apiBase}
|
|
|
|
|
onChange={(event) =>
|
|
|
|
|
setCustomProviderDraft((current) => ({
|
|
|
|
|
...current,
|
|
|
|
|
apiBase: event.target.value,
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
placeholder="https://api.example.com/v1"
|
|
|
|
|
autoCapitalize="none"
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
autoCorrect="off"
|
|
|
|
|
spellCheck={false}
|
|
|
|
|
className="h-9 rounded-full text-[13px]"
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
<label className="block space-y-1.5">
|
|
|
|
|
<span className="text-[12px] font-medium text-muted-foreground">
|
|
|
|
|
{t("settings.byok.apiKey")}
|
|
|
|
|
</span>
|
|
|
|
|
<div className="relative">
|
|
|
|
|
<Input
|
|
|
|
|
type={customProviderKeyVisible ? "text" : "password"}
|
|
|
|
|
value={customProviderDraft.apiKey}
|
|
|
|
|
onChange={(event) =>
|
|
|
|
|
setCustomProviderDraft((current) => ({
|
|
|
|
|
...current,
|
|
|
|
|
apiKey: event.target.value,
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
placeholder={t("settings.byok.apiKeyPlaceholder")}
|
|
|
|
|
autoCapitalize="none"
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
autoCorrect="off"
|
|
|
|
|
spellCheck={false}
|
|
|
|
|
className="h-9 rounded-full pr-11 text-[13px]"
|
|
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
|
|
|
|
onClick={() => setCustomProviderKeyVisible((visible) => !visible)}
|
|
|
|
|
aria-label={
|
|
|
|
|
customProviderKeyVisible
|
|
|
|
|
? t("settings.byok.hideApiKey")
|
|
|
|
|
: t("settings.byok.showApiKey")
|
|
|
|
|
}
|
2026-10-06 00:46:38 +08:00
|
|
|
className="absolute right-1 top-1/2 h-7 w-7 -translate-y-1/2 rounded-full text-muted-foreground hover:text-foreground"
|
2026-08-10 19:33:52 +08:00
|
|
|
>
|
|
|
|
|
{customProviderKeyVisible ? (
|
|
|
|
|
<EyeOff className="h-3.5 w-3.5" aria-hidden />
|
|
|
|
|
) : (
|
|
|
|
|
<Eye className="h-3.5 w-3.5" aria-hidden />
|
|
|
|
|
)}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</label>
|
2026-10-09 13:59:13 +08:00
|
|
|
{settings.provider_api_configuration_supported && (
|
|
|
|
|
<ProviderAPIControl
|
|
|
|
|
value={customProviderDraft.api}
|
|
|
|
|
onChange={(api) => setCustomProviderDraft((current) => ({ ...current, api }))}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2026-08-10 19:33:52 +08:00
|
|
|
<ProviderAdvancedOptions
|
|
|
|
|
fields={CUSTOM_PROVIDER_ADVANCED_FIELDS}
|
|
|
|
|
form={customProviderDraft}
|
|
|
|
|
onChange={(value) =>
|
|
|
|
|
setCustomProviderDraft((current) => ({ ...current, ...value }))
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
<div className="flex items-center justify-end gap-2">
|
|
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
onClick={cancelCustomProviderCreation}
|
|
|
|
|
disabled={customProviderSaving}
|
|
|
|
|
className="rounded-full"
|
|
|
|
|
>
|
|
|
|
|
{t("settings.actions.cancel")}
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="outline"
|
|
|
|
|
onClick={saveCustomProvider}
|
|
|
|
|
disabled={
|
|
|
|
|
customProviderSaving ||
|
|
|
|
|
!customProviderDraft.name.trim() ||
|
|
|
|
|
!customProviderDraft.apiBase.trim()
|
|
|
|
|
}
|
|
|
|
|
className="rounded-full"
|
|
|
|
|
>
|
|
|
|
|
{customProviderSaving
|
|
|
|
|
? t("settings.actions.saving")
|
|
|
|
|
: tx("settings.providers.saveProvider", "Save provider")}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
) : null;
|
|
|
|
|
return (
|
|
|
|
|
<div className="space-y-6">
|
2026-09-02 04:19:12 +08:00
|
|
|
<section>
|
2026-08-10 19:33:52 +08:00
|
|
|
<SettingsSectionTitle>
|
|
|
|
|
{tx("settings.providers.title", "Model providers")}
|
|
|
|
|
</SettingsSectionTitle>
|
|
|
|
|
<SettingsGroup>
|
2026-09-19 14:46:31 +08:00
|
|
|
{configuredProviders.map((provider) => renderProviderRow(provider))}
|
|
|
|
|
{selectedUnconfiguredProvider && !addingProvider
|
2026-08-10 19:33:52 +08:00
|
|
|
? renderProviderRow(selectedUnconfiguredProvider)
|
|
|
|
|
: null}
|
2026-09-19 14:46:31 +08:00
|
|
|
<ProviderSetupPanel
|
|
|
|
|
open={addingProvider}
|
|
|
|
|
onOpenChange={(open) => {
|
|
|
|
|
if (open) {
|
|
|
|
|
setProviderToAdd(null);
|
2026-09-19 14:48:42 +08:00
|
|
|
setProviderSearch("");
|
2026-09-19 14:46:31 +08:00
|
|
|
setAddingProvider(true);
|
|
|
|
|
} else closeAddProvider();
|
|
|
|
|
}}
|
|
|
|
|
title={creatingCustomProvider
|
|
|
|
|
? tx("settings.providers.customProvider", "Custom provider")
|
|
|
|
|
: selectedProviderToAdd?.label ?? tx("settings.providers.addProvider", "Add provider")}
|
|
|
|
|
onBack={creatingCustomProvider || selectedProviderToAdd ? backToProviderPicker : undefined}
|
|
|
|
|
trigger={
|
2026-08-10 19:33:52 +08:00
|
|
|
<button
|
|
|
|
|
type="button"
|
2026-09-19 14:46:31 +08:00
|
|
|
className="settings-list-row flex w-full items-center gap-3 py-2.5 text-left transition-colors settings-hover"
|
2026-08-10 19:33:52 +08:00
|
|
|
>
|
|
|
|
|
<span className="flex min-w-0 items-center gap-3">
|
2026-09-19 13:10:31 +08:00
|
|
|
<span className="grid h-8 w-8 shrink-0 place-items-center rounded-[9px] bg-muted text-muted-foreground">
|
2026-08-10 19:33:52 +08:00
|
|
|
<Plus className="h-5 w-5" aria-hidden />
|
|
|
|
|
</span>
|
2026-09-10 11:10:51 +08:00
|
|
|
<span className="truncate text-[14px] font-medium text-foreground">
|
2026-09-02 04:19:12 +08:00
|
|
|
{tx(
|
2026-09-19 14:46:31 +08:00
|
|
|
"settings.providers.addProvider",
|
|
|
|
|
"Add provider",
|
2026-09-02 04:19:12 +08:00
|
|
|
)}
|
2026-08-10 19:33:52 +08:00
|
|
|
</span>
|
|
|
|
|
</span>
|
|
|
|
|
</button>
|
2026-09-19 14:46:31 +08:00
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
{creatingCustomProvider || selectedProviderToAdd ? (
|
|
|
|
|
<div className="min-h-0 space-y-4 overflow-y-auto overscroll-contain px-5 pb-5">
|
|
|
|
|
{creatingCustomProvider ? customProviderForm : renderProviderRow(selectedProviderToAdd!, true)}
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<ProviderSearchList providers={unconfiguredProviders} showBrandLogos={showBrandLogos}
|
2026-09-19 14:48:42 +08:00
|
|
|
query={providerSearch} onQueryChange={setProviderSearch}
|
2026-09-19 14:46:31 +08:00
|
|
|
onSelect={(name) => {
|
|
|
|
|
setProviderToAdd(name);
|
|
|
|
|
onToggleProvider(name);
|
|
|
|
|
}}
|
|
|
|
|
onCustom={beginCustomProviderCreation} onClose={closeAddProvider} />
|
|
|
|
|
)}
|
|
|
|
|
</ProviderSetupPanel>
|
2026-08-10 19:33:52 +08:00
|
|
|
</SettingsGroup>
|
|
|
|
|
</section>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-19 14:46:31 +08:00
|
|
|
function ProviderSetupPanel({ open, onOpenChange, title, trigger, onBack, children }: {
|
|
|
|
|
open: boolean;
|
|
|
|
|
onOpenChange: (open: boolean) => void;
|
|
|
|
|
title: string;
|
|
|
|
|
trigger: ReactNode;
|
|
|
|
|
onBack?: () => void;
|
|
|
|
|
children: ReactNode;
|
|
|
|
|
}) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const mobile = useMediaQuery("(max-width: 639px)");
|
|
|
|
|
const [container, setContainer] = useState<HTMLDivElement | null>(null);
|
|
|
|
|
const configuring = Boolean(onBack);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (configuring) container?.focus({ preventScroll: true });
|
|
|
|
|
}, [configuring, container]);
|
|
|
|
|
const content = <>
|
|
|
|
|
<div className="flex h-16 shrink-0 items-center gap-2 px-5 pr-12">
|
|
|
|
|
{onBack ? <Button type="button" variant="ghost" size="icon" onClick={onBack}
|
|
|
|
|
aria-label={t("settings.providers.backToProviders", { defaultValue: "Back to providers" })}
|
|
|
|
|
className="-ml-2 h-9 w-9 shrink-0 rounded-full">
|
|
|
|
|
<ChevronLeft className="h-4 w-4" aria-hidden />
|
|
|
|
|
</Button> : null}
|
|
|
|
|
<DialogTitle className="truncate text-base font-semibold">{title}</DialogTitle>
|
|
|
|
|
</div>
|
|
|
|
|
{children}
|
|
|
|
|
</>;
|
|
|
|
|
const focusOnOpen = (event: Event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
const target = !mobile ? container?.querySelector<HTMLInputElement>("input") : null;
|
|
|
|
|
(target ?? container)?.focus({ preventScroll: true });
|
|
|
|
|
};
|
|
|
|
|
return <Dialog open={open} onOpenChange={onOpenChange}>
|
|
|
|
|
<DialogTrigger asChild>{trigger}</DialogTrigger>
|
|
|
|
|
{mobile ? <SheetContent side="bottom" ref={setContainer} aria-describedby={undefined}
|
|
|
|
|
onOpenAutoFocus={focusOnOpen}
|
2026-09-19 14:48:42 +08:00
|
|
|
className={cn("mx-auto max-h-[85dvh] max-w-md gap-0 overflow-hidden rounded-t-3xl pb-[env(safe-area-inset-bottom)] outline-none", !configuring && "h-[min(36rem,85dvh)]")}
|
2026-09-19 14:46:31 +08:00
|
|
|
closeButtonClassName="grid h-9 w-9 place-items-center right-3 top-3 rounded-full">
|
|
|
|
|
<FloatingPortalContext.Provider value={container}>{content}</FloatingPortalContext.Provider>
|
|
|
|
|
</SheetContent> : <DialogContent ref={setContainer} aria-describedby={undefined}
|
|
|
|
|
onOpenAutoFocus={focusOnOpen}
|
2026-09-19 14:48:42 +08:00
|
|
|
className={cn("flex max-h-[85dvh] w-[min(28rem,calc(100vw-2rem))] max-w-none flex-col gap-0 overflow-hidden p-0 outline-none", !configuring && "h-[min(32rem,85dvh)]")}>
|
2026-09-19 14:46:31 +08:00
|
|
|
{content}
|
|
|
|
|
</DialogContent>}
|
|
|
|
|
</Dialog>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const PROVIDER_SEARCH_ALIASES: Record<string, string> = {
|
|
|
|
|
anthropic: "claude 克劳德 克勞德",
|
|
|
|
|
deepseek: "深度求索 深度探索",
|
|
|
|
|
zhipu: "智谱 智譜 glm z.ai",
|
|
|
|
|
dashscope: "阿里 通义 通義 千问 千問 qwen",
|
|
|
|
|
moonshot: "月之暗面 kimi",
|
|
|
|
|
kimi_coding: "月之暗面 kimi coding",
|
|
|
|
|
volcengine: "火山引擎 豆包 doubao",
|
|
|
|
|
volcengine_coding_plan: "火山引擎 豆包 doubao",
|
|
|
|
|
minimax: "海螺 稀宇",
|
|
|
|
|
minimax_anthropic: "海螺 稀宇",
|
|
|
|
|
siliconflow: "硅基流动 矽基流動 硅基流動",
|
|
|
|
|
stepfun: "阶跃星辰 階躍星辰",
|
|
|
|
|
xiaomi_mimo: "小米 mimo",
|
|
|
|
|
longcat: "美团 美團",
|
|
|
|
|
qianfan: "百度 千帆 文心",
|
|
|
|
|
ant_ling: "蚂蚁 螞蟻 百灵 百靈",
|
|
|
|
|
};
|
|
|
|
|
|
2026-09-19 14:48:42 +08:00
|
|
|
function ProviderSearchList({ providers, showBrandLogos, query, onQueryChange, onSelect, onCustom, onClose }: {
|
2026-09-19 14:46:31 +08:00
|
|
|
providers: SettingsPayload["providers"];
|
|
|
|
|
showBrandLogos: boolean;
|
2026-09-19 14:48:42 +08:00
|
|
|
query: string;
|
|
|
|
|
onQueryChange: (query: string) => void;
|
2026-09-19 14:46:31 +08:00
|
|
|
onSelect: (name: string) => void;
|
|
|
|
|
onCustom: () => void;
|
|
|
|
|
onClose: () => void;
|
|
|
|
|
}) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const mobile = useMediaQuery("(max-width: 639px)");
|
|
|
|
|
const inputRef = useRef<HTMLInputElement>(null);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!mobile) inputRef.current?.focus({ preventScroll: true });
|
|
|
|
|
}, [mobile]);
|
|
|
|
|
const filtered = useMemo(() => {
|
|
|
|
|
const normalize = (value: string) => value.toLocaleLowerCase().replace(/[\s_.-]+/g, "");
|
|
|
|
|
return providers.filter((provider) => normalize(
|
|
|
|
|
`${provider.name} ${provider.label} ${PROVIDER_SEARCH_ALIASES[provider.name] ?? ""}`,
|
|
|
|
|
).includes(normalize(query)));
|
|
|
|
|
}, [providers, query]);
|
|
|
|
|
const values = useMemo(() => filtered.map((provider) => provider.name), [filtered]);
|
|
|
|
|
const { inputProps, listProps, getOptionProps } = useComboboxNavigation({
|
|
|
|
|
open: true, values, onSelect, onClose,
|
|
|
|
|
});
|
|
|
|
|
const searchLabel = t("settings.providers.searchPlaceholder", { defaultValue: "Search providers" });
|
|
|
|
|
return <>
|
|
|
|
|
<div className="relative mx-5 mb-3 shrink-0">
|
|
|
|
|
<Search className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" aria-hidden />
|
2026-09-19 14:48:42 +08:00
|
|
|
<Input {...inputProps} ref={inputRef} value={query} onChange={(event) => onQueryChange(event.target.value)}
|
2026-09-19 14:46:31 +08:00
|
|
|
aria-label={searchLabel} placeholder={searchLabel}
|
|
|
|
|
className="h-10 rounded-xl border-0 bg-muted/60 pl-9 text-sm shadow-none focus-visible:ring-1" />
|
|
|
|
|
</div>
|
|
|
|
|
<div {...listProps} aria-label={searchLabel}
|
|
|
|
|
className="min-h-0 flex-1 overflow-y-auto overscroll-contain px-3 pb-3 scrollbar-thin scrollbar-track-transparent">
|
|
|
|
|
{filtered.map((provider) => <ComboboxOption key={provider.name} {...getOptionProps(provider.name)}
|
2026-09-19 14:48:42 +08:00
|
|
|
aria-label={provider.label} className="min-h-11 gap-3 rounded-xl px-3 py-2 text-sm font-normal data-[highlighted]:bg-foreground/[0.06] dark:data-[highlighted]:bg-white/[0.08]">
|
2026-09-19 14:46:31 +08:00
|
|
|
<ProviderIcon provider={provider.name} showBrandLogos={showBrandLogos} compact />
|
|
|
|
|
<span className="min-w-0 flex-1 truncate">{provider.label}</span>
|
|
|
|
|
<ChevronRight className="h-3.5 w-3.5 shrink-0 text-muted-foreground/50" aria-hidden />
|
|
|
|
|
</ComboboxOption>)}
|
|
|
|
|
{filtered.length === 0 ? <p role="status" className="px-3 py-12 text-center text-sm text-muted-foreground">
|
|
|
|
|
{t("settings.providers.noMatches", { defaultValue: "No matching providers." })}
|
|
|
|
|
</p> : null}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="shrink-0 border-t border-border/50 p-3">
|
|
|
|
|
<button type="button" onClick={onCustom}
|
|
|
|
|
className="flex min-h-11 w-full items-center gap-3 rounded-xl px-3 text-left text-sm settings-hover focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">
|
|
|
|
|
<Plus className="h-6 w-6 shrink-0 p-0.5 text-muted-foreground" aria-hidden />
|
|
|
|
|
<span className="flex-1">{t("settings.providers.customProvider", { defaultValue: "Custom provider" })}</span>
|
|
|
|
|
<ChevronRight className="h-3.5 w-3.5 text-muted-foreground/50" aria-hidden />
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</>;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-02 04:19:12 +08:00
|
|
|
function orderUnconfiguredProviders(
|
2026-08-10 19:33:52 +08:00
|
|
|
providers: SettingsPayload["providers"],
|
|
|
|
|
): SettingsPayload["providers"] {
|
|
|
|
|
return providers
|
|
|
|
|
.map((provider, index) => ({ provider, index }))
|
|
|
|
|
.sort((left, right) => {
|
|
|
|
|
const rank = providerVisibilityRank(left.provider) - providerVisibilityRank(right.provider);
|
|
|
|
|
return rank || left.index - right.index;
|
|
|
|
|
})
|
|
|
|
|
.map(({ provider }) => provider);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function providerVisibilityRank(provider: SettingsPayload["providers"][number]): number {
|
2026-09-02 04:19:12 +08:00
|
|
|
const localRank = LOCAL_UNCONFIGURED_PROVIDER_ORDER.get(provider.name);
|
2026-08-10 19:33:52 +08:00
|
|
|
if (localRank !== undefined) return localRank;
|
|
|
|
|
if ((provider.api_key_required ?? true) === false) return 100;
|
|
|
|
|
return 200;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 11:10:51 +08:00
|
|
|
export function ProviderIcon({
|
2026-08-10 19:33:52 +08:00
|
|
|
provider,
|
|
|
|
|
showBrandLogos,
|
2026-09-19 14:46:31 +08:00
|
|
|
compact = false,
|
2026-08-10 19:33:52 +08:00
|
|
|
}: {
|
|
|
|
|
provider: string;
|
|
|
|
|
showBrandLogos: boolean;
|
2026-09-19 14:46:31 +08:00
|
|
|
compact?: boolean;
|
2026-08-10 19:33:52 +08:00
|
|
|
}) {
|
|
|
|
|
const brand = providerBrand(provider);
|
|
|
|
|
const Icon = PROVIDER_ICONS[provider] ?? Hexagon;
|
2026-09-19 11:53:31 +08:00
|
|
|
const { logoUrl, logoLoaded, onLogoError, onLogoLoad } = useLogoFallback(brand?.logoUrls);
|
|
|
|
|
const showRemoteLogo = showBrandLogos && Boolean(logoUrl);
|
|
|
|
|
const showLoadedLogo = showRemoteLogo && logoLoaded;
|
2026-09-19 13:57:53 +08:00
|
|
|
const isLogoTile = brand?.logoLayout === "tile" && logoUrl === brand.logoUrl;
|
2026-08-10 19:33:52 +08:00
|
|
|
|
2026-09-19 11:53:31 +08:00
|
|
|
return (
|
|
|
|
|
<span
|
|
|
|
|
data-testid={`provider-logo-${provider}`}
|
|
|
|
|
className={cn(
|
2026-09-19 14:46:31 +08:00
|
|
|
"relative grid shrink-0 place-items-center overflow-hidden font-semibold text-muted-foreground",
|
|
|
|
|
compact ? "h-6 w-6 rounded-[7px] text-[9px]" : "h-8 w-8 rounded-[9px] text-[11px]",
|
2026-09-19 14:11:27 +08:00
|
|
|
showLoadedLogo ? (isLogoTile ? "bg-transparent" : "bg-white") : "bg-muted",
|
2026-09-19 11:53:31 +08:00
|
|
|
)}
|
|
|
|
|
aria-hidden
|
|
|
|
|
>
|
2026-08-10 19:33:52 +08:00
|
|
|
<span
|
2026-09-19 11:53:31 +08:00
|
|
|
className={cn(
|
|
|
|
|
"transition-opacity duration-150 motion-reduce:transition-none",
|
|
|
|
|
showLoadedLogo ? "opacity-0" : "opacity-100",
|
|
|
|
|
)}
|
2026-08-10 19:33:52 +08:00
|
|
|
>
|
2026-09-19 11:53:31 +08:00
|
|
|
{showBrandLogos && brand
|
|
|
|
|
? brand.initials
|
|
|
|
|
: <Icon className="h-5 w-5" strokeWidth={2} />}
|
|
|
|
|
</span>
|
|
|
|
|
{showRemoteLogo ? (
|
2026-08-10 19:33:52 +08:00
|
|
|
<img
|
|
|
|
|
src={logoUrl}
|
|
|
|
|
alt=""
|
|
|
|
|
decoding="async"
|
|
|
|
|
loading="lazy"
|
2026-09-19 11:53:31 +08:00
|
|
|
referrerPolicy="no-referrer"
|
|
|
|
|
draggable={false}
|
|
|
|
|
className={cn(
|
2026-09-19 13:57:53 +08:00
|
|
|
"absolute object-contain transition-opacity duration-150 motion-reduce:transition-none",
|
2026-09-19 14:46:31 +08:00
|
|
|
isLogoTile ? (compact ? "h-6 w-6" : "h-8 w-8") : compact ? "h-[18px] w-[18px]" : "h-6 w-6",
|
2026-09-19 11:53:31 +08:00
|
|
|
logoLoaded ? "opacity-100" : "opacity-0",
|
|
|
|
|
)}
|
2026-08-10 19:33:52 +08:00
|
|
|
onLoad={onLogoLoad}
|
|
|
|
|
onError={onLogoError}
|
|
|
|
|
/>
|
2026-09-19 11:53:31 +08:00
|
|
|
) : null}
|
2026-08-10 19:33:52 +08:00
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
}
|