@agenticmail/enterprise 0.5.300 → 0.5.301
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/chunk-QKQWDX6M.js +1519 -0
- package/dist/chunk-VLVRDLYO.js +48 -0
- package/dist/chunk-VMVOJFCX.js +4338 -0
- package/dist/cli-agent-4GZGC6XO.js +1778 -0
- package/dist/cli-recover-WS27YEB7.js +487 -0
- package/dist/cli-serve-HVULKNQH.js +143 -0
- package/dist/cli-verify-CVYMUGKX.js +149 -0
- package/dist/cli.js +5 -5
- package/dist/dashboard/pages/organizations.js +166 -13
- package/dist/factory-XEBV2VGZ.js +9 -0
- package/dist/index.js +3 -3
- package/dist/postgres-NZBDKOQR.js +816 -0
- package/dist/server-3HZEV5X2.js +15 -0
- package/dist/setup-JUB67BUU.js +20 -0
- package/dist/sqlite-INPN4DQN.js +545 -0
- package/package.json +1 -1
- package/src/admin/routes.ts +94 -5
- package/src/dashboard/pages/organizations.js +166 -13
- package/src/db/postgres.ts +17 -0
- package/src/db/sqlite.ts +8 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createServer
|
|
3
|
+
} from "./chunk-VMVOJFCX.js";
|
|
4
|
+
import "./chunk-OF4MUWWS.js";
|
|
5
|
+
import "./chunk-UF3ZJMJO.js";
|
|
6
|
+
import "./chunk-3OC6RH7W.js";
|
|
7
|
+
import "./chunk-2DDKGTD6.js";
|
|
8
|
+
import "./chunk-YVK6F5OD.js";
|
|
9
|
+
import "./chunk-MKRNEM5A.js";
|
|
10
|
+
import "./chunk-DRXMYYKN.js";
|
|
11
|
+
import "./chunk-6WSX7QXF.js";
|
|
12
|
+
import "./chunk-KFQGP6VL.js";
|
|
13
|
+
export {
|
|
14
|
+
createServer
|
|
15
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {
|
|
2
|
+
promptCompanyInfo,
|
|
3
|
+
promptDatabase,
|
|
4
|
+
promptDeployment,
|
|
5
|
+
promptDomain,
|
|
6
|
+
promptRegistration,
|
|
7
|
+
provision,
|
|
8
|
+
runSetupWizard
|
|
9
|
+
} from "./chunk-QKQWDX6M.js";
|
|
10
|
+
import "./chunk-VLVRDLYO.js";
|
|
11
|
+
import "./chunk-KFQGP6VL.js";
|
|
12
|
+
export {
|
|
13
|
+
promptCompanyInfo,
|
|
14
|
+
promptDatabase,
|
|
15
|
+
promptDeployment,
|
|
16
|
+
promptDomain,
|
|
17
|
+
promptRegistration,
|
|
18
|
+
provision,
|
|
19
|
+
runSetupWizard
|
|
20
|
+
};
|
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getAllCreateStatements
|
|
3
|
+
} from "./chunk-XMDE2NGH.js";
|
|
4
|
+
import {
|
|
5
|
+
DatabaseAdapter
|
|
6
|
+
} from "./chunk-FLRYMSKY.js";
|
|
7
|
+
import "./chunk-KFQGP6VL.js";
|
|
8
|
+
|
|
9
|
+
// src/db/sqlite.ts
|
|
10
|
+
import { randomUUID, createHash } from "crypto";
|
|
11
|
+
var Database;
|
|
12
|
+
async function getSqlite() {
|
|
13
|
+
if (!Database) {
|
|
14
|
+
try {
|
|
15
|
+
const { resolveDriver } = await import("./resolve-driver-VQXMFKLJ.js");
|
|
16
|
+
const mod = await resolveDriver("better-sqlite3", "SQLite driver not found. Install it: npm install better-sqlite3");
|
|
17
|
+
Database = mod.default;
|
|
18
|
+
} catch {
|
|
19
|
+
throw new Error("SQLite driver not found. Install it: npm install better-sqlite3");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return Database;
|
|
23
|
+
}
|
|
24
|
+
var SqliteAdapter = class extends DatabaseAdapter {
|
|
25
|
+
type = "sqlite";
|
|
26
|
+
db = null;
|
|
27
|
+
async connect(config) {
|
|
28
|
+
const Db = await getSqlite();
|
|
29
|
+
const path = config.connectionString || config.database || "./agenticmail-enterprise.db";
|
|
30
|
+
this.db = new Db(path);
|
|
31
|
+
this.db.pragma("journal_mode = WAL");
|
|
32
|
+
this.db.pragma("foreign_keys = ON");
|
|
33
|
+
}
|
|
34
|
+
async disconnect() {
|
|
35
|
+
if (this.db) this.db.close();
|
|
36
|
+
}
|
|
37
|
+
isConnected() {
|
|
38
|
+
return this.db !== null;
|
|
39
|
+
}
|
|
40
|
+
async migrate() {
|
|
41
|
+
const stmts = getAllCreateStatements();
|
|
42
|
+
const tx = this.db.transaction(() => {
|
|
43
|
+
for (const stmt of stmts) this.db.exec(stmt);
|
|
44
|
+
this.db.prepare(
|
|
45
|
+
`INSERT OR IGNORE INTO retention_policy (id) VALUES ('default')`
|
|
46
|
+
).run();
|
|
47
|
+
this.db.prepare(
|
|
48
|
+
`INSERT OR IGNORE INTO company_settings (id, name, subdomain) VALUES ('default', 'My Company', 'my-company')`
|
|
49
|
+
).run();
|
|
50
|
+
try {
|
|
51
|
+
this.db.exec(`ALTER TABLE users ADD COLUMN permissions TEXT DEFAULT '"*"'`);
|
|
52
|
+
} catch {
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
this.db.exec(`ALTER TABLE users ADD COLUMN must_reset_password INTEGER DEFAULT 0`);
|
|
56
|
+
} catch {
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
this.db.exec(`ALTER TABLE users ADD COLUMN is_active INTEGER DEFAULT 1`);
|
|
60
|
+
} catch {
|
|
61
|
+
}
|
|
62
|
+
this.db.exec(`
|
|
63
|
+
CREATE TABLE IF NOT EXISTS client_organizations (
|
|
64
|
+
id TEXT PRIMARY KEY,
|
|
65
|
+
name TEXT NOT NULL,
|
|
66
|
+
slug TEXT NOT NULL UNIQUE,
|
|
67
|
+
contact_name TEXT,
|
|
68
|
+
contact_email TEXT,
|
|
69
|
+
description TEXT,
|
|
70
|
+
is_active INTEGER DEFAULT 1,
|
|
71
|
+
settings TEXT DEFAULT '{}',
|
|
72
|
+
created_at TEXT DEFAULT (datetime('now')),
|
|
73
|
+
updated_at TEXT DEFAULT (datetime('now'))
|
|
74
|
+
);
|
|
75
|
+
`);
|
|
76
|
+
try {
|
|
77
|
+
this.db.exec(`ALTER TABLE client_organizations ADD COLUMN billing_rate_per_agent REAL DEFAULT 0`);
|
|
78
|
+
} catch {
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
this.db.exec(`ALTER TABLE client_organizations ADD COLUMN currency TEXT DEFAULT 'USD'`);
|
|
82
|
+
} catch {
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
this.db.exec(`ALTER TABLE agents ADD COLUMN client_org_id TEXT REFERENCES client_organizations(id)`);
|
|
86
|
+
} catch {
|
|
87
|
+
}
|
|
88
|
+
this.db.exec(`CREATE TABLE IF NOT EXISTS org_billing_records (
|
|
89
|
+
id TEXT PRIMARY KEY, org_id TEXT NOT NULL, agent_id TEXT, month TEXT NOT NULL,
|
|
90
|
+
revenue REAL DEFAULT 0, token_cost REAL DEFAULT 0, input_tokens INTEGER DEFAULT 0,
|
|
91
|
+
output_tokens INTEGER DEFAULT 0, notes TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
92
|
+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, UNIQUE(org_id, agent_id, month)
|
|
93
|
+
)`);
|
|
94
|
+
this.db.exec(`
|
|
95
|
+
CREATE TABLE IF NOT EXISTS agent_knowledge_access (
|
|
96
|
+
id TEXT PRIMARY KEY,
|
|
97
|
+
agent_id TEXT NOT NULL,
|
|
98
|
+
knowledge_base_id TEXT NOT NULL,
|
|
99
|
+
access_type TEXT NOT NULL DEFAULT 'read',
|
|
100
|
+
created_at TEXT DEFAULT (datetime('now')),
|
|
101
|
+
UNIQUE(agent_id, knowledge_base_id)
|
|
102
|
+
);
|
|
103
|
+
`);
|
|
104
|
+
});
|
|
105
|
+
tx();
|
|
106
|
+
}
|
|
107
|
+
// ─── Engine Integration ──────────────────────────────────
|
|
108
|
+
getEngineDB() {
|
|
109
|
+
if (!this.db) return null;
|
|
110
|
+
const db = this.db;
|
|
111
|
+
return {
|
|
112
|
+
run: async (sql, params) => {
|
|
113
|
+
db.prepare(sql).run(...params || []);
|
|
114
|
+
},
|
|
115
|
+
get: async (sql, params) => {
|
|
116
|
+
return db.prepare(sql).get(...params || []);
|
|
117
|
+
},
|
|
118
|
+
all: async (sql, params) => {
|
|
119
|
+
return db.prepare(sql).all(...params || []);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
// ─── Company ─────────────────────────────────────────────
|
|
124
|
+
async getSettings() {
|
|
125
|
+
const r = this.db.prepare("SELECT * FROM company_settings WHERE id = ?").get("default");
|
|
126
|
+
return r ? this.mapSettings(r) : null;
|
|
127
|
+
}
|
|
128
|
+
async updateSettings(updates) {
|
|
129
|
+
const map = {
|
|
130
|
+
name: "name",
|
|
131
|
+
domain: "domain",
|
|
132
|
+
subdomain: "subdomain",
|
|
133
|
+
smtpHost: "smtp_host",
|
|
134
|
+
smtpPort: "smtp_port",
|
|
135
|
+
smtpUser: "smtp_user",
|
|
136
|
+
smtpPass: "smtp_pass",
|
|
137
|
+
dkimPrivateKey: "dkim_private_key",
|
|
138
|
+
logoUrl: "logo_url",
|
|
139
|
+
primaryColor: "primary_color",
|
|
140
|
+
plan: "plan",
|
|
141
|
+
deploymentKeyHash: "deployment_key_hash",
|
|
142
|
+
domainRegistrationId: "domain_registration_id",
|
|
143
|
+
domainDnsChallenge: "domain_dns_challenge",
|
|
144
|
+
domainVerifiedAt: "domain_verified_at",
|
|
145
|
+
domainRegisteredAt: "domain_registered_at",
|
|
146
|
+
domainStatus: "domain_status"
|
|
147
|
+
};
|
|
148
|
+
const sets = [];
|
|
149
|
+
const vals = [];
|
|
150
|
+
for (const [key, col] of Object.entries(map)) {
|
|
151
|
+
if (updates[key] !== void 0) {
|
|
152
|
+
sets.push(`${col} = ?`);
|
|
153
|
+
vals.push(updates[key]);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (updates.ssoConfig !== void 0) {
|
|
157
|
+
sets.push("sso_config = ?");
|
|
158
|
+
vals.push(JSON.stringify(updates.ssoConfig));
|
|
159
|
+
}
|
|
160
|
+
if (updates.toolSecurityConfig !== void 0) {
|
|
161
|
+
sets.push("tool_security_config = ?");
|
|
162
|
+
vals.push(JSON.stringify(updates.toolSecurityConfig));
|
|
163
|
+
}
|
|
164
|
+
if (updates.firewallConfig !== void 0) {
|
|
165
|
+
sets.push("firewall_config = ?");
|
|
166
|
+
vals.push(JSON.stringify(updates.firewallConfig));
|
|
167
|
+
}
|
|
168
|
+
if (updates.modelPricingConfig !== void 0) {
|
|
169
|
+
sets.push("model_pricing_config = ?");
|
|
170
|
+
vals.push(JSON.stringify(updates.modelPricingConfig));
|
|
171
|
+
}
|
|
172
|
+
sets.push("updated_at = datetime('now')");
|
|
173
|
+
vals.push("default");
|
|
174
|
+
this.db.prepare(`UPDATE company_settings SET ${sets.join(", ")} WHERE id = ?`).run(...vals);
|
|
175
|
+
return this.getSettings();
|
|
176
|
+
}
|
|
177
|
+
// ─── Agents ──────────────────────────────────────────────
|
|
178
|
+
async createAgent(input) {
|
|
179
|
+
const id = input.id || randomUUID();
|
|
180
|
+
const email = input.email || `${input.name.toLowerCase().replace(/\s+/g, "-")}@localhost`;
|
|
181
|
+
this.db.prepare(
|
|
182
|
+
`INSERT INTO agents (id, name, email, role, metadata, created_by) VALUES (?, ?, ?, ?, ?, ?)`
|
|
183
|
+
).run(id, input.name, email, input.role || "assistant", JSON.stringify(input.metadata || {}), input.createdBy);
|
|
184
|
+
return await this.getAgent(id);
|
|
185
|
+
}
|
|
186
|
+
async getAgent(id) {
|
|
187
|
+
const r = this.db.prepare("SELECT * FROM agents WHERE id = ?").get(id);
|
|
188
|
+
return r ? this.mapAgent(r) : null;
|
|
189
|
+
}
|
|
190
|
+
async getAgentByName(name) {
|
|
191
|
+
const r = this.db.prepare("SELECT * FROM agents WHERE name = ?").get(name);
|
|
192
|
+
return r ? this.mapAgent(r) : null;
|
|
193
|
+
}
|
|
194
|
+
async listAgents(opts) {
|
|
195
|
+
let q = "SELECT * FROM agents";
|
|
196
|
+
const params = [];
|
|
197
|
+
if (opts?.status) {
|
|
198
|
+
q += " WHERE status = ?";
|
|
199
|
+
params.push(opts.status);
|
|
200
|
+
}
|
|
201
|
+
q += " ORDER BY created_at DESC";
|
|
202
|
+
if (opts?.limit) q += ` LIMIT ${opts.limit}`;
|
|
203
|
+
if (opts?.offset) q += ` OFFSET ${opts.offset}`;
|
|
204
|
+
return this.db.prepare(q).all(...params).map((r) => this.mapAgent(r));
|
|
205
|
+
}
|
|
206
|
+
async updateAgent(id, updates) {
|
|
207
|
+
const fields = [];
|
|
208
|
+
const vals = [];
|
|
209
|
+
for (const [key, col] of Object.entries({ name: "name", email: "email", role: "role", status: "status" })) {
|
|
210
|
+
if (updates[key] !== void 0) {
|
|
211
|
+
fields.push(`${col} = ?`);
|
|
212
|
+
vals.push(updates[key]);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (updates.metadata) {
|
|
216
|
+
fields.push("metadata = ?");
|
|
217
|
+
vals.push(JSON.stringify(updates.metadata));
|
|
218
|
+
}
|
|
219
|
+
fields.push("updated_at = datetime('now')");
|
|
220
|
+
vals.push(id);
|
|
221
|
+
this.db.prepare(`UPDATE agents SET ${fields.join(", ")} WHERE id = ?`).run(...vals);
|
|
222
|
+
return await this.getAgent(id);
|
|
223
|
+
}
|
|
224
|
+
async archiveAgent(id) {
|
|
225
|
+
this.db.prepare("UPDATE agents SET status = 'archived', updated_at = datetime('now') WHERE id = ?").run(id);
|
|
226
|
+
}
|
|
227
|
+
async deleteAgent(id) {
|
|
228
|
+
this.db.prepare("DELETE FROM agents WHERE id = ?").run(id);
|
|
229
|
+
}
|
|
230
|
+
async countAgents(status) {
|
|
231
|
+
const r = status ? this.db.prepare("SELECT COUNT(*) as c FROM agents WHERE status = ?").get(status) : this.db.prepare("SELECT COUNT(*) as c FROM agents").get();
|
|
232
|
+
return r.c;
|
|
233
|
+
}
|
|
234
|
+
// ─── Users ───────────────────────────────────────────────
|
|
235
|
+
async createUser(input) {
|
|
236
|
+
const id = randomUUID();
|
|
237
|
+
let passwordHash = null;
|
|
238
|
+
if (input.password) {
|
|
239
|
+
const { default: bcrypt } = await import("bcryptjs");
|
|
240
|
+
passwordHash = await bcrypt.hash(input.password, 12);
|
|
241
|
+
}
|
|
242
|
+
this.db.prepare(
|
|
243
|
+
`INSERT INTO users (id, email, name, role, password_hash, sso_provider, sso_subject)
|
|
244
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
|
245
|
+
).run(id, input.email, input.name, input.role, passwordHash, input.ssoProvider || null, input.ssoSubject || null);
|
|
246
|
+
return await this.getUser(id);
|
|
247
|
+
}
|
|
248
|
+
async getUser(id) {
|
|
249
|
+
const r = this.db.prepare("SELECT * FROM users WHERE id = ?").get(id);
|
|
250
|
+
return r ? this.mapUser(r) : null;
|
|
251
|
+
}
|
|
252
|
+
async getUserByEmail(email) {
|
|
253
|
+
const r = this.db.prepare("SELECT * FROM users WHERE email = ?").get(email);
|
|
254
|
+
return r ? this.mapUser(r) : null;
|
|
255
|
+
}
|
|
256
|
+
async getUserBySso(provider, subject) {
|
|
257
|
+
const r = this.db.prepare("SELECT * FROM users WHERE sso_provider = ? AND sso_subject = ?").get(provider, subject);
|
|
258
|
+
return r ? this.mapUser(r) : null;
|
|
259
|
+
}
|
|
260
|
+
async listUsers(opts) {
|
|
261
|
+
let q = "SELECT * FROM users ORDER BY created_at DESC";
|
|
262
|
+
if (opts?.limit) q += ` LIMIT ${opts.limit}`;
|
|
263
|
+
if (opts?.offset) q += ` OFFSET ${opts.offset}`;
|
|
264
|
+
return this.db.prepare(q).all().map((r) => this.mapUser(r));
|
|
265
|
+
}
|
|
266
|
+
async updateUser(id, updates) {
|
|
267
|
+
const fields = [];
|
|
268
|
+
const vals = [];
|
|
269
|
+
for (const key of ["email", "name", "role"]) {
|
|
270
|
+
if (updates[key] !== void 0) {
|
|
271
|
+
fields.push(`${key} = ?`);
|
|
272
|
+
vals.push(updates[key]);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
fields.push("updated_at = datetime('now')");
|
|
276
|
+
vals.push(id);
|
|
277
|
+
this.db.prepare(`UPDATE users SET ${fields.join(", ")} WHERE id = ?`).run(...vals);
|
|
278
|
+
return await this.getUser(id);
|
|
279
|
+
}
|
|
280
|
+
async deleteUser(id) {
|
|
281
|
+
this.db.prepare("DELETE FROM users WHERE id = ?").run(id);
|
|
282
|
+
}
|
|
283
|
+
// ─── Audit ───────────────────────────────────────────────
|
|
284
|
+
async logEvent(event) {
|
|
285
|
+
this.db.prepare(
|
|
286
|
+
`INSERT INTO audit_log (id, actor, actor_type, action, resource, details, ip) VALUES (?, ?, ?, ?, ?, ?, ?)`
|
|
287
|
+
).run(
|
|
288
|
+
randomUUID(),
|
|
289
|
+
event.actor,
|
|
290
|
+
event.actorType,
|
|
291
|
+
event.action,
|
|
292
|
+
event.resource,
|
|
293
|
+
JSON.stringify(event.details || {}),
|
|
294
|
+
event.ip || null
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
async queryAudit(filters) {
|
|
298
|
+
const where = [];
|
|
299
|
+
const params = [];
|
|
300
|
+
if (filters.actor) {
|
|
301
|
+
where.push("actor = ?");
|
|
302
|
+
params.push(filters.actor);
|
|
303
|
+
}
|
|
304
|
+
if (filters.action) {
|
|
305
|
+
where.push("action = ?");
|
|
306
|
+
params.push(filters.action);
|
|
307
|
+
}
|
|
308
|
+
if (filters.resource) {
|
|
309
|
+
where.push("resource LIKE ?");
|
|
310
|
+
params.push(`%${filters.resource}%`);
|
|
311
|
+
}
|
|
312
|
+
if (filters.from) {
|
|
313
|
+
where.push("timestamp >= ?");
|
|
314
|
+
params.push(filters.from.toISOString());
|
|
315
|
+
}
|
|
316
|
+
if (filters.to) {
|
|
317
|
+
where.push("timestamp <= ?");
|
|
318
|
+
params.push(filters.to.toISOString());
|
|
319
|
+
}
|
|
320
|
+
const wc = where.length > 0 ? `WHERE ${where.join(" AND ")}` : "";
|
|
321
|
+
const total = this.db.prepare(`SELECT COUNT(*) as c FROM audit_log ${wc}`).get(...params).c;
|
|
322
|
+
let q = `SELECT * FROM audit_log ${wc} ORDER BY timestamp DESC`;
|
|
323
|
+
if (filters.limit) q += ` LIMIT ${filters.limit}`;
|
|
324
|
+
if (filters.offset) q += ` OFFSET ${filters.offset}`;
|
|
325
|
+
const rows = this.db.prepare(q).all(...params);
|
|
326
|
+
return {
|
|
327
|
+
events: rows.map((r) => ({
|
|
328
|
+
id: r.id,
|
|
329
|
+
timestamp: new Date(r.timestamp),
|
|
330
|
+
actor: r.actor,
|
|
331
|
+
actorType: r.actor_type,
|
|
332
|
+
action: r.action,
|
|
333
|
+
resource: r.resource,
|
|
334
|
+
details: JSON.parse(r.details || "{}"),
|
|
335
|
+
ip: r.ip
|
|
336
|
+
})),
|
|
337
|
+
total
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
// ─── API Keys ────────────────────────────────────────────
|
|
341
|
+
async createApiKey(input) {
|
|
342
|
+
const id = randomUUID();
|
|
343
|
+
const plaintext = `ek_${randomUUID().replace(/-/g, "")}`;
|
|
344
|
+
const keyHash = createHash("sha256").update(plaintext).digest("hex");
|
|
345
|
+
const keyPrefix = plaintext.substring(0, 11);
|
|
346
|
+
this.db.prepare(
|
|
347
|
+
`INSERT INTO api_keys (id, name, key_hash, key_prefix, scopes, created_by, expires_at)
|
|
348
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
|
349
|
+
).run(id, input.name, keyHash, keyPrefix, JSON.stringify(input.scopes), input.createdBy, input.expiresAt?.toISOString() || null);
|
|
350
|
+
return { key: await this.getApiKey(id), plaintext };
|
|
351
|
+
}
|
|
352
|
+
async getApiKey(id) {
|
|
353
|
+
const r = this.db.prepare("SELECT * FROM api_keys WHERE id = ?").get(id);
|
|
354
|
+
return r ? this.mapApiKey(r) : null;
|
|
355
|
+
}
|
|
356
|
+
async validateApiKey(plaintext) {
|
|
357
|
+
const keyHash = createHash("sha256").update(plaintext).digest("hex");
|
|
358
|
+
const r = this.db.prepare("SELECT * FROM api_keys WHERE key_hash = ? AND revoked = 0").get(keyHash);
|
|
359
|
+
if (!r) return null;
|
|
360
|
+
const key = this.mapApiKey(r);
|
|
361
|
+
if (key.expiresAt && /* @__PURE__ */ new Date() > key.expiresAt) return null;
|
|
362
|
+
this.db.prepare("UPDATE api_keys SET last_used_at = datetime('now') WHERE id = ?").run(key.id);
|
|
363
|
+
return key;
|
|
364
|
+
}
|
|
365
|
+
async listApiKeys(opts) {
|
|
366
|
+
let q = "SELECT * FROM api_keys WHERE revoked = 0";
|
|
367
|
+
const params = [];
|
|
368
|
+
if (opts?.createdBy) {
|
|
369
|
+
q += " AND created_by = ?";
|
|
370
|
+
params.push(opts.createdBy);
|
|
371
|
+
}
|
|
372
|
+
q += " ORDER BY created_at DESC";
|
|
373
|
+
return this.db.prepare(q).all(...params).map((r) => this.mapApiKey(r));
|
|
374
|
+
}
|
|
375
|
+
async revokeApiKey(id) {
|
|
376
|
+
this.db.prepare("UPDATE api_keys SET revoked = 1 WHERE id = ?").run(id);
|
|
377
|
+
}
|
|
378
|
+
// ─── Rules ───────────────────────────────────────────────
|
|
379
|
+
async createRule(rule) {
|
|
380
|
+
const id = randomUUID();
|
|
381
|
+
this.db.prepare(
|
|
382
|
+
`INSERT INTO email_rules (id, name, agent_id, conditions, actions, priority, enabled)
|
|
383
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
|
384
|
+
).run(
|
|
385
|
+
id,
|
|
386
|
+
rule.name,
|
|
387
|
+
rule.agentId || null,
|
|
388
|
+
JSON.stringify(rule.conditions),
|
|
389
|
+
JSON.stringify(rule.actions),
|
|
390
|
+
rule.priority,
|
|
391
|
+
rule.enabled ? 1 : 0
|
|
392
|
+
);
|
|
393
|
+
const r = this.db.prepare("SELECT * FROM email_rules WHERE id = ?").get(id);
|
|
394
|
+
return this.mapRule(r);
|
|
395
|
+
}
|
|
396
|
+
async getRules(agentId) {
|
|
397
|
+
let q = "SELECT * FROM email_rules";
|
|
398
|
+
const params = [];
|
|
399
|
+
if (agentId) {
|
|
400
|
+
q += " WHERE agent_id = ? OR agent_id IS NULL";
|
|
401
|
+
params.push(agentId);
|
|
402
|
+
}
|
|
403
|
+
q += " ORDER BY priority DESC";
|
|
404
|
+
return this.db.prepare(q).all(...params).map((r) => this.mapRule(r));
|
|
405
|
+
}
|
|
406
|
+
async updateRule(id, updates) {
|
|
407
|
+
const fields = [];
|
|
408
|
+
const vals = [];
|
|
409
|
+
if (updates.name !== void 0) {
|
|
410
|
+
fields.push("name = ?");
|
|
411
|
+
vals.push(updates.name);
|
|
412
|
+
}
|
|
413
|
+
if (updates.conditions) {
|
|
414
|
+
fields.push("conditions = ?");
|
|
415
|
+
vals.push(JSON.stringify(updates.conditions));
|
|
416
|
+
}
|
|
417
|
+
if (updates.actions) {
|
|
418
|
+
fields.push("actions = ?");
|
|
419
|
+
vals.push(JSON.stringify(updates.actions));
|
|
420
|
+
}
|
|
421
|
+
if (updates.priority !== void 0) {
|
|
422
|
+
fields.push("priority = ?");
|
|
423
|
+
vals.push(updates.priority);
|
|
424
|
+
}
|
|
425
|
+
if (updates.enabled !== void 0) {
|
|
426
|
+
fields.push("enabled = ?");
|
|
427
|
+
vals.push(updates.enabled ? 1 : 0);
|
|
428
|
+
}
|
|
429
|
+
fields.push("updated_at = datetime('now')");
|
|
430
|
+
vals.push(id);
|
|
431
|
+
this.db.prepare(`UPDATE email_rules SET ${fields.join(", ")} WHERE id = ?`).run(...vals);
|
|
432
|
+
const r = this.db.prepare("SELECT * FROM email_rules WHERE id = ?").get(id);
|
|
433
|
+
return this.mapRule(r);
|
|
434
|
+
}
|
|
435
|
+
async deleteRule(id) {
|
|
436
|
+
this.db.prepare("DELETE FROM email_rules WHERE id = ?").run(id);
|
|
437
|
+
}
|
|
438
|
+
// ─── Retention ───────────────────────────────────────────
|
|
439
|
+
async getRetentionPolicy() {
|
|
440
|
+
const r = this.db.prepare("SELECT * FROM retention_policy WHERE id = ?").get("default");
|
|
441
|
+
if (!r) return { enabled: false, retainDays: 365, archiveFirst: true };
|
|
442
|
+
return { enabled: !!r.enabled, retainDays: r.retain_days, excludeTags: JSON.parse(r.exclude_tags || "[]"), archiveFirst: !!r.archive_first };
|
|
443
|
+
}
|
|
444
|
+
async setRetentionPolicy(policy) {
|
|
445
|
+
this.db.prepare(
|
|
446
|
+
`UPDATE retention_policy SET enabled = ?, retain_days = ?, exclude_tags = ?, archive_first = ? WHERE id = 'default'`
|
|
447
|
+
).run(policy.enabled ? 1 : 0, policy.retainDays, JSON.stringify(policy.excludeTags || []), policy.archiveFirst ? 1 : 0);
|
|
448
|
+
}
|
|
449
|
+
// ─── Stats ───────────────────────────────────────────────
|
|
450
|
+
async getStats() {
|
|
451
|
+
return {
|
|
452
|
+
totalAgents: this.db.prepare("SELECT COUNT(*) as c FROM agents").get().c,
|
|
453
|
+
activeAgents: this.db.prepare("SELECT COUNT(*) as c FROM agents WHERE status = 'active'").get().c,
|
|
454
|
+
totalUsers: this.db.prepare("SELECT COUNT(*) as c FROM users").get().c,
|
|
455
|
+
totalEmails: 0,
|
|
456
|
+
totalAuditEvents: this.db.prepare("SELECT COUNT(*) as c FROM audit_log").get().c
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
// ─── Mappers ─────────────────────────────────────────────
|
|
460
|
+
mapAgent(r) {
|
|
461
|
+
return {
|
|
462
|
+
id: r.id,
|
|
463
|
+
name: r.name,
|
|
464
|
+
email: r.email,
|
|
465
|
+
role: r.role,
|
|
466
|
+
status: r.status,
|
|
467
|
+
metadata: typeof r.metadata === "string" ? JSON.parse(r.metadata) : r.metadata,
|
|
468
|
+
createdAt: new Date(r.created_at),
|
|
469
|
+
updatedAt: new Date(r.updated_at),
|
|
470
|
+
createdBy: r.created_by
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
mapUser(r) {
|
|
474
|
+
return {
|
|
475
|
+
id: r.id,
|
|
476
|
+
email: r.email,
|
|
477
|
+
name: r.name,
|
|
478
|
+
role: r.role,
|
|
479
|
+
passwordHash: r.password_hash,
|
|
480
|
+
ssoProvider: r.sso_provider,
|
|
481
|
+
ssoSubject: r.sso_subject,
|
|
482
|
+
createdAt: new Date(r.created_at),
|
|
483
|
+
updatedAt: new Date(r.updated_at),
|
|
484
|
+
lastLoginAt: r.last_login_at ? new Date(r.last_login_at) : void 0
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
mapApiKey(r) {
|
|
488
|
+
return {
|
|
489
|
+
id: r.id,
|
|
490
|
+
name: r.name,
|
|
491
|
+
keyHash: r.key_hash,
|
|
492
|
+
keyPrefix: r.key_prefix,
|
|
493
|
+
scopes: typeof r.scopes === "string" ? JSON.parse(r.scopes) : r.scopes,
|
|
494
|
+
createdBy: r.created_by,
|
|
495
|
+
createdAt: new Date(r.created_at),
|
|
496
|
+
lastUsedAt: r.last_used_at ? new Date(r.last_used_at) : void 0,
|
|
497
|
+
expiresAt: r.expires_at ? new Date(r.expires_at) : void 0,
|
|
498
|
+
revoked: !!r.revoked
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
mapRule(r) {
|
|
502
|
+
return {
|
|
503
|
+
id: r.id,
|
|
504
|
+
name: r.name,
|
|
505
|
+
agentId: r.agent_id,
|
|
506
|
+
conditions: typeof r.conditions === "string" ? JSON.parse(r.conditions) : r.conditions,
|
|
507
|
+
actions: typeof r.actions === "string" ? JSON.parse(r.actions) : r.actions,
|
|
508
|
+
priority: r.priority,
|
|
509
|
+
enabled: !!r.enabled,
|
|
510
|
+
createdAt: new Date(r.created_at),
|
|
511
|
+
updatedAt: new Date(r.updated_at)
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
mapSettings(r) {
|
|
515
|
+
return {
|
|
516
|
+
id: r.id,
|
|
517
|
+
name: r.name,
|
|
518
|
+
domain: r.domain,
|
|
519
|
+
subdomain: r.subdomain,
|
|
520
|
+
smtpHost: r.smtp_host,
|
|
521
|
+
smtpPort: r.smtp_port,
|
|
522
|
+
smtpUser: r.smtp_user,
|
|
523
|
+
smtpPass: r.smtp_pass,
|
|
524
|
+
dkimPrivateKey: r.dkim_private_key,
|
|
525
|
+
logoUrl: r.logo_url,
|
|
526
|
+
primaryColor: r.primary_color,
|
|
527
|
+
ssoConfig: r.sso_config ? typeof r.sso_config === "string" ? JSON.parse(r.sso_config) : r.sso_config : void 0,
|
|
528
|
+
toolSecurityConfig: r.tool_security_config ? typeof r.tool_security_config === "string" ? JSON.parse(r.tool_security_config) : r.tool_security_config : {},
|
|
529
|
+
firewallConfig: r.firewall_config ? typeof r.firewall_config === "string" ? JSON.parse(r.firewall_config) : r.firewall_config : {},
|
|
530
|
+
modelPricingConfig: r.model_pricing_config ? typeof r.model_pricing_config === "string" ? JSON.parse(r.model_pricing_config) : r.model_pricing_config : {},
|
|
531
|
+
plan: r.plan,
|
|
532
|
+
createdAt: new Date(r.created_at),
|
|
533
|
+
updatedAt: new Date(r.updated_at),
|
|
534
|
+
deploymentKeyHash: r.deployment_key_hash,
|
|
535
|
+
domainRegistrationId: r.domain_registration_id,
|
|
536
|
+
domainDnsChallenge: r.domain_dns_challenge,
|
|
537
|
+
domainVerifiedAt: r.domain_verified_at || void 0,
|
|
538
|
+
domainRegisteredAt: r.domain_registered_at || void 0,
|
|
539
|
+
domainStatus: r.domain_status || "unregistered"
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
export {
|
|
544
|
+
SqliteAdapter
|
|
545
|
+
};
|