11import { createLogger } from '@sim/logger'
2- import type { GoogleDriveDownloadResponse , GoogleDriveToolParams } from '@/tools/google_drive/types'
2+ import type {
3+ GoogleDriveDownloadResponse ,
4+ GoogleDriveFile ,
5+ GoogleDriveRevision ,
6+ GoogleDriveToolParams ,
7+ } from '@/tools/google_drive/types'
38import { DEFAULT_EXPORT_FORMATS , GOOGLE_WORKSPACE_MIME_TYPES } from '@/tools/google_drive/utils'
49import type { ToolConfig } from '@/tools/types'
510
611const logger = createLogger ( 'GoogleDriveDownloadTool' )
712
13+ // All available file metadata fields from Google Drive API v3
14+ const ALL_FILE_FIELDS = [
15+ // Basic Info
16+ 'id' ,
17+ 'name' ,
18+ 'mimeType' ,
19+ 'kind' ,
20+ 'description' ,
21+ 'originalFilename' ,
22+ 'fullFileExtension' ,
23+ 'fileExtension' ,
24+ // Ownership & Sharing
25+ 'owners' ,
26+ 'permissions' ,
27+ 'permissionIds' ,
28+ 'shared' ,
29+ 'ownedByMe' ,
30+ 'writersCanShare' ,
31+ 'viewersCanCopyContent' ,
32+ 'copyRequiresWriterPermission' ,
33+ 'sharingUser' ,
34+ // Labels/Tags
35+ 'starred' ,
36+ 'trashed' ,
37+ 'explicitlyTrashed' ,
38+ 'properties' ,
39+ 'appProperties' ,
40+ 'folderColorRgb' ,
41+ // Timestamps
42+ 'createdTime' ,
43+ 'modifiedTime' ,
44+ 'modifiedByMeTime' ,
45+ 'viewedByMeTime' ,
46+ 'sharedWithMeTime' ,
47+ 'trashedTime' ,
48+ // User Info
49+ 'lastModifyingUser' ,
50+ 'trashingUser' ,
51+ 'viewedByMe' ,
52+ 'modifiedByMe' ,
53+ // Links
54+ 'webViewLink' ,
55+ 'webContentLink' ,
56+ 'iconLink' ,
57+ 'thumbnailLink' ,
58+ 'exportLinks' ,
59+ // Size & Storage
60+ 'size' ,
61+ 'quotaBytesUsed' ,
62+ // Checksums
63+ 'md5Checksum' ,
64+ 'sha1Checksum' ,
65+ 'sha256Checksum' ,
66+ // Hierarchy & Location
67+ 'parents' ,
68+ 'spaces' ,
69+ 'driveId' ,
70+ 'teamDriveId' ,
71+ // Capabilities
72+ 'capabilities' ,
73+ // Versions
74+ 'version' ,
75+ 'headRevisionId' ,
76+ // Media Metadata
77+ 'hasThumbnail' ,
78+ 'thumbnailVersion' ,
79+ 'imageMediaMetadata' ,
80+ 'videoMediaMetadata' ,
81+ 'contentHints' ,
82+ // Other
83+ 'isAppAuthorized' ,
84+ 'contentRestrictions' ,
85+ 'resourceKey' ,
86+ 'shortcutDetails' ,
87+ 'linkShareMetadata' ,
88+ ] . join ( ',' )
89+
90+ // All revision fields
91+ const ALL_REVISION_FIELDS = [
92+ 'id' ,
93+ 'mimeType' ,
94+ 'modifiedTime' ,
95+ 'keepForever' ,
96+ 'published' ,
97+ 'publishAuto' ,
98+ 'publishedLink' ,
99+ 'publishedOutsideDomain' ,
100+ 'lastModifyingUser' ,
101+ 'originalFilename' ,
102+ 'md5Checksum' ,
103+ 'size' ,
104+ 'exportLinks' ,
105+ 'kind' ,
106+ ] . join ( ',' )
107+
8108export const downloadTool : ToolConfig < GoogleDriveToolParams , GoogleDriveDownloadResponse > = {
9109 id : 'google_drive_download' ,
10110 name : 'Download File from Google Drive' ,
11- description : 'Download a file from Google Drive (exports Google Workspace files automatically)' ,
111+ description :
112+ 'Download a file from Google Drive with complete metadata (exports Google Workspace files automatically)' ,
12113 version : '1.0' ,
13114
14115 oauth : {
@@ -41,11 +142,17 @@ export const downloadTool: ToolConfig<GoogleDriveToolParams, GoogleDriveDownload
41142 visibility : 'user-only' ,
42143 description : 'Optional filename override' ,
43144 } ,
145+ includeRevisions : {
146+ type : 'boolean' ,
147+ required : false ,
148+ visibility : 'user-or-llm' ,
149+ description : 'Whether to include revision history in the metadata (default: true)' ,
150+ } ,
44151 } ,
45152
46153 request : {
47154 url : ( params ) =>
48- `https://www.googleapis.com/drive/v3/files/${ params . fileId } ?fields=id,name,mimeType &supportsAllDrives=true` ,
155+ `https://www.googleapis.com/drive/v3/files/${ params . fileId } ?fields=${ ALL_FILE_FIELDS } &supportsAllDrives=true` ,
49156 method : 'GET' ,
50157 headers : ( params ) => ( {
51158 Authorization : `Bearer ${ params . accessToken } ` ,
@@ -64,7 +171,7 @@ export const downloadTool: ToolConfig<GoogleDriveToolParams, GoogleDriveDownload
64171 throw new Error ( errorDetails . error ?. message || 'Failed to get file metadata' )
65172 }
66173
67- const metadata = await response . json ( )
174+ const metadata : GoogleDriveFile = await response . json ( )
68175 const fileId = metadata . id
69176 const mimeType = metadata . mimeType
70177 const authHeader = `Bearer ${ params ?. accessToken || '' } `
@@ -132,13 +239,49 @@ export const downloadTool: ToolConfig<GoogleDriveToolParams, GoogleDriveDownload
132239 fileBuffer = Buffer . from ( arrayBuffer )
133240 }
134241
242+ // Fetch revisions if requested (default: true)
243+ const includeRevisions = params ?. includeRevisions !== false
244+ if ( includeRevisions ) {
245+ try {
246+ const revisionsResponse = await fetch (
247+ `https://www.googleapis.com/drive/v3/files/${ fileId } /revisions?fields=revisions(${ ALL_REVISION_FIELDS } )&pageSize=100` ,
248+ {
249+ headers : {
250+ Authorization : authHeader ,
251+ } ,
252+ }
253+ )
254+
255+ if ( revisionsResponse . ok ) {
256+ const revisionsData = await revisionsResponse . json ( )
257+ metadata . revisions = revisionsData . revisions as GoogleDriveRevision [ ]
258+ logger . info ( 'Fetched file revisions' , {
259+ fileId,
260+ revisionCount : metadata . revisions ?. length || 0 ,
261+ } )
262+ } else {
263+ logger . warn ( 'Failed to fetch revisions, continuing without them' , {
264+ status : revisionsResponse . status ,
265+ statusText : revisionsResponse . statusText ,
266+ } )
267+ }
268+ } catch ( revisionError : any ) {
269+ logger . warn ( 'Error fetching revisions, continuing without them' , {
270+ error : revisionError . message ,
271+ } )
272+ }
273+ }
274+
135275 const resolvedName = params ?. fileName || metadata . name || 'download'
136276
137277 logger . info ( 'File downloaded successfully' , {
138278 fileId,
139279 name : resolvedName ,
140280 size : fileBuffer . length ,
141281 mimeType : finalMimeType ,
282+ hasOwners : ! ! metadata . owners ?. length ,
283+ hasPermissions : ! ! metadata . permissions ?. length ,
284+ hasRevisions : ! ! metadata . revisions ?. length ,
142285 } )
143286
144287 return {
@@ -150,6 +293,7 @@ export const downloadTool: ToolConfig<GoogleDriveToolParams, GoogleDriveDownload
150293 data : fileBuffer ,
151294 size : fileBuffer . length ,
152295 } ,
296+ metadata,
153297 } ,
154298 }
155299 } catch ( error : any ) {
@@ -163,5 +307,10 @@ export const downloadTool: ToolConfig<GoogleDriveToolParams, GoogleDriveDownload
163307
164308 outputs : {
165309 file : { type : 'file' , description : 'Downloaded file stored in execution files' } ,
310+ metadata : {
311+ type : 'json' ,
312+ description :
313+ 'Complete file metadata including ownership, sharing, permissions, labels, checksums, capabilities, and revision history' ,
314+ } ,
166315 } ,
167316}
0 commit comments