| 1 | import { render } from "ink"; |
| 2 | import React from "react"; |
| 3 | |
| 4 | import { getAccessToken, loadAuthConfig } from "../auth/workos.js"; |
| 5 | import { env } from "../env.js"; |
| 6 | import { listSessions, loadSessionById } from "../session.js"; |
| 7 | import { SessionSelector } from "../ui/SessionSelector.js"; |
| 8 | import { logger } from "../util/logger.js"; |
| 9 | |
| 10 | import { chat } from "./chat.js"; |
| 11 | import { remote } from "./remote.js"; |
| 12 | |
| 13 | interface ListSessionsOptions { |
| 14 | format?: "json"; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Set a specific session ID for the current process |
| 19 | * This allows us to load a selected session as if it were the current session |
| 20 | */ |
| 21 | function setSessionId(sessionId: string): void { |
| 22 | // Use the same environment variable that getSessionId() checks |
| 23 | process.env.CONTINUE_CLI_TEST_SESSION_ID = sessionId.replace( |
| 24 | "continue-cli-", |
| 25 | "", |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | export async function getTunnelForAgent(agentId: string): Promise<string> { |
| 30 | const authConfig = loadAuthConfig(); |
| 31 | const accessToken = getAccessToken(authConfig); |
| 32 | |
| 33 | const resp = await fetch( |
| 34 | new URL(`agents/${encodeURIComponent(agentId)}/tunnel`, env.apiBase), |
| 35 | { |
| 36 | method: "POST", |
| 37 | headers: { |
| 38 | "Content-Type": "application/json", |
| 39 | Authorization: `Bearer ${accessToken}`, |
| 40 | }, |
| 41 | }, |
| 42 | ); |
| 43 | if (!resp.ok) { |
| 44 | throw new Error( |
| 45 | `Failed to get tunnel for agent ${agentId}: ${await resp.text()}`, |
| 46 | ); |
| 47 | } |
| 48 | const data = await resp.json(); |
| 49 | return data.url; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * List recent chat sessions and allow selection |
| 54 | */ |
| 55 | export async function listSessionsCommand( |
| 56 | options: ListSessionsOptions = {}, |
| 57 | ): Promise<void> { |
| 58 | // Handle JSON format output first |
| 59 | if (options.format === "json") { |
| 60 | const sessions = await listSessions(); |
| 61 | console.log( |
| 62 | JSON.stringify( |
| 63 | { |
| 64 | sessions: sessions.map((session) => ({ |
| 65 | id: session.sessionId, |
| 66 | timestamp: session.dateCreated, |
| 67 | workspaceDirectory: session.workspaceDirectory, |
| 68 | title: session.title, |
| 69 | firstUserMessage: session.firstUserMessage, |
| 70 | isRemote: session.isRemote, |
| 71 | remoteId: session.remoteId, |
| 72 | })), |
| 73 | }, |
| 74 | null, |
| 75 | 2, |
| 76 | ), |
| 77 | ); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | // For TUI mode, fetch more sessions than we might display so the UI can choose based on screen height |
| 82 | const sessions = await listSessions(); |
| 83 | |
| 84 | // Handle empty sessions case |
| 85 | if (sessions.length === 0) { |
| 86 | console.log( |
| 87 | "No previous sessions found. Start a new conversation with: cn", |
| 88 | ); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | // Start TUI selector |
| 93 | return new Promise<void>((resolve, reject) => { |
| 94 | const handleSelect = async (sessionId: string) => { |
| 95 | try { |
| 96 | app.unmount(); |
| 97 | |
| 98 | // Find the selected session to check if it's remote |
| 99 | const selectedSession = sessions.find((s) => s.sessionId === sessionId); |
| 100 | |
| 101 | if (selectedSession?.isRemote && selectedSession.remoteId) { |
| 102 | // Handle remote session - use the remote command with the agent URL |
| 103 | logger.info(`Opening remote session: ${selectedSession.remoteId}`); |
| 104 | |
| 105 | const tunnelUrl = await getTunnelForAgent(selectedSession.remoteId); |
| 106 | |
| 107 | await remote(undefined, { url: tunnelUrl }); |
| 108 | } else { |
| 109 | // Handle local session |
| 110 | const sessionHistory = loadSessionById(sessionId); |
| 111 | if (!sessionHistory) { |
| 112 | logger.error(`Session ${sessionId} could not be loaded.`); |
| 113 | resolve(); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | logger.info(`Loading session: ${sessionId}`); |
| 118 | |
| 119 | // Set the session ID so that when chat() runs, it will load this session |
| 120 | setSessionId(sessionId); |
| 121 | |
| 122 | // Start chat with resume flag to load the selected session |
| 123 | await chat(undefined, { |
| 124 | resume: true, |
| 125 | headless: false, |
| 126 | }); |
| 127 | } |
| 128 | |
| 129 | resolve(); |
| 130 | } catch (error) { |
| 131 | logger.error("Error loading session:", error); |
| 132 | reject(error); |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | const handleExit = () => { |
| 137 | app.unmount(); |
| 138 | resolve(); |
| 139 | }; |
| 140 | |
| 141 | const app = render( |
| 142 | React.createElement(SessionSelector, { |
| 143 | sessions, |
| 144 | onSelect: handleSelect, |
| 145 | onExit: handleExit, |
| 146 | }), |
| 147 | ); |
| 148 | }); |
| 149 | } |