@n24q02m/workspace 0.0.0-stage → 0.6.0-beta.1
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/LICENSE +202 -0
- package/NOTICE +46 -0
- package/README.md +342 -2
- package/bin/cli.mjs +144 -0
- package/build/src/auth/account-context.d.ts +3 -0
- package/build/src/auth/account-context.d.ts.map +1 -0
- package/build/src/auth/account-context.js +17 -0
- package/build/src/auth/account-context.js.map +1 -0
- package/build/src/auth/account-context.test.d.ts +2 -0
- package/build/src/auth/account-context.test.d.ts.map +1 -0
- package/build/src/auth/account-context.test.js +30 -0
- package/build/src/auth/account-context.test.js.map +1 -0
- package/build/src/auth/account-store.d.ts +87 -0
- package/build/src/auth/account-store.d.ts.map +1 -0
- package/build/src/auth/account-store.js +187 -0
- package/build/src/auth/account-store.js.map +1 -0
- package/build/src/auth/account-store.test.d.ts +2 -0
- package/build/src/auth/account-store.test.d.ts.map +1 -0
- package/build/src/auth/account-store.test.js +264 -0
- package/build/src/auth/account-store.test.js.map +1 -0
- package/build/src/auth/add-account.d.ts +12 -0
- package/build/src/auth/add-account.d.ts.map +1 -0
- package/build/src/auth/add-account.js +95 -0
- package/build/src/auth/add-account.js.map +1 -0
- package/build/src/auth/add-account.test.d.ts +2 -0
- package/build/src/auth/add-account.test.d.ts.map +1 -0
- package/build/src/auth/add-account.test.js +155 -0
- package/build/src/auth/add-account.test.js.map +1 -0
- package/build/src/auth/cli-auth.d.ts +2 -0
- package/build/src/auth/cli-auth.d.ts.map +1 -0
- package/build/src/auth/cli-auth.js +47 -0
- package/build/src/auth/cli-auth.js.map +1 -0
- package/build/src/auth/cli-auth.test.d.ts +2 -0
- package/build/src/auth/cli-auth.test.d.ts.map +1 -0
- package/build/src/auth/cli-auth.test.js +78 -0
- package/build/src/auth/cli-auth.test.js.map +1 -0
- package/build/src/auth/consent-server.d.ts +43 -0
- package/build/src/auth/consent-server.d.ts.map +1 -0
- package/build/src/auth/consent-server.js +56 -0
- package/build/src/auth/consent-server.js.map +1 -0
- package/build/src/auth/credential-state.d.ts +7 -0
- package/build/src/auth/credential-state.d.ts.map +1 -0
- package/build/src/auth/credential-state.js +64 -0
- package/build/src/auth/credential-state.js.map +1 -0
- package/build/src/auth/credential-state.test.d.ts +2 -0
- package/build/src/auth/credential-state.test.d.ts.map +1 -0
- package/build/src/auth/credential-state.test.js +142 -0
- package/build/src/auth/credential-state.test.js.map +1 -0
- package/build/src/auth/loopback-consent.d.ts +23 -0
- package/build/src/auth/loopback-consent.d.ts.map +1 -0
- package/build/src/auth/loopback-consent.js +145 -0
- package/build/src/auth/loopback-consent.js.map +1 -0
- package/build/src/auth/loopback-consent.test.d.ts +2 -0
- package/build/src/auth/loopback-consent.test.d.ts.map +1 -0
- package/build/src/auth/loopback-consent.test.js +131 -0
- package/build/src/auth/loopback-consent.test.js.map +1 -0
- package/build/src/auth/oauth-setup.d.ts +10 -0
- package/build/src/auth/oauth-setup.d.ts.map +1 -0
- package/build/src/auth/oauth-setup.js +92 -0
- package/build/src/auth/oauth-setup.js.map +1 -0
- package/build/src/auth/oauth-setup.test.d.ts +2 -0
- package/build/src/auth/oauth-setup.test.d.ts.map +1 -0
- package/build/src/auth/oauth-setup.test.js +164 -0
- package/build/src/auth/oauth-setup.test.js.map +1 -0
- package/build/src/auth/subject-context.d.ts +2 -0
- package/build/src/auth/subject-context.d.ts.map +1 -0
- package/build/src/auth/subject-context.js +16 -0
- package/build/src/auth/subject-context.js.map +1 -0
- package/build/src/auth/subject-context.test.d.ts +2 -0
- package/build/src/auth/subject-context.test.d.ts.map +1 -0
- package/build/src/auth/subject-context.test.js +34 -0
- package/build/src/auth/subject-context.test.js.map +1 -0
- package/build/src/auth/workspace-auth.d.ts +47 -0
- package/build/src/auth/workspace-auth.d.ts.map +1 -0
- package/build/src/auth/workspace-auth.js +123 -0
- package/build/src/auth/workspace-auth.js.map +1 -0
- package/build/src/auth/workspace-auth.test.d.ts +2 -0
- package/build/src/auth/workspace-auth.test.d.ts.map +1 -0
- package/build/src/auth/workspace-auth.test.js +242 -0
- package/build/src/auth/workspace-auth.test.js.map +1 -0
- package/build/src/constants.d.ts +6 -0
- package/build/src/constants.d.ts.map +1 -0
- package/build/src/constants.js +11 -0
- package/build/src/constants.js.map +1 -0
- package/build/src/create-server.d.ts +13 -0
- package/build/src/create-server.d.ts.map +1 -0
- package/build/src/create-server.js +34 -0
- package/build/src/create-server.js.map +1 -0
- package/build/src/create-server.test.d.ts +2 -0
- package/build/src/create-server.test.d.ts.map +1 -0
- package/build/src/create-server.test.js +13 -0
- package/build/src/create-server.test.js.map +1 -0
- package/build/src/docs/calendar.md +15 -0
- package/build/src/docs/chat.md +15 -0
- package/build/src/docs/config.md +117 -0
- package/build/src/docs/docs.md +36 -0
- package/build/src/docs/drive.md +15 -0
- package/build/src/docs/forms.md +119 -0
- package/build/src/docs/gmail.md +18 -0
- package/build/src/docs/overview.md +52 -0
- package/build/src/docs/people.md +10 -0
- package/build/src/docs/sheets.md +10 -0
- package/build/src/docs/slides.md +26 -0
- package/build/src/docs/tasks.md +13 -0
- package/build/src/docs/time.md +11 -0
- package/build/src/init-server.d.ts +10 -0
- package/build/src/init-server.d.ts.map +1 -0
- package/build/src/init-server.js +13 -0
- package/build/src/init-server.js.map +1 -0
- package/build/src/init-server.test.d.ts +2 -0
- package/build/src/init-server.test.d.ts.map +1 -0
- package/build/src/init-server.test.js +15 -0
- package/build/src/init-server.test.js.map +1 -0
- package/build/src/main.d.ts +33 -0
- package/build/src/main.d.ts.map +1 -0
- package/build/src/main.js +120 -0
- package/build/src/main.js.map +1 -0
- package/build/src/main.test.d.ts +2 -0
- package/build/src/main.test.d.ts.map +1 -0
- package/build/src/main.test.js +204 -0
- package/build/src/main.test.js.map +1 -0
- package/build/src/services/FormsService.d.ts +89 -0
- package/build/src/services/FormsService.d.ts.map +1 -0
- package/build/src/services/FormsService.js +140 -0
- package/build/src/services/FormsService.js.map +1 -0
- package/build/src/services/FormsService.test.d.ts +2 -0
- package/build/src/services/FormsService.test.d.ts.map +1 -0
- package/build/src/services/FormsService.test.js +173 -0
- package/build/src/services/FormsService.test.js.map +1 -0
- package/build/src/tools/config.d.ts +19 -0
- package/build/src/tools/config.d.ts.map +1 -0
- package/build/src/tools/config.js +100 -0
- package/build/src/tools/config.js.map +1 -0
- package/build/src/tools/config.test.d.ts +2 -0
- package/build/src/tools/config.test.d.ts.map +1 -0
- package/build/src/tools/config.test.js +201 -0
- package/build/src/tools/config.test.js.map +1 -0
- package/build/src/tools/domains/calendar.d.ts +3 -0
- package/build/src/tools/domains/calendar.d.ts.map +1 -0
- package/build/src/tools/domains/calendar.js +14 -0
- package/build/src/tools/domains/calendar.js.map +1 -0
- package/build/src/tools/domains/chat.d.ts +3 -0
- package/build/src/tools/domains/chat.d.ts.map +1 -0
- package/build/src/tools/domains/chat.js +16 -0
- package/build/src/tools/domains/chat.js.map +1 -0
- package/build/src/tools/domains/docs.d.ts +8 -0
- package/build/src/tools/domains/docs.d.ts.map +1 -0
- package/build/src/tools/domains/docs.js +11 -0
- package/build/src/tools/domains/docs.js.map +1 -0
- package/build/src/tools/domains/docs.test.d.ts +2 -0
- package/build/src/tools/domains/docs.test.d.ts.map +1 -0
- package/build/src/tools/domains/docs.test.js +111 -0
- package/build/src/tools/domains/docs.test.js.map +1 -0
- package/build/src/tools/domains/drive.d.ts +3 -0
- package/build/src/tools/domains/drive.d.ts.map +1 -0
- package/build/src/tools/domains/drive.js +14 -0
- package/build/src/tools/domains/drive.js.map +1 -0
- package/build/src/tools/domains/factory.d.ts +24 -0
- package/build/src/tools/domains/factory.d.ts.map +1 -0
- package/build/src/tools/domains/factory.js +43 -0
- package/build/src/tools/domains/factory.js.map +1 -0
- package/build/src/tools/domains/factory.test.d.ts +2 -0
- package/build/src/tools/domains/factory.test.d.ts.map +1 -0
- package/build/src/tools/domains/factory.test.js +48 -0
- package/build/src/tools/domains/factory.test.js.map +1 -0
- package/build/src/tools/domains/forms.d.ts +9 -0
- package/build/src/tools/domains/forms.d.ts.map +1 -0
- package/build/src/tools/domains/forms.js +12 -0
- package/build/src/tools/domains/forms.js.map +1 -0
- package/build/src/tools/domains/forms.test.d.ts +2 -0
- package/build/src/tools/domains/forms.test.d.ts.map +1 -0
- package/build/src/tools/domains/forms.test.js +93 -0
- package/build/src/tools/domains/forms.test.js.map +1 -0
- package/build/src/tools/domains/gmail.d.ts +3 -0
- package/build/src/tools/domains/gmail.d.ts.map +1 -0
- package/build/src/tools/domains/gmail.js +17 -0
- package/build/src/tools/domains/gmail.js.map +1 -0
- package/build/src/tools/domains/index.d.ts +20 -0
- package/build/src/tools/domains/index.d.ts.map +1 -0
- package/build/src/tools/domains/index.js +230 -0
- package/build/src/tools/domains/index.js.map +1 -0
- package/build/src/tools/domains/people.d.ts +3 -0
- package/build/src/tools/domains/people.d.ts.map +1 -0
- package/build/src/tools/domains/people.js +5 -0
- package/build/src/tools/domains/people.js.map +1 -0
- package/build/src/tools/domains/sheets.d.ts +3 -0
- package/build/src/tools/domains/sheets.d.ts.map +1 -0
- package/build/src/tools/domains/sheets.js +5 -0
- package/build/src/tools/domains/sheets.js.map +1 -0
- package/build/src/tools/domains/slides.d.ts +3 -0
- package/build/src/tools/domains/slides.d.ts.map +1 -0
- package/build/src/tools/domains/slides.js +25 -0
- package/build/src/tools/domains/slides.js.map +1 -0
- package/build/src/tools/domains/tasks.d.ts +3 -0
- package/build/src/tools/domains/tasks.d.ts.map +1 -0
- package/build/src/tools/domains/tasks.js +14 -0
- package/build/src/tools/domains/tasks.js.map +1 -0
- package/build/src/tools/domains/time.d.ts +3 -0
- package/build/src/tools/domains/time.d.ts.map +1 -0
- package/build/src/tools/domains/time.js +6 -0
- package/build/src/tools/domains/time.js.map +1 -0
- package/build/src/tools/helpers/errors.d.ts +48 -0
- package/build/src/tools/helpers/errors.d.ts.map +1 -0
- package/build/src/tools/helpers/errors.js +290 -0
- package/build/src/tools/helpers/errors.js.map +1 -0
- package/build/src/tools/helpers/errors.test.d.ts +2 -0
- package/build/src/tools/helpers/errors.test.d.ts.map +1 -0
- package/build/src/tools/helpers/errors.test.js +220 -0
- package/build/src/tools/helpers/errors.test.js.map +1 -0
- package/build/src/tools/registry.d.ts +14 -0
- package/build/src/tools/registry.d.ts.map +1 -0
- package/build/src/tools/registry.js +276 -0
- package/build/src/tools/registry.js.map +1 -0
- package/build/src/tools/registry.test.d.ts +2 -0
- package/build/src/tools/registry.test.d.ts.map +1 -0
- package/build/src/tools/registry.test.js +321 -0
- package/build/src/tools/registry.test.js.map +1 -0
- package/build/src/transports/http.d.ts +7 -0
- package/build/src/transports/http.d.ts.map +1 -0
- package/build/src/transports/http.js +56 -0
- package/build/src/transports/http.js.map +1 -0
- package/build/src/transports/http.test.d.ts +2 -0
- package/build/src/transports/http.test.d.ts.map +1 -0
- package/build/src/transports/http.test.js +76 -0
- package/build/src/transports/http.test.js.map +1 -0
- package/build/src/vendored/auth/AuthManager.d.ts +7 -0
- package/build/src/vendored/auth/AuthManager.d.ts.map +1 -0
- package/build/src/vendored/auth/AuthManager.js +11 -0
- package/build/src/vendored/auth/AuthManager.js.map +1 -0
- package/build/src/vendored/services/CalendarService.d.ts +164 -0
- package/build/src/vendored/services/CalendarService.d.ts.map +1 -0
- package/build/src/vendored/services/CalendarService.js +726 -0
- package/build/src/vendored/services/CalendarService.js.map +1 -0
- package/build/src/vendored/services/CalendarValidation.d.ts +9 -0
- package/build/src/vendored/services/CalendarValidation.d.ts.map +1 -0
- package/build/src/vendored/services/CalendarValidation.js +125 -0
- package/build/src/vendored/services/CalendarValidation.js.map +1 -0
- package/build/src/vendored/services/ChatService.d.ts +90 -0
- package/build/src/vendored/services/ChatService.d.ts.map +1 -0
- package/build/src/vendored/services/ChatService.js +451 -0
- package/build/src/vendored/services/ChatService.js.map +1 -0
- package/build/src/vendored/services/DocsService.d.ts +109 -0
- package/build/src/vendored/services/DocsService.d.ts.map +1 -0
- package/build/src/vendored/services/DocsService.js +663 -0
- package/build/src/vendored/services/DocsService.js.map +1 -0
- package/build/src/vendored/services/DocsService.test.d.ts +2 -0
- package/build/src/vendored/services/DocsService.test.d.ts.map +1 -0
- package/build/src/vendored/services/DocsService.test.js +77 -0
- package/build/src/vendored/services/DocsService.test.js.map +1 -0
- package/build/src/vendored/services/DriveService.d.ts +117 -0
- package/build/src/vendored/services/DriveService.d.ts.map +1 -0
- package/build/src/vendored/services/DriveService.js +527 -0
- package/build/src/vendored/services/DriveService.js.map +1 -0
- package/build/src/vendored/services/DriveService.test.d.ts +2 -0
- package/build/src/vendored/services/DriveService.test.d.ts.map +1 -0
- package/build/src/vendored/services/DriveService.test.js +115 -0
- package/build/src/vendored/services/DriveService.test.js.map +1 -0
- package/build/src/vendored/services/GmailService.d.ts +137 -0
- package/build/src/vendored/services/GmailService.d.ts.map +1 -0
- package/build/src/vendored/services/GmailService.js +681 -0
- package/build/src/vendored/services/GmailService.js.map +1 -0
- package/build/src/vendored/services/PeopleService.d.ts +42 -0
- package/build/src/vendored/services/PeopleService.d.ts.map +1 -0
- package/build/src/vendored/services/PeopleService.js +168 -0
- package/build/src/vendored/services/PeopleService.js.map +1 -0
- package/build/src/vendored/services/SheetsService.d.ts +52 -0
- package/build/src/vendored/services/SheetsService.d.ts.map +1 -0
- package/build/src/vendored/services/SheetsService.js +262 -0
- package/build/src/vendored/services/SheetsService.js.map +1 -0
- package/build/src/vendored/services/SheetsService.test.d.ts +2 -0
- package/build/src/vendored/services/SheetsService.test.d.ts.map +1 -0
- package/build/src/vendored/services/SheetsService.test.js +104 -0
- package/build/src/vendored/services/SheetsService.test.js.map +1 -0
- package/build/src/vendored/services/SlidesService.d.ts +167 -0
- package/build/src/vendored/services/SlidesService.d.ts.map +1 -0
- package/build/src/vendored/services/SlidesService.js +887 -0
- package/build/src/vendored/services/SlidesService.js.map +1 -0
- package/build/src/vendored/services/TasksService.d.ts +97 -0
- package/build/src/vendored/services/TasksService.d.ts.map +1 -0
- package/build/src/vendored/services/TasksService.js +234 -0
- package/build/src/vendored/services/TasksService.js.map +1 -0
- package/build/src/vendored/services/TimeService.d.ts +29 -0
- package/build/src/vendored/services/TimeService.d.ts.map +1 -0
- package/build/src/vendored/services/TimeService.js +71 -0
- package/build/src/vendored/services/TimeService.js.map +1 -0
- package/build/src/vendored/utils/DriveQueryBuilder.d.ts +15 -0
- package/build/src/vendored/utils/DriveQueryBuilder.d.ts.map +1 -0
- package/build/src/vendored/utils/DriveQueryBuilder.js +17 -0
- package/build/src/vendored/utils/DriveQueryBuilder.js.map +1 -0
- package/build/src/vendored/utils/GaxiosConfig.d.ts +9 -0
- package/build/src/vendored/utils/GaxiosConfig.d.ts.map +1 -0
- package/build/src/vendored/utils/GaxiosConfig.js +30 -0
- package/build/src/vendored/utils/GaxiosConfig.js.map +1 -0
- package/build/src/vendored/utils/IdUtils.d.ts +13 -0
- package/build/src/vendored/utils/IdUtils.d.ts.map +1 -0
- package/build/src/vendored/utils/IdUtils.js +29 -0
- package/build/src/vendored/utils/IdUtils.js.map +1 -0
- package/build/src/vendored/utils/MimeHelper.d.ts +64 -0
- package/build/src/vendored/utils/MimeHelper.d.ts.map +1 -0
- package/build/src/vendored/utils/MimeHelper.js +174 -0
- package/build/src/vendored/utils/MimeHelper.js.map +1 -0
- package/build/src/vendored/utils/MimeHelper.test.d.ts +2 -0
- package/build/src/vendored/utils/MimeHelper.test.d.ts.map +1 -0
- package/build/src/vendored/utils/MimeHelper.test.js +47 -0
- package/build/src/vendored/utils/MimeHelper.test.js.map +1 -0
- package/build/src/vendored/utils/constants.d.ts +9 -0
- package/build/src/vendored/utils/constants.d.ts.map +1 -0
- package/build/src/vendored/utils/constants.js +9 -0
- package/build/src/vendored/utils/constants.js.map +1 -0
- package/build/src/vendored/utils/logger.d.ts +3 -0
- package/build/src/vendored/utils/logger.d.ts.map +1 -0
- package/build/src/vendored/utils/logger.js +15 -0
- package/build/src/vendored/utils/logger.js.map +1 -0
- package/build/src/vendored/utils/paths.d.ts +2 -0
- package/build/src/vendored/utils/paths.d.ts.map +1 -0
- package/build/src/vendored/utils/paths.js +19 -0
- package/build/src/vendored/utils/paths.js.map +1 -0
- package/build/src/vendored/utils/validation.d.ts +100 -0
- package/build/src/vendored/utils/validation.d.ts.map +1 -0
- package/build/src/vendored/utils/validation.js +161 -0
- package/build/src/vendored/utils/validation.js.map +1 -0
- package/build/src/version.d.ts +2 -0
- package/build/src/version.d.ts.map +1 -0
- package/build/src/version.js +29 -0
- package/build/src/version.js.map +1 -0
- package/build/src/version.test.d.ts +2 -0
- package/build/src/version.test.d.ts.map +1 -0
- package/build/src/version.test.js +19 -0
- package/build/src/version.test.js.map +1 -0
- package/build/tsconfig.tsbuildinfo +1 -0
- package/package.json +69 -4
|
@@ -0,0 +1,681 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { google } from 'googleapis';
|
|
7
|
+
import * as fs from 'node:fs/promises';
|
|
8
|
+
import * as path from 'node:path';
|
|
9
|
+
import { logToFile } from '../utils/logger';
|
|
10
|
+
import { MimeHelper } from '../utils/MimeHelper';
|
|
11
|
+
import { GMAIL_SEARCH_MAX_RESULTS, GMAIL_BATCH_MODIFY_MAX_IDS, GMAIL_NO_LABEL_CHANGES_MESSAGE, } from '../utils/constants';
|
|
12
|
+
import { gaxiosOptions } from '../utils/GaxiosConfig';
|
|
13
|
+
import { emailArraySchema } from '../utils/validation';
|
|
14
|
+
import { ZodError } from 'zod';
|
|
15
|
+
// Extension to MIME type map for common file types
|
|
16
|
+
const EXTENSION_MIME_MAP = {
|
|
17
|
+
'.pdf': 'application/pdf',
|
|
18
|
+
'.doc': 'application/msword',
|
|
19
|
+
'.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
20
|
+
'.xls': 'application/vnd.ms-excel',
|
|
21
|
+
'.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
22
|
+
'.ppt': 'application/vnd.ms-powerpoint',
|
|
23
|
+
'.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
24
|
+
'.txt': 'text/plain',
|
|
25
|
+
'.csv': 'text/csv',
|
|
26
|
+
'.html': 'text/html',
|
|
27
|
+
'.htm': 'text/html',
|
|
28
|
+
'.json': 'application/json',
|
|
29
|
+
'.xml': 'application/xml',
|
|
30
|
+
'.zip': 'application/zip',
|
|
31
|
+
'.png': 'image/png',
|
|
32
|
+
'.jpg': 'image/jpeg',
|
|
33
|
+
'.jpeg': 'image/jpeg',
|
|
34
|
+
'.gif': 'image/gif',
|
|
35
|
+
'.svg': 'image/svg+xml',
|
|
36
|
+
'.mp4': 'video/mp4',
|
|
37
|
+
'.mp3': 'audio/mpeg',
|
|
38
|
+
};
|
|
39
|
+
// Maximum total raw (pre-encoding) size for all attachments, checked against
|
|
40
|
+
// the bytes on disk. Gmail's 25MB limit applies to the entire MIME message,
|
|
41
|
+
// and base64 encoding inflates binary data by ~33%: 18MB of raw bytes becomes
|
|
42
|
+
// ~24MB after encoding, leaving ~1MB of headroom under the 25MB cap for
|
|
43
|
+
// message headers and body.
|
|
44
|
+
const MAX_TOTAL_ATTACHMENT_SIZE_BYTES = 18 * 1024 * 1024;
|
|
45
|
+
function assertWithinAttachmentSizeLimit(totalSize) {
|
|
46
|
+
if (totalSize > MAX_TOTAL_ATTACHMENT_SIZE_BYTES) {
|
|
47
|
+
throw new Error(`Total attachment size (${(totalSize / 1024 / 1024).toFixed(2)}MB) exceeds the maximum allowed limit of ${MAX_TOTAL_ATTACHMENT_SIZE_BYTES / 1024 / 1024}MB.`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function getMimeTypeFromExtension(filePath) {
|
|
51
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
52
|
+
return EXTENSION_MIME_MAP[ext] ?? 'application/octet-stream';
|
|
53
|
+
}
|
|
54
|
+
export class GmailService {
|
|
55
|
+
constructor(authManager) {
|
|
56
|
+
this.authManager = authManager;
|
|
57
|
+
this.search = async ({ query, maxResults = GMAIL_SEARCH_MAX_RESULTS, pageToken, labelIds, includeSpamTrash = false, }) => {
|
|
58
|
+
try {
|
|
59
|
+
logToFile(`Gmail search - query: ${query}, maxResults: ${maxResults}`);
|
|
60
|
+
const gmail = await this.getGmailClient();
|
|
61
|
+
const response = await gmail.users.messages.list({
|
|
62
|
+
userId: 'me',
|
|
63
|
+
q: query,
|
|
64
|
+
maxResults,
|
|
65
|
+
pageToken,
|
|
66
|
+
labelIds,
|
|
67
|
+
includeSpamTrash,
|
|
68
|
+
});
|
|
69
|
+
const messages = response.data.messages || [];
|
|
70
|
+
const nextPageToken = response.data.nextPageToken;
|
|
71
|
+
const resultSizeEstimate = response.data.resultSizeEstimate;
|
|
72
|
+
logToFile(`Found ${messages.length} messages, estimated total: ${resultSizeEstimate}`);
|
|
73
|
+
return {
|
|
74
|
+
content: [
|
|
75
|
+
{
|
|
76
|
+
type: 'text',
|
|
77
|
+
text: JSON.stringify({
|
|
78
|
+
messages: messages.map((msg) => ({
|
|
79
|
+
id: msg.id,
|
|
80
|
+
threadId: msg.threadId,
|
|
81
|
+
})),
|
|
82
|
+
nextPageToken,
|
|
83
|
+
resultSizeEstimate,
|
|
84
|
+
}, null, 2),
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
return this.handleError(error, 'gmail.search');
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
this.get = async ({ messageId, format = 'full', }) => {
|
|
94
|
+
try {
|
|
95
|
+
logToFile(`Getting message ${messageId} with format: ${format}`);
|
|
96
|
+
const gmail = await this.getGmailClient();
|
|
97
|
+
const response = await gmail.users.messages.get({
|
|
98
|
+
userId: 'me',
|
|
99
|
+
id: messageId,
|
|
100
|
+
format,
|
|
101
|
+
});
|
|
102
|
+
const message = response.data;
|
|
103
|
+
// Extract useful information based on format
|
|
104
|
+
if (format === 'metadata' || format === 'full') {
|
|
105
|
+
const headers = message.payload?.headers || [];
|
|
106
|
+
const getHeader = (name) => headers.find((h) => h.name === name)?.value;
|
|
107
|
+
const subject = getHeader('Subject');
|
|
108
|
+
const from = getHeader('From');
|
|
109
|
+
const to = getHeader('To');
|
|
110
|
+
const date = getHeader('Date');
|
|
111
|
+
// Extract body and attachments for full format
|
|
112
|
+
let body = '';
|
|
113
|
+
let attachments = [];
|
|
114
|
+
if (format === 'full' && message.payload) {
|
|
115
|
+
const result = this.extractAttachmentsAndBody(message.payload);
|
|
116
|
+
body = result.body;
|
|
117
|
+
attachments = result.attachments;
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
content: [
|
|
121
|
+
{
|
|
122
|
+
type: 'text',
|
|
123
|
+
text: JSON.stringify({
|
|
124
|
+
id: message.id,
|
|
125
|
+
threadId: message.threadId,
|
|
126
|
+
labelIds: message.labelIds,
|
|
127
|
+
snippet: message.snippet,
|
|
128
|
+
subject,
|
|
129
|
+
from,
|
|
130
|
+
to,
|
|
131
|
+
date,
|
|
132
|
+
body: body || message.snippet,
|
|
133
|
+
attachments: attachments,
|
|
134
|
+
}, null, 2),
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
content: [
|
|
141
|
+
{
|
|
142
|
+
type: 'text',
|
|
143
|
+
text: JSON.stringify(message, null, 2),
|
|
144
|
+
},
|
|
145
|
+
],
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
return this.handleError(error, 'gmail.get');
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
this.downloadAttachment = async ({ messageId, attachmentId, localPath, }) => {
|
|
153
|
+
try {
|
|
154
|
+
logToFile(`Downloading attachment ${attachmentId} from message ${messageId} to ${localPath}`);
|
|
155
|
+
if (!path.isAbsolute(localPath)) {
|
|
156
|
+
throw new Error('localPath must be an absolute path.');
|
|
157
|
+
}
|
|
158
|
+
const gmail = await this.getGmailClient();
|
|
159
|
+
const response = await gmail.users.messages.attachments.get({
|
|
160
|
+
userId: 'me',
|
|
161
|
+
messageId: messageId,
|
|
162
|
+
id: attachmentId,
|
|
163
|
+
});
|
|
164
|
+
const data = response.data.data;
|
|
165
|
+
if (!data) {
|
|
166
|
+
throw new Error('Attachment data is empty');
|
|
167
|
+
}
|
|
168
|
+
// Ensure directory exists
|
|
169
|
+
await fs.mkdir(path.dirname(localPath), { recursive: true });
|
|
170
|
+
// Write file
|
|
171
|
+
const buffer = Buffer.from(data, 'base64url');
|
|
172
|
+
await fs.writeFile(localPath, buffer);
|
|
173
|
+
logToFile(`Attachment downloaded successfully to ${localPath}`);
|
|
174
|
+
return {
|
|
175
|
+
content: [
|
|
176
|
+
{
|
|
177
|
+
type: 'text',
|
|
178
|
+
text: JSON.stringify({
|
|
179
|
+
message: `Attachment downloaded successfully to ${localPath}`,
|
|
180
|
+
path: localPath,
|
|
181
|
+
}),
|
|
182
|
+
},
|
|
183
|
+
],
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
return this.handleError(error, 'gmail.downloadAttachment');
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
this.modify = async ({ messageId, addLabelIds = [], removeLabelIds = [], }) => {
|
|
191
|
+
try {
|
|
192
|
+
logToFile(`Modifying message ${messageId} with addLabelIds: ${addLabelIds}, removeLabelIds: ${removeLabelIds}`);
|
|
193
|
+
const gmail = await this.getGmailClient();
|
|
194
|
+
const response = await gmail.users.messages.modify({
|
|
195
|
+
userId: 'me',
|
|
196
|
+
id: messageId,
|
|
197
|
+
requestBody: {
|
|
198
|
+
addLabelIds,
|
|
199
|
+
removeLabelIds,
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
const message = response.data;
|
|
203
|
+
return {
|
|
204
|
+
content: [
|
|
205
|
+
{
|
|
206
|
+
type: 'text',
|
|
207
|
+
text: JSON.stringify(message, null, 2),
|
|
208
|
+
},
|
|
209
|
+
],
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
return this.handleError(error, 'gmail.modify');
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
this.batchModify = async ({ messageIds, addLabelIds = [], removeLabelIds = [], }) => {
|
|
217
|
+
try {
|
|
218
|
+
if (addLabelIds.length === 0 && removeLabelIds.length === 0) {
|
|
219
|
+
return {
|
|
220
|
+
content: [
|
|
221
|
+
{
|
|
222
|
+
type: 'text',
|
|
223
|
+
text: JSON.stringify({
|
|
224
|
+
status: 'noop',
|
|
225
|
+
message: GMAIL_NO_LABEL_CHANGES_MESSAGE,
|
|
226
|
+
}),
|
|
227
|
+
},
|
|
228
|
+
],
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
if (messageIds.length > GMAIL_BATCH_MODIFY_MAX_IDS) {
|
|
232
|
+
throw new Error(`Too many message IDs. Maximum is ${GMAIL_BATCH_MODIFY_MAX_IDS}, got ${messageIds.length}.`);
|
|
233
|
+
}
|
|
234
|
+
logToFile(`Batch modifying ${messageIds.length} messages with addLabelIds: ${addLabelIds}, removeLabelIds: ${removeLabelIds}`);
|
|
235
|
+
const gmail = await this.getGmailClient();
|
|
236
|
+
await gmail.users.messages.batchModify({
|
|
237
|
+
userId: 'me',
|
|
238
|
+
requestBody: {
|
|
239
|
+
ids: messageIds,
|
|
240
|
+
addLabelIds,
|
|
241
|
+
removeLabelIds,
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
return {
|
|
245
|
+
content: [
|
|
246
|
+
{
|
|
247
|
+
type: 'text',
|
|
248
|
+
text: JSON.stringify({
|
|
249
|
+
modifiedCount: messageIds.length,
|
|
250
|
+
addLabelIds,
|
|
251
|
+
removeLabelIds,
|
|
252
|
+
status: 'success',
|
|
253
|
+
}, null, 2),
|
|
254
|
+
},
|
|
255
|
+
],
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
catch (error) {
|
|
259
|
+
return this.handleError(error, 'gmail.batchModify');
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
this.modifyThread = async ({ threadId, addLabelIds = [], removeLabelIds = [], }) => {
|
|
263
|
+
try {
|
|
264
|
+
if (addLabelIds.length === 0 && removeLabelIds.length === 0) {
|
|
265
|
+
return {
|
|
266
|
+
content: [
|
|
267
|
+
{
|
|
268
|
+
type: 'text',
|
|
269
|
+
text: JSON.stringify({
|
|
270
|
+
status: 'noop',
|
|
271
|
+
message: GMAIL_NO_LABEL_CHANGES_MESSAGE,
|
|
272
|
+
}),
|
|
273
|
+
},
|
|
274
|
+
],
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
logToFile(`Modifying thread ${threadId} with addLabelIds: ${addLabelIds}, removeLabelIds: ${removeLabelIds}`);
|
|
278
|
+
const gmail = await this.getGmailClient();
|
|
279
|
+
const response = await gmail.users.threads.modify({
|
|
280
|
+
userId: 'me',
|
|
281
|
+
id: threadId,
|
|
282
|
+
requestBody: {
|
|
283
|
+
addLabelIds,
|
|
284
|
+
removeLabelIds,
|
|
285
|
+
},
|
|
286
|
+
});
|
|
287
|
+
const thread = response.data;
|
|
288
|
+
return {
|
|
289
|
+
content: [
|
|
290
|
+
{
|
|
291
|
+
type: 'text',
|
|
292
|
+
text: JSON.stringify(thread, null, 2),
|
|
293
|
+
},
|
|
294
|
+
],
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
catch (error) {
|
|
298
|
+
return this.handleError(error, 'gmail.modifyThread');
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
this.send = async ({ to, subject, body, cc, bcc, replyTo, isHtml = false, }) => {
|
|
302
|
+
try {
|
|
303
|
+
// Validate email addresses
|
|
304
|
+
const validationError = this.validateEmailAddresses({
|
|
305
|
+
to,
|
|
306
|
+
cc,
|
|
307
|
+
bcc,
|
|
308
|
+
replyTo,
|
|
309
|
+
});
|
|
310
|
+
if (validationError) {
|
|
311
|
+
return validationError;
|
|
312
|
+
}
|
|
313
|
+
logToFile(`Sending email to: ${to}, subject: ${subject}`);
|
|
314
|
+
// Create MIME message
|
|
315
|
+
const mimeMessage = MimeHelper.createMimeMessage({
|
|
316
|
+
to: Array.isArray(to) ? to.join(', ') : to,
|
|
317
|
+
subject,
|
|
318
|
+
body,
|
|
319
|
+
cc: cc ? (Array.isArray(cc) ? cc.join(', ') : cc) : undefined,
|
|
320
|
+
bcc: bcc ? (Array.isArray(bcc) ? bcc.join(', ') : bcc) : undefined,
|
|
321
|
+
replyTo,
|
|
322
|
+
isHtml,
|
|
323
|
+
});
|
|
324
|
+
const gmail = await this.getGmailClient();
|
|
325
|
+
const response = await gmail.users.messages.send({
|
|
326
|
+
userId: 'me',
|
|
327
|
+
requestBody: {
|
|
328
|
+
raw: mimeMessage,
|
|
329
|
+
},
|
|
330
|
+
});
|
|
331
|
+
logToFile(`Email sent successfully: ${response.data.id}`);
|
|
332
|
+
return {
|
|
333
|
+
content: [
|
|
334
|
+
{
|
|
335
|
+
type: 'text',
|
|
336
|
+
text: JSON.stringify({
|
|
337
|
+
id: response.data.id,
|
|
338
|
+
threadId: response.data.threadId,
|
|
339
|
+
labelIds: response.data.labelIds,
|
|
340
|
+
status: 'sent',
|
|
341
|
+
}, null, 2),
|
|
342
|
+
},
|
|
343
|
+
],
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
catch (error) {
|
|
347
|
+
return this.handleError(error, 'gmail.send');
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
this.createDraft = async ({ to, subject, body, cc, bcc, replyTo, isHtml = false, threadId, attachments, }) => {
|
|
351
|
+
try {
|
|
352
|
+
// Validate email addresses
|
|
353
|
+
const validationError = this.validateEmailAddresses({
|
|
354
|
+
to,
|
|
355
|
+
cc,
|
|
356
|
+
bcc,
|
|
357
|
+
replyTo,
|
|
358
|
+
});
|
|
359
|
+
if (validationError) {
|
|
360
|
+
return validationError;
|
|
361
|
+
}
|
|
362
|
+
logToFile(`Creating draft - to: ${to}, subject: ${subject}`);
|
|
363
|
+
const gmail = await this.getGmailClient();
|
|
364
|
+
// If threadId is provided, fetch the last message to get reply headers
|
|
365
|
+
let inReplyTo;
|
|
366
|
+
let references;
|
|
367
|
+
if (threadId) {
|
|
368
|
+
try {
|
|
369
|
+
const threadResponse = await gmail.users.threads.get({
|
|
370
|
+
userId: 'me',
|
|
371
|
+
id: threadId,
|
|
372
|
+
format: 'metadata',
|
|
373
|
+
metadataHeaders: ['Message-ID', 'References'],
|
|
374
|
+
});
|
|
375
|
+
const messages = threadResponse.data.messages || [];
|
|
376
|
+
if (messages.length > 0) {
|
|
377
|
+
const lastMessage = messages[messages.length - 1];
|
|
378
|
+
const headers = lastMessage.payload?.headers || [];
|
|
379
|
+
const messageIdHeader = headers.find((h) => h.name?.toLowerCase() === 'message-id');
|
|
380
|
+
const referencesHeader = headers.find((h) => h.name?.toLowerCase() === 'references');
|
|
381
|
+
if (messageIdHeader?.value) {
|
|
382
|
+
inReplyTo = messageIdHeader.value;
|
|
383
|
+
const previousReferences = referencesHeader?.value || '';
|
|
384
|
+
references = previousReferences
|
|
385
|
+
? `${previousReferences} ${messageIdHeader.value}`
|
|
386
|
+
: messageIdHeader.value;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
catch (threadError) {
|
|
391
|
+
logToFile(`Warning: Could not fetch thread ${threadId} for reply headers: ${threadError}`);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
// Create MIME message
|
|
395
|
+
let mimeMessage;
|
|
396
|
+
if (attachments && attachments.length > 0) {
|
|
397
|
+
// Validate all paths are absolute and check file sizes before reading anything
|
|
398
|
+
const attachmentSizes = await Promise.all(attachments.map(async (att) => {
|
|
399
|
+
if (!path.isAbsolute(att.filePath)) {
|
|
400
|
+
throw new Error(`Attachment filePath must be an absolute path: ${att.filePath}`);
|
|
401
|
+
}
|
|
402
|
+
let stats;
|
|
403
|
+
try {
|
|
404
|
+
stats = await fs.stat(att.filePath);
|
|
405
|
+
}
|
|
406
|
+
catch (statError) {
|
|
407
|
+
throw new Error(`Could not access attachment file ${att.filePath}: ${statError instanceof Error ? statError.message : String(statError)}`);
|
|
408
|
+
}
|
|
409
|
+
if (!stats.isFile()) {
|
|
410
|
+
throw new Error(`Attachment path is not a file: ${att.filePath}`);
|
|
411
|
+
}
|
|
412
|
+
return stats.size;
|
|
413
|
+
}));
|
|
414
|
+
const totalSize = attachmentSizes.reduce((sum, size) => sum + size, 0);
|
|
415
|
+
assertWithinAttachmentSizeLimit(totalSize);
|
|
416
|
+
// Read each file from disk
|
|
417
|
+
const resolvedAttachments = await Promise.all(attachments.map(async (att) => {
|
|
418
|
+
let content;
|
|
419
|
+
try {
|
|
420
|
+
content = await fs.readFile(att.filePath);
|
|
421
|
+
}
|
|
422
|
+
catch (readError) {
|
|
423
|
+
throw new Error(`Could not read attachment file ${att.filePath}: ${readError instanceof Error ? readError.message : String(readError)}`);
|
|
424
|
+
}
|
|
425
|
+
return {
|
|
426
|
+
// `||` (not `??`) so empty strings also fall back to defaults —
|
|
427
|
+
// an empty filename or MIME type is invalid in MIME headers.
|
|
428
|
+
filename: att.filename || path.basename(att.filePath),
|
|
429
|
+
content,
|
|
430
|
+
contentType: att.mimeType || getMimeTypeFromExtension(att.filePath),
|
|
431
|
+
};
|
|
432
|
+
}));
|
|
433
|
+
// The stat-based check above is a pre-flight guard so oversized files
|
|
434
|
+
// are rejected before being read into memory, but a file can change
|
|
435
|
+
// between stat and read (TOCTOU). Re-check against the bytes actually
|
|
436
|
+
// read so the size cap is authoritative.
|
|
437
|
+
assertWithinAttachmentSizeLimit(resolvedAttachments.reduce((sum, att) => sum + att.content.length, 0));
|
|
438
|
+
mimeMessage = MimeHelper.createMimeMessageWithAttachments({
|
|
439
|
+
to: Array.isArray(to) ? to.join(', ') : to,
|
|
440
|
+
subject,
|
|
441
|
+
body,
|
|
442
|
+
cc: cc ? (Array.isArray(cc) ? cc.join(', ') : cc) : undefined,
|
|
443
|
+
bcc: bcc ? (Array.isArray(bcc) ? bcc.join(', ') : bcc) : undefined,
|
|
444
|
+
replyTo,
|
|
445
|
+
inReplyTo,
|
|
446
|
+
references,
|
|
447
|
+
isHtml,
|
|
448
|
+
attachments: resolvedAttachments,
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
mimeMessage = MimeHelper.createMimeMessage({
|
|
453
|
+
to: Array.isArray(to) ? to.join(', ') : to,
|
|
454
|
+
subject,
|
|
455
|
+
body,
|
|
456
|
+
cc: cc ? (Array.isArray(cc) ? cc.join(', ') : cc) : undefined,
|
|
457
|
+
bcc: bcc ? (Array.isArray(bcc) ? bcc.join(', ') : bcc) : undefined,
|
|
458
|
+
replyTo,
|
|
459
|
+
isHtml,
|
|
460
|
+
inReplyTo,
|
|
461
|
+
references,
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
const response = await gmail.users.drafts.create({
|
|
465
|
+
userId: 'me',
|
|
466
|
+
requestBody: {
|
|
467
|
+
message: {
|
|
468
|
+
raw: mimeMessage,
|
|
469
|
+
...(threadId && { threadId }),
|
|
470
|
+
},
|
|
471
|
+
},
|
|
472
|
+
});
|
|
473
|
+
logToFile(`Draft created successfully: ${response.data.id}`);
|
|
474
|
+
return {
|
|
475
|
+
content: [
|
|
476
|
+
{
|
|
477
|
+
type: 'text',
|
|
478
|
+
text: JSON.stringify({
|
|
479
|
+
id: response.data.id,
|
|
480
|
+
message: {
|
|
481
|
+
id: response.data.message?.id,
|
|
482
|
+
threadId: response.data.message?.threadId,
|
|
483
|
+
labelIds: response.data.message?.labelIds,
|
|
484
|
+
},
|
|
485
|
+
status: 'draft_created',
|
|
486
|
+
}, null, 2),
|
|
487
|
+
},
|
|
488
|
+
],
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
catch (error) {
|
|
492
|
+
return this.handleError(error, 'gmail.createDraft');
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
this.sendDraft = async ({ draftId }) => {
|
|
496
|
+
try {
|
|
497
|
+
logToFile(`Sending draft: ${draftId}`);
|
|
498
|
+
const gmail = await this.getGmailClient();
|
|
499
|
+
const response = await gmail.users.drafts.send({
|
|
500
|
+
userId: 'me',
|
|
501
|
+
requestBody: {
|
|
502
|
+
id: draftId,
|
|
503
|
+
},
|
|
504
|
+
});
|
|
505
|
+
logToFile(`Draft sent successfully: ${response.data.id}`);
|
|
506
|
+
return {
|
|
507
|
+
content: [
|
|
508
|
+
{
|
|
509
|
+
type: 'text',
|
|
510
|
+
text: JSON.stringify({
|
|
511
|
+
id: response.data.id,
|
|
512
|
+
threadId: response.data.threadId,
|
|
513
|
+
labelIds: response.data.labelIds,
|
|
514
|
+
status: 'sent',
|
|
515
|
+
}, null, 2),
|
|
516
|
+
},
|
|
517
|
+
],
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
catch (error) {
|
|
521
|
+
return this.handleError(error, 'gmail.sendDraft');
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
this.listLabels = async () => {
|
|
525
|
+
try {
|
|
526
|
+
logToFile(`Listing Gmail labels`);
|
|
527
|
+
const gmail = await this.getGmailClient();
|
|
528
|
+
const response = await gmail.users.labels.list({
|
|
529
|
+
userId: 'me',
|
|
530
|
+
});
|
|
531
|
+
const labels = response.data.labels || [];
|
|
532
|
+
logToFile(`Found ${labels.length} labels`);
|
|
533
|
+
return {
|
|
534
|
+
content: [
|
|
535
|
+
{
|
|
536
|
+
type: 'text',
|
|
537
|
+
text: JSON.stringify({
|
|
538
|
+
labels: labels.map((label) => ({
|
|
539
|
+
id: label.id,
|
|
540
|
+
name: label.name,
|
|
541
|
+
type: label.type,
|
|
542
|
+
messageListVisibility: label.messageListVisibility,
|
|
543
|
+
labelListVisibility: label.labelListVisibility,
|
|
544
|
+
})),
|
|
545
|
+
}, null, 2),
|
|
546
|
+
},
|
|
547
|
+
],
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
catch (error) {
|
|
551
|
+
return this.handleError(error, 'gmail.listLabels');
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
this.createLabel = async ({ name, labelListVisibility = 'labelShow', messageListVisibility = 'show', }) => {
|
|
555
|
+
try {
|
|
556
|
+
logToFile(`Creating Gmail label: ${name}`);
|
|
557
|
+
const gmail = await this.getGmailClient();
|
|
558
|
+
const response = await gmail.users.labels.create({
|
|
559
|
+
userId: 'me',
|
|
560
|
+
requestBody: {
|
|
561
|
+
name,
|
|
562
|
+
labelListVisibility,
|
|
563
|
+
messageListVisibility,
|
|
564
|
+
},
|
|
565
|
+
});
|
|
566
|
+
const label = response.data;
|
|
567
|
+
logToFile(`Created label: ${label.name} with id: ${label.id}`);
|
|
568
|
+
return {
|
|
569
|
+
content: [
|
|
570
|
+
{
|
|
571
|
+
type: 'text',
|
|
572
|
+
text: JSON.stringify({
|
|
573
|
+
id: label.id,
|
|
574
|
+
name: label.name,
|
|
575
|
+
type: label.type,
|
|
576
|
+
messageListVisibility: label.messageListVisibility,
|
|
577
|
+
labelListVisibility: label.labelListVisibility,
|
|
578
|
+
status: 'created',
|
|
579
|
+
}, null, 2),
|
|
580
|
+
},
|
|
581
|
+
],
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
catch (error) {
|
|
585
|
+
return this.handleError(error, 'gmail.createLabel');
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
async getGmailClient() {
|
|
590
|
+
const auth = await this.authManager.getAuthenticatedClient();
|
|
591
|
+
const options = { ...gaxiosOptions, auth };
|
|
592
|
+
return google.gmail({ version: 'v1', ...options });
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Helper method to handle errors consistently across all methods
|
|
596
|
+
*/
|
|
597
|
+
handleError(error, context) {
|
|
598
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
599
|
+
logToFile(`Error during ${context}: ${errorMessage}`);
|
|
600
|
+
return {
|
|
601
|
+
content: [
|
|
602
|
+
{
|
|
603
|
+
type: 'text',
|
|
604
|
+
text: JSON.stringify({ error: errorMessage }),
|
|
605
|
+
},
|
|
606
|
+
],
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Validates recipient email addresses shared by send and createDraft.
|
|
611
|
+
* Returns a structured error response when validation fails, or null when
|
|
612
|
+
* all provided addresses are valid.
|
|
613
|
+
*/
|
|
614
|
+
validateEmailAddresses({ to, cc, bcc, replyTo, }) {
|
|
615
|
+
try {
|
|
616
|
+
emailArraySchema.parse(to);
|
|
617
|
+
if (cc)
|
|
618
|
+
emailArraySchema.parse(cc);
|
|
619
|
+
if (bcc)
|
|
620
|
+
emailArraySchema.parse(bcc);
|
|
621
|
+
if (replyTo)
|
|
622
|
+
emailArraySchema.parse(replyTo);
|
|
623
|
+
return null;
|
|
624
|
+
}
|
|
625
|
+
catch (error) {
|
|
626
|
+
// Only Zod validation failures mean the addresses were malformed;
|
|
627
|
+
// anything else is an internal fault and must not be mislabeled as an
|
|
628
|
+
// invalid-email error, so rethrow it for the caller's generic handler.
|
|
629
|
+
if (!(error instanceof ZodError)) {
|
|
630
|
+
throw error;
|
|
631
|
+
}
|
|
632
|
+
logToFile(`Rejected invalid email address input: ${error.message}`);
|
|
633
|
+
return {
|
|
634
|
+
content: [
|
|
635
|
+
{
|
|
636
|
+
type: 'text',
|
|
637
|
+
text: JSON.stringify({
|
|
638
|
+
error: 'Invalid email address format',
|
|
639
|
+
details: error.message,
|
|
640
|
+
}),
|
|
641
|
+
},
|
|
642
|
+
],
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
extractAttachmentsAndBody(payload, result = {
|
|
647
|
+
body: '',
|
|
648
|
+
attachments: [],
|
|
649
|
+
}) {
|
|
650
|
+
if (!payload)
|
|
651
|
+
return result;
|
|
652
|
+
// Handle body parts
|
|
653
|
+
if (payload.body?.data) {
|
|
654
|
+
// If it's the main body (and not an attachment)
|
|
655
|
+
if (!payload.filename || !payload.body.attachmentId) {
|
|
656
|
+
if (payload.mimeType?.startsWith('text/')) {
|
|
657
|
+
// Prioritize plain text over HTML for direct body extraction
|
|
658
|
+
if (!result.body || payload.mimeType === 'text/plain') {
|
|
659
|
+
result.body = Buffer.from(payload.body.data, 'base64').toString('utf-8');
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
// Handle attachments and recursive parts
|
|
665
|
+
if (payload.filename && payload.body?.attachmentId) {
|
|
666
|
+
result.attachments.push({
|
|
667
|
+
filename: payload.filename,
|
|
668
|
+
mimeType: payload.mimeType,
|
|
669
|
+
attachmentId: payload.body.attachmentId,
|
|
670
|
+
size: payload.body.size, // Size in bytes
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
if (payload.parts) {
|
|
674
|
+
for (const part of payload.parts) {
|
|
675
|
+
this.extractAttachmentsAndBody(part, result);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
return result;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
//# sourceMappingURL=GmailService.js.map
|