@agenticmail/enterprise 0.5.52 → 0.5.53

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.
Files changed (40) hide show
  1. package/dist/chunk-4ZVC4XJY.js +3563 -0
  2. package/dist/chunk-G54QH5PZ.js +9085 -0
  3. package/dist/chunk-GI5RMYH6.js +37 -0
  4. package/dist/chunk-HAN6MNXJ.js +374 -0
  5. package/dist/chunk-L447KLHG.js +13099 -0
  6. package/dist/chunk-ORN3ILZD.js +48 -0
  7. package/dist/chunk-QIBEF5G3.js +898 -0
  8. package/dist/chunk-WA6WJN3D.js +2115 -0
  9. package/dist/cidr-MQSAKEA6.js +17 -0
  10. package/dist/cli-build-skill-ZD5FURGY.js +235 -0
  11. package/dist/cli-recover-XKHGIGGR.js +97 -0
  12. package/dist/cli-submit-skill-R7HUAMYR.js +162 -0
  13. package/dist/cli-validate-BDWKT6KH.js +148 -0
  14. package/dist/cli-verify-RTYVUWD7.js +98 -0
  15. package/dist/config-store-44Y6KOVX.js +58 -0
  16. package/dist/db-adapter-FJZ5BESQ.js +7 -0
  17. package/dist/domain-lock-7EMDJXFQ.js +7 -0
  18. package/dist/dynamodb-CDDPVEXF.js +424 -0
  19. package/dist/factory-2SRP3B3U.js +9 -0
  20. package/dist/firewall-RTIDM4NY.js +10 -0
  21. package/dist/imap-flow-QTNT4Y75.js +44953 -0
  22. package/dist/index.js +322 -1
  23. package/dist/managed-2CZ3CSGR.js +17 -0
  24. package/dist/mongodb-UEHZUXYD.js +320 -0
  25. package/dist/mysql-4BBS773W.js +575 -0
  26. package/dist/nodemailer-JCKCTRMI.js +11711 -0
  27. package/dist/postgres-BCVODZLS.js +597 -0
  28. package/dist/providers-VPFJNW4H.js +17 -0
  29. package/dist/resolve-driver-ZLJYEK72.js +27 -0
  30. package/dist/routes-M5J73UQY.js +5783 -0
  31. package/dist/runtime-KQFFAIFR.js +47 -0
  32. package/dist/server-4FHO44MK.js +12 -0
  33. package/dist/setup-ZWEA6MUM.js +20 -0
  34. package/dist/skills-WMCZVXBK.js +14 -0
  35. package/dist/sqlite-K6HN7XRU.js +491 -0
  36. package/dist/turso-BBUTZACL.js +496 -0
  37. package/package.json +2 -2
  38. package/src/agenticmail/index.ts +2 -0
  39. package/src/agenticmail/providers/imap.ts +454 -0
  40. package/src/agenticmail/providers/index.ts +4 -2
