@goodfish/jarvy 0.12.0 → 0.12.2

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.
@@ -14,8 +14,8 @@ import { networkInterfaces } from "node:os";
14
14
  import { getRequestListener } from "@hono/node-server";
15
15
 
16
16
  // src/server/api/app.ts
17
- import { Hono as Hono15 } from "hono";
18
- import { HTTPException as HTTPException14 } from "hono/http-exception";
17
+ import { Hono as Hono16 } from "hono";
18
+ import { HTTPException as HTTPException15 } from "hono/http-exception";
19
19
  import { serveStatic } from "@hono/node-server/serve-static";
20
20
 
21
21
  // src/server/api/middleware/security.ts
@@ -179,13 +179,14 @@ function installAuthentication(api, account) {
179
179
  api.post("/logout", (c) => {
180
180
  account.logout(c.get("sessionToken"));
181
181
  sessionCookie(c, cookie2(c), "", 0);
182
+ c.header("Clear-Site-Data", '"cache"');
182
183
  return c.json({ ok: true });
183
184
  });
184
185
  }
185
186
 
186
187
  // src/server/api/routes/bots.ts
187
- import { Hono as Hono4 } from "hono";
188
- import { HTTPException as HTTPException7 } from "hono/http-exception";
188
+ import { Hono as Hono5 } from "hono";
189
+ import { HTTPException as HTTPException8 } from "hono/http-exception";
189
190
 
190
191
  // src/server/api/views/bot-status.ts
191
192
  function botStatusView(status) {
@@ -316,7 +317,8 @@ function messageView(message) {
316
317
  replyTo: message.replyTo,
317
318
  reactions: message.reactions,
318
319
  quotes: message.quotes,
319
- folded: message.folded
320
+ folded: message.folded,
321
+ refs: message.refs
320
322
  };
321
323
  }
322
324
 
@@ -357,19 +359,35 @@ function activityRoutes(bots) {
357
359
  }
358
360
 
359
361
  // src/server/api/routes/history.ts
362
+ import { Hono as Hono4 } from "hono";
363
+ import { HTTPException as HTTPException7 } from "hono/http-exception";
364
+
365
+ // src/server/api/routes/usage.ts
360
366
  import { Hono as Hono3 } from "hono";
361
367
  import { HTTPException as HTTPException6 } from "hono/http-exception";
368
+ function since(raw) {
369
+ const value2 = Number(raw ?? "");
370
+ if (!/^\d+$/.test(raw ?? "") || !Number.isSafeInteger(value2)) throw new HTTPException6(400, { message: "Invalid start time" });
371
+ return value2;
372
+ }
373
+ function usageRoutes(bots) {
374
+ const routes = new Hono3();
375
+ routes.get("/usage", (c) => c.json(bots.usage(since(c.req.query("since")))));
376
+ return routes;
377
+ }
378
+
379
+ // src/server/api/routes/history.ts
362
380
  function integer(raw, label) {
363
381
  const n = Number(raw);
364
- if (!/^\d+$/.test(raw) || !Number.isSafeInteger(n) || n < 1) throw new HTTPException6(400, { message: `Invalid ${label}` });
382
+ if (!/^\d+$/.test(raw) || !Number.isSafeInteger(n) || n < 1) throw new HTTPException7(400, { message: `Invalid ${label}` });
365
383
  return n;
366
384
  }
