@exulu/backend 5.17.2 → 5.18.0

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/index.cjs CHANGED
@@ -26073,12 +26073,12 @@ var channelView = (row) => ({
26073
26073
  needsCredentials: readSecrets(row) === null
26074
26074
  });
26075
26075
  var maskIdentity = (identity) => identity.length < 4 ? "\u2022\u2022\u2022\u2022" : `\u2022\u2022\u2022\u2022 ${identity.slice(-4)}`;
26076
- var linkPageUrl = (agentId) => {
26076
+ var linkPageUrl = (agentId, locale) => {
26077
26077
  const base = [process.env.CHANNEL_LINK_BASE_URL, process.env.FRONTEND, process.env.NEXTAUTH_URL].map((v) => (v ?? "").trim()).find((v) => v !== "");
26078
26078
  if (!base) {
26079
26079
  throw new ChannelSetupError("None of CHANNEL_LINK_BASE_URL, FRONTEND and NEXTAUTH_URL is set; the channel link page has no URL");
26080
26080
  }
26081
- return `${base.replace(/\/+$/, "")}/public/agents/${encodeURIComponent(agentId)}/link/whatsapp`;
26081
+ return `${base.replace(/\/+$/, "")}/public/agents/${encodeURIComponent(agentId)}/link/whatsapp${locale ? `?lang=${locale}` : ""}`;
26082
26082
  };
26083
26083
 
26084
26084
  // src/exulu/channels/decide.ts
@@ -26592,11 +26592,19 @@ var registerChannelApi = (app, deps) => {
26592
26592
  if (!row) return void res.json({ links: [] });
26593
26593
  const at = now();
26594
26594
  const links = await createLinkStore(db2).listForChannel(row.id);
26595
+ const mayReadUsers = gate.user.super_admin === true || gate.user.role?.users === "read" || gate.user.role?.users === "write";
26596
+ const emails = /* @__PURE__ */ new Map();
26597
+ const ids = [...new Set(links.map((l) => l.user))];
26598
+ if (mayReadUsers && ids.length > 0) {
26599
+ const owners = await db2("users").whereIn("id", ids).select("id", "email");
26600
+ for (const o of owners) emails.set(o.id, o.email ?? null);
26601
+ }
26595
26602
  res.json({
26596
26603
  links: links.map((l) => ({
26597
26604
  id: l.id,
26598
26605
  identityMasked: maskIdentity(l.identity),
26599
26606
  userId: l.user,
26607
+ userEmail: emails.get(l.user) ?? null,
26600
26608
  linked_at: l.linked_at,
26601
26609
  expires_at: l.expires_at,
26602
26610
  last_seen_at: l.last_seen_at,
@@ -26605,6 +26613,19 @@ var registerChannelApi = (app, deps) => {
26605
26613
  });
26606
26614
  })
26607
26615
  );
26616
+ app.post(
26617
+ `${base}/verify-token`,
26618
+ route("rotate verify token", async (req, res) => {
26619
+ const gate = await adminAgent(req, res);
26620
+ if (!gate) return;
26621
+ const db2 = await deps.getDb();
26622
+ const row = await loadChannel(db2, gate.agent.id);
26623
+ if (!row) return void res.status(404).json({ detail: "Channel not found." });
26624
+ const verifyToken = (0, import_crypto7.randomBytes)(24).toString("base64url");
26625
+ await db2(TABLE4).where({ id: row.id }).update({ verify_token: verifyToken, updatedAt: now() });
26626
+ res.json({ verifyToken });
26627
+ })
26628
+ );
26608
26629
  app.delete(
26609
26630
  `${base}/links/:linkId`,
26610
26631
  route("revoke link", async (req, res) => {
@@ -26625,24 +26646,53 @@ var registerChannelApi = (app, deps) => {
26625
26646
  res.status(204).end();
26626
26647
  })
26627
26648
  );
26628
- const customerChannel = async (req, opts) => {
26649
+ const customerChannel = async (req) => {
26629
26650
  const agent = await loadAgent(String(req.params.id ?? ""));
26630
26651
  if (!agent) return null;
26631
26652
  const db2 = await deps.getDb();
26632
26653
  const row = await loadChannel(db2, agent.id);
26633
26654
  if (!row) return null;
26634
- if (opts.requireEnabled && !row.enabled) return null;
26635
26655
  return { db: db2, agent, row };
26636
26656
  };
26657
+ const linkableConfig = (agent, row) => {
26658
+ if (!deps.entitled() || !row.enabled) return null;
26659
+ const config = tryParseConfig(row.config);
26660
+ return config && canEnableChannel(agent).ok ? config : null;
26661
+ };
26662
+ app.get(
26663
+ "/public-agents/:id/channels/whatsapp/links",
26664
+ route("customer links", async (req, res) => {
26665
+ const user = await signedIn(req, res);
26666
+ if (!user) return;
26667
+ const none = { available: false, links: [] };
26668
+ if (!deps.entitled()) return void res.json(none);
26669
+ const agent = await loadAgent(String(req.params.id ?? ""));
26670
+ if (!agent) return void res.json(none);
26671
+ const db2 = await deps.getDb();
26672
+ const row = await loadChannel(db2, agent.id);
26673
+ const links = row ? await createLinkStore(db2).listForUser(row.id, user.id) : [];
26674
+ const available = !!row && linkableConfig(agent, row) !== null && await channelAccessAllowed({
26675
+ db: db2,
26676
+ loadAgent: (id) => loadAgent(id),
26677
+ agentId: agent.id,
26678
+ channelId: row.id,
26679
+ userId: user.id
26680
+ });
26681
+ res.json({
26682
+ available,
26683
+ links: links.filter((l) => !l.revoked_at).map((l) => ({ id: l.id, identityMasked: maskIdentity(l.identity), expires_at: l.expires_at }))
26684
+ });
26685
+ })
26686
+ );
26637
26687
  app.post(
26638
26688
  "/public-agents/:id/channels/whatsapp/link-code",
26639
26689
  route("issue link code", async (req, res) => {
26640
26690
  const user = await signedIn(req, res);
26641
26691
  if (!user) return;
26642
26692
  if (!deps.entitled()) return void res.status(404).json(NOT_FOUND);
26643
- const found = await customerChannel(req, { requireEnabled: true });
26644
- const config = found ? tryParseConfig(found.row.config) : null;
26645
- if (!found || !config || !canEnableChannel(found.agent).ok) return void res.status(404).json(NOT_FOUND);
26693
+ const found = await customerChannel(req);
26694
+ const config = found ? linkableConfig(found.agent, found.row) : null;
26695
+ if (!found || !config) return void res.status(404).json(NOT_FOUND);
26646
26696
  const { db: db2, agent, row } = found;
26647
26697
  const allowed = await channelAccessAllowed({
26648
26698
  db: db2,
@@ -26687,7 +26737,7 @@ var registerChannelApi = (app, deps) => {
26687
26737
  if (!user) return;
26688
26738
  const linkId = String(req.params.linkId ?? "");
26689
26739
  if (!UUID_RE2.test(linkId)) return void res.status(404).json(NOT_FOUND);
26690
- const found = await customerChannel(req, { requireEnabled: false });
26740
+ const found = await customerChannel(req);
26691
26741
  if (!found) return void res.status(404).json(NOT_FOUND);
26692
26742
  const { db: db2, row } = found;
26693
26743
  const link = await db2("channel_links").where({ id: linkId, channel: row.id, user: user.id }).first();
@@ -31365,7 +31415,7 @@ var createDecideDeps = (p) => {
31365
31415
  now: env.now ?? (() => /* @__PURE__ */ new Date()),
31366
31416
  config,
31367
31417
  // lazily: a missing URL variable must only affect the link reply, never linked users
31368
- linkUrl: () => linkPageUrl(row.agent),
31418
+ linkUrl: () => linkPageUrl(row.agent, config.locale),
31369
31419
  links: createLinkStore(db2),
31370
31420
  consumeCode: (code) => consumeCode(store, code),
31371
31421
  codeAttemptsExceeded: (identity) => codeAttemptsExceeded(store, row.id, identity),
@@ -42965,6 +43015,25 @@ See docs/superpowers/specs/2026-05-31-in-chat-image-generation-design.md for the
42965
43015
  // src/exulu/routes.ts
42966
43016
  var import_node_path15 = require("path");
42967
43017
  init_rbac_resolver();
43018
+
43019
+ // src/exulu/session-access.ts
43020
+ init_cjs_shims();
43021
+ init_rbac_resolver();
43022
+ init_check_record_access();
43023
+ var SESSION_RBAC_ENTITY = "agent_session";
43024
+ async function hasSessionAccess(db2, session, rights, user) {
43025
+ const rbac = await RBACResolver(
43026
+ db2,
43027
+ SESSION_RBAC_ENTITY,
43028
+ session.id,
43029
+ session.rights_mode || "private"
43030
+ );
43031
+ const record = { ...session, RBAC: rbac };
43032
+ if (await checkRecordAccess(record, rights, user)) return true;
43033
+ return rights === "read" ? checkRecordAccess(record, "write", user) : false;
43034
+ }
43035
+
43036
+ // src/exulu/routes.ts
42968
43037
  init_tags();
42969
43038
  init_admin_client();
42970
43039
  init_env();
@@ -45032,17 +45101,7 @@ ${outputSchemaInstructions(outputSchema)}` : baseInstructions;
45032
45101
  res.status(404).json({ detail: `Session ${sessionId} not found.` });
45033
45102
  return null;
45034
45103
  }
45035
- const sessionRbac = await RBACResolver(
45036
- db2,
45037
- "agent_sessions",
45038
- session.id,
45039
- session.rights_mode || "private"
45040
- );
45041
- const allowed = await checkRecordAccess(
45042
- { ...session, RBAC: sessionRbac },
45043
- rights,
45044
- authResult.user
45045
- );
45104
+ const allowed = await hasSessionAccess(db2, session, rights, authResult.user);
45046
45105
  if (!allowed) {
45047
45106
  res.status(403).json({ detail: `You don't have ${rights} access to this session.` });
45048
45107
  return null;
@@ -45070,7 +45129,7 @@ ${outputSchemaInstructions(outputSchema)}` : baseInstructions;
45070
45129
  }
45071
45130
  const rbac = await RBACResolver(
45072
45131
  db2,
45073
- "platform_configurations",
45132
+ "platform_configuration",
45074
45133
  row.id,
45075
45134
  row.rights_mode || "private"
45076
45135
  );
@@ -49195,7 +49254,7 @@ var loadAvailableStyles = async (user) => {
49195
49254
  for (const row of rows) {
49196
49255
  const rbac = await RBACResolver(
49197
49256
  db2,
49198
- "platform_configurations",
49257
+ "platform_configuration",
49199
49258
  row.id,
49200
49259
  row.rights_mode || "private"
49201
49260
  );
package/dist/index.js CHANGED
@@ -2965,12 +2965,12 @@ var channelView = (row) => ({
2965
2965
  needsCredentials: readSecrets(row) === null
2966
2966
  });
2967
2967
  var maskIdentity = (identity) => identity.length < 4 ? "\u2022\u2022\u2022\u2022" : `\u2022\u2022\u2022\u2022 ${identity.slice(-4)}`;
2968
- var linkPageUrl = (agentId) => {
2968
+ var linkPageUrl = (agentId, locale) => {
2969
2969
  const base = [process.env.CHANNEL_LINK_BASE_URL, process.env.FRONTEND, process.env.NEXTAUTH_URL].map((v) => (v ?? "").trim()).find((v) => v !== "");
2970
2970
  if (!base) {
2971
2971
  throw new ChannelSetupError("None of CHANNEL_LINK_BASE_URL, FRONTEND and NEXTAUTH_URL is set; the channel link page has no URL");
2972
2972
  }
2973
- return `${base.replace(/\/+$/, "")}/public/agents/${encodeURIComponent(agentId)}/link/whatsapp`;
2973
+ return `${base.replace(/\/+$/, "")}/public/agents/${encodeURIComponent(agentId)}/link/whatsapp${locale ? `?lang=${locale}` : ""}`;
2974
2974
  };
2975
2975
 
2976
2976
  // src/exulu/channels/link-codes.ts
@@ -3478,11 +3478,19 @@ var registerChannelApi = (app, deps) => {
3478
3478
  if (!row) return void res.json({ links: [] });
3479
3479
  const at = now();
3480
3480
  const links = await createLinkStore(db).listForChannel(row.id);
3481
+ const mayReadUsers = gate.user.super_admin === true || gate.user.role?.users === "read" || gate.user.role?.users === "write";
3482
+ const emails = /* @__PURE__ */ new Map();
3483
+ const ids = [...new Set(links.map((l) => l.user))];
3484
+ if (mayReadUsers && ids.length > 0) {
3485
+ const owners = await db("users").whereIn("id", ids).select("id", "email");
3486
+ for (const o of owners) emails.set(o.id, o.email ?? null);
3487
+ }
3481
3488
  res.json({
3482
3489
  links: links.map((l) => ({
3483
3490
  id: l.id,
3484
3491
  identityMasked: maskIdentity(l.identity),
3485
3492
  userId: l.user,
3493
+ userEmail: emails.get(l.user) ?? null,
3486
3494
  linked_at: l.linked_at,
3487
3495
  expires_at: l.expires_at,
3488
3496
  last_seen_at: l.last_seen_at,
@@ -3491,6 +3499,19 @@ var registerChannelApi = (app, deps) => {
3491
3499
  });
3492
3500
  })
3493
3501
  );
3502
+ app.post(
3503
+ `${base}/verify-token`,
3504
+ route("rotate verify token", async (req, res) => {
3505
+ const gate = await adminAgent(req, res);
3506
+ if (!gate) return;
3507
+ const db = await deps.getDb();
3508
+ const row = await loadChannel(db, gate.agent.id);
3509
+ if (!row) return void res.status(404).json({ detail: "Channel not found." });
3510
+ const verifyToken = randomBytes(24).toString("base64url");
3511
+ await db(TABLE3).where({ id: row.id }).update({ verify_token: verifyToken, updatedAt: now() });
3512
+ res.json({ verifyToken });
3513
+ })
3514
+ );
3494
3515
  app.delete(
3495
3516
  `${base}/links/:linkId`,
3496
3517
  route("revoke link", async (req, res) => {
@@ -3511,24 +3532,53 @@ var registerChannelApi = (app, deps) => {
3511
3532
  res.status(204).end();
3512
3533
  })
3513
3534
  );
3514
- const customerChannel = async (req, opts) => {
3535
+ const customerChannel = async (req) => {
3515
3536
  const agent = await loadAgent(String(req.params.id ?? ""));
3516
3537
  if (!agent) return null;
3517
3538
  const db = await deps.getDb();
3518
3539
  const row = await loadChannel(db, agent.id);
3519
3540
  if (!row) return null;
3520
- if (opts.requireEnabled && !row.enabled) return null;
3521
3541
  return { db, agent, row };
3522
3542
  };
3543
+ const linkableConfig = (agent, row) => {
3544
+ if (!deps.entitled() || !row.enabled) return null;
3545
+ const config = tryParseConfig(row.config);
3546
+ return config && canEnableChannel(agent).ok ? config : null;
3547
+ };
3548
+ app.get(
3549
+ "/public-agents/:id/channels/whatsapp/links",
3550
+ route("customer links", async (req, res) => {
3551
+ const user = await signedIn(req, res);
3552
+ if (!user) return;
3553
+ const none = { available: false, links: [] };
3554
+ if (!deps.entitled()) return void res.json(none);
3555
+ const agent = await loadAgent(String(req.params.id ?? ""));
3556
+ if (!agent) return void res.json(none);
3557
+ const db = await deps.getDb();
3558
+ const row = await loadChannel(db, agent.id);
3559
+ const links = row ? await createLinkStore(db).listForUser(row.id, user.id) : [];
3560
+ const available = !!row && linkableConfig(agent, row) !== null && await channelAccessAllowed({
3561
+ db,
3562
+ loadAgent: (id) => loadAgent(id),
3563
+ agentId: agent.id,
3564
+ channelId: row.id,
3565
+ userId: user.id
3566
+ });
3567
+ res.json({
3568
+ available,
3569
+ links: links.filter((l) => !l.revoked_at).map((l) => ({ id: l.id, identityMasked: maskIdentity(l.identity), expires_at: l.expires_at }))
3570
+ });
3571
+ })
3572
+ );
3523
3573
  app.post(
3524
3574
  "/public-agents/:id/channels/whatsapp/link-code",
3525
3575
  route("issue link code", async (req, res) => {
3526
3576
  const user = await signedIn(req, res);
3527
3577
  if (!user) return;
3528
3578
  if (!deps.entitled()) return void res.status(404).json(NOT_FOUND);
3529
- const found = await customerChannel(req, { requireEnabled: true });
3530
- const config = found ? tryParseConfig(found.row.config) : null;
3531
- if (!found || !config || !canEnableChannel(found.agent).ok) return void res.status(404).json(NOT_FOUND);
3579
+ const found = await customerChannel(req);
3580
+ const config = found ? linkableConfig(found.agent, found.row) : null;
3581
+ if (!found || !config) return void res.status(404).json(NOT_FOUND);
3532
3582
  const { db, agent, row } = found;
3533
3583
  const allowed = await channelAccessAllowed({
3534
3584
  db,
@@ -3573,7 +3623,7 @@ var registerChannelApi = (app, deps) => {
3573
3623
  if (!user) return;
3574
3624
  const linkId = String(req.params.linkId ?? "");
3575
3625
  if (!UUID_RE2.test(linkId)) return void res.status(404).json(NOT_FOUND);
3576
- const found = await customerChannel(req, { requireEnabled: false });
3626
+ const found = await customerChannel(req);
3577
3627
  if (!found) return void res.status(404).json(NOT_FOUND);
3578
3628
  const { db, row } = found;
3579
3629
  const link = await db("channel_links").where({ id: linkId, channel: row.id, user: user.id }).first();
@@ -8087,7 +8137,7 @@ var createDecideDeps = (p) => {
8087
8137
  now: env.now ?? (() => /* @__PURE__ */ new Date()),
8088
8138
  config,
8089
8139
  // lazily: a missing URL variable must only affect the link reply, never linked users
8090
- linkUrl: () => linkPageUrl(row.agent),
8140
+ linkUrl: () => linkPageUrl(row.agent, config.locale),
8091
8141
  links: createLinkStore(db),
8092
8142
  consumeCode: (code) => consumeCode(store, code),
8093
8143
  codeAttemptsExceeded: (identity) => codeAttemptsExceeded(store, row.id, identity),
@@ -19463,6 +19513,20 @@ See docs/superpowers/specs/2026-05-31-in-chat-image-generation-design.md for the
19463
19513
  // src/exulu/routes.ts
19464
19514
  import { resolve as resolvePath } from "path";
19465
19515
 
19516
+ // src/exulu/session-access.ts
19517
+ var SESSION_RBAC_ENTITY = "agent_session";
19518
+ async function hasSessionAccess(db, session, rights, user) {
19519
+ const rbac = await RBACResolver(
19520
+ db,
19521
+ SESSION_RBAC_ENTITY,
19522
+ session.id,
19523
+ session.rights_mode || "private"
19524
+ );
19525
+ const record = { ...session, RBAC: rbac };
19526
+ if (await checkRecordAccess(record, rights, user)) return true;
19527
+ return rights === "read" ? checkRecordAccess(record, "write", user) : false;
19528
+ }
19529
+
19466
19530
  // src/exulu/litellm/passthrough-allowlist.ts
19467
19531
  var isLiteLLMPassthroughPathAllowed = (path) => path.startsWith("/v1/") || path === "/model/info";
19468
19532
 
@@ -21480,17 +21544,7 @@ ${outputSchemaInstructions(outputSchema)}` : baseInstructions;
21480
21544
  res.status(404).json({ detail: `Session ${sessionId} not found.` });
21481
21545
  return null;
21482
21546
  }
21483
- const sessionRbac = await RBACResolver(
21484
- db,
21485
- "agent_sessions",
21486
- session.id,
21487
- session.rights_mode || "private"
21488
- );
21489
- const allowed = await checkRecordAccess(
21490
- { ...session, RBAC: sessionRbac },
21491
- rights,
21492
- authResult.user
21493
- );
21547
+ const allowed = await hasSessionAccess(db, session, rights, authResult.user);
21494
21548
  if (!allowed) {
21495
21549
  res.status(403).json({ detail: `You don't have ${rights} access to this session.` });
21496
21550
  return null;
@@ -21518,7 +21572,7 @@ ${outputSchemaInstructions(outputSchema)}` : baseInstructions;
21518
21572
  }
21519
21573
  const rbac = await RBACResolver(
21520
21574
  db,
21521
- "platform_configurations",
21575
+ "platform_configuration",
21522
21576
  row.id,
21523
21577
  row.rights_mode || "private"
21524
21578
  );
@@ -25602,7 +25656,7 @@ var loadAvailableStyles = async (user) => {
25602
25656
  for (const row of rows) {
25603
25657
  const rbac = await RBACResolver(
25604
25658
  db,
25605
- "platform_configurations",
25659
+ "platform_configuration",
25606
25660
  row.id,
25607
25661
  row.rights_mode || "private"
25608
25662
  );
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@exulu/backend",
3
3
  "author": "Qventu B.V.",
4
- "version": "5.17.2",
4
+ "version": "5.18.0",
5
5
  "main": "./dist/index.js",
6
6
  "private": false,
7
7
  "publishConfig": {