Add /resume slash command to restore previous chat sessions

This commit is contained in:
Nate 2025-08-18 18:47:39 -07:00
parent 4d83e04dfd
commit 64500b87ff
9 changed files with 91 additions and 1 deletions

View file

@ -70,6 +70,11 @@ export const SYSTEM_SLASH_COMMANDS: SystemCommand[] = [
description: "Summarize chat history into a compact form",
category: "system",
},
{
name: "resume",
description: "Resume a previous chat session",
category: "system",
},
{
name: "exit",
description: "Exit the chat",

View file

@ -194,6 +194,13 @@ describe("handleSlashCommands", () => {
expect(result?.compact).toBe(true);
});
test("resume command should open session selector", async () => {
const result = await handleSlashCommands("/resume", mockAssistant);
expect(result).not.toBeNull();
expect(result?.openSessionSelector).toBe(true);
});
test("non-slash input should return null", async () => {
const result = await handleSlashCommands("regular input", mockAssistant);
expect(result).toBeNull();

View file

@ -197,6 +197,10 @@ const commandHandlers: Record<string, CommandHandler> = {
posthogService.capture("useSlashCommand", { name: "mcp" });
return { openMcpSelector: true };
},
resume: () => {
posthogService.capture("useSlashCommand", { name: "resume" });
return { openSessionSelector: true };
},
org: handleOrg,
};
@ -211,6 +215,7 @@ export async function handleSlashCommands(
openConfigSelector?: boolean;
openModelSelector?: boolean;
openMCPSelector?: boolean;
openSessionSelector?: boolean;
compact?: boolean;
} | null> {
// Only trigger slash commands if slash is the very first character

View file

@ -108,6 +108,7 @@ const TUIChat: React.FC<TUIChatProps> = ({
messages,
setMessages,
chatHistory,
setChatHistory,
isWaitingForResponse,
responseStartTime,
inputMode,
@ -128,6 +129,7 @@ const TUIChat: React.FC<TUIChatProps> = ({
onShowConfigSelector: () => navigateTo("config"),
onShowModelSelector: () => navigateTo("model"),
onShowMCPSelector: () => navigateTo("mcp"),
onShowSessionSelector: () => navigateTo("session"),
onLoginPrompt: handleLoginPrompt,
onReload: handleReload,
// Remote mode configuration
@ -146,6 +148,48 @@ const TUIChat: React.FC<TUIChatProps> = ({
const { handleOrganizationSelect, handleConfigSelect, handleModelSelect } =
useSelectors(configPath, setMessages, resetChatHistory);
// Session selection handler
const handleSessionSelect = async (sessionId: string) => {
try {
// Close the session selector
closeCurrentScreen();
// Import session functions
const { loadSessionById } = await import("../session.js");
// Load the session history
const sessionHistory = loadSessionById(sessionId);
if (!sessionHistory) {
console.error(`Session ${sessionId} could not be loaded.`);
return;
}
// Set the session ID so future operations use this session
process.env.CONTINUE_CLI_TEST_SESSION_ID = sessionId.replace(
"continue-cli-",
"",
);
// Directly set the chat history and messages to the loaded session
setChatHistory(sessionHistory);
// Convert chat history to display messages (exclude system messages)
const displayMessages = sessionHistory
.filter((msg) => msg.role !== "system")
.map((msg) => ({
role: msg.role,
content: msg.content as string,
}));
setMessages(displayMessages);
// Clear the intro message since we're now showing a resumed session
setShowIntroMessage(false);
} catch (error) {
console.error("Error loading session:", error);
}
};
// Determine if input should be disabled
// Allow input even when services are loading, but disable for UI overlays
const isInputDisabled =
@ -203,6 +247,7 @@ const TUIChat: React.FC<TUIChatProps> = ({
handleOrganizationSelect={handleOrganizationSelect}
handleConfigSelect={handleConfigSelect}
handleModelSelect={handleModelSelect}
handleSessionSelect={handleSessionSelect}
handleReload={handleReload}
closeCurrentScreen={closeCurrentScreen}
activePermissionRequest={activePermissionRequest}

View file

@ -1,12 +1,14 @@
import { Box, Text } from "ink";
import React from "react";
import { listSessions } from "../../session.js";
import { ConfigSelector } from "../ConfigSelector.js";
import type { NavigationScreen } from "../context/NavigationContext.js";
import { FreeTrialTransitionUI } from "../FreeTrialTransitionUI.js";
import { MCPSelector } from "../MCPSelector.js";
import { ModelSelector } from "../ModelSelector.js";
import { OrganizationSelector } from "../OrganizationSelector.js";
import { SessionSelector } from "../SessionSelector.js";
import type { ConfigOption, ModelOption } from "../types/selectorTypes.js";
import { UserInput } from "../UserInput.js";
@ -23,6 +25,7 @@ interface ScreenContentProps {
) => Promise<void>;
handleConfigSelect: (config: ConfigOption) => Promise<void>;
handleModelSelect: (model: ModelOption) => Promise<void>;
handleSessionSelect: (sessionId: string) => Promise<void>;
handleReload: () => Promise<void>;
closeCurrentScreen: () => void;
activePermissionRequest: any;
@ -49,6 +52,7 @@ export const ScreenContent: React.FC<ScreenContentProps> = ({
handleOrganizationSelect,
handleConfigSelect,
handleModelSelect,
handleSessionSelect,
handleReload,
closeCurrentScreen,
activePermissionRequest,
@ -122,6 +126,18 @@ export const ScreenContent: React.FC<ScreenContentProps> = ({
);
}
// Session selector
if (isScreenActive("session")) {
const sessions = listSessions(20);
return (
<SessionSelector
sessions={sessions}
onSelect={handleSessionSelect}
onExit={closeCurrentScreen}
/>
);
}
// Free trial transition UI
if (isScreenActive("free-trial")) {
return <FreeTrialTransitionUI onReload={handleReload} />;

View file

@ -17,7 +17,8 @@ export type NavigationScreen =
| "model" // Model selector
| "free-trial" // Free trial transition UI
| "login" // Login prompt
| "mcp"; // MCP selector
| "mcp" // MCP selector
| "session"; // Session selector
interface NavigationState {
currentScreen: NavigationScreen;

View file

@ -126,6 +126,7 @@ interface ProcessSlashCommandResultOptions {
onShowConfigSelector: () => void;
onShowModelSelector?: () => void;
onShowMCPSelector?: () => void;
onShowSessionSelector?: () => void;
}
/**
@ -140,6 +141,7 @@ export function processSlashCommandResult({
onShowConfigSelector,
onShowModelSelector,
onShowMCPSelector,
onShowSessionSelector,
}: ProcessSlashCommandResultOptions): string | null {
if (result.exit) {
exit();
@ -161,6 +163,11 @@ export function processSlashCommandResult({
return null;
}
if (result.openSessionSelector && onShowSessionSelector) {
onShowSessionSelector();
return null;
}
if (result.clear) {
const systemMessage = chatHistory.find((msg) => msg.role === "system");
const newHistory = systemMessage ? [systemMessage] : [];

View file

@ -46,6 +46,7 @@ export function useChat({
onShowConfigSelector,
onShowModelSelector,
onShowMCPSelector,
onShowSessionSelector,
onLoginPrompt: _onLoginPrompt,
isRemoteMode = false,
remoteUrl,
@ -197,6 +198,7 @@ export function useChat({
onShowConfigSelector,
onShowModelSelector,
onShowMCPSelector,
onShowSessionSelector,
});
if (newInput) {

View file

@ -15,6 +15,7 @@ export interface UseChatProps {
onShowConfigSelector: () => void;
onShowMCPSelector: () => void;
onShowModelSelector?: () => void;
onShowSessionSelector?: () => void;
onLoginPrompt?: (promptText: string) => Promise<string>;
onReload?: () => Promise<void>;
// Remote mode props
@ -58,5 +59,6 @@ export interface SlashCommandResult {
openConfigSelector?: boolean;
openModelSelector?: boolean;
openMcpSelector?: boolean;
openSessionSelector?: boolean;
compact?: boolean;
}