@datasynx/agentic-crm 1.6.0 → 1.7.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/dist/cli.js +4 -65
- package/dist/cli.js.map +1 -1
- package/dist/daemon/worker.js +24 -1
- package/dist/daemon/worker.js.map +1 -1
- package/dist/{imap-o6PRuBvm.js → imap-BRgNh3T3.js} +2 -2
- package/dist/{imap-o6PRuBvm.js.map → imap-BRgNh3T3.js.map} +1 -1
- package/dist/imap-DzeqMdZ3.js +2 -0
- package/dist/{index-DNHsURo5.d.cts → index-DcDaz_cu.d.cts} +3 -3
- package/dist/{index-DNHsURo5.d.cts.map → index-DcDaz_cu.d.cts.map} +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/{login-CYgla6-A.js → login-yt9OOQQk.js} +3 -2
- package/dist/{login-CYgla6-A.js.map → login-yt9OOQQk.js.map} +1 -1
- package/dist/mailbox-config-Bu-J1O4I.js +2 -0
- package/dist/mailbox-config-Dn2xTn9N.js +67 -0
- package/dist/mailbox-config-Dn2xTn9N.js.map +1 -0
- package/dist/mailbox-poll-B8dvFAXT.js +80 -0
- package/dist/mailbox-poll-B8dvFAXT.js.map +1 -0
- package/dist/{microsoft-DgbVlHdT.js → microsoft-dsC1fQQG.js} +2 -38
- package/dist/microsoft-dsC1fQQG.js.map +1 -0
- package/dist/{token-resolver-BRLOmRvF.js → token-resolver-D98qPOOf.js} +3 -2
- package/dist/{token-resolver-BRLOmRvF.js.map → token-resolver-D98qPOOf.js.map} +1 -1
- package/dist/token-store-B0h0USqe.js +43 -0
- package/dist/token-store-B0h0USqe.js.map +1 -0
- package/dist/token-store-CEmz8d-0.js +2 -0
- package/package.json +1 -1
- package/dist/microsoft-DgbVlHdT.js.map +0 -1
package/dist/daemon/worker.js
CHANGED
|
@@ -173,8 +173,31 @@ async function takeDailySnapshot() {
|
|
|
173
173
|
logger.error("daemon", "snapshot failed", { error: err.message });
|
|
174
174
|
}
|
|
175
175
|
}
|
|
176
|
-
|
|
176
|
+
/**
|
|
177
|
+
* Poll every configured mailbox (stored OAuth accounts + env IMAP) and
|
|
178
|
+
* auto-route new mail to customers by domain. The window overlaps the interval
|
|
179
|
+
* so nothing is missed; dedup keeps it idempotent.
|
|
180
|
+
*/
|
|
181
|
+
async function pollMailboxes(intervalMin) {
|
|
182
|
+
try {
|
|
183
|
+
const { listMailboxTokens } = await import("../token-store-CEmz8d-0.js");
|
|
184
|
+
const { imapConfigFromEnv } = await import("../mailbox-config-Bu-J1O4I.js");
|
|
185
|
+
if (listMailboxTokens(DATA_DIR).length === 0 && imapConfigFromEnv() === null) return;
|
|
186
|
+
const { runMailboxPollCycle } = await import("../mailbox-poll-B8dvFAXT.js");
|
|
187
|
+
const result = await runMailboxPollCycle(DATA_DIR, /* @__PURE__ */ new Date(Date.now() - (intervalMin + 5) * 60 * 1e3));
|
|
188
|
+
if (result.synced > 0) logger.info("daemon", "mailbox cycle", {
|
|
189
|
+
accounts: result.accounts,
|
|
190
|
+
synced: result.synced,
|
|
191
|
+
unrouted: result.unrouted
|
|
192
|
+
});
|
|
193
|
+
} catch (err) {
|
|
194
|
+
logger.error("daemon", "mailbox poll cycle failed", { error: err.message });
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const daemonIntervalMin = Math.max(1, parseInt(process.env["DXCRM_DAEMON_INTERVAL"] ?? "30", 10) || 30);
|
|
198
|
+
new CronJob(`*/${daemonIntervalMin} * * * *`, async () => {
|
|
177
199
|
await syncAllCustomers();
|
|
200
|
+
await pollMailboxes(daemonIntervalMin);
|
|
178
201
|
await checkAgentWakeTriggers().catch((err) => {
|
|
179
202
|
logger.error("daemon", "wake trigger check failed", { error: err.message });
|
|
180
203
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker.js","names":[],"sources":["../../src/daemon/worker.ts"],"sourcesContent":["// src/daemon/worker.ts\n// Standalone detached process — started by `dxcrm daemon start`\n// Handles background Gmail sync + transcript watching via cron\nimport { CronJob } from \"cron\";\nimport fs from \"fs\";\nimport path from \"path\";\nimport { logger } from \"../core/logger.js\";\nimport { writeJsonFile } from \"../fs/json-store.js\";\n\nconst DATA_DIR = process.env[\"DXCRM_DATA_DIR\"] ?? process.cwd();\n\nconst MAX_CUSTOMERS_PER_CYCLE = 50;\n\nasync function syncWithBackoff(fn: () => Promise<void>, maxRetries = 3): Promise<void> {\n for (let attempt = 0; attempt < maxRetries; attempt++) {\n try {\n await fn();\n return;\n } catch (err) {\n const msg = (err as Error).message;\n if (msg.includes(\"429\") || msg.includes(\"rateLimitExceeded\")) {\n const delay = Math.pow(2, attempt) * 2000; // 2s, 4s, 8s\n logger.warn(\"daemon\", \"rate limit, retrying\", { delayMs: delay });\n await new Promise((r) => setTimeout(r, delay));\n } else {\n throw err;\n }\n }\n }\n}\n\nasync function syncAllCustomers(): Promise<void> {\n const customersDir = path.join(DATA_DIR, \"customers\");\n if (!fs.existsSync(customersDir)) return;\n\n const slugs = fs.readdirSync(customersDir).filter((s) => {\n try {\n return fs.statSync(path.join(customersDir, s)).isDirectory();\n } catch {\n return false;\n }\n });\n\n const slugsToSync = slugs.slice(0, MAX_CUSTOMERS_PER_CYCLE);\n\n for (const slug of slugsToSync) {\n const sourcesPath = path.join(customersDir, slug, \"sources.json\");\n if (!fs.existsSync(sourcesPath)) continue;\n\n try {\n const sources = JSON.parse(fs.readFileSync(sourcesPath, \"utf-8\")) as {\n gmail?: { query?: string; enabled?: boolean };\n };\n\n if (sources.gmail?.enabled && sources.gmail.query) {\n // Gmail sync requires auth — skip if token not configured\n const tokenPath = path.join(DATA_DIR, \".agentic\", \"gmail-token.json\");\n const credPath = path.join(DATA_DIR, \".agentic\", \"gmail-credentials.json\");\n if (fs.existsSync(tokenPath) && fs.existsSync(credPath)) {\n const { getGmailAuth } = await import(\"../sync/gmail-auth.js\");\n const { syncGmail } = await import(\"../sync/gmail-sync.js\");\n const auth = await getGmailAuth(credPath, tokenPath);\n await syncWithBackoff(async () => {\n const result = await syncGmail({\n slug,\n dataDir: DATA_DIR,\n auth,\n query: sources.gmail!.query!,\n since: new Date(Date.now() - 30 * 60 * 1000), // last 30 min\n });\n if (result.synced > 0) {\n logger.info(\"daemon\", \"synced emails\", { slug, synced: result.synced });\n }\n // Update sync state after each successful customer sync\n const { updateSlugSyncState } = await import(\"../fs/sync-state.js\");\n updateSlugSyncState(DATA_DIR, slug, { lastGmailSync: new Date().toISOString() });\n });\n }\n }\n } catch (err) {\n logger.error(\"daemon\", \"error syncing customer\", { slug, error: (err as Error).message });\n }\n }\n}\n\n// Start transcript watcher\nasync function startWatcher(): Promise<void> {\n const agenticSourcesPath = path.join(DATA_DIR, \".agentic\", \"sources.json\");\n if (!fs.existsSync(agenticSourcesPath)) return;\n\n try {\n const sources = JSON.parse(fs.readFileSync(agenticSourcesPath, \"utf-8\")) as {\n transcripts?: { paths?: string[]; extensions?: string[]; enabled?: boolean };\n };\n\n if (sources.transcripts?.enabled && sources.transcripts.paths?.length) {\n const { watchTranscripts, processTranscriptFileAutoMatch } =\n await import(\"../sync/transcript-watcher.js\");\n watchTranscripts({\n paths: sources.transcripts.paths,\n extensions: sources.transcripts.extensions ?? [\".txt\", \".vtt\"],\n dataDir: DATA_DIR,\n onFile: (filePath) => processTranscriptFileAutoMatch(filePath, DATA_DIR),\n });\n logger.info(\"daemon\", \"watching transcripts (LLM auto-match)\");\n }\n } catch (err) {\n logger.error(\"daemon\", \"watcher error\", { error: (err as Error).message });\n }\n}\n\nasync function checkAgentWakeTriggers(): Promise<void> {\n const agentsDir = path.join(DATA_DIR, \".agentic\", \"agents\");\n if (!fs.existsSync(agentsDir)) return;\n\n const files = fs.readdirSync(agentsDir).filter((f) => f.endsWith(\".agent.json\"));\n\n for (const file of files) {\n try {\n const config = JSON.parse(fs.readFileSync(path.join(agentsDir, file), \"utf-8\") as string) as {\n slug: string;\n channel: string;\n wakeOn: string[];\n lastWake: string | null;\n telegramChatId?: string;\n };\n\n if (!config.wakeOn.includes(\"email\")) continue;\n\n const { getLastGmailSync } = await import(\"../fs/sync-state.js\");\n const lastSync = getLastGmailSync(DATA_DIR, config.slug);\n const lastWake = config.lastWake ? new Date(config.lastWake) : null;\n\n if (!lastSync) continue;\n if (lastWake && lastSync <= lastWake) continue;\n\n // New email since last wake — build context and send notification\n logger.info(\"daemon\", \"wake trigger\", { slug: config.slug });\n\n const { buildContext } = await import(\"../core/context-builder.js\");\n const context = await buildContext(DATA_DIR, config.slug).catch(() => null);\n if (!context) continue;\n\n if (\n config.channel === \"telegram\" &&\n process.env[\"TELEGRAM_BOT_TOKEN\"] &&\n (config.telegramChatId ?? process.env[\"TELEGRAM_CHAT_ID\"])\n ) {\n const chatId = config.telegramChatId ?? process.env[\"TELEGRAM_CHAT_ID\"]!;\n const token = process.env[\"TELEGRAM_BOT_TOKEN\"];\n const message = `📬 New activity: *${config.slug}*\\n\\n${context.slice(0, 800)}`;\n\n try {\n const { default: https } = await import(\"https\");\n const body = JSON.stringify({ chat_id: chatId, text: message, parse_mode: \"Markdown\" });\n await new Promise<void>((resolve, reject) => {\n const req = https.request(\n `https://api.telegram.org/bot${token}/sendMessage`,\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"Content-Length\": Buffer.byteLength(body),\n },\n },\n (res) => {\n res.resume();\n resolve();\n }\n );\n req.on(\"error\", reject);\n req.write(body);\n req.end();\n });\n logger.info(\"daemon\", \"telegram sent\", { slug: config.slug });\n } catch (err) {\n logger.error(\"daemon\", \"telegram failed\", { error: (err as Error).message });\n }\n }\n\n // Update lastWake\n config.lastWake = new Date().toISOString();\n writeJsonFile(path.join(agentsDir, file), config);\n } catch (err) {\n logger.error(\"daemon\", \"agent check error\", { file, error: (err as Error).message });\n }\n }\n}\n\n/**\n * Self-healing: each cycle, clean orphaned atomic-write temp files (a crash\n * signature) and log any *failed* health checks (e.g. invalid customer data).\n * Warn-level checks (log errors, stale backups) are intentionally not re-logged\n * here to avoid a feedback loop — `dxcrm doctor` surfaces those on demand.\n */\nasync function runSelfHeal(): Promise<void> {\n try {\n const { runDiagnostics, cleanupTempFiles } = await import(\"../core/doctor.js\");\n const removed = cleanupTempFiles(DATA_DIR);\n if (removed.length > 0) {\n logger.warn(\"daemon\", \"self-heal: removed orphaned temp files\", { count: removed.length });\n }\n const report = await runDiagnostics(DATA_DIR);\n for (const check of report.checks) {\n if (check.status === \"fail\") {\n logger.error(\"daemon\", `self-check failed: ${check.name}`, { detail: check.detail });\n }\n }\n } catch (err) {\n logger.error(\"daemon\", \"self-heal failed\", { error: (err as Error).message });\n }\n}\n\n/** Take a pipeline snapshot once per day (self-populating time-travel history). */\nasync function takeDailySnapshot(): Promise<void> {\n try {\n const { listSnapshots, takeSnapshot } = await import(\"../core/snapshots.js\");\n const today = new Date().toISOString().slice(0, 10);\n if (listSnapshots(DATA_DIR).some((s) => s.id === today)) return; // already taken today\n const snap = takeSnapshot(DATA_DIR, today);\n logger.info(\"daemon\", \"pipeline snapshot taken\", { id: snap.id, deals: snap.deals.length });\n } catch (err) {\n logger.error(\"daemon\", \"snapshot failed\", { error: (err as Error).message });\n }\n}\n\n// Gmail sync — interval configurable via DXCRM_DAEMON_INTERVAL (minutes, default 30)\nconst daemonIntervalMin = Math.max(\n 1,\n parseInt(process.env[\"DXCRM_DAEMON_INTERVAL\"] ?? \"30\", 10) || 30\n);\nnew CronJob(\n `*/${daemonIntervalMin} * * * *`,\n async () => {\n await syncAllCustomers();\n await checkAgentWakeTriggers().catch((err: unknown) => {\n logger.error(\"daemon\", \"wake trigger check failed\", { error: (err as Error).message });\n });\n await runSelfHeal();\n await takeDailySnapshot();\n },\n null,\n true,\n undefined,\n null,\n false,\n undefined,\n false, // unrefTimeout — keep event loop alive\n true // waitForCompletion\n);\n\n// Scheduled backup check — hourly, runs backup if >1 day since last\nnew CronJob(\n \"*/60 * * * *\",\n async () => {\n try {\n const { runScheduledBackupIfDue } = await import(\"../commands/backup.js\");\n await runScheduledBackupIfDue(DATA_DIR);\n } catch (err) {\n logger.error(\"daemon\", \"backup check error\", { error: (err as Error).message });\n }\n },\n null,\n true,\n undefined,\n null,\n false,\n undefined,\n false, // unrefTimeout — keep event loop alive\n true // waitForCompletion\n);\n\n// Daily push subscription renewal at 06:00\nnew CronJob(\n \"0 6 * * *\",\n async () => {\n try {\n const { renewExpiringSubscriptions } = await import(\"../sync/push-manager.js\");\n const { buildGmailRenewFn } = await import(\"../sync/gmail-webhook-handler.js\");\n const tokenPath = path.join(DATA_DIR, \".agentic\", \"gmail-token.json\");\n const credPath = path.join(DATA_DIR, \".agentic\", \"gmail-credentials.json\");\n const { readSubscriptions } = await import(\"../sync/push-manager.js\");\n const subs = await readSubscriptions(DATA_DIR);\n const gmailSubs = subs.filter((s) => s.provider === \"gmail\" && s.status === \"active\");\n if (gmailSubs.length === 0) return;\n if (!fs.existsSync(tokenPath) || !fs.existsSync(credPath)) return;\n const { getGmailAuth } = await import(\"../sync/gmail-auth.js\");\n const auth = await getGmailAuth(credPath, tokenPath);\n const token = (auth.credentials?.access_token as string | undefined) ?? \"\";\n const result = await renewExpiringSubscriptions(DATA_DIR, buildGmailRenewFn(token, \"\"), 24);\n if (result.renewed.length > 0) {\n logger.info(\"push\", \"renewed subscriptions\", { count: result.renewed.length });\n }\n if (result.errors.length > 0) {\n logger.warn(\"push\", \"renewal errors\", { errors: result.errors });\n }\n } catch (err) {\n logger.error(\"push\", \"renewal failed\", { error: (err as Error).message });\n }\n },\n null,\n true,\n undefined,\n null,\n false,\n undefined,\n false, // unrefTimeout — keep event loop alive\n true // waitForCompletion\n);\n\n// Daily proactive checks at 07:00 — relationship decay, deal risk, daily briefing\nnew CronJob(\n \"0 7 * * *\",\n async () => {\n try {\n const { runDailyProactiveChecks } = await import(\"../daemon/proactive-worker.js\");\n const result = await runDailyProactiveChecks(DATA_DIR);\n logger.info(\"proactive\", \"daily check\", {\n customersChecked: result.customersChecked,\n tasksEnqueued: result.tasksEnqueued,\n });\n if (result.errors.length > 0) {\n logger.warn(\"proactive\", \"errors during daily check\", { errors: result.errors });\n }\n const { drainProactiveQueue } = await import(\"../core/notification-dispatcher.js\");\n const drain = await drainProactiveQueue(DATA_DIR);\n logger.info(\"proactive\", \"dispatched tasks\", { sent: drain.sent, failed: drain.failed });\n const { syncGoalProgressFromPipeline } = await import(\"../core/goal-engine.js\");\n const goalSync = await syncGoalProgressFromPipeline(DATA_DIR);\n if (goalSync.updated.length > 0) {\n logger.info(\"goals\", \"progress synced\", { updated: goalSync.updated });\n }\n } catch (err) {\n logger.error(\"proactive\", \"daily check failed\", { error: (err as Error).message });\n }\n },\n null,\n true,\n undefined,\n null,\n false,\n undefined,\n false, // unrefTimeout — keep event loop alive\n true // waitForCompletion\n);\n\n// SLA breach check — daily at 08:00\nnew CronJob(\n \"0 8 * * *\",\n async () => {\n try {\n const { checkSlaBreaches } = await import(\"../core/sla-engine.js\");\n const today = new Date().toISOString().slice(0, 10);\n const breaches = await checkSlaBreaches(DATA_DIR, today);\n if (breaches.length > 0) {\n logger.warn(\"tickets\", \"SLA breaches found\", { count: breaches.length });\n for (const { slug, ticket } of breaches) {\n logger.warn(\"tickets\", \"SLA breach\", {\n slug,\n ticketId: ticket.id,\n title: ticket.title,\n due: ticket.slaDue,\n });\n }\n }\n } catch (err) {\n logger.error(\"tickets\", \"SLA check failed\", { error: (err as Error).message });\n }\n },\n null,\n true,\n undefined,\n null,\n false,\n undefined,\n false, // unrefTimeout — keep event loop alive\n true // waitForCompletion\n);\n\n// Email sequence cycle — every 6 hours\nnew CronJob(\n \"0 */6 * * *\",\n async () => {\n try {\n const { runSequenceCycle } = await import(\"../core/sequence-engine.js\");\n const today = new Date().toISOString().slice(0, 10);\n const result = await runSequenceCycle(DATA_DIR, today);\n logger.info(\"sequences\", \"cycle complete\", {\n sent: result.sent,\n completed: result.completed,\n errors: result.errors.length,\n });\n } catch (err) {\n logger.error(\"sequences\", \"cycle failed\", { error: (err as Error).message });\n }\n },\n null,\n true,\n undefined,\n null,\n false,\n undefined,\n false, // unrefTimeout — keep event loop alive\n true // waitForCompletion\n);\n\nawait startWatcher();\n\n// Signal ready\nif (process.send) process.send(\"ready\");\nlogger.info(\"daemon\", \"daemon started\");\n"],"mappings":";;;;;;AASA,MAAM,WAAW,QAAQ,IAAI,qBAAqB,QAAQ,IAAI;AAE9D,MAAM,0BAA0B;AAEhC,eAAe,gBAAgB,IAAyB,aAAa,GAAkB;CACrF,KAAK,IAAI,UAAU,GAAG,UAAU,YAAY,WAC1C,IAAI;EACF,MAAM,GAAG;EACT;CACF,SAAS,KAAK;EACZ,MAAM,MAAO,IAAc;EAC3B,IAAI,IAAI,SAAS,KAAK,KAAK,IAAI,SAAS,mBAAmB,GAAG;GAC5D,MAAM,QAAQ,KAAK,IAAI,GAAG,OAAO,IAAI;GACrC,OAAO,KAAK,UAAU,wBAAwB,EAAE,SAAS,MAAM,CAAC;GAChE,MAAM,IAAI,SAAS,MAAM,WAAW,GAAG,KAAK,CAAC;EAC/C,OACE,MAAM;CAEV;AAEJ;AAEA,eAAe,mBAAkC;CAC/C,MAAM,eAAe,KAAK,KAAK,UAAU,WAAW;CACpD,IAAI,CAAC,GAAG,WAAW,YAAY,GAAG;CAUlC,MAAM,cARQ,GAAG,YAAY,YAAY,EAAE,QAAQ,MAAM;EACvD,IAAI;GACF,OAAO,GAAG,SAAS,KAAK,KAAK,cAAc,CAAC,CAAC,EAAE,YAAY;EAC7D,QAAQ;GACN,OAAO;EACT;CACF,CAEwB,EAAE,MAAM,GAAG,uBAAuB;CAE1D,KAAK,MAAM,QAAQ,aAAa;EAC9B,MAAM,cAAc,KAAK,KAAK,cAAc,MAAM,cAAc;EAChE,IAAI,CAAC,GAAG,WAAW,WAAW,GAAG;EAEjC,IAAI;GACF,MAAM,UAAU,KAAK,MAAM,GAAG,aAAa,aAAa,OAAO,CAAC;GAIhE,IAAI,QAAQ,OAAO,WAAW,QAAQ,MAAM,OAAO;IAEjD,MAAM,YAAY,KAAK,KAA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ACtC,OAAO,KAAK,UAAU,gBAAgB"}
|
|
1
|
+
{"version":3,"file":"worker.js","names":[],"sources":["../../src/daemon/worker.ts"],"sourcesContent":["// src/daemon/worker.ts\n// Standalone detached process — started by `dxcrm daemon start`\n// Handles background Gmail sync + transcript watching via cron\nimport { CronJob } from \"cron\";\nimport fs from \"fs\";\nimport path from \"path\";\nimport { logger } from \"../core/logger.js\";\nimport { writeJsonFile } from \"../fs/json-store.js\";\n\nconst DATA_DIR = process.env[\"DXCRM_DATA_DIR\"] ?? process.cwd();\n\nconst MAX_CUSTOMERS_PER_CYCLE = 50;\n\nasync function syncWithBackoff(fn: () => Promise<void>, maxRetries = 3): Promise<void> {\n for (let attempt = 0; attempt < maxRetries; attempt++) {\n try {\n await fn();\n return;\n } catch (err) {\n const msg = (err as Error).message;\n if (msg.includes(\"429\") || msg.includes(\"rateLimitExceeded\")) {\n const delay = Math.pow(2, attempt) * 2000; // 2s, 4s, 8s\n logger.warn(\"daemon\", \"rate limit, retrying\", { delayMs: delay });\n await new Promise((r) => setTimeout(r, delay));\n } else {\n throw err;\n }\n }\n }\n}\n\nasync function syncAllCustomers(): Promise<void> {\n const customersDir = path.join(DATA_DIR, \"customers\");\n if (!fs.existsSync(customersDir)) return;\n\n const slugs = fs.readdirSync(customersDir).filter((s) => {\n try {\n return fs.statSync(path.join(customersDir, s)).isDirectory();\n } catch {\n return false;\n }\n });\n\n const slugsToSync = slugs.slice(0, MAX_CUSTOMERS_PER_CYCLE);\n\n for (const slug of slugsToSync) {\n const sourcesPath = path.join(customersDir, slug, \"sources.json\");\n if (!fs.existsSync(sourcesPath)) continue;\n\n try {\n const sources = JSON.parse(fs.readFileSync(sourcesPath, \"utf-8\")) as {\n gmail?: { query?: string; enabled?: boolean };\n };\n\n if (sources.gmail?.enabled && sources.gmail.query) {\n // Gmail sync requires auth — skip if token not configured\n const tokenPath = path.join(DATA_DIR, \".agentic\", \"gmail-token.json\");\n const credPath = path.join(DATA_DIR, \".agentic\", \"gmail-credentials.json\");\n if (fs.existsSync(tokenPath) && fs.existsSync(credPath)) {\n const { getGmailAuth } = await import(\"../sync/gmail-auth.js\");\n const { syncGmail } = await import(\"../sync/gmail-sync.js\");\n const auth = await getGmailAuth(credPath, tokenPath);\n await syncWithBackoff(async () => {\n const result = await syncGmail({\n slug,\n dataDir: DATA_DIR,\n auth,\n query: sources.gmail!.query!,\n since: new Date(Date.now() - 30 * 60 * 1000), // last 30 min\n });\n if (result.synced > 0) {\n logger.info(\"daemon\", \"synced emails\", { slug, synced: result.synced });\n }\n // Update sync state after each successful customer sync\n const { updateSlugSyncState } = await import(\"../fs/sync-state.js\");\n updateSlugSyncState(DATA_DIR, slug, { lastGmailSync: new Date().toISOString() });\n });\n }\n }\n } catch (err) {\n logger.error(\"daemon\", \"error syncing customer\", { slug, error: (err as Error).message });\n }\n }\n}\n\n// Start transcript watcher\nasync function startWatcher(): Promise<void> {\n const agenticSourcesPath = path.join(DATA_DIR, \".agentic\", \"sources.json\");\n if (!fs.existsSync(agenticSourcesPath)) return;\n\n try {\n const sources = JSON.parse(fs.readFileSync(agenticSourcesPath, \"utf-8\")) as {\n transcripts?: { paths?: string[]; extensions?: string[]; enabled?: boolean };\n };\n\n if (sources.transcripts?.enabled && sources.transcripts.paths?.length) {\n const { watchTranscripts, processTranscriptFileAutoMatch } =\n await import(\"../sync/transcript-watcher.js\");\n watchTranscripts({\n paths: sources.transcripts.paths,\n extensions: sources.transcripts.extensions ?? [\".txt\", \".vtt\"],\n dataDir: DATA_DIR,\n onFile: (filePath) => processTranscriptFileAutoMatch(filePath, DATA_DIR),\n });\n logger.info(\"daemon\", \"watching transcripts (LLM auto-match)\");\n }\n } catch (err) {\n logger.error(\"daemon\", \"watcher error\", { error: (err as Error).message });\n }\n}\n\nasync function checkAgentWakeTriggers(): Promise<void> {\n const agentsDir = path.join(DATA_DIR, \".agentic\", \"agents\");\n if (!fs.existsSync(agentsDir)) return;\n\n const files = fs.readdirSync(agentsDir).filter((f) => f.endsWith(\".agent.json\"));\n\n for (const file of files) {\n try {\n const config = JSON.parse(fs.readFileSync(path.join(agentsDir, file), \"utf-8\") as string) as {\n slug: string;\n channel: string;\n wakeOn: string[];\n lastWake: string | null;\n telegramChatId?: string;\n };\n\n if (!config.wakeOn.includes(\"email\")) continue;\n\n const { getLastGmailSync } = await import(\"../fs/sync-state.js\");\n const lastSync = getLastGmailSync(DATA_DIR, config.slug);\n const lastWake = config.lastWake ? new Date(config.lastWake) : null;\n\n if (!lastSync) continue;\n if (lastWake && lastSync <= lastWake) continue;\n\n // New email since last wake — build context and send notification\n logger.info(\"daemon\", \"wake trigger\", { slug: config.slug });\n\n const { buildContext } = await import(\"../core/context-builder.js\");\n const context = await buildContext(DATA_DIR, config.slug).catch(() => null);\n if (!context) continue;\n\n if (\n config.channel === \"telegram\" &&\n process.env[\"TELEGRAM_BOT_TOKEN\"] &&\n (config.telegramChatId ?? process.env[\"TELEGRAM_CHAT_ID\"])\n ) {\n const chatId = config.telegramChatId ?? process.env[\"TELEGRAM_CHAT_ID\"]!;\n const token = process.env[\"TELEGRAM_BOT_TOKEN\"];\n const message = `📬 New activity: *${config.slug}*\\n\\n${context.slice(0, 800)}`;\n\n try {\n const { default: https } = await import(\"https\");\n const body = JSON.stringify({ chat_id: chatId, text: message, parse_mode: \"Markdown\" });\n await new Promise<void>((resolve, reject) => {\n const req = https.request(\n `https://api.telegram.org/bot${token}/sendMessage`,\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"Content-Length\": Buffer.byteLength(body),\n },\n },\n (res) => {\n res.resume();\n resolve();\n }\n );\n req.on(\"error\", reject);\n req.write(body);\n req.end();\n });\n logger.info(\"daemon\", \"telegram sent\", { slug: config.slug });\n } catch (err) {\n logger.error(\"daemon\", \"telegram failed\", { error: (err as Error).message });\n }\n }\n\n // Update lastWake\n config.lastWake = new Date().toISOString();\n writeJsonFile(path.join(agentsDir, file), config);\n } catch (err) {\n logger.error(\"daemon\", \"agent check error\", { file, error: (err as Error).message });\n }\n }\n}\n\n/**\n * Self-healing: each cycle, clean orphaned atomic-write temp files (a crash\n * signature) and log any *failed* health checks (e.g. invalid customer data).\n * Warn-level checks (log errors, stale backups) are intentionally not re-logged\n * here to avoid a feedback loop — `dxcrm doctor` surfaces those on demand.\n */\nasync function runSelfHeal(): Promise<void> {\n try {\n const { runDiagnostics, cleanupTempFiles } = await import(\"../core/doctor.js\");\n const removed = cleanupTempFiles(DATA_DIR);\n if (removed.length > 0) {\n logger.warn(\"daemon\", \"self-heal: removed orphaned temp files\", { count: removed.length });\n }\n const report = await runDiagnostics(DATA_DIR);\n for (const check of report.checks) {\n if (check.status === \"fail\") {\n logger.error(\"daemon\", `self-check failed: ${check.name}`, { detail: check.detail });\n }\n }\n } catch (err) {\n logger.error(\"daemon\", \"self-heal failed\", { error: (err as Error).message });\n }\n}\n\n/** Take a pipeline snapshot once per day (self-populating time-travel history). */\nasync function takeDailySnapshot(): Promise<void> {\n try {\n const { listSnapshots, takeSnapshot } = await import(\"../core/snapshots.js\");\n const today = new Date().toISOString().slice(0, 10);\n if (listSnapshots(DATA_DIR).some((s) => s.id === today)) return; // already taken today\n const snap = takeSnapshot(DATA_DIR, today);\n logger.info(\"daemon\", \"pipeline snapshot taken\", { id: snap.id, deals: snap.deals.length });\n } catch (err) {\n logger.error(\"daemon\", \"snapshot failed\", { error: (err as Error).message });\n }\n}\n\n/**\n * Poll every configured mailbox (stored OAuth accounts + env IMAP) and\n * auto-route new mail to customers by domain. The window overlaps the interval\n * so nothing is missed; dedup keeps it idempotent.\n */\nasync function pollMailboxes(intervalMin: number): Promise<void> {\n try {\n const { listMailboxTokens } = await import(\"../sync/oauth/token-store.js\");\n const { imapConfigFromEnv } = await import(\"../sync/mailbox-config.js\");\n if (listMailboxTokens(DATA_DIR).length === 0 && imapConfigFromEnv() === null) return;\n\n const { runMailboxPollCycle } = await import(\"./mailbox-poll.js\");\n const since = new Date(Date.now() - (intervalMin + 5) * 60 * 1000);\n const result = await runMailboxPollCycle(DATA_DIR, since);\n if (result.synced > 0) {\n logger.info(\"daemon\", \"mailbox cycle\", {\n accounts: result.accounts,\n synced: result.synced,\n unrouted: result.unrouted,\n });\n }\n } catch (err) {\n logger.error(\"daemon\", \"mailbox poll cycle failed\", { error: (err as Error).message });\n }\n}\n\n// Gmail sync — interval configurable via DXCRM_DAEMON_INTERVAL (minutes, default 30)\nconst daemonIntervalMin = Math.max(\n 1,\n parseInt(process.env[\"DXCRM_DAEMON_INTERVAL\"] ?? \"30\", 10) || 30\n);\nnew CronJob(\n `*/${daemonIntervalMin} * * * *`,\n async () => {\n await syncAllCustomers();\n await pollMailboxes(daemonIntervalMin);\n await checkAgentWakeTriggers().catch((err: unknown) => {\n logger.error(\"daemon\", \"wake trigger check failed\", { error: (err as Error).message });\n });\n await runSelfHeal();\n await takeDailySnapshot();\n },\n null,\n true,\n undefined,\n null,\n false,\n undefined,\n false, // unrefTimeout — keep event loop alive\n true // waitForCompletion\n);\n\n// Scheduled backup check — hourly, runs backup if >1 day since last\nnew CronJob(\n \"*/60 * * * *\",\n async () => {\n try {\n const { runScheduledBackupIfDue } = await import(\"../commands/backup.js\");\n await runScheduledBackupIfDue(DATA_DIR);\n } catch (err) {\n logger.error(\"daemon\", \"backup check error\", { error: (err as Error).message });\n }\n },\n null,\n true,\n undefined,\n null,\n false,\n undefined,\n false, // unrefTimeout — keep event loop alive\n true // waitForCompletion\n);\n\n// Daily push subscription renewal at 06:00\nnew CronJob(\n \"0 6 * * *\",\n async () => {\n try {\n const { renewExpiringSubscriptions } = await import(\"../sync/push-manager.js\");\n const { buildGmailRenewFn } = await import(\"../sync/gmail-webhook-handler.js\");\n const tokenPath = path.join(DATA_DIR, \".agentic\", \"gmail-token.json\");\n const credPath = path.join(DATA_DIR, \".agentic\", \"gmail-credentials.json\");\n const { readSubscriptions } = await import(\"../sync/push-manager.js\");\n const subs = await readSubscriptions(DATA_DIR);\n const gmailSubs = subs.filter((s) => s.provider === \"gmail\" && s.status === \"active\");\n if (gmailSubs.length === 0) return;\n if (!fs.existsSync(tokenPath) || !fs.existsSync(credPath)) return;\n const { getGmailAuth } = await import(\"../sync/gmail-auth.js\");\n const auth = await getGmailAuth(credPath, tokenPath);\n const token = (auth.credentials?.access_token as string | undefined) ?? \"\";\n const result = await renewExpiringSubscriptions(DATA_DIR, buildGmailRenewFn(token, \"\"), 24);\n if (result.renewed.length > 0) {\n logger.info(\"push\", \"renewed subscriptions\", { count: result.renewed.length });\n }\n if (result.errors.length > 0) {\n logger.warn(\"push\", \"renewal errors\", { errors: result.errors });\n }\n } catch (err) {\n logger.error(\"push\", \"renewal failed\", { error: (err as Error).message });\n }\n },\n null,\n true,\n undefined,\n null,\n false,\n undefined,\n false, // unrefTimeout — keep event loop alive\n true // waitForCompletion\n);\n\n// Daily proactive checks at 07:00 — relationship decay, deal risk, daily briefing\nnew CronJob(\n \"0 7 * * *\",\n async () => {\n try {\n const { runDailyProactiveChecks } = await import(\"../daemon/proactive-worker.js\");\n const result = await runDailyProactiveChecks(DATA_DIR);\n logger.info(\"proactive\", \"daily check\", {\n customersChecked: result.customersChecked,\n tasksEnqueued: result.tasksEnqueued,\n });\n if (result.errors.length > 0) {\n logger.warn(\"proactive\", \"errors during daily check\", { errors: result.errors });\n }\n const { drainProactiveQueue } = await import(\"../core/notification-dispatcher.js\");\n const drain = await drainProactiveQueue(DATA_DIR);\n logger.info(\"proactive\", \"dispatched tasks\", { sent: drain.sent, failed: drain.failed });\n const { syncGoalProgressFromPipeline } = await import(\"../core/goal-engine.js\");\n const goalSync = await syncGoalProgressFromPipeline(DATA_DIR);\n if (goalSync.updated.length > 0) {\n logger.info(\"goals\", \"progress synced\", { updated: goalSync.updated });\n }\n } catch (err) {\n logger.error(\"proactive\", \"daily check failed\", { error: (err as Error).message });\n }\n },\n null,\n true,\n undefined,\n null,\n false,\n undefined,\n false, // unrefTimeout — keep event loop alive\n true // waitForCompletion\n);\n\n// SLA breach check — daily at 08:00\nnew CronJob(\n \"0 8 * * *\",\n async () => {\n try {\n const { checkSlaBreaches } = await import(\"../core/sla-engine.js\");\n const today = new Date().toISOString().slice(0, 10);\n const breaches = await checkSlaBreaches(DATA_DIR, today);\n if (breaches.length > 0) {\n logger.warn(\"tickets\", \"SLA breaches found\", { count: breaches.length });\n for (const { slug, ticket } of breaches) {\n logger.warn(\"tickets\", \"SLA breach\", {\n slug,\n ticketId: ticket.id,\n title: ticket.title,\n due: ticket.slaDue,\n });\n }\n }\n } catch (err) {\n logger.error(\"tickets\", \"SLA check failed\", { error: (err as Error).message });\n }\n },\n null,\n true,\n undefined,\n null,\n false,\n undefined,\n false, // unrefTimeout — keep event loop alive\n true // waitForCompletion\n);\n\n// Email sequence cycle — every 6 hours\nnew CronJob(\n \"0 */6 * * *\",\n async () => {\n try {\n const { runSequenceCycle } = await import(\"../core/sequence-engine.js\");\n const today = new Date().toISOString().slice(0, 10);\n const result = await runSequenceCycle(DATA_DIR, today);\n logger.info(\"sequences\", \"cycle complete\", {\n sent: result.sent,\n completed: result.completed,\n errors: result.errors.length,\n });\n } catch (err) {\n logger.error(\"sequences\", \"cycle failed\", { error: (err as Error).message });\n }\n },\n null,\n true,\n undefined,\n null,\n false,\n undefined,\n false, // unrefTimeout — keep event loop alive\n true // waitForCompletion\n);\n\nawait startWatcher();\n\n// Signal ready\nif (process.send) process.send(\"ready\");\nlogger.info(\"daemon\", \"daemon started\");\n"],"mappings":";;;;;;AASA,MAAM,WAAW,QAAQ,IAAI,qBAAqB,QAAQ,IAAI;AAE9D,MAAM,0BAA0B;AAEhC,eAAe,gBAAgB,IAAyB,aAAa,GAAkB;CACrF,KAAK,IAAI,UAAU,GAAG,UAAU,YAAY,WAC1C,IAAI;EACF,MAAM,GAAG;EACT;CACF,SAAS,KAAK;EACZ,MAAM,MAAO,IAAc;EAC3B,IAAI,IAAI,SAAS,KAAK,KAAK,IAAI,SAAS,mBAAmB,GAAG;GAC5D,MAAM,QAAQ,KAAK,IAAI,GAAG,OAAO,IAAI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A,KAAA,GACA,OACA,IACF;AAEA,MAAM,aAAa;AAGnB,IAAI,QAAQ,MAAM,QAAQ,KAAK,OAAO;AACtC,OAAO,KAAK,UAAU,gBAAgB"}
|
|
@@ -265,6 +265,6 @@ function directionFor(msg, user) {
|
|
|
265
265
|
return (msg.from.match(/<([^>]+)>/)?.[1] ?? msg.from).toLowerCase() === user.toLowerCase() ? "outbound" : "inbound";
|
|
266
266
|
}
|
|
267
267
|
//#endregion
|
|
268
|
-
export { syncImapMailbox };
|
|
268
|
+
export { syncImapMailbox as n, normalizeParsedEmail as t };
|
|
269
269
|
|
|
270
|
-
//# sourceMappingURL=imap-
|
|
270
|
+
//# sourceMappingURL=imap-BRgNh3T3.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"imap-o6PRuBvm.js","names":[],"sources":["../src/sync/email-ingest.ts","../src/sync/email-router.ts","../src/sync/connectors/imap.ts"],"sourcesContent":["// src/sync/email-ingest.ts\nimport { appendInteraction } from \"../fs/interactions-writer.js\";\nimport { persistAttachment, DEFAULT_MAX_ATTACHMENT_BYTES } from \"./attachments.js\";\nimport { chunkText } from \"../core/chunk.js\";\nimport { logger } from \"../core/logger.js\";\n\n/** An attachment with its bytes already in hand (no provider fetch needed). */\nexport interface NormalizedAttachment {\n filename: string;\n mimeType: string;\n content: Buffer;\n}\n\n/**\n * A provider-independent email, normalized from Gmail / IMAP / Graph into a\n * single shape so the downstream pipeline (summary, attachments→Markdown,\n * indexing, interaction log) is written once and reused everywhere.\n */\nexport interface NormalizedEmail {\n /** Stable id for dedup and as the attachment filename prefix. */\n messageId: string;\n /** Raw `From` header value (display name + address). */\n from: string;\n /** Recipient addresses (to + cc), lowercased. */\n toAddresses: string[];\n subject: string;\n /** Message date as YYYY-MM-DD. */\n date: string;\n /** Body already rendered to Markdown (plain verbatim or HTML→MD). */\n bodyMarkdown: string;\n attachments: NormalizedAttachment[];\n /** Canonical source ref, e.g. `imap://user@host/INBOX/42`. */\n sourceRef: string;\n}\n\nexport interface IngestOptions {\n includeAttachments?: boolean;\n maxAttachmentBytes?: number;\n direction?: \"inbound\" | \"outbound\";\n}\n\n/**\n * Ingest one normalized email into a customer: convert + index its\n * attachments, summarize it, append the interaction (with attachment links),\n * and index the full body (chunked) for semantic search. Caller is responsible\n * for deduplication (skip messages whose sourceRef is already logged).\n */\nexport async function ingestEmail(\n dataDir: string,\n slug: string,\n msg: NormalizedEmail,\n options: IngestOptions = {}\n): Promise<{ attachments: number; chunks: number }> {\n const includeAttachments = options.includeAttachments ?? true;\n const maxBytes = options.maxAttachmentBytes ?? DEFAULT_MAX_ATTACHMENT_BYTES;\n\n // Attachments first, so the interaction entry can link to the Markdown.\n const attachmentLinks: string[] = [];\n if (includeAttachments) {\n for (const att of msg.attachments) {\n if (att.content.length > maxBytes) {\n logger.warn(\"email-ingest\", \"skipping oversized attachment\", {\n filename: att.filename,\n bytes: att.content.length,\n });\n continue;\n }\n try {\n const saved = await persistAttachment({\n dataDir,\n slug,\n messageId: msg.messageId,\n source: msg.sourceRef,\n date: msg.date,\n filename: att.filename,\n mimeType: att.mimeType,\n buffer: att.content,\n });\n attachmentLinks.push(saved.markdownName);\n } catch (err) {\n logger.warn(\"email-ingest\", \"attachment failed\", {\n filename: att.filename,\n error: (err as Error).message,\n });\n }\n }\n }\n\n // LLM summary — non-blocking fallback to the raw body when no API key.\n const { summarizeEmail } = await import(\"../core/llm.js\");\n const summary = await summarizeEmail(msg.subject, msg.bodyMarkdown, msg.from);\n\n await appendInteraction(dataDir, slug, {\n date: msg.date,\n type: \"Email\",\n direction: options.direction ?? \"inbound\",\n with: msg.from,\n subject: msg.subject,\n summary: summary.summary,\n nextSteps: summary.nextSteps,\n ...(attachmentLinks.length > 0 ? { attachments: attachmentLinks } : {}),\n sourceRef: msg.sourceRef,\n synced: new Date().toISOString(),\n });\n\n // Index the full email (subject + body), chunked for long threads.\n const { indexInLanceDB } = await import(\"../core/lancedb.js\");\n const bodyChunks = chunkText(`${msg.subject}\\n${msg.bodyMarkdown}`);\n for (let i = 0; i < bodyChunks.length; i++) {\n const ref = i === 0 ? msg.sourceRef : `${msg.sourceRef}#${i}`;\n await indexInLanceDB(dataDir, slug, bodyChunks[i]!, ref, {\n date: msg.date,\n type: \"Email\",\n }).catch((err: unknown) => {\n logger.error(\"email-ingest\", \"LanceDB index failed\", { error: (err as Error).message });\n });\n }\n\n return { attachments: attachmentLinks.length, chunks: bodyChunks.length };\n}\n","// src/sync/email-router.ts\nimport fs from \"fs\";\nimport path from \"path\";\nimport matter from \"gray-matter\";\nimport { listCustomerSlugs } from \"../fs/customer-dir.js\";\n\nexport interface CustomerRoutingInfo {\n slug: string;\n /** Lowercased domains that identify this customer (e.g. \"acme.com\"). */\n domains: string[];\n /** Lowercased full email addresses that identify this customer. */\n emails: string[];\n}\n\n/** Extract the bare email address from a header value like `\"Name <a@b.com>\"`. */\nexport function extractEmailAddress(headerValue: string): string {\n const angle = headerValue.match(/<([^>]+)>/);\n const raw = (angle ? angle[1] : headerValue) ?? \"\";\n return raw.trim().toLowerCase();\n}\n\n/** The domain part of an email address, lowercased (empty string if malformed). */\nexport function domainOf(email: string): string {\n const at = email.lastIndexOf(\"@\");\n return at >= 0 ? email.slice(at + 1).trim().toLowerCase() : \"\";\n}\n\n/**\n * Split a header that may contain several comma-separated addresses\n * (To/Cc) into individual lowercased email addresses.\n */\nexport function parseAddressList(headerValue: string | undefined): string[] {\n if (!headerValue) return [];\n return headerValue\n .split(\",\")\n .map((part) => extractEmailAddress(part))\n .filter((a) => a.includes(\"@\"));\n}\n\n/** Read just the routing-relevant fields from a customer's main_facts (tolerant). */\nfunction readRoutingFields(\n dataDir: string,\n slug: string\n): { domain?: string | undefined; email?: string | undefined; primary_contact?: string | undefined } {\n const file = path.join(dataDir, \"customers\", slug, \"main_facts.md\");\n if (!fs.existsSync(file)) return {};\n try {\n const data = matter(fs.readFileSync(file, \"utf-8\")).data as Record<string, unknown>;\n return {\n domain: typeof data[\"domain\"] === \"string\" ? data[\"domain\"] : undefined,\n email: typeof data[\"email\"] === \"string\" ? data[\"email\"] : undefined,\n primary_contact:\n typeof data[\"primary_contact\"] === \"string\" ? data[\"primary_contact\"] : undefined,\n };\n } catch {\n return {};\n }\n}\n\n/**\n * Build the routing table from every customer's main_facts. A customer is\n * identified by its `domain`, `email`, and `primary_contact` (when it looks\n * like an email). Customers without any identifier are still listed (empty\n * arrays) so callers can see them, but they never match.\n */\nexport function buildRoutingTable(dataDir: string): CustomerRoutingInfo[] {\n return listCustomerSlugs(dataDir).map((slug) => {\n const facts = readRoutingFields(dataDir, slug);\n const domains = new Set<string>();\n const emails = new Set<string>();\n if (facts.domain) domains.add(facts.domain.trim().toLowerCase());\n for (const candidate of [facts.email, facts.primary_contact]) {\n if (candidate && candidate.includes(\"@\")) {\n const addr = candidate.trim().toLowerCase();\n emails.add(addr);\n const d = domainOf(addr);\n if (d) domains.add(d);\n }\n }\n return { slug, domains: [...domains], emails: [...emails] };\n });\n}\n\n/**\n * Route a message to a customer slug by matching any of its addresses\n * (from/to/cc) against the routing table. Exact email matches win over domain\n * matches. Returns the matched slug, or null when nothing matches (the message\n * is \"unrouted\").\n */\nexport function routeMessage(addresses: string[], table: CustomerRoutingInfo[]): string | null {\n const addrs = addresses.map((a) => a.trim().toLowerCase()).filter((a) => a.includes(\"@\"));\n if (addrs.length === 0) return null;\n const domains = new Set(addrs.map(domainOf).filter(Boolean));\n\n // Pass 1: exact email match (most specific).\n for (const c of table) {\n if (c.emails.some((e) => addrs.includes(e))) return c.slug;\n }\n // Pass 2: domain match.\n for (const c of table) {\n if (c.domains.some((d) => domains.has(d))) return c.slug;\n }\n return null;\n}\n","// src/sync/connectors/imap.ts\nimport { readInteractions } from \"../../fs/interactions-writer.js\";\nimport { ingestEmail, type NormalizedEmail } from \"../email-ingest.js\";\nimport { buildRoutingTable, routeMessage, domainOf } from \"../email-router.js\";\nimport { htmlToMarkdown } from \"../converters/html.js\";\nimport { logger } from \"../../core/logger.js\";\n\nexport interface ImapMailboxConfig {\n host: string;\n port?: number;\n secure?: boolean;\n /** Either a password (legacy IMAP) or an OAuth2 access token (XOAUTH2). */\n auth: { user: string; pass?: string; accessToken?: string };\n mailbox?: string;\n}\n\n/** Minimal slice of the ImapFlow client surface we depend on (for testability). */\nexport interface ImapMessage {\n uid: number;\n source: Buffer;\n}\nexport interface ImapClient {\n connect(): Promise<void>;\n getMailboxLock(mailbox: string): Promise<{ release: () => void }>;\n fetch(\n range: unknown,\n query: { uid?: boolean; source?: boolean }\n ): AsyncIterable<ImapMessage>;\n logout(): Promise<void>;\n}\n\nexport interface SyncImapOptions {\n dataDir: string;\n config: ImapMailboxConfig;\n since?: Date;\n /** Fixed customer slug. When omitted, messages are auto-routed by domain. */\n slug?: string;\n includeAttachments?: boolean;\n maxAttachmentBytes?: number;\n /** Inject a client (tests); defaults to a real ImapFlow connection. */\n clientFactory?: (config: ImapMailboxConfig) => ImapClient;\n}\n\nexport interface SyncImapResult {\n synced: number;\n skipped: number;\n unrouted: number;\n}\n\n/** Build a real ImapFlow client. Loaded lazily so the dep stays off hot paths. */\nasync function defaultClientFactory(config: ImapMailboxConfig): Promise<ImapClient> {\n const { ImapFlow } = await import(\"imapflow\");\n const auth = config.auth.accessToken\n ? { user: config.auth.user, accessToken: config.auth.accessToken }\n : { user: config.auth.user, pass: config.auth.pass ?? \"\" };\n return new ImapFlow({\n host: config.host,\n port: config.port ?? 993,\n secure: config.secure ?? true,\n auth,\n logger: false,\n }) as unknown as ImapClient;\n}\n\n/** Fields extracted from a parsed message, decoupled from mailparser's types. */\nexport interface ParsedEmailInput {\n messageId?: string | undefined;\n fromText?: string | undefined;\n toAddresses?: string[] | undefined;\n subject?: string | undefined;\n date?: Date | undefined;\n text?: string | undefined;\n html?: string | false | undefined;\n attachments?:\n | Array<{ filename?: string | undefined; contentType?: string | undefined; content: Buffer }>\n | undefined;\n}\n\n/** Normalize extracted email fields into the provider-independent email shape. */\nexport async function normalizeParsedEmail(\n parsed: ParsedEmailInput,\n ctx: { user: string; host: string; mailbox: string; uid: number }\n): Promise<NormalizedEmail> {\n const toAddresses = (parsed.toAddresses ?? [])\n .map((a) => a.toLowerCase())\n .filter((a) => a.includes(\"@\"));\n\n const plain = (parsed.text ?? \"\").trim();\n const bodyMarkdown = plain\n ? plain\n : parsed.html\n ? (await htmlToMarkdown(parsed.html)).trim()\n : \"\";\n\n const rawId = (parsed.messageId ?? \"\").replace(/[<>]/g, \"\").trim();\n const messageId = rawId || `uid-${ctx.uid}`;\n\n return {\n messageId,\n from: parsed.fromText ?? \"\",\n toAddresses,\n subject: parsed.subject ?? \"(no subject)\",\n date: (parsed.date ?? new Date()).toISOString().slice(0, 10),\n bodyMarkdown,\n attachments: (parsed.attachments ?? [])\n .filter((a) => a.filename)\n .map((a) => ({\n filename: a.filename!,\n mimeType: a.contentType ?? \"application/octet-stream\",\n content: a.content,\n })),\n sourceRef: `imap://${ctx.user}@${ctx.host}/${ctx.mailbox}/${ctx.uid}`,\n };\n}\n\n/** Flatten mailparser's AddressObject | AddressObject[] | undefined to addresses. */\nfunction flattenAddresses(\n field:\n | { value?: Array<{ address?: string | undefined }> }\n | Array<{ value?: Array<{ address?: string | undefined }> }>\n | undefined\n): string[] {\n if (!field) return [];\n const objects = Array.isArray(field) ? field : [field];\n return objects\n .flatMap((o) => o.value ?? [])\n .map((a) => (a.address ?? \"\").toLowerCase())\n .filter((a) => a.includes(\"@\"));\n}\n\n/**\n * Sync a whole IMAP mailbox (any provider). Each message is parsed, routed to a\n * customer — by a fixed `slug` or auto-routed by sender/recipient domain — and\n * ingested through the shared pipeline (attachments→Markdown, summary, index).\n * Messages that match no customer are counted as `unrouted` and skipped.\n */\nexport async function syncImapMailbox(opts: SyncImapOptions): Promise<SyncImapResult> {\n const result: SyncImapResult = { synced: 0, skipped: 0, unrouted: 0 };\n const mailbox = opts.config.mailbox ?? \"INBOX\";\n const { simpleParser } = await import(\"mailparser\");\n\n const client = opts.clientFactory\n ? opts.clientFactory(opts.config)\n : await defaultClientFactory(opts.config);\n\n // Routing table (auto-route mode) + per-slug dedup cache.\n const table = opts.slug ? null : buildRoutingTable(opts.dataDir);\n const dedupCache = new Map<string, string>();\n const seen = async (slug: string, sourceRef: string): Promise<boolean> => {\n let content = dedupCache.get(slug);\n if (content === undefined) {\n content = await readInteractions(opts.dataDir, slug).catch(() => \"\");\n dedupCache.set(slug, content);\n }\n return content.includes(sourceRef);\n };\n\n await client.connect();\n const lock = await client.getMailboxLock(mailbox);\n try {\n const range = opts.since ? { since: opts.since } : { all: true };\n for await (const message of client.fetch(range, { uid: true, source: true })) {\n try {\n const parsed = await simpleParser(message.source);\n const msg = await normalizeParsedEmail(\n {\n messageId: parsed.messageId,\n fromText: parsed.from?.text,\n toAddresses: [...flattenAddresses(parsed.to), ...flattenAddresses(parsed.cc)],\n subject: parsed.subject,\n date: parsed.date,\n text: parsed.text,\n html: parsed.html,\n attachments: parsed.attachments,\n },\n {\n user: opts.config.auth.user,\n host: opts.config.host,\n mailbox,\n uid: message.uid,\n }\n );\n\n // Route: fixed slug, or auto-route by any from/to/cc domain.\n let slug = opts.slug ?? null;\n if (!slug && table) {\n const fromAddr = (msg.from.match(/<([^>]+)>/)?.[1] ?? msg.from).toLowerCase();\n const addresses = [fromAddr, ...msg.toAddresses].filter((a) => domainOf(a));\n slug = routeMessage(addresses, table);\n }\n if (!slug) {\n result.unrouted++;\n continue;\n }\n\n if (await seen(slug, msg.sourceRef)) {\n result.skipped++;\n continue;\n }\n\n await ingestEmail(opts.dataDir, slug, msg, {\n ...(opts.includeAttachments !== undefined\n ? { includeAttachments: opts.includeAttachments }\n : {}),\n ...(opts.maxAttachmentBytes !== undefined\n ? { maxAttachmentBytes: opts.maxAttachmentBytes }\n : {}),\n direction: directionFor(msg, opts.config.auth.user),\n });\n dedupCache.set(slug, (dedupCache.get(slug) ?? \"\") + msg.sourceRef);\n result.synced++;\n } catch (err) {\n logger.warn(\"imap-sync\", \"message failed\", {\n uid: message.uid,\n error: (err as Error).message,\n });\n result.skipped++;\n }\n }\n } finally {\n lock.release();\n await client.logout().catch(() => undefined);\n }\n\n return result;\n}\n\n/** Inbound unless the mailbox owner is the sender. */\nfunction directionFor(msg: NormalizedEmail, user: string): \"inbound\" | \"outbound\" {\n const fromAddr = (msg.from.match(/<([^>]+)>/)?.[1] ?? msg.from).toLowerCase();\n return fromAddr === user.toLowerCase() ? \"outbound\" : \"inbound\";\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA+CA,eAAsB,YACpB,SACA,MACA,KACA,UAAyB,CAAC,GACwB;CAClD,MAAM,qBAAqB,QAAQ,sBAAsB;CACzD,MAAM,WAAW,QAAQ,sBAAA;CAGzB,MAAM,kBAA4B,CAAC;CACnC,IAAI,oBACF,KAAK,MAAM,OAAO,IAAI,aAAa;EACjC,IAAI,IAAI,QAAQ,SAAS,UAAU;GACjC,OAAO,KAAK,gBAAgB,iCAAiC;IAC3D,UAAU,IAAI;IACd,OAAO,IAAI,QAAQ;GACrB,CAAC;GACD;EACF;EACA,IAAI;GACF,MAAM,QAAQ,MAAM,kBAAkB;IACpC;IACA;IACA,WAAW,IAAI;IACf,QAAQ,IAAI;IACZ,MAAM,IAAI;IACV,UAAU,IAAI;IACd,UAAU,IAAI;IACd,QAAQ,IAAI;GACd,CAAC;GACD,gBAAgB,KAAK,MAAM,YAAY;EACzC,SAAS,KAAK;GACZ,OAAO,KAAK,gBAAgB,qBAAqB;IAC/C,UAAU,IAAI;IACd,OAAQ,IAAc;GACxB,CAAC;EACH;CACF;CAIF,MAAM,EAAE,mBAAmB,MAAM,OAAO;CACxC,MAAM,UAAU,MAAM,eAAe,IAAI,SAAS,IAAI,cAAc,IAAI,IAAI;CAE5E,MAAM,kBAAkB,SAAS,MAAM;EACrC,MAAM,IAAI;EACV,MAAM;EACN,WAAW,QAAQ,aAAa;EAChC,MAAM,IAAI;EACV,SAAS,IAAI;EACb,SAAS,QAAQ;EACjB,WAAW,QAAQ;EACnB,GAAI,gBAAgB,SAAS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O,aADW,EADA,IAAI,KAAK,MAAM,WAAW,IAAI,MAAM,IAAI,MAAM,YACtC,GAAG,GAAG,IAAI,WAAW,EAAE,QAAQ,MAAM,SAAS,CAAC,CAC7C,GAAG,KAAK;GAEtC,IAAI,CAAC,MAAM;IACT,OAAO;IACP;GACF;GAEA,IAAI,MAAM,KAAK,MAAM,IAAI,SAAS,GAAG;IACnC,OAAO;IACP;GACF;GAEA,MAAM,YAAY,KAAK,SAAS,MAAM,KAAK;IACzC,GAAI,KAAK,uBAAuB,KAAA,IAC5B,EAAE,oBAAoB,KAAK,mBAAmB,IAC9C,CAAC;IACL,GAAI,KAAK,uBAAuB,KAAA,IAC5B,EAAE,oBAAoB,KAAK,mBAAmB,IAC9C,CAAC;IACL,WAAW,aAAa,KAAK,KAAK,OAAO,KAAK,IAAI;GACpD,CAAC;GACD,WAAW,IAAI,OAAO,WAAW,IAAI,IAAI,KAAK,MAAM,IAAI,SAAS;GACjE,OAAO;EACT,SAAS,KAAK;GACZ,OAAO,KAAK,aAAa,kBAAkB;IACzC,KAAK,QAAQ;IACb,OAAQ,IAAc;GACxB,CAAC;GACD,OAAO;EACT;CAEJ,UAAU;EACR,KAAK,QAAQ;EACb,MAAM,OAAO,OAAO,EAAE,YAAY,KAAA,CAAS;CAC7C;CAEA,OAAO;AACT;;AAGA,SAAS,aAAa,KAAsB,MAAsC;CAEhF,QADkB,IAAI,KAAK,MAAM,WAAW,IAAI,MAAM,IAAI,MAAM,YAClD,MAAM,KAAK,YAAY,IAAI,aAAa;AACxD"}
|
|
1
|
+
{"version":3,"file":"imap-BRgNh3T3.js","names":[],"sources":["../src/sync/email-ingest.ts","../src/sync/email-router.ts","../src/sync/connectors/imap.ts"],"sourcesContent":["// src/sync/email-ingest.ts\nimport { appendInteraction } from \"../fs/interactions-writer.js\";\nimport { persistAttachment, DEFAULT_MAX_ATTACHMENT_BYTES } from \"./attachments.js\";\nimport { chunkText } from \"../core/chunk.js\";\nimport { logger } from \"../core/logger.js\";\n\n/** An attachment with its bytes already in hand (no provider fetch needed). */\nexport interface NormalizedAttachment {\n filename: string;\n mimeType: string;\n content: Buffer;\n}\n\n/**\n * A provider-independent email, normalized from Gmail / IMAP / Graph into a\n * single shape so the downstream pipeline (summary, attachments→Markdown,\n * indexing, interaction log) is written once and reused everywhere.\n */\nexport interface NormalizedEmail {\n /** Stable id for dedup and as the attachment filename prefix. */\n messageId: string;\n /** Raw `From` header value (display name + address). */\n from: string;\n /** Recipient addresses (to + cc), lowercased. */\n toAddresses: string[];\n subject: string;\n /** Message date as YYYY-MM-DD. */\n date: string;\n /** Body already rendered to Markdown (plain verbatim or HTML→MD). */\n bodyMarkdown: string;\n attachments: NormalizedAttachment[];\n /** Canonical source ref, e.g. `imap://user@host/INBOX/42`. */\n sourceRef: string;\n}\n\nexport interface IngestOptions {\n includeAttachments?: boolean;\n maxAttachmentBytes?: number;\n direction?: \"inbound\" | \"outbound\";\n}\n\n/**\n * Ingest one normalized email into a customer: convert + index its\n * attachments, summarize it, append the interaction (with attachment links),\n * and index the full body (chunked) for semantic search. Caller is responsible\n * for deduplication (skip messages whose sourceRef is already logged).\n */\nexport async function ingestEmail(\n dataDir: string,\n slug: string,\n msg: NormalizedEmail,\n options: IngestOptions = {}\n): Promise<{ attachments: number; chunks: number }> {\n const includeAttachments = options.includeAttachments ?? true;\n const maxBytes = options.maxAttachmentBytes ?? DEFAULT_MAX_ATTACHMENT_BYTES;\n\n // Attachments first, so the interaction entry can link to the Markdown.\n const attachmentLinks: string[] = [];\n if (includeAttachments) {\n for (const att of msg.attachments) {\n if (att.content.length > maxBytes) {\n logger.warn(\"email-ingest\", \"skipping oversized attachment\", {\n filename: att.filename,\n bytes: att.content.length,\n });\n continue;\n }\n try {\n const saved = await persistAttachment({\n dataDir,\n slug,\n messageId: msg.messageId,\n source: msg.sourceRef,\n date: msg.date,\n filename: att.filename,\n mimeType: att.mimeType,\n buffer: att.content,\n });\n attachmentLinks.push(saved.markdownName);\n } catch (err) {\n logger.warn(\"email-ingest\", \"attachment failed\", {\n filename: att.filename,\n error: (err as Error).message,\n });\n }\n }\n }\n\n // LLM summary — non-blocking fallback to the raw body when no API key.\n const { summarizeEmail } = await import(\"../core/llm.js\");\n const summary = await summarizeEmail(msg.subject, msg.bodyMarkdown, msg.from);\n\n await appendInteraction(dataDir, slug, {\n date: msg.date,\n type: \"Email\",\n direction: options.direction ?? \"inbound\",\n with: msg.from,\n subject: msg.subject,\n summary: summary.summary,\n nextSteps: summary.nextSteps,\n ...(attachmentLinks.length > 0 ? { attachments: attachmentLinks } : {}),\n sourceRef: msg.sourceRef,\n synced: new Date().toISOString(),\n });\n\n // Index the full email (subject + body), chunked for long threads.\n const { indexInLanceDB } = await import(\"../core/lancedb.js\");\n const bodyChunks = chunkText(`${msg.subject}\\n${msg.bodyMarkdown}`);\n for (let i = 0; i < bodyChunks.length; i++) {\n const ref = i === 0 ? msg.sourceRef : `${msg.sourceRef}#${i}`;\n await indexInLanceDB(dataDir, slug, bodyChunks[i]!, ref, {\n date: msg.date,\n type: \"Email\",\n }).catch((err: unknown) => {\n logger.error(\"email-ingest\", \"LanceDB index failed\", { error: (err as Error).message });\n });\n }\n\n return { attachments: attachmentLinks.length, chunks: bodyChunks.length };\n}\n","// src/sync/email-router.ts\nimport fs from \"fs\";\nimport path from \"path\";\nimport matter from \"gray-matter\";\nimport { listCustomerSlugs } from \"../fs/customer-dir.js\";\n\nexport interface CustomerRoutingInfo {\n slug: string;\n /** Lowercased domains that identify this customer (e.g. \"acme.com\"). */\n domains: string[];\n /** Lowercased full email addresses that identify this customer. */\n emails: string[];\n}\n\n/** Extract the bare email address from a header value like `\"Name <a@b.com>\"`. */\nexport function extractEmailAddress(headerValue: string): string {\n const angle = headerValue.match(/<([^>]+)>/);\n const raw = (angle ? angle[1] : headerValue) ?? \"\";\n return raw.trim().toLowerCase();\n}\n\n/** The domain part of an email address, lowercased (empty string if malformed). */\nexport function domainOf(email: string): string {\n const at = email.lastIndexOf(\"@\");\n return at >= 0 ? email.slice(at + 1).trim().toLowerCase() : \"\";\n}\n\n/**\n * Split a header that may contain several comma-separated addresses\n * (To/Cc) into individual lowercased email addresses.\n */\nexport function parseAddressList(headerValue: string | undefined): string[] {\n if (!headerValue) return [];\n return headerValue\n .split(\",\")\n .map((part) => extractEmailAddress(part))\n .filter((a) => a.includes(\"@\"));\n}\n\n/** Read just the routing-relevant fields from a customer's main_facts (tolerant). */\nfunction readRoutingFields(\n dataDir: string,\n slug: string\n): { domain?: string | undefined; email?: string | undefined; primary_contact?: string | undefined } {\n const file = path.join(dataDir, \"customers\", slug, \"main_facts.md\");\n if (!fs.existsSync(file)) return {};\n try {\n const data = matter(fs.readFileSync(file, \"utf-8\")).data as Record<string, unknown>;\n return {\n domain: typeof data[\"domain\"] === \"string\" ? data[\"domain\"] : undefined,\n email: typeof data[\"email\"] === \"string\" ? data[\"email\"] : undefined,\n primary_contact:\n typeof data[\"primary_contact\"] === \"string\" ? data[\"primary_contact\"] : undefined,\n };\n } catch {\n return {};\n }\n}\n\n/**\n * Build the routing table from every customer's main_facts. A customer is\n * identified by its `domain`, `email`, and `primary_contact` (when it looks\n * like an email). Customers without any identifier are still listed (empty\n * arrays) so callers can see them, but they never match.\n */\nexport function buildRoutingTable(dataDir: string): CustomerRoutingInfo[] {\n return listCustomerSlugs(dataDir).map((slug) => {\n const facts = readRoutingFields(dataDir, slug);\n const domains = new Set<string>();\n const emails = new Set<string>();\n if (facts.domain) domains.add(facts.domain.trim().toLowerCase());\n for (const candidate of [facts.email, facts.primary_contact]) {\n if (candidate && candidate.includes(\"@\")) {\n const addr = candidate.trim().toLowerCase();\n emails.add(addr);\n const d = domainOf(addr);\n if (d) domains.add(d);\n }\n }\n return { slug, domains: [...domains], emails: [...emails] };\n });\n}\n\n/**\n * Route a message to a customer slug by matching any of its addresses\n * (from/to/cc) against the routing table. Exact email matches win over domain\n * matches. Returns the matched slug, or null when nothing matches (the message\n * is \"unrouted\").\n */\nexport function routeMessage(addresses: string[], table: CustomerRoutingInfo[]): string | null {\n const addrs = addresses.map((a) => a.trim().toLowerCase()).filter((a) => a.includes(\"@\"));\n if (addrs.length === 0) return null;\n const domains = new Set(addrs.map(domainOf).filter(Boolean));\n\n // Pass 1: exact email match (most specific).\n for (const c of table) {\n if (c.emails.some((e) => addrs.includes(e))) return c.slug;\n }\n // Pass 2: domain match.\n for (const c of table) {\n if (c.domains.some((d) => domains.has(d))) return c.slug;\n }\n return null;\n}\n","// src/sync/connectors/imap.ts\nimport { readInteractions } from \"../../fs/interactions-writer.js\";\nimport { ingestEmail, type NormalizedEmail } from \"../email-ingest.js\";\nimport { buildRoutingTable, routeMessage, domainOf } from \"../email-router.js\";\nimport { htmlToMarkdown } from \"../converters/html.js\";\nimport { logger } from \"../../core/logger.js\";\n\nexport interface ImapMailboxConfig {\n host: string;\n port?: number;\n secure?: boolean;\n /** Either a password (legacy IMAP) or an OAuth2 access token (XOAUTH2). */\n auth: { user: string; pass?: string; accessToken?: string };\n mailbox?: string;\n}\n\n/** Minimal slice of the ImapFlow client surface we depend on (for testability). */\nexport interface ImapMessage {\n uid: number;\n source: Buffer;\n}\nexport interface ImapClient {\n connect(): Promise<void>;\n getMailboxLock(mailbox: string): Promise<{ release: () => void }>;\n fetch(\n range: unknown,\n query: { uid?: boolean; source?: boolean }\n ): AsyncIterable<ImapMessage>;\n logout(): Promise<void>;\n}\n\nexport interface SyncImapOptions {\n dataDir: string;\n config: ImapMailboxConfig;\n since?: Date;\n /** Fixed customer slug. When omitted, messages are auto-routed by domain. */\n slug?: string;\n includeAttachments?: boolean;\n maxAttachmentBytes?: number;\n /** Inject a client (tests); defaults to a real ImapFlow connection. */\n clientFactory?: (config: ImapMailboxConfig) => ImapClient;\n}\n\nexport interface SyncImapResult {\n synced: number;\n skipped: number;\n unrouted: number;\n}\n\n/** Build a real ImapFlow client. Loaded lazily so the dep stays off hot paths. */\nasync function defaultClientFactory(config: ImapMailboxConfig): Promise<ImapClient> {\n const { ImapFlow } = await import(\"imapflow\");\n const auth = config.auth.accessToken\n ? { user: config.auth.user, accessToken: config.auth.accessToken }\n : { user: config.auth.user, pass: config.auth.pass ?? \"\" };\n return new ImapFlow({\n host: config.host,\n port: config.port ?? 993,\n secure: config.secure ?? true,\n auth,\n logger: false,\n }) as unknown as ImapClient;\n}\n\n/** Fields extracted from a parsed message, decoupled from mailparser's types. */\nexport interface ParsedEmailInput {\n messageId?: string | undefined;\n fromText?: string | undefined;\n toAddresses?: string[] | undefined;\n subject?: string | undefined;\n date?: Date | undefined;\n text?: string | undefined;\n html?: string | false | undefined;\n attachments?:\n | Array<{ filename?: string | undefined; contentType?: string | undefined; content: Buffer }>\n | undefined;\n}\n\n/** Normalize extracted email fields into the provider-independent email shape. */\nexport async function normalizeParsedEmail(\n parsed: ParsedEmailInput,\n ctx: { user: string; host: string; mailbox: string; uid: number }\n): Promise<NormalizedEmail> {\n const toAddresses = (parsed.toAddresses ?? [])\n .map((a) => a.toLowerCase())\n .filter((a) => a.includes(\"@\"));\n\n const plain = (parsed.text ?? \"\").trim();\n const bodyMarkdown = plain\n ? plain\n : parsed.html\n ? (await htmlToMarkdown(parsed.html)).trim()\n : \"\";\n\n const rawId = (parsed.messageId ?? \"\").replace(/[<>]/g, \"\").trim();\n const messageId = rawId || `uid-${ctx.uid}`;\n\n return {\n messageId,\n from: parsed.fromText ?? \"\",\n toAddresses,\n subject: parsed.subject ?? \"(no subject)\",\n date: (parsed.date ?? new Date()).toISOString().slice(0, 10),\n bodyMarkdown,\n attachments: (parsed.attachments ?? [])\n .filter((a) => a.filename)\n .map((a) => ({\n filename: a.filename!,\n mimeType: a.contentType ?? \"application/octet-stream\",\n content: a.content,\n })),\n sourceRef: `imap://${ctx.user}@${ctx.host}/${ctx.mailbox}/${ctx.uid}`,\n };\n}\n\n/** Flatten mailparser's AddressObject | AddressObject[] | undefined to addresses. */\nfunction flattenAddresses(\n field:\n | { value?: Array<{ address?: string | undefined }> }\n | Array<{ value?: Array<{ address?: string | undefined }> }>\n | undefined\n): string[] {\n if (!field) return [];\n const objects = Array.isArray(field) ? field : [field];\n return objects\n .flatMap((o) => o.value ?? [])\n .map((a) => (a.address ?? \"\").toLowerCase())\n .filter((a) => a.includes(\"@\"));\n}\n\n/**\n * Sync a whole IMAP mailbox (any provider). Each message is parsed, routed to a\n * customer — by a fixed `slug` or auto-routed by sender/recipient domain — and\n * ingested through the shared pipeline (attachments→Markdown, summary, index).\n * Messages that match no customer are counted as `unrouted` and skipped.\n */\nexport async function syncImapMailbox(opts: SyncImapOptions): Promise<SyncImapResult> {\n const result: SyncImapResult = { synced: 0, skipped: 0, unrouted: 0 };\n const mailbox = opts.config.mailbox ?? \"INBOX\";\n const { simpleParser } = await import(\"mailparser\");\n\n const client = opts.clientFactory\n ? opts.clientFactory(opts.config)\n : await defaultClientFactory(opts.config);\n\n // Routing table (auto-route mode) + per-slug dedup cache.\n const table = opts.slug ? null : buildRoutingTable(opts.dataDir);\n const dedupCache = new Map<string, string>();\n const seen = async (slug: string, sourceRef: string): Promise<boolean> => {\n let content = dedupCache.get(slug);\n if (content === undefined) {\n content = await readInteractions(opts.dataDir, slug).catch(() => \"\");\n dedupCache.set(slug, content);\n }\n return content.includes(sourceRef);\n };\n\n await client.connect();\n const lock = await client.getMailboxLock(mailbox);\n try {\n const range = opts.since ? { since: opts.since } : { all: true };\n for await (const message of client.fetch(range, { uid: true, source: true })) {\n try {\n const parsed = await simpleParser(message.source);\n const msg = await normalizeParsedEmail(\n {\n messageId: parsed.messageId,\n fromText: parsed.from?.text,\n toAddresses: [...flattenAddresses(parsed.to), ...flattenAddresses(parsed.cc)],\n subject: parsed.subject,\n date: parsed.date,\n text: parsed.text,\n html: parsed.html,\n attachments: parsed.attachments,\n },\n {\n user: opts.config.auth.user,\n host: opts.config.host,\n mailbox,\n uid: message.uid,\n }\n );\n\n // Route: fixed slug, or auto-route by any from/to/cc domain.\n let slug = opts.slug ?? null;\n if (!slug && table) {\n const fromAddr = (msg.from.match(/<([^>]+)>/)?.[1] ?? msg.from).toLowerCase();\n const addresses = [fromAddr, ...msg.toAddresses].filter((a) => domainOf(a));\n slug = routeMessage(addresses, table);\n }\n if (!slug) {\n result.unrouted++;\n continue;\n }\n\n if (await seen(slug, msg.sourceRef)) {\n result.skipped++;\n continue;\n }\n\n await ingestEmail(opts.dataDir, slug, msg, {\n ...(opts.includeAttachments !== undefined\n ? { includeAttachments: opts.includeAttachments }\n : {}),\n ...(opts.maxAttachmentBytes !== undefined\n ? { maxAttachmentBytes: opts.maxAttachmentBytes }\n : {}),\n direction: directionFor(msg, opts.config.auth.user),\n });\n dedupCache.set(slug, (dedupCache.get(slug) ?? \"\") + msg.sourceRef);\n result.synced++;\n } catch (err) {\n logger.warn(\"imap-sync\", \"message failed\", {\n uid: message.uid,\n error: (err as Error).message,\n });\n result.skipped++;\n }\n }\n } finally {\n lock.release();\n await client.logout().catch(() => undefined);\n }\n\n return result;\n}\n\n/** Inbound unless the mailbox owner is the sender. */\nfunction directionFor(msg: NormalizedEmail, user: string): \"inbound\" | \"outbound\" {\n const fromAddr = (msg.from.match(/<([^>]+)>/)?.[1] ?? msg.from).toLowerCase();\n return fromAddr === user.toLowerCase() ? \"outbound\" : \"inbound\";\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA+CA,eAAsB,YACpB,SACA,MACA,KACA,UAAyB,CAAC,GACwB;CAClD,MAAM,qBAAqB,QAAQ,sBAAsB;CACzD,MAAM,WAAW,QAAQ,sBAAA;CAGzB,MAAM,kBAA4B,CAAC;CACnC,IAAI,oBACF,KAAK,MAAM,OAAO,IAAI,aAAa;EACjC,IAAI,IAAI,QAAQ,SAAS,UAAU;GACjC,OAAO,KAAK,gBAAgB,iCAAiC;IAC3D,UAAU,IAAI;IACd,OAAO,IAAI,QAAQ;GACrB,CAAC;GACD;EACF;EACA,IAAI;GACF,MAAM,QAAQ,MAAM,kBAAkB;IACpC;IACA;IACA,WAAW,IAAI;IACf,QAAQ,IAAI;IACZ,MAAM,IAAI;IACV,UA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b,MAAM,OAAO,OAAO,EAAE,YAAY,KAAA,CAAS;CAC7C;CAEA,OAAO;AACT;;AAGA,SAAS,aAAa,KAAsB,MAAsC;CAEhF,QADkB,IAAI,KAAK,MAAM,WAAW,IAAI,MAAM,IAAI,MAAM,YAClD,MAAM,KAAK,YAAY,IAAI,aAAa;AACxD"}
|
|
@@ -140,8 +140,8 @@ declare const InteractionEntrySchema: z.ZodObject<{
|
|
|
140
140
|
sourceRef: z.ZodString;
|
|
141
141
|
synced: z.ZodString;
|
|
142
142
|
}, "strip", z.ZodTypeAny, {
|
|
143
|
-
type: "Email" | "Call" | "Meeting" | "Note" | "Demo" | "Proposal" | "Contract" | "Other";
|
|
144
143
|
date: string;
|
|
144
|
+
type: "Email" | "Call" | "Meeting" | "Note" | "Demo" | "Proposal" | "Contract" | "Other";
|
|
145
145
|
with: string;
|
|
146
146
|
summary: string;
|
|
147
147
|
nextSteps: string[];
|
|
@@ -151,8 +151,8 @@ declare const InteractionEntrySchema: z.ZodObject<{
|
|
|
151
151
|
subject?: string | undefined;
|
|
152
152
|
attachments?: string[] | undefined;
|
|
153
153
|
}, {
|
|
154
|
-
type: "Email" | "Call" | "Meeting" | "Note" | "Demo" | "Proposal" | "Contract" | "Other";
|
|
155
154
|
date: string;
|
|
155
|
+
type: "Email" | "Call" | "Meeting" | "Note" | "Demo" | "Proposal" | "Contract" | "Other";
|
|
156
156
|
with: string;
|
|
157
157
|
summary: string;
|
|
158
158
|
sourceRef: string;
|
|
@@ -543,4 +543,4 @@ declare const VERSION = "0.1.0";
|
|
|
543
543
|
|
|
544
544
|
//#endregion
|
|
545
545
|
export { type GlobalSources, type InteractionEntry, type KbArticle, type MainFacts, type PipelineDeal, type QuoteLineItem, type Quote as QuoteRecord, type SurveyDefinition, type SurveyResponse, type TicketPriority, type Ticket as TicketRecord, type TicketStatus, VERSION, canSeeCustomer, clearSession, createCustomer, customerExists, filterAuditLog, getRbacConfig, getRole, getSession, readAuditLog, readMainFacts, runAudit, runBackup, runValidate, setSession };
|
|
546
|
-
//# sourceMappingURL=index-
|
|
546
|
+
//# sourceMappingURL=index-DcDaz_cu.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index-
|
|
1
|
+
{"version":3,"file":"index-DcDaz_cu.d.cts","names":[],"sources":["../src/schemas/sources.ts","../src/schemas/main-facts.ts","../src/schemas/interaction.ts","../src/schemas/pipeline.ts","../src/schemas/ticket.ts","../src/schemas/quote.ts","../src/schemas/kb-article.ts","../src/schemas/survey.ts","../src/commands/create.ts","../src/commands/backup.ts","../src/commands/audit.ts","../src/commands/validate.ts","../src/fs/customer-dir.ts","../src/fs/audit-log.ts","../src/core/rbac.ts","../src/core/session-store.ts","../src/version.ts"],"mappings":";;;;cAea,qBAAmB,CAAA,CAAA;;IAAA,IAAA,cAAA,CAAA,OAAA,CAAA;IAAA,KAAA,aAAA;IAUpB,OAAA,cAAa,aAAA,CAAA;EAAA,CAAA,EAAA,OAAA,cAAA,EAAA;IAAkB,IAAA,EAAA,OAAA;IAAf,KAAE,EAAA,MAAA;IAAK,OAAA,EAAA,OAAA;;;;ICvBtB,OAAA,CAAA,EAAA,OAoBX,GAAA,SAAA;EAAA,CAAA,CAAA,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;SApB0B,EAAA,MAAA;EAAA,OAAA,EAAA,MAAA;EAsBhB,KAAA,CAAA,EAAA;IAAS,IAAA,EAAA,OAAA;IAAkB,KAAA,EAAA,MAAA;IAAf,OAAE,EAAA,OAAA;EAAK,CAAA,GAAA,SAAA;;;;ECtBlB,WAAA,CAAA,EAAA;IAYX,IAAA,EAAA,YAAA;;;;;;;;;;;;;;;;;;IAZiC,OAAA,CAAA,EAAA,OAAA,GAAA,SAAA;IAAA,UAAA,CAAA,EAAA,MAAA,EAAA,GAAA,SAAA;EAcvB,CAAA,GAAA,SAAA;EAAgB,OAAA,CAAA,EAAA,MAAA,GAAA,SAAA;;KFShB,aAAA,GAAgB,CAAA,CAAE,a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AA,MAAA;EAMX,IAAA,EAAA,MAAA;;;iBacc,YAAA,mBAA+B;iBA6B/B,cAAA,UACL;;;;IAER;;;;KC/DS,IAAA;UAEK,UAAA;UACP,eAAe;EdQZ,OAAA,CAAA,EcPD,IdOC;EAMX,eAAA,CAAA,EcZkB,MdYlB,CAAA,MAAA,EAAA,MAAA,EAAA,CAAA;;ccVY,eAAe;;iBAsBb,aAAA,mBAAgC;iBAUhC,OAAA,kCAAyC;iBAuBzC,cAAA;;;;;;;;UClEC,OAAA;;;EfeJ,SAAA,EAAA,MAAA;EAMX,KAAA,CAAA,EAAA,MAAA;;iBeZc,UAAA,IAAc;iBAId,UAAA,CAAA,GAAc;iBAId,YAAA,CAAA;;;;cCjBH,OAAA"}
|
package/dist/index.d.cts
CHANGED
|
@@ -140,8 +140,8 @@ declare const InteractionEntrySchema: z.ZodObject<{
|
|
|
140
140
|
sourceRef: z.ZodString;
|
|
141
141
|
synced: z.ZodString;
|
|
142
142
|
}, "strip", z.ZodTypeAny, {
|
|
143
|
-
type: "Email" | "Call" | "Meeting" | "Note" | "Demo" | "Proposal" | "Contract" | "Other";
|
|
144
143
|
date: string;
|
|
144
|
+
type: "Email" | "Call" | "Meeting" | "Note" | "Demo" | "Proposal" | "Contract" | "Other";
|
|
145
145
|
with: string;
|
|
146
146
|
summary: string;
|
|
147
147
|
nextSteps: string[];
|
|
@@ -151,8 +151,8 @@ declare const InteractionEntrySchema: z.ZodObject<{
|
|
|
151
151
|
subject?: string | undefined;
|
|
152
152
|
attachments?: string[] | undefined;
|
|
153
153
|
}, {
|
|
154
|
-
type: "Email" | "Call" | "Meeting" | "Note" | "Demo" | "Proposal" | "Contract" | "Other";
|
|
155
154
|
date: string;
|
|
155
|
+
type: "Email" | "Call" | "Meeting" | "Note" | "Demo" | "Proposal" | "Contract" | "Other";
|
|
156
156
|
with: string;
|
|
157
157
|
summary: string;
|
|
158
158
|
sourceRef: string;
|
|
@@ -543,4 +543,4 @@ declare const VERSION = "0.1.0";
|
|
|
543
543
|
|
|
544
544
|
//#endregion
|
|
545
545
|
export { type GlobalSources, type InteractionEntry, type KbArticle, type MainFacts, type PipelineDeal, type QuoteLineItem, type Quote as QuoteRecord, type SurveyDefinition, type SurveyResponse, type TicketPriority, type Ticket as TicketRecord, type TicketStatus, VERSION, canSeeCustomer, clearSession, createCustomer, customerExists, filterAuditLog, getRbacConfig, getRole, getSession, readAuditLog, readMainFacts, runAudit, runBackup, runValidate, setSession };
|
|
546
|
-
//# sourceMappingURL=index-
|
|
546
|
+
//# sourceMappingURL=index-DcDaz_cu.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index-
|
|
1
|
+
{"version":3,"file":"index-DcDaz_cu.d.cts","names":[],"sources":["../src/schemas/sources.ts","../src/schemas/main-facts.ts","../src/schemas/interaction.ts","../src/schemas/pipeline.ts","../src/schemas/ticket.ts","../src/schemas/quote.ts","../src/schemas/kb-article.ts","../src/schemas/survey.ts","../src/commands/create.ts","../src/commands/backup.ts","../src/commands/audit.ts","../src/commands/validate.ts","../src/fs/customer-dir.ts","../src/fs/audit-log.ts","../src/core/rbac.ts","../src/core/session-store.ts","../src/version.ts"],"mappings":";;;;cAea,qBAAmB,CAAA,CAAA;;IAAA,IAAA,cAAA,CAAA,OAAA,CAAA;IAAA,KAAA,aAAA;IAUpB,OAAA,cAAa,aAAA,CAAA;EAAA,CAAA,EAAA,OAAA,cAAA,EAAA;IAAkB,IAAA,EAAA,OAAA;IAAf,KAAE,EAAA,MAAA;IAAK,OAAA,EAAA,OAAA;;;;ICvBtB,OAAA,CAAA,EAAA,OAoBX,GAAA,SAAA;EAAA,CAAA,CAAA,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;SApB0B,EAAA,MAAA;EAAA,OAAA,EAAA,MAAA;EAsBhB,KAAA,CAAA,EAAA;IAAS,IAAA,EAAA,OAAA;IAAkB,KAAA,EAAA,MAAA;IAAf,OAAE,EAAA,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AAA;QAAb,CAAA,EAAA,MAAA;AAAK,CAAA,CAAA,ES2JhC,OT3JgC,CS2JxB,cT3JwB,GAAA,IAAA,CAAA;;;iBUpBb,QAAA;;EVUT,KAAA,CAAA,EAAA,MAAA;EAMX,KAAA,CAAA,EAAA,MAAA;;sBURC;;;iBC4BmB,WAAA;;qBAAuD;;;iBCnB7D,cAAA;;;iBAsCM,aAAA,iCAA8C,QAAQ;;;;UCzD3D,UAAA;;;EbYJ,IAAA,EAAA,MAAA;EAMX,IAAA,EAAA,MAAA;;;iBacc,YAAA,mBAA+B;iBA6B/B,cAAA,UACL;;;;IAER;;;;KC/DS,IAAA;UAEK,UAAA;UACP,eAAe;EdQZ,OAAA,CAAA,EcPD,IdOC;EAMX,eAAA,CAAA,EcZkB,MdYlB,CAAA,MAAA,EAAA,MAAA,EAAA,CAAA;;ccVY,eAAe;;iBAsBb,aAAA,mBAAgC;iBAUhC,OAAA,kCAAyC;iBAuBzC,cAAA;;;;;;;;UClEC,OAAA;;;EfeJ,SAAA,EAAA,MAAA;EAMX,KAAA,CAAA,EAAA,MAAA;;iBeZc,UAAA,IAAc;iBAId,UAAA,CAAA,GAAc;iBAId,YAAA,CAAA;;;;cCjBH,OAAA"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i as saveMailboxToken } from "./token-store-B0h0USqe.js";
|
|
2
|
+
import { a as buildAuthUrl, o as createOAuthClient, r as requestDeviceCode, s as exchangeCodeForTokens, t as pollForToken } from "./microsoft-dsC1fQQG.js";
|
|
2
3
|
//#region src/sync/oauth/login.ts
|
|
3
4
|
/** Accept either a raw auth code or the full loopback redirect URL and return the code. */
|
|
4
5
|
function extractAuthCode(input) {
|
|
@@ -70,4 +71,4 @@ async function runMicrosoftLogin(opts) {
|
|
|
70
71
|
//#endregion
|
|
71
72
|
export { runGmailLogin, runMicrosoftLogin };
|
|
72
73
|
|
|
73
|
-
//# sourceMappingURL=login-
|
|
74
|
+
//# sourceMappingURL=login-yt9OOQQk.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"login-
|
|
1
|
+
{"version":3,"file":"login-yt9OOQQk.js","names":[],"sources":["../src/sync/oauth/login.ts"],"sourcesContent":["// src/sync/oauth/login.ts\nimport { saveMailboxToken, type MailboxToken } from \"./token-store.js\";\nimport {\n createOAuthClient,\n buildAuthUrl,\n exchangeCodeForTokens,\n DEFAULT_REDIRECT,\n type GoogleOAuthClient,\n} from \"./google.js\";\nimport {\n requestDeviceCode,\n pollForToken,\n type DeviceCodeResponse,\n type MicrosoftTokens,\n} from \"./microsoft.js\";\n\n/** Accept either a raw auth code or the full loopback redirect URL and return the code. */\nexport function extractAuthCode(input: string): string {\n const trimmed = input.trim();\n if (trimmed.includes(\"code=\")) {\n const m = trimmed.match(/[?&]code=([^&\\s]+)/);\n if (m?.[1]) return decodeURIComponent(m[1]);\n }\n return trimmed;\n}\n\nexport interface GmailLoginOptions {\n dataDir: string;\n clientId: string;\n clientSecret: string;\n user: string;\n prompt: (question: string) => Promise<string>;\n print: (line: string) => void;\n redirectUri?: string;\n // Injection points for tests:\n createClient?: (id: string, secret: string, redirect: string) => GoogleOAuthClient;\n exchange?: typeof exchangeCodeForTokens;\n}\n\n/**\n * Drive the Gmail installed-app OAuth flow: show the consent URL, read back the\n * authorization code (or the pasted redirect URL), exchange it for tokens with\n * the full `mail.google.com` IMAP scope, and persist them.\n */\nexport async function runGmailLogin(opts: GmailLoginOptions): Promise<MailboxToken> {\n const redirect = opts.redirectUri ?? DEFAULT_REDIRECT;\n const create = opts.createClient ?? createOAuthClient;\n const exchange = opts.exchange ?? exchangeCodeForTokens;\n\n const client = create(opts.clientId, opts.clientSecret, redirect);\n const authUrl = buildAuthUrl(client, redirect);\n\n opts.print(\"Authorize Gmail IMAP access by visiting this URL:\\n\");\n opts.print(authUrl + \"\\n\");\n opts.print(\n \"After approving, your browser is redirected to a 127.0.0.1 URL that won't load — \" +\n \"copy that whole URL (or just the code) and paste it here.\"\n );\n\n const answer = await opts.prompt(\"Paste the redirect URL or code: \");\n const code = extractAuthCode(answer);\n if (!code) throw new Error(\"No authorization code provided.\");\n\n const tokens = await exchange(client, code);\n if (!tokens.refreshToken) {\n opts.print(\n \"Warning: Google did not return a refresh token. Remove the app's access at \" +\n \"myaccount.google.com/permissions and log in again to force a fresh consent.\"\n );\n }\n\n const token: MailboxToken = {\n provider: \"gmail\",\n user: opts.user,\n accessToken: tokens.accessToken,\n ...(tokens.refreshToken ? { refreshToken: tokens.refreshToken } : {}),\n expiresAt: tokens.expiresAt,\n scope: \"https://mail.google.com/\",\n };\n saveMailboxToken(opts.dataDir, token);\n return token;\n}\n\nexport interface MicrosoftLoginOptions {\n dataDir: string;\n clientId: string;\n user: string;\n tenant?: string;\n print: (line: string) => void;\n // Injection points for tests:\n requestDeviceCodeFn?: (clientId: string, tenant: string) => Promise<DeviceCodeResponse>;\n pollFn?: (opts: {\n clientId: string;\n deviceCode: string;\n tenant: string;\n interval: number;\n expiresIn: number;\n }) => Promise<MicrosoftTokens>;\n}\n\n/**\n * Drive the Microsoft device-code flow: print the short user code + URL, poll\n * until the user authorizes, and persist the IMAP tokens.\n */\nexport async function runMicrosoftLogin(opts: MicrosoftLoginOptions): Promise<MailboxToken> {\n const tenant = opts.tenant ?? \"common\";\n const requestCode =\n opts.requestDeviceCodeFn ?? ((id: string, t: string) => requestDeviceCode(id, t));\n const poll =\n opts.pollFn ??\n ((o: {\n clientId: string;\n deviceCode: string;\n tenant: string;\n interval: number;\n expiresIn: number;\n }) => pollForToken(o));\n\n const device = await requestCode(opts.clientId, tenant);\n opts.print(`\\nTo sign in, open ${device.verification_uri} and enter code: ${device.user_code}\\n`);\n opts.print(\"Waiting for authorization…\");\n\n const tokens = await poll({\n clientId: opts.clientId,\n deviceCode: device.device_code,\n tenant,\n interval: device.interval,\n expiresIn: device.expires_in,\n });\n\n const token: MailboxToken = {\n provider: \"microsoft\",\n user: opts.user,\n accessToken: tokens.accessToken,\n ...(tokens.refreshToken ? { refreshToken: tokens.refreshToken } : {}),\n expiresAt: tokens.expiresAt,\n scope: \"https://outlook.office365.com/IMAP.AccessAsUser.All\",\n };\n saveMailboxToken(opts.dataDir, token);\n return token;\n}\n"],"mappings":";;;;AAiBA,SAAgB,gBAAgB,OAAuB;CACrD,MAAM,UAAU,MAAM,KAAK;CAC3B,IAAI,QAAQ,SAAS,OAAO,GAAG;EAC7B,MAAM,IAAI,QAAQ,MAAM,oBAAoB;EAC5C,IAAI,IAAI,IAAI,OAAO,mBAAmB,EAAE,EAAE;CAC5C;CACA,OAAO;AACT;;;;;;AAoBA,eAAsB,cAAc,MAAgD;CAClF,MAAM,WAAW,KAAK,eAAA;CACtB,MAAM,SAAS,KAAK,gBAAgB;CACpC,MAAM,WAAW,KAAK,YAAY;CAElC,MAAM,SAAS,OAAO,KAAK,UAAU,KAAK,cAAc,QAAQ;CAChE,MAAM,UAAU,aAAa,QAAQ,QAAQ;CAE7C,KAAK,MAAM,qDAAqD;CAChE,KAAK,MAAM,UAAU,IAAI;CACzB,KAAK,MACH,4IAEF;CAGA,MAAM,OAAO,gBAAgB,MADR,KAAK,OAAO,kCAAkC,CAChC;CACnC,IAAI,CAAC,MAAM,MAAM,IAAI,MAAM,iCAAiC;CAE5D,MAAM,SAAS,MAAM,SAAS,QAAQ,IAAI;CAC1C,IAAI,CAAC,OAAO,cACV,KAAK,MACH,wJAEF;CAGF,MAAM,QAAsB;EAC1B,UAAU;EACV,MAAM,KAAK;EACX,aAAa,OAAO;EACpB,GAAI,OAAO,eAAe,EAAE,cAAc,OAAO,aAAa,IAAI,CAAC;EACnE,WAAW,OAAO;EAClB,OAAO;CACT;CACA,iBAAiB,KAAK,SAAS,KAAK;CACpC,OAAO;AACT;;;;;AAuBA,eAAsB,kBAAkB,MAAoD;CAC1F,MAAM,SAAS,KAAK,UAAU;CAC9B,MAAM,cACJ,KAAK,yBAAyB,IAAY,MAAc,kBAAkB,IAAI,CAAC;CACjF,MAAM,OACJ,KAAK,YACH,MAMI,aAAa,CAAC;CAEtB,MAAM,SAAS,MAAM,YAAY,KAAK,UAAU,MAAM;CACtD,KAAK,MAAM,sBAAsB,OAAO,iBAAiB,mBAAmB,OAAO,UAAU,GAAG;CAChG,KAAK,MAAM,4BAA4B;CAEvC,MAAM,SAAS,MAAM,KAAK;EACxB,UAAU,KAAK;EACf,YAAY,OAAO;EACnB;EACA,UAAU,OAAO;EACjB,WAAW,OAAO;CACpB,CAAC;CAED,MAAM,QAAsB;EAC1B,UAAU;EACV,MAAM,KAAK;EACX,aAAa,OAAO;EACpB,GAAI,OAAO,eAAe,EAAE,cAAc,OAAO,aAAa,IAAI,CAAC;EACnE,WAAW,OAAO;EAClB,OAAO;CACT;CACA,iBAAiB,KAAK,SAAS,KAAK;CACpC,OAAO;AACT"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
//#region src/sync/mailbox-config.ts
|
|
2
|
+
/** Default IMAP endpoints for OAuth providers. */
|
|
3
|
+
const PROVIDER_IMAP_HOST = {
|
|
4
|
+
gmail: {
|
|
5
|
+
host: "imap.gmail.com",
|
|
6
|
+
port: 993
|
|
7
|
+
},
|
|
8
|
+
microsoft: {
|
|
9
|
+
host: "outlook.office365.com",
|
|
10
|
+
port: 993
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
/** Read IMAP mailbox connection settings from the environment. */
|
|
14
|
+
function imapConfigFromEnv(env = process.env) {
|
|
15
|
+
const host = env["DXCRM_IMAP_HOST"];
|
|
16
|
+
const user = env["DXCRM_IMAP_USER"];
|
|
17
|
+
const pass = env["DXCRM_IMAP_PASS"];
|
|
18
|
+
const accessToken = env["DXCRM_IMAP_TOKEN"];
|
|
19
|
+
if (!host || !user || !pass && !accessToken) return null;
|
|
20
|
+
return {
|
|
21
|
+
host,
|
|
22
|
+
port: env["DXCRM_IMAP_PORT"] ? Number(env["DXCRM_IMAP_PORT"]) : 993,
|
|
23
|
+
secure: env["DXCRM_IMAP_SECURE"] !== "false",
|
|
24
|
+
mailbox: env["DXCRM_IMAP_MAILBOX"] ?? "INBOX",
|
|
25
|
+
auth: accessToken ? {
|
|
26
|
+
user,
|
|
27
|
+
accessToken
|
|
28
|
+
} : {
|
|
29
|
+
user,
|
|
30
|
+
pass
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** Parse a "provider:user" account string. */
|
|
35
|
+
function parseAccount(account) {
|
|
36
|
+
const idx = account.indexOf(":");
|
|
37
|
+
if (idx < 0) return null;
|
|
38
|
+
const provider = account.slice(0, idx);
|
|
39
|
+
const user = account.slice(idx + 1);
|
|
40
|
+
if (provider !== "gmail" && provider !== "microsoft" || !user) return null;
|
|
41
|
+
return {
|
|
42
|
+
provider,
|
|
43
|
+
user
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** Build an IMAP config for a stored OAuth account, refreshing the token if needed. */
|
|
47
|
+
async function resolveAccountConfig(dataDir, account, env = process.env, mailbox) {
|
|
48
|
+
const parsed = parseAccount(account);
|
|
49
|
+
if (!parsed) throw new Error(`Invalid account '${account}'. Use 'gmail:you@gmail.com' or 'microsoft:you@org.com'.`);
|
|
50
|
+
const { getFreshAccessToken } = await import("./token-resolver-D98qPOOf.js");
|
|
51
|
+
const accessToken = await getFreshAccessToken(dataDir, parsed.provider, parsed.user, { env });
|
|
52
|
+
const { host, port } = PROVIDER_IMAP_HOST[parsed.provider];
|
|
53
|
+
return {
|
|
54
|
+
host,
|
|
55
|
+
port,
|
|
56
|
+
secure: true,
|
|
57
|
+
mailbox: mailbox ?? env["DXCRM_IMAP_MAILBOX"] ?? "INBOX",
|
|
58
|
+
auth: {
|
|
59
|
+
user: parsed.user,
|
|
60
|
+
accessToken
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
65
|
+
export { resolveAccountConfig as i, imapConfigFromEnv as n, parseAccount as r, PROVIDER_IMAP_HOST as t };
|
|
66
|
+
|
|
67
|
+
//# sourceMappingURL=mailbox-config-Dn2xTn9N.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mailbox-config-Dn2xTn9N.js","names":[],"sources":["../src/sync/mailbox-config.ts"],"sourcesContent":["// src/sync/mailbox-config.ts\n// Mailbox connection resolution shared by the CLI command and the daemon.\nimport type { ImapMailboxConfig } from \"./connectors/imap.js\";\n\n/** Default IMAP endpoints for OAuth providers. */\nexport const PROVIDER_IMAP_HOST: Record<\"gmail\" | \"microsoft\", { host: string; port: number }> = {\n gmail: { host: \"imap.gmail.com\", port: 993 },\n microsoft: { host: \"outlook.office365.com\", port: 993 },\n};\n\n/** Read IMAP mailbox connection settings from the environment. */\nexport function imapConfigFromEnv(env: NodeJS.ProcessEnv = process.env): ImapMailboxConfig | null {\n const host = env[\"DXCRM_IMAP_HOST\"];\n const user = env[\"DXCRM_IMAP_USER\"];\n const pass = env[\"DXCRM_IMAP_PASS\"];\n const accessToken = env[\"DXCRM_IMAP_TOKEN\"];\n if (!host || !user || (!pass && !accessToken)) return null;\n\n return {\n host,\n port: env[\"DXCRM_IMAP_PORT\"] ? Number(env[\"DXCRM_IMAP_PORT\"]) : 993,\n secure: env[\"DXCRM_IMAP_SECURE\"] !== \"false\",\n mailbox: env[\"DXCRM_IMAP_MAILBOX\"] ?? \"INBOX\",\n auth: accessToken ? { user, accessToken } : { user, pass: pass! },\n };\n}\n\n/** Parse a \"provider:user\" account string. */\nexport function parseAccount(\n account: string\n): { provider: \"gmail\" | \"microsoft\"; user: string } | null {\n const idx = account.indexOf(\":\");\n if (idx < 0) return null;\n const provider = account.slice(0, idx);\n const user = account.slice(idx + 1);\n if ((provider !== \"gmail\" && provider !== \"microsoft\") || !user) return null;\n return { provider, user };\n}\n\n/** Build an IMAP config for a stored OAuth account, refreshing the token if needed. */\nexport async function resolveAccountConfig(\n dataDir: string,\n account: string,\n env: NodeJS.ProcessEnv = process.env,\n mailbox?: string\n): Promise<ImapMailboxConfig> {\n const parsed = parseAccount(account);\n if (!parsed) {\n throw new Error(\n `Invalid account '${account}'. Use 'gmail:you@gmail.com' or 'microsoft:you@org.com'.`\n );\n }\n const { getFreshAccessToken } = await import(\"./oauth/token-resolver.js\");\n const accessToken = await getFreshAccessToken(dataDir, parsed.provider, parsed.user, { env });\n const { host, port } = PROVIDER_IMAP_HOST[parsed.provider];\n return {\n host,\n port,\n secure: true,\n mailbox: mailbox ?? env[\"DXCRM_IMAP_MAILBOX\"] ?? \"INBOX\",\n auth: { user: parsed.user, accessToken },\n };\n}\n"],"mappings":";;AAKA,MAAa,qBAAoF;CAC/F,OAAO;EAAE,MAAM;EAAkB,MAAM;CAAI;CAC3C,WAAW;EAAE,MAAM;EAAyB,MAAM;CAAI;AACxD;;AAGA,SAAgB,kBAAkB,MAAyB,QAAQ,KAA+B;CAChG,MAAM,OAAO,IAAI;CACjB,MAAM,OAAO,IAAI;CACjB,MAAM,OAAO,IAAI;CACjB,MAAM,cAAc,IAAI;CACxB,IAAI,CAAC,QAAQ,CAAC,QAAS,CAAC,QAAQ,CAAC,aAAc,OAAO;CAEtD,OAAO;EACL;EACA,MAAM,IAAI,qBAAqB,OAAO,IAAI,kBAAkB,IAAI;EAChE,QAAQ,IAAI,yBAAyB;EACrC,SAAS,IAAI,yBAAyB;EACtC,MAAM,cAAc;GAAE;GAAM;EAAY,IAAI;GAAE;GAAY;EAAM;CAClE;AACF;;AAGA,SAAgB,aACd,SAC0D;CAC1D,MAAM,MAAM,QAAQ,QAAQ,GAAG;CAC/B,IAAI,MAAM,GAAG,OAAO;CACpB,MAAM,WAAW,QAAQ,MAAM,GAAG,GAAG;CACrC,MAAM,OAAO,QAAQ,MAAM,MAAM,CAAC;CAClC,IAAK,aAAa,WAAW,aAAa,eAAgB,CAAC,MAAM,OAAO;CACxE,OAAO;EAAE;EAAU;CAAK;AAC1B;;AAGA,eAAsB,qBACpB,SACA,SACA,MAAyB,QAAQ,KACjC,SAC4B;CAC5B,MAAM,SAAS,aAAa,OAAO;CACnC,IAAI,CAAC,QACH,MAAM,IAAI,MACR,oBAAoB,QAAQ,yDAC9B;CAEF,MAAM,EAAE,wBAAwB,MAAM,OAAO;CAC7C,MAAM,cAAc,MAAM,oBAAoB,SAAS,OAAO,UAAU,OAAO,MAAM,EAAE,IAAI,CAAC;CAC5F,MAAM,EAAE,MAAM,SAAS,mBAAmB,OAAO;CACjD,OAAO;EACL;EACA;EACA,QAAQ;EACR,SAAS,WAAW,IAAI,yBAAyB;EACjD,MAAM;GAAE,MAAM,OAAO;GAAM;EAAY;CACzC;AACF"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { n as logger } from "./logger-Dyl4VcLO.js";
|
|
2
|
+
import { i as resolveAccountConfig, n as imapConfigFromEnv } from "./mailbox-config-Dn2xTn9N.js";
|
|
3
|
+
import { n as listMailboxTokens } from "./token-store-B0h0USqe.js";
|
|
4
|
+
import { n as syncImapMailbox } from "./imap-BRgNh3T3.js";
|
|
5
|
+
//#region src/daemon/mailbox-poll.ts
|
|
6
|
+
/**
|
|
7
|
+
* Poll every configured mailbox once and auto-route new mail to customers.
|
|
8
|
+
* Resolves a fresh access token per OAuth account (refreshing as needed) and
|
|
9
|
+
* also syncs an env-configured IMAP mailbox when present. Per-account failures
|
|
10
|
+
* are collected, never thrown, so one bad mailbox can't stall the daemon.
|
|
11
|
+
*/
|
|
12
|
+
async function runMailboxPollCycle(dataDir, since, deps = {}) {
|
|
13
|
+
const env = deps.env ?? process.env;
|
|
14
|
+
const listTokens = deps.listTokens ?? listMailboxTokens;
|
|
15
|
+
const resolveConfig = deps.resolveConfig ?? resolveAccountConfig;
|
|
16
|
+
const envConfig = deps.envConfig ?? imapConfigFromEnv;
|
|
17
|
+
const syncFn = deps.syncFn ?? ((o) => syncImapMailbox({
|
|
18
|
+
dataDir: o.dataDir,
|
|
19
|
+
config: o.config,
|
|
20
|
+
since: o.since
|
|
21
|
+
}));
|
|
22
|
+
const result = {
|
|
23
|
+
accounts: 0,
|
|
24
|
+
synced: 0,
|
|
25
|
+
skipped: 0,
|
|
26
|
+
unrouted: 0,
|
|
27
|
+
errors: []
|
|
28
|
+
};
|
|
29
|
+
const configs = [];
|
|
30
|
+
for (const token of listTokens(dataDir)) {
|
|
31
|
+
if (token.provider !== "gmail" && token.provider !== "microsoft") continue;
|
|
32
|
+
const label = `${token.provider}:${token.user}`;
|
|
33
|
+
try {
|
|
34
|
+
configs.push({
|
|
35
|
+
label,
|
|
36
|
+
config: await resolveConfig(dataDir, label, env)
|
|
37
|
+
});
|
|
38
|
+
} catch (err) {
|
|
39
|
+
result.errors.push(`${label}: ${err.message}`);
|
|
40
|
+
logger.warn("daemon", "mailbox token unusable", {
|
|
41
|
+
account: label,
|
|
42
|
+
error: err.message
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const envCfg = envConfig(env);
|
|
47
|
+
if (envCfg) configs.push({
|
|
48
|
+
label: `imap:${envCfg.auth.user}`,
|
|
49
|
+
config: envCfg
|
|
50
|
+
});
|
|
51
|
+
for (const { label, config } of configs) {
|
|
52
|
+
result.accounts++;
|
|
53
|
+
try {
|
|
54
|
+
const r = await syncFn({
|
|
55
|
+
dataDir,
|
|
56
|
+
config,
|
|
57
|
+
since
|
|
58
|
+
});
|
|
59
|
+
result.synced += r.synced;
|
|
60
|
+
result.skipped += r.skipped;
|
|
61
|
+
result.unrouted += r.unrouted;
|
|
62
|
+
if (r.synced > 0 || r.unrouted > 0) logger.info("daemon", "mailbox polled", {
|
|
63
|
+
account: label,
|
|
64
|
+
synced: r.synced,
|
|
65
|
+
unrouted: r.unrouted
|
|
66
|
+
});
|
|
67
|
+
} catch (err) {
|
|
68
|
+
result.errors.push(`${label}: ${err.message}`);
|
|
69
|
+
logger.error("daemon", "mailbox poll failed", {
|
|
70
|
+
account: label,
|
|
71
|
+
error: err.message
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
//#endregion
|
|
78
|
+
export { runMailboxPollCycle };
|
|
79
|
+
|
|
80
|
+
//# sourceMappingURL=mailbox-poll-B8dvFAXT.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mailbox-poll-B8dvFAXT.js","names":[],"sources":["../src/daemon/mailbox-poll.ts"],"sourcesContent":["// src/daemon/mailbox-poll.ts\n// Background polling of every configured mailbox (stored OAuth accounts +\n// optional env-configured IMAP). Each mailbox is auto-routed to customers by\n// domain. Kept separate from worker.ts so it is unit-testable.\nimport { logger } from \"../core/logger.js\";\nimport { listMailboxTokens } from \"../sync/oauth/token-store.js\";\nimport { resolveAccountConfig, imapConfigFromEnv } from \"../sync/mailbox-config.js\";\nimport {\n syncImapMailbox,\n type ImapMailboxConfig,\n type SyncImapResult,\n} from \"../sync/connectors/imap.js\";\n\nexport interface MailboxPollResult {\n accounts: number;\n synced: number;\n skipped: number;\n unrouted: number;\n errors: string[];\n}\n\nexport interface MailboxPollDeps {\n env?: NodeJS.ProcessEnv;\n listTokens?: typeof listMailboxTokens;\n resolveConfig?: typeof resolveAccountConfig;\n envConfig?: (env: NodeJS.ProcessEnv) => ImapMailboxConfig | null;\n syncFn?: (opts: {\n dataDir: string;\n config: ImapMailboxConfig;\n since: Date;\n }) => Promise<SyncImapResult>;\n}\n\n/**\n * Poll every configured mailbox once and auto-route new mail to customers.\n * Resolves a fresh access token per OAuth account (refreshing as needed) and\n * also syncs an env-configured IMAP mailbox when present. Per-account failures\n * are collected, never thrown, so one bad mailbox can't stall the daemon.\n */\nexport async function runMailboxPollCycle(\n dataDir: string,\n since: Date,\n deps: MailboxPollDeps = {}\n): Promise<MailboxPollResult> {\n const env = deps.env ?? process.env;\n const listTokens = deps.listTokens ?? listMailboxTokens;\n const resolveConfig = deps.resolveConfig ?? resolveAccountConfig;\n const envConfig = deps.envConfig ?? imapConfigFromEnv;\n const syncFn =\n deps.syncFn ??\n ((o: { dataDir: string; config: ImapMailboxConfig; since: Date }) =>\n syncImapMailbox({ dataDir: o.dataDir, config: o.config, since: o.since }));\n\n const result: MailboxPollResult = { accounts: 0, synced: 0, skipped: 0, unrouted: 0, errors: [] };\n\n // 1. Build the list of mailbox configs to poll (OAuth accounts + env IMAP).\n const configs: Array<{ label: string; config: ImapMailboxConfig }> = [];\n for (const token of listTokens(dataDir)) {\n if (token.provider !== \"gmail\" && token.provider !== \"microsoft\") continue;\n const label = `${token.provider}:${token.user}`;\n try {\n configs.push({ label, config: await resolveConfig(dataDir, label, env) });\n } catch (err) {\n result.errors.push(`${label}: ${(err as Error).message}`);\n logger.warn(\"daemon\", \"mailbox token unusable\", {\n account: label,\n error: (err as Error).message,\n });\n }\n }\n const envCfg = envConfig(env);\n if (envCfg) configs.push({ label: `imap:${envCfg.auth.user}`, config: envCfg });\n\n // 2. Sync each mailbox (auto-route).\n for (const { label, config } of configs) {\n result.accounts++;\n try {\n const r = await syncFn({ dataDir, config, since });\n result.synced += r.synced;\n result.skipped += r.skipped;\n result.unrouted += r.unrouted;\n if (r.synced > 0 || r.unrouted > 0) {\n logger.info(\"daemon\", \"mailbox polled\", {\n account: label,\n synced: r.synced,\n unrouted: r.unrouted,\n });\n }\n } catch (err) {\n result.errors.push(`${label}: ${(err as Error).message}`);\n logger.error(\"daemon\", \"mailbox poll failed\", {\n account: label,\n error: (err as Error).message,\n });\n }\n }\n\n return result;\n}\n"],"mappings":";;;;;;;;;;;AAuCA,eAAsB,oBACpB,SACA,OACA,OAAwB,CAAC,GACG;CAC5B,MAAM,MAAM,KAAK,OAAO,QAAQ;CAChC,MAAM,aAAa,KAAK,cAAc;CACtC,MAAM,gBAAgB,KAAK,iBAAiB;CAC5C,MAAM,YAAY,KAAK,aAAa;CACpC,MAAM,SACJ,KAAK,YACH,MACA,gBAAgB;EAAE,SAAS,EAAE;EAAS,QAAQ,EAAE;EAAQ,OAAO,EAAE;CAAM,CAAC;CAE5E,MAAM,SAA4B;EAAE,UAAU;EAAG,QAAQ;EAAG,SAAS;EA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