@mastra/google-drive 0.2.0 → 0.2.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,519 +1,557 @@
1
- 'use strict';
2
-
3
- var crypto = require('crypto');
4
- var workspace = require('@mastra/core/workspace');
5
-
6
- // src/filesystem/index.ts
7
- var DRIVE_API = "https://www.googleapis.com/drive/v3";
8
- var DRIVE_UPLOAD_API = "https://www.googleapis.com/upload/drive/v3";
9
- var OAUTH_TOKEN_URL = "https://oauth2.googleapis.com/token";
10
- var FOLDER_MIME_TYPE = "application/vnd.google-apps.folder";
11
- var DEFAULT_SCOPES = ["https://www.googleapis.com/auth/drive"];
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let crypto = require("crypto");
3
+ let _mastra_core_workspace = require("@mastra/core/workspace");
4
+ //#region src/filesystem/index.ts
5
+ const DRIVE_API = "https://www.googleapis.com/drive/v3";
6
+ const DRIVE_UPLOAD_API = "https://www.googleapis.com/upload/drive/v3";
7
+ const OAUTH_TOKEN_URL = "https://oauth2.googleapis.com/token";
8
+ const FOLDER_MIME_TYPE = "application/vnd.google-apps.folder";
9
+ const DEFAULT_SCOPES = ["https://www.googleapis.com/auth/drive"];
10
+ /**
11
+ * Resolve an instructions override against default instructions.
12
+ *
13
+ * - `undefined` → return default
14
+ * - `string` → return the string as-is
15
+ * - `function` → call with { defaultInstructions, requestContext }
16
+ */
12
17
  function resolveInstructions(override, getDefault, requestContext) {
13
- if (typeof override === "string") return override;
14
- const defaultInstructions = getDefault();
15
- if (override === void 0) return defaultInstructions;
16
- return override({ defaultInstructions, requestContext });
18
+ if (typeof override === "string") return override;
19
+ const defaultInstructions = getDefault();
20
+ if (override === void 0) return defaultInstructions;
21
+ return override({
22
+ defaultInstructions,
23
+ requestContext
24
+ });
17
25
  }
