mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-21 11:04:35 +00:00
Merge branch 'tmp-2025-01-11' - resume scope toggle with async loading (#620)
This commit is contained in:
commit
9d49b4d4ed
9 changed files with 382 additions and 125 deletions
|
|
@ -10,9 +10,13 @@
|
||||||
- AgentSession: `branch()` → `fork()`, `getUserMessagesForBranching()` → `getUserMessagesForForking()`
|
- AgentSession: `branch()` → `fork()`, `getUserMessagesForBranching()` → `getUserMessagesForForking()`
|
||||||
- Extension events: `session_before_branch` → `session_before_fork`, `session_branch` → `session_fork`
|
- Extension events: `session_before_branch` → `session_before_fork`, `session_branch` → `session_fork`
|
||||||
- Settings: `doubleEscapeAction: "branch" | "tree"` → `"fork" | "tree"`
|
- Settings: `doubleEscapeAction: "branch" | "tree"` → `"fork" | "tree"`
|
||||||
|
- `SessionManager.list()` and `SessionManager.listAll()` are now async, returning `Promise<SessionInfo[]>`. Callers must await them. ([#620](https://github.com/badlogic/pi-mono/pull/620) by [@tmustier](https://github.com/tmustier))
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
- `/resume` selector now toggles between current-folder and all sessions with Tab, showing the session cwd in the All view and loading progress. ([#620](https://github.com/badlogic/pi-mono/pull/620) by [@tmustier](https://github.com/tmustier))
|
||||||
|
- `SessionManager.list()` and `SessionManager.listAll()` accept optional `onProgress` callback for progress updates
|
||||||
|
- `SessionInfo.cwd` field containing the session's working directory (empty string for old sessions)
|
||||||
|
- `SessionListProgress` type export for progress callbacks
|
||||||
- `/models` command to enable/disable models for Ctrl+P cycling. Changes persist to `enabledModels` in settings.json and take effect immediately. ([#626](https://github.com/badlogic/pi-mono/pull/626) by [@CarlosGtrz](https://github.com/CarlosGtrz))
|
- `/models` command to enable/disable models for Ctrl+P cycling. Changes persist to `enabledModels` in settings.json and take effect immediately. ([#626](https://github.com/badlogic/pi-mono/pull/626) by [@CarlosGtrz](https://github.com/CarlosGtrz))
|
||||||
- `model_select` extension hook fires when model changes via `/model`, model cycling, or session restore with `source` field and `previousModel` ([#628](https://github.com/badlogic/pi-mono/pull/628) by [@marckrenn](https://github.com/marckrenn))
|
- `model_select` extension hook fires when model changes via `/model`, model cycling, or session restore with `source` field and `previousModel` ([#628](https://github.com/badlogic/pi-mono/pull/628) by [@marckrenn](https://github.com/marckrenn))
|
||||||
- `ctx.ui.setWorkingMessage()` extension API to customize the "Working..." message during streaming ([#625](https://github.com/badlogic/pi-mono/pull/625) by [@nicobailon](https://github.com/nicobailon))
|
- `ctx.ui.setWorkingMessage()` extension API to customize the "Working..." message during streaming ([#625](https://github.com/badlogic/pi-mono/pull/625) by [@nicobailon](https://github.com/nicobailon))
|
||||||
|
|
|
||||||
|
|
@ -459,7 +459,7 @@ Sessions auto-save to `~/.pi/agent/sessions/` organized by working directory.
|
||||||
pi --continue # Continue most recent session
|
pi --continue # Continue most recent session
|
||||||
pi -c # Short form
|
pi -c # Short form
|
||||||
|
|
||||||
pi --resume # Browse and select from past sessions
|
pi --resume # Browse and select from past sessions (Tab to toggle Current Folder / All)
|
||||||
pi -r # Short form
|
pi -r # Short form
|
||||||
|
|
||||||
pi --no-session # Ephemeral mode (don't save)
|
pi --no-session # Ephemeral mode (don't save)
|
||||||
|
|
|
||||||
|
|
@ -636,12 +636,17 @@ const { session } = await createAgentSession({
|
||||||
sessionManager: SessionManager.open("/path/to/session.jsonl"),
|
sessionManager: SessionManager.open("/path/to/session.jsonl"),
|
||||||
});
|
});
|
||||||
|
|
||||||
// List available sessions
|
// List available sessions (async with optional progress callback)
|
||||||
const sessions = SessionManager.list(process.cwd());
|
const sessions = await SessionManager.list(process.cwd());
|
||||||
for (const info of sessions) {
|
for (const info of sessions) {
|
||||||
console.log(`${info.id}: ${info.firstMessage} (${info.messageCount} messages)`);
|
console.log(`${info.id}: ${info.firstMessage} (${info.messageCount} messages, cwd: ${info.cwd})`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// List all sessions across all projects
|
||||||
|
const allSessions = await SessionManager.listAll((loaded, total) => {
|
||||||
|
console.log(`Loading ${loaded}/${total}...`);
|
||||||
|
});
|
||||||
|
|
||||||
// Custom session directory (no cwd encoding)
|
// Custom session directory (no cwd encoding)
|
||||||
const customDir = "/path/to/my-sessions";
|
const customDir = "/path/to/my-sessions";
|
||||||
const { session } = await createAgentSession({
|
const { session } = await createAgentSession({
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ if (modelFallbackMessage) console.log("Note:", modelFallbackMessage);
|
||||||
console.log("Continued session:", continued.sessionFile);
|
console.log("Continued session:", continued.sessionFile);
|
||||||
|
|
||||||
// List and open specific session
|
// List and open specific session
|
||||||
const sessions = SessionManager.list(process.cwd());
|
const sessions = await SessionManager.list(process.cwd());
|
||||||
console.log(`\nFound ${sessions.length} sessions:`);
|
console.log(`\nFound ${sessions.length} sessions:`);
|
||||||
for (const info of sessions.slice(0, 3)) {
|
for (const info of sessions.slice(0, 3)) {
|
||||||
console.log(` ${info.id.slice(0, 8)}... - "${info.firstMessage.slice(0, 30)}..."`);
|
console.log(` ${info.id.slice(0, 8)}... - "${info.firstMessage.slice(0, 30)}..."`);
|
||||||
|
|
|
||||||
|
|
@ -3,17 +3,23 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { ProcessTerminal, TUI } from "@mariozechner/pi-tui";
|
import { ProcessTerminal, TUI } from "@mariozechner/pi-tui";
|
||||||
import type { SessionInfo } from "../core/session-manager.js";
|
import type { SessionInfo, SessionListProgress } from "../core/session-manager.js";
|
||||||
import { SessionSelectorComponent } from "../modes/interactive/components/session-selector.js";
|
import { SessionSelectorComponent } from "../modes/interactive/components/session-selector.js";
|
||||||
|
|
||||||
|
type SessionsLoader = (onProgress?: SessionListProgress) => Promise<SessionInfo[]>;
|
||||||
|
|
||||||
/** Show TUI session selector and return selected session path or null if cancelled */
|
/** Show TUI session selector and return selected session path or null if cancelled */
|
||||||
export async function selectSession(sessions: SessionInfo[]): Promise<string | null> {
|
export async function selectSession(
|
||||||
|
currentSessionsLoader: SessionsLoader,
|
||||||
|
allSessionsLoader: SessionsLoader,
|
||||||
|
): Promise<string | null> {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const ui = new TUI(new ProcessTerminal());
|
const ui = new TUI(new ProcessTerminal());
|
||||||
let resolved = false;
|
let resolved = false;
|
||||||
|
|
||||||
const selector = new SessionSelectorComponent(
|
const selector = new SessionSelectorComponent(
|
||||||
sessions,
|
currentSessionsLoader,
|
||||||
|
allSessionsLoader,
|
||||||
(path: string) => {
|
(path: string) => {
|
||||||
if (!resolved) {
|
if (!resolved) {
|
||||||
resolved = true;
|
resolved = true;
|
||||||
|
|
@ -32,6 +38,7 @@ export async function selectSession(sessions: SessionInfo[]): Promise<string | n
|
||||||
ui.stop();
|
ui.stop();
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
},
|
},
|
||||||
|
() => ui.requestRender(),
|
||||||
);
|
);
|
||||||
|
|
||||||
ui.addChild(selector);
|
ui.addChild(selector);
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,9 @@ import {
|
||||||
statSync,
|
statSync,
|
||||||
writeFileSync,
|
writeFileSync,
|
||||||
} from "fs";
|
} from "fs";
|
||||||
|
import { readdir, readFile, stat } from "fs/promises";
|
||||||
import { join, resolve } from "path";
|
import { join, resolve } from "path";
|
||||||
import { getAgentDir as getDefaultAgentDir } from "../config.js";
|
import { getAgentDir as getDefaultAgentDir, getSessionsDir } from "../config.js";
|
||||||
import {
|
import {
|
||||||
type BashExecutionMessage,
|
type BashExecutionMessage,
|
||||||
type CustomMessage,
|
type CustomMessage,
|
||||||
|
|
@ -156,6 +157,8 @@ export interface SessionContext {
|
||||||
export interface SessionInfo {
|
export interface SessionInfo {
|
||||||
path: string;
|
path: string;
|
||||||
id: string;
|
id: string;
|
||||||
|
/** Working directory where the session was started. Empty string for old sessions. */
|
||||||
|
cwd: string;
|
||||||
created: Date;
|
created: Date;
|
||||||
modified: Date;
|
modified: Date;
|
||||||
messageCount: number;
|
messageCount: number;
|
||||||
|
|
@ -470,6 +473,118 @@ export function findMostRecentSession(sessionDir: string): string | null {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isMessageWithContent(message: AgentMessage): message is Message {
|
||||||
|
return typeof (message as Message).role === "string" && "content" in message;
|
||||||
|
}
|
||||||
|
|
||||||
|
function extractTextContent(message: Message): string {
|
||||||
|
const content = message.content;
|
||||||
|
if (typeof content === "string") {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
return content
|
||||||
|
.filter((block): block is TextContent => block.type === "text")
|
||||||
|
.map((block) => block.text)
|
||||||
|
.join(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
async function buildSessionInfo(filePath: string): Promise<SessionInfo | null> {
|
||||||
|
try {
|
||||||
|
const content = await readFile(filePath, "utf8");
|
||||||
|
const entries: FileEntry[] = [];
|
||||||
|
const lines = content.trim().split("\n");
|
||||||
|
|
||||||
|
for (const line of lines) {
|
||||||
|
if (!line.trim()) continue;
|
||||||
|
try {
|
||||||
|
entries.push(JSON.parse(line) as FileEntry);
|
||||||
|
} catch {
|
||||||
|
// Skip malformed lines
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entries.length === 0) return null;
|
||||||
|
const header = entries[0];
|
||||||
|
if (header.type !== "session") return null;
|
||||||
|
|
||||||
|
const stats = await stat(filePath);
|
||||||
|
let messageCount = 0;
|
||||||
|
let firstMessage = "";
|
||||||
|
const allMessages: string[] = [];
|
||||||
|
|
||||||
|
for (const entry of entries) {
|
||||||
|
if (entry.type !== "message") continue;
|
||||||
|
messageCount++;
|
||||||
|
|
||||||
|
const message = (entry as SessionMessageEntry).message;
|
||||||
|
if (!isMessageWithContent(message)) continue;
|
||||||
|
if (message.role !== "user" && message.role !== "assistant") continue;
|
||||||
|
|
||||||
|
const textContent = extractTextContent(message);
|
||||||
|
if (!textContent) continue;
|
||||||
|
|
||||||
|
allMessages.push(textContent);
|
||||||
|
if (!firstMessage && message.role === "user") {
|
||||||
|
firstMessage = textContent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const cwd = typeof (header as SessionHeader).cwd === "string" ? (header as SessionHeader).cwd : "";
|
||||||
|
|
||||||
|
return {
|
||||||
|
path: filePath,
|
||||||
|
id: (header as SessionHeader).id,
|
||||||
|
cwd,
|
||||||
|
created: new Date((header as SessionHeader).timestamp),
|
||||||
|
modified: stats.mtime,
|
||||||
|
messageCount,
|
||||||
|
firstMessage: firstMessage || "(no messages)",
|
||||||
|
allMessagesText: allMessages.join(" "),
|
||||||
|
};
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type SessionListProgress = (loaded: number, total: number) => void;
|
||||||
|
|
||||||
|
async function listSessionsFromDir(
|
||||||
|
dir: string,
|
||||||
|
onProgress?: SessionListProgress,
|
||||||
|
progressOffset = 0,
|
||||||
|
progressTotal?: number,
|
||||||
|
): Promise<SessionInfo[]> {
|
||||||
|
const sessions: SessionInfo[] = [];
|
||||||
|
if (!existsSync(dir)) {
|
||||||
|
return sessions;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const dirEntries = await readdir(dir);
|
||||||
|
const files = dirEntries.filter((f) => f.endsWith(".jsonl")).map((f) => join(dir, f));
|
||||||
|
const total = progressTotal ?? files.length;
|
||||||
|
|
||||||
|
let loaded = 0;
|
||||||
|
const results = await Promise.all(
|
||||||
|
files.map(async (file) => {
|
||||||
|
const info = await buildSessionInfo(file);
|
||||||
|
loaded++;
|
||||||
|
onProgress?.(progressOffset + loaded, total);
|
||||||
|
return info;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
for (const info of results) {
|
||||||
|
if (info) {
|
||||||
|
sessions.push(info);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// Return empty list on error
|
||||||
|
}
|
||||||
|
|
||||||
|
return sessions;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages conversation sessions as append-only trees stored in JSONL files.
|
* Manages conversation sessions as append-only trees stored in JSONL files.
|
||||||
*
|
*
|
||||||
|
|
@ -1057,88 +1172,69 @@ export class SessionManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List all sessions.
|
* List all sessions for a directory.
|
||||||
* @param cwd Working directory (used to compute default session directory)
|
* @param cwd Working directory (used to compute default session directory)
|
||||||
* @param sessionDir Optional session directory. If omitted, uses default (~/.pi/agent/sessions/<encoded-cwd>/).
|
* @param sessionDir Optional session directory. If omitted, uses default (~/.pi/agent/sessions/<encoded-cwd>/).
|
||||||
|
* @param onProgress Optional callback for progress updates (loaded, total)
|
||||||
*/
|
*/
|
||||||
static list(cwd: string, sessionDir?: string): SessionInfo[] {
|
static async list(cwd: string, sessionDir?: string, onProgress?: SessionListProgress): Promise<SessionInfo[]> {
|
||||||
const dir = sessionDir ?? getDefaultSessionDir(cwd);
|
const dir = sessionDir ?? getDefaultSessionDir(cwd);
|
||||||
|
const sessions = await listSessionsFromDir(dir, onProgress);
|
||||||
|
sessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());
|
||||||
|
return sessions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List all sessions across all project directories.
|
||||||
|
* @param onProgress Optional callback for progress updates (loaded, total)
|
||||||
|
*/
|
||||||
|
static async listAll(onProgress?: SessionListProgress): Promise<SessionInfo[]> {
|
||||||
|
const sessionsDir = getSessionsDir();
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!existsSync(sessionsDir)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
const entries = await readdir(sessionsDir, { withFileTypes: true });
|
||||||
|
const dirs = entries.filter((e) => e.isDirectory()).map((e) => join(sessionsDir, e.name));
|
||||||
|
|
||||||
|
// Count total files first for accurate progress
|
||||||
|
let totalFiles = 0;
|
||||||
|
const dirFiles: string[][] = [];
|
||||||
|
for (const dir of dirs) {
|
||||||
|
try {
|
||||||
|
const files = (await readdir(dir)).filter((f) => f.endsWith(".jsonl"));
|
||||||
|
dirFiles.push(files.map((f) => join(dir, f)));
|
||||||
|
totalFiles += files.length;
|
||||||
|
} catch {
|
||||||
|
dirFiles.push([]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process all files with progress tracking
|
||||||
|
let loaded = 0;
|
||||||
const sessions: SessionInfo[] = [];
|
const sessions: SessionInfo[] = [];
|
||||||
|
const allFiles = dirFiles.flat();
|
||||||
|
|
||||||
try {
|
const results = await Promise.all(
|
||||||
const files = readdirSync(dir)
|
allFiles.map(async (file) => {
|
||||||
.filter((f) => f.endsWith(".jsonl"))
|
const info = await buildSessionInfo(file);
|
||||||
.map((f) => join(dir, f));
|
loaded++;
|
||||||
|
onProgress?.(loaded, totalFiles);
|
||||||
|
return info;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
for (const file of files) {
|
for (const info of results) {
|
||||||
try {
|
if (info) {
|
||||||
const content = readFileSync(file, "utf8");
|
sessions.push(info);
|
||||||
const lines = content.trim().split("\n");
|
|
||||||
if (lines.length === 0) continue;
|
|
||||||
|
|
||||||
// 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 (let i = 1; i < lines.length; i++) {
|
|
||||||
try {
|
|
||||||
const entry = JSON.parse(lines[i]);
|
|
||||||
|
|
||||||
if (entry.type === "message") {
|
|
||||||
messageCount++;
|
|
||||||
|
|
||||||
if (entry.message.role === "user" || entry.message.role === "assistant") {
|
|
||||||
const textContent = entry.message.content
|
|
||||||
.filter((c: any) => c.type === "text")
|
|
||||||
.map((c: any) => c.text)
|
|
||||||
.join(" ");
|
|
||||||
|
|
||||||
if (textContent) {
|
|
||||||
allMessages.push(textContent);
|
|
||||||
|
|
||||||
if (!firstMessage && entry.message.role === "user") {
|
|
||||||
firstMessage = textContent;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Skip malformed lines
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sessions.push({
|
|
||||||
path: file,
|
|
||||||
id: header.id,
|
|
||||||
created: new Date(header.timestamp),
|
|
||||||
modified: stats.mtime,
|
|
||||||
messageCount,
|
|
||||||
firstMessage: firstMessage || "(no messages)",
|
|
||||||
allMessagesText: allMessages.join(" "),
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
// Skip files that can't be read
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());
|
sessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());
|
||||||
} catch {
|
|
||||||
// Return empty list on error
|
|
||||||
}
|
|
||||||
|
|
||||||
return sessions;
|
return sessions;
|
||||||
|
} catch {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -61,14 +61,14 @@ async function prepareInitialMessage(
|
||||||
* Resolve a session argument to a file path.
|
* Resolve a session argument to a file path.
|
||||||
* If it looks like a path, use as-is. Otherwise try to match as session ID prefix.
|
* If it looks like a path, use as-is. Otherwise try to match as session ID prefix.
|
||||||
*/
|
*/
|
||||||
function resolveSessionPath(sessionArg: string, cwd: string, sessionDir?: string): string {
|
async function resolveSessionPath(sessionArg: string, cwd: string, sessionDir?: string): Promise<string> {
|
||||||
// If it looks like a file path, use as-is
|
// If it looks like a file path, use as-is
|
||||||
if (sessionArg.includes("/") || sessionArg.includes("\\") || sessionArg.endsWith(".jsonl")) {
|
if (sessionArg.includes("/") || sessionArg.includes("\\") || sessionArg.endsWith(".jsonl")) {
|
||||||
return sessionArg;
|
return sessionArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to match as session ID (full or partial UUID)
|
// Try to match as session ID (full or partial UUID)
|
||||||
const sessions = SessionManager.list(cwd, sessionDir);
|
const sessions = await SessionManager.list(cwd, sessionDir);
|
||||||
const matches = sessions.filter((s) => s.id.startsWith(sessionArg));
|
const matches = sessions.filter((s) => s.id.startsWith(sessionArg));
|
||||||
|
|
||||||
if (matches.length >= 1) {
|
if (matches.length >= 1) {
|
||||||
|
|
@ -79,12 +79,12 @@ function resolveSessionPath(sessionArg: string, cwd: string, sessionDir?: string
|
||||||
return sessionArg;
|
return sessionArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
function createSessionManager(parsed: Args, cwd: string): SessionManager | undefined {
|
async function createSessionManager(parsed: Args, cwd: string): Promise<SessionManager | undefined> {
|
||||||
if (parsed.noSession) {
|
if (parsed.noSession) {
|
||||||
return SessionManager.inMemory();
|
return SessionManager.inMemory();
|
||||||
}
|
}
|
||||||
if (parsed.session) {
|
if (parsed.session) {
|
||||||
const resolvedPath = resolveSessionPath(parsed.session, cwd, parsed.sessionDir);
|
const resolvedPath = await resolveSessionPath(parsed.session, cwd, parsed.sessionDir);
|
||||||
return SessionManager.open(resolvedPath, parsed.sessionDir);
|
return SessionManager.open(resolvedPath, parsed.sessionDir);
|
||||||
}
|
}
|
||||||
if (parsed.continue) {
|
if (parsed.continue) {
|
||||||
|
|
@ -314,7 +314,7 @@ export async function main(args: string[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create session manager based on CLI flags
|
// Create session manager based on CLI flags
|
||||||
let sessionManager = createSessionManager(parsed, cwd);
|
let sessionManager = await createSessionManager(parsed, cwd);
|
||||||
time("createSessionManager");
|
time("createSessionManager");
|
||||||
|
|
||||||
// Handle --resume: show session picker
|
// Handle --resume: show session picker
|
||||||
|
|
@ -322,13 +322,10 @@ export async function main(args: string[]) {
|
||||||
// Initialize keybindings so session picker respects user config
|
// Initialize keybindings so session picker respects user config
|
||||||
KeybindingsManager.create();
|
KeybindingsManager.create();
|
||||||
|
|
||||||
const sessions = SessionManager.list(cwd, parsed.sessionDir);
|
const selectedPath = await selectSession(
|
||||||
time("SessionManager.list");
|
(onProgress) => SessionManager.list(cwd, parsed.sessionDir, onProgress),
|
||||||
if (sessions.length === 0) {
|
SessionManager.listAll,
|
||||||
console.log(chalk.dim("No sessions found"));
|
);
|
||||||
return;
|
|
||||||
}
|
|
||||||
const selectedPath = await selectSession(sessions);
|
|
||||||
time("selectSession");
|
time("selectSession");
|
||||||
if (!selectedPath) {
|
if (!selectedPath) {
|
||||||
console.log(chalk.dim("No session selected"));
|
console.log(chalk.dim("No session selected"));
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import * as os from "node:os";
|
||||||
import {
|
import {
|
||||||
type Component,
|
type Component,
|
||||||
Container,
|
Container,
|
||||||
|
|
@ -5,69 +6,25 @@ import {
|
||||||
getEditorKeybindings,
|
getEditorKeybindings,
|
||||||
Input,
|
Input,
|
||||||
Spacer,
|
Spacer,
|
||||||
Text,
|
|
||||||
truncateToWidth,
|
truncateToWidth,
|
||||||
|
visibleWidth,
|
||||||
} from "@mariozechner/pi-tui";
|
} from "@mariozechner/pi-tui";
|
||||||
import type { SessionInfo } from "../../../core/session-manager.js";
|
import type { SessionInfo, SessionListProgress } from "../../../core/session-manager.js";
|
||||||
import { theme } from "../theme/theme.js";
|
import { theme } from "../theme/theme.js";
|
||||||
import { DynamicBorder } from "./dynamic-border.js";
|
import { DynamicBorder } from "./dynamic-border.js";
|
||||||
|
|
||||||
/**
|
type SessionScope = "current" | "all";
|
||||||
* Custom session list component with multi-line items and search
|
|
||||||
*/
|
|
||||||
class SessionList implements Component {
|
|
||||||
private allSessions: SessionInfo[] = [];
|
|
||||||
private filteredSessions: SessionInfo[] = [];
|
|
||||||
private selectedIndex: number = 0;
|
|
||||||
private searchInput: Input;
|
|
||||||
public onSelect?: (sessionPath: string) => void;
|
|
||||||
public onCancel?: () => void;
|
|
||||||
public onExit: () => void = () => {};
|
|
||||||
private maxVisible: number = 5; // Max sessions visible (each session is 3 lines: msg + metadata + blank)
|
|
||||||
|
|
||||||
constructor(sessions: SessionInfo[]) {
|
function shortenPath(path: string): string {
|
||||||
this.allSessions = sessions;
|
const home = os.homedir();
|
||||||
this.filteredSessions = sessions;
|
if (!path) return path;
|
||||||
this.searchInput = new Input();
|
if (path.startsWith(home)) {
|
||||||
|
return `~${path.slice(home.length)}`;
|
||||||
// Handle Enter in search input - select current item
|
|
||||||
this.searchInput.onSubmit = () => {
|
|
||||||
if (this.filteredSessions[this.selectedIndex]) {
|
|
||||||
const selected = this.filteredSessions[this.selectedIndex];
|
|
||||||
if (this.onSelect) {
|
|
||||||
this.onSelect(selected.path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
private filterSessions(query: string): void {
|
function formatSessionDate(date: Date): string {
|
||||||
this.filteredSessions = fuzzyFilter(
|
|
||||||
this.allSessions,
|
|
||||||
query,
|
|
||||||
(session) => `${session.id} ${session.allMessagesText}`,
|
|
||||||
);
|
|
||||||
this.selectedIndex = Math.min(this.selectedIndex, Math.max(0, this.filteredSessions.length - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
invalidate(): void {
|
|
||||||
// No cached state to invalidate currently
|
|
||||||
}
|
|
||||||
|
|
||||||
render(width: number): string[] {
|
|
||||||
const lines: string[] = [];
|
|
||||||
|
|
||||||
// Render search input
|
|
||||||
lines.push(...this.searchInput.render(width));
|
|
||||||
lines.push(""); // Blank line after search
|
|
||||||
|
|
||||||
if (this.filteredSessions.length === 0) {
|
|
||||||
lines.push(theme.fg("muted", " No sessions found"));
|
|
||||||
return lines;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Format dates
|
|
||||||
const formatDate = (date: Date): string => {
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const diffMs = now.getTime() - date.getTime();
|
const diffMs = now.getTime() - date.getTime();
|
||||||
const diffMins = Math.floor(diffMs / 60000);
|
const diffMins = Math.floor(diffMs / 60000);
|
||||||
|
|
@ -81,7 +38,116 @@ class SessionList implements Component {
|
||||||
if (diffDays < 7) return `${diffDays} days ago`;
|
if (diffDays < 7) return `${diffDays} days ago`;
|
||||||
|
|
||||||
return date.toLocaleDateString();
|
return date.toLocaleDateString();
|
||||||
|
}
|
||||||
|
|
||||||
|
class SessionSelectorHeader implements Component {
|
||||||
|
private scope: SessionScope;
|
||||||
|
private loading = false;
|
||||||
|
private loadProgress: { loaded: number; total: number } | null = null;
|
||||||
|
|
||||||
|
constructor(scope: SessionScope) {
|
||||||
|
this.scope = scope;
|
||||||
|
}
|
||||||
|
|
||||||
|
setScope(scope: SessionScope): void {
|
||||||
|
this.scope = scope;
|
||||||
|
}
|
||||||
|
|
||||||
|
setLoading(loading: boolean): void {
|
||||||
|
this.loading = loading;
|
||||||
|
if (!loading) {
|
||||||
|
this.loadProgress = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setProgress(loaded: number, total: number): void {
|
||||||
|
this.loadProgress = { loaded, total };
|
||||||
|
}
|
||||||
|
|
||||||
|
invalidate(): void {}
|
||||||
|
|
||||||
|
render(width: number): string[] {
|
||||||
|
const title = this.scope === "current" ? "Resume Session (Current Folder)" : "Resume Session (All)";
|
||||||
|
const leftText = theme.bold(title);
|
||||||
|
let scopeText: string;
|
||||||
|
if (this.loading) {
|
||||||
|
const progressText = this.loadProgress ? `${this.loadProgress.loaded}/${this.loadProgress.total}` : "...";
|
||||||
|
scopeText = `${theme.fg("muted", "○ Current Folder | ")}${theme.fg("accent", `Loading ${progressText}`)}`;
|
||||||
|
} else {
|
||||||
|
scopeText =
|
||||||
|
this.scope === "current"
|
||||||
|
? `${theme.fg("accent", "◉ Current Folder")}${theme.fg("muted", " | ○ All")}`
|
||||||
|
: `${theme.fg("muted", "○ Current Folder | ")}${theme.fg("accent", "◉ All")}`;
|
||||||
|
}
|
||||||
|
const rightText = truncateToWidth(scopeText, width, "");
|
||||||
|
const availableLeft = Math.max(0, width - visibleWidth(rightText) - 1);
|
||||||
|
const left = truncateToWidth(leftText, availableLeft, "");
|
||||||
|
const spacing = Math.max(0, width - visibleWidth(left) - visibleWidth(rightText));
|
||||||
|
const hint = theme.fg("muted", "Tab to toggle scope");
|
||||||
|
return [`${left}${" ".repeat(spacing)}${rightText}`, hint];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom session list component with multi-line items and search
|
||||||
|
*/
|
||||||
|
class SessionList implements Component {
|
||||||
|
private allSessions: SessionInfo[] = [];
|
||||||
|
private filteredSessions: SessionInfo[] = [];
|
||||||
|
private selectedIndex: number = 0;
|
||||||
|
private searchInput: Input;
|
||||||
|
private showCwd = false;
|
||||||
|
public onSelect?: (sessionPath: string) => void;
|
||||||
|
public onCancel?: () => void;
|
||||||
|
public onExit: () => void = () => {};
|
||||||
|
public onToggleScope?: () => void;
|
||||||
|
private maxVisible: number = 5; // Max sessions visible (each session is 3 lines: msg + metadata + blank)
|
||||||
|
|
||||||
|
constructor(sessions: SessionInfo[], showCwd: boolean) {
|
||||||
|
this.allSessions = sessions;
|
||||||
|
this.filteredSessions = sessions;
|
||||||
|
this.searchInput = new Input();
|
||||||
|
this.showCwd = showCwd;
|
||||||
|
|
||||||
|
// Handle Enter in search input - select current item
|
||||||
|
this.searchInput.onSubmit = () => {
|
||||||
|
if (this.filteredSessions[this.selectedIndex]) {
|
||||||
|
const selected = this.filteredSessions[this.selectedIndex];
|
||||||
|
if (this.onSelect) {
|
||||||
|
this.onSelect(selected.path);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
setSessions(sessions: SessionInfo[], showCwd: boolean): void {
|
||||||
|
this.allSessions = sessions;
|
||||||
|
this.showCwd = showCwd;
|
||||||
|
this.filterSessions(this.searchInput.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
private filterSessions(query: string): void {
|
||||||
|
this.filteredSessions = fuzzyFilter(
|
||||||
|
this.allSessions,
|
||||||
|
query,
|
||||||
|
(session) => `${session.id} ${session.allMessagesText} ${session.cwd}`,
|
||||||
|
);
|
||||||
|
this.selectedIndex = Math.min(this.selectedIndex, Math.max(0, this.filteredSessions.length - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
invalidate(): void {}
|
||||||
|
|
||||||
|
render(width: number): string[] {
|
||||||
|
const lines: string[] = [];
|
||||||
|
|
||||||
|
// Render search input
|
||||||
|
lines.push(...this.searchInput.render(width));
|
||||||
|
lines.push(""); // Blank line after search
|
||||||
|
|
||||||
|
if (this.filteredSessions.length === 0) {
|
||||||
|
lines.push(theme.fg("muted", " No sessions found"));
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
|
||||||
// Calculate visible range with scrolling
|
// Calculate visible range with scrolling
|
||||||
const startIndex = Math.max(
|
const startIndex = Math.max(
|
||||||
|
|
@ -105,9 +171,13 @@ class SessionList implements Component {
|
||||||
const messageLine = cursor + (isSelected ? theme.bold(truncatedMsg) : truncatedMsg);
|
const messageLine = cursor + (isSelected ? theme.bold(truncatedMsg) : truncatedMsg);
|
||||||
|
|
||||||
// Second line: metadata (dimmed) - also truncate for safety
|
// Second line: metadata (dimmed) - also truncate for safety
|
||||||
const modified = formatDate(session.modified);
|
const modified = formatSessionDate(session.modified);
|
||||||
const msgCount = `${session.messageCount} message${session.messageCount !== 1 ? "s" : ""}`;
|
const msgCount = `${session.messageCount} message${session.messageCount !== 1 ? "s" : ""}`;
|
||||||
const metadata = ` ${modified} · ${msgCount}`;
|
const metadataParts = [modified, msgCount];
|
||||||
|
if (this.showCwd && session.cwd) {
|
||||||
|
metadataParts.push(shortenPath(session.cwd));
|
||||||
|
}
|
||||||
|
const metadata = ` ${metadataParts.join(" · ")}`;
|
||||||
const metadataLine = theme.fg("dim", truncateToWidth(metadata, width, ""));
|
const metadataLine = theme.fg("dim", truncateToWidth(metadata, width, ""));
|
||||||
|
|
||||||
lines.push(messageLine);
|
lines.push(messageLine);
|
||||||
|
|
@ -127,6 +197,12 @@ class SessionList implements Component {
|
||||||
|
|
||||||
handleInput(keyData: string): void {
|
handleInput(keyData: string): void {
|
||||||
const kb = getEditorKeybindings();
|
const kb = getEditorKeybindings();
|
||||||
|
if (kb.matches(keyData, "tab")) {
|
||||||
|
if (this.onToggleScope) {
|
||||||
|
this.onToggleScope();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
// Up arrow
|
// Up arrow
|
||||||
if (kb.matches(keyData, "selectUp")) {
|
if (kb.matches(keyData, "selectUp")) {
|
||||||
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
|
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
|
||||||
|
|
@ -156,32 +232,50 @@ class SessionList implements Component {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SessionsLoader = (onProgress?: SessionListProgress) => Promise<SessionInfo[]>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Component that renders a session selector
|
* Component that renders a session selector
|
||||||
*/
|
*/
|
||||||
export class SessionSelectorComponent extends Container {
|
export class SessionSelectorComponent extends Container {
|
||||||
private sessionList: SessionList;
|
private sessionList: SessionList;
|
||||||
|
private header: SessionSelectorHeader;
|
||||||
|
private scope: SessionScope = "current";
|
||||||
|
private currentSessions: SessionInfo[] | null = null;
|
||||||
|
private allSessions: SessionInfo[] | null = null;
|
||||||
|
private currentSessionsLoader: SessionsLoader;
|
||||||
|
private allSessionsLoader: SessionsLoader;
|
||||||
|
private onCancel: () => void;
|
||||||
|
private requestRender: () => void;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
sessions: SessionInfo[],
|
currentSessionsLoader: SessionsLoader,
|
||||||
|
allSessionsLoader: SessionsLoader,
|
||||||
onSelect: (sessionPath: string) => void,
|
onSelect: (sessionPath: string) => void,
|
||||||
onCancel: () => void,
|
onCancel: () => void,
|
||||||
onExit: () => void,
|
onExit: () => void,
|
||||||
|
requestRender: () => void,
|
||||||
) {
|
) {
|
||||||
super();
|
super();
|
||||||
|
this.currentSessionsLoader = currentSessionsLoader;
|
||||||
|
this.allSessionsLoader = allSessionsLoader;
|
||||||
|
this.onCancel = onCancel;
|
||||||
|
this.requestRender = requestRender;
|
||||||
|
this.header = new SessionSelectorHeader(this.scope);
|
||||||
|
|
||||||
// Add header
|
// Add header
|
||||||
this.addChild(new Spacer(1));
|
this.addChild(new Spacer(1));
|
||||||
this.addChild(new Text(theme.bold("Resume Session"), 1, 0));
|
this.addChild(this.header);
|
||||||
this.addChild(new Spacer(1));
|
this.addChild(new Spacer(1));
|
||||||
this.addChild(new DynamicBorder());
|
this.addChild(new DynamicBorder());
|
||||||
this.addChild(new Spacer(1));
|
this.addChild(new Spacer(1));
|
||||||
|
|
||||||
// Create session list
|
// Create session list (starts empty, will be populated after load)
|
||||||
this.sessionList = new SessionList(sessions);
|
this.sessionList = new SessionList([], false);
|
||||||
this.sessionList.onSelect = onSelect;
|
this.sessionList.onSelect = onSelect;
|
||||||
this.sessionList.onCancel = onCancel;
|
this.sessionList.onCancel = onCancel;
|
||||||
this.sessionList.onExit = onExit;
|
this.sessionList.onExit = onExit;
|
||||||
|
this.sessionList.onToggleScope = () => this.toggleScope();
|
||||||
|
|
||||||
this.addChild(this.sessionList);
|
this.addChild(this.sessionList);
|
||||||
|
|
||||||
|
|
@ -189,9 +283,61 @@ export class SessionSelectorComponent extends Container {
|
||||||
this.addChild(new Spacer(1));
|
this.addChild(new Spacer(1));
|
||||||
this.addChild(new DynamicBorder());
|
this.addChild(new DynamicBorder());
|
||||||
|
|
||||||
// Auto-cancel if no sessions
|
// Start loading current sessions immediately
|
||||||
|
this.loadCurrentSessions();
|
||||||
|
}
|
||||||
|
|
||||||
|
private loadCurrentSessions(): void {
|
||||||
|
this.header.setLoading(true);
|
||||||
|
this.requestRender();
|
||||||
|
this.currentSessionsLoader((loaded, total) => {
|
||||||
|
this.header.setProgress(loaded, total);
|
||||||
|
this.requestRender();
|
||||||
|
}).then((sessions) => {
|
||||||
|
this.currentSessions = sessions;
|
||||||
|
this.header.setLoading(false);
|
||||||
|
this.sessionList.setSessions(sessions, false);
|
||||||
|
this.requestRender();
|
||||||
|
// If no sessions found, cancel
|
||||||
if (sessions.length === 0) {
|
if (sessions.length === 0) {
|
||||||
setTimeout(() => onCancel(), 100);
|
this.onCancel();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private toggleScope(): void {
|
||||||
|
if (this.scope === "current") {
|
||||||
|
// Switching to "all" - load if not already loaded
|
||||||
|
if (this.allSessions === null) {
|
||||||
|
this.header.setLoading(true);
|
||||||
|
this.header.setScope("all");
|
||||||
|
this.sessionList.setSessions([], true); // Clear list while loading
|
||||||
|
this.requestRender();
|
||||||
|
// Load asynchronously with progress updates
|
||||||
|
this.allSessionsLoader((loaded, total) => {
|
||||||
|
this.header.setProgress(loaded, total);
|
||||||
|
this.requestRender();
|
||||||
|
}).then((sessions) => {
|
||||||
|
this.allSessions = sessions;
|
||||||
|
this.header.setLoading(false);
|
||||||
|
this.scope = "all";
|
||||||
|
this.sessionList.setSessions(this.allSessions, true);
|
||||||
|
this.requestRender();
|
||||||
|
// If no sessions in All scope either, cancel
|
||||||
|
if (this.allSessions.length === 0 && (this.currentSessions?.length ?? 0) === 0) {
|
||||||
|
this.onCancel();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.scope = "all";
|
||||||
|
this.sessionList.setSessions(this.allSessions, true);
|
||||||
|
this.header.setScope(this.scope);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Switching back to "current"
|
||||||
|
this.scope = "current";
|
||||||
|
this.sessionList.setSessions(this.currentSessions ?? [], false);
|
||||||
|
this.header.setScope(this.scope);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2876,9 +2876,10 @@ export class InteractiveMode {
|
||||||
|
|
||||||
private showSessionSelector(): void {
|
private showSessionSelector(): void {
|
||||||
this.showSelector((done) => {
|
this.showSelector((done) => {
|
||||||
const sessions = SessionManager.list(this.sessionManager.getCwd(), this.sessionManager.getSessionDir());
|
|
||||||
const selector = new SessionSelectorComponent(
|
const selector = new SessionSelectorComponent(
|
||||||
sessions,
|
(onProgress) =>
|
||||||
|
SessionManager.list(this.sessionManager.getCwd(), this.sessionManager.getSessionDir(), onProgress),
|
||||||
|
SessionManager.listAll,
|
||||||
async (sessionPath) => {
|
async (sessionPath) => {
|
||||||
done();
|
done();
|
||||||
await this.handleResumeSession(sessionPath);
|
await this.handleResumeSession(sessionPath);
|
||||||
|
|
@ -2890,6 +2891,7 @@ export class InteractiveMode {
|
||||||
() => {
|
() => {
|
||||||
void this.shutdown();
|
void this.shutdown();
|
||||||
},
|
},
|
||||||
|
() => this.ui.requestRender(),
|
||||||
);
|
);
|
||||||
return { component: selector, focus: selector.getSessionList() };
|
return { component: selector, focus: selector.getSessionList() };
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue