fix(model-fetch): support /models for Anthropic-compat subpath providers

Providers like DeepSeek, Kimi, Zhipu GLM and MiniMax expose the
Anthropic-compatible API on a subpath (e.g. /anthropic) while the
OpenAI-style /models endpoint lives at the API root. The previous
heuristic blindly appended /v1/models to the Base URL, so every such
provider returned 404 and the UI mislabeled it as "provider does not
support fetching models".

Backend now generates a candidate list and tries them in order:
preset override -> baseURL /v1/models -> stripped-subpath /v1/models ->
stripped-subpath /models. Non-404/405 responses (auth, network) stop
immediately so we never retry against hostile status codes. Known
compat suffixes are kept in a length-descending constant so the
longest match wins; response bodies are truncated to 512 chars to
avoid HTML 404 pages bloating the error string.

Preset type gains an optional modelsUrl (DeepSeek points at
https://api.deepseek.com/models). Frontend threads the override
through fetchModelsForConfig when the current Base URL still matches
the preset default. A new fetchModelsEndpointNotFound i18n key
replaces the misleading "not supported" toast for exhausted-candidate
and 404/405 cases (zh/en/ja).
This commit is contained in:
Jason
2026-04-24 11:49:49 +08:00
parent fcd83ee30d
commit 67dbfc0a8c
8 changed files with 342 additions and 69 deletions
+15 -4
View File
@@ -10,15 +10,21 @@ export interface FetchedModel {
/**
* 从供应商获取可用模型列表
*
* 使用 OpenAI 兼容的 GET /v1/models 端点。
* 主要面向第三方聚合站(硅基流动、OpenRouter 等)。
* 使用 OpenAI 兼容的 GET /v1/models 端点。优先用 `modelsUrl` 精确覆写;
* 否则后端会对 baseURL 生成候选列表并按序尝试(含"剥离 /anthropic 等兼容子路径"兜底)。
*/
export async function fetchModelsForConfig(
baseUrl: string,
apiKey: string,
isFullUrl?: boolean,
modelsUrl?: string,
): Promise<FetchedModel[]> {
return invoke("fetch_models_for_config", { baseUrl, apiKey, isFullUrl });
return invoke("fetch_models_for_config", {
baseUrl,
apiKey,
isFullUrl,
modelsUrl,
});
}
/**
@@ -50,8 +56,13 @@ export function showFetchModelsError(
toast.error(t("providerForm.fetchModelsAuthFailed"));
return;
}
// 所有候选端点均返回 404/405:供应商可能未开放 /models 接口,或 Base URL 有误
if (msg.includes("All candidates failed")) {
toast.error(t("providerForm.fetchModelsEndpointNotFound"));
return;
}
if (msg.includes("HTTP 404") || msg.includes("HTTP 405")) {
toast.error(t("providerForm.fetchModelsNotSupported"));
toast.error(t("providerForm.fetchModelsEndpointNotFound"));
return;
}
if (msg.includes("timeout") || msg.includes("timed out")) {