From 55b39e39eb640e0e210ab72974e67cc9c8bd0597 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Fri, 10 Jul 2026 11:56:05 -0500 Subject: [PATCH] refac --- cptr/frontend/src/lib/apis/chat.ts | 7 +++++ .../lib/components/chat/ChatHistory.svelte | 18 +++++++++++ .../src/lib/components/chat/ChatPanel.svelte | 30 ++++++++++++++++++- 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/cptr/frontend/src/lib/apis/chat.ts b/cptr/frontend/src/lib/apis/chat.ts index 6c0d6a8..2e79a8c 100644 --- a/cptr/frontend/src/lib/apis/chat.ts +++ b/cptr/frontend/src/lib/apis/chat.ts @@ -97,6 +97,13 @@ export const getChat = (chatId: string, modelId?: string) => { export const deleteChat = (chatId: string) => fetchJSON<{ ok: boolean }>(`/api/chats/${chatId}`, { method: 'DELETE' }); +export const updateChatTitle = (chatId: string, title: string) => + fetchJSON<{ ok: boolean; title: string }>(`/api/chats/${chatId}`, { + method: 'PATCH', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ title }) + }); + export const forkChat = (chatId: string, messageId?: string | null) => fetchJSON<{ ok: boolean; chat_id: string }>( `/api/chats/${chatId}/fork`, diff --git a/cptr/frontend/src/lib/components/chat/ChatHistory.svelte b/cptr/frontend/src/lib/components/chat/ChatHistory.svelte index 127bd7a..58cf1c6 100644 --- a/cptr/frontend/src/lib/components/chat/ChatHistory.svelte +++ b/cptr/frontend/src/lib/components/chat/ChatHistory.svelte @@ -9,6 +9,8 @@ chats: ChatInfo[]; onopen: (id: string) => void; ondelete: (id: string) => void; + onrename: (id: string) => void; + oncopy: (id: string) => void; page?: number; totalPages?: number; perPage?: number; @@ -21,6 +23,8 @@ chats, onopen, ondelete, + onrename, + oncopy, page = 1, totalPages = 1, perPage = 10, @@ -144,6 +148,20 @@ anchor={menuAnchor} align="end" items={[ + { + label: $t('files.copyPath'), + icon: 'copy', + onclick: () => { + if (menuChatId) oncopy(menuChatId); + } + }, + { + label: $t('files.rename'), + icon: 'pencil', + onclick: () => { + if (menuChatId) onrename(menuChatId); + } + }, { label: $t('chat.history.delete'), icon: 'trash', diff --git a/cptr/frontend/src/lib/components/chat/ChatPanel.svelte b/cptr/frontend/src/lib/components/chat/ChatPanel.svelte index 1d1d4a0..a8454cf 100644 --- a/cptr/frontend/src/lib/components/chat/ChatPanel.svelte +++ b/cptr/frontend/src/lib/components/chat/ChatPanel.svelte @@ -3,6 +3,7 @@ getChat, getChats, deleteChat as apiDeleteChat, + updateChatTitle, forkChat as apiForkChat, sendMessage as apiSendMessage, approveToolCall, @@ -445,6 +446,30 @@ previousChats = previousChats.filter((c) => c.id !== id); } + async function renameChat(id: string) { + const chat = previousChats.find((c) => c.id === id); + const title = window.prompt($t('files.rename'), chat?.title)?.trim(); + if (!title || title === chat?.title) return; + try { + await updateChatTitle(id, title); + previousChats = previousChats.map((c) => (c.id === id ? { ...c, title } : c)); + if (chatId === id) { + chatTitle = title; + if (tabId) updateTab(tabId, id, title); + } + } catch (e) { + toast.error(e instanceof Error ? e.message : $t('files.rename')); + } + } + + function copyChatPath(id: string) { + const chat = previousChats.find((c) => c.id === id); + if (!chat) return; + navigator.clipboard.writeText( + `${workspace.replace(/\/$/, '')}/.cptr/chats/${chat.folder ? `${chat.folder}/` : ''}${chat.id}.json` + ); + } + // ── Socket listener ───────────────────────────────────────── let landingRefreshTimer: ReturnType | null = null; @@ -493,8 +518,9 @@ return; } - // Title generated by backend — update tab label + // Title updates also keep open chat tabs in sync. if (data.title && tabId) { + chatTitle = data.title; updateTab(tabId, data.chat_id, data.title); } @@ -1558,6 +1584,8 @@ chats={previousChats} onopen={openChat} ondelete={deleteChat} + onrename={renameChat} + oncopy={copyChatPath} page={chatPage} {totalPages} perPage={CHATS_PAGE_SIZE}