18
- var GoogleDriveFilesystem = class extends workspace.MastraFilesystem {
19
- id;
20
- name = "GoogleDriveFilesystem";
21
- provider = "google-drive";
22
- readOnly;
23
- icon = "drive";
24
- displayName = "Google Drive";
25
- status = "pending";
26
- accessToken;
27
- tokenExpiresAt = 0;
28
- tokenRefreshPromise;
29
- folderId;
30
- getAccessToken;
31
- serviceAccount;
32
- instructionsOverride;
33
- constructor(options) {
34
- super({ name: "GoogleDriveFilesystem", ...options });
35
- this.id = options.id ?? `google-drive:${options.folderId}`;
36
- this.folderId = options.folderId;
37
- this.accessToken = options.accessToken;
38
- this.getAccessToken = options.getAccessToken;
39
- this.serviceAccount = options.serviceAccount;
40
- this.readOnly = options.readOnly;
41
- this.instructionsOverride = options.instructions;
42
- }
43
- async init() {
44
- const driveFile = await this.request(`${DRIVE_API}/files/${encodeURIComponent(this.folderId)}`, {
45
- searchParams: { fields: "id,name,mimeType,trashed", supportsAllDrives: "true" }
46
- });
47
- if (driveFile.trashed) {
48
- throw new Error(`Google Drive folder ${this.folderId} is trashed and cannot be used as a filesystem root.`);
49
- }
50
- if (driveFile.mimeType !== FOLDER_MIME_TYPE) {
51
- throw new Error(
52
- `Google Drive root ${this.folderId} must be a folder, but received mimeType ${driveFile.mimeType ?? "unknown"}.`
53
- );
54
- }
55
- }
56
- async destroy() {
57
- }
58
- async isReady() {
59
- return this.status === "ready";
60
- }
61
- getInfo() {
62
- return {
63
- id: this.id,
64
- name: this.name,
65
- provider: this.provider,
66
- status: this.status,
67
- error: this.error,
68
- readOnly: this.readOnly,
69
- icon: this.icon,
70
- metadata: { folderId: this.folderId }
71
- };
72
- }
73
- getInstructions(opts) {
74
- const defaultInstructions = [
75
- "Google Drive filesystem mounted to a single folder.",
76
- "Use POSIX-style paths relative to that folder, for example /notes/todo.txt.",
77
- "Directories are Google Drive folders. File names must be unique within each folder for path-based operations.",
78
- this.readOnly ? "This Google Drive filesystem is read-only." : "You can read, create, update, move, copy, and delete files in this folder."
79
- ].join("\n");
80
- return resolveInstructions(this.instructionsOverride, () => defaultInstructions, opts?.requestContext);
81
- }
82
- async readFile(path, options) {
83
- await this.ensureReady();
84
- const file = await this.getFile(path);
85
- if (file.mimeType === FOLDER_MIME_TYPE) throw new workspace.IsDirectoryError(path);
86
- const response = await this.fetch(`${DRIVE_API}/files/${encodeURIComponent(file.id)}`, {
87
- method: "GET",
88
- searchParams: { alt: "media" }
89
- });
90
- const buffer = Buffer.from(await response.arrayBuffer());
91
- return options?.encoding ? buffer.toString(options.encoding) : buffer;
92
- }
93
- async writeFile(path, content, options) {
94
- await this.ensureReady();
95
- this.assertWritable("writeFile");
96
- const existing = await this.findFile(path);
97
- if (existing) {
98
- if (existing.mimeType === FOLDER_MIME_TYPE) throw new workspace.IsDirectoryError(path);
99
- if (options?.overwrite === false) throw new workspace.FileExistsError(path);
100
- if (options?.expectedMtime) {
101
- if (!existing.modifiedTime) throw new workspace.StaleFileError(path, options.expectedMtime, /* @__PURE__ */ new Date(0));
102
- const actual = new Date(existing.modifiedTime);
103
- if (actual.getTime() !== options.expectedMtime.getTime())
104
- throw new workspace.StaleFileError(path, options.expectedMtime, actual);
105
- }
106
- await this.upload(existing.id, content, options?.mimeType, "PATCH");
107
- return;
108
- }
109
- const { parentId, name } = await this.resolveParent(path, options?.recursive ?? true);
110
- await this.upload(void 0, content, options?.mimeType, "POST", { name, parents: [parentId] });
111
- }
112
- async appendFile(path, content) {
113
- await this.ensureReady();
114
- this.assertWritable("appendFile");
115
- const existing = await this.findFile(path);
116
- if (existing) {
117
- if (existing.mimeType === FOLDER_MIME_TYPE) throw new workspace.IsDirectoryError(path);
118
- const current = await this.readFile(path);
119
- const expectedMtime = existing.modifiedTime ? new Date(existing.modifiedTime) : void 0;
120
- await this.writeFile(path, Buffer.concat([this.toBuffer(current), this.toBuffer(content)]), {
121
- expectedMtime
122
- });
123
- } else {
124
- await this.writeFile(path, content, { recursive: true });
125
- }
126
- }
127
- async deleteFile(path, options) {
128
- await this.ensureReady();
129
- this.assertWritable("deleteFile");
130
- const file = await this.findFile(path);
131
- if (!file) {
132
- if (options?.force) return;
133
- throw new workspace.FileNotFoundError(path);
134
- }
135
- if (file.mimeType === FOLDER_MIME_TYPE) throw new workspace.IsDirectoryError(path);
136
- await this.request(`${DRIVE_API}/files/${encodeURIComponent(file.id)}`, { method: "DELETE" });
137
- }
138
- async copyFile(src, dest, options) {
139
- await this.ensureReady();
140
- this.assertWritable("copyFile");
141
- const source = await this.getFile(src);
142
- if (source.mimeType === FOLDER_MIME_TYPE) throw new workspace.IsDirectoryError(src);
143
- const existing = await this.findFile(dest);
144
- if (existing) {
145
- if (existing.id === source.id) throw new workspace.FileExistsError(dest);
146
- if (existing.mimeType === FOLDER_MIME_TYPE || options?.overwrite === false) throw new workspace.FileExistsError(dest);
147
- await this.deleteAny(existing, dest, true);
148
- }
149
- const { parentId, name } = await this.resolveParent(dest, options?.recursive ?? true);
150
- await this.request(`${DRIVE_API}/files/${encodeURIComponent(source.id)}/copy`, {
151
- method: "POST",
152
- body: JSON.stringify({ name, parents: [parentId] })
153
- });
154
- }
155
- async moveFile(src, dest, options) {
156
- await this.ensureReady();
157
- this.assertWritable("moveFile");
158
- const source = await this.getFile(src);
159
- if (options?.overwrite === false && await this.exists(dest)) throw new workspace.FileExistsError(dest);
160
- const existing = await this.findFile(dest);
161
- if (existing && existing.id !== source.id) {
162
- if (existing.mimeType === FOLDER_MIME_TYPE || options?.overwrite === false) throw new workspace.FileExistsError(dest);
163
- await this.deleteAny(existing, dest, true);
164
- }
165
- const { parentId, name } = await this.resolveParent(dest, options?.recursive ?? true);
166
- const searchParams = { addParents: parentId, fields: "id", supportsAllDrives: "true" };
167
- const oldParents = source.parents?.join(",");
168
- if (oldParents) searchParams.removeParents = oldParents;
169
- await this.request(`${DRIVE_API}/files/${encodeURIComponent(source.id)}`, {
170
- method: "PATCH",
171
- searchParams,
172
- body: JSON.stringify({ name })
173
- });
174
- }
175
- async mkdir(path, options) {
176
- await this.ensureReady();
177
- this.assertWritable("mkdir");
178
- if (this.normalize(path) === "/") return;
179
- const existing = await this.findFile(path);
180
- if (existing) {
181
- if (existing.mimeType !== FOLDER_MIME_TYPE) throw new workspace.FileExistsError(path);
182
- return;
183
- }
184
- const { parentId, name } = await this.resolveParent(path, options?.recursive ?? true);
185
- await this.createFolder(parentId, name);
186
- }
187
- async rmdir(path, options) {
188
- await this.ensureReady();
189
- this.assertWritable("rmdir");
190
- const dir = await this.findFile(path);
191
- if (!dir) {
192
- if (options?.force) return;
193
- throw new workspace.DirectoryNotFoundError(path);
194
- }
195
- if (dir.mimeType !== FOLDER_MIME_TYPE) throw new workspace.NotDirectoryError(path);
196
- if (!options?.recursive) {
197
- const children = await this.listChildren(dir.id);
198
- if (children.length) throw new workspace.DirectoryNotEmptyError(path);
199
- }
200
- await this.request(`${DRIVE_API}/files/${encodeURIComponent(dir.id)}`, { method: "DELETE" });
201
- }
202
- async readdir(path, options) {
203
- await this.ensureReady();
204
- const dir = await this.getFile(path);
205
- if (dir.mimeType !== FOLDER_MIME_TYPE) throw new workspace.NotDirectoryError(path);
206
- const entries = await this.readdirRecursive(dir.id, options, 0);
207
- const extensions = Array.isArray(options?.extension) ? options.extension : options?.extension ? [options.extension] : void 0;
208
- return extensions ? entries.filter((entry) => entry.type === "directory" || extensions.some((ext) => entry.name.endsWith(ext))) : entries;
209
- }
210
- async exists(path) {
211
- await this.ensureReady();
212
- return Boolean(await this.findFile(path));
213
- }
214
- async stat(path) {
215
- await this.ensureReady();
216
- const file = await this.getFile(path);
217
- const isDirectory = file.mimeType === FOLDER_MIME_TYPE;
218
- return {
219
- name: file.name,
220
- path: this.normalize(path),
221
- type: isDirectory ? "directory" : "file",
222
- size: Number(file.size ?? 0),
223
- createdAt: file.createdTime ? new Date(file.createdTime) : /* @__PURE__ */ new Date(0),
224
- modifiedAt: file.modifiedTime ? new Date(file.modifiedTime) : /* @__PURE__ */ new Date(0),
225
- mimeType: isDirectory ? void 0 : file.mimeType
226
- };
227
- }
228
- async realpath(path) {
229
- return this.normalize(path);
230
- }
231
- assertWritable(operation) {
232
- if (this.readOnly) throw new workspace.WorkspaceReadOnlyError(operation);
233
- }
234
- toBuffer(content) {
235
- if (Buffer.isBuffer(content)) return content;
236
- if (content instanceof Uint8Array) return Buffer.from(content);
237
- return Buffer.from(content, "utf-8");
238
- }
239
- normalize(path) {
240
- const parts = path.split("/").filter(Boolean);
241
- const stack = [];
242
- for (const part of parts) {
243
- if (part === ".") continue;
244
- if (part === "..") stack.pop();
245
- else stack.push(part);
246
- }
247
- return `/${stack.join("/")}`;
248
- }
249
- async getFile(path) {
250
- const file = await this.findFile(path);
251
- if (!file) throw new workspace.FileNotFoundError(path);
252
- return file;
253
- }
254
- async findFile(path) {
255
- const normalized = this.normalize(path);
256
- if (normalized === "/") return this.rootFile();
257
- const names = normalized.split("/").filter(Boolean);
258
- let parentId = this.folderId;
259
- let file;
260
- for (const name of names) {
261
- file = await this.findChild(parentId, name);
262
- if (!file) return void 0;
263
- parentId = file.id;
264
- }
265
- return file;
266
- }
267
- async rootFile() {
268
- return this.request(`${DRIVE_API}/files/${encodeURIComponent(this.folderId)}`, {
269
- searchParams: { fields: "id,name,mimeType,size,createdTime,modifiedTime,parents", supportsAllDrives: "true" }
270
- });
271
- }
272
- async resolveParent(path, recursive) {
273
- const normalized = this.normalize(path);
274
- const parts = normalized.split("/").filter(Boolean);
275
- const name = parts.pop();
276
- if (!name) throw new workspace.IsDirectoryError(path);
277
- const parentPath = `/${parts.join("/")}`;
278
- const parent = recursive ? await this.resolveDir(parentPath, true) : await this.findFile(parentPath);
279
- if (!parent) throw new workspace.DirectoryNotFoundError(parentPath);
280
- if (parent.mimeType !== FOLDER_MIME_TYPE) throw new workspace.NotDirectoryError(parentPath);
281
- return { parentId: parent.id, name };
282
- }
283
- async resolveDir(path, recursive) {
284
- const normalized = this.normalize(path);
285
- if (normalized === "/") return this.rootFile();
286
- const names = normalized.split("/").filter(Boolean);
287
- let parentId = this.folderId;
288
- let current;
289
- for (const name of names) {
290
- current = await this.findChild(parentId, name);
291
- if (current) {
292
- if (current.mimeType !== FOLDER_MIME_TYPE) throw new workspace.NotDirectoryError(name);
293
- parentId = current.id;
294
- continue;
295
- }
296
- if (!recursive) throw new workspace.DirectoryNotFoundError(normalized);
297
- current = await this.createFolder(parentId, name);
298
- parentId = current.id;
299
- }
300
- return current;
301
- }
302
- async createFolder(parentId, name) {
303
- return this.request(`${DRIVE_API}/files`, {
304
- method: "POST",
305
- searchParams: { fields: "id,name,mimeType,size,createdTime,modifiedTime,parents", supportsAllDrives: "true" },
306
- body: JSON.stringify({ name, mimeType: FOLDER_MIME_TYPE, parents: [parentId] })
307
- });
308
- }
309
- async findChild(parentId, name) {
310
- const files = await this.listChildren(parentId, `name = '${this.escapeQuery(name)}'`);
311
- return files[0];
312
- }
313
- async listChildren(parentId, extraQuery) {
314
- const query = [`'${this.escapeQuery(parentId)}' in parents`, "trashed = false", extraQuery].filter(Boolean).join(" and ");
315
- const files = [];
316
- let pageToken;
317
- do {
318
- const result = await this.request(`${DRIVE_API}/files`, {
319
- searchParams: {
320
- q: query,
321
- fields: "nextPageToken,files(id,name,mimeType,size,createdTime,modifiedTime,parents)",
322
- pageSize: "1000",
323
- supportsAllDrives: "true",
324
- includeItemsFromAllDrives: "true",
325
- ...pageToken ? { pageToken } : {}
326
- }
327
- });
328
- files.push(...result.files ?? []);
329
- pageToken = result.nextPageToken;
330
- } while (pageToken);
331
- return files;
332
- }
333
- async readdirRecursive(parentId, options, depth) {
334
- const children = await this.listChildren(parentId);
335
- const entries = [];
336
- for (const child of children) {
337
- const isDirectory = child.mimeType === FOLDER_MIME_TYPE;
338
- entries.push({ name: child.name, type: isDirectory ? "directory" : "file", size: Number(child.size ?? 0) });
339
- const shouldDescend = isDirectory && options?.recursive && (options.maxDepth === void 0 || depth < options.maxDepth);
340
- if (shouldDescend) {
341
- const nested = await this.readdirRecursive(child.id, options, depth + 1);
342
- entries.push(...nested.map((entry) => ({ ...entry, name: `${child.name}/${entry.name}` })));
343
- }
344
- }
345
- return entries;
346
- }
347
- async deleteAny(file, path, recursive) {
348
- if (file.mimeType === FOLDER_MIME_TYPE) await this.rmdir(path, { recursive, force: true });
349
- else await this.deleteFile(path, { force: true });
350
- }
351
- async upload(fileId, content, mimeType = "application/octet-stream", method, metadata) {
352
- const boundary = `mastra-${Date.now()}`;
353
- const body = Buffer.concat([
354
- Buffer.from(
355
- `--${boundary}\r
356
- Content-Type: application/json; charset=UTF-8\r
357
- \r
358
- ${JSON.stringify(metadata ?? {})}\r
359
- `
360
- ),
361
- Buffer.from(`--${boundary}\r
362
- Content-Type: ${mimeType}\r
363
- \r
364
- `),
365
- this.toBuffer(content),
366
- Buffer.from(`\r
367
- --${boundary}--`)
368
- ]);
369
- const url = fileId ? `${DRIVE_UPLOAD_API}/files/${encodeURIComponent(fileId)}` : `${DRIVE_UPLOAD_API}/files`;
370
- await this.request(url, {
371
- method,
372
- searchParams: { uploadType: "multipart", fields: "id", supportsAllDrives: "true" },
373
- headers: { "Content-Type": `multipart/related; boundary=${boundary}` },
374
- body
375
- });
376
- }
377
- escapeQuery(value) {
378
- return value.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
379
- }
380
- async request(url, init = {}) {
381
- const response = await this.fetch(url, init);
382
- if (response.status === 204) return void 0;
383
- return await response.json();
384
- }
385
- async fetch(url, init = {}) {
386
- const token = await this.getToken();
387
- const target = new URL(url);
388
- for (const [key, value] of Object.entries(init.searchParams ?? {})) target.searchParams.set(key, value);
389
- const headers = { Authorization: `Bearer ${token}` };
390
- if (init.body && typeof init.body === "string" && !init.headers) {
391
- headers["Content-Type"] = "application/json";
392
- }
393
- const response = await globalThis.fetch(target, {
394
- ...init,
395
- headers: { ...headers, ...init.headers }
396
- });
397
- if (!response.ok) {
398
- const message = await response.text().catch(() => response.statusText);
399
- throw new Error(`Google Drive API request failed (${response.status}): ${message}`);
400
- }
401
- return response;
402
- }
403
- async getToken() {
404
- if (this.accessToken && Date.now() < this.tokenExpiresAt - 6e4) return this.accessToken;
405
- if (this.getAccessToken) return this.getAccessToken();
406
- if (this.serviceAccount) {
407
- if (!this.tokenRefreshPromise) {
408
- this.tokenRefreshPromise = this.getServiceAccountToken().finally(() => {
409
- this.tokenRefreshPromise = void 0;
410
- });
411
- }
412
- return this.tokenRefreshPromise;
413
- }
414
- if (this.accessToken) return this.accessToken;
415
- throw new Error("GoogleDriveFilesystem requires accessToken, getAccessToken, or serviceAccount authentication.");
416
- }
417
- async getServiceAccountToken() {
418
- const account = this.serviceAccount;
419
- const now = Math.floor(Date.now() / 1e3);
420
- const header = { alg: "RS256", typ: "JWT", ...account.privateKeyId ? { kid: account.privateKeyId } : {} };
421
- const claim = {
422
- iss: account.clientEmail,
423
- scope: (account.scopes ?? DEFAULT_SCOPES).join(" "),
424
- aud: OAUTH_TOKEN_URL,
425
- exp: now + 3600,
426
- iat: now,
427
- ...account.subject ? { sub: account.subject } : {}
428
- };
429
- const unsigned = `${this.base64Url(JSON.stringify(header))}.${this.base64Url(JSON.stringify(claim))}`;
430
- const privateKey = this.normalizePrivateKey(account.privateKey);
431
- let signature;
432
- try {
433
- signature = crypto.createSign("RSA-SHA256").update(unsigned).sign(privateKey, "base64url");
434
- } catch (err) {
435
- const hasBegin = privateKey.includes("-----BEGIN");
436
- const hasEnd = privateKey.includes("-----END");
437
- throw new Error(
438
- `Google service account private key signing failed (${err.message}). Key has BEGIN marker: ${hasBegin}, END marker: ${hasEnd}. Ensure your .env value contains the raw PEM with \\n for newlines, without extra surrounding quotes or commas.`
439
- );
440
- }
441
- const response = await globalThis.fetch(OAUTH_TOKEN_URL, {
442
- method: "POST",
443
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
444
- body: new URLSearchParams({
445
- grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
446
- assertion: `${unsigned}.${signature}`
447
- })
448
- });
449
- if (!response.ok)
450
- throw new Error(`Google service account token request failed (${response.status}): ${await response.text()}`);
451
- const json = await response.json();
452
- this.accessToken = json.access_token;
453
- this.tokenExpiresAt = Date.now() + json.expires_in * 1e3;
454
- return json.access_token;
455
- }
456
- base64Url(value) {
457
- return Buffer.from(value).toString("base64url");
458
- }
459
- normalizePrivateKey(key) {
460
- let out = key.trim();
461
- for (let i = 0; i < 5; i++) {
462
- const before = out;
463
- if (out.endsWith(",")) out = out.slice(0, -1).trim();
464
- if (out.startsWith('"') && out.endsWith('"') || out.startsWith("'") && out.endsWith("'")) {
465
- out = out.slice(1, -1);
466
- }
467
- if (out.startsWith('\\"') && out.endsWith('\\"') || out.startsWith("\\'") && out.endsWith("\\'")) {
468
- out = out.slice(2, -2);
469
- }
470
- if (out === before) break;
471
- }
472
- out = out.replace(/\\n/g, "\n");
473
- out = out.replace(/\\"/g, '"').replace(/\\'/g, "'");
474
- out = out.replace(/\r\n?/g, "\n");
475
- if (!out.endsWith("\n")) out += "\n";
476
- return out;
477
- }
26
+ var GoogleDriveFilesystem = class extends _mastra_core_workspace.MastraFilesystem {
27
+ id;
28
+ name = "GoogleDriveFilesystem";
29
+ provider = "google-drive";
30
+ readOnly;
31
+ icon = "drive";
32
+ displayName = "Google Drive";
33
+ status = "pending";
34
+ accessToken;
35
+ tokenExpiresAt = 0;
36
+ tokenRefreshPromise;
37
+ folderId;
38
+ getAccessToken;
39
+ serviceAccount;
40
+ instructionsOverride;
41
+ constructor(options) {
42
+ super({
43
+ name: "GoogleDriveFilesystem",
44
+ ...options
45
+ });
46
+ this.id = options.id ?? `google-drive:${options.folderId}`;
47
+ this.folderId = options.folderId;
48
+ this.accessToken = options.accessToken;
49
+ this.getAccessToken = options.getAccessToken;
50
+ this.serviceAccount = options.serviceAccount;
51
+ this.readOnly = options.readOnly;
52
+ this.instructionsOverride = options.instructions;
53
+ }
54
+ async init() {
55
+ const driveFile = await this.request(`${DRIVE_API}/files/${encodeURIComponent(this.folderId)}`, { searchParams: {
56
+ fields: "id,name,mimeType,trashed",
57
+ supportsAllDrives: "true"
58
+ } });
59
+ if (driveFile.trashed) throw new Error(`Google Drive folder ${this.folderId} is trashed and cannot be used as a filesystem root.`);
60
+ if (driveFile.mimeType !== FOLDER_MIME_TYPE) throw new Error(`Google Drive root ${this.folderId} must be a folder, but received mimeType ${driveFile.mimeType ?? "unknown"}.`);
61
+ }
62
+ async destroy() {}
63
+ async isReady() {
64
+ return this.status === "ready";
65
+ }
66
+ getInfo() {
67
+ return {
68
+ id: this.id,
69
+ name: this.name,
70
+ provider: this.provider,
71
+ status: this.status,
72
+ error: this.error,
73
+ readOnly: this.readOnly,
74
+ icon: this.icon,
75
+ metadata: { folderId: this.folderId }
76
+ };
77
+ }
78
+ getInstructions(opts) {
79
+ const defaultInstructions = [
80
+ "Google Drive filesystem mounted to a single folder.",
81
+ "Use POSIX-style paths relative to that folder, for example /notes/todo.txt.",
82
+ "Directories are Google Drive folders. File names must be unique within each folder for path-based operations.",
83
+ this.readOnly ? "This Google Drive filesystem is read-only." : "You can read, create, update, move, copy, and delete files in this folder."
84
+ ].join("\n");
85
+ return resolveInstructions(this.instructionsOverride, () => defaultInstructions, opts?.requestContext);
86
+ }
87
+ async readFile(path, options) {
88
+ await this.ensureReady();
89
+ const file = await this.getFile(path);
90
+ if (file.mimeType === FOLDER_MIME_TYPE) throw new _mastra_core_workspace.IsDirectoryError(path);
91
+ const response = await this.fetch(`${DRIVE_API}/files/${encodeURIComponent(file.id)}`, {
92
+ method: "GET",
93
+ searchParams: { alt: "media" }
94
+ });
95
+ const buffer = Buffer.from(await response.arrayBuffer());
96
+ return options?.encoding ? buffer.toString(options.encoding) : buffer;
97
+ }
98
+ async writeFile(path, content, options) {
99
+ await this.ensureReady();
100
+ this.assertWritable("writeFile");
101
+ const existing = await this.findFile(path);
102
+ if (existing) {
103
+ if (existing.mimeType === FOLDER_MIME_TYPE) throw new _mastra_core_workspace.IsDirectoryError(path);
104
+ if (options?.overwrite === false) throw new _mastra_core_workspace.FileExistsError(path);
105
+ if (options?.expectedMtime) {
106
+ if (!existing.modifiedTime) throw new _mastra_core_workspace.StaleFileError(path, options.expectedMtime, /* @__PURE__ */ new Date(0));
107
+ const actual = new Date(existing.modifiedTime);
108
+ if (actual.getTime() !== options.expectedMtime.getTime()) throw new _mastra_core_workspace.StaleFileError(path, options.expectedMtime, actual);
109
+ }
110
+ await this.upload(existing.id, content, options?.mimeType, "PATCH");
111
+ return;
112
+ }
113
+ const { parentId, name } = await this.resolveParent(path, options?.recursive ?? true);
114
+ await this.upload(void 0, content, options?.mimeType, "POST", {
115
+ name,
116
+ parents: [parentId]
117
+ });
118
+ }
119
+ async appendFile(path, content) {
120
+ await this.ensureReady();
121
+ this.assertWritable("appendFile");
122
+ const existing = await this.findFile(path);
123
+ if (existing) {
124
+ if (existing.mimeType === FOLDER_MIME_TYPE) throw new _mastra_core_workspace.IsDirectoryError(path);
125
+ const current = await this.readFile(path);
126
+ const expectedMtime = existing.modifiedTime ? new Date(existing.modifiedTime) : void 0;
127
+ await this.writeFile(path, Buffer.concat([this.toBuffer(current), this.toBuffer(content)]), { expectedMtime });
128
+ } else await this.writeFile(path, content, { recursive: true });
129
+ }
130
+ async deleteFile(path, options) {
131
+ await this.ensureReady();
132
+ this.assertWritable("deleteFile");
133
+ const file = await this.findFile(path);
134
+ if (!file) {
135
+ if (options?.force) return;
136
+ throw new _mastra_core_workspace.FileNotFoundError(path);
137
+ }
138
+ if (file.mimeType === FOLDER_MIME_TYPE) throw new _mastra_core_workspace.IsDirectoryError(path);
139
+ await this.request(`${DRIVE_API}/files/${encodeURIComponent(file.id)}`, { method: "DELETE" });
140
+ }
141
+ async copyFile(src, dest, options) {
142
+ await this.ensureReady();
143
+ this.assertWritable("copyFile");
144
+ const source = await this.getFile(src);
145
+ if (source.mimeType === FOLDER_MIME_TYPE) throw new _mastra_core_workspace.IsDirectoryError(src);
146
+ const existing = await this.findFile(dest);
147
+ if (existing) {
148
+ if (existing.id === source.id) throw new _mastra_core_workspace.FileExistsError(dest);
149
+ if (existing.mimeType === FOLDER_MIME_TYPE || options?.overwrite === false) throw new _mastra_core_workspace.FileExistsError(dest);
150
+ await this.deleteAny(existing, dest, true);
151
+ }
152
+ const { parentId, name } = await this.resolveParent(dest, options?.recursive ?? true);
153
+ await this.request(`${DRIVE_API}/files/${encodeURIComponent(source.id)}/copy`, {
154
+ method: "POST",
155
+ body: JSON.stringify({
156
+ name,
157
+ parents: [parentId]
158
+ })
159
+ });
160
+ }
161
+ async moveFile(src, dest, options) {
162
+ await this.ensureReady();
163
+ this.assertWritable("moveFile");
164
+ const source = await this.getFile(src);
165
+ if (options?.overwrite === false && await this.exists(dest)) throw new _mastra_core_workspace.FileExistsError(dest);
166
+ const existing = await this.findFile(dest);
167
+ if (existing && existing.id !== source.id) {
168
+ if (existing.mimeType === FOLDER_MIME_TYPE || options?.overwrite === false) throw new _mastra_core_workspace.FileExistsError(dest);
169
+ await this.deleteAny(existing, dest, true);
170
+ }
171
+ const { parentId, name } = await this.resolveParent(dest, options?.recursive ?? true);
172
+ const searchParams = {
173
+ addParents: parentId,
174
+ fields: "id",
175
+ supportsAllDrives: "true"
176
+ };
177
+ const oldParents = source.parents?.join(",");
178
+ if (oldParents) searchParams.removeParents = oldParents;
179
+ await this.request(`${DRIVE_API}/files/${encodeURIComponent(source.id)}`, {
180
+ method: "PATCH",
181
+ searchParams,
182
+ body: JSON.stringify({ name })
183
+ });
184
+ }
185
+ async mkdir(path, options) {
186
+ await this.ensureReady();
187
+ this.assertWritable("mkdir");
188
+ if (this.normalize(path) === "/") return;
189
+ const existing = await this.findFile(path);
190
+ if (existing) {
191
+ if (existing.mimeType !== FOLDER_MIME_TYPE) throw new _mastra_core_workspace.FileExistsError(path);
192
+ return;
193
+ }
194
+ const { parentId, name } = await this.resolveParent(path, options?.recursive ?? true);
195
+ await this.createFolder(parentId, name);
196
+ }
197
+ async rmdir(path, options) {
198
+ await this.ensureReady();
199
+ this.assertWritable("rmdir");
200
+ const dir = await this.findFile(path);
201
+ if (!dir) {
202
+ if (options?.force) return;
203
+ throw new _mastra_core_workspace.DirectoryNotFoundError(path);
204
+ }
205
+ if (dir.mimeType !== FOLDER_MIME_TYPE) throw new _mastra_core_workspace.NotDirectoryError(path);
206
+ if (!options?.recursive) {
207
+ if ((await this.listChildren(dir.id)).length) throw new _mastra_core_workspace.DirectoryNotEmptyError(path);
208
+ }
209
+ await this.request(`${DRIVE_API}/files/${encodeURIComponent(dir.id)}`, { method: "DELETE" });
210
+ }
211
+ async readdir(path, options) {
212
+ await this.ensureReady();
213
+ const dir = await this.getFile(path);
214
+ if (dir.mimeType !== FOLDER_MIME_TYPE) throw new _mastra_core_workspace.NotDirectoryError(path);
215
+ const entries = await this.readdirRecursive(dir.id, options, 0);
216
+ const extensions = Array.isArray(options?.extension) ? options.extension : options?.extension ? [options.extension] : void 0;
217
+ return extensions ? entries.filter((entry) => entry.type === "directory" || extensions.some((ext) => entry.name.endsWith(ext))) : entries;
218
+ }
219
+ async exists(path) {
220
+ await this.ensureReady();
221
+ return Boolean(await this.findFile(path));
222
+ }
223
+ async stat(path) {
224
+ await this.ensureReady();
225
+ const file = await this.getFile(path);
226
+ const isDirectory = file.mimeType === FOLDER_MIME_TYPE;
227
+ return {
228
+ name: file.name,
229
+ path: this.normalize(path),
230
+ type: isDirectory ? "directory" : "file",
231
+ size: Number(file.size ?? 0),
232
+ createdAt: file.createdTime ? new Date(file.createdTime) : /* @__PURE__ */ new Date(0),
233
+ modifiedAt: file.modifiedTime ? new Date(file.modifiedTime) : /* @__PURE__ */ new Date(0),
234
+ mimeType: isDirectory ? void 0 : file.mimeType
235
+ };
236
+ }
237
+ async realpath(path) {
238
+ return this.normalize(path);
239
+ }
240
+ assertWritable(operation) {
241
+ if (this.readOnly) throw new _mastra_core_workspace.WorkspaceReadOnlyError(operation);
242
+ }
243
+ toBuffer(content) {
244
+ if (Buffer.isBuffer(content)) return content;
245
+ if (content instanceof Uint8Array) return Buffer.from(content);
246
+ return Buffer.from(content, "utf-8");
247
+ }
248
+ normalize(path) {
249
+ const parts = path.split("/").filter(Boolean);
250
+ const stack = [];
251
+ for (const part of parts) {
252
+ if (part === ".") continue;
253
+ if (part === "..") stack.pop();
254
+ else stack.push(part);
255
+ }
256
+ return `/${stack.join("/")}`;
257
+ }
258
+ async getFile(path) {
259
+ const file = await this.findFile(path);
260
+ if (!file) throw new _mastra_core_workspace.FileNotFoundError(path);
261
+ return file;
262
+ }
263
+ async findFile(path) {
264
+ const normalized = this.normalize(path);
265
+ if (normalized === "/") return this.rootFile();
266
+ const names = normalized.split("/").filter(Boolean);
267
+ let parentId = this.folderId;
268
+ let file;
269
+ for (const name of names) {
270
+ file = await this.findChild(parentId, name);
271
+ if (!file) return void 0;
272
+ parentId = file.id;
273
+ }
274
+ return file;
275
+ }
276
+ async rootFile() {
277
+ return this.request(`${DRIVE_API}/files/${encodeURIComponent(this.folderId)}`, { searchParams: {
278
+ fields: "id,name,mimeType,size,createdTime,modifiedTime,parents",
279
+ supportsAllDrives: "true"
280
+ } });
281
+ }
282
+ async resolveParent(path, recursive) {
283
+ const parts = this.normalize(path).split("/").filter(Boolean);
284
+ const name = parts.pop();
285
+ if (!name) throw new _mastra_core_workspace.IsDirectoryError(path);
286
+ const parentPath = `/${parts.join("/")}`;
287
+ const parent = recursive ? await this.resolveDir(parentPath, true) : await this.findFile(parentPath);
288
+ if (!parent) throw new _mastra_core_workspace.DirectoryNotFoundError(parentPath);
289
+ if (parent.mimeType !== FOLDER_MIME_TYPE) throw new _mastra_core_workspace.NotDirectoryError(parentPath);
290
+ return {
291
+ parentId: parent.id,
292
+ name
293
+ };
294
+ }
295
+ async resolveDir(path, recursive) {
296
+ const normalized = this.normalize(path);
297
+ if (normalized === "/") return this.rootFile();
298
+ const names = normalized.split("/").filter(Boolean);
299
+ let parentId = this.folderId;
300
+ let current;
301
+ for (const name of names) {
302
+ current = await this.findChild(parentId, name);
303
+ if (current) {
304
+ if (current.mimeType !== FOLDER_MIME_TYPE) throw new _mastra_core_workspace.NotDirectoryError(name);
305
+ parentId = current.id;
306
+ continue;
307
+ }
308
+ if (!recursive) throw new _mastra_core_workspace.DirectoryNotFoundError(normalized);
309
+ current = await this.createFolder(parentId, name);
310
+ parentId = current.id;
311
+ }
312
+ return current;
313
+ }
314
+ async createFolder(parentId, name) {
315
+ return this.request(`${DRIVE_API}/files`, {
316
+ method: "POST",
317
+ searchParams: {
318
+ fields: "id,name,mimeType,size,createdTime,modifiedTime,parents",
319
+ supportsAllDrives: "true"
320
+ },
321
+ body: JSON.stringify({
322
+ name,
323
+ mimeType: FOLDER_MIME_TYPE,
324
+ parents: [parentId]
325
+ })
326
+ });
327
+ }
328
+ async findChild(parentId, name) {
329
+ return (await this.listChildren(parentId, `name = '${this.escapeQuery(name)}'`))[0];
330
+ }
331
+ async listChildren(parentId, extraQuery) {
332
+ const query = [
333
+ `'${this.escapeQuery(parentId)}' in parents`,
334
+ "trashed = false",
335
+ extraQuery
336
+ ].filter(Boolean).join(" and ");
337
+ const files = [];
338
+ let pageToken;
339
+ do {
340
+ const result = await this.request(`${DRIVE_API}/files`, { searchParams: {
341
+ q: query,
342
+ fields: "nextPageToken,files(id,name,mimeType,size,createdTime,modifiedTime,parents)",
343
+ pageSize: "1000",
344
+ supportsAllDrives: "true",
345
+ includeItemsFromAllDrives: "true",
346
+ ...pageToken ? { pageToken } : {}
347
+ } });
348
+ files.push(...result.files ?? []);
349
+ pageToken = result.nextPageToken;
350
+ } while (pageToken);
351
+ return files;
352
+ }
353
+ async readdirRecursive(parentId, options, depth) {
354
+ const children = await this.listChildren(parentId);
355
+ const entries = [];
356
+ for (const child of children) {
357
+ const isDirectory = child.mimeType === FOLDER_MIME_TYPE;
358
+ entries.push({
359
+ name: child.name,
360
+ type: isDirectory ? "directory" : "file",
361
+ size: Number(child.size ?? 0)
362
+ });
363
+ if (isDirectory && options?.recursive && (options.maxDepth === void 0 || depth < options.maxDepth)) {
364
+ const nested = await this.readdirRecursive(child.id, options, depth + 1);
365
+ entries.push(...nested.map((entry) => ({
366
+ ...entry,
367
+ name: `${child.name}/${entry.name}`
368
+ })));
369
+ }
370
+ }
371
+ return entries;
372
+ }
373
+ async deleteAny(file, path, recursive) {
374
+ if (file.mimeType === FOLDER_MIME_TYPE) await this.rmdir(path, {
375
+ recursive,
376
+ force: true
377
+ });
378
+ else await this.deleteFile(path, { force: true });
379
+ }
380
+ async upload(fileId, content, mimeType = "application/octet-stream", method, metadata) {
381
+ const boundary = `mastra-${Date.now()}`;
382
+ const body = Buffer.concat([
383
+ Buffer.from(`--${boundary}\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n${JSON.stringify(metadata ?? {})}\r\n`),
384
+ Buffer.from(`--${boundary}\r\nContent-Type: ${mimeType}\r\n\r\n`),
385
+ this.toBuffer(content),
386
+ Buffer.from(`\r\n--${boundary}--`)
387
+ ]);
388
+ const url = fileId ? `${DRIVE_UPLOAD_API}/files/${encodeURIComponent(fileId)}` : `${DRIVE_UPLOAD_API}/files`;
389
+ await this.request(url, {
390
+ method,
391
+ searchParams: {
392
+ uploadType: "multipart",
393
+ fields: "id",
394
+ supportsAllDrives: "true"
395
+ },
396
+ headers: { "Content-Type": `multipart/related; boundary=${boundary}` },
397
+ body
398
+ });
399
+ }
400
+ escapeQuery(value) {
401
+ return value.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
402
+ }
403
+ async request(url, init = {}) {
404
+ const response = await this.fetch(url, init);
405
+ if (response.status === 204) return void 0;
406
+ return await response.json();
407
+ }
408
+ async fetch(url, init = {}) {
409
+ const token = await this.getToken();
410
+ const target = new URL(url);
411
+ for (const [key, value] of Object.entries(init.searchParams ?? {})) target.searchParams.set(key, value);
412
+ const headers = { Authorization: `Bearer ${token}` };
413
+ if (init.body && typeof init.body === "string" && !init.headers) headers["Content-Type"] = "application/json";
414
+ const response = await globalThis.fetch(target, {
415
+ ...init,
416
+ headers: {
417
+ ...headers,
418
+ ...init.headers
419
+ }
420
+ });
421
+ if (!response.ok) {
422
+ const message = await response.text().catch(() => response.statusText);
423
+ throw new Error(`Google Drive API request failed (${response.status}): ${message}`);
424
+ }
425
+ return response;
426
+ }
427
+ async getToken() {
428
+ if (this.accessToken && Date.now() < this.tokenExpiresAt - 6e4) return this.accessToken;
429
+ if (this.getAccessToken) return this.getAccessToken();
430
+ if (this.serviceAccount) {
431
+ if (!this.tokenRefreshPromise) this.tokenRefreshPromise = this.getServiceAccountToken().finally(() => {
432
+ this.tokenRefreshPromise = void 0;
433
+ });
434
+ return this.tokenRefreshPromise;
435
+ }
436
+ if (this.accessToken) return this.accessToken;
437
+ throw new Error("GoogleDriveFilesystem requires accessToken, getAccessToken, or serviceAccount authentication.");
438
+ }
439
+ async getServiceAccountToken() {
440
+ const account = this.serviceAccount;
441
+ const now = Math.floor(Date.now() / 1e3);
442
+ const header = {
443
+ alg: "RS256",
444
+ typ: "JWT",
445
+ ...account.privateKeyId ? { kid: account.privateKeyId } : {}
446
+ };
447
+ const claim = {
448
+ iss: account.clientEmail,
449
+ scope: (account.scopes ?? DEFAULT_SCOPES).join(" "),
450
+ aud: OAUTH_TOKEN_URL,
451
+ exp: now + 3600,
452
+ iat: now,
453
+ ...account.subject ? { sub: account.subject } : {}
454
+ };
455
+ const unsigned = `${this.base64Url(JSON.stringify(header))}.${this.base64Url(JSON.stringify(claim))}`;
456
+ const privateKey = this.normalizePrivateKey(account.privateKey);
457
+ let signature;
458
+ try {
459
+ signature = (0, crypto.createSign)("RSA-SHA256").update(unsigned).sign(privateKey, "base64url");
460
+ } catch (err) {
461
+ const hasBegin = privateKey.includes("-----BEGIN");
462
+ const hasEnd = privateKey.includes("-----END");
463
+ throw new Error(`Google service account private key signing failed (${err.message}). Key has BEGIN marker: ${hasBegin}, END marker: ${hasEnd}. Ensure your .env value contains the raw PEM with \\n for newlines, without extra surrounding quotes or commas.`);
464
+ }
465
+ const response = await globalThis.fetch(OAUTH_TOKEN_URL, {
466
+ method: "POST",
467
+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
468
+ body: new URLSearchParams({
469
+ grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
470
+ assertion: `${unsigned}.${signature}`
471
+ })
472
+ });
473
+ if (!response.ok) throw new Error(`Google service account token request failed (${response.status}): ${await response.text()}`);
474
+ const json = await response.json();
475
+ this.accessToken = json.access_token;
476
+ this.tokenExpiresAt = Date.now() + json.expires_in * 1e3;
477
+ return json.access_token;
478
+ }
479
+ base64Url(value) {
480
+ return Buffer.from(value).toString("base64url");
481
+ }
482
+ normalizePrivateKey(key) {
483
+ let out = key.trim();
484
+ for (let i = 0; i < 5; i++) {
485
+ const before = out;
486
+ if (out.endsWith(",")) out = out.slice(0, -1).trim();
487
+ if (out.startsWith("\"") && out.endsWith("\"") || out.startsWith("'") && out.endsWith("'")) out = out.slice(1, -1);
488
+ if (out.startsWith("\\\"") && out.endsWith("\\\"") || out.startsWith("\\'") && out.endsWith("\\'")) out = out.slice(2, -2);
489
+ if (out === before) break;
490
+ }
491
+ out = out.replace(/\\n/g, "\n");
492
+ out = out.replace(/\\"/g, "\"").replace(/\\'/g, "'");
493
+ out = out.replace(/\r\n?/g, "\n");
494
+ if (!out.endsWith("\n")) out += "\n";
495
+ return out;
496
+ }
478
497
  };
479
-
480
- // src/provider.ts
481
- var googleDriveFilesystemProvider = {
482
- id: "google-drive",
483
- name: "Google Drive",
484
- description: "Google Drive folder mounted as a filesystem",
485
- configSchema: {
486
- type: "object",
487
- required: ["folderId"],
488
- properties: {
489
- folderId: { type: "string", description: "Google Drive folder ID to mount as the workspace root" },
490
- accessToken: {
491
- type: "string",
492
- description: "OAuth access token with the https://www.googleapis.com/auth/drive scope"
493
- },
494
- serviceAccount: {
495
- type: "object",
496
- required: ["clientEmail", "privateKey"],
497
- properties: {
498
- clientEmail: { type: "string", description: "Google service account email" },
499
- privateKey: { type: "string", description: "PEM-encoded private key" },
500
- privateKeyId: { type: "string", description: "Optional private key ID" },
501
- scopes: {
502
- type: "array",
503
- items: { type: "string" },
504
- description: "Optional OAuth scopes override"
505
- },
506
- subject: { type: "string", description: "Optional delegated user email" }
507
- },
508
- description: "Service account credentials for server-to-server auth"
509
- },
510
- readOnly: { type: "boolean", description: "Mount as read-only", default: false }
511
- }
512
- },
513
- createFilesystem: (config) => new GoogleDriveFilesystem(config)
498
+ //#endregion
499
+ //#region src/provider.ts
500
+ const googleDriveFilesystemProvider = {
501
+ id: "google-drive",
502
+ name: "Google Drive",
503
+ description: "Google Drive folder mounted as a filesystem",
504
+ configSchema: {
505
+ type: "object",
506
+ required: ["folderId"],
507
+ properties: {
508
+ folderId: {
509
+ type: "string",
510
+ description: "Google Drive folder ID to mount as the workspace root"
511
+ },
512
+ accessToken: {
513
+ type: "string",
514
+ description: "OAuth access token with the https://www.googleapis.com/auth/drive scope"
515
+ },
516
+ serviceAccount: {
517
+ type: "object",
518
+ required: ["clientEmail", "privateKey"],
519
+ properties: {
520
+ clientEmail: {
521
+ type: "string",
522
+ description: "Google service account email"
523
+ },
524
+ privateKey: {
525
+ type: "string",
526
+ description: "PEM-encoded private key"
527
+ },
528
+ privateKeyId: {
529
+ type: "string",
530
+ description: "Optional private key ID"
531
+ },
532
+ scopes: {
533
+ type: "array",
534
+ items: { type: "string" },
535
+ description: "Optional OAuth scopes override"
536
+ },
537
+ subject: {
538
+ type: "string",
539
+ description: "Optional delegated user email"
540
+ }
541
+ },
542
+ description: "Service account credentials for server-to-server auth"
543
+ },
544
+ readOnly: {
545
+ type: "boolean",
546
+ description: "Mount as read-only",
547
+ default: false
548
+ }
549
+ }
550
+ },
551
+ createFilesystem: (config) => new GoogleDriveFilesystem(config)
514
552
  };
515
-
553
+ //#endregion
516
554
  exports.GoogleDriveFilesystem = GoogleDriveFilesystem;
517
555
  exports.googleDriveFilesystemProvider = googleDriveFilesystemProvider;
518
- //# sourceMappingURL=index.cjs.map
556
+
519
557
  //# sourceMappingURL=index.cjs.map