From e611cb6206be28d5d869c58d44ff52882e820296 Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 27 Aug 2025 11:24:57 -0700 Subject: [PATCH] fix: start a new session on /clear --- extensions/cli/src/session.test.ts | 49 +++++++++++++++++++ extensions/cli/src/session.ts | 21 ++++++++ .../cli/src/ui/hooks/useChat.clear.test.ts | 25 ++++++++++ .../cli/src/ui/hooks/useChat.helpers.ts | 6 ++- 4 files changed, 100 insertions(+), 1 deletion(-) diff --git a/extensions/cli/src/session.test.ts b/extensions/cli/src/session.test.ts index 35e042e74..0fb5f4525 100644 --- a/extensions/cli/src/session.test.ts +++ b/extensions/cli/src/session.test.ts @@ -2,6 +2,7 @@ import fs from "fs"; import os from "os"; import type { ChatHistoryItem, Session } from "core/index.js"; +import { v4 as uuidv4 } from "uuid"; import { beforeEach, describe, expect, it, vi } from "vitest"; import { @@ -11,6 +12,7 @@ import { hasSession, loadSession, saveSession, + startNewSession, updateSessionHistory, updateSessionTitle, } from "./session.js"; @@ -352,4 +354,51 @@ describe("SessionManager", () => { expect(session.history[0].message.content).toBe("Test message"); }); }); + + describe("startNewSession", () => { + it("should clear the current session and create a new one", () => { + const firstSession = createSession(); + const firstSessionId = firstSession.sessionId; + + vi.mocked(uuidv4).mockReturnValue("new-uuid-456" as any); + + const secondSession = startNewSession(); + + expect(secondSession.sessionId).toBe("new-uuid-456"); + expect(secondSession.sessionId).not.toBe(firstSessionId); + expect(secondSession.title).toBe("Untitled Session"); + expect(secondSession.history).toEqual([]); + }); + + it("should create a new session with provided history", () => { + const history: ChatHistoryItem[] = [ + { + message: { + role: "system", + content: "You are a helpful assistant", + }, + contextItems: [], + }, + ]; + + vi.mocked(uuidv4).mockReturnValue("new-uuid-789" as any); + + const session = startNewSession(history); + + expect(session.sessionId).toBe("new-uuid-789"); + expect(session.history).toBe(history); + }); + + it("should set the new session as current", () => { + const originalSession = createSession(); + + vi.mocked(uuidv4).mockReturnValue("new-session-id" as any); + + const newSession = startNewSession(); + const currentSession = getCurrentSession(); + + expect(currentSession).toBe(newSession); + expect(currentSession).not.toBe(originalSession); + }); + }); }); diff --git a/extensions/cli/src/session.ts b/extensions/cli/src/session.ts index 89fc6dbf9..ea1b3e174 100644 --- a/extensions/cli/src/session.ts +++ b/extensions/cli/src/session.ts @@ -347,3 +347,24 @@ export function updateSessionTitle(title: string): void { export function getCurrentSession(): Session { return SessionManager.getInstance().getCurrentSession(); } + +/** + * Start a new session with a new sessionId + */ +export function startNewSession(history: ChatHistoryItem[] = []): Session { + const manager = SessionManager.getInstance(); + + // Clear the current session from memory (don't delete the file) + manager.clear(); + + // Create a new session with a new sessionId + const newSession: Session = { + sessionId: uuidv4(), + title: "Untitled Session", + workspaceDirectory: process.cwd(), + history, + }; + + manager.setSession(newSession); + return newSession; +} diff --git a/extensions/cli/src/ui/hooks/useChat.clear.test.ts b/extensions/cli/src/ui/hooks/useChat.clear.test.ts index 91f7e62d4..39517aa2a 100644 --- a/extensions/cli/src/ui/hooks/useChat.clear.test.ts +++ b/extensions/cli/src/ui/hooks/useChat.clear.test.ts @@ -2,9 +2,18 @@ import { ChatCompletionMessageParam } from "openai/resources.mjs"; import { beforeEach, describe, expect, it, vi } from "vitest"; import { convertToUnifiedHistory } from "../../messageConversion.js"; +import { startNewSession } from "../../session.js"; import { processSlashCommandResult } from "./useChat.helpers.js"; + +// Mock the session module +vi.mock("../../session.js", () => ({ + loadSession: vi.fn(), + updateSessionHistory: vi.fn(), + startNewSession: vi.fn(), +})); + describe("useChat clear command", () => { let mockSetChatHistory: ReturnType; let mockExit: ReturnType; @@ -41,6 +50,11 @@ describe("useChat clear command", () => { // Verify that onClear was called expect(mockOnClear).toHaveBeenCalledOnce(); + // Verify that a new session was started with the system message + expect(startNewSession).toHaveBeenCalledWith([ + chatHistory[0], // The system message ChatHistoryItem + ]); + // Verify that chat history was reset (keeping only system message) expect(mockSetChatHistory).toHaveBeenCalledWith([ chatHistory[0], // The system message ChatHistoryItem @@ -81,6 +95,11 @@ describe("useChat clear command", () => { }); }).not.toThrow(); + // Verify that a new session was started with the system message + expect(startNewSession).toHaveBeenCalledWith([ + chatHistory[0], // The system message ChatHistoryItem + ]); + // Verify that history is still reset expect(mockSetChatHistory).toHaveBeenCalledWith([ chatHistory[0], // The system message ChatHistoryItem @@ -111,6 +130,9 @@ describe("useChat clear command", () => { onClear: mockOnClear, }); + // Verify that a new session was started with the system message + expect(startNewSession).toHaveBeenCalledWith([systemHistoryItem]); + // Should keep only the system message expect(mockSetChatHistory).toHaveBeenCalledWith([systemHistoryItem]); expect(mockOnClear).toHaveBeenCalledOnce(); @@ -130,6 +152,9 @@ describe("useChat clear command", () => { onClear: mockOnClear, }); + // Verify that a new session was started with empty history + expect(startNewSession).toHaveBeenCalledWith([]); + // Should set empty history when no system message exists expect(mockSetChatHistory).toHaveBeenCalledWith([]); expect(mockOnClear).toHaveBeenCalledOnce(); diff --git a/extensions/cli/src/ui/hooks/useChat.helpers.ts b/extensions/cli/src/ui/hooks/useChat.helpers.ts index c5cbdc4c5..8e65998ec 100644 --- a/extensions/cli/src/ui/hooks/useChat.helpers.ts +++ b/extensions/cli/src/ui/hooks/useChat.helpers.ts @@ -6,7 +6,7 @@ import { v4 as uuidv4 } from "uuid"; import { initializeChatHistory } from "../../commands/chat.js"; import { compactChatHistory } from "../../compaction.js"; -import { loadSession, updateSessionHistory } from "../../session.js"; +import { loadSession, updateSessionHistory, startNewSession } from "../../session.js"; import { posthogService } from "../../telemetry/posthogService.js"; import { telemetryService } from "../../telemetry/telemetryService.js"; import { formatError } from "../../util/formatError.js"; @@ -177,6 +177,10 @@ export function processSlashCommandResult({ (item) => item.message.role === "system", ); const newHistory = systemMessage ? [systemMessage] : []; + + // Start a new session with a new sessionId + startNewSession(newHistory); + setChatHistory(newHistory); // Reset intro message state to show it again after clearing