Incremental engine for long horizon agents 🌟 Star if you like it!
Rust agentic-data-frameworkaiai-agentschange-data-capturecodebase-intelligencecontext-engineeringdata-engineeringdata-indexingdata-processingetlhelp-wantedindexingknowledge-graphllmlong-horizon-agentpythonragreal-timerustsemantic-search
fix(google_drive): index folders and files that live in a shared drive (#2815)
Alex Castillo González9ee204877d03
diff --git a/examples/cocoindex_agents/tests/fake_drive.py b/examples/cocoindex_agents/tests/fake_drive.pyindex 02e8656f..cb080bd9 100644--- a/examples/cocoindex_agents/tests/fake_drive.py+++ b/examples/cocoindex_agents/tests/fake_drive.py@@ -108,7 +108,15 @@ class _Request:class _Files:drive: FakeDrive- def list(self, *, q: str, fields: str, pageToken: str | None) -> _Request:+ def list(+ self,+ *,+ q: str,+ fields: str,+ pageToken: str | None,+ includeItemsFromAllDrives: bool,+ supportsAllDrives: bool,+ ) -> _Request:self.drive.lists += 1folder_id = q.split("'")[1]return _Request(@@ -121,10 +129,10 @@ class _Files:})- def get(self, *, fileId: str, fields: str) -> _Request:+ def get(self, *, fileId: str, fields: str, supportsAllDrives: bool) -> _Request:return _Request(self.drive._metadata(fileId, self.drive.entries[fileId]))- def get_media(self, *, fileId: str) -> _Request:+ def get_media(self, *, fileId: str, supportsAllDrives: bool) -> _Request:self.drive.downloads += 1return _Request(self.drive.entries[fileId].content)diff --git a/python/cocoindex/connectors/google_drive/_source.py b/python/cocoindex/connectors/google_drive/_source.pyindex 64835723..f6d62515 100644--- a/python/cocoindex/connectors/google_drive/_source.py+++ b/python/cocoindex/connectors/google_drive/_source.py@@ -143,6 +143,7 @@ class DriveFile(file.FileLike[str]):.get(fileId=self._info.file_id,fields="size, modifiedTime, md5Checksum",+ supportsAllDrives=True,).execute(num_retries=_RETRIES))@@ -168,7 +169,9 @@ class DriveFile(file.FileLike[str]):fileId=file_id, mimeType=export_mime)else:- request = self._service.files().get_media(fileId=file_id)+ request = self._service.files().get_media(+ fileId=file_id, supportsAllDrives=True+ )content: bytes = request.execute(num_retries=_RETRIES)return content@@ -254,6 +257,10 @@ def _list_children(service: Any, folder_id: str) -> list[DriveFileInfo]:" files(id, name, mimeType, size, modifiedTime, md5Checksum)"),pageToken=page_token,+ # Without these, Drive lists no children of a folder that+ # lives in a shared drive.+ includeItemsFromAllDrives=True,+ supportsAllDrives=True,).execute(num_retries=_RETRIES))diff --git a/python/tests/connectors/test_google_drive_source.py b/python/tests/connectors/test_google_drive_source.pyindex 579be453..71f58f7f 100644--- a/python/tests/connectors/test_google_drive_source.py+++ b/python/tests/connectors/test_google_drive_source.py@@ -242,7 +242,15 @@ class _FakeDriveFiles:def __init__(self, children: dict[str, list[dict[str, str]]]) -> None:self.children = children- def list(self, *, q: str, fields: str, pageToken: str | None) -> _FakeRequest:+ def list(+ self,+ *,+ q: str,+ fields: str,+ pageToken: str | None,+ includeItemsFromAllDrives: bool,+ supportsAllDrives: bool,+ ) -> _FakeRequest:folder_id = q.split("'")[1]return _FakeRequest({"files": self.children[folder_id]})@@ -437,3 +445,40 @@ def test_a_rate_limited_listing_is_retried(monkeypatch: pytest.MonkeyPatch) -> Nfiles = list(list_files(GoogleDriveSourceSpec("unused.json", ["root"])))assert [f.file_path.resolve() for f in files] == ["f1"]assert len(sleeps) == 2 # one backoff per refused attempt+++@requires_google_drive+async def test_requests_cover_shared_drives(monkeypatch: pytest.MonkeyPatch) -> None:+ """A folder in a shared drive lists nothing, and its files give a 404, unless asked for."""+ import json+ from urllib.parse import parse_qs, urlsplit++ from googleapiclient.discovery import build+ from googleapiclient.http import HttpMockSequence++ from cocoindex.connectors.google_drive import _source++ listing = {"files": [_drive_entry("f1", "a.bin", size="3", md5Checksum="abc")]}+ http = HttpMockSequence(+ [+ ({"status": "200"}, json.dumps(listing)),+ ({"status": "200"}, b"abc"),+ ({"status": "200"}, json.dumps({"size": "3"})),+ ]+ )+ service = build("drive", "v3", http=http, static_discovery=True)+ monkeypatch.setattr(_source, "_build_service", lambda _: service)++ [drive_file] = list_files(GoogleDriveSourceSpec("unused.json", ["root"]))+ assert await drive_file.read() == b"abc"+ assert (await drive_file._fetch_metadata()).size == 3++ list_query, media_query, get_query = (+ parse_qs(urlsplit(request[0]).query) for request in http.request_sequence+ )+ assert list_query["includeItemsFromAllDrives"] == ["true"]+ assert list_query["supportsAllDrives"] == ["true"]+ assert media_query["alt"] == ["media"]+ assert media_query["supportsAllDrives"] == ["true"]+ assert get_query["supportsAllDrives"] == ["true"]