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