| 1 | import { render } from "ink"; |
| 2 | import React from "react"; |
| 3 | |
| 4 | import { loadSessionById, listSessions } from "../session.js"; |
| 5 | import { SessionSelector } from "../ui/SessionSelector.js"; |
| 6 | import { logger } from "../util/logger.js"; |
| 7 | |
| 8 | import { chat } from "./chat.js"; |
| 9 | |
| 10 | interface ListSessionsOptions { |
| 11 | format?: "json"; |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * Set a specific session ID for the current process |
| 16 | * This allows us to load a selected session as if it were the current session |
| 17 | */ |
| 18 | function setSessionId(sessionId: string): void { |
| 19 | // Use the same environment variable that getSessionId() checks |
| 20 | process.env.CONTINUE_CLI_TEST_SESSION_ID = sessionId.replace( |
| 21 | "continue-cli-", |
| 22 | "", |
| 23 | ); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * List recent chat sessions and allow selection |
| 28 | */ |
| 29 | export async function listSessionsCommand( |
| 30 | options: ListSessionsOptions = {}, |
| 31 | ): Promise<void> { |
| 32 | // Fetch more sessions than we might display so the UI can choose based on screen height |
| 33 | // But still limit for JSON output to keep it reasonable |
| 34 | const sessions = listSessions(options.format === "json" ? 10 : 20); |
| 35 | |
| 36 | // Handle JSON format output |
| 37 | if (options.format === "json") { |
| 38 | console.log( |
| 39 | JSON.stringify( |
| 40 | { |
| 41 | sessions: sessions.map((session) => ({ |
| 42 | id: session.sessionId, |
| 43 | timestamp: session.dateCreated, |
| 44 | workspaceDirectory: session.workspaceDirectory, |
| 45 | title: session.title, |
| 46 | firstUserMessage: session.firstUserMessage, |
| 47 | })), |
| 48 | }, |
| 49 | null, |
| 50 | 2, |
| 51 | ), |
| 52 | ); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | // Handle empty sessions case |
| 57 | if (sessions.length === 0) { |
| 58 | console.log( |
| 59 | "No previous sessions found. Start a new conversation with: cn", |
| 60 | ); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | // Start TUI selector |
| 65 | return new Promise<void>((resolve, reject) => { |
| 66 | const handleSelect = async (sessionId: string) => { |
| 67 | try { |
| 68 | app.unmount(); |
| 69 | |
| 70 | // Verify the session exists before trying to load it |
| 71 | const sessionHistory = loadSessionById(sessionId); |
| 72 | if (!sessionHistory) { |
| 73 | console.error(`Session ${sessionId} could not be loaded.`); |
| 74 | resolve(); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | logger.info(`Loading session: ${sessionId}`); |
| 79 | |
| 80 | // Set the session ID so that when chat() runs, it will load this session |
| 81 | setSessionId(sessionId); |
| 82 | |
| 83 | // Start chat with resume flag to load the selected session |
| 84 | await chat(undefined, { |
| 85 | resume: true, |
| 86 | headless: false, |
| 87 | }); |
| 88 | |
| 89 | resolve(); |
| 90 | } catch (error) { |
| 91 | logger.error("Error loading session:", error); |
| 92 | reject(error); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | const handleExit = () => { |
| 97 | app.unmount(); |
| 98 | resolve(); |
| 99 | }; |
| 100 | |
| 101 | const app = render( |
| 102 | React.createElement(SessionSelector, { |
| 103 | sessions, |
| 104 | onSelect: handleSelect, |
| 105 | onExit: handleExit, |
| 106 | }), |
| 107 | ); |
| 108 | }); |
| 109 | } |