import type { FileItem } from "../host-types"; import { createKeyedRequestCache } from "./keyed-request-cache"; export interface FileContentResult { content: string; path: string; encoding?: "base64" | "utf8"; } const cache = createKeyedRequestCache(30_000, 24); const keyFor = (sessionId: string, path: string) => `${sessionId}\0${path}`; export function getCachedFileContent( sessionId: string, path: string, loader: () => Promise, force = false, ): Promise { return cache.get(keyFor(sessionId, path), loader, { force }); } export function invalidateCachedFileContent( sessionId: string, path: string, ): void { cache.invalidate(keyFor(sessionId, path)); } export function shouldPrefetchFileContent( file: FileItem, maxPrefetchBytes: number, ): boolean { return ( file.type === "file" && typeof file.size === "number" && file.size <= maxPrefetchBytes ); }