continue-custom/core/util/history.ts
Nate Sesti 53b61288ec
🎨 Refactor core (#1281)
* 🎨 refactor out react context

* 🎨 refactor local storage

* 🎨 refactor IdeMessenger

* 🔥 removing unused files

* 🚚 move scripts

* ✅ setup VSCode integration testing

* 🎨 fix test-related stuff

* 🚧 testing

* 🎨 tweak terminal cmd+L

* 🧑‍💻 install biome in core

* 🎨 run biome check on core

* 🎨 run biome check in vscode

* 🎨 run biome check in binary

* 🧑‍💻 global biome config

* 🎨 run biome check on core

* 🎨 run biome check in vscode

* 🎨 fix a few biome warnings

* 🚧 WIP on protocols

* 🚧 progress, organizing, about to remove webviewCore protocol

* 🚧 now running

* 🚧 move indexing code to core

* 🚧 WIP on JetBrains indexing implementation

* 🎨 finish listDir

* 🚧 gui fixes

* 🏷️ update IMessenger.on return type

* 🐛 fix jetbrains types + IDE detection

* 🧑‍💻 set up debugging for binary with TcpMessenger

* 👷 fix prepackage.js

* 🧑‍💻 turn off debug mode for intellij

* 🐛 merge fixes

* 🐛 fixes after refactor

* 🐛 merge fixes

* 💄 increase font size in JB

* 🔀 merge changes

* 🩹 small fixes

* 🐛 fix jetbrains build

* 🩹 more jetbrains fixes

* 🎨 jetbrains improvements

* 🐛 fix lancedb prob in jetbrains build

* 👷 update jetbrains build process

* ✨ intellij problems context provider

* 👷 add script utils file

* 💚 fix jetbrains build regression

* 🎨 dynamic import transformers.js

* 🩹 small jetbrains updates

* ✨ folder context provider in jetbrains

* 🎨 many more jetbrains improvements

* 🔀 merge fixes

* ⚡️ small improvements

* 🎨 tell users transformers.js not supported

* 🎨 trial updates

* 📝 update jetbrains readmej

* ⚡️ only use smaller context for local autocomplete models

* ⚡️ improve bracket filter

* ✨ global .continue/.prompts folder

* 💄 improved model setup process

* 🥅 display VSCode req errs
2024-05-24 19:38:13 -07:00

137 lines
4.2 KiB
TypeScript

import * as fs from "fs";
import { PersistedSessionInfo, SessionInfo } from "../index.js";
import { ListHistoryOptions } from "../protocol/core.js";
import { getSessionFilePath, getSessionsListPath } from "./paths.js";
class HistoryManager {
list(options: ListHistoryOptions): SessionInfo[] {
const filepath = getSessionsListPath();
if (!fs.existsSync(filepath)) {
return [];
}
const content = fs.readFileSync(filepath, "utf8");
let sessions = JSON.parse(content).filter((session: any) => {
// Filter out old format
return typeof session.session_id !== "string";
});
// Apply limit and offset
if (options.limit) {
const offset = options.offset || 0;
sessions = sessions.slice(offset, offset + options.limit);
}
return sessions;
}
delete(sessionId: string) {
// Delete a session
const sessionFile = getSessionFilePath(sessionId);
if (!fs.existsSync(sessionFile)) {
throw new Error(`Session file ${sessionFile} does not exist`);
}
fs.unlinkSync(sessionFile);
// Read and update the sessions list
const sessionsListFile = getSessionsListPath();
const sessionsListRaw = fs.readFileSync(sessionsListFile, "utf-8");
let sessionsList: SessionInfo[];
try {
sessionsList = JSON.parse(sessionsListRaw);
} catch (error) {
throw new Error(
`It looks like there is a JSON formatting error in your sessions.json file (${sessionsListFile}). Please fix this before creating a new session.`,
);
}
sessionsList = sessionsList.filter(
(session) => session.sessionId !== sessionId,
);
fs.writeFileSync(sessionsListFile, JSON.stringify(sessionsList));
}
load(sessionId: string): PersistedSessionInfo {
try {
const sessionFile = getSessionFilePath(sessionId);
if (!fs.existsSync(sessionFile)) {
throw new Error(`Session file ${sessionFile} does not exist`);
}
const session: PersistedSessionInfo = JSON.parse(
fs.readFileSync(sessionFile, "utf8"),
);
session.sessionId = sessionId;
return session;
} catch (e) {
console.log(`Error migrating session: ${e}`);
return {
history: [],
title: "Failed to load session",
workspaceDirectory: "",
sessionId: sessionId,
};
}
}
save(session: PersistedSessionInfo) {
// Save the main session json file
fs.writeFileSync(
getSessionFilePath(session.sessionId),
JSON.stringify(session),
);
// Read and update the sessions list
const sessionsListFilePath = getSessionsListPath();
try {
const rawSessionsList = fs.readFileSync(sessionsListFilePath, "utf-8");
let sessionsList: SessionInfo[];
try {
sessionsList = JSON.parse(rawSessionsList);
} catch (e) {
if (rawSessionsList.trim() === "") {
fs.writeFileSync(sessionsListFilePath, JSON.stringify([]));
sessionsList = [];
} else {
throw e;
}
}
let found = false;
for (const sessionInfo of sessionsList) {
if (sessionInfo.sessionId === session.sessionId) {
sessionInfo.title = session.title;
sessionInfo.workspaceDirectory = session.workspaceDirectory;
sessionInfo.dateCreated = String(Date.now());
found = true;
break;
}
}
if (!found) {
const sessionInfo: SessionInfo = {
sessionId: session.sessionId,
title: session.title,
dateCreated: String(Date.now()),
workspaceDirectory: session.workspaceDirectory,
};
sessionsList.push(sessionInfo);
}
fs.writeFileSync(sessionsListFilePath, JSON.stringify(sessionsList));
} catch (error) {
if (error instanceof SyntaxError) {
throw new Error(
`It looks like there is a JSON formatting error in your sessions.json file (${sessionsListFilePath}). Please fix this before creating a new session.`,
);
}
throw new Error(
`It looks like there is a validation error in your sessions.json file (${sessionsListFilePath}). Please fix this before creating a new session. Error: ${error}`,
);
}
}
}
const historyManager = new HistoryManager();
export default historyManager;