From 12077a671d35e6db970797d0d941f223f5de0f99 Mon Sep 17 00:00:00 2001 From: "Ashwal Vishwanath (Persistent Systems Inc)" Date: Tue, 9 Jun 2026 19:05:05 +0530 Subject: [PATCH] Fix clear-all chat history fallback when bulk endpoint is unavailable --- src/App/src/api/api.ts | 62 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/src/App/src/api/api.ts b/src/App/src/api/api.ts index 779708709..eeb3b96fd 100644 --- a/src/App/src/api/api.ts +++ b/src/App/src/api/api.ts @@ -146,11 +146,12 @@ export const historyRead = async (convId: string): Promise => { }; export const historyList = async ( - offset = 0 + offset = 0, + limit = 25 ): Promise => { try { const response = await httpClient.get("/history/list", { - params: { offset }, + params: { offset, limit }, }); const payload = await parseResponseJson(response); @@ -294,7 +295,62 @@ export const historyDelete = async (convId: string): Promise => { export const historyDeleteAll = async (): Promise => { try { - return await httpClient.delete("/history/delete_all", {}); + const response = await httpClient.delete("/history/delete_all", {}); + + if (response.ok || response.status !== 404) { + return response; + } + + // Some deployments expose single-delete APIs but not the bulk-delete route. + // Fall back to paginated delete so "clear all" still works. + const pageSize = 25; + const conversationIds: string[] = []; + let offset = 0; + + while (true) { + const page = await historyList(offset, pageSize); + if (!Array.isArray(page) || page.length === 0) { + break; + } + + conversationIds.push(...page.map((conversation) => conversation.id)); + if (page.length < pageSize) { + break; + } + + offset += pageSize; + } + + if (conversationIds.length === 0) { + return response; + } + + const deleteResults = await Promise.allSettled( + conversationIds.map((conversationId) => historyDelete(conversationId)) + ); + + const hasFailedDelete = deleteResults.some( + (result) => result.status === "rejected" || !result.value.ok + ); + + if (hasFailedDelete) { + return createErrorResponse( + 500, + "There was an issue clearing chat history." + ); + } + + return new Response( + JSON.stringify({ + message: "Successfully deleted all conversations.", + }), + { + status: 200, + headers: { + "Content-Type": "application/json", + }, + } + ); } catch { return createErrorResponse(500, "There was an issue clearing chat history."); }