Optional hidden details in drive search and list
This commit is contained in:
@@ -181,6 +181,7 @@ def build_drive_list_params(
|
||||
drive_id: Optional[str] = None,
|
||||
include_items_from_all_drives: bool = True,
|
||||
corpora: Optional[str] = None,
|
||||
detailed: bool = True,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Helper function to build common list parameters for Drive API calls.
|
||||
@@ -191,14 +192,20 @@ def build_drive_list_params(
|
||||
drive_id: Optional shared drive ID
|
||||
include_items_from_all_drives: Whether to include items from all drives
|
||||
corpora: Optional corpus specification
|
||||
detailed: Whether to request size, modifiedTime, and webViewLink fields.
|
||||
Defaults to True to preserve existing behavior.
|
||||
|
||||
Returns:
|
||||
Dictionary of parameters for Drive API list calls
|
||||
"""
|
||||
if detailed:
|
||||
fields = "nextPageToken, files(id, name, mimeType, webViewLink, iconLink, modifiedTime, size)"
|
||||
else:
|
||||
fields = "nextPageToken, files(id, name, mimeType)"
|
||||
list_params = {
|
||||
"q": query,
|
||||
"pageSize": page_size,
|
||||
"fields": "nextPageToken, files(id, name, mimeType, webViewLink, iconLink, modifiedTime, size)",
|
||||
"fields": fields,
|
||||
"supportsAllDrives": True,
|
||||
"includeItemsFromAllDrives": include_items_from_all_drives,
|
||||
}
|
||||
|
||||
@@ -60,6 +60,7 @@ async def search_drive_files(
|
||||
drive_id: Optional[str] = None,
|
||||
include_items_from_all_drives: bool = True,
|
||||
corpora: Optional[str] = None,
|
||||
detailed: bool = True,
|
||||
) -> str:
|
||||
"""
|
||||
Searches for files and folders within a user's Google Drive, including shared drives.
|
||||
@@ -73,9 +74,10 @@ async def search_drive_files(
|
||||
corpora (Optional[str]): Bodies of items to query (e.g., 'user', 'domain', 'drive', 'allDrives').
|
||||
If 'drive_id' is specified and 'corpora' is None, it defaults to 'drive'.
|
||||
Otherwise, Drive API default behavior applies. Prefer 'user' or 'drive' over 'allDrives' for efficiency.
|
||||
detailed (bool): Whether to include size, modified time, and link in results. Defaults to True.
|
||||
|
||||
Returns:
|
||||
str: A formatted list of found files/folders with their details (ID, name, type, size, modified time, link).
|
||||
str: A formatted list of found files/folders with their details (ID, name, type, and optionally size, modified time, link).
|
||||
"""
|
||||
logger.info(
|
||||
f"[search_drive_files] Invoked. Email: '{user_google_email}', Query: '{query}'"
|
||||
@@ -104,6 +106,7 @@ async def search_drive_files(
|
||||
drive_id=drive_id,
|
||||
include_items_from_all_drives=include_items_from_all_drives,
|
||||
corpora=corpora,
|
||||
detailed=detailed,
|
||||
)
|
||||
|
||||
results = await asyncio.to_thread(service.files().list(**list_params).execute)
|
||||
@@ -115,10 +118,15 @@ async def search_drive_files(
|
||||
f"Found {len(files)} files for {user_google_email} matching '{query}':"
|
||||
]
|
||||
for item in files:
|
||||
size_str = f", Size: {item.get('size', 'N/A')}" if "size" in item else ""
|
||||
formatted_files_text_parts.append(
|
||||
f'- Name: "{item["name"]}" (ID: {item["id"]}, Type: {item["mimeType"]}{size_str}, Modified: {item.get("modifiedTime", "N/A")}) Link: {item.get("webViewLink", "#")}'
|
||||
)
|
||||
if detailed:
|
||||
size_str = f", Size: {item.get('size', 'N/A')}" if "size" in item else ""
|
||||
formatted_files_text_parts.append(
|
||||
f'- Name: "{item["name"]}" (ID: {item["id"]}, Type: {item["mimeType"]}{size_str}, Modified: {item.get("modifiedTime", "N/A")}) Link: {item.get("webViewLink", "#")}'
|
||||
)
|
||||
else:
|
||||
formatted_files_text_parts.append(
|
||||
f'- Name: "{item["name"]}" (ID: {item["id"]}, Type: {item["mimeType"]})'
|
||||
)
|
||||
text_output = "\n".join(formatted_files_text_parts)
|
||||
return text_output
|
||||
|
||||
@@ -414,6 +422,7 @@ async def list_drive_items(
|
||||
drive_id: Optional[str] = None,
|
||||
include_items_from_all_drives: bool = True,
|
||||
corpora: Optional[str] = None,
|
||||
detailed: bool = True,
|
||||
) -> str:
|
||||
"""
|
||||
Lists files and folders, supporting shared drives.
|
||||
@@ -427,6 +436,7 @@ async def list_drive_items(
|
||||
drive_id (Optional[str]): ID of the shared drive. If provided, the listing is scoped to this drive.
|
||||
include_items_from_all_drives (bool): Whether items from all accessible shared drives should be included if `drive_id` is not set. Defaults to True.
|
||||
corpora (Optional[str]): Corpus to query ('user', 'drive', 'allDrives'). If `drive_id` is set and `corpora` is None, 'drive' is used. If None and no `drive_id`, API defaults apply.
|
||||
detailed (bool): Whether to include size, modified time, and link in results. Defaults to True.
|
||||
|
||||
Returns:
|
||||
str: A formatted list of files/folders in the specified folder.
|
||||
@@ -444,6 +454,7 @@ async def list_drive_items(
|
||||
drive_id=drive_id,
|
||||
include_items_from_all_drives=include_items_from_all_drives,
|
||||
corpora=corpora,
|
||||
detailed=detailed,
|
||||
)
|
||||
|
||||
results = await asyncio.to_thread(service.files().list(**list_params).execute)
|
||||
@@ -455,10 +466,15 @@ async def list_drive_items(
|
||||
f"Found {len(files)} items in folder '{folder_id}' for {user_google_email}:"
|
||||
]
|
||||
for item in files:
|
||||
size_str = f", Size: {item.get('size', 'N/A')}" if "size" in item else ""
|
||||
formatted_items_text_parts.append(
|
||||
f'- Name: "{item["name"]}" (ID: {item["id"]}, Type: {item["mimeType"]}{size_str}, Modified: {item.get("modifiedTime", "N/A")}) Link: {item.get("webViewLink", "#")}'
|
||||
)
|
||||
if detailed:
|
||||
size_str = f", Size: {item.get('size', 'N/A')}" if "size" in item else ""
|
||||
formatted_items_text_parts.append(
|
||||
f'- Name: "{item["name"]}" (ID: {item["id"]}, Type: {item["mimeType"]}{size_str}, Modified: {item.get("modifiedTime", "N/A")}) Link: {item.get("webViewLink", "#")}'
|
||||
)
|
||||
else:
|
||||
formatted_items_text_parts.append(
|
||||
f'- Name: "{item["name"]}" (ID: {item["id"]}, Type: {item["mimeType"]})'
|
||||
)
|
||||
text_output = "\n".join(formatted_items_text_parts)
|
||||
return text_output
|
||||
|
||||
|
||||
Reference in New Issue
Block a user