🚧 history

This commit is contained in:
Nate Sesti 2023-12-09 11:37:27 -08:00
parent c4371c2121
commit 7edfd3d651
7 changed files with 64 additions and 41 deletions

View file

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

View file

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

View file

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

View file

@ -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) => {

View file

@ -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<PersistedSessionInfo> {
return await ideRequest("loadSession", { sessionId });
return await ideRequest("loadSession", sessionId);
}
return { getHistory, saveSession, deleteSession, loadSession };

View file

@ -364,7 +364,7 @@ function GUI(props: GUIProps) {
return () => {
window.removeEventListener("message", handler);
};
}, []);
}, [saveSession]);
useEffect(() => {
const eventListener = (event: any) => {

View file

@ -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 (
<td
@ -79,14 +79,10 @@ function TableRow({
// Save current session
saveSession();
// Load new session
const response = await fetch(
`${apiUrl}/sessions/${session.sessionId}`
console.log(session);
const json: PersistedSessionInfo = await loadSession(
session.sessionId
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const json: PersistedSessionInfo = await response.json();
dispatch(newSession(json));
navigate("/");
}}