367
385
  function historyRoutes(bots) {
368
- const routes = new Hono3();
386
+ const routes = new Hono4();
369
387
  routes.get("/:id/history", (c) => {
370
388
  const memory = bots.memory(c.req.param("id"));
371
389
  const before = c.req.query("before"), limit2 = integer(c.req.query("limit") ?? "50", "page size");
372
- if (limit2 > 100) throw new HTTPException6(400, { message: "The page size can't be over 100" });
390
+ if (limit2 > 100) throw new HTTPException7(400, { message: "The page size can't be over 100" });
373
391
  return c.json(memory.list(before === void 0 ? void 0 : integer(before, "page position"), limit2));
374
392
  });
375
393
  routes.get("/:id/history/:recordId{[0-9]+}", (c) => {
@@ -378,23 +396,21 @@ function historyRoutes(bots) {
378
396
  return c.json(detail);
379
397
  });
380
398
  routes.get("/:id/usage", (c) => {
381
- const raw = c.req.query("since") ?? "";
382
- const since = Number(raw);
383
- if (!/^\d+$/.test(raw) || !Number.isSafeInteger(since)) throw new HTTPException6(400, { message: "Invalid start time" });
384
- return c.json({ since, models: bots.memory(c.req.param("id")).usage(since) });
399
+ const from = since(c.req.query("since"));
400
+ return c.json({ since: from, models: bots.memory(c.req.param("id")).usage(from) });
385
401
  });
386
402
  routes.all("/:id/history", () => {
387
- throw new HTTPException6(405, { message: "History is read-only" });
403
+ throw new HTTPException7(405, { message: "History is read-only" });
388
404
  });
389
405
  routes.all("/:id/history/:recordId{[0-9]+}", () => {
390
- throw new HTTPException6(405, { message: "History is read-only" });
406
+ throw new HTTPException7(405, { message: "History is read-only" });
391
407
  });
392
408
  return routes;
393
409
  }
394
410
 
395
411
  // src/server/api/routes/bots.ts
396
412
  function botRoutes(bots) {
397
- const routes = new Hono4();
413
+ const routes = new Hono5();
398
414
  routes.get("/", (c) => c.json({ bots: bots.list() }));
399
415
  routes.post("/", jsonBody, async (c) => {
400
416
  const input = await readJson(c);
@@ -402,7 +418,7 @@ function botRoutes(bots) {
402
418
  });
403
419
  routes.put("/:id/environment", jsonBody, async (c) => {
404
420
  const input = await readJson(c), id = c.req.param("id");
405
- if (typeof input.machineId !== "string" || typeof input.granted !== "boolean") throw new HTTPException7(400, { message: "machineId and granted (true or false) are needed" });
421
+ if (typeof input.machineId !== "string" || typeof input.granted !== "boolean") throw new HTTPException8(400, { message: "machineId and granted (true or false) are needed" });
406
422
  return c.json(bots.grantMachine(id, input.machineId, input.granted));
407
423
  });
408
424
  routes.put("/:id/computer/limits", jsonBody, async (c) => {
@@ -412,7 +428,7 @@ function botRoutes(bots) {
412
428
  routes.get("/:id/computer/resources", async (c) => c.json(await bots.computerResources(c.req.param("id"))));
413
429
  routes.post("/:id/computer/:action", async (c) => {
414
430
  const action = c.req.param("action");
415
- if (action !== "start" && action !== "stop" && action !== "reset") throw new HTTPException7(404, { message: "Not found" });
431
+ if (action !== "start" && action !== "stop" && action !== "reset") throw new HTTPException8(404, { message: "Not found" });
416
432
  return c.json(await bots.computer(c.req.param("id"), action));
417
433
  });
418
434
  routes.get("/:id", (c) => c.json(bots.describe(c.req.param("id"))));
@@ -465,8 +481,8 @@ function botRoutes(bots) {
465
481
 
466
482
  // src/server/api/routes/chats.ts
467
483
  import { Readable } from "node:stream";
468
- import { Hono as Hono5 } from "hono";
469
- import { HTTPException as HTTPException8 } from "hono/http-exception";
484
+ import { Hono as Hono6 } from "hono";
485
+ import { HTTPException as HTTPException9 } from "hono/http-exception";
470
486
 
471
487
  // src/server/api/views/download.ts
472
488
  var MEDIA_TYPES = /* @__PURE__ */ new Set(["video/webm", "video/mp4", "video/quicktime", "audio/mpeg", "audio/wav", "audio/x-wav", "audio/ogg", "audio/webm", "audio/mp4"]);
@@ -490,14 +506,73 @@ function byteRange(header, size) {
490
506
  var ATTACHMENT_MAX_BYTES = 4 * 1024 ** 3;
491
507
 
492
508
  // src/server/infra/files/attachments.ts
493
- import { createHash, randomUUID } from "node:crypto";
509
+ import { createHash, randomUUID as randomUUID2 } from "node:crypto";
494
510
  import { createReadStream } from "node:fs";
495
- import { mkdir, open, readdir, rename, rm, stat } from "node:fs/promises";
511
+ import { mkdir, open, readdir, rename as rename2, rm as rm2, stat as stat2 } from "node:fs/promises";
496
512
  import { dirname, join } from "node:path";
513
+
514
+ // src/server/infra/files/thumbnails.ts
515
+ import { randomUUID } from "node:crypto";
516
+ import { rename, rm, stat } from "node:fs/promises";
517
+ import sharp from "sharp";
518
+ var WIDTH = 1600;
519
+ var HEIGHT = 640;
520
+ var SMALL = 300 * 1024;
521
+ var FORMATS = /* @__PURE__ */ new Set(["jpeg", "png", "webp", "avif"]);
522
+ var AT_ONCE = 4;
523
+ var Thumbnails = class {
524
+ making = /* @__PURE__ */ new Map();
525
+ running = 0;
526
+ waiting = [];
527
+ /** The thumbnail's path for the original at `source`, kept at `target`; null to show the original. */
528
+ async thumbnail(source2, target3) {
529
+ if (await stat(target3).then(() => true, () => false)) return target3;
530
+ let pending = this.making.get(target3);
531
+ if (!pending) {
532
+ pending = this.make(source2, target3).finally(() => this.making.delete(target3));
533
+ this.making.set(target3, pending);
534
+ }
535
+ return pending;
536
+ }
537
+ async make(source2, target3) {
538
+ const meta = await sharp(source2).metadata().catch(() => null);
539
+ if (!meta?.width || !meta.height || !FORMATS.has(meta.format ?? "") || (meta.pages ?? 1) > 1) return null;
540
+ const [width, height] = (meta.orientation ?? 1) >= 5 ? [meta.height, meta.width] : [meta.width, meta.height];
541
+ if (width <= WIDTH && height <= HEIGHT && (meta.size ?? (await stat(source2)).size) <= SMALL) return null;
542
+ await this.turn();
543
+ const part = `${target3}.${randomUUID()}.part`;
544
+ try {
545
+ await sharp(source2).rotate().resize(WIDTH, HEIGHT, { fit: "inside", withoutEnlargement: true }).webp({ quality: 80 }).toFile(part);
546
+ await rename(part, target3);
547
+ return target3;
548
+ } catch (error) {
549
+ console.warn("[files] could not make a thumbnail", { source: source2, error: error instanceof Error ? error.message : String(error) });
550
+ await rm(part, { force: true });
551
+ return null;
552
+ } finally {
553
+ this.done();
554
+ }
555
+ }
556
+ async turn() {
557
+ if (this.running < AT_ONCE) {
558
+ this.running++;
559
+ return;
560
+ }
561
+ await new Promise((resolve5) => this.waiting.push(resolve5));
562
+ }
563
+ done() {
564
+ const next = this.waiting.shift();
565
+ if (next) next();
566
+ else this.running--;
567
+ }
568
+ };
569
+
570
+ // src/server/infra/files/attachments.ts
497
571
  var attachmentPath = (root, id) => join(root, id.slice(0, 2), id);
498
572
  var INCOMING = "incoming";
499
573
  var SHARD = /^[a-f\d]{2}$/;
500
574
  var ID = /^[a-f\d]{8}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{12}$/;
575
+ var THUMB = ".thumb";
501
576
  var FileTooLarge = class extends Error {
502
577
  constructor() {
503
578
  super("File is too large");
@@ -505,13 +580,14 @@ var FileTooLarge = class extends Error {
505
580
  };
506
581
  var AttachmentFiles = class {
507
582
  root;
583
+ thumbnails = new Thumbnails();
508
584
  constructor(dataDir) {
509
585
  this.root = join(dataDir, "attachments");
510
586
  }
511
587
  /** Writes `chunks` to a new file under a new id; past `limit` bytes it stops and throws FileTooLarge. Nothing is
512
588
  * left behind when it throws. */
513
589
  async stage(chunks, limit2, signal) {
514
- const id = randomUUID(), part = this.part(id), hash = createHash("sha256");
590
+ const id = randomUUID2(), part = this.part(id), hash = createHash("sha256");
515
591
  await mkdir(dirname(part), { recursive: true, mode: 448 });
516
592
  const handle = await open(part, "wx", 384);
517
593
  let size = 0;
@@ -527,7 +603,7 @@ var AttachmentFiles = class {
527
603
  await handle.sync();
528
604
  } catch (error) {
529
605
  await handle.close().catch(() => void 0);
530
- await rm(part, { force: true });
606
+ await rm2(part, { force: true });
531
607
  throw error;
532
608
  }
533
609
  await handle.close();
@@ -537,11 +613,17 @@ var AttachmentFiles = class {
537
613
  async commit(id) {
538
614
  const target3 = this.path(id);
539
615
  await mkdir(dirname(target3), { recursive: true, mode: 448 });
540
- await rename(this.part(id), target3);
616
+ await rename2(this.part(id), target3);
541
617
  }
542
- /** Drops files, staged or in place; ids with no file are skipped. */
618
+ /** Drops files, staged or in place, with their thumbnails; ids with no file are skipped. */
543
619
  async discard(ids) {
544
- for (const id of ids) if (ID.test(id)) await Promise.all([rm(this.part(id), { force: true }), rm(this.path(id), { force: true })]);
620
+ for (const id of ids) if (ID.test(id)) await Promise.all([rm2(this.part(id), { force: true }), rm2(this.path(id), { force: true }), rm2(this.path(id) + THUMB, { force: true })]);
621
+ }
622
+ /** A picture's thumbnail (A80), made the first time it is asked for; null when the original is to be shown. */
623
+ async thumbnail(id) {
624
+ const path = await this.thumbnails.thumbnail(this.path(id), this.path(id) + THUMB);
625
+ if (!path) return null;
626
+ return { size: (await stat2(path)).size, stream: createReadStream(path) };
545
627
  }
546
628
  /** The file's bytes from `start` to `end` (inclusive), as a stream. */
547
629
  stream(id, range) {
@@ -571,31 +653,33 @@ var AttachmentFiles = class {
571
653
  }
572
654
  async size(id) {
573
655
  try {
574
- return (await stat(this.path(id))).size;
656
+ return (await stat2(this.path(id))).size;
575
657
  } catch {
576
658
  return null;
577
659
  }
578
660
  }
579
- /** Removes what no attachment owns: files left in incoming/ by a stop mid-upload, files whose attachment is gone
580
- * (`known` says which ids still have one), and anything else under the root, such as the readable copies older
581
- * versions kept in `<id>/<name>` folders. Folders left empty go too. Returns how many files went. */
661
+ /** Removes what no attachment owns: files left in incoming/ by a stop mid-upload, files and thumbnails whose
662
+ * attachment is gone (`known` says which ids still have one), and anything else under the root, such as the readable
663
+ * copies older versions kept in `<id>/<name>` folders or a thumbnail cut off midway. Folders left empty go too.
664
+ * Returns how many files went. */
582
665
  async sweep(known) {
583
666
  let removed = 0;
584
667
  for (const entry of await entries(this.root)) {
585
668
  const path = join(this.root, entry.name);
586
669
  if (!entry.isDirectory() || !(SHARD.test(entry.name) || entry.name === INCOMING)) {
587
670
  removed += await count(path);
588
- await rm(path, { force: true, recursive: true });
671
+ await rm2(path, { force: true, recursive: true });
589
672
  continue;
590
673
  }
591
674
  const files = await entries(path);
592
675
  for (const file of files) {
593
- if (entry.name === INCOMING || !ID.test(file.name) || !file.name.startsWith(entry.name) || !known(file.name)) {
676
+ const id = file.name.endsWith(THUMB) ? file.name.slice(0, -THUMB.length) : file.name;
677
+ if (entry.name === INCOMING || !ID.test(id) || !id.startsWith(entry.name) || !known(id)) {
594
678
  removed += await count(join(path, file.name));
595
- await rm(join(path, file.name), { force: true, recursive: true });
679
+ await rm2(join(path, file.name), { force: true, recursive: true });
596
680
  }
597
681
  }
598
- if (entry.name !== INCOMING && !(await entries(path)).length) await rm(path, { force: true, recursive: true });
682
+ if (entry.name !== INCOMING && !(await entries(path)).length) await rm2(path, { force: true, recursive: true });
599
683
  }
600
684
  return removed;
601
685
  }
@@ -609,7 +693,7 @@ var AttachmentFiles = class {
609
693
  };
610
694
  async function count(path) {
611
695
  const inside2 = await entries(path);
612
- if (!inside2.length) return (await stat(path).catch(() => null))?.isFile() ? 1 : 0;
696
+ if (!inside2.length) return (await stat2(path).catch(() => null))?.isFile() ? 1 : 0;
613
697
  let total = 0;
614
698
  for (const entry of inside2) total += entry.isDirectory() ? await count(join(path, entry.name)) : 1;
615
699
  return total;
@@ -668,6 +752,10 @@ var ChatAttachments = class {
668
752
  stream(file, range) {
669
753
  return this.files.stream(file.id, range);
670
754
  }
755
+ /** A picture's thumbnail (A80), made on first use; null when its original is to be shown. */
756
+ thumbnail(file) {
757
+ return this.files.thumbnail(file.id);
758
+ }
671
759
  /** The first bytes of a file, to tell its kind. */
672
760
  head(file, bytes) {
673
761
  return this.files.head(file.id, bytes);
@@ -704,7 +792,7 @@ var tooLarge = () => new ChatError(413, "File is too large", "file_too_large", {
704
792
 
705
793
  // src/server/api/routes/chats.ts
706
794
  function chatRoutes({ chats, bots }) {
707
- const routes = new Hono5();
795
+ const routes = new Hono6();
708
796
  const view = (chat) => chatView(chat, (id) => chats.member(id));
709
797
  routes.get("/", (c) => c.json({ chats: chats.list(c.get("actorId")).map(view), followed: chats.threads.followedSummary(c.get("actorId")) }));
710
798
  routes.post("/", jsonBody, async (c) => {
@@ -735,33 +823,37 @@ function chatRoutes({ chats, bots }) {
735
823
  try {
736
824
  name3 = decodeURIComponent(c.req.header("x-file-name") ?? "");
737
825
  } catch {
738
- throw new HTTPException8(400, { message: "Invalid filename" });
826
+ throw new HTTPException9(400, { message: "Invalid filename" });
739
827
  }
740
828
  const declared = Number(c.req.header("content-length") ?? NaN);
741
829
  if (declared > ATTACHMENT_MAX_BYTES) throw tooLarge();
742
830
  const body = c.req.raw.body;
743
- if (!body) throw new HTTPException8(400, { message: "Empty attachment" });
831
+ if (!body) throw new HTTPException9(400, { message: "Empty attachment" });
744
832
  const mime = (c.req.header("content-type") ?? "application/octet-stream").slice(0, 100);
745
833
  return c.json(await chats.attachments.upload(c.get("actorId"), c.req.param("id"), name3, mime, idle(body, c.req.raw.signal)), 201);
746
834
  });
747
835
  routes.delete("/:id", async (c) => {
748
836
  const id = c.req.param("id");
749
- if (!await chats.delete(c.get("actorId"), id)) throw new HTTPException8(404, { message: "Chat not found" });
837
+ if (!await chats.delete(c.get("actorId"), id)) throw new HTTPException9(404, { message: "Chat not found" });
750
838
  await bots.chatDeleted(id);
751
839
  return c.json({ ok: true });
752
840
  });
753
841
  return routes;
754
842
  }
843
+ var ATTACHMENT_CACHE = "private, max-age=31536000, immutable";
844
+ var disposition = (name3) => `attachment; filename*=UTF-8''${encodeURIComponent(name3)}`;
755
845
  function attachmentRoutes(chats) {
756
- const routes = new Hono5();
846
+ const routes = new Hono6();
757
847
  routes.get("/:id", (c) => {
758
848
  const file = chats.attachments.find(c.get("actorId"), c.req.param("id"));
759
- if (!file) throw new HTTPException8(404, { message: "Attachment not found" });
849
+ if (!file) throw new HTTPException9(404, { message: "Attachment not found" });
760
850
  const size = file.size, range = byteRange(c.req.header("Range"), size);
761
851
  const headers2 = {
762
852
  "Content-Type": downloadType(file.mimeType),
763
853
  "Accept-Ranges": "bytes",
764
- "Content-Disposition": `attachment; filename*=UTF-8''${encodeURIComponent(file.name)}`
854
+ "Cache-Control": ATTACHMENT_CACHE,
855
+ "Vary": "Cookie",
856
+ "Content-Disposition": disposition(file.name)
765
857
  };
766
858
  if (range === "unsatisfiable") return c.body(null, 416, { ...headers2, "Content-Range": `bytes */${size}` });
767
859
  if (!range) return c.body(web(chats.attachments.stream(file)), 200, { ...headers2, "Content-Length": String(size) });
@@ -771,15 +863,23 @@ function attachmentRoutes(chats) {
771
863
  { ...headers2, "Content-Length": String(range.end - range.start + 1), "Content-Range": `bytes ${range.start}-${range.end}/${size}` }
772
864
  );
773
865
  });
866
+ routes.get("/:id/thumb", async (c) => {
867
+ const file = chats.attachments.find(c.get("actorId"), c.req.param("id"));
868
+ if (!file) throw new HTTPException9(404, { message: "Attachment not found" });
869
+ const thumbnail = await chats.attachments.thumbnail(file);
870
+ const kept = { "Cache-Control": ATTACHMENT_CACHE, "Vary": "Cookie", "Content-Disposition": disposition(file.name) };
871
+ if (thumbnail) return c.body(web(thumbnail.stream), 200, { ...kept, "Content-Type": "image/webp", "Content-Length": String(thumbnail.size) });
872
+ return c.body(web(chats.attachments.stream(file)), 200, { ...kept, "Content-Type": downloadType(file.mimeType), "Content-Length": String(file.size) });
873
+ });
774
874
  routes.get("/:id/run", (c) => {
775
875
  const file = chats.attachments.find(c.get("actorId"), c.req.param("id"));
776
- if (!file) throw new HTTPException8(404, { message: "Attachment not found" });
777
- if (!/\.html?$/i.test(file.name) && file.mimeType !== "text/html") throw new HTTPException8(415, { message: "Only HTML attachments can run" });
876
+ if (!file) throw new HTTPException9(404, { message: "Attachment not found" });
877
+ if (!/\.html?$/i.test(file.name) && file.mimeType !== "text/html") throw new HTTPException9(415, { message: "Only HTML attachments can run" });
778
878
  c.set("sandboxedDocument", true);
779
879
  return c.body(web(chats.attachments.stream(file)), 200, { "Content-Type": "text/html; charset=utf-8", "Content-Length": String(file.size) });
780
880
  });
781
881
  routes.delete("/:id", async (c) => {
782
- if (!await chats.attachments.removePending(c.get("actorId"), c.req.param("id"))) throw new HTTPException8(404, { message: "Pending attachment not found" });
882
+ if (!await chats.attachments.removePending(c.get("actorId"), c.req.param("id"))) throw new HTTPException9(404, { message: "Pending attachment not found" });
783
883
  return c.json({ ok: true });
784
884
  });
785
885
  return routes;
@@ -788,10 +888,10 @@ async function* idle(body, signal) {
788
888
  const reader = body.getReader();
789
889
  try {
790
890
  for (; ; ) {
791
- if (signal.aborted) throw new HTTPException8(400, { message: "Upload cancelled" });
891
+ if (signal.aborted) throw new HTTPException9(400, { message: "Upload cancelled" });
792
892
  let timer;
793
893
  const stalled = new Promise((_, reject) => {
794
- timer = setTimeout(() => reject(new HTTPException8(408, { message: "Upload stalled" })), UPLOAD_IDLE_MS);
894
+ timer = setTimeout(() => reject(new HTTPException9(408, { message: "Upload stalled" })), UPLOAD_IDLE_MS);
795
895
  });
796
896
  try {
797
897
  const { done: done2, value: value2 } = await Promise.race([reader.read(), stalled]);
@@ -809,15 +909,15 @@ var UPLOAD_IDLE_MS = 6e4;
809
909
  var web = (stream) => Readable.toWeb(stream);
810
910
 
811
911
  // src/server/api/routes/models.ts
812
- import { Hono as Hono6 } from "hono";
912
+ import { Hono as Hono7 } from "hono";
813
913
  function modelRoutes(models) {
814
- const routes = new Hono6();
914
+ const routes = new Hono7();
815
915
  routes.get("/model-providers", (c) => c.json({ providers: models.providers() }));
816
916
  routes.get("/model-providers/:provider/models", (c) => c.json({ models: models.builtinModels(c.req.param("provider")) }));
817
917
  routes.get("/model-options", (c) => c.json({ connections: models.options() }));
818
918
  routes.post("/model-autoconfig", jsonBody, async (c) => c.json(models.autoconfigure(await readJson(c))));
819
919
  routes.post("/model-discovery", jsonBody, async (c) => c.json({ models: await models.discover(await readJson(c), c.req.raw.signal) }));
820
- const connections = new Hono6();
920
+ const connections = new Hono7();
821
921
  connections.get("/", (c) => c.json({ connections: models.list() }));
822
922
  connections.post("/", jsonBody, async (c) => c.json(models.create(await readJson(c)), 201));
823
923
  connections.put("/:id", jsonBody, async (c) => c.json(models.update(c.req.param("id"), await readJson(c))));
@@ -854,7 +954,7 @@ function modelRoutes(models) {
854
954
  }
855
955
 
856
956
  // src/server/api/routes/events.ts
857
- import { Hono as Hono7 } from "hono";
957
+ import { Hono as Hono8 } from "hono";
858
958
 
859
959
  // src/server/api/event-stream.ts
860
960
  function eventStream(c, authenticated, open2) {
@@ -914,7 +1014,7 @@ data: ${JSON.stringify(data)}
914
1014
 
915
1015
  // src/server/api/routes/events.ts
916
1016
  function eventRoutes({ events, account, chats, bots }) {
917
- const routes = new Hono7();
1017
+ const routes = new Hono8();
918
1018
  routes.get("/events", (c) => {
919
1019
  const actorId = c.get("actorId"), token = c.get("sessionToken");
920
1020
  const after2 = c.req.header("Last-Event-ID") ?? c.req.query("after");
@@ -979,14 +1079,14 @@ function once(entries2) {
979
1079
  }
980
1080
 
981
1081
  // src/server/api/routes/channels.ts
982
- import { Hono as Hono8 } from "hono";
983
- import { HTTPException as HTTPException9 } from "hono/http-exception";
1082
+ import { Hono as Hono9 } from "hono";
1083
+ import { HTTPException as HTTPException10 } from "hono/http-exception";
984
1084
  function channelRoutes({ chats }) {
985
- const routes = new Hono8();
1085
+ const routes = new Hono9();
986
1086
  const channels = chats.channels;
987
1087
  const chat = (actorId, id) => {
988
1088
  const found = chats.get(actorId, id);
989
- if (!found) throw new HTTPException9(404, { message: "Chat not found" });
1089
+ if (!found) throw new HTTPException10(404, { message: "Chat not found" });
990
1090
  return chatView(found, (member) => chats.member(member));
991
1091
  };
992
1092
  routes.get("/members", (c) => c.json({ members: channels.everyone(c.get("actorId")) }));
@@ -1009,7 +1109,7 @@ function channelRoutes({ chats }) {
1009
1109
  routes.post("/sections", jsonBody, async (c) => c.json(channels.createSection(c.get("actorId"), await readJson(c)), 201));
1010
1110
  routes.patch("/sections/:id", jsonBody, async (c) => c.json(channels.updateSection(c.get("actorId"), c.req.param("id"), await readJson(c))));
1011
1111
  routes.delete("/sections/:id", (c) => {
1012
- if (!channels.deleteSection(c.get("actorId"), c.req.param("id"))) throw new HTTPException9(404, { message: "Section not found" });
1112
+ if (!channels.deleteSection(c.get("actorId"), c.req.param("id"))) throw new HTTPException10(404, { message: "Section not found" });
1013
1113
  return c.json({ ok: true });
1014
1114
  });
1015
1115
  routes.post("/channels", jsonBody, async (c) => {
@@ -1025,7 +1125,7 @@ function channelRoutes({ chats }) {
1025
1125
  routes.get("/chats/:id", (c) => c.json(chat(c.get("actorId"), c.req.param("id"))));
1026
1126
  routes.get("/messages/:id", (c) => {
1027
1127
  const found = chats.message(c.get("actorId"), c.req.param("id"));
1028
- if (!found) throw new HTTPException9(404, { message: "Message not found" });
1128
+ if (!found) throw new HTTPException10(404, { message: "Message not found" });
1029
1129
  return c.json(messageView(found));
1030
1130
  });
1031
1131
  routes.post("/messages/:id/thread", (c) => {
@@ -1064,9 +1164,9 @@ function channelRoutes({ chats }) {
1064
1164
  }
1065
1165
 
1066
1166
  // src/server/api/routes/live.ts
1067
- import { Hono as Hono9 } from "hono";
1167
+ import { Hono as Hono10 } from "hono";
1068
1168
  function liveRoutes({ account, bots }) {
1069
- const routes = new Hono9();
1169
+ const routes = new Hono10();
1070
1170
  routes.get("/bots/:id/live", (c) => {
1071
1171
  const token = c.get("sessionToken"), live = bots.live(c.req.param("id"));
1072
1172
  return eventStream(c, () => account.authenticated(token), ({ send, close, signedIn }) => {
@@ -1078,34 +1178,34 @@ function liveRoutes({ account, bots }) {
1078
1178
  }
1079
1179
 
1080
1180
  // src/server/api/routes/connection.ts
1081
- import { Hono as Hono10 } from "hono";
1082
- import { HTTPException as HTTPException10 } from "hono/http-exception";
1181
+ import { Hono as Hono11 } from "hono";
1182
+ import { HTTPException as HTTPException11 } from "hono/http-exception";
1083
1183
  function rootCertificateRoute(connection) {
1084
- const routes = new Hono10();
1184
+ const routes = new Hono11();
1085
1185
  routes.get("/connection/root.crt", (c) => {
1086
1186
  const der = connection.rootCertificate();
1087
- if (!der) throw new HTTPException10(404, { message: "Jarvy serves HTTPS only when it listens beyond this machine" });
1187
+ if (!der) throw new HTTPException11(404, { message: "Jarvy serves HTTPS only when it listens beyond this machine" });
1088
1188
  return c.body(new Uint8Array(der), 200, { "Content-Type": "application/x-x509-ca-cert", "Content-Disposition": 'attachment; filename="jarvy-root.crt"' });
1089
1189
  });
1090
1190
  return routes;
1091
1191
  }
1092
1192
  function connectionRoutes(connection) {
1093
- const routes = new Hono10();
1193
+ const routes = new Hono11();
1094
1194
  routes.get("/connection", (c) => c.json(connection.info()));
1095
1195
  routes.put("/connection", jsonBody, async (c) => {
1096
1196
  const input = await readJson(c);
1097
1197
  if (input.ports !== void 0) return c.json(connection.setPortRange(input.ports));
1098
- if (typeof input.allowHttp !== "boolean") throw new HTTPException10(400, { message: "allowHttp must be true or false" });
1198
+ if (typeof input.allowHttp !== "boolean") throw new HTTPException11(400, { message: "allowHttp must be true or false" });
1099
1199
  return c.json(connection.setAllowHttp(input.allowHttp));
1100
1200
  });
1101
1201
  return routes;
1102
1202
  }
1103
1203
 
1104
1204
  // src/server/api/routes/secrets.ts
1105
- import { Hono as Hono11 } from "hono";
1106
- import { HTTPException as HTTPException11 } from "hono/http-exception";
1205
+ import { Hono as Hono12 } from "hono";
1206
+ import { HTTPException as HTTPException12 } from "hono/http-exception";
1107
1207
  function secretRoutes(secrets) {
1108
- const routes = new Hono11();
1208
+ const routes = new Hono12();
1109
1209
  routes.get("/", (c) => c.json(secrets.list()));
1110
1210
  routes.post("/", jsonBody, async (c) => {
1111
1211
  const input = await readJson(c);
@@ -1121,7 +1221,7 @@ function secretRoutes(secrets) {
1121
1221
  });
1122
1222
  routes.put("/:id/grants/:botId", jsonBody, async (c) => {
1123
1223
  const input = await readJson(c);
1124
- if (typeof input.granted !== "boolean") throw new HTTPException11(400, { message: "granted must be true or false" });
1224
+ if (typeof input.granted !== "boolean") throw new HTTPException12(400, { message: "granted must be true or false" });
1125
1225
  return c.json(secrets.grant(c.req.param("id"), c.req.param("botId"), input.granted));
1126
1226
  });
1127
1227
  routes.get("/:id/uses", (c) => c.json({ uses: secrets.uses(c.req.param("id")) }));
@@ -1133,10 +1233,10 @@ function secretRoutes(secrets) {
1133
1233
  }
1134
1234
 
1135
1235
  // src/server/api/routes/environments.ts
1136
- import { Hono as Hono12 } from "hono";
1137
- import { HTTPException as HTTPException12 } from "hono/http-exception";
1236
+ import { Hono as Hono13 } from "hono";
1237
+ import { HTTPException as HTTPException13 } from "hono/http-exception";
1138
1238
  function environmentRoutes(bots) {
1139
- const routes = new Hono12(), machines = bots.machines;
1239
+ const routes = new Hono13(), machines = bots.machines;
1140
1240
  routes.get("/", (c) => c.json({ machines: machines.list() }));
1141
1241
  routes.post("/scan", jsonBody, async (c) => {
1142
1242
  const input = await readJson(c);
@@ -1176,7 +1276,7 @@ function environmentRoutes(bots) {
1176
1276
  });
1177
1277
  routes.put("/:id/grants/:botId", jsonBody, async (c) => {
1178
1278
  const input = await readJson(c);
1179
- if (typeof input.granted !== "boolean") throw new HTTPException12(400, { message: "granted must be true or false" });
1279
+ if (typeof input.granted !== "boolean") throw new HTTPException13(400, { message: "granted must be true or false" });
1180
1280
  bots.grantMachine(c.req.param("botId"), c.req.param("id"), input.granted);
1181
1281
  return c.json(machines.get(c.req.param("id")));
1182
1282
  });
@@ -1194,14 +1294,14 @@ function environmentRoutes(bots) {
1194
1294
  function hostKeys(value2) {
1195
1295
  const keys = value2;
1196
1296
  const strings = (list) => Array.isArray(list) && list.length > 0 && list.length <= 20 && list.every((item) => typeof item === "string" && item.length <= 2e3);
1197
- if (!keys || !strings(keys.keys) || !strings(keys.fingerprints)) throw new HTTPException12(400, { message: "hostKeys must be what the scan returned" });
1297
+ if (!keys || !strings(keys.keys) || !strings(keys.fingerprints)) throw new HTTPException13(400, { message: "hostKeys must be what the scan returned" });
1198
1298
  return { keys: keys.keys, fingerprints: keys.fingerprints };
1199
1299
  }
1200
1300
 
1201
1301
  // src/server/api/routes/tasks.ts
1202
- import { Hono as Hono13 } from "hono";
1302
+ import { Hono as Hono14 } from "hono";
1203
1303
  function taskRoutes(services) {
1204
- const routes = new Hono13();
1304
+ const routes = new Hono14();
1205
1305
  const bot = (id) => {
1206
1306
  services.bots.describe(id);
1207
1307
  return id;
@@ -1218,10 +1318,10 @@ function taskRoutes(services) {
1218
1318
  }
1219
1319
 
1220
1320
  // src/server/api/routes/ports.ts
1221
- import { Hono as Hono14 } from "hono";
1222
- import { HTTPException as HTTPException13 } from "hono/http-exception";
1321
+ import { Hono as Hono15 } from "hono";
1322
+ import { HTTPException as HTTPException14 } from "hono/http-exception";
1223
1323
  function portRoutes(services) {
1224
- const routes = new Hono14();
1324
+ const routes = new Hono15();
1225
1325
  const bot = (id) => {
1226
1326
  services.bots.describe(id);
1227
1327
  return id;
@@ -1229,7 +1329,7 @@ function portRoutes(services) {
1229
1329
  routes.get("/bots/:id/ports", async (c) => c.json({ ports: await services.bots.ports.list(bot(c.req.param("id"))) }));
1230
1330
  routes.delete("/bots/:id/ports/:port", (c) => {
1231
1331
  const port = Number(c.req.param("port"));
1232
- if (!Number.isInteger(port)) throw new HTTPException13(400, { message: "port must be a number" });
1332
+ if (!Number.isInteger(port)) throw new HTTPException14(400, { message: "port must be a number" });
1233
1333
  services.bots.ports.unpublish(bot(c.req.param("id")), port);
1234
1334
  return c.json({ ok: true });
1235
1335
  });
@@ -1238,15 +1338,16 @@ function portRoutes(services) {
1238
1338
 
1239
1339
  // src/server/api/app.ts
1240
1340
  function createHttpApp(services, webRoot) {
1241
- const app = new Hono15();
1341
+ const app = new Hono16();
1242
1342
  app.use("*", headers);
1243
1343
  app.onError(handleError);
1244
- const api = new Hono15();
1344
+ const api = new Hono16();
1245
1345
  api.use("*", apiSecurity);
1246
1346
  api.route("/", rootCertificateRoute(services.connection));
1247
1347
  installAuthentication(api, services.account);
1248
1348
  api.route("/", taskRoutes(services));
1249
1349
  api.route("/", portRoutes(services));
1350
+ api.route("/", usageRoutes(services.bots));
1250
1351
  api.route("/bots", botRoutes(services.bots));
1251
1352
  api.route("/", channelRoutes(services));
1252
1353
  api.route("/chats", chatRoutes(services));
@@ -1258,7 +1359,7 @@ function createHttpApp(services, webRoot) {
1258
1359
  api.route("/secrets", secretRoutes(services.secrets));
1259
1360
  api.route("/environments", environmentRoutes(services.bots));
1260
1361
  api.all("*", () => {
1261
- throw new HTTPException14(404, { message: "API route not found" });
1362
+ throw new HTTPException15(404, { message: "API route not found" });
1262
1363
  });
1263
1364
  app.route("/api", api);
1264
1365
  app.get("*", serveStatic({ root: webRoot, onFound: (path, c) => {
@@ -1268,8 +1369,8 @@ function createHttpApp(services, webRoot) {
1268
1369
  c.header("Cache-Control", "no-store");
1269
1370
  } }));
1270
1371
  app.all("*", (c) => {
1271
- if (c.req.method !== "GET" && c.req.method !== "HEAD") throw new HTTPException14(405, { message: "Method not allowed" });
1272
- throw new HTTPException14(404, { message: "Web assets missing; run npm run build" });
1372
+ if (c.req.method !== "GET" && c.req.method !== "HEAD") throw new HTTPException15(405, { message: "Method not allowed" });
1373
+ throw new HTTPException15(404, { message: "Web assets missing; run npm run build" });
1273
1374
  });
1274
1375
  return app;
1275
1376
  }
@@ -1649,13 +1750,13 @@ function migrateChannels(db) {
1649
1750
  SELECT cm.chat_id, cm.member_id, COALESCE((SELECT MAX(sequence) FROM messages m WHERE m.chat_id=cm.chat_id), 0) FROM chat_members cm;
1650
1751
  `);
1651
1752
  const taken = new Set(db.prepare("SELECT username FROM account").all().map((row2) => row2.username.toLowerCase()));
1652
- const rename4 = db.prepare("UPDATE bots SET name=? WHERE id=?");
1753
+ const rename5 = db.prepare("UPDATE bots SET name=? WHERE id=?");
1653
1754
  for (const bot of db.prepare("SELECT id,name FROM bots ORDER BY created_at,id").all()) {
1654
1755
  const base = bot.name.replace(/@/g, "").trim() || "Bot";
1655
1756
  let name3 = base;
1656
1757
  for (let suffix = 2; taken.has(name3.toLowerCase()); suffix++) name3 = `${base} ${suffix}`;
1657
1758
  taken.add(name3.toLowerCase());
1658
- if (name3 !== bot.name) rename4.run(name3, bot.id);
1759
+ if (name3 !== bot.name) rename5.run(name3, bot.id);
1659
1760
  }
1660
1761
  db.exec("PRAGMA user_version=12; COMMIT;");
1661
1762
  } catch (error) {
@@ -2150,7 +2251,7 @@ function sizeOf(path) {
2150
2251
  }
2151
2252
 
2152
2253
  // src/server/infra/storage/migrations/own-computer-schema.ts
2153
- import { createHash as createHash2, randomUUID as randomUUID2 } from "node:crypto";
2254
+ import { createHash as createHash2, randomUUID as randomUUID3 } from "node:crypto";
2154
2255
  function migrateOwnComputers(db, dataDir) {
2155
2256
  const tag = createHash2("sha256").update(dataDir).digest("hex").slice(0, 6);
2156
2257
  migration(db, 29, () => {
@@ -2161,7 +2262,7 @@ function migrateOwnComputers(db, dataDir) {
2161
2262
  for (const bot of bots) {
2162
2263
  if (bot.homeKind === "computer") continue;
2163
2264
  const existing = db.prepare("SELECT id FROM environments WHERE bot_id=? AND kind='computer' AND deleted_at IS NULL").get(bot.id);
2164
- const id = existing?.id ?? randomUUID2();
2265
+ const id = existing?.id ?? randomUUID3();
2165
2266
  if (!existing) db.prepare("INSERT INTO environments(id,owner_id,kind,connection,directory,bot_id,created_at) VALUES(?,?,'computer',?,'/workspace',?,?)").run(id, owner, JSON.stringify({ container: `jarvy-${tag}-${bot.id.slice(0, 8)}` }), bot.id, now);
2166
2267
  db.prepare("UPDATE bots SET home_environment_id=?, current_environment_id=? WHERE id=?").run(id, id, bot.id);
2167
2268
  }
@@ -2187,10 +2288,10 @@ function migratePublishedPorts(db) {
2187
2288
  }
2188
2289
 
2189
2290
  // src/server/infra/storage/migrations/schema.ts
2190
- import { randomUUID as randomUUID4 } from "node:crypto";
2291
+ import { randomUUID as randomUUID5 } from "node:crypto";
2191
2292
 
2192
2293
  // src/server/infra/storage/migrations/prototype-schema.ts
2193
- import { randomUUID as randomUUID3 } from "node:crypto";
2294
+ import { randomUUID as randomUUID4 } from "node:crypto";
2194
2295
  function migratePrototype(db) {
2195
2296
  const version = db.prepare("PRAGMA user_version").get().user_version;
2196
2297
  if (version >= 4) return;
@@ -2256,7 +2357,7 @@ function migratePrototype(db) {
2256
2357
  const legacy = db.prepare("SELECT provider,base_url AS baseUrl,model,api_key AS apiKey FROM model_credentials WHERE id=1").get();
2257
2358
  const botModels = db.prepare("SELECT DISTINCT model_id AS modelId FROM bots WHERE model_id<>''").all();
2258
2359
  if (legacy && (legacy.baseUrl || legacy.model || legacy.apiKey || botModels.length)) {
2259
- const id = randomUUID3(), now = Date.now();
2360
+ const id = randomUUID4(), now = Date.now();
2260
2361
  db.prepare("INSERT INTO provider_connections(id,provider_name,display_name,kind,auth_type,base_url,api,created_at,updated_at) VALUES(?,?,?,?,?,?,?,?,?)").run(id, legacy.provider || "openai-compatible", "\u65E7\u6A21\u578B\u914D\u7F6E", "custom", "api_key", legacy.baseUrl, "openai-completions", now, now);
2261
2362
  if (legacy.apiKey) db.prepare("INSERT INTO provider_credentials(connection_id,type,credential_json,updated_at) VALUES(?,'api_key',?,?)").run(id, JSON.stringify({ type: "api_key", key: legacy.apiKey }), now);
2262
2363
  const modelIds = new Set([legacy.model, ...botModels.map((row2) => row2.modelId)].filter(Boolean));
@@ -2340,7 +2441,7 @@ function migrateDatabase(db, cipher, dataDir) {
2340
2441
  }
2341
2442
  db.exec("PRAGMA foreign_keys=OFF; BEGIN IMMEDIATE");
2342
2443
  try {
2343
- const userId = randomUUID4();
2444
+ const userId = randomUUID5();
2344
2445
  db.exec(`
2345
2446
  CREATE TABLE members(id TEXT PRIMARY KEY, kind TEXT NOT NULL CHECK(kind IN ('user','bot')));
2346
2447
  ALTER TABLE account ADD COLUMN member_id TEXT REFERENCES members(id);
@@ -2424,7 +2525,7 @@ function migrateDatabase(db, cipher, dataDir) {
2424
2525
  }
2425
2526
 
2426
2527
  // src/server/infra/storage/account-repository.ts
2427
- import { createHash as createHash3, randomBytes as randomBytes2, randomUUID as randomUUID5, scryptSync, timingSafeEqual } from "node:crypto";
2528
+ import { createHash as createHash3, randomBytes as randomBytes2, randomUUID as randomUUID6, scryptSync, timingSafeEqual } from "node:crypto";
2428
2529
 
2429
2530
  // src/server/infra/storage/text.ts
2430
2531
  function requireText(value2, label, max) {
@@ -2452,7 +2553,7 @@ var AccountRepository = class {
2452
2553
  installationDeviceId() {
2453
2554
  const row2 = this.db.prepare("SELECT device_id FROM installation WHERE id=1").get();
2454
2555
  if (row2) return row2.device_id;
2455
- const id = randomUUID5();
2556
+ const id = randomUUID6();
2456
2557
  this.db.prepare("INSERT INTO installation(id,device_id) VALUES(1,?)").run(id);
2457
2558
  return id;
2458
2559
  }
@@ -2499,13 +2600,13 @@ var AccountRepository = class {
2499
2600
  // src/server/infra/storage/bot-repository.ts
2500
2601
  import { mkdirSync as mkdirSync2 } from "node:fs";
2501
2602
  import { join as join4 } from "node:path";
2502
- import { randomInt as randomInt2, randomUUID as randomUUID7 } from "node:crypto";
2603
+ import { randomInt as randomInt2, randomUUID as randomUUID8 } from "node:crypto";
2503
2604
 
2504
2605
  // src/server/infra/storage/model-connection-repository.ts
2505
- import { randomUUID as randomUUID6 } from "node:crypto";
2606
+ import { randomUUID as randomUUID7 } from "node:crypto";
2506
2607
 
2507
2608
  // src/server/infra/pi/catalog.ts
2508
- import { mkdir as mkdir2, readFile, rename as rename2, writeFile } from "node:fs/promises";
2609
+ import { mkdir as mkdir2, readFile, rename as rename3, writeFile } from "node:fs/promises";
2509
2610
  import { dirname as dirname3, join as join3 } from "node:path";
2510
2611
  import { builtinProviders, getBuiltinModelDataGeneratedAt } from "@earendil-works/pi-ai/providers/all";
2511
2612
  var CATALOG_URL = "https://pi.dev/api/models";
@@ -2634,7 +2735,7 @@ var ModelCatalog = class {
2634
2735
  await mkdir2(dirname3(this.path), { recursive: true, mode: 448 });
2635
2736
  const next = `${this.path}.tmp`;
2636
2737
  await writeFile(next, JSON.stringify(this.kept), { mode: 384 });
2637
- await rename2(next, this.path);
2738
+ await rename3(next, this.path);
2638
2739
  }
2639
2740
  };
2640
2741
  function merge(models, newer) {
@@ -2755,7 +2856,7 @@ var ModelConnectionRepository = class {
2755
2856
  const baseUrl = input.baseUrl === void 0 ? "" : modelUrl(optionalText(input.baseUrl, "Base URL", 2048));
2756
2857
  const key = input.apiKey === void 0 ? null : requireText(input.apiKey, "API Key", 8192);
2757
2858
  if (key && authType !== "api_key") throw new ValidationError("Invalid credential type");
2758
- const id = randomUUID6(), now = Date.now();
2859
+ const id = randomUUID7(), now = Date.now();
2759
2860
  transaction(this.db, () => {
2760
2861
  this.db.prepare("INSERT INTO provider_connections(id,provider_name,display_name,kind,auth_type,base_url,api,created_at,updated_at,owner_id) VALUES(?,?,?,?,?,?,?,?,?,?)").run(id, providerName, displayName, kind, authType, baseUrl, api, now, now, this.ownerId());
2761
2862
  if (key) this.db.prepare("INSERT INTO provider_credentials(connection_id,type,credential_json,updated_at) VALUES(?,'api_key',?,?)").run(id, this.credentials.stored(id, { type: "api_key", key }), now);
@@ -2918,7 +3019,7 @@ var BotRepository = class {
2918
3019
  /** The caller names the Bot (A45: the interface picks a free default in its own language). */
2919
3020
  create(name3) {
2920
3021
  const clean = this.botName(name3);
2921
- const id = randomUUID7(), createdAt = Date.now();
3022
+ const id = randomUUID8(), createdAt = Date.now();
2922
3023
  transaction(this.db, () => {
2923
3024
  this.db.prepare("INSERT INTO members VALUES(?,'bot')").run(id);
2924
3025
  this.db.prepare("INSERT INTO bots(id,name,description,system_prompt,model_id,created_at,owner_id,thinking_level,color) VALUES(?,?,?,?,?,?,?,'auto',?)").run(id, clean, "", "", "", createdAt, this.ownerId(), BOT_COLORS[randomInt2(BOT_COLORS.length)]);
@@ -2953,10 +3054,10 @@ var BotRepository = class {
2953
3054
  };
2954
3055
 
2955
3056
  // src/server/infra/storage/chat-repository.ts
2956
- import { randomUUID as randomUUID10 } from "node:crypto";
3057
+ import { randomUUID as randomUUID11 } from "node:crypto";
2957
3058
 
2958
3059
  // src/server/infra/storage/channel-repository.ts
2959
- import { randomUUID as randomUUID8 } from "node:crypto";
3060
+ import { randomUUID as randomUUID9 } from "node:crypto";
2960
3061
 
2961
3062
  // src/server/shared/delivery.ts
2962
3063
  var CATEGORY = {
@@ -3068,13 +3169,14 @@ var InboxRepository = class {
3068
3169
  for (const id of ids) this.db.prepare("UPDATE inbox SET state='told',told_at=? WHERE id=? AND member_id=? AND state='pending'").run(now, id, botId);
3069
3170
  }
3070
3171
  // A person's rows: what their chat list shows.
3071
- /** What a person's chat row shows (A41): unread interrupt messages, and the newest queued activity. */
3172
+ /** What a person's chat row shows (A41): unread interrupt messages, and the newest activity not read yet (queued,
3173
+ * or a notice version, which is a digest, A77). */
3072
3174
  rowState(memberId, chatId) {
3073
3175
  const counts = this.db.prepare(`SELECT count(*) AS n,COALESCE(SUM(reason IN ('mention','reply')),0) AS at FROM inbox
3074
3176
  WHERE member_id=? AND chat_id=? AND category='message' AND urgency='interrupt' AND state='pending'`).get(memberId, chatId);
3075
3177
  const row2 = this.db.prepare(`SELECT i.kind,i.chat_id AS chatId,c.type AS chatType,c.title,i.actor_id AS actorId,i.payload,i.created_at AS createdAt
3076
3178
  FROM inbox i JOIN chats c ON c.id=i.chat_id
3077
- WHERE i.member_id=? AND i.category='activity' AND i.urgency='queue' AND i.state='pending' AND (i.chat_id=? OR c.parent_id=?) ORDER BY i.id DESC LIMIT 1`).get(memberId, chatId, chatId);
3179
+ WHERE i.member_id=? AND i.category='activity' AND i.urgency IN ('queue','digest') AND i.state='pending' AND (i.chat_id=? OR c.parent_id=?) ORDER BY i.id DESC LIMIT 1`).get(memberId, chatId, chatId);
3078
3180
  if (!row2) return { alerts: counts.n, atMe: counts.at > 0, hint: null };
3079
3181
  const payload = row2.payload ? JSON.parse(row2.payload) : {};
3080
3182
  const detail = row2.kind === "reaction" ? payload.emoji ?? "" : String(payload.version ?? "");
@@ -3142,7 +3244,7 @@ var ChannelRepository = class {
3142
3244
  return this.db.prepare(`SELECT ${SECTION_COLUMNS} FROM sections WHERE id=? AND owner_id=?`).get(id, ownerId) ?? null;
3143
3245
  }
3144
3246
  createSection(ownerId, name3) {
3145
- const id = randomUUID8(), now = Date.now();
3247
+ const id = randomUUID9(), now = Date.now();
3146
3248
  const position = this.db.prepare("SELECT COALESCE(MAX(position),0)+1 AS next FROM sections WHERE owner_id=?").get(ownerId).next;
3147
3249
  this.db.prepare("INSERT INTO sections(id,owner_id,name,position,created_at,updated_at) VALUES(?,?,?,?,?,?)").run(id, ownerId, name3, position, now, now);
3148
3250
  return this.section(ownerId, id);
@@ -3162,7 +3264,7 @@ var ChannelRepository = class {
3162
3264
  const allowed = new Set(this.directory(ownerId).map((member) => member.id));
3163
3265
  const members = [.../* @__PURE__ */ new Set([ownerId, ...input.memberIds])];
3164
3266
  if (members.some((id2) => !allowed.has(id2))) throw new ChatError(400, "Invalid channel members");
3165
- const id = randomUUID8(), now = Date.now();
3267
+ const id = randomUUID9(), now = Date.now();
3166
3268
  return transaction(this.db, () => {
3167
3269
  this.db.prepare("INSERT INTO chats(id,type,title,owner_id,created_at,updated_at,section_id,topic) VALUES(?,'channel',?,?,?,?,?,?)").run(id, input.name, ownerId, now, now, input.sectionId, input.topic);
3168
3270
  for (const memberId of members) {
@@ -3291,7 +3393,7 @@ var ChannelRepository = class {
3291
3393
  };
3292
3394
 
3293
3395
  // src/server/infra/storage/thread-repository.ts
3294
- import { randomUUID as randomUUID9 } from "node:crypto";
3396
+ import { randomUUID as randomUUID10 } from "node:crypto";
3295
3397
  function threadTitle(content2, names, file = "") {
3296
3398
  const line = (content2.split("\n").map((item) => item.trim()).find(Boolean) ?? "").replace(/[*`~]/g, "").replace(/^[#>\s-]+/, "");
3297
3399
  const sorted = [...names].sort((a, b) => b.length - a.length);
@@ -3321,7 +3423,7 @@ var ThreadRepository = class {
3321
3423
  if (root.type !== "channel") throw new ChatError(400, "Threads start from channel messages");
3322
3424
  let id = this.db.prepare("SELECT id FROM chats WHERE root_message_id=?").get(rootMessageId)?.id;
3323
3425
  if (!id) {
3324
- id = randomUUID9();
3426
+ id = randomUUID10();
3325
3427
  const now = Date.now();
3326
3428
  const firstFile = this.db.prepare("SELECT name FROM attachments WHERE message_id=? ORDER BY created_at,rowid LIMIT 1").get(rootMessageId)?.name;
3327
3429
  this.db.prepare(`INSERT INTO chats(id,type,title,owner_id,created_at,updated_at,parent_id,root_message_id,status)
@@ -3451,6 +3553,44 @@ var QuoteRepository = class {
3451
3553
  }
3452
3554
  };
3453
3555
 
3556
+ // src/contract/message-refs.ts
3557
+ var MESSAGE_ID = /(?<![A-Za-z0-9_/#.@-])m\d{1,9}(?![A-Za-z0-9_-]|\.[A-Za-z0-9])/g;
3558
+ var MESSAGE_REFS_MAX = 20;
3559
+ function messageIds(text5) {
3560
+ const ids = [];
3561
+ for (const [id] of text5.matchAll(MESSAGE_ID)) if (!ids.includes(id) && ids.push(id) >= MESSAGE_REFS_MAX) break;
3562
+ return ids;
3563
+ }
3564
+
3565
+ // src/server/infra/storage/message-ref-repository.ts
3566
+ var MessageRefRepository = class {
3567
+ constructor(db, quotes) {
3568
+ this.db = db;
3569
+ this.quotes = quotes;
3570
+ }
3571
+ of(readerId, messageId, content2) {
3572
+ const ids = messageIds(content2).filter((id) => id !== messageId);
3573
+ if (!ids.length) return [];
3574
+ const rows3 = this.db.prepare(`SELECT m.id,m.sender_id AS senderId,m.content,m.created_at AS createdAt,c.id AS chatId,c.type,p.title AS parentTitle,
3575
+ CASE WHEN c.type='dm' THEN COALESCE((SELECT group_concat(b.name,' \xB7 ') FROM chat_members d JOIN bots b ON b.id=d.member_id WHERE d.chat_id=c.id),c.title) ELSE c.title END AS title
3576
+ FROM messages m JOIN chats c ON c.id=m.chat_id LEFT JOIN chats p ON p.id=c.parent_id
3577
+ WHERE m.id IN (${ids.map(() => "?").join(",")}) AND (EXISTS (SELECT 1 FROM chat_members cm WHERE cm.chat_id=COALESCE(c.parent_id,c.id)
3578
+ AND cm.member_id=? AND cm.can_read=1) OR (c.type='dm' AND c.owner_id=?))`).all(...ids, readerId, readerId);
3579
+ const found = new Map(rows3.map((row2) => [row2.id, row2]));
3580
+ return ids.flatMap((id) => {
3581
+ const row2 = found.get(id);
3582
+ return row2 ? [{
3583
+ id,
3584
+ senderId: row2.senderId,
3585
+ content: clipMarked(row2.content, 200),
3586
+ quotes: this.quotes.of(id),
3587
+ createdAt: row2.createdAt,
3588
+ chat: { id: row2.chatId, type: row2.type, title: row2.title, parentTitle: row2.parentTitle }
3589
+ }] : [];
3590
+ });
3591
+ }
3592
+ };
3593
+
3454
3594
  // src/server/infra/storage/chat-repository.ts
3455
3595
  var MESSAGE_COLUMNS = "id,chat_id AS chatId,sequence,sender_id AS senderId,content,created_at AS createdAt,history_id AS historyId,tool_call_id AS toolCallId,question,echo_of AS echoOf,reply_to AS replyTo,folded_at AS foldedAt";
3456
3596
  var CHAT_COLUMNS = `c.id,c.type,c.title,c.owner_id AS ownerId,c.created_at AS createdAt,c.updated_at AS updatedAt,
@@ -3466,6 +3606,7 @@ var ChatRepository = class {
3466
3606
  this.threads = new ThreadRepository(db, this);
3467
3607
  this.interactions = new InteractionRepository(db, this);
3468
3608
  this.quotes = new QuoteRepository(db);
3609
+ this.refs = new MessageRefRepository(db, this.quotes);
3469
3610
  this.inbox = new InboxRepository(db, this);
3470
3611
  }
3471
3612
  /** Sections, channels, notes, read positions and threads share this connection. */
@@ -3473,6 +3614,7 @@ var ChatRepository = class {
3473
3614
  threads;
3474
3615
  interactions;
3475
3616
  quotes;
3617
+ refs;
3476
3618
  /** Every delivery (A41, A61): the one place inbox SQL lives. */
3477
3619
  inbox;
3478
3620
  member(id) {
@@ -3547,7 +3689,7 @@ var ChatRepository = class {
3547
3689
  const id = transaction(this.db, () => {
3548
3690
  const existing = this.db.prepare(`SELECT c.id FROM chats c JOIN chat_members a ON a.chat_id=c.id AND a.member_id=?
3549
3691
  JOIN chat_members b ON b.chat_id=c.id AND b.member_id=? WHERE c.type='dm' ORDER BY c.updated_at DESC,c.id DESC LIMIT 1`).get(actorId, targetId);
3550
- const id2 = existing?.id ?? randomUUID10();
3692
+ const id2 = existing?.id ?? randomUUID11();
3551
3693
  if (!existing) {
3552
3694
  this.db.prepare("INSERT INTO chats(id,type,title,owner_id,created_at,updated_at) VALUES(?,'dm',?,?,?,?)").run(id2, title, ownerId, now, now);
3553
3695
  this.db.prepare("INSERT INTO chat_members(chat_id,member_id) VALUES(?,?),(?,?)").run(id2, actorId, id2, targetId);
@@ -3590,14 +3732,16 @@ var ChatRepository = class {
3590
3732
  messagesBefore(actorId, chatId, before, limit2) {
3591
3733
  this.assertAccess(actorId, chatId);
3592
3734
  const rows3 = this.db.prepare(`SELECT ${MESSAGE_COLUMNS} FROM messages WHERE chat_id=? AND sequence<? ORDER BY sequence DESC LIMIT ?`).all(chatId, before, limit2);
3593
- return rows3.reverse().map((row2) => this.messageFromRow(row2));
3735
+ const reader = this.reader(actorId);
3736
+ return rows3.reverse().map((row2) => this.messageFromRow(row2, reader));
3594
3737
  }
3595
3738
  /** Exactly the messages counted as unread for this member (same rule as `unread`), oldest first. */
3596
3739
  unreadMessages(memberId, chatId, limit2) {
3597
3740
  this.assertAccess(memberId, chatId);
3598
3741
  if (this.db.prepare("SELECT type FROM chats WHERE id=?").get(chatId).type === "thread" && !this.threads.isFollowing(memberId, chatId)) return [];
3599
3742
  const rows3 = this.db.prepare(`SELECT ${MESSAGE_COLUMNS.split(",").map((column) => `m.${column}`).join(",")} ${unreadIn(":chat")} ORDER BY m.sequence LIMIT :limit`).all({ chat: chatId, member: memberId, limit: limit2 });
3600
- return rows3.map((row2) => this.messageFromRow(row2));
3743
+ const reader = this.reader(memberId);
3744
+ return rows3.map((row2) => this.messageFromRow(row2, reader));
3601
3745
  }
3602
3746
  /** Search every chat the member can read: its DMs and channels and their threads (newest first). `within` keeps
3603
3747
  * one chat, a channel with its threads; `sender` keeps one member's messages. */
@@ -3606,7 +3750,8 @@ var ChatRepository = class {
3606
3750
  const rows3 = this.db.prepare(`SELECT ${MESSAGE_COLUMNS.split(",").map((column) => `m.${column}`).join(",")} FROM messages m JOIN chats c ON c.id=m.chat_id
3607
3751
  JOIN chat_members cm ON cm.chat_id=COALESCE(c.parent_id,c.id) AND cm.member_id=? AND cm.can_read=1
3608
3752
  WHERE instr(lower(m.content),lower(?))>0 ${scope} ORDER BY m.position DESC LIMIT ?`).all(actorId, query, ...within ? [within, within] : [], ...sender ? [sender] : [], limit2);
3609
- return rows3.map((row2) => this.messageFromRow(row2));
3753
+ const reader = this.reader(actorId);
3754
+ return rows3.map((row2) => this.messageFromRow(row2, reader));
3610
3755
  }
3611
3756
  /** Channels and threads the member can read whose title, topic or notice / summary contains the query (newest first). */
3612
3757
  searchChatIds(actorId, query, limit2) {
@@ -3618,17 +3763,19 @@ var ChatRepository = class {
3618
3763
  messages(actorId, chatId, after2 = 0, limit2 = 1e3) {
3619
3764
  this.assertAccess(actorId, chatId);
3620
3765
  const rows3 = this.db.prepare(`SELECT ${MESSAGE_COLUMNS} FROM messages WHERE chat_id=? AND sequence>? ORDER BY sequence LIMIT ?`).all(chatId, after2, limit2);
3621
- return rows3.map((row2) => this.messageFromRow(row2));
3766
+ const reader = this.reader(actorId);
3767
+ return rows3.map((row2) => this.messageFromRow(row2, reader));
3622
3768
  }
3623
3769
  message(actorId, id) {
3624
3770
  const row2 = this.db.prepare(`SELECT ${MESSAGE_COLUMNS} FROM messages WHERE id=?`).get(id);
3625
- return row2 && this.canAccess(actorId, row2.chatId) ? this.messageFromRow(row2) : null;
3771
+ return row2 && this.canAccess(actorId, row2.chatId) ? this.messageFromRow(row2, this.reader(actorId)) : null;
3626
3772
  }
3627
3773
  search(actorId, chatId, query, limit2, sender) {
3628
3774
  this.assertAccess(actorId, chatId);
3629
3775
  const rows3 = this.db.prepare(`SELECT ${MESSAGE_COLUMNS} FROM messages WHERE chat_id=? AND instr(lower(content),lower(?))>0 ${sender ? "AND sender_id=?" : ""}
3630
3776
  ORDER BY sequence DESC LIMIT ?`).all(chatId, query, ...sender ? [sender] : [], limit2);
3631
- return rows3.map((row2) => this.messageFromRow(row2));
3777
+ const reader = this.reader(actorId);
3778
+ return rows3.map((row2) => this.messageFromRow(row2, reader));
3632
3779
  }
3633
3780
  sentActivity(actorId, senderId, before, limit2) {
3634
3781
  const condition = !before ? "" : before.kind === "chat" ? "AND (m.created_at<? OR (m.created_at=? AND m.id<?))" : "AND m.created_at<=?";
@@ -3641,7 +3788,11 @@ var ChatRepository = class {
3641
3788
  WHERE m.sender_id=? ${condition} ORDER BY m.created_at DESC,m.id DESC LIMIT ?`).all(actorId, senderId, ...cursor2, limit2);
3642
3789
  return rows3.map(({ historyId, toolCallId, ...row2 }) => ({ ...row2, source: historyId === null ? null : { historyId, toolCallId } }));
3643
3790
  }
3644
- messageFromRow(row2) {
3791
+ /** Who reads the snapshots: a person gets the messages a text names by ID (A82); a Bot reads IDs itself. */
3792
+ reader(actorId) {
3793
+ return this.member(actorId)?.kind === "bot" ? null : actorId;
3794
+ }
3795
+ messageFromRow(row2, reader) {
3645
3796
  const attachments = this.db.prepare("SELECT id,name,mime_type AS mimeType,size FROM attachments WHERE message_id=? ORDER BY created_at,rowid").all(row2.id);
3646
3797
  return {
3647
3798
  id: row2.id,
@@ -3659,7 +3810,8 @@ var ChatRepository = class {
3659
3810
  replyTo: row2.replyTo === null ? null : this.quote(row2.replyTo),
3660
3811
  reactions: this.interactions.reactions(row2.id),
3661
3812
  quotes: this.quotes.of(row2.id),
3662
- folded: row2.foldedAt !== null
3813
+ folded: row2.foldedAt !== null,
3814
+ refs: reader ? this.refs.of(reader, row2.id, row2.content) : []
3663
3815
  };
3664
3816
  }
3665
3817
  /** What a reply shows of the message it answers: who wrote it and the start of its content, with its quotes ("" is attachments only). */
@@ -3675,7 +3827,7 @@ var ChatRepository = class {
3675
3827
  const prior = this.db.prepare(`SELECT ${MESSAGE_COLUMNS},request_hash AS requestHash FROM messages WHERE sender_id=? AND client_key=?`).get(actorId, draft.clientMessageId);
3676
3828
  if (prior) {
3677
3829
  if (prior.requestHash !== requestHash) throw new ChatError(409, "Message key already used with different content");
3678
- const message2 = this.messageFromRow(prior);
3830
+ const message2 = this.messageFromRow(prior, this.reader(actorId));
3679
3831
  return { message: message2, duplicate: true };
3680
3832
  }
3681
3833
  }
@@ -3721,13 +3873,13 @@ var ChatRepository = class {
3721
3873
  echoOf: chatId,
3722
3874
  replyTo: null,
3723
3875
  foldedAt: null
3724
- });
3876
+ }, this.reader(actorId));
3725
3877
  }
3726
3878
  }
3727
3879
  const file = this.db.prepare("SELECT name FROM attachments WHERE message_id=? ORDER BY created_at,rowid LIMIT 1").get(id);
3728
3880
  const title = draft.content ? previewQuotes(draft.content, draft.quotes ?? []).slice(0, 46).replace(/\s+/g, " ") : file?.name.slice(0, 46) ?? "";
3729
3881
  this.db.prepare("UPDATE chats SET title=CASE WHEN title='' AND ?=1 THEN ? ELSE title END,updated_at=? WHERE id=?").run(sequence, title, createdAt, chatId);
3730
- const message = this.messageFromRow({ id, chatId, sequence, senderId: actorId, content: draft.content, createdAt, historyId: draft.source?.historyId ?? null, toolCallId: draft.source?.toolCallId ?? null, question, echoOf: null, replyTo: draft.replyTo ?? null, foldedAt: null });
3882
+ const message = this.messageFromRow({ id, chatId, sequence, senderId: actorId, content: draft.content, createdAt, historyId: draft.source?.historyId ?? null, toolCallId: draft.source?.toolCallId ?? null, question, echoOf: null, replyTo: draft.replyTo ?? null, foldedAt: null }, this.reader(actorId));
3731
3883
  return { message, duplicate: false, ...echo ? { echo } : {} };
3732
3884
  });
3733
3885
  }
@@ -3779,7 +3931,7 @@ var ChatRepository = class {
3779
3931
  };
3780
3932
 
3781
3933
  // src/server/infra/storage/runtime-repository.ts
3782
- import { randomUUID as randomUUID11 } from "node:crypto";
3934
+ import { randomUUID as randomUUID12 } from "node:crypto";
3783
3935
  var RUN_COLUMNS = "id,bot_id AS botId,state,selection_json AS selectionJson,created_at AS createdAt,ended_at AS endedAt,error_code AS errorCode";
3784
3936
  var RuntimeRepository = class {
3785
3937
  /** `inbox`: a run claims and settles the Bot's message deliveries there (A41). */
@@ -3811,7 +3963,7 @@ var RuntimeRepository = class {
3811
3963
  return transaction(this.db, () => {
3812
3964
  const inputs = this.inbox.pendingMessages(botId);
3813
3965
  if (!inputs.length && !allowEmpty) return null;
3814
- const id = randomUUID11();
3966
+ const id = randomUUID12();
3815
3967
  this.db.prepare("INSERT INTO bot_runs(id,bot_id,state,selection_json,created_at) VALUES(?,?,'claimed',?,?)").run(id, botId, JSON.stringify(selection), Date.now());
3816
3968
  inputs.forEach((input, position) => this.db.prepare("INSERT INTO bot_run_inputs VALUES(?,?,?,?)").run(id, position + 1, input.chatId, input.messageId));
3817
3969
  this.inbox.claimMessages(botId, id);
@@ -3866,7 +4018,7 @@ var RuntimeRepository = class {
3866
4018
  return !!this.db.prepare("SELECT 1 FROM bot_model_changes WHERE bot_id=? AND state IN ('applying','unknown') LIMIT 1").get(botId);
3867
4019
  }
3868
4020
  beginModelChange(botId, selection) {
3869
- const id = randomUUID11();
4021
+ const id = randomUUID12();
3870
4022
  this.db.prepare("INSERT INTO bot_model_changes(id,bot_id,selection_json,state,created_at) VALUES(?,?,?,'applying',?)").run(id, botId, JSON.stringify(selection), Date.now());
3871
4023
  return id;
3872
4024
  }
@@ -3965,7 +4117,7 @@ var AgentRepository = class {
3965
4117
  };
3966
4118
 
3967
4119
  // src/server/infra/storage/environment-repository.ts
3968
- import { randomUUID as randomUUID12 } from "node:crypto";
4120
+ import { randomUUID as randomUUID13 } from "node:crypto";
3969
4121
  var FIELDS = (table) => ["id", "owner_id AS ownerId", "kind", "bot_id AS botId", "name", "directory", "description", "connection"].map((column) => `${table}${column}`).join(", ");
3970
4122
  var COLUMNS = FIELDS("");
3971
4123
  var record = (row2) => ({
@@ -4001,7 +4153,7 @@ var EnvironmentRepository = class {
4001
4153
  addComputer(botId, container) {
4002
4154
  const existing = this.computerOf(botId);
4003
4155
  if (existing) return existing;
4004
- const id = randomUUID12();
4156
+ const id = randomUUID13();
4005
4157
  this.db.prepare("INSERT INTO environments(id,owner_id,kind,connection,directory,bot_id,created_at) VALUES(?,?,'computer',?,'/workspace',?,?)").run(id, this.ownerId(), JSON.stringify({ container }), botId, Date.now());
4006
4158
  return this.get(id);
4007
4159
  }
@@ -4061,7 +4213,7 @@ var EnvironmentRepository = class {
4061
4213
  sshMachines() {
4062
4214
  return this.db.prepare(`SELECT ${COLUMNS} FROM environments WHERE kind='ssh' AND deleted_at IS NULL ORDER BY created_at, id`).all().map(record);
4063
4215
  }
4064
- addSsh(input, id = randomUUID12()) {
4216
+ addSsh(input, id = randomUUID13()) {
4065
4217
  const { host, port, user, home, platform } = input;
4066
4218
  this.db.prepare("INSERT INTO environments(id,owner_id,kind,name,connection,directory,description,key,host_keys,password,created_at) VALUES(?,?,'ssh',?,?,?,?,?,?,?,?)").run(
4067
4219
  id,
@@ -4134,7 +4286,7 @@ var SettingsRepository = class {
4134
4286
  };
4135
4287
 
4136
4288
  // src/server/infra/storage/secret-repository.ts
4137
- import { randomUUID as randomUUID13 } from "node:crypto";
4289
+ import { randomUUID as randomUUID14 } from "node:crypto";
4138
4290
 
4139
4291
  // src/server/shared/secret.ts
4140
4292
  var SECRET_NAME = /^[A-Z][A-Z0-9_]{0,63}$/;
@@ -4177,7 +4329,7 @@ var SecretRepository = class {
4177
4329
  if (typeof input.name !== "string" || !SECRET_NAME.test(input.name)) throw new ValidationError("Names use capital letters, digits and underscores, and start with a letter", "secret_name_invalid");
4178
4330
  const description = this.checkDescription(input.description), value2 = this.checkValue(input.value);
4179
4331
  if (this.db.prepare("SELECT 1 FROM secrets WHERE owner_id=? AND kind='value' AND name=?").get(this.ownerId(), input.name)) throw new ConflictError("Another secret has this name", "secret_name_taken");
4180
- const id = randomUUID13(), now = Date.now();
4332
+ const id = randomUUID14(), now = Date.now();
4181
4333
  this.db.prepare("INSERT INTO secrets(id,owner_id,name,kind,bot_id,description,value,fingerprint,created_at,updated_at) VALUES(?,?,?,'value',NULL,?,?,?,?,?)").run(id, this.ownerId(), input.name, description, this.cipher.seal(value2, `secret:${id}`), this.cipher.fingerprint(value2), now, now);
4182
4334
  return this.get(id);
4183
4335
  }
@@ -4238,7 +4390,7 @@ var SecretRepository = class {
4238
4390
  };
4239
4391
 
4240
4392
  // src/server/infra/storage/task-repository.ts
4241
- import { randomUUID as randomUUID14 } from "node:crypto";
4393
+ import { randomUUID as randomUUID15 } from "node:crypto";
4242
4394
 
4243
4395
  // src/server/shared/task.ts
4244
4396
  var TASK_MINUTES_MIN = 5;
@@ -4333,7 +4485,7 @@ var TaskRepository = class {
4333
4485
  create(input) {
4334
4486
  const now = Date.now(), at2 = nextAfter(input.schedule, now);
4335
4487
  if (at2 === null) throw new ValidationError("A one-off task has to be in the future", "task_time_past");
4336
- const id = randomUUID14(), columns = scheduleColumns(input.schedule);
4488
+ const id = randomUUID15(), columns = scheduleColumns(input.schedule);
4337
4489
  this.db.prepare(`INSERT INTO tasks(id,bot_id,name,instruction,schedule_kind,schedule_at,schedule_minutes,schedule_hour,schedule_minute,schedule_weekday,
4338
4490
  state,next_run_at,last_run_at,created_at,updated_at) VALUES(?,?,?,?,?,?,?,?,?,?,'active',?,NULL,?,?)`).run(id, input.botId, input.name, input.instruction, columns.kind, columns.at, columns.minutes, columns.hour, columns.minute, columns.weekday, at2, now, now);
4339
4491
  return this.get(input.botId, id);
@@ -4785,7 +4937,7 @@ var AccountService = class {
4785
4937
 
4786
4938
  // src/server/infra/pi/model.ts
4787
4939
  import { ModelRuntime } from "@earendil-works/pi-coding-agent";
4788
- var FORMATS = /* @__PURE__ */ new Set(["openai", "openrouter", "deepseek", "together", "baseten", "zai", "qwen", "chat-template", "qwen-chat-template", "string-thinking", "ant-ling"]);
4940
+ var FORMATS2 = /* @__PURE__ */ new Set(["openai", "openrouter", "deepseek", "together", "baseten", "zai", "qwen", "chat-template", "qwen-chat-template", "string-thinking", "ant-ling"]);
4789
4941
  function installModel(runtime, connection, configured2, headers2 = {}, catalog) {
4790
4942
  const api = configured2.api || connection.api;
4791
4943
  const bundled = runtime.getModel(connection.providerName, configured2.id);
@@ -4795,7 +4947,7 @@ function installModel(runtime, connection, configured2, headers2 = {}, catalog)
4795
4947
  const maxTokens = configured2.maxTokens ?? builtin?.maxTokens;
4796
4948
  const baseUrl = connection.baseUrl || configured2.baseUrl || builtin?.baseUrl || runtime.getModels(connection.providerName)[0]?.baseUrl;
4797
4949
  if (!contextWindow || !maxTokens) throw new RuntimeFault("model_incomplete", "Fill in the model's context window and max output.", 422);
4798
- if (configured2.thinkingFormat && !FORMATS.has(configured2.thinkingFormat)) throw new RuntimeFault("thinking_format", "This thinking format isn't supported.", 422);
4950
+ if (configured2.thinkingFormat && !FORMATS2.has(configured2.thinkingFormat)) throw new RuntimeFault("thinking_format", "This thinking format isn't supported.", 422);
4799
4951
  const compat = { ...builtin?.compat, ...configured2.compat, ...configured2.thinkingFormat ? { thinkingFormat: configured2.thinkingFormat } : {} };
4800
4952
  runtime.registerProvider(connection.providerName, {
4801
4953
  baseUrl,
@@ -5251,7 +5403,7 @@ var ModelService = class {
5251
5403
  };
5252
5404
 
5253
5405
  // src/server/infra/pi/auth.ts
5254
- import { randomUUID as randomUUID15 } from "node:crypto";
5406
+ import { randomUUID as randomUUID16 } from "node:crypto";
5255
5407
  import { ModelRuntime as ModelRuntime2 } from "@earendil-works/pi-coding-agent";
5256
5408
  var CredentialLogins = class {
5257
5409
  constructor(store, credentials, changed) {
@@ -5299,7 +5451,7 @@ var CredentialLogins = class {
5299
5451
  prompt(flow, prompt) {
5300
5452
  const signal = prompt.signal ? AbortSignal.any([flow.controller.signal, prompt.signal]) : flow.controller.signal;
5301
5453
  signal.throwIfAborted();
5302
- const id = randomUUID15();
5454
+ const id = randomUUID16();
5303
5455
  flow.state.prompt = {
5304
5456
  id,
5305
5457
  type: prompt.type,
@@ -5409,11 +5561,11 @@ function mentionedMembers(source2, candidates2) {
5409
5561
  function messageTargets({ dm, sender, members, mentioned, replied, subscribers }) {
5410
5562
  if (dm) return members.filter((member) => member.id !== sender.id).map((member) => ({ member, reason: "dm" }));
5411
5563
  const targets = mentioned.filter((member) => member.id !== sender.id).map((member) => ({ member, reason: "mention" }));
5412
- const add = (member, reason) => {
5564
+ const add2 = (member, reason) => {
5413
5565
  if (member.id !== sender.id && !targets.some((target3) => target3.member.id === member.id)) targets.push({ member, reason });
5414
5566
  };
5415
- if (replied && members.some((member) => member.id === replied.id)) add(replied, "reply");
5416
- for (const member of members) if (subscribers.includes(member.id)) add(member, "subscription");
5567
+ if (replied && members.some((member) => member.id === replied.id)) add2(replied, "reply");
5568
+ for (const member of members) if (subscribers.includes(member.id)) add2(member, "subscription");
5417
5569
  return targets;
5418
5570
  }
5419
5571
  function echoTargets({ sender, members, subscribers, reached }) {
@@ -5432,8 +5584,9 @@ var POLICY = {
5432
5584
  message: { dm: "interrupt", mention: "interrupt", reply: "interrupt", subscription: "interrupt" },
5433
5585
  // Marks on your messages are queued: they wake you but never interrupt (A39).
5434
5586
  reaction: { author: "queue" },
5435
- // A new version of a notice or summary is queued like a mark.
5436
- note: { member: "queue", follower: "queue" },
5587
+ // A new version of a notice or summary rides along with the Bot's next batch: a Bot that is working hears it at
5588
+ // once, and one that is idle is not woken for it (A77). A person's sidebar still shows the hint line.
5589
+ note: { member: "digest", follower: "digest" },
5437
5590
  // The owner just gave the agent Bot its first model: it should welcome them now (A48).
5438
5591
  onboard: { setup: "interrupt" },
5439
5592
  // What the agent Bot changed about a Bot, or the owner about where it works, rides along with that Bot's next batch,
@@ -6300,9 +6453,9 @@ var ChatFiles = class {
6300
6453
  constructor(chats) {
6301
6454
  this.chats = chats;
6302
6455
  }
6303
- async envelope(botId, messageIds, signal) {
6456
+ async envelope(botId, messageIds2, signal) {
6304
6457
  const output = [];
6305
- for (const id of messageIds) {
6458
+ for (const id of messageIds2) {
6306
6459
  signal?.throwIfAborted();
6307
6460
  const message = this.chats.message(botId, id);
6308
6461
  if (!message) throw new ChatError(404, "Message not found");
@@ -6362,12 +6515,12 @@ function byteSize(bytes) {
6362
6515
  }
6363
6516
 
6364
6517
  // src/server/service/events.ts
6365
- import { randomUUID as randomUUID16 } from "node:crypto";
6518
+ import { randomUUID as randomUUID17 } from "node:crypto";
6366
6519
  var KEEP_MS = 10 * 6e4;
6367
6520
  var KEEP_MAX = 5e3;
6368
6521
  var Changes = class {
6369
6522
  /** This run's changes: a number from before a restart says nothing. */
6370
- run = randomUUID16().slice(0, 8);
6523
+ run = randomUUID17().slice(0, 8);
6371
6524
  last = 0;
6372
6525
  kept = [];
6373
6526
  listeners = /* @__PURE__ */ new Set();
@@ -6418,9 +6571,35 @@ function activityPage(messages, history, limit2) {
6418
6571
  return { items, nextBefore: candidates2.length > limit2 ? position : null };
6419
6572
  }
6420
6573
 
6574
+ // src/server/service/bot/usage.ts
6575
+ var empty = () => ({ calls: 0, input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: null });
6576
+ function add(into, row2) {
6577
+ into.calls += row2.calls;
6578
+ into.input += row2.input;
6579
+ into.output += row2.output;
6580
+ into.cacheRead += row2.cacheRead;
6581
+ into.cacheWrite += row2.cacheWrite;
6582
+ if (row2.cost !== null) into.cost = (into.cost ?? 0) + row2.cost;
6583
+ }
6584
+ var costly = (a, b) => (b.cost ?? 0) - (a.cost ?? 0) || b.calls - a.calls;
6585
+ function usageOverview(since2, perBot) {
6586
+ const models = /* @__PURE__ */ new Map(), bots = [];
6587
+ for (const [botId, rows3] of perBot) {
6588
+ if (!rows3.length) continue;
6589
+ const bot = { botId, ...empty() };
6590
+ for (const { provider, model, ...row2 } of rows3) {
6591
+ const key = `${provider}\0${model}`;
6592
+ add(models.get(key) ?? models.set(key, { provider, model, ...empty() }).get(key), row2);
6593
+ add(bot, row2);
6594
+ }
6595
+ bots.push(bot);
6596
+ }
6597
+ return { since: since2, models: [...models.values()].sort(costly), bots: bots.sort(costly) };
6598
+ }
6599
+
6421
6600
  // src/server/service/bot/service.ts
6422
6601
  import { dirname as dirname10, join as join16 } from "node:path";
6423
- import { rm as rm5 } from "node:fs/promises";
6602
+ import { rm as rm6 } from "node:fs/promises";
6424
6603
 
6425
6604
  // src/server/infra/pi/history.ts
6426
6605
  var value = (input) => JSON.parse(JSON.stringify(input));
@@ -6742,7 +6921,7 @@ function resultText(result) {
6742
6921
 
6743
6922
  // src/server/infra/pi/environment-tools.ts
6744
6923
  import { AsyncLocalStorage } from "node:async_hooks";
6745
- import { readFile as readLocal, rm as rm2 } from "node:fs/promises";
6924
+ import { readFile as readLocal, rm as rm3 } from "node:fs/promises";
6746
6925
  import { basename } from "node:path";
6747
6926
  import { createBashToolDefinition, createEditToolDefinition, createReadToolDefinition, createWriteToolDefinition } from "@earendil-works/pi-coding-agent";
6748
6927
 
@@ -7057,7 +7236,7 @@ function relocated(definition, current) {
7057
7236
  async function moveOut(path, env) {
7058
7237
  const target3 = `${env.temp ?? `${env.cwd}/temp`}/${basename(path)}`;
7059
7238
  await env.writeFile(target3, await readLocal(path));
7060
- await rm2(path, { force: true });
7239
+ await rm3(path, { force: true });
7061
7240
  return target3;
7062
7241
  }
7063
7242
  function defined(vars) {
@@ -7375,7 +7554,7 @@ var PiSession = class _PiSession {
7375
7554
  import { execFile, spawn } from "node:child_process";
7376
7555
  import { createHash as createHash6 } from "node:crypto";
7377
7556
  import { createWriteStream } from "node:fs";
7378
- import { mkdir as mkdir4, mkdtemp, readFile as readFile2, rename as rename3, rm as rm3, writeFile as writeFile2 } from "node:fs/promises";
7557
+ import { mkdir as mkdir4, mkdtemp, readFile as readFile2, rename as rename4, rm as rm4, writeFile as writeFile2 } from "node:fs/promises";
7379
7558
  import { tmpdir } from "node:os";
7380
7559
  import { join as join9 } from "node:path";
7381
7560
  import { Connection, RemoteExecutionEnv, packagedDaemon } from "@earendil-works/pi-env";
@@ -7531,7 +7710,7 @@ async function buildImage(tag, uid, gid, logFile) {
7531
7710
  }).finally(() => log.end());
7532
7711
  if (code !== 0) throw Object.assign(new Error(`docker build exited with ${code}`), { code: "docker_failed" });
7533
7712
  } finally {
7534
- await rm3(dir, { recursive: true, force: true });
7713
+ await rm4(dir, { recursive: true, force: true });
7535
7714
  }
7536
7715
  }
7537
7716
  async function containerStates(label) {
@@ -7626,7 +7805,7 @@ async function syncDaemon(dir) {
7626
7805
  await mkdir4(dir, { recursive: true, mode: 448 });
7627
7806
  const temp = `${target3}.${process.pid}.tmp`;
7628
7807
  await writeFile2(temp, wanted, { mode: 493 });
7629
- await rename3(temp, target3);
7808
+ await rename4(temp, target3);
7630
7809
  }
7631
7810
  function containerEnvironment(id, container, log) {
7632
7811
  const connection = new Connection({ command: ["docker", "exec", "-i", container, `${DAEMON_DIR}/pi-env`], onLog: (text5) => log(text5.trim()) });
@@ -8263,13 +8442,13 @@ var BotEnvironments = class {
8263
8442
  };
8264
8443
 
8265
8444
  // src/server/service/bot/machines.ts
8266
- import { randomUUID as randomUUID17 } from "node:crypto";
8445
+ import { randomUUID as randomUUID18 } from "node:crypto";
8267
8446
  import { dirname as dirname6, join as join12, posix } from "node:path";
8268
8447
 
8269
8448
  // src/server/infra/env/ssh.ts
8270
8449
  import { spawn as spawn2 } from "node:child_process";
8271
8450
  import { mkdirSync as mkdirSync6, rmSync as rmSync2, writeFileSync as writeFileSync3 } from "node:fs";
8272
- import { mkdir as mkdir5, mkdtemp as mkdtemp2, readFile as readFile3, rm as rm4 } from "node:fs/promises";
8451
+ import { mkdir as mkdir5, mkdtemp as mkdtemp2, readFile as readFile3, rm as rm5 } from "node:fs/promises";
8273
8452
  import { join as join11 } from "node:path";
8274
8453
  import { HostKeyChangedError, HostKeyUnknownError, RemoteExecutionEnv as RemoteExecutionEnv2, detectPlatform, scanHostKey, sshArguments, sshConnection } from "@earendil-works/pi-env";
8275
8454
  var SshFault = class extends Error {
@@ -8299,7 +8478,7 @@ async function generateKey(runtime, comment) {
8299
8478
  await run("ssh-keygen", ["-q", "-t", "ed25519", "-N", "", "-C", comment, "-f", join11(directory, "key")]);
8300
8479
  return { privateKey: await readFile3(join11(directory, "key"), "utf8"), publicKey: (await readFile3(join11(directory, "key.pub"), "utf8")).trim() };
8301
8480
  } finally {
8302
- await rm4(directory, { recursive: true, force: true });
8481
+ await rm5(directory, { recursive: true, force: true });
8303
8482
  }
8304
8483
  }
8305
8484
  function publicKeyOf(privateKey, comment) {
@@ -8390,7 +8569,7 @@ async function withFiles(access, work) {
8390
8569
  try {
8391
8570
  return await work(writeFilesSync(directory, access));
8392
8571
  } finally {
8393
- await rm4(directory, { recursive: true, force: true });
8572
+ await rm5(directory, { recursive: true, force: true });
8394
8573
  }
8395
8574
  }
8396
8575
  function writeFilesSync(directory, access) {
@@ -8490,7 +8669,7 @@ var SshMachines = class {
8490
8669
  async prepareKey() {
8491
8670
  const now = Date.now();
8492
8671
  for (const [keyId2, key2] of this.prepared) if (key2.until < now) this.prepared.delete(keyId2);
8493
- const id = randomUUID17(), key = await this.wrap(() => generateKey(this.runtime, keyComment(id)));
8672
+ const id = randomUUID18(), key = await this.wrap(() => generateKey(this.runtime, keyComment(id)));
8494
8673
  this.prepared.set(id, { id, ...key, until: now + PREPARED_MS });
8495
8674
  return { keyId: id, publicKey: key.publicKey };
8496
8675
  }
@@ -8506,7 +8685,7 @@ var SshMachines = class {
8506
8685
  if (!password && (!prepared || prepared.until < Date.now())) throw new ChatError(409, "That key is no longer kept; show a new one and add it to the machine", "ssh_key_expired");
8507
8686
  if (!input.hostKeys.keys.length) throw new ChatError(400, "The host keys are missing; scan the machine first");
8508
8687
  directoryIn("/", input.directory);
8509
- const id = prepared && !password ? prepared.id : randomUUID17(), comment = keyComment(id);
8688
+ const id = prepared && !password ? prepared.id : randomUUID18(), comment = keyComment(id);
8510
8689
  const record3 = await this.wrap(async () => {
8511
8690
  const key = prepared && !password ? prepared : await generateKey(this.runtime, comment);
8512
8691
  const access = { ...where2, runtime: this.runtime, alias: alias(id), hostKeys: input.hostKeys.keys, privateKey: key.privateKey };
@@ -8806,13 +8985,13 @@ var HistoryRepository = class {
8806
8985
  });
8807
8986
  }
8808
8987
  /** Model calls since a time, per provider and model: every assistant record with usage is one call. */
8809
- usage(since) {
8988
+ usage(since2) {
8810
8989
  const rows3 = this.db.prepare(`SELECT json_extract(model,'$.provider') AS provider,json_extract(model,'$.id') AS model,count(*) AS calls,
8811
8990
  COALESCE(SUM(json_extract(usage,'$.input_tokens')),0) AS input,COALESCE(SUM(json_extract(usage,'$.output_tokens')),0) AS output,
8812
8991
  COALESCE(SUM(json_extract(usage,'$.cache_read_tokens')),0) AS cacheRead,COALESCE(SUM(json_extract(usage,'$.cache_write_tokens')),0) AS cacheWrite,
8813
8992
  COALESCE(SUM(json_extract(usage,'$.cost.total')),0) AS cost,COALESCE(MAX(json_extract(usage,'$.cost.total')),0)>0 AS priced
8814
8993
  FROM history WHERE role='assistant' AND usage IS NOT NULL AND model IS NOT NULL AND timestamp>=?
8815
- GROUP BY 1,2 ORDER BY cost DESC,calls DESC`).all(since);
8994
+ GROUP BY 1,2 ORDER BY cost DESC,calls DESC`).all(since2);
8816
8995
  return rows3.map(({ priced, cost, ...row2 }) => ({ ...row2, cost: priced ? cost : null }));
8817
8996
  }
8818
8997
  decodeSummary(row2) {
@@ -8873,8 +9052,8 @@ var Memory = class {
8873
9052
  timeline(before, limit2) {
8874
9053
  return this.history().timeline(before, limit2);
8875
9054
  }
8876
- usage(since) {
8877
- return this.history().usage(since);
9055
+ usage(since2) {
9056
+ return this.history().usage(since2);
8878
9057
  }
8879
9058
  read(id) {
8880
9059
  const record3 = this.history().read(id);
@@ -8967,9 +9146,12 @@ var Bot = class extends Member {
8967
9146
  status() {
8968
9147
  return this.state.snapshot();
8969
9148
  }
8970
- /** 投递已保存(A41);Bot 按需激活,交给运行层按急迫程度处理。附带的不必为它启动。 */
9149
+ /** 投递已保存(A41);Bot 按需激活,交给运行层按急迫程度处理。附带的不必为它启动:只在它正跑着一轮时插进去(A77)。 */
8971
9150
  async deliver(urgency) {
8972
- if (urgency === "digest") return;
9151
+ if (urgency === "digest") {
9152
+ if (!this.stopping && this.status().status !== "stop") this.agentRuntime.deliver(urgency);
9153
+ return;
9154
+ }
8973
9155
  if (this.stopping) await this.stopping;
8974
9156
  if (this.status().status === "stop") await this.start();
8975
9157
  this.agentRuntime.deliver(urgency);
@@ -9355,7 +9537,7 @@ var ToolContext = class {
9355
9537
  };
9356
9538
 
9357
9539
  // src/server/service/bot/history-read.ts
9358
- import { randomUUID as randomUUID18 } from "node:crypto";
9540
+ import { randomUUID as randomUUID19 } from "node:crypto";
9359
9541
  import { Type as Type2 } from "typebox";
9360
9542
 
9361
9543
  // src/server/service/bot/history-format.ts
@@ -9435,7 +9617,7 @@ async function readHistory(memory, workspace, input, signal) {
9435
9617
  const rendered = formatHistory(result, format);
9436
9618
  signal?.throwIfAborted();
9437
9619
  if (!input.to_file) return rendered;
9438
- const name3 = `history-${randomUUID18()}.${format === "json" ? "json" : "md"}`;
9620
+ const name3 = `history-${randomUUID19()}.${format === "json" ? "json" : "md"}`;
9439
9621
  return workspace.writeTempFile(name3, rendered, signal);
9440
9622
  }
9441
9623
 
@@ -10573,9 +10755,11 @@ var BotInbox = class {
10573
10755
  if (notices.length) this.tell(notices);
10574
10756
  }
10575
10757
  /** Notices the Bot has not been told (A39), as the XML that goes into a steered message or a run of notices alone.
10576
- * `steer`: what may go into a running turn; a task waits for the turn to end and opens one of its own (A58). */
10577
- notices(prompt, steer = false) {
10578
- const pending = this.chats.notices(this.botId).filter((notice) => !steer || notice.kind !== "task");
10758
+ * `steer`: what may go into a running turn. A queued delivery takes everything but a task, which waits for the turn
10759
+ * to end and opens one of its own (A58); a digest (a new notice version) takes only chat activity, so the system
10760
+ * notices keep riding with the next batch (A48, A73, A77). */
10761
+ notices(prompt, steer) {
10762
+ const pending = this.chats.notices(this.botId).filter((notice) => !steer || (steer === "queue" ? notice.kind !== "task" : notice.chat !== null));
10579
10763
  if (!pending.length) return null;
10580
10764
  const { kept, stale } = this.convert(pending);
10581
10765
  if (!kept.length && !prompt) {
@@ -10664,9 +10848,14 @@ var AgentRuntime = class {
10664
10848
  }
10665
10849
  /** Something was delivered (A41); the runtime only looks at how urgent it is. Interrupt: abort the running
10666
10850
  * turn and start over with everything pending (A05). Queue: steer it into the running turn, or wake an idle
10667
- * Bot (A39). Digest: nothing now, it rides along with the next run. The delivery itself is durable. */
10851
+ * Bot (A39). Digest: chat activity into a running turn, never a wake; otherwise it rides along with the next run (A77).
10852
+ * The delivery itself is durable. */
10668
10853
  deliver(urgency) {
10669
- if (this.stopping || urgency === "digest") return;
10854
+ if (this.stopping) return;
10855
+ if (urgency === "digest") {
10856
+ if (this.prompting) this.steer("digest");
10857
+ return;
10858
+ }
10670
10859
  if (urgency === "interrupt") {
10671
10860
  this.inputVersion++;
10672
10861
  if (this.prompting) this.session?.requestAbort();
@@ -10677,12 +10866,16 @@ var AgentRuntime = class {
10677
10866
  this.wake();
10678
10867
  return;
10679
10868
  }
10869
+ this.steer("queue");
10870
+ }
10871
+ /** Puts the notices a running turn has not been told into it (A39, A77); nothing is woken. A steer that does not
10872
+ * go through leaves them for the next batch. Serialised, so two quick deliveries cannot both read the same rows. */
10873
+ steer(urgency) {
10680
10874
  this.notices = this.notices.catch(() => {
10681
10875
  }).then(async () => {
10682
- const pending = this.inbox.notices(null, true);
10683
- if (!pending || this.stopping) return;
10684
- if (this.prompting && this.session?.steer(pending.xml)) this.inbox.tell(pending.ids);
10685
- else this.wake();
10876
+ const pending = this.inbox.notices(null, urgency);
10877
+ if (!pending || this.stopping || !this.prompting) return;
10878
+ if (this.session?.steer(pending.xml)) this.inbox.tell(pending.ids);
10686
10879
  }).catch((error) => {
10687
10880
  console.error("[bot] steer failed", { botId: this.inbox.botId, error });
10688
10881
  });
@@ -11232,8 +11425,8 @@ var AgentOps = class {
11232
11425
  return this.bots.status(bot.id).status;
11233
11426
  }
11234
11427
  usage(bot, days) {
11235
- const since = Date.now() - days * 24 * 60 * 60 * 1e3;
11236
- return { since: new Date(since).toISOString(), models: this.bots.memory(bot.id).usage(since) };
11428
+ const since2 = Date.now() - days * 24 * 60 * 60 * 1e3;
11429
+ return { since: new Date(since2).toISOString(), models: this.bots.memory(bot.id).usage(since2) };
11237
11430
  }
11238
11431
  /** Deletions run as the owner, who has just confirmed them on a card. */
11239
11432
  async deleteBot(bot) {
@@ -11725,6 +11918,10 @@ var BotService = class {
11725
11918
  memory(id) {
11726
11919
  return this.get(id).memory;
11727
11920
  }
11921
+ /** Usage across every Bot since a time (A81), from each one's History. */
11922
+ usage(since2) {
11923
+ return usageOverview(since2, this.deps.store.bots.list().map((record3) => [record3.id, this.memory(record3.id).usage(since2)]));
11924
+ }
11728
11925
  /** A delivery reached a Bot (A41); the Bot responds to its urgency. */
11729
11926
  deliver(id, urgency) {
11730
11927
  if (this.closed || !this.deps.store.bots.get(id)) return;
@@ -11803,7 +12000,7 @@ var BotService = class {
11803
12000
  if (!this.deps.store.bots.delete(id)) throw new RuntimeFault("bot_missing", "Bot not found", 404);
11804
12001
  this.ports.changed();
11805
12002
  await this.deps.chats.attachments.clean();
11806
- if (!keepFiles) await rm5(dirname10(workspacePath), { recursive: true, force: true });
12003
+ if (!keepFiles) await rm6(dirname10(workspacePath), { recursive: true, force: true });
11807
12004
  }
11808
12005
  async close() {
11809
12006
  if (this.closed) return;