mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-22 16:43:35 +00:00
fix: 优化claude code系统提示词判断
This commit is contained in:
@@ -91,7 +91,7 @@ class ClaudeRelayService {
|
||||
|
||||
// 🔍 判断是否是真实的 Claude Code 请求
|
||||
isRealClaudeCodeRequest(requestBody) {
|
||||
return ClaudeCodeValidator.hasClaudeCodeSystemPrompt(requestBody)
|
||||
return ClaudeCodeValidator.includesClaudeCodeSystemPrompt(requestBody, 1)
|
||||
}
|
||||
|
||||
// 🚀 转发请求到Claude API
|
||||
|
||||
@@ -40,7 +40,7 @@ class ClaudeCodeValidator {
|
||||
* @param {Object} body - 请求体
|
||||
* @returns {boolean} 是否包含 Claude Code 系统提示词
|
||||
*/
|
||||
static hasClaudeCodeSystemPrompt(body) {
|
||||
static hasClaudeCodeSystemPrompt(body, customThreshold) {
|
||||
if (!body || typeof body !== 'object') {
|
||||
return false
|
||||
}
|
||||
@@ -55,12 +55,17 @@ class ClaudeCodeValidator {
|
||||
return false
|
||||
}
|
||||
|
||||
const threshold =
|
||||
typeof customThreshold === 'number' && Number.isFinite(customThreshold)
|
||||
? customThreshold
|
||||
: SYSTEM_PROMPT_THRESHOLD
|
||||
|
||||
for (const entry of systemEntries) {
|
||||
const rawText = typeof entry?.text === 'string' ? entry.text : ''
|
||||
const { bestScore } = bestSimilarityByTemplates(rawText)
|
||||
if (bestScore < SYSTEM_PROMPT_THRESHOLD) {
|
||||
if (bestScore < threshold) {
|
||||
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
|
||||
}
|
||||
@@ -68,6 +73,54 @@ class ClaudeCodeValidator {
|
||||
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
|
||||
* @param {Object} req - Express 请求对象
|
||||
|
||||
Reference in New Issue
Block a user