@agenticmail/api 0.7.2 → 0.7.3

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 (2) hide show
  1. package/dist/index.js +105 -20
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -246,8 +246,72 @@ function createHealthRoutes(stalwart) {
246
246
  }
247
247
 
248
248
  // src/routes/accounts.ts
249
- import { Router as Router2 } from "express";
249
+ import { Router as Router3 } from "express";
250
250
  import { AGENT_ROLES, AgentDeletionService } from "@agenticmail/core";
251
+
252
+ // src/routes/system-events.ts
253
+ import { Router as Router2 } from "express";
254
+ var listeners = /* @__PURE__ */ new Set();
255
+ function pushSystemEvent(event) {
256
+ if (listeners.size === 0) return;
257
+ const data = `data: ${JSON.stringify(event)}
258
+
259
+ `;
260
+ for (const entry of listeners) {
261
+ try {
262
+ entry.res.write(data);
263
+ } catch {
264
+ }
265
+ }
266
+ }
267
+ function closeAllSystemEventListeners() {
268
+ for (const entry of listeners) {
269
+ try {
270
+ entry.cleanup();
271
+ } catch {
272
+ }
273
+ try {
274
+ entry.res.end();
275
+ } catch {
276
+ }
277
+ }
278
+ listeners.clear();
279
+ }
280
+ function createSystemEventRoutes() {
281
+ const router = Router2();
282
+ router.get("/system/events", requireMaster, (req, res) => {
283
+ res.setHeader("Content-Type", "text/event-stream");
284
+ res.setHeader("Cache-Control", "no-cache");
285
+ res.setHeader("Connection", "keep-alive");
286
+ res.setHeader("X-Accel-Buffering", "no");
287
+ res.flushHeaders();
288
+ res.write(`data: ${JSON.stringify({ type: "connected" })}
289
+
290
+ `);
291
+ const pingTimer = setInterval(() => {
292
+ try {
293
+ res.write(`: ping
294
+
295
+ `);
296
+ } catch {
297
+ }
298
+ }, 3e4);
299
+ const entry = {
300
+ res,
301
+ cleanup: () => {
302
+ clearInterval(pingTimer);
303
+ }
304
+ };
305
+ listeners.add(entry);
306
+ req.on("close", () => {
307
+ entry.cleanup();
308
+ listeners.delete(entry);
309
+ });
310
+ });
311
+ return router;
312
+ }
313
+
314
+ // src/routes/accounts.ts
251
315
  function sanitizeAgent(agent) {
252
316
  if (!agent) return agent;
253
317
  const { metadata, ...rest } = agent;
@@ -261,7 +325,7 @@ function sanitizeAgent(agent) {
261
325
  return agent;
262
326
  }
263
327
  function createAccountRoutes(accountManager, db, config) {
264
- const router = Router2();
328
+ const router = Router3();
265
329
  const deletionService = new AgentDeletionService(db, accountManager, config);
266
330
  router.post("/accounts", requireMaster, async (req, res, next) => {
267
331
  if (!req.body || typeof req.body !== "object") {
@@ -306,6 +370,13 @@ function createAccountRoutes(accountManager, db, config) {
306
370
  } catch {
307
371
  }
308
372
  }
373
+ try {
374
+ pushSystemEvent({
375
+ type: "account_created",
376
+ account: sanitizeAgent(agent)
377
+ });
378
+ } catch {
379
+ }
309
380
  res.status(201).json(sanitizeAgent(agent));
310
381
  } catch (err) {
311
382
  const msg = err.message ?? "";
@@ -463,9 +534,14 @@ function createAccountRoutes(accountManager, db, config) {
463
534
  const archive = req.query.archive !== "false";
464
535
  const reason = req.query.reason || void 0;
465
536
  const deletedBy = req.query.deletedBy || "api";
537
+ const deletingAgent = allAgents.find((a) => a.id === req.params.id);
466
538
  if (archive) {
467
539
  const report = await deletionService.archiveAndDelete(req.params.id, { deletedBy, reason });
468
540
  const { emails: _emails, ...summary } = report;
541
+ try {
542
+ pushSystemEvent({ type: "account_deleted", accountId: req.params.id, name: deletingAgent?.name });
543
+ } catch {
544
+ }
469
545
  res.json(summary);
470
546
  } else {
471
547
  const deleted = await accountManager.delete(req.params.id);
@@ -473,6 +549,10 @@ function createAccountRoutes(accountManager, db, config) {
473
549
  res.status(404).json({ error: "Agent not found" });
474
550
  return;
475
551
  }
552
+ try {
553
+ pushSystemEvent({ type: "account_deleted", accountId: req.params.id, name: deletingAgent?.name });
554
+ } catch {
555
+ }
476
556
  res.status(204).send();
477
557
  }
478
558
  } catch (err) {
@@ -488,7 +568,7 @@ function createAccountRoutes(accountManager, db, config) {
488
568
  }
489
569
 
490
570
  // src/routes/mail.ts
491
- import { Router as Router5 } from "express";
571
+ import { Router as Router6 } from "express";
492
572
  import crypto from "crypto";
493
573
  import {
494
574
  MailSender as MailSender2,
@@ -501,7 +581,7 @@ import {
501
581
  } from "@agenticmail/core";
502
582
 
503
583
  // src/routes/events.ts
504
- import { Router as Router4 } from "express";
584
+ import { Router as Router5 } from "express";
505
585
  import {
506
586
  InboxWatcher,
507
587
  MailReceiver,
@@ -513,7 +593,7 @@ import {
513
593
  import { v4 as uuidv42 } from "uuid";
514
594
 
515
595
  // src/routes/features.ts
516
- import { Router as Router3 } from "express";
596
+ import { Router as Router4 } from "express";
517
597
  import { v4 as uuidv4 } from "uuid";
518
598
  import {
519
599
  MailSender
@@ -633,7 +713,7 @@ function parseScheduleTime(input) {
633
713
  return isNaN(fallback.getTime()) ? null : fallback;
634
714
  }
635
715
  function createFeatureRoutes(db, _accountManager, config, gatewayManager) {
636
- const router = Router3();
716
+ const router = Router4();
637
717
  router.get("/contacts", requireAgent, async (req, res, next) => {
638
718
  try {
639
719
  const rows = db.prepare("SELECT * FROM contacts WHERE agent_id = ? ORDER BY name, email").all(req.agent.id);
@@ -1210,7 +1290,7 @@ async function closeAllWatchers() {
1210
1290
  activeWatchers.clear();
1211
1291
  }
1212
1292
  function createEventRoutes(accountManager, config, db) {
1213
- const router = Router4();
1293
+ const router = Router5();
1214
1294
  router.get("/events", requireAgent, async (req, res, next) => {
1215
1295
  try {
1216
1296
  const agent = req.agent;
@@ -1673,7 +1753,7 @@ function saveSentCopy(authUser, password, config, raw) {
1673
1753
  })();
1674
1754
  }
1675
1755
  function createMailRoutes(accountManager, config, db, gatewayManager) {
1676
- const router = Router5();
1756
+ const router = Router6();
1677
1757
  router.post("/mail/send", requireAgent, async (req, res, next) => {
1678
1758
  try {
1679
1759
  if (!req.body || typeof req.body !== "object") {
@@ -2449,7 +2529,7 @@ function createMailRoutes(accountManager, config, db, gatewayManager) {
2449
2529
  }
2450
2530
 
2451
2531
  // src/routes/inbound.ts
2452
- import { Router as Router6 } from "express";
2532
+ import { Router as Router7 } from "express";
2453
2533
  import { randomUUID } from "crypto";
2454
2534
  import {
2455
2535
  parseEmail as parseEmail3,
@@ -2463,7 +2543,7 @@ var INBOUND_SECRET = process.env.AGENTICMAIL_INBOUND_SECRET || (() => {
2463
2543
  })();
2464
2544
  var DEBUG = () => !!process.env.AGENTICMAIL_DEBUG;
2465
2545
  function createInboundRoutes(accountManager, config, gatewayManager) {
2466
- const router = Router6();
2546
+ const router = Router7();
2467
2547
  router.post("/mail/inbound", async (req, res, next) => {
2468
2548
  try {
2469
2549
  const secret = req.headers["x-inbound-secret"];
@@ -2540,9 +2620,9 @@ function createInboundRoutes(accountManager, config, gatewayManager) {
2540
2620
  }
2541
2621
 
2542
2622
  // src/routes/domains.ts
2543
- import { Router as Router7 } from "express";
2623
+ import { Router as Router8 } from "express";
2544
2624
  function createDomainRoutes(domainManager) {
2545
- const router = Router7();
2625
+ const router = Router8();
2546
2626
  router.post("/domains", requireMaster, async (req, res, next) => {
2547
2627
  try {
2548
2628
  const { domain } = req.body;
@@ -2596,13 +2676,13 @@ function createDomainRoutes(domainManager) {
2596
2676
  }
2597
2677
 
2598
2678
  // src/routes/gateway.ts
2599
- import { Router as Router8 } from "express";
2679
+ import { Router as Router9 } from "express";
2600
2680
  import {
2601
2681
  RELAY_PRESETS,
2602
2682
  AGENT_ROLES as AGENT_ROLES2
2603
2683
  } from "@agenticmail/core";
2604
2684
  function createGatewayRoutes(gatewayManager) {
2605
- const router = Router8();
2685
+ const router = Router9();
2606
2686
  router.get("/gateway/setup-guide", requireMaster, async (_req, res) => {
2607
2687
  res.json({
2608
2688
  modes: [
@@ -2958,12 +3038,12 @@ function createGatewayRoutes(gatewayManager) {
2958
3038
  }
2959
3039
 
2960
3040
  // src/routes/tasks.ts
2961
- import { Router as Router9 } from "express";
3041
+ import { Router as Router10 } from "express";
2962
3042
  import { v4 as uuidv43 } from "uuid";
2963
3043
  import { MailSender as MailSender4 } from "@agenticmail/core";
2964
3044
  var rpcResolvers = /* @__PURE__ */ new Map();
2965
3045
  function createTaskRoutes(db, accountManager, config) {
2966
- const router = Router9();
3046
+ const router = Router10();
2967
3047
  router.post("/tasks/assign", requireAuth, async (req, res, next) => {
2968
3048
  try {
2969
3049
  const { assignee, taskType, payload, expiresInSeconds } = req.body || {};
@@ -3284,7 +3364,7 @@ function parseTask(row) {
3284
3364
  }
3285
3365
 
3286
3366
  // src/routes/sms.ts
3287
- import { Router as Router10 } from "express";
3367
+ import { Router as Router11 } from "express";
3288
3368
  import {
3289
3369
  SmsManager,
3290
3370
  parseGoogleVoiceSms,
@@ -3293,7 +3373,7 @@ import {
3293
3373
  isValidPhoneNumber
3294
3374
  } from "@agenticmail/core";
3295
3375
  function createSmsRoutes(db, accountManager, config, gatewayManager) {
3296
- const router = Router10();
3376
+ const router = Router11();
3297
3377
  const smsManager = new SmsManager(db);
3298
3378
  function getAgent(req, res) {
3299
3379
  const agent = req.agent;
@@ -3535,7 +3615,7 @@ function createSmsRoutes(db, accountManager, config, gatewayManager) {
3535
3615
  }
3536
3616
 
3537
3617
  // src/routes/storage.ts
3538
- import { Router as Router11 } from "express";
3618
+ import { Router as Router12 } from "express";
3539
3619
  function mapColumnType(col, dialect) {
3540
3620
  const typeMap = {
3541
3621
  sqlite: { text: "TEXT", integer: "INTEGER", real: "REAL", boolean: "INTEGER", json: "JSON", blob: "BLOB", timestamp: "TEXT" },
@@ -3682,7 +3762,7 @@ function adaptBetterSqlite(raw) {
3682
3762
  }
3683
3763
  function createStorageRoutes(rawDb, accountManager, config, dialect = "sqlite") {
3684
3764
  const db = adaptBetterSqlite(rawDb);
3685
- const router = Router11();
3765
+ const router = Router12();
3686
3766
  function getAgent(req, res) {
3687
3767
  const agent = req.agent;
3688
3768
  if (!agent) {
@@ -4595,6 +4675,7 @@ function createApp(configOverrides) {
4595
4675
  app2.use("/api/agenticmail", createTaskRoutes(db, accountManager, config));
4596
4676
  app2.use("/api/agenticmail", createSmsRoutes(db, accountManager, config, gatewayManager));
4597
4677
  app2.use("/api/agenticmail", createStorageRoutes(db, accountManager, config));
4678
+ app2.use("/api/agenticmail", createSystemEventRoutes());
4598
4679
  app2.use("/api/agenticmail", (_req, res) => {
4599
4680
  res.status(404).json({ error: "Not found" });
4600
4681
  });
@@ -4700,6 +4781,10 @@ async function shutdown() {
4700
4781
  await closeAllWatchers();
4701
4782
  } catch {
4702
4783
  }
4784
+ try {
4785
+ closeAllSystemEventListeners();
4786
+ } catch {
4787
+ }
4703
4788
  try {
4704
4789
  await closeCaches();
4705
4790
  } catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agenticmail/api",
3
- "version": "0.7.2",
3
+ "version": "0.7.3",
4
4
  "description": "REST API server for AgenticMail — email and SMS endpoints for AI agents",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",