mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-22 16:43:35 +00:00
Merge pull request #727 from xilu0/main
fix: 修复 Claude API 400 错误:tool_result/tool_use 不匹配问题
This commit is contained in:
@@ -789,7 +789,8 @@ class ClaudeRelayService {
|
||||
return total
|
||||
}
|
||||
|
||||
const removeFromMessages = () => {
|
||||
// 只移除 cache_control 属性,保留内容本身,避免丢失用户消息
|
||||
const removeCacheControlFromMessages = () => {
|
||||
if (!Array.isArray(body.messages)) {
|
||||
return false
|
||||
}
|
||||
@@ -803,12 +804,8 @@ class ClaudeRelayService {
|
||||
for (let contentIndex = 0; contentIndex < message.content.length; contentIndex += 1) {
|
||||
const contentItem = message.content[contentIndex]
|
||||
if (contentItem && contentItem.cache_control) {
|
||||
message.content.splice(contentIndex, 1)
|
||||
|
||||
if (message.content.length === 0) {
|
||||
body.messages.splice(messageIndex, 1)
|
||||
}
|
||||
|
||||
// 只删除 cache_control 属性,保留内容
|
||||
delete contentItem.cache_control
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -817,7 +814,8 @@ class ClaudeRelayService {
|
||||
return false
|
||||
}
|
||||
|
||||
const removeFromSystem = () => {
|
||||
// 只移除 cache_control 属性,保留 system 内容
|
||||
const removeCacheControlFromSystem = () => {
|
||||
if (!Array.isArray(body.system)) {
|
||||
return false
|
||||
}
|
||||
@@ -825,12 +823,8 @@ class ClaudeRelayService {
|
||||
for (let index = 0; index < body.system.length; index += 1) {
|
||||
const systemItem = body.system[index]
|
||||
if (systemItem && systemItem.cache_control) {
|
||||
body.system.splice(index, 1)
|
||||
|
||||
if (body.system.length === 0) {
|
||||
delete body.system
|
||||
}
|
||||
|
||||
// 只删除 cache_control 属性,保留内容
|
||||
delete systemItem.cache_control
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -841,12 +835,13 @@ class ClaudeRelayService {
|
||||
let total = countCacheControlBlocks()
|
||||
|
||||
while (total > MAX_CACHE_CONTROL_BLOCKS) {
|
||||
if (removeFromMessages()) {
|
||||
// 优先从 messages 中移除 cache_control,再从 system 中移除
|
||||
if (removeCacheControlFromMessages()) {
|
||||
total -= 1
|
||||
continue
|
||||
}
|
||||
|
||||
if (removeFromSystem()) {
|
||||
if (removeCacheControlFromSystem()) {
|
||||
total -= 1
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user