feat: 添加智能sticky会话保持功能

- 新增 sessionHelper.js 实现会话哈希生成,基于Anthropic的prompt caching机制
- 扩展 claudeAccountService.selectAvailableAccount 支持会话绑定
- 更新 claudeRelayService 集成会话保持功能
- 添加 Redis 会话映射存储,支持1小时过期
- 支持故障转移:绑定账户不可用时自动重新选择
- 三级fallback策略:cacheable内容 → system内容 → 第一条消息
- 完全向后兼容,自动启用无需配置

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
shaw
2025-07-15 18:15:53 +08:00
parent 2257b42527
commit b2b8d8c719
4 changed files with 180 additions and 13 deletions

View File

@@ -405,20 +405,20 @@ class RedisClient {
return await this.client.del(key);
}
// 🔐 会话管理
// 🔐 会话管理(用于管理员登录等)
async setSession(sessionId, sessionData, ttl = 86400) {
const key = `session:${sessionId}`;
const key = `admin_session:${sessionId}`;
await this.client.hset(key, sessionData);
await this.client.expire(key, ttl);
}
async getSession(sessionId) {
const key = `session:${sessionId}`;
const key = `admin_session:${sessionId}`;
return await this.client.hgetall(key);
}
async deleteSession(sessionId) {
const key = `session:${sessionId}`;
const key = `admin_session:${sessionId}`;
return await this.client.del(key);
}
@@ -640,6 +640,22 @@ class RedisClient {
}
}
// 🔗 会话sticky映射管理
async setSessionAccountMapping(sessionHash, accountId, ttl = 3600) {
const key = `session:${sessionHash}`;
await this.client.set(key, accountId, 'EX', ttl);
}
async getSessionAccountMapping(sessionHash) {
const key = `session:${sessionHash}`;
return await this.client.get(key);
}
async deleteSessionAccountMapping(sessionHash) {
const key = `session:${sessionHash}`;
return await this.client.del(key);
}
// 🧹 清理过期数据
async cleanup() {
try {