From 10af5d3f3e19a04058f75a996dfd0549a30ce549 Mon Sep 17 00:00:00 2001 From: Anish Sarkar <104695310+AnishSarkar22@users.noreply.github.com> Date: Fri, 20 Mar 2026 16:12:18 +0530 Subject: [PATCH] feat: add authentication expiration handling for Notion tools - Implemented checks for expired authentication in the Notion page creation and deletion tools, returning appropriate error messages for re-authentication. - Updated the Notion tool metadata service to track account health and persist authentication status, improving error handling and user feedback during operations. --- .../new_chat/tools/notion/delete_page.py | 13 +++++++++++- .../new_chat/tools/notion/update_page.py | 11 ++++++++++ .../services/notion/tool_metadata_service.py | 20 ++++++++++++++++++- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/surfsense_backend/app/agents/new_chat/tools/notion/delete_page.py b/surfsense_backend/app/agents/new_chat/tools/notion/delete_page.py index f1a65a14a..91c31519a 100644 --- a/surfsense_backend/app/agents/new_chat/tools/notion/delete_page.py +++ b/surfsense_backend/app/agents/new_chat/tools/notion/delete_page.py @@ -95,8 +95,19 @@ def create_delete_notion_page_tool( "message": error_msg, } + account = context.get("account", {}) + if account.get("auth_expired"): + logger.warning( + "Notion account %s has expired authentication", + account.get("id"), + ) + return { + "status": "auth_error", + "message": "The Notion account for this page needs re-authentication. Please re-authenticate in your connector settings.", + } + page_id = context.get("page_id") - connector_id_from_context = context.get("account", {}).get("id") + connector_id_from_context = account.get("id") document_id = context.get("document_id") logger.info( diff --git a/surfsense_backend/app/agents/new_chat/tools/notion/update_page.py b/surfsense_backend/app/agents/new_chat/tools/notion/update_page.py index 306cd4b68..965ffc278 100644 --- a/surfsense_backend/app/agents/new_chat/tools/notion/update_page.py +++ b/surfsense_backend/app/agents/new_chat/tools/notion/update_page.py @@ -110,6 +110,17 @@ def create_update_notion_page_tool( "message": error_msg, } + account = context.get("account", {}) + if account.get("auth_expired"): + logger.warning( + "Notion account %s has expired authentication", + account.get("id"), + ) + return { + "status": "auth_error", + "message": "The Notion account for this page needs re-authentication. Please re-authenticate in your connector settings.", + } + page_id = context.get("page_id") document_id = context.get("document_id") connector_id_from_context = context.get("account", {}).get("id") diff --git a/surfsense_backend/app/services/notion/tool_metadata_service.py b/surfsense_backend/app/services/notion/tool_metadata_service.py index ca882c5c0..694f8cf6f 100644 --- a/surfsense_backend/app/services/notion/tool_metadata_service.py +++ b/surfsense_backend/app/services/notion/tool_metadata_service.py @@ -167,8 +167,26 @@ class NotionToolMetadataService: if not page_id: return {"error": "Page ID not found in document metadata"} + acc_dict = account.to_dict() + auth_expired = await self._check_account_health(connector.id) + acc_dict["auth_expired"] = auth_expired + if auth_expired: + try: + db_connector = connector + if not db_connector.config.get("auth_expired"): + db_connector.config = {**db_connector.config, "auth_expired": True} + flag_modified(db_connector, "config") + await self._db_session.commit() + await self._db_session.refresh(db_connector) + except Exception: + logger.warning( + "Failed to persist auth_expired for connector %s", + connector.id, + exc_info=True, + ) + return { - "account": account.to_dict(), + "account": acc_dict, "page_id": page_id, "current_title": document.title, "document_id": document.id,