-
+
-
- {isCopied ? : }
-
+
+
+
+
+
+ {isCopied ? : }
+
+
+
+ Copy
+
+
+
+
+
+
+
+
+
+
+ Fork
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Regenerate
+
+
+
+
+
+
+ Regenerate Response
+
+ Regenerating this response will clear all conversation that happened after it.
+ This action cannot be undone.
+
+
+
+ Cancel
+
+ Regenerate & Clear Future Messages
+
+
+
+
+
+
@@ -578,6 +887,213 @@ function ChatComponent() {
[localChat, model, openai, persistChat, queryClient, setUserPrompt, setSystemPrompt, chatId]
);
+ // Message action handlers
+ const handleEditMessage = useCallback(
+ async (messageIndex: number, newText: string) => {
+ if (!localChat) return;
+
+ // Get visible messages (non-system)
+ const visibleMessages = localChat.messages.filter((m) => m.role !== "system");
+ const messageToEdit = visibleMessages[messageIndex];
+
+ // Find the actual index in the full messages array
+ const actualIndex = localChat.messages.findIndex((m) => m === messageToEdit);
+
+ if (actualIndex >= 0) {
+ // Create new messages array - DESTRUCTIVE: clear all messages after this one
+ const updatedMessages = [...localChat.messages];
+ updatedMessages[actualIndex] = { ...updatedMessages[actualIndex], content: newText };
+
+ // Remove all messages after the edited message
+ const messagesUpToEdit = updatedMessages.slice(0, actualIndex + 1);
+
+ // Update local state
+ setLocalChat((prev) => ({ ...prev, messages: messagesUpToEdit }));
+
+ // Persist the changes
+ await persistChat({ ...localChat, messages: messagesUpToEdit });
+
+ // Invalidate queries to reflect changes
+ queryClient.invalidateQueries({
+ queryKey: ["chat", chatId],
+ refetchType: "all"
+ });
+
+ // Automatically send to completions API after editing (Save & Send)
+ if (messageToEdit.role === "user") {
+ await sendMessage(newText);
+ }
+ }
+ },
+ [localChat, persistChat, queryClient, chatId, sendMessage]
+ );
+
+ const handleDeleteMessage = useCallback(
+ async (messageIndex: number) => {
+ if (!localChat) return;
+
+ // Get visible messages (non-system)
+ const visibleMessages = localChat.messages.filter((m) => m.role !== "system");
+ const messageToDelete = visibleMessages[messageIndex];
+
+ // Find the actual index in the full messages array
+ const actualIndex = localChat.messages.findIndex((m) => m === messageToDelete);
+
+ if (actualIndex >= 0) {
+ // DESTRUCTIVE: Remove the message and all messages after it
+ const messagesUpToDelete = localChat.messages.slice(0, actualIndex);
+
+ // Update local state
+ setLocalChat((prev) => ({ ...prev, messages: messagesUpToDelete }));
+
+ // Persist the changes
+ await persistChat({ ...localChat, messages: messagesUpToDelete });
+
+ // Invalidate queries to reflect changes
+ queryClient.invalidateQueries({
+ queryKey: ["chat", chatId],
+ refetchType: "all"
+ });
+ }
+ },
+ [localChat, persistChat, queryClient, chatId]
+ );
+
+ const handleRegenerateMessage = useCallback(
+ async (messageIndex: number) => {
+ if (!localChat || isLoading) return;
+
+ try {
+ setIsLoading(true);
+ setError("");
+
+ // Get visible messages (non-system)
+ const visibleMessages = localChat.messages.filter((m) => m.role !== "system");
+ const assistantMessage = visibleMessages[messageIndex];
+
+ // Find the actual index of this assistant message in the full messages array
+ const assistantActualIndex = localChat.messages.findIndex((m) => m === assistantMessage);
+
+ if (assistantActualIndex < 0) return;
+
+ // DESTRUCTIVE: Clear out all messages at and after the assistant message being regenerated
+ const messagesUpToBeforeAssistant = localChat.messages.slice(0, assistantActualIndex);
+
+ // Update local state to remove the assistant message and everything after it
+ setLocalChat((prev) => ({ ...prev, messages: messagesUpToBeforeAssistant }));
+
+ // Regenerate from the existing conversation without adding a new user message
+ const stream = openai.beta.chat.completions.stream({
+ model,
+ messages: messagesUpToBeforeAssistant,
+ stream: true
+ });
+
+ let fullResponse = "";
+ let isFirstChunk = true;
+
+ for await (const chunk of stream) {
+ const content = chunk.choices[0]?.delta?.content || "";
+ fullResponse += content;
+ setCurrentStreamingMessage(fullResponse);
+
+ // Scroll to bottom on first chunk of the response
+ if (isFirstChunk && content.trim()) {
+ requestAnimationFrame(() => {
+ chatContainerRef.current?.scrollTo({
+ top: chatContainerRef.current.scrollHeight,
+ behavior: "smooth"
+ });
+ });
+ isFirstChunk = false;
+ }
+ }
+
+ // Add the new assistant response
+ const finalMessages = [
+ ...messagesUpToBeforeAssistant,
+ { role: "assistant", content: fullResponse } as ChatMessage
+ ];
+
+ setLocalChat((prev) => ({ ...prev, messages: finalMessages }));
+ setCurrentStreamingMessage(undefined);
+
+ // Persist the changes
+ await persistChat({ ...localChat, messages: finalMessages });
+
+ // Invalidate queries to reflect changes
+ queryClient.invalidateQueries({
+ queryKey: ["chat", chatId],
+ refetchType: "all"
+ });
+ } catch (error) {
+ console.error("Error regenerating message:", error);
+ setError("Failed to regenerate message. Please try again.");
+ } finally {
+ setIsLoading(false);
+ }
+ },
+ [localChat, isLoading, model, openai, persistChat, queryClient, chatId, setError]
+ );
+
+ const handleForkMessage = useCallback(
+ async (messageIndex: number) => {
+ if (!localChat) return;
+
+ try {
+ // Get visible messages (non-system)
+ const visibleMessages = localChat.messages.filter((m) => m.role !== "system");
+ const messageToForkFrom = visibleMessages[messageIndex];
+
+ // Find the actual index in the full messages array
+ const actualIndex = localChat.messages.findIndex((m) => m === messageToForkFrom);
+
+ if (actualIndex >= 0) {
+ // Create messages up to and including the fork point
+ const messagesUpToFork = localChat.messages.slice(0, actualIndex + 1);
+
+ // Create a new chat with the forked history
+ const forkTitle = `${localChat.title} (Fork)`;
+ const newChatId = await addChat(forkTitle);
+
+ // Create the new chat with the forked messages
+ const forkedChat: Chat = {
+ id: newChatId,
+ title: forkTitle,
+ messages: messagesUpToFork,
+ model: localChat.model
+ };
+
+ // Persist the forked chat
+ await persistChat(forkedChat);
+
+ // Invalidate queries to refresh chat list
+ queryClient.invalidateQueries({
+ queryKey: ["chatHistory"],
+ refetchType: "all"
+ });
+
+ queryClient.invalidateQueries({
+ queryKey: ["chat", newChatId],
+ refetchType: "all"
+ });
+
+ // Navigate to the new forked chat
+ navigate({ to: "/chat/$chatId", params: { chatId: newChatId } });
+ }
+ } catch (error) {
+ console.error("Error forking chat:", error);
+ setError("Failed to fork chat. Please try again.");
+ }
+ },
+ [localChat, addChat, persistChat, queryClient, navigate, setError]
+ );
+
+ const handleCopyMessage = useCallback((text: string) => {
+ // Copy functionality is handled within the individual message components
+ console.log("Message copied:", text.substring(0, 50) + "...");
+ }, []);
+
// Chat compression function
const compressChat = useCallback(async () => {
try {
@@ -676,7 +1192,7 @@ END OF INSTRUCTIONS`;
} finally {
setIsSummarizing(false);
}
- }, [localChat, model, openai, addChat, navigate, setUserPrompt]);
+ }, [localChat, openai, addChat, navigate, setUserPrompt, persistChat, queryClient]);
return (
@@ -710,25 +1226,52 @@ END OF INSTRUCTIONS`;
{/* Show all messages including system messages */}
- {localChat.messages?.map((message, index) => (
-
- {message.role === "system" && }
- {message.role === "user" && }
- {message.role === "assistant" && (
-
- )}
-
- ))}
+ {localChat.messages?.map((message, index) => {
+ // Get the index among visible (non-system) messages for edit/delete operations
+ const visibleMessages = localChat.messages.filter((m) => m.role !== "system");
+ const visibleIndex = visibleMessages.indexOf(message);
+
+ return (
+
+ {message.role === "system" && }
+ {message.role === "user" && (
+
+ )}
+ {message.role === "assistant" && (
+
+ )}
+
+ );
+ })}
{(currentStreamingMessage || isLoading) && (
{}}
+ onCopy={handleCopyMessage}
+ onFork={() => {}}
/>
)}