From 3096544cf20fb37cc5838433471850ea18e293b2 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Wed, 22 Nov 2023 03:13:53 -0800 Subject: [PATCH] Create API endpoint to clear user's chat history --- src/khoj/database/adapters/__init__.py | 4 ++++ src/khoj/routers/api.py | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/khoj/database/adapters/__init__.py b/src/khoj/database/adapters/__init__.py index ea0c0a85..9b820ea9 100644 --- a/src/khoj/database/adapters/__init__.py +++ b/src/khoj/database/adapters/__init__.py @@ -232,6 +232,10 @@ class ConversationAdapters: return await conversation.afirst() return await Conversation.objects.acreate(user=user) + @staticmethod + async def adelete_conversation_by_user(user: KhojUser): + return await Conversation.objects.filter(user=user).adelete() + @staticmethod def has_any_conversation_config(user: KhojUser): return ChatModelOptions.objects.filter(user=user).exists() diff --git a/src/khoj/routers/api.py b/src/khoj/routers/api.py index f2e5c966..66ec75d5 100644 --- a/src/khoj/routers/api.py +++ b/src/khoj/routers/api.py @@ -534,6 +534,27 @@ def chat_history( return {"status": "ok", "response": meta_log.get("chat", [])} +@api.delete("/chat/history") +@requires(["authenticated"]) +async def clear_chat_history( + request: Request, + common: CommonQueryParams, +): + user = request.user.object + + # Clear Conversation History + await ConversationAdapters.adelete_conversation_by_user(user) + + update_telemetry_state( + request=request, + telemetry_type="api", + api="clear_chat_history", + **common.__dict__, + ) + + return {"status": "ok", "message": "Conversation history cleared"} + + @api.get("/chat/options", response_class=Response) @requires(["authenticated"]) async def chat_options(