Fix undo and redo by using initial commit hash for chat (#94)
This commit is contained in:
1
drizzle/0004_flawless_jigsaw.sql
Normal file
1
drizzle/0004_flawless_jigsaw.sql
Normal file
@@ -0,0 +1 @@
|
||||
ALTER TABLE `chats` ADD `initial_commit_hash` text;
|
||||
220
drizzle/meta/0004_snapshot.json
Normal file
220
drizzle/meta/0004_snapshot.json
Normal 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": {}
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,13 @@
|
||||
"when": 1746209201530,
|
||||
"tag": "0003_open_bucky",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 4,
|
||||
"version": "6",
|
||||
"when": 1746556241557,
|
||||
"tag": "0004_flawless_jigsaw",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import { Loader2, RefreshCw, Undo } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { useVersions } from "@/hooks/useVersions";
|
||||
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 { chatMessagesAtom } from "@/atoms/chatAtoms";
|
||||
|
||||
@@ -46,9 +46,10 @@ export const MessagesList = forwardRef<HTMLDivElement, MessagesListProps>(
|
||||
{!isAnyProviderSetup() && <SetupBanner />}
|
||||
</div>
|
||||
)}
|
||||
{messages.length > 3 && !isStreaming && (
|
||||
{!isStreaming && (
|
||||
<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 && (
|
||||
<Button
|
||||
variant="outline"
|
||||
@@ -59,31 +60,49 @@ export const MessagesList = forwardRef<HTMLDivElement, MessagesListProps>(
|
||||
console.error("No chat selected or app ID not available");
|
||||
return;
|
||||
}
|
||||
if (versions.length < 2) {
|
||||
showError("Cannot undo; no previous version");
|
||||
return;
|
||||
}
|
||||
|
||||
setIsUndoLoading(true);
|
||||
try {
|
||||
const previousAssistantMessage =
|
||||
messages[messages.length - 3];
|
||||
if (
|
||||
previousAssistantMessage?.role === "assistant" &&
|
||||
previousAssistantMessage?.commitHash
|
||||
) {
|
||||
console.debug(
|
||||
"Reverting to previous assistant version"
|
||||
);
|
||||
await revertVersion({
|
||||
versionId: previousAssistantMessage.commitHash,
|
||||
});
|
||||
if (selectedChatId) {
|
||||
if (messages.length >= 3) {
|
||||
const previousAssistantMessage =
|
||||
messages[messages.length - 3];
|
||||
if (
|
||||
previousAssistantMessage?.role === "assistant" &&
|
||||
previousAssistantMessage?.commitHash
|
||||
) {
|
||||
console.debug(
|
||||
"Reverting to previous assistant version"
|
||||
);
|
||||
await revertVersion({
|
||||
versionId: previousAssistantMessage.commitHash,
|
||||
});
|
||||
const chat = await IpcClient.getInstance().getChat(
|
||||
selectedChatId
|
||||
);
|
||||
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) {
|
||||
console.error("Error during undo operation:", error);
|
||||
@@ -101,81 +120,93 @@ export const MessagesList = forwardRef<HTMLDivElement, MessagesListProps>(
|
||||
Undo
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
disabled={isRetryLoading}
|
||||
onClick={async () => {
|
||||
if (!selectedChatId) {
|
||||
console.error("No chat selected");
|
||||
return;
|
||||
}
|
||||
|
||||
setIsRetryLoading(true);
|
||||
try {
|
||||
// The last message is usually an assistant, but it might not be.
|
||||
const lastVersion = versions[0];
|
||||
const lastMessage = messages[messages.length - 1];
|
||||
let reverted = false;
|
||||
if (
|
||||
lastVersion.oid === lastMessage.commitHash &&
|
||||
lastMessage.role === "assistant"
|
||||
) {
|
||||
if (versions.length < 2) {
|
||||
showError("Cannot retry message; no previous version");
|
||||
return;
|
||||
}
|
||||
const previousAssistantMessage =
|
||||
messages[messages.length - 3];
|
||||
if (
|
||||
previousAssistantMessage?.role === "assistant" &&
|
||||
previousAssistantMessage?.commitHash
|
||||
) {
|
||||
console.debug("Reverting to previous assistant version");
|
||||
await revertVersion({
|
||||
versionId: previousAssistantMessage.commitHash,
|
||||
});
|
||||
reverted = true;
|
||||
} else {
|
||||
showSuccess(
|
||||
"You will need to manually revert to an earlier revision"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Find the last user message
|
||||
const lastUserMessage = [...messages]
|
||||
.reverse()
|
||||
.find((message) => message.role === "user");
|
||||
if (!lastUserMessage) {
|
||||
console.error("No user message found");
|
||||
{!!messages.length && (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
disabled={isRetryLoading}
|
||||
onClick={async () => {
|
||||
if (!selectedChatId) {
|
||||
console.error("No chat selected");
|
||||
return;
|
||||
}
|
||||
// If we reverted, we don't need to mark "redo" because
|
||||
// the old message has already been deleted.
|
||||
const redo = !reverted;
|
||||
console.debug("Streaming message with redo", redo);
|
||||
|
||||
streamMessage({
|
||||
prompt: lastUserMessage.content,
|
||||
chatId: selectedChatId,
|
||||
redo,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error during retry operation:", error);
|
||||
showError("Failed to retry message");
|
||||
} finally {
|
||||
setIsRetryLoading(false);
|
||||
}
|
||||
}}
|
||||
>
|
||||
{isRetryLoading ? (
|
||||
<Loader2 size={16} className="mr-1 animate-spin" />
|
||||
) : (
|
||||
<RefreshCw size={16} />
|
||||
)}
|
||||
Retry
|
||||
</Button>
|
||||
setIsRetryLoading(true);
|
||||
try {
|
||||
// The last message is usually an assistant, but it might not be.
|
||||
const lastVersion = versions[0];
|
||||
const lastMessage = messages[messages.length - 1];
|
||||
let shouldRedo = true;
|
||||
if (
|
||||
lastVersion.oid === lastMessage.commitHash &&
|
||||
lastMessage.role === "assistant"
|
||||
) {
|
||||
const previousAssistantMessage =
|
||||
messages[messages.length - 3];
|
||||
if (
|
||||
previousAssistantMessage?.role === "assistant" &&
|
||||
previousAssistantMessage?.commitHash
|
||||
) {
|
||||
console.debug(
|
||||
"Reverting to previous assistant version"
|
||||
);
|
||||
await revertVersion({
|
||||
versionId: previousAssistantMessage.commitHash,
|
||||
});
|
||||
shouldRedo = false;
|
||||
} else {
|
||||
const chat = await IpcClient.getInstance().getChat(
|
||||
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"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Find the last user message
|
||||
const lastUserMessage = [...messages]
|
||||
.reverse()
|
||||
.find((message) => message.role === "user");
|
||||
if (!lastUserMessage) {
|
||||
console.error("No user message found");
|
||||
return;
|
||||
}
|
||||
// Need to do a redo, if we didn't delete the message from a revert.
|
||||
const redo = shouldRedo;
|
||||
console.debug("Streaming message with redo", redo);
|
||||
|
||||
streamMessage({
|
||||
prompt: lastUserMessage.content,
|
||||
chatId: selectedChatId,
|
||||
redo,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error during retry operation:", error);
|
||||
showError("Failed to retry message");
|
||||
} finally {
|
||||
setIsRetryLoading(false);
|
||||
}
|
||||
}}
|
||||
>
|
||||
{isRetryLoading ? (
|
||||
<Loader2 size={16} className="mr-1 animate-spin" />
|
||||
) : (
|
||||
<RefreshCw size={16} />
|
||||
)}
|
||||
Retry
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
<div ref={messagesEndRef} />
|
||||
|
||||
@@ -23,6 +23,7 @@ export const chats = sqliteTable("chats", {
|
||||
.notNull()
|
||||
.references(() => apps.id, { onDelete: "cascade" }),
|
||||
title: text("title"),
|
||||
initialCommitHash: text("initial_commit_hash"),
|
||||
createdAt: integer("created_at", { mode: "timestamp" })
|
||||
.notNull()
|
||||
.default(sql`(unixepoch())`),
|
||||
|
||||
@@ -189,12 +189,20 @@ export function registerAppHandlers() {
|
||||
});
|
||||
|
||||
// Create initial commit
|
||||
await git.commit({
|
||||
const commitHash = await git.commit({
|
||||
fs: fs,
|
||||
dir: fullAppPath,
|
||||
message: "Init from react vite template",
|
||||
author: await getGitAuthor(),
|
||||
});
|
||||
|
||||
// Update chat with initial commit hash
|
||||
await db
|
||||
.update(chats)
|
||||
.set({
|
||||
initialCommitHash: commitHash,
|
||||
})
|
||||
.where(eq(chats.id, chat.id));
|
||||
} catch (error) {
|
||||
logger.error("Error in background app initialization:", error);
|
||||
}
|
||||
|
||||
@@ -1,19 +1,59 @@
|
||||
import { ipcMain } from "electron";
|
||||
import { db } from "../../db";
|
||||
import { chats } from "../../db/schema";
|
||||
import { apps, chats, messages } from "../../db/schema";
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
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() {
|
||||
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
|
||||
const [chat] = await db
|
||||
.insert(chats)
|
||||
.values({
|
||||
appId,
|
||||
initialCommitHash,
|
||||
})
|
||||
.returning();
|
||||
|
||||
logger.info(
|
||||
"Created chat:",
|
||||
chat.id,
|
||||
"for app:",
|
||||
appId,
|
||||
"with initial commit hash:",
|
||||
initialCommitHash
|
||||
);
|
||||
return chat.id;
|
||||
});
|
||||
|
||||
@@ -70,7 +110,17 @@ export function registerChatHandlers() {
|
||||
await db.delete(chats).where(eq(chats.id, chatId));
|
||||
return { success: true };
|
||||
} 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 };
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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 {
|
||||
const result = (await this.ipcRenderer.invoke("delete-chat", chatId)) as {
|
||||
success: boolean;
|
||||
};
|
||||
return result;
|
||||
const result = await this.ipcRenderer.invoke("delete-chat", chatId);
|
||||
return result as { success: boolean; error?: string };
|
||||
} catch (error) {
|
||||
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) {
|
||||
showError(error);
|
||||
throw error;
|
||||
|
||||
@@ -54,6 +54,7 @@ export interface Chat {
|
||||
id: number;
|
||||
title: string;
|
||||
messages: Message[];
|
||||
initialCommitHash?: string | null;
|
||||
}
|
||||
|
||||
export interface App {
|
||||
|
||||
@@ -58,6 +58,8 @@ const validInvokeChannels = [
|
||||
"window:get-platform",
|
||||
"upload-to-signed-url",
|
||||
"delete-chat",
|
||||
"delete-messages",
|
||||
"start-chat-stream",
|
||||
] as const;
|
||||
|
||||
// Add valid receive channels
|
||||
|
||||
Reference in New Issue
Block a user