shadok-ai 0.20.52 → 0.20.54

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/server.js CHANGED
@@ -85,6 +85,7 @@ import { mediaDirForUnit, mediaFileMode, parseMediaRequest, unitForMediaKey, pas
85
85
  import { runPrivileged, TREE_OP_TIMEOUT_MS } from "./privileged.js";
86
86
  import { HOME_SKILLS_BASE, missingHomeSkillLinks, rewriteSkillsBase } from "./skill-paths.js";
87
87
  import { instanceKey } from "./paths.js";
88
+ import { loadWidgets, publicWidget, saveWidgets, widgetDeleteFlow, widgetDoors, widgetSnippet, widgetWriteFlow, withWidgetJoined, } from "./widgets.js";
88
89
  import { prefsFor, savePrefsFor } from "./user-prefs.js";
89
90
  import { peerFileFor, loadPeers, savePeers, peerFromToken, signPeerToken, newPeer, withSalts, redeemVerdict, redeemPeer, normPeerName, removePeer, ownedByPeer, peersOutFileFor, tierPeersOutSeed, agentInvitePrompt, peerScope, setPeerScope, peerGroupCandidates, principalClashRefusal, } from "./peers.js";
90
91
  import { loadCrons, saveCrons, upsertCron, removeCron, resolveCronId, nextRunFor, nextRunAfterFailure, isTransient, CRON_MAX_RETRIES, markCronPrompt, isCronPrompt, normalizeSchedule, parseDays, onceAt, stateAfterFire, scheduleLabel, cronTimeZone, defaultTimeZone, systemTimeZone, isValidTimeZone, } from "./crons.js";
@@ -8333,17 +8334,90 @@ app.put("/whatsapp/guests", (req, res) => {
8333
8334
  // WhatsApp has no owner to protect: its door is always `dm-only`.
8334
8335
  hasOwner: true,
8335
8336
  other: guestSetupForCwd(loadConfig(), cwd),
8337
+ // The web widgets are doors too: a group one of them still targets keeps
8338
+ // the instance as a member.
8339
+ others: widgetDoors(loadWidgets()),
8336
8340
  });
8337
8341
  if (!flow.ok)
8338
8342
  return res.status(flow.status).json({ error: flow.error });
8339
8343
  saveGroups(flow.groups);
8340
8344
  setWaGuestSetupForCwd(loadConfig(), cwd, flow.setup);
8345
+ if (flow.handOverTo !== undefined)
8346
+ saveWidgets(withWidgetJoined(loadWidgets(), flow.handOverTo));
8341
8347
  // The membership this door would have taken back stays for Telegram's: it
8342
8348
  // is now Telegram's to take back when IT closes.
8343
8349
  if (flow.handOver)
8344
8350
  setGuestSetupForCwd(loadConfig(), cwd, { ...guestSetupForCwd(loadConfig(), cwd), joined: true });
8345
8351
  res.json({ guests: waGuestSetupForCwd(loadConfig(), cwd) });
8346
8352
  });
