refactor(webui): keep provider search flow for ShengSuanYun setup
This commit is contained in:
1 parent
221e121387
commit
2b889b8408
4 files changed
+61
-70
No files matched your search
@@ -34,9 +34,12 @@ const PROVIDER_SEARCH_ALIASES: Record<string, string> = {
|
||||
ant_ling: "蚂蚁 螞蟻 百灵 百靈",
|
||||
};
|
||||
|
||||
export function ProviderSearchList({ providers, setupOption, showBrandLogos, query, onQueryChange, onSelect, onCustom, onClose }: {
|
||||
providers: SettingsPayload["providers"];
|
||||
setupOption?: { id: string; label: string; searchAliases: string; onSelect: () => void };
|
||||
type ProviderSearchOption = Pick<
|
||||
SettingsPayload["providers"][number], "name" | "label" | "auth_type" | "model_catalog"
|
||||
> & { searchAliases?: string; icon?: string };
|
||||
|
||||
export function ProviderSearchList({ providers, showBrandLogos, query, onQueryChange, onSelect, onCustom, onClose }: {
|
||||
providers: ProviderSearchOption[];
|
||||
showBrandLogos: boolean;
|
||||
query: string;
|
||||
onQueryChange: (query: string) => void;
|
||||
@@ -51,34 +54,23 @@ export function ProviderSearchList({ providers, setupOption, showBrandLogos, que
|
||||
useEffect(() => {
|
||||
if (!mobile) inputRef.current?.focus({ preventScroll: true });
|
||||
}, [mobile]);
|
||||
const groups = useMemo(() => {
|
||||
const filtered = useMemo(() => {
|
||||
const normalize = (value: string) => value.toLocaleLowerCase().replace(/[\s_.-]+/g, "");
|
||||
const normalizedQuery = normalize(query);
|
||||
const options = providers.map((provider) => ({
|
||||
id: `provider:${provider.name}`,
|
||||
icon: provider.name,
|
||||
label: provider.label,
|
||||
searchText: `${provider.name} ${provider.label} ${PROVIDER_SEARCH_ALIASES[provider.name] ?? ""}`,
|
||||
group: providerGroup(provider),
|
||||
select: () => onSelect(provider.name),
|
||||
}));
|
||||
if (setupOption) options.push({
|
||||
id: setupOption.id, icon: setupOption.id, label: setupOption.label, group: "api_key",
|
||||
searchText: `${setupOption.label} ${setupOption.searchAliases}`,
|
||||
select: setupOption.onSelect,
|
||||
});
|
||||
const filtered = options.filter(({ searchText }) =>
|
||||
normalize(searchText).includes(normalizedQuery),
|
||||
);
|
||||
return PROVIDER_GROUPS.map((group) => ({
|
||||
...group, options: filtered.filter((option) => option.group === group.id),
|
||||
})).filter((group) => group.options.length > 0);
|
||||
}, [providers, query, setupOption, onSelect]);
|
||||
const options = groups.flatMap((group) => group.options);
|
||||
const values = options.map((option) => option.id);
|
||||
return providers.filter((provider) => normalize(
|
||||
`${provider.name} ${provider.label} ${provider.searchAliases ?? PROVIDER_SEARCH_ALIASES[provider.name] ?? ""}`,
|
||||
).includes(normalizedQuery));
|
||||
}, [providers, query]);
|
||||
const groups = useMemo(() => PROVIDER_GROUPS.map((group) => ({
|
||||
...group,
|
||||
providers: filtered.filter((provider) => providerGroup(provider) === group.id),
|
||||
})).filter((group) => group.providers.length > 0), [filtered]);
|
||||
const values = useMemo(
|
||||
() => groups.flatMap((group) => group.providers.map((provider) => provider.name)),
|
||||
[groups],
|
||||
);
|
||||
const { inputProps, listProps, getOptionProps } = useComboboxNavigation({
|
||||
open: true, values, onClose,
|
||||
onSelect: (id) => options.find((option) => option.id === id)?.select(),
|
||||
open: true, values, onSelect, onClose,
|
||||
});
|
||||
const searchLabel = t("settings.providers.searchPlaceholder", { defaultValue: "Search providers" });
|
||||
return <>
|
||||
@@ -99,17 +91,17 @@ export function ProviderSearchList({ providers, setupOption, showBrandLogos, que
|
||||
{t(group.labelKey, { defaultValue: group.label })}
|
||||
</div>
|
||||
<div className="grid grid-cols-[repeat(auto-fit,minmax(min(100%,9rem),1fr))] gap-x-2 gap-y-0.5">
|
||||
{group.options.map((option) => <ComboboxOption key={option.id} {...getOptionProps(option.id)}
|
||||
aria-label={option.label}
|
||||
{group.providers.map((provider) => <ComboboxOption key={provider.name} {...getOptionProps(provider.name)}
|
||||
aria-label={provider.label}
|
||||
className="min-h-11 min-w-0 gap-2 rounded-xl px-2 py-2 text-[13px] font-normal sm:gap-3 sm:px-3 sm:text-sm">
|
||||
<ProviderIcon provider={option.icon} showBrandLogos={showBrandLogos} compact />
|
||||
<TruncatedTextTooltip text={option.label} className="flex-1" />
|
||||
<ProviderIcon provider={provider.icon ?? provider.name} showBrandLogos={showBrandLogos} compact />
|
||||
<TruncatedTextTooltip text={provider.label} className="flex-1" />
|
||||
</ComboboxOption>)}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
{groups.length === 0 ? <p role="status" className="px-3 py-12 text-center text-sm text-muted-foreground">
|
||||
{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>
|
||||
@@ -123,7 +115,7 @@ export function ProviderSearchList({ providers, setupOption, showBrandLogos, que
|
||||
</>;
|
||||
}
|
||||
|
||||
function providerGroup(provider: SettingsPayload["providers"][number]): typeof PROVIDER_GROUPS[number]["id"] {
|
||||
function providerGroup(provider: ProviderSearchOption): typeof PROVIDER_GROUPS[number]["id"] {
|
||||
if (provider.auth_type === "oauth") return "oauth";
|
||||
return provider.model_catalog === "local" ? "local" : "api_key";
|
||||
}
|
||||
|
||||
@@ -47,7 +47,6 @@ import { Textarea } from "@/components/ui/textarea";
|
||||
import { ProviderParameterFields } from "@/components/settings/models/ProviderParameterFields";
|
||||
import {
|
||||
CUSTOM_PROVIDER_CREATION_KEY,
|
||||
SHENGSUANYUN_TEMPLATE,
|
||||
emptyCustomProviderDraft,
|
||||
parseProviderObject,
|
||||
providerFormFromRow,
|
||||
@@ -68,6 +67,17 @@ import type {
|
||||
} 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;
|
||||
@@ -593,13 +603,14 @@ export function ProvidersSettings({
|
||||
emptyCustomProviderDraft,
|
||||
);
|
||||
const configuredProviders = settings.providers.filter((provider) => provider.configured);
|
||||
const unconfiguredProviders = useMemo(
|
||||
() =>
|
||||
settings.providers.filter(
|
||||
(provider) => !provider.configured && provider.name !== "custom",
|
||||
),
|
||||
[settings.providers],
|
||||
);
|
||||
const { providerOptions, templateId } = useMemo(() => {
|
||||
const providerOptions = settings.providers.filter((provider) => !provider.configured && provider.name !== "custom");
|
||||
// This ID is only used by the picker, and must not shadow a saved provider.
|
||||
let templateId = SHENGSUANYUN_TEMPLATE.icon;
|
||||
while (providerOptions.some((provider) => provider.name === templateId)) templateId += ":";
|
||||
const { label, icon, searchAliases } = SHENGSUANYUN_TEMPLATE;
|
||||
return { templateId, providerOptions: [...providerOptions, { name: templateId, label, icon, searchAliases }] };
|
||||
}, [settings.providers]);
|
||||
const customProviderSaving = providerSaving === CUSTOM_PROVIDER_CREATION_KEY;
|
||||
const selectedProvider = view.kind === "provider"
|
||||
? settings.providers.find((provider) => provider.name === view.name)
|
||||
@@ -641,20 +652,21 @@ export function ProvidersSettings({
|
||||
returnToOrigin();
|
||||
}
|
||||
};
|
||||
const beginCustomProviderCreation = (template?: typeof SHENGSUANYUN_TEMPLATE) => {
|
||||
const beginCustomProviderCreation = (fromTemplate = false) => {
|
||||
const draft = emptyCustomProviderDraft();
|
||||
if (template) {
|
||||
draft.name = template.name;
|
||||
if (fromTemplate) {
|
||||
const { label, apiBase, api } = SHENGSUANYUN_TEMPLATE;
|
||||
draft.name = label;
|
||||
const names = new Set(settings.providers.map((provider) => provider.label.trim().toLowerCase()));
|
||||
for (let suffix = 2; names.has(draft.name.toLowerCase()); suffix += 1) {
|
||||
draft.name = `${template.name} ${suffix}`;
|
||||
draft.name = `${label} ${suffix}`;
|
||||
}
|
||||
draft.apiBase = template.apiBase;
|
||||
draft.api = template.api;
|
||||
draft.apiBase = apiBase;
|
||||
draft.api = api;
|
||||
}
|
||||
setCustomProviderDraft(draft);
|
||||
setCustomProviderKeyVisible(false);
|
||||
setView({ kind: "custom", requiresApiKey: Boolean(template) });
|
||||
setView({ kind: "custom", requiresApiKey: fromTemplate });
|
||||
};
|
||||
const saveCustomProvider = async () => {
|
||||
if (customProviderSaving) return;
|
||||
@@ -1141,16 +1153,10 @@ export function ProvidersSettings({
|
||||
</div>
|
||||
</div>
|
||||
) : view.kind === "picker" ? (
|
||||
<ProviderSearchList providers={unconfiguredProviders} showBrandLogos={showBrandLogos}
|
||||
<ProviderSearchList providers={providerOptions} showBrandLogos={showBrandLogos}
|
||||
query={providerSearch} onQueryChange={setProviderSearch}
|
||||
onSelect={(name) => editProvider(name, true)}
|
||||
onSelect={(name) => name === templateId ? beginCustomProviderCreation(true) : editProvider(name, true)}
|
||||
onCustom={() => beginCustomProviderCreation()}
|
||||
setupOption={{
|
||||
id: SHENGSUANYUN_TEMPLATE.id,
|
||||
label: SHENGSUANYUN_TEMPLATE.name,
|
||||
searchAliases: SHENGSUANYUN_TEMPLATE.searchAliases,
|
||||
onSelect: () => beginCustomProviderCreation(SHENGSUANYUN_TEMPLATE),
|
||||
}}
|
||||
onClose={entry.kind === "manage" ? goBack : closePanel} />
|
||||
) : (
|
||||
<div className="min-h-0 space-y-4 overflow-y-auto overscroll-contain px-5 pb-5">
|
||||
|
||||
@@ -19,17 +19,6 @@ export type ProviderForm = {
|
||||
export type CustomProviderDraft = ProviderForm & { name: string };
|
||||
export const CUSTOM_PROVIDER_CREATION_KEY = "__custom_provider__";
|
||||
|
||||
export const SHENGSUANYUN_TEMPLATE = {
|
||||
id: "template:shengsuanyun",
|
||||
name: "ShengSuanYun Router",
|
||||
searchAliases: "胜算云 勝算雲",
|
||||
apiBase: "https://router.shengsuanyun.com/api/v1",
|
||||
api: {
|
||||
supported_apis: ["chat_completions", "responses"],
|
||||
preferred_api: "chat_completions",
|
||||
} satisfies ModelAPIConfig,
|
||||
};
|
||||
|
||||
export function providerJsonValue(value: Record<string, unknown> | null | undefined): string {
|
||||
return value && Object.keys(value).length > 0 ? JSON.stringify(value, null, 2) : "";
|
||||
}
|
||||
|
||||
@@ -25,15 +25,19 @@ describe("Settings providers", () => {
|
||||
const user = userEvent.setup();
|
||||
const payload = settingsPayload();
|
||||
const existing = { name: "shengsuanyun_router", label: "ShengSuanYun Router", configured: true };
|
||||
payload.providers = [existing];
|
||||
const unconfigured = { name: "template:shengsuanyun", label: "Existing custom connection", configured: false };
|
||||
payload.providers = [existing, unconfigured];
|
||||
payload.provider_api_configuration_supported = true;
|
||||
requestMutationMock.mockResolvedValueOnce({
|
||||
...payload,
|
||||
created_provider: "custom-shengsuanyun-router-2",
|
||||
providers: [existing, { name: "custom-shengsuanyun-router-2", label: "ShengSuanYun Router 2", configured: true, is_custom: true }],
|
||||
providers: [existing, unconfigured, { name: "custom-shengsuanyun-router-2", label: "ShengSuanYun Router 2", configured: true, is_custom: true }],
|
||||
});
|
||||
renderProviderManager({ initialSection: "models", initialSettings: payload });
|
||||
await user.click(screen.getByRole("button", { name: "Add provider" }));
|
||||
await user.click(screen.getByRole("option", { name: unconfigured.label }));
|
||||
const existingForm = screen.getByRole("dialog", { name: unconfigured.label });
|
||||
await user.click(within(existingForm).getByRole("button", { name: "Back to providers" }));
|
||||
await user.type(screen.getByRole("combobox", { name: "Search providers" }), "胜算云");
|
||||
await user.keyboard("{ArrowDown}{Enter}");
|
||||
const form = screen.getByRole("dialog", { name: "Custom" });
|
||||
|
||||
Reference in new issue
Block a user