YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
feat: add retry, branch in new chat, and rename to chat UI (#2826)
Ansh Groverc9c3bb41cac5
diff --git a/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.ts b/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.tsindex 367e845c7..f1b3dfd36 100644--- a/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.ts+++ b/apps/screenpipe-app-tauri/components/hooks/use-chat-conversations.ts@@ -232,6 +232,17 @@ export function useChatConversations(opts: UseChatConversationsOpts) {// eslint-disable-next-line react-hooks/exhaustive-deps}, [isLoading, messages]);+ // ---- renameConversation ----+ const renameConversation = async (convId: string, newTitle: string) => {+ const trimmed = newTitle.trim();+ if (!trimmed) return;+ const { loadConversationFile } = await import("@/lib/chat-storage");+ const conv = await loadConversationFile(convId);+ if (!conv) return;+ await saveConversationFile({ ...conv, title: trimmed, updatedAt: Date.now() });+ await refreshFileConversations();+ };+// ---- deleteConversation ----const deleteConversation = async (convId: string) => {await deleteConversationFile(convId);@@ -317,6 +328,81 @@ export function useChatConversations(opts: UseChatConversationsOpts) {}};+ // ---- branchConversation ----+ // Creates a new conversation forked from the current one up to (and including)+ // the given message ID, then switches to it. Pi gets history re-injected on+ // the next message (piSessionSyncedRef = false).+ const branchConversation = async (upToMessageId: string) => {+ const msgIndex = messages.findIndex((m) => m.id === upToMessageId);+ if (msgIndex === -1) return;++ const branchedMessages = messages.slice(0, msgIndex + 1);+ if (branchedMessages.length === 0) return;++ const newId = crypto.randomUUID();+ const firstUserMsg = branchedMessages.find((m) => m.role === "user");+ const title = (firstUserMsg?.content.slice(0, 47) || "Branched Chat") + "…";++ const conversation: ChatConversation = {+ id: newId,+ title,+ messages: branchedMessages.slice(-100).map((m) => {+ let content = m.content;+ if (!content && m.contentBlocks?.length) {+ content = m.contentBlocks+ .filter((b: any) => b.type === "text")+ .map((b: any) => b.text)+ .join("\n") || "(tool result)";+ }+ const blocks = m.contentBlocks?.map((b: any) => {+ if (b.type === "tool") {+ const { isRunning, ...rest } = b.toolCall;+ return { type: "tool", toolCall: { ...rest, isRunning: false, result: rest.result?.slice(0, 4000) } };+ }+ if (b.type === "thinking") return { ...b, isThinking: false };+ return b;+ });+ return {+ id: m.id,+ role: m.role,+ content,+ timestamp: m.timestamp,+ ...(blocks?.length ? { contentBlocks: blocks } : {}),+ ...(m.images?.length ? { images: m.images } : {}),+ ...(m.model ? { model: m.model } : {}),+ ...(m.provider ? { provider: m.provider } : {}),+ };+ }),+ createdAt: Date.now(),+ updatedAt: Date.now(),+ };++ await saveConversationFile(conversation);+ await refreshFileConversations();++ // Switch to the branched conversation+ piSessionIdRef.current = newId;+ piSessionSyncedRef.current = false;+ setMessages(branchedMessages);+ setConversationId(newId);+ setShowHistory(false);++ try {+ const { getStore } = await import("@/lib/hooks/use-settings");+ const store = await getStore();+ const freshSettings = await store.get<any>("settings");+ if (freshSettings?.chatHistory) {+ await store.set("settings", {+ ...freshSettings,+ chatHistory: { ...freshSettings.chatHistory, activeConversationId: newId },+ });+ await store.save();+ }+ } catch (e) {+ console.warn("[chat] failed to update activeConversationId for branch:", e);+ }+ };+// ---- startNewConversation ----// Assigns a fresh session ID so the next message starts a brand-new Pi// process. The old session stays alive (backend evicts LRU when > 4).@@ -396,6 +482,8 @@ export function useChatConversations(opts: UseChatConversationsOpts) {saveConversation,loadConversation,deleteConversation,+ renameConversation,startNewConversation,+ branchConversation,};}diff --git a/apps/screenpipe-app-tauri/components/standalone-chat.tsx b/apps/screenpipe-app-tauri/components/standalone-chat.tsxindex a4cb389e5..86293794f 100644--- a/apps/screenpipe-app-tauri/components/standalone-chat.tsx+++ b/apps/screenpipe-app-tauri/components/standalone-chat.tsx@@ -10,7 +10,7 @@ import { Input } from "@/components/ui/input";import { Button } from "@/components/ui/button";import { useSettings, ChatMessage, ChatConversation } from "@/lib/hooks/use-settings";import { cn } from "@/lib/utils";-import { Loader2, Send, Square, User, Settings, ExternalLink, X, ImageIcon, History, Search, Trash2, ChevronLeft, ChevronRight, ChevronDown, ChevronUp, Plus, Copy, Check, Clock, Paperclip, Filter, RefreshCw } from "lucide-react";+import { Loader2, Send, Square, User, Settings, ExternalLink, X, ImageIcon, History, Search, Trash2, ChevronLeft, ChevronRight, ChevronDown, ChevronUp, Plus, Copy, Check, Clock, Paperclip, Filter, RefreshCw, GitBranch, MoreHorizontal, Pencil } from "lucide-react";import { SchedulePromptDialog } from "@/components/chat/schedule-prompt-dialog";import { toast } from "@/components/ui/use-toast";import { motion, AnimatePresence } from "framer-motion";@@ -37,7 +37,7 @@ import { usePlatform } from "@/lib/hooks/use-platform";import { useSqlAutocomplete } from "@/lib/hooks/use-sql-autocomplete";import { homeDir, join } from "@tauri-apps/api/path";import { useTimelineStore } from "@/lib/hooks/use-timeline-store";-import { Dialog, DialogContent } from "@/components/ui/dialog";+import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog";import {parseMentions,buildAppMentionSuggestions,@@ -935,6 +935,11 @@ export function StandaloneChat({ className }: { className?: string } = {}) {const [isStreaming, setIsStreaming] = useState(false);const [streamedCharCount, setStreamedCharCount] = useState(0);const [copiedMessageId, setCopiedMessageId] = useState<string | null>(null);+ const [openMessageMenuId, setOpenMessageMenuId] = useState<string | null>(null);+ const [openConvMenuId, setOpenConvMenuId] = useState<string | null>(null);+ const [renamingConvId, setRenamingConvId] = useState<string | null>(null);+ const [renameValue, setRenameValue] = useState("");+ const [deletingConvId, setDeletingConvId] = useState<string | null>(null);const [activePreset, setActivePreset] = useState<AIPreset | undefined>();const [showMentionDropdown, setShowMentionDropdown] = useState(false);const [mentionFilter, setMentionFilter] = useState("");@@ -1043,7 +1048,9 @@ export function StandaloneChat({ className }: { className?: string } = {}) {saveConversation,loadConversation,deleteConversation,+ renameConversation,startNewConversation,+ branchConversation,} = useChatConversations({messages,setMessages,@@ -3073,17 +3080,47 @@ export function StandaloneChat({ className }: { className?: string } = {}) {{conv.messages.length} messages</p></div>- <Button- variant="ghost"- size="sm"- onClick={(e) => {- e.stopPropagation();- deleteConversation(conv.id);- }}- className="h-6 w-6 p-0 opacity-0 group-hover:opacity-100 transition-opacity text-muted-foreground hover:text-destructive"+ <Popover+ open={openConvMenuId === conv.id}+ onOpenChange={(open) => setOpenConvMenuId(open ? conv.id : null)}>- <Trash2 size={12} />- </Button>+ <PopoverTrigger asChild>+ <Button+ variant="ghost"+ size="sm"+ onClick={(e) => e.stopPropagation()}+ className="h-6 w-6 p-0 opacity-0 group-hover:opacity-100 transition-opacity text-muted-foreground hover:text-foreground"+ >+ <MoreHorizontal size={12} />+ </Button>+ </PopoverTrigger>+ <PopoverContent className="w-40 p-1" align="end" side="right">+ <button+ className="w-full flex items-center gap-2 px-2 py-1.5 text-sm rounded-md hover:bg-muted text-left"+ onClick={(e) => {+ e.stopPropagation();+ setOpenConvMenuId(null);+ setRenameValue(conv.title);+ setRenamingConvId(conv.id);+ }}+ >+ <Pencil className="h-3.5 w-3.5 shrink-0" />+ Rename+ </button>+ <div className="my-1 border-t border-border" />+ <button+ className="w-full flex items-center gap-2 px-2 py-1.5 text-sm rounded-md hover:bg-muted text-destructive text-left"+ onClick={(e) => {+ e.stopPropagation();+ setOpenConvMenuId(null);+ setDeletingConvId(conv.id);+ }}+ >+ <Trash2 className="h-3.5 w-3.5 shrink-0" />+ Delete+ </button>+ </PopoverContent>+ </Popover></div>))}</div>@@ -3253,6 +3290,27 @@ export function StandaloneChat({ className }: { className?: string } = {}) {<Copy className="h-3 w-3" />)}</button>+ {message.role === "assistant" && !isLoading && (+ <button+ onClick={() => {+ const msgIndex = messages.findIndex((m) => m.id === message.id);+ // Find the preceding user message index+ let userMsgIndex = -1;+ for (let i = msgIndex - 1; i >= 0; i--) {+ if (messages[i].role === "user") { userMsgIndex = i; break; }+ }+ if (userMsgIndex === -1) return;+ const userMsg = messages[userMsgIndex];+ // Remove user message and everything after it, then resend+ setMessages((prev) => prev.slice(0, userMsgIndex));+ sendMessage(userMsg.content, userMsg.displayContent);+ }}+ className="p-1 rounded-md hover:bg-muted text-muted-foreground hover:text-foreground"+ title="Retry"+ >+ <RefreshCw className="h-3 w-3" />+ </button>+ )}{message.role === "assistant" && !message.content.includes("used all your free queries") && !message.content.startsWith("Error") && message.content !== "Processing..." && (<buttononClick={() => {@@ -3272,6 +3330,36 @@ export function StandaloneChat({ className }: { className?: string } = {}) {<Clock className="h-3 w-3" /></button>)}+ {message.role === "assistant" && (+ <Popover+ open={openMessageMenuId === message.id}+ onOpenChange={(open) => setOpenMessageMenuId(open ? message.id : null)}+ >+ <PopoverTrigger asChild>+ <button+ className="p-1 rounded-md hover:bg-muted text-muted-foreground hover:text-foreground"+ title="More options"+ >+ <MoreHorizontal className="h-3 w-3" />+ </button>+ </PopoverTrigger>+ <PopoverContent className="w-48 p-1" align="end" side="top">+ <div className="text-xs text-muted-foreground px-2 py-1 mb-1">+ {new Date(message.timestamp).toLocaleString([], { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit" })}+ </div>+ <button+ onClick={() => {+ setOpenMessageMenuId(null);+ branchConversation(message.id);+ }}+ className="w-full flex items-center gap-2 px-2 py-1.5 text-sm rounded-md hover:bg-muted text-left"+ >+ <GitBranch className="h-3.5 w-3.5 shrink-0" />+ Branch in new chat+ </button>+ </PopoverContent>+ </Popover>+ )}</div></div></motion.div>@@ -3902,6 +3990,66 @@ export function StandaloneChat({ className }: { className?: string } = {}) {)}</DialogContent></Dialog>++ {/* Delete chat confirmation dialog */}+ <Dialog open={!!deletingConvId} onOpenChange={(open) => !open && setDeletingConvId(null)}>+ <DialogContent className="sm:max-w-sm">+ <DialogHeader>+ <DialogTitle>Delete chat</DialogTitle>+ <p className="text-sm text-muted-foreground">Are you sure you want to delete this chat?</p>+ </DialogHeader>+ <DialogFooter>+ <Button variant="outline" onClick={() => setDeletingConvId(null)}>+ Cancel+ </Button>+ <Button+ variant="destructive"+ onClick={() => {+ deleteConversation(deletingConvId!);+ setDeletingConvId(null);+ }}+ >+ Delete+ </Button>+ </DialogFooter>+ </DialogContent>+ </Dialog>++ {/* Rename chat dialog */}+ <Dialog open={!!renamingConvId} onOpenChange={(open) => !open && setRenamingConvId(null)}>+ <DialogContent className="sm:max-w-sm">+ <DialogHeader>+ <DialogTitle>Rename chat</DialogTitle>+ </DialogHeader>+ <input+ autoFocus+ className="w-full rounded-md border border-input bg-background px-3 py-2 text-sm outline-none focus:ring-1 focus:ring-ring"+ value={renameValue}+ onChange={(e) => setRenameValue(e.target.value)}+ onKeyDown={(e) => {+ if (e.key === "Enter") {+ renameConversation(renamingConvId!, renameValue);+ setRenamingConvId(null);+ } else if (e.key === "Escape") {+ setRenamingConvId(null);+ }+ }}+ />+ <DialogFooter>+ <Button variant="outline" onClick={() => setRenamingConvId(null)}>+ Cancel+ </Button>+ <Button+ onClick={() => {+ renameConversation(renamingConvId!, renameValue);+ setRenamingConvId(null);+ }}+ >+ Save+ </Button>+ </DialogFooter>+ </DialogContent>+ </Dialog></div>);}