feat: store working directory for sessions (#1559)

This commit is contained in:
Salman Mohammed
2025-03-07 11:12:57 -05:00
committed by GitHub
parent 2d0cd8e245
commit 32f20cd690
20 changed files with 334 additions and 144 deletions
+23 -4
View File
@@ -4,6 +4,17 @@ export interface SessionMetadata {
description: string;
message_count: number;
total_tokens: number | null;
working_dir: string; // Required in type, but may be missing in old sessions
}
// Helper function to ensure working directory is set
export function ensureWorkingDir(metadata: Partial<SessionMetadata>): SessionMetadata {
return {
description: metadata.description || '',
message_count: metadata.message_count || 0,
total_tokens: metadata.total_tokens || null,
working_dir: metadata.working_dir || process.env.HOME || '',
};
}
export interface Session {
@@ -67,9 +78,13 @@ export async function fetchSessions(): Promise<SessionsResponse> {
// TODO: remove this logic once everyone migrates to the new sessions format
// for now, filter out sessions whose description is empty (old CLI sessions)
const sessions = (await response.json()).sessions.filter(
(session: Session) => session.metadata.description !== ''
);
const rawSessions = await response.json();
const sessions = rawSessions.sessions
.filter((session: Session) => session.metadata.description !== '')
.map((session: Session) => ({
...session,
metadata: ensureWorkingDir(session.metadata),
}));
// order sessions by 'modified' date descending
sessions.sort(
@@ -102,7 +117,11 @@ export async function fetchSessionDetails(sessionId: string): Promise<SessionDet
throw new Error(`Failed to fetch session details: ${response.status} ${response.statusText}`);
}
return await response.json();
const details = await response.json();
return {
...details,
metadata: ensureWorkingDir(details.metadata),
};
} catch (error) {
console.error(`Error fetching session details for ${sessionId}:`, error);
throw error;