feat(cli): add JSON output support for list-sessions#24690#24711
feat(cli): add JSON output support for list-sessions#24690#24711Khalodddd wants to merge 2 commits intogoogle-gemini:mainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the list-sessions command by introducing JSON output support, enabling better integration with external tools and scripts. It ensures that the output remains clean by conditionally suppressing auxiliary summary logs when JSON mode is active and standardizes the session sorting logic for consistency. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces JSON output support for the listSessions command and prevents summary generation when JSON is requested. The review feedback identifies a redundant sorting operation that can be removed and suggests using grapheme-aware string truncation to avoid issues with multi-byte Unicode characters in the terminal output.
packages/cli/src/utils/sessions.ts
Outdated
| const sortedSessions = sessions.sort( | ||
| (a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime(), | ||
| ); |
There was a problem hiding this comment.
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;
Summary
This PR resolves #24690 by adding support for the
--output-format jsonflag to thelist-sessionscommand. Previously, the command would only output human-readable text, regardless of the format flag, making it difficult to use session data in automated scripts or third-party integrations.Problem
The
listSessionsfunction was previously hardcoded for string-based terminal output only. Additionally, thegenerateSummaryfunction was called unconditionally, which often produced background logs (e.g., "Generating summary...") that would corrupt the JSON output if a user tried to pipe the data to another tool likejq.Details
packages/cli/src/utils/sessions.tsto detectOutputFormat.JSONfrom the config.generateSummarycall in a conditional check. It is now skipped when JSON is requested, ensuring a clean data stream tostdout.sortedSessionsconstant. This ensures that the order of sessions (sorted bystartTime) is identical in both text and JSON modes.JSON.stringify(sortedSessions, null, 2)for standardized, pretty-printed JSON.Related Issues
Fixes #24690
How to Validate
See attached Image for validation