|
|
|
|
@@ -171,9 +171,13 @@ export function buildSessionContext(entries: SessionEntry[]): SessionContext {
|
|
|
|
|
return { messages, thinkingLevel, model };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getSessionDirectory(cwd: string, agentDir: string): string {
|
|
|
|
|
/**
|
|
|
|
|
* Compute the default session directory for a cwd.
|
|
|
|
|
* Encodes cwd into a safe directory name under ~/.pi/agent/sessions/.
|
|
|
|
|
*/
|
|
|
|
|
function getDefaultSessionDir(cwd: string): string {
|
|
|
|
|
const safePath = `--${cwd.replace(/^[/\\]/, "").replace(/[/\\:]/g, "-")}--`;
|
|
|
|
|
const sessionDir = join(agentDir, "sessions", safePath);
|
|
|
|
|
const sessionDir = join(getDefaultAgentDir(), "sessions", safePath);
|
|
|
|
|
if (!existsSync(sessionDir)) {
|
|
|
|
|
mkdirSync(sessionDir, { recursive: true });
|
|
|
|
|
}
|
|
|
|
|
@@ -225,9 +229,12 @@ export class SessionManager {
|
|
|
|
|
private flushed: boolean = false;
|
|
|
|
|
private inMemoryEntries: SessionEntry[] = [];
|
|
|
|
|
|
|
|
|
|
private constructor(cwd: string, agentDir: string, sessionFile: string | null, persist: boolean) {
|
|
|
|
|
private constructor(cwd: string, sessionDir: string, sessionFile: string | null, persist: boolean) {
|
|
|
|
|
this.cwd = cwd;
|
|
|
|
|
this.sessionDir = getSessionDirectory(cwd, agentDir);
|
|
|
|
|
this.sessionDir = sessionDir;
|
|
|
|
|
if (persist && sessionDir && !existsSync(sessionDir)) {
|
|
|
|
|
mkdirSync(sessionDir, { recursive: true });
|
|
|
|
|
}
|
|
|
|
|
this.persist = persist;
|
|
|
|
|
|
|
|
|
|
if (sessionFile) {
|
|
|
|
|
@@ -235,7 +242,7 @@ export class SessionManager {
|
|
|
|
|
} else {
|
|
|
|
|
this.sessionId = uuidv4();
|
|
|
|
|
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
|
|
|
|
|
const sessionFile = join(this.sessionDir, `${timestamp}_${this.sessionId}.jsonl`);
|
|
|
|
|
const sessionFile = join(this.getSessionDir(), `${timestamp}_${this.sessionId}.jsonl`);
|
|
|
|
|
this.setSessionFile(sessionFile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -270,6 +277,10 @@ export class SessionManager {
|
|
|
|
|
return this.cwd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getSessionDir(): string {
|
|
|
|
|
return this.sessionDir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getSessionId(): string {
|
|
|
|
|
return this.sessionId;
|
|
|
|
|
}
|
|
|
|
|
@@ -282,7 +293,7 @@ export class SessionManager {
|
|
|
|
|
this.sessionId = uuidv4();
|
|
|
|
|
this.flushed = false;
|
|
|
|
|
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
|
|
|
|
|
this.sessionFile = join(this.sessionDir, `${timestamp}_${this.sessionId}.jsonl`);
|
|
|
|
|
this.sessionFile = join(this.getSessionDir(), `${timestamp}_${this.sessionId}.jsonl`);
|
|
|
|
|
this.inMemoryEntries = [
|
|
|
|
|
{
|
|
|
|
|
type: "session",
|
|
|
|
|
@@ -365,7 +376,7 @@ export class SessionManager {
|
|
|
|
|
createBranchedSessionFromEntries(entries: SessionEntry[], branchBeforeIndex: number): string | null {
|
|
|
|
|
const newSessionId = uuidv4();
|
|
|
|
|
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
|
|
|
|
|
const newSessionFile = join(this.sessionDir, `${timestamp}_${newSessionId}.jsonl`);
|
|
|
|
|
const newSessionFile = join(this.getSessionDir(), `${timestamp}_${newSessionId}.jsonl`);
|
|
|
|
|
|
|
|
|
|
const newEntries: SessionEntry[] = [];
|
|
|
|
|
for (let i = 0; i < branchBeforeIndex; i++) {
|
|
|
|
|
@@ -394,65 +405,90 @@ export class SessionManager {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Create a new session for the given directory */
|
|
|
|
|
static create(cwd: string, agentDir: string = getDefaultAgentDir()): SessionManager {
|
|
|
|
|
return new SessionManager(cwd, agentDir, null, true);
|
|
|
|
|
/**
|
|
|
|
|
* Create a new session.
|
|
|
|
|
* @param cwd Working directory (stored in session header)
|
|
|
|
|
* @param sessionDir Optional session directory. If omitted, uses default (~/.pi/agent/sessions/<encoded-cwd>/).
|
|
|
|
|
*/
|
|
|
|
|
static create(cwd: string, sessionDir?: string): SessionManager {
|
|
|
|
|
const dir = sessionDir ?? getDefaultSessionDir(cwd);
|
|
|
|
|
return new SessionManager(cwd, dir, null, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Open a specific session file */
|
|
|
|
|
static open(path: string, agentDir: string = getDefaultAgentDir()): SessionManager {
|
|
|
|
|
/**
|
|
|
|
|
* Open a specific session file.
|
|
|
|
|
* @param path Path to session file
|
|
|
|
|
* @param sessionDir Optional session directory for /new or /branch. If omitted, derives from file's parent.
|
|
|
|
|
*/
|
|
|
|
|
static open(path: string, sessionDir?: string): SessionManager {
|
|
|
|
|
// Extract cwd from session header if possible, otherwise use process.cwd()
|
|
|
|
|
const entries = loadEntriesFromFile(path);
|
|
|
|
|
const header = entries.find((e) => e.type === "session") as SessionHeader | undefined;
|
|
|
|
|
const cwd = header?.cwd ?? process.cwd();
|
|
|
|
|
return new SessionManager(cwd, agentDir, path, true);
|
|
|
|
|
// If no sessionDir provided, derive from file's parent directory
|
|
|
|
|
const dir = sessionDir ?? resolve(path, "..");
|
|
|
|
|
return new SessionManager(cwd, dir, path, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Continue the most recent session for the given directory, or create new if none */
|
|
|
|
|
static continueRecent(cwd: string, agentDir: string = getDefaultAgentDir()): SessionManager {
|
|
|
|
|
const sessionDir = getSessionDirectory(cwd, agentDir);
|
|
|
|
|
const mostRecent = findMostRecentSession(sessionDir);
|
|
|
|
|
/**
|
|
|
|
|
* Continue the most recent session, or create new if none.
|
|
|
|
|
* @param cwd Working directory
|
|
|
|
|
* @param sessionDir Optional session directory. If omitted, uses default (~/.pi/agent/sessions/<encoded-cwd>/).
|
|
|
|
|
*/
|
|
|
|
|
static continueRecent(cwd: string, sessionDir?: string): SessionManager {
|
|
|
|
|
const dir = sessionDir ?? getDefaultSessionDir(cwd);
|
|
|
|
|
const mostRecent = findMostRecentSession(dir);
|
|
|
|
|
if (mostRecent) {
|
|
|
|
|
return new SessionManager(cwd, agentDir, mostRecent, true);
|
|
|
|
|
return new SessionManager(cwd, dir, mostRecent, true);
|
|
|
|
|
}
|
|
|
|
|
return new SessionManager(cwd, agentDir, null, true);
|
|
|
|
|
return new SessionManager(cwd, dir, null, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Create an in-memory session (no file persistence) */
|
|
|
|
|
static inMemory(): SessionManager {
|
|
|
|
|
return new SessionManager(process.cwd(), getDefaultAgentDir(), null, false);
|
|
|
|
|
static inMemory(cwd: string = process.cwd()): SessionManager {
|
|
|
|
|
return new SessionManager(cwd, "", null, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** List all sessions for a directory */
|
|
|
|
|
static list(cwd: string, agentDir: string = getDefaultAgentDir()): SessionInfo[] {
|
|
|
|
|
const sessionDir = getSessionDirectory(cwd, agentDir);
|
|
|
|
|
/**
|
|
|
|
|
* List all sessions.
|
|
|
|
|
* @param cwd Working directory (used to compute default session directory)
|
|
|
|
|
* @param sessionDir Optional session directory. If omitted, uses default (~/.pi/agent/sessions/<encoded-cwd>/).
|
|
|
|
|
*/
|
|
|
|
|
static list(cwd: string, sessionDir?: string): SessionInfo[] {
|
|
|
|
|
const dir = sessionDir ?? getDefaultSessionDir(cwd);
|
|
|
|
|
const sessions: SessionInfo[] = [];
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const files = readdirSync(sessionDir)
|
|
|
|
|
const files = readdirSync(dir)
|
|
|
|
|
.filter((f) => f.endsWith(".jsonl"))
|
|
|
|
|
.map((f) => join(sessionDir, f));
|
|
|
|
|
.map((f) => join(dir, f));
|
|
|
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
|
try {
|
|
|
|
|
const stats = statSync(file);
|
|
|
|
|
const content = readFileSync(file, "utf8");
|
|
|
|
|
const lines = content.trim().split("\n");
|
|
|
|
|
if (lines.length === 0) continue;
|
|
|
|
|
|
|
|
|
|
let sessionId = "";
|
|
|
|
|
let created = stats.birthtime;
|
|
|
|
|
// Check first line for valid session header
|
|
|
|
|
let header: { type: string; id: string; timestamp: string } | null = null;
|
|
|
|
|
try {
|
|
|
|
|
const first = JSON.parse(lines[0]);
|
|
|
|
|
if (first.type === "session" && first.id) {
|
|
|
|
|
header = first;
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// Not valid JSON
|
|
|
|
|
}
|
|
|
|
|
if (!header) continue;
|
|
|
|
|
|
|
|
|
|
const stats = statSync(file);
|
|
|
|
|
let messageCount = 0;
|
|
|
|
|
let firstMessage = "";
|
|
|
|
|
const allMessages: string[] = [];
|
|
|
|
|
|
|
|
|
|
for (const line of lines) {
|
|
|
|
|
for (let i = 1; i < lines.length; i++) {
|
|
|
|
|
try {
|
|
|
|
|
const entry = JSON.parse(line);
|
|
|
|
|
|
|
|
|
|
if (entry.type === "session" && !sessionId) {
|
|
|
|
|
sessionId = entry.id;
|
|
|
|
|
created = new Date(entry.timestamp);
|
|
|
|
|
}
|
|
|
|
|
const entry = JSON.parse(lines[i]);
|
|
|
|
|
|
|
|
|
|
if (entry.type === "message") {
|
|
|
|
|
messageCount++;
|
|
|
|
|
@@ -479,8 +515,8 @@ export class SessionManager {
|
|
|
|
|
|
|
|
|
|
sessions.push({
|
|
|
|
|
path: file,
|
|
|
|
|
id: sessionId || "unknown",
|
|
|
|
|
created,
|
|
|
|
|
id: header.id,
|
|
|
|
|
created: new Date(header.timestamp),
|
|
|
|
|
modified: stats.mtime,
|
|
|
|
|
messageCount,
|
|
|
|
|
firstMessage: firstMessage || "(no messages)",
|
|
|
|
|
|