brain-memory 0.1.0-beta.2 → 0.1.0-beta.4

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.
@@ -0,0 +1,496 @@
1
+ /**
2
+ * Brain Memory — Google Drive Sync Provider
3
+ *
4
+ * Google Drive API v3 integration using appDataFolder space.
5
+ * Key challenge: Drive is ID-based, not path-based. Maintains a path↔ID cache.
6
+ */
7
+
8
+ const crypto = require('crypto');
9
+ const { SyncProvider } = require('../provider');
10
+
11
+ const API_BASE = 'https://www.googleapis.com/drive/v3';
12
+ const UPLOAD_BASE = 'https://www.googleapis.com/upload/drive/v3';
13
+
14
+ class GoogleDriveProvider extends SyncProvider {
15
+ constructor(config) {
16
+ super(config);
17
+ this._pathToId = new Map(); // 'professional/skills/ts.md' → fileId
18
+ this._idToPath = new Map(); // fileId → 'professional/skills/ts.md'
19
+ this._parentCache = new Map(); // folderId → parentFolderId
20
+
21
+ // Restore cache from persisted state if available
22
+ if (config.options?.pathCache) {
23
+ for (const [p, id] of Object.entries(config.options.pathCache)) {
24
+ this._pathToId.set(p, id);
25
+ this._idToPath.set(id, p);
26
+ }
27
+ }
28
+ }
29
+
30
+ /**
31
+ * Compute content hash using MD5 (matches Drive's md5Checksum).
32
+ *
33
+ * @param {Buffer} buffer - File content
34
+ * @returns {string} Hex-encoded MD5 hash
35
+ */
36
+ computeContentHash(buffer) {
37
+ return crypto.createHash('md5').update(buffer).digest('hex');
38
+ }
39
+
40
+ /**
41
+ * Export the path↔ID cache for persistence in sync-state.json.
42
+ *
43
+ * @returns {Object} Cache as plain object
44
+ */
45
+ exportPathCache() {
46
+ const cache = {};
47
+ for (const [p, id] of this._pathToId) {
48
+ cache[p] = id;
49
+ }
50
+ return cache;
51
+ }
52
+
53
+ async listFiles(cursor) {
54
+ if (cursor) {
55
+ return this._listChanges(cursor);
56
+ }
57
+ return this._buildFullListing();
58
+ }
59
+
60
+ async _buildFullListing() {
61
+ const files = [];
62
+ let pageToken = null;
63
+
64
+ do {
65
+ const params = new URLSearchParams({
66
+ spaces: 'appDataFolder',
67
+ fields: 'nextPageToken, files(id, name, parents, md5Checksum, modifiedTime, size, mimeType)',
68
+ pageSize: '1000',
69
+ });
70
+ if (pageToken) params.set('pageToken', pageToken);
71
+
72
+ const response = await fetch(`${API_BASE}/files?${params}`, {
73
+ headers: { 'Authorization': `Bearer ${this.accessToken}` },
74
+ });
75
+
76
+ if (!response.ok) {
77
+ const text = await response.text();
78
+ throw new Error(`Google Drive list failed (${response.status}): ${text}`);
79
+ }
80
+
81
+ const data = await response.json();
82
+
83
+ for (const file of data.files || []) {
84
+ if (file.mimeType === 'application/vnd.google-apps.folder') {
85
+ // Cache folder ID → name for path resolution
86
+ this._idToPath.set(file.id, file.name);
87
+ if (file.parents?.[0]) {
88
+ this._parentCache.set(file.id, file.parents[0]);
89
+ }
90
+ } else {
91
+ files.push(file);
92
+ }
93
+ }
94
+
95
+ pageToken = data.nextPageToken;
96
+ } while (pageToken);
97
+
98
+ // Resolve full paths for all files
99
+ const result = [];
100
+ for (const file of files) {
101
+ const filePath = this._resolveIdToPath(file);
102
+ if (filePath) {
103
+ this._pathToId.set(filePath, file.id);
104
+ this._idToPath.set(file.id, filePath);
105
+ result.push({
106
+ path: filePath,
107
+ rev: file.id, // Use file ID as revision identifier
108
+ hash: file.md5Checksum || '',
109
+ modified: file.modifiedTime,
110
+ size: parseInt(file.size || '0', 10),
111
+ });
112
+ }
113
+ }
114
+
115
+ // Get the initial changes start token for delta sync
116
+ const startTokenResp = await fetch(
117
+ `${API_BASE}/changes/getStartPageToken?${new URLSearchParams({ spaces: 'appDataFolder' })}`,
118
+ { headers: { 'Authorization': `Bearer ${this.accessToken}` } }
119
+ );
120
+ const startTokenData = await startTokenResp.json();
121
+
122
+ return {
123
+ files: result,
124
+ cursor: startTokenData.startPageToken,
125
+ hasMore: false,
126
+ };
127
+ }
128
+
129
+ _resolveIdToPath(file) {
130
+ const parts = [file.name];
131
+ let parentId = file.parents?.[0];
132
+
133
+ // Walk up the parent chain
134
+ while (parentId) {
135
+ const parentName = this._idToPath.get(parentId);
136
+ const grandparentId = this._parentCache.get(parentId);
137
+
138
+ // If we've reached the appDataFolder root, stop
139
+ if (!grandparentId || !parentName) break;
140
+
141
+ parts.unshift(parentName);
142
+ parentId = grandparentId;
143
+ }
144
+
145
+ return parts.join('/');
146
+ }
147
+
148
+ async _listChanges(cursor) {
149
+ const params = new URLSearchParams({
150
+ pageToken: cursor,
151
+ spaces: 'appDataFolder',
152
+ fields: 'nextPageToken, newStartPageToken, changes(fileId, removed, file(id, name, parents, md5Checksum, modifiedTime, size, mimeType))',
153
+ });
154
+
155
+ const response = await fetch(`${API_BASE}/changes/list?${params}`, {
156
+ headers: { 'Authorization': `Bearer ${this.accessToken}` },
157
+ });
158
+
159
+ if (!response.ok) {
160
+ const text = await response.text();
161
+ throw new Error(`Google Drive changes.list failed (${response.status}): ${text}`);
162
+ }
163
+
164
+ const data = await response.json();
165
+ const files = [];
166
+
167
+ for (const change of data.changes || []) {
168
+ if (change.removed) {
169
+ // File was deleted — find its cached path
170
+ const cachedPath = this._idToPath.get(change.fileId);
171
+ if (cachedPath) {
172
+ files.push({
173
+ path: cachedPath,
174
+ rev: change.fileId,
175
+ hash: '',
176
+ modified: '',
177
+ size: 0,
178
+ deleted: true,
179
+ });
180
+ this._pathToId.delete(cachedPath);
181
+ this._idToPath.delete(change.fileId);
182
+ }
183
+ } else if (change.file && change.file.mimeType !== 'application/vnd.google-apps.folder') {
184
+ const filePath = this._resolveIdToPath(change.file);
185
+ if (filePath) {
186
+ this._pathToId.set(filePath, change.file.id);
187
+ this._idToPath.set(change.file.id, filePath);
188
+ files.push({
189
+ path: filePath,
190
+ rev: change.file.id,
191
+ hash: change.file.md5Checksum || '',
192
+ modified: change.file.modifiedTime,
193
+ size: parseInt(change.file.size || '0', 10),
194
+ });
195
+ }
196
+ }
197
+ }
198
+
199
+ return {
200
+ files,
201
+ cursor: data.newStartPageToken || data.nextPageToken,
202
+ hasMore: !!data.nextPageToken,
203
+ };
204
+ }
205
+
206
+ async downloadFile(remotePath) {
207
+ const fileId = await this._resolvePathToId(remotePath);
208
+ if (!fileId) throw new Error(`File not found in Google Drive: ${remotePath}`);
209
+
210
+ const response = await fetch(`${API_BASE}/files/${fileId}?alt=media`, {
211
+ headers: { 'Authorization': `Bearer ${this.accessToken}` },
212
+ });
213
+
214
+ if (!response.ok) {
215
+ const text = await response.text();
216
+ throw new Error(`Google Drive download failed (${response.status}): ${text}`);
217
+ }
218
+
219
+ const buffer = Buffer.from(await response.arrayBuffer());
220
+
221
+ // Get metadata for hash
222
+ const metaResp = await fetch(
223
+ `${API_BASE}/files/${fileId}?fields=md5Checksum`,
224
+ { headers: { 'Authorization': `Bearer ${this.accessToken}` } }
225
+ );
226
+ const meta = metaResp.ok ? await metaResp.json() : {};
227
+
228
+ return {
229
+ content: buffer,
230
+ rev: fileId,
231
+ hash: meta.md5Checksum || this.computeContentHash(buffer),
232
+ };
233
+ }
234
+
235
+ async uploadFile(remotePath, content, options = {}) {
236
+ const existingId = await this._resolvePathToId(remotePath);
237
+
238
+ if (existingId) {
239
+ return this._updateFile(existingId, remotePath, content);
240
+ }
241
+
242
+ return this._createFile(remotePath, content);
243
+ }
244
+
245
+ async _createFile(remotePath, content) {
246
+ const parts = remotePath.split('/');
247
+ const fileName = parts.pop();
248
+ const parentId = await this._ensureParentFolders(parts);
249
+
250
+ // Multipart upload: metadata + content
251
+ const boundary = `brain_${Date.now()}`;
252
+ const metadata = JSON.stringify({
253
+ name: fileName,
254
+ parents: [parentId],
255
+ });
256
+
257
+ const body = Buffer.concat([
258
+ Buffer.from(`--${boundary}\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n`),
259
+ Buffer.from(metadata),
260
+ Buffer.from(`\r\n--${boundary}\r\nContent-Type: application/octet-stream\r\n\r\n`),
261
+ content,
262
+ Buffer.from(`\r\n--${boundary}--`),
263
+ ]);
264
+
265
+ const response = await fetch(
266
+ `${UPLOAD_BASE}/files?uploadType=multipart&fields=id,md5Checksum`,
267
+ {
268
+ method: 'POST',
269
+ headers: {
270
+ 'Authorization': `Bearer ${this.accessToken}`,
271
+ 'Content-Type': `multipart/related; boundary=${boundary}`,
272
+ },
273
+ body,
274
+ }
275
+ );
276
+
277
+ if (!response.ok) {
278
+ const text = await response.text();
279
+ throw new Error(`Google Drive upload failed (${response.status}): ${text}`);
280
+ }
281
+
282
+ const data = await response.json();
283
+ this._pathToId.set(remotePath, data.id);
284
+ this._idToPath.set(data.id, remotePath);
285
+
286
+ return {
287
+ rev: data.id,
288
+ hash: data.md5Checksum || '',
289
+ };
290
+ }
291
+
292
+ async _updateFile(fileId, remotePath, content) {
293
+ const response = await fetch(
294
+ `${UPLOAD_BASE}/files/${fileId}?uploadType=media&fields=id,md5Checksum`,
295
+ {
296
+ method: 'PATCH',
297
+ headers: {
298
+ 'Authorization': `Bearer ${this.accessToken}`,
299
+ 'Content-Type': 'application/octet-stream',
300
+ },
301
+ body: content,
302
+ }
303
+ );
304
+
305
+ if (!response.ok) {
306
+ const text = await response.text();
307
+ throw new Error(`Google Drive update failed (${response.status}): ${text}`);
308
+ }
309
+
310
+ const data = await response.json();
311
+ return {
312
+ rev: data.id,
313
+ hash: data.md5Checksum || '',
314
+ };
315
+ }
316
+
317
+ async _ensureParentFolders(pathParts) {
318
+ let parentId = 'appDataFolder';
319
+
320
+ for (const folderName of pathParts) {
321
+ const existingId = this._pathToId.get(
322
+ this._buildPartialPath(pathParts, folderName)
323
+ );
324
+ if (existingId) {
325
+ parentId = existingId;
326
+ continue;
327
+ }
328
+
329
+ // Search for existing folder
330
+ const query = `name='${folderName}' and '${parentId}' in parents and mimeType='application/vnd.google-apps.folder' and trashed=false`;
331
+ const params = new URLSearchParams({
332
+ q: query,
333
+ spaces: 'appDataFolder',
334
+ fields: 'files(id)',
335
+ });
336
+
337
+ const searchResp = await fetch(`${API_BASE}/files?${params}`, {
338
+ headers: { 'Authorization': `Bearer ${this.accessToken}` },
339
+ });
340
+
341
+ if (searchResp.ok) {
342
+ const searchData = await searchResp.json();
343
+ if (searchData.files?.length > 0) {
344
+ parentId = searchData.files[0].id;
345
+ continue;
346
+ }
347
+ }
348
+
349
+ // Create folder
350
+ const createResp = await fetch(`${API_BASE}/files`, {
351
+ method: 'POST',
352
+ headers: {
353
+ 'Authorization': `Bearer ${this.accessToken}`,
354
+ 'Content-Type': 'application/json',
355
+ },
356
+ body: JSON.stringify({
357
+ name: folderName,
358
+ mimeType: 'application/vnd.google-apps.folder',
359
+ parents: [parentId],
360
+ }),
361
+ });
362
+
363
+ if (!createResp.ok) {
364
+ const text = await createResp.text();
365
+ throw new Error(`Google Drive folder creation failed (${createResp.status}): ${text}`);
366
+ }
367
+
368
+ const folder = await createResp.json();
369
+ parentId = folder.id;
370
+ }
371
+
372
+ return parentId;
373
+ }
374
+
375
+ _buildPartialPath(allParts, upToName) {
376
+ const idx = allParts.indexOf(upToName);
377
+ return allParts.slice(0, idx + 1).join('/');
378
+ }
379
+
380
+ async deleteFile(remotePath) {
381
+ const fileId = await this._resolvePathToId(remotePath);
382
+ if (!fileId) return { success: true }; // Already gone
383
+
384
+ const response = await fetch(`${API_BASE}/files/${fileId}`, {
385
+ method: 'DELETE',
386
+ headers: { 'Authorization': `Bearer ${this.accessToken}` },
387
+ });
388
+
389
+ if (!response.ok && response.status !== 404) {
390
+ const text = await response.text();
391
+ throw new Error(`Google Drive delete failed (${response.status}): ${text}`);
392
+ }
393
+
394
+ this._pathToId.delete(remotePath);
395
+ this._idToPath.delete(fileId);
396
+
397
+ return { success: true };
398
+ }
399
+
400
+ async getFileMetadata(remotePath) {
401
+ const fileId = await this._resolvePathToId(remotePath);
402
+ if (!fileId) return null;
403
+
404
+ const params = new URLSearchParams({
405
+ fields: 'id, md5Checksum, modifiedTime, size',
406
+ });
407
+
408
+ const response = await fetch(`${API_BASE}/files/${fileId}?${params}`, {
409
+ headers: { 'Authorization': `Bearer ${this.accessToken}` },
410
+ });
411
+
412
+ if (!response.ok) {
413
+ if (response.status === 404) return null;
414
+ const text = await response.text();
415
+ throw new Error(`Google Drive metadata failed (${response.status}): ${text}`);
416
+ }
417
+
418
+ const data = await response.json();
419
+ return {
420
+ rev: data.id,
421
+ hash: data.md5Checksum || '',
422
+ modified: data.modifiedTime,
423
+ size: parseInt(data.size || '0', 10),
424
+ };
425
+ }
426
+
427
+ async createFolder(remotePath) {
428
+ const parts = remotePath.split('/');
429
+ await this._ensureParentFolders(parts);
430
+ }
431
+
432
+ async testConnection() {
433
+ const response = await fetch('https://www.googleapis.com/drive/v3/about?fields=user', {
434
+ headers: { 'Authorization': `Bearer ${this.accessToken}` },
435
+ });
436
+
437
+ if (!response.ok) {
438
+ return { ok: false, account: null };
439
+ }
440
+
441
+ const data = await response.json();
442
+ return {
443
+ ok: true,
444
+ account: data.user?.emailAddress || data.user?.displayName || 'Unknown',
445
+ };
446
+ }
447
+
448
+ /**
449
+ * Resolve a relative path to a Google Drive file ID.
450
+ * Uses cache first, falls back to API search.
451
+ *
452
+ * @param {string} remotePath - Relative path
453
+ * @returns {Promise<string|null>} File ID or null
454
+ */
455
+ async _resolvePathToId(remotePath) {
456
+ // Check cache first
457
+ const cached = this._pathToId.get(remotePath);
458
+ if (cached) return cached;
459
+
460
+ // Walk path components via API
461
+ const parts = remotePath.split('/');
462
+ let parentId = 'appDataFolder';
463
+
464
+ for (let i = 0; i < parts.length; i++) {
465
+ const name = parts[i];
466
+ const isLast = i === parts.length - 1;
467
+ const mimeFilter = isLast ? '' : ` and mimeType='application/vnd.google-apps.folder'`;
468
+
469
+ const query = `name='${name}' and '${parentId}' in parents${mimeFilter} and trashed=false`;
470
+ const params = new URLSearchParams({
471
+ q: query,
472
+ spaces: 'appDataFolder',
473
+ fields: 'files(id)',
474
+ });
475
+
476
+ const response = await fetch(`${API_BASE}/files?${params}`, {
477
+ headers: { 'Authorization': `Bearer ${this.accessToken}` },
478
+ });
479
+
480
+ if (!response.ok) return null;
481
+
482
+ const data = await response.json();
483
+ if (!data.files?.length) return null;
484
+
485
+ parentId = data.files[0].id;
486
+ }
487
+
488
+ // Cache the result
489
+ this._pathToId.set(remotePath, parentId);
490
+ this._idToPath.set(parentId, remotePath);
491
+
492
+ return parentId;
493
+ }
494
+ }
495
+
496
+ module.exports = { GoogleDriveProvider };