@agenticmail/enterprise 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ARCHITECTURE.md +183 -0
- package/agenticmail-enterprise.db +0 -0
- package/dashboards/README.md +120 -0
- package/dashboards/dotnet/Program.cs +261 -0
- package/dashboards/express/app.js +146 -0
- package/dashboards/go/main.go +513 -0
- package/dashboards/html/index.html +535 -0
- package/dashboards/java/AgenticMailDashboard.java +376 -0
- package/dashboards/php/index.php +414 -0
- package/dashboards/python/app.py +273 -0
- package/dashboards/ruby/app.rb +195 -0
- package/dist/chunk-77IDQJL3.js +7 -0
- package/dist/chunk-7RGCCHIT.js +115 -0
- package/dist/chunk-DXNKR3TG.js +1355 -0
- package/dist/chunk-IQWA44WT.js +970 -0
- package/dist/chunk-LCUZGIDH.js +965 -0
- package/dist/chunk-N2JVTNNJ.js +2553 -0
- package/dist/chunk-O462UJBH.js +363 -0
- package/dist/chunk-PNKVD2UK.js +26 -0
- package/dist/cli.js +218 -0
- package/dist/dashboard/index.html +558 -0
- package/dist/db-adapter-DEWEFNIV.js +7 -0
- package/dist/dynamodb-CCGL2E77.js +426 -0
- package/dist/engine/index.js +1261 -0
- package/dist/index.js +522 -0
- package/dist/mongodb-ODTXIVPV.js +319 -0
- package/dist/mysql-RM3S2FV5.js +521 -0
- package/dist/postgres-LN7A6MGQ.js +518 -0
- package/dist/routes-2JEPIIKC.js +441 -0
- package/dist/routes-74ZLKJKP.js +399 -0
- package/dist/server.js +7 -0
- package/dist/sqlite-3K5YOZ4K.js +439 -0
- package/dist/turso-LDWODSDI.js +442 -0
- package/package.json +49 -0
- package/src/admin/routes.ts +331 -0
- package/src/auth/routes.ts +130 -0
- package/src/cli.ts +260 -0
- package/src/dashboard/index.html +558 -0
- package/src/db/adapter.ts +230 -0
- package/src/db/dynamodb.ts +456 -0
- package/src/db/factory.ts +51 -0
- package/src/db/mongodb.ts +360 -0
- package/src/db/mysql.ts +472 -0
- package/src/db/postgres.ts +479 -0
- package/src/db/sql-schema.ts +123 -0
- package/src/db/sqlite.ts +391 -0
- package/src/db/turso.ts +411 -0
- package/src/deploy/fly.ts +368 -0
- package/src/deploy/managed.ts +213 -0
- package/src/engine/activity.ts +474 -0
- package/src/engine/agent-config.ts +429 -0
- package/src/engine/agenticmail-bridge.ts +296 -0
- package/src/engine/approvals.ts +278 -0
- package/src/engine/db-adapter.ts +682 -0
- package/src/engine/db-schema.ts +335 -0
- package/src/engine/deployer.ts +595 -0
- package/src/engine/index.ts +134 -0
- package/src/engine/knowledge.ts +486 -0
- package/src/engine/lifecycle.ts +635 -0
- package/src/engine/openclaw-hook.ts +371 -0
- package/src/engine/routes.ts +528 -0
- package/src/engine/skills.ts +473 -0
- package/src/engine/tenant.ts +345 -0
- package/src/engine/tool-catalog.ts +189 -0
- package/src/index.ts +64 -0
- package/src/lib/resilience.ts +326 -0
- package/src/middleware/index.ts +286 -0
- package/src/server.ts +310 -0
- package/tsconfig.json +14 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database Adapter Factory
|
|
3
|
+
*
|
|
4
|
+
* Creates the right adapter based on config.
|
|
5
|
+
* Adapters are lazy-loaded to avoid bundling unused drivers.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { DatabaseAdapter, DatabaseConfig, DatabaseType } from './adapter.js';
|
|
9
|
+
|
|
10
|
+
const ADAPTER_MAP: Record<DatabaseType, () => Promise<new () => DatabaseAdapter>> = {
|
|
11
|
+
postgres: () => import('./postgres.js').then(m => m.PostgresAdapter),
|
|
12
|
+
supabase: () => import('./postgres.js').then(m => m.PostgresAdapter), // Supabase IS Postgres
|
|
13
|
+
neon: () => import('./postgres.js').then(m => m.PostgresAdapter), // Neon IS Postgres
|
|
14
|
+
cockroachdb: () => import('./postgres.js').then(m => m.PostgresAdapter), // CockroachDB is PG-compatible
|
|
15
|
+
mysql: () => import('./mysql.js').then(m => m.MysqlAdapter),
|
|
16
|
+
planetscale: () => import('./mysql.js').then(m => m.MysqlAdapter), // PlanetScale IS MySQL
|
|
17
|
+
mongodb: () => import('./mongodb.js').then(m => m.MongoAdapter),
|
|
18
|
+
sqlite: () => import('./sqlite.js').then(m => m.SqliteAdapter),
|
|
19
|
+
turso: () => import('./turso.js').then(m => m.TursoAdapter),
|
|
20
|
+
dynamodb: () => import('./dynamodb.js').then(m => m.DynamoAdapter),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export async function createAdapter(config: DatabaseConfig): Promise<DatabaseAdapter> {
|
|
24
|
+
const loader = ADAPTER_MAP[config.type];
|
|
25
|
+
if (!loader) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
`Unsupported database type: "${config.type}". ` +
|
|
28
|
+
`Supported: ${Object.keys(ADAPTER_MAP).join(', ')}`
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const AdapterClass = await loader();
|
|
33
|
+
const adapter = new AdapterClass();
|
|
34
|
+
await adapter.connect(config);
|
|
35
|
+
return adapter;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function getSupportedDatabases(): { type: DatabaseType; label: string; group: string }[] {
|
|
39
|
+
return [
|
|
40
|
+
{ type: 'postgres', label: 'PostgreSQL', group: 'SQL' },
|
|
41
|
+
{ type: 'mysql', label: 'MySQL / MariaDB', group: 'SQL' },
|
|
42
|
+
{ type: 'sqlite', label: 'SQLite (embedded, dev/small)',group: 'SQL' },
|
|
43
|
+
{ type: 'mongodb', label: 'MongoDB', group: 'NoSQL' },
|
|
44
|
+
{ type: 'turso', label: 'Turso (LibSQL, edge)', group: 'Edge' },
|
|
45
|
+
{ type: 'dynamodb', label: 'DynamoDB (AWS)', group: 'Cloud' },
|
|
46
|
+
{ type: 'supabase', label: 'Supabase (managed Postgres)', group: 'Cloud' },
|
|
47
|
+
{ type: 'neon', label: 'Neon (serverless Postgres)', group: 'Cloud' },
|
|
48
|
+
{ type: 'planetscale', label: 'PlanetScale (managed MySQL)', group: 'Cloud' },
|
|
49
|
+
{ type: 'cockroachdb', label: 'CockroachDB', group: 'Distributed' },
|
|
50
|
+
];
|
|
51
|
+
}
|
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MongoDB Database Adapter
|
|
3
|
+
*
|
|
4
|
+
* For organizations using MongoDB/Atlas.
|
|
5
|
+
* Uses the official mongodb driver with connection pooling.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { randomUUID, createHash } from 'crypto';
|
|
9
|
+
import {
|
|
10
|
+
DatabaseAdapter, DatabaseConfig,
|
|
11
|
+
Agent, AgentInput, User, UserInput,
|
|
12
|
+
AuditEvent, AuditFilters, ApiKey, ApiKeyInput,
|
|
13
|
+
EmailRule, RetentionPolicy, CompanySettings,
|
|
14
|
+
} from './adapter.js';
|
|
15
|
+
|
|
16
|
+
let mongoMod: any;
|
|
17
|
+
|
|
18
|
+
async function getMongo() {
|
|
19
|
+
if (!mongoMod) {
|
|
20
|
+
try {
|
|
21
|
+
mongoMod = await import('mongodb');
|
|
22
|
+
} catch {
|
|
23
|
+
throw new Error('MongoDB driver not found. Install: npm install mongodb');
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return mongoMod;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class MongoAdapter extends DatabaseAdapter {
|
|
30
|
+
readonly type = 'mongodb' as const;
|
|
31
|
+
private client: any = null;
|
|
32
|
+
private db: any = null;
|
|
33
|
+
|
|
34
|
+
async connect(config: DatabaseConfig): Promise<void> {
|
|
35
|
+
const { MongoClient } = await getMongo();
|
|
36
|
+
const uri = config.connectionString || `mongodb://${config.host || 'localhost'}:${config.port || 27017}`;
|
|
37
|
+
this.client = new MongoClient(uri);
|
|
38
|
+
await this.client.connect();
|
|
39
|
+
const dbName = config.database || new URL(uri.replace('mongodb+srv://', 'https://')).pathname.slice(1) || 'agenticmail';
|
|
40
|
+
this.db = this.client.db(dbName);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async disconnect(): Promise<void> {
|
|
44
|
+
if (this.client) await this.client.close();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
isConnected(): boolean {
|
|
48
|
+
return this.client !== null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private col(name: string) { return this.db.collection(name); }
|
|
52
|
+
|
|
53
|
+
async migrate(): Promise<void> {
|
|
54
|
+
// Create indexes
|
|
55
|
+
await this.col('agents').createIndex({ name: 1 }, { unique: true });
|
|
56
|
+
await this.col('agents').createIndex({ email: 1 }, { unique: true });
|
|
57
|
+
await this.col('agents').createIndex({ status: 1 });
|
|
58
|
+
await this.col('users').createIndex({ email: 1 }, { unique: true });
|
|
59
|
+
await this.col('users').createIndex({ ssoProvider: 1, ssoSubject: 1 });
|
|
60
|
+
await this.col('audit_log').createIndex({ timestamp: -1 });
|
|
61
|
+
await this.col('audit_log').createIndex({ actor: 1 });
|
|
62
|
+
await this.col('audit_log').createIndex({ action: 1 });
|
|
63
|
+
await this.col('api_keys').createIndex({ keyHash: 1 }, { unique: true });
|
|
64
|
+
await this.col('email_rules').createIndex({ agentId: 1 });
|
|
65
|
+
|
|
66
|
+
// Seed defaults
|
|
67
|
+
await this.col('settings').updateOne(
|
|
68
|
+
{ _id: 'default' },
|
|
69
|
+
{ $setOnInsert: { name: '', subdomain: '', plan: 'free', primaryColor: '#6366f1', createdAt: new Date(), updatedAt: new Date() } },
|
|
70
|
+
{ upsert: true },
|
|
71
|
+
);
|
|
72
|
+
await this.col('retention_policy').updateOne(
|
|
73
|
+
{ _id: 'default' },
|
|
74
|
+
{ $setOnInsert: { enabled: false, retainDays: 365, excludeTags: [], archiveFirst: true } },
|
|
75
|
+
{ upsert: true },
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ─── Company ─────────────────────────────────────────────
|
|
80
|
+
|
|
81
|
+
async getSettings(): Promise<CompanySettings> {
|
|
82
|
+
const r = await this.col('settings').findOne({ _id: 'default' });
|
|
83
|
+
if (!r) return null!;
|
|
84
|
+
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, plan: r.plan, createdAt: r.createdAt, updatedAt: r.updatedAt };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async updateSettings(updates: Partial<CompanySettings>): Promise<CompanySettings> {
|
|
88
|
+
const { id, ...rest } = updates as any;
|
|
89
|
+
await this.col('settings').updateOne({ _id: 'default' }, { $set: { ...rest, updatedAt: new Date() } }, { upsert: true });
|
|
90
|
+
return this.getSettings();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ─── Agents ──────────────────────────────────────────────
|
|
94
|
+
|
|
95
|
+
async createAgent(input: AgentInput): Promise<Agent> {
|
|
96
|
+
const doc = {
|
|
97
|
+
_id: randomUUID(),
|
|
98
|
+
name: input.name,
|
|
99
|
+
email: input.email || `${input.name.toLowerCase().replace(/\s+/g, '-')}@localhost`,
|
|
100
|
+
role: input.role || 'assistant',
|
|
101
|
+
status: 'active' as const,
|
|
102
|
+
metadata: input.metadata || {},
|
|
103
|
+
createdBy: input.createdBy,
|
|
104
|
+
createdAt: new Date(),
|
|
105
|
+
updatedAt: new Date(),
|
|
106
|
+
};
|
|
107
|
+
await this.col('agents').insertOne(doc);
|
|
108
|
+
return this.docToAgent(doc);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async getAgent(id: string): Promise<Agent | null> {
|
|
112
|
+
const r = await this.col('agents').findOne({ _id: id });
|
|
113
|
+
return r ? this.docToAgent(r) : null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async getAgentByName(name: string): Promise<Agent | null> {
|
|
117
|
+
const r = await this.col('agents').findOne({ name });
|
|
118
|
+
return r ? this.docToAgent(r) : null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async listAgents(opts?: { status?: string; limit?: number; offset?: number }): Promise<Agent[]> {
|
|
122
|
+
const filter: any = {};
|
|
123
|
+
if (opts?.status) filter.status = opts.status;
|
|
124
|
+
const cursor = this.col('agents').find(filter).sort({ createdAt: -1 });
|
|
125
|
+
if (opts?.offset) cursor.skip(opts.offset);
|
|
126
|
+
if (opts?.limit) cursor.limit(opts.limit);
|
|
127
|
+
return (await cursor.toArray()).map((r: any) => this.docToAgent(r));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async updateAgent(id: string, updates: Partial<Agent>): Promise<Agent> {
|
|
131
|
+
const set: any = { updatedAt: new Date() };
|
|
132
|
+
for (const key of ['name', 'email', 'role', 'status', 'metadata']) {
|
|
133
|
+
if ((updates as any)[key] !== undefined) set[key] = (updates as any)[key];
|
|
134
|
+
}
|
|
135
|
+
await this.col('agents').updateOne({ _id: id }, { $set: set });
|
|
136
|
+
return (await this.getAgent(id))!;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async archiveAgent(id: string): Promise<void> {
|
|
140
|
+
await this.col('agents').updateOne({ _id: id }, { $set: { status: 'archived', updatedAt: new Date() } });
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async deleteAgent(id: string): Promise<void> {
|
|
144
|
+
await this.col('agents').deleteOne({ _id: id });
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async countAgents(status?: string): Promise<number> {
|
|
148
|
+
const filter: any = {};
|
|
149
|
+
if (status) filter.status = status;
|
|
150
|
+
return this.col('agents').countDocuments(filter);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// ─── Users ───────────────────────────────────────────────
|
|
154
|
+
|
|
155
|
+
async createUser(input: UserInput): Promise<User> {
|
|
156
|
+
let passwordHash: string | null = null;
|
|
157
|
+
if (input.password) {
|
|
158
|
+
const { default: bcrypt } = await import('bcryptjs');
|
|
159
|
+
passwordHash = await bcrypt.hash(input.password, 12);
|
|
160
|
+
}
|
|
161
|
+
const doc = {
|
|
162
|
+
_id: randomUUID(),
|
|
163
|
+
email: input.email,
|
|
164
|
+
name: input.name,
|
|
165
|
+
role: input.role,
|
|
166
|
+
passwordHash,
|
|
167
|
+
ssoProvider: input.ssoProvider || null,
|
|
168
|
+
ssoSubject: input.ssoSubject || null,
|
|
169
|
+
createdAt: new Date(),
|
|
170
|
+
updatedAt: new Date(),
|
|
171
|
+
lastLoginAt: null as Date | null,
|
|
172
|
+
};
|
|
173
|
+
await this.col('users').insertOne(doc);
|
|
174
|
+
return this.docToUser(doc);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async getUser(id: string): Promise<User | null> {
|
|
178
|
+
const r = await this.col('users').findOne({ _id: id });
|
|
179
|
+
return r ? this.docToUser(r) : null;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async getUserByEmail(email: string): Promise<User | null> {
|
|
183
|
+
const r = await this.col('users').findOne({ email });
|
|
184
|
+
return r ? this.docToUser(r) : null;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async getUserBySso(provider: string, subject: string): Promise<User | null> {
|
|
188
|
+
const r = await this.col('users').findOne({ ssoProvider: provider, ssoSubject: subject });
|
|
189
|
+
return r ? this.docToUser(r) : null;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async listUsers(opts?: { limit?: number; offset?: number }): Promise<User[]> {
|
|
193
|
+
const cursor = this.col('users').find({}).sort({ createdAt: -1 });
|
|
194
|
+
if (opts?.offset) cursor.skip(opts.offset);
|
|
195
|
+
if (opts?.limit) cursor.limit(opts.limit);
|
|
196
|
+
return (await cursor.toArray()).map((r: any) => this.docToUser(r));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async updateUser(id: string, updates: Partial<User>): Promise<User> {
|
|
200
|
+
const set: any = { updatedAt: new Date() };
|
|
201
|
+
for (const key of ['email', 'name', 'role', 'lastLoginAt']) {
|
|
202
|
+
if ((updates as any)[key] !== undefined) set[key] = (updates as any)[key];
|
|
203
|
+
}
|
|
204
|
+
await this.col('users').updateOne({ _id: id }, { $set: set });
|
|
205
|
+
return (await this.getUser(id))!;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async deleteUser(id: string): Promise<void> {
|
|
209
|
+
await this.col('users').deleteOne({ _id: id });
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// ─── Audit ───────────────────────────────────────────────
|
|
213
|
+
|
|
214
|
+
async logEvent(event: Omit<AuditEvent, 'id' | 'timestamp'>): Promise<void> {
|
|
215
|
+
await this.col('audit_log').insertOne({
|
|
216
|
+
_id: randomUUID(),
|
|
217
|
+
timestamp: new Date(),
|
|
218
|
+
actor: event.actor,
|
|
219
|
+
actorType: event.actorType,
|
|
220
|
+
action: event.action,
|
|
221
|
+
resource: event.resource,
|
|
222
|
+
details: event.details || {},
|
|
223
|
+
ip: event.ip || null,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
async queryAudit(filters: AuditFilters): Promise<{ events: AuditEvent[]; total: number }> {
|
|
228
|
+
const filter: any = {};
|
|
229
|
+
if (filters.actor) filter.actor = filters.actor;
|
|
230
|
+
if (filters.action) filter.action = filters.action;
|
|
231
|
+
if (filters.resource) filter.resource = { $regex: filters.resource, $options: 'i' };
|
|
232
|
+
if (filters.from || filters.to) {
|
|
233
|
+
filter.timestamp = {};
|
|
234
|
+
if (filters.from) filter.timestamp.$gte = filters.from;
|
|
235
|
+
if (filters.to) filter.timestamp.$lte = filters.to;
|
|
236
|
+
}
|
|
237
|
+
const total = await this.col('audit_log').countDocuments(filter);
|
|
238
|
+
const cursor = this.col('audit_log').find(filter).sort({ timestamp: -1 });
|
|
239
|
+
if (filters.offset) cursor.skip(filters.offset);
|
|
240
|
+
if (filters.limit) cursor.limit(filters.limit);
|
|
241
|
+
const rows = await cursor.toArray();
|
|
242
|
+
return {
|
|
243
|
+
events: rows.map((r: any) => ({
|
|
244
|
+
id: r._id, timestamp: r.timestamp, actor: r.actor, actorType: r.actorType,
|
|
245
|
+
action: r.action, resource: r.resource, details: r.details, ip: r.ip,
|
|
246
|
+
})),
|
|
247
|
+
total,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ─── API Keys ────────────────────────────────────────────
|
|
252
|
+
|
|
253
|
+
async createApiKey(input: ApiKeyInput): Promise<{ key: ApiKey; plaintext: string }> {
|
|
254
|
+
const id = randomUUID();
|
|
255
|
+
const plaintext = `ek_${randomUUID().replace(/-/g, '')}`;
|
|
256
|
+
const keyHash = createHash('sha256').update(plaintext).digest('hex');
|
|
257
|
+
const keyPrefix = plaintext.substring(0, 11);
|
|
258
|
+
const doc = {
|
|
259
|
+
_id: id, name: input.name, keyHash, keyPrefix, scopes: input.scopes,
|
|
260
|
+
createdBy: input.createdBy, createdAt: new Date(), lastUsedAt: null as Date | null,
|
|
261
|
+
expiresAt: input.expiresAt || null, revoked: false,
|
|
262
|
+
};
|
|
263
|
+
await this.col('api_keys').insertOne(doc);
|
|
264
|
+
return { key: this.docToApiKey(doc), plaintext };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
async getApiKey(id: string): Promise<ApiKey | null> {
|
|
268
|
+
const r = await this.col('api_keys').findOne({ _id: id });
|
|
269
|
+
return r ? this.docToApiKey(r) : null;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
async validateApiKey(plaintext: string): Promise<ApiKey | null> {
|
|
273
|
+
const keyHash = createHash('sha256').update(plaintext).digest('hex');
|
|
274
|
+
const r = await this.col('api_keys').findOne({ keyHash, revoked: false });
|
|
275
|
+
if (!r) return null;
|
|
276
|
+
const key = this.docToApiKey(r);
|
|
277
|
+
if (key.expiresAt && new Date() > key.expiresAt) return null;
|
|
278
|
+
await this.col('api_keys').updateOne({ _id: r._id }, { $set: { lastUsedAt: new Date() } });
|
|
279
|
+
return key;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async listApiKeys(opts?: { createdBy?: string }): Promise<ApiKey[]> {
|
|
283
|
+
const filter: any = {};
|
|
284
|
+
if (opts?.createdBy) filter.createdBy = opts.createdBy;
|
|
285
|
+
return (await this.col('api_keys').find(filter).sort({ createdAt: -1 }).toArray()).map((r: any) => this.docToApiKey(r));
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
async revokeApiKey(id: string): Promise<void> {
|
|
289
|
+
await this.col('api_keys').updateOne({ _id: id }, { $set: { revoked: true } });
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// ─── Rules ───────────────────────────────────────────────
|
|
293
|
+
|
|
294
|
+
async createRule(rule: Omit<EmailRule, 'id' | 'createdAt' | 'updatedAt'>): Promise<EmailRule> {
|
|
295
|
+
const doc = {
|
|
296
|
+
_id: randomUUID(), ...rule, createdAt: new Date(), updatedAt: new Date(),
|
|
297
|
+
};
|
|
298
|
+
await this.col('email_rules').insertOne(doc);
|
|
299
|
+
return this.docToRule(doc);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
async getRules(agentId?: string): Promise<EmailRule[]> {
|
|
303
|
+
const filter: any = {};
|
|
304
|
+
if (agentId) filter.$or = [{ agentId }, { agentId: null }];
|
|
305
|
+
return (await this.col('email_rules').find(filter).sort({ priority: -1 }).toArray()).map((r: any) => this.docToRule(r));
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
async updateRule(id: string, updates: Partial<EmailRule>): Promise<EmailRule> {
|
|
309
|
+
const { id: _id, createdAt, ...rest } = updates as any;
|
|
310
|
+
await this.col('email_rules').updateOne({ _id: id }, { $set: { ...rest, updatedAt: new Date() } });
|
|
311
|
+
const r = await this.col('email_rules').findOne({ _id: id });
|
|
312
|
+
return this.docToRule(r);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
async deleteRule(id: string): Promise<void> {
|
|
316
|
+
await this.col('email_rules').deleteOne({ _id: id });
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// ─── Retention ───────────────────────────────────────────
|
|
320
|
+
|
|
321
|
+
async getRetentionPolicy(): Promise<RetentionPolicy> {
|
|
322
|
+
const r = await this.col('retention_policy').findOne({ _id: 'default' });
|
|
323
|
+
if (!r) return { enabled: false, retainDays: 365, archiveFirst: true };
|
|
324
|
+
return { enabled: r.enabled, retainDays: r.retainDays, excludeTags: r.excludeTags || [], archiveFirst: r.archiveFirst };
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
async setRetentionPolicy(policy: RetentionPolicy): Promise<void> {
|
|
328
|
+
await this.col('retention_policy').updateOne({ _id: 'default' }, { $set: policy }, { upsert: true });
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// ─── Stats ───────────────────────────────────────────────
|
|
332
|
+
|
|
333
|
+
async getStats() {
|
|
334
|
+
const [totalAgents, activeAgents, totalUsers, totalAuditEvents] = await Promise.all([
|
|
335
|
+
this.col('agents').countDocuments(),
|
|
336
|
+
this.col('agents').countDocuments({ status: 'active' }),
|
|
337
|
+
this.col('users').countDocuments(),
|
|
338
|
+
this.col('audit_log').countDocuments(),
|
|
339
|
+
]);
|
|
340
|
+
return { totalAgents, activeAgents, totalUsers, totalEmails: 0, totalAuditEvents };
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// ─── Mappers ─────────────────────────────────────────────
|
|
344
|
+
|
|
345
|
+
private docToAgent(r: any): Agent {
|
|
346
|
+
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 };
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
private docToUser(r: any): User {
|
|
350
|
+
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 || undefined };
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
private docToApiKey(r: any): ApiKey {
|
|
354
|
+
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 || undefined, expiresAt: r.expiresAt || undefined, revoked: r.revoked };
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
private docToRule(r: any): EmailRule {
|
|
358
|
+
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 };
|
|
359
|
+
}
|
|
360
|
+
}
|