Simplify handlers & IPC client: move from Result pattern to throwing errors (#120)

This commit is contained in:
Will Chen
2025-05-09 15:14:12 -07:00
committed by GitHub
parent 26305ee090
commit c71638a508
25 changed files with 618 additions and 990 deletions

View File

@@ -92,11 +92,7 @@ export function ChatList({ show }: { show?: boolean }) {
const handleDeleteChat = async (chatId: number) => {
try {
const result = await IpcClient.getInstance().deleteChat(chatId);
if (!result.success) {
showError("Failed to delete chat");
return;
}
await IpcClient.getInstance().deleteChat(chatId);
showSuccess("Chat deleted successfully");
// If the deleted chat was selected, navigate to home

View File

@@ -153,18 +153,14 @@ export function GitHubConnector({ appId, folderName }: GitHubConnectorProps) {
setIsCreatingRepo(true);
setCreateRepoSuccess(false);
try {
const result = await IpcClient.getInstance().createGithubRepo(
await IpcClient.getInstance().createGithubRepo(
githubOrg,
repoName,
appId!,
);
if (result.success) {
setCreateRepoSuccess(true);
setRepoCheckError(null);
refreshApp();
} else {
setCreateRepoError(result.error || "Failed to create repository.");
}
setCreateRepoSuccess(true);
setRepoCheckError(null);
refreshApp();
} catch (err: any) {
setCreateRepoError(err.message || "Failed to create repository.");
} finally {
@@ -180,12 +176,8 @@ export function GitHubConnector({ appId, folderName }: GitHubConnectorProps) {
setIsDisconnecting(true);
setDisconnectError(null);
try {
const result = await IpcClient.getInstance().disconnectGithubRepo(appId);
if (result.success) {
refreshApp();
} else {
setDisconnectError(result.error || "Failed to disconnect repository.");
}
await IpcClient.getInstance().disconnectGithubRepo(appId);
refreshApp();
} catch (err: any) {
setDisconnectError(err.message || "Failed to disconnect repository.");
} finally {

View File

@@ -171,17 +171,12 @@ ${debugInfo.logs.slice(-3_500) || "No logs available"}
const { uploadUrl, filename } = await response.json();
// Upload to the signed URL using IPC
const uploadResult = await IpcClient.getInstance().uploadToSignedUrl(
await IpcClient.getInstance().uploadToSignedUrl(
uploadUrl,
"application/json",
chatLogsJson,
);
if (!uploadResult.success) {
throw new Error(`Failed to upload logs: ${uploadResult.error}`);
}
// Extract session ID (filename without extension)
const sessionId = filename.replace(".json", "");
setSessionId(sessionId);

View File

@@ -58,7 +58,7 @@ import { useVersions } from "@/hooks/useVersions";
import { useAttachments } from "@/hooks/useAttachments";
import { AttachmentsList } from "./AttachmentsList";
import { DragDropOverlay } from "./DragDropOverlay";
import { showUncommittedFilesWarning } from "@/lib/toast";
import { showError, showUncommittedFilesWarning } from "@/lib/toast";
const showTokenBarAtom = atom(false);
export function ChatInput({ chatId }: { chatId?: number }) {
@@ -182,13 +182,6 @@ export function ChatInput({ chatId }: { chatId?: number }) {
chatId,
messageId,
});
if (result.success) {
console.log("Proposal approved successfully");
// TODO: Maybe refresh proposal state or show confirmation?
} else {
console.error("Failed to approve proposal:", result.error);
setError(result.error || "Failed to approve proposal");
}
if (result.uncommittedFiles) {
showUncommittedFilesWarning(result.uncommittedFiles);
}
@@ -215,17 +208,10 @@ export function ChatInput({ chatId }: { chatId?: number }) {
setIsRejecting(true);
posthog.capture("chat:reject");
try {
const result = await IpcClient.getInstance().rejectProposal({
await IpcClient.getInstance().rejectProposal({
chatId,
messageId,
});
if (result.success) {
console.log("Proposal rejected successfully");
// TODO: Maybe refresh proposal state or show confirmation?
} else {
console.error("Failed to reject proposal:", result.error);
setError(result.error || "Failed to reject proposal");
}
} catch (err) {
console.error("Error rejecting proposal:", err);
setError((err as Error)?.message || "An error occurred while rejecting");
@@ -389,13 +375,17 @@ function SummarizeInNewChatButton() {
console.error("No app id found");
return;
}
const newChatId = await IpcClient.getInstance().createChat(appId);
// navigate to new chat
await navigate({ to: "/chat", search: { id: newChatId } });
await streamMessage({
prompt: "Summarize from chat-id=" + chatId,
chatId: newChatId,
});
try {
const newChatId = await IpcClient.getInstance().createChat(appId);
// navigate to new chat
await navigate({ to: "/chat", search: { id: newChatId } });
await streamMessage({
prompt: "Summarize from chat-id=" + chatId,
chatId: newChatId,
});
} catch (err) {
showError(err);
}
};
return (
<TooltipProvider>

View File

@@ -89,14 +89,13 @@ export const MessagesList = forwardRef<HTMLDivElement, MessagesListProps>(
await revertVersion({
versionId: chat.initialCommitHash,
});
const result =
try {
await IpcClient.getInstance().deleteMessages(
selectedChatId,
);
if (result.success) {
setMessages([]);
} else {
showError(result.error);
} catch (err) {
showError(err);
}
} else {
showWarning(

View File

@@ -6,6 +6,7 @@ import { ChevronRight, Circle } from "lucide-react";
import "@/components/chat/monaco";
import { IpcClient } from "@/ipc/ipc_client";
import { useSettings } from "@/hooks/useSettings";
import { showError } from "@/lib/toast";
interface FileEditorProps {
appId: number | null;
@@ -132,8 +133,7 @@ export const FileEditor = ({ appId, filePath }: FileEditorProps) => {
needsSaveRef.current = false;
setDisplayUnsavedChanges(false);
} catch (error) {
console.error("Error saving file:", error);
// Could add error notification here
showError(error);
} finally {
isSavingRef.current = false;
}