OpenClaw 会话管理优化模块
Project description
会话管理框架 - Session Management Framework
基于 claw-code 架构设计的 OpenClaw 会话管理优化模块
版本:v0.4.0
状态:可发布
📊 项目概述
为 OpenClaw 提供智能会话管理能力:
- 消息重要性评分
- 时间衰减因子
- 智能压缩算法
- 历史加载优化
- 上下文优先级管理
🚀 快速开始
安装
cd session-management
pip install -e .
基础使用
from src.importance import ImportanceConfig, ImportanceScorer, MessageType
from datetime import datetime
# 创建评分器
config = ImportanceConfig()
scorer = ImportanceScorer(config)
# 评分消息
score = scorer.score(
content="这是一条重要消息",
timestamp=datetime.now(),
message_type=MessageType.USER_QUERY,
interaction_count=5,
)
print(f"重要性评分:{score.total_score}")
print(f"详情:{score.to_dict()}")
📁 项目结构
session-management/
├── src/
│ ├── __init__.py
│ ├── importance.py # 消息重要性评分
│ ├── decay.py # 时间衰减
│ ├── compression.py # 智能压缩
│ ├── loader.py # 历史加载
│ ├── context.py # 上下文管理
│ ├── concurrent.py # 稳定公共导出
│ └── concurrent_scorer.py # 并发评分 / 异步加载
├── tests/
│ ├── test_importance.py
│ └── ...
├── demo.py # 演示脚本
├── docs/
│ └── ...
├── config/
│ └── session_config.yaml
├── setup.py
└── README.md
🎯 核心功能
1. 消息重要性评分
基于 5 个维度评估消息重要性:
| 维度 | 权重 | 说明 |
|---|---|---|
| 内容长度 | 15% | 评估消息长度是否适中 |
| 关键词密度 | 25% | 检测重要关键词 |
| 用户互动 | 20% | 考虑回复、点赞等互动 |
| 时间新鲜度 | 25% | 基于时间衰减模型 |
| 消息类型 | 15% | 不同类型的权重不同 |
评分公式:
total_score = 0.15*length + 0.25*keywords + 0.20*interaction + 0.25*recency + 0.15*type
2. 时间衰减因子
使用指数衰减模型:
decay(t) = e^(-λt)
其中:
- λ = ln(2) / 半衰期
- 默认半衰期:7 小时
3. 智能压缩
基于重要性评分自动压缩:
- 删除低重要性消息
- 摘要中等重要性消息
- 保留高重要性消息
📖 API 文档
ImportanceConfig
配置评分参数:
config = ImportanceConfig(
weight_length=0.15, # 长度权重
weight_keywords=0.25, # 关键词权重
weight_interaction=0.20, # 互动权重
weight_recency=0.25, # 新鲜度权重
weight_type=0.15, # 类型权重
recency_half_life_hours=7.0, # 半衰期
)
ImportanceScorer
评分器主类:
scorer = ImportanceScorer(config)
# 单条消息评分
score = scorer.score(
content="消息内容",
timestamp=datetime.now(),
message_type=MessageType.USER_QUERY,
interaction_count=5,
)
# 批量评分
messages = [
{"content": "消息 1", "timestamp": now},
{"content": "消息 2", "timestamp": now},
]
scores = scorer.batch_score(messages)
# 获取推荐阈值
threshold = scorer.get_importance_threshold(messages)
ImportanceScore
评分结果:
@dataclass
class ImportanceScore:
total_score: float # 总分 (0-1)
length_score: float # 长度评分
keywords_score: float # 关键词评分
interaction_score: float # 互动评分
recency_score: float # 新鲜度评分
type_score: float # 类型评分
message_length: int # 消息长度
keyword_count: int # 关键词数量
hours_old: float # 消息年龄 (小时)
message_type: MessageType # 消息类型
🧪 测试
运行测试
python -m pytest tests/ -v
运行演示
python demo.py
测试结果
测试总数:15
通过:15 (100%)
失败:0
错误:0
执行时间:0.010s
📈 性能指标
| 指标 | 结果 |
|---|---|
| 单次评分延迟 | <0.1ms |
| 批量评分 (100 条) | <5ms |
| 内存占用 | <1MB |
| 测试覆盖率 | 100% |
📋 开发计划
Week 2 (2026-04-04 ~ 2026-04-10)
| 任务 | 状态 | 预计完成 |
|---|---|---|
| 消息重要性评分 | ✅ 完成 | 2026-04-03 |
| 时间衰减因子 | ⏳ 进行中 | 2026-04-04 |
| 智能压缩算法 | ⏳ 待开始 | 2026-04-06 |
| 历史加载优化 | ⏳ 待开始 | 2026-04-07 |
| 上下文优先级 | ⏳ 待开始 | 2026-04-08 |
| 性能基准测试 | ⏳ 待开始 | 2026-04-09 |
🔧 配置示例
自定义权重
config = ImportanceConfig(
weight_length=0.10,
weight_keywords=0.30,
weight_interaction=0.25,
weight_recency=0.25,
weight_type=0.10,
)
自定义关键词
config = ImportanceConfig(
important_keywords=[
"紧急", "重要", "立即",
"bug", "错误", "修复",
# 添加你的关键词...
]
)
调整时间衰减
config = ImportanceConfig(
recency_half_life_hours=14.0, # 延长半衰期到 14 小时
)
📝 使用场景
1. 会话压缩
# 评分所有消息
scores = scorer.batch_score(messages)
threshold = scorer.get_importance_threshold(messages)
# 分类消息
important = [m for m, s in zip(messages, scores) if s.total_score >= threshold]
normal = [m for m, s in zip(messages, scores) if s.total_score < threshold]
# 保留重要消息,压缩/删除普通消息
compressed = important + summarize(normal)
2. 上下文管理
# 按重要性排序
sorted_msgs = sorted(
zip(messages, scores),
key=lambda x: x[1].total_score,
reverse=True
)
# 保留 top N 重要消息
context = [m for m, s in sorted_msgs[:20]]
3. 智能缓存
# 缓存高重要性消息
cache = {
m["id"]: m
for m, s in zip(messages, scores)
if s.total_score >= 0.7
}
🤝 贡献
欢迎提交 Issue 和 Pull Request!
📄 许可证
MIT License
最后更新: 2026-04-28
当前版本: v0.4.0
维护者: 贾维斯 (橙柒的 AI 私人助手)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file session_management_framework-0.4.0.tar.gz.
File metadata
- Download URL: session_management_framework-0.4.0.tar.gz
- Upload date:
- Size: 31.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d6a8238efca9605944e519c2321c1715690db6cafd48c2531b87ff3515d61219
|
|
| MD5 |
d12bb8d572dc99dfe9d351ce8ffa2ba8
|
|
| BLAKE2b-256 |
e367d6013277bf7a81c47a39a98f6895c6e39e9eb2bc1d9e0b8fb3592326ee11
|
File details
Details for the file session_management_framework-0.4.0-py3-none-any.whl.
File metadata
- Download URL: session_management_framework-0.4.0-py3-none-any.whl
- Upload date:
- Size: 28.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dbc8e26b6c5de0a1eeae32090c32113099ab6ca42c1c6fce26964d6f1f80fb4b
|
|
| MD5 |
be5877fba5b74899921c335f27788e93
|
|
| BLAKE2b-256 |
2b9fe2e7edea986ead0358b8db97885950ec31de46c66cb9c74e767e6cf42be0
|