diff --git a/extensions/vscode/src/activation/environmentSetup.ts b/extensions/vscode/src/activation/environmentSetup.ts index 0f2623b7e..626dd531f 100644 --- a/extensions/vscode/src/activation/environmentSetup.ts +++ b/extensions/vscode/src/activation/environmentSetup.ts @@ -1,17 +1,16 @@ -import { getExtensionUri, getUniqueId } from "../util/vscode"; -import { promisify } from "util"; -import { exec as execCb } from "child_process"; -import { spawn } from "child_process"; -import * as path from "path"; -import * as fs from "fs"; -import { getContinueServerUrl } from "../bridge"; -import fetch from "node-fetch"; -import * as vscode from "vscode"; -import * as os from "os"; +import { exec as execCb, spawn } from "child_process"; import fkill from "fkill"; +import * as fs from "fs"; +import fetch from "node-fetch"; +import * as os from "os"; +import * as path from "path"; import { finished } from "stream/promises"; -import request = require("request"); +import { promisify } from "util"; +import * as vscode from "vscode"; +import { getContinueServerUrl } from "../bridge"; import { capture } from "../extension"; +import { getExtensionUri, getUniqueId } from "../util/vscode"; +import request = require("request"); const exec = promisify(execCb); diff --git a/extensions/vscode/src/debugPanel.ts b/extensions/vscode/src/debugPanel.ts index c60061735..ce44d1a8f 100644 --- a/extensions/vscode/src/debugPanel.ts +++ b/extensions/vscode/src/debugPanel.ts @@ -3,6 +3,7 @@ import * as vscode from "vscode"; import { FileEdit } from "../schema/FileEdit"; import { ideProtocolClient, windowId } from "./activation/activate"; import { getContinueServerUrl } from "./bridge"; +import historyManager from "./history"; import VsCodeIde from "./ideProtocol"; import { getExtensionUri, getNonce, getUniqueId } from "./util/vscode"; @@ -233,6 +234,8 @@ export function getSidebarContent( break; } + + // IDE case "getDiff": { respond(await ide.getDiff()); break; @@ -273,6 +276,25 @@ export function getSidebarContent( respond(await ide.runCommand(data.command)); break; } + // History + case "history": { + respond(historyManager.list()); + break; + } + case "saveSession": { + historyManager.save(data.message); + respond({}); + break; + } + case "deleteSession": { + historyManager.delete(data.message); + respond({}); + break; + } + case "loadSession": { + respond(historyManager.load(data.message)); + break; + } } }); diff --git a/extensions/vscode/src/history.ts b/extensions/vscode/src/history.ts index cf11a416e..49d6a871a 100644 --- a/extensions/vscode/src/history.ts +++ b/extensions/vscode/src/history.ts @@ -1,9 +1,9 @@ +import { PersistedSessionInfo, SessionInfo } from "core/types"; +import * as fs from "fs"; import { getSessionFilePath, getSessionsListPath, } from "./activation/environmentSetup"; -import * as fs from "fs"; -import { PersistedSessionInfo, SessionInfo } from "core/types"; class HistoryManager { list(): PersistedSessionInfo[] { @@ -12,7 +12,10 @@ class HistoryManager { return []; } const content = fs.readFileSync(filepath, "utf8"); - const sessions = JSON.parse(content); + const sessions = JSON.parse(content).filter((session: any) => { + // Filter out old format + return typeof session.session_id !== "string"; + }); return sessions; } @@ -113,3 +116,7 @@ class HistoryManager { } } } + +const historyManager = new HistoryManager(); + +export default historyManager; diff --git a/gui/src/components/Layout.tsx b/gui/src/components/Layout.tsx index efa7d0102..06476ad33 100644 --- a/gui/src/components/Layout.tsx +++ b/gui/src/components/Layout.tsx @@ -8,7 +8,7 @@ import { useDispatch, useSelector } from "react-redux"; import { Outlet, useLocation, useNavigate } from "react-router-dom"; import styled from "styled-components"; import { defaultBorderRadius, secondaryDark, vscForeground } from "."; -import { newSession } from "../redux/slices/stateSlice"; +import useHistory from "../hooks/useHistory"; import { setBottomMessage, setBottomMessageCloseTimeout, @@ -152,20 +152,7 @@ const Layout = () => { }; }, [timeline]); - useEffect(() => { - // Override the VS Code shortcut so that highlighted but unfocused code isn't added - const handleKeyDown = (event: any) => { - if (event.metaKey && event.code === "KeyM") { - dispatch(newSession()); - } - }; - - window.addEventListener("keydown", handleKeyDown); - - return () => { - window.removeEventListener("keydown", handleKeyDown); - }; - }, []); + const { saveSession } = useHistory(); useEffect(() => { const handler = (event: any) => { diff --git a/gui/src/hooks/useHistory.tsx b/gui/src/hooks/useHistory.tsx index 9d3c58144..f1f30f6bd 100644 --- a/gui/src/hooks/useHistory.tsx +++ b/gui/src/hooks/useHistory.tsx @@ -3,6 +3,13 @@ import { PersistedSessionInfo, SessionInfo } from "core/types"; import { useSelector } from "react-redux"; import { RootStore } from "../redux/store"; +function truncateText(text: string, maxLength: number) { + if (text.length > maxLength) { + return text.slice(0, maxLength - 3) + "..."; + } + return text; +} + function useHistory() { const state = useSelector((state: RootStore) => state.state); @@ -11,9 +18,14 @@ function useHistory() { } async function saveSession() { + console.log("HIStory", state); + if (state.history.length === 0) return; const sessionInfo: PersistedSessionInfo = { history: state.history, - title: state.title, + title: + state.title === "New Session" + ? truncateText(state.history[0].message.content, 50) + : state.title, sessionId: state.sessionId, workspaceDirectory: (window as any).workspacePaths?.[0] || "", }; @@ -21,11 +33,11 @@ function useHistory() { } async function deleteSession(sessionId: string) { - return await ideRequest("deleteSession", { sessionId }); + return await ideRequest("deleteSession", sessionId); } async function loadSession(sessionId: string): Promise { - return await ideRequest("loadSession", { sessionId }); + return await ideRequest("loadSession", sessionId); } return { getHistory, saveSession, deleteSession, loadSession }; diff --git a/gui/src/pages/gui.tsx b/gui/src/pages/gui.tsx index 4595238b4..d051f71f7 100644 --- a/gui/src/pages/gui.tsx +++ b/gui/src/pages/gui.tsx @@ -364,7 +364,7 @@ function GUI(props: GUIProps) { return () => { window.removeEventListener("message", handler); }; - }, []); + }, [saveSession]); useEffect(() => { const eventListener = (event: any) => { diff --git a/gui/src/pages/history.tsx b/gui/src/pages/history.tsx index 98045f4b6..75ae2fee7 100644 --- a/gui/src/pages/history.tsx +++ b/gui/src/pages/history.tsx @@ -66,7 +66,7 @@ function TableRow({ const workspacePaths = (window as any).workspacePaths || [""]; const [hovered, setHovered] = useState(false); - const { saveSession, deleteSession } = useHistory(); + const { saveSession, deleteSession, loadSession } = useHistory(); return (