feat: surface cached token counts in /status output (openclaw#21248) thanks @vishaltandale00
Verified: - pnpm build - pnpm check - pnpm test:macmini Co-authored-by: vishaltandale00 <9222298+vishaltandale00@users.noreply.github.com> Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
This commit is contained in:
@@ -36,6 +36,35 @@ const WORKSPACE_STATE_VERSION = 1;
|
||||
const workspaceTemplateCache = new Map<string, Promise<string>>();
|
||||
let gitAvailabilityPromise: Promise<boolean> | null = null;
|
||||
|
||||
// File content cache with mtime invalidation to avoid redundant reads
|
||||
const workspaceFileCache = new Map<string, { content: string; mtimeMs: number }>();
|
||||
|
||||
/**
|
||||
* Read file with caching based on mtime. Returns cached content if file
|
||||
* hasn't changed, otherwise reads from disk and updates cache.
|
||||
*/
|
||||
async function readFileWithCache(filePath: string): Promise<string> {
|
||||
try {
|
||||
const stats = await fs.stat(filePath);
|
||||
const mtimeMs = stats.mtimeMs;
|
||||
const cached = workspaceFileCache.get(filePath);
|
||||
|
||||
// Return cached content if mtime matches
|
||||
if (cached && cached.mtimeMs === mtimeMs) {
|
||||
return cached.content;
|
||||
}
|
||||
|
||||
// Read from disk and update cache
|
||||
const content = await fs.readFile(filePath, "utf-8");
|
||||
workspaceFileCache.set(filePath, { content, mtimeMs });
|
||||
return content;
|
||||
} catch (error) {
|
||||
// Remove from cache if file doesn't exist or is unreadable
|
||||
workspaceFileCache.delete(filePath);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function stripFrontMatter(content: string): string {
|
||||
if (!content.startsWith("---")) {
|
||||
return content;
|
||||
@@ -451,7 +480,7 @@ export async function loadWorkspaceBootstrapFiles(dir: string): Promise<Workspac
|
||||
const result: WorkspaceBootstrapFile[] = [];
|
||||
for (const entry of entries) {
|
||||
try {
|
||||
const content = await fs.readFile(entry.filePath, "utf-8");
|
||||
const content = await readFileWithCache(entry.filePath);
|
||||
result.push({
|
||||
name: entry.name,
|
||||
path: entry.filePath,
|
||||
@@ -531,7 +560,7 @@ export async function loadExtraBootstrapFiles(
|
||||
if (!VALID_BOOTSTRAP_NAMES.has(baseName)) {
|
||||
continue;
|
||||
}
|
||||
const content = await fs.readFile(realFilePath, "utf-8");
|
||||
const content = await readFileWithCache(realFilePath);
|
||||
result.push({
|
||||
name: baseName as WorkspaceBootstrapFileName,
|
||||
path: filePath,
|
||||
|
||||
Reference in New Issue
Block a user