fix: 优化 Anthropic 流式对话错误提示并移除 -thinking 处理

- 统一并增强错误消息:在响应包含 "prompt is too long" 或 "提示词太长" 时,增加友好提示,建议在 claudecode 中执行 /compact 或开启新会话重试。
- 将流式与非流式的异常信息处理统一,抛出包含详细提示的异常并保留日志。
- 移除对 input.Model.EndsWith("-thinking") 及替换 "-thinking" 的处理(清理冗余逻辑)。
This commit is contained in:
chenchun
2026-01-26 11:37:31 +08:00
parent 9b5826a6b1
commit ca43879cc3

View File

@@ -77,9 +77,9 @@ public class AnthropicChatCompletionsService(
var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
var message = $"恭喜你运气爆棚遇到了错误尊享包对话异常StatusCode【{response.StatusCode.GetHashCode()}】ErrorId【{errorId}】"; var message = $"恭喜你运气爆棚遇到了错误尊享包对话异常StatusCode【{response.StatusCode.GetHashCode()}】ErrorId【{errorId}】";
if (error.Contains("prompt is too long")) if (error.Contains("prompt is too long") || error.Contains("提示词太长"))
{ {
message += $", Response: {error}"; message += $", tip: 当前提示词过长,上下文已达到上限,如在 claudecode中使用建议执行/compact压缩当前会话或开启新会话后重试";
} }
logger.LogError( logger.LogError(
@@ -115,9 +115,6 @@ public class AnthropicChatCompletionsService(
{ "anthropic-version", "2023-06-01" } { "anthropic-version", "2023-06-01" }
}; };
var isThinking = input.Model.EndsWith("thinking");
input.Model = input.Model.Replace("-thinking", string.Empty);
var response = await client.HttpRequestRaw(options.Endpoint.TrimEnd('/') + "/v1/messages", input, string.Empty, var response = await client.HttpRequestRaw(options.Endpoint.TrimEnd('/') + "/v1/messages", input, string.Empty,
headers); headers);
@@ -129,11 +126,17 @@ public class AnthropicChatCompletionsService(
{ {
Guid errorId = Guid.NewGuid(); Guid errorId = Guid.NewGuid();
var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
var message = $"恭喜你运气爆棚遇到了错误尊享包对话异常StatusCode【{response.StatusCode.GetHashCode()}】ErrorId【{errorId}】";
if (error.Contains("prompt is too long") || error.Contains("提示词太长"))
{
message += $", tip: 当前提示词过长,上下文已达到上限,如在 claudecode中使用建议执行/compact压缩当前会话或开启新会话后重试";
}
logger.LogError( logger.LogError(
$"Anthropic流式对话异常 请求地址:{options.Endpoint},ErrorId{errorId}, StatusCode: {response.StatusCode.GetHashCode()}, Response: {error}"); $"Anthropic流式对话异常 请求地址:{options.Endpoint},ErrorId{errorId}, StatusCode: {response.StatusCode.GetHashCode()}, Response: {error}");
throw new Exception( throw new Exception(message);
$"恭喜你运气爆棚遇到了错误尊享包对话异常StatusCode【{response.StatusCode.GetHashCode()}】ErrorId【{errorId}】");
} }
using var stream = new StreamReader(await response.Content.ReadAsStreamAsync(cancellationToken)); using var stream = new StreamReader(await response.Content.ReadAsStreamAsync(cancellationToken));