import { useEffect, useId, useMemo, useRef, useState, type ReactNode } from "react"; import { DisclosureContent } from "@/components/ui/disclosure"; import { ChevronDown, ChevronLeft, Clipboard, Eye, EyeOff, ExternalLink, Globe2, Loader2, Pencil, Plus, Zap, } from "lucide-react"; import { useTranslation } from "react-i18next"; import type { ProviderOperation } from "@/components/settings/models/useModelSettingsState"; import { ProviderIcon } from "@/components/settings/shared/ProviderIcon"; import { ProviderSearchList } from "@/components/settings/models/ProviderSearchList"; export { ProviderIcon } from "@/components/settings/shared/ProviderIcon"; import { CapabilityInstallNotice } from "@/components/settings/shared/SettingsControls"; import { ToggleButton } from "@/components/ui/toggle-button"; import { ModelAPIControl } from "@/components/settings/models/ModelAPIControl"; import { ProviderAPIControl } from "@/components/settings/models/ProviderAPIControl"; import { Button } from "@/components/ui/button"; import { SettingsHint } from "@/components/settings/shared/SettingsHint"; import { ProxySettingsControl } from "@/components/settings/shared/ProxySettingsControl"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Input } from "@/components/ui/input"; import { SheetContent } from "@/components/ui/sheet"; import { FloatingPortalContext } from "@/components/ui/floating-portal"; import { Textarea } from "@/components/ui/textarea"; import { ProviderParameterFields } from "@/components/settings/models/ProviderParameterFields"; import { CUSTOM_PROVIDER_CREATION_KEY, emptyCustomProviderDraft, parseProviderObject, providerFormFromRow, providerJsonValue, type CustomProviderDraft, type ProviderAdvancedField, type ProviderForm, } from "@/components/settings/models/providerForm"; export type { CustomProviderDraft, ProviderForm } from "@/components/settings/models/providerForm"; import { useMediaQuery } from "@/hooks/useMediaQuery"; import { cn } from "@/lib/utils"; import { copyTextToClipboard } from "@/lib/clipboard"; import type { NanobotFeaturesPayload, ModelAPIConfig, ProviderOAuthAuthorizationRequired, SettingsPayload, } from "@/lib/types"; const OAUTH_PROXY_PROVIDERS = new Set(["openai_codex", "xai_grok"]); const SHENGSUANYUN_TEMPLATE = { icon: "template:shengsuanyun", label: "ShengSuanYun Router", searchAliases: "胜算云 勝算雲", apiBase: "https://router.shengsuanyun.com/api/v1", api: { supported_apis: ["chat_completions", "responses"], preferred_api: "chat_completions", } satisfies ModelAPIConfig, }; 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> = { openai_codex: [{ kind: "priority", titleKey: "settings.providers.capabilityFastMode", title: "Fast mode", helpKey: "settings.providers.capabilityFastModeHelp", help: "Faster responses, increased usage.", }], 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, }], 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, }], }; const CUSTOM_PROVIDER_ADVANCED_FIELDS: ProviderAdvancedField[] = [ "extra_headers", "extra_body", "extra_query", "use_proxy", "thinking_style", ]; 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).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, ): 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 { const extraBody = { ...(parseProviderObject(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 ? { api: { supported_apis: ["responses"], preferred_api: "responses" } satisfies ModelAPIConfig } : {}), }; } 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"; const isDeviceCode = flow?.completion_input === "device_code"; const [copiedCode, setCopiedCode] = useState(null); const inputId = expectsCallbackUrl ? "provider-oauth-callback" : "provider-oauth-code"; const inputLabel = expectsCallbackUrl ? t("settings.oauth.callbackUrl") : t("settings.oauth.authorizationCode"); return ( { if (!open) onClose(); }} >
{ event.preventDefault(); onComplete(); }} > {providerLabel} {isDeviceCode ? t("settings.oauth.deviceCodeHelp") : expectsCallbackUrl ? remoteBrowserAccess ? t("settings.oauth.remoteCallbackHelp") : t("settings.oauth.localCallbackHelp") : remoteBrowserAccess ? t("settings.oauth.remoteCodeHelp") : t("settings.oauth.localCodeHelp")}
{expectsCallbackUrl && remoteBrowserAccess ? ( ) : ( )} {isDeviceCode ? t("settings.oauth.waitingForDeviceApproval") : expectsCallbackUrl && remoteBrowserAccess ? t("settings.oauth.pasteCallbackToContinue") : t("settings.oauth.waitingForCallback")}
{isDeviceCode ? (
event.target.select()} className="h-12 text-center font-mono text-xl tracking-widest" />
) :
{expectsCallbackUrl ? (