话术redis更新
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -7,3 +7,4 @@ ragflow/client_backup.go.bak
|
|||||||
ragflow/为什么不能使用gclient.md
|
ragflow/为什么不能使用gclient.md
|
||||||
ragflow/agent文档.md
|
ragflow/agent文档.md
|
||||||
/.idea/MarsCodeWorkspaceAppSettings.xml
|
/.idea/MarsCodeWorkspaceAppSettings.xml
|
||||||
|
consul/配置中心方案.md
|
||||||
|
|||||||
@@ -479,45 +479,84 @@ func Unlock(ctx context.Context, key string) {
|
|||||||
// ============== 对话计数相关(用于卡片触发)==============
|
// ============== 对话计数相关(用于卡片触发)==============
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// ConversationCountKeyPrefix 对话计数 Key 前缀
|
// UserStateKeyPrefix 用户会话状态 Key 前缀(融合阶段+计数)
|
||||||
ConversationCountKeyPrefix = "ragflow:conversation:count:"
|
UserStateKeyPrefix = "ragflow:user:state:"
|
||||||
|
// UserStateExpireSeconds 用户状态过期时间(5分钟)
|
||||||
|
UserStateExpireSeconds = 300
|
||||||
)
|
)
|
||||||
|
|
||||||
// IncrConversationCount 增加用户对话计数,返回当前轮数
|
// UserState 用户会话状态(阶段+对话计数,统一5分钟过期)
|
||||||
// 用于判断是否触发发送卡片(如对话5轮后发送)
|
type UserState struct {
|
||||||
// expireSeconds: 过期时间(秒),建议与会话超时一致(如7200秒=2小时)
|
Stage int `json:"stage"` // 用户阶段:0=AI模型 1=打招呼 2=业务 3=发卡片
|
||||||
func IncrConversationCount(ctx context.Context, userId, platform string, expireSeconds int64) (count int64, err error) {
|
Count int64 `json:"count"` // 对话计数
|
||||||
key := ConversationCountKeyPrefix + userId + "_" + platform
|
|
||||||
result, err := redisClient.Do(ctx, "INCR", key)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
count = result.Int64()
|
|
||||||
|
|
||||||
// 首次设置过期时间
|
|
||||||
if count == 1 {
|
|
||||||
redisClient.Do(ctx, "EXPIRE", key, expireSeconds)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetConversationCount 获取用户当前对话轮数
|
// GetUserState 获取用户状态(阶段+计数)
|
||||||
func GetConversationCount(ctx context.Context, userId, platform string) (count int64, err error) {
|
func GetUserState(ctx context.Context, userId, platform string) (state *UserState, err error) {
|
||||||
key := ConversationCountKeyPrefix + userId + "_" + platform
|
key := UserStateKeyPrefix + userId + "_" + platform
|
||||||
result, err := redisClient.Get(ctx, key)
|
result, err := redisClient.Do(ctx, "HGETALL", key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
state = &UserState{}
|
||||||
if result.IsEmpty() {
|
if result.IsEmpty() {
|
||||||
return 0, nil
|
return // 返回默认值 stage=0, count=0
|
||||||
}
|
}
|
||||||
count = result.Int64()
|
|
||||||
|
m := result.Map()
|
||||||
|
state.Stage = gconv.Int(m["stage"])
|
||||||
|
state.Count = gconv.Int64(m["count"])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResetConversationCount 重置用户对话计数(归档/卡片发送后调用)
|
// SetUserStage 设置用户阶段,并刷新过期时间
|
||||||
func ResetConversationCount(ctx context.Context, userId, platform string) error {
|
func SetUserStage(ctx context.Context, userId, platform string, stage int) error {
|
||||||
key := ConversationCountKeyPrefix + userId + "_" + platform
|
key := UserStateKeyPrefix + userId + "_" + platform
|
||||||
|
_, err := redisClient.Do(ctx, "HSET", key, "stage", stage)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = redisClient.Do(ctx, "EXPIRE", key, UserStateExpireSeconds)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// IncrUserCount 增加用户对话计数,返回当前轮数,并刷新过期时间
|
||||||
|
func IncrUserCount(ctx context.Context, userId, platform string) (count int64, err error) {
|
||||||
|
key := UserStateKeyPrefix + userId + "_" + platform
|
||||||
|
result, err := redisClient.Do(ctx, "HINCRBY", key, "count", 1)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
count = result.Int64()
|
||||||
|
_, err = redisClient.Do(ctx, "EXPIRE", key, UserStateExpireSeconds)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResetUserState 重置用户状态(归档时调用)
|
||||||
|
func ResetUserState(ctx context.Context, userId, platform string) error {
|
||||||
|
key := UserStateKeyPrefix + userId + "_" + platform
|
||||||
_, err := redisClient.Del(ctx, key)
|
_, err := redisClient.Del(ctx, key)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ========== 以下为兼容旧接口(内部调用新实现)==========
|
||||||
|
|
||||||
|
// IncrConversationCount 增加用户对话计数(兼容旧接口)
|
||||||
|
func IncrConversationCount(ctx context.Context, userId, platform string, _ int64) (count int64, err error) {
|
||||||
|
return IncrUserCount(ctx, userId, platform)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetConversationCount 获取用户当前对话轮数(兼容旧接口)
|
||||||
|
func GetConversationCount(ctx context.Context, userId, platform string) (count int64, err error) {
|
||||||
|
state, err := GetUserState(ctx, userId, platform)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return state.Count, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResetConversationCount 重置用户对话计数(兼容旧接口)
|
||||||
|
func ResetConversationCount(ctx context.Context, userId, platform string) error {
|
||||||
|
return ResetUserState(ctx, userId, platform)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user