Advanced context management with auto-compaction and dynamic context optimization for DeepSeek's 64k context window. Features intelligent compaction (merging, summarizing, extracting), query-aware relevance scoring, and hierarchical memory system with context archive. Logs optimization events to chat.
专为DeepSeek的64k上下文窗口优化的高级上下文管理。提供智能修剪、压缩和令牌优化,在保留重要信息的同时防止上下文溢出。
const pruner = createContextPruner({
contextLimit: 64000, // DeepSeek的限制
autoCompact: true, // 启用自动压缩
dynamicContext: true, // 启用基于相关性的动态上下文
strategies: [semantic, temporal, extractive, adaptive],
queryAwareCompaction: true, // 基于当前查询相关性进行压缩
});
await pruner.initialize();
// 使用自动压缩和动态上下文处理消息
const processed = await pruner.processMessages(messages, currentQuery);
// 获取上下文健康状态
const status = pruner.getStatus();
console.log(上下文健康度: ${status.health}, 相关性分数: ${status.relevanceScores});
// 需要时手动压缩
const compacted = await pruner.autoCompact(messages, currentQuery);
if (archiveResult.found) {
// 将相关片段添加到当前上下文
const archiveContext = archiveResult.snippets.join(\n\n);
// 在提示中使用archiveContext
console.log(找到 ${archiveResult.sources.length} 个相关来源);
console.log(从存档中检索了 ${archiveResult.totalTokens} 个令牌);
}
上下文存档提供RAM与存储的对比方式:
javascript
{
contextLimit: 64000, // DeepSeek的上下文窗口
autoCompact: true, // 启用自动压缩
compactThreshold: 0.75, // 使用率达到75%时开始压缩
aggressiveCompactThreshold: 0.9, // 使用率达到90%时激进压缩
dynamicContext: true, // 启用动态上下文管理
relevanceDecay: 0.95, // 每个时间步相关性衰减5%
minRelevanceScore: 0.3, // 保留的最低相关性
queryAwareCompaction: true, // 基于当前查询相关性进行压缩
strategies: [semantic, temporal, extractive, adaptive],
preserveRecent: 10, // 始终保留最后N条消息
preserveSystem: true, // 始终保留系统消息
minSimilarity: 0.85, // 语义相似度阈值
// 存档设置
enableArchive: true, // 启用分层记忆系统
archivePath: ./context-archive,
archiveSearchLimit: 10,
archiveMaxSize: 100 1024 1024, // 100MB
archiveIndexing: true,
// 聊天日志
logToChat: true, // 将优化事件记录到聊天
chatLogLevel: brief, // brief、detailed或none
chatLogFormat: 📊 {action}: {details}, // 聊天消息格式
// 性能
batchSize: 5, // 批量处理的消息数
maxCompactionRatio: 0.5, // 单次压缩最多50%
}
上下文优化器可以直接将事件记录到聊天:
javascript
// 示例聊天日志消息:
// 📊 上下文优化:压缩15条消息→8条(减少47%)
// 📊 存档搜索:找到3个相关片段(42%相似度)
// 📊 动态上下文:过滤12条低相关性消息
// 配置日志:
const pruner = createContextPruner({
logToChat: true,
chatLogLevel: brief, // 选项:brief、detailed、none
chatLogFormat: 📊 {action}: {details},
// 自定义日志处理器(可选)
onLog: (level, message, data) => {
if (level === info && data.action === compaction) {
// 发送到聊天
console.log(🧠 上下文优化:${message});
}
}
});
添加到您的Clawdbot配置:
yaml
skills:
context-pruner:
enabled: true
config:
contextLimit: 64000
autoPrune: true
修剪器将自动监控上下文使用情况,并应用适当的修剪策略以保持在DeepSeek的64k限制内。
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 context-optimizer-1776377907 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 context-optimizer-1776377907 技能
skillhub install context-optimizer-1776377907
文件大小: 32.3 KB | 发布时间: 2026-4-17 14:16