package/dist/index.js CHANGED
@@ -626,6 +626,327 @@ ${original.body}`
626
626
  }
627
627
  };
628
628
 
629
+ // src/agenticmail/providers/imap.ts
630
+ var ImapEmailProvider = class {
631
+ provider = "imap";
632
+ identity = null;
633
+ // IMAP connection (lazy-loaded to avoid bundling the dep if not used)
634
+ imapClient = null;
635
+ smtpClient = null;
636
+ getIdentity() {
637
+ if (!this.identity) throw new Error("Not connected \u2014 call connect() first");
638
+ return this.identity;
639
+ }
640
+ // ─── Connection ─────────────────────────────────────
641
+ async connect(identity) {
642
+ const imapIdentity = identity;
643
+ if (!imapIdentity.imapHost) throw new Error("IMAP host is required");
644
+ if (!imapIdentity.smtpHost) throw new Error("SMTP host is required");
645
+ this.identity = imapIdentity;
646
+ try {
647
+ const { ImapFlow } = await import("imapflow");
648
+ this.imapClient = new ImapFlow({
649
+ host: imapIdentity.imapHost,
650
+ port: imapIdentity.imapPort || 993,
651
+ secure: true,
652
+ auth: {
653
+ user: imapIdentity.email,
654
+ pass: imapIdentity.password || imapIdentity.accessToken
655
+ },
656
+ logger: false
657
+ });
658
+ await this.imapClient.connect();
659
+ } catch (err) {
660
+ if (err.code === "ERR_MODULE_NOT_FOUND" || err.message?.includes("Cannot find")) {
661
+ console.warn("[imap-provider] imapflow not installed \u2014 IMAP operations will fail. Install with: npm install imapflow");
662
+ this.imapClient = null;
663
+ } else {
664
+ throw new Error(`Failed to connect to IMAP server ${imapIdentity.imapHost}: ${err.message}`);
665
+ }
666
+ }
667
+ }
668
+ async disconnect() {
669
+ if (this.imapClient) {
670
+ try {
671
+ await this.imapClient.logout();
672
+ } catch {
673
+ }
674
+ this.imapClient = null;
675
+ }
676
+ this.identity = null;
677
+ }
678
+ // ─── List / Read ────────────────────────────────────
679
+ async listMessages(folder, opts) {
680
+ if (!this.imapClient) throw new Error("IMAP not connected. Ensure imapflow is installed and connection succeeded.");
681
+ const limit = opts?.limit || 20;
682
+ const lock = await this.imapClient.getMailboxLock(folder || "INBOX");
683
+ try {
684
+ const envelopes = [];
685
+ const status = await this.imapClient.status(folder || "INBOX", { messages: true });
686
+ const total = status.messages || 0;
687
+ const start = Math.max(1, total - (opts?.offset || 0) - limit + 1);
688
+ const end = total - (opts?.offset || 0);
689
+ if (start > end || end < 1) return [];
690
+ for await (const msg of this.imapClient.fetch(`${start}:${end}`, { envelope: true, flags: true, bodyStructure: true })) {
691
+ envelopes.push({
692
+ uid: String(msg.uid),
693
+ from: {
694
+ name: msg.envelope?.from?.[0]?.name || void 0,
695
+ email: msg.envelope?.from?.[0]?.address || ""
696
+ },
697
+ to: (msg.envelope?.to || []).map((t) => ({
698
+ name: t.name || void 0,
699
+ email: t.address || ""
700
+ })),
701
+ subject: msg.envelope?.subject || "",
702
+ date: msg.envelope?.date?.toISOString() || "",
703
+ read: msg.flags?.has("\\Seen") || false,
704
+ flagged: msg.flags?.has("\\Flagged") || false,
705
+ hasAttachments: msg.bodyStructure?.childNodes?.length > 1 || false
706
+ });
707
+ }
708
+ return envelopes.reverse();
709
+ } finally {
710
+ lock.release();
711
+ }
712
+ }
713
+ async readMessage(uid, folder) {
714
+ if (!this.imapClient) throw new Error("IMAP not connected");
715
+ const lock = await this.imapClient.getMailboxLock(folder || "INBOX");
716
+ try {
717
+ const msg = await this.imapClient.fetchOne(uid, {
718
+ envelope: true,
719
+ flags: true,
720
+ source: true
721
+ // full RFC822 message
722
+ }, { uid: true });
723
+ if (!msg) throw new Error(`Message ${uid} not found`);
724
+ const source = msg.source?.toString("utf-8") || "";
725
+ const bodyMatch = source.match(/\r?\n\r?\n([\s\S]*)/);
726
+ const body = bodyMatch ? bodyMatch[1] : "";
727
+ return {
728
+ uid: String(msg.uid),
729
+ from: {
730
+ name: msg.envelope?.from?.[0]?.name || void 0,
731
+ email: msg.envelope?.from?.[0]?.address || ""
732
+ },
733
+ to: (msg.envelope?.to || []).map((t) => ({
734
+ name: t.name || void 0,
735
+ email: t.address || ""
736
+ })),
737
+ cc: (msg.envelope?.cc || []).map((c) => ({
738
+ name: c.name || void 0,
739
+ email: c.address || ""
740
+ })),
741
+ subject: msg.envelope?.subject || "",
742
+ body,
743
+ date: msg.envelope?.date?.toISOString() || "",
744
+ read: msg.flags?.has("\\Seen") || false,
745
+ flagged: msg.flags?.has("\\Flagged") || false,
746
+ folder: folder || "INBOX",
747
+ messageId: msg.envelope?.messageId || void 0,
748
+ inReplyTo: msg.envelope?.inReplyTo || void 0
749
+ };
750
+ } finally {
751
+ lock.release();
752
+ }
753
+ }
754
+ async searchMessages(criteria) {
755
+ if (!this.imapClient) throw new Error("IMAP not connected");
756
+ const lock = await this.imapClient.getMailboxLock("INBOX");
757
+ try {
758
+ const query = {};
759
+ if (criteria.from) query.from = criteria.from;
760
+ if (criteria.to) query.to = criteria.to;
761
+ if (criteria.subject) query.subject = criteria.subject;
762
+ if (criteria.text) query.body = criteria.text;
763
+ if (criteria.since) query.since = new Date(criteria.since);
764
+ if (criteria.before) query.before = new Date(criteria.before);
765
+ if (criteria.seen === true) query.seen = true;
766
+ if (criteria.seen === false) query.unseen = true;
767
+ const uids = await this.imapClient.search(query, { uid: true });
768
+ if (!uids.length) return [];
769
+ const envelopes = [];
770
+ const uidRange = uids.slice(-50).join(",");
771
+ for await (const msg of this.imapClient.fetch(uidRange, { envelope: true, flags: true }, { uid: true })) {
772
+ envelopes.push({
773
+ uid: String(msg.uid),
774
+ from: {
775
+ name: msg.envelope?.from?.[0]?.name || void 0,
776
+ email: msg.envelope?.from?.[0]?.address || ""
777
+ },
778
+ to: (msg.envelope?.to || []).map((t) => ({
779
+ name: t.name || void 0,
780
+ email: t.address || ""
781
+ })),
782
+ subject: msg.envelope?.subject || "",
783
+ date: msg.envelope?.date?.toISOString() || "",
784
+ read: msg.flags?.has("\\Seen") || false,
785
+ flagged: msg.flags?.has("\\Flagged") || false,
786
+ hasAttachments: false
787
+ });
788
+ }
789
+ return envelopes.reverse();
790
+ } finally {
791
+ lock.release();
792
+ }
793
+ }
794
+ async listFolders() {
795
+ if (!this.imapClient) throw new Error("IMAP not connected");
796
+ const folders = [];
797
+ const mailboxes = await this.imapClient.list();
798
+ for (const mb of mailboxes) {
799
+ try {
800
+ const status = await this.imapClient.status(mb.path, { messages: true, unseen: true });
801
+ folders.push({
802
+ name: mb.name,
803
+ path: mb.path,
804
+ unread: status.unseen || 0,
805
+ total: status.messages || 0
806
+ });
807
+ } catch {
808
+ folders.push({ name: mb.name, path: mb.path, unread: 0, total: 0 });
809
+ }
810
+ }
811
+ return folders;
812
+ }
813
+ async createFolder(name) {
814
+ if (!this.imapClient) throw new Error("IMAP not connected");
815
+ await this.imapClient.mailboxCreate(name);
816
+ }
817
+ // ─── Send (via SMTP) ───────────────────────────────
818
+ async send(options) {
819
+ const identity = this.getIdentity();
820
+ try {
821
+ const nodemailer = await import("nodemailer");
822
+ const transporter = nodemailer.createTransport({
823
+ host: identity.smtpHost,
824
+ port: identity.smtpPort || 587,
825
+ secure: (identity.smtpPort || 587) === 465,
826
+ auth: {
827
+ user: identity.email,
828
+ pass: identity.password || identity.accessToken
829
+ }
830
+ });
831
+ const result = await transporter.sendMail({
832
+ from: identity.email,
833
+ to: options.to,
834
+ cc: options.cc,
835
+ bcc: options.bcc,
836
+ subject: options.subject,
837
+ text: options.body,
838
+ html: options.html,
839
+ inReplyTo: options.inReplyTo,
840
+ references: options.references?.join(" "),
841
+ attachments: options.attachments?.map((a) => ({
842
+ filename: a.filename,
843
+ content: a.content,
844
+ contentType: a.contentType,
845
+ encoding: a.encoding
846
+ }))
847
+ });
848
+ return { messageId: result.messageId || `smtp-${Date.now()}` };
849
+ } catch (err) {
850
+ if (err.code === "ERR_MODULE_NOT_FOUND" || err.message?.includes("Cannot find")) {
851
+ throw new Error("nodemailer is required for SMTP sending. Install with: npm install nodemailer");
852
+ }
853
+ throw new Error(`SMTP send failed: ${err.message}`);
854
+ }
855
+ }
856
+ async reply(uid, body, replyAll = false) {
857
+ const original = await this.readMessage(uid);
858
+ const to = replyAll ? [original.from.email, ...(original.to || []).map((t) => t.email), ...(original.cc || []).map((c) => c.email)].filter((e) => e !== this.identity?.email).join(", ") : original.from.email;
859
+ return this.send({
860
+ to,
861
+ subject: original.subject.startsWith("Re:") ? original.subject : `Re: ${original.subject}`,
862
+ body,
863
+ inReplyTo: original.messageId,
864
+ references: original.references ? [...original.references, original.messageId] : original.messageId ? [original.messageId] : void 0
865
+ });
866
+ }
867
+ async forward(uid, to, body) {
868
+ const original = await this.readMessage(uid);
869
+ return this.send({
870
+ to,
871
+ subject: `Fwd: ${original.subject}`,
872
+ body: (body ? body + "\n\n" : "") + `---------- Forwarded message ----------
873
+ From: ${original.from.email}
874
+ Date: ${original.date}
875
+ Subject: ${original.subject}
876
+
877
+ ` + original.body
878
+ });
879
+ }
880
+ // ─── Organize ───────────────────────────────────────
881
+ async moveMessage(uid, toFolder, _fromFolder) {
882
+ if (!this.imapClient) throw new Error("IMAP not connected");
883
+ const lock = await this.imapClient.getMailboxLock(_fromFolder || "INBOX");
884
+ try {
885
+ await this.imapClient.messageMove(uid, toFolder, { uid: true });
886
+ } finally {
887
+ lock.release();
888
+ }
889
+ }
890
+ async deleteMessage(uid, folder) {
891
+ if (!this.imapClient) throw new Error("IMAP not connected");
892
+ const lock = await this.imapClient.getMailboxLock(folder || "INBOX");
893
+ try {
894
+ await this.imapClient.messageDelete(uid, { uid: true });
895
+ } finally {
896
+ lock.release();
897
+ }
898
+ }
899
+ async markRead(uid, folder) {
900
+ if (!this.imapClient) throw new Error("IMAP not connected");
901
+ const lock = await this.imapClient.getMailboxLock(folder || "INBOX");
902
+ try {
903
+ await this.imapClient.messageFlagsAdd(uid, ["\\Seen"], { uid: true });
904
+ } finally {
905
+ lock.release();
906
+ }
907
+ }
908
+ async markUnread(uid, folder) {
909
+ if (!this.imapClient) throw new Error("IMAP not connected");
910
+ const lock = await this.imapClient.getMailboxLock(folder || "INBOX");
911
+ try {
912
+ await this.imapClient.messageFlagsRemove(uid, ["\\Seen"], { uid: true });
913
+ } finally {
914
+ lock.release();
915
+ }
916
+ }
917
+ async flagMessage(uid, folder) {
918
+ if (!this.imapClient) throw new Error("IMAP not connected");
919
+ const lock = await this.imapClient.getMailboxLock(folder || "INBOX");
920
+ try {
921
+ await this.imapClient.messageFlagsAdd(uid, ["\\Flagged"], { uid: true });
922
+ } finally {
923
+ lock.release();
924
+ }
925
+ }
926
+ async unflagMessage(uid, folder) {
927
+ if (!this.imapClient) throw new Error("IMAP not connected");
928
+ const lock = await this.imapClient.getMailboxLock(folder || "INBOX");
929
+ try {
930
+ await this.imapClient.messageFlagsRemove(uid, ["\\Flagged"], { uid: true });
931
+ } finally {
932
+ lock.release();
933
+ }
934
+ }
935
+ // ─── Batch ──────────────────────────────────────────
936
+ async batchMarkRead(uids, folder) {
937
+ for (const uid of uids) await this.markRead(uid, folder);
938
+ }
939
+ async batchMarkUnread(uids, folder) {
940
+ for (const uid of uids) await this.markUnread(uid, folder);
941
+ }
942
+ async batchMove(uids, toFolder, fromFolder) {
943
+ for (const uid of uids) await this.moveMessage(uid, toFolder, fromFolder);
944
+ }
945
+ async batchDelete(uids, folder) {
946
+ for (const uid of uids) await this.deleteMessage(uid, folder);
947
+ }
948
+ };
949
+
629
950
  // src/agenticmail/providers/index.ts
630
951
  function createEmailProvider(provider) {
631
952
  switch (provider) {
@@ -634,7 +955,7 @@ function createEmailProvider(provider) {
634
955
  case "google":
635
956
  return new GoogleEmailProvider();
636
957
  case "imap":
637
- throw new Error("Generic IMAP provider not yet implemented \u2014 use Microsoft or Google");
958
+ return new ImapEmailProvider();
638
959
  default:
639
960
  throw new Error(`Unknown email provider: ${provider}`);
640
961
  }
@@ -0,0 +1,17 @@
1
+ import {
2
+ deployToCloud,
3
+ generateDockerCompose,
4
+ generateEnvFile,
5
+ generateFlyToml,
6
+ generateRailwayConfig
7
+ } from "./chunk-7XMV4YKZ.js";
8
+ import "./chunk-3SMTCIR4.js";
9
+ import "./chunk-JLSQOQ5L.js";
10
+ import "./chunk-GI5RMYH6.js";
11
+ export {
12
+ deployToCloud,
13
+ generateDockerCompose,
14
+ generateEnvFile,
15
+ generateFlyToml,
16
+ generateRailwayConfig
17
+ };
@@ -0,0 +1,320 @@
1
+ import {
2
+ DatabaseAdapter
3
+ } from "./chunk-FLRYMSKY.js";
4
+ import "./chunk-GI5RMYH6.js";
5
+
6
+ // src/db/mongodb.ts
7
+ import { randomUUID, createHash } from "crypto";
8
+ var mongoMod;
9
+ async function getMongo() {
10
+ if (!mongoMod) {
11
+ try {
12
+ const { resolveDriver } = await import("./resolve-driver-ZLJYEK72.js");
13
+ mongoMod = await resolveDriver("mongodb", "MongoDB driver not found. Install it: npm install mongodb");
14
+ } catch {
15
+ throw new Error("MongoDB driver not found. Install: npm install mongodb");
16
+ }
17
+ }
18
+ return mongoMod;
19
+ }
20
+ var MongoAdapter = class extends DatabaseAdapter {
21
+ type = "mongodb";
22
+ client = null;
23
+ db = null;
24
+ async connect(config) {
25
+ const { MongoClient } = await getMongo();
26
+ const uri = config.connectionString || `mongodb://${config.host || "localhost"}:${config.port || 27017}`;
27
+ this.client = new MongoClient(uri);
28
+ await this.client.connect();
29
+ const dbName = config.database || new URL(uri.replace("mongodb+srv://", "https://")).pathname.slice(1) || "agenticmail";
30
+ this.db = this.client.db(dbName);
31
+ }
32
+ async disconnect() {
33
+ if (this.client) await this.client.close();
34
+ }
35
+ isConnected() {
36
+ return this.client !== null;
37
+ }
38
+ col(name) {
39
+ return this.db.collection(name);
40
+ }
41
+ async migrate() {
42
+ await this.col("agents").createIndex({ name: 1 }, { unique: true });
43
+ await this.col("agents").createIndex({ email: 1 }, { unique: true });
44
+ await this.col("agents").createIndex({ status: 1 });
45
+ await this.col("users").createIndex({ email: 1 }, { unique: true });
46
+ await this.col("users").createIndex({ ssoProvider: 1, ssoSubject: 1 });
47
+ await this.col("audit_log").createIndex({ timestamp: -1 });
48
+ await this.col("audit_log").createIndex({ actor: 1 });
49
+ await this.col("audit_log").createIndex({ action: 1 });
50
+ await this.col("api_keys").createIndex({ keyHash: 1 }, { unique: true });
51
+ await this.col("email_rules").createIndex({ agentId: 1 });
52
+ await this.col("settings").updateOne(
53
+ { _id: "default" },
54
+ { $setOnInsert: { name: "", subdomain: "", plan: "free", primaryColor: "#6366f1", createdAt: /* @__PURE__ */ new Date(), updatedAt: /* @__PURE__ */ new Date() } },
55
+ { upsert: true }
56
+ );
57
+ await this.col("retention_policy").updateOne(
58
+ { _id: "default" },
59
+ { $setOnInsert: { enabled: false, retainDays: 365, excludeTags: [], archiveFirst: true } },
60
+ { upsert: true }
61
+ );
62
+ }
63
+ // ─── Company ─────────────────────────────────────────────
64
+ async getSettings() {
65
+ const r = await this.col("settings").findOne({ _id: "default" });
66
+ if (!r) return null;
67
+ return { id: "default", name: r.name, domain: r.domain, subdomain: r.subdomain, smtpHost: r.smtpHost, smtpPort: r.smtpPort, smtpUser: r.smtpUser, smtpPass: r.smtpPass, dkimPrivateKey: r.dkimPrivateKey, logoUrl: r.logoUrl, primaryColor: r.primaryColor, ssoConfig: r.ssoConfig, toolSecurityConfig: r.toolSecurityConfig || {}, firewallConfig: r.firewallConfig || {}, modelPricingConfig: r.modelPricingConfig || {}, plan: r.plan, deploymentKeyHash: r.deploymentKeyHash, domainRegistrationId: r.domainRegistrationId, domainDnsChallenge: r.domainDnsChallenge, domainVerifiedAt: r.domainVerifiedAt || void 0, domainRegisteredAt: r.domainRegisteredAt || void 0, domainStatus: r.domainStatus || "unregistered", createdAt: r.createdAt, updatedAt: r.updatedAt };
68
+ }
69
+ async updateSettings(updates) {
70
+ const { id, ...rest } = updates;
71
+ await this.col("settings").updateOne({ _id: "default" }, { $set: { ...rest, updatedAt: /* @__PURE__ */ new Date() } }, { upsert: true });
72
+ return this.getSettings();
73
+ }
74
+ // ─── Agents ──────────────────────────────────────────────
75
+ async createAgent(input) {
76
+ const doc = {
77
+ _id: input.id || randomUUID(),
78
+ name: input.name,
79
+ email: input.email || `${input.name.toLowerCase().replace(/\s+/g, "-")}@localhost`,
80
+ role: input.role || "assistant",
81
+ status: "active",
82
+ metadata: input.metadata || {},
83
+ createdBy: input.createdBy,
84
+ createdAt: /* @__PURE__ */ new Date(),
85
+ updatedAt: /* @__PURE__ */ new Date()
86
+ };
87
+ await this.col("agents").insertOne(doc);
88
+ return this.docToAgent(doc);
89
+ }
90
+ async getAgent(id) {
91
+ const r = await this.col("agents").findOne({ _id: id });
92
+ return r ? this.docToAgent(r) : null;
93
+ }
94
+ async getAgentByName(name) {
95
+ const r = await this.col("agents").findOne({ name });
96
+ return r ? this.docToAgent(r) : null;
97
+ }
98
+ async listAgents(opts) {
99
+ const filter = {};
100
+ if (opts?.status) filter.status = opts.status;
101
+ const cursor = this.col("agents").find(filter).sort({ createdAt: -1 });
102
+ if (opts?.offset) cursor.skip(opts.offset);
103
+ if (opts?.limit) cursor.limit(opts.limit);
104
+ return (await cursor.toArray()).map((r) => this.docToAgent(r));
105
+ }
106
+ async updateAgent(id, updates) {
107
+ const set = { updatedAt: /* @__PURE__ */ new Date() };
108
+ for (const key of ["name", "email", "role", "status", "metadata"]) {
109
+ if (updates[key] !== void 0) set[key] = updates[key];
110
+ }
111
+ await this.col("agents").updateOne({ _id: id }, { $set: set });
112
+ return await this.getAgent(id);
113
+ }
114
+ async archiveAgent(id) {
115
+ await this.col("agents").updateOne({ _id: id }, { $set: { status: "archived", updatedAt: /* @__PURE__ */ new Date() } });
116
+ }
117
+ async deleteAgent(id) {
118
+ await this.col("agents").deleteOne({ _id: id });
119
+ }
120
+ async countAgents(status) {
121
+ const filter = {};
122
+ if (status) filter.status = status;
123
+ return this.col("agents").countDocuments(filter);
124
+ }
125
+ // ─── Users ───────────────────────────────────────────────
126
+ async createUser(input) {
127
+ let passwordHash = null;
128
+ if (input.password) {
129
+ const { default: bcrypt } = await import("bcryptjs");
130
+ passwordHash = await bcrypt.hash(input.password, 12);
131
+ }
132
+ const doc = {
133
+ _id: randomUUID(),
134
+ email: input.email,
135
+ name: input.name,
136
+ role: input.role,
137
+ passwordHash,
138
+ ssoProvider: input.ssoProvider || null,
139
+ ssoSubject: input.ssoSubject || null,
140
+ createdAt: /* @__PURE__ */ new Date(),
141
+ updatedAt: /* @__PURE__ */ new Date(),
142
+ lastLoginAt: null
143
+ };
144
+ await this.col("users").insertOne(doc);
145
+ return this.docToUser(doc);
146
+ }
147
+ async getUser(id) {
148
+ const r = await this.col("users").findOne({ _id: id });
149
+ return r ? this.docToUser(r) : null;
150
+ }
151
+ async getUserByEmail(email) {
152
+ const r = await this.col("users").findOne({ email });
153
+ return r ? this.docToUser(r) : null;
154
+ }
155
+ async getUserBySso(provider, subject) {
156
+ const r = await this.col("users").findOne({ ssoProvider: provider, ssoSubject: subject });
157
+ return r ? this.docToUser(r) : null;
158
+ }
159
+ async listUsers(opts) {
160
+ const cursor = this.col("users").find({}).sort({ createdAt: -1 });
161
+ if (opts?.offset) cursor.skip(opts.offset);
162
+ if (opts?.limit) cursor.limit(opts.limit);
163
+ return (await cursor.toArray()).map((r) => this.docToUser(r));
164
+ }
165
+ async updateUser(id, updates) {
166
+ const set = { updatedAt: /* @__PURE__ */ new Date() };
167
+ for (const key of ["email", "name", "role", "lastLoginAt"]) {
168
+ if (updates[key] !== void 0) set[key] = updates[key];
169
+ }
170
+ await this.col("users").updateOne({ _id: id }, { $set: set });
171
+ return await this.getUser(id);
172
+ }
173
+ async deleteUser(id) {
174
+ await this.col("users").deleteOne({ _id: id });
175
+ }
176
+ // ─── Audit ───────────────────────────────────────────────
177
+ async logEvent(event) {
178
+ await this.col("audit_log").insertOne({
179
+ _id: randomUUID(),
180
+ timestamp: /* @__PURE__ */ new Date(),
181
+ actor: event.actor,
182
+ actorType: event.actorType,
183
+ action: event.action,
184
+ resource: event.resource,
185
+ details: event.details || {},
186
+ ip: event.ip || null
187
+ });
188
+ }
189
+ async queryAudit(filters) {
190
+ const filter = {};
191
+ if (filters.actor) filter.actor = filters.actor;
192
+ if (filters.action) filter.action = filters.action;
193
+ if (filters.resource) filter.resource = { $regex: filters.resource, $options: "i" };
194
+ if (filters.from || filters.to) {
195
+ filter.timestamp = {};
196
+ if (filters.from) filter.timestamp.$gte = filters.from;
197
+ if (filters.to) filter.timestamp.$lte = filters.to;
198
+ }
199
+ const total = await this.col("audit_log").countDocuments(filter);
200
+ const cursor = this.col("audit_log").find(filter).sort({ timestamp: -1 });
201
+ if (filters.offset) cursor.skip(filters.offset);
202
+ if (filters.limit) cursor.limit(filters.limit);
203
+ const rows = await cursor.toArray();
204
+ return {
205
+ events: rows.map((r) => ({
206
+ id: r._id,
207
+ timestamp: r.timestamp,
208
+ actor: r.actor,
209
+ actorType: r.actorType,
210
+ action: r.action,
211
+ resource: r.resource,
212
+ details: r.details,
213
+ ip: r.ip
214
+ })),
215
+ total
216
+ };
217
+ }
218
+ // ─── API Keys ────────────────────────────────────────────
219
+ async createApiKey(input) {
220
+ const id = randomUUID();
221
+ const plaintext = `ek_${randomUUID().replace(/-/g, "")}`;
222
+ const keyHash = createHash("sha256").update(plaintext).digest("hex");
223
+ const keyPrefix = plaintext.substring(0, 11);
224
+ const doc = {
225
+ _id: id,
226
+ name: input.name,
227
+ keyHash,
228
+ keyPrefix,
229
+ scopes: input.scopes,
230
+ createdBy: input.createdBy,
231
+ createdAt: /* @__PURE__ */ new Date(),
232
+ lastUsedAt: null,
233
+ expiresAt: input.expiresAt || null,
234
+ revoked: false
235
+ };
236
+ await this.col("api_keys").insertOne(doc);
237
+ return { key: this.docToApiKey(doc), plaintext };
238
+ }
239
+ async getApiKey(id) {
240
+ const r = await this.col("api_keys").findOne({ _id: id });
241
+ return r ? this.docToApiKey(r) : null;
242
+ }
243
+ async validateApiKey(plaintext) {
244
+ const keyHash = createHash("sha256").update(plaintext).digest("hex");
245
+ const r = await this.col("api_keys").findOne({ keyHash, revoked: false });
246
+ if (!r) return null;
247
+ const key = this.docToApiKey(r);
248
+ if (key.expiresAt && /* @__PURE__ */ new Date() > key.expiresAt) return null;
249
+ await this.col("api_keys").updateOne({ _id: r._id }, { $set: { lastUsedAt: /* @__PURE__ */ new Date() } });
250
+ return key;
251
+ }
252
+ async listApiKeys(opts) {
253
+ const filter = {};
254
+ if (opts?.createdBy) filter.createdBy = opts.createdBy;
255
+ return (await this.col("api_keys").find(filter).sort({ createdAt: -1 }).toArray()).map((r) => this.docToApiKey(r));
256
+ }
257
+ async revokeApiKey(id) {
258
+ await this.col("api_keys").updateOne({ _id: id }, { $set: { revoked: true } });
259
+ }
260
+ // ─── Rules ───────────────────────────────────────────────
261
+ async createRule(rule) {
262
+ const doc = {
263
+ _id: randomUUID(),
264
+ ...rule,
265
+ createdAt: /* @__PURE__ */ new Date(),
266
+ updatedAt: /* @__PURE__ */ new Date()
267
+ };
268
+ await this.col("email_rules").insertOne(doc);
269
+ return this.docToRule(doc);
270
+ }
271
+ async getRules(agentId) {
272
+ const filter = {};
273
+ if (agentId) filter.$or = [{ agentId }, { agentId: null }];
274
+ return (await this.col("email_rules").find(filter).sort({ priority: -1 }).toArray()).map((r) => this.docToRule(r));
275
+ }
276
+ async updateRule(id, updates) {
277
+ const { id: _id, createdAt, ...rest } = updates;
278
+ await this.col("email_rules").updateOne({ _id: id }, { $set: { ...rest, updatedAt: /* @__PURE__ */ new Date() } });
279
+ const r = await this.col("email_rules").findOne({ _id: id });
280
+ return this.docToRule(r);
281
+ }
282
+ async deleteRule(id) {
283
+ await this.col("email_rules").deleteOne({ _id: id });
284
+ }
285
+ // ─── Retention ───────────────────────────────────────────
286
+ async getRetentionPolicy() {
287
+ const r = await this.col("retention_policy").findOne({ _id: "default" });
288
+ if (!r) return { enabled: false, retainDays: 365, archiveFirst: true };
289
+ return { enabled: r.enabled, retainDays: r.retainDays, excludeTags: r.excludeTags || [], archiveFirst: r.archiveFirst };
290
+ }
291
+ async setRetentionPolicy(policy) {
292
+ await this.col("retention_policy").updateOne({ _id: "default" }, { $set: policy }, { upsert: true });
293
+ }
294
+ // ─── Stats ───────────────────────────────────────────────
295
+ async getStats() {
296
+ const [totalAgents, activeAgents, totalUsers, totalAuditEvents] = await Promise.all([
297
+ this.col("agents").countDocuments(),
298
+ this.col("agents").countDocuments({ status: "active" }),
299
+ this.col("users").countDocuments(),
300
+ this.col("audit_log").countDocuments()
301
+ ]);
302
+ return { totalAgents, activeAgents, totalUsers, totalEmails: 0, totalAuditEvents };
303
+ }
304
+ // ─── Mappers ─────────────────────────────────────────────
305
+ docToAgent(r) {
306
+ return { id: r._id, name: r.name, email: r.email, role: r.role, status: r.status, metadata: r.metadata || {}, createdBy: r.createdBy, createdAt: r.createdAt, updatedAt: r.updatedAt };
307
+ }
308
+ docToUser(r) {
309
+ return { id: r._id, email: r.email, name: r.name, role: r.role, passwordHash: r.passwordHash, ssoProvider: r.ssoProvider, ssoSubject: r.ssoSubject, createdAt: r.createdAt, updatedAt: r.updatedAt, lastLoginAt: r.lastLoginAt || void 0 };
310
+ }
311
+ docToApiKey(r) {
312
+ return { id: r._id, name: r.name, keyHash: r.keyHash, keyPrefix: r.keyPrefix, scopes: r.scopes || [], createdBy: r.createdBy, createdAt: r.createdAt, lastUsedAt: r.lastUsedAt || void 0, expiresAt: r.expiresAt || void 0, revoked: r.revoked };
313
+ }
314
+ docToRule(r) {
315
+ return { id: r._id, name: r.name, agentId: r.agentId, conditions: r.conditions || {}, actions: r.actions || {}, priority: r.priority, enabled: r.enabled, createdAt: r.createdAt, updatedAt: r.updatedAt };
316
+ }
317
+ };
318
+ export {
319
+ MongoAdapter
320
+ };