8353
+ // ── Web widgets: the record (design 2026-11-30, decisions 1 and 10) ────────
8354
+ // A widget is a door into an EXTERNAL group, set up by an admin from that
8355
+ // group's panel. These routes only keep the RECORD: nothing serves a visitor
8356
+ // yet, so a widget saved here opens nothing until the socket and the frame
8357
+ // exist. Admin-only, and every write browser-only, like the two bridges' doors.
8358
+ function widgetFlowContext() {
8359
+ const cwd = process.cwd();
8360
+ const cfg = loadConfig();
8361
+ return {
8362
+ mode: DEPLOYMENT_MODE,
8363
+ widgets: loadWidgets(),
8364
+ groups: loadGroups(),
8365
+ profileExists: (n) => !!getProfile(n),
8366
+ profileSecrets: (n) => getProfile(n)?.secrets ?? [],
8367
+ telegram: guestSetupForCwd(cfg, cwd),
8368
+ whatsapp: waGuestSetupForCwd(cfg, cwd),
8369
+ now: Date.now(),
8370
+ };
8371
+ }
8372
+ /** Persist a settled flow. The groups go first: a widget saved open while the
8373
+ * instance is not yet a member would be a door whose every spawn is refused. */
8374
+ function applyWidgetFlow(flow) {
8375
+ const cwd = process.cwd();
8376
+ saveGroups(flow.groups);
8377
+ saveWidgets(flow.widgets);
8378
+ if (flow.handOver === "telegram")
8379
+ setGuestSetupForCwd(loadConfig(), cwd, { ...guestSetupForCwd(loadConfig(), cwd), joined: true });
8380
+ if (flow.handOver === "whatsapp")
8381
+ setWaGuestSetupForCwd(loadConfig(), cwd, { ...waGuestSetupForCwd(loadConfig(), cwd), joined: true });
8382
+ }
8383
+ function widgetView(w) {
8384
+ return { ...publicWidget(w), snippet: widgetSnippet(loadPublicOrigin(), w.id) };
8385
+ }
8386
+ app.get("/widgets", (req, res) => {
8387
+ if (!accountAdmin(req, res, false, "view or change the web widgets"))
8388
+ return;
8389
+ res.json({
8390
+ // Why the feature is unavailable here, or null. Stated rather than hidden:
8391
+ // an empty list on a mono-tenant instance would read as "nothing set up".
8392
+ unavailable: migratedIsolationRefusal(DEPLOYMENT_MODE, groupsMigrated),
8393
+ widgets: loadWidgets().filter((w) => w.deleted === undefined).map(widgetView),
8394
+ });
8395
+ });
8396
+ app.put("/widgets", (req, res) => {
8397
+ if (!accountAdmin(req, res, true, "view or change the web widgets"))
8398
+ return;
8399
+ const refusal = migratedIsolationRefusal(DEPLOYMENT_MODE, groupsMigrated);
8400
+ if (refusal)
8401
+ return res.status(409).json({ error: refusal });
8402
+ const b = (req.body ?? {});
8403
+ const id = typeof b.id === "string" && b.id ? b.id : undefined;
8404
+ const flow = widgetWriteFlow({ ...widgetFlowContext(), ...(id ? { id } : {}), want: b });
8405
+ if (!flow.ok)
8406
+ return res.status(flow.status).json({ error: flow.error });
8407
+ applyWidgetFlow(flow);
8408
+ res.json({ widget: widgetView(flow.widget) });
8409
+ });
8410
+ // Deleting closes the door and keeps a tombstone; the conversations it opened
8411
+ // stay in the group for its members.
8412
+ app.delete("/widgets/:id", (req, res) => {
8413
+ if (!accountAdmin(req, res, true, "view or change the web widgets"))
8414
+ return;
8415
+ const flow = widgetDeleteFlow({ ...widgetFlowContext(), id: String(req.params.id) });
8416
+ if (!flow.ok)
8417
+ return res.status(flow.status).json({ error: flow.error });
8418
+ applyWidgetFlow(flow);
8419
+ res.json({ ok: true });
8420
+ });
8347
8421
  const WA_FAKE_ACTIONS = ["scan", "drop", "goSilent", "removeFromPhone", "rotateQr", "ping", "offline", "online", "fatal", "deliver", "mapLid"];
8348
8422
  app.post("/whatsapp/fake/:action", (req, res) => {
8349
8423
  if (!waFake)
@@ -8460,11 +8534,15 @@ app.put("/telegram", async (req, res) => {
8460
8534
  hasOwner: loadTgOwner() !== null,
8461
8535
  // The instance's membership is shared with the WhatsApp door.
8462
8536
  other: waGuestSetupForCwd(loadConfig(), cwd),
8537
+ // And with the web widgets (see `PUT /whatsapp/guests`).
8538
+ others: widgetDoors(loadWidgets()),
8463
8539
  });
8464
8540
  if (!flow.ok)
8465
8541
  return res.status(flow.status).json({ error: flow.error });
8466
8542
  saveGroups(flow.groups);
8467
8543
  setGuestSetupForCwd(loadConfig(), cwd, flow.setup);
8544
+ if (flow.handOverTo !== undefined)
8545
+ saveWidgets(withWidgetJoined(loadWidgets(), flow.handOverTo));
8468
8546
  if (flow.handOver)
8469
8547
  setWaGuestSetupForCwd(loadConfig(), cwd, { ...waGuestSetupForCwd(loadConfig(), cwd), joined: true });
8470
8548
  guestsChanged = true;