fix(models): 获取模型列表 404 改为友好提示 + 助手侧走后端绕 CORS

场景:部分服务商(如某些厂商的 Anthropic 兼容接口)不提供 /models 列表接口,
之前会直接显示 HTTP 404 Not Found / Failed to fetch 等技术错误,用户体验差。

- Rust list_remote_models:识别 404/405/501 作为"不支持自动获取"场景,
  返回带 [NOT_SUPPORTED] 前缀的友好错误,而非裸 HTTP 状态码
- 模型配置页「获取列表」:识别 [NOT_SUPPORTED] 后弹出引导对话框,
  点击「模型」按钮直接进入手动添加流程
- 助手设置页「获取列表」:改为走 Rust 后端 api.listRemoteModels,
  原先直接用前端 fetch 会被 WebView CORS 拦截(provider 不返回 CORS 头),
  改走后端既绕开 CORS 又能获得一致的友好提示
- Web 模式 dev-api.js 同步 404 识别逻辑,保证桌面 / Web 行为一致
- 补齐 models.fetchNotSupported / assistant.fetchNotSupported 多语言文案
This commit is contained in:
晴天
2026-04-20 16:01:24 +08:00
parent 12cdc72d2b
commit 1ef9ca8ede
6 changed files with 38 additions and 45 deletions

View File

@@ -1378,7 +1378,20 @@ async function fetchRemoteModels(btn, page, state, providerKey) {
} catch (e) {
btn.disabled = false
btn.textContent = t('models.fetchList')
toast(t('models.fetchFailed', { error: e }), 'error')
const errStr = String(e?.message || e)
// 服务商不支持 /models 接口 → 友好弹窗引导手动添加
if (errStr.includes('[NOT_SUPPORTED]') || errStr.includes('不支持自动获取')) {
const msg = errStr.replace('[NOT_SUPPORTED] ', '').replace('获取模型列表失败: ', '')
showConfirm(t('models.fetchNotSupported', { error: msg }), {
title: t('models.fetchNotSupportedTitle'),
confirmText: t('models.addModel').replace('+ ', ''),
cancelText: t('common.close'),
}).then(yes => {
if (yes) addModel(btn.closest('.page') || document.querySelector('.page'), { config: state.config, save: state.save }, providerKey)
})
} else {
toast(t('models.fetchFailed', { error: errStr }), 'error')
}
}
}