Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions packages/cli/src/utils/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
generateSummary,
writeToStderr,
writeToStdout,
OutputFormat,
type Config,
} from '@google/gemini-cli-core';
import {
Expand All @@ -18,37 +19,43 @@ import {
} from './sessionUtils.js';

export async function listSessions(config: Config): Promise<void> {
// Generate summary for most recent session if needed
await generateSummary(config);
// Only generate summaries for interactive display to keep JSON output clean
if (config.getOutputFormat() !== OutputFormat.JSON) {
await generateSummary(config);
}

const sessionSelector = new SessionSelector(config);
const sessions = await sessionSelector.listSessions();

if (sessions.length === 0) {
const sortedSessions = sessions.sort(
(a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime(),
);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The SessionSelector.listSessions() method already returns an array of sessions sorted by startTime (ascending), as implemented in the underlying getSessionFiles utility. Re-sorting the array here is redundant and adds unnecessary complexity. You can simply use the sessions array directly.

  const sortedSessions = sessions;


if (config.getOutputFormat() === OutputFormat.JSON) {
writeToStdout(JSON.stringify(sortedSessions, null, 2) + '\n');
return;
}

if (sortedSessions.length === 0) {
writeToStdout('No previous sessions found for this project.');
return;
}

writeToStdout(
`\nAvailable sessions for this project (${sessions.length}):\n`,
`\nAvailable sessions for this project (${sortedSessions.length}):\n`,
);

sessions
.sort(
(a, b) =>
new Date(a.startTime).getTime() - new Date(b.startTime).getTime(),
)
.forEach((session, index) => {
const current = session.isCurrentSession ? ', current' : '';
const time = formatRelativeTime(session.lastUpdated);
const title =
session.displayName.length > 100
? session.displayName.slice(0, 97) + '...'
: session.displayName;
writeToStdout(
` ${index + 1}. ${title} (${time}${current}) [${session.id}]\n`,
);
});
sortedSessions.forEach((session, index) => {
const current = session.isCurrentSession ? ', current' : '';
const time = formatRelativeTime(session.lastUpdated);
const title =
session.displayName.length > 100
? session.displayName.slice(0, 97) + '...'
: session.displayName;
writeToStdout(
` ${index + 1}. ${title} (${time}${current}) [${session.id}]\n`,
);
});
}

export async function deleteSession(
Expand Down