本文档设计两个API来增强agent模块中Redis对话历史记录的管理能力:
/api/v0/conversation_limit_enforcement
- 对话限额执行API/api/v0/conversation_cleanup
- 对话清理API增强版这两个API专门操作agent模块写入Redis的三种Key:
conversation:{conversation_id}:meta
(对话元数据)conversation:{conversation_id}:messages
(对话消息列表)user:{user_id}:conversations
(用户对话索引)POST /api/v0/conversation_limit_enforcement
LTRIM
策略可能遗漏的情况参数 | 类型 | 必填 | 描述 |
---|---|---|---|
user_id |
String | 否 | 指定用户ID,如果提供则只处理该用户的数据 |
user_max_conversations |
Integer | 否 | 每个用户保留的最大对话数,默认从 app_config.py 获取 |
conversation_max_length |
Integer | 否 | 每个对话保留的最大消息数,默认从 app_config.py 获取 |
dry_run |
Boolean | 否 | 是否为试运行模式,默认 false 。为 true 时只返回分析结果,不执行实际操作 |
user_id
)user:*:conversations
Keyuser_id
)user:{user_id}:conversations
用户对话数限制:
user:{user_id}:conversations
user_max_conversations
:meta
, :messages
)单对话消息数限制:
conversation_max_length
LTRIM
只保留最新的N条消息{
"success": true,
"message": "限额执行完成",
"data": {
"mode": "global|user_specific",
"dry_run": false,
"parameters": {
"user_max_conversations": 5,
"conversation_max_length": 10
},
"processed_users": 15,
"total_conversations_processed": 45,
"total_conversations_deleted": 8,
"total_messages_trimmed": 120,
"execution_summary": [
{
"user_id": "guest",
"original_conversations": 7,
"kept_conversations": 5,
"deleted_conversations": 2,
"messages_trimmed": 15
}
],
"execution_time_ms": 1250
}
}
POST /api/v0/conversation_cleanup
参数 | 类型 | 必填 | 描述 |
---|---|---|---|
user_id |
String | 否 | 删除指定用户的所有对话数据 |
conversation_id |
String | 否 | 删除指定的对话 (支持 conversation_id 格式) |
thread_id |
String | 否 | 删除指定的对话 (支持 thread_id 格式,与 conversation_id 等效) |
clear_all_agent_data |
Boolean | 否 | 是否清空所有agent对话数据,默认 false |
cleanup_invalid_refs |
Boolean | 否 | 是否只清理无效引用,默认 false |
注意: 所有操作模式参数互斥,一次请求只能执行一种操作:
user_id
(模式1)conversation_id
或 thread_id
(模式2)clear_all_agent_data: true
(模式3)cleanup_invalid_refs: true
(模式4)如果同时提供多个操作参数,API将返回参数冲突错误。
user_id
)user:{user_id}:conversations
:meta
和 :messages
user:{user_id}:conversations
conversation_id
或 thread_id
)conversation:{id}:meta
conversation:{id}:messages
user:{user_id}:conversations
中移除该对话IDclear_all_agent_data: true
)⚠️ 危险操作: 此模式将完全清空所有agent对话历史数据,不可恢复!
参数冲突处理: 如果同时提供了其他操作模式参数(如 user_id
、conversation_id
、thread_id
、cleanup_invalid_refs
),API将立即返回参数冲突错误,不会执行任何删除操作。
执行步骤:
conversation:*:meta
Keyconversation:*:messages
Keyuser:*:conversations
Keycleanup_invalid_refs: true
)user:*:conversations
列表conversation:{id}:meta
是否存在如果请求中没有提供任何参数,API将返回错误,要求明确指定操作模式。
{
"success": false,
"message": "参数冲突:不能同时提供多个操作模式参数",
"error": "检测到冲突参数: user_id 和 clear_all_agent_data",
"valid_modes": [
"user_id (删除指定用户)",
"conversation_id 或 thread_id (删除指定对话,两参数等同)",
"clear_all_agent_data (清空所有数据)",
"cleanup_invalid_refs (清理无效引用)"
]
}
{
"success": true,
"message": "用户对话数据删除完成",
"data": {
"operation_mode": "delete_user",
"user_id": "guest",
"deleted_conversations": 5,
"deleted_messages": 47,
"execution_time_ms": 150
}
}
{
"success": true,
"message": "对话删除完成",
"data": {
"operation_mode": "delete_conversation",
"conversation_id": "guest:20250125143022155",
"user_id": "guest",
"deleted_messages": 8,
"execution_time_ms": 45
}
}
{
"success": true,
"message": "所有agent对话数据清空完成",
"data": {
"operation_mode": "clear_all_agent_data",
"deleted_conversation_metas": 150,
"deleted_conversation_messages": 150,
"deleted_user_conversations": 25,
"total_keys_deleted": 325,
"execution_time_ms": 2500
}
}
{
"success": true,
"message": "无效引用清理完成",
"data": {
"operation_mode": "cleanup_invalid_refs",
"processed_users": 12,
"cleaned_references": 3,
"execution_time_ms": 200
}
}
clear_all_agent_data
是危险操作,建议增加二次确认机制user_id
/ conversation_id
(thread_id
) / clear_all_agent_data
/cleanup_invalid_refs
conversation_id
和 thread_id
完全等同clear_all_agent_data
操作,增加额外的安全检查全局限额执行:
curl -X POST -H "Content-Type: application/json" \
-d '{"user_max_conversations": 3, "conversation_max_length": 8}' \
http://127.0.0.1:5000/api/v0/conversation_limit_enforcement
bash
curl -X POST -H "Content-Type: application/json" \
-d '{"user_id": "guest", "user_max_conversations": 5}' \
http://127.0.0.1:5000/api/v0/conversation_limit_enforcement
试运行模式:
curl -X POST -H "Content-Type: application/json" \
-d '{"dry_run": true}' \
http://127.0.0.1:5000/api/v0/conversation_limit_enforcement
bash
curl -X POST -H "Content-Type: application/json" \
-d '{"user_id": "guest"}' \
http://127.0.0.1:5000/api/v0/conversation_cleanup
删除指定对话:
curl -X POST -H "Content-Type: application/json" \
-d '{"conversation_id": "guest:20250125143022155"}' \
http://127.0.0.1:5000/api/v0/conversation_cleanup
bash
curl -X POST -H "Content-Type: application/json" \
-d '{"clear_all_agent_data": true}' \
http://127.0.0.1:5000/api/v0/conversation_cleanup
清理无效引用:
curl -X POST -H "Content-Type: application/json" \
-d '{"cleanup_invalid_refs": true}' \
http://127.0.0.1:5000/api/v0/conversation_cleanup
{ "success": false, "message": "参数错误:必须指定操作模式", "error": "请提供以下参数组合之一: user_id | conversation_id/thread_id | clear_all_agent_data | cleanup_invalid_refs" } ```
这两个API的设计目标是:
两个API相互补充,形成完整的agent对话数据管理体系,满足从日常维护到紧急清理的各种需求。