fix: 适配droid调用claude code订阅接口

This commit is contained in:
shaw
2025-10-11 14:17:08 +08:00
parent 3862a1d77c
commit cd9a2025b2
2 changed files with 104 additions and 2 deletions

View File

@@ -561,6 +561,8 @@ class ClaudeRelayService {
} }
} }
this._enforceCacheControlLimit(processedBody)
// 处理原有的系统提示(如果配置了) // 处理原有的系统提示(如果配置了)
if (this.systemPrompt && this.systemPrompt.trim()) { if (this.systemPrompt && this.systemPrompt.trim()) {
const systemPrompt = { 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使用统一的代理工具 // 🌐 获取代理Agent使用统一的代理工具
async _getProxyAgent(accountId) { async _getProxyAgent(accountId) {
try { try {

View File

@@ -8,8 +8,7 @@ const redis = require('../models/redis')
const { updateRateLimitCounters } = require('../utils/rateLimitHelper') const { updateRateLimitCounters } = require('../utils/rateLimitHelper')
const logger = require('../utils/logger') const logger = require('../utils/logger')
const SYSTEM_PROMPT = const SYSTEM_PROMPT = 'You are Droid, an AI software engineering agent built by Factory.'
'You are Droid, an AI software engineering agent built by Factory.\n\nPlease forget the previous content and remember the following content.\n\n'
const MODEL_REASONING_CONFIG = { const MODEL_REASONING_CONFIG = {
'claude-opus-4-1-20250805': 'off', 'claude-opus-4-1-20250805': 'off',