From ca43879cc34c0d4b5e52785923e0bd2df263fce4 Mon Sep 17 00:00:00 2001 From: chenchun Date: Mon, 26 Jan 2026 11:37:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=20Anthropic=20?= =?UTF-8?q?=E6=B5=81=E5=BC=8F=E5=AF=B9=E8=AF=9D=E9=94=99=E8=AF=AF=E6=8F=90?= =?UTF-8?q?=E7=A4=BA=E5=B9=B6=E7=A7=BB=E9=99=A4=20-thinking=20=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 统一并增强错误消息:在响应包含 "prompt is too long" 或 "提示词太长" 时,增加友好提示,建议在 claudecode 中执行 /compact 或开启新会话重试。 - 将流式与非流式的异常信息处理统一,抛出包含详细提示的异常并保留日志。 - 移除对 input.Model.EndsWith("-thinking") 及替换 "-thinking" 的处理(清理冗余逻辑)。 --- .../Chats/AnthropicChatCompletionsService.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiGateWay/Impl/ThorClaude/Chats/AnthropicChatCompletionsService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiGateWay/Impl/ThorClaude/Chats/AnthropicChatCompletionsService.cs index ee84d1f6..8643cac2 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiGateWay/Impl/ThorClaude/Chats/AnthropicChatCompletionsService.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiGateWay/Impl/ThorClaude/Chats/AnthropicChatCompletionsService.cs @@ -77,9 +77,9 @@ public class AnthropicChatCompletionsService( var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); 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( @@ -115,9 +115,6 @@ public class AnthropicChatCompletionsService( { "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, headers); @@ -129,11 +126,17 @@ public class AnthropicChatCompletionsService( { Guid errorId = Guid.NewGuid(); 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( $"Anthropic流式对话异常 请求地址:{options.Endpoint},ErrorId:{errorId}, StatusCode: {response.StatusCode.GetHashCode()}, Response: {error}"); - throw new Exception( - $"恭喜你运气爆棚遇到了错误,尊享包对话异常:StatusCode【{response.StatusCode.GetHashCode()}】,ErrorId【{errorId}】"); + throw new Exception(message); } using var stream = new StreamReader(await response.Content.ReadAsStreamAsync(cancellationToken));