import type { SessionManager } from "@earendil-works/pi-coding-agent"; export interface TrackedFile { path: string; readAt: number; } const trackedFiles = new Map(); export function addFile(path: string, readAt: number): void { trackedFiles.set(path, { path, readAt }); } export function clearFiles(): void { trackedFiles.clear(); } export function getFiles(): Map { return trackedFiles; } export function getFileCount(): number { return trackedFiles.size; } export function rebuildFromSession(sessionManager: Pick): void { trackedFiles.clear(); for (const entry of sessionManager.getBranch()) { if (entry.type === "custom" && entry.customType === "reader-file") { const data = entry.data as { path: string; readAt: number } | undefined; if (data?.path) { trackedFiles.set(data.path, { path: data.path, readAt: data.readAt ?? 0 }); } continue; } if (entry.type !== "message" || entry.message.role !== "assistant") continue; const msg = entry.message as { content?: Array<{ type: string; name?: string; arguments?: Record }> }; if (!Array.isArray(msg.content)) continue; for (const block of msg.content) { if (block.type !== "toolCall" || block.name !== "read") continue; const path = block.arguments?.path; if (typeof path === "string" && path.length > 0) { const readAt = entry.timestamp ? new Date(entry.timestamp).getTime() : 0; trackedFiles.set(path, { path, readAt }); } } } }