feat:优化屎山
This commit is contained in:
@@ -322,7 +322,18 @@ class RedisCache:
|
||||
logger.error(f"获取对话历史失败: {chat_id}, {e}")
|
||||
return []
|
||||
|
||||
def add_chat_message(self, chat_id: str, role: str, content, ttl: int = 86400) -> bool:
|
||||
def add_chat_message(
|
||||
self,
|
||||
chat_id: str,
|
||||
role: str,
|
||||
content,
|
||||
ttl: int = 86400,
|
||||
*,
|
||||
nickname: str = None,
|
||||
sender_wxid: str = None,
|
||||
record_id: str = None,
|
||||
timestamp: float = None,
|
||||
) -> bool:
|
||||
"""
|
||||
添加消息到对话历史
|
||||
|
||||
@@ -331,6 +342,10 @@ class RedisCache:
|
||||
role: 角色 (user/assistant)
|
||||
content: 消息内容(字符串或列表)
|
||||
ttl: 过期时间(秒),默认24小时
|
||||
nickname: 可选昵称(用于统一 schema)
|
||||
sender_wxid: 可选发送者 wxid
|
||||
record_id: 可选记录 ID
|
||||
timestamp: 可选时间戳
|
||||
|
||||
Returns:
|
||||
是否添加成功
|
||||
@@ -340,7 +355,18 @@ class RedisCache:
|
||||
|
||||
try:
|
||||
key = self._make_key("chat_history", chat_id)
|
||||
message = {"role": role, "content": content}
|
||||
import time as _time
|
||||
message = {
|
||||
"role": role or "user",
|
||||
"content": content,
|
||||
}
|
||||
if nickname:
|
||||
message["nickname"] = nickname
|
||||
if sender_wxid:
|
||||
message["wxid"] = sender_wxid
|
||||
if record_id:
|
||||
message["id"] = record_id
|
||||
message["timestamp"] = timestamp or _time.time()
|
||||
self.client.rpush(key, json.dumps(message, ensure_ascii=False))
|
||||
self.client.expire(key, ttl)
|
||||
return True
|
||||
@@ -416,8 +442,17 @@ class RedisCache:
|
||||
logger.error(f"获取群聊历史失败: {group_id}, {e}")
|
||||
return []
|
||||
|
||||
def add_group_message(self, group_id: str, nickname: str, content,
|
||||
record_id: str = None, ttl: int = 86400) -> bool:
|
||||
def add_group_message(
|
||||
self,
|
||||
group_id: str,
|
||||
nickname: str,
|
||||
content,
|
||||
record_id: str = None,
|
||||
*,
|
||||
role: str = "user",
|
||||
sender_wxid: str = None,
|
||||
ttl: int = 86400,
|
||||
) -> bool:
|
||||
"""
|
||||
添加消息到群聊历史
|
||||
|
||||
@@ -426,6 +461,8 @@ class RedisCache:
|
||||
nickname: 发送者昵称
|
||||
content: 消息内容
|
||||
record_id: 可选的记录ID,用于后续更新
|
||||
role: 角色 (user/assistant),默认 user
|
||||
sender_wxid: 可选的发送者 wxid
|
||||
ttl: 过期时间(秒),默认24小时
|
||||
|
||||
Returns:
|
||||
@@ -438,10 +475,13 @@ class RedisCache:
|
||||
import time
|
||||
key = self._make_key("group_history", group_id)
|
||||
message = {
|
||||
"role": role or "user",
|
||||
"nickname": nickname,
|
||||
"content": content,
|
||||
"timestamp": time.time()
|
||||
}
|
||||
if sender_wxid:
|
||||
message["wxid"] = sender_wxid
|
||||
if record_id:
|
||||
message["id"] = record_id
|
||||
|
||||
|
||||
Reference in New Issue
Block a user