From 8feb50e89468235cd3e0986da80edbd3acaf2c40 Mon Sep 17 00:00:00 2001 From: Beyrem Weslati <74115035+beyremweslati@users.noreply.github.com> Date: Tue, 13 Oct 2026 18:57:19 +0100 Subject: [PATCH] feat(notes): attach selected Notes/Todos as Agent context (#6495) * feat: add Notes and Todos as agent chat context - Send selected Note/Todo IDs from the Notes UI - Load selected notes with owner scoping in chat routes - Format note content and checklist items for the model - Inject selected notes as untrusted chat context - Add a selected-context indicator beside Shell Access - Keep selected context associated with the active chat session * Improved selected notes context handling and discouragin redundant note lookup and permission requests * Fix selected notes UI when switching chats * fix(notes): harden selected context lifecycle and validation --------- Co-authored-by: Alexandre Teixeira <111787685+alteixeira20@users.noreply.github.com> --- routes/chat_helpers.py | 18 ++++++ routes/chat_routes.py | 83 +++++++++++++++++++++++++++- src/agent_loop.py | 4 +- static/index.html | 15 +++++ static/js/chat.js | 68 +++++++++++++++++++++++ static/js/notes.js | 14 +++++ static/js/sessions.js | 15 +++++ tests/selectedNotesContext.test.mjs | 81 +++++++++++++++++++++++++++ tests/test_selected_notes_context.py | 28 ++++++++++ 9 files changed, 324 insertions(+), 2 deletions(-) create mode 100644 tests/selectedNotesContext.test.mjs create mode 100644 tests/test_selected_notes_context.py diff --git a/routes/chat_helpers.py b/routes/chat_helpers.py index 472128eaf..7debf70b6 100644 --- a/routes/chat_helpers.py +++ b/routes/chat_helpers.py @@ -1010,6 +1010,7 @@ async def build_chat_context( chat_processor, message: str, session_id: str, + selected_notes_context: str | None = None, preset_id=None, att_ids: list = None, use_web=None, @@ -1182,6 +1183,23 @@ async def build_chat_context( if search_context and allow_tool_preprocessing and not casual_low_signal: preface.append(untrusted_context_message("prefetched search context", search_context)) + # Inject selected notes as context + if selected_notes_context: + selected_notes_context = ( + "The following selected notes and todos are reference context explicitly chosen " + "by the user. Use them directly when relevant to the user's request. Do not " + "ask for permission to use this context, and do not call note-management " + "tools just to retrieve the same notes; use those tools only for a requested " + "change or when this context is insufficient.\n\n" + + selected_notes_context + ) + preface.append( + untrusted_context_message( + "Selected notes and todos", + selected_notes_context, + ) + ) + # YouTube transcripts for transcript in preprocessed.youtube_transcripts: preface.append(untrusted_context_message("youtube transcript", transcript)) diff --git a/routes/chat_routes.py b/routes/chat_routes.py index e3f7bfa02..5a42d3351 100644 --- a/routes/chat_routes.py +++ b/routes/chat_routes.py @@ -58,7 +58,7 @@ from src.auth_helpers import ( from routes.session_routes import _verify_session_owner from routes.document_helpers import _owner_session_filter from core.database import SessionLocal, get_session_mode, set_session_mode -from core.database import Session as DBSession, ChatMessage as DBChatMessage +from core.database import Session as DBSession, ChatMessage as DBChatMessage, Note from core.database import Document as DBDocument, ModelEndpoint from core.log_safety import redact_url from routes.research_routes import _resolve_research_endpoint @@ -2153,6 +2153,68 @@ def _set_user_time_from_request(request: Request) -> None: except Exception: pass +def _parse_selected_note_ids(raw) -> list[str]: + """Validate user-selected Note IDs before passing them to SQLAlchemy.""" + if raw is None or raw == "": + return [] + if not isinstance(raw, str) or len(raw) > 8192: + raise HTTPException(400, "Invalid selected_note_ids payload") + try: + parsed = json.loads(raw) + except (TypeError, ValueError) as exc: + raise HTTPException(400, "Invalid selected_note_ids JSON") from exc + if ( + not isinstance(parsed, list) + or len(parsed) > 50 + or any( + not isinstance(note_id, str) + or not note_id.strip() + or len(note_id) > 128 + for note_id in parsed + ) + ): + raise HTTPException(400, "selected_note_ids must be an array of up to 50 non-empty string IDs") + return list(dict.fromkeys(parsed)) + + +def _format_selected_notes_context(notes) -> str: + """Format selected Notes/Todos as reference context for the model.""" + sections = [] + + for index, note in enumerate(notes, start=1): + lines = [ + f"--- Selected Note {index} ---", + f"Title: {note.title or '(untitled)'}", + f"Type: {note.note_type or 'note'}", + ] + + if note.content: + lines.extend([ + "Content:", + str(note.content), + ]) + + if note.items: + try: + items = json.loads(note.items) + except (TypeError, json.JSONDecodeError): + items = [] + + if isinstance(items, list): + lines.append("Items:") + for item in items: + if not isinstance(item, dict): + continue + + status = "done" if item.get("done") else "pending" + text = str(item.get("text") or "").strip() + + if text: + lines.append(f"- [{status}] {text}") + + sections.append("\n".join(lines)) + + return "\n\n".join(sections) def _resolve_prompt_thinking_mode(explicit_mode, preset_id, preset_manager): """Use an explicit request override, then fall back to the active preset.""" @@ -2423,6 +2485,8 @@ def setup_chat_routes( form_data = await request.form() message = form_data.get("message") session = form_data.get("session") + selected_note_ids = _parse_selected_note_ids(form_data.get("selected_note_ids")) + attachments = form_data.get("attachments") use_web = form_data.get("use_web") use_research = form_data.get("use_research") @@ -2730,6 +2794,22 @@ def setup_chat_routes( current_rejected=workspace_rejected, ) owner = effective_user(request) + selected_notes = [] + if selected_note_ids: + notes_db = SessionLocal() + try: + selected_notes = ( + notes_db.query(Note) + .filter( + Note.id.in_(selected_note_ids), + Note.owner == owner, + ) + .all() + ) + finally: + notes_db.close() + selected_notes_context = _format_selected_notes_context(selected_notes) + if tool_approval_id: _reject_delegated_tool_approval(request) from src.agent_runtime.authority import require_user_approval_request @@ -3001,6 +3081,7 @@ def setup_chat_routes( sess, request, chat_handler, chat_processor, message=message, session_id=session, + selected_notes_context=selected_notes_context, preset_id=preset_id, att_ids=att_ids, use_web=use_web, diff --git a/src/agent_loop.py b/src/agent_loop.py index f3609b41b..6301e86a5 100644 --- a/src/agent_loop.py +++ b/src/agent_loop.py @@ -6933,6 +6933,7 @@ _AGENT_RULES = """\ - If the user asks for multiple specific emails and you call `read_email` more than once, your final answer MUST include every successfully read email, clearly separated and linked by UID. Do not answer with only the last email you read. - Multiple email accounts: if tool output says "Other accounts" or the user asks "my Gmail?", "other inbox?", "work mail?", "custom domain mail?", or names any mailbox/account, DO NOT answer from memory. Call `list_email_accounts` if needed, then call `list_emails`/`read_email`/`bulk_email` with the exact `account` value for that mailbox. Account names are user-defined labels; if the user typo-matches a known account, use the closest listed account instead of claiming it does not exist. NEVER use `app_api` or `/api/email/accounts` to discover email accounts; that route is owner-filtered in tool context and can falsely return empty. - User identity facts/preferences ("my name is ", "I live in ", "I prefer concise replies", "call me ") → use `manage_memory` with action=add. NEVER use `manage_contact` for facts about the user unless the user explicitly says to create/update a contact and provides contact details such as an email or phone. +- When the prompt contains "Selected notes and todos", treat that content as already-provided reference context. Use it directly when it answers the request; do not call `manage_notes` to list/search/view the same notes, and do not ask for permission merely to use the selected context. Call `manage_notes` only when the user requests a note change or the selected context is insufficient and a fresh lookup is needed. - "Create/add/write a note" / "notes" / "todos" / "remind me to X at