Personal AI, On Personal Devices
Python
feat: wire Gmail since param to after: search operator
krypticmousedc4c77331641
diff --git a/src/openjarvis/connectors/gmail.py b/src/openjarvis/connectors/gmail.pyindex d486cfb1..5c0a9959 100644--- a/src/openjarvis/connectors/gmail.py+++ b/src/openjarvis/connectors/gmail.py@@ -31,9 +31,7 @@ from openjarvis.tools._stubs import ToolSpec_GMAIL_API_BASE = "https://gmail.googleapis.com/gmail/v1/users/me"_GMAIL_SCOPE = "https://www.googleapis.com/auth/gmail.readonly"-_DEFAULT_CREDENTIALS_PATH = str(- DEFAULT_CONFIG_DIR / "connectors" / "gmail.json"-)+_DEFAULT_CREDENTIALS_PATH = str(DEFAULT_CONFIG_DIR / "connectors" / "gmail.json")# ---------------------------------------------------------------------------# Module-level API functions (easy to patch in tests)@@ -227,7 +225,7 @@ class GmailConnector(BaseConnector):def sync(self,*,- since: Optional[datetime] = None, # noqa: ARG002 — reserved for future use+ since: Optional[datetime] = None,cursor: Optional[str] = None,) -> Iterator[Document]:"""Yield :class:`Document` objects for Gmail messages.@@ -238,7 +236,8 @@ class GmailConnector(BaseConnector):Parameters----------since:- Not yet used (Gmail API filtering is done server-side via query).+ When provided, only messages received after this timestamp are+ returned. Translated to a Gmail ``after:<epoch>`` search query.cursor:``nextPageToken`` from a previous sync to resume pagination."""@@ -250,11 +249,19 @@ class GmailConnector(BaseConnector):if not token:return+ query = ""+ if since is not None:+ # Gmail's after: operator accepts Unix epoch seconds.+ epoch = int(since.timestamp())+ query = f"after:{epoch}"+page_token: Optional[str] = cursorsynced = 0while True:- list_resp = _gmail_api_list_messages(token, page_token=page_token)+ list_resp = _gmail_api_list_messages(+ token, page_token=page_token, query=query+ )messages: List[Dict[str, Any]] = list_resp.get("messages", [])for msg_stub in messages:@@ -352,9 +359,7 @@ class GmailConnector(BaseConnector):),ToolSpec(name="gmail_get_thread",- description=(- "Retrieve all messages in a Gmail thread by thread ID."- ),+ description=("Retrieve all messages in a Gmail thread by thread ID."),parameters={"type": "object","properties": {diff --git a/tests/connectors/test_gmail.py b/tests/connectors/test_gmail.pyindex e7272774..3e34f7e2 100644--- a/tests/connectors/test_gmail.py+++ b/tests/connectors/test_gmail.py@@ -130,15 +130,11 @@ def test_sync_yields_documents("""sync() yields one Document per message with correct metadata."""# Set up fake credentials so is_connected() returns Truecreds_path = Path(connector._credentials_path)- creds_path.write_text(- json.dumps({"token": "fake-access-token"}), encoding="utf-8"- )+ creds_path.write_text(json.dumps({"token": "fake-access-token"}), encoding="utf-8")# Configure mocksmock_list.return_value = _LIST_RESPONSE- mock_get.side_effect = lambda token, msg_id: (- _MSG1 if msg_id == "msg1" else _MSG2- )+ mock_get.side_effect = lambda token, msg_id: _MSG1 if msg_id == "msg1" else _MSG2docs: List[Document] = list(connector.sync())@@ -174,9 +170,7 @@ def test_sync_yields_documents(def test_disconnect(connector, tmp_path: Path) -> None:"""disconnect() deletes the credentials file."""creds_path = Path(connector._credentials_path)- creds_path.write_text(- json.dumps({"token": "fake-access-token"}), encoding="utf-8"- )+ creds_path.write_text(json.dumps({"token": "fake-access-token"}), encoding="utf-8")assert connector.is_connected() is Trueconnector.disconnect()@@ -201,7 +195,66 @@ def test_mcp_tools(connector) -> None:# ----------------------------------------------------------------------------# Test 7 — ConnectorRegistry contains "gmail" after import+# Test 7 — sync passes since as an after: query to the list messages API+# ---------------------------------------------------------------------------+++@patch("openjarvis.connectors.gmail._gmail_api_list_messages")+@patch("openjarvis.connectors.gmail._gmail_api_get_message")+def test_sync_passes_since_as_query(+ mock_get,+ mock_list,+ connector,+ tmp_path: Path,+) -> None:+ """sync(since=...) passes an after:<epoch> query to _gmail_api_list_messages."""+ from datetime import datetime, timezone # noqa: PLC0415++ creds_path = Path(connector._credentials_path)+ creds_path.write_text(json.dumps({"token": "fake-access-token"}), encoding="utf-8")++ mock_list.return_value = {"messages": []} # no messages needed for this test++ since_dt = datetime(2024, 6, 1, 0, 0, 0, tzinfo=timezone.utc)+ list(connector.sync(since=since_dt))++ mock_list.assert_called_once()+ _, call_kwargs = mock_list.call_args+ assert "query" in call_kwargs+ assert call_kwargs["query"].startswith("after:")+ # Verify the epoch value is correct+ expected_epoch = int(since_dt.timestamp())+ assert call_kwargs["query"] == f"after:{expected_epoch}"+++# ---------------------------------------------------------------------------+# Test 8 — sync without since passes an empty query+# ---------------------------------------------------------------------------+++@patch("openjarvis.connectors.gmail._gmail_api_list_messages")+@patch("openjarvis.connectors.gmail._gmail_api_get_message")+def test_sync_without_since_passes_empty_query(+ mock_get,+ mock_list,+ connector,+ tmp_path: Path,+) -> None:+ """sync() without since= passes an empty query string."""+ creds_path = Path(connector._credentials_path)+ creds_path.write_text(json.dumps({"token": "fake-access-token"}), encoding="utf-8")++ mock_list.return_value = {"messages": []}++ list(connector.sync())++ mock_list.assert_called_once()+ _, call_kwargs = mock_list.call_args+ assert call_kwargs.get("query", "") == ""+++# ---------------------------------------------------------------------------+# Test 9 — ConnectorRegistry contains "gmail" after import# ---------------------------------------------------------------------------