logancyang_obsidian-copilot/src/cache/autocompleteCache.ts
Logan Yang b35290568a Squashed 2.9.0 preview 250327
Update preview version

Merge latest from Composer

Support creating new files

Update version to 2.9.0 preview

Add minor fixes

Update autocomplete call and prefix logic

Merge Project mode into 2.9.0-preview

Squashed commits from:

commit a762629dee
Author: Logan Yang <logancyang@gmail.com>
Date:   Tue Mar 25 16:29:31 2025 -0700

    Add projectEnabled flag to CustomModel interface

To

commit 1694357823
Author: wyh <emt934841028@gmail.com>
Date:   Wed Feb 19 14:45:22 2025 +0800

    feat: Support project-based feature.

Fix package vulnerabilities

Prerelease 250325

Update label for PROJECT_CHAIN to "Plus Projects (alpha)" in BasicSettings component
2025-03-27 15:42:32 -07:00

64 lines
1.6 KiB
TypeScript

import { logInfo } from "@/logger";
interface CacheEntry {
completion: string;
timestamp: number;
}
export class AutocompleteCache {
private static instance: AutocompleteCache;
private cache: Map<string, CacheEntry>;
private readonly maxSize: number;
private readonly ttlMs: number;
private constructor() {
this.cache = new Map();
this.maxSize = 100; // Cache up to 100 completions
this.ttlMs = 5 * 60 * 1000; // 5 minutes TTL
}
static getInstance(): AutocompleteCache {
if (!AutocompleteCache.instance) {
AutocompleteCache.instance = new AutocompleteCache();
}
return AutocompleteCache.instance;
}
get(key: string): string | undefined {
const entry = this.cache.get(key);
if (!entry) return undefined;
// Check if entry has expired
if (Date.now() - entry.timestamp > this.ttlMs) {
this.cache.delete(key);
return undefined;
}
return entry.completion;
}
set(key: string, completion: string): void {
// If cache is full, remove oldest entry
if (this.cache.size >= this.maxSize) {
const oldestKey = this.cache.keys().next().value;
this.cache.delete(oldestKey);
}
this.cache.set(key, {
completion,
timestamp: Date.now(),
});
logInfo("Cached autocomplete suggestion for key:", key);
}
clear(): void {
this.cache.clear();
logInfo("Cleared autocomplete cache");
}
generateKey(prefix: string): string {
// Use last N characters as key to keep context relevant
const keyLength = 100;
return prefix.length <= keyLength ? prefix : prefix.slice(prefix.length - keyLength);
}
}