import fs from 'node:fs'; import path from 'node:path'; /** * Parsed session data from a JSONL file. */ export interface ParsedSession { id: string; project: string; cwd: string; startedAt: string; endedAt: string | null; messages: ParsedMessage[]; } /** * A single parsed message from a session. */ export interface ParsedMessage { id: string; role: 'user' | 'assistant' | 'system'; content: string; timestamp: string; toolCalls?: string[]; } /** * Raw JSONL entry types. */ interface JsonlEntry { type: string; id?: string; parentId?: string | null; timestamp?: string; cwd?: string; message?: { role?: string; content?: unknown; timestamp?: number; }; customType?: string; [key: string]: unknown; } /** * Extract text content from a message's content array. */ function extractTextContent(content: unknown): string { if (typeof content === 'string') return content; if (!Array.isArray(content)) return ''; const parts: string[] = []; for (const block of content) { if (!block || typeof block !== 'object') continue; const b = block as Record; switch (b.type) { case 'text': if (typeof b.text === 'string') parts.push(b.text); break; case 'thinking': // Skip thinking blocks — they're internal reasoning break; case 'tool_use': // Skip tool_use blocks — we track tool calls separately break; case 'tool_result': // Tool results commonly contain full file or command output. They are // ephemeral and can be very large; tool names are indexed separately. break; } } return parts.join('\n').trim(); } /** * Extract tool call names from a message's content array. */ function extractToolCalls(content: unknown): string[] | undefined { if (!Array.isArray(content)) return undefined; const toolNames: string[] = []; for (const block of content) { if (!block || typeof block !== 'object') continue; const b = block as Record; if ((b.type === 'tool_use' || b.type === 'toolCall') && typeof b.name === 'string') { toolNames.push(b.name); } } return toolNames.length > 0 ? toolNames : undefined; } /** Remove a leading UTF-8 byte-order mark (U+FEFF) from decoded text. */ function stripLeadingBom(text: string): string { return text.startsWith('\uFEFF') ? text.slice(1) : text; } /** * Read a file as UTF-8 with a leading byte-order mark removed. * * Decoding with `utf-8` keeps a BOM as a leading U+FEFF character, which makes * the raw first line fail `JSON.parse`. For a session JSONL that means the * session header is skipped, the file parses to `null`, and the indexer reports * it as "Failed to parse" even though its content is intact. */ function readUtf8WithoutBom(filePath: string): string { return stripLeadingBom(fs.readFileSync(filePath, 'utf-8')); } /** * Parse a Pi session JSONL file. * * @param filePath — Path to the .jsonl file * @returns Parsed session data, or null if the file is invalid */ export function parseSessionFile(filePath: string): ParsedSession | null { const content = readUtf8WithoutBom(filePath); const lines = content.split('\n').filter(line => line.trim()); if (lines.length === 0) return null; let sessionId: string | null = null; let sessionCwd: string | null = null; let sessionTimestamp: string | null = null; const messages: ParsedMessage[] = []; for (const line of lines) { let entry: JsonlEntry; try { entry = JSON.parse(line); } catch { continue; // Skip malformed lines } switch (entry.type) { case 'session': sessionId = entry.id ?? null; sessionCwd = entry.cwd ?? null; sessionTimestamp = entry.timestamp ?? null; break; case 'message': { if (!entry.message || !entry.id || !entry.timestamp) break; const role = entry.message.role; if (role !== 'user' && role !== 'assistant' && role !== 'system') break; const textContent = extractTextContent(entry.message.content); if (!textContent) break; // Skip empty messages const toolCalls = role === 'assistant' ? extractToolCalls(entry.message.content) : undefined; messages.push({ id: entry.id, role, content: textContent, timestamp: entry.timestamp, toolCalls, }); break; } // Skip other entry types (model_change, thinking_level_change, custom, etc.) } } if (!sessionId || !sessionCwd || !sessionTimestamp) return null; // Decode project name from cwd-encoded directory name // The directory is named like "--Users-chandrateja-Documents-pi-hermes-memory--" // We extract the last segment as the project name const project = sessionCwd.split('/').pop() ?? sessionCwd; return { id: sessionId, project, cwd: sessionCwd, startedAt: sessionTimestamp, endedAt: null, // We don't know when it ended from the JSONL messages, }; } /** * Get all session JSONL files for a project (or all projects). * * @param sessionsDir — Path to ~/.selesai/agent/sessions/ * @param projectDir — Optional: specific project directory name (e.g., "--Users-...--") * @returns Array of file paths */ /** * Cheap content sniff: does this file look like a Pi session JSONL? * * The format contract (see Pi docs/session-format.md) requires the first line * to be a `{"type":"session",...}` header entry. The sessions directory is a * shared namespace — extensions (e.g. pi-subagents' `subagent-artifacts/`) * drop non-session JSONL artifacts there, and indexing them used to surface a * "Failed to parse" error per file on every startup backfill. Sniffing the * first line (bounded 4 KB read, never the whole file) lets callers skip such * artifacts silently. * * Unreadable files return true so the existing parse path reports them as * real errors instead of hiding genuinely broken session files. */ export function isSessionFile(filePath: string): boolean { const SNIFF_BYTES = 4096; let fd: number | undefined; try { fd = fs.openSync(filePath, 'r'); const buf = Buffer.alloc(SNIFF_BYTES); const bytesRead = fs.readSync(fd, buf, 0, SNIFF_BYTES, 0); if (bytesRead === 0) return false; const text = stripLeadingBom(buf.subarray(0, bytesRead).toString('utf-8')); const newline = text.indexOf('\n'); const firstLine = (newline === -1 ? text : text.slice(0, newline)).trim(); if (!firstLine) return false; try { const entry = JSON.parse(firstLine); return entry !== null && typeof entry === 'object' && (entry as { type?: unknown }).type === 'session'; } catch { // Unparseable first line. If no newline was observed the line may simply // run past the sniff window (a truncated fragment) — fail open and let // the full parser decide, so a real session with a big header is never // silently dropped. A complete line that fails to parse is foreign. return newline === -1; } } catch { return true; } finally { if (fd !== undefined) { try { fs.closeSync(fd); } catch { /* ignore */ } } } } /** Match a first-level directory name against exact names or `*` globs. */ function matchesExcludePattern(name: string, patterns: string[]): boolean { return patterns.some((pattern) => { const normalized = pattern.trim(); if (!normalized.includes('*')) return normalized === name; const escaped = normalized.split('*').map((part) => part.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('.*'); return new RegExp(`^${escaped}$`).test(name); }); } export function getSessionFiles( sessionsDir: string, projectDir?: string, excludeDirs: string[] = [], sessionOnly = false, ): string[] { const collect = (dir: string, recursive: boolean): string[] => { if (!fs.existsSync(dir)) return []; const out: string[] = []; for (const entry of fs.readdirSync(dir)) { const entryPath = path.join(dir, entry); const stat = fs.statSync(entryPath); if (stat.isDirectory()) { if (recursive && matchesExcludePattern(entry, excludeDirs)) { // User-configured exclusion (sessionIndexExclude): skip the whole dir. continue; } if (recursive) { // Scan .jsonl files inside project subdirectories for (const f of fs.readdirSync(entryPath)) { if (f.endsWith('.jsonl')) { out.push(path.join(entryPath, f)); } } } } else if (stat.isFile() && entry.endsWith('.jsonl')) { out.push(entryPath); } } return out; }; const files = projectDir ? collect(path.join(sessionsDir, projectDir), false) : collect(sessionsDir, true); return sessionOnly ? files.filter(isSessionFile) : files; } /** * Decode a project directory name to a human-readable project name. * "--Users-chandrateja-Documents-pi-hermes-memory--" → "pi-hermes-memory" */ export function decodeProjectDir(dirName: string): string { // Remove leading/trailing dashes const cleaned = dirName.replace(/^-+|-+$/g, ''); // Split by dash and take the last segment (project name) const segments = cleaned.split('-'); return segments[segments.length - 1] ?? cleaned; }