@mastra/google-drive 0.0.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/CHANGELOG.md +1 -0
- package/README.md +52 -0
- package/dist/filesystem/index.d.ts +78 -0
- package/dist/filesystem/index.d.ts.map +1 -0
- package/dist/index.cjs +474 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +471 -0
- package/dist/index.js.map +1 -0
- package/dist/provider.d.ts +16 -0
- package/dist/provider.d.ts.map +1 -0
- package/package.json +63 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @mastra/google-drive
|
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# @mastra/google-drive
|
|
2
|
+
|
|
3
|
+
Google Drive filesystem provider for Mastra workspaces. Mounts a Google Drive folder as an agent workspace, exposing it through the standard `WorkspaceFilesystem` interface so agents can read, write, list, copy, move, and delete files in Drive.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @mastra/google-drive
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Agent } from '@mastra/core/agent';
|
|
15
|
+
import { Workspace } from '@mastra/core/workspace';
|
|
16
|
+
import { GoogleDriveFilesystem } from '@mastra/google-drive';
|
|
17
|
+
|
|
18
|
+
const workspace = new Workspace({
|
|
19
|
+
filesystem: new GoogleDriveFilesystem({
|
|
20
|
+
folderId: process.env.GOOGLE_DRIVE_FOLDER_ID!,
|
|
21
|
+
accessToken: process.env.GOOGLE_DRIVE_ACCESS_TOKEN!,
|
|
22
|
+
}),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const agent = new Agent({
|
|
26
|
+
name: 'my-agent',
|
|
27
|
+
model: 'anthropic/claude-opus-4-5',
|
|
28
|
+
workspace,
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Authentication
|
|
33
|
+
|
|
34
|
+
Supply one of:
|
|
35
|
+
|
|
36
|
+
- **`accessToken`** — A pre-obtained OAuth access token (use the `https://www.googleapis.com/auth/drive` scope).
|
|
37
|
+
- **`getAccessToken`** — A callback that returns a token; useful when tokens are refreshed externally.
|
|
38
|
+
- **`serviceAccount`** — A Google service account. Share the target folder with the service account email.
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
new GoogleDriveFilesystem({
|
|
42
|
+
folderId: process.env.GOOGLE_DRIVE_FOLDER_ID!,
|
|
43
|
+
serviceAccount: {
|
|
44
|
+
clientEmail: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL!,
|
|
45
|
+
privateKey: process.env.GOOGLE_SERVICE_ACCOUNT_KEY!,
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Documentation
|
|
51
|
+
|
|
52
|
+
For more information, see the [Mastra Workspaces documentation](https://mastra.ai/docs/workspace/overview) and the [GoogleDriveFilesystem reference](https://mastra.ai/reference/workspace/google-drive-filesystem).
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { MastraFilesystem } from '@mastra/core/workspace';
|
|
2
|
+
import type { CopyOptions, FileContent, FileEntry, FileStat, FilesystemInfo, InstructionsOption, ListOptions, MastraFilesystemOptions, ProviderStatus, ReadOptions, RemoveOptions, WriteOptions } from '@mastra/core/workspace';
|
|
3
|
+
import type { RequestContext } from '@mastra/core/request-context';
|
|
4
|
+
export interface GoogleDriveServiceAccount {
|
|
5
|
+
clientEmail: string;
|
|
6
|
+
privateKey: string;
|
|
7
|
+
privateKeyId?: string;
|
|
8
|
+
scopes?: string[];
|
|
9
|
+
subject?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface GoogleDriveFilesystemOptions extends MastraFilesystemOptions {
|
|
12
|
+
id?: string;
|
|
13
|
+
folderId: string;
|
|
14
|
+
accessToken?: string;
|
|
15
|
+
getAccessToken?: () => string | Promise<string>;
|
|
16
|
+
serviceAccount?: GoogleDriveServiceAccount;
|
|
17
|
+
readOnly?: boolean;
|
|
18
|
+
instructions?: InstructionsOption;
|
|
19
|
+
}
|
|
20
|
+
export declare class GoogleDriveFilesystem extends MastraFilesystem {
|
|
21
|
+
readonly id: string;
|
|
22
|
+
readonly name = "GoogleDriveFilesystem";
|
|
23
|
+
readonly provider = "google-drive";
|
|
24
|
+
readonly readOnly?: boolean;
|
|
25
|
+
readonly icon = "drive";
|
|
26
|
+
readonly displayName = "Google Drive";
|
|
27
|
+
status: ProviderStatus;
|
|
28
|
+
private accessToken?;
|
|
29
|
+
private tokenExpiresAt;
|
|
30
|
+
private readonly folderId;
|
|
31
|
+
private readonly getAccessToken?;
|
|
32
|
+
private readonly serviceAccount?;
|
|
33
|
+
private readonly instructionsOverride?;
|
|
34
|
+
constructor(options: GoogleDriveFilesystemOptions);
|
|
35
|
+
init(): Promise<void>;
|
|
36
|
+
destroy(): Promise<void>;
|
|
37
|
+
isReady(): Promise<boolean>;
|
|
38
|
+
getInfo(): FilesystemInfo;
|
|
39
|
+
getInstructions(opts?: {
|
|
40
|
+
requestContext?: RequestContext;
|
|
41
|
+
}): string;
|
|
42
|
+
readFile(path: string, options?: ReadOptions): Promise<string | Buffer>;
|
|
43
|
+
writeFile(path: string, content: FileContent, options?: WriteOptions): Promise<void>;
|
|
44
|
+
appendFile(path: string, content: FileContent): Promise<void>;
|
|
45
|
+
deleteFile(path: string, options?: RemoveOptions): Promise<void>;
|
|
46
|
+
copyFile(src: string, dest: string, options?: CopyOptions): Promise<void>;
|
|
47
|
+
moveFile(src: string, dest: string, options?: CopyOptions): Promise<void>;
|
|
48
|
+
mkdir(path: string, options?: {
|
|
49
|
+
recursive?: boolean;
|
|
50
|
+
}): Promise<void>;
|
|
51
|
+
rmdir(path: string, options?: RemoveOptions): Promise<void>;
|
|
52
|
+
readdir(path: string, options?: ListOptions): Promise<FileEntry[]>;
|
|
53
|
+
exists(path: string): Promise<boolean>;
|
|
54
|
+
stat(path: string): Promise<FileStat>;
|
|
55
|
+
realpath(path: string): Promise<string>;
|
|
56
|
+
private assertWritable;
|
|
57
|
+
private toBuffer;
|
|
58
|
+
private normalize;
|
|
59
|
+
private getFile;
|
|
60
|
+
private findFile;
|
|
61
|
+
private rootFile;
|
|
62
|
+
private resolveParent;
|
|
63
|
+
private resolveDir;
|
|
64
|
+
private createFolder;
|
|
65
|
+
private findChild;
|
|
66
|
+
private listChildren;
|
|
67
|
+
private readdirRecursive;
|
|
68
|
+
private deleteAny;
|
|
69
|
+
private upload;
|
|
70
|
+
private escapeQuery;
|
|
71
|
+
private request;
|
|
72
|
+
private fetch;
|
|
73
|
+
private getToken;
|
|
74
|
+
private getServiceAccountToken;
|
|
75
|
+
private base64Url;
|
|
76
|
+
private normalizePrivateKey;
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/filesystem/index.ts"],"names":[],"mappings":"AACA,OAAO,EAML,gBAAgB,EAIjB,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EACV,WAAW,EACX,WAAW,EACX,SAAS,EACT,QAAQ,EACR,cAAc,EACd,kBAAkB,EAClB,WAAW,EACX,uBAAuB,EACvB,cAAc,EACd,WAAW,EACX,aAAa,EACb,YAAY,EACb,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AA6BnE,MAAM,WAAW,yBAAyB;IACxC,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,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AO;YAMP,QAAQ;YAcR,QAAQ;YAMR,aAAa;YAYb,UAAU;YAoBV,YAAY;YAQZ,SAAS;YAKT,YAAY;YAyBZ,gBAAgB;YAoBhB,SAAS;YAKT,MAAM;IAyBpB,OAAO,CAAC,WAAW;YAIL,OAAO;YASP,KAAK;YAkBL,QAAQ;YAQR,sBAAsB;IA0CpC,OAAO,CAAC,SAAS;IAIjB,OAAO,CAAC,mBAAmB;CA2B5B"}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,474 @@
|
|
|
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"];
|
|
12
|
+
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 });
|
|
17
|
+
}
|
|
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
|
+
folderId;
|
|
29
|
+
getAccessToken;
|
|
30
|
+
serviceAccount;
|
|
31
|
+
instructionsOverride;
|
|
32
|
+
constructor(options) {
|
|
33
|
+
super({ name: "GoogleDriveFilesystem", ...options });
|
|
34
|
+
this.id = options.id ?? `google-drive:${options.folderId}`;
|
|
35
|
+
this.folderId = options.folderId;
|
|
36
|
+
this.accessToken = options.accessToken;
|
|
37
|
+
this.getAccessToken = options.getAccessToken;
|
|
38
|
+
this.serviceAccount = options.serviceAccount;
|
|
39
|
+
this.readOnly = options.readOnly;
|
|
40
|
+
this.instructionsOverride = options.instructions;
|
|
41
|
+
}
|
|
42
|
+
async init() {
|
|
43
|
+
const driveFile = await this.request(`${DRIVE_API}/files/${encodeURIComponent(this.folderId)}`, {
|
|
44
|
+
searchParams: { fields: "id,name,mimeType,trashed", supportsAllDrives: "true" }
|
|
45
|
+
});
|
|
46
|
+
if (driveFile.trashed) {
|
|
47
|
+
throw new Error(`Google Drive folder ${this.folderId} is trashed and cannot be used as a filesystem root.`);
|
|
48
|
+
}
|
|
49
|
+
if (driveFile.mimeType !== FOLDER_MIME_TYPE) {
|
|
50
|
+
throw new Error(
|
|
51
|
+
`Google Drive root ${this.folderId} must be a folder, but received mimeType ${driveFile.mimeType ?? "unknown"}.`
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async destroy() {
|
|
56
|
+
}
|
|
57
|
+
async isReady() {
|
|
58
|
+
return this.status === "ready";
|
|
59
|
+
}
|
|
60
|
+
getInfo() {
|
|
61
|
+
return {
|
|
62
|
+
id: this.id,
|
|
63
|
+
name: this.name,
|
|
64
|
+
provider: this.provider,
|
|
65
|
+
status: this.status,
|
|
66
|
+
error: this.error,
|
|
67
|
+
readOnly: this.readOnly,
|
|
68
|
+
icon: this.icon,
|
|
69
|
+
metadata: { folderId: this.folderId }
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
getInstructions(opts) {
|
|
73
|
+
const defaultInstructions = [
|
|
74
|
+
"Google Drive filesystem mounted to a single folder.",
|
|
75
|
+
"Use POSIX-style paths relative to that folder, for example /notes/todo.txt.",
|
|
76
|
+
"Directories are Google Drive folders. File names must be unique within each folder for path-based operations.",
|
|
77
|
+
this.readOnly ? "This Google Drive filesystem is read-only." : "You can read, create, update, move, copy, and delete files in this folder."
|
|
78
|
+
].join("\n");
|
|
79
|
+
return resolveInstructions(this.instructionsOverride, () => defaultInstructions, opts?.requestContext);
|
|
80
|
+
}
|
|
81
|
+
async readFile(path, options) {
|
|
82
|
+
await this.ensureReady();
|
|
83
|
+
const file = await this.getFile(path);
|
|
84
|
+
if (file.mimeType === FOLDER_MIME_TYPE) throw new workspace.IsDirectoryError(path);
|
|
85
|
+
const response = await this.fetch(`${DRIVE_API}/files/${encodeURIComponent(file.id)}?alt=media`, { method: "GET" });
|
|
86
|
+
const buffer = Buffer.from(await response.arrayBuffer());
|
|
87
|
+
return options?.encoding ? buffer.toString(options.encoding) : buffer;
|
|
88
|
+
}
|
|
89
|
+
async writeFile(path, content, options) {
|
|
90
|
+
await this.ensureReady();
|
|
91
|
+
this.assertWritable("writeFile");
|
|
92
|
+
const existing = await this.findFile(path);
|
|
93
|
+
if (existing) {
|
|
94
|
+
if (existing.mimeType === FOLDER_MIME_TYPE) throw new workspace.IsDirectoryError(path);
|
|
95
|
+
if (options?.overwrite === false) throw new workspace.FileExistsError(path);
|
|
96
|
+
if (options?.expectedMtime) {
|
|
97
|
+
if (!existing.modifiedTime) throw new workspace.StaleFileError(path, options.expectedMtime, /* @__PURE__ */ new Date(0));
|
|
98
|
+
const actual = new Date(existing.modifiedTime);
|
|
99
|
+
if (actual.getTime() !== options.expectedMtime.getTime())
|
|
100
|
+
throw new workspace.StaleFileError(path, options.expectedMtime, actual);
|
|
101
|
+
}
|
|
102
|
+
await this.upload(existing.id, content, options?.mimeType, "PATCH");
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const { parentId, name } = await this.resolveParent(path, options?.recursive ?? true);
|
|
106
|
+
await this.upload(void 0, content, options?.mimeType, "POST", { name, parents: [parentId] });
|
|
107
|
+
}
|
|
108
|
+
async appendFile(path, content) {
|
|
109
|
+
const current = await this.exists(path) ? await this.readFile(path) : Buffer.alloc(0);
|
|
110
|
+
await this.writeFile(path, Buffer.concat([this.toBuffer(current), this.toBuffer(content)]), { recursive: true });
|
|
111
|
+
}
|
|
112
|
+
async deleteFile(path, options) {
|
|
113
|
+
await this.ensureReady();
|
|
114
|
+
this.assertWritable("deleteFile");
|
|
115
|
+
const file = await this.findFile(path);
|
|
116
|
+
if (!file) {
|
|
117
|
+
if (options?.force) return;
|
|
118
|
+
throw new workspace.FileNotFoundError(path);
|
|
119
|
+
}
|
|
120
|
+
if (file.mimeType === FOLDER_MIME_TYPE) throw new workspace.IsDirectoryError(path);
|
|
121
|
+
await this.request(`${DRIVE_API}/files/${encodeURIComponent(file.id)}`, { method: "DELETE" });
|
|
122
|
+
}
|
|
123
|
+
async copyFile(src, dest, options) {
|
|
124
|
+
await this.ensureReady();
|
|
125
|
+
this.assertWritable("copyFile");
|
|
126
|
+
const source = await this.getFile(src);
|
|
127
|
+
if (source.mimeType === FOLDER_MIME_TYPE) throw new workspace.IsDirectoryError(src);
|
|
128
|
+
const existing = await this.findFile(dest);
|
|
129
|
+
if (existing) {
|
|
130
|
+
if (existing.mimeType === FOLDER_MIME_TYPE || options?.overwrite === false) throw new workspace.FileExistsError(dest);
|
|
131
|
+
await this.deleteAny(existing, dest, true);
|
|
132
|
+
}
|
|
133
|
+
const { parentId, name } = await this.resolveParent(dest, options?.recursive ?? true);
|
|
134
|
+
await this.request(`${DRIVE_API}/files/${encodeURIComponent(source.id)}/copy`, {
|
|
135
|
+
method: "POST",
|
|
136
|
+
body: JSON.stringify({ name, parents: [parentId] })
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
async moveFile(src, dest, options) {
|
|
140
|
+
await this.ensureReady();
|
|
141
|
+
this.assertWritable("moveFile");
|
|
142
|
+
const source = await this.getFile(src);
|
|
143
|
+
if (options?.overwrite === false && await this.exists(dest)) throw new workspace.FileExistsError(dest);
|
|
144
|
+
const existing = await this.findFile(dest);
|
|
145
|
+
if (existing && existing.id !== source.id) {
|
|
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
|
+
const searchParams = { addParents: parentId, fields: "id", supportsAllDrives: "true" };
|
|
151
|
+
const oldParents = source.parents?.join(",");
|
|
152
|
+
if (oldParents) searchParams.removeParents = oldParents;
|
|
153
|
+
await this.request(`${DRIVE_API}/files/${encodeURIComponent(source.id)}`, {
|
|
154
|
+
method: "PATCH",
|
|
155
|
+
searchParams,
|
|
156
|
+
body: JSON.stringify({ name })
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
async mkdir(path, options) {
|
|
160
|
+
await this.ensureReady();
|
|
161
|
+
this.assertWritable("mkdir");
|
|
162
|
+
if (this.normalize(path) === "/") return;
|
|
163
|
+
const existing = await this.findFile(path);
|
|
164
|
+
if (existing) {
|
|
165
|
+
if (existing.mimeType !== FOLDER_MIME_TYPE) throw new workspace.FileExistsError(path);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
const { parentId, name } = await this.resolveParent(path, options?.recursive ?? false);
|
|
169
|
+
await this.createFolder(parentId, name);
|
|
170
|
+
}
|
|
171
|
+
async rmdir(path, options) {
|
|
172
|
+
await this.ensureReady();
|
|
173
|
+
this.assertWritable("rmdir");
|
|
174
|
+
const dir = await this.findFile(path);
|
|
175
|
+
if (!dir) {
|
|
176
|
+
if (options?.force) return;
|
|
177
|
+
throw new workspace.DirectoryNotFoundError(path);
|
|
178
|
+
}
|
|
179
|
+
if (dir.mimeType !== FOLDER_MIME_TYPE) throw new workspace.NotDirectoryError(path);
|
|
180
|
+
const children = await this.listChildren(dir.id);
|
|
181
|
+
if (children.length && !options?.recursive) throw new workspace.DirectoryNotEmptyError(path);
|
|
182
|
+
await this.request(`${DRIVE_API}/files/${encodeURIComponent(dir.id)}`, { method: "DELETE" });
|
|
183
|
+
}
|
|
184
|
+
async readdir(path, options) {
|
|
185
|
+
await this.ensureReady();
|
|
186
|
+
const dir = await this.getFile(path);
|
|
187
|
+
if (dir.mimeType !== FOLDER_MIME_TYPE) throw new workspace.NotDirectoryError(path);
|
|
188
|
+
const entries = await this.readdirRecursive(dir.id, options, 0);
|
|
189
|
+
const extensions = Array.isArray(options?.extension) ? options.extension : options?.extension ? [options.extension] : void 0;
|
|
190
|
+
return extensions ? entries.filter((entry) => entry.type === "directory" || extensions.some((ext) => entry.name.endsWith(ext))) : entries;
|
|
191
|
+
}
|
|
192
|
+
async exists(path) {
|
|
193
|
+
await this.ensureReady();
|
|
194
|
+
return Boolean(await this.findFile(path));
|
|
195
|
+
}
|
|
196
|
+
async stat(path) {
|
|
197
|
+
await this.ensureReady();
|
|
198
|
+
const file = await this.getFile(path);
|
|
199
|
+
const isDirectory = file.mimeType === FOLDER_MIME_TYPE;
|
|
200
|
+
return {
|
|
201
|
+
name: file.name,
|
|
202
|
+
path: this.normalize(path),
|
|
203
|
+
type: isDirectory ? "directory" : "file",
|
|
204
|
+
size: Number(file.size ?? 0),
|
|
205
|
+
createdAt: file.createdTime ? new Date(file.createdTime) : /* @__PURE__ */ new Date(0),
|
|
206
|
+
modifiedAt: file.modifiedTime ? new Date(file.modifiedTime) : /* @__PURE__ */ new Date(0),
|
|
207
|
+
mimeType: isDirectory ? void 0 : file.mimeType
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
async realpath(path) {
|
|
211
|
+
return this.normalize(path);
|
|
212
|
+
}
|
|
213
|
+
assertWritable(operation) {
|
|
214
|
+
if (this.readOnly) throw new workspace.WorkspaceReadOnlyError(operation);
|
|
215
|
+
}
|
|
216
|
+
toBuffer(content) {
|
|
217
|
+
if (Buffer.isBuffer(content)) return content;
|
|
218
|
+
if (content instanceof Uint8Array) return Buffer.from(content);
|
|
219
|
+
return Buffer.from(content, "utf-8");
|
|
220
|
+
}
|
|
221
|
+
normalize(path) {
|
|
222
|
+
const parts = path.split("/").filter(Boolean);
|
|
223
|
+
const stack = [];
|
|
224
|
+
for (const part of parts) {
|
|
225
|
+
if (part === ".") continue;
|
|
226
|
+
if (part === "..") stack.pop();
|
|
227
|
+
else stack.push(part);
|
|
228
|
+
}
|
|
229
|
+
return `/${stack.join("/")}`;
|
|
230
|
+
}
|
|
231
|
+
async getFile(path) {
|
|
232
|
+
const file = await this.findFile(path);
|
|
233
|
+
if (!file) throw new workspace.FileNotFoundError(path);
|
|
234
|
+
return file;
|
|
235
|
+
}
|
|
236
|
+
async findFile(path) {
|
|
237
|
+
const normalized = this.normalize(path);
|
|
238
|
+
if (normalized === "/") return this.rootFile();
|
|
239
|
+
const names = normalized.split("/").filter(Boolean);
|
|
240
|
+
let parentId = this.folderId;
|
|
241
|
+
let file;
|
|
242
|
+
for (const name of names) {
|
|
243
|
+
file = await this.findChild(parentId, name);
|
|
244
|
+
if (!file) return void 0;
|
|
245
|
+
parentId = file.id;
|
|
246
|
+
}
|
|
247
|
+
return file;
|
|
248
|
+
}
|
|
249
|
+
async rootFile() {
|
|
250
|
+
return this.request(`${DRIVE_API}/files/${encodeURIComponent(this.folderId)}`, {
|
|
251
|
+
searchParams: { fields: "id,name,mimeType,size,createdTime,modifiedTime,parents", supportsAllDrives: "true" }
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
async resolveParent(path, recursive) {
|
|
255
|
+
const normalized = this.normalize(path);
|
|
256
|
+
const parts = normalized.split("/").filter(Boolean);
|
|
257
|
+
const name = parts.pop();
|
|
258
|
+
if (!name) throw new workspace.IsDirectoryError(path);
|
|
259
|
+
const parentPath = `/${parts.join("/")}`;
|
|
260
|
+
const parent = recursive ? await this.resolveDir(parentPath, true) : await this.findFile(parentPath);
|
|
261
|
+
if (!parent) throw new workspace.DirectoryNotFoundError(parentPath);
|
|
262
|
+
if (parent.mimeType !== FOLDER_MIME_TYPE) throw new workspace.NotDirectoryError(parentPath);
|
|
263
|
+
return { parentId: parent.id, name };
|
|
264
|
+
}
|
|
265
|
+
async resolveDir(path, recursive) {
|
|
266
|
+
const normalized = this.normalize(path);
|
|
267
|
+
if (normalized === "/") return this.rootFile();
|
|
268
|
+
const names = normalized.split("/").filter(Boolean);
|
|
269
|
+
let parentId = this.folderId;
|
|
270
|
+
let current;
|
|
271
|
+
for (const name of names) {
|
|
272
|
+
current = await this.findChild(parentId, name);
|
|
273
|
+
if (current) {
|
|
274
|
+
if (current.mimeType !== FOLDER_MIME_TYPE) throw new workspace.NotDirectoryError(name);
|
|
275
|
+
parentId = current.id;
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
if (!recursive) throw new workspace.DirectoryNotFoundError(normalized);
|
|
279
|
+
current = await this.createFolder(parentId, name);
|
|
280
|
+
parentId = current.id;
|
|
281
|
+
}
|
|
282
|
+
return current;
|
|
283
|
+
}
|
|
284
|
+
async createFolder(parentId, name) {
|
|
285
|
+
return this.request(`${DRIVE_API}/files`, {
|
|
286
|
+
method: "POST",
|
|
287
|
+
searchParams: { fields: "id,name,mimeType,size,createdTime,modifiedTime,parents", supportsAllDrives: "true" },
|
|
288
|
+
body: JSON.stringify({ name, mimeType: FOLDER_MIME_TYPE, parents: [parentId] })
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
async findChild(parentId, name) {
|
|
292
|
+
const files = await this.listChildren(parentId, `name = '${this.escapeQuery(name)}'`);
|
|
293
|
+
return files[0];
|
|
294
|
+
}
|
|
295
|
+
async listChildren(parentId, extraQuery) {
|
|
296
|
+
const query = [`'${this.escapeQuery(parentId)}' in parents`, "trashed = false", extraQuery].filter(Boolean).join(" and ");
|
|
297
|
+
const files = [];
|
|
298
|
+
let pageToken;
|
|
299
|
+
do {
|
|
300
|
+
const result = await this.request(`${DRIVE_API}/files`, {
|
|
301
|
+
searchParams: {
|
|
302
|
+
q: query,
|
|
303
|
+
fields: "nextPageToken,files(id,name,mimeType,size,createdTime,modifiedTime,parents)",
|
|
304
|
+
pageSize: "1000",
|
|
305
|
+
supportsAllDrives: "true",
|
|
306
|
+
includeItemsFromAllDrives: "true",
|
|
307
|
+
...pageToken ? { pageToken } : {}
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
files.push(...result.files ?? []);
|
|
311
|
+
pageToken = result.nextPageToken;
|
|
312
|
+
} while (pageToken);
|
|
313
|
+
return files;
|
|
314
|
+
}
|
|
315
|
+
async readdirRecursive(parentId, options, depth) {
|
|
316
|
+
const children = await this.listChildren(parentId);
|
|
317
|
+
const entries = [];
|
|
318
|
+
for (const child of children) {
|
|
319
|
+
const isDirectory = child.mimeType === FOLDER_MIME_TYPE;
|
|
320
|
+
entries.push({ name: child.name, type: isDirectory ? "directory" : "file", size: Number(child.size ?? 0) });
|
|
321
|
+
const shouldDescend = isDirectory && options?.recursive && (options.maxDepth === void 0 || depth < options.maxDepth);
|
|
322
|
+
if (shouldDescend) {
|
|
323
|
+
const nested = await this.readdirRecursive(child.id, options, depth + 1);
|
|
324
|
+
entries.push(...nested.map((entry) => ({ ...entry, name: `${child.name}/${entry.name}` })));
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return entries;
|
|
328
|
+
}
|
|
329
|
+
async deleteAny(file, path, recursive) {
|
|
330
|
+
if (file.mimeType === FOLDER_MIME_TYPE) await this.rmdir(path, { recursive, force: true });
|
|
331
|
+
else await this.deleteFile(path, { force: true });
|
|
332
|
+
}
|
|
333
|
+
async upload(fileId, content, mimeType = "application/octet-stream", method, metadata) {
|
|
334
|
+
const boundary = `mastra-${Date.now()}`;
|
|
335
|
+
const body = Buffer.concat([
|
|
336
|
+
Buffer.from(
|
|
337
|
+
`--${boundary}\r
|
|
338
|
+
Content-Type: application/json; charset=UTF-8\r
|
|
339
|
+
\r
|
|
340
|
+
${JSON.stringify(metadata ?? {})}\r
|
|
341
|
+
`
|
|
342
|
+
),
|
|
343
|
+
Buffer.from(`--${boundary}\r
|
|
344
|
+
Content-Type: ${mimeType}\r
|
|
345
|
+
\r
|
|
346
|
+
`),
|
|
347
|
+
this.toBuffer(content),
|
|
348
|
+
Buffer.from(`\r
|
|
349
|
+
--${boundary}--`)
|
|
350
|
+
]);
|
|
351
|
+
const url = fileId ? `${DRIVE_UPLOAD_API}/files/${encodeURIComponent(fileId)}` : `${DRIVE_UPLOAD_API}/files`;
|
|
352
|
+
await this.request(url, {
|
|
353
|
+
method,
|
|
354
|
+
searchParams: { uploadType: "multipart", fields: "id", supportsAllDrives: "true" },
|
|
355
|
+
headers: { "Content-Type": `multipart/related; boundary=${boundary}` },
|
|
356
|
+
body
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
escapeQuery(value) {
|
|
360
|
+
return value.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
|
|
361
|
+
}
|
|
362
|
+
async request(url, init = {}) {
|
|
363
|
+
const response = await this.fetch(url, init);
|
|
364
|
+
if (response.status === 204) return void 0;
|
|
365
|
+
return await response.json();
|
|
366
|
+
}
|
|
367
|
+
async fetch(url, init = {}) {
|
|
368
|
+
const token = await this.getToken();
|
|
369
|
+
const target = new URL(url);
|
|
370
|
+
for (const [key, value] of Object.entries(init.searchParams ?? {})) target.searchParams.set(key, value);
|
|
371
|
+
const response = await globalThis.fetch(target, {
|
|
372
|
+
...init,
|
|
373
|
+
headers: { Authorization: `Bearer ${token}`, ...init.headers }
|
|
374
|
+
});
|
|
375
|
+
if (!response.ok) {
|
|
376
|
+
const message = await response.text().catch(() => response.statusText);
|
|
377
|
+
throw new Error(`Google Drive API request failed (${response.status}): ${message}`);
|
|
378
|
+
}
|
|
379
|
+
return response;
|
|
380
|
+
}
|
|
381
|
+
async getToken() {
|
|
382
|
+
if (this.accessToken && Date.now() < this.tokenExpiresAt - 6e4) return this.accessToken;
|
|
383
|
+
if (this.getAccessToken) return this.getAccessToken();
|
|
384
|
+
if (this.serviceAccount) return this.getServiceAccountToken();
|
|
385
|
+
if (this.accessToken) return this.accessToken;
|
|
386
|
+
throw new Error("GoogleDriveFilesystem requires accessToken, getAccessToken, or serviceAccount authentication.");
|
|
387
|
+
}
|
|
388
|
+
async getServiceAccountToken() {
|
|
389
|
+
const account = this.serviceAccount;
|
|
390
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
391
|
+
const header = { alg: "RS256", typ: "JWT", ...account.privateKeyId ? { kid: account.privateKeyId } : {} };
|
|
392
|
+
const claim = {
|
|
393
|
+
iss: account.clientEmail,
|
|
394
|
+
scope: (account.scopes ?? DEFAULT_SCOPES).join(" "),
|
|
395
|
+
aud: OAUTH_TOKEN_URL,
|
|
396
|
+
exp: now + 3600,
|
|
397
|
+
iat: now,
|
|
398
|
+
...account.subject ? { sub: account.subject } : {}
|
|
399
|
+
};
|
|
400
|
+
const unsigned = `${this.base64Url(JSON.stringify(header))}.${this.base64Url(JSON.stringify(claim))}`;
|
|
401
|
+
const privateKey = this.normalizePrivateKey(account.privateKey);
|
|
402
|
+
let signature;
|
|
403
|
+
try {
|
|
404
|
+
signature = crypto.createSign("RSA-SHA256").update(unsigned).sign(privateKey, "base64url");
|
|
405
|
+
} catch (err) {
|
|
406
|
+
const hasBegin = privateKey.includes("-----BEGIN");
|
|
407
|
+
const hasEnd = privateKey.includes("-----END");
|
|
408
|
+
throw new Error(
|
|
409
|
+
`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.`
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
const response = await globalThis.fetch(OAUTH_TOKEN_URL, {
|
|
413
|
+
method: "POST",
|
|
414
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
415
|
+
body: new URLSearchParams({
|
|
416
|
+
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
|
|
417
|
+
assertion: `${unsigned}.${signature}`
|
|
418
|
+
})
|
|
419
|
+
});
|
|
420
|
+
if (!response.ok)
|
|
421
|
+
throw new Error(`Google service account token request failed (${response.status}): ${await response.text()}`);
|
|
422
|
+
const json = await response.json();
|
|
423
|
+
this.accessToken = json.access_token;
|
|
424
|
+
this.tokenExpiresAt = Date.now() + json.expires_in * 1e3;
|
|
425
|
+
return json.access_token;
|
|
426
|
+
}
|
|
427
|
+
base64Url(value) {
|
|
428
|
+
return Buffer.from(value).toString("base64url");
|
|
429
|
+
}
|
|
430
|
+
normalizePrivateKey(key) {
|
|
431
|
+
let out = key.trim();
|
|
432
|
+
for (let i = 0; i < 5; i++) {
|
|
433
|
+
const before = out;
|
|
434
|
+
if (out.endsWith(",")) out = out.slice(0, -1).trim();
|
|
435
|
+
if (out.startsWith('"') && out.endsWith('"') || out.startsWith("'") && out.endsWith("'")) {
|
|
436
|
+
out = out.slice(1, -1);
|
|
437
|
+
}
|
|
438
|
+
if (out.startsWith('\\"') && out.endsWith('\\"') || out.startsWith("\\'") && out.endsWith("\\'")) {
|
|
439
|
+
out = out.slice(2, -2);
|
|
440
|
+
}
|
|
441
|
+
if (out === before) break;
|
|
442
|
+
}
|
|
443
|
+
out = out.replace(/\\n/g, "\n");
|
|
444
|
+
out = out.replace(/\\"/g, '"').replace(/\\'/g, "'");
|
|
445
|
+
out = out.replace(/\r\n?/g, "\n");
|
|
446
|
+
if (!out.endsWith("\n")) out += "\n";
|
|
447
|
+
return out;
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
// src/provider.ts
|
|
452
|
+
var googleDriveFilesystemProvider = {
|
|
453
|
+
id: "google-drive",
|
|
454
|
+
name: "Google Drive",
|
|
455
|
+
description: "Google Drive folder mounted as a filesystem",
|
|
456
|
+
configSchema: {
|
|
457
|
+
type: "object",
|
|
458
|
+
required: ["folderId"],
|
|
459
|
+
properties: {
|
|
460
|
+
folderId: { type: "string", description: "Google Drive folder ID to mount as the workspace root" },
|
|
461
|
+
accessToken: {
|
|
462
|
+
type: "string",
|
|
463
|
+
description: "OAuth access token with the https://www.googleapis.com/auth/drive scope"
|
|
464
|
+
},
|
|
465
|
+
readOnly: { type: "boolean", description: "Mount as read-only", default: false }
|
|
466
|
+
}
|
|
467
|
+
},
|
|
468
|
+
createFilesystem: (config) => new GoogleDriveFilesystem(config)
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
exports.GoogleDriveFilesystem = GoogleDriveFilesystem;
|
|
472
|
+
exports.googleDriveFilesystemProvider = googleDriveFilesystemProvider;
|
|
473
|
+
//# sourceMappingURL=index.cjs.map
|
|
474
|
+
//# sourceMappingURL=index.cjs.map
|