* 获取格式化的上下文用于 Prompt 构建
(filterOptions?: FilterOptions)
| 264 | * 获取格式化的上下文用于 Prompt 构建 |
| 265 | */ |
| 266 | async getFormattedContext(filterOptions?: FilterOptions): Promise<{ |
| 267 | context: ContextData; |
| 268 | compressed?: CompressedContext; |
| 269 | tokenCount: number; |
| 270 | }> { |
| 271 | const contextData = this.memory.getContext(); |
| 272 | if (!contextData) { |
| 273 | throw new Error('没有可用的上下文数据'); |
| 274 | } |
| 275 | |
| 276 | // 应用过滤器 |
| 277 | const filteredContext = this.filter.filter(contextData, filterOptions); |
| 278 | |
| 279 | // 检查是否需要压缩 |
| 280 | const shouldCompress = this.shouldCompress(filteredContext); |
| 281 | let compressed: CompressedContext | undefined; |
| 282 | |
| 283 | if (shouldCompress) { |
| 284 | // 尝试从缓存获取压缩结果 |
| 285 | const contextHash = this.hashContext(filteredContext); |
| 286 | compressed = this.cache.getCompressedContext(contextHash); |
| 287 | |
| 288 | if (!compressed) { |
| 289 | compressed = await this.compressor.compress(filteredContext); |
| 290 | this.cache.cacheCompressedContext(contextHash, compressed); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return { |
| 295 | context: filteredContext, |
| 296 | compressed, |
| 297 | tokenCount: compressed ? compressed.tokenCount : filteredContext.metadata.totalTokens, |
| 298 | }; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * 搜索历史会话 |
no test coverage detected