botinabox 2.4.2 → 2.5.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/LICENSE +21 -21
- package/README.md +190 -190
- package/bin/botinabox.mjs +2 -2
- package/dist/channels/slack/index.d.ts +1 -1
- package/dist/{chat-pipeline-BWrtVqEP.d.ts → chat-pipeline-DuNX5WoL.d.ts} +3 -0
- package/dist/cli.js +0 -0
- package/dist/index.d.ts +11 -2
- package/dist/index.js +64 -27
- package/package.json +100 -99
- package/dist/channels/discord/adapter.d.ts +0 -32
- package/dist/channels/discord/adapter.js +0 -70
- package/dist/channels/discord/inbound.d.ts +0 -25
- package/dist/channels/discord/inbound.js +0 -24
- package/dist/channels/discord/models.d.ts +0 -8
- package/dist/channels/discord/models.js +0 -5
- package/dist/channels/discord/outbound.d.ts +0 -14
- package/dist/channels/discord/outbound.js +0 -38
- package/dist/channels/slack/adapter.d.ts +0 -33
- package/dist/channels/slack/adapter.js +0 -74
- package/dist/channels/slack/inbound.d.ts +0 -59
- package/dist/channels/slack/inbound.js +0 -96
- package/dist/channels/slack/models.d.ts +0 -9
- package/dist/channels/slack/models.js +0 -5
- package/dist/channels/slack/outbound.d.ts +0 -12
- package/dist/channels/slack/outbound.js +0 -18
- package/dist/channels/slack/transcribe.d.ts +0 -41
- package/dist/channels/slack/transcribe.js +0 -106
- package/dist/channels/webhook/adapter.d.ts +0 -23
- package/dist/channels/webhook/adapter.js +0 -86
- package/dist/channels/webhook/hmac.d.ts +0 -13
- package/dist/channels/webhook/hmac.js +0 -26
- package/dist/channels/webhook/models.d.ts +0 -9
- package/dist/channels/webhook/models.js +0 -5
- package/dist/channels/webhook/server.d.ts +0 -20
- package/dist/channels/webhook/server.js +0 -91
- package/dist/chat-pipeline-C-XlLGNl.d.ts +0 -648
- package/dist/chat-pipeline-CR1KF6eX.d.ts +0 -652
- package/dist/chat-pipeline-DisuC8SB.d.ts +0 -643
- package/dist/chunk-2LGXQPEA.js +0 -41
- package/dist/chunk-3X3YKI4T.js +0 -357
- package/dist/chunk-D47AIFOD.js +0 -351
- package/dist/chunk-DSNJKNEW.js +0 -328
- package/dist/chunk-GS2JFL6I.js +0 -144
- package/dist/chunk-J6S6QMUY.js +0 -144
- package/dist/chunk-QLA6YOFN.js +0 -22
- package/dist/chunk-UACT2WXX.js +0 -381
- package/dist/cli/templates/config.yml.d.ts +0 -7
- package/dist/cli/templates/config.yml.js +0 -61
- package/dist/cli/templates/env.d.ts +0 -1
- package/dist/cli/templates/env.js +0 -30
- package/dist/cli/templates/index.ts.d.ts +0 -2
- package/dist/cli/templates/index.ts.js +0 -30
- package/dist/cli/templates/package.json.d.ts +0 -5
- package/dist/cli/templates/package.json.js +0 -28
- package/dist/connector-DDahQw-2.d.ts +0 -63
- package/dist/connectors/google/calendar-connector.d.ts +0 -40
- package/dist/connectors/google/calendar-connector.js +0 -243
- package/dist/connectors/google/gmail-connector.d.ts +0 -42
- package/dist/connectors/google/gmail-connector.js +0 -345
- package/dist/connectors/google/oauth.d.ts +0 -48
- package/dist/connectors/google/oauth.js +0 -112
- package/dist/connectors/google/types.d.ts +0 -78
- package/dist/connectors/google/types.js +0 -2
- package/dist/core/chat/auto-discovery.d.ts +0 -16
- package/dist/core/chat/auto-discovery.js +0 -54
- package/dist/core/chat/channel-registry.d.ts +0 -45
- package/dist/core/chat/channel-registry.js +0 -96
- package/dist/core/chat/chat-pipeline.d.ts +0 -113
- package/dist/core/chat/chat-pipeline.js +0 -395
- package/dist/core/chat/chat-responder.d.ts +0 -90
- package/dist/core/chat/chat-responder.js +0 -185
- package/dist/core/chat/formatter.d.ts +0 -11
- package/dist/core/chat/formatter.js +0 -60
- package/dist/core/chat/index.d.ts +0 -24
- package/dist/core/chat/index.js +0 -18
- package/dist/core/chat/message-interpreter.d.ts +0 -91
- package/dist/core/chat/message-interpreter.js +0 -166
- package/dist/core/chat/message-store.d.ts +0 -66
- package/dist/core/chat/message-store.js +0 -131
- package/dist/core/chat/notification-queue.d.ts +0 -34
- package/dist/core/chat/notification-queue.js +0 -111
- package/dist/core/chat/pipeline.d.ts +0 -38
- package/dist/core/chat/pipeline.js +0 -89
- package/dist/core/chat/policies.d.ts +0 -16
- package/dist/core/chat/policies.js +0 -25
- package/dist/core/chat/routing.d.ts +0 -17
- package/dist/core/chat/routing.js +0 -36
- package/dist/core/chat/session-key.d.ts +0 -30
- package/dist/core/chat/session-key.js +0 -65
- package/dist/core/chat/session-manager.d.ts +0 -17
- package/dist/core/chat/session-manager.js +0 -23
- package/dist/core/chat/text-chunker.d.ts +0 -9
- package/dist/core/chat/text-chunker.js +0 -48
- package/dist/core/chat/triage-router.d.ts +0 -75
- package/dist/core/chat/triage-router.js +0 -142
- package/dist/core/chat/types.d.ts +0 -5
- package/dist/core/chat/types.js +0 -5
- package/dist/core/config/defaults.d.ts +0 -2
- package/dist/core/config/defaults.js +0 -38
- package/dist/core/config/index.d.ts +0 -6
- package/dist/core/config/index.js +0 -4
- package/dist/core/config/interpolate.d.ts +0 -5
- package/dist/core/config/interpolate.js +0 -27
- package/dist/core/config/loader.d.ts +0 -24
- package/dist/core/config/loader.js +0 -59
- package/dist/core/config/schema.d.ts +0 -5
- package/dist/core/config/schema.js +0 -119
- package/dist/core/data/core-entity-contexts.d.ts +0 -14
- package/dist/core/data/core-entity-contexts.js +0 -197
- package/dist/core/data/core-migrations.d.ts +0 -5
- package/dist/core/data/core-migrations.js +0 -45
- package/dist/core/data/core-schema.d.ts +0 -6
- package/dist/core/data/core-schema.js +0 -454
- package/dist/core/data/data-store.d.ts +0 -67
- package/dist/core/data/data-store.js +0 -218
- package/dist/core/data/domain-entity-contexts.d.ts +0 -29
- package/dist/core/data/domain-entity-contexts.js +0 -321
- package/dist/core/data/domain-schema.d.ts +0 -36
- package/dist/core/data/domain-schema.js +0 -323
- package/dist/core/data/index.d.ts +0 -7
- package/dist/core/data/index.js +0 -7
- package/dist/core/data/types.d.ts +0 -111
- package/dist/core/data/types.js +0 -1
- package/dist/core/hooks/hook-bus.d.ts +0 -18
- package/dist/core/hooks/hook-bus.js +0 -120
- package/dist/core/hooks/index.d.ts +0 -2
- package/dist/core/hooks/index.js +0 -1
- package/dist/core/hooks/types.d.ts +0 -19
- package/dist/core/hooks/types.js +0 -1
- package/dist/core/index.d.ts +0 -4
- package/dist/core/index.js +0 -4
- package/dist/core/llm/auto-discovery.d.ts +0 -11
- package/dist/core/llm/auto-discovery.js +0 -49
- package/dist/core/llm/cost-tracker.d.ts +0 -6
- package/dist/core/llm/cost-tracker.js +0 -38
- package/dist/core/llm/index.d.ts +0 -4
- package/dist/core/llm/index.js +0 -3
- package/dist/core/llm/model-router.d.ts +0 -25
- package/dist/core/llm/model-router.js +0 -49
- package/dist/core/llm/provider-registry.d.ts +0 -9
- package/dist/core/llm/provider-registry.js +0 -25
- package/dist/core/llm/types.d.ts +0 -2
- package/dist/core/llm/types.js +0 -2
- package/dist/core/orchestrator/adapters/api-adapter.d.ts +0 -34
- package/dist/core/orchestrator/adapters/api-adapter.js +0 -88
- package/dist/core/orchestrator/adapters/cli-adapter.d.ts +0 -22
- package/dist/core/orchestrator/adapters/cli-adapter.js +0 -69
- package/dist/core/orchestrator/adapters/deterministic-adapter.d.ts +0 -35
- package/dist/core/orchestrator/adapters/deterministic-adapter.js +0 -75
- package/dist/core/orchestrator/adapters/env-whitelist.d.ts +0 -4
- package/dist/core/orchestrator/adapters/env-whitelist.js +0 -27
- package/dist/core/orchestrator/adapters/output-extractor.d.ts +0 -11
- package/dist/core/orchestrator/adapters/output-extractor.js +0 -59
- package/dist/core/orchestrator/adapters/process-manager.d.ts +0 -15
- package/dist/core/orchestrator/adapters/process-manager.js +0 -26
- package/dist/core/orchestrator/adapters/tool-loop.d.ts +0 -22
- package/dist/core/orchestrator/adapters/tool-loop.js +0 -66
- package/dist/core/orchestrator/agent-registry.d.ts +0 -31
- package/dist/core/orchestrator/agent-registry.js +0 -135
- package/dist/core/orchestrator/budget-controller.d.ts +0 -19
- package/dist/core/orchestrator/budget-controller.js +0 -73
- package/dist/core/orchestrator/chain-guard.d.ts +0 -14
- package/dist/core/orchestrator/chain-guard.js +0 -23
- package/dist/core/orchestrator/circuit-breaker.d.ts +0 -65
- package/dist/core/orchestrator/circuit-breaker.js +0 -159
- package/dist/core/orchestrator/claude-stream-parser.d.ts +0 -31
- package/dist/core/orchestrator/claude-stream-parser.js +0 -99
- package/dist/core/orchestrator/config-revisions.d.ts +0 -6
- package/dist/core/orchestrator/config-revisions.js +0 -17
- package/dist/core/orchestrator/dependency-resolver.d.ts +0 -20
- package/dist/core/orchestrator/dependency-resolver.js +0 -78
- package/dist/core/orchestrator/governance-gate.d.ts +0 -110
- package/dist/core/orchestrator/governance-gate.js +0 -170
- package/dist/core/orchestrator/learning-pipeline.d.ts +0 -109
- package/dist/core/orchestrator/learning-pipeline.js +0 -249
- package/dist/core/orchestrator/loop-detector.d.ts +0 -51
- package/dist/core/orchestrator/loop-detector.js +0 -133
- package/dist/core/orchestrator/ndjson-logger.d.ts +0 -6
- package/dist/core/orchestrator/ndjson-logger.js +0 -18
- package/dist/core/orchestrator/permission-relay.d.ts +0 -72
- package/dist/core/orchestrator/permission-relay.js +0 -164
- package/dist/core/orchestrator/run-manager.d.ts +0 -31
- package/dist/core/orchestrator/run-manager.js +0 -178
- package/dist/core/orchestrator/scheduler.d.ts +0 -70
- package/dist/core/orchestrator/scheduler.js +0 -198
- package/dist/core/orchestrator/secret-store.d.ts +0 -57
- package/dist/core/orchestrator/secret-store.js +0 -171
- package/dist/core/orchestrator/session-manager.d.ts +0 -13
- package/dist/core/orchestrator/session-manager.js +0 -66
- package/dist/core/orchestrator/task-queue.d.ts +0 -34
- package/dist/core/orchestrator/task-queue.js +0 -83
- package/dist/core/orchestrator/template-interpolate.d.ts +0 -5
- package/dist/core/orchestrator/template-interpolate.js +0 -18
- package/dist/core/orchestrator/user-registry.d.ts +0 -47
- package/dist/core/orchestrator/user-registry.js +0 -76
- package/dist/core/orchestrator/wakeup-queue.d.ts +0 -9
- package/dist/core/orchestrator/wakeup-queue.js +0 -45
- package/dist/core/orchestrator/workflow-engine.d.ts +0 -47
- package/dist/core/orchestrator/workflow-engine.js +0 -204
- package/dist/core/security/audit.d.ts +0 -20
- package/dist/core/security/audit.js +0 -33
- package/dist/core/security/column-validator.d.ts +0 -20
- package/dist/core/security/column-validator.js +0 -37
- package/dist/core/security/index.d.ts +0 -5
- package/dist/core/security/index.js +0 -5
- package/dist/core/security/process-env.d.ts +0 -13
- package/dist/core/security/process-env.js +0 -49
- package/dist/core/security/sanitizer.d.ts +0 -11
- package/dist/core/security/sanitizer.js +0 -39
- package/dist/core/security/types.d.ts +0 -11
- package/dist/core/security/types.js +0 -1
- package/dist/core/update/auto-update.d.ts +0 -21
- package/dist/core/update/auto-update.js +0 -102
- package/dist/core/update/backup-manager.d.ts +0 -7
- package/dist/core/update/backup-manager.js +0 -24
- package/dist/core/update/index.d.ts +0 -8
- package/dist/core/update/index.js +0 -6
- package/dist/core/update/migration-hooks.d.ts +0 -11
- package/dist/core/update/migration-hooks.js +0 -10
- package/dist/core/update/types.d.ts +0 -11
- package/dist/core/update/types.js +0 -1
- package/dist/core/update/update-checker.d.ts +0 -11
- package/dist/core/update/update-checker.js +0 -63
- package/dist/core/update/update-manager.d.ts +0 -25
- package/dist/core/update/update-manager.js +0 -101
- package/dist/core/update/version-utils.d.ts +0 -6
- package/dist/core/update/version-utils.js +0 -34
- package/dist/gmail-connector-2FVYTQJH.js +0 -6
- package/dist/gmail-connector-MNUBRNFM.js +0 -6
- package/dist/gmail-connector-PS2VLGNE.js +0 -6
- package/dist/gmail-connector-ULSMN6X2.js +0 -6
- package/dist/gmail-connector-URRFX6A3.js +0 -6
- package/dist/inbound-AFBUPSPG.js +0 -10
- package/dist/inbound-AFOHYNUY.js +0 -6
- package/dist/inbound-CGIXRXGC.js +0 -8
- package/dist/inbound-MCOLRH6U.js +0 -10
- package/dist/inbound-SNEMBLGA.js +0 -6
- package/dist/inbound-ZJHAYVMF.js +0 -10
- package/dist/provider-qqJYv9nv.d.ts +0 -75
- package/dist/providers/anthropic/models.d.ts +0 -2
- package/dist/providers/anthropic/models.js +0 -29
- package/dist/providers/anthropic/provider.d.ts +0 -13
- package/dist/providers/anthropic/provider.js +0 -119
- package/dist/providers/anthropic/tool-converter.d.ts +0 -10
- package/dist/providers/anthropic/tool-converter.js +0 -7
- package/dist/providers/ollama/provider.d.ts +0 -17
- package/dist/providers/ollama/provider.js +0 -185
- package/dist/providers/openai/models.d.ts +0 -2
- package/dist/providers/openai/models.js +0 -29
- package/dist/providers/openai/provider.d.ts +0 -13
- package/dist/providers/openai/provider.js +0 -163
- package/dist/providers/openai/tool-converter.d.ts +0 -10
- package/dist/providers/openai/tool-converter.js +0 -10
- package/dist/shared/constants.d.ts +0 -50
- package/dist/shared/constants.js +0 -64
- package/dist/shared/index.d.ts +0 -14
- package/dist/shared/index.js +0 -14
- package/dist/shared/types/agent.d.ts +0 -36
- package/dist/shared/types/agent.js +0 -2
- package/dist/shared/types/channel.d.ts +0 -70
- package/dist/shared/types/channel.js +0 -2
- package/dist/shared/types/config.d.ts +0 -111
- package/dist/shared/types/config.js +0 -2
- package/dist/shared/types/connector.d.ts +0 -77
- package/dist/shared/types/connector.js +0 -2
- package/dist/shared/types/execution.d.ts +0 -29
- package/dist/shared/types/execution.js +0 -2
- package/dist/shared/types/provider.d.ts +0 -73
- package/dist/shared/types/provider.js +0 -2
- package/dist/shared/types/task.d.ts +0 -47
- package/dist/shared/types/task.js +0 -2
- package/dist/shared/types/workflow.d.ts +0 -39
- package/dist/shared/types/workflow.js +0 -2
- package/dist/shared/utils.d.ts +0 -6
- package/dist/shared/utils.js +0 -13
- package/dist/update-check.d.ts +0 -5
- package/dist/update-check.js +0 -56
package/dist/chunk-3X3YKI4T.js
DELETED
|
@@ -1,357 +0,0 @@
|
|
|
1
|
-
// src/connectors/google/oauth.ts
|
|
2
|
-
var _google;
|
|
3
|
-
async function getGoogle() {
|
|
4
|
-
if (!_google) {
|
|
5
|
-
try {
|
|
6
|
-
const mod = await import("googleapis");
|
|
7
|
-
_google = mod.google;
|
|
8
|
-
} catch {
|
|
9
|
-
throw new Error(
|
|
10
|
-
"googleapis is required for Google connectors. Install it: npm install googleapis"
|
|
11
|
-
);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
return _google;
|
|
15
|
-
}
|
|
16
|
-
async function createOAuth2Client(config) {
|
|
17
|
-
const google = await getGoogle();
|
|
18
|
-
return new google.auth.OAuth2(
|
|
19
|
-
config.clientId,
|
|
20
|
-
config.clientSecret,
|
|
21
|
-
config.redirectUri
|
|
22
|
-
);
|
|
23
|
-
}
|
|
24
|
-
function getAuthUrl(client, scopes) {
|
|
25
|
-
return client.generateAuthUrl({
|
|
26
|
-
access_type: "offline",
|
|
27
|
-
prompt: "consent",
|
|
28
|
-
scope: scopes
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
async function exchangeCode(client, code) {
|
|
32
|
-
const { tokens } = await client.getToken(code);
|
|
33
|
-
return tokens;
|
|
34
|
-
}
|
|
35
|
-
async function createServiceAccountClient(config, scopes) {
|
|
36
|
-
const google = await getGoogle();
|
|
37
|
-
const auth = new google.auth.GoogleAuth({
|
|
38
|
-
...config.keyFile ? { keyFile: config.keyFile } : {},
|
|
39
|
-
...config.credentials ? { credentials: config.credentials } : {},
|
|
40
|
-
scopes,
|
|
41
|
-
clientOptions: { subject: config.subject }
|
|
42
|
-
});
|
|
43
|
-
return auth.getClient();
|
|
44
|
-
}
|
|
45
|
-
async function loadTokens(getter, accountKey) {
|
|
46
|
-
const raw = await getter(`google_tokens:${accountKey}`);
|
|
47
|
-
if (!raw) return null;
|
|
48
|
-
try {
|
|
49
|
-
return JSON.parse(raw);
|
|
50
|
-
} catch {
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
async function saveTokens(setter, accountKey, tokens) {
|
|
55
|
-
await setter(`google_tokens:${accountKey}`, JSON.stringify(tokens));
|
|
56
|
-
}
|
|
57
|
-
async function refreshIfNeeded(client, tokens, saver) {
|
|
58
|
-
const buffer = 6e4;
|
|
59
|
-
const isExpired = tokens.expiry_date != null && Date.now() >= tokens.expiry_date - buffer;
|
|
60
|
-
if (!isExpired) return tokens;
|
|
61
|
-
client.setCredentials(tokens);
|
|
62
|
-
const { credentials } = await client.refreshAccessToken();
|
|
63
|
-
const refreshed = {
|
|
64
|
-
access_token: credentials.access_token,
|
|
65
|
-
refresh_token: credentials.refresh_token ?? tokens.refresh_token,
|
|
66
|
-
expiry_date: credentials.expiry_date ?? void 0,
|
|
67
|
-
token_type: credentials.token_type ?? "Bearer"
|
|
68
|
-
};
|
|
69
|
-
if (saver) {
|
|
70
|
-
await saver(refreshed);
|
|
71
|
-
}
|
|
72
|
-
return refreshed;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// src/connectors/google/gmail-connector.ts
|
|
76
|
-
var GoogleGmailConnector = class {
|
|
77
|
-
id = "google-gmail";
|
|
78
|
-
meta = {
|
|
79
|
-
displayName: "Google Gmail",
|
|
80
|
-
provider: "google",
|
|
81
|
-
dataType: "email"
|
|
82
|
-
};
|
|
83
|
-
tokenLoader;
|
|
84
|
-
tokenSaver;
|
|
85
|
-
client = null;
|
|
86
|
-
config = null;
|
|
87
|
-
tokens = null;
|
|
88
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
89
|
-
gmail = null;
|
|
90
|
-
constructor(opts = {}) {
|
|
91
|
-
this.tokenLoader = opts.tokenLoader;
|
|
92
|
-
this.tokenSaver = opts.tokenSaver;
|
|
93
|
-
}
|
|
94
|
-
// ── Lifecycle ──────────────────────────────────────────────────
|
|
95
|
-
async connect(config) {
|
|
96
|
-
this.config = config;
|
|
97
|
-
const scopes = config.scopes ?? [
|
|
98
|
-
"https://www.googleapis.com/auth/gmail.readonly"
|
|
99
|
-
];
|
|
100
|
-
if (config.serviceAccount) {
|
|
101
|
-
this.client = await createServiceAccountClient(config.serviceAccount, scopes);
|
|
102
|
-
} else if (config.oauth) {
|
|
103
|
-
this.client = await createOAuth2Client(config.oauth);
|
|
104
|
-
if (!this.tokenLoader) {
|
|
105
|
-
throw new Error("tokenLoader required for OAuth2 flow");
|
|
106
|
-
}
|
|
107
|
-
this.tokens = await loadTokens(this.tokenLoader, config.account);
|
|
108
|
-
if (!this.tokens) {
|
|
109
|
-
throw new Error(
|
|
110
|
-
`No stored tokens for account ${config.account}. Complete the OAuth flow first.`
|
|
111
|
-
);
|
|
112
|
-
}
|
|
113
|
-
this.tokens = await refreshIfNeeded(
|
|
114
|
-
this.client,
|
|
115
|
-
this.tokens,
|
|
116
|
-
this.tokenSaver ? async (t) => saveTokens(this.tokenSaver, config.account, t) : void 0
|
|
117
|
-
);
|
|
118
|
-
this.client.setCredentials(this.tokens);
|
|
119
|
-
} else {
|
|
120
|
-
throw new Error("Either serviceAccount or oauth config is required");
|
|
121
|
-
}
|
|
122
|
-
const { google } = await import("googleapis");
|
|
123
|
-
this.gmail = google.gmail({ version: "v1", auth: this.client });
|
|
124
|
-
}
|
|
125
|
-
async disconnect() {
|
|
126
|
-
this.client = null;
|
|
127
|
-
this.gmail = null;
|
|
128
|
-
this.tokens = null;
|
|
129
|
-
this.config = null;
|
|
130
|
-
}
|
|
131
|
-
async healthCheck() {
|
|
132
|
-
try {
|
|
133
|
-
this.ensureConnected();
|
|
134
|
-
const res = await this.gmail.users.getProfile({ userId: "me" });
|
|
135
|
-
return { ok: true, account: res.data.emailAddress };
|
|
136
|
-
} catch (err) {
|
|
137
|
-
return { ok: false, error: errorMessage(err) };
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
// ── Auth ───────────────────────────────────────────────────────
|
|
141
|
-
async authenticate(codeProvider) {
|
|
142
|
-
if (!this.config) {
|
|
143
|
-
return { success: false, error: "Call connect() first to set config, or pass config and call authenticate() before connect()." };
|
|
144
|
-
}
|
|
145
|
-
try {
|
|
146
|
-
if (!this.config.oauth) {
|
|
147
|
-
return { success: false, error: "OAuth config required for browser-based authenticate(). Use serviceAccount for headless auth." };
|
|
148
|
-
}
|
|
149
|
-
if (!this.tokenSaver) {
|
|
150
|
-
return { success: false, error: "tokenSaver required for authenticate() flow." };
|
|
151
|
-
}
|
|
152
|
-
const client = await createOAuth2Client(this.config.oauth);
|
|
153
|
-
const scopes = this.config.scopes ?? [
|
|
154
|
-
"https://www.googleapis.com/auth/gmail.readonly",
|
|
155
|
-
"https://www.googleapis.com/auth/gmail.send"
|
|
156
|
-
];
|
|
157
|
-
const authUrl = getAuthUrl(client, scopes);
|
|
158
|
-
const code = await codeProvider(authUrl);
|
|
159
|
-
const tokens = await exchangeCode(client, code);
|
|
160
|
-
await saveTokens(this.tokenSaver, this.config.account, tokens);
|
|
161
|
-
this.tokens = tokens;
|
|
162
|
-
this.client = client;
|
|
163
|
-
this.client.setCredentials(tokens);
|
|
164
|
-
const { google } = await import("googleapis");
|
|
165
|
-
this.gmail = google.gmail({ version: "v1", auth: this.client });
|
|
166
|
-
return { success: true, account: this.config.account };
|
|
167
|
-
} catch (err) {
|
|
168
|
-
return { success: false, error: errorMessage(err) };
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
// ── Sync ───────────────────────────────────────────────────────
|
|
172
|
-
async sync(options) {
|
|
173
|
-
this.ensureConnected();
|
|
174
|
-
if (options?.cursor) {
|
|
175
|
-
return this.syncIncremental(options.cursor, options.limit);
|
|
176
|
-
}
|
|
177
|
-
return this.syncFull(options);
|
|
178
|
-
}
|
|
179
|
-
/** Incremental sync using Gmail history API. */
|
|
180
|
-
async syncIncremental(startHistoryId, limit) {
|
|
181
|
-
const records = [];
|
|
182
|
-
const errors = [];
|
|
183
|
-
const seenIds = /* @__PURE__ */ new Set();
|
|
184
|
-
let pageToken;
|
|
185
|
-
let latestHistoryId = startHistoryId;
|
|
186
|
-
do {
|
|
187
|
-
const res = await this.gmail.users.history.list({
|
|
188
|
-
userId: "me",
|
|
189
|
-
startHistoryId,
|
|
190
|
-
historyTypes: ["messageAdded"],
|
|
191
|
-
...pageToken ? { pageToken } : {}
|
|
192
|
-
});
|
|
193
|
-
latestHistoryId = res.data.historyId ?? latestHistoryId;
|
|
194
|
-
const histories = res.data.history ?? [];
|
|
195
|
-
for (const h of histories) {
|
|
196
|
-
for (const added of h.messagesAdded ?? []) {
|
|
197
|
-
const msgId = added.message?.id;
|
|
198
|
-
if (!msgId || seenIds.has(msgId)) continue;
|
|
199
|
-
seenIds.add(msgId);
|
|
200
|
-
try {
|
|
201
|
-
const record = await this.fetchMessage(msgId);
|
|
202
|
-
records.push(record);
|
|
203
|
-
} catch (err) {
|
|
204
|
-
errors.push({ id: msgId, error: errorMessage(err) });
|
|
205
|
-
}
|
|
206
|
-
if (limit && records.length >= limit) {
|
|
207
|
-
return { records, cursor: latestHistoryId, hasMore: true, errors };
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
pageToken = res.data.nextPageToken ?? void 0;
|
|
212
|
-
} while (pageToken);
|
|
213
|
-
return { records, cursor: latestHistoryId, hasMore: false, errors };
|
|
214
|
-
}
|
|
215
|
-
/** Full sync — list messages and fetch each one. */
|
|
216
|
-
async syncFull(options) {
|
|
217
|
-
const records = [];
|
|
218
|
-
const errors = [];
|
|
219
|
-
const maxResults = options?.limit ?? 100;
|
|
220
|
-
let query = "";
|
|
221
|
-
if (options?.since) {
|
|
222
|
-
const epoch = Math.floor(new Date(options.since).getTime() / 1e3);
|
|
223
|
-
query = `after:${epoch}`;
|
|
224
|
-
}
|
|
225
|
-
if (options?.filters?.q) {
|
|
226
|
-
query = query ? `${query} ${options.filters.q}` : String(options.filters.q);
|
|
227
|
-
}
|
|
228
|
-
let pageToken;
|
|
229
|
-
let collected = 0;
|
|
230
|
-
do {
|
|
231
|
-
const res = await this.gmail.users.messages.list({
|
|
232
|
-
userId: "me",
|
|
233
|
-
maxResults: Math.min(maxResults - collected, 100),
|
|
234
|
-
...query ? { q: query } : {},
|
|
235
|
-
...pageToken ? { pageToken } : {}
|
|
236
|
-
});
|
|
237
|
-
const messages = res.data.messages ?? [];
|
|
238
|
-
for (const msg of messages) {
|
|
239
|
-
try {
|
|
240
|
-
const record = await this.fetchMessage(msg.id);
|
|
241
|
-
records.push(record);
|
|
242
|
-
} catch (err) {
|
|
243
|
-
errors.push({ id: msg.id, error: errorMessage(err) });
|
|
244
|
-
}
|
|
245
|
-
collected++;
|
|
246
|
-
if (collected >= maxResults) break;
|
|
247
|
-
}
|
|
248
|
-
pageToken = res.data.nextPageToken ?? void 0;
|
|
249
|
-
} while (pageToken && collected < maxResults);
|
|
250
|
-
const profile = await this.gmail.users.getProfile({ userId: "me" });
|
|
251
|
-
const cursor = profile.data.historyId ?? void 0;
|
|
252
|
-
return {
|
|
253
|
-
records,
|
|
254
|
-
cursor,
|
|
255
|
-
hasMore: !!pageToken,
|
|
256
|
-
errors
|
|
257
|
-
};
|
|
258
|
-
}
|
|
259
|
-
// ── Push (send email) ─────────────────────────────────────────
|
|
260
|
-
async push(payload) {
|
|
261
|
-
this.ensureConnected();
|
|
262
|
-
try {
|
|
263
|
-
const toHeader = payload.to.map(formatAddress).join(", ");
|
|
264
|
-
const ccHeader = payload.cc.length ? `Cc: ${payload.cc.map(formatAddress).join(", ")}\r
|
|
265
|
-
` : "";
|
|
266
|
-
const bccHeader = payload.bcc.length ? `Bcc: ${payload.bcc.map(formatAddress).join(", ")}\r
|
|
267
|
-
` : "";
|
|
268
|
-
const mime = [
|
|
269
|
-
`To: ${toHeader}\r
|
|
270
|
-
`,
|
|
271
|
-
ccHeader,
|
|
272
|
-
bccHeader,
|
|
273
|
-
`Subject: ${payload.subject}\r
|
|
274
|
-
`,
|
|
275
|
-
`Content-Type: text/plain; charset="UTF-8"\r
|
|
276
|
-
`,
|
|
277
|
-
`\r
|
|
278
|
-
`,
|
|
279
|
-
payload.body ?? ""
|
|
280
|
-
].join("");
|
|
281
|
-
const encoded = Buffer.from(mime).toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
282
|
-
const res = await this.gmail.users.messages.send({
|
|
283
|
-
userId: "me",
|
|
284
|
-
requestBody: { raw: encoded }
|
|
285
|
-
});
|
|
286
|
-
return { success: true, externalId: res.data.id };
|
|
287
|
-
} catch (err) {
|
|
288
|
-
return { success: false, error: errorMessage(err) };
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
// ── Internals ─────────────────────────────────────────────────
|
|
292
|
-
ensureConnected() {
|
|
293
|
-
if (!this.gmail || !this.config) {
|
|
294
|
-
throw new Error("GoogleGmailConnector is not connected. Call connect() first.");
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
/** Fetch a single message by ID and parse into an EmailRecord. */
|
|
298
|
-
async fetchMessage(messageId) {
|
|
299
|
-
const res = await this.gmail.users.messages.get({
|
|
300
|
-
userId: "me",
|
|
301
|
-
id: messageId,
|
|
302
|
-
format: "metadata",
|
|
303
|
-
metadataHeaders: ["From", "To", "Cc", "Bcc", "Subject", "Date"]
|
|
304
|
-
});
|
|
305
|
-
const msg = res.data;
|
|
306
|
-
const headers = msg.payload?.headers ?? [];
|
|
307
|
-
const getHeader = (name) => headers.find((h) => h.name.toLowerCase() === name.toLowerCase())?.value ?? "";
|
|
308
|
-
return {
|
|
309
|
-
gmailId: msg.id,
|
|
310
|
-
threadId: msg.threadId,
|
|
311
|
-
account: this.config.account,
|
|
312
|
-
subject: getHeader("Subject"),
|
|
313
|
-
from: parseAddress(getHeader("From")),
|
|
314
|
-
to: parseAddressList(getHeader("To")),
|
|
315
|
-
cc: parseAddressList(getHeader("Cc")),
|
|
316
|
-
bcc: parseAddressList(getHeader("Bcc")),
|
|
317
|
-
date: new Date(getHeader("Date")).toISOString(),
|
|
318
|
-
snippet: msg.snippet ?? "",
|
|
319
|
-
labels: msg.labelIds ?? [],
|
|
320
|
-
isRead: !(msg.labelIds ?? []).includes("UNREAD")
|
|
321
|
-
};
|
|
322
|
-
}
|
|
323
|
-
};
|
|
324
|
-
function parseAddress(raw) {
|
|
325
|
-
const match = raw.match(/^(.+?)\s*<([^>]+)>$/);
|
|
326
|
-
if (match) {
|
|
327
|
-
return { name: match[1].replace(/^["']|["']$/g, "").trim(), email: match[2] };
|
|
328
|
-
}
|
|
329
|
-
return { email: raw.trim() };
|
|
330
|
-
}
|
|
331
|
-
function parseAddressList(raw) {
|
|
332
|
-
if (!raw.trim()) return [];
|
|
333
|
-
const results = [];
|
|
334
|
-
const parts = raw.split(/,(?=(?:[^<]*<[^>]*>)*[^>]*$)/);
|
|
335
|
-
for (const part of parts) {
|
|
336
|
-
const trimmed = part.trim();
|
|
337
|
-
if (trimmed) results.push(parseAddress(trimmed));
|
|
338
|
-
}
|
|
339
|
-
return results;
|
|
340
|
-
}
|
|
341
|
-
function formatAddress(addr) {
|
|
342
|
-
return addr.name ? `${addr.name} <${addr.email}>` : addr.email;
|
|
343
|
-
}
|
|
344
|
-
function errorMessage(err) {
|
|
345
|
-
return err instanceof Error ? err.message : String(err);
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
export {
|
|
349
|
-
createOAuth2Client,
|
|
350
|
-
getAuthUrl,
|
|
351
|
-
exchangeCode,
|
|
352
|
-
createServiceAccountClient,
|
|
353
|
-
loadTokens,
|
|
354
|
-
saveTokens,
|
|
355
|
-
refreshIfNeeded,
|
|
356
|
-
GoogleGmailConnector
|
|
357
|
-
};
|
package/dist/chunk-D47AIFOD.js
DELETED
|
@@ -1,351 +0,0 @@
|
|
|
1
|
-
// src/connectors/google/oauth.ts
|
|
2
|
-
var _google;
|
|
3
|
-
async function getGoogle() {
|
|
4
|
-
if (!_google) {
|
|
5
|
-
try {
|
|
6
|
-
const mod = await import("googleapis");
|
|
7
|
-
_google = mod.google;
|
|
8
|
-
} catch {
|
|
9
|
-
throw new Error(
|
|
10
|
-
"googleapis is required for Google connectors. Install it: npm install googleapis"
|
|
11
|
-
);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
return _google;
|
|
15
|
-
}
|
|
16
|
-
async function createOAuth2Client(config) {
|
|
17
|
-
const google = await getGoogle();
|
|
18
|
-
return new google.auth.OAuth2(
|
|
19
|
-
config.clientId,
|
|
20
|
-
config.clientSecret,
|
|
21
|
-
config.redirectUri
|
|
22
|
-
);
|
|
23
|
-
}
|
|
24
|
-
function getAuthUrl(client, scopes) {
|
|
25
|
-
return client.generateAuthUrl({
|
|
26
|
-
access_type: "offline",
|
|
27
|
-
prompt: "consent",
|
|
28
|
-
scope: scopes
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
async function exchangeCode(client, code) {
|
|
32
|
-
const { tokens } = await client.getToken(code);
|
|
33
|
-
return tokens;
|
|
34
|
-
}
|
|
35
|
-
async function createServiceAccountClient(config, scopes) {
|
|
36
|
-
const google = await getGoogle();
|
|
37
|
-
const auth = new google.auth.GoogleAuth({
|
|
38
|
-
...config.keyFile ? { keyFile: config.keyFile } : {},
|
|
39
|
-
...config.credentials ? { credentials: config.credentials } : {},
|
|
40
|
-
scopes,
|
|
41
|
-
clientOptions: { subject: config.subject }
|
|
42
|
-
});
|
|
43
|
-
return auth.getClient();
|
|
44
|
-
}
|
|
45
|
-
async function loadTokens(getter, accountKey) {
|
|
46
|
-
const raw = await getter(`google_tokens:${accountKey}`);
|
|
47
|
-
if (!raw) return null;
|
|
48
|
-
try {
|
|
49
|
-
return JSON.parse(raw);
|
|
50
|
-
} catch {
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
async function saveTokens(setter, accountKey, tokens) {
|
|
55
|
-
await setter(`google_tokens:${accountKey}`, JSON.stringify(tokens));
|
|
56
|
-
}
|
|
57
|
-
async function refreshIfNeeded(client, tokens, saver) {
|
|
58
|
-
const buffer = 6e4;
|
|
59
|
-
const isExpired = tokens.expiry_date != null && Date.now() >= tokens.expiry_date - buffer;
|
|
60
|
-
if (!isExpired) return tokens;
|
|
61
|
-
client.setCredentials(tokens);
|
|
62
|
-
const { credentials } = await client.refreshAccessToken();
|
|
63
|
-
const refreshed = {
|
|
64
|
-
access_token: credentials.access_token,
|
|
65
|
-
refresh_token: credentials.refresh_token ?? tokens.refresh_token,
|
|
66
|
-
expiry_date: credentials.expiry_date ?? void 0,
|
|
67
|
-
token_type: credentials.token_type ?? "Bearer"
|
|
68
|
-
};
|
|
69
|
-
if (saver) {
|
|
70
|
-
await saver(refreshed);
|
|
71
|
-
}
|
|
72
|
-
return refreshed;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// src/connectors/google/gmail-connector.ts
|
|
76
|
-
var GoogleGmailConnector = class {
|
|
77
|
-
id = "google-gmail";
|
|
78
|
-
meta = {
|
|
79
|
-
displayName: "Google Gmail",
|
|
80
|
-
provider: "google",
|
|
81
|
-
dataType: "email"
|
|
82
|
-
};
|
|
83
|
-
tokenLoader;
|
|
84
|
-
tokenSaver;
|
|
85
|
-
client = null;
|
|
86
|
-
config = null;
|
|
87
|
-
tokens = null;
|
|
88
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
89
|
-
gmail = null;
|
|
90
|
-
constructor(opts = {}) {
|
|
91
|
-
this.tokenLoader = opts.tokenLoader;
|
|
92
|
-
this.tokenSaver = opts.tokenSaver;
|
|
93
|
-
}
|
|
94
|
-
// ── Lifecycle ──────────────────────────────────────────────────
|
|
95
|
-
async connect(config) {
|
|
96
|
-
this.config = config;
|
|
97
|
-
const scopes = config.scopes ?? [
|
|
98
|
-
"https://www.googleapis.com/auth/gmail.readonly"
|
|
99
|
-
];
|
|
100
|
-
if (config.serviceAccount) {
|
|
101
|
-
this.client = await createServiceAccountClient(config.serviceAccount, scopes);
|
|
102
|
-
} else if (config.oauth) {
|
|
103
|
-
this.client = await createOAuth2Client(config.oauth);
|
|
104
|
-
if (!this.tokenLoader) {
|
|
105
|
-
throw new Error("tokenLoader required for OAuth2 flow");
|
|
106
|
-
}
|
|
107
|
-
this.tokens = await loadTokens(this.tokenLoader, config.account);
|
|
108
|
-
if (!this.tokens) {
|
|
109
|
-
throw new Error(
|
|
110
|
-
`No stored tokens for account ${config.account}. Complete the OAuth flow first.`
|
|
111
|
-
);
|
|
112
|
-
}
|
|
113
|
-
this.tokens = await refreshIfNeeded(
|
|
114
|
-
this.client,
|
|
115
|
-
this.tokens,
|
|
116
|
-
this.tokenSaver ? async (t) => saveTokens(this.tokenSaver, config.account, t) : void 0
|
|
117
|
-
);
|
|
118
|
-
this.client.setCredentials(this.tokens);
|
|
119
|
-
} else {
|
|
120
|
-
throw new Error("Either serviceAccount or oauth config is required");
|
|
121
|
-
}
|
|
122
|
-
const { google } = await import("googleapis");
|
|
123
|
-
this.gmail = google.gmail({ version: "v1", auth: this.client });
|
|
124
|
-
}
|
|
125
|
-
async disconnect() {
|
|
126
|
-
this.client = null;
|
|
127
|
-
this.gmail = null;
|
|
128
|
-
this.tokens = null;
|
|
129
|
-
this.config = null;
|
|
130
|
-
}
|
|
131
|
-
async healthCheck() {
|
|
132
|
-
try {
|
|
133
|
-
this.ensureConnected();
|
|
134
|
-
const res = await this.gmail.users.getProfile({ userId: "me" });
|
|
135
|
-
return { ok: true, account: res.data.emailAddress };
|
|
136
|
-
} catch (err) {
|
|
137
|
-
return { ok: false, error: errorMessage(err) };
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
// ── Auth ───────────────────────────────────────────────────────
|
|
141
|
-
async authenticate(codeProvider) {
|
|
142
|
-
if (!this.config) {
|
|
143
|
-
return { success: false, error: "Call connect() first to set config, or pass config and call authenticate() before connect()." };
|
|
144
|
-
}
|
|
145
|
-
try {
|
|
146
|
-
const client = await createOAuth2Client(this.config.oauth);
|
|
147
|
-
const scopes = this.config.scopes ?? [
|
|
148
|
-
"https://www.googleapis.com/auth/gmail.readonly",
|
|
149
|
-
"https://www.googleapis.com/auth/gmail.send"
|
|
150
|
-
];
|
|
151
|
-
const authUrl = getAuthUrl(client, scopes);
|
|
152
|
-
const code = await codeProvider(authUrl);
|
|
153
|
-
const tokens = await exchangeCode(client, code);
|
|
154
|
-
await saveTokens(this.tokenSaver, this.config.account, tokens);
|
|
155
|
-
this.tokens = tokens;
|
|
156
|
-
this.client = client;
|
|
157
|
-
this.client.setCredentials(tokens);
|
|
158
|
-
const { google } = await import("googleapis");
|
|
159
|
-
this.gmail = google.gmail({ version: "v1", auth: this.client });
|
|
160
|
-
return { success: true, account: this.config.account };
|
|
161
|
-
} catch (err) {
|
|
162
|
-
return { success: false, error: errorMessage(err) };
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
// ── Sync ───────────────────────────────────────────────────────
|
|
166
|
-
async sync(options) {
|
|
167
|
-
this.ensureConnected();
|
|
168
|
-
if (options?.cursor) {
|
|
169
|
-
return this.syncIncremental(options.cursor, options.limit);
|
|
170
|
-
}
|
|
171
|
-
return this.syncFull(options);
|
|
172
|
-
}
|
|
173
|
-
/** Incremental sync using Gmail history API. */
|
|
174
|
-
async syncIncremental(startHistoryId, limit) {
|
|
175
|
-
const records = [];
|
|
176
|
-
const errors = [];
|
|
177
|
-
const seenIds = /* @__PURE__ */ new Set();
|
|
178
|
-
let pageToken;
|
|
179
|
-
let latestHistoryId = startHistoryId;
|
|
180
|
-
do {
|
|
181
|
-
const res = await this.gmail.users.history.list({
|
|
182
|
-
userId: "me",
|
|
183
|
-
startHistoryId,
|
|
184
|
-
historyTypes: ["messageAdded"],
|
|
185
|
-
...pageToken ? { pageToken } : {}
|
|
186
|
-
});
|
|
187
|
-
latestHistoryId = res.data.historyId ?? latestHistoryId;
|
|
188
|
-
const histories = res.data.history ?? [];
|
|
189
|
-
for (const h of histories) {
|
|
190
|
-
for (const added of h.messagesAdded ?? []) {
|
|
191
|
-
const msgId = added.message?.id;
|
|
192
|
-
if (!msgId || seenIds.has(msgId)) continue;
|
|
193
|
-
seenIds.add(msgId);
|
|
194
|
-
try {
|
|
195
|
-
const record = await this.fetchMessage(msgId);
|
|
196
|
-
records.push(record);
|
|
197
|
-
} catch (err) {
|
|
198
|
-
errors.push({ id: msgId, error: errorMessage(err) });
|
|
199
|
-
}
|
|
200
|
-
if (limit && records.length >= limit) {
|
|
201
|
-
return { records, cursor: latestHistoryId, hasMore: true, errors };
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
pageToken = res.data.nextPageToken ?? void 0;
|
|
206
|
-
} while (pageToken);
|
|
207
|
-
return { records, cursor: latestHistoryId, hasMore: false, errors };
|
|
208
|
-
}
|
|
209
|
-
/** Full sync — list messages and fetch each one. */
|
|
210
|
-
async syncFull(options) {
|
|
211
|
-
const records = [];
|
|
212
|
-
const errors = [];
|
|
213
|
-
const maxResults = options?.limit ?? 100;
|
|
214
|
-
let query = "";
|
|
215
|
-
if (options?.since) {
|
|
216
|
-
const epoch = Math.floor(new Date(options.since).getTime() / 1e3);
|
|
217
|
-
query = `after:${epoch}`;
|
|
218
|
-
}
|
|
219
|
-
if (options?.filters?.q) {
|
|
220
|
-
query = query ? `${query} ${options.filters.q}` : String(options.filters.q);
|
|
221
|
-
}
|
|
222
|
-
let pageToken;
|
|
223
|
-
let collected = 0;
|
|
224
|
-
do {
|
|
225
|
-
const res = await this.gmail.users.messages.list({
|
|
226
|
-
userId: "me",
|
|
227
|
-
maxResults: Math.min(maxResults - collected, 100),
|
|
228
|
-
...query ? { q: query } : {},
|
|
229
|
-
...pageToken ? { pageToken } : {}
|
|
230
|
-
});
|
|
231
|
-
const messages = res.data.messages ?? [];
|
|
232
|
-
for (const msg of messages) {
|
|
233
|
-
try {
|
|
234
|
-
const record = await this.fetchMessage(msg.id);
|
|
235
|
-
records.push(record);
|
|
236
|
-
} catch (err) {
|
|
237
|
-
errors.push({ id: msg.id, error: errorMessage(err) });
|
|
238
|
-
}
|
|
239
|
-
collected++;
|
|
240
|
-
if (collected >= maxResults) break;
|
|
241
|
-
}
|
|
242
|
-
pageToken = res.data.nextPageToken ?? void 0;
|
|
243
|
-
} while (pageToken && collected < maxResults);
|
|
244
|
-
const profile = await this.gmail.users.getProfile({ userId: "me" });
|
|
245
|
-
const cursor = profile.data.historyId ?? void 0;
|
|
246
|
-
return {
|
|
247
|
-
records,
|
|
248
|
-
cursor,
|
|
249
|
-
hasMore: !!pageToken,
|
|
250
|
-
errors
|
|
251
|
-
};
|
|
252
|
-
}
|
|
253
|
-
// ── Push (send email) ─────────────────────────────────────────
|
|
254
|
-
async push(payload) {
|
|
255
|
-
this.ensureConnected();
|
|
256
|
-
try {
|
|
257
|
-
const toHeader = payload.to.map(formatAddress).join(", ");
|
|
258
|
-
const ccHeader = payload.cc.length ? `Cc: ${payload.cc.map(formatAddress).join(", ")}\r
|
|
259
|
-
` : "";
|
|
260
|
-
const bccHeader = payload.bcc.length ? `Bcc: ${payload.bcc.map(formatAddress).join(", ")}\r
|
|
261
|
-
` : "";
|
|
262
|
-
const mime = [
|
|
263
|
-
`To: ${toHeader}\r
|
|
264
|
-
`,
|
|
265
|
-
ccHeader,
|
|
266
|
-
bccHeader,
|
|
267
|
-
`Subject: ${payload.subject}\r
|
|
268
|
-
`,
|
|
269
|
-
`Content-Type: text/plain; charset="UTF-8"\r
|
|
270
|
-
`,
|
|
271
|
-
`\r
|
|
272
|
-
`,
|
|
273
|
-
payload.body ?? ""
|
|
274
|
-
].join("");
|
|
275
|
-
const encoded = Buffer.from(mime).toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
276
|
-
const res = await this.gmail.users.messages.send({
|
|
277
|
-
userId: "me",
|
|
278
|
-
requestBody: { raw: encoded }
|
|
279
|
-
});
|
|
280
|
-
return { success: true, externalId: res.data.id };
|
|
281
|
-
} catch (err) {
|
|
282
|
-
return { success: false, error: errorMessage(err) };
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
// ── Internals ─────────────────────────────────────────────────
|
|
286
|
-
ensureConnected() {
|
|
287
|
-
if (!this.gmail || !this.config) {
|
|
288
|
-
throw new Error("GoogleGmailConnector is not connected. Call connect() first.");
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
/** Fetch a single message by ID and parse into an EmailRecord. */
|
|
292
|
-
async fetchMessage(messageId) {
|
|
293
|
-
const res = await this.gmail.users.messages.get({
|
|
294
|
-
userId: "me",
|
|
295
|
-
id: messageId,
|
|
296
|
-
format: "metadata",
|
|
297
|
-
metadataHeaders: ["From", "To", "Cc", "Bcc", "Subject", "Date"]
|
|
298
|
-
});
|
|
299
|
-
const msg = res.data;
|
|
300
|
-
const headers = msg.payload?.headers ?? [];
|
|
301
|
-
const getHeader = (name) => headers.find((h) => h.name.toLowerCase() === name.toLowerCase())?.value ?? "";
|
|
302
|
-
return {
|
|
303
|
-
gmailId: msg.id,
|
|
304
|
-
threadId: msg.threadId,
|
|
305
|
-
account: this.config.account,
|
|
306
|
-
subject: getHeader("Subject"),
|
|
307
|
-
from: parseAddress(getHeader("From")),
|
|
308
|
-
to: parseAddressList(getHeader("To")),
|
|
309
|
-
cc: parseAddressList(getHeader("Cc")),
|
|
310
|
-
bcc: parseAddressList(getHeader("Bcc")),
|
|
311
|
-
date: new Date(getHeader("Date")).toISOString(),
|
|
312
|
-
snippet: msg.snippet ?? "",
|
|
313
|
-
labels: msg.labelIds ?? [],
|
|
314
|
-
isRead: !(msg.labelIds ?? []).includes("UNREAD")
|
|
315
|
-
};
|
|
316
|
-
}
|
|
317
|
-
};
|
|
318
|
-
function parseAddress(raw) {
|
|
319
|
-
const match = raw.match(/^(.+?)\s*<([^>]+)>$/);
|
|
320
|
-
if (match) {
|
|
321
|
-
return { name: match[1].replace(/^["']|["']$/g, "").trim(), email: match[2] };
|
|
322
|
-
}
|
|
323
|
-
return { email: raw.trim() };
|
|
324
|
-
}
|
|
325
|
-
function parseAddressList(raw) {
|
|
326
|
-
if (!raw.trim()) return [];
|
|
327
|
-
const results = [];
|
|
328
|
-
const parts = raw.split(/,(?=(?:[^<]*<[^>]*>)*[^>]*$)/);
|
|
329
|
-
for (const part of parts) {
|
|
330
|
-
const trimmed = part.trim();
|
|
331
|
-
if (trimmed) results.push(parseAddress(trimmed));
|
|
332
|
-
}
|
|
333
|
-
return results;
|
|
334
|
-
}
|
|
335
|
-
function formatAddress(addr) {
|
|
336
|
-
return addr.name ? `${addr.name} <${addr.email}>` : addr.email;
|
|
337
|
-
}
|
|
338
|
-
function errorMessage(err) {
|
|
339
|
-
return err instanceof Error ? err.message : String(err);
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
export {
|
|
343
|
-
createOAuth2Client,
|
|
344
|
-
getAuthUrl,
|
|
345
|
-
exchangeCode,
|
|
346
|
-
createServiceAccountClient,
|
|
347
|
-
loadTokens,
|
|
348
|
-
saveTokens,
|
|
349
|
-
refreshIfNeeded,
|
|
350
|
-
GoogleGmailConnector
|
|
351
|
-
};
|