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