fix: 优化claude code系统提示词判断

This commit is contained in:
shaw
2025-10-11 17:34:17 +08:00
parent b408a7122d
commit 56fe7be8ec
2 changed files with 57 additions and 4 deletions

View File

@@ -91,7 +91,7 @@ class ClaudeRelayService {
// 🔍 判断是否是真实的 Claude Code 请求 // 🔍 判断是否是真实的 Claude Code 请求
isRealClaudeCodeRequest(requestBody) { isRealClaudeCodeRequest(requestBody) {
return ClaudeCodeValidator.hasClaudeCodeSystemPrompt(requestBody) return ClaudeCodeValidator.includesClaudeCodeSystemPrompt(requestBody, 1)
} }
// 🚀 转发请求到Claude API // 🚀 转发请求到Claude API

View File

@@ -40,7 +40,7 @@ class ClaudeCodeValidator {
* @param {Object} body - 请求体 * @param {Object} body - 请求体
* @returns {boolean} 是否包含 Claude Code 系统提示词 * @returns {boolean} 是否包含 Claude Code 系统提示词
*/ */
static hasClaudeCodeSystemPrompt(body) { static hasClaudeCodeSystemPrompt(body, customThreshold) {
if (!body || typeof body !== 'object') { if (!body || typeof body !== 'object') {
return false return false
} }
@@ -55,12 +55,17 @@ class ClaudeCodeValidator {
return false return false
} }
const threshold =
typeof customThreshold === 'number' && Number.isFinite(customThreshold)
? customThreshold
: SYSTEM_PROMPT_THRESHOLD
for (const entry of systemEntries) { for (const entry of systemEntries) {
const rawText = typeof entry?.text === 'string' ? entry.text : '' const rawText = typeof entry?.text === 'string' ? entry.text : ''
const { bestScore } = bestSimilarityByTemplates(rawText) const { bestScore } = bestSimilarityByTemplates(rawText)
if (bestScore < SYSTEM_PROMPT_THRESHOLD) { if (bestScore < threshold) {
logger.error( logger.error(
`Claude system prompt similarity below threshold: score=${bestScore.toFixed(4)}, threshold=${SYSTEM_PROMPT_THRESHOLD}, prompt=${rawText}` `Claude system prompt similarity below threshold: score=${bestScore.toFixed(4)}, threshold=${threshold}, prompt=${rawText}`
) )
return false return false
} }
@@ -68,6 +73,54 @@ class ClaudeCodeValidator {
return true return true
} }
/**
* 判断是否存在 Claude Code 系统提示词(存在即返回 true
* @param {Object} body - 请求体
* @param {number} [customThreshold] - 自定义阈值
* @returns {boolean} 是否存在 Claude Code 系统提示词
*/
static includesClaudeCodeSystemPrompt(body, customThreshold) {
if (!body || typeof body !== 'object') {
return false
}
const model = typeof body.model === 'string' ? body.model : null
if (!model) {
return false
}
const systemEntries = Array.isArray(body.system) ? body.system : null
if (!systemEntries) {
return false
}
const threshold =
typeof customThreshold === 'number' && Number.isFinite(customThreshold)
? customThreshold
: SYSTEM_PROMPT_THRESHOLD
let bestMatchScore = 0
for (const entry of systemEntries) {
const rawText = typeof entry?.text === 'string' ? entry.text : ''
const { bestScore } = bestSimilarityByTemplates(rawText)
if (bestScore > bestMatchScore) {
bestMatchScore = bestScore
}
if (bestScore >= threshold) {
return true
}
}
logger.debug(
`Claude system prompt not detected: bestScore=${bestMatchScore.toFixed(4)}, threshold=${threshold}`
)
return false
}
/** /**
* 验证请求是否来自 Claude Code CLI * 验证请求是否来自 Claude Code CLI
* @param {Object} req - Express 请求对象 * @param {Object} req - Express 请求对象