本文档描述了React Agent Checkpoint管理API的设计方案,提供checkpoint清理和统计功能,通过直接操作Redis实现,不依赖LangGraph工作流。
路由: POST /api/v0/checkpoint/direct/cleanup
功能: 清理checkpoint,保留最近N个
请求参数:
{
"keep_count": 10, // 可选,保留数量,默认使用配置值
"user_id": "wang1", // 可选,指定用户ID
"thread_id": "wang1:20250729235038043" // 可选,指定线程ID
}
参数逻辑:
user_id
:清理指定用户的所有threadthread_id
:清理指定的threaduser_id
和thread_id
同时存在:以thread_id
为准响应格式:
{
"code": 200,
"success": true,
"message": "Checkpoint清理完成",
"data": {
"operation_type": "cleanup_all|cleanup_user|cleanup_thread",
"target": "all|wang1|wang1:20250729235038043",
"keep_count": 10,
"total_processed": 15,
"total_deleted": 45,
"details": {
"wang1:20250729235038043": {
"original_count": 36,
"deleted_count": 26,
"remaining_count": 10,
"status": "success"
},
"wang1:20250731141657916": {
"original_count": 16,
"deleted_count": 6,
"remaining_count": 10,
"status": "success"
}
},
"timestamp": "2025-01-31T10:30:00"
}
}
路由: GET /api/v0/checkpoint/direct/stats
功能: 获取checkpoint统计信息
查询参数:
user_id
: 可选,指定用户ID调用方式:
# 获取全部统计信息
GET /api/v0/checkpoint/direct/stats
# 获取指定用户统计信息
GET /api/v0/checkpoint/direct/stats?user_id=wang1
响应格式:
全部统计信息:
{
"code": 200,
"success": true,
"message": "获取系统checkpoint统计成功",
"data": {
"operation_type": "system_stats",
"total_users": 2,
"total_threads": 4,
"total_checkpoints": 132,
"users": [
{
"user_id": "wang1",
"thread_count": 3,
"total_checkpoints": 116,
"threads": [
{
"thread_id": "wang1:20250729235038043",
"checkpoint_count": 36
},
{
"thread_id": "wang1:20250731141657916",
"checkpoint_count": 16
},
{
"thread_id": "wang1:20250801171843665",
"checkpoint_count": 64
}
]
},
{
"user_id": "wang2",
"thread_count": 1,
"total_checkpoints": 16,
"threads": [
{
"thread_id": "wang2:20250731141659949",
"checkpoint_count": 16
}
]
}
],
"timestamp": "2025-01-31T10:30:00"
}
}
指定用户统计信息:
{
"code": 200,
"success": true,
"message": "获取用户wang1统计成功",
"data": {
"operation_type": "user_stats",
"user_id": "wang1",
"thread_count": 3,
"total_checkpoints": 116,
"threads": [
{
"thread_id": "wang1:20250801171843665",
"checkpoint_count": 64
},
{
"thread_id": "wang1:20250729235038043",
"checkpoint_count": 36
},
{
"thread_id": "wang1:20250731141657916",
"checkpoint_count": 16
}
],
"timestamp": "2025-01-31T10:30:00"
}
}
使用common/result.py
的标准化响应格式:
from common.result import success_response, error_response, internal_error_response
# 成功响应
return jsonify(success_response(
response_text="Checkpoint清理完成",
data={
"operation_type": "cleanup_all",
"total_deleted": 45,
# ... 其他数据
}
))
# 错误响应
return jsonify(internal_error_response(
response_text="Redis连接失败"
))
参考react_agent/enhanced_redis_api.py
的模式:
# 直接创建Redis连接
redis_client = redis.Redis(
host=config.REDIS_HOST,
port=config.REDIS_PORT,
db=config.REDIS_DB,
password=config.REDIS_PASSWORD,
decode_responses=True
)
基于现有系统,checkpoint key格式:
checkpoint:user_id:timestamp:checkpoint_id
示例:
checkpoint:wang1:20250729235038043:01936451-dd24-641c-8005-c07e5896ad38
checkpoint:wang1:20250729235038043:01936451-dd29-624b-8006-fc1f3a83e4f5
checkpoint:wang2:20250731141659949:01936462-72a1-6e5c-8009-378fd98058aa
扫描Keys:
# 扫描所有checkpoint
pattern = "checkpoint:*"
# 扫描指定用户
pattern = f"checkpoint:{user_id}:*"
# 扫描指定thread
pattern = f"checkpoint:{thread_id}:*"
keys = []
cursor = 0
while True:
cursor, batch = redis_client.scan(cursor=cursor, match=pattern, count=1000)
keys.extend(batch)
if cursor == 0:
break
数据分组:
# 按thread_id分组
thread_groups = {}
for key in keys:
parts = key.split(':')
if len(parts) >= 3:
user_id = parts[1]
timestamp = parts[2]
thread_id = f"{user_id}:{timestamp}"
if thread_id not in thread_groups:
thread_groups[thread_id] = []
thread_groups[thread_id].append(key)
批量清理操作:
# 保留最近N个checkpoint,使用Redis批量删除
for thread_id, keys in thread_groups.items():
if len(keys) > keep_count:
# 按key排序(key包含timestamp,天然有序)
keys.sort()
# 删除旧的keys
keys_to_delete = keys[:-keep_count]
# 使用Redis Pipeline批量删除,提升性能
if keys_to_delete:
pipeline = redis_client.pipeline()
for key in keys_to_delete:
pipeline.delete(key)
pipeline.execute() # 批量执行删除命令
在react_agent/config.py
中添加:
# --- Checkpoint管理配置 ---
CHECKPOINT_KEEP_COUNT = 10 # 每个thread保留的checkpoint数量(API默认值)
unified_api.py
/api/v0/checkpoint/direct/
react_agent.config
、redis
、common.result
使用common/result.py
的标准化错误响应:
{
"code": 500,
"success": false,
"message": "请求处理失败",
"data": {
"response": "具体错误信息",
"error_type": "REDIS_CONNECTION_ERROR|INVALID_THREAD_ID|USER_NOT_FOUND|DELETE_FAILED",
"timestamp": "2025-01-31T10:30:00"
}
}
# 1. 清理所有thread,每个保留10个checkpoint
curl -X POST http://localhost:8084/api/v0/checkpoint/direct/cleanup \
-H "Content-Type: application/json" \
-d '{"keep_count": 10}'
# 2. 清理用户wang1的所有thread,每个保留5个
curl -X POST http://localhost:8084/api/v0/checkpoint/direct/cleanup \
-H "Content-Type: application/json" \
-d '{"user_id": "wang1", "keep_count": 5}'
# 3. 清理指定thread,保留8个
curl -X POST http://localhost:8084/api/v0/checkpoint/direct/cleanup \
-H "Content-Type: application/json" \
-d '{"thread_id": "wang1:20250729235038043", "keep_count": 8}'
# 4. 获取全部统计信息
curl http://localhost:8084/api/v0/checkpoint/direct/stats
# 5. 获取用户wang1的统计信息
curl "http://localhost:8084/api/v0/checkpoint/direct/stats?user_id=wang1"
scan
命令避免阻塞Redis本文档描述了checkpoint管理API的完整设计方案,为实际开发提供详细的技术规范。