Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(web-app): clean up a thread's artifacts only after its delete succeeds

Akarshan Biswasf3d1ebde65ed
diff --git a/web-app/src/hooks/__tests__/useThreads.coverage.test.ts b/web-app/src/hooks/__tests__/useThreads.coverage.test.ts
index 288955d7c..8af034145 100644
--- a/web-app/src/hooks/__tests__/useThreads.coverage.test.ts
+++ b/web-app/src/hooks/__tests__/useThreads.coverage.test.ts
@@ -307,7 +307,7 @@ describe('useThreads - coverage', () => {
expect(result.current.threads['t3']).toBeDefined()
})
- it('drops the vector DB collection of each thread deleted in bulk', () => {
+ it('drops the vector DB collection of each thread deleted in bulk', async () => {
const { result } = renderHook(() => useThreads())
act(() => {
result.current.setThreads([{ id: 't2', title: 'Normal' } as Thread])
@@ -315,7 +315,25 @@ describe('useThreads - coverage', () => {
act(() => {
result.current.deleteAllThreads()
})
- expect(vectorDB.deleteCollection).toHaveBeenCalledWith('attachments_t2')
+ await vi.waitFor(() =>
+ expect(vectorDB.deleteCollection).toHaveBeenCalledWith('attachments_t2')
+ )
+ })
+
+ // A delete that fails brings the thread back on restart, so what lives
+ // outside the thread store must still be there for it.
+ it('keeps the artifacts of a thread whose delete failed', async () => {
+ mockDeleteThread.mockRejectedValueOnce(new Error('file in use'))
+ const { result } = renderHook(() => useThreads())
+ act(() => {
+ result.current.setThreads([{ id: 't2', title: 'Normal' } as Thread])
+ })
+ act(() => {
+ result.current.deleteThread('t2')
+ })
+ await vi.waitFor(() => expect(mockDeleteThread).toHaveBeenCalledWith('t2'))
+ await new Promise((r) => setTimeout(r, 0))
+ expect(vectorDB.deleteCollection).not.toHaveBeenCalled()
})
// Removing the chat session is what ends a turn still running in the
diff --git a/web-app/src/hooks/useThreads.ts b/web-app/src/hooks/useThreads.ts
index fd082782f..a85022149 100644
--- a/web-app/src/hooks/useThreads.ts
+++ b/web-app/src/hooks/useThreads.ts
@@ -143,20 +143,23 @@ const deleteThreadEverywhere = (threadId: string, thread: Thread | undefined) =>
deleteCoworkConversation(threadId)
return
}
- cleanupThreadArtifacts(threadId)
// The row is already gone from the list; when the files stay behind the
- // conversation comes back on restart, so the user is told now.
+ // conversation comes back on restart, so the user is told now and its
+ // artifacts are kept for it.
void getServiceHub()
.threads()
.deleteThread(threadId)
- .catch((error) => {
- console.error(`Could not delete thread ${threadId}:`, error)
- // Same id as the dialog's "deleted" toast, so this replaces it.
- toast.error(i18n.t('common:deleteThreadFailed'), {
- id: 'delete-thread',
- description: String(error),
- })
- })
+ .then(
+ () => cleanupThreadArtifacts(threadId),
+ (error) => {
+ console.error(`Could not delete thread ${threadId}:`, error)
+ // Same id as the dialog's "deleted" toast, so this replaces it.
+ toast.error(i18n.t('common:deleteThreadFailed'), {
+ id: 'delete-thread',
+ description: String(error),
+ })
+ }
+ )
}
export const useThreads = create<ThreadState>()((set, get) => ({