Prechádzať zdrojové kódy

fix(proxy): add 'cannot be modified' error detection to thinking signature rectifier

Extend the thinking signature rectifier to detect and handle the
Anthropic API error when thinking/redacted_thinking blocks have been
modified from their original response. This error occurs when clients
inadvertently modify these blocks in multi-turn conversations.

The rectifier will now remove these blocks and retry the request,
similar to how it handles other thinking-related signature errors.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
ding113 1 týždeň pred
rodič
commit
e2b1fb2c09

+ 16 - 0
src/app/v1/_lib/proxy/thinking-signature-rectifier.test.ts

@@ -88,6 +88,22 @@ describe("thinking-signature-rectifier", () => {
         detectThinkingSignatureRectifierTrigger("Signature: EXTRA INPUTS ARE NOT PERMITTED")
       ).toBe("invalid_signature_in_thinking_block");
     });
+
+    test("should detect 'cannot be modified' error", () => {
+      // thinking/redacted_thinking block was modified by client
+      expect(
+        detectThinkingSignatureRectifierTrigger(
+          "`thinking` or `redacted_thinking` blocks in the latest assistant message cannot be modified. These blocks must remain as they were in the original response."
+        )
+      ).toBe("invalid_signature_in_thinking_block");
+
+      // JSON format with request id
+      expect(
+        detectThinkingSignatureRectifierTrigger(
+          '{"error":{"type":"<nil>","message":"***.***.content.53: `thinking` or `redacted_thinking` blocks in the latest assistant message cannot be modified. These blocks must remain as they were in the original response. (request id: 20260203160354671091064E4xAq0t0)"},"type":"error"}'
+        )
+      ).toBe("invalid_signature_in_thinking_block");
+    });
   });
 
   describe("rectifyAnthropicRequestMessage", () => {

+ 11 - 0
src/app/v1/_lib/proxy/thinking-signature-rectifier.ts

@@ -70,6 +70,17 @@ export function detectThinkingSignatureRectifierTrigger(
     return "invalid_signature_in_thinking_block"; // 复用现有触发类型,整流逻辑相同
   }
 
+  // 检测:thinking/redacted_thinking 块被修改的错误
+  // 场景:客户端回传的 thinking 块内容与原始响应不一致
+  // 错误信息:"thinking or redacted_thinking blocks ... cannot be modified"
+  const looksLikeThinkingBlockModified =
+    (lower.includes("thinking") || lower.includes("redacted_thinking")) &&
+    lower.includes("cannot be modified");
+
+  if (looksLikeThinkingBlockModified) {
+    return "invalid_signature_in_thinking_block"; // 复用现有触发类型,整流逻辑相同
+  }
+
   // 与默认错误规则保持一致(Issue #432 / Rule 6)
   if (/非法请求|illegal request|invalid request/i.test(errorMessage)) {
     return "invalid_request";