mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-23 19:46:16 +00:00
fix: 适配droid调用claude code订阅接口
This commit is contained in:
@@ -561,6 +561,8 @@ class ClaudeRelayService {
|
||||
}
|
||||
}
|
||||
|
||||
this._enforceCacheControlLimit(processedBody)
|
||||
|
||||
// 处理原有的系统提示(如果配置了)
|
||||
if (this.systemPrompt && this.systemPrompt.trim()) {
|
||||
const systemPrompt = {
|
||||
@@ -707,6 +709,107 @@ class ClaudeRelayService {
|
||||
}
|
||||
}
|
||||
|
||||
// ⚖️ 限制带缓存控制的内容数量
|
||||
_enforceCacheControlLimit(body) {
|
||||
const MAX_CACHE_CONTROL_BLOCKS = 4
|
||||
|
||||
if (!body || typeof body !== 'object') {
|
||||
return
|
||||
}
|
||||
|
||||
const countCacheControlBlocks = () => {
|
||||
let total = 0
|
||||
|
||||
if (Array.isArray(body.messages)) {
|
||||
body.messages.forEach((message) => {
|
||||
if (!message || !Array.isArray(message.content)) {
|
||||
return
|
||||
}
|
||||
message.content.forEach((item) => {
|
||||
if (item && item.cache_control) {
|
||||
total += 1
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
if (Array.isArray(body.system)) {
|
||||
body.system.forEach((item) => {
|
||||
if (item && item.cache_control) {
|
||||
total += 1
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return total
|
||||
}
|
||||
|
||||
const removeFromMessages = () => {
|
||||
if (!Array.isArray(body.messages)) {
|
||||
return false
|
||||
}
|
||||
|
||||
for (let messageIndex = 0; messageIndex < body.messages.length; messageIndex += 1) {
|
||||
const message = body.messages[messageIndex]
|
||||
if (!message || !Array.isArray(message.content)) {
|
||||
continue
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
const removeFromSystem = () => {
|
||||
if (!Array.isArray(body.system)) {
|
||||
return false
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
let total = countCacheControlBlocks()
|
||||
|
||||
while (total > MAX_CACHE_CONTROL_BLOCKS) {
|
||||
if (removeFromMessages()) {
|
||||
total -= 1
|
||||
continue
|
||||
}
|
||||
|
||||
if (removeFromSystem()) {
|
||||
total -= 1
|
||||
continue
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// 🌐 获取代理Agent(使用统一的代理工具)
|
||||
async _getProxyAgent(accountId) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user