@goodfish/jarvy 0.11.1 → 0.12.1

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 Hono14 } from "hono";
18
- import { HTTPException as HTTPException13 } from "hono/http-exception";
17
+ import { Hono as Hono15 } from "hono";
18
+ import { HTTPException as HTTPException14 } from "hono/http-exception";
19
19
  import { serveStatic } from "@hono/node-server/serve-static";
20
20
 
21
21
  // src/server/api/middleware/security.ts
@@ -157,18 +157,19 @@ var encrypted = (c) => !!c.env.incoming.socket.encrypted;
157
157
  function sessionCookie(c, name3, value2, maxAge) {
158
158
  setCookie(c, name3, value2, { httpOnly: true, sameSite: "Strict", path: "/", maxAge, secure: encrypted(c) });
159
159
  }
160
+ var sessionCookieName = (instance, secure) => secure ? `jarvy_session_${instance}_tls` : `jarvy_session_${instance}`;
160
161
  function installAuthentication(api, account) {
161
- const BASE = `jarvy_session_${account.instance}`, cookie = (c) => encrypted(c) ? `${BASE}_tls` : BASE;
162
+ const cookie2 = (c) => sessionCookieName(account.instance, encrypted(c));
162
163
  api.post("/login", jsonBody, async (c) => {
163
164
  const input = await readJson(c);
164
165
  const result = account.login(input.username, input.password, c.env.incoming.socket.remoteAddress ?? "unknown");
165
166
  if (result.type === "limited") throw new HTTPException4(429, { message: "Too many login attempts; try again later" });
166
167
  if (result.type === "invalid") throw new HTTPException4(401, { message: "Invalid username or password" });
167
- sessionCookie(c, cookie(c), result.token, 7 * 24 * 60 * 60);
168
+ sessionCookie(c, cookie2(c), result.token, 7 * 24 * 60 * 60);
168
169
  return c.json({ username: result.username });
169
170
  });
170
171
  api.use("*", async (c, next) => {
171
- const token = getCookie(c, cookie(c));
172
+ const token = getCookie(c, cookie2(c));
172
173
  if (!account.authenticated(token)) throw new HTTPException4(401, { message: "Login required" });
173
174
  c.set("sessionToken", token);
174
175
  c.set("actorId", account.identity().id);
@@ -177,7 +178,8 @@ function installAuthentication(api, account) {
177
178
  api.get("/me", (c) => c.json({ username: account.identity().username }));
178
179
  api.post("/logout", (c) => {
179
180
  account.logout(c.get("sessionToken"));
180
- sessionCookie(c, cookie(c), "", 0);
181
+ sessionCookie(c, cookie2(c), "", 0);
182
+ c.header("Clear-Site-Data", '"cache"');
181
183
  return c.json({ ok: true });
182
184
  });
183
185
  }
@@ -211,9 +213,9 @@ var MARK = /\[\[q(\d+)\]\]/g;
211
213
  function quoteMarks(content2) {
212
214
  return [...content2.matchAll(MARK)].map((match) => Number(match[1]));
213
215
  }
214
- function clipQuote(text4) {
215
- const chars = [...text4];
216
- return chars.length > 200 ? `${chars.slice(0, 120).join("")}\u2026\u2026${chars.slice(-60).join("")}` : text4;
216
+ function clipQuote(text5) {
217
+ const chars = [...text5];
218
+ return chars.length > 200 ? `${chars.slice(0, 120).join("")}\u2026\u2026${chars.slice(-60).join("")}` : text5;
217
219
  }
218
220
  function previewQuotes(content2, quotes) {
219
221
  if (!quotes.length) return content2;
@@ -239,8 +241,8 @@ function expandQuotes(content2, quotes, name3) {
239
241
  return `${offset > 0 && whole[offset - 1] !== "\n" ? "\n" : ""}${block}${end < whole.length && whole[end] !== "\n" ? "\n" : ""}`;
240
242
  });
241
243
  }
242
- function quoteComparable(text4) {
243
- return text4.replace(MARK, "").replace(/!\[[^\]]*\]\([^)]*\)/g, "").replace(/\[([^\]]*)\]\([^)]*\)/g, "$1").replace(/<(https?:\/\/[^>\s]+)>/g, "$1").replace(/^[ \t|:*_=-]*$/gm, (line) => /[-*_=]{3}/.test(line) ? "" : line).replace(/^[ \t]*(?:>[ \t>]*|#{1,6}[ \t]+|(?:[-*+]|\d+[.)])[ \t]+|```.*|~~~.*)/gm, "").replace(/\\([\\`*_{}[\]()#+\-.!|~>])/g, "$1").replace(/[*_~`|]/g, "").replace(/\s+/g, "");
244
+ function quoteComparable(text5) {
245
+ return text5.replace(MARK, "").replace(/!\[[^\]]*\]\([^)]*\)/g, "").replace(/\[([^\]]*)\]\([^)]*\)/g, "$1").replace(/<(https?:\/\/[^>\s]+)>/g, "$1").replace(/^[ \t|:*_=-]*$/gm, (line) => /[-*_=]{3}/.test(line) ? "" : line).replace(/^[ \t]*(?:>[ \t>]*|#{1,6}[ \t]+|(?:[-*+]|\d+[.)])[ \t]+|```.*|~~~.*)/gm, "").replace(/\\([\\`*_{}[\]()#+\-.!|~>])/g, "$1").replace(/[*_~`|]/g, "").replace(/\s+/g, "");
244
246
  }
245
247
 
246
248
  // src/server/api/views/chat.ts
@@ -337,9 +339,9 @@ function cursor(raw) {
337
339
  function activityRoutes(bots) {
338
340
  const routes = new Hono2();
339
341
  routes.get("/:id/activity", (c) => {
340
- const limit = Number(c.req.query("limit") ?? 40);
341
- if (!Number.isSafeInteger(limit) || limit < 1 || limit > 100) throw new HTTPException5(400, { message: "Invalid page size" });
342
- const page2 = bots.activity(c.req.param("id"), cursor(c.req.query("before")), limit);
342
+ const limit2 = Number(c.req.query("limit") ?? 40);
343
+ if (!Number.isSafeInteger(limit2) || limit2 < 1 || limit2 > 100) throw new HTTPException5(400, { message: "Invalid page size" });
344
+ const page2 = bots.activity(c.req.param("id"), cursor(c.req.query("before")), limit2);
343
345
  return c.json({ items: page2.items, nextBefore: page2.nextBefore ? Buffer.from(JSON.stringify(page2.nextBefore)).toString("base64url") : null });
344
346
  });
345
347
  routes.get("/:id/activity/messages/:messageId", (c) => {
@@ -367,9 +369,9 @@ function historyRoutes(bots) {
367
369
  const routes = new Hono3();
368
370
  routes.get("/:id/history", (c) => {
369
371
  const memory = bots.memory(c.req.param("id"));
370
- const before = c.req.query("before"), limit = integer(c.req.query("limit") ?? "50", "page size");
371
- if (limit > 100) throw new HTTPException6(400, { message: "The page size can't be over 100" });
372
- return c.json(memory.list(before === void 0 ? void 0 : integer(before, "page position"), limit));
372
+ const before = c.req.query("before"), limit2 = integer(c.req.query("limit") ?? "50", "page size");
373
+ if (limit2 > 100) throw new HTTPException6(400, { message: "The page size can't be over 100" });
374
+ return c.json(memory.list(before === void 0 ? void 0 : integer(before, "page position"), limit2));
373
375
  });
374
376
  routes.get("/:id/history/:recordId{[0-9]+}", (c) => {
375
377
  const detail = bots.memory(c.req.param("id")).read(integer(c.req.param("recordId"), "record ID"));
@@ -404,6 +406,11 @@ function botRoutes(bots) {
404
406
  if (typeof input.machineId !== "string" || typeof input.granted !== "boolean") throw new HTTPException7(400, { message: "machineId and granted (true or false) are needed" });
405
407
  return c.json(bots.grantMachine(id, input.machineId, input.granted));
406
408
  });
409
+ routes.put("/:id/computer/limits", jsonBody, async (c) => {
410
+ const input = await readJson(c);
411
+ return c.json(await bots.setComputerLimits(c.req.param("id"), { memory: input.memory, cpus: input.cpus }));
412
+ });
413
+ routes.get("/:id/computer/resources", async (c) => c.json(await bots.computerResources(c.req.param("id"))));
407
414
  routes.post("/:id/computer/:action", async (c) => {
408
415
  const action = c.req.param("action");
409
416
  if (action !== "start" && action !== "stop" && action !== "reset") throw new HTTPException7(404, { message: "Not found" });
@@ -484,14 +491,73 @@ function byteRange(header, size) {
484
491
  var ATTACHMENT_MAX_BYTES = 4 * 1024 ** 3;
485
492
 
486
493
  // src/server/infra/files/attachments.ts
487
- import { createHash, randomUUID } from "node:crypto";
494
+ import { createHash, randomUUID as randomUUID2 } from "node:crypto";
488
495
  import { createReadStream } from "node:fs";
489
- import { mkdir, open, readdir, rename, rm, stat } from "node:fs/promises";
496
+ import { mkdir, open, readdir, rename as rename2, rm as rm2, stat as stat2 } from "node:fs/promises";
490
497
  import { dirname, join } from "node:path";
498
+
499
+ // src/server/infra/files/thumbnails.ts
500
+ import { randomUUID } from "node:crypto";
501
+ import { rename, rm, stat } from "node:fs/promises";
502
+ import sharp from "sharp";
503
+ var WIDTH = 1600;
504
+ var HEIGHT = 640;
505
+ var SMALL = 300 * 1024;
506
+ var FORMATS = /* @__PURE__ */ new Set(["jpeg", "png", "webp", "avif"]);
507
+ var AT_ONCE = 4;
508
+ var Thumbnails = class {
509
+ making = /* @__PURE__ */ new Map();
510
+ running = 0;
511
+ waiting = [];
512
+ /** The thumbnail's path for the original at `source`, kept at `target`; null to show the original. */
513
+ async thumbnail(source2, target3) {
514
+ if (await stat(target3).then(() => true, () => false)) return target3;
515
+ let pending = this.making.get(target3);
516
+ if (!pending) {
517
+ pending = this.make(source2, target3).finally(() => this.making.delete(target3));
518
+ this.making.set(target3, pending);
519
+ }
520
+ return pending;
521
+ }
522
+ async make(source2, target3) {
523
+ const meta = await sharp(source2).metadata().catch(() => null);
524
+ if (!meta?.width || !meta.height || !FORMATS.has(meta.format ?? "") || (meta.pages ?? 1) > 1) return null;
525
+ const [width, height] = (meta.orientation ?? 1) >= 5 ? [meta.height, meta.width] : [meta.width, meta.height];
526
+ if (width <= WIDTH && height <= HEIGHT && (meta.size ?? (await stat(source2)).size) <= SMALL) return null;
527
+ await this.turn();
528
+ const part = `${target3}.${randomUUID()}.part`;
529
+ try {
530
+ await sharp(source2).rotate().resize(WIDTH, HEIGHT, { fit: "inside", withoutEnlargement: true }).webp({ quality: 80 }).toFile(part);
531
+ await rename(part, target3);
532
+ return target3;
533
+ } catch (error) {
534
+ console.warn("[files] could not make a thumbnail", { source: source2, error: error instanceof Error ? error.message : String(error) });
535
+ await rm(part, { force: true });
536
+ return null;
537
+ } finally {
538
+ this.done();
539
+ }
540
+ }
541
+ async turn() {
542
+ if (this.running < AT_ONCE) {
543
+ this.running++;
544
+ return;
545
+ }
546
+ await new Promise((resolve5) => this.waiting.push(resolve5));
547
+ }
548
+ done() {
549
+ const next = this.waiting.shift();
550
+ if (next) next();
551
+ else this.running--;
552
+ }
553
+ };
554
+
555
+ // src/server/infra/files/attachments.ts
491
556
  var attachmentPath = (root, id) => join(root, id.slice(0, 2), id);
492
557
  var INCOMING = "incoming";
493
558
  var SHARD = /^[a-f\d]{2}$/;
494
559
  var ID = /^[a-f\d]{8}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{12}$/;
560
+ var THUMB = ".thumb";
495
561
  var FileTooLarge = class extends Error {
496
562
  constructor() {
497
563
  super("File is too large");
@@ -499,13 +565,14 @@ var FileTooLarge = class extends Error {
499
565
  };
500
566
  var AttachmentFiles = class {
501
567
  root;
568
+ thumbnails = new Thumbnails();
502
569
  constructor(dataDir) {
503
570
  this.root = join(dataDir, "attachments");
504
571
  }
505
572
  /** Writes `chunks` to a new file under a new id; past `limit` bytes it stops and throws FileTooLarge. Nothing is
506
573
  * left behind when it throws. */
507
- async stage(chunks, limit, signal) {
508
- const id = randomUUID(), part = this.part(id), hash = createHash("sha256");
574
+ async stage(chunks, limit2, signal) {
575
+ const id = randomUUID2(), part = this.part(id), hash = createHash("sha256");
509
576
  await mkdir(dirname(part), { recursive: true, mode: 448 });
510
577
  const handle = await open(part, "wx", 384);
511
578
  let size = 0;
@@ -513,7 +580,7 @@ var AttachmentFiles = class {
513
580
  for await (const chunk of chunks) {
514
581
  signal?.throwIfAborted();
515
582
  size += chunk.length;
516
- if (size > limit) throw new FileTooLarge();
583
+ if (size > limit2) throw new FileTooLarge();
517
584
  hash.update(chunk);
518
585
  for (let written = 0; written < chunk.length; ) written += (await handle.write(chunk, written)).bytesWritten;
519
586
  }
@@ -521,7 +588,7 @@ var AttachmentFiles = class {
521
588
  await handle.sync();
522
589
  } catch (error) {
523
590
  await handle.close().catch(() => void 0);
524
- await rm(part, { force: true });
591
+ await rm2(part, { force: true });
525
592
  throw error;
526
593
  }
527
594
  await handle.close();
@@ -531,11 +598,17 @@ var AttachmentFiles = class {
531
598
  async commit(id) {
532
599
  const target3 = this.path(id);
533
600
  await mkdir(dirname(target3), { recursive: true, mode: 448 });
534
- await rename(this.part(id), target3);
601
+ await rename2(this.part(id), target3);
535
602
  }
536
- /** Drops files, staged or in place; ids with no file are skipped. */
603
+ /** Drops files, staged or in place, with their thumbnails; ids with no file are skipped. */
537
604
  async discard(ids) {
538
- for (const id of ids) if (ID.test(id)) await Promise.all([rm(this.part(id), { force: true }), rm(this.path(id), { force: true })]);
605
+ 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 })]);
606
+ }
607
+ /** A picture's thumbnail (A80), made the first time it is asked for; null when the original is to be shown. */
608
+ async thumbnail(id) {
609
+ const path = await this.thumbnails.thumbnail(this.path(id), this.path(id) + THUMB);
610
+ if (!path) return null;
611
+ return { size: (await stat2(path)).size, stream: createReadStream(path) };
539
612
  }
540
613
  /** The file's bytes from `start` to `end` (inclusive), as a stream. */
541
614
  stream(id, range) {
@@ -543,11 +616,11 @@ var AttachmentFiles = class {
543
616
  }
544
617
  /** The whole file, for the few readers that need it in memory (a picture or text a Bot looks at); `limit` guards
545
618
  * against reading a large file by mistake. */
546
- async read(id, limit) {
619
+ async read(id, limit2) {
547
620
  const handle = await open(this.path(id), "r");
548
621
  try {
549
622
  const { size } = await handle.stat();
550
- if (size > limit) throw new FileTooLarge();
623
+ if (size > limit2) throw new FileTooLarge();
551
624
  return await handle.readFile();
552
625
  } finally {
553
626
  await handle.close();
@@ -565,31 +638,33 @@ var AttachmentFiles = class {
565
638
  }
566
639
  async size(id) {
567
640
  try {
568
- return (await stat(this.path(id))).size;
641
+ return (await stat2(this.path(id))).size;
569
642
  } catch {
570
643
  return null;
571
644
  }
572
645
  }
573
- /** Removes what no attachment owns: files left in incoming/ by a stop mid-upload, files whose attachment is gone
574
- * (`known` says which ids still have one), and anything else under the root, such as the readable copies older
575
- * versions kept in `<id>/<name>` folders. Folders left empty go too. Returns how many files went. */
646
+ /** Removes what no attachment owns: files left in incoming/ by a stop mid-upload, files and thumbnails whose
647
+ * attachment is gone (`known` says which ids still have one), and anything else under the root, such as the readable
648
+ * copies older versions kept in `<id>/<name>` folders or a thumbnail cut off midway. Folders left empty go too.
649
+ * Returns how many files went. */
576
650
  async sweep(known) {
577
651
  let removed = 0;
578
652
  for (const entry of await entries(this.root)) {
579
653
  const path = join(this.root, entry.name);
580
654
  if (!entry.isDirectory() || !(SHARD.test(entry.name) || entry.name === INCOMING)) {
581
655
  removed += await count(path);
582
- await rm(path, { force: true, recursive: true });
656
+ await rm2(path, { force: true, recursive: true });
583
657
  continue;
584
658
  }
585
659
  const files = await entries(path);
586
660
  for (const file of files) {
587
- if (entry.name === INCOMING || !ID.test(file.name) || !file.name.startsWith(entry.name) || !known(file.name)) {
661
+ const id = file.name.endsWith(THUMB) ? file.name.slice(0, -THUMB.length) : file.name;
662
+ if (entry.name === INCOMING || !ID.test(id) || !id.startsWith(entry.name) || !known(id)) {
588
663
  removed += await count(join(path, file.name));
589
- await rm(join(path, file.name), { force: true, recursive: true });
664
+ await rm2(join(path, file.name), { force: true, recursive: true });
590
665
  }
591
666
  }
592
- if (entry.name !== INCOMING && !(await entries(path)).length) await rm(path, { force: true, recursive: true });
667
+ if (entry.name !== INCOMING && !(await entries(path)).length) await rm2(path, { force: true, recursive: true });
593
668
  }
594
669
  return removed;
595
670
  }
@@ -603,7 +678,7 @@ var AttachmentFiles = class {
603
678
  };
604
679
  async function count(path) {
605
680
  const inside2 = await entries(path);
606
- if (!inside2.length) return (await stat(path).catch(() => null))?.isFile() ? 1 : 0;
681
+ if (!inside2.length) return (await stat2(path).catch(() => null))?.isFile() ? 1 : 0;
607
682
  let total = 0;
608
683
  for (const entry of inside2) total += entry.isDirectory() ? await count(join(path, entry.name)) : 1;
609
684
  return total;
@@ -662,13 +737,17 @@ var ChatAttachments = class {
662
737
  stream(file, range) {
663
738
  return this.files.stream(file.id, range);
664
739
  }
740
+ /** A picture's thumbnail (A80), made on first use; null when its original is to be shown. */
741
+ thumbnail(file) {
742
+ return this.files.thumbnail(file.id);
743
+ }
665
744
  /** The first bytes of a file, to tell its kind. */
666
745
  head(file, bytes) {
667
746
  return this.files.head(file.id, bytes);
668
747
  }
669
748
  /** A small file whole (a picture or text a Bot looks at). */
670
- read(file, limit) {
671
- return this.files.read(file.id, limit);
749
+ read(file, limit2) {
750
+ return this.files.read(file.id, limit2);
672
751
  }
673
752
  async removePending(actorId, id) {
674
753
  const removed = this.repository.removePendingAttachment(actorId, id);
@@ -712,9 +791,9 @@ function chatRoutes({ chats, bots }) {
712
791
  return c.json({ messages: page2.messages.map(messageView), hasOlder: page2.hasOlder });
713
792
  }
714
793
  const after2 = Number(c.req.query("after") ?? 0);
715
- const limit = Number(c.req.query("limit") ?? 1e3);
716
- const messages = chats.read(c.get("actorId"), c.req.param("id"), after2, limit);
717
- return c.json({ messages: messages.map(messageView), nextSequence: messages.at(-1)?.sequence ?? after2, hasMore: messages.length === limit });
794
+ const limit2 = Number(c.req.query("limit") ?? 1e3);
795
+ const messages = chats.read(c.get("actorId"), c.req.param("id"), after2, limit2);
796
+ return c.json({ messages: messages.map(messageView), nextSequence: messages.at(-1)?.sequence ?? after2, hasMore: messages.length === limit2 });
718
797
  });
719
798
  routes.post("/:id/messages", jsonBody, async (c) => {
720
799
  const message = await chats.send(c.get("actorId"), c.req.param("id"), await readJson(c));
@@ -746,6 +825,8 @@ function chatRoutes({ chats, bots }) {
746
825
  });
747
826
  return routes;
748
827
  }
828
+ var ATTACHMENT_CACHE = "private, max-age=31536000, immutable";
829
+ var disposition = (name3) => `attachment; filename*=UTF-8''${encodeURIComponent(name3)}`;
749
830
  function attachmentRoutes(chats) {
750
831
  const routes = new Hono5();
751
832
  routes.get("/:id", (c) => {
@@ -755,7 +836,9 @@ function attachmentRoutes(chats) {
755
836
  const headers2 = {
756
837
  "Content-Type": downloadType(file.mimeType),
757
838
  "Accept-Ranges": "bytes",
758
- "Content-Disposition": `attachment; filename*=UTF-8''${encodeURIComponent(file.name)}`
839
+ "Cache-Control": ATTACHMENT_CACHE,
840
+ "Vary": "Cookie",
841
+ "Content-Disposition": disposition(file.name)
759
842
  };
760
843
  if (range === "unsatisfiable") return c.body(null, 416, { ...headers2, "Content-Range": `bytes */${size}` });
761
844
  if (!range) return c.body(web(chats.attachments.stream(file)), 200, { ...headers2, "Content-Length": String(size) });
@@ -765,6 +848,14 @@ function attachmentRoutes(chats) {
765
848
  { ...headers2, "Content-Length": String(range.end - range.start + 1), "Content-Range": `bytes ${range.start}-${range.end}/${size}` }
766
849
  );
767
850
  });
851
+ routes.get("/:id/thumb", async (c) => {
852
+ const file = chats.attachments.find(c.get("actorId"), c.req.param("id"));
853
+ if (!file) throw new HTTPException8(404, { message: "Attachment not found" });
854
+ const thumbnail = await chats.attachments.thumbnail(file);
855
+ const kept = { "Cache-Control": ATTACHMENT_CACHE, "Vary": "Cookie", "Content-Disposition": disposition(file.name) };
856
+ if (thumbnail) return c.body(web(thumbnail.stream), 200, { ...kept, "Content-Type": "image/webp", "Content-Length": String(thumbnail.size) });
857
+ return c.body(web(chats.attachments.stream(file)), 200, { ...kept, "Content-Type": downloadType(file.mimeType), "Content-Length": String(file.size) });
858
+ });
768
859
  routes.get("/:id/run", (c) => {
769
860
  const file = chats.attachments.find(c.get("actorId"), c.req.param("id"));
770
861
  if (!file) throw new HTTPException8(404, { message: "Attachment not found" });
@@ -985,8 +1076,8 @@ function channelRoutes({ chats }) {
985
1076
  };
986
1077
  routes.get("/members", (c) => c.json({ members: channels.everyone(c.get("actorId")) }));
987
1078
  routes.get("/search", (c) => {
988
- const within = c.req.query("in") || void 0, limit = Number(c.req.query("limit") ?? 50), view = (found2) => chatView(found2, (member) => chats.member(member));
989
- const found = chats.find(c.get("actorId"), c.req.query("q") ?? "", { within, limit });
1079
+ const within = c.req.query("in") || void 0, limit2 = Number(c.req.query("limit") ?? 50), view = (found2) => chatView(found2, (member) => chats.member(member));
1080
+ const found = chats.find(c.get("actorId"), c.req.query("q") ?? "", { within, limit: limit2 });
990
1081
  const result = { chats: found.chats.map(view), messages: found.messages.map(({ message, chat: place }) => ({ message: messageView(message), chat: view(place) })) };
991
1082
  return c.json(result);
992
1083
  });
@@ -1088,6 +1179,7 @@ function connectionRoutes(connection) {
1088
1179
  routes.get("/connection", (c) => c.json(connection.info()));
1089
1180
  routes.put("/connection", jsonBody, async (c) => {
1090
1181
  const input = await readJson(c);
1182
+ if (input.ports !== void 0) return c.json(connection.setPortRange(input.ports));
1091
1183
  if (typeof input.allowHttp !== "boolean") throw new HTTPException10(400, { message: "allowHttp must be true or false" });
1092
1184
  return c.json(connection.setAllowHttp(input.allowHttp));
1093
1185
  });
@@ -1210,16 +1302,36 @@ function taskRoutes(services) {
1210
1302
  return routes;
1211
1303
  }
1212
1304
 
1305
+ // src/server/api/routes/ports.ts
1306
+ import { Hono as Hono14 } from "hono";
1307
+ import { HTTPException as HTTPException13 } from "hono/http-exception";
1308
+ function portRoutes(services) {
1309
+ const routes = new Hono14();
1310
+ const bot = (id) => {
1311
+ services.bots.describe(id);
1312
+ return id;
1313
+ };
1314
+ routes.get("/bots/:id/ports", async (c) => c.json({ ports: await services.bots.ports.list(bot(c.req.param("id"))) }));
1315
+ routes.delete("/bots/:id/ports/:port", (c) => {
1316
+ const port = Number(c.req.param("port"));
1317
+ if (!Number.isInteger(port)) throw new HTTPException13(400, { message: "port must be a number" });
1318
+ services.bots.ports.unpublish(bot(c.req.param("id")), port);
1319
+ return c.json({ ok: true });
1320
+ });
1321
+ return routes;
1322
+ }
1323
+
1213
1324
  // src/server/api/app.ts
1214
1325
  function createHttpApp(services, webRoot) {
1215
- const app = new Hono14();
1326
+ const app = new Hono15();
1216
1327
  app.use("*", headers);
1217
1328
  app.onError(handleError);
1218
- const api = new Hono14();
1329
+ const api = new Hono15();
1219
1330
  api.use("*", apiSecurity);
1220
1331
  api.route("/", rootCertificateRoute(services.connection));
1221
1332
  installAuthentication(api, services.account);
1222
1333
  api.route("/", taskRoutes(services));
1334
+ api.route("/", portRoutes(services));
1223
1335
  api.route("/bots", botRoutes(services.bots));
1224
1336
  api.route("/", channelRoutes(services));
1225
1337
  api.route("/chats", chatRoutes(services));
@@ -1231,7 +1343,7 @@ function createHttpApp(services, webRoot) {
1231
1343
  api.route("/secrets", secretRoutes(services.secrets));
1232
1344
  api.route("/environments", environmentRoutes(services.bots));
1233
1345
  api.all("*", () => {
1234
- throw new HTTPException13(404, { message: "API route not found" });
1346
+ throw new HTTPException14(404, { message: "API route not found" });
1235
1347
  });
1236
1348
  app.route("/api", api);
1237
1349
  app.get("*", serveStatic({ root: webRoot, onFound: (path, c) => {
@@ -1241,8 +1353,8 @@ function createHttpApp(services, webRoot) {
1241
1353
  c.header("Cache-Control", "no-store");
1242
1354
  } }));
1243
1355
  app.all("*", (c) => {
1244
- if (c.req.method !== "GET" && c.req.method !== "HEAD") throw new HTTPException13(405, { message: "Method not allowed" });
1245
- throw new HTTPException13(404, { message: "Web assets missing; run npm run build" });
1356
+ if (c.req.method !== "GET" && c.req.method !== "HEAD") throw new HTTPException14(405, { message: "Method not allowed" });
1357
+ throw new HTTPException14(404, { message: "Web assets missing; run npm run build" });
1246
1358
  });
1247
1359
  return app;
1248
1360
  }
@@ -1255,32 +1367,39 @@ function fromThisMachine(address2 = "") {
1255
1367
  if (Date.now() - own.at > 6e4) own = { at: Date.now(), addresses: new Set(Object.values(networkInterfaces()).flatMap((list) => (list ?? []).map((item) => item.address))) };
1256
1368
  return own.addresses.has(plain2);
1257
1369
  }
1258
- function createJarvyServer(services, webRoot, tls) {
1259
- const app = createHttpApp(services, webRoot);
1260
- const handle = getRequestListener((request, bindings) => app.fetch(request, bindings), { overrideGlobalObjects: false });
1370
+ function dualProtocol(tls, serve, upgrade) {
1261
1371
  const options = { requestTimeout: 0 };
1262
- const plain2 = createServer(options, (request, response) => {
1263
- if (tls && !fromThisMachine(request.socket.remoteAddress) && !services.connection.allowHttp()) {
1264
- response.writeHead(308, { Location: `https://${request.headers.host ?? ""}${request.url ?? "/"}`, "Cache-Control": "no-store" }).end();
1265
- return;
1266
- }
1267
- void handle(request, response);
1268
- });
1269
- if (!tls) return {
1270
- listen: (port, host) => new Promise((resolve5, reject) => {
1271
- plain2.once("error", reject);
1272
- plain2.listen(port, host, () => {
1273
- plain2.off("error", reject);
1274
- resolve5();
1275
- });
1276
- }),
1277
- close: () => {
1278
- plain2.close();
1279
- plain2.closeAllConnections();
1280
- }
1372
+ const plain2 = createServer(options, serve);
1373
+ if (upgrade) plain2.on("upgrade", upgrade);
1374
+ const sockets = /* @__PURE__ */ new Set();
1375
+ const track = (socket) => {
1376
+ sockets.add(socket);
1377
+ socket.once("close", () => sockets.delete(socket));
1378
+ };
1379
+ const drop = () => {
1380
+ for (const socket of sockets) socket.destroy();
1381
+ sockets.clear();
1281
1382
  };
1282
- const secure = createSecureServer({ ...options, key: tls.key, cert: tls.cert }, (request, response) => void handle(request, response));
1383
+ if (!tls) {
1384
+ plain2.on("connection", track);
1385
+ return {
1386
+ listen: (port, host) => new Promise((resolve5, reject) => {
1387
+ plain2.once("error", reject);
1388
+ plain2.listen(port, host, () => {
1389
+ plain2.off("error", reject);
1390
+ resolve5();
1391
+ });
1392
+ }),
1393
+ close: () => {
1394
+ plain2.close();
1395
+ drop();
1396
+ }
1397
+ };
1398
+ }
1399
+ const secure = createSecureServer({ ...options, key: tls.key, cert: tls.cert }, serve);
1400
+ if (upgrade) secure.on("upgrade", upgrade);
1283
1401
  const front = createListener({ pauseOnConnect: true }, (socket) => {
1402
+ track(socket);
1284
1403
  socket.on("error", () => socket.destroy());
1285
1404
  socket.setTimeout(6e4, () => socket.destroy());
1286
1405
  socket.once("readable", () => {
@@ -1304,11 +1423,186 @@ function createJarvyServer(services, webRoot, tls) {
1304
1423
  }),
1305
1424
  close: () => {
1306
1425
  front.close();
1307
- plain2.closeAllConnections();
1308
- secure.closeAllConnections();
1426
+ drop();
1309
1427
  }
1310
1428
  };
1311
1429
  }
1430
+ var encrypted2 = (request) => !!request.socket.encrypted;
1431
+ function redirectsToHttps(request, tls, allowHttp) {
1432
+ return !!tls && !encrypted2(request) && !fromThisMachine(request.socket.remoteAddress) && !allowHttp();
1433
+ }
1434
+ function redirectToHttps(request, response) {
1435
+ response.writeHead(308, { Location: `https://${request.headers.host ?? ""}${request.url ?? "/"}`, "Cache-Control": "no-store" }).end();
1436
+ }
1437
+ function createJarvyServer(services, webRoot, tls) {
1438
+ const app = createHttpApp(services, webRoot);
1439
+ const handle = getRequestListener((request, bindings) => app.fetch(request, bindings), { overrideGlobalObjects: false });
1440
+ return dualProtocol(tls, (request, response) => {
1441
+ if (redirectsToHttps(request, tls, () => services.connection.allowHttp())) {
1442
+ redirectToHttps(request, response);
1443
+ return;
1444
+ }
1445
+ void handle(request, response);
1446
+ });
1447
+ }
1448
+
1449
+ // src/server/api/views/port-page.ts
1450
+ var WORDS = {
1451
+ zh: {
1452
+ signin: ["\u9700\u8981\u767B\u5F55 Jarvy", "\u6B64\u7F51\u9875\u7531 Jarvy \u4ECE Bot \u6C99\u7BB1\u8F6C\u53D1\u3002\u8BF7\u5148\u5728\u6B64\u6D4F\u89C8\u5668\u4E2D\u767B\u5F55 Jarvy\uFF0C\u7136\u540E\u5237\u65B0\u672C\u9875\u3002"],
1453
+ stopped: ["Bot \u7684\u6C99\u7BB1\u6CA1\u6709\u8FD0\u884C", "\u6B64\u7F51\u9875\u6240\u5728\u7684 Bot \u6C99\u7BB1\u5DF2\u505C\u6B62\u3002\u8BF7\u5728 Jarvy \u7684 Bot \u4FE1\u606F\u4E2D\u542F\u52A8\u6C99\u7BB1\uFF0C\u7136\u540E\u5237\u65B0\u672C\u9875\u3002"],
1454
+ unreachable: ["\u7F51\u9875\u670D\u52A1\u6CA1\u6709\u54CD\u5E94", "Jarvy \u6B63\u5728\u8F6C\u53D1\u6B64\u7AEF\u53E3\uFF0C\u4F46 Bot \u6C99\u7BB1\u5185\u6CA1\u6709\u670D\u52A1\u76D1\u542C\u8BE5\u7AEF\u53E3\uFF0C\u6216\u670D\u52A1\u4EC5\u76D1\u542C 127.0.0.1\u3002"],
1455
+ missing: ["\u8FD9\u4E2A\u94FE\u63A5\u5DF2\u5931\u6548", "Jarvy \u5DF2\u505C\u6B62\u8F6C\u53D1\u6B64\u7AEF\u53E3\u3002"],
1456
+ open: "\u6253\u5F00 Jarvy"
1457
+ },
1458
+ en: {
1459
+ signin: ["Sign in to Jarvy", "Jarvy forwards this page from a Bot's sandbox. Sign in to Jarvy in this browser, then reload this page."],
1460
+ stopped: ["The Bot's sandbox isn't running", "The sandbox this page comes from has stopped. Start it in the Bot's info in Jarvy, then reload."],
1461
+ unreachable: ["The web service isn't answering", "Jarvy forwards this port, but no service in the Bot's sandbox listens on it, or the service listens on 127.0.0.1 only."],
1462
+ missing: ["This link no longer works", "Jarvy no longer forwards this port."],
1463
+ open: "Open Jarvy"
1464
+ }
1465
+ };
1466
+ var escape = (text5) => text5.replace(/[&<>"']/g, (ch) => `&#${ch.charCodeAt(0)};`);
1467
+ function portPage(kind, acceptLanguage, jarvyUrl) {
1468
+ const languages = (acceptLanguage ?? "").toLowerCase().split(",").map((part) => part.trim().slice(0, 2));
1469
+ const zh = languages.indexOf("zh"), en = languages.indexOf("en");
1470
+ const lang = zh !== -1 && (en === -1 || zh < en) ? "zh" : "en", words = WORDS[lang], [title, text5] = words[kind];
1471
+ const link = jarvyUrl ? `<a href="${escape(jarvyUrl)}">${escape(words.open)}</a>` : "";
1472
+ return `<!doctype html><html lang="${lang === "zh" ? "zh-CN" : "en"}"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
1473
+ <title>${escape(title)}</title><style>
1474
+ :root{color-scheme:light dark;--fg:#1f2328;--muted:#59636e;--bg:#f6f8fa;--card:#fff;--line:#d1d9e0;--accent:#0969da}
1475
+ @media (prefers-color-scheme:dark){:root{--fg:#e6edf3;--muted:#9198a1;--bg:#0d1117;--card:#151b23;--line:#3d444d;--accent:#4493f8}}
1476
+ body{margin:0;min-height:100vh;display:grid;place-items:center;background:var(--bg);color:var(--fg);font:15px/1.6 system-ui,-apple-system,"Segoe UI","Noto Sans SC","PingFang SC",sans-serif}
1477
+ main{max-width:440px;margin:24px;padding:28px 28px 24px;background:var(--card);border:1px solid var(--line);border-radius:12px}
1478
+ h1{margin:0 0 8px;font-size:18px}p{margin:0 0 20px;color:var(--muted)}a{color:var(--accent);font-weight:600;text-decoration:none}
1479
+ </style></head><body><main><h1>${escape(title)}</h1><p>${escape(text5)}</p>${link}</main></body></html>`;
1480
+ }
1481
+
1482
+ // src/server/api/published.ts
1483
+ var STATUS = { signin: 401, stopped: 503, unreachable: 502, missing: 404 };
1484
+ function cookie(request, name3) {
1485
+ for (const part of (request.headers.cookie ?? "").split(";")) {
1486
+ const [key, ...value2] = part.trim().split("=");
1487
+ if (key === name3) return value2.join("=");
1488
+ }
1489
+ return void 0;
1490
+ }
1491
+ var PublishedListeners = class {
1492
+ constructor(services, tls, host, jarvyPort) {
1493
+ this.services = services;
1494
+ this.tls = tls;
1495
+ this.host = host;
1496
+ this.jarvyPort = jarvyPort;
1497
+ }
1498
+ open = /* @__PURE__ */ new Map();
1499
+ queue = Promise.resolve();
1500
+ unsubscribe;
1501
+ closed = false;
1502
+ start() {
1503
+ this.unsubscribe = this.services.bots.ports.onChange(() => this.sync());
1504
+ this.sync();
1505
+ }
1506
+ close() {
1507
+ this.closed = true;
1508
+ this.unsubscribe?.();
1509
+ for (const server of this.open.values()) server.close();
1510
+ this.open.clear();
1511
+ }
1512
+ sync() {
1513
+ this.queue = this.queue.then(() => this.follow()).catch((error) => console.error("[ports] listening failed", { error: error.message }));
1514
+ }
1515
+ async follow() {
1516
+ if (this.closed) return;
1517
+ const ports = this.services.bots.ports, records = ports.all(), wanted = new Set(records.map((record3) => record3.hostPort));
1518
+ for (const [hostPort, server] of this.open) if (!wanted.has(hostPort)) {
1519
+ server.close();
1520
+ this.open.delete(hostPort);
1521
+ }
1522
+ for (const record3 of records) {
1523
+ if (this.open.has(record3.hostPort)) continue;
1524
+ const hostPort = record3.hostPort;
1525
+ const server = dualProtocol(
1526
+ this.tls,
1527
+ (request, response) => void this.serve(hostPort, request, response).catch(() => response.destroy()),
1528
+ (request, socket, head) => void this.upgrade(hostPort, request, socket, head).catch(() => socket.destroy())
1529
+ );
1530
+ try {
1531
+ await server.listen(hostPort, this.host);
1532
+ this.open.set(hostPort, server);
1533
+ } catch (error) {
1534
+ if (error.code !== "EADDRINUSE") {
1535
+ console.error(`[ports] can't listen on ${hostPort}`, { error: error.message });
1536
+ continue;
1537
+ }
1538
+ console.warn(`[ports] ${hostPort} is taken by another program; moving ${record3.name} to a free port`);
1539
+ await ports.relocate(record3).catch((reason) => console.error("[ports] no free port", { error: reason.message }));
1540
+ }
1541
+ }
1542
+ }
1543
+ /** Who is asking: the signed-in owner, one of this Jarvy's Bots, or no one (or someone to send to HTTPS first). */
1544
+ async caller(request) {
1545
+ const { account, bots, connection } = this.services;
1546
+ if (account.authenticated(cookie(request, sessionCookieName(account.instance, encrypted2(request))))) return { owner: true };
1547
+ const bot = await bots.ports.caller(request.socket.remoteAddress ?? "");
1548
+ if (bot) return { bot };
1549
+ return redirectsToHttps(request, this.tls, () => connection.allowHttp()) ? "redirect" : null;
1550
+ }
1551
+ async serve(hostPort, request, response) {
1552
+ const caller = await this.caller(request);
1553
+ if (caller === "redirect") {
1554
+ redirectToHttps(request, response);
1555
+ return;
1556
+ }
1557
+ if (!caller) {
1558
+ this.page(request, response, "signin");
1559
+ return;
1560
+ }
1561
+ const target3 = await this.services.bots.ports.target(hostPort);
1562
+ if (target3.kind !== "open") {
1563
+ this.page(request, response, target3.kind);
1564
+ return;
1565
+ }
1566
+ this.services.bots.ports.forward(request, response, target3, this.client(request), () => this.page(request, response, "unreachable"));
1567
+ }
1568
+ async upgrade(hostPort, request, socket, head) {
1569
+ const refuse = (status) => socket.end(`HTTP/1.1 ${status}\r
1570
+ Connection: close\r
1571
+ Content-Length: 0\r
1572
+ \r
1573
+ `);
1574
+ const caller = await this.caller(request);
1575
+ if (!caller || caller === "redirect") {
1576
+ refuse("401 Unauthorized");
1577
+ return;
1578
+ }
1579
+ const target3 = await this.services.bots.ports.target(hostPort);
1580
+ if (target3.kind !== "open") {
1581
+ refuse(target3.kind === "missing" ? "404 Not Found" : "503 Service Unavailable");
1582
+ return;
1583
+ }
1584
+ this.services.bots.ports.forwardUpgrade(request, socket, head, target3, this.client(request), () => refuse("502 Bad Gateway"));
1585
+ }
1586
+ client(request) {
1587
+ return { address: request.socket.remoteAddress ?? "", secure: encrypted2(request) };
1588
+ }
1589
+ page(request, response, kind) {
1590
+ if (response.headersSent) {
1591
+ response.destroy();
1592
+ return;
1593
+ }
1594
+ const host = (request.headers.host ?? "").replace(/:\d+$/, "");
1595
+ const jarvyUrl = host ? `${encrypted2(request) ? "https" : "http"}://${host}:${this.jarvyPort}/` : null;
1596
+ response.writeHead(STATUS[kind], {
1597
+ "Content-Type": "text/html; charset=utf-8",
1598
+ "Cache-Control": "no-store",
1599
+ "X-Content-Type-Options": "nosniff",
1600
+ "Content-Security-Policy": "default-src 'none'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'",
1601
+ "Referrer-Policy": "no-referrer"
1602
+ });
1603
+ response.end(request.method === "HEAD" ? void 0 : portPage(kind, request.headers["accept-language"], jarvyUrl));
1604
+ }
1605
+ };
1312
1606
 
1313
1607
  // src/server/infra/storage/store.ts
1314
1608
  import { chmodSync as chmodSync2, mkdirSync as mkdirSync4 } from "node:fs";
@@ -1439,14 +1733,14 @@ function migrateChannels(db) {
1439
1733
  INSERT INTO chat_states(chat_id, member_id, read_sequence)
1440
1734
  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;
1441
1735
  `);
1442
- const taken = new Set(db.prepare("SELECT username FROM account").all().map((row) => row.username.toLowerCase()));
1443
- const rename4 = db.prepare("UPDATE bots SET name=? WHERE id=?");
1736
+ const taken = new Set(db.prepare("SELECT username FROM account").all().map((row2) => row2.username.toLowerCase()));
1737
+ const rename5 = db.prepare("UPDATE bots SET name=? WHERE id=?");
1444
1738
  for (const bot of db.prepare("SELECT id,name FROM bots ORDER BY created_at,id").all()) {
1445
1739
  const base = bot.name.replace(/@/g, "").trim() || "Bot";
1446
1740
  let name3 = base;
1447
1741
  for (let suffix = 2; taken.has(name3.toLowerCase()); suffix++) name3 = `${base} ${suffix}`;
1448
1742
  taken.add(name3.toLowerCase());
1449
- if (name3 !== bot.name) rename4.run(name3, bot.id);
1743
+ if (name3 !== bot.name) rename5.run(name3, bot.id);
1450
1744
  }
1451
1745
  db.exec("PRAGMA user_version=12; COMMIT;");
1452
1746
  } catch (error) {
@@ -1674,8 +1968,8 @@ var Cipher = class _Cipher {
1674
1968
  location() {
1675
1969
  return this.master.location;
1676
1970
  }
1677
- static sealed(text4) {
1678
- return text4.startsWith(PREFIX);
1971
+ static sealed(text5) {
1972
+ return text5.startsWith(PREFIX);
1679
1973
  }
1680
1974
  seal(plain2, context2) {
1681
1975
  const iv = randomBytes(12), cipher = createCipheriv("aes-256-gcm", this.master.key, iv);
@@ -1726,7 +2020,7 @@ function migrateSecrets(db, cipher) {
1726
2020
  `);
1727
2021
  const rows3 = db.prepare("SELECT connection_id AS id, credential_json AS json FROM provider_credentials").all();
1728
2022
  const update = db.prepare("UPDATE provider_credentials SET credential_json=? WHERE connection_id=?");
1729
- for (const row of rows3) if (!Cipher.sealed(row.json)) update.run(cipher.seal(row.json, `credential:${row.id}`), row.id);
2023
+ for (const row2 of rows3) if (!Cipher.sealed(row2.json)) update.run(cipher.seal(row2.json, `credential:${row2.id}`), row2.id);
1730
2024
  db.exec("PRAGMA user_version=21");
1731
2025
  db.exec("COMMIT");
1732
2026
  } catch (error) {
@@ -1909,7 +2203,7 @@ function migrateAttachmentFiles(db, dataDir) {
1909
2203
  if (userVersion(db) >= 28) return;
1910
2204
  const root = join2(dataDir, "attachments");
1911
2205
  const rows3 = db.prepare("SELECT id,size,data FROM attachments").iterate();
1912
- for (const row of rows3) writeOut(attachmentPath(root, row.id), row.data);
2206
+ for (const row2 of rows3) writeOut(attachmentPath(root, row2.id), row2.data);
1913
2207
  migration(db, 28, () => {
1914
2208
  for (const { id, size } of db.prepare("SELECT id,size FROM attachments").all()) {
1915
2209
  if (sizeOf(attachmentPath(root, id)) !== size) throw new Error(`Attachment ${id} was not written out`);
@@ -1941,7 +2235,7 @@ function sizeOf(path) {
1941
2235
  }
1942
2236
 
1943
2237
  // src/server/infra/storage/migrations/own-computer-schema.ts
1944
- import { createHash as createHash2, randomUUID as randomUUID2 } from "node:crypto";
2238
+ import { createHash as createHash2, randomUUID as randomUUID3 } from "node:crypto";
1945
2239
  function migrateOwnComputers(db, dataDir) {
1946
2240
  const tag = createHash2("sha256").update(dataDir).digest("hex").slice(0, 6);
1947
2241
  migration(db, 29, () => {
@@ -1952,11 +2246,11 @@ function migrateOwnComputers(db, dataDir) {
1952
2246
  for (const bot of bots) {
1953
2247
  if (bot.homeKind === "computer") continue;
1954
2248
  const existing = db.prepare("SELECT id FROM environments WHERE bot_id=? AND kind='computer' AND deleted_at IS NULL").get(bot.id);
1955
- const id = existing?.id ?? randomUUID2();
2249
+ const id = existing?.id ?? randomUUID3();
1956
2250
  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);
1957
2251
  db.prepare("UPDATE bots SET home_environment_id=?, current_environment_id=? WHERE id=?").run(id, id, bot.id);
1958
2252
  }
1959
- const hosts = db.prepare("SELECT id FROM environments WHERE kind='host' AND deleted_at IS NULL").all().map((row) => row.id);
2253
+ const hosts = db.prepare("SELECT id FROM environments WHERE kind='host' AND deleted_at IS NULL").all().map((row2) => row2.id);
1960
2254
  for (const host of hosts) {
1961
2255
  db.prepare("UPDATE bots SET current_environment_id=home_environment_id WHERE current_environment_id=?").run(host);
1962
2256
  db.prepare("DELETE FROM environment_grants WHERE environment_id=?").run(host);
@@ -1965,11 +2259,23 @@ function migrateOwnComputers(db, dataDir) {
1965
2259
  });
1966
2260
  }
1967
2261
 
2262
+ // src/server/infra/storage/migrations/published-port-schema.ts
2263
+ function migratePublishedPorts(db) {
2264
+ migration(db, 30, () => {
2265
+ db.exec(`
2266
+ CREATE TABLE published_ports(bot_id TEXT NOT NULL REFERENCES bots(id) ON DELETE CASCADE,
2267
+ port INTEGER NOT NULL CHECK(port BETWEEN 1 AND 65535), name TEXT NOT NULL, description TEXT NOT NULL,
2268
+ host_port INTEGER NOT NULL UNIQUE CHECK(host_port BETWEEN 1 AND 65535),
2269
+ created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, PRIMARY KEY(bot_id, port));
2270
+ `);
2271
+ });
2272
+ }
2273
+
1968
2274
  // src/server/infra/storage/migrations/schema.ts
1969
- import { randomUUID as randomUUID4 } from "node:crypto";
2275
+ import { randomUUID as randomUUID5 } from "node:crypto";
1970
2276
 
1971
2277
  // src/server/infra/storage/migrations/prototype-schema.ts
1972
- import { randomUUID as randomUUID3 } from "node:crypto";
2278
+ import { randomUUID as randomUUID4 } from "node:crypto";
1973
2279
  function migratePrototype(db) {
1974
2280
  const version = db.prepare("PRAGMA user_version").get().user_version;
1975
2281
  if (version >= 4) return;
@@ -2035,10 +2341,10 @@ function migratePrototype(db) {
2035
2341
  const legacy = db.prepare("SELECT provider,base_url AS baseUrl,model,api_key AS apiKey FROM model_credentials WHERE id=1").get();
2036
2342
  const botModels = db.prepare("SELECT DISTINCT model_id AS modelId FROM bots WHERE model_id<>''").all();
2037
2343
  if (legacy && (legacy.baseUrl || legacy.model || legacy.apiKey || botModels.length)) {
2038
- const id = randomUUID3(), now = Date.now();
2344
+ const id = randomUUID4(), now = Date.now();
2039
2345
  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);
2040
2346
  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);
2041
- const modelIds = new Set([legacy.model, ...botModels.map((row) => row.modelId)].filter(Boolean));
2347
+ const modelIds = new Set([legacy.model, ...botModels.map((row2) => row2.modelId)].filter(Boolean));
2042
2348
  for (const modelId of modelIds) db.prepare("INSERT INTO connection_models(connection_id,model_id,config_json,created_at,updated_at) VALUES(?,?,?,?,?)").run(id, modelId, JSON.stringify({ id: modelId, name: modelId }), now, now);
2043
2349
  db.prepare("UPDATE bots SET connection_id=?,model_id=CASE WHEN model_id='' THEN ? ELSE model_id END WHERE model_id<>'' OR ?<>''").run(id, legacy.model, legacy.model);
2044
2350
  }
@@ -2087,7 +2393,7 @@ function migrateRuntime(db) {
2087
2393
  // src/server/infra/storage/migrations/schema.ts
2088
2394
  function migrateDatabase(db, cipher, dataDir) {
2089
2395
  const version = db.prepare("PRAGMA user_version").get().user_version;
2090
- if (version > 29) throw new Error(`Unsupported database version: ${version}`);
2396
+ if (version > 30) throw new Error(`Unsupported database version: ${version}`);
2091
2397
  migratePrototype(db);
2092
2398
  if (version >= 5) {
2093
2399
  migrateRuntime(db);
@@ -2114,11 +2420,12 @@ function migrateDatabase(db, cipher, dataDir) {
2114
2420
  migrateMachineDescriptions(db);
2115
2421
  migrateAttachmentFiles(db, dataDir);
2116
2422
  migrateOwnComputers(db, dataDir);
2423
+ migratePublishedPorts(db);
2117
2424
  return;
2118
2425
  }
2119
2426
  db.exec("PRAGMA foreign_keys=OFF; BEGIN IMMEDIATE");
2120
2427
  try {
2121
- const userId = randomUUID4();
2428
+ const userId = randomUUID5();
2122
2429
  db.exec(`
2123
2430
  CREATE TABLE members(id TEXT PRIMARY KEY, kind TEXT NOT NULL CHECK(kind IN ('user','bot')));
2124
2431
  ALTER TABLE account ADD COLUMN member_id TEXT REFERENCES members(id);
@@ -2198,10 +2505,11 @@ function migrateDatabase(db, cipher, dataDir) {
2198
2505
  migrateMachineDescriptions(db);
2199
2506
  migrateAttachmentFiles(db, dataDir);
2200
2507
  migrateOwnComputers(db, dataDir);
2508
+ migratePublishedPorts(db);
2201
2509
  }
2202
2510
 
2203
2511
  // src/server/infra/storage/account-repository.ts
2204
- import { createHash as createHash3, randomBytes as randomBytes2, randomUUID as randomUUID5, scryptSync, timingSafeEqual } from "node:crypto";
2512
+ import { createHash as createHash3, randomBytes as randomBytes2, randomUUID as randomUUID6, scryptSync, timingSafeEqual } from "node:crypto";
2205
2513
 
2206
2514
  // src/server/infra/storage/text.ts
2207
2515
  function requireText(value2, label, max) {
@@ -2227,9 +2535,9 @@ var AccountRepository = class {
2227
2535
  }
2228
2536
  /** Installation identity, not an account/connection ID. Pi requests it only for ChatGPT login. */
2229
2537
  installationDeviceId() {
2230
- const row = this.db.prepare("SELECT device_id FROM installation WHERE id=1").get();
2231
- if (row) return row.device_id;
2232
- const id = randomUUID5();
2538
+ const row2 = this.db.prepare("SELECT device_id FROM installation WHERE id=1").get();
2539
+ if (row2) return row2.device_id;
2540
+ const id = randomUUID6();
2233
2541
  this.db.prepare("INSERT INTO installation(id,device_id) VALUES(1,?)").run(id);
2234
2542
  return id;
2235
2543
  }
@@ -2254,11 +2562,11 @@ var AccountRepository = class {
2254
2562
  });
2255
2563
  }
2256
2564
  login(username, password) {
2257
- const row = this.db.prepare("SELECT username,password_salt,password_hash FROM account WHERE id=1").get();
2258
- if (!row || typeof username !== "string" || typeof password !== "string") return null;
2259
- const candidate = Buffer.from(passwordHash(password, row.password_salt), "hex");
2260
- const expected = Buffer.from(row.password_hash, "hex");
2261
- if (!timingSafeEqual(candidate, expected) || username !== row.username) return null;
2565
+ const row2 = this.db.prepare("SELECT username,password_salt,password_hash FROM account WHERE id=1").get();
2566
+ if (!row2 || typeof username !== "string" || typeof password !== "string") return null;
2567
+ const candidate = Buffer.from(passwordHash(password, row2.password_salt), "hex");
2568
+ const expected = Buffer.from(row2.password_hash, "hex");
2569
+ if (!timingSafeEqual(candidate, expected) || username !== row2.username) return null;
2262
2570
  const token = randomBytes2(32).toString("base64url");
2263
2571
  this.db.prepare("DELETE FROM auth_sessions WHERE expires_at<?").run(Date.now());
2264
2572
  this.db.prepare("INSERT INTO auth_sessions VALUES (?,?)").run(tokenHash(token), Date.now() + SESSION_LIFETIME_MS);
@@ -2276,13 +2584,13 @@ var AccountRepository = class {
2276
2584
  // src/server/infra/storage/bot-repository.ts
2277
2585
  import { mkdirSync as mkdirSync2 } from "node:fs";
2278
2586
  import { join as join4 } from "node:path";
2279
- import { randomInt as randomInt2, randomUUID as randomUUID7 } from "node:crypto";
2587
+ import { randomInt as randomInt2, randomUUID as randomUUID8 } from "node:crypto";
2280
2588
 
2281
2589
  // src/server/infra/storage/model-connection-repository.ts
2282
- import { randomUUID as randomUUID6 } from "node:crypto";
2590
+ import { randomUUID as randomUUID7 } from "node:crypto";
2283
2591
 
2284
2592
  // src/server/infra/pi/catalog.ts
2285
- import { mkdir as mkdir2, readFile, rename as rename2, writeFile } from "node:fs/promises";
2593
+ import { mkdir as mkdir2, readFile, rename as rename3, writeFile } from "node:fs/promises";
2286
2594
  import { dirname as dirname3, join as join3 } from "node:path";
2287
2595
  import { builtinProviders, getBuiltinModelDataGeneratedAt } from "@earendil-works/pi-ai/providers/all";
2288
2596
  var CATALOG_URL = "https://pi.dev/api/models";
@@ -2411,7 +2719,7 @@ var ModelCatalog = class {
2411
2719
  await mkdir2(dirname3(this.path), { recursive: true, mode: 448 });
2412
2720
  const next = `${this.path}.tmp`;
2413
2721
  await writeFile(next, JSON.stringify(this.kept), { mode: 384 });
2414
- await rename2(next, this.path);
2722
+ await rename3(next, this.path);
2415
2723
  }
2416
2724
  };
2417
2725
  function merge(models, newer) {
@@ -2502,23 +2810,23 @@ var ModelConnectionRepository = class {
2502
2810
  WHERE c.owner_id=? ORDER BY c.created_at,c.id`).all(this.ownerId());
2503
2811
  const models = this.db.prepare("SELECT m.connection_id AS connectionId,m.config_json AS configJson FROM connection_models m JOIN provider_connections c ON c.id=m.connection_id WHERE c.owner_id=? ORDER BY m.created_at,m.model_id").all(this.ownerId());
2504
2812
  const byConnection = /* @__PURE__ */ new Map();
2505
- for (const row of models) byConnection.set(row.connectionId, [...byConnection.get(row.connectionId) ?? [], normalizeModel(JSON.parse(row.configJson))]);
2506
- return rows3.map((row) => ({
2507
- id: row.id,
2508
- providerName: row.providerName,
2509
- displayName: row.displayName,
2510
- kind: row.kind,
2511
- baseUrl: row.baseUrl,
2512
- api: row.api,
2513
- authType: row.authType,
2514
- configured: row.credentialType !== null,
2515
- models: byConnection.get(row.id) ?? [],
2516
- createdAt: row.createdAt,
2517
- updatedAt: row.updatedAt
2813
+ for (const row2 of models) byConnection.set(row2.connectionId, [...byConnection.get(row2.connectionId) ?? [], normalizeModel(JSON.parse(row2.configJson))]);
2814
+ return rows3.map((row2) => ({
2815
+ id: row2.id,
2816
+ providerName: row2.providerName,
2817
+ displayName: row2.displayName,
2818
+ kind: row2.kind,
2819
+ baseUrl: row2.baseUrl,
2820
+ api: row2.api,
2821
+ authType: row2.authType,
2822
+ configured: row2.credentialType !== null,
2823
+ models: byConnection.get(row2.id) ?? [],
2824
+ createdAt: row2.createdAt,
2825
+ updatedAt: row2.updatedAt
2518
2826
  }));
2519
2827
  }
2520
2828
  get(id) {
2521
- return this.list().find((row) => row.id === id) ?? null;
2829
+ return this.list().find((row2) => row2.id === id) ?? null;
2522
2830
  }
2523
2831
  create(input) {
2524
2832
  const providerName = requireText(input.providerName, "Provider name", 80).toLowerCase();
@@ -2532,7 +2840,7 @@ var ModelConnectionRepository = class {
2532
2840
  const baseUrl = input.baseUrl === void 0 ? "" : modelUrl(optionalText(input.baseUrl, "Base URL", 2048));
2533
2841
  const key = input.apiKey === void 0 ? null : requireText(input.apiKey, "API Key", 8192);
2534
2842
  if (key && authType !== "api_key") throw new ValidationError("Invalid credential type");
2535
- const id = randomUUID6(), now = Date.now();
2843
+ const id = randomUUID7(), now = Date.now();
2536
2844
  transaction(this.db, () => {
2537
2845
  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());
2538
2846
  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);
@@ -2558,7 +2866,7 @@ var ModelConnectionRepository = class {
2558
2866
  /** Deleted Bots keep their row (deleteBot) but use nothing: their link to the connection is cleared with it. */
2559
2867
  delete(id) {
2560
2868
  if (!this.get(id)) return false;
2561
- const users = this.db.prepare("SELECT name FROM bots WHERE connection_id=? AND deleted_at IS NULL ORDER BY name").all(id).map((row) => row.name);
2869
+ const users = this.db.prepare("SELECT name FROM bots WHERE connection_id=? AND deleted_at IS NULL ORDER BY name").all(id).map((row2) => row2.name);
2562
2870
  if (users.length) throw new ConflictError(`In use by ${users.join(", ")}`, "connection_in_use", { bots: users.join(", "), count: users.length });
2563
2871
  return transaction(this.db, () => {
2564
2872
  this.db.prepare("UPDATE bots SET connection_id=NULL WHERE connection_id=? AND deleted_at IS NOT NULL").run(id);
@@ -2580,8 +2888,8 @@ var ModelConnectionRepository = class {
2580
2888
  }
2581
2889
  headers(id, modelId) {
2582
2890
  if (!this.get(id)) return {};
2583
- const row = this.db.prepare("SELECT config_json FROM connection_models WHERE connection_id=? AND model_id=?").get(id, modelId);
2584
- return row ? JSON.parse(row.config_json).headers ?? {} : {};
2891
+ const row2 = this.db.prepare("SELECT config_json FROM connection_models WHERE connection_id=? AND model_id=?").get(id, modelId);
2892
+ return row2 ? JSON.parse(row2.config_json).headers ?? {} : {};
2585
2893
  }
2586
2894
  saveModel(id, raw, originalId) {
2587
2895
  if (!this.get(id)) return null;
@@ -2620,7 +2928,7 @@ var ModelConnectionRepository = class {
2620
2928
  }
2621
2929
  /** Names of the Bots (not deleted) whose model this is. */
2622
2930
  botsUsing(id, modelId) {
2623
- return this.db.prepare("SELECT name FROM bots WHERE connection_id=? AND model_id=? AND deleted_at IS NULL ORDER BY name").all(id, modelId).map((row) => row.name);
2931
+ return this.db.prepare("SELECT name FROM bots WHERE connection_id=? AND model_id=? AND deleted_at IS NULL ORDER BY name").all(id, modelId).map((row2) => row2.name);
2624
2932
  }
2625
2933
  /** A built-in connection back to its catalog (A62), in one transaction: its address cleared and its protocol set; each of
2626
2934
  * `models` saved as given, keeping the request headers added to it; `removed` deleted, and the Bots using them left
@@ -2631,7 +2939,7 @@ var ModelConnectionRepository = class {
2631
2939
  const now = Date.now(), rows3 = models.map((model) => ({ id: model.id, stored: JSON.stringify({ ...normalizeModel(model), headers: this.headers(id, model.id) }) }));
2632
2940
  transaction(this.db, () => {
2633
2941
  this.db.prepare("UPDATE provider_connections SET base_url='',api=?,updated_at=? WHERE id=?").run(api, now, id);
2634
- for (const row of rows3) this.db.prepare("UPDATE connection_models SET config_json=?,updated_at=? WHERE connection_id=? AND model_id=?").run(row.stored, now, id, row.id);
2942
+ for (const row2 of rows3) this.db.prepare("UPDATE connection_models SET config_json=?,updated_at=? WHERE connection_id=? AND model_id=?").run(row2.stored, now, id, row2.id);
2635
2943
  for (const modelId of removed) {
2636
2944
  this.db.prepare("UPDATE bots SET connection_id=NULL,model_id='' WHERE connection_id=? AND model_id=?").run(id, modelId);
2637
2945
  this.db.prepare("DELETE FROM connection_models WHERE connection_id=? AND model_id=?").run(id, modelId);
@@ -2651,18 +2959,18 @@ var BotRepository = class {
2651
2959
  this.connections = connections;
2652
2960
  this.inbox = inbox;
2653
2961
  }
2654
- fromRow(row) {
2655
- const workspacePath = join4(this.dataDir, "bots", row.id, "workspace");
2962
+ fromRow(row2) {
2963
+ const workspacePath = join4(this.dataDir, "bots", row2.id, "workspace");
2656
2964
  mkdirSync2(join4(workspacePath, "temp"), { recursive: true, mode: 448 });
2657
- return { ...row, workspacePath };
2965
+ return { ...row2, workspacePath };
2658
2966
  }
2659
2967
  list() {
2660
2968
  const rows3 = this.db.prepare("SELECT id,name,description,system_prompt AS systemPrompt,connection_id AS connectionId,model_id AS modelId,thinking_level AS thinkingLevel,color,mood,created_at AS createdAt FROM bots WHERE owner_id=? AND deleted_at IS NULL ORDER BY created_at,id").all(this.ownerId());
2661
- return rows3.map((row) => this.fromRow(row));
2969
+ return rows3.map((row2) => this.fromRow(row2));
2662
2970
  }
2663
2971
  get(id) {
2664
- const row = this.db.prepare("SELECT id,name,description,system_prompt AS systemPrompt,connection_id AS connectionId,model_id AS modelId,thinking_level AS thinkingLevel,color,mood,created_at AS createdAt FROM bots WHERE id=? AND owner_id=? AND deleted_at IS NULL").get(id, this.ownerId());
2665
- return row ? this.fromRow(row) : null;
2972
+ const row2 = this.db.prepare("SELECT id,name,description,system_prompt AS systemPrompt,connection_id AS connectionId,model_id AS modelId,thinking_level AS thinkingLevel,color,mood,created_at AS createdAt FROM bots WHERE id=? AND owner_id=? AND deleted_at IS NULL").get(id, this.ownerId());
2973
+ return row2 ? this.fromRow(row2) : null;
2666
2974
  }
2667
2975
  /** Member names are unique across people and Bots, ignoring case, so "@name" in a message is unambiguous (A37). */
2668
2976
  nameTaken(name3, exceptBotId) {
@@ -2670,7 +2978,7 @@ var BotRepository = class {
2670
2978
  UNION ALL SELECT 1 FROM bots WHERE lower(name)=lower(:name) AND id<>:except AND deleted_at IS NULL LIMIT 1`).get({ name: name3, except: exceptBotId ?? "" });
2671
2979
  }
2672
2980
  /** Deletes a Bot: its DMs (with anyone), channel memberships and states, queued deliveries,
2673
- * notices and session row go; the member row stays, marked deleted, so messages it left in channels keep their
2981
+ * notices, session row and published ports go; the member row stays, marked deleted, so messages it left in channels keep their
2674
2982
  * author, and its name is free again. Files are the caller's (BotService). */
2675
2983
  delete(id) {
2676
2984
  if (!this.get(id)) return false;
@@ -2682,6 +2990,7 @@ var BotRepository = class {
2682
2990
  this.db.prepare("DELETE FROM bot_sessions WHERE bot_id=?").run(id);
2683
2991
  this.db.prepare("DELETE FROM environment_grants WHERE bot_id=?").run(id);
2684
2992
  this.db.prepare("DELETE FROM secret_grants WHERE bot_id=?").run(id);
2993
+ this.db.prepare("DELETE FROM published_ports WHERE bot_id=?").run(id);
2685
2994
  });
2686
2995
  return true;
2687
2996
  }
@@ -2694,7 +3003,7 @@ var BotRepository = class {
2694
3003
  /** The caller names the Bot (A45: the interface picks a free default in its own language). */
2695
3004
  create(name3) {
2696
3005
  const clean = this.botName(name3);
2697
- const id = randomUUID7(), createdAt = Date.now();
3006
+ const id = randomUUID8(), createdAt = Date.now();
2698
3007
  transaction(this.db, () => {
2699
3008
  this.db.prepare("INSERT INTO members VALUES(?,'bot')").run(id);
2700
3009
  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)]);
@@ -2729,10 +3038,10 @@ var BotRepository = class {
2729
3038
  };
2730
3039
 
2731
3040
  // src/server/infra/storage/chat-repository.ts
2732
- import { randomUUID as randomUUID10 } from "node:crypto";
3041
+ import { randomUUID as randomUUID11 } from "node:crypto";
2733
3042
 
2734
3043
  // src/server/infra/storage/channel-repository.ts
2735
- import { randomUUID as randomUUID8 } from "node:crypto";
3044
+ import { randomUUID as randomUUID9 } from "node:crypto";
2736
3045
 
2737
3046
  // src/server/shared/delivery.ts
2738
3047
  var CATEGORY = {
@@ -2826,15 +3135,15 @@ var InboxRepository = class {
2826
3135
  const rows3 = this.db.prepare(`SELECT n.id,n.kind,n.urgency,n.chat_id AS chatId,c.title AS chatTitle,c.type AS chatType,n.message_id AS messageId,
2827
3136
  n.actor_id AS actorId,n.payload,n.created_at AS createdAt
2828
3137
  FROM inbox n LEFT JOIN chats c ON c.id=n.chat_id WHERE n.member_id=? AND n.category<>'message' AND n.state='pending' ORDER BY n.id`).all(botId);
2829
- return rows3.filter((row) => row.chatId === null || this.chats.canAccess(botId, row.chatId)).map((row) => ({
2830
- id: row.id,
2831
- kind: row.kind,
2832
- urgency: row.urgency,
2833
- createdAt: row.createdAt,
2834
- actor: row.actorId ? this.chats.member(row.actorId) : null,
2835
- payload: row.payload === null ? null : JSON.parse(row.payload),
2836
- messageId: row.messageId,
2837
- chat: row.chatId === null ? null : { id: row.chatId, type: row.chatType, title: row.chatTitle ?? "" }
3138
+ return rows3.filter((row2) => row2.chatId === null || this.chats.canAccess(botId, row2.chatId)).map((row2) => ({
3139
+ id: row2.id,
3140
+ kind: row2.kind,
3141
+ urgency: row2.urgency,
3142
+ createdAt: row2.createdAt,
3143
+ actor: row2.actorId ? this.chats.member(row2.actorId) : null,
3144
+ payload: row2.payload === null ? null : JSON.parse(row2.payload),
3145
+ messageId: row2.messageId,
3146
+ chat: row2.chatId === null ? null : { id: row2.chatId, type: row2.chatType, title: row2.chatTitle ?? "" }
2838
3147
  }));
2839
3148
  }
2840
3149
  /** These notices reached the Bot (steered into its run, or in a batch it was given). A new notice version told
@@ -2844,24 +3153,25 @@ var InboxRepository = class {
2844
3153
  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);
2845
3154
  }
2846
3155
  // A person's rows: what their chat list shows.
2847
- /** What a person's chat row shows (A41): unread interrupt messages, and the newest queued activity. */
3156
+ /** What a person's chat row shows (A41): unread interrupt messages, and the newest activity not read yet (queued,
3157
+ * or a notice version, which is a digest, A77). */
2848
3158
  rowState(memberId, chatId) {
2849
3159
  const counts = this.db.prepare(`SELECT count(*) AS n,COALESCE(SUM(reason IN ('mention','reply')),0) AS at FROM inbox
2850
3160
  WHERE member_id=? AND chat_id=? AND category='message' AND urgency='interrupt' AND state='pending'`).get(memberId, chatId);
2851
- const row = 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
3161
+ 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
2852
3162
  FROM inbox i JOIN chats c ON c.id=i.chat_id
2853
- 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);
2854
- if (!row) return { alerts: counts.n, atMe: counts.at > 0, hint: null };
2855
- const payload = row.payload ? JSON.parse(row.payload) : {};
2856
- const detail = row.kind === "reaction" ? payload.emoji ?? "" : String(payload.version ?? "");
3163
+ 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);
3164
+ if (!row2) return { alerts: counts.n, atMe: counts.at > 0, hint: null };
3165
+ const payload = row2.payload ? JSON.parse(row2.payload) : {};
3166
+ const detail = row2.kind === "reaction" ? payload.emoji ?? "" : String(payload.version ?? "");
2857
3167
  return { alerts: counts.n, atMe: counts.at > 0, hint: {
2858
- kind: row.kind,
2859
- chatId: row.chatId,
2860
- chatType: row.chatType,
2861
- title: row.title,
2862
- actor: row.actorId && this.chats.member(row.actorId)?.name || "",
3168
+ kind: row2.kind,
3169
+ chatId: row2.chatId,
3170
+ chatType: row2.chatType,
3171
+ title: row2.title,
3172
+ actor: row2.actorId && this.chats.member(row2.actorId)?.name || "",
2863
3173
  detail,
2864
- createdAt: row.createdAt
3174
+ createdAt: row2.createdAt
2865
3175
  } };
2866
3176
  }
2867
3177
  /** A person read up to `sequence` in a chat: the messages delivered to them there are told. */
@@ -2899,11 +3209,11 @@ var ChannelRepository = class {
2899
3209
  everyone(ownerId) {
2900
3210
  return this.db.prepare(`SELECT m.id,m.kind,COALESCE(a.username,b.name,m.id) AS name,b.deleted_at IS NOT NULL AS deleted FROM members m
2901
3211
  LEFT JOIN account a ON a.member_id=m.id LEFT JOIN bots b ON b.id=m.id
2902
- WHERE m.id=:owner OR b.owner_id=:owner ORDER BY m.kind DESC,b.created_at,m.id`).all({ owner: ownerId }).map((row) => ({ id: row.id, kind: row.kind, name: row.name, ...row.deleted ? { deleted: true } : {} }));
3212
+ WHERE m.id=:owner OR b.owner_id=:owner ORDER BY m.kind DESC,b.created_at,m.id`).all({ owner: ownerId }).map((row2) => ({ id: row2.id, kind: row2.kind, name: row2.name, ...row2.deleted ? { deleted: true } : {} }));
2903
3213
  }
2904
3214
  /** The directory with each Bot's description, for a Bot's members.list. */
2905
3215
  profiles(ownerId) {
2906
- const descriptions = new Map(this.db.prepare("SELECT id,description FROM bots WHERE owner_id=? AND deleted_at IS NULL").all(ownerId).map((row) => [row.id, row.description]));
3216
+ const descriptions = new Map(this.db.prepare("SELECT id,description FROM bots WHERE owner_id=? AND deleted_at IS NULL").all(ownerId).map((row2) => [row2.id, row2.description]));
2907
3217
  return this.directory(ownerId).map((member) => ({ ...member, description: descriptions.get(member.id) ?? "" }));
2908
3218
  }
2909
3219
  /** The person a member acts for: itself, or a Bot's owner. */
@@ -2918,7 +3228,7 @@ var ChannelRepository = class {
2918
3228
  return this.db.prepare(`SELECT ${SECTION_COLUMNS} FROM sections WHERE id=? AND owner_id=?`).get(id, ownerId) ?? null;
2919
3229
  }
2920
3230
  createSection(ownerId, name3) {
2921
- const id = randomUUID8(), now = Date.now();
3231
+ const id = randomUUID9(), now = Date.now();
2922
3232
  const position = this.db.prepare("SELECT COALESCE(MAX(position),0)+1 AS next FROM sections WHERE owner_id=?").get(ownerId).next;
2923
3233
  this.db.prepare("INSERT INTO sections(id,owner_id,name,position,created_at,updated_at) VALUES(?,?,?,?,?,?)").run(id, ownerId, name3, position, now, now);
2924
3234
  return this.section(ownerId, id);
@@ -2938,7 +3248,7 @@ var ChannelRepository = class {
2938
3248
  const allowed = new Set(this.directory(ownerId).map((member) => member.id));
2939
3249
  const members = [.../* @__PURE__ */ new Set([ownerId, ...input.memberIds])];
2940
3250
  if (members.some((id2) => !allowed.has(id2))) throw new ChatError(400, "Invalid channel members");
2941
- const id = randomUUID8(), now = Date.now();
3251
+ const id = randomUUID9(), now = Date.now();
2942
3252
  return transaction(this.db, () => {
2943
3253
  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);
2944
3254
  for (const memberId of members) {
@@ -2975,18 +3285,18 @@ var ChannelRepository = class {
2975
3285
  const type = this.db.prepare("SELECT type FROM chats WHERE id=?").get(chatId)?.type ?? "dm";
2976
3286
  const ids = type === "thread" ? this.db.prepare(`SELECT s.member_id AS id FROM chat_states s JOIN chats c ON c.id=s.chat_id
2977
3287
  JOIN chat_members cm ON cm.chat_id=c.parent_id AND cm.member_id=s.member_id AND cm.can_read=1 WHERE s.chat_id=? AND s.following=1`).all(chatId) : this.db.prepare("SELECT member_id AS id FROM chat_members WHERE chat_id=? AND can_read=1").all(chatId);
2978
- return { type, members: ids.map((row) => this.chats.member(row.id)).filter((member) => member !== null) };
3288
+ return { type, members: ids.map((row2) => this.chats.member(row2.id)).filter((member) => member !== null) };
2979
3289
  }
2980
3290
  /** Channel notice / thread summary: any member who can write there, people and Bots alike. The routed
2981
3291
  * deliveries (A41) are written with the new version, which is their payload. */
2982
- setNote(actorId, chatId, text4, deliveries = []) {
3292
+ setNote(actorId, chatId, text5, deliveries = []) {
2983
3293
  this.chats.assertAccess(actorId, chatId, true);
2984
3294
  const type = this.db.prepare("SELECT type FROM chats WHERE id=?").get(chatId).type;
2985
3295
  if (type === "dm") throw new ChatError(400, "Direct messages have no notice");
2986
3296
  const now = Date.now();
2987
3297
  transaction(this.db, () => {
2988
3298
  this.db.prepare(`INSERT INTO chat_notes(chat_id,text,updated_by,updated_at,version) VALUES(?,?,?,?,1)
2989
- ON CONFLICT(chat_id) DO UPDATE SET text=excluded.text,updated_by=excluded.updated_by,updated_at=excluded.updated_at,version=version+1`).run(chatId, text4, actorId, now);
3299
+ ON CONFLICT(chat_id) DO UPDATE SET text=excluded.text,updated_by=excluded.updated_by,updated_at=excluded.updated_at,version=version+1`).run(chatId, text5, actorId, now);
2990
3300
  this.db.prepare("UPDATE chats SET updated_at=? WHERE id=?").run(now, chatId);
2991
3301
  this.markNoteSeen(actorId, chatId);
2992
3302
  const version = this.db.prepare("SELECT version FROM chat_notes WHERE chat_id=?").get(chatId).version;
@@ -3011,9 +3321,9 @@ var ChannelRepository = class {
3011
3321
  unreadNotes(memberId, chatIds) {
3012
3322
  const rows3 = [];
3013
3323
  for (const chatId of new Set(chatIds)) {
3014
- const row = this.db.prepare(`SELECT c.id AS chatId,c.title,c.type,n.version,s.note_seen AS seen FROM chats c
3324
+ const row2 = this.db.prepare(`SELECT c.id AS chatId,c.title,c.type,n.version,s.note_seen AS seen FROM chats c
3015
3325
  JOIN chat_notes n ON n.chat_id=c.id LEFT JOIN chat_states s ON s.chat_id=c.id AND s.member_id=? WHERE c.id=?`).get(memberId, chatId);
3016
- if (row && (row.seen ?? 0) < row.version) rows3.push({ chatId: row.chatId, title: row.title, type: row.type, version: row.version });
3326
+ if (row2 && (row2.seen ?? 0) < row2.version) rows3.push({ chatId: row2.chatId, title: row2.title, type: row2.type, version: row2.version });
3017
3327
  }
3018
3328
  return rows3;
3019
3329
  }
@@ -3043,13 +3353,13 @@ var ChannelRepository = class {
3043
3353
  }
3044
3354
  /** Members subscribed to this chat (whether they can still read it is checked by the caller). */
3045
3355
  subscribers(chatId) {
3046
- return this.db.prepare("SELECT member_id AS id FROM chat_states WHERE chat_id=? AND subscribed=1").all(chatId).map((row) => row.id);
3356
+ return this.db.prepare("SELECT member_id AS id FROM chat_states WHERE chat_id=? AND subscribed=1").all(chatId).map((row2) => row2.id);
3047
3357
  }
3048
3358
  /** Channels and threads the member is subscribed to and can still read, most recently active first. */
3049
3359
  subscriptions(memberId) {
3050
3360
  return this.db.prepare(`SELECT c.id FROM chats c JOIN chat_states s ON s.chat_id=c.id AND s.member_id=:member AND s.subscribed=1
3051
3361
  JOIN chat_members cm ON cm.chat_id=COALESCE(c.parent_id,c.id) AND cm.member_id=:member AND cm.can_read=1
3052
- ORDER BY c.updated_at DESC,c.id`).all({ member: memberId }).map((row) => row.id);
3362
+ ORDER BY c.updated_at DESC,c.id`).all({ member: memberId }).map((row2) => row2.id);
3053
3363
  }
3054
3364
  /** For a Bot's batch and `messages.read unread=true` (A37, A70): its chats with unread, most recently active first —
3055
3365
  * DMs, channels, and the threads it follows while they are open. A finished thread stays out of this overview,
@@ -3067,7 +3377,7 @@ var ChannelRepository = class {
3067
3377
  };
3068
3378
 
3069
3379
  // src/server/infra/storage/thread-repository.ts
3070
- import { randomUUID as randomUUID9 } from "node:crypto";
3380
+ import { randomUUID as randomUUID10 } from "node:crypto";
3071
3381
  function threadTitle(content2, names, file = "") {
3072
3382
  const line = (content2.split("\n").map((item) => item.trim()).find(Boolean) ?? "").replace(/[*`~]/g, "").replace(/^[#>\s-]+/, "");
3073
3383
  const sorted = [...names].sort((a, b) => b.length - a.length);
@@ -3097,7 +3407,7 @@ var ThreadRepository = class {
3097
3407
  if (root.type !== "channel") throw new ChatError(400, "Threads start from channel messages");
3098
3408
  let id = this.db.prepare("SELECT id FROM chats WHERE root_message_id=?").get(rootMessageId)?.id;
3099
3409
  if (!id) {
3100
- id = randomUUID9();
3410
+ id = randomUUID10();
3101
3411
  const now = Date.now();
3102
3412
  const firstFile = this.db.prepare("SELECT name FROM attachments WHERE message_id=? ORDER BY created_at,rowid LIMIT 1").get(rootMessageId)?.name;
3103
3413
  this.db.prepare(`INSERT INTO chats(id,type,title,owner_id,created_at,updated_at,parent_id,root_message_id,status)
@@ -3134,11 +3444,11 @@ var ThreadRepository = class {
3134
3444
  list(actorId, channelId) {
3135
3445
  this.chats.assertAccess(actorId, channelId);
3136
3446
  return this.db.prepare(`SELECT id FROM chats c WHERE parent_id=? AND type='thread' AND EXISTS(SELECT 1 FROM messages WHERE chat_id=c.id)
3137
- ORDER BY updated_at DESC,id`).all(channelId).map((row) => row.id);
3447
+ ORDER BY updated_at DESC,id`).all(channelId).map((row2) => row2.id);
3138
3448
  }
3139
3449
  /** Threads the member follows in channels it can still read (with at least one reply). */
3140
3450
  followed(memberId) {
3141
- return this.db.prepare(`SELECT c.id ${FOLLOWED} ORDER BY c.updated_at DESC,c.id`).all({ member: memberId }).map((row) => row.id);
3451
+ return this.db.prepare(`SELECT c.id ${FOLLOWED} ORDER BY c.updated_at DESC,c.id`).all({ member: memberId }).map((row2) => row2.id);
3142
3452
  }
3143
3453
  /** The same threads with what orders, filters and counts them (A69): status, creation, last reply, and the member's
3144
3454
  * unread and @ counts by the rule a chat's own count follows, in one statement however many there are. */
@@ -3168,7 +3478,7 @@ var InteractionRepository = class {
3168
3478
  reactions(messageId) {
3169
3479
  const rows3 = this.db.prepare("SELECT emoji,member_id AS memberId FROM message_reactions WHERE message_id=? ORDER BY created_at,rowid").all(messageId);
3170
3480
  const groups = /* @__PURE__ */ new Map();
3171
- for (const row of rows3) groups.set(row.emoji, [...groups.get(row.emoji) ?? [], row.memberId]);
3481
+ for (const row2 of rows3) groups.set(row2.emoji, [...groups.get(row2.emoji) ?? [], row2.memberId]);
3172
3482
  return [...groups].map(([emoji, memberIds]) => ({ emoji, memberIds }));
3173
3483
  }
3174
3484
  /** Adds or removes the actor's mark (anyone who can write in the chat), writing the routed deliveries with a new
@@ -3179,16 +3489,16 @@ var InteractionRepository = class {
3179
3489
  }
3180
3490
  react(actorId, messageId, emoji, on, deliveries) {
3181
3491
  return transaction(this.db, () => {
3182
- const row = this.db.prepare("SELECT chat_id AS chatId FROM messages WHERE id=?").get(messageId);
3183
- if (!row || !this.chats.canAccess(actorId, row.chatId, true)) throw new ChatError(404, "Message not found");
3492
+ const row2 = this.db.prepare("SELECT chat_id AS chatId FROM messages WHERE id=?").get(messageId);
3493
+ if (!row2 || !this.chats.canAccess(actorId, row2.chatId, true)) throw new ChatError(404, "Message not found");
3184
3494
  if (on) {
3185
3495
  const added = Number(this.db.prepare("INSERT OR IGNORE INTO message_reactions(message_id,member_id,emoji,created_at) VALUES(?,?,?,?)").run(messageId, actorId, emoji, Date.now()).changes) > 0;
3186
- if (added) this.chats.inbox.write(deliveries.map((delivery) => ({ delivery, chatId: row.chatId, messageId, actorId, payload: { emoji }, createdAt: Date.now() })));
3187
- return { chatId: row.chatId, added };
3496
+ if (added) this.chats.inbox.write(deliveries.map((delivery) => ({ delivery, chatId: row2.chatId, messageId, actorId, payload: { emoji }, createdAt: Date.now() })));
3497
+ return { chatId: row2.chatId, added };
3188
3498
  }
3189
3499
  this.db.prepare("DELETE FROM message_reactions WHERE message_id=? AND member_id=? AND emoji=?").run(messageId, actorId, emoji);
3190
3500
  this.chats.inbox.withdrawReaction(messageId, actorId, emoji);
3191
- return { chatId: row.chatId, added: false };
3501
+ return { chatId: row2.chatId, added: false };
3192
3502
  });
3193
3503
  }
3194
3504
  /** 免打扰 for one member and chat. */
@@ -3200,7 +3510,7 @@ var InteractionRepository = class {
3200
3510
  /** Members who muted a chat; a thread follows its channel's 免打扰 too. A route input (A41). */
3201
3511
  mutedIn(chatId) {
3202
3512
  return this.db.prepare(`SELECT DISTINCT member_id AS id FROM chat_states WHERE muted=1 AND chat_id IN
3203
- (SELECT id FROM chats WHERE id=? UNION SELECT parent_id FROM chats WHERE id=? AND parent_id IS NOT NULL)`).all(chatId, chatId).map((row) => row.id);
3513
+ (SELECT id FROM chats WHERE id=? UNION SELECT parent_id FROM chats WHERE id=? AND parent_id IS NOT NULL)`).all(chatId, chatId).map((row2) => row2.id);
3204
3514
  }
3205
3515
  /** Folds a message to one line in the interface, or unfolds it (A60); its content and everything else stay. */
3206
3516
  setFolded(messageId, folded) {
@@ -3252,9 +3562,9 @@ var ChatRepository = class {
3252
3562
  /** Every delivery (A41, A61): the one place inbox SQL lives. */
3253
3563
  inbox;
3254
3564
  member(id) {
3255
- const row = this.db.prepare(`SELECT m.id,m.kind,COALESCE(a.username,b.name,m.id) AS name,b.deleted_at IS NOT NULL AS deleted FROM members m
3565
+ const row2 = this.db.prepare(`SELECT m.id,m.kind,COALESCE(a.username,b.name,m.id) AS name,b.deleted_at IS NOT NULL AS deleted FROM members m
3256
3566
  LEFT JOIN account a ON a.member_id=m.id LEFT JOIN bots b ON b.id=m.id WHERE m.id=?`).get(id);
3257
- return row ? { id: row.id, kind: row.kind, name: row.name, ...row.deleted ? { deleted: true } : {} } : null;
3567
+ return row2 ? { id: row2.id, kind: row2.kind, name: row2.name, ...row2.deleted ? { deleted: true } : {} } : null;
3258
3568
  }
3259
3569
  /** A thread has no member list of its own: reading and writing follow its parent channel (A37). The owner may
3260
3570
  * read (never write) a DM between two of its Bots. */
@@ -3278,8 +3588,8 @@ var ChatRepository = class {
3278
3588
  return { unread, mentions, readSequence: read3?.n ?? 0 };
3279
3589
  }
3280
3590
  note(chatId) {
3281
- const row = this.db.prepare("SELECT text,updated_by AS updatedBy,updated_at AS updatedAt,version FROM chat_notes WHERE chat_id=?").get(chatId);
3282
- return row ? { ...row, updatedBy: this.member(row.updatedBy) } : null;
3591
+ const row2 = this.db.prepare("SELECT text,updated_by AS updatedBy,updated_at AS updatedAt,version FROM chat_notes WHERE chat_id=?").get(chatId);
3592
+ return row2 ? { ...row2, updatedBy: this.member(row2.updatedBy) } : null;
3283
3593
  }
3284
3594
  assertAccess(actorId, chatId, write2 = false) {
3285
3595
  if (!this.canAccess(actorId, chatId, write2)) throw new ChatError(404, "Chat not found");
@@ -3289,16 +3599,16 @@ var ChatRepository = class {
3289
3599
  const found = this.db.prepare(`SELECT ${CHAT_COLUMNS} FROM chats c WHERE c.id=?`).get(id);
3290
3600
  if (!found) return null;
3291
3601
  const { lastMessageId, lastMessage: content2, ...rest } = found;
3292
- const row = { ...rest, lastMessage: content2 !== null && lastMessageId ? { content: clipMarked(content2, 300), quotes: this.quotes.of(lastMessageId) } : null };
3293
- const members = this.db.prepare("SELECT member_id AS id FROM chat_members WHERE chat_id=? ORDER BY member_id").all(row.parentId ?? id);
3602
+ const row2 = { ...rest, lastMessage: content2 !== null && lastMessageId ? { content: clipMarked(content2, 300), quotes: this.quotes.of(lastMessageId) } : null };
3603
+ const members = this.db.prepare("SELECT member_id AS id FROM chat_members WHERE chat_id=? ORDER BY member_id").all(row2.parentId ?? id);
3294
3604
  const state = this.db.prepare("SELECT following,subscribed,pinned,muted FROM chat_states WHERE chat_id=? AND member_id=?").get(id, actorId);
3295
3605
  return {
3296
- ...row,
3606
+ ...row2,
3297
3607
  memberIds: members.map((item) => item.id),
3298
3608
  note: this.note(id),
3299
3609
  ...this.unread(actorId, id),
3300
3610
  ...this.inbox.rowState(actorId, id),
3301
- following: row.type === "thread" && !!state?.following,
3611
+ following: row2.type === "thread" && !!state?.following,
3302
3612
  subscribed: !!state?.subscribed,
3303
3613
  pinned: !!state?.pinned,
3304
3614
  muted: !!state?.muted
@@ -3323,7 +3633,7 @@ var ChatRepository = class {
3323
3633
  const id = transaction(this.db, () => {
3324
3634
  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=?
3325
3635
  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);
3326
- const id2 = existing?.id ?? randomUUID10();
3636
+ const id2 = existing?.id ?? randomUUID11();
3327
3637
  if (!existing) {
3328
3638
  this.db.prepare("INSERT INTO chats(id,type,title,owner_id,created_at,updated_at) VALUES(?,'dm',?,?,?,?)").run(id2, title, ownerId, now, now);
3329
3639
  this.db.prepare("INSERT INTO chat_members(chat_id,member_id) VALUES(?,?),(?,?)").run(id2, actorId, id2, targetId);
@@ -3336,7 +3646,7 @@ var ChatRepository = class {
3336
3646
  memberships(actorId, chatId) {
3337
3647
  this.assertAccess(actorId, chatId);
3338
3648
  return this.db.prepare(`SELECT member_id AS id,can_read AS canRead,can_write AS canWrite FROM chat_members
3339
- WHERE chat_id=COALESCE((SELECT parent_id FROM chats WHERE id=?),?) ORDER BY member_id`).all(chatId, chatId).map((row) => ({ member: this.member(row.id), canRead: !!row.canRead, canWrite: !!row.canWrite }));
3649
+ WHERE chat_id=COALESCE((SELECT parent_id FROM chats WHERE id=?),?) ORDER BY member_id`).all(chatId, chatId).map((row2) => ({ member: this.member(row2.id), canRead: !!row2.canRead, canWrite: !!row2.canWrite }));
3340
3650
  }
3341
3651
  updateTitle(actorId, chatId, title) {
3342
3652
  this.assertOwner(actorId, chatId);
@@ -3363,50 +3673,50 @@ var ChatRepository = class {
3363
3673
  return ids.map(({ id }) => this.getChat(ownerId, id));
3364
3674
  }
3365
3675
  /** Up to `limit` messages before a sequence, oldest first. */
3366
- messagesBefore(actorId, chatId, before, limit) {
3676
+ messagesBefore(actorId, chatId, before, limit2) {
3367
3677
  this.assertAccess(actorId, chatId);
3368
- const rows3 = this.db.prepare(`SELECT ${MESSAGE_COLUMNS} FROM messages WHERE chat_id=? AND sequence<? ORDER BY sequence DESC LIMIT ?`).all(chatId, before, limit);
3369
- return rows3.reverse().map((row) => this.messageFromRow(row));
3678
+ const rows3 = this.db.prepare(`SELECT ${MESSAGE_COLUMNS} FROM messages WHERE chat_id=? AND sequence<? ORDER BY sequence DESC LIMIT ?`).all(chatId, before, limit2);
3679
+ return rows3.reverse().map((row2) => this.messageFromRow(row2));
3370
3680
  }
3371
3681
  /** Exactly the messages counted as unread for this member (same rule as `unread`), oldest first. */
3372
- unreadMessages(memberId, chatId, limit) {
3682
+ unreadMessages(memberId, chatId, limit2) {
3373
3683
  this.assertAccess(memberId, chatId);
3374
3684
  if (this.db.prepare("SELECT type FROM chats WHERE id=?").get(chatId).type === "thread" && !this.threads.isFollowing(memberId, chatId)) return [];
3375
- 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 });
3376
- return rows3.map((row) => this.messageFromRow(row));
3685
+ 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 });
3686
+ return rows3.map((row2) => this.messageFromRow(row2));
3377
3687
  }
3378
3688
  /** Search every chat the member can read: its DMs and channels and their threads (newest first). `within` keeps
3379
3689
  * one chat, a channel with its threads; `sender` keeps one member's messages. */
3380
- searchAll(actorId, query, limit, within, sender) {
3690
+ searchAll(actorId, query, limit2, within, sender) {
3381
3691
  const scope = (within ? "AND (c.id=? OR c.parent_id=?) " : "") + (sender ? "AND m.sender_id=?" : "");
3382
3692
  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
3383
3693
  JOIN chat_members cm ON cm.chat_id=COALESCE(c.parent_id,c.id) AND cm.member_id=? AND cm.can_read=1
3384
- WHERE instr(lower(m.content),lower(?))>0 ${scope} ORDER BY m.position DESC LIMIT ?`).all(actorId, query, ...within ? [within, within] : [], ...sender ? [sender] : [], limit);
3385
- return rows3.map((row) => this.messageFromRow(row));
3694
+ WHERE instr(lower(m.content),lower(?))>0 ${scope} ORDER BY m.position DESC LIMIT ?`).all(actorId, query, ...within ? [within, within] : [], ...sender ? [sender] : [], limit2);
3695
+ return rows3.map((row2) => this.messageFromRow(row2));
3386
3696
  }
3387
3697
  /** Channels and threads the member can read whose title, topic or notice / summary contains the query (newest first). */
3388
- searchChatIds(actorId, query, limit) {
3698
+ searchChatIds(actorId, query, limit2) {
3389
3699
  return this.db.prepare(`SELECT c.id FROM chats c JOIN chat_members cm ON cm.chat_id=COALESCE(c.parent_id,c.id) AND cm.member_id=? AND cm.can_read=1
3390
3700
  LEFT JOIN chat_notes n ON n.chat_id=c.id WHERE c.type<>'dm'
3391
3701
  AND (instr(lower(c.title),lower(?))>0 OR instr(lower(c.topic),lower(?))>0 OR instr(lower(COALESCE(n.text,'')),lower(?))>0)
3392
- ORDER BY c.updated_at DESC LIMIT ?`).all(actorId, query, query, query, limit).map((row) => row.id);
3702
+ ORDER BY c.updated_at DESC LIMIT ?`).all(actorId, query, query, query, limit2).map((row2) => row2.id);
3393
3703
  }
3394
- messages(actorId, chatId, after2 = 0, limit = 1e3) {
3704
+ messages(actorId, chatId, after2 = 0, limit2 = 1e3) {
3395
3705
  this.assertAccess(actorId, chatId);
3396
- const rows3 = this.db.prepare(`SELECT ${MESSAGE_COLUMNS} FROM messages WHERE chat_id=? AND sequence>? ORDER BY sequence LIMIT ?`).all(chatId, after2, limit);
3397
- return rows3.map((row) => this.messageFromRow(row));
3706
+ const rows3 = this.db.prepare(`SELECT ${MESSAGE_COLUMNS} FROM messages WHERE chat_id=? AND sequence>? ORDER BY sequence LIMIT ?`).all(chatId, after2, limit2);
3707
+ return rows3.map((row2) => this.messageFromRow(row2));
3398
3708
  }
3399
3709
  message(actorId, id) {
3400
- const row = this.db.prepare(`SELECT ${MESSAGE_COLUMNS} FROM messages WHERE id=?`).get(id);
3401
- return row && this.canAccess(actorId, row.chatId) ? this.messageFromRow(row) : null;
3710
+ const row2 = this.db.prepare(`SELECT ${MESSAGE_COLUMNS} FROM messages WHERE id=?`).get(id);
3711
+ return row2 && this.canAccess(actorId, row2.chatId) ? this.messageFromRow(row2) : null;
3402
3712
  }
3403
- search(actorId, chatId, query, limit, sender) {
3713
+ search(actorId, chatId, query, limit2, sender) {
3404
3714
  this.assertAccess(actorId, chatId);
3405
3715
  const rows3 = this.db.prepare(`SELECT ${MESSAGE_COLUMNS} FROM messages WHERE chat_id=? AND instr(lower(content),lower(?))>0 ${sender ? "AND sender_id=?" : ""}
3406
- ORDER BY sequence DESC LIMIT ?`).all(chatId, query, ...sender ? [sender] : [], limit);
3407
- return rows3.map((row) => this.messageFromRow(row));
3716
+ ORDER BY sequence DESC LIMIT ?`).all(chatId, query, ...sender ? [sender] : [], limit2);
3717
+ return rows3.map((row2) => this.messageFromRow(row2));
3408
3718
  }
3409
- sentActivity(actorId, senderId, before, limit) {
3719
+ sentActivity(actorId, senderId, before, limit2) {
3410
3720
  const condition = !before ? "" : before.kind === "chat" ? "AND (m.created_at<? OR (m.created_at=? AND m.id<?))" : "AND m.created_at<=?";
3411
3721
  const cursor2 = !before ? [] : before.kind === "chat" ? [before.timestamp, before.timestamp, before.id] : [before.timestamp];
3412
3722
  const rows3 = this.db.prepare(`SELECT m.id,m.chat_id AS chatId,c.title AS chatTitle,m.sequence,m.created_at AS timestamp,
@@ -3414,34 +3724,34 @@ var ChatRepository = class {
3414
3724
  (SELECT count(*) FROM attachments a WHERE a.message_id=m.id) AS attachmentCount
3415
3725
  FROM messages m JOIN chats c ON c.id=m.chat_id
3416
3726
  JOIN chat_members cm ON cm.chat_id=COALESCE(c.parent_id,c.id) AND cm.member_id=? AND cm.can_read=1
3417
- WHERE m.sender_id=? ${condition} ORDER BY m.created_at DESC,m.id DESC LIMIT ?`).all(actorId, senderId, ...cursor2, limit);
3418
- return rows3.map(({ historyId, toolCallId, ...row }) => ({ ...row, source: historyId === null ? null : { historyId, toolCallId } }));
3727
+ WHERE m.sender_id=? ${condition} ORDER BY m.created_at DESC,m.id DESC LIMIT ?`).all(actorId, senderId, ...cursor2, limit2);
3728
+ return rows3.map(({ historyId, toolCallId, ...row2 }) => ({ ...row2, source: historyId === null ? null : { historyId, toolCallId } }));
3419
3729
  }
3420
- messageFromRow(row) {
3421
- const attachments = this.db.prepare("SELECT id,name,mime_type AS mimeType,size FROM attachments WHERE message_id=? ORDER BY created_at,rowid").all(row.id);
3730
+ messageFromRow(row2) {
3731
+ 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);
3422
3732
  return {
3423
- id: row.id,
3424
- chatId: row.chatId,
3425
- sequence: row.sequence,
3426
- sender: this.member(row.senderId),
3427
- content: row.content,
3428
- createdAt: row.createdAt,
3733
+ id: row2.id,
3734
+ chatId: row2.chatId,
3735
+ sequence: row2.sequence,
3736
+ sender: this.member(row2.senderId),
3737
+ content: row2.content,
3738
+ createdAt: row2.createdAt,
3429
3739
  attachments,
3430
- source: row.historyId === null ? null : { historyId: row.historyId, toolCallId: row.toolCallId },
3431
- question: row.question === null ? null : JSON.parse(row.question),
3432
- mentions: this.db.prepare("SELECT member_id AS id FROM message_mentions WHERE message_id=? ORDER BY rowid").all(row.id).map((item) => item.id),
3433
- thread: this.threads.info(row.id),
3434
- echoOf: row.echoOf === null ? null : { id: row.echoOf, title: this.db.prepare("SELECT title FROM chats WHERE id=?").get(row.echoOf).title },
3435
- replyTo: row.replyTo === null ? null : this.quote(row.replyTo),
3436
- reactions: this.interactions.reactions(row.id),
3437
- quotes: this.quotes.of(row.id),
3438
- folded: row.foldedAt !== null
3740
+ source: row2.historyId === null ? null : { historyId: row2.historyId, toolCallId: row2.toolCallId },
3741
+ question: row2.question === null ? null : JSON.parse(row2.question),
3742
+ mentions: this.db.prepare("SELECT member_id AS id FROM message_mentions WHERE message_id=? ORDER BY rowid").all(row2.id).map((item) => item.id),
3743
+ thread: this.threads.info(row2.id),
3744
+ echoOf: row2.echoOf === null ? null : { id: row2.echoOf, title: this.db.prepare("SELECT title FROM chats WHERE id=?").get(row2.echoOf).title },
3745
+ replyTo: row2.replyTo === null ? null : this.quote(row2.replyTo),
3746
+ reactions: this.interactions.reactions(row2.id),
3747
+ quotes: this.quotes.of(row2.id),
3748
+ folded: row2.foldedAt !== null
3439
3749
  };
3440
3750
  }
3441
3751
  /** What a reply shows of the message it answers: who wrote it and the start of its content, with its quotes ("" is attachments only). */
3442
3752
  quote(id) {
3443
- const row = this.db.prepare("SELECT id,sender_id AS senderId,content FROM messages WHERE id=?").get(id);
3444
- return row ? { id: row.id, senderId: row.senderId, content: clipMarked(row.content, 200), quotes: this.quotes.of(row.id) } : null;
3753
+ const row2 = this.db.prepare("SELECT id,sender_id AS senderId,content FROM messages WHERE id=?").get(id);
3754
+ return row2 ? { id: row2.id, senderId: row2.senderId, content: clipMarked(row2.content, 200), quotes: this.quotes.of(row2.id) } : null;
3445
3755
  }
3446
3756
  append(actorId, chatId, draft, requestHash, files = [], deliveries = { message: [], echo: [] }, mentions = []) {
3447
3757
  return transaction(this.db, () => {
@@ -3531,15 +3841,15 @@ var ChatRepository = class {
3531
3841
  return file;
3532
3842
  }
3533
3843
  attachment(actorId, id) {
3534
- const row = this.db.prepare(`SELECT id,chat_id AS chatId,name,mime_type AS mimeType,size FROM attachments WHERE id=? AND message_id IS NOT NULL`).get(id);
3535
- return row && this.canAccess(actorId, row.chatId) ? { id: row.id, name: row.name, mimeType: row.mimeType, size: row.size } : null;
3844
+ const row2 = this.db.prepare(`SELECT id,chat_id AS chatId,name,mime_type AS mimeType,size FROM attachments WHERE id=? AND message_id IS NOT NULL`).get(id);
3845
+ return row2 && this.canAccess(actorId, row2.chatId) ? { id: row2.id, name: row2.name, mimeType: row2.mimeType, size: row2.size } : null;
3536
3846
  }
3537
3847
  hasAttachment(id) {
3538
3848
  return Boolean(this.db.prepare("SELECT 1 FROM attachments WHERE id=?").get(id));
3539
3849
  }
3540
3850
  /** Ids of attachments deleted since the last call, cascades included (the trigger of v28), whose files can go. */
3541
3851
  removedAttachments() {
3542
- return this.db.prepare("SELECT id FROM attachment_removals").all().map((row) => row.id);
3852
+ return this.db.prepare("SELECT id FROM attachment_removals").all().map((row2) => row2.id);
3543
3853
  }
3544
3854
  forgetRemoved(ids) {
3545
3855
  const forget = this.db.prepare("DELETE FROM attachment_removals WHERE id=?");
@@ -3555,7 +3865,7 @@ var ChatRepository = class {
3555
3865
  };
3556
3866
 
3557
3867
  // src/server/infra/storage/runtime-repository.ts
3558
- import { randomUUID as randomUUID11 } from "node:crypto";
3868
+ import { randomUUID as randomUUID12 } from "node:crypto";
3559
3869
  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";
3560
3870
  var RuntimeRepository = class {
3561
3871
  /** `inbox`: a run claims and settles the Bot's message deliveries there (A41). */
@@ -3564,8 +3874,8 @@ var RuntimeRepository = class {
3564
3874
  this.inbox = inbox;
3565
3875
  }
3566
3876
  session(botId) {
3567
- const row = this.db.prepare("SELECT bot_id AS botId,file,runtime_session_id AS sessionId,persisted FROM bot_sessions WHERE bot_id=?").get(botId);
3568
- return row ? { ...row, persisted: !!row.persisted } : null;
3877
+ const row2 = this.db.prepare("SELECT bot_id AS botId,file,runtime_session_id AS sessionId,persisted FROM bot_sessions WHERE bot_id=?").get(botId);
3878
+ return row2 ? { ...row2, persisted: !!row2.persisted } : null;
3569
3879
  }
3570
3880
  ensureSession(botId, file) {
3571
3881
  this.db.prepare("INSERT INTO bot_sessions(bot_id,file,created_at) VALUES(?,?,?) ON CONFLICT(bot_id) DO NOTHING").run(botId, file, Date.now());
@@ -3587,7 +3897,7 @@ var RuntimeRepository = class {
3587
3897
  return transaction(this.db, () => {
3588
3898
  const inputs = this.inbox.pendingMessages(botId);
3589
3899
  if (!inputs.length && !allowEmpty) return null;
3590
- const id = randomUUID11();
3900
+ const id = randomUUID12();
3591
3901
  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());
3592
3902
  inputs.forEach((input, position) => this.db.prepare("INSERT INTO bot_run_inputs VALUES(?,?,?,?)").run(id, position + 1, input.chatId, input.messageId));
3593
3903
  this.inbox.claimMessages(botId, id);
@@ -3621,19 +3931,19 @@ var RuntimeRepository = class {
3621
3931
  });
3622
3932
  }
3623
3933
  run(id) {
3624
- const row = this.db.prepare(`SELECT ${RUN_COLUMNS} FROM bot_runs WHERE id=?`).get(id);
3625
- return row ? this.fromRow(row) : null;
3934
+ const row2 = this.db.prepare(`SELECT ${RUN_COLUMNS} FROM bot_runs WHERE id=?`).get(id);
3935
+ return row2 ? this.fromRow(row2) : null;
3626
3936
  }
3627
3937
  runs(botId) {
3628
- return this.db.prepare(`SELECT ${RUN_COLUMNS} FROM bot_runs WHERE bot_id=? ORDER BY created_at DESC,rowid DESC LIMIT 50`).all(botId).map((row) => this.fromRow(row));
3938
+ return this.db.prepare(`SELECT ${RUN_COLUMNS} FROM bot_runs WHERE bot_id=? ORDER BY created_at DESC,rowid DESC LIMIT 50`).all(botId).map((row2) => this.fromRow(row2));
3629
3939
  }
3630
3940
  /** The error code of the Bot's latest run; null when that run did not fail. */
3631
3941
  latestErrorCode(botId) {
3632
- const row = this.db.prepare("SELECT error_code AS errorCode FROM bot_runs WHERE bot_id=? ORDER BY created_at DESC,rowid DESC LIMIT 1").get(botId);
3633
- return row?.errorCode ?? null;
3942
+ const row2 = this.db.prepare("SELECT error_code AS errorCode FROM bot_runs WHERE bot_id=? ORDER BY created_at DESC,rowid DESC LIMIT 1").get(botId);
3943
+ return row2?.errorCode ?? null;
3634
3944
  }
3635
- fromRow({ selectionJson, ...row }) {
3636
- return { ...row, selection: JSON.parse(selectionJson) };
3945
+ fromRow({ selectionJson, ...row2 }) {
3946
+ return { ...row2, selection: JSON.parse(selectionJson) };
3637
3947
  }
3638
3948
  inputs(runId) {
3639
3949
  return this.db.prepare("SELECT chat_id AS chatId,message_id AS messageId FROM bot_run_inputs WHERE run_id=? ORDER BY position").all(runId);
@@ -3642,7 +3952,7 @@ var RuntimeRepository = class {
3642
3952
  return !!this.db.prepare("SELECT 1 FROM bot_model_changes WHERE bot_id=? AND state IN ('applying','unknown') LIMIT 1").get(botId);
3643
3953
  }
3644
3954
  beginModelChange(botId, selection) {
3645
- const id = randomUUID11();
3955
+ const id = randomUUID12();
3646
3956
  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());
3647
3957
  return id;
3648
3958
  }
@@ -3681,16 +3991,16 @@ var CredentialRepository = class {
3681
3991
  }
3682
3992
  /** Like `read`, but a value that doesn't open under this installation's key comes back as null, so it can be replaced. */
3683
3993
  current(ownerId, connectionId) {
3684
- const row = this.db.prepare(`SELECT p.credential_json AS json FROM provider_credentials p JOIN provider_connections c ON c.id=p.connection_id
3994
+ const row2 = this.db.prepare(`SELECT p.credential_json AS json FROM provider_credentials p JOIN provider_connections c ON c.id=p.connection_id
3685
3995
  WHERE c.id=? AND c.owner_id=?`).get(connectionId, ownerId);
3686
- if (!row) return null;
3687
- const plain2 = Cipher.sealed(row.json) ? this.cipher.open(row.json, `credential:${connectionId}`) : row.json;
3688
- return { value: plain2 === null ? null : credentialValue(JSON.parse(plain2)), revision: row.json };
3996
+ if (!row2) return null;
3997
+ const plain2 = Cipher.sealed(row2.json) ? this.cipher.open(row2.json, `credential:${connectionId}`) : row2.json;
3998
+ return { value: plain2 === null ? null : credentialValue(JSON.parse(plain2)), revision: row2.json };
3689
3999
  }
3690
4000
  read(ownerId, connectionId) {
3691
- const row = this.current(ownerId, connectionId);
3692
- if (row && !row.value) throw new RuntimeFault("credential_unreadable", "The saved credential can't be decrypted with this installation's master key; enter it again in Settings.");
3693
- return row;
4001
+ const row2 = this.current(ownerId, connectionId);
4002
+ if (row2 && !row2.value) throw new RuntimeFault("credential_unreadable", "The saved credential can't be decrypted with this installation's master key; enter it again in Settings.");
4003
+ return row2;
3694
4004
  }
3695
4005
  replace(ownerId, connectionId, prior, value2) {
3696
4006
  const next = this.stored(connectionId, value2);
@@ -3718,14 +4028,14 @@ var AgentRepository = class {
3718
4028
  /** Brings the agent Bot back when it is missing (deleted before it became undeletable): the same id, the first free
3719
4029
  * name of Jarvy, Jarvy 2, …, no persona and no model, so the owner picks one and is welcomed again. */
3720
4030
  ensure() {
3721
- const row = this.db.prepare("SELECT deleted_at AS deletedAt FROM bots WHERE id=?").get(AGENT_BOT_ID);
3722
- if (row && row.deletedAt === null) return;
4031
+ const row2 = this.db.prepare("SELECT deleted_at AS deletedAt FROM bots WHERE id=?").get(AGENT_BOT_ID);
4032
+ if (row2 && row2.deletedAt === null) return;
3723
4033
  const taken = this.db.prepare(`SELECT 1 FROM account WHERE lower(username)=lower(:name)
3724
4034
  UNION ALL SELECT 1 FROM bots WHERE lower(name)=lower(:name) AND deleted_at IS NULL LIMIT 1`);
3725
4035
  let name3 = "Jarvy";
3726
4036
  for (let n = 2; taken.get({ name: name3 }); n++) name3 = `Jarvy ${n}`;
3727
4037
  transaction(this.db, () => {
3728
- if (row) this.db.prepare(`UPDATE bots SET deleted_at=NULL,name=?,description='',system_prompt='',connection_id=NULL,model_id='',thinking_level='auto'
4038
+ if (row2) this.db.prepare(`UPDATE bots SET deleted_at=NULL,name=?,description='',system_prompt='',connection_id=NULL,model_id='',thinking_level='auto'
3729
4039
  WHERE id=?`).run(name3, AGENT_BOT_ID);
3730
4040
  else {
3731
4041
  this.db.prepare("INSERT OR IGNORE INTO members VALUES(?,'bot')").run(AGENT_BOT_ID);
@@ -3741,18 +4051,18 @@ var AgentRepository = class {
3741
4051
  };
3742
4052
 
3743
4053
  // src/server/infra/storage/environment-repository.ts
3744
- import { randomUUID as randomUUID12 } from "node:crypto";
4054
+ import { randomUUID as randomUUID13 } from "node:crypto";
3745
4055
  var FIELDS = (table) => ["id", "owner_id AS ownerId", "kind", "bot_id AS botId", "name", "directory", "description", "connection"].map((column) => `${table}${column}`).join(", ");
3746
4056
  var COLUMNS = FIELDS("");
3747
- var record = (row) => ({
3748
- id: row.id,
3749
- ownerId: row.ownerId,
3750
- kind: row.kind,
3751
- botId: row.botId,
3752
- name: row.name,
3753
- directory: row.directory,
3754
- description: row.description,
3755
- spec: JSON.parse(row.connection)
4057
+ var record = (row2) => ({
4058
+ id: row2.id,
4059
+ ownerId: row2.ownerId,
4060
+ kind: row2.kind,
4061
+ botId: row2.botId,
4062
+ name: row2.name,
4063
+ directory: row2.directory,
4064
+ description: row2.description,
4065
+ spec: JSON.parse(row2.connection)
3756
4066
  });
3757
4067
  var EnvironmentRepository = class {
3758
4068
  constructor(db, cipher, ownerId) {
@@ -3761,12 +4071,12 @@ var EnvironmentRepository = class {
3761
4071
  this.ownerId = ownerId;
3762
4072
  }
3763
4073
  get(id) {
3764
- const row = this.db.prepare(`SELECT ${COLUMNS} FROM environments WHERE id=? AND deleted_at IS NULL`).get(id);
3765
- return row ? record(row) : null;
4074
+ const row2 = this.db.prepare(`SELECT ${COLUMNS} FROM environments WHERE id=? AND deleted_at IS NULL`).get(id);
4075
+ return row2 ? record(row2) : null;
3766
4076
  }
3767
4077
  computerOf(botId) {
3768
- const row = this.db.prepare(`SELECT ${COLUMNS} FROM environments WHERE bot_id=? AND kind='computer' AND deleted_at IS NULL`).get(botId);
3769
- return row ? record(row) : null;
4078
+ const row2 = this.db.prepare(`SELECT ${COLUMNS} FROM environments WHERE bot_id=? AND kind='computer' AND deleted_at IS NULL`).get(botId);
4079
+ return row2 ? record(row2) : null;
3770
4080
  }
3771
4081
  /** Every live Bot's computer. */
3772
4082
  computers() {
@@ -3777,10 +4087,22 @@ var EnvironmentRepository = class {
3777
4087
  addComputer(botId, container) {
3778
4088
  const existing = this.computerOf(botId);
3779
4089
  if (existing) return existing;
3780
- const id = randomUUID12();
4090
+ const id = randomUUID13();
3781
4091
  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());
3782
4092
  return this.get(id);
3783
4093
  }
4094
+ /** Keeps a computer's limits (A75) with how it is reached: memory in bytes and CPUs in cores, each left out when
4095
+ * there is none. */
4096
+ setLimits(id, limits2) {
4097
+ const computer = this.get(id);
4098
+ if (computer?.kind !== "computer") return;
4099
+ const { memory: _memory, cpus: _cpus, ...spec } = computer.spec;
4100
+ this.db.prepare("UPDATE environments SET connection=? WHERE id=?").run(JSON.stringify({
4101
+ ...spec,
4102
+ ...limits2.memory !== null ? { memory: String(limits2.memory) } : {},
4103
+ ...limits2.cpus !== null ? { cpus: String(limits2.cpus) } : {}
4104
+ }), id);
4105
+ }
3784
4106
  /** The Bot is deleted, and its computer with it. Its row stays its home, deleted, so what reads a deleted Bot's
3785
4107
  * placement still finds one. */
3786
4108
  dropComputer(botId) {
@@ -3794,11 +4116,11 @@ var EnvironmentRepository = class {
3794
4116
  /** The Bot's default environment, its computer, and the one it works in now. Every Bot has its computer (A68):
3795
4117
  * one is made with the Bot, and v29 gave one to every Bot from before. */
3796
4118
  placement(botId) {
3797
- const row = this.db.prepare("SELECT home_environment_id AS home, current_environment_id AS current FROM bots WHERE id=?").get(botId);
3798
- const found = row?.home ? this.db.prepare(`SELECT ${COLUMNS} FROM environments WHERE id=? AND kind='computer'`).get(row.home) : void 0;
4119
+ const row2 = this.db.prepare("SELECT home_environment_id AS home, current_environment_id AS current FROM bots WHERE id=?").get(botId);
4120
+ const found = row2?.home ? this.db.prepare(`SELECT ${COLUMNS} FROM environments WHERE id=? AND kind='computer'`).get(row2.home) : void 0;
3799
4121
  if (!found) throw new Error(`Bot ${botId} has no computer`);
3800
4122
  const home = record(found);
3801
- const current = row?.current && this.get(row.current) || home;
4123
+ const current = row2?.current && this.get(row2.current) || home;
3802
4124
  return { home, current };
3803
4125
  }
3804
4126
  /** Makes an environment the Bot's default and moves the Bot there. */
@@ -3825,7 +4147,7 @@ var EnvironmentRepository = class {
3825
4147
  sshMachines() {
3826
4148
  return this.db.prepare(`SELECT ${COLUMNS} FROM environments WHERE kind='ssh' AND deleted_at IS NULL ORDER BY created_at, id`).all().map(record);
3827
4149
  }
3828
- addSsh(input, id = randomUUID12()) {
4150
+ addSsh(input, id = randomUUID13()) {
3829
4151
  const { host, port, user, home, platform } = input;
3830
4152
  this.db.prepare("INSERT INTO environments(id,owner_id,kind,name,connection,directory,description,key,host_keys,password,created_at) VALUES(?,?,'ssh',?,?,?,?,?,?,?,?)").run(
3831
4153
  id,
@@ -3852,14 +4174,14 @@ var EnvironmentRepository = class {
3852
4174
  /** The machine's private key (null when it doesn't open under this installation's key), its password when it is
3853
4175
  * reached with one (A59; `passwordUnreadable` when it is kept but doesn't open) and the accepted host keys. */
3854
4176
  sshSecrets(id) {
3855
- const row = this.db.prepare("SELECT key, password, host_keys AS hostKeys FROM environments WHERE id=? AND kind='ssh' AND deleted_at IS NULL").get(id);
3856
- if (!row) return null;
3857
- const password = row.password ? this.cipher.open(row.password, `environment-password:${id}`) : null;
4177
+ const row2 = this.db.prepare("SELECT key, password, host_keys AS hostKeys FROM environments WHERE id=? AND kind='ssh' AND deleted_at IS NULL").get(id);
4178
+ if (!row2) return null;
4179
+ const password = row2.password ? this.cipher.open(row2.password, `environment-password:${id}`) : null;
3858
4180
  return {
3859
- privateKey: row.key ? this.cipher.open(row.key, `environment:${id}`) : null,
4181
+ privateKey: row2.key ? this.cipher.open(row2.key, `environment:${id}`) : null,
3860
4182
  password,
3861
- passwordUnreadable: !!row.password && password === null,
3862
- hostKeys: row.hostKeys ? JSON.parse(row.hostKeys) : { keys: [], fingerprints: [] }
4183
+ passwordUnreadable: !!row2.password && password === null,
4184
+ hostKeys: row2.hostKeys ? JSON.parse(row2.hostKeys) : { keys: [], fingerprints: [] }
3863
4185
  };
3864
4186
  }
3865
4187
  /** Whether a machine is reached with its password rather than the key (A59). */
@@ -3868,12 +4190,12 @@ var EnvironmentRepository = class {
3868
4190
  }
3869
4191
  /** The Bots that may use an environment. */
3870
4192
  grantees(environmentId) {
3871
- return this.db.prepare("SELECT g.bot_id AS botId FROM environment_grants g JOIN bots b ON b.id=g.bot_id WHERE g.environment_id=? AND b.deleted_at IS NULL ORDER BY g.granted_at").all(environmentId).map((row) => row.botId);
4193
+ return this.db.prepare("SELECT g.bot_id AS botId FROM environment_grants g JOIN bots b ON b.id=g.bot_id WHERE g.environment_id=? AND b.deleted_at IS NULL ORDER BY g.granted_at").all(environmentId).map((row2) => row2.botId);
3872
4194
  }
3873
4195
  /** Deletes an SSH machine with its key: Bots working there are back in their default. Returns those Bots. */
3874
4196
  dropSsh(id) {
3875
4197
  return transaction(this.db, () => {
3876
- const moved = this.db.prepare("SELECT id FROM bots WHERE current_environment_id=?").all(id).map((row) => row.id);
4198
+ const moved = this.db.prepare("SELECT id FROM bots WHERE current_environment_id=?").all(id).map((row2) => row2.id);
3877
4199
  this.db.prepare("UPDATE bots SET current_environment_id=home_environment_id WHERE current_environment_id=?").run(id);
3878
4200
  this.db.prepare("DELETE FROM environment_grants WHERE environment_id=?").run(id);
3879
4201
  this.db.prepare("UPDATE environments SET deleted_at=?, key=NULL, password=NULL WHERE id=? AND kind='ssh'").run(Date.now(), id);
@@ -3888,8 +4210,8 @@ var SettingsRepository = class {
3888
4210
  this.db = db;
3889
4211
  }
3890
4212
  get(key) {
3891
- const row = this.db.prepare("SELECT value FROM settings WHERE key=?").get(key);
3892
- return row?.value ?? null;
4213
+ const row2 = this.db.prepare("SELECT value FROM settings WHERE key=?").get(key);
4214
+ return row2?.value ?? null;
3893
4215
  }
3894
4216
  set(key, value2) {
3895
4217
  if (value2 === null) this.db.prepare("DELETE FROM settings WHERE key=?").run(key);
@@ -3898,7 +4220,7 @@ var SettingsRepository = class {
3898
4220
  };
3899
4221
 
3900
4222
  // src/server/infra/storage/secret-repository.ts
3901
- import { randomUUID as randomUUID13 } from "node:crypto";
4223
+ import { randomUUID as randomUUID14 } from "node:crypto";
3902
4224
 
3903
4225
  // src/server/shared/secret.ts
3904
4226
  var SECRET_NAME = /^[A-Z][A-Z0-9_]{0,63}$/;
@@ -3913,19 +4235,19 @@ var SecretRepository = class {
3913
4235
  this.cipher = cipher;
3914
4236
  this.ownerId = ownerId;
3915
4237
  }
3916
- toRecord(row) {
4238
+ toRecord(row2) {
3917
4239
  const grants = this.db.prepare(`SELECT bot_id AS botId, scope, granted_at AS grantedAt FROM secret_grants g WHERE secret_id=?
3918
- AND (scope='always' OR EXISTS(SELECT 1 FROM bot_runs r WHERE r.id=g.run_id AND r.ended_at IS NULL)) ORDER BY granted_at`).all(row.id).map((grant) => ({ ...grant }));
3919
- const { value: value2, ...rest } = row;
3920
- return { ...rest, readable: this.cipher.open(value2, `secret:${row.id}`) !== null, grants };
4240
+ AND (scope='always' OR EXISTS(SELECT 1 FROM bot_runs r WHERE r.id=g.run_id AND r.ended_at IS NULL)) ORDER BY granted_at`).all(row2.id).map((grant) => ({ ...grant }));
4241
+ const { value: value2, ...rest } = row2;
4242
+ return { ...rest, readable: this.cipher.open(value2, `secret:${row2.id}`) !== null, grants };
3921
4243
  }
3922
4244
  list() {
3923
4245
  this.purge();
3924
- return this.db.prepare(`SELECT ${COLUMNS2} FROM secrets s WHERE s.owner_id=? ORDER BY s.kind, lower(s.name)`).all(this.ownerId()).map((row) => this.toRecord(row));
4246
+ return this.db.prepare(`SELECT ${COLUMNS2} FROM secrets s WHERE s.owner_id=? ORDER BY s.kind, lower(s.name)`).all(this.ownerId()).map((row2) => this.toRecord(row2));
3925
4247
  }
3926
4248
  get(id) {
3927
- const row = this.db.prepare(`SELECT ${COLUMNS2} FROM secrets s WHERE s.id=? AND s.owner_id=?`).get(id, this.ownerId());
3928
- return row ? this.toRecord(row) : null;
4249
+ const row2 = this.db.prepare(`SELECT ${COLUMNS2} FROM secrets s WHERE s.id=? AND s.owner_id=?`).get(id, this.ownerId());
4250
+ return row2 ? this.toRecord(row2) : null;
3929
4251
  }
3930
4252
  checkValue(value2) {
3931
4253
  if (typeof value2 !== "string" || !value2) throw new ValidationError("A secret needs a value", "secret_value_missing");
@@ -3941,7 +4263,7 @@ var SecretRepository = class {
3941
4263
  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");
3942
4264
  const description = this.checkDescription(input.description), value2 = this.checkValue(input.value);
3943
4265
  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");
3944
- const id = randomUUID13(), now = Date.now();
4266
+ const id = randomUUID14(), now = Date.now();
3945
4267
  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);
3946
4268
  return this.get(id);
3947
4269
  }
@@ -3965,27 +4287,27 @@ var SecretRepository = class {
3965
4287
  revoke(id, botId) {
3966
4288
  return Number(this.db.prepare("DELETE FROM secret_grants WHERE secret_id=? AND bot_id=?").run(id, botId).changes) > 0;
3967
4289
  }
3968
- uses(id, limit = 100) {
4290
+ uses(id, limit2 = 100) {
3969
4291
  return this.db.prepare(`SELECT id, secret_name AS secretName, bot_id AS botId, at, how, environment_id AS environmentId, history_id AS historyId,
3970
- tool_call_id AS toolCallId, detail FROM secret_uses WHERE secret_id=? ORDER BY at DESC, id DESC LIMIT ?`).all(id, limit).map((use) => ({ ...use }));
4292
+ tool_call_id AS toolCallId, detail FROM secret_uses WHERE secret_id=? ORDER BY at DESC, id DESC LIMIT ?`).all(id, limit2).map((use) => ({ ...use }));
3971
4293
  }
3972
4294
  /** Every value the owner keeps that opens, for masking. */
3973
4295
  values() {
3974
- return this.db.prepare("SELECT id, name, value FROM secrets WHERE owner_id=? AND kind='value'").all(this.ownerId()).map((row) => ({ name: row.name, value: this.cipher.open(row.value, `secret:${row.id}`) })).filter((row) => row.value !== null);
4296
+ return this.db.prepare("SELECT id, name, value FROM secrets WHERE owner_id=? AND kind='value'").all(this.ownerId()).map((row2) => ({ name: row2.name, value: this.cipher.open(row2.value, `secret:${row2.id}`) })).filter((row2) => row2.value !== null);
3975
4297
  }
3976
4298
  /** The secrets a Bot may use: granted until revoked, or for the run that is still going. */
3977
4299
  grantedTo(botId) {
3978
4300
  return this.db.prepare(`SELECT s.id, s.name, s.description, g.scope FROM secrets s JOIN secret_grants g ON g.secret_id=s.id
3979
4301
  WHERE g.bot_id=? AND s.owner_id=? AND s.kind='value' AND (g.scope='always' OR EXISTS(SELECT 1 FROM bot_runs r WHERE r.id=g.run_id AND r.ended_at IS NULL))
3980
- ORDER BY s.name`).all(botId, this.ownerId()).map((row) => ({ ...row }));
4302
+ ORDER BY s.name`).all(botId, this.ownerId()).map((row2) => ({ ...row2 }));
3981
4303
  }
3982
4304
  /** The Bot's run in progress, which a "this time only" grant lasts for. */
3983
4305
  currentRun(botId) {
3984
4306
  return this.db.prepare("SELECT id FROM bot_runs WHERE bot_id=? AND ended_at IS NULL ORDER BY created_at DESC LIMIT 1").get(botId)?.id ?? null;
3985
4307
  }
3986
4308
  byName(name3) {
3987
- const row = this.db.prepare("SELECT id FROM secrets WHERE owner_id=? AND kind='value' AND name=?").get(this.ownerId(), name3);
3988
- return row ? this.get(row.id) : null;
4309
+ const row2 = this.db.prepare("SELECT id FROM secrets WHERE owner_id=? AND kind='value' AND name=?").get(this.ownerId(), name3);
4310
+ return row2 ? this.get(row2.id) : null;
3989
4311
  }
3990
4312
  /** "This time only" grants whose run has ended. */
3991
4313
  purge() {
@@ -3996,13 +4318,13 @@ var SecretRepository = class {
3996
4318
  }
3997
4319
  /** The plain value, for a command that uses it; null when it does not open. */
3998
4320
  value(id) {
3999
- const row = this.db.prepare("SELECT value FROM secrets WHERE id=?").get(id);
4000
- return row ? this.cipher.open(row.value, `secret:${id}`) : null;
4321
+ const row2 = this.db.prepare("SELECT value FROM secrets WHERE id=?").get(id);
4322
+ return row2 ? this.cipher.open(row2.value, `secret:${id}`) : null;
4001
4323
  }
4002
4324
  };
4003
4325
 
4004
4326
  // src/server/infra/storage/task-repository.ts
4005
- import { randomUUID as randomUUID14 } from "node:crypto";
4327
+ import { randomUUID as randomUUID15 } from "node:crypto";
4006
4328
 
4007
4329
  // src/server/shared/task.ts
4008
4330
  var TASK_MINUTES_MIN = 5;
@@ -4043,30 +4365,30 @@ function localIso(time2) {
4043
4365
  var COLUMNS3 = `id,bot_id,name,instruction,schedule_kind,schedule_at,schedule_minutes,schedule_hour,schedule_minute,schedule_weekday,
4044
4366
  state,next_run_at,last_run_at,created_at,updated_at`;
4045
4367
  var LIMIT = 200;
4046
- function scheduleOf(row) {
4047
- switch (row.schedule_kind) {
4368
+ function scheduleOf(row2) {
4369
+ switch (row2.schedule_kind) {
4048
4370
  case "once":
4049
- return { kind: "once", at: row.schedule_at };
4371
+ return { kind: "once", at: row2.schedule_at };
4050
4372
  case "every":
4051
- return { kind: "every", minutes: row.schedule_minutes, since: row.schedule_at };
4373
+ return { kind: "every", minutes: row2.schedule_minutes, since: row2.schedule_at };
4052
4374
  case "daily":
4053
- return { kind: "daily", hour: row.schedule_hour, minute: row.schedule_minute };
4375
+ return { kind: "daily", hour: row2.schedule_hour, minute: row2.schedule_minute };
4054
4376
  case "weekly":
4055
- return { kind: "weekly", weekday: row.schedule_weekday, hour: row.schedule_hour, minute: row.schedule_minute };
4377
+ return { kind: "weekly", weekday: row2.schedule_weekday, hour: row2.schedule_hour, minute: row2.schedule_minute };
4056
4378
  }
4057
4379
  }
4058
- function toTask(row) {
4380
+ function toTask(row2) {
4059
4381
  return {
4060
- id: row.id,
4061
- botId: row.bot_id,
4062
- name: row.name,
4063
- instruction: row.instruction,
4064
- schedule: scheduleOf(row),
4065
- state: row.state,
4066
- nextRunAt: row.next_run_at,
4067
- lastRunAt: row.last_run_at,
4068
- createdAt: row.created_at,
4069
- updatedAt: row.updated_at
4382
+ id: row2.id,
4383
+ botId: row2.bot_id,
4384
+ name: row2.name,
4385
+ instruction: row2.instruction,
4386
+ schedule: scheduleOf(row2),
4387
+ state: row2.state,
4388
+ nextRunAt: row2.next_run_at,
4389
+ lastRunAt: row2.last_run_at,
4390
+ createdAt: row2.created_at,
4391
+ updatedAt: row2.updated_at
4070
4392
  };
4071
4393
  }
4072
4394
  function scheduleColumns(schedule) {
@@ -4091,13 +4413,13 @@ var TaskRepository = class {
4091
4413
  return rows3.map(toTask);
4092
4414
  }
4093
4415
  get(botId, id) {
4094
- const row = this.db.prepare(`SELECT ${COLUMNS3} FROM tasks WHERE id=? AND bot_id=? AND deleted_at IS NULL`).get(id, botId);
4095
- return row ? toTask(row) : null;
4416
+ const row2 = this.db.prepare(`SELECT ${COLUMNS3} FROM tasks WHERE id=? AND bot_id=? AND deleted_at IS NULL`).get(id, botId);
4417
+ return row2 ? toTask(row2) : null;
4096
4418
  }
4097
4419
  create(input) {
4098
4420
  const now = Date.now(), at2 = nextAfter(input.schedule, now);
4099
4421
  if (at2 === null) throw new ValidationError("A one-off task has to be in the future", "task_time_past");
4100
- const id = randomUUID14(), columns = scheduleColumns(input.schedule);
4422
+ const id = randomUUID15(), columns = scheduleColumns(input.schedule);
4101
4423
  this.db.prepare(`INSERT INTO tasks(id,bot_id,name,instruction,schedule_kind,schedule_at,schedule_minutes,schedule_hour,schedule_minute,schedule_weekday,
4102
4424
  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);
4103
4425
  return this.get(input.botId, id);
@@ -4149,8 +4471,8 @@ var TaskRepository = class {
4149
4471
  }
4150
4472
  /** The soonest time any task runs; the scheduler sleeps until then. Null when nothing is scheduled. */
4151
4473
  soonest() {
4152
- const row = this.db.prepare("SELECT min(next_run_at) AS at FROM tasks WHERE deleted_at IS NULL AND state='active' AND next_run_at IS NOT NULL").get();
4153
- return row?.at ?? null;
4474
+ const row2 = this.db.prepare("SELECT min(next_run_at) AS at FROM tasks WHERE deleted_at IS NULL AND state='active' AND next_run_at IS NOT NULL").get();
4475
+ return row2?.at ?? null;
4154
4476
  }
4155
4477
  due(now) {
4156
4478
  return this.db.prepare(`SELECT ${COLUMNS3.replace(/(\w+)/g, "t.$1")} FROM tasks t JOIN bots b ON b.id=t.bot_id
@@ -4164,18 +4486,18 @@ var TaskRepository = class {
4164
4486
  if (!due.length) return [];
4165
4487
  return transaction(this.db, () => {
4166
4488
  const fired = [];
4167
- for (const row of due) {
4168
- const at2 = row.next_run_at, next = nextAfter(scheduleOf(row), now);
4169
- this.db.prepare("UPDATE tasks SET last_run_at=?,next_run_at=?,state=?,updated_at=? WHERE id=?").run(at2, next, next === null ? "done" : "active", now, row.id);
4489
+ for (const row2 of due) {
4490
+ const at2 = row2.next_run_at, next = nextAfter(scheduleOf(row2), now);
4491
+ this.db.prepare("UPDATE tasks SET last_run_at=?,next_run_at=?,state=?,updated_at=? WHERE id=?").run(at2, next, next === null ? "done" : "active", now, row2.id);
4170
4492
  this.inbox.write([{
4171
- delivery: { member: { id: row.bot_id }, reason: "schedule", kind: "task", urgency },
4493
+ delivery: { member: { id: row2.bot_id }, reason: "schedule", kind: "task", urgency },
4172
4494
  chatId: null,
4173
4495
  messageId: null,
4174
4496
  actorId: null,
4175
- payload: { task: row.id, due: at2 },
4497
+ payload: { task: row2.id, due: at2 },
4176
4498
  createdAt: now
4177
4499
  }]);
4178
- fired.push({ taskId: row.id, botId: row.bot_id, at: at2 });
4500
+ fired.push({ taskId: row2.id, botId: row2.bot_id, at: at2 });
4179
4501
  }
4180
4502
  return fired;
4181
4503
  });
@@ -4184,14 +4506,70 @@ var TaskRepository = class {
4184
4506
  * past is done without running. Returns how many were skipped. */
4185
4507
  skipOverdue(now = Date.now()) {
4186
4508
  const rows3 = this.db.prepare(`SELECT ${COLUMNS3} FROM tasks WHERE deleted_at IS NULL AND state='active' AND next_run_at<=?`).all(now);
4187
- for (const row of rows3) {
4188
- const next = nextAfter(scheduleOf(row), now);
4189
- this.db.prepare("UPDATE tasks SET next_run_at=?,state=?,updated_at=? WHERE id=?").run(next, next === null ? "done" : "active", now, row.id);
4509
+ for (const row2 of rows3) {
4510
+ const next = nextAfter(scheduleOf(row2), now);
4511
+ this.db.prepare("UPDATE tasks SET next_run_at=?,state=?,updated_at=? WHERE id=?").run(next, next === null ? "done" : "active", now, row2.id);
4190
4512
  }
4191
4513
  return rows3.length;
4192
4514
  }
4193
4515
  };
4194
4516
 
4517
+ // src/server/infra/storage/port-repository.ts
4518
+ var COLUMNS4 = "bot_id,port,name,description,host_port,created_at,updated_at";
4519
+ var toRecord = (row2) => ({
4520
+ botId: row2.bot_id,
4521
+ port: row2.port,
4522
+ name: row2.name,
4523
+ description: row2.description,
4524
+ hostPort: row2.host_port,
4525
+ createdAt: row2.created_at,
4526
+ updatedAt: row2.updated_at
4527
+ });
4528
+ var PortRepository = class {
4529
+ constructor(db) {
4530
+ this.db = db;
4531
+ }
4532
+ /** Every published port, of every Bot that still exists, by Jarvy's port. */
4533
+ all() {
4534
+ return this.db.prepare(`SELECT ${COLUMNS4} FROM published_ports p WHERE EXISTS(SELECT 1 FROM bots b WHERE b.id=p.bot_id AND b.deleted_at IS NULL)
4535
+ ORDER BY host_port`).all().map(toRecord);
4536
+ }
4537
+ /** A Bot's published ports, the first published first. */
4538
+ list(botId) {
4539
+ return this.db.prepare(`SELECT ${COLUMNS4} FROM published_ports WHERE bot_id=? ORDER BY created_at, port`).all(botId).map(toRecord);
4540
+ }
4541
+ get(botId, port) {
4542
+ const row2 = this.db.prepare(`SELECT ${COLUMNS4} FROM published_ports WHERE bot_id=? AND port=?`).get(botId, port);
4543
+ return row2 ? toRecord(row2) : null;
4544
+ }
4545
+ byHostPort(hostPort) {
4546
+ const row2 = this.db.prepare(`SELECT ${COLUMNS4} FROM published_ports WHERE host_port=?`).get(hostPort);
4547
+ return row2 ? toRecord(row2) : null;
4548
+ }
4549
+ /** Jarvy's ports already taken, for picking a free one. */
4550
+ hostPorts() {
4551
+ return new Set(this.db.prepare("SELECT host_port FROM published_ports").all().map((row2) => row2.host_port));
4552
+ }
4553
+ /** Publishes a port on `hostPort`, or renames one already published, which keeps its own Jarvy port. */
4554
+ save(input) {
4555
+ const now = Date.now();
4556
+ this.db.prepare(`INSERT INTO published_ports(${COLUMNS4}) VALUES(?,?,?,?,?,?,?)
4557
+ ON CONFLICT(bot_id, port) DO UPDATE SET name=excluded.name, description=excluded.description, updated_at=excluded.updated_at`).run(input.botId, input.port, input.name, input.description, input.hostPort, now, now);
4558
+ return this.get(input.botId, input.port);
4559
+ }
4560
+ /** Moves a published port to another Jarvy port, when its own was taken by another program meanwhile. */
4561
+ move(botId, port, hostPort) {
4562
+ this.db.prepare("UPDATE published_ports SET host_port=?, updated_at=? WHERE bot_id=? AND port=?").run(hostPort, Date.now(), botId, port);
4563
+ }
4564
+ remove(botId, port) {
4565
+ return Number(this.db.prepare("DELETE FROM published_ports WHERE bot_id=? AND port=?").run(botId, port).changes) > 0;
4566
+ }
4567
+ /** Unpublishes all of a Bot's ports, as when its sandbox is reset. */
4568
+ removeAll(botId) {
4569
+ return Number(this.db.prepare("DELETE FROM published_ports WHERE bot_id=?").run(botId).changes);
4570
+ }
4571
+ };
4572
+
4195
4573
  // src/server/infra/vault/master-key.ts
4196
4574
  import { randomBytes as randomBytes3 } from "node:crypto";
4197
4575
  import { chmodSync, existsSync, mkdirSync as mkdirSync3, readFileSync, writeFileSync } from "node:fs";
@@ -4241,6 +4619,8 @@ var Store = class {
4241
4619
  settings;
4242
4620
  /** Scheduled tasks (A58). */
4243
4621
  tasks;
4622
+ /** Ports Bots published from their sandboxes (A74). */
4623
+ ports;
4244
4624
  secrets;
4245
4625
  /** Seals secrets and model credentials under the master key (A52). */
4246
4626
  cipher;
@@ -4269,6 +4649,7 @@ var Store = class {
4269
4649
  this.environments = new EnvironmentRepository(this.db, this.cipher, ownerId);
4270
4650
  this.settings = new SettingsRepository(this.db);
4271
4651
  this.tasks = new TaskRepository(this.db, this.chats.inbox);
4652
+ this.ports = new PortRepository(this.db);
4272
4653
  this.secrets = new SecretRepository(this.db, this.cipher, ownerId);
4273
4654
  this.agent.ensure();
4274
4655
  }
@@ -4300,10 +4681,10 @@ var set = (...items) => tlv(49, Buffer.concat(items));
4300
4681
  var integer2 = (bytes) => tlv(2, bytes[0] & 128 ? Buffer.concat([Buffer.from([0]), bytes]) : bytes);
4301
4682
  var bits = (bytes, unused = 0) => tlv(3, Buffer.concat([Buffer.from([unused]), bytes]));
4302
4683
  var octets = (bytes) => tlv(4, bytes);
4303
- var utf8 = (text4) => tlv(12, Buffer.from(text4, "utf8"));
4684
+ var utf8 = (text5) => tlv(12, Buffer.from(text5, "utf8"));
4304
4685
  var TRUE = tlv(1, Buffer.from([255]));
4305
- function oid(text4) {
4306
- const [a, b, ...rest] = text4.split(".").map(Number);
4686
+ function oid(text5) {
4687
+ const [a, b, ...rest] = text5.split(".").map(Number);
4307
4688
  const out = [a * 40 + b];
4308
4689
  for (const value2 of rest) {
4309
4690
  const chunk = [value2 & 127];
@@ -4405,8 +4786,8 @@ function read2(dir, file) {
4405
4786
  const path = join7(dir, file);
4406
4787
  return existsSync2(path) ? readFileSync2(path, "utf8") : null;
4407
4788
  }
4408
- function write(dir, file, text4, secret) {
4409
- writeFileSync2(join7(dir, file), text4, { mode: secret ? 384 : 420 });
4789
+ function write(dir, file, text5, secret) {
4790
+ writeFileSync2(join7(dir, file), text5, { mode: secret ? 384 : 420 });
4410
4791
  }
4411
4792
  function readKey(dir, name3, cipher, log) {
4412
4793
  const plain2 = read2(dir, `${name3}.pem`);
@@ -4490,7 +4871,7 @@ var AccountService = class {
4490
4871
 
4491
4872
  // src/server/infra/pi/model.ts
4492
4873
  import { ModelRuntime } from "@earendil-works/pi-coding-agent";
4493
- var FORMATS = /* @__PURE__ */ new Set(["openai", "openrouter", "deepseek", "together", "baseten", "zai", "qwen", "chat-template", "qwen-chat-template", "string-thinking", "ant-ling"]);
4874
+ var FORMATS2 = /* @__PURE__ */ new Set(["openai", "openrouter", "deepseek", "together", "baseten", "zai", "qwen", "chat-template", "qwen-chat-template", "string-thinking", "ant-ling"]);
4494
4875
  function installModel(runtime, connection, configured2, headers2 = {}, catalog) {
4495
4876
  const api = configured2.api || connection.api;
4496
4877
  const bundled = runtime.getModel(connection.providerName, configured2.id);
@@ -4500,7 +4881,7 @@ function installModel(runtime, connection, configured2, headers2 = {}, catalog)
4500
4881
  const maxTokens = configured2.maxTokens ?? builtin?.maxTokens;
4501
4882
  const baseUrl = connection.baseUrl || configured2.baseUrl || builtin?.baseUrl || runtime.getModels(connection.providerName)[0]?.baseUrl;
4502
4883
  if (!contextWindow || !maxTokens) throw new RuntimeFault("model_incomplete", "Fill in the model's context window and max output.", 422);
4503
- if (configured2.thinkingFormat && !FORMATS.has(configured2.thinkingFormat)) throw new RuntimeFault("thinking_format", "This thinking format isn't supported.", 422);
4884
+ if (configured2.thinkingFormat && !FORMATS2.has(configured2.thinkingFormat)) throw new RuntimeFault("thinking_format", "This thinking format isn't supported.", 422);
4504
4885
  const compat = { ...builtin?.compat, ...configured2.compat, ...configured2.thinkingFormat ? { thinkingFormat: configured2.thinkingFormat } : {} };
4505
4886
  runtime.registerProvider(connection.providerName, {
4506
4887
  baseUrl,
@@ -4605,14 +4986,14 @@ var ConnectionCredentials = class {
4605
4986
  async read(providerId, options) {
4606
4987
  options?.signal?.throwIfAborted();
4607
4988
  if (providerId !== this.providerName) return void 0;
4608
- const row = this.repository.read(this.ownerId, this.connectionId);
4609
- if (!row) throw new RuntimeFault("credentials_missing", "Set up this connection's credential first.", 422);
4610
- return row.value;
4989
+ const row2 = this.repository.read(this.ownerId, this.connectionId);
4990
+ if (!row2) throw new RuntimeFault("credentials_missing", "Set up this connection's credential first.", 422);
4991
+ return row2.value;
4611
4992
  }
4612
4993
  async list(options) {
4613
4994
  options?.signal?.throwIfAborted();
4614
- const row = this.repository.current(this.ownerId, this.connectionId);
4615
- return row?.value ? [{ providerId: this.providerName, type: row.value.type }] : [];
4995
+ const row2 = this.repository.current(this.ownerId, this.connectionId);
4996
+ return row2?.value ? [{ providerId: this.providerName, type: row2.value.type }] : [];
4616
4997
  }
4617
4998
  async modify(providerId, fn, options) {
4618
4999
  if (providerId !== this.providerName) throw new RuntimeFault("credential_scope", "The credential belongs to another connection.");
@@ -4685,17 +5066,17 @@ var strip = (id) => id.toLowerCase().replace(/^.*\//, "").replace(/-(\d{6,8})$/,
4685
5066
  function rows(providers) {
4686
5067
  return providers.flatMap((provider) => provider.getModels().map((model) => ({ provider: provider.id, model, official: officialScore(provider.id, model.id) })));
4687
5068
  }
4688
- var source = (row) => ({ provider: row.provider, modelId: row.model.id, name: row.model.name, api: row.model.api, official: row.official === 2 });
4689
- var completeness = (row) => Object.values(row.model.thinkingLevelMap ?? {}).filter((value2) => value2 !== void 0).length + Object.keys(row.model.compat ?? {}).length + (row.model.cost.input > 0 ? 1 : 0);
5069
+ var source = (row2) => ({ provider: row2.provider, modelId: row2.model.id, name: row2.model.name, api: row2.model.api, official: row2.official === 2 });
5070
+ var completeness = (row2) => Object.values(row2.model.thinkingLevelMap ?? {}).filter((value2) => value2 !== void 0).length + Object.keys(row2.model.compat ?? {}).length + (row2.model.cost.input > 0 ? 1 : 0);
4690
5071
  function candidates(all, query) {
4691
5072
  const q = query.trim().toLowerCase();
4692
5073
  if (!q) return [];
4693
- const same3 = all.filter((row) => row.model.id.toLowerCase() === q || strip(row.model.id) === strip(q));
4694
- return same3.length ? same3 : all.filter((row) => row.model.id.toLowerCase().includes(strip(q)));
5074
+ const same3 = all.filter((row2) => row2.model.id.toLowerCase() === q || strip(row2.model.id) === strip(q));
5075
+ return same3.length ? same3 : all.filter((row2) => row2.model.id.toLowerCase().includes(strip(q)));
4695
5076
  }
4696
5077
  function similar(all, query) {
4697
5078
  const q = strip(query);
4698
- return all.map((row) => ({ row, ...similarity(strip(row.model.id), q) })).filter((item) => item.prefix >= 3).sort((a, b) => b.score - a.score || b.row.official - a.row.official).slice(0, 5).map((item) => item.row);
5079
+ return all.map((row2) => ({ row: row2, ...similarity(strip(row2.model.id), q) })).filter((item) => item.prefix >= 3).sort((a, b) => b.score - a.score || b.row.official - a.row.official).slice(0, 5).map((item) => item.row);
4699
5080
  }
4700
5081
  function similarity(a, b) {
4701
5082
  let prefix = 0;
@@ -4711,12 +5092,12 @@ function levenshtein(a, b) {
4711
5092
  }
4712
5093
  return previous[b.length];
4713
5094
  }
4714
- function matched(row, api, alternatives, reseller) {
4715
- const model = publicModel(row.model);
4716
- const sameApi = row.model.api === api;
5095
+ function matched(row2, api, alternatives, reseller) {
5096
+ const model = publicModel(row2.model);
5097
+ const sameApi = row2.model.api === api;
4717
5098
  return {
4718
5099
  status: "matched",
4719
- source: source(row),
5100
+ source: source(row2),
4720
5101
  reseller,
4721
5102
  apiMismatch: !sameApi,
4722
5103
  fields: {
@@ -4727,28 +5108,28 @@ function matched(row, api, alternatives, reseller) {
4727
5108
  cost: model.cost,
4728
5109
  thinkingLevelMap: model.thinkingLevelMap,
4729
5110
  thinkingFormat: sameApi ? model.thinkingFormat : "",
4730
- compat: sameApi ? catalogCompat(row.model) : {}
5111
+ compat: sameApi ? catalogCompat(row2.model) : {}
4731
5112
  },
4732
- alternatives: alternatives.filter((item) => item !== row).slice(0, 8).map(source)
5113
+ alternatives: alternatives.filter((item) => item !== row2).slice(0, 8).map(source)
4733
5114
  };
4734
5115
  }
4735
5116
  function autoconfigure(providers, modelId, api, choice2) {
4736
5117
  const all = rows(providers), hits = candidates(all, modelId);
4737
5118
  if (choice2) {
4738
- const row = all.find((item) => item.provider === choice2.provider && item.model.id === choice2.modelId);
4739
- if (row) return matched(row, api, hits, row.official < 2);
5119
+ const row2 = all.find((item) => item.provider === choice2.provider && item.model.id === choice2.modelId);
5120
+ if (row2) return matched(row2, api, hits, row2.official < 2);
4740
5121
  }
4741
5122
  if (!hits.length) {
4742
5123
  const options = similar(all, modelId);
4743
5124
  return options.length ? { status: "choose", reason: "similar", options: options.map(source) } : { status: "none" };
4744
5125
  }
4745
- const official = hits.filter((row) => row.official === 2);
5126
+ const official = hits.filter((row2) => row2.official === 2);
4746
5127
  const ranked = [...hits].sort((a, b) => b.official - a.official || completeness(b) - completeness(a));
4747
5128
  if (official.length === 1) return matched(official[0], api, ranked, false);
4748
5129
  if (official.length > 1) {
4749
5130
  const words = vendorWords(official[0].model.id);
4750
- const exact = official.filter((row) => words.includes(row.provider));
4751
- const sameApi = official.filter((row) => row.model.api === api);
5131
+ const exact = official.filter((row2) => words.includes(row2.provider));
5132
+ const sameApi = official.filter((row2) => row2.model.api === api);
4752
5133
  const winner = exact.length === 1 ? exact[0] : sameApi.length === 1 ? sameApi[0] : void 0;
4753
5134
  if (winner) return matched(winner, api, ranked, false);
4754
5135
  return { status: "choose", reason: "multiple", options: official.sort((a, b) => a.provider.length - b.provider.length).map(source) };
@@ -4919,13 +5300,13 @@ var ModelService = class {
4919
5300
  }
4920
5301
  /** Suggests model fields from Pi's catalog (with pi.dev's newer entries, A49); the caller decides whether to save them. */
4921
5302
  autoconfigure(input) {
4922
- const text4 = (value2) => typeof value2 === "string" && value2.trim() && value2.length <= 200 ? value2.trim() : null;
4923
- const modelId = text4(input.modelId), api = text4(input.api);
5303
+ const text5 = (value2) => typeof value2 === "string" && value2.trim() && value2.length <= 200 ? value2.trim() : null;
5304
+ const modelId = text5(input.modelId), api = text5(input.api);
4924
5305
  if (!modelId || !api) throw new RuntimeFault("autoconfig_model", "Enter the model ID.", 400);
4925
5306
  let choice2;
4926
5307
  if (input.choice !== void 0) {
4927
5308
  const raw = input.choice;
4928
- const provider = text4(raw?.provider), chosen = text4(raw?.modelId);
5309
+ const provider = text5(raw?.provider), chosen = text5(raw?.modelId);
4929
5310
  if (!provider || !chosen) throw new RuntimeFault("autoconfig_source", "The setup source isn't valid.", 400);
4930
5311
  choice2 = { provider, modelId: chosen };
4931
5312
  }
@@ -4956,7 +5337,7 @@ var ModelService = class {
4956
5337
  };
4957
5338
 
4958
5339
  // src/server/infra/pi/auth.ts
4959
- import { randomUUID as randomUUID15 } from "node:crypto";
5340
+ import { randomUUID as randomUUID16 } from "node:crypto";
4960
5341
  import { ModelRuntime as ModelRuntime2 } from "@earendil-works/pi-coding-agent";
4961
5342
  var CredentialLogins = class {
4962
5343
  constructor(store, credentials, changed) {
@@ -5004,7 +5385,7 @@ var CredentialLogins = class {
5004
5385
  prompt(flow, prompt) {
5005
5386
  const signal = prompt.signal ? AbortSignal.any([flow.controller.signal, prompt.signal]) : flow.controller.signal;
5006
5387
  signal.throwIfAborted();
5007
- const id = randomUUID15();
5388
+ const id = randomUUID16();
5008
5389
  flow.state.prompt = {
5009
5390
  id,
5010
5391
  type: prompt.type,
@@ -5089,14 +5470,14 @@ import lockfile from "proper-lockfile";
5089
5470
  // src/contract/mentions.ts
5090
5471
  var WORD = /[\p{Script=Latin}\p{Nd}_]/u;
5091
5472
  function findMentions(source2, candidates2) {
5092
- const text4 = source2.replace(/```[\s\S]*?```|`[^`\n]*`/g, (code) => " ".repeat(code.length));
5093
- const lower = text4.toLowerCase(), sorted = [...candidates2].sort((a, b) => b.name.length - a.name.length), found = [];
5473
+ const text5 = source2.replace(/```[\s\S]*?```|`[^`\n]*`/g, (code) => " ".repeat(code.length));
5474
+ const lower = text5.toLowerCase(), sorted = [...candidates2].sort((a, b) => b.name.length - a.name.length), found = [];
5094
5475
  for (let at2 = lower.indexOf("@"); at2 >= 0; at2 = lower.indexOf("@", at2 + 1)) {
5095
- if (at2 > 0 && WORD.test(text4[at2 - 1])) continue;
5476
+ if (at2 > 0 && WORD.test(text5[at2 - 1])) continue;
5096
5477
  const member = sorted.find((candidate) => {
5097
5478
  const name3 = candidate.name.toLowerCase();
5098
5479
  if (!name3 || !lower.startsWith(name3, at2 + 1)) return false;
5099
- const next = text4[at2 + 1 + name3.length];
5480
+ const next = text5[at2 + 1 + name3.length];
5100
5481
  return !(next && WORD.test(next) && WORD.test(name3.at(-1)));
5101
5482
  });
5102
5483
  if (member) {
@@ -5137,8 +5518,9 @@ var POLICY = {
5137
5518
  message: { dm: "interrupt", mention: "interrupt", reply: "interrupt", subscription: "interrupt" },
5138
5519
  // Marks on your messages are queued: they wake you but never interrupt (A39).
5139
5520
  reaction: { author: "queue" },
5140
- // A new version of a notice or summary is queued like a mark.
5141
- note: { member: "queue", follower: "queue" },
5521
+ // A new version of a notice or summary rides along with the Bot's next batch: a Bot that is working hears it at
5522
+ // once, and one that is idle is not woken for it (A77). A person's sidebar still shows the hint line.
5523
+ note: { member: "digest", follower: "digest" },
5142
5524
  // The owner just gave the agent Bot its first model: it should welcome them now (A48).
5143
5525
  onboard: { setup: "interrupt" },
5144
5526
  // What the agent Bot changed about a Bot, or the owner about where it works, rides along with that Bot's next batch,
@@ -5345,11 +5727,11 @@ var ChatMessages = class {
5345
5727
  const message = this.repository.message(this.actor, id);
5346
5728
  return message?.chatId === this.id ? message : null;
5347
5729
  }
5348
- async list({ after: after2 = 0, limit }) {
5349
- return this.repository.messages(this.actor, this.id, after2, limit);
5730
+ async list({ after: after2 = 0, limit: limit2 }) {
5731
+ return this.repository.messages(this.actor, this.id, after2, limit2);
5350
5732
  }
5351
- async search(query, { limit }) {
5352
- return this.repository.search(this.actor, this.id, query, limit);
5733
+ async search(query, { limit: limit2 }) {
5734
+ return this.repository.search(this.actor, this.id, query, limit2);
5353
5735
  }
5354
5736
  };
5355
5737
 
@@ -5671,32 +6053,32 @@ var ChatThreads = class {
5671
6053
  * cursor is the last row's sort key, so a thread that moves while you scroll is neither repeated nor skipped. */
5672
6054
  followedPage(actorId, query) {
5673
6055
  const filter = choice(query.filter, THREAD_FILTERS, "all", "filter"), sort = choice(query.sort, THREAD_SORTS, "updated", "sort");
5674
- const limit = query.limit === void 0 ? THREAD_PAGE : Number(query.limit);
5675
- if (!Number.isInteger(limit) || limit < 1 || limit > 100) throw new ChatError(400, "Invalid limit");
6056
+ const limit2 = query.limit === void 0 ? THREAD_PAGE : Number(query.limit);
6057
+ if (!Number.isInteger(limit2) || limit2 < 1 || limit2 > 100) throw new ChatError(400, "Invalid limit");
5676
6058
  const after2 = query.cursor === void 0 ? null : cursorKey(query.cursor);
5677
- const rows3 = this.store.followedKeys(actorId).filter((row) => filter === "all" || (filter === "unread" ? row.unread > 0 : row.status === filter)).map((row) => ({ id: row.id, key: threadKey(row, sort) })).sort((a, b) => compareKeys(a.key, b.key)).filter((row) => !after2 || compareKeys(row.key, after2) > 0);
5678
- const page2 = rows3.slice(0, limit);
5679
- return { entries: page2.map((row) => this.entry(actorId, row.id)), next: rows3.length > limit ? Buffer.from(JSON.stringify(page2.at(-1).key)).toString("base64url") : null };
6059
+ const rows3 = this.store.followedKeys(actorId).filter((row2) => filter === "all" || (filter === "unread" ? row2.unread > 0 : row2.status === filter)).map((row2) => ({ id: row2.id, key: threadKey(row2, sort) })).sort((a, b) => compareKeys(a.key, b.key)).filter((row2) => !after2 || compareKeys(row2.key, after2) > 0);
6060
+ const page2 = rows3.slice(0, limit2);
6061
+ return { entries: page2.map((row2) => this.entry(actorId, row2.id)), next: rows3.length > limit2 ? Buffer.from(JSON.stringify(page2.at(-1).key)).toString("base64url") : null };
5680
6062
  }
5681
6063
  /** The followed threads in numbers (A69), sent with the chat list for the sidebar and the page's tabs. */
5682
6064
  followedSummary(actorId) {
5683
6065
  const summary = { total: 0, open: 0, done: 0, unreadThreads: 0, unread: 0, mentions: 0 };
5684
- for (const row of this.store.followedKeys(actorId)) {
6066
+ for (const row2 of this.store.followedKeys(actorId)) {
5685
6067
  summary.total++;
5686
- summary[row.status]++;
5687
- summary.unread += row.unread;
5688
- summary.mentions += row.mentions;
5689
- if (row.unread > 0) summary.unreadThreads++;
6068
+ summary[row2.status]++;
6069
+ summary.unread += row2.unread;
6070
+ summary.mentions += row2.mentions;
6071
+ if (row2.unread > 0) summary.unreadThreads++;
5690
6072
  }
5691
6073
  return summary;
5692
6074
  }
5693
6075
  /** The open threads a member follows, most recently active first, up to `limit`, and how many there are (A73): what a
5694
6076
  * Bot is reminded of after a compaction. Finished threads are left out, as from a Bot's unread (A70). */
5695
- openFollowed(actorId, limit) {
5696
- const open2 = this.store.followedKeys(actorId).filter((row) => row.status === "open").sort((a, b) => b.lastReplyAt - a.lastReplyAt || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
5697
- const threads = open2.slice(0, limit).flatMap((row) => {
5698
- const chat = this.repository.getChat(actorId, row.id), channel2 = chat?.parentId ? this.repository.getChat(actorId, chat.parentId) : null;
5699
- return chat && channel2 ? [{ id: chat.id, title: chat.title, channel: { id: channel2.id, title: channel2.title }, lastReplyAt: row.lastReplyAt, unread: row.unread }] : [];
6077
+ openFollowed(actorId, limit2) {
6078
+ const open2 = this.store.followedKeys(actorId).filter((row2) => row2.status === "open").sort((a, b) => b.lastReplyAt - a.lastReplyAt || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
6079
+ const threads = open2.slice(0, limit2).flatMap((row2) => {
6080
+ const chat = this.repository.getChat(actorId, row2.id), channel2 = chat?.parentId ? this.repository.getChat(actorId, chat.parentId) : null;
6081
+ return chat && channel2 ? [{ id: chat.id, title: chat.title, channel: { id: channel2.id, title: channel2.title }, lastReplyAt: row2.lastReplyAt, unread: row2.unread }] : [];
5700
6082
  });
5701
6083
  return { total: open2.length, threads };
5702
6084
  }
@@ -5810,8 +6192,8 @@ var ChatService = class {
5810
6192
  get(actorId, chatId) {
5811
6193
  return this.repository.getChat(actorId, chatId);
5812
6194
  }
5813
- sentActivity(actorId, senderId, before, limit) {
5814
- return this.repository.sentActivity(actorId, senderId, before, limit);
6195
+ sentActivity(actorId, senderId, before, limit2) {
6196
+ return this.repository.sentActivity(actorId, senderId, before, limit2);
5815
6197
  }
5816
6198
  member(id) {
5817
6199
  return this.repository.member(id);
@@ -5902,19 +6284,19 @@ var ChatService = class {
5902
6284
  ];
5903
6285
  }
5904
6286
  /** Exactly the unread messages of a chat, oldest first; the caller moves the read position. */
5905
- unreadMessages(actorId, chatId, limit) {
5906
- return this.repository.unreadMessages(actorId, chatId, limit);
6287
+ unreadMessages(actorId, chatId, limit2) {
6288
+ return this.repository.unreadMessages(actorId, chatId, limit2);
5907
6289
  }
5908
6290
  /** `sender`: only that member's messages (a Bot looking for what it sent itself). */
5909
- searchAll(actorId, query, limit = 30, within, sender) {
5910
- if (!query.trim() || query.length > 500 || !Number.isInteger(limit) || limit < 1 || limit > 100) throw new ChatError(400, "Invalid search query or limit");
6291
+ searchAll(actorId, query, limit2 = 30, within, sender) {
6292
+ if (!query.trim() || query.length > 500 || !Number.isInteger(limit2) || limit2 < 1 || limit2 > 100) throw new ChatError(400, "Invalid search query or limit");
5911
6293
  if (within) this.repository.assertAccess(actorId, within);
5912
- return this.repository.searchAll(actorId, query.trim(), limit, within, sender);
6294
+ return this.repository.searchAll(actorId, query.trim(), limit2, within, sender);
5913
6295
  }
5914
6296
  /** The search box: channels and threads by title, topic or notice / summary, and messages by text, with the chat
5915
6297
  * each message is in. `within` searches one chat (a channel with its threads) and returns no chats. */
5916
- find(actorId, query, { within, limit = 50 } = {}) {
5917
- const messages = this.searchAll(actorId, query, limit, within);
6298
+ find(actorId, query, { within, limit: limit2 = 50 } = {}) {
6299
+ const messages = this.searchAll(actorId, query, limit2, within);
5918
6300
  const chats = within ? [] : this.repository.searchChatIds(actorId, query.trim(), 30).map((id) => this.repository.getChat(actorId, id)).filter((chat) => chat !== null);
5919
6301
  const where2 = /* @__PURE__ */ new Map();
5920
6302
  const chatOf = (id) => {
@@ -5927,16 +6309,16 @@ var ChatService = class {
5927
6309
  }) };
5928
6310
  }
5929
6311
  /** The newest `limit` messages before a sequence (null: the newest of all), oldest first, for scrolling back. */
5930
- readBefore(actorId, chatId, before, limit) {
5931
- if (before !== null && (!Number.isSafeInteger(before) || before < 1) || !Number.isSafeInteger(limit) || limit < 1 || limit > 200)
6312
+ readBefore(actorId, chatId, before, limit2) {
6313
+ if (before !== null && (!Number.isSafeInteger(before) || before < 1) || !Number.isSafeInteger(limit2) || limit2 < 1 || limit2 > 200)
5932
6314
  throw new ChatError(400, "Invalid message cursor or limit");
5933
- const messages = this.repository.messagesBefore(actorId, chatId, before ?? Number.MAX_SAFE_INTEGER, limit + 1);
5934
- return { messages: messages.slice(-limit), hasOlder: messages.length > limit };
6315
+ const messages = this.repository.messagesBefore(actorId, chatId, before ?? Number.MAX_SAFE_INTEGER, limit2 + 1);
6316
+ return { messages: messages.slice(-limit2), hasOlder: messages.length > limit2 };
5935
6317
  }
5936
- read(actorId, chatId, after2 = 0, limit = 1e3) {
5937
- if (!Number.isSafeInteger(after2) || after2 < 0 || !Number.isSafeInteger(limit) || limit < 1 || limit > 1e3)
6318
+ read(actorId, chatId, after2 = 0, limit2 = 1e3) {
6319
+ if (!Number.isSafeInteger(after2) || after2 < 0 || !Number.isSafeInteger(limit2) || limit2 < 1 || limit2 > 1e3)
5938
6320
  throw new ChatError(400, "Invalid message cursor or limit");
5939
- return this.repository.messages(actorId, chatId, after2, limit);
6321
+ return this.repository.messages(actorId, chatId, after2, limit2);
5940
6322
  }
5941
6323
  instance(actorId, chatId) {
5942
6324
  this.repository.assertAccess(actorId, chatId);
@@ -5979,9 +6361,9 @@ var ChatService = class {
5979
6361
  message(actorId, id) {
5980
6362
  return this.repository.message(actorId, id);
5981
6363
  }
5982
- search(actorId, chatId, query, limit = 30, sender) {
5983
- if (!query.trim() || query.length > 500 || !Number.isInteger(limit) || limit < 1 || limit > 100) throw new ChatError(400, "Invalid search query or limit");
5984
- return this.repository.search(actorId, chatId, query, limit, sender);
6364
+ search(actorId, chatId, query, limit2 = 30, sender) {
6365
+ if (!query.trim() || query.length > 500 || !Number.isInteger(limit2) || limit2 < 1 || limit2 > 100) throw new ChatError(400, "Invalid search query or limit");
6366
+ return this.repository.search(actorId, chatId, query, limit2, sender);
5985
6367
  }
5986
6368
  notify(change) {
5987
6369
  try {
@@ -6052,8 +6434,8 @@ var ChatFiles = class {
6052
6434
  head(file, bytes) {
6053
6435
  return this.chats.attachments.head(file, bytes);
6054
6436
  }
6055
- read(file, limit) {
6056
- return this.chats.attachments.read(file, limit);
6437
+ read(file, limit2) {
6438
+ return this.chats.attachments.read(file, limit2);
6057
6439
  }
6058
6440
  };
6059
6441
  function listing(message) {
@@ -6067,12 +6449,12 @@ function byteSize(bytes) {
6067
6449
  }
6068
6450
 
6069
6451
  // src/server/service/events.ts
6070
- import { randomUUID as randomUUID16 } from "node:crypto";
6452
+ import { randomUUID as randomUUID17 } from "node:crypto";
6071
6453
  var KEEP_MS = 10 * 6e4;
6072
6454
  var KEEP_MAX = 5e3;
6073
6455
  var Changes = class {
6074
6456
  /** This run's changes: a number from before a restart says nothing. */
6075
- run = randomUUID16().slice(0, 8);
6457
+ run = randomUUID17().slice(0, 8);
6076
6458
  last = 0;
6077
6459
  kept = [];
6078
6460
  listeners = /* @__PURE__ */ new Set();
@@ -6108,7 +6490,7 @@ var Changes = class {
6108
6490
  };
6109
6491
 
6110
6492
  // src/server/service/bot/activity.ts
6111
- function activityPage(messages, history, limit) {
6493
+ function activityPage(messages, history, limit2) {
6112
6494
  const candidates2 = [
6113
6495
  ...messages.map((record3) => ({ kind: "chat", record: record3 })),
6114
6496
  ...history.map((record3) => ({ kind: "history", record: record3 }))
@@ -6118,14 +6500,14 @@ function activityPage(messages, history, limit) {
6118
6500
  if (a.kind !== b.kind) return a.kind === "history" ? -1 : 1;
6119
6501
  return a.record.id === b.record.id ? 0 : a.record.id > b.record.id ? -1 : 1;
6120
6502
  });
6121
- const items = candidates2.slice(0, limit), last = items.at(-1);
6503
+ const items = candidates2.slice(0, limit2), last = items.at(-1);
6122
6504
  const position = !last ? null : last.kind === "history" ? { kind: "history", timestamp: last.record.timestamp, id: last.record.id } : { kind: "chat", timestamp: last.record.timestamp, id: last.record.id };
6123
- return { items, nextBefore: candidates2.length > limit ? position : null };
6505
+ return { items, nextBefore: candidates2.length > limit2 ? position : null };
6124
6506
  }
6125
6507
 
6126
6508
  // src/server/service/bot/service.ts
6127
6509
  import { dirname as dirname10, join as join16 } from "node:path";
6128
- import { rm as rm5 } from "node:fs/promises";
6510
+ import { rm as rm6 } from "node:fs/promises";
6129
6511
 
6130
6512
  // src/server/infra/pi/history.ts
6131
6513
  var value = (input) => JSON.parse(JSON.stringify(input));
@@ -6189,9 +6571,9 @@ var PiHistoryAdapter = class {
6189
6571
  if (m.role === "system") {
6190
6572
  if (m.sections) {
6191
6573
  const sections = {};
6192
- for (const [name3, text4] of Object.entries(m.sections)) {
6193
- sections[name3] = text4 === null ? null : message.content.length;
6194
- if (text4 !== null) message.content.push({ type: "text", text: text4 });
6574
+ for (const [name3, text5] of Object.entries(m.sections)) {
6575
+ sections[name3] = text5 === null ? null : message.content.length;
6576
+ if (text5 !== null) message.content.push({ type: "text", text: text5 });
6195
6577
  }
6196
6578
  pi.sections = sections;
6197
6579
  }
@@ -6328,7 +6710,7 @@ function promptChange(before, after2) {
6328
6710
  break;
6329
6711
  }
6330
6712
  }
6331
- const cut = (text4) => text4.length > TEXT_LIMIT ? `${text4.slice(0, TEXT_LIMIT)}\u2026` : text4;
6713
+ const cut = (text5) => text5.length > TEXT_LIMIT ? `${text5.slice(0, TEXT_LIMIT)}\u2026` : text5;
6332
6714
  if (!tooLong) return { parts, diff: cut(lines.join("\n")), whole: false, next };
6333
6715
  return { parts, diff: cut(changed.map((part) => `@@ ${part.name}
6334
6716
  ${part.text.trim()}`).join("\n\n")), whole: true, next };
@@ -6447,7 +6829,7 @@ function resultText(result) {
6447
6829
 
6448
6830
  // src/server/infra/pi/environment-tools.ts
6449
6831
  import { AsyncLocalStorage } from "node:async_hooks";
6450
- import { readFile as readLocal, rm as rm2 } from "node:fs/promises";
6832
+ import { readFile as readLocal, rm as rm3 } from "node:fs/promises";
6451
6833
  import { basename } from "node:path";
6452
6834
  import { createBashToolDefinition, createEditToolDefinition, createReadToolDefinition, createWriteToolDefinition } from "@earendil-works/pi-coding-agent";
6453
6835
 
@@ -6456,7 +6838,7 @@ import { StringDecoder } from "node:string_decoder";
6456
6838
  var placeholder = (name3) => `{{secret:${name3}}}`;
6457
6839
  var MIN_MASKED = 4;
6458
6840
  var MIN_FRAGMENT = 8;
6459
- var xml = (text4) => text4.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll('"', "&quot;");
6841
+ var xml = (text5) => text5.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll('"', "&quot;");
6460
6842
  function base64Fragments(value2) {
6461
6843
  const bytes = Buffer.from(value2, "utf8"), found = [];
6462
6844
  for (const offset of [0, 1, 2]) {
@@ -6485,8 +6867,8 @@ var Masker = class {
6485
6867
  get empty() {
6486
6868
  return this.pairs.length === 0;
6487
6869
  }
6488
- mask(text4) {
6489
- let result = text4;
6870
+ mask(text5) {
6871
+ let result = text5;
6490
6872
  for (const { form, name: name3 } of this.pairs) if (result.includes(form)) result = result.split(form).join(placeholder(name3));
6491
6873
  return result;
6492
6874
  }
@@ -6568,9 +6950,9 @@ var u16le = (bytes, offset) => at(bytes, offset) + (at(bytes, offset + 1) << 8);
6568
6950
  var u32be = (bytes, offset) => at(bytes, offset) * 16777216 + (at(bytes, offset + 1) << 16) + (at(bytes, offset + 2) << 8) + at(bytes, offset + 3);
6569
6951
  var u32le = (bytes, offset) => at(bytes, offset) + (at(bytes, offset + 1) << 8) + (at(bytes, offset + 2) << 16) + at(bytes, offset + 3) * 16777216;
6570
6952
  var startsWith = (bytes, prefix) => bytes.length >= prefix.length && prefix.every((byte, index) => bytes[index] === byte);
6571
- function ascii(bytes, offset, text4) {
6572
- if (bytes.length < offset + text4.length) return false;
6573
- for (let index = 0; index < text4.length; index++) if (bytes[offset + index] !== text4.charCodeAt(index)) return false;
6953
+ function ascii(bytes, offset, text5) {
6954
+ if (bytes.length < offset + text5.length) return false;
6955
+ for (let index = 0; index < text5.length; index++) if (bytes[offset + index] !== text5.charCodeAt(index)) return false;
6574
6956
  return true;
6575
6957
  }
6576
6958
 
@@ -6745,7 +7127,7 @@ function relocated(definition, current) {
6745
7127
  try {
6746
7128
  const result = await execute(...args), path = result.details?.fullOutputPath;
6747
7129
  if (typeof path !== "string") return result;
6748
- const target3 = await moveOut(path, env), swap = (text4) => text4?.replaceAll(path, target3);
7130
+ const target3 = await moveOut(path, env), swap = (text5) => text5?.replaceAll(path, target3);
6749
7131
  return {
6750
7132
  ...result,
6751
7133
  content: result.content.map((part) => part.type === "text" ? { ...part, text: swap(part.text) } : part),
@@ -6762,7 +7144,7 @@ function relocated(definition, current) {
6762
7144
  async function moveOut(path, env) {
6763
7145
  const target3 = `${env.temp ?? `${env.cwd}/temp`}/${basename(path)}`;
6764
7146
  await env.writeFile(target3, await readLocal(path));
6765
- await rm2(path, { force: true });
7147
+ await rm3(path, { force: true });
6766
7148
  return target3;
6767
7149
  }
6768
7150
  function defined(vars) {
@@ -6882,10 +7264,10 @@ var PiSession = class _PiSession {
6882
7264
  let changed = false;
6883
7265
  const content2 = event.content.map((part) => {
6884
7266
  if (part.type !== "text") return part;
6885
- const text4 = masker.mask(part.text);
6886
- if (text4 === part.text) return part;
7267
+ const text5 = masker.mask(part.text);
7268
+ if (text5 === part.text) return part;
6887
7269
  changed = true;
6888
- return { ...part, text: text4 };
7270
+ return { ...part, text: text5 };
6889
7271
  });
6890
7272
  if (!changed) return void 0;
6891
7273
  return { content: content2, ...event.structuredContent !== void 0 ? { structuredContent: masker.maskDeep(event.structuredContent) } : {} };
@@ -7080,7 +7462,7 @@ var PiSession = class _PiSession {
7080
7462
  import { execFile, spawn } from "node:child_process";
7081
7463
  import { createHash as createHash6 } from "node:crypto";
7082
7464
  import { createWriteStream } from "node:fs";
7083
- import { mkdir as mkdir4, mkdtemp, readFile as readFile2, rename as rename3, rm as rm3, writeFile as writeFile2 } from "node:fs/promises";
7465
+ import { mkdir as mkdir4, mkdtemp, readFile as readFile2, rename as rename4, rm as rm4, writeFile as writeFile2 } from "node:fs/promises";
7084
7466
  import { tmpdir } from "node:os";
7085
7467
  import { join as join9 } from "node:path";
7086
7468
  import { Connection, RemoteExecutionEnv, packagedDaemon } from "@earendil-works/pi-env";
@@ -7128,7 +7510,7 @@ function overDurable(id, kind, env, options) {
7128
7510
  cwd: exec.cwd,
7129
7511
  ...options.passEnv && exec.env ? { env: { ...exec.env, ...exec.secrets }, inheritEnv: false } : { ...exec.secrets ? { env: exec.secrets } : {}, inheritEnv: true },
7130
7512
  ...exec.timeout ? { timeout: exec.timeout } : {},
7131
- onOutput: (text4) => exec.onData(Buffer.from(text4))
7513
+ onOutput: (text5) => exec.onData(Buffer.from(text5))
7132
7514
  }, context(exec.signal));
7133
7515
  if (result.ok) return { exitCode: result.value.exitCode };
7134
7516
  if (result.error.code === "aborted") throw new Error("aborted");
@@ -7236,7 +7618,7 @@ async function buildImage(tag, uid, gid, logFile) {
7236
7618
  }).finally(() => log.end());
7237
7619
  if (code !== 0) throw Object.assign(new Error(`docker build exited with ${code}`), { code: "docker_failed" });
7238
7620
  } finally {
7239
- await rm3(dir, { recursive: true, force: true });
7621
+ await rm4(dir, { recursive: true, force: true });
7240
7622
  }
7241
7623
  }
7242
7624
  async function containerStates(label) {
@@ -7268,6 +7650,10 @@ async function runContainer(spec) {
7268
7650
  "--shm-size",
7269
7651
  "1g",
7270
7652
  ...Object.entries(spec.labels).flatMap(([key, value2]) => ["--label", `${key}=${value2}`]),
7653
+ ...spec.network ? ["--network", spec.network] : [],
7654
+ ...Object.entries(spec.hosts ?? {}).flatMap(([name3, address2]) => ["--add-host", `${name3}:${address2}`]),
7655
+ ...spec.limits?.memory ? memoryFlags(spec.limits.memory, spec.limits.memory) : [],
7656
+ ...spec.limits?.cpus ? ["--cpus", String(spec.limits.cpus)] : [],
7271
7657
  "-v",
7272
7658
  `${spec.workspace}:${COMPUTER_CWD}`,
7273
7659
  "-v",
@@ -7279,6 +7665,28 @@ async function runContainer(spec) {
7279
7665
  "infinity"
7280
7666
  ], 12e4);
7281
7667
  }
7668
+ var memoryFlags = (memory, withSwap) => ["--memory", String(memory), "--memory-swap", String(withSwap)];
7669
+ async function updateLimits(name3, change, machine) {
7670
+ const args = [
7671
+ ...change.memory === void 0 ? [] : change.memory ? memoryFlags(change.memory, change.memory) : memoryFlags(machine.memory, machine.memory * 2),
7672
+ ...change.cpus === void 0 ? [] : ["--cpus", String(change.cpus ?? machine.cpus)]
7673
+ ];
7674
+ if (args.length) await docker(["update", ...args, name3], 6e4);
7675
+ }
7676
+ async function dockerCapacity() {
7677
+ const [cpus, memory] = (await docker(["info", "--format", "{{.NCPU}} {{.MemTotal}}"], 15e3)).split(" ").map(Number);
7678
+ if (!cpus || !memory) throw Object.assign(new Error("docker info gave no CPUs or memory"), { code: "docker_failed" });
7679
+ return { cpus, memory };
7680
+ }
7681
+ async function containerUsage(name3) {
7682
+ const [cpu = "", memory = ""] = (await docker(["stats", "--no-stream", "--format", "{{.CPUPerc}} {{.MemUsage}}", name3], 3e4)).split(" ");
7683
+ return { cpu: parseFloat(cpu) || 0, memory: sizeBytes(memory.split("/")[0] ?? "") };
7684
+ }
7685
+ var UNITS = { b: 1, kib: 2 ** 10, mib: 2 ** 20, gib: 2 ** 30, tib: 2 ** 40, kb: 1e3, mb: 1e6, gb: 1e9, tb: 1e12 };
7686
+ function sizeBytes(text5) {
7687
+ const match = /^\s*([\d.]+)\s*([a-z]+)\s*$/i.exec(text5);
7688
+ return match ? Math.round(parseFloat(match[1]) * (UNITS[match[2].toLowerCase()] ?? 1)) : 0;
7689
+ }
7282
7690
  async function startContainer(name3) {
7283
7691
  await docker(["start", name3], 6e4);
7284
7692
  }
@@ -7305,214 +7713,96 @@ async function syncDaemon(dir) {
7305
7713
  await mkdir4(dir, { recursive: true, mode: 448 });
7306
7714
  const temp = `${target3}.${process.pid}.tmp`;
7307
7715
  await writeFile2(temp, wanted, { mode: 493 });
7308
- await rename3(temp, target3);
7716
+ await rename4(temp, target3);
7309
7717
  }
7310
7718
  function containerEnvironment(id, container, log) {
7311
- const connection = new Connection({ command: ["docker", "exec", "-i", container, `${DAEMON_DIR}/pi-env`], onLog: (text4) => log(text4.trim()) });
7719
+ const connection = new Connection({ command: ["docker", "exec", "-i", container, `${DAEMON_DIR}/pi-env`], onLog: (text5) => log(text5.trim()) });
7312
7720
  return overDurable(id, "computer", new RemoteExecutionEnv({ connection, id, cwd: COMPUTER_CWD }), { passEnv: false, close: () => connection.close() });
7313
7721
  }
7314
-
7315
- // src/server/service/bot/kinds/sandbox.ts
7316
- var sandbox = {
7317
- kind: "computer",
7318
- home: true,
7319
- grantable: false,
7320
- open: (record3, botId, _workspacePath, deps) => deps.computers.environment(record3.botId ?? botId, record3),
7321
- place: (record3, botId) => ({
7322
- id: record3.id,
7323
- kind: "sandbox",
7324
- name: record3.botId === botId ? "your sandbox" : "another Bot's sandbox",
7325
- directory: COMPUTER_CWD
7326
- }),
7327
- ensure: async (record3, botId, deps) => {
7328
- if (deps.computers.available) await deps.computers.ensure(record3.botId ?? botId);
7329
- },
7330
- connects: false,
7331
- summary: (record3, botId) => record3.botId === botId ? "own sandbox" : "another Bot's sandbox",
7332
- describe: (record3, botId, deps) => ({ state: deps.computers.status(record3.botId ?? botId)?.state ?? "stopped" }),
7333
- about: (_record, _botId, deps) => "A Linux container only you use, with Node, Python, git, build tools, and Playwright with its browsers. You can install software there with sudo; it stays until the owner resets your sandbox, and /workspace, your home there, is kept when it is reset." + (deps.computers.available ? "" : " This machine has no Docker yet, so your file and command tools are unavailable here; your chat tools work as usual."),
7334
- signature: (record3) => record3.id
7335
- };
7336
-
7337
- // src/server/service/bot/kinds/ssh.ts
7338
- var ssh = {
7339
- kind: "ssh",
7340
- home: false,
7341
- grantable: true,
7342
- open: (record3, botId, _workspacePath, deps) => deps.machines.environment(record3, botId),
7343
- place: (record3) => ({ id: record3.id, kind: "ssh", name: record3.name, directory: record3.directory ?? record3.spec.home ?? "/" }),
7344
- connects: true,
7345
- summary: (record3) => record3.name,
7346
- describe: (record3, _botId, deps) => ({ address: deps.machines.address(record3), ...record3.description ? { description: record3.description } : {} }),
7347
- about: (record3, botId, deps) => deps.machines.about(record3, botId),
7348
- signature: (record3) => `${record3.id}@${record3.directory ?? ""}`
7349
- };
7350
-
7351
- // src/server/service/bot/kinds/index.ts
7352
- var KINDS = { computer: sandbox, ssh };
7353
-
7354
- // src/server/service/bot/environments.ts
7355
- var BotEnvironments = class {
7356
- constructor(store, computers, machines) {
7357
- this.store = store;
7358
- this.computers = computers;
7359
- this.kinds = { computers, machines };
7722
+ async function ensureNetwork(name3, labels) {
7723
+ const gateway2 = () => docker(["network", "inspect", "--format", "{{range .IPAM.Config}}{{.Gateway}} {{end}}", name3]).then((out) => out.split(" ").find(Boolean) ?? "");
7724
+ try {
7725
+ return await gateway2();
7726
+ } catch (error) {
7727
+ if (!/not found|no such network/i.test(error.message)) throw error;
7360
7728
  }
7361
- open = /* @__PURE__ */ new Map();
7362
- kinds;
7363
- current(botId, workspacePath) {
7364
- const target3 = this.store.environments.placement(botId).current, signature = this.signature(target3);
7365
- const open2 = this.open.get(botId);
7366
- if (open2?.id === signature) return open2.view;
7367
- if (open2) this.retire(open2);
7368
- const opened = this.track(signature, KINDS[target3.kind].open(target3, botId, workspacePath, this.kinds));
7369
- this.open.set(botId, opened);
7370
- return opened.view;
7729
+ try {
7730
+ await docker([
7731
+ "network",
7732
+ "create",
7733
+ "--driver",
7734
+ "bridge",
7735
+ "--opt",
7736
+ "com.docker.network.bridge.enable_icc=false",
7737
+ ...Object.entries(labels).flatMap(([key, value2]) => ["--label", `${key}=${value2}`]),
7738
+ name3
7739
+ ]);
7740
+ } catch (error) {
7741
+ if (!/already exists/i.test(error.message)) throw error;
7371
7742
  }
7372
- placement(botId) {
7373
- const environments = this.store.environments, { current } = environments.placement(botId);
7374
- const granted = new Set(environments.grants(botId).map((grant) => grant.id));
7375
- return {
7376
- current: current.kind,
7377
- ...current.kind === "ssh" ? { currentName: current.name } : {},
7378
- computer: this.computers.info(botId),
7379
- docker: this.computers.available,
7380
- machines: environments.sshMachines().map((machine) => ({ id: machine.id, name: machine.name, granted: granted.has(machine.id) }))
7381
- };
7743
+ return gateway2();
7744
+ }
7745
+ async function containerNetworks(label) {
7746
+ const out = await docker(["ps", "-a", "--filter", `label=${label}`, "--format", "{{.Names}} {{.Networks}}"]);
7747
+ return new Map(out.split("\n").filter(Boolean).map((line) => {
7748
+ const [name3, nets = ""] = line.split(" ");
7749
+ return [name3, nets.split(",").filter(Boolean)];
7750
+ }));
7751
+ }
7752
+ async function networkAddresses(network) {
7753
+ const out = await docker(["network", "inspect", "--format", "{{range .Containers}}{{.Name}}={{.IPv4Address}} {{end}}", network]);
7754
+ return new Map(out.split(" ").filter(Boolean).map((entry) => {
7755
+ const [name3, address2 = ""] = entry.split("=");
7756
+ return [name3, address2.split("/")[0]];
7757
+ }));
7758
+ }
7759
+ async function moveToNetwork(container, network) {
7760
+ await docker(["network", "connect", network, container]).catch((error) => {
7761
+ if (!/already exists|already attached/i.test(error.message)) throw error;
7762
+ });
7763
+ await docker(["network", "disconnect", "bridge", container]).catch((error) => {
7764
+ if (!/not connected/i.test(error.message)) throw error;
7765
+ });
7766
+ }
7767
+ async function addHost(container, name3, address2) {
7768
+ await docker(["exec", "-u", "root", container, "sh", "-c", `grep -q '[[:space:]]${name3}$' /etc/hosts || printf '%s\\t%s\\n' '${address2}' '${name3}' >> /etc/hosts`], 3e4);
7769
+ }
7770
+ async function listeningPorts(container) {
7771
+ const out = await docker(["exec", container, "sh", "-c", "cat /proc/net/tcp /proc/net/tcp6 2>/dev/null"], 3e4);
7772
+ const ports = /* @__PURE__ */ new Map();
7773
+ for (const line of out.split("\n")) {
7774
+ const fields = line.trim().split(/\s+/);
7775
+ if (fields[3] !== "0A") continue;
7776
+ const [address2, hex] = (fields[1] ?? "").split(":");
7777
+ if (!address2 || !hex) continue;
7778
+ const port = parseInt(hex, 16), loopback = loopbackAddress(address2);
7779
+ const known = ports.get(port);
7780
+ ports.set(port, { loopbackOnly: (known?.loopbackOnly ?? true) && loopback });
7382
7781
  }
7383
- /** The environments the Bot may be in: its sandbox, its default, and the ones its owner granted it. */
7384
- usable(botId) {
7385
- const environments = this.store.environments;
7386
- const all = [environments.computerOf(botId), environments.placement(botId).home, ...environments.grants(botId)];
7387
- return [...new Map(all.filter((record3) => !!record3).map((record3) => [record3.id, record3])).values()];
7388
- }
7389
- place(botId, record3, workspacePath) {
7390
- return KINDS[record3.kind].place(record3, botId, workspacePath);
7391
- }
7392
- /** A Bot's environments as other Bots see them in bots.list. */
7393
- summary(botId) {
7394
- const name3 = (record3) => KINDS[record3.kind].summary(record3, botId);
7395
- const { home, current } = this.store.environments.placement(botId);
7396
- return { default: name3(home), current: name3(current), usable: this.usable(botId).map(name3) };
7397
- }
7398
- /** The Bot's default environment (A68): its own sandbox. */
7399
- default(botId, workspacePath) {
7400
- return this.place(botId, this.store.environments.placement(botId).home, workspacePath);
7401
- }
7402
- /** Where the Bot works now, for the end of each batch. */
7403
- here(botId, workspacePath) {
7404
- return this.place(botId, this.store.environments.placement(botId).current, workspacePath);
7405
- }
7406
- /** environments.list: where the Bot is and where else it may go. */
7407
- list(botId, workspacePath) {
7408
- const { home, current } = this.store.environments.placement(botId);
7409
- return { current: current.id, environments: this.usable(botId).map((record3) => ({
7410
- ...this.place(botId, record3, workspacePath),
7411
- ...record3.id === home.id ? { default: true } : {},
7412
- ...KINDS[record3.kind].describe(record3, botId, this.kinds)
7413
- })) };
7414
- }
7415
- /** environments.switch: the Bot moves itself, like cd, until it switches back. A sandbox is started first, an SSH
7416
- * machine connected first; if that fails, the Bot stays where it was. */
7417
- async switch(botId, environmentId, workspacePath) {
7418
- const target3 = this.usable(botId).find((record3) => record3.id === environmentId);
7419
- if (!target3) throw new ChatError(404, "No work environment with this id that you may use; see environments.list");
7420
- const previous = this.store.environments.placement(botId).current, already = previous.id === target3.id, entry = KINDS[target3.kind];
7421
- await entry.ensure?.(target3, botId, this.kinds);
7422
- this.store.environments.setCurrent(botId, target3.id);
7423
- if (entry.connects && !already) {
7424
- const place = this.place(botId, target3, workspacePath);
7425
- try {
7426
- if (!await this.current(botId, workspacePath).exists(place.directory)) throw new Error(`${place.directory} doesn't exist there; ask your owner to change the machine's directory, or switch back`);
7427
- } catch (error) {
7428
- const opened = this.open.get(botId);
7429
- if (opened) {
7430
- this.open.delete(botId);
7431
- this.retire(opened);
7432
- }
7433
- this.store.environments.setCurrent(botId, previous.id);
7434
- throw new ChatError(409, `Couldn't connect to ${target3.name}, so you stay where you were: ${error instanceof Error ? error.message : String(error)}`);
7435
- }
7436
- }
7437
- return {
7438
- ...this.place(botId, target3, workspacePath),
7439
- about: entry.about(target3, botId, this.kinds),
7440
- ...already ? { note: "You were already here." } : {}
7441
- };
7442
- }
7443
- /** The owner grants or takes back an SSH machine (A57); taking it back while the Bot works there sends it home. */
7444
- grant(botId, environmentId, granted, by, workspacePath) {
7445
- const environments = this.store.environments, target3 = environments.get(environmentId);
7446
- if (!target3 || !KINDS[target3.kind].grantable) throw new ChatError(404, "No such environment");
7447
- const name3 = this.place(botId, target3, workspacePath).name;
7448
- if (granted) return { changed: environments.grant(target3.id, botId, by), moved: null, name: name3 };
7449
- const changed = environments.revoke(target3.id, botId), { home, current } = environments.placement(botId);
7450
- if (current.id !== target3.id || home.id === target3.id) return { changed, moved: null, name: name3 };
7451
- const from = this.here(botId, workspacePath);
7452
- environments.setCurrent(botId, home.id);
7453
- return { changed, moved: { from, to: this.here(botId, workspacePath) }, name: name3 };
7454
- }
7455
- async release(botId) {
7456
- const open2 = this.open.get(botId);
7457
- this.open.delete(botId);
7458
- await open2?.env.close();
7459
- await this.computers.remove(botId);
7460
- }
7461
- async close() {
7462
- const all = [...this.open.values()];
7463
- this.open.clear();
7464
- await Promise.all(all.map((open2) => open2.env.close()));
7465
- await this.computers.close();
7466
- }
7467
- /** What an open environment is for: changing a machine's directory reopens it where the owner put it. */
7468
- signature(record3) {
7469
- return KINDS[record3.kind].signature(record3);
7470
- }
7471
- retire(open2) {
7472
- open2.retired = true;
7473
- if (!open2.busy) void open2.env.close();
7474
- }
7475
- /** Counts the operations running on an environment, so one the Bot has left closes only when they are done. */
7476
- track(id, env) {
7477
- const opened = { id, env, view: env, busy: 0, retired: false };
7478
- const run2 = async (work) => {
7479
- opened.busy++;
7480
- try {
7481
- return await work();
7482
- } finally {
7483
- if (--opened.busy === 0 && opened.retired) void env.close();
7484
- }
7485
- };
7486
- opened.view = {
7487
- id: env.id,
7488
- kind: env.kind,
7489
- local: env.local,
7490
- cwd: env.cwd,
7491
- ...env.temp ? { temp: env.temp } : {},
7492
- exec: (command, options) => run2(() => env.exec(command, options)),
7493
- readFile: (path) => run2(() => env.readFile(path)),
7494
- head: (path, bytes) => run2(() => env.head(path, bytes)),
7495
- readRange: (path, offset, length) => run2(() => env.readRange(path, offset, length)),
7496
- writeFile: (path, content2) => run2(() => env.writeFile(path, content2)),
7497
- appendFile: (path, content2) => run2(() => env.appendFile(path, content2)),
7498
- mkdir: (path) => run2(() => env.mkdir(path)),
7499
- exists: (path) => run2(() => env.exists(path)),
7500
- fileSize: (path) => run2(() => env.fileSize(path)),
7501
- close: () => env.close()
7502
- };
7503
- return opened;
7504
- }
7505
- };
7506
-
7507
- // src/server/service/bot/computers.ts
7508
- import { createHash as createHash7 } from "node:crypto";
7509
- import { dirname as dirname5, join as join10 } from "node:path";
7510
- var SWEEP_MS = 6e4;
7511
- var Computers = class {
7512
- constructor(deps) {
7513
- this.deps = deps;
7514
- this.dataDir = dirname5(deps.store.path);
7515
- this.tag = createHash7("sha256").update(this.dataDir).digest("hex").slice(0, 12);
7782
+ return ports;
7783
+ }
7784
+ function loopbackAddress(hex) {
7785
+ if (hex.length === 8) return hex.slice(6, 8) === "7F";
7786
+ if (hex === "00000000000000000000000001000000") return true;
7787
+ return hex.startsWith("0000000000000000FFFF0000") && hex.slice(30, 32) === "7F";
7788
+ }
7789
+
7790
+ // src/server/service/bot/computers.ts
7791
+ import { createHash as createHash7 } from "node:crypto";
7792
+ import { dirname as dirname5, join as join10 } from "node:path";
7793
+ var SWEEP_MS = 6e4;
7794
+ var HOST_NAME = "host.docker.internal";
7795
+ var MIN_MEMORY = 256 * 2 ** 20;
7796
+ var MIN_CPUS = 0.1;
7797
+ function computerLimits(record3) {
7798
+ const read3 = (value2) => value2 && Number(value2) > 0 ? Number(value2) : null;
7799
+ return { memory: read3(record3.spec.memory), cpus: read3(record3.spec.cpus) };
7800
+ }
7801
+ var Computers = class {
7802
+ constructor(deps) {
7803
+ this.deps = deps;
7804
+ this.dataDir = dirname5(deps.store.path);
7805
+ this.tag = createHash7("sha256").update(this.dataDir).digest("hex").slice(0, 12);
7516
7806
  }
7517
7807
  docker = null;
7518
7808
  probing = null;
@@ -7521,8 +7811,14 @@ var Computers = class {
7521
7811
  states = /* @__PURE__ */ new Map();
7522
7812
  preparing = /* @__PURE__ */ new Map();
7523
7813
  connections = /* @__PURE__ */ new Map();
7524
- /** Containers whose home was checked since Jarvy started (`homeAtWorkspace`). */
7814
+ /** Containers set up since they last started: their home checked (`homeAtWorkspace`), and the host's name added. */
7525
7815
  homes = /* @__PURE__ */ new Map();
7816
+ /** This Jarvy's network and this machine's address on it; null on Docker's default bridge, if it couldn't be made. */
7817
+ network = null;
7818
+ /** Running containers' addresses, by name, as last read. */
7819
+ book = null;
7820
+ /** This machine's CPUs and memory, asked of Docker once. */
7821
+ machine = null;
7526
7822
  dataDir;
7527
7823
  /** Tells this Jarvy's containers from those of other data directories on the same Docker. */
7528
7824
  tag;
@@ -7543,6 +7839,7 @@ var Computers = class {
7543
7839
  this.log(`computers disabled: ${error.message}`);
7544
7840
  return;
7545
7841
  }
7842
+ await this.joinNetwork().catch((error) => this.log(`sandbox network unavailable, staying on the default bridge: ${error.message}`));
7546
7843
  await this.sweep().catch((error) => this.log(`computer sweep failed: ${error.message}`));
7547
7844
  this.timer = setInterval(() => void this.sweep().catch((error) => this.log(`computer sweep failed: ${error.message}`)), SWEEP_MS);
7548
7845
  this.timer.unref();
@@ -7560,7 +7857,42 @@ var Computers = class {
7560
7857
  info(botId) {
7561
7858
  const computer = this.deps.store.environments.computerOf(botId), status = this.status(botId);
7562
7859
  if (!computer || !status) return null;
7563
- return { ...status, container: computer.spec.container, image: computerImage(this.uid, this.gid), workspace: this.workspace(botId), mount: COMPUTER_CWD };
7860
+ return {
7861
+ ...status,
7862
+ container: computer.spec.container,
7863
+ image: computerImage(this.uid, this.gid),
7864
+ workspace: this.workspace(botId),
7865
+ mount: COMPUTER_CWD,
7866
+ limits: computerLimits(computer)
7867
+ };
7868
+ }
7869
+ /** What the sandbox uses now, when it runs, and what this machine has; null for what Docker doesn't answer. */
7870
+ async resources(botId) {
7871
+ const computer = this.need(botId);
7872
+ const usage = this.status(botId)?.state === "running" ? await containerUsage(computer.spec.container).catch(() => null) : null;
7873
+ return { usage, capacity: await this.capacity().catch(() => null) };
7874
+ }
7875
+ /** Sets what the sandbox may use at most (A75): a container that exists changes at once, running or not, and one made
7876
+ * later, after a reset too, starts with them. Each is null for no limit, or from a floor up to this machine's size. */
7877
+ async setLimits(botId, input) {
7878
+ const computer = this.need(botId), machine = await this.capacity();
7879
+ const memory = limit(input.memory, MIN_MEMORY, machine.memory, 2 ** 20), cpus = limit(input.cpus, MIN_CPUS, machine.cpus, 0.01);
7880
+ if (memory === void 0 || cpus === void 0) throw new ValidationError(
7881
+ "Limits are null or a number within this machine's size",
7882
+ "limits_invalid",
7883
+ { memory: Math.floor(machine.memory / 2 ** 30 * 10) / 10, cpus: machine.cpus }
7884
+ );
7885
+ const before = computerLimits(computer), change = { ...memory !== before.memory ? { memory } : {}, ...cpus !== before.cpus ? { cpus } : {} };
7886
+ if (Object.keys(change).length && await containerState(computer.spec.container) !== "missing") await updateLimits(computer.spec.container, change, machine);
7887
+ this.deps.store.environments.setLimits(computer.id, { memory, cpus });
7888
+ this.deps.changed();
7889
+ }
7890
+ capacity() {
7891
+ this.machine ??= dockerCapacity().catch((error) => {
7892
+ this.machine = null;
7893
+ throw error;
7894
+ });
7895
+ return this.machine;
7564
7896
  }
7565
7897
  /** Every Bot has its computer (A68): one restored or made without one, such as the agent Bot on a new installation,
7566
7898
  * gets it now. */
@@ -7609,17 +7941,67 @@ var Computers = class {
7609
7941
  const container = await this.ensure(botId);
7610
7942
  let home = this.homes.get(container);
7611
7943
  if (!home) {
7612
- home = homeAtWorkspace(container).catch((error) => this.log(`home of ${botId} not moved: ${error.message}`));
7944
+ home = this.prepare(container).catch((error) => this.log(`sandbox of ${botId} not set up: ${error.message}`));
7613
7945
  this.homes.set(container, home);
7614
7946
  }
7615
7947
  await home;
7616
7948
  let env = this.connections.get(botId);
7617
7949
  if (!env) {
7618
- env = containerEnvironment(record3.id, container, (text4) => this.log(`[${botId.slice(0, 8)}] ${text4}`));
7950
+ env = containerEnvironment(record3.id, container, (text5) => this.log(`[${botId.slice(0, 8)}] ${text5}`));
7619
7951
  this.connections.set(botId, env);
7620
7952
  }
7621
7953
  return env;
7622
7954
  }
7955
+ /** Sets up a container after it starts: the workspace as its home, and this machine's name for one made before
7956
+ * containers got it at creation. */
7957
+ async prepare(container) {
7958
+ await homeAtWorkspace(container);
7959
+ if (this.network) await addHost(container, HOST_NAME, this.network.gateway);
7960
+ }
7961
+ /** Makes this Jarvy's network and moves the containers still on Docker's default bridge there, running or not. */
7962
+ async joinNetwork() {
7963
+ const name3 = `jarvy-${this.tag.slice(0, 6)}`;
7964
+ this.network = { name: name3, gateway: await ensureNetwork(name3, { "jarvy.data": this.tag }) };
7965
+ const owned = new Set(this.deps.store.environments.computers().map((computer) => computer.spec.container));
7966
+ for (const [container, networks] of await containerNetworks(`jarvy.data=${this.tag}`)) {
7967
+ if (!owned.has(container) || networks.includes(name3)) continue;
7968
+ await moveToNetwork(container, name3).then(() => {
7969
+ this.homes.delete(container);
7970
+ this.log(`moved ${container} to ${name3}`);
7971
+ }).catch((error) => this.log(`moving ${container} to ${name3} failed: ${error.message}`));
7972
+ }
7973
+ }
7974
+ /** The address a Bot's running sandbox has on the network, or null. */
7975
+ async address(botId) {
7976
+ const container = this.deps.store.environments.computerOf(botId)?.spec.container;
7977
+ if (!container || !this.available) return null;
7978
+ return (await this.addresses()).get(container) ?? (await this.addresses(true)).get(container) ?? null;
7979
+ }
7980
+ /** The Bot whose running sandbox has this address, if one of this Jarvy's has it. */
7981
+ async botAt(address2) {
7982
+ if (!this.available) return null;
7983
+ const plain2 = address2.replace(/^::ffff:/, "");
7984
+ const find = (book) => [...book].find(([, value2]) => value2 === plain2)?.[0];
7985
+ const container = find(await this.addresses()) ?? find(await this.addresses(true));
7986
+ return container ? this.deps.store.environments.computers().find((computer) => computer.spec.container === container)?.botId ?? null : null;
7987
+ }
7988
+ /** The ports something listens on in a Bot's running sandbox; null when it isn't running. */
7989
+ async listening(botId) {
7990
+ const container = this.deps.store.environments.computerOf(botId)?.spec.container;
7991
+ if (!container || this.status(botId)?.state !== "running") return null;
7992
+ return listeningPorts(container);
7993
+ }
7994
+ /** How a sandbox names this machine. */
7995
+ get hostName() {
7996
+ return HOST_NAME;
7997
+ }
7998
+ /** Read again when asked fresh, at most once a second, so callers that miss don't each ask Docker. */
7999
+ async addresses(fresh = false) {
8000
+ const age = this.book ? Date.now() - this.book.at : Infinity;
8001
+ if (this.book && (fresh ? age < 1e3 : age < 5e3)) return this.book.byName;
8002
+ this.book = { at: Date.now(), byName: await networkAddresses(this.network?.name ?? "bridge").catch(() => /* @__PURE__ */ new Map()) };
8003
+ return this.book.byName;
8004
+ }
7623
8005
  /** Makes sure the container exists and runs, building the image first when needed; returns its name. */
7624
8006
  ensure(botId) {
7625
8007
  const pending = this.preparing.get(botId);
@@ -7642,9 +8024,15 @@ var Computers = class {
7642
8024
  daemonDir: join10(this.dataDir, "computers", "bin"),
7643
8025
  uid: this.uid,
7644
8026
  gid: this.gid,
7645
- labels: { "jarvy.data": this.tag, "jarvy.bot": botId }
8027
+ labels: { "jarvy.data": this.tag, "jarvy.bot": botId },
8028
+ limits: computerLimits(computer),
8029
+ ...this.network ? { network: this.network.name, hosts: { [HOST_NAME]: this.network.gateway } } : {}
7646
8030
  });
7647
8031
  else if (state === "stopped") await startContainer(container);
8032
+ if (state !== "running") {
8033
+ this.homes.delete(container);
8034
+ this.book = null;
8035
+ }
7648
8036
  this.set(botId, { state: "running" });
7649
8037
  return container;
7650
8038
  } catch (error) {
@@ -7743,22 +8131,232 @@ var Computers = class {
7743
8131
  await this.disconnect(botId);
7744
8132
  continue;
7745
8133
  }
8134
+ if (this.states.get(botId)?.state !== "running") this.homes.delete(computer.spec.container);
7746
8135
  this.set(botId, { state: "running" });
7747
8136
  }
7748
8137
  }
7749
- log(text4) {
7750
- (this.deps.log ?? console.log)(`[computers] ${text4}`);
8138
+ log(text5) {
8139
+ (this.deps.log ?? console.log)(`[computers] ${text5}`);
8140
+ }
8141
+ };
8142
+ function limit(value2, min, max, step) {
8143
+ if (value2 === null) return null;
8144
+ if (typeof value2 !== "number" || !Number.isFinite(value2)) return void 0;
8145
+ const rounded = Math.round(value2 / step) * step;
8146
+ return rounded >= min && rounded <= max ? Number(rounded.toFixed(2)) : void 0;
8147
+ }
8148
+
8149
+ // src/server/service/bot/kinds/sandbox.ts
8150
+ function limits(record3) {
8151
+ const { memory, cpus } = computerLimits(record3);
8152
+ const parts = [memory ? `${Number((memory / 2 ** 30).toFixed(2))} GB of memory` : "", cpus ? `${cpus} ${cpus === 1 ? "CPU" : "CPUs"}` : ""].filter(Boolean);
8153
+ return parts.length ? parts.join(" and ") : null;
8154
+ }
8155
+ var sandbox = {
8156
+ kind: "computer",
8157
+ home: true,
8158
+ grantable: false,
8159
+ open: (record3, botId, _workspacePath, deps) => deps.computers.environment(record3.botId ?? botId, record3),
8160
+ place: (record3, botId) => ({
8161
+ id: record3.id,
8162
+ kind: "sandbox",
8163
+ name: record3.botId === botId ? "your sandbox" : "another Bot's sandbox",
8164
+ directory: COMPUTER_CWD
8165
+ }),
8166
+ ensure: async (record3, botId, deps) => {
8167
+ if (deps.computers.available) await deps.computers.ensure(record3.botId ?? botId);
8168
+ },
8169
+ connects: false,
8170
+ summary: (record3, botId) => record3.botId === botId ? "own sandbox" : "another Bot's sandbox",
8171
+ describe: (record3, botId, deps) => {
8172
+ const most = limits(record3);
8173
+ return { state: deps.computers.status(record3.botId ?? botId)?.state ?? "stopped", ...most ? { limits: `at most ${most}` } : {} };
8174
+ },
8175
+ about: (record3, _botId, deps) => {
8176
+ const most = limits(record3);
8177
+ return "A Linux container only you use, with Node, Python, git, build tools, and Playwright with its browsers. You can install software there with sudo; it stays until the owner resets your sandbox, and /workspace, your home there, is kept when it is reset. Other Bots' sandboxes can't be reached from it; services of this machine that listen on all its addresses are at host.docker.internal. To show a web service of yours to people or other Bots, publish its port with ports.publish." + (most ? ` It may use at most ${most}; a process that takes more memory than that is ended.` : "") + (deps.computers.available ? "" : " This machine has no Docker yet, so your file and command tools are unavailable here; your chat tools work as usual.");
8178
+ },
8179
+ signature: (record3) => record3.id
8180
+ };
8181
+
8182
+ // src/server/service/bot/kinds/ssh.ts
8183
+ var ssh = {
8184
+ kind: "ssh",
8185
+ home: false,
8186
+ grantable: true,
8187
+ open: (record3, botId, _workspacePath, deps) => deps.machines.environment(record3, botId),
8188
+ place: (record3) => ({ id: record3.id, kind: "ssh", name: record3.name, directory: record3.directory ?? record3.spec.home ?? "/" }),
8189
+ connects: true,
8190
+ summary: (record3) => record3.name,
8191
+ describe: (record3, _botId, deps) => ({ address: deps.machines.address(record3), ...record3.description ? { description: record3.description } : {} }),
8192
+ about: (record3, botId, deps) => deps.machines.about(record3, botId),
8193
+ signature: (record3) => `${record3.id}@${record3.directory ?? ""}`
8194
+ };
8195
+
8196
+ // src/server/service/bot/kinds/index.ts
8197
+ var KINDS = { computer: sandbox, ssh };
8198
+
8199
+ // src/server/service/bot/environments.ts
8200
+ var BotEnvironments = class {
8201
+ constructor(store, computers, machines) {
8202
+ this.store = store;
8203
+ this.computers = computers;
8204
+ this.kinds = { computers, machines };
8205
+ }
8206
+ open = /* @__PURE__ */ new Map();
8207
+ kinds;
8208
+ current(botId, workspacePath) {
8209
+ const target3 = this.store.environments.placement(botId).current, signature = this.signature(target3);
8210
+ const open2 = this.open.get(botId);
8211
+ if (open2?.id === signature) return open2.view;
8212
+ if (open2) this.retire(open2);
8213
+ const opened = this.track(signature, KINDS[target3.kind].open(target3, botId, workspacePath, this.kinds));
8214
+ this.open.set(botId, opened);
8215
+ return opened.view;
8216
+ }
8217
+ placement(botId) {
8218
+ const environments = this.store.environments, { current } = environments.placement(botId);
8219
+ const granted = new Set(environments.grants(botId).map((grant) => grant.id));
8220
+ return {
8221
+ current: current.kind,
8222
+ ...current.kind === "ssh" ? { currentName: current.name } : {},
8223
+ computer: this.computers.info(botId),
8224
+ docker: this.computers.available,
8225
+ machines: environments.sshMachines().map((machine) => ({ id: machine.id, name: machine.name, granted: granted.has(machine.id) }))
8226
+ };
8227
+ }
8228
+ /** The environments the Bot may be in: its sandbox, its default, and the ones its owner granted it. */
8229
+ usable(botId) {
8230
+ const environments = this.store.environments;
8231
+ const all = [environments.computerOf(botId), environments.placement(botId).home, ...environments.grants(botId)];
8232
+ return [...new Map(all.filter((record3) => !!record3).map((record3) => [record3.id, record3])).values()];
8233
+ }
8234
+ place(botId, record3, workspacePath) {
8235
+ return KINDS[record3.kind].place(record3, botId, workspacePath);
8236
+ }
8237
+ /** A Bot's environments as other Bots see them in bots.list. */
8238
+ summary(botId) {
8239
+ const name3 = (record3) => KINDS[record3.kind].summary(record3, botId);
8240
+ const { home, current } = this.store.environments.placement(botId);
8241
+ return { default: name3(home), current: name3(current), usable: this.usable(botId).map(name3) };
8242
+ }
8243
+ /** The Bot's default environment (A68): its own sandbox. */
8244
+ default(botId, workspacePath) {
8245
+ return this.place(botId, this.store.environments.placement(botId).home, workspacePath);
8246
+ }
8247
+ /** Where the Bot works now, for the end of each batch. */
8248
+ here(botId, workspacePath) {
8249
+ return this.place(botId, this.store.environments.placement(botId).current, workspacePath);
8250
+ }
8251
+ /** environments.list: where the Bot is and where else it may go. */
8252
+ list(botId, workspacePath) {
8253
+ const { home, current } = this.store.environments.placement(botId);
8254
+ return { current: current.id, environments: this.usable(botId).map((record3) => ({
8255
+ ...this.place(botId, record3, workspacePath),
8256
+ ...record3.id === home.id ? { default: true } : {},
8257
+ ...KINDS[record3.kind].describe(record3, botId, this.kinds)
8258
+ })) };
8259
+ }
8260
+ /** environments.switch: the Bot moves itself, like cd, until it switches back. A sandbox is started first, an SSH
8261
+ * machine connected first; if that fails, the Bot stays where it was. */
8262
+ async switch(botId, environmentId, workspacePath) {
8263
+ const target3 = this.usable(botId).find((record3) => record3.id === environmentId);
8264
+ if (!target3) throw new ChatError(404, "No work environment with this id that you may use; see environments.list");
8265
+ const previous = this.store.environments.placement(botId).current, already = previous.id === target3.id, entry = KINDS[target3.kind];
8266
+ await entry.ensure?.(target3, botId, this.kinds);
8267
+ this.store.environments.setCurrent(botId, target3.id);
8268
+ if (entry.connects && !already) {
8269
+ const place = this.place(botId, target3, workspacePath);
8270
+ try {
8271
+ if (!await this.current(botId, workspacePath).exists(place.directory)) throw new Error(`${place.directory} doesn't exist there; ask your owner to change the machine's directory, or switch back`);
8272
+ } catch (error) {
8273
+ const opened = this.open.get(botId);
8274
+ if (opened) {
8275
+ this.open.delete(botId);
8276
+ this.retire(opened);
8277
+ }
8278
+ this.store.environments.setCurrent(botId, previous.id);
8279
+ throw new ChatError(409, `Couldn't connect to ${target3.name}, so you stay where you were: ${error instanceof Error ? error.message : String(error)}`);
8280
+ }
8281
+ }
8282
+ return {
8283
+ ...this.place(botId, target3, workspacePath),
8284
+ about: entry.about(target3, botId, this.kinds),
8285
+ ...already ? { note: "You were already here." } : {}
8286
+ };
8287
+ }
8288
+ /** The owner grants or takes back an SSH machine (A57); taking it back while the Bot works there sends it home. */
8289
+ grant(botId, environmentId, granted, by, workspacePath) {
8290
+ const environments = this.store.environments, target3 = environments.get(environmentId);
8291
+ if (!target3 || !KINDS[target3.kind].grantable) throw new ChatError(404, "No such environment");
8292
+ const name3 = this.place(botId, target3, workspacePath).name;
8293
+ if (granted) return { changed: environments.grant(target3.id, botId, by), moved: null, name: name3 };
8294
+ const changed = environments.revoke(target3.id, botId), { home, current } = environments.placement(botId);
8295
+ if (current.id !== target3.id || home.id === target3.id) return { changed, moved: null, name: name3 };
8296
+ const from = this.here(botId, workspacePath);
8297
+ environments.setCurrent(botId, home.id);
8298
+ return { changed, moved: { from, to: this.here(botId, workspacePath) }, name: name3 };
8299
+ }
8300
+ async release(botId) {
8301
+ const open2 = this.open.get(botId);
8302
+ this.open.delete(botId);
8303
+ await open2?.env.close();
8304
+ await this.computers.remove(botId);
8305
+ }
8306
+ async close() {
8307
+ const all = [...this.open.values()];
8308
+ this.open.clear();
8309
+ await Promise.all(all.map((open2) => open2.env.close()));
8310
+ await this.computers.close();
8311
+ }
8312
+ /** What an open environment is for: changing a machine's directory reopens it where the owner put it. */
8313
+ signature(record3) {
8314
+ return KINDS[record3.kind].signature(record3);
8315
+ }
8316
+ retire(open2) {
8317
+ open2.retired = true;
8318
+ if (!open2.busy) void open2.env.close();
8319
+ }
8320
+ /** Counts the operations running on an environment, so one the Bot has left closes only when they are done. */
8321
+ track(id, env) {
8322
+ const opened = { id, env, view: env, busy: 0, retired: false };
8323
+ const run2 = async (work) => {
8324
+ opened.busy++;
8325
+ try {
8326
+ return await work();
8327
+ } finally {
8328
+ if (--opened.busy === 0 && opened.retired) void env.close();
8329
+ }
8330
+ };
8331
+ opened.view = {
8332
+ id: env.id,
8333
+ kind: env.kind,
8334
+ local: env.local,
8335
+ cwd: env.cwd,
8336
+ ...env.temp ? { temp: env.temp } : {},
8337
+ exec: (command, options) => run2(() => env.exec(command, options)),
8338
+ readFile: (path) => run2(() => env.readFile(path)),
8339
+ head: (path, bytes) => run2(() => env.head(path, bytes)),
8340
+ readRange: (path, offset, length) => run2(() => env.readRange(path, offset, length)),
8341
+ writeFile: (path, content2) => run2(() => env.writeFile(path, content2)),
8342
+ appendFile: (path, content2) => run2(() => env.appendFile(path, content2)),
8343
+ mkdir: (path) => run2(() => env.mkdir(path)),
8344
+ exists: (path) => run2(() => env.exists(path)),
8345
+ fileSize: (path) => run2(() => env.fileSize(path)),
8346
+ close: () => env.close()
8347
+ };
8348
+ return opened;
7751
8349
  }
7752
8350
  };
7753
8351
 
7754
8352
  // src/server/service/bot/machines.ts
7755
- import { randomUUID as randomUUID17 } from "node:crypto";
8353
+ import { randomUUID as randomUUID18 } from "node:crypto";
7756
8354
  import { dirname as dirname6, join as join12, posix } from "node:path";
7757
8355
 
7758
8356
  // src/server/infra/env/ssh.ts
7759
8357
  import { spawn as spawn2 } from "node:child_process";
7760
8358
  import { mkdirSync as mkdirSync6, rmSync as rmSync2, writeFileSync as writeFileSync3 } from "node:fs";
7761
- import { mkdir as mkdir5, mkdtemp as mkdtemp2, readFile as readFile3, rm as rm4 } from "node:fs/promises";
8359
+ import { mkdir as mkdir5, mkdtemp as mkdtemp2, readFile as readFile3, rm as rm5 } from "node:fs/promises";
7762
8360
  import { join as join11 } from "node:path";
7763
8361
  import { HostKeyChangedError, HostKeyUnknownError, RemoteExecutionEnv as RemoteExecutionEnv2, detectPlatform, scanHostKey, sshArguments, sshConnection } from "@earendil-works/pi-env";
7764
8362
  var SshFault = class extends Error {
@@ -7788,7 +8386,7 @@ async function generateKey(runtime, comment) {
7788
8386
  await run("ssh-keygen", ["-q", "-t", "ed25519", "-N", "", "-C", comment, "-f", join11(directory, "key")]);
7789
8387
  return { privateKey: await readFile3(join11(directory, "key"), "utf8"), publicKey: (await readFile3(join11(directory, "key.pub"), "utf8")).trim() };
7790
8388
  } finally {
7791
- await rm4(directory, { recursive: true, force: true });
8389
+ await rm5(directory, { recursive: true, force: true });
7792
8390
  }
7793
8391
  }
7794
8392
  function publicKeyOf(privateKey, comment) {
@@ -7879,7 +8477,7 @@ async function withFiles(access, work) {
7879
8477
  try {
7880
8478
  return await work(writeFilesSync(directory, access));
7881
8479
  } finally {
7882
- await rm4(directory, { recursive: true, force: true });
8480
+ await rm5(directory, { recursive: true, force: true });
7883
8481
  }
7884
8482
  }
7885
8483
  function writeFilesSync(directory, access) {
@@ -7931,23 +8529,23 @@ function run(program, args, stdin = "", env) {
7931
8529
  }
7932
8530
  function classify(error, address2, how = "key") {
7933
8531
  if (error instanceof SshFault) return error;
7934
- const text4 = error instanceof Error ? error.message : String(error), where2 = `${address2.user}@${address2.host}:${address2.port}`;
7935
- if (error instanceof HostKeyChangedError || /REMOTE HOST IDENTIFICATION HAS CHANGED|Host key verification failed/.test(text4))
8532
+ const text5 = error instanceof Error ? error.message : String(error), where2 = `${address2.user}@${address2.host}:${address2.port}`;
8533
+ if (error instanceof HostKeyChangedError || /REMOTE HOST IDENTIFICATION HAS CHANGED|Host key verification failed/.test(text5))
7936
8534
  return new SshFault("ssh_host_key_changed", `The host key of ${where2} is not the one accepted`);
7937
8535
  if (error instanceof HostKeyUnknownError) return new SshFault("ssh_host_key_changed", `The host key of ${where2} is not the one accepted`);
7938
- const denied = /Permission denied \(([^)]*)\)/.exec(text4);
7939
- if (denied || /Permission denied|Too many authentication failures/.test(text4)) {
8536
+ const denied = /Permission denied \(([^)]*)\)/.exec(text5);
8537
+ if (denied || /Permission denied|Too many authentication failures/.test(text5)) {
7940
8538
  const methods = denied ? denied[1].split(",").map((method) => method.trim()) : [];
7941
8539
  if (how === "password" && methods.length && !methods.some((method) => method === "password" || method === "keyboard-interactive"))
7942
8540
  return new SshFault("ssh_password_refused", `${where2} doesn't accept password logins`);
7943
8541
  if (how === "key") return new SshFault("ssh_key_refused", `${where2} didn't accept Jarvy's key`);
7944
8542
  return new SshFault("ssh_auth_failed", `${where2} refused the password`);
7945
8543
  }
7946
- if (/Connection refused|timed out|No route to host|Could not resolve|Network is unreachable|Connection closed|No host key received/i.test(text4))
7947
- return new SshFault("ssh_unreachable", `${where2} can't be reached: ${text4.split("\n").pop()}`);
7948
- return new SshFault("ssh_failed", text4.split("\n").slice(-3).join(" ").slice(0, 300) || `ssh ${where2} failed`);
8544
+ if (/Connection refused|timed out|No route to host|Could not resolve|Network is unreachable|Connection closed|No host key received/i.test(text5))
8545
+ return new SshFault("ssh_unreachable", `${where2} can't be reached: ${text5.split("\n").pop()}`);
8546
+ return new SshFault("ssh_failed", text5.split("\n").slice(-3).join(" ").slice(0, 300) || `ssh ${where2} failed`);
7949
8547
  }
7950
- var quote = (text4) => `'${text4.replaceAll("'", "'\\''")}'`;
8548
+ var quote = (text5) => `'${text5.replaceAll("'", "'\\''")}'`;
7951
8549
 
7952
8550
  // src/server/service/bot/machines.ts
7953
8551
  var HOST = /^(?!-)[A-Za-z0-9._:-]{1,253}$/;
@@ -7979,7 +8577,7 @@ var SshMachines = class {
7979
8577
  async prepareKey() {
7980
8578
  const now = Date.now();
7981
8579
  for (const [keyId2, key2] of this.prepared) if (key2.until < now) this.prepared.delete(keyId2);
7982
- const id = randomUUID17(), key = await this.wrap(() => generateKey(this.runtime, keyComment(id)));
8580
+ const id = randomUUID18(), key = await this.wrap(() => generateKey(this.runtime, keyComment(id)));
7983
8581
  this.prepared.set(id, { id, ...key, until: now + PREPARED_MS });
7984
8582
  return { keyId: id, publicKey: key.publicKey };
7985
8583
  }
@@ -7995,7 +8593,7 @@ var SshMachines = class {
7995
8593
  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");
7996
8594
  if (!input.hostKeys.keys.length) throw new ChatError(400, "The host keys are missing; scan the machine first");
7997
8595
  directoryIn("/", input.directory);
7998
- const id = prepared && !password ? prepared.id : randomUUID17(), comment = keyComment(id);
8596
+ const id = prepared && !password ? prepared.id : randomUUID18(), comment = keyComment(id);
7999
8597
  const record3 = await this.wrap(async () => {
8000
8598
  const key = prepared && !password ? prepared : await generateKey(this.runtime, comment);
8001
8599
  const access = { ...where2, runtime: this.runtime, alias: alias(id), hostKeys: input.hostKeys.keys, privateKey: key.privateKey };
@@ -8097,7 +8695,7 @@ var SshMachines = class {
8097
8695
  this.access(record3),
8098
8696
  record3.directory ?? home,
8099
8697
  posix.join(home, ".cache", "jarvy", botId),
8100
- (text4) => console.error(`[ssh ${record3.name}] ${text4.trim()}`)
8698
+ (text5) => console.error(`[ssh ${record3.name}] ${text5.trim()}`)
8101
8699
  );
8102
8700
  }
8103
8701
  /** What a Bot is told of a machine. */
@@ -8269,29 +8867,29 @@ var HistoryRepository = class {
8269
8867
  const result = this.db.prepare("INSERT INTO history(timestamp,role,model,usage,message,extra) VALUES(?,?,?,?,?,?)").run(message.timestamp, message.role, json(message.model), json(message.usage), JSON.stringify(message.message), JSON.stringify(message.extra));
8270
8868
  return Number(result.lastInsertRowid);
8271
8869
  }
8272
- page(before, limit) {
8273
- const rows3 = before === void 0 ? this.db.prepare(`${SUMMARY} ORDER BY id DESC LIMIT ?`).all(limit + 1) : this.db.prepare(`${SUMMARY} WHERE id<? ORDER BY id DESC LIMIT ?`).all(before, limit + 1);
8274
- const items = rows3.slice(0, limit).map((row) => this.decodeSummary(row));
8275
- return { items, nextBefore: rows3.length > limit ? items.at(-1).id : null };
8870
+ page(before, limit2) {
8871
+ const rows3 = before === void 0 ? this.db.prepare(`${SUMMARY} ORDER BY id DESC LIMIT ?`).all(limit2 + 1) : this.db.prepare(`${SUMMARY} WHERE id<? ORDER BY id DESC LIMIT ?`).all(before, limit2 + 1);
8872
+ const items = rows3.slice(0, limit2).map((row2) => this.decodeSummary(row2));
8873
+ return { items, nextBefore: rows3.length > limit2 ? items.at(-1).id : null };
8276
8874
  }
8277
- timeline(before, limit) {
8875
+ timeline(before, limit2) {
8278
8876
  const condition = !before ? "" : before.kind === "history" ? "WHERE timestamp<? OR (timestamp=? AND id<?)" : "WHERE timestamp<?";
8279
8877
  const cursor2 = !before ? [] : before.kind === "history" ? [before.timestamp, before.timestamp, before.id] : [before.timestamp];
8280
- const rows3 = this.db.prepare(`${SUMMARY} ${condition} ORDER BY timestamp DESC,id DESC LIMIT ?`).all(...cursor2, limit);
8281
- return rows3.map((row) => this.decodeSummary(row));
8878
+ const rows3 = this.db.prepare(`${SUMMARY} ${condition} ORDER BY timestamp DESC,id DESC LIMIT ?`).all(...cursor2, limit2);
8879
+ return rows3.map((row2) => this.decodeSummary(row2));
8282
8880
  }
8283
8881
  summaries(ids) {
8284
8882
  const query = this.db.prepare(`${SUMMARY} WHERE id=?`);
8285
8883
  return ids.map((id) => {
8286
- const row = query.get(id);
8287
- return row ? this.decodeSummary(row) : null;
8884
+ const row2 = query.get(id);
8885
+ return row2 ? this.decodeSummary(row2) : null;
8288
8886
  });
8289
8887
  }
8290
8888
  details(ids) {
8291
8889
  const query = this.db.prepare("SELECT id,timestamp,role,model,message FROM history WHERE id=?");
8292
8890
  return ids.map((id) => {
8293
- const row = query.get(id);
8294
- return row ? { ...row, model: row.model === null ? null : JSON.parse(row.model), message: JSON.parse(row.message) } : null;
8891
+ const row2 = query.get(id);
8892
+ return row2 ? { ...row2, model: row2.model === null ? null : JSON.parse(row2.model), message: JSON.parse(row2.message) } : null;
8295
8893
  });
8296
8894
  }
8297
8895
  /** Model calls since a time, per provider and model: every assistant record with usage is one call. */
@@ -8302,15 +8900,15 @@ var HistoryRepository = class {
8302
8900
  COALESCE(SUM(json_extract(usage,'$.cost.total')),0) AS cost,COALESCE(MAX(json_extract(usage,'$.cost.total')),0)>0 AS priced
8303
8901
  FROM history WHERE role='assistant' AND usage IS NOT NULL AND model IS NOT NULL AND timestamp>=?
8304
8902
  GROUP BY 1,2 ORDER BY cost DESC,calls DESC`).all(since);
8305
- return rows3.map(({ priced, cost, ...row }) => ({ ...row, cost: priced ? cost : null }));
8903
+ return rows3.map(({ priced, cost, ...row2 }) => ({ ...row2, cost: priced ? cost : null }));
8306
8904
  }
8307
- decodeSummary(row) {
8308
- const tools = JSON.parse(row.tools).map(({ kind, name: name3, error }) => error ? { kind, name: name3, error: true } : { kind, name: name3 });
8309
- return { ...row, model: row.model === null ? null : JSON.parse(row.model), preview: row.preview.replace(/\s+/g, " ").trim(), tools };
8905
+ decodeSummary(row2) {
8906
+ const tools = JSON.parse(row2.tools).map(({ kind, name: name3, error }) => error ? { kind, name: name3, error: true } : { kind, name: name3 });
8907
+ return { ...row2, model: row2.model === null ? null : JSON.parse(row2.model), preview: row2.preview.replace(/\s+/g, " ").trim(), tools };
8310
8908
  }
8311
8909
  read(id) {
8312
- const row = this.db.prepare("SELECT * FROM history WHERE id=?").get(id);
8313
- return row ? this.decode(row) : null;
8910
+ const row2 = this.db.prepare("SELECT * FROM history WHERE id=?").get(id);
8911
+ return row2 ? this.decode(row2) : null;
8314
8912
  }
8315
8913
  links(record3) {
8316
8914
  const links = [];
@@ -8328,8 +8926,8 @@ var HistoryRepository = class {
8328
8926
  }
8329
8927
  return links;
8330
8928
  }
8331
- decode(row) {
8332
- return { ...row, model: row.model === null ? null : JSON.parse(row.model), usage: row.usage === null ? null : JSON.parse(row.usage), message: JSON.parse(row.message), extra: JSON.parse(row.extra) };
8929
+ decode(row2) {
8930
+ return { ...row2, model: row2.model === null ? null : JSON.parse(row2.model), usage: row2.usage === null ? null : JSON.parse(row2.usage), message: JSON.parse(row2.message), extra: JSON.parse(row2.extra) };
8333
8931
  }
8334
8932
  close() {
8335
8933
  this.db.close();
@@ -8356,11 +8954,11 @@ var Memory = class {
8356
8954
  append(message) {
8357
8955
  return this.history().append(message);
8358
8956
  }
8359
- list(before, limit = 50) {
8360
- return this.history().page(before, limit);
8957
+ list(before, limit2 = 50) {
8958
+ return this.history().page(before, limit2);
8361
8959
  }
8362
- timeline(before, limit) {
8363
- return this.history().timeline(before, limit);
8960
+ timeline(before, limit2) {
8961
+ return this.history().timeline(before, limit2);
8364
8962
  }
8365
8963
  usage(since) {
8366
8964
  return this.history().usage(since);
@@ -8456,9 +9054,12 @@ var Bot = class extends Member {
8456
9054
  status() {
8457
9055
  return this.state.snapshot();
8458
9056
  }
8459
- /** 投递已保存(A41);Bot 按需激活,交给运行层按急迫程度处理。附带的不必为它启动。 */
9057
+ /** 投递已保存(A41);Bot 按需激活,交给运行层按急迫程度处理。附带的不必为它启动:只在它正跑着一轮时插进去(A77)。 */
8460
9058
  async deliver(urgency) {
8461
- if (urgency === "digest") return;
9059
+ if (urgency === "digest") {
9060
+ if (!this.stopping && this.status().status !== "stop") this.agentRuntime.deliver(urgency);
9061
+ return;
9062
+ }
8462
9063
  if (this.stopping) await this.stopping;
8463
9064
  if (this.status().status === "stop") await this.start();
8464
9065
  this.agentRuntime.deliver(urgency);
@@ -8739,7 +9340,7 @@ var BotState = class {
8739
9340
  };
8740
9341
 
8741
9342
  // src/server/service/bot/tools.ts
8742
- import { Type as Type11 } from "typebox";
9343
+ import { Type as Type12 } from "typebox";
8743
9344
  import { Value as Value2 } from "typebox/value";
8744
9345
 
8745
9346
  // src/server/service/bot/tool-errors.ts
@@ -8844,16 +9445,16 @@ var ToolContext = class {
8844
9445
  };
8845
9446
 
8846
9447
  // src/server/service/bot/history-read.ts
8847
- import { randomUUID as randomUUID18 } from "node:crypto";
9448
+ import { randomUUID as randomUUID19 } from "node:crypto";
8848
9449
  import { Type as Type2 } from "typebox";
8849
9450
 
8850
9451
  // src/server/service/bot/history-format.ts
8851
- function fence(text4, language = "text") {
9452
+ function fence(text5, language = "text") {
8852
9453
  let length = 3;
8853
- for (const match of text4.matchAll(/`+/g)) length = Math.max(length, match[0].length + 1);
9454
+ for (const match of text5.matchAll(/`+/g)) length = Math.max(length, match[0].length + 1);
8854
9455
  const marker = "`".repeat(length);
8855
9456
  return `${marker}${language}
8856
- ${text4}
9457
+ ${text5}
8857
9458
  ${marker}`;
8858
9459
  }
8859
9460
  var jsonBlock = (value2) => fence(JSON.stringify(value2, null, 2), "json");
@@ -8924,7 +9525,7 @@ async function readHistory(memory, workspace, input, signal) {
8924
9525
  const rendered = formatHistory(result, format);
8925
9526
  signal?.throwIfAborted();
8926
9527
  if (!input.to_file) return rendered;
8927
- const name3 = `history-${randomUUID18()}.${format === "json" ? "json" : "md"}`;
9528
+ const name3 = `history-${randomUUID19()}.${format === "json" ? "json" : "md"}`;
8928
9529
  return workspace.writeTempFile(name3, rendered, signal);
8929
9530
  }
8930
9531
 
@@ -8955,7 +9556,7 @@ var done = (chat) => chat.type === "thread" && chat.status === "done";
8955
9556
  var name2 = (chats) => (memberId) => chats.member(memberId)?.name ?? memberId;
8956
9557
  function chatRows(chats, botId) {
8957
9558
  const sections = new Map(chats.channels.sections(botId).map((section) => [section.id, section.name]));
8958
- const row = (chat) => ({
9559
+ const row2 = (chat) => ({
8959
9560
  id: chat.id,
8960
9561
  type: chat.type,
8961
9562
  title: chat.title,
@@ -8970,7 +9571,7 @@ function chatRows(chats, botId) {
8970
9571
  ...chat.note ? { noticeVersion: chat.note.version } : {},
8971
9572
  updatedAt: iso(chat.updatedAt)
8972
9573
  });
8973
- return [...chats.list(botId).map(row), ...chats.threads.followedChats(botId).map(row)];
9574
+ return [...chats.list(botId).map(row2), ...chats.threads.followedChats(botId).map(row2)];
8974
9575
  }
8975
9576
  function chatDetail(chats, botId, chat) {
8976
9577
  const section = chat.sectionId ? chats.channels.sections(botId).find((item) => item.id === chat.sectionId)?.name : void 0;
@@ -9254,10 +9855,10 @@ async function rows2(context2, messages, signal) {
9254
9855
  signal?.throwIfAborted();
9255
9856
  return messages.map((message) => messageRow(context2.chats, context2.bot, message, listing(message)));
9256
9857
  }
9257
- async function page(context2, chatId, messages, limit, signal) {
9258
- const shown = messages.slice(0, limit);
9858
+ async function page(context2, chatId, messages, limit2, signal) {
9859
+ const shown = messages.slice(0, limit2);
9259
9860
  if (shown.length) context2.chats.channels.markRead(context2.bot, chatId, shown.at(-1).sequence);
9260
- return { messages: await rows2(context2, shown, signal), ...messages.length > limit ? { hasMore: true } : {} };
9861
+ return { messages: await rows2(context2, shown, signal), ...messages.length > limit2 ? { hasMore: true } : {} };
9261
9862
  }
9262
9863
  var messageTools = [
9263
9864
  tool(
@@ -9273,7 +9874,7 @@ var messageTools = [
9273
9874
  }, { additionalProperties: false }),
9274
9875
  async (input, context2, { signal }) => {
9275
9876
  const chats = context2.chats, bot = context2.bot;
9276
- const limit = input.limit ?? 30, modes = [input.after !== void 0, input.around !== void 0, input.unread === true].filter(Boolean).length;
9877
+ const limit2 = input.limit ?? 30, modes = [input.after !== void 0, input.around !== void 0, input.unread === true].filter(Boolean).length;
9277
9878
  if (modes > 1) throw new ChatError(400, "Use one of after, around or unread");
9278
9879
  if (input.around !== void 0) {
9279
9880
  const messages2 = chats.around(bot, input.around, input.context ?? 5);
@@ -9282,7 +9883,7 @@ var messageTools = [
9282
9883
  }
9283
9884
  if (input.unread && input.chatId === void 0) {
9284
9885
  const found = [];
9285
- let left = limit, more = false;
9886
+ let left = limit2, more = false;
9286
9887
  for (const chat2 of chats.channels.digest(bot).unread) {
9287
9888
  if (!left) {
9288
9889
  more = true;
@@ -9297,8 +9898,8 @@ var messageTools = [
9297
9898
  }
9298
9899
  if (input.chatId === void 0) throw new ChatError(400, "chatId is required unless unread=true");
9299
9900
  const chat = context2.chat(input.chatId);
9300
- const messages = input.unread ? chats.unreadMessages(bot, chat.id, limit + 1) : chats.read(bot, chat.id, input.after ?? 0, limit + 1);
9301
- return { chatId: chat.id, ...await page(context2, chat.id, messages, limit, signal) };
9901
+ const messages = input.unread ? chats.unreadMessages(bot, chat.id, limit2 + 1) : chats.read(bot, chat.id, input.after ?? 0, limit2 + 1);
9902
+ return { chatId: chat.id, ...await page(context2, chat.id, messages, limit2, signal) };
9302
9903
  }
9303
9904
  ),
9304
9905
  // `mine`: only what this Bot sent; those messages carry the History ID of the turn that sent them (A21).
@@ -9427,10 +10028,10 @@ async function viewFile(file, contents) {
9427
10028
  if (file.size > PICTURE_LIMIT) throw new ChatError(400, "This picture is over 4 MB; use files.get and look at a smaller copy");
9428
10029
  return { kind: "media", text: head, images: [{ data: (await contents.read(file, PICTURE_LIMIT)).toString("base64"), mimeType: picture }] };
9429
10030
  }
9430
- const text4 = file.size <= TEXT_LIMIT2 ? asText(await contents.read(file, TEXT_LIMIT2)) : null;
9431
- if (text4 !== null) return `${head}
10031
+ const text5 = file.size <= TEXT_LIMIT2 ? asText(await contents.read(file, TEXT_LIMIT2)) : null;
10032
+ if (text5 !== null) return `${head}
9432
10033
 
9433
- ${text4}`;
10034
+ ${text5}`;
9434
10035
  throw new ChatError(400, "files.view shows pictures and text files up to 64 KB; use files.get to work on this file with your tools");
9435
10036
  }
9436
10037
  function pictureType(data) {
@@ -9629,29 +10230,81 @@ var taskTools = [
9629
10230
  )
9630
10231
  ];
9631
10232
 
9632
- // src/server/service/bot/tools/agent.ts
10233
+ // src/server/service/bot/tools/ports.ts
9633
10234
  import { Type as Type10 } from "typebox";
9634
- var Name2 = Type10.String({ minLength: 1, maxLength: 80, description: "Unique among all members, people and Bots" });
9635
- var BotName = Type10.String({ minLength: 1, maxLength: 80, description: "The Bot's name, as bots.list shows it" });
9636
- var Model2 = Type10.Object({
9637
- connection: Type10.String({ minLength: 1, maxLength: 200, description: "A connection's name or ID from models.list" }),
9638
- model: Type10.String({ minLength: 1, maxLength: 200, description: "A model's ID or name in that connection" })
10235
+ var Port = Type10.Integer({ minimum: 1, maximum: 65535, description: "The port your service listens on in your sandbox" });
10236
+ var LOOPBACK = "Something listens on this port only on 127.0.0.1 (or ::1) in your sandbox, which the link can't reach: restart it listening on 0.0.0.0, such as vite --host 0.0.0.0, next dev -H 0.0.0.0 or python3 -m http.server --bind 0.0.0.0.";
10237
+ var NOTE = {
10238
+ live: "Your service is reachable through the link.",
10239
+ local: LOOPBACK,
10240
+ idle: "Nothing listens on this port in your sandbox yet; the link works once your service listens there on 0.0.0.0.",
10241
+ stopped: "Your sandbox isn't running; the link works once it runs and your service listens there.",
10242
+ unknown: "Whether your service listens there couldn't be checked."
10243
+ };
10244
+ function row(context2, port) {
10245
+ const { fromSandboxes } = context2.scope.ports.links(port.hostPort);
10246
+ return {
10247
+ port: port.port,
10248
+ name: port.name,
10249
+ description: port.description,
10250
+ url: port.urls[0] ?? null,
10251
+ ...port.urls.length > 1 ? { otherUrls: port.urls.slice(1) } : {},
10252
+ ...fromSandboxes ? { fromSandboxes } : {},
10253
+ state: port.state,
10254
+ note: NOTE[port.state]
10255
+ };
10256
+ }
10257
+ var portTools = [
10258
+ tool(
10259
+ "ports.publish",
10260
+ "Publish a web service that runs in your sandbox, so people can open it in their browser and other Bots of this chat platform can reach it. You get a link (url) on this chat platform's own port for it: send it in the chat for people; other Bots use fromSandboxes. Only people signed in to this chat platform and its Bots can open it. The service has to listen on 0.0.0.0, not only on 127.0.0.1. Publishing a port again changes its name and description and keeps its link. It stays published until you unpublish it or your sandbox is reset; your owner sees it in your Bot info and can unpublish it there.",
10261
+ Type10.Object({
10262
+ port: Port,
10263
+ name: Type10.String({ minLength: 1, maxLength: 60, description: "A short name, in your owner's language, such as the app's name" }),
10264
+ description: Type10.String({ minLength: 1, maxLength: 300, description: "What it is and what to look at there, in your owner's language" })
10265
+ }, { additionalProperties: false }),
10266
+ async (input, context2) => row(context2, await context2.scope.ports.publish(context2.bot, input))
10267
+ ),
10268
+ tool(
10269
+ "ports.list",
10270
+ "List the ports you published: each one's port, name, description, link, the link other Bots use (fromSandboxes), and state: live (reachable), local (listening on 127.0.0.1 only, which the link can't reach), idle (nothing listens), stopped (your sandbox isn't running).",
10271
+ None,
10272
+ async (_args, context2) => ({ ports: (await context2.scope.ports.list(context2.bot)).map((port) => row(context2, port)) })
10273
+ ),
10274
+ tool(
10275
+ "ports.unpublish",
10276
+ "Unpublish one of your ports: its link stops working. Your service keeps running in your sandbox.",
10277
+ Type10.Object({ port: Port }, { additionalProperties: false }),
10278
+ ({ port }, context2) => {
10279
+ context2.scope.ports.unpublish(context2.bot, port);
10280
+ return { port, unpublished: true };
10281
+ }
10282
+ )
10283
+ ];
10284
+
10285
+ // src/server/service/bot/tools/agent.ts
10286
+ import { Type as Type11 } from "typebox";
10287
+ var Name2 = Type11.String({ minLength: 1, maxLength: 80, description: "Unique among all members, people and Bots" });
10288
+ var BotName = Type11.String({ minLength: 1, maxLength: 80, description: "The Bot's name, as bots.list shows it" });
10289
+ var Model2 = Type11.Object({
10290
+ connection: Type11.String({ minLength: 1, maxLength: 200, description: "A connection's name or ID from models.list" }),
10291
+ model: Type11.String({ minLength: 1, maxLength: 200, description: "A model's ID or name in that connection" })
9639
10292
  }, { additionalProperties: false });
9640
- var Thinking = Type10.Union(
9641
- ["auto", "off", "minimal", "low", "medium", "high", "xhigh", "max"].map((level) => Type10.Literal(level)),
10293
+ var Thinking = Type11.Union(
10294
+ ["auto", "off", "minimal", "low", "medium", "high", "xhigh", "max"].map((level) => Type11.Literal(level)),
9642
10295
  { description: "How much the model thinks; auto lets this chat platform choose. models.list gives the levels each model has" }
9643
10296
  );
9644
10297
  var Settings = {
9645
- description: Type10.Optional(Type10.String({ maxLength: 1e3, description: "One line that other members see in members.list" })),
9646
- persona: Type10.Optional(Type10.String({ maxLength: 16e3, description: "Its standing instructions: who it is and how it works (the Persona section of its prompt)" })),
9647
- model: Type10.Optional(Model2),
9648
- thinking: Type10.Optional(Thinking)
10298
+ description: Type11.Optional(Type11.String({ maxLength: 1e3, description: "One line that other members see in members.list" })),
10299
+ persona: Type11.Optional(Type11.String({ maxLength: 16e3, description: "Its standing instructions: who it is and how it works (the Persona section of its prompt)" })),
10300
+ model: Type11.Optional(Model2),
10301
+ thinking: Type11.Optional(Thinking)
9649
10302
  };
9650
10303
  var Confirm = {
9651
- askIn: Type10.String({ description: "ID of a chat the owner is in, where the confirmation card goes; usually the chat you were asked in" }),
9652
- question: Type10.String({ minLength: 1, maxLength: 500, description: "The card's question, in the owner's language: what will be deleted and what goes with it" }),
9653
- confirm: Type10.String({ minLength: 1, maxLength: 40, description: "Label of the button that deletes, in the owner's language" }),
9654
- cancel: Type10.String({ minLength: 1, maxLength: 40, description: "Label of the button that keeps everything, in the owner's language" })
10304
+ askIn: Type11.String({ description: "ID of a chat the owner is in, where the confirmation card goes; usually the chat you were asked in" }),
10305
+ question: Type11.String({ minLength: 1, maxLength: 500, description: "The card's question, in the owner's language: what will be deleted and what goes with it" }),
10306
+ confirm: Type11.String({ minLength: 1, maxLength: 40, description: "Label of the button that deletes, in the owner's language" }),
10307
+ cancel: Type11.String({ minLength: 1, maxLength: 40, description: "Label of the button that keeps everything, in the owner's language" })
9655
10308
  };
9656
10309
  var after = "The owner first gets a card in askIn, and nothing is deleted unless they confirm; the result says what they chose.";
9657
10310
  var ops = (context2) => context2.scope.agent;
@@ -9682,7 +10335,7 @@ var agentTools = [
9682
10335
  tool(
9683
10336
  "bots.create",
9684
10337
  "Create a Bot. Give it a model so it can work; without one it waits until it gets one. It starts when a message first arrives for it. bots.update changes everything later.",
9685
- Type10.Object({ name: Name2, ...Settings }, { additionalProperties: false }),
10338
+ Type11.Object({ name: Name2, ...Settings }, { additionalProperties: false }),
9686
10339
  async (settings, context2) => {
9687
10340
  const agent = ops(context2);
9688
10341
  const bot = await agent.create(settings);
@@ -9692,7 +10345,7 @@ var agentTools = [
9692
10345
  tool(
9693
10346
  "bots.update",
9694
10347
  "Change a Bot's name, description, persona, model or thinking; give only what changes. Works for any Bot, yourself included. The Bot is told what changed with its next batch.",
9695
- Type10.Object({ bot: BotName, name: Type10.Optional(Name2), ...Settings }, { additionalProperties: false }),
10348
+ Type11.Object({ bot: BotName, name: Type11.Optional(Name2), ...Settings }, { additionalProperties: false }),
9696
10349
  async ({ bot, ...settings }, context2) => {
9697
10350
  const result = await ops(context2).update(ops(context2).bot(bot), settings);
9698
10351
  return { name: result.bot.name, changed: result.changed };
@@ -9701,7 +10354,7 @@ var agentTools = [
9701
10354
  tool(
9702
10355
  "bots.start",
9703
10356
  "Start a stopped Bot. A stopped Bot also starts by itself when a message arrives for it.",
9704
- Type10.Object({ bot: BotName }, { additionalProperties: false }),
10357
+ Type11.Object({ bot: BotName }, { additionalProperties: false }),
9705
10358
  async (input, context2) => {
9706
10359
  const bot = ops(context2).bot(input.bot);
9707
10360
  return { name: bot.name, status: await ops(context2).run(bot, "start") };
@@ -9710,7 +10363,7 @@ var agentTools = [
9710
10363
  tool(
9711
10364
  "bots.stop",
9712
10365
  "Stop a Bot (not yourself): its current turn ends, and it stays stopped until it is started or a message arrives for it.",
9713
- Type10.Object({ bot: BotName }, { additionalProperties: false }),
10366
+ Type11.Object({ bot: BotName }, { additionalProperties: false }),
9714
10367
  async (input, context2) => {
9715
10368
  const bot = ops(context2).bot(input.bot);
9716
10369
  return { name: bot.name, status: await ops(context2).run(bot, "stop") };
@@ -9719,7 +10372,7 @@ var agentTools = [
9719
10372
  tool(
9720
10373
  "bots.usage",
9721
10374
  "A Bot's token use and cost over the last days (7 by default), per model.",
9722
- Type10.Object({ bot: BotName, days: Type10.Optional(Type10.Integer({ minimum: 1, maximum: 365 })) }, { additionalProperties: false }),
10375
+ Type11.Object({ bot: BotName, days: Type11.Optional(Type11.Integer({ minimum: 1, maximum: 365 })) }, { additionalProperties: false }),
9723
10376
  (input, context2) => {
9724
10377
  const bot = ops(context2).bot(input.bot);
9725
10378
  return { name: bot.name, ...ops(context2).usage(bot, input.days ?? 7) };
@@ -9728,7 +10381,7 @@ var agentTools = [
9728
10381
  tool(
9729
10382
  "bots.delete",
9730
10383
  `Delete a Bot (not yourself). ${after} Its DMs and channel memberships go; its messages in channels and its workspace stay.`,
9731
- Type10.Object({ bot: BotName, ...Confirm }, { additionalProperties: false }),
10384
+ Type11.Object({ bot: BotName, ...Confirm }, { additionalProperties: false }),
9732
10385
  async (input, context2, call) => {
9733
10386
  const agent = ops(context2), bot = agent.bot(input.bot);
9734
10387
  if (bot.id === agent.agentId) throw new ChatError(400, "You can't delete yourself");
@@ -9741,7 +10394,7 @@ var agentTools = [
9741
10394
  tool(
9742
10395
  "chats.delete",
9743
10396
  `Delete a channel you are in, with its threads, messages and files. ${after}`,
9744
- Type10.Object({ chatId: Type10.String({ description: "Channel ID" }), ...Confirm }, { additionalProperties: false }),
10397
+ Type11.Object({ chatId: Type11.String({ description: "Channel ID" }), ...Confirm }, { additionalProperties: false }),
9745
10398
  async (input, context2, call) => {
9746
10399
  const chat = channel(context2, input.chatId);
9747
10400
  const declined = await confirmed(context2, input, call);
@@ -9753,7 +10406,7 @@ var agentTools = [
9753
10406
  tool(
9754
10407
  "chats.removeMembers",
9755
10408
  `Remove members from a channel you are in, by name (see members.list). ${after}`,
9756
- Type10.Object({ chatId: Type10.String({ description: "Channel ID" }), members: Type10.Array(Type10.String({ minLength: 1, maxLength: 80 }), { minItems: 1, maxItems: 50 }), ...Confirm }, { additionalProperties: false }),
10409
+ Type11.Object({ chatId: Type11.String({ description: "Channel ID" }), members: Type11.Array(Type11.String({ minLength: 1, maxLength: 80 }), { minItems: 1, maxItems: 50 }), ...Confirm }, { additionalProperties: false }),
9757
10410
  async (input, context2, call) => {
9758
10411
  const chat = channel(context2, input.chatId);
9759
10412
  const members = context2.members(input.members).filter((member) => chat.memberIds.includes(member.id));
@@ -9767,7 +10420,7 @@ var agentTools = [
9767
10420
  tool(
9768
10421
  "sections.delete",
9769
10422
  `Delete a channel group; its channels stay, outside any group. ${after}`,
9770
- Type10.Object({ section: Type10.String({ minLength: 1, maxLength: 80, description: "The group's name or ID (see sections.list)" }), ...Confirm }, { additionalProperties: false }),
10423
+ Type11.Object({ section: Type11.String({ minLength: 1, maxLength: 80, description: "The group's name or ID (see sections.list)" }), ...Confirm }, { additionalProperties: false }),
9771
10424
  async (input, context2, call) => {
9772
10425
  const section = context2.section(input.section);
9773
10426
  const declined = await confirmed(context2, input, call);
@@ -9779,11 +10432,11 @@ var agentTools = [
9779
10432
  ];
9780
10433
 
9781
10434
  // src/server/service/bot/tools.ts
9782
- var EVERY_BOT = [...historyTools, ...chatTools, ...messageTools, ...fileTools, ...environmentTools2, ...secretTools, ...taskTools];
9783
- var Gateway = Type11.Object({
9784
- action: Type11.Unsafe({ type: "string", enum: ["list", "describe", "call"] }),
9785
- tool: Type11.Optional(Type11.String()),
9786
- args: Type11.Optional(Type11.Unsafe({ type: "object" }))
10435
+ var EVERY_BOT = [...historyTools, ...chatTools, ...messageTools, ...fileTools, ...environmentTools2, ...portTools, ...secretTools, ...taskTools];
10436
+ var Gateway = Type12.Object({
10437
+ action: Type12.Unsafe({ type: "string", enum: ["list", "describe", "call"] }),
10438
+ tool: Type12.Optional(Type12.String()),
10439
+ args: Type12.Optional(Type12.Unsafe({ type: "object" }))
9787
10440
  }, { additionalProperties: false });
9788
10441
  var Tools = class {
9789
10442
  send;
@@ -9834,13 +10487,13 @@ var welcomeLanguage = (value2) => typeof value2 === "string" && Object.hasOwn(WE
9834
10487
 
9835
10488
  // src/server/service/chat/envelope.ts
9836
10489
  var INVALID_XML = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\ud800-\udfff\ufffe\uffff]/u;
9837
- function escape(value2) {
10490
+ function escape2(value2) {
9838
10491
  if (INVALID_XML.test(value2)) throw new Error("Invalid XML character");
9839
10492
  return value2.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\r/g, "&#13;");
9840
10493
  }
9841
10494
  function attribute(name3, value2) {
9842
10495
  if (!value2.trim()) throw new Error(`Missing ${name3}`);
9843
- return ` ${name3}="${escape(value2).replace(/"/g, "&quot;").replace(/\n/g, "&#10;").replace(/\t/g, "&#9;")}"`;
10496
+ return ` ${name3}="${escape2(value2).replace(/"/g, "&quot;").replace(/\n/g, "&#10;").replace(/\t/g, "&#9;")}"`;
9844
10497
  }
9845
10498
  function remember(map, id, definition) {
9846
10499
  const old = map.get(id);
@@ -9862,7 +10515,7 @@ function encodeNoticeBatch(botId, notices, environment, prompt, unreadNotes) {
9862
10515
  function instructionsLine(change) {
9863
10516
  if (!change) return "";
9864
10517
  const how = change.whole ? "The new text of the changed parts follows." : "Lines starting with - were removed and lines starting with + were added; the others are context.";
9865
- return `<instructions_changed${attribute("parts", change.parts.join(", "))}>${escape(change.diff)}<note>Your instructions changed. ${how} Follow the new version from now on. Your system prompt is not rewritten yet, so that the conversation stays cached; it will be after the next compaction.</note></instructions_changed>`;
10518
+ return `<instructions_changed${attribute("parts", change.parts.join(", "))}>${escape2(change.diff)}<note>Your instructions changed. ${how} Follow the new version from now on. Your system prompt is not rewritten yet, so that the conversation stays cached; it will be after the next compaction.</note></instructions_changed>`;
9866
10519
  }
9867
10520
  function noticeTail(notices, chats, members) {
9868
10521
  for (const notice of notices) {
@@ -9881,14 +10534,14 @@ function noticeTail(notices, chats, members) {
9881
10534
  const latest = /* @__PURE__ */ new Map();
9882
10535
  for (const notice of notices) if (notice.kind === "note" && (latest.get(notice.chat.id)?.version ?? 0) < notice.version) latest.set(notice.chat.id, notice);
9883
10536
  const updates = [...latest.values()].map((note) => `<${note.chat.type === "thread" ? "summary" : "notice"}${attribute("chat", note.chat.id)}${attribute("version", String(note.version))}${attribute("by", note.actor.id)}${attribute("at", iso2(note.createdAt))}/>`);
9884
- const marks = notices.flatMap((notice) => notice.kind !== "reaction" ? [] : [`<reaction${attribute("message", notice.message.id)}${attribute("chat", notice.chat.id)}${attribute("by", notice.actor.id)}${attribute("emoji", notice.emoji)}${attribute("at", iso2(notice.createdAt))}>${escape(notice.message.text)}</reaction>`]);
10537
+ const marks = notices.flatMap((notice) => notice.kind !== "reaction" ? [] : [`<reaction${attribute("message", notice.message.id)}${attribute("chat", notice.chat.id)}${attribute("by", notice.actor.id)}${attribute("emoji", notice.emoji)}${attribute("at", iso2(notice.createdAt))}>${escape2(notice.message.text)}</reaction>`]);
9885
10538
  const changed = notices.filter((notice) => notice.kind === "change");
9886
- const changes = changed.map((notice) => `<change${attribute("by", notice.actor.id)}${attribute("what", notice.what)}${attribute("at", iso2(notice.createdAt))}${notice.value === null ? "/>" : `>${escape(notice.value)}</change>`}`);
10539
+ const changes = changed.map((notice) => `<change${attribute("by", notice.actor.id)}${attribute("what", notice.what)}${attribute("at", iso2(notice.createdAt))}${notice.value === null ? "/>" : `>${escape2(notice.value)}</change>`}`);
9887
10540
  const agent = changed.find((notice) => notice.actor.kind === "bot")?.actor, byOwner = changed.some((notice) => notice.actor.kind === "user");
9888
- const onboard = notices.flatMap((notice) => notice.kind !== "onboard" ? [] : [`<onboard${attribute("by", notice.actor.id)}${attribute("at", iso2(notice.createdAt))}><note>${escape(welcome(notice.actor.name, notice.language))}</note></onboard>`]).slice(-1);
10541
+ const onboard = notices.flatMap((notice) => notice.kind !== "onboard" ? [] : [`<onboard${attribute("by", notice.actor.id)}${attribute("at", iso2(notice.createdAt))}><note>${escape2(welcome(notice.actor.name, notice.language))}</note></onboard>`]).slice(-1);
9889
10542
  const followed = notices.flatMap((notice) => notice.kind !== "followed" ? [] : [`<followed${attribute("total", String(notice.total))}>` + notice.threads.map((thread) => `<thread${attribute("chat", thread.id)}${attribute("at", iso2(thread.lastReplyAt))}${thread.unread ? attribute("unread", String(thread.unread)) : ""}/>`).join("") + `<note>Your conversation was compacted. These are the open threads you follow, most recently active first` + (notice.total > notice.threads.length ? ` (${notice.threads.length} of ${notice.total}; chats.list lists them all)` : "") + ". Before you act in one, read its summary with chats.info and what you missed with messages.read. Unfollow a thread that no longer concerns you with threads.follow; one that is done leaves this list by itself.</note></followed>"]).slice(-1);
9890
- const tasks = notices.flatMap((notice) => notice.kind !== "task" ? [] : [`<task${attribute("id", notice.task.id)}${attribute("name", notice.task.name)}${attribute("schedule", notice.task.schedule)}${attribute("due", iso2(notice.due))}>${escape(notice.task.instruction)}</task>`]);
9891
- return (tasks.length ? `<tasks>${tasks.join("")}<note>These are your scheduled tasks, not messages from anyone. Do what each says, then send what is worth sending, to your owner unless the task says where; if nothing is, do nothing.</note></tasks>` : "") + (updates.length ? `<updates>${updates.join("")}</updates>` : "") + (marks.length ? `<notices>${marks.join("")}<note>These are reactions to your messages. Act on them if you need to; no reply is required.</note></notices>` : "") + (changes.length ? `<changes>${changes.join("")}` + (agent ? `<note>The agent Bot changed your settings, and the changes already apply. A change to your name, description or persona is a change to your instructions too. If something is unclear, ask it in a DM (messages.send with to: "${escape(agent.name)}").</note>` : "") + (byOwner ? "<note>Your owner changed where you work, and it already applies: environment is where your file and command tools run now, and access is what environments.switch can take you to. If you were in the middle of something, carry on where you are now.</note>" : "") + "</changes>" : "") + onboard.join("") + followed.join("");
10543
+ const tasks = notices.flatMap((notice) => notice.kind !== "task" ? [] : [`<task${attribute("id", notice.task.id)}${attribute("name", notice.task.name)}${attribute("schedule", notice.task.schedule)}${attribute("due", iso2(notice.due))}>${escape2(notice.task.instruction)}</task>`]);
10544
+ return (tasks.length ? `<tasks>${tasks.join("")}<note>These are your scheduled tasks, not messages from anyone. Do what each says, then send what is worth sending, to your owner unless the task says where; if nothing is, do nothing.</note></tasks>` : "") + (updates.length ? `<updates>${updates.join("")}</updates>` : "") + (marks.length ? `<notices>${marks.join("")}<note>These are reactions to your messages. Act on them if you need to; no reply is required.</note></notices>` : "") + (changes.length ? `<changes>${changes.join("")}` + (agent ? `<note>The agent Bot changed your settings, and the changes already apply. A change to your name, description or persona is a change to your instructions too. If something is unclear, ask it in a DM (messages.send with to: "${escape2(agent.name)}").</note>` : "") + (byOwner ? "<note>Your owner changed where you work, and it already applies: environment is where your file and command tools run now, and access is what environments.switch can take you to. If you were in the middle of something, carry on where you are now.</note>" : "") + "</changes>" : "") + onboard.join("") + followed.join("");
9892
10545
  }
9893
10546
  function encodeChatBatch(botId, messages, digest, notices, prompt) {
9894
10547
  if (!messages.length) throw new Error("Empty chat batch");
@@ -9903,9 +10556,9 @@ function encodeChatBatch(botId, messages, digest, notices, prompt) {
9903
10556
  const attributes = attribute("id", message.id) + attribute("chat", message.chat.id) + attribute("from", message.sender.id) + attribute("at", at2);
9904
10557
  const reply = message.replyTo;
9905
10558
  if (reply) remember(members, reply.sender.id, `<member${attribute("id", reply.sender.id)}${attribute("name", reply.sender.name)}${attribute("kind", reply.sender.kind)}/>`);
9906
- const quote2 = reply ? `<reply_to${attribute("message", reply.id)}${attribute("from", reply.sender.id)}>${escape(reply.text)}</reply_to>` : "";
10559
+ const quote2 = reply ? `<reply_to${attribute("message", reply.id)}${attribute("from", reply.sender.id)}>${escape2(reply.text)}</reply_to>` : "";
9907
10560
  const files = message.attachments.map((file) => `<file${attribute("id", file.id)}${attribute("name", file.name)}${attribute("type", file.type)}${attribute("size", file.size)}/>`).join("");
9908
- return `<message${attributes}>${quote2}${message.content ? `<text>${escape(message.content)}</text>` : ""}${files}</message>`;
10561
+ return `<message${attributes}>${quote2}${message.content ? `<text>${escape2(message.content)}</text>` : ""}${files}</message>`;
9909
10562
  });
9910
10563
  const ref = (key, item) => attribute(key, item.chatId) + (chats.has(item.chatId) ? "" : attribute("title", item.title) + attribute("type", item.type));
9911
10564
  let tail = (notices?.length ? noticeTail(notices, chats, members) : "") + instructionsLine(prompt);
@@ -9995,8 +10648,8 @@ var BotInbox = class {
9995
10648
  }
9996
10649
  case "reaction": {
9997
10650
  const marked = this.chats.message(this.botId, notice.messageId);
9998
- const text4 = marked ? previewQuotes(marked.content, marked.quotes).slice(0, 120) : "";
9999
- return { kind: "reaction", id, chat: notice.chat, message: { id: notice.messageId, text: text4 }, actor: notice.actor, emoji: notice.payload.emoji, createdAt };
10651
+ const text5 = marked ? previewQuotes(marked.content, marked.quotes).slice(0, 120) : "";
10652
+ return { kind: "reaction", id, chat: notice.chat, message: { id: notice.messageId, text: text5 }, actor: notice.actor, emoji: notice.payload.emoji, createdAt };
10000
10653
  }
10001
10654
  // Its conversation was compacted (A73): the open threads it follows, as they are now.
10002
10655
  case "followed": {
@@ -10010,9 +10663,11 @@ var BotInbox = class {
10010
10663
  if (notices.length) this.tell(notices);
10011
10664
  }
10012
10665
  /** Notices the Bot has not been told (A39), as the XML that goes into a steered message or a run of notices alone.
10013
- * `steer`: what may go into a running turn; a task waits for the turn to end and opens one of its own (A58). */
10014
- notices(prompt, steer = false) {
10015
- const pending = this.chats.notices(this.botId).filter((notice) => !steer || notice.kind !== "task");
10666
+ * `steer`: what may go into a running turn. A queued delivery takes everything but a task, which waits for the turn
10667
+ * to end and opens one of its own (A58); a digest (a new notice version) takes only chat activity, so the system
10668
+ * notices keep riding with the next batch (A48, A73, A77). */
10669
+ notices(prompt, steer) {
10670
+ const pending = this.chats.notices(this.botId).filter((notice) => !steer || (steer === "queue" ? notice.kind !== "task" : notice.chat !== null));
10016
10671
  if (!pending.length) return null;
10017
10672
  const { kept, stale } = this.convert(pending);
10018
10673
  if (!kept.length && !prompt) {
@@ -10101,9 +10756,14 @@ var AgentRuntime = class {
10101
10756
  }
10102
10757
  /** Something was delivered (A41); the runtime only looks at how urgent it is. Interrupt: abort the running
10103
10758
  * turn and start over with everything pending (A05). Queue: steer it into the running turn, or wake an idle
10104
- * Bot (A39). Digest: nothing now, it rides along with the next run. The delivery itself is durable. */
10759
+ * Bot (A39). Digest: chat activity into a running turn, never a wake; otherwise it rides along with the next run (A77).
10760
+ * The delivery itself is durable. */
10105
10761
  deliver(urgency) {
10106
- if (this.stopping || urgency === "digest") return;
10762
+ if (this.stopping) return;
10763
+ if (urgency === "digest") {
10764
+ if (this.prompting) this.steer("digest");
10765
+ return;
10766
+ }
10107
10767
  if (urgency === "interrupt") {
10108
10768
  this.inputVersion++;
10109
10769
  if (this.prompting) this.session?.requestAbort();
@@ -10114,12 +10774,16 @@ var AgentRuntime = class {
10114
10774
  this.wake();
10115
10775
  return;
10116
10776
  }
10777
+ this.steer("queue");
10778
+ }
10779
+ /** Puts the notices a running turn has not been told into it (A39, A77); nothing is woken. A steer that does not
10780
+ * go through leaves them for the next batch. Serialised, so two quick deliveries cannot both read the same rows. */
10781
+ steer(urgency) {
10117
10782
  this.notices = this.notices.catch(() => {
10118
10783
  }).then(async () => {
10119
- const pending = this.inbox.notices(null, true);
10120
- if (!pending || this.stopping) return;
10121
- if (this.prompting && this.session?.steer(pending.xml)) this.inbox.tell(pending.ids);
10122
- else this.wake();
10784
+ const pending = this.inbox.notices(null, urgency);
10785
+ if (!pending || this.stopping || !this.prompting) return;
10786
+ if (this.session?.steer(pending.xml)) this.inbox.tell(pending.ids);
10123
10787
  }).catch((error) => {
10124
10788
  console.error("[bot] steer failed", { botId: this.inbox.botId, error });
10125
10789
  });
@@ -10316,7 +10980,7 @@ When something will take a while, post a short progress note partway through, an
10316
10980
  function workspaceSection(setup) {
10317
10981
  return `## Workspace
10318
10982
  You have a workspace of your own: ${setup.home().cwd} on your own computer, a machine only you use. Keep in it the files you want to keep (they stay when the machine is reset), keep it tidy, and put temporary files under temp and clear them out.
10319
- Other members work on their own machines. They can't reach your files, your addresses or anything you start there, and you can't reach theirs. To give someone a file, send it as an attachment; anything else, say it in the chat.
10983
+ Other members work on their own machines. They can't reach your files, your addresses or anything you start there, and you can't reach theirs. To give someone a file, send it as an attachment; to show a web service you run in your sandbox, publish its port with ports.publish; anything else, say it in the chat.
10320
10984
 
10321
10985
  ## Work environment
10322
10986
  Your file and command tools run on your own computer: a Linux container only you use, with Node, Python, git, build tools and Playwright with its browsers. You can install software there with sudo; it stays until the owner resets your sandbox. Where the machine has no Docker, those tools are unavailable until it does, and the chat tools work as usual.
@@ -10341,8 +11005,8 @@ var LIVE_INPUT = 300;
10341
11005
  var LIVE_MESSAGE = 2e3;
10342
11006
 
10343
11007
  // src/server/service/bot/live-input.ts
10344
- function clip(text4, length) {
10345
- return text4.length > length ? `${text4.slice(0, length - 1)}\u2026` : text4;
11008
+ function clip(text5, length) {
11009
+ return text5.length > length ? `${text5.slice(0, length - 1)}\u2026` : text5;
10346
11010
  }
10347
11011
  var field = (value2, length = LIVE_INPUT) => typeof value2 === "string" ? clip(value2, length) : void 0;
10348
11012
  var record2 = (value2) => value2 && typeof value2 === "object" && !Array.isArray(value2) ? value2 : {};
@@ -10396,10 +11060,10 @@ function sentChat(output, chat) {
10396
11060
 
10397
11061
  // src/server/service/bot/live.ts
10398
11062
  var FLUSH_MS = 100;
10399
- function liveOutput(text4) {
10400
- if (!text4) return null;
10401
- if (text4.length <= 2 * LIVE_OUTPUT_END) return { head: text4, tail: "", omitted: 0 };
10402
- return { head: text4.slice(0, LIVE_OUTPUT_END), tail: text4.slice(-LIVE_OUTPUT_END), omitted: text4.length - 2 * LIVE_OUTPUT_END };
11063
+ function liveOutput(text5) {
11064
+ if (!text5) return null;
11065
+ if (text5.length <= 2 * LIVE_OUTPUT_END) return { head: text5, tail: "", omitted: 0 };
11066
+ return { head: text5.slice(0, LIVE_OUTPUT_END), tail: text5.slice(-LIVE_OUTPUT_END), omitted: text5.length - 2 * LIVE_OUTPUT_END };
10403
11067
  }
10404
11068
  var LiveTurns = class {
10405
11069
  constructor(chat) {
@@ -10456,8 +11120,8 @@ var LiveTurns = class {
10456
11120
  }
10457
11121
  const step = this.steps.get(id);
10458
11122
  if (!step) return;
10459
- const text4 = (step.text ?? "") + event.delta, over = Math.max(0, text4.length - LIVE_TEXT);
10460
- step.text = text4.slice(over);
11123
+ const text5 = (step.text ?? "") + event.delta, over = Math.max(0, text5.length - LIVE_TEXT);
11124
+ step.text = text5.slice(over);
10461
11125
  step.omitted = (step.omitted ?? 0) + over;
10462
11126
  this.queue({ op: "text", id, append: event.delta });
10463
11127
  return;
@@ -10703,6 +11367,222 @@ var AgentOps = class {
10703
11367
  }
10704
11368
  };
10705
11369
 
11370
+ // src/server/infra/net/proxy.ts
11371
+ import { request as httpRequest } from "node:http";
11372
+ import { connect, createServer as createServer2 } from "node:net";
11373
+ var HOP = /* @__PURE__ */ new Set(["connection", "keep-alive", "proxy-connection", "transfer-encoding", "te", "trailer", "upgrade", "proxy-authorization", "proxy-authenticate"]);
11374
+ function cookies(header, keep) {
11375
+ if (!header) return void 0;
11376
+ const kept = header.split(";").map((part) => part.trim()).filter((part) => part && keep(part.split("=")[0].trim()));
11377
+ return kept.length ? kept.join("; ") : void 0;
11378
+ }
11379
+ function requestHeaders(request, policy2, upgrade) {
11380
+ const headers2 = {};
11381
+ for (const [name3, value2] of Object.entries(request.headers)) {
11382
+ if (value2 === void 0 || name3 === "cookie" || name3.startsWith("x-forwarded-")) continue;
11383
+ if (HOP.has(name3) && !(upgrade && (name3 === "upgrade" || name3 === "connection"))) continue;
11384
+ headers2[name3] = value2;
11385
+ }
11386
+ const cookie2 = cookies(request.headers.cookie, policy2.cookie);
11387
+ if (cookie2) headers2.cookie = cookie2;
11388
+ headers2["x-forwarded-for"] = policy2.client.address.replace(/^::ffff:/, "");
11389
+ headers2["x-forwarded-proto"] = policy2.client.secure ? "https" : "http";
11390
+ if (request.headers.host) headers2["x-forwarded-host"] = request.headers.host;
11391
+ return headers2;
11392
+ }
11393
+ function responseHeaders(headers2, policy2) {
11394
+ const out = {};
11395
+ for (const [name3, value2] of Object.entries(headers2)) {
11396
+ if (value2 === void 0 || HOP.has(name3)) continue;
11397
+ if (name3 === "set-cookie") {
11398
+ const kept = (Array.isArray(value2) ? value2 : [value2]).filter((line) => policy2.setCookie(line.split("=")[0].trim()));
11399
+ if (kept.length) out[name3] = kept;
11400
+ continue;
11401
+ }
11402
+ out[name3] = value2;
11403
+ }
11404
+ return out;
11405
+ }
11406
+ function forward(request, response, upstream, policy2, failed) {
11407
+ const outgoing = httpRequest({ host: upstream.host, port: upstream.port, method: request.method, path: request.url, headers: requestHeaders(request, policy2, false) }, (incoming) => {
11408
+ response.writeHead(incoming.statusCode ?? 502, incoming.statusMessage, responseHeaders(incoming.headers, policy2));
11409
+ incoming.pipe(response);
11410
+ incoming.on("error", () => response.destroy());
11411
+ });
11412
+ outgoing.on("error", (error) => {
11413
+ if (response.headersSent) response.destroy();
11414
+ else failed(error);
11415
+ });
11416
+ response.on("close", () => {
11417
+ if (!response.writableFinished) outgoing.destroy();
11418
+ });
11419
+ request.pipe(outgoing);
11420
+ }
11421
+ function forwardUpgrade(request, socket, head, upstream, policy2, failed) {
11422
+ const target3 = connect(upstream.port, upstream.host);
11423
+ let joined = false;
11424
+ target3.once("connect", () => {
11425
+ joined = true;
11426
+ const headers2 = requestHeaders(request, policy2, true);
11427
+ const lines = [`${request.method} ${request.url} HTTP/${request.httpVersion}`];
11428
+ for (const [name3, value2] of Object.entries(headers2)) for (const item of Array.isArray(value2) ? value2 : [value2]) lines.push(`${name3}: ${item}`);
11429
+ target3.write(`${lines.join("\r\n")}\r
11430
+ \r
11431
+ `);
11432
+ if (head.length) target3.write(head);
11433
+ socket.pipe(target3).pipe(socket);
11434
+ });
11435
+ target3.on("error", (error) => {
11436
+ if (joined) socket.destroy();
11437
+ else failed(error);
11438
+ });
11439
+ socket.on("error", () => target3.destroy());
11440
+ socket.on("close", () => target3.destroy());
11441
+ target3.on("close", () => socket.destroy());
11442
+ }
11443
+ function portFree(host, port) {
11444
+ return new Promise((resolve5) => {
11445
+ const server = createServer2();
11446
+ server.once("error", () => resolve5(false));
11447
+ server.listen({ host, port, exclusive: true }, () => server.close(() => resolve5(true)));
11448
+ });
11449
+ }
11450
+
11451
+ // src/server/service/bot/ports.ts
11452
+ var MAX_PER_BOT = 20;
11453
+ var SESSION_COOKIE = /^jarvy_session_/;
11454
+ var policy = (client) => ({ cookie: (name3) => !SESSION_COOKIE.test(name3), setCookie: (name3) => !SESSION_COOKIE.test(name3), client });
11455
+ var wildcard = (host) => host === "0.0.0.0" || host === "::";
11456
+ var PublishedPorts = class {
11457
+ constructor(store, computers, listening) {
11458
+ this.store = store;
11459
+ this.computers = computers;
11460
+ this.listening = listening;
11461
+ }
11462
+ listeners = /* @__PURE__ */ new Set();
11463
+ /** Taking a port is one at a time, so two publishes don't pick the same free one. */
11464
+ taking = Promise.resolve();
11465
+ onChange(listener) {
11466
+ this.listeners.add(listener);
11467
+ return () => this.listeners.delete(listener);
11468
+ }
11469
+ /** Rows changed elsewhere, such as a Bot deleted with its ports. */
11470
+ changed() {
11471
+ for (const listener of this.listeners) listener();
11472
+ }
11473
+ /** Every published port, for the listening. */
11474
+ all() {
11475
+ return this.store.ports.all();
11476
+ }
11477
+ /** A Bot's published ports with what each finds now. */
11478
+ async list(botId) {
11479
+ const records = this.store.ports.list(botId);
11480
+ if (!records.length) return [];
11481
+ const states = await this.states(botId);
11482
+ return records.map((record3) => this.view(record3, states(record3.port)));
11483
+ }
11484
+ /** Publishes a port of the Bot's sandbox, or renames one it published, which keeps its link. */
11485
+ async publish(botId, input) {
11486
+ if (!this.store.environments.computerOf(botId)) throw new RuntimeFault("computer_missing", "This Bot has no sandbox.", 404);
11487
+ const port = input.port;
11488
+ if (!Number.isInteger(port) || port < 1 || port > 65535) throw new ValidationError("A port is 1-65535", "port_invalid");
11489
+ const name3 = text4(input.name, "A name", 60), description = text4(input.description, "A description", 300);
11490
+ const existing = this.store.ports.get(botId, port);
11491
+ if (!existing && this.store.ports.list(botId).length >= MAX_PER_BOT) throw new ValidationError(`At most ${MAX_PER_BOT} published ports a Bot`, "port_invalid");
11492
+ const record3 = await this.take(async () => {
11493
+ const hostPort = this.store.ports.get(botId, port)?.hostPort ?? await this.freePort();
11494
+ return this.store.ports.save({ botId, port, name: name3, description, hostPort });
11495
+ });
11496
+ this.changed();
11497
+ return this.view(record3, (await this.states(botId))(record3.port));
11498
+ }
11499
+ unpublish(botId, port) {
11500
+ if (!this.store.ports.remove(botId, port)) throw new ValidationError("That port isn't published", "port_missing");
11501
+ this.changed();
11502
+ }
11503
+ /** The sandbox is reset: what ran there is gone, and so are its links. */
11504
+ unpublishAll(botId) {
11505
+ if (this.store.ports.removeAll(botId)) this.changed();
11506
+ }
11507
+ /** A published port's own port was taken by another program while Jarvy was off: it moves to a free one. */
11508
+ async relocate(record3) {
11509
+ await this.take(async () => this.store.ports.move(record3.botId, record3.port, await this.freePort()));
11510
+ this.changed();
11511
+ }
11512
+ /** Where a request to Jarvy's `hostPort` goes now. */
11513
+ async target(hostPort) {
11514
+ const record3 = this.store.ports.byHostPort(hostPort);
11515
+ if (!record3) return { kind: "missing" };
11516
+ const host = await this.computers.address(record3.botId);
11517
+ return host ? { kind: "open", botId: record3.botId, host, port: record3.port } : { kind: "stopped" };
11518
+ }
11519
+ /** Forwards a request the API layer let through to the sandbox; `failed` when nothing answers there. */
11520
+ forward(request, response, to, client, failed) {
11521
+ forward(request, response, to, policy(client), failed);
11522
+ }
11523
+ forwardUpgrade(request, socket, head, to, client, failed) {
11524
+ forwardUpgrade(request, socket, head, to, policy(client), failed);
11525
+ }
11526
+ /** The Bot whose sandbox a request came from, if it is one of this Jarvy's. */
11527
+ caller(address2) {
11528
+ return this.computers.botAt(address2);
11529
+ }
11530
+ /** Links to it: from other devices (or this machine, when Jarvy listens on it only), and from sandboxes. */
11531
+ links(hostPort) {
11532
+ const where2 = this.listening();
11533
+ if (!where2) return { urls: [], fromSandboxes: null };
11534
+ const url = (scheme, host) => `${scheme}://${host.includes(":") ? `[${host}]` : host}:${hostPort}`;
11535
+ const urls = where2.addresses.length ? where2.addresses.map((host) => url(where2.https ? "https" : "http", host)) : [url("http", "localhost")];
11536
+ const fromSandboxes = wildcard(where2.host) ? url("http", this.computers.hostName) : where2.addresses[0] ? url("http", where2.addresses[0]) : null;
11537
+ return { urls, fromSandboxes };
11538
+ }
11539
+ take(work) {
11540
+ const done2 = this.taking.then(work, work);
11541
+ this.taking = done2.catch(() => void 0);
11542
+ return done2;
11543
+ }
11544
+ view(record3, state) {
11545
+ return {
11546
+ botId: record3.botId,
11547
+ port: record3.port,
11548
+ name: record3.name,
11549
+ description: record3.description,
11550
+ hostPort: record3.hostPort,
11551
+ urls: this.links(record3.hostPort).urls,
11552
+ state,
11553
+ createdAt: record3.createdAt,
11554
+ updatedAt: record3.updatedAt
11555
+ };
11556
+ }
11557
+ async states(botId) {
11558
+ try {
11559
+ const listening = await this.computers.listening(botId);
11560
+ if (!listening) return () => "stopped";
11561
+ return (port) => {
11562
+ const found = listening.get(port);
11563
+ return !found ? "idle" : found.loopbackOnly ? "local" : "live";
11564
+ };
11565
+ } catch {
11566
+ return () => "unknown";
11567
+ }
11568
+ }
11569
+ /** The first port of the range no published port has and nothing listens on, skipping Jarvy's own. */
11570
+ async freePort() {
11571
+ const where2 = this.listening();
11572
+ if (!where2) throw new RuntimeFault("stopping", "Jarvy isn't listening yet.", 503);
11573
+ const taken = this.store.ports.hostPorts();
11574
+ for (let port = where2.range.from; port <= where2.range.to; port++) {
11575
+ if (port === where2.port || taken.has(port)) continue;
11576
+ if (await portFree(where2.host, port)) return port;
11577
+ }
11578
+ throw new ValidationError(`No free port left in ${where2.range.from}-${where2.range.to}`, "ports_exhausted", { from: where2.range.from, to: where2.range.to });
11579
+ }
11580
+ };
11581
+ function text4(value2, label, max) {
11582
+ if (typeof value2 !== "string" || !value2.trim() || value2.length > max || value2.includes("\0")) throw new ValidationError(`${label} must be 1-${max} characters`, "port_invalid");
11583
+ return value2.trim();
11584
+ }
11585
+
10706
11586
  // src/server/service/bot/service.ts
10707
11587
  var BotService = class {
10708
11588
  constructor(deps) {
@@ -10710,6 +11590,7 @@ var BotService = class {
10710
11590
  this.agent = new AgentOps(this, deps.store, deps.chats, deps.deliveries);
10711
11591
  this.computers = new Computers({ store: deps.store, changed: () => this.listChanged() });
10712
11592
  this.computers.adopt(deps.store.bots.list().map((record3) => record3.id));
11593
+ this.ports = new PublishedPorts(deps.store, this.computers, () => deps.listening?.() ?? null);
10713
11594
  this.machines = new SshMachines(deps.store, deps.chats, () => this.listChanged());
10714
11595
  this.environments = new BotEnvironments(deps.store, this.computers, this.machines);
10715
11596
  }
@@ -10719,6 +11600,8 @@ var BotService = class {
10719
11600
  environments;
10720
11601
  /** The owner's SSH machines (A57). */
10721
11602
  machines;
11603
+ /** Ports Bots publish from their sandboxes (A74). */
11604
+ ports;
10722
11605
  closed = false;
10723
11606
  started = false;
10724
11607
  /** What only the agent Bot does (A48). */
@@ -10786,6 +11669,7 @@ var BotService = class {
10786
11669
  },
10787
11670
  secrets: { list: () => this.deps.secrets.listFor(id), prepare: (name3, purpose) => this.deps.secrets.prepareRequest(id, name3, purpose) },
10788
11671
  tasks: this.deps.tasks,
11672
+ ports: this.ports,
10789
11673
  ...id === AGENT_BOT_ID ? { agent: this.agent } : {}
10790
11674
  }),
10791
11675
  agentRuntime: runtime,
@@ -10899,12 +11783,26 @@ var BotService = class {
10899
11783
  this.describe(id);
10900
11784
  if (action === "stop") await this.computers.stop(id);
10901
11785
  else {
10902
- if (action === "reset") await this.computers.reset(id);
11786
+ if (action === "reset") {
11787
+ await this.computers.reset(id);
11788
+ this.ports.unpublishAll(id);
11789
+ }
10903
11790
  void this.computers.ensure(id).catch(() => {
10904
11791
  });
10905
11792
  }
10906
11793
  return this.describe(id);
10907
11794
  }
11795
+ /** What the sandbox may use at most (A75); returns the Bot with them. */
11796
+ async setComputerLimits(id, limits2) {
11797
+ this.describe(id);
11798
+ await this.computers.setLimits(id, limits2);
11799
+ return this.describe(id);
11800
+ }
11801
+ /** What the sandbox uses now, and this machine's size. */
11802
+ computerResources(id) {
11803
+ this.describe(id);
11804
+ return this.computers.resources(id);
11805
+ }
10908
11806
  describe(id) {
10909
11807
  return this.details(this.record(id));
10910
11808
  }
@@ -10912,10 +11810,10 @@ var BotService = class {
10912
11810
  this.describe(id);
10913
11811
  return this.deps.store.runtime.runs(id);
10914
11812
  }
10915
- activity(id, before, limit = 40) {
11813
+ activity(id, before, limit2 = 40) {
10916
11814
  const bot = this.get(id);
10917
- const messages = this.deps.chats.sentActivity(this.deps.store.account.userMemberId(), id, before, limit + 1);
10918
- return activityPage(messages, bot.memory.timeline(before, limit + 1), limit);
11815
+ const messages = this.deps.chats.sentActivity(this.deps.store.account.userMemberId(), id, before, limit2 + 1);
11816
+ return activityPage(messages, bot.memory.timeline(before, limit2 + 1), limit2);
10919
11817
  }
10920
11818
  activityMessage(id, messageId) {
10921
11819
  this.describe(id);
@@ -11004,8 +11902,9 @@ var BotService = class {
11004
11902
  this.lives.delete(id);
11005
11903
  await this.environments.release(id);
11006
11904
  if (!this.deps.store.bots.delete(id)) throw new RuntimeFault("bot_missing", "Bot not found", 404);
11905
+ this.ports.changed();
11007
11906
  await this.deps.chats.attachments.clean();
11008
- if (!keepFiles) await rm5(dirname10(workspacePath), { recursive: true, force: true });
11907
+ if (!keepFiles) await rm6(dirname10(workspacePath), { recursive: true, force: true });
11009
11908
  }
11010
11909
  async close() {
11011
11910
  if (this.closed) return;
@@ -11019,6 +11918,9 @@ var where = (moved) => `${moved.to.name}, in ${moved.to.directory}`;
11019
11918
 
11020
11919
  // src/server/service/connection.ts
11021
11920
  var ALLOW_HTTP = "allow_http";
11921
+ var PORT_RANGE = "published_port_range";
11922
+ var DEFAULT_PORT_RANGE = { from: 12600, to: 12699 };
11923
+ var MAX_RANGE = 1e3;
11022
11924
  var ConnectionService = class {
11023
11925
  constructor(store, dataDir) {
11024
11926
  this.store = store;
@@ -11039,10 +11941,37 @@ var ConnectionService = class {
11039
11941
  this.store.settings.set(ALLOW_HTTP, allow ? "1" : null);
11040
11942
  return this.info();
11041
11943
  }
11944
+ portRange() {
11945
+ const [from, to] = (this.store.settings.get(PORT_RANGE) ?? "").split("-").map(Number);
11946
+ return Number.isInteger(from) && Number.isInteger(to) ? { from, to } : DEFAULT_PORT_RANGE;
11947
+ }
11948
+ /** Ports already published keep theirs; new ones take from the new range. */
11949
+ setPortRange(value2) {
11950
+ const range = value2;
11951
+ const from = range?.from, to = range?.to;
11952
+ if (!Number.isInteger(from) || !Number.isInteger(to) || from < 1024 || to > 65535 || from > to || to - from + 1 > MAX_RANGE)
11953
+ throw new ValidationError(`Ports are 1024-65535, from no more than to, at most ${MAX_RANGE} of them`, "port_range_invalid");
11954
+ if (this.listening && from <= this.listening.port && this.listening.port <= to) throw new ValidationError("The range can't include Jarvy's own port", "port_range_own");
11955
+ const same3 = from === DEFAULT_PORT_RANGE.from && to === DEFAULT_PORT_RANGE.to;
11956
+ this.store.settings.set(PORT_RANGE, same3 ? null : `${from}-${to}`);
11957
+ return this.info();
11958
+ }
11042
11959
  info() {
11043
11960
  const listening = this.listening, material = this.material;
11044
11961
  const addresses = listening && material ? lanAddresses(listening.host).map((address2) => `https://${address2.includes(":") ? `[${address2}]` : address2}:${listening.port}`) : [];
11045
- return { https: !!material, allowHttp: this.allowHttp(), addresses, fingerprint: material?.fingerprint ?? null, fingerprintSha1: material?.fingerprintSha1 ?? null };
11962
+ return {
11963
+ https: !!material,
11964
+ allowHttp: this.allowHttp(),
11965
+ addresses,
11966
+ fingerprint: material?.fingerprint ?? null,
11967
+ fingerprintSha1: material?.fingerprintSha1 ?? null,
11968
+ ports: this.portRange()
11969
+ };
11970
+ }
11971
+ /** Null until Jarvy listens. `addresses`: hosts other devices may use, bare. */
11972
+ where() {
11973
+ const listening = this.listening;
11974
+ return listening ? { ...listening, https: !!this.material, addresses: lanAddresses(listening.host), range: this.portRange() } : null;
11046
11975
  }
11047
11976
  /** The root certificate (DER) for devices to install, when serving HTTPS. */
11048
11977
  rootCertificate() {
@@ -11309,7 +12238,8 @@ var Application = class _Application {
11309
12238
  catalog,
11310
12239
  changed: (id) => this.events.publish({ kind: "bot", id, status: this.bots.status(id) }),
11311
12240
  listChanged: () => this.events.publish({ kind: "bots" }),
11312
- factory
12241
+ factory,
12242
+ listening: () => this.connection.where()
11313
12243
  });
11314
12244
  this.auth = new CredentialLogins(store, this.credentials, (id) => this.bots.connectionChanged(id));
11315
12245
  this.connection = new ConnectionService(store, dataDir);
@@ -11541,8 +12471,8 @@ function listenOptions(args) {
11541
12471
  const host = option(args, "--host", "127.0.0.1");
11542
12472
  const port = Number(option(args, "--port", "8080"));
11543
12473
  if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error("Port must be between 1 and 65535");
11544
- const wildcard = host === "0.0.0.0" || host === "::";
11545
- return { host, port, url: wildcard || isLoopbackHost(host) ? `http://${wildcard ? "localhost" : host}:${port}` : `https://${host}:${port}` };
12474
+ const wildcard2 = host === "0.0.0.0" || host === "::";
12475
+ return { host, port, url: wildcard2 || isLoopbackHost(host) ? `http://${wildcard2 ? "localhost" : host}:${port}` : `https://${host}:${port}` };
11546
12476
  }
11547
12477
  async function startDetached(args, dir) {
11548
12478
  const { host, port } = listenOptions(args);
@@ -11615,10 +12545,16 @@ async function main() {
11615
12545
  const { host, port, url } = listenOptions(args);
11616
12546
  const webRoot = join19(dirname11(fileURLToPath2(import.meta.url)), "../web");
11617
12547
  if (!existsSync5(join19(webRoot, "index.html"))) throw new Error("Web build missing. Run npm run build first");
11618
- const server = createJarvyServer(app, webRoot, app.connection.prepare(host, port));
11619
- closeHttp = () => server.close();
12548
+ const tls = app.connection.prepare(host, port);
12549
+ const server = createJarvyServer(app, webRoot, tls);
12550
+ const published = new PublishedListeners(app, tls, host, port);
12551
+ closeHttp = () => {
12552
+ server.close();
12553
+ published.close();
12554
+ };
11620
12555
  await server.listen(port, host);
11621
12556
  app.start();
12557
+ published.start();
11622
12558
  writePidFile(dir, url);
11623
12559
  console.log(`Jarvy running at ${url} (listening on ${host}, pid ${process.pid})`);
11624
12560
  const notes = reachNotes(host, port, app.connection.allowHttp());
@@ -11631,6 +12567,7 @@ async function main() {
11631
12567
  if (shuttingDown) return;
11632
12568
  shuttingDown = true;
11633
12569
  server.close();
12570
+ published.close();
11634
12571
  void app.close().then(() => {
11635
12572
  removePidFile(dir);
11636
12573
  console.log("Jarvy stopped.");