Fix undo and redo by using initial commit hash for chat (#94)

This commit is contained in:
Will Chen
2025-05-06 12:15:42 -07:00
committed by GitHub
parent 390496f8f8
commit 20362d7b08
10 changed files with 434 additions and 101 deletions

View File

@@ -0,0 +1 @@
ALTER TABLE `chats` ADD `initial_commit_hash` text;

View File

@@ -0,0 +1,220 @@
{
"version": "6",
"dialect": "sqlite",
"id": "ceedb797-6aa3-4a50-b42f-bc85ee08b3df",
"prevId": "859942b1-88b8-4a16-b2d0-77c9ece76693",
"tables": {
"apps": {
"name": "apps",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"path": {
"name": "path",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"created_at": {
"name": "created_at",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "(unixepoch())"
},
"updated_at": {
"name": "updated_at",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "(unixepoch())"
},
"github_org": {
"name": "github_org",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"github_repo": {
"name": "github_repo",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"supabase_project_id": {
"name": "supabase_project_id",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {}
},
"chats": {
"name": "chats",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"app_id": {
"name": "app_id",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"title": {
"name": "title",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"initial_commit_hash": {
"name": "initial_commit_hash",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"created_at": {
"name": "created_at",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "(unixepoch())"
}
},
"indexes": {},
"foreignKeys": {
"chats_app_id_apps_id_fk": {
"name": "chats_app_id_apps_id_fk",
"tableFrom": "chats",
"tableTo": "apps",
"columnsFrom": [
"app_id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {}
},
"messages": {
"name": "messages",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"chat_id": {
"name": "chat_id",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"role": {
"name": "role",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"content": {
"name": "content",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"approval_state": {
"name": "approval_state",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"commit_hash": {
"name": "commit_hash",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"created_at": {
"name": "created_at",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "(unixepoch())"
}
},
"indexes": {},
"foreignKeys": {
"messages_chat_id_chats_id_fk": {
"name": "messages_chat_id_chats_id_fk",
"tableFrom": "messages",
"tableTo": "chats",
"columnsFrom": [
"chat_id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {}
}
},
"views": {},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
},
"internal": {
"indexes": {}
}
}

View File

@@ -29,6 +29,13 @@
"when": 1746209201530, "when": 1746209201530,
"tag": "0003_open_bucky", "tag": "0003_open_bucky",
"breakpoints": true "breakpoints": true
},
{
"idx": 4,
"version": "6",
"when": 1746556241557,
"tag": "0004_flawless_jigsaw",
"breakpoints": true
} }
] ]
} }

View File

@@ -11,7 +11,7 @@ import { Loader2, RefreshCw, Undo } from "lucide-react";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { useVersions } from "@/hooks/useVersions"; import { useVersions } from "@/hooks/useVersions";
import { selectedAppIdAtom } from "@/atoms/appAtoms"; import { selectedAppIdAtom } from "@/atoms/appAtoms";
import { showError, showSuccess } from "@/lib/toast"; import { showError, showSuccess, showWarning } from "@/lib/toast";
import { IpcClient } from "@/ipc/ipc_client"; import { IpcClient } from "@/ipc/ipc_client";
import { chatMessagesAtom } from "@/atoms/chatAtoms"; import { chatMessagesAtom } from "@/atoms/chatAtoms";
@@ -46,9 +46,10 @@ export const MessagesList = forwardRef<HTMLDivElement, MessagesListProps>(
{!isAnyProviderSetup() && <SetupBanner />} {!isAnyProviderSetup() && <SetupBanner />}
</div> </div>
)} )}
{messages.length > 3 && !isStreaming && ( {!isStreaming && (
<div className="flex max-w-3xl mx-auto gap-2"> <div className="flex max-w-3xl mx-auto gap-2">
{messages[messages.length - 1].role === "assistant" && {!!messages.length &&
messages[messages.length - 1].role === "assistant" &&
messages[messages.length - 1].commitHash && ( messages[messages.length - 1].commitHash && (
<Button <Button
variant="outline" variant="outline"
@@ -59,13 +60,10 @@ export const MessagesList = forwardRef<HTMLDivElement, MessagesListProps>(
console.error("No chat selected or app ID not available"); console.error("No chat selected or app ID not available");
return; return;
} }
if (versions.length < 2) {
showError("Cannot undo; no previous version");
return;
}
setIsUndoLoading(true); setIsUndoLoading(true);
try { try {
if (messages.length >= 3) {
const previousAssistantMessage = const previousAssistantMessage =
messages[messages.length - 3]; messages[messages.length - 3];
if ( if (
@@ -78,12 +76,33 @@ export const MessagesList = forwardRef<HTMLDivElement, MessagesListProps>(
await revertVersion({ await revertVersion({
versionId: previousAssistantMessage.commitHash, versionId: previousAssistantMessage.commitHash,
}); });
if (selectedChatId) {
const chat = await IpcClient.getInstance().getChat( const chat = await IpcClient.getInstance().getChat(
selectedChatId selectedChatId
); );
setMessages(chat.messages); setMessages(chat.messages);
} }
} else {
const chat = await IpcClient.getInstance().getChat(
selectedChatId
);
if (chat.initialCommitHash) {
await revertVersion({
versionId: chat.initialCommitHash,
});
const result =
await IpcClient.getInstance().deleteMessages(
selectedChatId
);
if (result.success) {
setMessages([]);
} else {
showError(result.error);
}
} else {
showWarning(
"No initial commit hash found for chat. Need to manually undo code changes"
);
}
} }
} catch (error) { } catch (error) {
console.error("Error during undo operation:", error); console.error("Error during undo operation:", error);
@@ -101,6 +120,7 @@ export const MessagesList = forwardRef<HTMLDivElement, MessagesListProps>(
Undo Undo
</Button> </Button>
)} )}
{!!messages.length && (
<Button <Button
variant="outline" variant="outline"
size="sm" size="sm"
@@ -116,30 +136,41 @@ export const MessagesList = forwardRef<HTMLDivElement, MessagesListProps>(
// The last message is usually an assistant, but it might not be. // The last message is usually an assistant, but it might not be.
const lastVersion = versions[0]; const lastVersion = versions[0];
const lastMessage = messages[messages.length - 1]; const lastMessage = messages[messages.length - 1];
let reverted = false; let shouldRedo = true;
if ( if (
lastVersion.oid === lastMessage.commitHash && lastVersion.oid === lastMessage.commitHash &&
lastMessage.role === "assistant" lastMessage.role === "assistant"
) { ) {
if (versions.length < 2) {
showError("Cannot retry message; no previous version");
return;
}
const previousAssistantMessage = const previousAssistantMessage =
messages[messages.length - 3]; messages[messages.length - 3];
if ( if (
previousAssistantMessage?.role === "assistant" && previousAssistantMessage?.role === "assistant" &&
previousAssistantMessage?.commitHash previousAssistantMessage?.commitHash
) { ) {
console.debug("Reverting to previous assistant version"); console.debug(
"Reverting to previous assistant version"
);
await revertVersion({ await revertVersion({
versionId: previousAssistantMessage.commitHash, versionId: previousAssistantMessage.commitHash,
}); });
reverted = true; shouldRedo = false;
} else { } else {
showSuccess( const chat = await IpcClient.getInstance().getChat(
"You will need to manually revert to an earlier revision" selectedChatId
); );
if (chat.initialCommitHash) {
console.debug(
"Reverting to initial commit hash",
chat.initialCommitHash
);
await revertVersion({
versionId: chat.initialCommitHash,
});
} else {
showWarning(
"No initial commit hash found for chat. Need to manually undo code changes"
);
}
} }
} }
@@ -151,9 +182,8 @@ export const MessagesList = forwardRef<HTMLDivElement, MessagesListProps>(
console.error("No user message found"); console.error("No user message found");
return; return;
} }
// If we reverted, we don't need to mark "redo" because // Need to do a redo, if we didn't delete the message from a revert.
// the old message has already been deleted. const redo = shouldRedo;
const redo = !reverted;
console.debug("Streaming message with redo", redo); console.debug("Streaming message with redo", redo);
streamMessage({ streamMessage({
@@ -176,6 +206,7 @@ export const MessagesList = forwardRef<HTMLDivElement, MessagesListProps>(
)} )}
Retry Retry
</Button> </Button>
)}
</div> </div>
)} )}
<div ref={messagesEndRef} /> <div ref={messagesEndRef} />

View File

@@ -23,6 +23,7 @@ export const chats = sqliteTable("chats", {
.notNull() .notNull()
.references(() => apps.id, { onDelete: "cascade" }), .references(() => apps.id, { onDelete: "cascade" }),
title: text("title"), title: text("title"),
initialCommitHash: text("initial_commit_hash"),
createdAt: integer("created_at", { mode: "timestamp" }) createdAt: integer("created_at", { mode: "timestamp" })
.notNull() .notNull()
.default(sql`(unixepoch())`), .default(sql`(unixepoch())`),

View File

@@ -189,12 +189,20 @@ export function registerAppHandlers() {
}); });
// Create initial commit // Create initial commit
await git.commit({ const commitHash = await git.commit({
fs: fs, fs: fs,
dir: fullAppPath, dir: fullAppPath,
message: "Init from react vite template", message: "Init from react vite template",
author: await getGitAuthor(), author: await getGitAuthor(),
}); });
// Update chat with initial commit hash
await db
.update(chats)
.set({
initialCommitHash: commitHash,
})
.where(eq(chats.id, chat.id));
} catch (error) { } catch (error) {
logger.error("Error in background app initialization:", error); logger.error("Error in background app initialization:", error);
} }

View File

@@ -1,19 +1,59 @@
import { ipcMain } from "electron"; import { ipcMain } from "electron";
import { db } from "../../db"; import { db } from "../../db";
import { chats } from "../../db/schema"; import { apps, chats, messages } from "../../db/schema";
import { desc, eq } from "drizzle-orm"; import { desc, eq } from "drizzle-orm";
import type { ChatSummary } from "../../lib/schemas"; import type { ChatSummary } from "../../lib/schemas";
import * as git from "isomorphic-git";
import * as fs from "fs";
import * as path from "path";
import log from "electron-log";
import { getDyadAppPath } from "../../paths/paths";
const logger = log.scope("chat_handlers");
export function registerChatHandlers() { export function registerChatHandlers() {
ipcMain.handle("create-chat", async (_, appId: number) => { ipcMain.handle("create-chat", async (_, appId: number) => {
// Get the app's path first
const app = await db.query.apps.findFirst({
where: eq(apps.id, appId),
columns: {
path: true,
},
});
if (!app) {
throw new Error("App not found");
}
let initialCommitHash = null;
try {
// Get the current git revision of main branch
initialCommitHash = await git.resolveRef({
fs,
dir: getDyadAppPath(app.path),
ref: "main",
});
} catch (error) {
logger.error("Error getting git revision:", error);
// Continue without the git revision
}
// Create a new chat // Create a new chat
const [chat] = await db const [chat] = await db
.insert(chats) .insert(chats)
.values({ .values({
appId, appId,
initialCommitHash,
}) })
.returning(); .returning();
logger.info(
"Created chat:",
chat.id,
"for app:",
appId,
"with initial commit hash:",
initialCommitHash
);
return chat.id; return chat.id;
}); });
@@ -70,7 +110,17 @@ export function registerChatHandlers() {
await db.delete(chats).where(eq(chats.id, chatId)); await db.delete(chats).where(eq(chats.id, chatId));
return { success: true }; return { success: true };
} catch (error) { } catch (error) {
console.error("Error deleting chat:", error); logger.error("Error deleting chat:", error);
return { success: false, error: (error as Error).message };
}
});
ipcMain.handle("delete-messages", async (_, chatId: number) => {
try {
await db.delete(messages).where(eq(messages.chatId, chatId));
return { success: true };
} catch (error) {
logger.error("Error deleting messages:", error);
return { success: false, error: (error as Error).message }; return { success: false, error: (error as Error).message };
} }
}); });

View File

@@ -330,12 +330,24 @@ export class IpcClient {
} }
} }
public async deleteChat(chatId: number): Promise<{ success: boolean }> { public async deleteChat(
chatId: number
): Promise<{ success: boolean; error?: string }> {
try { try {
const result = (await this.ipcRenderer.invoke("delete-chat", chatId)) as { const result = await this.ipcRenderer.invoke("delete-chat", chatId);
success: boolean; return result as { success: boolean; error?: string };
}; } catch (error) {
return result; showError(error);
throw error;
}
}
public async deleteMessages(
chatId: number
): Promise<{ success: boolean; error?: string }> {
try {
const result = await this.ipcRenderer.invoke("delete-messages", chatId);
return result as { success: boolean; error?: string };
} catch (error) { } catch (error) {
showError(error); showError(error);
throw error; throw error;

View File

@@ -54,6 +54,7 @@ export interface Chat {
id: number; id: number;
title: string; title: string;
messages: Message[]; messages: Message[];
initialCommitHash?: string | null;
} }
export interface App { export interface App {

View File

@@ -58,6 +58,8 @@ const validInvokeChannels = [
"window:get-platform", "window:get-platform",
"upload-to-signed-url", "upload-to-signed-url",
"delete-chat", "delete-chat",
"delete-messages",
"start-chat-stream",
] as const; ] as const;
// Add valid receive channels // Add valid receive channels