Undo chat history (#74)

This commit is contained in:
Will Chen
2025-05-02 13:47:59 -07:00
committed by GitHub
parent 4fb4e49c5d
commit 3529627172
11 changed files with 309 additions and 17 deletions

View File

@@ -1,7 +1,7 @@
import { ipcMain } from "electron";
import { db, getDatabasePath } from "../../db";
import { apps, chats } from "../../db/schema";
import { desc, eq } from "drizzle-orm";
import { apps, chats, messages } from "../../db/schema";
import { desc, eq, and, gte, sql, gt } from "drizzle-orm";
import type {
App,
CreateAppParams,
@@ -572,6 +572,44 @@ export function registerAppHandlers() {
author: await getGitAuthor(),
});
// Find the chat and message associated with the commit hash
const messageWithCommit = await db.query.messages.findFirst({
where: eq(messages.commitHash, previousVersionId),
with: {
chat: true,
},
});
// If we found a message with this commit hash, delete all subsequent messages (but keep this message)
if (messageWithCommit) {
const chatId = messageWithCommit.chatId;
// Find all messages in this chat with IDs > the one with our commit hash
const messagesToDelete = await db.query.messages.findMany({
where: and(
eq(messages.chatId, chatId),
gt(messages.id, messageWithCommit.id)
),
orderBy: desc(messages.id),
});
logger.log(
`Deleting ${messagesToDelete.length} messages after commit ${previousVersionId} from chat ${chatId}`
);
// Delete the messages
if (messagesToDelete.length > 0) {
await db
.delete(messages)
.where(
and(
eq(messages.chatId, chatId),
gt(messages.id, messageWithCommit.id)
)
);
}
}
return { success: true };
} catch (error: any) {
logger.error(