mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 22:19:42 +08:00
- Check value-constraint markers before parameter-unknown markers in classify_reasoning_param_error so mixed phrasings like 'Unsupported parameter: ... Supported values are ...' retry once without disabling reasoning params for the whole endpoint. - Align MiniMax-M3 Thinking toggle (none/high) across backend resolve_reasoning_control and frontend validation so turning Thinking on survives effectiveReasoningEffort/dropUnvalidatedReasoningEffort. - Filter non-chat model IDs (e.g. text-embedding-3-small) when /models probe entries omit services metadata. - Cancel and discard stale in-flight Quick Setup probe/apply responses when switching vendors/regions or closing the form so old responses cannot save the prior vendor or clear the newly entered API key. - Sync THINKING_OFF_EFFORTS symbol rename from upstream/dev.
364 lines
11 KiB
TypeScript
364 lines
11 KiB
TypeScript
import type { Discovery, ServiceName } from "@/lib/model-catalog-types";
|
|
|
|
export interface QuickProviderRegion {
|
|
id: string;
|
|
labelKey: string;
|
|
baseUrl: string;
|
|
keyUrl: string;
|
|
}
|
|
|
|
export interface QuickProviderPreset {
|
|
id: string;
|
|
nameKey: string;
|
|
taglineKey: string;
|
|
keyUrl?: string;
|
|
keyPlaceholderKey?: string;
|
|
recommendedModels: string[];
|
|
badgeKey?: string;
|
|
requiresBaseUrl?: boolean;
|
|
requiresManualModel?: boolean;
|
|
regions?: QuickProviderRegion[];
|
|
}
|
|
|
|
export const FEATURED_DOMESTIC_PROVIDERS: QuickProviderPreset[] = [
|
|
{
|
|
id: "deepseek",
|
|
nameKey: "quickSetup.vendor.deepseek.name",
|
|
taglineKey: "quickSetup.vendor.deepseek.tagline",
|
|
keyUrl: "https://platform.deepseek.com/api_keys",
|
|
recommendedModels: ["deepseek-chat", "deepseek-reasoner"],
|
|
badgeKey: "quickSetup.badge.recommended",
|
|
},
|
|
{
|
|
id: "dashscope",
|
|
nameKey: "quickSetup.vendor.dashscope.name",
|
|
taglineKey: "quickSetup.vendor.dashscope.tagline",
|
|
keyUrl: "https://bailian.console.aliyun.com/?apiKey=1",
|
|
recommendedModels: ["qwen-plus", "qwen-max", "qwen-turbo"],
|
|
badgeKey: "quickSetup.badge.allInOne",
|
|
},
|
|
{
|
|
id: "siliconflow",
|
|
nameKey: "quickSetup.vendor.siliconflow.name",
|
|
taglineKey: "quickSetup.vendor.siliconflow.tagline",
|
|
keyUrl: "https://cloud.siliconflow.cn/account/ak",
|
|
recommendedModels: [
|
|
"deepseek-ai/DeepSeek-V3",
|
|
"deepseek-ai/DeepSeek-R1",
|
|
"Qwen/Qwen2.5-72B-Instruct",
|
|
],
|
|
badgeKey: "quickSetup.badge.starterCredits",
|
|
},
|
|
{
|
|
id: "zhipu",
|
|
nameKey: "quickSetup.vendor.zhipu.name",
|
|
taglineKey: "quickSetup.vendor.zhipu.tagline",
|
|
keyUrl: "https://open.bigmodel.cn/usercenter/apikeys",
|
|
keyPlaceholderKey: "quickSetup.placeholder.apiKey",
|
|
recommendedModels: ["glm-4-plus", "glm-4-flash", "glm-4-air"],
|
|
},
|
|
{
|
|
id: "moonshot",
|
|
nameKey: "quickSetup.vendor.moonshot.name",
|
|
taglineKey: "quickSetup.vendor.moonshot.tagline",
|
|
keyUrl: "https://platform.moonshot.cn/console/api-keys",
|
|
recommendedModels: ["kimi-k2-0711-preview", "moonshot-v1-8k", "moonshot-v1-32k"],
|
|
},
|
|
{
|
|
id: "volcengine",
|
|
nameKey: "quickSetup.vendor.volcengine.name",
|
|
taglineKey: "quickSetup.vendor.volcengine.tagline",
|
|
keyUrl: "https://console.volcengine.com/ark/region:ark+cn-beijing/apiKey",
|
|
keyPlaceholderKey: "quickSetup.placeholder.apiKey",
|
|
recommendedModels: ["doubao-1-5-pro-32k-250115", "doubao-seed-1-6-250615"],
|
|
},
|
|
];
|
|
|
|
export const MORE_PROVIDERS: QuickProviderPreset[] = [
|
|
{
|
|
id: "minimax",
|
|
nameKey: "quickSetup.vendor.minimax.name",
|
|
taglineKey: "quickSetup.vendor.minimax.tagline",
|
|
keyUrl: "https://platform.minimax.io/user-center/basic-information/interface-key",
|
|
keyPlaceholderKey: "quickSetup.placeholder.apiKey",
|
|
recommendedModels: ["MiniMax-M3.1-Flash", "MiniMax-M3", "MiniMax-M2.7"],
|
|
regions: [
|
|
{
|
|
id: "cn",
|
|
labelKey: "quickSetup.region.minimax.cn",
|
|
baseUrl: "https://api.minimaxi.com/v1",
|
|
keyUrl: "https://platform.minimaxi.com/user-center/basic-information/interface-key",
|
|
},
|
|
{
|
|
id: "global",
|
|
labelKey: "quickSetup.region.minimax.global",
|
|
baseUrl: "https://api.minimax.io/v1",
|
|
keyUrl: "https://platform.minimax.io/user-center/basic-information/interface-key",
|
|
},
|
|
],
|
|
},
|
|
{
|
|
id: "qianfan",
|
|
nameKey: "quickSetup.vendor.qianfan.name",
|
|
taglineKey: "quickSetup.vendor.qianfan.tagline",
|
|
keyUrl: "https://console.bce.baidu.com/qianfan/ais/console/apiKey",
|
|
keyPlaceholderKey: "quickSetup.placeholder.apiKey",
|
|
recommendedModels: ["ernie-4.5-turbo-128k", "ernie-4.0-8k"],
|
|
},
|
|
{
|
|
id: "openai",
|
|
nameKey: "quickSetup.vendor.openai.name",
|
|
taglineKey: "quickSetup.vendor.openai.tagline",
|
|
keyUrl: "https://platform.openai.com/api-keys",
|
|
recommendedModels: ["gpt-4.1", "gpt-4o", "gpt-4o-mini"],
|
|
},
|
|
{
|
|
id: "anthropic",
|
|
nameKey: "quickSetup.vendor.anthropic.name",
|
|
taglineKey: "quickSetup.vendor.anthropic.tagline",
|
|
keyUrl: "https://console.anthropic.com/settings/keys",
|
|
recommendedModels: ["claude-sonnet-4-20250514", "claude-3-7-sonnet-20250219"],
|
|
},
|
|
{
|
|
id: "gemini",
|
|
nameKey: "quickSetup.vendor.gemini.name",
|
|
taglineKey: "quickSetup.vendor.gemini.tagline",
|
|
keyUrl: "https://aistudio.google.com/apikey",
|
|
recommendedModels: ["gemini-2.5-flash", "gemini-2.5-pro"],
|
|
},
|
|
{
|
|
id: "openrouter",
|
|
nameKey: "quickSetup.vendor.openrouter.name",
|
|
taglineKey: "quickSetup.vendor.openrouter.tagline",
|
|
keyUrl: "https://openrouter.ai/keys",
|
|
recommendedModels: ["openai/gpt-4o-mini", "deepseek/deepseek-chat-v3-0324"],
|
|
},
|
|
{
|
|
id: "custom",
|
|
nameKey: "quickSetup.vendor.custom.name",
|
|
taglineKey: "quickSetup.vendor.custom.tagline",
|
|
recommendedModels: [],
|
|
requiresBaseUrl: true,
|
|
requiresManualModel: true,
|
|
},
|
|
];
|
|
|
|
export const ALL_QUICK_PROVIDERS: QuickProviderPreset[] = [
|
|
...FEATURED_DOMESTIC_PROVIDERS,
|
|
...MORE_PROVIDERS,
|
|
];
|
|
|
|
const GLOBAL_FEATURED_IDS = [
|
|
"openai",
|
|
"anthropic",
|
|
"gemini",
|
|
"openrouter",
|
|
"deepseek",
|
|
"custom",
|
|
];
|
|
|
|
/**
|
|
* Return featured + overflow provider lists tailored to the UI locale:
|
|
* Chinese locales prioritize domestic providers; non-Chinese locales
|
|
* prioritize international providers.
|
|
*/
|
|
export function getQuickProviderGroups(language: string): {
|
|
featured: QuickProviderPreset[];
|
|
more: QuickProviderPreset[];
|
|
} {
|
|
const isZh = language.toLowerCase().startsWith("zh");
|
|
if (isZh) {
|
|
return {
|
|
featured: FEATURED_DOMESTIC_PROVIDERS,
|
|
more: MORE_PROVIDERS,
|
|
};
|
|
}
|
|
const byId = new Map(ALL_QUICK_PROVIDERS.map((item) => [item.id, item]));
|
|
const featured = GLOBAL_FEATURED_IDS.map((id) => byId.get(id)).filter(
|
|
(item): item is QuickProviderPreset => Boolean(item),
|
|
);
|
|
const featuredSet = new Set(featured.map((item) => item.id));
|
|
const more = ALL_QUICK_PROVIDERS.filter((item) => !featuredSet.has(item.id));
|
|
return { featured, more };
|
|
}
|
|
|
|
export function findQuickPreset(providerId: string): QuickProviderPreset | undefined {
|
|
return ALL_QUICK_PROVIDERS.find((item) => item.id === providerId);
|
|
}
|
|
|
|
/**
|
|
* Resolve the active region for a multi-region provider (e.g. MiniMax CN vs Global).
|
|
* Defaults to China when the UI language is Chinese and International otherwise,
|
|
* ensuring the API key console URL and API endpoint always belong to the same region.
|
|
*/
|
|
export function resolveQuickPresetRegion(
|
|
preset: QuickProviderPreset,
|
|
selectedRegionId: string | null | undefined,
|
|
language: string,
|
|
): QuickProviderRegion | null {
|
|
const regions = preset.regions;
|
|
if (!regions || regions.length === 0) return null;
|
|
if (selectedRegionId) {
|
|
const matched = regions.find((region) => region.id === selectedRegionId);
|
|
if (matched) return matched;
|
|
}
|
|
const preferredId = language.toLowerCase().startsWith("zh") ? "cn" : "global";
|
|
return regions.find((region) => region.id === preferredId) ?? regions[0];
|
|
}
|
|
|
|
/**
|
|
* Model ids that are clearly not chat/completion models (embeddings, rerankers,
|
|
* speech, image/video generation, moderation). Used only when the provider's
|
|
* /models listing carries no `services` metadata — e.g. OpenAI returns
|
|
* `text-embedding-3-small` alongside chat models with nothing to tell them
|
|
* apart, and it must never become the default chat model.
|
|
*/
|
|
const NON_CHAT_MODEL_ID_RE =
|
|
/(?:^|[-_/.:])(?:text-)?embed(?:ding)?s?(?:$|[-_/.:\d])|embedding|rerank|(?:^|[-_/])bge-|(?:^|[-_/])m3e|(?:^|[-_/])tts(?:$|[-_/.\d])|(?:^|[-_/])speech|whisper|transcri|(?:^|[-_/])asr(?:$|[-_/.\d])|dall-e|imagen|(?:^|[-_/])image-|-image(?:$|[-_/.:])|stable-diffusion|sdxl|(?:^|[-_/])flux|cogview|cogvideo|wanx|(?:^|[-_/])veo|-video(?:$|[-_/.:])|(?:^|[-_/])video-|moderation/;
|
|
|
|
export function isLikelyChatModelId(id: string): boolean {
|
|
return !NON_CHAT_MODEL_ID_RE.test(id.trim().toLowerCase());
|
|
}
|
|
|
|
/**
|
|
* Chat-capable model ids from a probe listing. Explicit `services` metadata
|
|
* always wins; entries without it fall back to the id heuristic above.
|
|
*/
|
|
export function selectChatModelIds(
|
|
models: ReadonlyArray<{ id: string; services?: string[] | null }>,
|
|
): string[] {
|
|
return models
|
|
.filter((m) =>
|
|
m.services?.length ? m.services.includes("llm") : isLikelyChatModelId(m.id),
|
|
)
|
|
.map((m) => m.id.trim())
|
|
.filter(Boolean);
|
|
}
|
|
|
|
/** Pick the best default model given discovered models and vendor recommendations. */
|
|
export function pickRecommendedModel(
|
|
preset: QuickProviderPreset,
|
|
discovered: string[],
|
|
): string {
|
|
// Defence in depth: callers should already pass chat-only ids, but a
|
|
// non-chat id must never be returned as the default chat model.
|
|
discovered = discovered.filter(isLikelyChatModelId);
|
|
if (discovered.length > 0) {
|
|
const lowerMap = new Map(discovered.map((id) => [id.toLowerCase(), id]));
|
|
for (const candidate of preset.recommendedModels) {
|
|
const exact = lowerMap.get(candidate.toLowerCase());
|
|
if (exact) return exact;
|
|
}
|
|
for (const candidate of preset.recommendedModels) {
|
|
const prefix = discovered.find((id) =>
|
|
id.toLowerCase().includes(candidate.toLowerCase()),
|
|
);
|
|
if (prefix) return prefix;
|
|
}
|
|
return discovered[0];
|
|
}
|
|
return preset.recommendedModels[0] ?? "";
|
|
}
|
|
|
|
export interface FriendlyErrorResult {
|
|
titleKey: string;
|
|
detailKey: string;
|
|
raw?: string;
|
|
}
|
|
|
|
/** Translate technical HTTP/provider errors into i18n keys for plain, actionable guidance. */
|
|
export function formatFriendlySetupError(
|
|
discovery?: Discovery | null,
|
|
rawError?: string,
|
|
): FriendlyErrorResult {
|
|
const status = discovery?.status ?? "";
|
|
const raw =
|
|
rawError ||
|
|
discovery?.warning ||
|
|
(discovery?.http_status ? `HTTP ${discovery.http_status}` : "") ||
|
|
status ||
|
|
"";
|
|
const lower = raw.toLowerCase();
|
|
|
|
if (
|
|
status === "auth_error" ||
|
|
lower.includes("401") ||
|
|
lower.includes("invalid_api_key") ||
|
|
lower.includes("incorrect api key") ||
|
|
lower.includes("unauthorized") ||
|
|
lower.includes("authentication")
|
|
) {
|
|
return {
|
|
titleKey: "quickSetup.error.authTitle",
|
|
detailKey: "quickSetup.error.authDetail",
|
|
raw,
|
|
};
|
|
}
|
|
|
|
if (
|
|
lower.includes("402") ||
|
|
lower.includes("insufficient") ||
|
|
lower.includes("balance") ||
|
|
lower.includes("quota") ||
|
|
lower.includes("billing") ||
|
|
lower.includes("credit")
|
|
) {
|
|
return {
|
|
titleKey: "quickSetup.error.quotaTitle",
|
|
detailKey: "quickSetup.error.quotaDetail",
|
|
raw,
|
|
};
|
|
}
|
|
|
|
if (
|
|
status === "timeout" ||
|
|
status === "unreachable" ||
|
|
lower.includes("timeout") ||
|
|
lower.includes("connect") ||
|
|
lower.includes("network") ||
|
|
lower.includes("dns")
|
|
) {
|
|
return {
|
|
titleKey: "quickSetup.error.networkTitle",
|
|
detailKey: "quickSetup.error.networkDetail",
|
|
raw,
|
|
};
|
|
}
|
|
|
|
if (lower.includes("403") || lower.includes("404") || lower.includes("permission")) {
|
|
return {
|
|
titleKey: "quickSetup.error.accessTitle",
|
|
detailKey: "quickSetup.error.accessDetail",
|
|
raw,
|
|
};
|
|
}
|
|
|
|
return {
|
|
titleKey: "quickSetup.error.genericTitle",
|
|
detailKey: "quickSetup.error.genericDetail",
|
|
raw,
|
|
};
|
|
}
|
|
|
|
export const SYNCABLE_EXTRA_SERVICES: {
|
|
service: ServiceName;
|
|
titleKey: string;
|
|
descKey: string;
|
|
}[] = [
|
|
{
|
|
service: "embedding",
|
|
titleKey: "quickSetup.extra.embedding.title",
|
|
descKey: "quickSetup.extra.embedding.desc",
|
|
},
|
|
{
|
|
service: "tts",
|
|
titleKey: "quickSetup.extra.tts.title",
|
|
descKey: "quickSetup.extra.tts.desc",
|
|
},
|
|
{
|
|
service: "stt",
|
|
titleKey: "quickSetup.extra.stt.title",
|
|
descKey: "quickSetup.extra.stt.desc",
|
|
},
|
|
];
|