UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

136 lines • 5.18 kB
import { open } from "fs/promises"; import { StringDecoder } from "string_decoder"; import { parseRepositoryIdentity, REPOSITORY_IDENTITY_ENTRY_TYPE, } from "./repository-identity.js"; import { parseEntryLine, sessionInfoName, visibleMessage } from "./session-record.js"; function newAccumulator() { return { header: null, name: undefined, firstUserMessage: "", messageCount: 0, lastActivityTime: undefined, repositoryIdentity: undefined, texts: [], }; } /** * Fold one JSONL line into the accumulator. * * Returns false when the file's first parsable record is not a session header, * which means the file is not a session and the caller stops reading. */ function accumulateLine(accumulator, line) { const entry = parseEntryLine(line); if (!entry) return true; if (!accumulator.header) { if (entry.type !== "session" || typeof entry.id !== "string") return false; accumulator.header = entry; return true; } if (entry.type === "custom" && entry.customType === REPOSITORY_IDENTITY_ENTRY_TYPE) { accumulator.repositoryIdentity = parseRepositoryIdentity(entry.data) ?? accumulator.repositoryIdentity; return true; } const infoName = sessionInfoName(entry); if (infoName !== null) { accumulator.name = infoName; return true; } if (entry.type !== "message") return true; accumulator.messageCount++; const visible = visibleMessage(entry); if (!visible) return true; if (typeof visible.time === "number") { accumulator.lastActivityTime = Math.max(accumulator.lastActivityTime ?? 0, visible.time); } if (!visible.text) return true; accumulator.texts.push(visible.text); if (!accumulator.firstUserMessage && visible.role === "user") { accumulator.firstUserMessage = visible.text; } return true; } const LINE_READ_BUFFER_SIZE = 1024 * 1024; /** * Read a file line by line through one reused 1 MiB buffer, handing each line * (LF-split, a trailing CR dropped) to `onLine` until it returns false. * * This replaces `readline`, whose per-line async iteration dominated summary * cost (senpi#2087). Only the unfinished tail of a chunk is carried between * reads, and the next newline search resumes where the carried tail ends, so a * record larger than the buffer is not rescanned per chunk. Read errors throw. */ export async function readFileLines(filePath, onLine) { const handle = await open(filePath, "r"); try { const decoder = new StringDecoder("utf8"); const buffer = Buffer.allocUnsafe(LINE_READ_BUFFER_SIZE); let pending = ""; let bytes = 0; let lastByte = -1; const emit = (line) => onLine(line.endsWith("\r") ? line.slice(0, -1) : line); while (true) { const { bytesRead } = await handle.read(buffer, 0, buffer.length, null); if (bytesRead === 0) break; bytes += bytesRead; lastByte = buffer[bytesRead - 1] ?? -1; const searchFrom = pending.length; pending += decoder.write(buffer.subarray(0, bytesRead)); let lineStart = 0; let newlineIndex = pending.indexOf("\n", searchFrom); while (newlineIndex !== -1) { if (!emit(pending.slice(lineStart, newlineIndex))) return { completed: false, bytes, endsWithNewline: false }; lineStart = newlineIndex + 1; newlineIndex = pending.indexOf("\n", lineStart); } pending = pending.slice(lineStart); } pending += decoder.end(); if (pending && !emit(pending)) return { completed: false, bytes, endsWithNewline: false }; return { completed: true, bytes, endsWithNewline: lastByte === 0x0a }; } finally { await handle.close(); } } /** * Read one session file line by line and fold it into an exact summary. * * Memory stays bounded by one read buffer plus the transcript text the summary * contract requires; no full-file string is ever materialized. A file whose * first record is not a session header, or that cannot be read, yields null. */ export async function readSessionSummary(filePath) { const accumulator = newAccumulator(); try { const read = await readFileLines(filePath, (line) => accumulateLine(accumulator, line)); if (!read.completed) return null; } catch (error) { if (error instanceof Error) return null; throw error; } const header = accumulator.header; if (!header) return null; return { header, name: accumulator.name, firstUserMessage: accumulator.firstUserMessage, messageCount: accumulator.messageCount, lastActivityTime: accumulator.lastActivityTime, allMessagesText: accumulator.texts.join(" "), ...(accumulator.repositoryIdentity ? { repositoryIdentity: accumulator.repositoryIdentity } : {}), }; } //# sourceMappingURL=session-summary.js.map