diff --git a/surfsense_backend/app/agents/new_chat/tools/gmail/trash_email.py b/surfsense_backend/app/agents/new_chat/tools/gmail/trash_email.py index 9e8edb47e..84d97d89c 100644 --- a/surfsense_backend/app/agents/new_chat/tools/gmail/trash_email.py +++ b/surfsense_backend/app/agents/new_chat/tools/gmail/trash_email.py @@ -47,6 +47,11 @@ def create_trash_gmail_email_tool( to verify the email subject or check if it has been indexed. - If status is "insufficient_permissions", the connector lacks the required OAuth scope. Inform the user they need to re-authenticate and do NOT retry this tool. + - ONLY call this tool ONCE per user request. The system automatically picks the + most relevant match when multiple emails share the same subject. The user will + see the exact email details (sender, date) in the approval card and can reject + if it is not the right one. Do NOT call this tool multiple times for the same + email subject. Examples: - "Delete the email about 'Meeting Cancelled'" diff --git a/surfsense_backend/app/agents/new_chat/tools/gmail/update_draft.py b/surfsense_backend/app/agents/new_chat/tools/gmail/update_draft.py index f3e3705ea..d43d0a760 100644 --- a/surfsense_backend/app/agents/new_chat/tools/gmail/update_draft.py +++ b/surfsense_backend/app/agents/new_chat/tools/gmail/update_draft.py @@ -63,6 +63,10 @@ def create_update_gmail_draft_tool( Respond with a brief acknowledgment and do NOT retry or suggest alternatives. - If status is "not_found", relay the exact message to the user and ask them to verify the draft subject or check if it has been indexed. + - ONLY call this tool ONCE per user request. The system automatically picks the + most relevant match when multiple drafts share the same subject. The user will + see the exact draft details in the approval card and can reject if it is not + the right one. Do NOT call this tool multiple times for the same draft subject. - If status is "insufficient_permissions", the connector lacks the required OAuth scope. Inform the user they need to re-authenticate and do NOT retry the action. diff --git a/surfsense_backend/app/agents/new_chat/tools/google_calendar/delete_event.py b/surfsense_backend/app/agents/new_chat/tools/google_calendar/delete_event.py index 04858751f..704ae9725 100644 --- a/surfsense_backend/app/agents/new_chat/tools/google_calendar/delete_event.py +++ b/surfsense_backend/app/agents/new_chat/tools/google_calendar/delete_event.py @@ -46,6 +46,11 @@ def create_delete_calendar_event_tool( acknowledgment and do NOT retry or suggest alternatives. - If status is "not_found", relay the exact message to the user and ask them to verify the event name or check if it has been indexed. + - ONLY call this tool ONCE per user request. The system automatically picks the + most relevant match when multiple events share the same name. The user will + see the exact event details (date, time, location) in the approval card and + can reject if it is not the right one. Do NOT call this tool multiple times + for the same event name. Examples: - "Delete the team standup event" diff --git a/surfsense_backend/app/agents/new_chat/tools/google_calendar/update_event.py b/surfsense_backend/app/agents/new_chat/tools/google_calendar/update_event.py index 505b07f39..5c6dce026 100644 --- a/surfsense_backend/app/agents/new_chat/tools/google_calendar/update_event.py +++ b/surfsense_backend/app/agents/new_chat/tools/google_calendar/update_event.py @@ -54,6 +54,11 @@ def create_update_calendar_event_tool( acknowledgment and do NOT retry or suggest alternatives. - If status is "not_found", relay the exact message to the user and ask them to verify the event name or check if it has been indexed. + - ONLY call this tool ONCE per user request. The system automatically picks the + most relevant match when multiple events share the same name. The user will + see the exact event details (date, time, location) in the approval card and + can reject if it is not the right one. Do NOT call this tool multiple times + for the same event name. Examples: - "Reschedule the team standup to 3pm" diff --git a/surfsense_backend/app/agents/new_chat/tools/google_drive/trash_file.py b/surfsense_backend/app/agents/new_chat/tools/google_drive/trash_file.py index 4c037625f..edee120c3 100644 --- a/surfsense_backend/app/agents/new_chat/tools/google_drive/trash_file.py +++ b/surfsense_backend/app/agents/new_chat/tools/google_drive/trash_file.py @@ -47,6 +47,10 @@ def create_delete_google_drive_file_tool( to verify the file name or check if it has been indexed. - If status is "insufficient_permissions", the connector lacks the required OAuth scope. Inform the user they need to re-authenticate and do NOT retry this tool. + - ONLY call this tool ONCE per user request. The system automatically picks the + most relevant match when multiple files share the same name. The user will + see the exact file details in the approval card and can reject if it is not + the right one. Do NOT call this tool multiple times for the same file name. Examples: - "Delete the 'Meeting Notes' file from Google Drive" diff --git a/surfsense_backend/app/agents/new_chat/tools/linear/delete_issue.py b/surfsense_backend/app/agents/new_chat/tools/linear/delete_issue.py index 7f41e32e2..b13cdbfa5 100644 --- a/surfsense_backend/app/agents/new_chat/tools/linear/delete_issue.py +++ b/surfsense_backend/app/agents/new_chat/tools/linear/delete_issue.py @@ -64,6 +64,10 @@ def create_delete_linear_issue_tool( - If status is "not_found", inform the user conversationally using the exact message provided. Do NOT treat this as an error. Simply relay the message and ask the user to verify the issue title or identifier, or check if it has been indexed. + - ONLY call this tool ONCE per user request. The system automatically picks the + most relevant match when multiple issues share the same title. The user will + see the exact issue details in the approval card and can reject if it is not + the right one. Do NOT call this tool multiple times for the same issue reference. Examples: - "Delete the 'Fix login bug' Linear issue" diff --git a/surfsense_backend/app/agents/new_chat/tools/linear/update_issue.py b/surfsense_backend/app/agents/new_chat/tools/linear/update_issue.py index 19af851c1..117fdb8ed 100644 --- a/surfsense_backend/app/agents/new_chat/tools/linear/update_issue.py +++ b/surfsense_backend/app/agents/new_chat/tools/linear/update_issue.py @@ -78,6 +78,10 @@ def create_update_linear_issue_tool( - If status is "not_found", inform the user conversationally using the exact message provided. Do NOT treat this as an error. Simply relay the message and ask the user to verify the issue title or identifier, or check if it has been indexed. + - ONLY call this tool ONCE per user request. The system automatically picks the + most relevant match when multiple issues share the same title. The user will + see the exact issue details in the approval card and can reject if it is not + the right one. Do NOT call this tool multiple times for the same issue reference. Examples: - "Mark the 'Fix login bug' issue as done" 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 91c31519a..9ce15a28a 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 @@ -54,6 +54,11 @@ def create_delete_notion_page_tool( - message: Success or error message - deleted_from_kb: Whether the page was also removed from knowledge base (if success) + ONLY call this tool ONCE per user request. The system automatically picks the + most relevant match when multiple pages share the same title. The user will + see the exact page details in the approval card and can reject if it is not + the right one. Do NOT call this tool multiple times for the same page title. + Examples: - "Delete the 'Meeting Notes' Notion page" - "Remove the 'Old Project Plan' Notion page" 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 965ffc278..69c119cf4 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 @@ -63,6 +63,10 @@ def create_update_notion_page_tool( Example: "I couldn't find the page '[page_title]' in your indexed Notion pages. [message details]" Do NOT treat this as an error. Do NOT invent information. Simply relay the message and ask the user to verify the page title or check if it's been indexed. + - ONLY call this tool ONCE per user request. The system automatically picks the + most relevant match when multiple pages share the same title. The user will + see the exact page details in the approval card and can reject if it is not + the right one. Do NOT call this tool multiple times for the same page title. Examples: - "Add today's meeting notes to the 'Meeting Notes' Notion page" diff --git a/surfsense_backend/app/services/gmail/tool_metadata_service.py b/surfsense_backend/app/services/gmail/tool_metadata_service.py index 8d155be59..3292ae2f1 100644 --- a/surfsense_backend/app/services/gmail/tool_metadata_service.py +++ b/surfsense_backend/app/services/gmail/tool_metadata_service.py @@ -436,6 +436,8 @@ class GmailToolMetadataService: ), ) ) + .order_by(Document.updated_at.desc().nullslast()) + .limit(1) ) row = result.first() if row: diff --git a/surfsense_backend/app/services/google_calendar/tool_metadata_service.py b/surfsense_backend/app/services/google_calendar/tool_metadata_service.py index 17f6e8e67..d25736d1d 100644 --- a/surfsense_backend/app/services/google_calendar/tool_metadata_service.py +++ b/surfsense_backend/app/services/google_calendar/tool_metadata_service.py @@ -396,6 +396,8 @@ class GoogleCalendarToolMetadataService: ), ) ) + .order_by(Document.updated_at.desc().nullslast()) + .limit(1) ) row = result.first() if row: diff --git a/surfsense_backend/app/services/google_drive/tool_metadata_service.py b/surfsense_backend/app/services/google_drive/tool_metadata_service.py index e48d5f9e1..f118c2c7a 100644 --- a/surfsense_backend/app/services/google_drive/tool_metadata_service.py +++ b/surfsense_backend/app/services/google_drive/tool_metadata_service.py @@ -111,6 +111,8 @@ class GoogleDriveToolMetadataService: SearchSourceConnector.user_id == user_id, ) ) + .order_by(Document.updated_at.desc().nullslast()) + .limit(1) ) document = result.scalars().first() diff --git a/surfsense_backend/app/services/linear/tool_metadata_service.py b/surfsense_backend/app/services/linear/tool_metadata_service.py index bae8ab367..ebd4ead92 100644 --- a/surfsense_backend/app/services/linear/tool_metadata_service.py +++ b/surfsense_backend/app/services/linear/tool_metadata_service.py @@ -358,6 +358,7 @@ class LinearToolMetadataService: ), ) ) + .order_by(Document.updated_at.desc().nullslast()) .limit(1) ) return result.scalars().first() diff --git a/surfsense_backend/app/services/notion/tool_metadata_service.py b/surfsense_backend/app/services/notion/tool_metadata_service.py index 0a0513629..f6e55bdab 100644 --- a/surfsense_backend/app/services/notion/tool_metadata_service.py +++ b/surfsense_backend/app/services/notion/tool_metadata_service.py @@ -135,6 +135,8 @@ class NotionToolMetadataService: SearchSourceConnector.user_id == user_id, ) ) + .order_by(Document.updated_at.desc().nullslast()) + .limit(1) ) document = result.scalars().first()