diff --git a/cptr/frontend/src/lib/components/SidebarWorkspaceList.svelte b/cptr/frontend/src/lib/components/SidebarWorkspaceList.svelte index fcaf790..2908143 100644 --- a/cptr/frontend/src/lib/components/SidebarWorkspaceList.svelte +++ b/cptr/frontend/src/lib/components/SidebarWorkspaceList.svelte @@ -63,7 +63,20 @@ const data = await getChats(path, 5, append ? existing.length : 0, 'updated_at', 'desc'); wsChatsCache = new Map([ ...wsChatsCache, - [path, append ? [...existing, ...(data.chats || [])] : data.chats || []] + [ + path, + append + ? [...existing, ...(data.chats || [])].sort( + (a, b) => + Number( + !b.is_active && (b.last_read_at === null || b.updated_at > b.last_read_at) + ) - + Number( + !a.is_active && (a.last_read_at === null || a.updated_at > a.last_read_at) + ) || b.updated_at - a.updated_at + ) + : data.chats || [] + ] ]); updateChatStatuses(data.chats || [], path); wsChatsHasMore = new Map([...wsChatsHasMore, [path, data.has_more]]); @@ -196,10 +209,14 @@ } let known = false; + const shouldReorder = + typeof data.updated_at === 'number' || + typeof data.last_read_at === 'number' || + typeof data.active === 'boolean'; + wsChatsCache = new Map( - [...wsChatsCache].map(([path, chats]) => [ - path, - chats.map((chat) => { + [...wsChatsCache].map(([path, chats]) => { + const nextChats = chats.map((chat) => { if (chat.id !== data.chat_id) return chat; known = true; return { @@ -209,14 +226,35 @@ ...(typeof data.last_read_at === 'number' ? { last_read_at: data.last_read_at } : {}), ...(typeof data.active === 'boolean' ? { is_active: data.active } : {}) }; - }) - ]) + }); + return [ + path, + shouldReorder + ? nextChats.sort( + (a, b) => + Number( + !b.is_active && (b.last_read_at === null || b.updated_at > b.last_read_at) + ) - + Number( + !a.is_active && (a.last_read_at === null || a.updated_at > a.last_read_at) + ) || b.updated_at - a.updated_at + ) + : nextChats + ] as [string, ChatInfo[]]; + }) ); // A chat created in another session is not yet in this sidebar's page. // Refresh only that expanded workspace; all known rows update in place. if (!known && data.workspace && expandedWorkspaces.has(data.workspace)) { void fetchWorkspaceChats(data.workspace); + } else if ( + known && + typeof data.last_read_at === 'number' && + data.workspace && + expandedWorkspaces.has(data.workspace) + ) { + void fetchWorkspaceChats(data.workspace); } } diff --git a/cptr/frontend/src/lib/components/chat/ChatPanel.svelte b/cptr/frontend/src/lib/components/chat/ChatPanel.svelte index 1f64586..780d129 100644 --- a/cptr/frontend/src/lib/components/chat/ChatPanel.svelte +++ b/cptr/frontend/src/lib/components/chat/ChatPanel.svelte @@ -462,7 +462,17 @@ try { const offset = (page - 1) * CHATS_PAGE_SIZE; const data = await getChats(workspace, CHATS_PAGE_SIZE, offset, chatSortBy, chatSortDir); - previousChats = data.chats || []; + previousChats = + chatSortBy === 'updated_at' + ? [...(data.chats || [])].sort( + (a, b) => + Number(!b.is_active && (b.last_read_at === null || b.updated_at > b.last_read_at)) - + Number( + !a.is_active && (a.last_read_at === null || a.updated_at > a.last_read_at) + ) || + (chatSortDir === 'desc' ? b.updated_at - a.updated_at : a.updated_at - b.updated_at) + ) + : data.chats || []; totalChats = data.total; chatPage = page; } catch { @@ -538,6 +548,10 @@ pending_inputs_processed?: boolean; async_subagent_pending?: boolean; title?: string; + workspace?: string; + active?: boolean; + updated_at?: number; + last_read_at?: number; }) { // On the landing page, update the chat list in place from socket events if (isLanding) { @@ -550,15 +564,41 @@ loadPreviousChats(chatPage); }, 300); } else { - if (data.done) { - previousChats = previousChats.map((c) => - c.id === data.chat_id ? { ...c, is_active: false } : c - ); - } - if (data.title) { - previousChats = previousChats.map((c) => - c.id === data.chat_id ? { ...c, title: data.title! } : c - ); + const nextChats = previousChats.map((c) => + c.id === data.chat_id + ? { + ...c, + ...(data.title ? { title: data.title } : {}), + ...(data.done ? { is_active: false } : {}), + ...(typeof data.active === 'boolean' ? { is_active: data.active } : {}), + ...(typeof data.updated_at === 'number' ? { updated_at: data.updated_at } : {}), + ...(typeof data.last_read_at === 'number' + ? { last_read_at: data.last_read_at } + : {}) + } + : c + ); + previousChats = + chatSortBy === 'updated_at' + ? nextChats.sort( + (a, b) => + Number( + !b.is_active && (b.last_read_at === null || b.updated_at > b.last_read_at) + ) - + Number( + !a.is_active && (a.last_read_at === null || a.updated_at > a.last_read_at) + ) || + (chatSortDir === 'desc' + ? b.updated_at - a.updated_at + : a.updated_at - b.updated_at) + ) + : nextChats; + if (typeof data.last_read_at === 'number') { + if (landingRefreshTimer) clearTimeout(landingRefreshTimer); + landingRefreshTimer = setTimeout(() => { + landingRefreshTimer = null; + loadPreviousChats(chatPage); + }, 100); } } } diff --git a/cptr/routers/chat.py b/cptr/routers/chat.py index 63e4c96..fb9d749 100644 --- a/cptr/routers/chat.py +++ b/cptr/routers/chat.py @@ -192,8 +192,22 @@ async def list_chats( reverse = sort_dir != "asc" if sort_field == "updated_at": visible_chats.sort( - key=lambda c: c["updated_at"] if isinstance(c["updated_at"], int) else 0, - reverse=reverse, + key=lambda c: ( + not ( + not c.get("is_active") + and (c["updated_at"] if isinstance(c["updated_at"], int) else 0) > 0 + and ( + c["last_read_at"] is None + or (c["updated_at"] if isinstance(c["updated_at"], int) else 0) + > c["last_read_at"] + ) + ), + -(c["updated_at"] if isinstance(c["updated_at"], int) else 0) + if reverse + else c["updated_at"] + if isinstance(c["updated_at"], int) + else 0, + ), ) else: visible_chats.sort(key=lambda c: str(c["title"] or ""), reverse=reverse)