mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-08-04 11:43:57 +08:00
feat(stream-check): refresh default models and detect model-not-found errors (#2099)
* chore(stream-check): update default health check models to latest Replaces deprecated gpt-5.1-codex@low with gpt-5.4@low and switches the Gemini default from gemini-3-pro-preview to gemini-3-flash-preview to pick the lightest variant of the latest series for fast, low-cost health checks. https://claude.ai/code/session_01NGWLchcTP76rJHjiP5Ehte * feat(stream-check): detect model-not-found errors with dedicated toast Health check previously classified failures purely by HTTP status code, which meant deprecated/invalid models showed up as a generic "Not found (404)" error pointing users to check the Base URL — misleading when the URL is fine and only the test model is wrong (e.g. gpt-5.1-codex after it was retired). Backend: add detect_error_category() that inspects 4xx response bodies for model-not-found indicators (model_not_found, does not exist, invalid model, not_found_error, etc.) and returns a "modelNotFound" category. Thread the resolved test model through build_stream_check_result so the failed result carries it in model_used. Add StreamCheckResult .error_category field (serde-skipped when None). Frontend: useStreamCheck branches on errorCategory === "modelNotFound" before the HTTP-status fallback and renders a toast.error with the model name and a description pointing to Model Test Config. Add i18n keys (modelNotFound / modelNotFoundHint) for zh/en/ja. Tests: unit-test detect_error_category against real OpenAI/Anthropic error shapes, 5xx false-positive avoidance, and plain 401 auth errors. https://claude.ai/code/session_01NGWLchcTP76rJHjiP5Ehte * fix(stream-check): add missing error_category field in fallback The error_category field was added to StreamCheckResult in this branch but the fallback constructor in stream_check_all_providers was not updated, which broke cargo build. --------- Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -133,6 +133,7 @@ pub async fn stream_check_all_providers(
|
|||||||
model_used: String::new(),
|
model_used: String::new(),
|
||||||
tested_at: chrono::Utc::now().timestamp(),
|
tested_at: chrono::Utc::now().timestamp(),
|
||||||
retry_count: 0,
|
retry_count: 0,
|
||||||
|
error_category: None,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -55,8 +55,8 @@ impl Default for StreamCheckConfig {
|
|||||||
max_retries: 2,
|
max_retries: 2,
|
||||||
degraded_threshold_ms: 6000,
|
degraded_threshold_ms: 6000,
|
||||||
claude_model: "claude-haiku-4-5-20251001".to_string(),
|
claude_model: "claude-haiku-4-5-20251001".to_string(),
|
||||||
codex_model: "gpt-5.1-codex@low".to_string(),
|
codex_model: "gpt-5.4@low".to_string(),
|
||||||
gemini_model: "gemini-3-pro-preview".to_string(),
|
gemini_model: "gemini-3-flash-preview".to_string(),
|
||||||
test_prompt: default_test_prompt(),
|
test_prompt: default_test_prompt(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -74,6 +74,9 @@ pub struct StreamCheckResult {
|
|||||||
pub model_used: String,
|
pub model_used: String,
|
||||||
pub tested_at: i64,
|
pub tested_at: i64,
|
||||||
pub retry_count: u32,
|
pub retry_count: u32,
|
||||||
|
/// 细粒度错误分类(如 "modelNotFound"),前端据此渲染专门的文案
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub error_category: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 流式健康检查服务
|
/// 流式健康检查服务
|
||||||
@@ -143,6 +146,7 @@ impl StreamCheckService {
|
|||||||
model_used: String::new(),
|
model_used: String::new(),
|
||||||
tested_at: chrono::Utc::now().timestamp(),
|
tested_at: chrono::Utc::now().timestamp(),
|
||||||
retry_count: effective_config.max_retries,
|
retry_count: effective_config.max_retries,
|
||||||
|
error_category: None,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -275,6 +279,7 @@ impl StreamCheckService {
|
|||||||
result,
|
result,
|
||||||
response_time,
|
response_time,
|
||||||
config.degraded_threshold_ms,
|
config.degraded_threshold_ms,
|
||||||
|
&model_to_test,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -670,16 +675,21 @@ impl StreamCheckService {
|
|||||||
result,
|
result,
|
||||||
response_time,
|
response_time,
|
||||||
config.degraded_threshold_ms,
|
config.degraded_threshold_ms,
|
||||||
|
&model_to_test,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 将 check_*_stream 的原始结果包装成 StreamCheckResult
|
/// 将 check_*_stream 的原始结果包装成 StreamCheckResult
|
||||||
///
|
///
|
||||||
/// 抽取自 check_once 的末尾逻辑,以便 OpenCode/OpenClaw 的独立分支复用。
|
/// 抽取自 check_once 的末尾逻辑,以便 OpenCode/OpenClaw 的独立分支复用。
|
||||||
|
///
|
||||||
|
/// `model_tested` 是本次探测使用的模型名,用于在失败场景下仍能把模型信息透传给前端,
|
||||||
|
/// 方便针对"模型不存在 / 已下架"这类错误渲染专门的提示。
|
||||||
fn build_stream_check_result(
|
fn build_stream_check_result(
|
||||||
result: Result<(u16, String), AppError>,
|
result: Result<(u16, String), AppError>,
|
||||||
response_time: u64,
|
response_time: u64,
|
||||||
degraded_threshold_ms: u64,
|
degraded_threshold_ms: u64,
|
||||||
|
model_tested: &str,
|
||||||
) -> StreamCheckResult {
|
) -> StreamCheckResult {
|
||||||
let tested_at = chrono::Utc::now().timestamp();
|
let tested_at = chrono::Utc::now().timestamp();
|
||||||
match result {
|
match result {
|
||||||
@@ -692,14 +702,19 @@ impl StreamCheckService {
|
|||||||
model_used: model,
|
model_used: model,
|
||||||
tested_at,
|
tested_at,
|
||||||
retry_count: 0,
|
retry_count: 0,
|
||||||
|
error_category: None,
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let (http_status, message) = match &e {
|
let (http_status, message, error_category) = match &e {
|
||||||
AppError::HttpStatus { status, .. } => (
|
AppError::HttpStatus { status, body } => {
|
||||||
Some(*status),
|
let category = Self::detect_error_category(*status, body);
|
||||||
Self::classify_http_status(*status).to_string(),
|
(
|
||||||
),
|
Some(*status),
|
||||||
_ => (None, e.to_string()),
|
Self::classify_http_status(*status).to_string(),
|
||||||
|
category.map(|s| s.to_string()),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
_ => (None, e.to_string(), None),
|
||||||
};
|
};
|
||||||
StreamCheckResult {
|
StreamCheckResult {
|
||||||
status: HealthStatus::Failed,
|
status: HealthStatus::Failed,
|
||||||
@@ -707,14 +722,47 @@ impl StreamCheckService {
|
|||||||
message,
|
message,
|
||||||
response_time_ms: Some(response_time),
|
response_time_ms: Some(response_time),
|
||||||
http_status,
|
http_status,
|
||||||
model_used: String::new(),
|
model_used: model_tested.to_string(),
|
||||||
tested_at,
|
tested_at,
|
||||||
retry_count: 0,
|
retry_count: 0,
|
||||||
|
error_category,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 基于 HTTP 状态码和响应体识别细粒度错误分类。
|
||||||
|
///
|
||||||
|
/// 目前仅识别"模型不存在 / 已下架":各厂商该类错误通常返回 4xx,body 中会包含
|
||||||
|
/// 如 `model_not_found`(OpenAI)、`does not exist`、`invalid model`、`not_found_error`
|
||||||
|
/// + `model` 字样(Anthropic)等标记。
|
||||||
|
pub(crate) fn detect_error_category(status: u16, body: &str) -> Option<&'static str> {
|
||||||
|
// 只检查 4xx;5xx 的错误信息里可能巧合出现"model"之类的词,容易误判
|
||||||
|
if !(400..500).contains(&status) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let lower = body.to_lowercase();
|
||||||
|
// 必须提到 "model",避免通用 404 / 400 被误判
|
||||||
|
if !lower.contains("model") {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let indicators = [
|
||||||
|
"model_not_found",
|
||||||
|
"model not found",
|
||||||
|
"does not exist",
|
||||||
|
"invalid_model",
|
||||||
|
"invalid model",
|
||||||
|
"unknown_model",
|
||||||
|
"unknown model",
|
||||||
|
"is not a valid model",
|
||||||
|
"not_found_error", // Anthropic 的 type 字段
|
||||||
|
];
|
||||||
|
if indicators.iter().any(|s| lower.contains(s)) {
|
||||||
|
return Some("modelNotFound");
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
/// OpenClaw 流式检查分发器
|
/// OpenClaw 流式检查分发器
|
||||||
///
|
///
|
||||||
/// 根据 `settings_config.api` 字段分发到对应协议的检查器。
|
/// 根据 `settings_config.api` 字段分发到对应协议的检查器。
|
||||||
@@ -1474,6 +1522,51 @@ mod tests {
|
|||||||
assert_eq!(effort, None);
|
assert_eq!(effort, None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_detect_model_not_found() {
|
||||||
|
// OpenAI 典型响应:404 + model_not_found 错误码
|
||||||
|
let openai_404 = r#"{"error":{"message":"The model `gpt-5.1-codex` does not exist or you do not have access to it","type":"invalid_request_error","param":null,"code":"model_not_found"}}"#;
|
||||||
|
assert_eq!(
|
||||||
|
StreamCheckService::detect_error_category(404, openai_404),
|
||||||
|
Some("modelNotFound")
|
||||||
|
);
|
||||||
|
|
||||||
|
// Anthropic 典型响应:404 + not_found_error + 提到 model
|
||||||
|
let anthropic_404 = r#"{"type":"error","error":{"type":"not_found_error","message":"model: claude-deprecated"}}"#;
|
||||||
|
assert_eq!(
|
||||||
|
StreamCheckService::detect_error_category(404, anthropic_404),
|
||||||
|
Some("modelNotFound")
|
||||||
|
);
|
||||||
|
|
||||||
|
// 400 + invalid model 也算
|
||||||
|
let bad_req = r#"{"error":{"message":"invalid model specified"}}"#;
|
||||||
|
assert_eq!(
|
||||||
|
StreamCheckService::detect_error_category(400, bad_req),
|
||||||
|
Some("modelNotFound")
|
||||||
|
);
|
||||||
|
|
||||||
|
// 通用 404(比如 Base URL 错误),body 里没有 model 字样 → 不应误判
|
||||||
|
let generic_404 = r#"{"error":"Not Found"}"#;
|
||||||
|
assert_eq!(
|
||||||
|
StreamCheckService::detect_error_category(404, generic_404),
|
||||||
|
None
|
||||||
|
);
|
||||||
|
|
||||||
|
// 5xx 就算 body 里有 "model does not exist" 也不分类(避免误判)
|
||||||
|
let server_error = r#"{"error":"model does not exist"}"#;
|
||||||
|
assert_eq!(
|
||||||
|
StreamCheckService::detect_error_category(500, server_error),
|
||||||
|
None
|
||||||
|
);
|
||||||
|
|
||||||
|
// 401 鉴权错误(body 里没有 model 字样)
|
||||||
|
let auth_err = r#"{"error":"Invalid API key"}"#;
|
||||||
|
assert_eq!(
|
||||||
|
StreamCheckService::detect_error_category(401, auth_err),
|
||||||
|
None
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_os_name() {
|
fn test_get_os_name() {
|
||||||
let os_name = StreamCheckService::get_os_name();
|
let os_name = StreamCheckService::get_os_name();
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export function ModelTestConfigPanel() {
|
|||||||
degradedThresholdMs: "6000",
|
degradedThresholdMs: "6000",
|
||||||
claudeModel: "claude-haiku-4-5-20251001",
|
claudeModel: "claude-haiku-4-5-20251001",
|
||||||
codexModel: "gpt-5.4@low",
|
codexModel: "gpt-5.4@low",
|
||||||
geminiModel: "gemini-3-pro-preview",
|
geminiModel: "gemini-3-flash-preview",
|
||||||
testPrompt: "Who are you?",
|
testPrompt: "Who are you?",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -46,6 +46,22 @@ export function useStreamCheck(appId: AppId) {
|
|||||||
|
|
||||||
// 降级状态也重置熔断器,因为至少能通信
|
// 降级状态也重置熔断器,因为至少能通信
|
||||||
resetCircuitBreaker.mutate({ providerId, appType: appId });
|
resetCircuitBreaker.mutate({ providerId, appType: appId });
|
||||||
|
} else if (result.errorCategory === "modelNotFound") {
|
||||||
|
// 专门处理"模型不存在/已下架":指向配置入口,比通用 404 文案更有指导性
|
||||||
|
toast.error(
|
||||||
|
t("streamCheck.modelNotFound", {
|
||||||
|
providerName: providerName,
|
||||||
|
model: result.modelUsed,
|
||||||
|
defaultValue: `${providerName} 测试模型 ${result.modelUsed} 不存在或已下架`,
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
description: t("streamCheck.modelNotFoundHint", {
|
||||||
|
defaultValue: "",
|
||||||
|
}),
|
||||||
|
duration: 10000,
|
||||||
|
closeButton: true,
|
||||||
|
},
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
const httpStatus = result.httpStatus;
|
const httpStatus = result.httpStatus;
|
||||||
const hintKey = httpStatus
|
const hintKey = httpStatus
|
||||||
|
|||||||
@@ -2131,6 +2131,8 @@
|
|||||||
"failed": "{{providerName}} check failed: {{message}}",
|
"failed": "{{providerName}} check failed: {{message}}",
|
||||||
"rejected": "{{providerName}} check rejected: {{message}}",
|
"rejected": "{{providerName}} check rejected: {{message}}",
|
||||||
"error": "{{providerName}} check error: {{error}}",
|
"error": "{{providerName}} check error: {{error}}",
|
||||||
|
"modelNotFound": "{{providerName}} test model {{model}} does not exist or has been deprecated",
|
||||||
|
"modelNotFoundHint": "This model may have been retired by the provider. Update the default test model in \"Model Test Config\".",
|
||||||
"httpHint": {
|
"httpHint": {
|
||||||
"400": "Provider rejected request format. Health check probe may differ from actual usage.",
|
"400": "Provider rejected request format. Health check probe may differ from actual usage.",
|
||||||
"401": "API key may be invalid, or provider uses OAuth auth. Check failure doesn't mean it's unusable.",
|
"401": "API key may be invalid, or provider uses OAuth auth. Check failure doesn't mean it's unusable.",
|
||||||
|
|||||||
@@ -2131,6 +2131,8 @@
|
|||||||
"failed": "{{providerName}} のチェックに失敗しました: {{message}}",
|
"failed": "{{providerName}} のチェックに失敗しました: {{message}}",
|
||||||
"rejected": "{{providerName}} のチェックが拒否されました: {{message}}",
|
"rejected": "{{providerName}} のチェックが拒否されました: {{message}}",
|
||||||
"error": "{{providerName}} のチェックでエラーが発生しました: {{error}}",
|
"error": "{{providerName}} のチェックでエラーが発生しました: {{error}}",
|
||||||
|
"modelNotFound": "{{providerName}} のテストモデル {{model}} は存在しないか廃止されています",
|
||||||
|
"modelNotFoundHint": "このモデルはプロバイダーにより廃止された可能性があります。「モデルテスト設定」でデフォルトのテストモデルを更新してください。",
|
||||||
"httpHint": {
|
"httpHint": {
|
||||||
"400": "リクエスト形式が拒否されました。ヘルスチェックの形式は実際の使用と異なる場合があります。",
|
"400": "リクエスト形式が拒否されました。ヘルスチェックの形式は実際の使用と異なる場合があります。",
|
||||||
"401": "APIキーが無効か、OAuthなどの認証方式を使用しています。チェック失敗は実際に使えないことを意味しません。",
|
"401": "APIキーが無効か、OAuthなどの認証方式を使用しています。チェック失敗は実際に使えないことを意味しません。",
|
||||||
|
|||||||
@@ -2132,6 +2132,8 @@
|
|||||||
"failed": "{{providerName}} 检查失败: {{message}}",
|
"failed": "{{providerName}} 检查失败: {{message}}",
|
||||||
"rejected": "{{providerName}} 检查被拒: {{message}}",
|
"rejected": "{{providerName}} 检查被拒: {{message}}",
|
||||||
"error": "{{providerName}} 检查出错: {{error}}",
|
"error": "{{providerName}} 检查出错: {{error}}",
|
||||||
|
"modelNotFound": "{{providerName}} 测试模型 {{model}} 不存在或已下架",
|
||||||
|
"modelNotFoundHint": "该模型可能已被供应商弃用。请在\"模型测试配置\"中更新默认测试模型。",
|
||||||
"httpHint": {
|
"httpHint": {
|
||||||
"400": "供应商拒绝了请求格式。健康检查的探测格式可能与实际使用不同。",
|
"400": "供应商拒绝了请求格式。健康检查的探测格式可能与实际使用不同。",
|
||||||
"401": "API Key 可能无效,或供应商使用 OAuth 等认证方式。检查失败不代表实际不可用。",
|
"401": "API Key 可能无效,或供应商使用 OAuth 等认证方式。检查失败不代表实际不可用。",
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ export interface StreamCheckResult {
|
|||||||
modelUsed: string;
|
modelUsed: string;
|
||||||
testedAt: number;
|
testedAt: number;
|
||||||
retryCount: number;
|
retryCount: number;
|
||||||
|
/** 细粒度错误分类,如 "modelNotFound" */
|
||||||
|
errorCategory?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===== 流式健康检查 API =====
|
// ===== 流式健康检查 API =====
|
||||||
|
|||||||
Reference in New Issue
Block a user