fix: start a new session on /clear

This commit is contained in:
Nate 2025-08-27 11:24:57 -07:00
parent 0a1187e223
commit e611cb6206
4 changed files with 100 additions and 1 deletions

View file

@ -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);
});
});
});

View file

@ -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;
}

View file

@ -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<typeof vi.fn>;
let mockExit: ReturnType<typeof vi.fn>;
@ -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();

View file

@ -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