MCPcopy Create free account
hub / github.com/echoVic/blade-code / getFormattedContext

Method getFormattedContext

src/context/ContextManager.ts:266–299  ·  view source on GitHub ↗

* 获取格式化的上下文用于 Prompt 构建

(filterOptions?: FilterOptions)

Source from the content-addressed store, hash-verified

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 * 搜索历史会话

Callers 1

exampleFunction · 0.45

Calls 7

shouldCompressMethod · 0.95
hashContextMethod · 0.95
getContextMethod · 0.80
getCompressedContextMethod · 0.80
compressMethod · 0.80
filterMethod · 0.45

Tested by

no test coverage detected