botinabox 2.7.10 → 2.8.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 +1 -1
- package/dist/channel-DziSPayj.d.ts +73 -0
- package/dist/channels/discord/index.d.ts +1 -1
- package/dist/channels/slack/index.d.ts +75 -4
- package/dist/channels/slack/index.js +140 -6
- package/dist/channels/webhook/index.d.ts +1 -1
- package/dist/chat-pipeline-BGgmH_ap.d.ts +655 -0
- package/dist/chat-pipeline-BWrtVqEP.d.ts +652 -0
- package/dist/chunk-OEMM2LEA.js +223 -0
- package/dist/chunk-XYF5PSB2.js +389 -0
- package/dist/cli.js +0 -0
- package/dist/gmail-connector-Z7SO6VOS.js +7 -0
- package/dist/inbound-5FKJBWPL.js +11 -0
- package/dist/index.d.ts +6 -6
- package/dist/provider-BHkqkSdq.d.ts +89 -0
- package/dist/providers/anthropic/index.d.ts +1 -1
- package/dist/providers/ollama/index.d.ts +1 -1
- package/dist/providers/openai/index.d.ts +1 -1
- package/package.json +100 -100
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
// src/channels/slack/transcribe.ts
|
|
2
|
+
import { execFileSync } from "child_process";
|
|
3
|
+
import { writeFileSync, unlinkSync, mkdirSync } from "fs";
|
|
4
|
+
import { join } from "path";
|
|
5
|
+
import { randomUUID } from "crypto";
|
|
6
|
+
import os from "os";
|
|
7
|
+
import { createRequire } from "module";
|
|
8
|
+
var TEMP_DIR = join(os.tmpdir(), "botinabox-audio");
|
|
9
|
+
async function transcribeAudio(audioBuffer, filename, opts) {
|
|
10
|
+
let whisper;
|
|
11
|
+
try {
|
|
12
|
+
const require2 = createRequire(import.meta.url);
|
|
13
|
+
const mod = require2("whisper-node");
|
|
14
|
+
whisper = mod.whisper ?? mod.default ?? mod;
|
|
15
|
+
} catch {
|
|
16
|
+
console.warn("[botinabox] whisper-node not installed \u2014 voice transcription unavailable. Run: npm install whisper-node && npx whisper-node download");
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
execFileSync("ffmpeg", ["-version"], { stdio: "ignore" });
|
|
21
|
+
} catch {
|
|
22
|
+
console.warn("[botinabox] ffmpeg not found \u2014 required for audio conversion. Install: brew install ffmpeg");
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
const id = randomUUID().slice(0, 8);
|
|
26
|
+
const ext = filename.split(".").pop() ?? "aac";
|
|
27
|
+
mkdirSync(TEMP_DIR, { recursive: true });
|
|
28
|
+
const inputPath = join(TEMP_DIR, `${id}.${ext}`);
|
|
29
|
+
const wavPath = join(TEMP_DIR, `${id}.wav`);
|
|
30
|
+
try {
|
|
31
|
+
writeFileSync(inputPath, audioBuffer);
|
|
32
|
+
execFileSync("ffmpeg", ["-y", "-i", inputPath, "-ar", "16000", "-ac", "1", "-c:a", "pcm_s16le", wavPath], {
|
|
33
|
+
stdio: "ignore",
|
|
34
|
+
timeout: 3e4
|
|
35
|
+
});
|
|
36
|
+
const segments = await whisper(wavPath, {
|
|
37
|
+
modelName: opts?.modelName ?? "base.en",
|
|
38
|
+
whisperOptions: {
|
|
39
|
+
language: opts?.language ?? "auto"
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
if (!segments || segments.length === 0) return null;
|
|
43
|
+
return segments.map((s) => s.speech).join(" ").trim();
|
|
44
|
+
} catch (err) {
|
|
45
|
+
console.error("[botinabox] Transcription failed:", err);
|
|
46
|
+
return null;
|
|
47
|
+
} finally {
|
|
48
|
+
try {
|
|
49
|
+
unlinkSync(inputPath);
|
|
50
|
+
} catch {
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
unlinkSync(wavPath);
|
|
54
|
+
} catch {
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async function downloadAudio(url, token) {
|
|
59
|
+
try {
|
|
60
|
+
const resp = await fetch(url, {
|
|
61
|
+
headers: { Authorization: `Bearer ${token}` }
|
|
62
|
+
});
|
|
63
|
+
if (!resp.ok) {
|
|
64
|
+
console.error(`[botinabox] Audio download failed: ${resp.status} ${resp.statusText}`);
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
return Buffer.from(await resp.arrayBuffer());
|
|
68
|
+
} catch (err) {
|
|
69
|
+
console.error("[botinabox] Audio download error:", err);
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/channels/slack/media-type.ts
|
|
75
|
+
var FILETYPE_MAP = {
|
|
76
|
+
// image
|
|
77
|
+
jpg: "image",
|
|
78
|
+
jpeg: "image",
|
|
79
|
+
png: "image",
|
|
80
|
+
gif: "image",
|
|
81
|
+
webp: "image",
|
|
82
|
+
heic: "image",
|
|
83
|
+
svg: "image",
|
|
84
|
+
bmp: "image",
|
|
85
|
+
// video
|
|
86
|
+
mp4: "video",
|
|
87
|
+
mov: "video",
|
|
88
|
+
webm: "video",
|
|
89
|
+
avi: "video",
|
|
90
|
+
mkv: "video",
|
|
91
|
+
// audio (also handled by voice-message path — included for completeness)
|
|
92
|
+
aac: "audio",
|
|
93
|
+
m4a: "audio",
|
|
94
|
+
mp3: "audio",
|
|
95
|
+
wav: "audio",
|
|
96
|
+
ogg: "audio",
|
|
97
|
+
flac: "audio",
|
|
98
|
+
// pdf
|
|
99
|
+
pdf: "pdf",
|
|
100
|
+
// doc
|
|
101
|
+
gdoc: "doc",
|
|
102
|
+
docx: "doc",
|
|
103
|
+
doc: "doc",
|
|
104
|
+
md: "doc",
|
|
105
|
+
txt: "doc",
|
|
106
|
+
rtf: "doc",
|
|
107
|
+
// excel
|
|
108
|
+
gsheet: "excel",
|
|
109
|
+
xlsx: "excel",
|
|
110
|
+
xls: "excel",
|
|
111
|
+
csv: "excel",
|
|
112
|
+
tsv: "excel",
|
|
113
|
+
// presentation
|
|
114
|
+
gslide: "presentation",
|
|
115
|
+
pptx: "presentation",
|
|
116
|
+
ppt: "presentation",
|
|
117
|
+
key: "presentation",
|
|
118
|
+
// html
|
|
119
|
+
html: "html",
|
|
120
|
+
htm: "html"
|
|
121
|
+
};
|
|
122
|
+
function slackFiletypeToMediaType(filetype) {
|
|
123
|
+
if (!filetype) return "misc";
|
|
124
|
+
return FILETYPE_MAP[filetype.toLowerCase()] ?? "misc";
|
|
125
|
+
}
|
|
126
|
+
var URL_REGEX = /https?:\/\/[^\s<>"')]+/g;
|
|
127
|
+
function extractUrls(text) {
|
|
128
|
+
if (!text) return [];
|
|
129
|
+
const matches = text.match(URL_REGEX);
|
|
130
|
+
if (!matches) return [];
|
|
131
|
+
return Array.from(new Set(matches.map((u) => u.replace(/[.,;:!?)]+$/, ""))));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// src/channels/slack/inbound.ts
|
|
135
|
+
var AUDIO_TYPES = /* @__PURE__ */ new Set(["aac", "mp4", "m4a", "ogg", "webm", "mp3", "wav"]);
|
|
136
|
+
function extractVoiceTranscript(file) {
|
|
137
|
+
const isAudio = file.subtype === "slack_audio" || AUDIO_TYPES.has(file.filetype ?? "");
|
|
138
|
+
if (!isAudio) return null;
|
|
139
|
+
const transcript = file.transcription?.preview?.content ?? (typeof file.preview === "string" ? file.preview : null);
|
|
140
|
+
return transcript ?? null;
|
|
141
|
+
}
|
|
142
|
+
function parseSlackEvent(event) {
|
|
143
|
+
const id = event.client_msg_id ?? event.ts ?? event.event_ts ?? `slack-${Date.now()}`;
|
|
144
|
+
const channel = event.channel ?? "unknown";
|
|
145
|
+
const from = event.user ?? "unknown";
|
|
146
|
+
const threadId = event.thread_ts !== void 0 ? event.thread_ts : void 0;
|
|
147
|
+
const receivedAt = event.ts ? new Date(parseFloat(event.ts) * 1e3).toISOString() : (/* @__PURE__ */ new Date()).toISOString();
|
|
148
|
+
let body = event.text ?? "";
|
|
149
|
+
if (event.subtype === "file_share" && event.files?.length) {
|
|
150
|
+
for (const file of event.files) {
|
|
151
|
+
const transcript = extractVoiceTranscript(file);
|
|
152
|
+
if (transcript) {
|
|
153
|
+
body = body ? `${body}
|
|
154
|
+
|
|
155
|
+
[Voice message] ${transcript}` : `[Voice message] ${transcript}`;
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (event.subtype === "file_share" && event.files?.length && !body) {
|
|
161
|
+
const hasAudio = event.files.some(
|
|
162
|
+
(f) => f.subtype === "slack_audio" || AUDIO_TYPES.has(f.filetype ?? "")
|
|
163
|
+
);
|
|
164
|
+
if (hasAudio) {
|
|
165
|
+
body = "[Voice message \u2014 no transcript available]";
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const attachments = [];
|
|
169
|
+
if (event.subtype === "file_share" && event.files?.length) {
|
|
170
|
+
for (const file of event.files) {
|
|
171
|
+
const isAudio = file.subtype === "slack_audio" || AUDIO_TYPES.has(file.filetype ?? "");
|
|
172
|
+
if (isAudio) continue;
|
|
173
|
+
attachments.push({
|
|
174
|
+
type: slackFiletypeToMediaType(file.filetype),
|
|
175
|
+
url: file.url_private,
|
|
176
|
+
mimeType: file.mimetype,
|
|
177
|
+
filename: file.name ?? file.title,
|
|
178
|
+
size: file.size
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
const urls = extractUrls(body);
|
|
183
|
+
for (const url of urls) {
|
|
184
|
+
attachments.push({ type: "link", url });
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
id,
|
|
188
|
+
channel,
|
|
189
|
+
from,
|
|
190
|
+
body,
|
|
191
|
+
threadId,
|
|
192
|
+
attachments: attachments.length > 0 ? attachments : void 0,
|
|
193
|
+
receivedAt,
|
|
194
|
+
raw: event
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
async function enrichVoiceMessage(msg, botToken) {
|
|
198
|
+
if (!msg.body.includes("[Voice message \u2014 no transcript available]")) return msg;
|
|
199
|
+
const raw = msg.raw;
|
|
200
|
+
const files = raw?.files;
|
|
201
|
+
if (!files?.length) return msg;
|
|
202
|
+
const audioFile = files.find(
|
|
203
|
+
(f) => f.subtype === "slack_audio" || AUDIO_TYPES.has(f.filetype ?? "")
|
|
204
|
+
);
|
|
205
|
+
if (!audioFile?.url_private) return msg;
|
|
206
|
+
const buffer = await downloadAudio(audioFile.url_private, botToken);
|
|
207
|
+
if (!buffer) return msg;
|
|
208
|
+
const filename = audioFile.name ?? `voice.${audioFile.filetype ?? "aac"}`;
|
|
209
|
+
const transcript = await transcribeAudio(buffer, filename);
|
|
210
|
+
if (!transcript) return msg;
|
|
211
|
+
return {
|
|
212
|
+
...msg,
|
|
213
|
+
body: `[Voice message] ${transcript}`
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export {
|
|
218
|
+
transcribeAudio,
|
|
219
|
+
downloadAudio,
|
|
220
|
+
extractVoiceTranscript,
|
|
221
|
+
parseSlackEvent,
|
|
222
|
+
enrichVoiceMessage
|
|
223
|
+
};
|
|
@@ -0,0 +1,389 @@
|
|
|
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: "full"
|
|
303
|
+
});
|
|
304
|
+
const msg = res.data;
|
|
305
|
+
const headers = msg.payload?.headers ?? [];
|
|
306
|
+
const getHeader = (name) => headers.find((h) => h.name.toLowerCase() === name.toLowerCase())?.value ?? "";
|
|
307
|
+
return {
|
|
308
|
+
gmailId: msg.id,
|
|
309
|
+
threadId: msg.threadId,
|
|
310
|
+
account: this.config.account,
|
|
311
|
+
subject: getHeader("Subject"),
|
|
312
|
+
from: parseAddress(getHeader("From")),
|
|
313
|
+
to: parseAddressList(getHeader("To")),
|
|
314
|
+
cc: parseAddressList(getHeader("Cc")),
|
|
315
|
+
bcc: parseAddressList(getHeader("Bcc")),
|
|
316
|
+
date: new Date(getHeader("Date")).toISOString(),
|
|
317
|
+
snippet: msg.snippet ?? "",
|
|
318
|
+
body: extractPlainTextBody(msg.payload),
|
|
319
|
+
labels: msg.labelIds ?? [],
|
|
320
|
+
isRead: !(msg.labelIds ?? []).includes("UNREAD")
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
function extractPlainTextBody(payload) {
|
|
325
|
+
if (!payload) return void 0;
|
|
326
|
+
if (payload.mimeType === "text/plain" && payload.body?.data) {
|
|
327
|
+
return decodeBase64Url(payload.body.data);
|
|
328
|
+
}
|
|
329
|
+
if (payload.parts) {
|
|
330
|
+
for (const part of payload.parts) {
|
|
331
|
+
if (part.mimeType === "text/plain" && part.body?.data) {
|
|
332
|
+
return decodeBase64Url(part.body.data);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
for (const part of payload.parts) {
|
|
336
|
+
if (part.mimeType?.startsWith("multipart/")) {
|
|
337
|
+
const result = extractPlainTextBody(part);
|
|
338
|
+
if (result) return result;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
return void 0;
|
|
343
|
+
}
|
|
344
|
+
function decodeBase64Url(data) {
|
|
345
|
+
const base64 = data.replace(/-/g, "+").replace(/_/g, "/");
|
|
346
|
+
return Buffer.from(base64, "base64").toString("utf-8");
|
|
347
|
+
}
|
|
348
|
+
function parseAddress(raw) {
|
|
349
|
+
const match = raw.match(/^(.+?)\s*<([^>]+)>$/);
|
|
350
|
+
if (match) {
|
|
351
|
+
return { name: match[1].replace(/^["']|["']$/g, "").trim(), email: match[2] };
|
|
352
|
+
}
|
|
353
|
+
return { email: raw.trim() };
|
|
354
|
+
}
|
|
355
|
+
function parseAddressList(raw) {
|
|
356
|
+
if (!raw.trim()) return [];
|
|
357
|
+
const results = [];
|
|
358
|
+
let current = "";
|
|
359
|
+
let depth = 0;
|
|
360
|
+
for (const ch of raw) {
|
|
361
|
+
if (ch === "<") depth++;
|
|
362
|
+
else if (ch === ">") depth--;
|
|
363
|
+
else if (ch === "," && depth === 0) {
|
|
364
|
+
if (current.trim()) results.push(parseAddress(current.trim()));
|
|
365
|
+
current = "";
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
current += ch;
|
|
369
|
+
}
|
|
370
|
+
if (current.trim()) results.push(parseAddress(current.trim()));
|
|
371
|
+
return results;
|
|
372
|
+
}
|
|
373
|
+
function formatAddress(addr) {
|
|
374
|
+
return addr.name ? `${addr.name} <${addr.email}>` : addr.email;
|
|
375
|
+
}
|
|
376
|
+
function errorMessage(err) {
|
|
377
|
+
return err instanceof Error ? err.message : String(err);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
export {
|
|
381
|
+
createOAuth2Client,
|
|
382
|
+
getAuthUrl,
|
|
383
|
+
exchangeCode,
|
|
384
|
+
createServiceAccountClient,
|
|
385
|
+
loadTokens,
|
|
386
|
+
saveTokens,
|
|
387
|
+
refreshIfNeeded,
|
|
388
|
+
GoogleGmailConnector
|
|
389
|
+
};
|
package/dist/cli.js
CHANGED
|
File without changes
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { C as ChannelAdapter, H as HealthStatus, I as InboundMessage } from './channel-
|
|
2
|
-
export { A as Attachment, a as
|
|
3
|
-
import { T as TokenUsage, L as LLMProvider, M as ModelInfo, R as ResolvedModel, C as ChatMessage } from './provider-
|
|
4
|
-
export { a as ChatParams, b as ChatResult, c as ContentBlock, d as ToolUse } from './provider-
|
|
1
|
+
import { C as ChannelAdapter, H as HealthStatus, I as InboundMessage } from './channel-DziSPayj.js';
|
|
2
|
+
export { A as Attachment, a as AttachmentMediaType, b as ChannelCapabilities, c as ChannelConfig, d as ChannelMeta, e as ChatType, F as FormattingMode, O as OutboundPayload, S as SendResult } from './channel-DziSPayj.js';
|
|
3
|
+
import { T as TokenUsage, L as LLMProvider, M as ModelInfo, R as ResolvedModel, C as ChatMessage } from './provider-BHkqkSdq.js';
|
|
4
|
+
export { a as ChatParams, b as ChatResult, c as ContentBlock, d as ToolUse } from './provider-BHkqkSdq.js';
|
|
5
5
|
import { C as ConnectorConfig } from './connector-B4Mj0P1b.js';
|
|
6
6
|
export { A as AuthResult, a as Connector, b as ConnectorMeta, P as PushResult, S as SyncOptions, c as SyncResult } from './connector-B4Mj0P1b.js';
|
|
7
|
-
import { C as ChatResponderConfig, D as DataStore, H as HookBus, M as MessageStore, a as ChatResponder, b as MessageInterpreter, E as Extractor } from './chat-pipeline-
|
|
8
|
-
export { c as ChatPipeline, d as ChatPipelineConfig, e as DataStoreError, f as EntityContextDef, g as EntityFileSpec, h as EntitySource, i as ExtractedFile, j as ExtractedMemory, k as ExtractedTask, l as ExtractedUserContext, F as Filter, m as HookHandler, n as HookOptions, o as HookRegistration, I as InterpretationResult, L as LLMCallFn, p as MessageInterpreterConfig, P as PkLookup, Q as QueryOptions, R as RelationDef, q as RoutingDecision, r as RoutingRule, s as Row, S as SeedItem, t as SqliteAdapter, u as StoreResult, v as StoredAttachment, T as TableDefinition, w as TableInfoRow, x as TriageRouter, y as TriageRouterConfig, U as Unsubscribe } from './chat-pipeline-
|
|
7
|
+
import { C as ChatResponderConfig, D as DataStore, H as HookBus, M as MessageStore, a as ChatResponder, b as MessageInterpreter, E as Extractor } from './chat-pipeline-BGgmH_ap.js';
|
|
8
|
+
export { c as ChatPipeline, d as ChatPipelineConfig, e as DataStoreError, f as EntityContextDef, g as EntityFileSpec, h as EntitySource, i as ExtractedFile, j as ExtractedMemory, k as ExtractedTask, l as ExtractedUserContext, F as Filter, m as HookHandler, n as HookOptions, o as HookRegistration, I as InterpretationResult, L as LLMCallFn, p as MessageInterpreterConfig, P as PkLookup, Q as QueryOptions, R as RelationDef, q as RoutingDecision, r as RoutingRule, s as Row, S as SeedItem, t as SqliteAdapter, u as StoreResult, v as StoredAttachment, T as TableDefinition, w as TableInfoRow, x as TriageRouter, y as TriageRouterConfig, U as Unsubscribe } from './chat-pipeline-BGgmH_ap.js';
|
|
9
9
|
import 'better-sqlite3';
|
|
10
10
|
|
|
11
11
|
/** Execution adapter types — Story 1.5 / 3.4 / 3.5 */
|