mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-23 09:38:02 +00:00
refactor: standardize code formatting and linting configuration
- Replace .eslintrc.js with .eslintrc.cjs for better ES module compatibility - Add .prettierrc configuration for consistent code formatting - Update package.json with new lint and format scripts - Add nodemon.json for development hot reloading configuration - Standardize code formatting across all JavaScript and Vue files - Update web admin SPA with improved linting rules and formatting - Add prettier configuration to web admin SPA 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
const crypto = require('crypto');
|
||||
const logger = require('./logger');
|
||||
const crypto = require('crypto')
|
||||
const logger = require('./logger')
|
||||
|
||||
class SessionHelper {
|
||||
/**
|
||||
@@ -10,92 +10,104 @@ class SessionHelper {
|
||||
*/
|
||||
generateSessionHash(requestBody) {
|
||||
if (!requestBody || typeof requestBody !== 'object') {
|
||||
return null;
|
||||
return null
|
||||
}
|
||||
|
||||
let cacheableContent = '';
|
||||
const system = requestBody.system || '';
|
||||
const messages = requestBody.messages || [];
|
||||
let cacheableContent = ''
|
||||
const system = requestBody.system || ''
|
||||
const messages = requestBody.messages || []
|
||||
|
||||
// 1. 优先提取带有cache_control: {"type": "ephemeral"}的内容
|
||||
// 检查system中的cacheable内容
|
||||
if (Array.isArray(system)) {
|
||||
for (const part of system) {
|
||||
if (part && part.cache_control && part.cache_control.type === 'ephemeral') {
|
||||
cacheableContent += part.text || '';
|
||||
cacheableContent += part.text || ''
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 检查messages中的cacheable内容
|
||||
for (const msg of messages) {
|
||||
const content = msg.content || '';
|
||||
const content = msg.content || ''
|
||||
if (Array.isArray(content)) {
|
||||
for (const part of content) {
|
||||
if (part && part.cache_control && part.cache_control.type === 'ephemeral') {
|
||||
if (part.type === 'text') {
|
||||
cacheableContent += part.text || '';
|
||||
cacheableContent += part.text || ''
|
||||
}
|
||||
// 其他类型(如image)不参与hash计算
|
||||
}
|
||||
}
|
||||
} else if (typeof content === 'string' && msg.cache_control && msg.cache_control.type === 'ephemeral') {
|
||||
} else if (
|
||||
typeof content === 'string' &&
|
||||
msg.cache_control &&
|
||||
msg.cache_control.type === 'ephemeral'
|
||||
) {
|
||||
// 罕见情况,但需要检查
|
||||
cacheableContent += content;
|
||||
cacheableContent += content
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 如果有cacheable内容,直接使用
|
||||
if (cacheableContent) {
|
||||
const hash = crypto.createHash('sha256').update(cacheableContent).digest('hex').substring(0, 32);
|
||||
logger.debug(`📋 Session hash generated from cacheable content: ${hash}`);
|
||||
return hash;
|
||||
const hash = crypto
|
||||
.createHash('sha256')
|
||||
.update(cacheableContent)
|
||||
.digest('hex')
|
||||
.substring(0, 32)
|
||||
logger.debug(`📋 Session hash generated from cacheable content: ${hash}`)
|
||||
return hash
|
||||
}
|
||||
|
||||
// 3. Fallback: 使用system内容
|
||||
if (system) {
|
||||
let systemText = '';
|
||||
let systemText = ''
|
||||
if (typeof system === 'string') {
|
||||
systemText = system;
|
||||
systemText = system
|
||||
} else if (Array.isArray(system)) {
|
||||
systemText = system.map(part => part.text || '').join('');
|
||||
systemText = system.map((part) => part.text || '').join('')
|
||||
}
|
||||
|
||||
|
||||
if (systemText) {
|
||||
const hash = crypto.createHash('sha256').update(systemText).digest('hex').substring(0, 32);
|
||||
logger.debug(`📋 Session hash generated from system content: ${hash}`);
|
||||
return hash;
|
||||
const hash = crypto.createHash('sha256').update(systemText).digest('hex').substring(0, 32)
|
||||
logger.debug(`📋 Session hash generated from system content: ${hash}`)
|
||||
return hash
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 最后fallback: 使用第一条消息内容
|
||||
if (messages.length > 0) {
|
||||
const firstMessage = messages[0];
|
||||
let firstMessageText = '';
|
||||
|
||||
const firstMessage = messages[0]
|
||||
let firstMessageText = ''
|
||||
|
||||
if (typeof firstMessage.content === 'string') {
|
||||
firstMessageText = firstMessage.content;
|
||||
firstMessageText = firstMessage.content
|
||||
} else if (Array.isArray(firstMessage.content)) {
|
||||
if (!firstMessage.content) {
|
||||
logger.error('📋 Session hash generated from first message failed: ', firstMessage);
|
||||
logger.error('📋 Session hash generated from first message failed: ', firstMessage)
|
||||
}
|
||||
|
||||
firstMessageText = firstMessage.content
|
||||
.filter(part => part.type === 'text')
|
||||
.map(part => part.text || '')
|
||||
.join('');
|
||||
.filter((part) => part.type === 'text')
|
||||
.map((part) => part.text || '')
|
||||
.join('')
|
||||
}
|
||||
|
||||
|
||||
if (firstMessageText) {
|
||||
const hash = crypto.createHash('sha256').update(firstMessageText).digest('hex').substring(0, 32);
|
||||
logger.debug(`📋 Session hash generated from first message: ${hash}`);
|
||||
return hash;
|
||||
const hash = crypto
|
||||
.createHash('sha256')
|
||||
.update(firstMessageText)
|
||||
.digest('hex')
|
||||
.substring(0, 32)
|
||||
logger.debug(`📋 Session hash generated from first message: ${hash}`)
|
||||
return hash
|
||||
}
|
||||
}
|
||||
|
||||
// 无法生成会话哈希
|
||||
logger.debug('📋 Unable to generate session hash - no suitable content found');
|
||||
return null;
|
||||
logger.debug('📋 Unable to generate session hash - no suitable content found')
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,7 +116,7 @@ class SessionHelper {
|
||||
* @returns {string} - Redis键名
|
||||
*/
|
||||
getSessionRedisKey(sessionHash) {
|
||||
return `sticky_session:${sessionHash}`;
|
||||
return `sticky_session:${sessionHash}`
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,10 +125,12 @@ class SessionHelper {
|
||||
* @returns {boolean} - 是否有效
|
||||
*/
|
||||
isValidSessionHash(sessionHash) {
|
||||
return typeof sessionHash === 'string' &&
|
||||
sessionHash.length === 32 &&
|
||||
/^[a-f0-9]{32}$/.test(sessionHash);
|
||||
return (
|
||||
typeof sessionHash === 'string' &&
|
||||
sessionHash.length === 32 &&
|
||||
/^[a-f0-9]{32}$/.test(sessionHash)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new SessionHelper();
|
||||
module.exports = new SessionHelper()
|
||||
|
||||
Reference in New Issue
Block a user