Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | import * as vscode from 'vscode';
import * as path from 'path';
import { IMemoryManager } from '../interfaces/IMemoryManager';
import { Context, SearchResult } from '../types/Memory';
export class MemoryManager implements IMemoryManager {
private storage: Map<string, any> = new Map();
private storagePath: string;
constructor(private globalStorageUri: vscode.Uri) {
this.storagePath = globalStorageUri.fsPath;
}
async store(key: string, value: any): Promise<void> {
this.storage.set(key, {
value,
timestamp: Date.now()
});
}
async retrieve(key: string): Promise<any> {
const item = this.storage.get(key);
return item?.value;
}
async delete(key: string): Promise<void> {
this.storage.delete(key);
}
async exists(key: string): Promise<boolean> {
return this.storage.has(key);
}
async searchSemantic(query: string, limit: number = 10): Promise<SearchResult[]> {
// Simple keyword search for now
const results: SearchResult[] = [];
for (const [key, item] of this.storage.entries()) {
const content = JSON.stringify(item.value).toLowerCase();
if (content.includes(query.toLowerCase())) {
results.push({
id: key,
content: item.value,
score: 1.0, // Simple match
source: 'memory',
metadata: { timestamp: item.timestamp }
});
}
}
return results.slice(0, limit);
}
async getRelevantContext(query: string, maxTokens: number): Promise<Context[]> {
const searchResults = await this.searchSemantic(query);
const contexts: Context[] = [];
for (const result of searchResults) {
contexts.push({
id: result.id,
content: JSON.stringify(result.content),
source: result.source,
relevance: result.score,
tokens: this.estimateTokens(JSON.stringify(result.content)),
timestamp: result.metadata?.timestamp || Date.now()
});
}
return this.pruneContext(contexts, maxTokens);
}
pruneContext(contexts: Context[], maxTokens: number): Context[] {
// Sort by relevance
contexts.sort((a, b) => b.relevance - a.relevance);
const pruned: Context[] = [];
let totalTokens = 0;
for (const context of contexts) {
if (totalTokens + context.tokens <= maxTokens) {
pruned.push(context);
totalTokens += context.tokens;
}
}
return pruned;
}
getStoragePath(): string {
return this.storagePath;
}
async getStorageSize(): Promise<number> {
let size = 0;
for (const [key, value] of this.storage.entries()) {
size += JSON.stringify({ key, value }).length;
}
return size;
}
async clearStorage(): Promise<void> {
this.storage.clear();
}
dispose(): void {
this.storage.clear();
}
private estimateTokens(text: string): number {
// Simple estimation: ~4 characters per token
return Math.ceil(text.length / 4);
}
} |