@goodfish/jarvy 0.11.1 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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,7 @@ 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);
181
182
  return c.json({ ok: true });
182
183
  });
183
184
  }
@@ -211,9 +212,9 @@ var MARK = /\[\[q(\d+)\]\]/g;
211
212
  function quoteMarks(content2) {
212
213
  return [...content2.matchAll(MARK)].map((match) => Number(match[1]));
213
214
  }
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;
215
+ function clipQuote(text5) {
216
+ const chars = [...text5];
217
+ return chars.length > 200 ? `${chars.slice(0, 120).join("")}\u2026\u2026${chars.slice(-60).join("")}` : text5;
217
218
  }
218
219
  function previewQuotes(content2, quotes) {
219
220
  if (!quotes.length) return content2;
@@ -239,8 +240,8 @@ function expandQuotes(content2, quotes, name3) {
239
240
  return `${offset > 0 && whole[offset - 1] !== "\n" ? "\n" : ""}${block}${end < whole.length && whole[end] !== "\n" ? "\n" : ""}`;
240
241
  });
241
242
  }
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, "");
243
+ function quoteComparable(text5) {
244
+ 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
245
  }
245
246
 
246
247
  // src/server/api/views/chat.ts
@@ -337,9 +338,9 @@ function cursor(raw) {
337
338
  function activityRoutes(bots) {
338
339
  const routes = new Hono2();
339
340
  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);
341
+ const limit2 = Number(c.req.query("limit") ?? 40);
342
+ if (!Number.isSafeInteger(limit2) || limit2 < 1 || limit2 > 100) throw new HTTPException5(400, { message: "Invalid page size" });
343
+ const page2 = bots.activity(c.req.param("id"), cursor(c.req.query("before")), limit2);
343
344
  return c.json({ items: page2.items, nextBefore: page2.nextBefore ? Buffer.from(JSON.stringify(page2.nextBefore)).toString("base64url") : null });
344
345
  });
345
346
  routes.get("/:id/activity/messages/:messageId", (c) => {
@@ -367,9 +368,9 @@ function historyRoutes(bots) {
367
368
  const routes = new Hono3();
368
369
  routes.get("/:id/history", (c) => {
369
370
  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));
371
+ const before = c.req.query("before"), limit2 = integer(c.req.query("limit") ?? "50", "page size");
372
+ if (limit2 > 100) throw new HTTPException6(400, { message: "The page size can't be over 100" });
373
+ return c.json(memory.list(before === void 0 ? void 0 : integer(before, "page position"), limit2));
373
374
  });
374
375
  routes.get("/:id/history/:recordId{[0-9]+}", (c) => {
375
376
  const detail = bots.memory(c.req.param("id")).read(integer(c.req.param("recordId"), "record ID"));
@@ -404,6 +405,11 @@ function botRoutes(bots) {
404
405
  if (typeof input.machineId !== "string" || typeof input.granted !== "boolean") throw new HTTPException7(400, { message: "machineId and granted (true or false) are needed" });
405
406
  return c.json(bots.grantMachine(id, input.machineId, input.granted));
406
407
  });
408
+ routes.put("/:id/computer/limits", jsonBody, async (c) => {
409
+ const input = await readJson(c);
410
+ return c.json(await bots.setComputerLimits(c.req.param("id"), { memory: input.memory, cpus: input.cpus }));
411
+ });
412
+ routes.get("/:id/computer/resources", async (c) => c.json(await bots.computerResources(c.req.param("id"))));
407
413
  routes.post("/:id/computer/:action", async (c) => {
408
414
  const action = c.req.param("action");
409
415
  if (action !== "start" && action !== "stop" && action !== "reset") throw new HTTPException7(404, { message: "Not found" });
@@ -504,7 +510,7 @@ var AttachmentFiles = class {
504
510
  }
505
511
  /** Writes `chunks` to a new file under a new id; past `limit` bytes it stops and throws FileTooLarge. Nothing is
506
512
  * left behind when it throws. */
507
- async stage(chunks, limit, signal) {
513
+ async stage(chunks, limit2, signal) {
508
514
  const id = randomUUID(), part = this.part(id), hash = createHash("sha256");
509
515
  await mkdir(dirname(part), { recursive: true, mode: 448 });
510
516
  const handle = await open(part, "wx", 384);
@@ -513,7 +519,7 @@ var AttachmentFiles = class {
513
519
  for await (const chunk of chunks) {
514
520
  signal?.throwIfAborted();
515
521
  size += chunk.length;
516
- if (size > limit) throw new FileTooLarge();
522
+ if (size > limit2) throw new FileTooLarge();
517
523
  hash.update(chunk);
518
524
  for (let written = 0; written < chunk.length; ) written += (await handle.write(chunk, written)).bytesWritten;
519
525
  }
@@ -543,11 +549,11 @@ var AttachmentFiles = class {
543
549
  }
544
550
  /** The whole file, for the few readers that need it in memory (a picture or text a Bot looks at); `limit` guards
545
551
  * against reading a large file by mistake. */
546
- async read(id, limit) {
552
+ async read(id, limit2) {
547
553
  const handle = await open(this.path(id), "r");
548
554
  try {
549
555
  const { size } = await handle.stat();
550
- if (size > limit) throw new FileTooLarge();
556
+ if (size > limit2) throw new FileTooLarge();
551
557
  return await handle.readFile();
552
558
  } finally {
553
559
  await handle.close();
@@ -667,8 +673,8 @@ var ChatAttachments = class {
667
673
  return this.files.head(file.id, bytes);
668
674
  }
669
675
  /** A small file whole (a picture or text a Bot looks at). */
670
- read(file, limit) {
671
- return this.files.read(file.id, limit);
676
+ read(file, limit2) {
677
+ return this.files.read(file.id, limit2);
672
678
  }
673
679
  async removePending(actorId, id) {
674
680
  const removed = this.repository.removePendingAttachment(actorId, id);
@@ -712,9 +718,9 @@ function chatRoutes({ chats, bots }) {
712
718
  return c.json({ messages: page2.messages.map(messageView), hasOlder: page2.hasOlder });
713
719
  }
714
720
  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 });
721
+ const limit2 = Number(c.req.query("limit") ?? 1e3);
722
+ const messages = chats.read(c.get("actorId"), c.req.param("id"), after2, limit2);
723
+ return c.json({ messages: messages.map(messageView), nextSequence: messages.at(-1)?.sequence ?? after2, hasMore: messages.length === limit2 });
718
724
  });
719
725
  routes.post("/:id/messages", jsonBody, async (c) => {
720
726
  const message = await chats.send(c.get("actorId"), c.req.param("id"), await readJson(c));
@@ -985,8 +991,8 @@ function channelRoutes({ chats }) {
985
991
  };
986
992
  routes.get("/members", (c) => c.json({ members: channels.everyone(c.get("actorId")) }));
987
993
  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 });
994
+ const within = c.req.query("in") || void 0, limit2 = Number(c.req.query("limit") ?? 50), view = (found2) => chatView(found2, (member) => chats.member(member));
995
+ const found = chats.find(c.get("actorId"), c.req.query("q") ?? "", { within, limit: limit2 });
990
996
  const result = { chats: found.chats.map(view), messages: found.messages.map(({ message, chat: place }) => ({ message: messageView(message), chat: view(place) })) };
991
997
  return c.json(result);
992
998
  });
@@ -1088,6 +1094,7 @@ function connectionRoutes(connection) {
1088
1094
  routes.get("/connection", (c) => c.json(connection.info()));
1089
1095
  routes.put("/connection", jsonBody, async (c) => {
1090
1096
  const input = await readJson(c);
1097
+ if (input.ports !== void 0) return c.json(connection.setPortRange(input.ports));
1091
1098
  if (typeof input.allowHttp !== "boolean") throw new HTTPException10(400, { message: "allowHttp must be true or false" });
1092
1099
  return c.json(connection.setAllowHttp(input.allowHttp));
1093
1100
  });
@@ -1210,16 +1217,36 @@ function taskRoutes(services) {
1210
1217
  return routes;
1211
1218
  }
1212
1219
 
1220
+ // src/server/api/routes/ports.ts
1221
+ import { Hono as Hono14 } from "hono";
1222
+ import { HTTPException as HTTPException13 } from "hono/http-exception";
1223
+ function portRoutes(services) {
1224
+ const routes = new Hono14();
1225
+ const bot = (id) => {
1226
+ services.bots.describe(id);
1227
+ return id;
1228
+ };
1229
+ routes.get("/bots/:id/ports", async (c) => c.json({ ports: await services.bots.ports.list(bot(c.req.param("id"))) }));
1230
+ routes.delete("/bots/:id/ports/:port", (c) => {
1231
+ const port = Number(c.req.param("port"));
1232
+ if (!Number.isInteger(port)) throw new HTTPException13(400, { message: "port must be a number" });
1233
+ services.bots.ports.unpublish(bot(c.req.param("id")), port);
1234
+ return c.json({ ok: true });
1235
+ });
1236
+ return routes;
1237
+ }
1238
+
1213
1239
  // src/server/api/app.ts
1214
1240
  function createHttpApp(services, webRoot) {
1215
- const app = new Hono14();
1241
+ const app = new Hono15();
1216
1242
  app.use("*", headers);
1217
1243
  app.onError(handleError);
1218
- const api = new Hono14();
1244
+ const api = new Hono15();
1219
1245
  api.use("*", apiSecurity);
1220
1246
  api.route("/", rootCertificateRoute(services.connection));
1221
1247
  installAuthentication(api, services.account);
1222
1248
  api.route("/", taskRoutes(services));
1249
+ api.route("/", portRoutes(services));
1223
1250
  api.route("/bots", botRoutes(services.bots));
1224
1251
  api.route("/", channelRoutes(services));
1225
1252
  api.route("/chats", chatRoutes(services));
@@ -1231,7 +1258,7 @@ function createHttpApp(services, webRoot) {
1231
1258
  api.route("/secrets", secretRoutes(services.secrets));
1232
1259
  api.route("/environments", environmentRoutes(services.bots));
1233
1260
  api.all("*", () => {
1234
- throw new HTTPException13(404, { message: "API route not found" });
1261
+ throw new HTTPException14(404, { message: "API route not found" });
1235
1262
  });
1236
1263
  app.route("/api", api);
1237
1264
  app.get("*", serveStatic({ root: webRoot, onFound: (path, c) => {
@@ -1241,8 +1268,8 @@ function createHttpApp(services, webRoot) {
1241
1268
  c.header("Cache-Control", "no-store");
1242
1269
  } }));
1243
1270
  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" });
1271
+ if (c.req.method !== "GET" && c.req.method !== "HEAD") throw new HTTPException14(405, { message: "Method not allowed" });
1272
+ throw new HTTPException14(404, { message: "Web assets missing; run npm run build" });
1246
1273
  });
1247
1274
  return app;
1248
1275
  }
@@ -1255,32 +1282,39 @@ function fromThisMachine(address2 = "") {
1255
1282
  if (Date.now() - own.at > 6e4) own = { at: Date.now(), addresses: new Set(Object.values(networkInterfaces()).flatMap((list) => (list ?? []).map((item) => item.address))) };
1256
1283
  return own.addresses.has(plain2);
1257
1284
  }
1258
- function createJarvyServer(services, webRoot, tls) {
1259
- const app = createHttpApp(services, webRoot);
1260
- const handle = getRequestListener((request, bindings) => app.fetch(request, bindings), { overrideGlobalObjects: false });
1285
+ function dualProtocol(tls, serve, upgrade) {
1261
1286
  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
- }
1287
+ const plain2 = createServer(options, serve);
1288
+ if (upgrade) plain2.on("upgrade", upgrade);
1289
+ const sockets = /* @__PURE__ */ new Set();
1290
+ const track = (socket) => {
1291
+ sockets.add(socket);
1292
+ socket.once("close", () => sockets.delete(socket));
1281
1293
  };
1282
- const secure = createSecureServer({ ...options, key: tls.key, cert: tls.cert }, (request, response) => void handle(request, response));
1294
+ const drop = () => {
1295
+ for (const socket of sockets) socket.destroy();
1296
+ sockets.clear();
1297
+ };
1298
+ if (!tls) {
1299
+ plain2.on("connection", track);
1300
+ return {
1301
+ listen: (port, host) => new Promise((resolve5, reject) => {
1302
+ plain2.once("error", reject);
1303
+ plain2.listen(port, host, () => {
1304
+ plain2.off("error", reject);
1305
+ resolve5();
1306
+ });
1307
+ }),
1308
+ close: () => {
1309
+ plain2.close();
1310
+ drop();
1311
+ }
1312
+ };
1313
+ }
1314
+ const secure = createSecureServer({ ...options, key: tls.key, cert: tls.cert }, serve);
1315
+ if (upgrade) secure.on("upgrade", upgrade);
1283
1316
  const front = createListener({ pauseOnConnect: true }, (socket) => {
1317
+ track(socket);
1284
1318
  socket.on("error", () => socket.destroy());
1285
1319
  socket.setTimeout(6e4, () => socket.destroy());
1286
1320
  socket.once("readable", () => {
@@ -1304,11 +1338,186 @@ function createJarvyServer(services, webRoot, tls) {
1304
1338
  }),
1305
1339
  close: () => {
1306
1340
  front.close();
1307
- plain2.closeAllConnections();
1308
- secure.closeAllConnections();
1341
+ drop();
1309
1342
  }
1310
1343
  };
1311
1344
  }
1345
+ var encrypted2 = (request) => !!request.socket.encrypted;
1346
+ function redirectsToHttps(request, tls, allowHttp) {
1347
+ return !!tls && !encrypted2(request) && !fromThisMachine(request.socket.remoteAddress) && !allowHttp();
1348
+ }
1349
+ function redirectToHttps(request, response) {
1350
+ response.writeHead(308, { Location: `https://${request.headers.host ?? ""}${request.url ?? "/"}`, "Cache-Control": "no-store" }).end();
1351
+ }
1352
+ function createJarvyServer(services, webRoot, tls) {
1353
+ const app = createHttpApp(services, webRoot);
1354
+ const handle = getRequestListener((request, bindings) => app.fetch(request, bindings), { overrideGlobalObjects: false });
1355
+ return dualProtocol(tls, (request, response) => {
1356
+ if (redirectsToHttps(request, tls, () => services.connection.allowHttp())) {
1357
+ redirectToHttps(request, response);
1358
+ return;
1359
+ }
1360
+ void handle(request, response);
1361
+ });
1362
+ }
1363
+
1364
+ // src/server/api/views/port-page.ts
1365
+ var WORDS = {
1366
+ zh: {
1367
+ 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"],
1368
+ 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"],
1369
+ 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"],
1370
+ missing: ["\u8FD9\u4E2A\u94FE\u63A5\u5DF2\u5931\u6548", "Jarvy \u5DF2\u505C\u6B62\u8F6C\u53D1\u6B64\u7AEF\u53E3\u3002"],
1371
+ open: "\u6253\u5F00 Jarvy"
1372
+ },
1373
+ en: {
1374
+ 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."],
1375
+ 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."],
1376
+ 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."],
1377
+ missing: ["This link no longer works", "Jarvy no longer forwards this port."],
1378
+ open: "Open Jarvy"
1379
+ }
1380
+ };
1381
+ var escape = (text5) => text5.replace(/[&<>"']/g, (ch) => `&#${ch.charCodeAt(0)};`);
1382
+ function portPage(kind, acceptLanguage, jarvyUrl) {
1383
+ const languages = (acceptLanguage ?? "").toLowerCase().split(",").map((part) => part.trim().slice(0, 2));
1384
+ const zh = languages.indexOf("zh"), en = languages.indexOf("en");
1385
+ const lang = zh !== -1 && (en === -1 || zh < en) ? "zh" : "en", words = WORDS[lang], [title, text5] = words[kind];
1386
+ const link = jarvyUrl ? `<a href="${escape(jarvyUrl)}">${escape(words.open)}</a>` : "";
1387
+ 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">
1388
+ <title>${escape(title)}</title><style>
1389
+ :root{color-scheme:light dark;--fg:#1f2328;--muted:#59636e;--bg:#f6f8fa;--card:#fff;--line:#d1d9e0;--accent:#0969da}
1390
+ @media (prefers-color-scheme:dark){:root{--fg:#e6edf3;--muted:#9198a1;--bg:#0d1117;--card:#151b23;--line:#3d444d;--accent:#4493f8}}
1391
+ 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}
1392
+ main{max-width:440px;margin:24px;padding:28px 28px 24px;background:var(--card);border:1px solid var(--line);border-radius:12px}
1393
+ 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}
1394
+ </style></head><body><main><h1>${escape(title)}</h1><p>${escape(text5)}</p>${link}</main></body></html>`;
1395
+ }
1396
+
1397
+ // src/server/api/published.ts
1398
+ var STATUS = { signin: 401, stopped: 503, unreachable: 502, missing: 404 };
1399
+ function cookie(request, name3) {
1400
+ for (const part of (request.headers.cookie ?? "").split(";")) {
1401
+ const [key, ...value2] = part.trim().split("=");
1402
+ if (key === name3) return value2.join("=");
1403
+ }
1404
+ return void 0;
1405
+ }
1406
+ var PublishedListeners = class {
1407
+ constructor(services, tls, host, jarvyPort) {
1408
+ this.services = services;
1409
+ this.tls = tls;
1410
+ this.host = host;
1411
+ this.jarvyPort = jarvyPort;
1412
+ }
1413
+ open = /* @__PURE__ */ new Map();
1414
+ queue = Promise.resolve();
1415
+ unsubscribe;
1416
+ closed = false;
1417
+ start() {
1418
+ this.unsubscribe = this.services.bots.ports.onChange(() => this.sync());
1419
+ this.sync();
1420
+ }
1421
+ close() {
1422
+ this.closed = true;
1423
+ this.unsubscribe?.();
1424
+ for (const server of this.open.values()) server.close();
1425
+ this.open.clear();
1426
+ }
1427
+ sync() {
1428
+ this.queue = this.queue.then(() => this.follow()).catch((error) => console.error("[ports] listening failed", { error: error.message }));
1429
+ }
1430
+ async follow() {
1431
+ if (this.closed) return;
1432
+ const ports = this.services.bots.ports, records = ports.all(), wanted = new Set(records.map((record3) => record3.hostPort));
1433
+ for (const [hostPort, server] of this.open) if (!wanted.has(hostPort)) {
1434
+ server.close();
1435
+ this.open.delete(hostPort);
1436
+ }
1437
+ for (const record3 of records) {
1438
+ if (this.open.has(record3.hostPort)) continue;
1439
+ const hostPort = record3.hostPort;
1440
+ const server = dualProtocol(
1441
+ this.tls,
1442
+ (request, response) => void this.serve(hostPort, request, response).catch(() => response.destroy()),
1443
+ (request, socket, head) => void this.upgrade(hostPort, request, socket, head).catch(() => socket.destroy())
1444
+ );
1445
+ try {
1446
+ await server.listen(hostPort, this.host);
1447
+ this.open.set(hostPort, server);
1448
+ } catch (error) {
1449
+ if (error.code !== "EADDRINUSE") {
1450
+ console.error(`[ports] can't listen on ${hostPort}`, { error: error.message });
1451
+ continue;
1452
+ }
1453
+ console.warn(`[ports] ${hostPort} is taken by another program; moving ${record3.name} to a free port`);
1454
+ await ports.relocate(record3).catch((reason) => console.error("[ports] no free port", { error: reason.message }));
1455
+ }
1456
+ }
1457
+ }
1458
+ /** Who is asking: the signed-in owner, one of this Jarvy's Bots, or no one (or someone to send to HTTPS first). */
1459
+ async caller(request) {
1460
+ const { account, bots, connection } = this.services;
1461
+ if (account.authenticated(cookie(request, sessionCookieName(account.instance, encrypted2(request))))) return { owner: true };
1462
+ const bot = await bots.ports.caller(request.socket.remoteAddress ?? "");
1463
+ if (bot) return { bot };
1464
+ return redirectsToHttps(request, this.tls, () => connection.allowHttp()) ? "redirect" : null;
1465
+ }
1466
+ async serve(hostPort, request, response) {
1467
+ const caller = await this.caller(request);
1468
+ if (caller === "redirect") {
1469
+ redirectToHttps(request, response);
1470
+ return;
1471
+ }
1472
+ if (!caller) {
1473
+ this.page(request, response, "signin");
1474
+ return;
1475
+ }
1476
+ const target3 = await this.services.bots.ports.target(hostPort);
1477
+ if (target3.kind !== "open") {
1478
+ this.page(request, response, target3.kind);
1479
+ return;
1480
+ }
1481
+ this.services.bots.ports.forward(request, response, target3, this.client(request), () => this.page(request, response, "unreachable"));
1482
+ }
1483
+ async upgrade(hostPort, request, socket, head) {
1484
+ const refuse = (status) => socket.end(`HTTP/1.1 ${status}\r
1485
+ Connection: close\r
1486
+ Content-Length: 0\r
1487
+ \r
1488
+ `);
1489
+ const caller = await this.caller(request);
1490
+ if (!caller || caller === "redirect") {
1491
+ refuse("401 Unauthorized");
1492
+ return;
1493
+ }
1494
+ const target3 = await this.services.bots.ports.target(hostPort);
1495
+ if (target3.kind !== "open") {
1496
+ refuse(target3.kind === "missing" ? "404 Not Found" : "503 Service Unavailable");
1497
+ return;
1498
+ }
1499
+ this.services.bots.ports.forwardUpgrade(request, socket, head, target3, this.client(request), () => refuse("502 Bad Gateway"));
1500
+ }
1501
+ client(request) {
1502
+ return { address: request.socket.remoteAddress ?? "", secure: encrypted2(request) };
1503
+ }
1504
+ page(request, response, kind) {
1505
+ if (response.headersSent) {
1506
+ response.destroy();
1507
+ return;
1508
+ }
1509
+ const host = (request.headers.host ?? "").replace(/:\d+$/, "");
1510
+ const jarvyUrl = host ? `${encrypted2(request) ? "https" : "http"}://${host}:${this.jarvyPort}/` : null;
1511
+ response.writeHead(STATUS[kind], {
1512
+ "Content-Type": "text/html; charset=utf-8",
1513
+ "Cache-Control": "no-store",
1514
+ "X-Content-Type-Options": "nosniff",
1515
+ "Content-Security-Policy": "default-src 'none'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'",
1516
+ "Referrer-Policy": "no-referrer"
1517
+ });
1518
+ response.end(request.method === "HEAD" ? void 0 : portPage(kind, request.headers["accept-language"], jarvyUrl));
1519
+ }
1520
+ };
1312
1521
 
1313
1522
  // src/server/infra/storage/store.ts
1314
1523
  import { chmodSync as chmodSync2, mkdirSync as mkdirSync4 } from "node:fs";
@@ -1439,7 +1648,7 @@ function migrateChannels(db) {
1439
1648
  INSERT INTO chat_states(chat_id, member_id, read_sequence)
1440
1649
  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
1650
  `);
1442
- const taken = new Set(db.prepare("SELECT username FROM account").all().map((row) => row.username.toLowerCase()));
1651
+ const taken = new Set(db.prepare("SELECT username FROM account").all().map((row2) => row2.username.toLowerCase()));
1443
1652
  const rename4 = db.prepare("UPDATE bots SET name=? WHERE id=?");
1444
1653
  for (const bot of db.prepare("SELECT id,name FROM bots ORDER BY created_at,id").all()) {
1445
1654
  const base = bot.name.replace(/@/g, "").trim() || "Bot";
@@ -1674,8 +1883,8 @@ var Cipher = class _Cipher {
1674
1883
  location() {
1675
1884
  return this.master.location;
1676
1885
  }
1677
- static sealed(text4) {
1678
- return text4.startsWith(PREFIX);
1886
+ static sealed(text5) {
1887
+ return text5.startsWith(PREFIX);
1679
1888
  }
1680
1889
  seal(plain2, context2) {
1681
1890
  const iv = randomBytes(12), cipher = createCipheriv("aes-256-gcm", this.master.key, iv);
@@ -1726,7 +1935,7 @@ function migrateSecrets(db, cipher) {
1726
1935
  `);
1727
1936
  const rows3 = db.prepare("SELECT connection_id AS id, credential_json AS json FROM provider_credentials").all();
1728
1937
  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);
1938
+ for (const row2 of rows3) if (!Cipher.sealed(row2.json)) update.run(cipher.seal(row2.json, `credential:${row2.id}`), row2.id);
1730
1939
  db.exec("PRAGMA user_version=21");
1731
1940
  db.exec("COMMIT");
1732
1941
  } catch (error) {
@@ -1909,7 +2118,7 @@ function migrateAttachmentFiles(db, dataDir) {
1909
2118
  if (userVersion(db) >= 28) return;
1910
2119
  const root = join2(dataDir, "attachments");
1911
2120
  const rows3 = db.prepare("SELECT id,size,data FROM attachments").iterate();
1912
- for (const row of rows3) writeOut(attachmentPath(root, row.id), row.data);
2121
+ for (const row2 of rows3) writeOut(attachmentPath(root, row2.id), row2.data);
1913
2122
  migration(db, 28, () => {
1914
2123
  for (const { id, size } of db.prepare("SELECT id,size FROM attachments").all()) {
1915
2124
  if (sizeOf(attachmentPath(root, id)) !== size) throw new Error(`Attachment ${id} was not written out`);
@@ -1956,7 +2165,7 @@ function migrateOwnComputers(db, dataDir) {
1956
2165
  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
2166
  db.prepare("UPDATE bots SET home_environment_id=?, current_environment_id=? WHERE id=?").run(id, id, bot.id);
1958
2167
  }
1959
- const hosts = db.prepare("SELECT id FROM environments WHERE kind='host' AND deleted_at IS NULL").all().map((row) => row.id);
2168
+ const hosts = db.prepare("SELECT id FROM environments WHERE kind='host' AND deleted_at IS NULL").all().map((row2) => row2.id);
1960
2169
  for (const host of hosts) {
1961
2170
  db.prepare("UPDATE bots SET current_environment_id=home_environment_id WHERE current_environment_id=?").run(host);
1962
2171
  db.prepare("DELETE FROM environment_grants WHERE environment_id=?").run(host);
@@ -1965,6 +2174,18 @@ function migrateOwnComputers(db, dataDir) {
1965
2174
  });
1966
2175
  }
1967
2176
 
2177
+ // src/server/infra/storage/migrations/published-port-schema.ts
2178
+ function migratePublishedPorts(db) {
2179
+ migration(db, 30, () => {
2180
+ db.exec(`
2181
+ CREATE TABLE published_ports(bot_id TEXT NOT NULL REFERENCES bots(id) ON DELETE CASCADE,
2182
+ port INTEGER NOT NULL CHECK(port BETWEEN 1 AND 65535), name TEXT NOT NULL, description TEXT NOT NULL,
2183
+ host_port INTEGER NOT NULL UNIQUE CHECK(host_port BETWEEN 1 AND 65535),
2184
+ created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, PRIMARY KEY(bot_id, port));
2185
+ `);
2186
+ });
2187
+ }
2188
+
1968
2189
  // src/server/infra/storage/migrations/schema.ts
1969
2190
  import { randomUUID as randomUUID4 } from "node:crypto";
1970
2191
 
@@ -2038,7 +2259,7 @@ function migratePrototype(db) {
2038
2259
  const id = randomUUID3(), now = Date.now();
2039
2260
  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
2261
  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));
2262
+ const modelIds = new Set([legacy.model, ...botModels.map((row2) => row2.modelId)].filter(Boolean));
2042
2263
  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
2264
  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
2265
  }
@@ -2087,7 +2308,7 @@ function migrateRuntime(db) {
2087
2308
  // src/server/infra/storage/migrations/schema.ts
2088
2309
  function migrateDatabase(db, cipher, dataDir) {
2089
2310
  const version = db.prepare("PRAGMA user_version").get().user_version;
2090
- if (version > 29) throw new Error(`Unsupported database version: ${version}`);
2311
+ if (version > 30) throw new Error(`Unsupported database version: ${version}`);
2091
2312
  migratePrototype(db);
2092
2313
  if (version >= 5) {
2093
2314
  migrateRuntime(db);
@@ -2114,6 +2335,7 @@ function migrateDatabase(db, cipher, dataDir) {
2114
2335
  migrateMachineDescriptions(db);
2115
2336
  migrateAttachmentFiles(db, dataDir);
2116
2337
  migrateOwnComputers(db, dataDir);
2338
+ migratePublishedPorts(db);
2117
2339
  return;
2118
2340
  }
2119
2341
  db.exec("PRAGMA foreign_keys=OFF; BEGIN IMMEDIATE");
@@ -2198,6 +2420,7 @@ function migrateDatabase(db, cipher, dataDir) {
2198
2420
  migrateMachineDescriptions(db);
2199
2421
  migrateAttachmentFiles(db, dataDir);
2200
2422
  migrateOwnComputers(db, dataDir);
2423
+ migratePublishedPorts(db);
2201
2424
  }
2202
2425
 
2203
2426
  // src/server/infra/storage/account-repository.ts
@@ -2227,8 +2450,8 @@ var AccountRepository = class {
2227
2450
  }
2228
2451
  /** Installation identity, not an account/connection ID. Pi requests it only for ChatGPT login. */
2229
2452
  installationDeviceId() {
2230
- const row = this.db.prepare("SELECT device_id FROM installation WHERE id=1").get();
2231
- if (row) return row.device_id;
2453
+ const row2 = this.db.prepare("SELECT device_id FROM installation WHERE id=1").get();
2454
+ if (row2) return row2.device_id;
2232
2455
  const id = randomUUID5();
2233
2456
  this.db.prepare("INSERT INTO installation(id,device_id) VALUES(1,?)").run(id);
2234
2457
  return id;
@@ -2254,11 +2477,11 @@ var AccountRepository = class {
2254
2477
  });
2255
2478
  }
2256
2479
  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;
2480
+ const row2 = this.db.prepare("SELECT username,password_salt,password_hash FROM account WHERE id=1").get();
2481
+ if (!row2 || typeof username !== "string" || typeof password !== "string") return null;
2482
+ const candidate = Buffer.from(passwordHash(password, row2.password_salt), "hex");
2483
+ const expected = Buffer.from(row2.password_hash, "hex");
2484
+ if (!timingSafeEqual(candidate, expected) || username !== row2.username) return null;
2262
2485
  const token = randomBytes2(32).toString("base64url");
2263
2486
  this.db.prepare("DELETE FROM auth_sessions WHERE expires_at<?").run(Date.now());
2264
2487
  this.db.prepare("INSERT INTO auth_sessions VALUES (?,?)").run(tokenHash(token), Date.now() + SESSION_LIFETIME_MS);
@@ -2502,23 +2725,23 @@ var ModelConnectionRepository = class {
2502
2725
  WHERE c.owner_id=? ORDER BY c.created_at,c.id`).all(this.ownerId());
2503
2726
  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
2727
  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
2728
+ for (const row2 of models) byConnection.set(row2.connectionId, [...byConnection.get(row2.connectionId) ?? [], normalizeModel(JSON.parse(row2.configJson))]);
2729
+ return rows3.map((row2) => ({
2730
+ id: row2.id,
2731
+ providerName: row2.providerName,
2732
+ displayName: row2.displayName,
2733
+ kind: row2.kind,
2734
+ baseUrl: row2.baseUrl,
2735
+ api: row2.api,
2736
+ authType: row2.authType,
2737
+ configured: row2.credentialType !== null,
2738
+ models: byConnection.get(row2.id) ?? [],
2739
+ createdAt: row2.createdAt,
2740
+ updatedAt: row2.updatedAt
2518
2741
  }));
2519
2742
  }
2520
2743
  get(id) {
2521
- return this.list().find((row) => row.id === id) ?? null;
2744
+ return this.list().find((row2) => row2.id === id) ?? null;
2522
2745
  }
2523
2746
  create(input) {
2524
2747
  const providerName = requireText(input.providerName, "Provider name", 80).toLowerCase();
@@ -2558,7 +2781,7 @@ var ModelConnectionRepository = class {
2558
2781
  /** Deleted Bots keep their row (deleteBot) but use nothing: their link to the connection is cleared with it. */
2559
2782
  delete(id) {
2560
2783
  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);
2784
+ 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
2785
  if (users.length) throw new ConflictError(`In use by ${users.join(", ")}`, "connection_in_use", { bots: users.join(", "), count: users.length });
2563
2786
  return transaction(this.db, () => {
2564
2787
  this.db.prepare("UPDATE bots SET connection_id=NULL WHERE connection_id=? AND deleted_at IS NOT NULL").run(id);
@@ -2580,8 +2803,8 @@ var ModelConnectionRepository = class {
2580
2803
  }
2581
2804
  headers(id, modelId) {
2582
2805
  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 ?? {} : {};
2806
+ const row2 = this.db.prepare("SELECT config_json FROM connection_models WHERE connection_id=? AND model_id=?").get(id, modelId);
2807
+ return row2 ? JSON.parse(row2.config_json).headers ?? {} : {};
2585
2808
  }
2586
2809
  saveModel(id, raw, originalId) {
2587
2810
  if (!this.get(id)) return null;
@@ -2620,7 +2843,7 @@ var ModelConnectionRepository = class {
2620
2843
  }
2621
2844
  /** Names of the Bots (not deleted) whose model this is. */
2622
2845
  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);
2846
+ 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
2847
  }
2625
2848
  /** A built-in connection back to its catalog (A62), in one transaction: its address cleared and its protocol set; each of
2626
2849
  * `models` saved as given, keeping the request headers added to it; `removed` deleted, and the Bots using them left
@@ -2631,7 +2854,7 @@ var ModelConnectionRepository = class {
2631
2854
  const now = Date.now(), rows3 = models.map((model) => ({ id: model.id, stored: JSON.stringify({ ...normalizeModel(model), headers: this.headers(id, model.id) }) }));
2632
2855
  transaction(this.db, () => {
2633
2856
  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);
2857
+ 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
2858
  for (const modelId of removed) {
2636
2859
  this.db.prepare("UPDATE bots SET connection_id=NULL,model_id='' WHERE connection_id=? AND model_id=?").run(id, modelId);
2637
2860
  this.db.prepare("DELETE FROM connection_models WHERE connection_id=? AND model_id=?").run(id, modelId);
@@ -2651,18 +2874,18 @@ var BotRepository = class {
2651
2874
  this.connections = connections;
2652
2875
  this.inbox = inbox;
2653
2876
  }
2654
- fromRow(row) {
2655
- const workspacePath = join4(this.dataDir, "bots", row.id, "workspace");
2877
+ fromRow(row2) {
2878
+ const workspacePath = join4(this.dataDir, "bots", row2.id, "workspace");
2656
2879
  mkdirSync2(join4(workspacePath, "temp"), { recursive: true, mode: 448 });
2657
- return { ...row, workspacePath };
2880
+ return { ...row2, workspacePath };
2658
2881
  }
2659
2882
  list() {
2660
2883
  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));
2884
+ return rows3.map((row2) => this.fromRow(row2));
2662
2885
  }
2663
2886
  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;
2887
+ 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());
2888
+ return row2 ? this.fromRow(row2) : null;
2666
2889
  }
2667
2890
  /** Member names are unique across people and Bots, ignoring case, so "@name" in a message is unambiguous (A37). */
2668
2891
  nameTaken(name3, exceptBotId) {
@@ -2670,7 +2893,7 @@ var BotRepository = class {
2670
2893
  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
2894
  }
2672
2895
  /** 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
2896
+ * notices, session row and published ports go; the member row stays, marked deleted, so messages it left in channels keep their
2674
2897
  * author, and its name is free again. Files are the caller's (BotService). */
2675
2898
  delete(id) {
2676
2899
  if (!this.get(id)) return false;
@@ -2682,6 +2905,7 @@ var BotRepository = class {
2682
2905
  this.db.prepare("DELETE FROM bot_sessions WHERE bot_id=?").run(id);
2683
2906
  this.db.prepare("DELETE FROM environment_grants WHERE bot_id=?").run(id);
2684
2907
  this.db.prepare("DELETE FROM secret_grants WHERE bot_id=?").run(id);
2908
+ this.db.prepare("DELETE FROM published_ports WHERE bot_id=?").run(id);
2685
2909
  });
2686
2910
  return true;
2687
2911
  }
@@ -2826,15 +3050,15 @@ var InboxRepository = class {
2826
3050
  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
3051
  n.actor_id AS actorId,n.payload,n.created_at AS createdAt
2828
3052
  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 ?? "" }
3053
+ return rows3.filter((row2) => row2.chatId === null || this.chats.canAccess(botId, row2.chatId)).map((row2) => ({
3054
+ id: row2.id,
3055
+ kind: row2.kind,
3056
+ urgency: row2.urgency,
3057
+ createdAt: row2.createdAt,
3058
+ actor: row2.actorId ? this.chats.member(row2.actorId) : null,
3059
+ payload: row2.payload === null ? null : JSON.parse(row2.payload),
3060
+ messageId: row2.messageId,
3061
+ chat: row2.chatId === null ? null : { id: row2.chatId, type: row2.chatType, title: row2.chatTitle ?? "" }
2838
3062
  }));
2839
3063
  }
2840
3064
  /** These notices reached the Bot (steered into its run, or in a batch it was given). A new notice version told
@@ -2848,20 +3072,20 @@ var InboxRepository = class {
2848
3072
  rowState(memberId, chatId) {
2849
3073
  const counts = this.db.prepare(`SELECT count(*) AS n,COALESCE(SUM(reason IN ('mention','reply')),0) AS at FROM inbox
2850
3074
  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
3075
+ 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
3076
  FROM inbox i JOIN chats c ON c.id=i.chat_id
2853
3077
  WHERE i.member_id=? AND i.category='activity' AND i.urgency='queue' AND i.state='pending' AND (i.chat_id=? OR c.parent_id=?) ORDER BY i.id DESC LIMIT 1`).get(memberId, chatId, chatId);
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 ?? "");
3078
+ if (!row2) return { alerts: counts.n, atMe: counts.at > 0, hint: null };
3079
+ const payload = row2.payload ? JSON.parse(row2.payload) : {};
3080
+ const detail = row2.kind === "reaction" ? payload.emoji ?? "" : String(payload.version ?? "");
2857
3081
  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 || "",
3082
+ kind: row2.kind,
3083
+ chatId: row2.chatId,
3084
+ chatType: row2.chatType,
3085
+ title: row2.title,
3086
+ actor: row2.actorId && this.chats.member(row2.actorId)?.name || "",
2863
3087
  detail,
2864
- createdAt: row.createdAt
3088
+ createdAt: row2.createdAt
2865
3089
  } };
2866
3090
  }
2867
3091
  /** A person read up to `sequence` in a chat: the messages delivered to them there are told. */
@@ -2899,11 +3123,11 @@ var ChannelRepository = class {
2899
3123
  everyone(ownerId) {
2900
3124
  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
3125
  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 } : {} }));
3126
+ 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
3127
  }
2904
3128
  /** The directory with each Bot's description, for a Bot's members.list. */
2905
3129
  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]));
3130
+ 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
3131
  return this.directory(ownerId).map((member) => ({ ...member, description: descriptions.get(member.id) ?? "" }));
2908
3132
  }
2909
3133
  /** The person a member acts for: itself, or a Bot's owner. */
@@ -2975,18 +3199,18 @@ var ChannelRepository = class {
2975
3199
  const type = this.db.prepare("SELECT type FROM chats WHERE id=?").get(chatId)?.type ?? "dm";
2976
3200
  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
3201
  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) };
3202
+ return { type, members: ids.map((row2) => this.chats.member(row2.id)).filter((member) => member !== null) };
2979
3203
  }
2980
3204
  /** Channel notice / thread summary: any member who can write there, people and Bots alike. The routed
2981
3205
  * deliveries (A41) are written with the new version, which is their payload. */
2982
- setNote(actorId, chatId, text4, deliveries = []) {
3206
+ setNote(actorId, chatId, text5, deliveries = []) {
2983
3207
  this.chats.assertAccess(actorId, chatId, true);
2984
3208
  const type = this.db.prepare("SELECT type FROM chats WHERE id=?").get(chatId).type;
2985
3209
  if (type === "dm") throw new ChatError(400, "Direct messages have no notice");
2986
3210
  const now = Date.now();
2987
3211
  transaction(this.db, () => {
2988
3212
  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);
3213
+ 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
3214
  this.db.prepare("UPDATE chats SET updated_at=? WHERE id=?").run(now, chatId);
2991
3215
  this.markNoteSeen(actorId, chatId);
2992
3216
  const version = this.db.prepare("SELECT version FROM chat_notes WHERE chat_id=?").get(chatId).version;
@@ -3011,9 +3235,9 @@ var ChannelRepository = class {
3011
3235
  unreadNotes(memberId, chatIds) {
3012
3236
  const rows3 = [];
3013
3237
  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
3238
+ 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
3239
  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 });
3240
+ if (row2 && (row2.seen ?? 0) < row2.version) rows3.push({ chatId: row2.chatId, title: row2.title, type: row2.type, version: row2.version });
3017
3241
  }
3018
3242
  return rows3;
3019
3243
  }
@@ -3043,13 +3267,13 @@ var ChannelRepository = class {
3043
3267
  }
3044
3268
  /** Members subscribed to this chat (whether they can still read it is checked by the caller). */
3045
3269
  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);
3270
+ 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
3271
  }
3048
3272
  /** Channels and threads the member is subscribed to and can still read, most recently active first. */
3049
3273
  subscriptions(memberId) {
3050
3274
  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
3275
  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);
3276
+ ORDER BY c.updated_at DESC,c.id`).all({ member: memberId }).map((row2) => row2.id);
3053
3277
  }
3054
3278
  /** For a Bot's batch and `messages.read unread=true` (A37, A70): its chats with unread, most recently active first —
3055
3279
  * DMs, channels, and the threads it follows while they are open. A finished thread stays out of this overview,
@@ -3134,11 +3358,11 @@ var ThreadRepository = class {
3134
3358
  list(actorId, channelId) {
3135
3359
  this.chats.assertAccess(actorId, channelId);
3136
3360
  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);
3361
+ ORDER BY updated_at DESC,id`).all(channelId).map((row2) => row2.id);
3138
3362
  }
3139
3363
  /** Threads the member follows in channels it can still read (with at least one reply). */
3140
3364
  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);
3365
+ return this.db.prepare(`SELECT c.id ${FOLLOWED} ORDER BY c.updated_at DESC,c.id`).all({ member: memberId }).map((row2) => row2.id);
3142
3366
  }
3143
3367
  /** The same threads with what orders, filters and counts them (A69): status, creation, last reply, and the member's
3144
3368
  * unread and @ counts by the rule a chat's own count follows, in one statement however many there are. */
@@ -3168,7 +3392,7 @@ var InteractionRepository = class {
3168
3392
  reactions(messageId) {
3169
3393
  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
3394
  const groups = /* @__PURE__ */ new Map();
3171
- for (const row of rows3) groups.set(row.emoji, [...groups.get(row.emoji) ?? [], row.memberId]);
3395
+ for (const row2 of rows3) groups.set(row2.emoji, [...groups.get(row2.emoji) ?? [], row2.memberId]);
3172
3396
  return [...groups].map(([emoji, memberIds]) => ({ emoji, memberIds }));
3173
3397
  }
3174
3398
  /** Adds or removes the actor's mark (anyone who can write in the chat), writing the routed deliveries with a new
@@ -3179,16 +3403,16 @@ var InteractionRepository = class {
3179
3403
  }
3180
3404
  react(actorId, messageId, emoji, on, deliveries) {
3181
3405
  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");
3406
+ const row2 = this.db.prepare("SELECT chat_id AS chatId FROM messages WHERE id=?").get(messageId);
3407
+ if (!row2 || !this.chats.canAccess(actorId, row2.chatId, true)) throw new ChatError(404, "Message not found");
3184
3408
  if (on) {
3185
3409
  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 };
3410
+ if (added) this.chats.inbox.write(deliveries.map((delivery) => ({ delivery, chatId: row2.chatId, messageId, actorId, payload: { emoji }, createdAt: Date.now() })));
3411
+ return { chatId: row2.chatId, added };
3188
3412
  }
3189
3413
  this.db.prepare("DELETE FROM message_reactions WHERE message_id=? AND member_id=? AND emoji=?").run(messageId, actorId, emoji);
3190
3414
  this.chats.inbox.withdrawReaction(messageId, actorId, emoji);
3191
- return { chatId: row.chatId, added: false };
3415
+ return { chatId: row2.chatId, added: false };
3192
3416
  });
3193
3417
  }
3194
3418
  /** 免打扰 for one member and chat. */
@@ -3200,7 +3424,7 @@ var InteractionRepository = class {
3200
3424
  /** Members who muted a chat; a thread follows its channel's 免打扰 too. A route input (A41). */
3201
3425
  mutedIn(chatId) {
3202
3426
  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);
3427
+ (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
3428
  }
3205
3429
  /** Folds a message to one line in the interface, or unfolds it (A60); its content and everything else stay. */
3206
3430
  setFolded(messageId, folded) {
@@ -3252,9 +3476,9 @@ var ChatRepository = class {
3252
3476
  /** Every delivery (A41, A61): the one place inbox SQL lives. */
3253
3477
  inbox;
3254
3478
  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
3479
+ 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
3480
  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;
3481
+ return row2 ? { id: row2.id, kind: row2.kind, name: row2.name, ...row2.deleted ? { deleted: true } : {} } : null;
3258
3482
  }
3259
3483
  /** A thread has no member list of its own: reading and writing follow its parent channel (A37). The owner may
3260
3484
  * read (never write) a DM between two of its Bots. */
@@ -3278,8 +3502,8 @@ var ChatRepository = class {
3278
3502
  return { unread, mentions, readSequence: read3?.n ?? 0 };
3279
3503
  }
3280
3504
  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;
3505
+ const row2 = this.db.prepare("SELECT text,updated_by AS updatedBy,updated_at AS updatedAt,version FROM chat_notes WHERE chat_id=?").get(chatId);
3506
+ return row2 ? { ...row2, updatedBy: this.member(row2.updatedBy) } : null;
3283
3507
  }
3284
3508
  assertAccess(actorId, chatId, write2 = false) {
3285
3509
  if (!this.canAccess(actorId, chatId, write2)) throw new ChatError(404, "Chat not found");
@@ -3289,16 +3513,16 @@ var ChatRepository = class {
3289
3513
  const found = this.db.prepare(`SELECT ${CHAT_COLUMNS} FROM chats c WHERE c.id=?`).get(id);
3290
3514
  if (!found) return null;
3291
3515
  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);
3516
+ const row2 = { ...rest, lastMessage: content2 !== null && lastMessageId ? { content: clipMarked(content2, 300), quotes: this.quotes.of(lastMessageId) } : null };
3517
+ 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
3518
  const state = this.db.prepare("SELECT following,subscribed,pinned,muted FROM chat_states WHERE chat_id=? AND member_id=?").get(id, actorId);
3295
3519
  return {
3296
- ...row,
3520
+ ...row2,
3297
3521
  memberIds: members.map((item) => item.id),
3298
3522
  note: this.note(id),
3299
3523
  ...this.unread(actorId, id),
3300
3524
  ...this.inbox.rowState(actorId, id),
3301
- following: row.type === "thread" && !!state?.following,
3525
+ following: row2.type === "thread" && !!state?.following,
3302
3526
  subscribed: !!state?.subscribed,
3303
3527
  pinned: !!state?.pinned,
3304
3528
  muted: !!state?.muted
@@ -3336,7 +3560,7 @@ var ChatRepository = class {
3336
3560
  memberships(actorId, chatId) {
3337
3561
  this.assertAccess(actorId, chatId);
3338
3562
  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 }));
3563
+ 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
3564
  }
3341
3565
  updateTitle(actorId, chatId, title) {
3342
3566
  this.assertOwner(actorId, chatId);
@@ -3363,50 +3587,50 @@ var ChatRepository = class {
3363
3587
  return ids.map(({ id }) => this.getChat(ownerId, id));
3364
3588
  }
3365
3589
  /** Up to `limit` messages before a sequence, oldest first. */
3366
- messagesBefore(actorId, chatId, before, limit) {
3590
+ messagesBefore(actorId, chatId, before, limit2) {
3367
3591
  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));
3592
+ const rows3 = this.db.prepare(`SELECT ${MESSAGE_COLUMNS} FROM messages WHERE chat_id=? AND sequence<? ORDER BY sequence DESC LIMIT ?`).all(chatId, before, limit2);
3593
+ return rows3.reverse().map((row2) => this.messageFromRow(row2));
3370
3594
  }
3371
3595
  /** Exactly the messages counted as unread for this member (same rule as `unread`), oldest first. */
3372
- unreadMessages(memberId, chatId, limit) {
3596
+ unreadMessages(memberId, chatId, limit2) {
3373
3597
  this.assertAccess(memberId, chatId);
3374
3598
  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));
3599
+ const rows3 = this.db.prepare(`SELECT ${MESSAGE_COLUMNS.split(",").map((column) => `m.${column}`).join(",")} ${unreadIn(":chat")} ORDER BY m.sequence LIMIT :limit`).all({ chat: chatId, member: memberId, limit: limit2 });
3600
+ return rows3.map((row2) => this.messageFromRow(row2));
3377
3601
  }
3378
3602
  /** Search every chat the member can read: its DMs and channels and their threads (newest first). `within` keeps
3379
3603
  * one chat, a channel with its threads; `sender` keeps one member's messages. */
3380
- searchAll(actorId, query, limit, within, sender) {
3604
+ searchAll(actorId, query, limit2, within, sender) {
3381
3605
  const scope = (within ? "AND (c.id=? OR c.parent_id=?) " : "") + (sender ? "AND m.sender_id=?" : "");
3382
3606
  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
3607
  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));
3608
+ WHERE instr(lower(m.content),lower(?))>0 ${scope} ORDER BY m.position DESC LIMIT ?`).all(actorId, query, ...within ? [within, within] : [], ...sender ? [sender] : [], limit2);
3609
+ return rows3.map((row2) => this.messageFromRow(row2));
3386
3610
  }
3387
3611
  /** Channels and threads the member can read whose title, topic or notice / summary contains the query (newest first). */
3388
- searchChatIds(actorId, query, limit) {
3612
+ searchChatIds(actorId, query, limit2) {
3389
3613
  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
3614
  LEFT JOIN chat_notes n ON n.chat_id=c.id WHERE c.type<>'dm'
3391
3615
  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);
3616
+ ORDER BY c.updated_at DESC LIMIT ?`).all(actorId, query, query, query, limit2).map((row2) => row2.id);
3393
3617
  }
3394
- messages(actorId, chatId, after2 = 0, limit = 1e3) {
3618
+ messages(actorId, chatId, after2 = 0, limit2 = 1e3) {
3395
3619
  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));
3620
+ const rows3 = this.db.prepare(`SELECT ${MESSAGE_COLUMNS} FROM messages WHERE chat_id=? AND sequence>? ORDER BY sequence LIMIT ?`).all(chatId, after2, limit2);
3621
+ return rows3.map((row2) => this.messageFromRow(row2));
3398
3622
  }
3399
3623
  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;
3624
+ const row2 = this.db.prepare(`SELECT ${MESSAGE_COLUMNS} FROM messages WHERE id=?`).get(id);
3625
+ return row2 && this.canAccess(actorId, row2.chatId) ? this.messageFromRow(row2) : null;
3402
3626
  }
3403
- search(actorId, chatId, query, limit, sender) {
3627
+ search(actorId, chatId, query, limit2, sender) {
3404
3628
  this.assertAccess(actorId, chatId);
3405
3629
  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));
3630
+ ORDER BY sequence DESC LIMIT ?`).all(chatId, query, ...sender ? [sender] : [], limit2);
3631
+ return rows3.map((row2) => this.messageFromRow(row2));
3408
3632
  }
3409
- sentActivity(actorId, senderId, before, limit) {
3633
+ sentActivity(actorId, senderId, before, limit2) {
3410
3634
  const condition = !before ? "" : before.kind === "chat" ? "AND (m.created_at<? OR (m.created_at=? AND m.id<?))" : "AND m.created_at<=?";
3411
3635
  const cursor2 = !before ? [] : before.kind === "chat" ? [before.timestamp, before.timestamp, before.id] : [before.timestamp];
3412
3636
  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 +3638,34 @@ var ChatRepository = class {
3414
3638
  (SELECT count(*) FROM attachments a WHERE a.message_id=m.id) AS attachmentCount
3415
3639
  FROM messages m JOIN chats c ON c.id=m.chat_id
3416
3640
  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 } }));
3641
+ WHERE m.sender_id=? ${condition} ORDER BY m.created_at DESC,m.id DESC LIMIT ?`).all(actorId, senderId, ...cursor2, limit2);
3642
+ return rows3.map(({ historyId, toolCallId, ...row2 }) => ({ ...row2, source: historyId === null ? null : { historyId, toolCallId } }));
3419
3643
  }
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);
3644
+ messageFromRow(row2) {
3645
+ 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
3646
  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,
3647
+ id: row2.id,
3648
+ chatId: row2.chatId,
3649
+ sequence: row2.sequence,
3650
+ sender: this.member(row2.senderId),
3651
+ content: row2.content,
3652
+ createdAt: row2.createdAt,
3429
3653
  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
3654
+ source: row2.historyId === null ? null : { historyId: row2.historyId, toolCallId: row2.toolCallId },
3655
+ question: row2.question === null ? null : JSON.parse(row2.question),
3656
+ 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),
3657
+ thread: this.threads.info(row2.id),
3658
+ echoOf: row2.echoOf === null ? null : { id: row2.echoOf, title: this.db.prepare("SELECT title FROM chats WHERE id=?").get(row2.echoOf).title },
3659
+ replyTo: row2.replyTo === null ? null : this.quote(row2.replyTo),
3660
+ reactions: this.interactions.reactions(row2.id),
3661
+ quotes: this.quotes.of(row2.id),
3662
+ folded: row2.foldedAt !== null
3439
3663
  };
3440
3664
  }
3441
3665
  /** 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
3666
  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;
3667
+ const row2 = this.db.prepare("SELECT id,sender_id AS senderId,content FROM messages WHERE id=?").get(id);
3668
+ return row2 ? { id: row2.id, senderId: row2.senderId, content: clipMarked(row2.content, 200), quotes: this.quotes.of(row2.id) } : null;
3445
3669
  }
3446
3670
  append(actorId, chatId, draft, requestHash, files = [], deliveries = { message: [], echo: [] }, mentions = []) {
3447
3671
  return transaction(this.db, () => {
@@ -3531,15 +3755,15 @@ var ChatRepository = class {
3531
3755
  return file;
3532
3756
  }
3533
3757
  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;
3758
+ 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);
3759
+ return row2 && this.canAccess(actorId, row2.chatId) ? { id: row2.id, name: row2.name, mimeType: row2.mimeType, size: row2.size } : null;
3536
3760
  }
3537
3761
  hasAttachment(id) {
3538
3762
  return Boolean(this.db.prepare("SELECT 1 FROM attachments WHERE id=?").get(id));
3539
3763
  }
3540
3764
  /** Ids of attachments deleted since the last call, cascades included (the trigger of v28), whose files can go. */
3541
3765
  removedAttachments() {
3542
- return this.db.prepare("SELECT id FROM attachment_removals").all().map((row) => row.id);
3766
+ return this.db.prepare("SELECT id FROM attachment_removals").all().map((row2) => row2.id);
3543
3767
  }
3544
3768
  forgetRemoved(ids) {
3545
3769
  const forget = this.db.prepare("DELETE FROM attachment_removals WHERE id=?");
@@ -3564,8 +3788,8 @@ var RuntimeRepository = class {
3564
3788
  this.inbox = inbox;
3565
3789
  }
3566
3790
  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;
3791
+ 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);
3792
+ return row2 ? { ...row2, persisted: !!row2.persisted } : null;
3569
3793
  }
3570
3794
  ensureSession(botId, file) {
3571
3795
  this.db.prepare("INSERT INTO bot_sessions(bot_id,file,created_at) VALUES(?,?,?) ON CONFLICT(bot_id) DO NOTHING").run(botId, file, Date.now());
@@ -3621,19 +3845,19 @@ var RuntimeRepository = class {
3621
3845
  });
3622
3846
  }
3623
3847
  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;
3848
+ const row2 = this.db.prepare(`SELECT ${RUN_COLUMNS} FROM bot_runs WHERE id=?`).get(id);
3849
+ return row2 ? this.fromRow(row2) : null;
3626
3850
  }
3627
3851
  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));
3852
+ 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
3853
  }
3630
3854
  /** The error code of the Bot's latest run; null when that run did not fail. */
3631
3855
  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;
3856
+ 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);
3857
+ return row2?.errorCode ?? null;
3634
3858
  }
3635
- fromRow({ selectionJson, ...row }) {
3636
- return { ...row, selection: JSON.parse(selectionJson) };
3859
+ fromRow({ selectionJson, ...row2 }) {
3860
+ return { ...row2, selection: JSON.parse(selectionJson) };
3637
3861
  }
3638
3862
  inputs(runId) {
3639
3863
  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);
@@ -3681,16 +3905,16 @@ var CredentialRepository = class {
3681
3905
  }
3682
3906
  /** Like `read`, but a value that doesn't open under this installation's key comes back as null, so it can be replaced. */
3683
3907
  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
3908
+ 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
3909
  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 };
3910
+ if (!row2) return null;
3911
+ const plain2 = Cipher.sealed(row2.json) ? this.cipher.open(row2.json, `credential:${connectionId}`) : row2.json;
3912
+ return { value: plain2 === null ? null : credentialValue(JSON.parse(plain2)), revision: row2.json };
3689
3913
  }
3690
3914
  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;
3915
+ const row2 = this.current(ownerId, connectionId);
3916
+ 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.");
3917
+ return row2;
3694
3918
  }
3695
3919
  replace(ownerId, connectionId, prior, value2) {
3696
3920
  const next = this.stored(connectionId, value2);
@@ -3718,14 +3942,14 @@ var AgentRepository = class {
3718
3942
  /** Brings the agent Bot back when it is missing (deleted before it became undeletable): the same id, the first free
3719
3943
  * name of Jarvy, Jarvy 2, …, no persona and no model, so the owner picks one and is welcomed again. */
3720
3944
  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;
3945
+ const row2 = this.db.prepare("SELECT deleted_at AS deletedAt FROM bots WHERE id=?").get(AGENT_BOT_ID);
3946
+ if (row2 && row2.deletedAt === null) return;
3723
3947
  const taken = this.db.prepare(`SELECT 1 FROM account WHERE lower(username)=lower(:name)
3724
3948
  UNION ALL SELECT 1 FROM bots WHERE lower(name)=lower(:name) AND deleted_at IS NULL LIMIT 1`);
3725
3949
  let name3 = "Jarvy";
3726
3950
  for (let n = 2; taken.get({ name: name3 }); n++) name3 = `Jarvy ${n}`;
3727
3951
  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'
3952
+ if (row2) this.db.prepare(`UPDATE bots SET deleted_at=NULL,name=?,description='',system_prompt='',connection_id=NULL,model_id='',thinking_level='auto'
3729
3953
  WHERE id=?`).run(name3, AGENT_BOT_ID);
3730
3954
  else {
3731
3955
  this.db.prepare("INSERT OR IGNORE INTO members VALUES(?,'bot')").run(AGENT_BOT_ID);
@@ -3744,15 +3968,15 @@ var AgentRepository = class {
3744
3968
  import { randomUUID as randomUUID12 } from "node:crypto";
3745
3969
  var FIELDS = (table) => ["id", "owner_id AS ownerId", "kind", "bot_id AS botId", "name", "directory", "description", "connection"].map((column) => `${table}${column}`).join(", ");
3746
3970
  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)
3971
+ var record = (row2) => ({
3972
+ id: row2.id,
3973
+ ownerId: row2.ownerId,
3974
+ kind: row2.kind,
3975
+ botId: row2.botId,
3976
+ name: row2.name,
3977
+ directory: row2.directory,
3978
+ description: row2.description,
3979
+ spec: JSON.parse(row2.connection)
3756
3980
  });
3757
3981
  var EnvironmentRepository = class {
3758
3982
  constructor(db, cipher, ownerId) {
@@ -3761,12 +3985,12 @@ var EnvironmentRepository = class {
3761
3985
  this.ownerId = ownerId;
3762
3986
  }
3763
3987
  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;
3988
+ const row2 = this.db.prepare(`SELECT ${COLUMNS} FROM environments WHERE id=? AND deleted_at IS NULL`).get(id);
3989
+ return row2 ? record(row2) : null;
3766
3990
  }
3767
3991
  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;
3992
+ const row2 = this.db.prepare(`SELECT ${COLUMNS} FROM environments WHERE bot_id=? AND kind='computer' AND deleted_at IS NULL`).get(botId);
3993
+ return row2 ? record(row2) : null;
3770
3994
  }
3771
3995
  /** Every live Bot's computer. */
3772
3996
  computers() {
@@ -3781,6 +4005,18 @@ var EnvironmentRepository = class {
3781
4005
  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
4006
  return this.get(id);
3783
4007
  }
4008
+ /** Keeps a computer's limits (A75) with how it is reached: memory in bytes and CPUs in cores, each left out when
4009
+ * there is none. */
4010
+ setLimits(id, limits2) {
4011
+ const computer = this.get(id);
4012
+ if (computer?.kind !== "computer") return;
4013
+ const { memory: _memory, cpus: _cpus, ...spec } = computer.spec;
4014
+ this.db.prepare("UPDATE environments SET connection=? WHERE id=?").run(JSON.stringify({
4015
+ ...spec,
4016
+ ...limits2.memory !== null ? { memory: String(limits2.memory) } : {},
4017
+ ...limits2.cpus !== null ? { cpus: String(limits2.cpus) } : {}
4018
+ }), id);
4019
+ }
3784
4020
  /** The Bot is deleted, and its computer with it. Its row stays its home, deleted, so what reads a deleted Bot's
3785
4021
  * placement still finds one. */
3786
4022
  dropComputer(botId) {
@@ -3794,11 +4030,11 @@ var EnvironmentRepository = class {
3794
4030
  /** The Bot's default environment, its computer, and the one it works in now. Every Bot has its computer (A68):
3795
4031
  * one is made with the Bot, and v29 gave one to every Bot from before. */
3796
4032
  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;
4033
+ const row2 = this.db.prepare("SELECT home_environment_id AS home, current_environment_id AS current FROM bots WHERE id=?").get(botId);
4034
+ const found = row2?.home ? this.db.prepare(`SELECT ${COLUMNS} FROM environments WHERE id=? AND kind='computer'`).get(row2.home) : void 0;
3799
4035
  if (!found) throw new Error(`Bot ${botId} has no computer`);
3800
4036
  const home = record(found);
3801
- const current = row?.current && this.get(row.current) || home;
4037
+ const current = row2?.current && this.get(row2.current) || home;
3802
4038
  return { home, current };
3803
4039
  }
3804
4040
  /** Makes an environment the Bot's default and moves the Bot there. */
@@ -3852,14 +4088,14 @@ var EnvironmentRepository = class {
3852
4088
  /** The machine's private key (null when it doesn't open under this installation's key), its password when it is
3853
4089
  * reached with one (A59; `passwordUnreadable` when it is kept but doesn't open) and the accepted host keys. */
3854
4090
  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;
4091
+ 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);
4092
+ if (!row2) return null;
4093
+ const password = row2.password ? this.cipher.open(row2.password, `environment-password:${id}`) : null;
3858
4094
  return {
3859
- privateKey: row.key ? this.cipher.open(row.key, `environment:${id}`) : null,
4095
+ privateKey: row2.key ? this.cipher.open(row2.key, `environment:${id}`) : null,
3860
4096
  password,
3861
- passwordUnreadable: !!row.password && password === null,
3862
- hostKeys: row.hostKeys ? JSON.parse(row.hostKeys) : { keys: [], fingerprints: [] }
4097
+ passwordUnreadable: !!row2.password && password === null,
4098
+ hostKeys: row2.hostKeys ? JSON.parse(row2.hostKeys) : { keys: [], fingerprints: [] }
3863
4099
  };
3864
4100
  }
3865
4101
  /** Whether a machine is reached with its password rather than the key (A59). */
@@ -3868,12 +4104,12 @@ var EnvironmentRepository = class {
3868
4104
  }
3869
4105
  /** The Bots that may use an environment. */
3870
4106
  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);
4107
+ 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
4108
  }
3873
4109
  /** Deletes an SSH machine with its key: Bots working there are back in their default. Returns those Bots. */
3874
4110
  dropSsh(id) {
3875
4111
  return transaction(this.db, () => {
3876
- const moved = this.db.prepare("SELECT id FROM bots WHERE current_environment_id=?").all(id).map((row) => row.id);
4112
+ const moved = this.db.prepare("SELECT id FROM bots WHERE current_environment_id=?").all(id).map((row2) => row2.id);
3877
4113
  this.db.prepare("UPDATE bots SET current_environment_id=home_environment_id WHERE current_environment_id=?").run(id);
3878
4114
  this.db.prepare("DELETE FROM environment_grants WHERE environment_id=?").run(id);
3879
4115
  this.db.prepare("UPDATE environments SET deleted_at=?, key=NULL, password=NULL WHERE id=? AND kind='ssh'").run(Date.now(), id);
@@ -3888,8 +4124,8 @@ var SettingsRepository = class {
3888
4124
  this.db = db;
3889
4125
  }
3890
4126
  get(key) {
3891
- const row = this.db.prepare("SELECT value FROM settings WHERE key=?").get(key);
3892
- return row?.value ?? null;
4127
+ const row2 = this.db.prepare("SELECT value FROM settings WHERE key=?").get(key);
4128
+ return row2?.value ?? null;
3893
4129
  }
3894
4130
  set(key, value2) {
3895
4131
  if (value2 === null) this.db.prepare("DELETE FROM settings WHERE key=?").run(key);
@@ -3913,19 +4149,19 @@ var SecretRepository = class {
3913
4149
  this.cipher = cipher;
3914
4150
  this.ownerId = ownerId;
3915
4151
  }
3916
- toRecord(row) {
4152
+ toRecord(row2) {
3917
4153
  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 };
4154
+ 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 }));
4155
+ const { value: value2, ...rest } = row2;
4156
+ return { ...rest, readable: this.cipher.open(value2, `secret:${row2.id}`) !== null, grants };
3921
4157
  }
3922
4158
  list() {
3923
4159
  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));
4160
+ 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
4161
  }
3926
4162
  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;
4163
+ const row2 = this.db.prepare(`SELECT ${COLUMNS2} FROM secrets s WHERE s.id=? AND s.owner_id=?`).get(id, this.ownerId());
4164
+ return row2 ? this.toRecord(row2) : null;
3929
4165
  }
3930
4166
  checkValue(value2) {
3931
4167
  if (typeof value2 !== "string" || !value2) throw new ValidationError("A secret needs a value", "secret_value_missing");
@@ -3965,27 +4201,27 @@ var SecretRepository = class {
3965
4201
  revoke(id, botId) {
3966
4202
  return Number(this.db.prepare("DELETE FROM secret_grants WHERE secret_id=? AND bot_id=?").run(id, botId).changes) > 0;
3967
4203
  }
3968
- uses(id, limit = 100) {
4204
+ uses(id, limit2 = 100) {
3969
4205
  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 }));
4206
+ 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
4207
  }
3972
4208
  /** Every value the owner keeps that opens, for masking. */
3973
4209
  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);
4210
+ 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
4211
  }
3976
4212
  /** The secrets a Bot may use: granted until revoked, or for the run that is still going. */
3977
4213
  grantedTo(botId) {
3978
4214
  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
4215
  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 }));
4216
+ ORDER BY s.name`).all(botId, this.ownerId()).map((row2) => ({ ...row2 }));
3981
4217
  }
3982
4218
  /** The Bot's run in progress, which a "this time only" grant lasts for. */
3983
4219
  currentRun(botId) {
3984
4220
  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
4221
  }
3986
4222
  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;
4223
+ const row2 = this.db.prepare("SELECT id FROM secrets WHERE owner_id=? AND kind='value' AND name=?").get(this.ownerId(), name3);
4224
+ return row2 ? this.get(row2.id) : null;
3989
4225
  }
3990
4226
  /** "This time only" grants whose run has ended. */
3991
4227
  purge() {
@@ -3996,8 +4232,8 @@ var SecretRepository = class {
3996
4232
  }
3997
4233
  /** The plain value, for a command that uses it; null when it does not open. */
3998
4234
  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;
4235
+ const row2 = this.db.prepare("SELECT value FROM secrets WHERE id=?").get(id);
4236
+ return row2 ? this.cipher.open(row2.value, `secret:${id}`) : null;
4001
4237
  }
4002
4238
  };
4003
4239
 
@@ -4043,30 +4279,30 @@ function localIso(time2) {
4043
4279
  var COLUMNS3 = `id,bot_id,name,instruction,schedule_kind,schedule_at,schedule_minutes,schedule_hour,schedule_minute,schedule_weekday,
4044
4280
  state,next_run_at,last_run_at,created_at,updated_at`;
4045
4281
  var LIMIT = 200;
4046
- function scheduleOf(row) {
4047
- switch (row.schedule_kind) {
4282
+ function scheduleOf(row2) {
4283
+ switch (row2.schedule_kind) {
4048
4284
  case "once":
4049
- return { kind: "once", at: row.schedule_at };
4285
+ return { kind: "once", at: row2.schedule_at };
4050
4286
  case "every":
4051
- return { kind: "every", minutes: row.schedule_minutes, since: row.schedule_at };
4287
+ return { kind: "every", minutes: row2.schedule_minutes, since: row2.schedule_at };
4052
4288
  case "daily":
4053
- return { kind: "daily", hour: row.schedule_hour, minute: row.schedule_minute };
4289
+ return { kind: "daily", hour: row2.schedule_hour, minute: row2.schedule_minute };
4054
4290
  case "weekly":
4055
- return { kind: "weekly", weekday: row.schedule_weekday, hour: row.schedule_hour, minute: row.schedule_minute };
4291
+ return { kind: "weekly", weekday: row2.schedule_weekday, hour: row2.schedule_hour, minute: row2.schedule_minute };
4056
4292
  }
4057
4293
  }
4058
- function toTask(row) {
4294
+ function toTask(row2) {
4059
4295
  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
4296
+ id: row2.id,
4297
+ botId: row2.bot_id,
4298
+ name: row2.name,
4299
+ instruction: row2.instruction,
4300
+ schedule: scheduleOf(row2),
4301
+ state: row2.state,
4302
+ nextRunAt: row2.next_run_at,
4303
+ lastRunAt: row2.last_run_at,
4304
+ createdAt: row2.created_at,
4305
+ updatedAt: row2.updated_at
4070
4306
  };
4071
4307
  }
4072
4308
  function scheduleColumns(schedule) {
@@ -4091,8 +4327,8 @@ var TaskRepository = class {
4091
4327
  return rows3.map(toTask);
4092
4328
  }
4093
4329
  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;
4330
+ const row2 = this.db.prepare(`SELECT ${COLUMNS3} FROM tasks WHERE id=? AND bot_id=? AND deleted_at IS NULL`).get(id, botId);
4331
+ return row2 ? toTask(row2) : null;
4096
4332
  }
4097
4333
  create(input) {
4098
4334
  const now = Date.now(), at2 = nextAfter(input.schedule, now);
@@ -4149,8 +4385,8 @@ var TaskRepository = class {
4149
4385
  }
4150
4386
  /** The soonest time any task runs; the scheduler sleeps until then. Null when nothing is scheduled. */
4151
4387
  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;
4388
+ 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();
4389
+ return row2?.at ?? null;
4154
4390
  }
4155
4391
  due(now) {
4156
4392
  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 +4400,18 @@ var TaskRepository = class {
4164
4400
  if (!due.length) return [];
4165
4401
  return transaction(this.db, () => {
4166
4402
  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);
4403
+ for (const row2 of due) {
4404
+ const at2 = row2.next_run_at, next = nextAfter(scheduleOf(row2), now);
4405
+ 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
4406
  this.inbox.write([{
4171
- delivery: { member: { id: row.bot_id }, reason: "schedule", kind: "task", urgency },
4407
+ delivery: { member: { id: row2.bot_id }, reason: "schedule", kind: "task", urgency },
4172
4408
  chatId: null,
4173
4409
  messageId: null,
4174
4410
  actorId: null,
4175
- payload: { task: row.id, due: at2 },
4411
+ payload: { task: row2.id, due: at2 },
4176
4412
  createdAt: now
4177
4413
  }]);
4178
- fired.push({ taskId: row.id, botId: row.bot_id, at: at2 });
4414
+ fired.push({ taskId: row2.id, botId: row2.bot_id, at: at2 });
4179
4415
  }
4180
4416
  return fired;
4181
4417
  });
@@ -4184,14 +4420,70 @@ var TaskRepository = class {
4184
4420
  * past is done without running. Returns how many were skipped. */
4185
4421
  skipOverdue(now = Date.now()) {
4186
4422
  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);
4423
+ for (const row2 of rows3) {
4424
+ const next = nextAfter(scheduleOf(row2), now);
4425
+ this.db.prepare("UPDATE tasks SET next_run_at=?,state=?,updated_at=? WHERE id=?").run(next, next === null ? "done" : "active", now, row2.id);
4190
4426
  }
4191
4427
  return rows3.length;
4192
4428
  }
4193
4429
  };
4194
4430
 
4431
+ // src/server/infra/storage/port-repository.ts
4432
+ var COLUMNS4 = "bot_id,port,name,description,host_port,created_at,updated_at";
4433
+ var toRecord = (row2) => ({
4434
+ botId: row2.bot_id,
4435
+ port: row2.port,
4436
+ name: row2.name,
4437
+ description: row2.description,
4438
+ hostPort: row2.host_port,
4439
+ createdAt: row2.created_at,
4440
+ updatedAt: row2.updated_at
4441
+ });
4442
+ var PortRepository = class {
4443
+ constructor(db) {
4444
+ this.db = db;
4445
+ }
4446
+ /** Every published port, of every Bot that still exists, by Jarvy's port. */
4447
+ all() {
4448
+ 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)
4449
+ ORDER BY host_port`).all().map(toRecord);
4450
+ }
4451
+ /** A Bot's published ports, the first published first. */
4452
+ list(botId) {
4453
+ return this.db.prepare(`SELECT ${COLUMNS4} FROM published_ports WHERE bot_id=? ORDER BY created_at, port`).all(botId).map(toRecord);
4454
+ }
4455
+ get(botId, port) {
4456
+ const row2 = this.db.prepare(`SELECT ${COLUMNS4} FROM published_ports WHERE bot_id=? AND port=?`).get(botId, port);
4457
+ return row2 ? toRecord(row2) : null;
4458
+ }
4459
+ byHostPort(hostPort) {
4460
+ const row2 = this.db.prepare(`SELECT ${COLUMNS4} FROM published_ports WHERE host_port=?`).get(hostPort);
4461
+ return row2 ? toRecord(row2) : null;
4462
+ }
4463
+ /** Jarvy's ports already taken, for picking a free one. */
4464
+ hostPorts() {
4465
+ return new Set(this.db.prepare("SELECT host_port FROM published_ports").all().map((row2) => row2.host_port));
4466
+ }
4467
+ /** Publishes a port on `hostPort`, or renames one already published, which keeps its own Jarvy port. */
4468
+ save(input) {
4469
+ const now = Date.now();
4470
+ this.db.prepare(`INSERT INTO published_ports(${COLUMNS4}) VALUES(?,?,?,?,?,?,?)
4471
+ 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);
4472
+ return this.get(input.botId, input.port);
4473
+ }
4474
+ /** Moves a published port to another Jarvy port, when its own was taken by another program meanwhile. */
4475
+ move(botId, port, hostPort) {
4476
+ this.db.prepare("UPDATE published_ports SET host_port=?, updated_at=? WHERE bot_id=? AND port=?").run(hostPort, Date.now(), botId, port);
4477
+ }
4478
+ remove(botId, port) {
4479
+ return Number(this.db.prepare("DELETE FROM published_ports WHERE bot_id=? AND port=?").run(botId, port).changes) > 0;
4480
+ }
4481
+ /** Unpublishes all of a Bot's ports, as when its sandbox is reset. */
4482
+ removeAll(botId) {
4483
+ return Number(this.db.prepare("DELETE FROM published_ports WHERE bot_id=?").run(botId).changes);
4484
+ }
4485
+ };
4486
+
4195
4487
  // src/server/infra/vault/master-key.ts
4196
4488
  import { randomBytes as randomBytes3 } from "node:crypto";
4197
4489
  import { chmodSync, existsSync, mkdirSync as mkdirSync3, readFileSync, writeFileSync } from "node:fs";
@@ -4241,6 +4533,8 @@ var Store = class {
4241
4533
  settings;
4242
4534
  /** Scheduled tasks (A58). */
4243
4535
  tasks;
4536
+ /** Ports Bots published from their sandboxes (A74). */
4537
+ ports;
4244
4538
  secrets;
4245
4539
  /** Seals secrets and model credentials under the master key (A52). */
4246
4540
  cipher;
@@ -4269,6 +4563,7 @@ var Store = class {
4269
4563
  this.environments = new EnvironmentRepository(this.db, this.cipher, ownerId);
4270
4564
  this.settings = new SettingsRepository(this.db);
4271
4565
  this.tasks = new TaskRepository(this.db, this.chats.inbox);
4566
+ this.ports = new PortRepository(this.db);
4272
4567
  this.secrets = new SecretRepository(this.db, this.cipher, ownerId);
4273
4568
  this.agent.ensure();
4274
4569
  }
@@ -4300,10 +4595,10 @@ var set = (...items) => tlv(49, Buffer.concat(items));
4300
4595
  var integer2 = (bytes) => tlv(2, bytes[0] & 128 ? Buffer.concat([Buffer.from([0]), bytes]) : bytes);
4301
4596
  var bits = (bytes, unused = 0) => tlv(3, Buffer.concat([Buffer.from([unused]), bytes]));
4302
4597
  var octets = (bytes) => tlv(4, bytes);
4303
- var utf8 = (text4) => tlv(12, Buffer.from(text4, "utf8"));
4598
+ var utf8 = (text5) => tlv(12, Buffer.from(text5, "utf8"));
4304
4599
  var TRUE = tlv(1, Buffer.from([255]));
4305
- function oid(text4) {
4306
- const [a, b, ...rest] = text4.split(".").map(Number);
4600
+ function oid(text5) {
4601
+ const [a, b, ...rest] = text5.split(".").map(Number);
4307
4602
  const out = [a * 40 + b];
4308
4603
  for (const value2 of rest) {
4309
4604
  const chunk = [value2 & 127];
@@ -4405,8 +4700,8 @@ function read2(dir, file) {
4405
4700
  const path = join7(dir, file);
4406
4701
  return existsSync2(path) ? readFileSync2(path, "utf8") : null;
4407
4702
  }
4408
- function write(dir, file, text4, secret) {
4409
- writeFileSync2(join7(dir, file), text4, { mode: secret ? 384 : 420 });
4703
+ function write(dir, file, text5, secret) {
4704
+ writeFileSync2(join7(dir, file), text5, { mode: secret ? 384 : 420 });
4410
4705
  }
4411
4706
  function readKey(dir, name3, cipher, log) {
4412
4707
  const plain2 = read2(dir, `${name3}.pem`);
@@ -4605,14 +4900,14 @@ var ConnectionCredentials = class {
4605
4900
  async read(providerId, options) {
4606
4901
  options?.signal?.throwIfAborted();
4607
4902
  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;
4903
+ const row2 = this.repository.read(this.ownerId, this.connectionId);
4904
+ if (!row2) throw new RuntimeFault("credentials_missing", "Set up this connection's credential first.", 422);
4905
+ return row2.value;
4611
4906
  }
4612
4907
  async list(options) {
4613
4908
  options?.signal?.throwIfAborted();
4614
- const row = this.repository.current(this.ownerId, this.connectionId);
4615
- return row?.value ? [{ providerId: this.providerName, type: row.value.type }] : [];
4909
+ const row2 = this.repository.current(this.ownerId, this.connectionId);
4910
+ return row2?.value ? [{ providerId: this.providerName, type: row2.value.type }] : [];
4616
4911
  }
4617
4912
  async modify(providerId, fn, options) {
4618
4913
  if (providerId !== this.providerName) throw new RuntimeFault("credential_scope", "The credential belongs to another connection.");
@@ -4685,17 +4980,17 @@ var strip = (id) => id.toLowerCase().replace(/^.*\//, "").replace(/-(\d{6,8})$/,
4685
4980
  function rows(providers) {
4686
4981
  return providers.flatMap((provider) => provider.getModels().map((model) => ({ provider: provider.id, model, official: officialScore(provider.id, model.id) })));
4687
4982
  }
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);
4983
+ var source = (row2) => ({ provider: row2.provider, modelId: row2.model.id, name: row2.model.name, api: row2.model.api, official: row2.official === 2 });
4984
+ 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
4985
  function candidates(all, query) {
4691
4986
  const q = query.trim().toLowerCase();
4692
4987
  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)));
4988
+ const same3 = all.filter((row2) => row2.model.id.toLowerCase() === q || strip(row2.model.id) === strip(q));
4989
+ return same3.length ? same3 : all.filter((row2) => row2.model.id.toLowerCase().includes(strip(q)));
4695
4990
  }
4696
4991
  function similar(all, query) {
4697
4992
  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);
4993
+ 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
4994
  }
4700
4995
  function similarity(a, b) {
4701
4996
  let prefix = 0;
@@ -4711,12 +5006,12 @@ function levenshtein(a, b) {
4711
5006
  }
4712
5007
  return previous[b.length];
4713
5008
  }
4714
- function matched(row, api, alternatives, reseller) {
4715
- const model = publicModel(row.model);
4716
- const sameApi = row.model.api === api;
5009
+ function matched(row2, api, alternatives, reseller) {
5010
+ const model = publicModel(row2.model);
5011
+ const sameApi = row2.model.api === api;
4717
5012
  return {
4718
5013
  status: "matched",
4719
- source: source(row),
5014
+ source: source(row2),
4720
5015
  reseller,
4721
5016
  apiMismatch: !sameApi,
4722
5017
  fields: {
@@ -4727,28 +5022,28 @@ function matched(row, api, alternatives, reseller) {
4727
5022
  cost: model.cost,
4728
5023
  thinkingLevelMap: model.thinkingLevelMap,
4729
5024
  thinkingFormat: sameApi ? model.thinkingFormat : "",
4730
- compat: sameApi ? catalogCompat(row.model) : {}
5025
+ compat: sameApi ? catalogCompat(row2.model) : {}
4731
5026
  },
4732
- alternatives: alternatives.filter((item) => item !== row).slice(0, 8).map(source)
5027
+ alternatives: alternatives.filter((item) => item !== row2).slice(0, 8).map(source)
4733
5028
  };
4734
5029
  }
4735
5030
  function autoconfigure(providers, modelId, api, choice2) {
4736
5031
  const all = rows(providers), hits = candidates(all, modelId);
4737
5032
  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);
5033
+ const row2 = all.find((item) => item.provider === choice2.provider && item.model.id === choice2.modelId);
5034
+ if (row2) return matched(row2, api, hits, row2.official < 2);
4740
5035
  }
4741
5036
  if (!hits.length) {
4742
5037
  const options = similar(all, modelId);
4743
5038
  return options.length ? { status: "choose", reason: "similar", options: options.map(source) } : { status: "none" };
4744
5039
  }
4745
- const official = hits.filter((row) => row.official === 2);
5040
+ const official = hits.filter((row2) => row2.official === 2);
4746
5041
  const ranked = [...hits].sort((a, b) => b.official - a.official || completeness(b) - completeness(a));
4747
5042
  if (official.length === 1) return matched(official[0], api, ranked, false);
4748
5043
  if (official.length > 1) {
4749
5044
  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);
5045
+ const exact = official.filter((row2) => words.includes(row2.provider));
5046
+ const sameApi = official.filter((row2) => row2.model.api === api);
4752
5047
  const winner = exact.length === 1 ? exact[0] : sameApi.length === 1 ? sameApi[0] : void 0;
4753
5048
  if (winner) return matched(winner, api, ranked, false);
4754
5049
  return { status: "choose", reason: "multiple", options: official.sort((a, b) => a.provider.length - b.provider.length).map(source) };
@@ -4919,13 +5214,13 @@ var ModelService = class {
4919
5214
  }
4920
5215
  /** Suggests model fields from Pi's catalog (with pi.dev's newer entries, A49); the caller decides whether to save them. */
4921
5216
  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);
5217
+ const text5 = (value2) => typeof value2 === "string" && value2.trim() && value2.length <= 200 ? value2.trim() : null;
5218
+ const modelId = text5(input.modelId), api = text5(input.api);
4924
5219
  if (!modelId || !api) throw new RuntimeFault("autoconfig_model", "Enter the model ID.", 400);
4925
5220
  let choice2;
4926
5221
  if (input.choice !== void 0) {
4927
5222
  const raw = input.choice;
4928
- const provider = text4(raw?.provider), chosen = text4(raw?.modelId);
5223
+ const provider = text5(raw?.provider), chosen = text5(raw?.modelId);
4929
5224
  if (!provider || !chosen) throw new RuntimeFault("autoconfig_source", "The setup source isn't valid.", 400);
4930
5225
  choice2 = { provider, modelId: chosen };
4931
5226
  }
@@ -5089,14 +5384,14 @@ import lockfile from "proper-lockfile";
5089
5384
  // src/contract/mentions.ts
5090
5385
  var WORD = /[\p{Script=Latin}\p{Nd}_]/u;
5091
5386
  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 = [];
5387
+ const text5 = source2.replace(/```[\s\S]*?```|`[^`\n]*`/g, (code) => " ".repeat(code.length));
5388
+ const lower = text5.toLowerCase(), sorted = [...candidates2].sort((a, b) => b.name.length - a.name.length), found = [];
5094
5389
  for (let at2 = lower.indexOf("@"); at2 >= 0; at2 = lower.indexOf("@", at2 + 1)) {
5095
- if (at2 > 0 && WORD.test(text4[at2 - 1])) continue;
5390
+ if (at2 > 0 && WORD.test(text5[at2 - 1])) continue;
5096
5391
  const member = sorted.find((candidate) => {
5097
5392
  const name3 = candidate.name.toLowerCase();
5098
5393
  if (!name3 || !lower.startsWith(name3, at2 + 1)) return false;
5099
- const next = text4[at2 + 1 + name3.length];
5394
+ const next = text5[at2 + 1 + name3.length];
5100
5395
  return !(next && WORD.test(next) && WORD.test(name3.at(-1)));
5101
5396
  });
5102
5397
  if (member) {
@@ -5345,11 +5640,11 @@ var ChatMessages = class {
5345
5640
  const message = this.repository.message(this.actor, id);
5346
5641
  return message?.chatId === this.id ? message : null;
5347
5642
  }
5348
- async list({ after: after2 = 0, limit }) {
5349
- return this.repository.messages(this.actor, this.id, after2, limit);
5643
+ async list({ after: after2 = 0, limit: limit2 }) {
5644
+ return this.repository.messages(this.actor, this.id, after2, limit2);
5350
5645
  }
5351
- async search(query, { limit }) {
5352
- return this.repository.search(this.actor, this.id, query, limit);
5646
+ async search(query, { limit: limit2 }) {
5647
+ return this.repository.search(this.actor, this.id, query, limit2);
5353
5648
  }
5354
5649
  };
5355
5650
 
@@ -5671,32 +5966,32 @@ var ChatThreads = class {
5671
5966
  * cursor is the last row's sort key, so a thread that moves while you scroll is neither repeated nor skipped. */
5672
5967
  followedPage(actorId, query) {
5673
5968
  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");
5969
+ const limit2 = query.limit === void 0 ? THREAD_PAGE : Number(query.limit);
5970
+ if (!Number.isInteger(limit2) || limit2 < 1 || limit2 > 100) throw new ChatError(400, "Invalid limit");
5676
5971
  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 };
5972
+ 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);
5973
+ const page2 = rows3.slice(0, limit2);
5974
+ 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
5975
  }
5681
5976
  /** The followed threads in numbers (A69), sent with the chat list for the sidebar and the page's tabs. */
5682
5977
  followedSummary(actorId) {
5683
5978
  const summary = { total: 0, open: 0, done: 0, unreadThreads: 0, unread: 0, mentions: 0 };
5684
- for (const row of this.store.followedKeys(actorId)) {
5979
+ for (const row2 of this.store.followedKeys(actorId)) {
5685
5980
  summary.total++;
5686
- summary[row.status]++;
5687
- summary.unread += row.unread;
5688
- summary.mentions += row.mentions;
5689
- if (row.unread > 0) summary.unreadThreads++;
5981
+ summary[row2.status]++;
5982
+ summary.unread += row2.unread;
5983
+ summary.mentions += row2.mentions;
5984
+ if (row2.unread > 0) summary.unreadThreads++;
5690
5985
  }
5691
5986
  return summary;
5692
5987
  }
5693
5988
  /** The open threads a member follows, most recently active first, up to `limit`, and how many there are (A73): what a
5694
5989
  * 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 }] : [];
5990
+ openFollowed(actorId, limit2) {
5991
+ 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));
5992
+ const threads = open2.slice(0, limit2).flatMap((row2) => {
5993
+ const chat = this.repository.getChat(actorId, row2.id), channel2 = chat?.parentId ? this.repository.getChat(actorId, chat.parentId) : null;
5994
+ return chat && channel2 ? [{ id: chat.id, title: chat.title, channel: { id: channel2.id, title: channel2.title }, lastReplyAt: row2.lastReplyAt, unread: row2.unread }] : [];
5700
5995
  });
5701
5996
  return { total: open2.length, threads };
5702
5997
  }
@@ -5810,8 +6105,8 @@ var ChatService = class {
5810
6105
  get(actorId, chatId) {
5811
6106
  return this.repository.getChat(actorId, chatId);
5812
6107
  }
5813
- sentActivity(actorId, senderId, before, limit) {
5814
- return this.repository.sentActivity(actorId, senderId, before, limit);
6108
+ sentActivity(actorId, senderId, before, limit2) {
6109
+ return this.repository.sentActivity(actorId, senderId, before, limit2);
5815
6110
  }
5816
6111
  member(id) {
5817
6112
  return this.repository.member(id);
@@ -5902,19 +6197,19 @@ var ChatService = class {
5902
6197
  ];
5903
6198
  }
5904
6199
  /** 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);
6200
+ unreadMessages(actorId, chatId, limit2) {
6201
+ return this.repository.unreadMessages(actorId, chatId, limit2);
5907
6202
  }
5908
6203
  /** `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");
6204
+ searchAll(actorId, query, limit2 = 30, within, sender) {
6205
+ if (!query.trim() || query.length > 500 || !Number.isInteger(limit2) || limit2 < 1 || limit2 > 100) throw new ChatError(400, "Invalid search query or limit");
5911
6206
  if (within) this.repository.assertAccess(actorId, within);
5912
- return this.repository.searchAll(actorId, query.trim(), limit, within, sender);
6207
+ return this.repository.searchAll(actorId, query.trim(), limit2, within, sender);
5913
6208
  }
5914
6209
  /** The search box: channels and threads by title, topic or notice / summary, and messages by text, with the chat
5915
6210
  * 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);
6211
+ find(actorId, query, { within, limit: limit2 = 50 } = {}) {
6212
+ const messages = this.searchAll(actorId, query, limit2, within);
5918
6213
  const chats = within ? [] : this.repository.searchChatIds(actorId, query.trim(), 30).map((id) => this.repository.getChat(actorId, id)).filter((chat) => chat !== null);
5919
6214
  const where2 = /* @__PURE__ */ new Map();
5920
6215
  const chatOf = (id) => {
@@ -5927,16 +6222,16 @@ var ChatService = class {
5927
6222
  }) };
5928
6223
  }
5929
6224
  /** 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)
6225
+ readBefore(actorId, chatId, before, limit2) {
6226
+ if (before !== null && (!Number.isSafeInteger(before) || before < 1) || !Number.isSafeInteger(limit2) || limit2 < 1 || limit2 > 200)
5932
6227
  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 };
6228
+ const messages = this.repository.messagesBefore(actorId, chatId, before ?? Number.MAX_SAFE_INTEGER, limit2 + 1);
6229
+ return { messages: messages.slice(-limit2), hasOlder: messages.length > limit2 };
5935
6230
  }
5936
- read(actorId, chatId, after2 = 0, limit = 1e3) {
5937
- if (!Number.isSafeInteger(after2) || after2 < 0 || !Number.isSafeInteger(limit) || limit < 1 || limit > 1e3)
6231
+ read(actorId, chatId, after2 = 0, limit2 = 1e3) {
6232
+ if (!Number.isSafeInteger(after2) || after2 < 0 || !Number.isSafeInteger(limit2) || limit2 < 1 || limit2 > 1e3)
5938
6233
  throw new ChatError(400, "Invalid message cursor or limit");
5939
- return this.repository.messages(actorId, chatId, after2, limit);
6234
+ return this.repository.messages(actorId, chatId, after2, limit2);
5940
6235
  }
5941
6236
  instance(actorId, chatId) {
5942
6237
  this.repository.assertAccess(actorId, chatId);
@@ -5979,9 +6274,9 @@ var ChatService = class {
5979
6274
  message(actorId, id) {
5980
6275
  return this.repository.message(actorId, id);
5981
6276
  }
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);
6277
+ search(actorId, chatId, query, limit2 = 30, sender) {
6278
+ if (!query.trim() || query.length > 500 || !Number.isInteger(limit2) || limit2 < 1 || limit2 > 100) throw new ChatError(400, "Invalid search query or limit");
6279
+ return this.repository.search(actorId, chatId, query, limit2, sender);
5985
6280
  }
5986
6281
  notify(change) {
5987
6282
  try {
@@ -6052,8 +6347,8 @@ var ChatFiles = class {
6052
6347
  head(file, bytes) {
6053
6348
  return this.chats.attachments.head(file, bytes);
6054
6349
  }
6055
- read(file, limit) {
6056
- return this.chats.attachments.read(file, limit);
6350
+ read(file, limit2) {
6351
+ return this.chats.attachments.read(file, limit2);
6057
6352
  }
6058
6353
  };
6059
6354
  function listing(message) {
@@ -6108,7 +6403,7 @@ var Changes = class {
6108
6403
  };
6109
6404
 
6110
6405
  // src/server/service/bot/activity.ts
6111
- function activityPage(messages, history, limit) {
6406
+ function activityPage(messages, history, limit2) {
6112
6407
  const candidates2 = [
6113
6408
  ...messages.map((record3) => ({ kind: "chat", record: record3 })),
6114
6409
  ...history.map((record3) => ({ kind: "history", record: record3 }))
@@ -6118,9 +6413,9 @@ function activityPage(messages, history, limit) {
6118
6413
  if (a.kind !== b.kind) return a.kind === "history" ? -1 : 1;
6119
6414
  return a.record.id === b.record.id ? 0 : a.record.id > b.record.id ? -1 : 1;
6120
6415
  });
6121
- const items = candidates2.slice(0, limit), last = items.at(-1);
6416
+ const items = candidates2.slice(0, limit2), last = items.at(-1);
6122
6417
  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 };
6418
+ return { items, nextBefore: candidates2.length > limit2 ? position : null };
6124
6419
  }
6125
6420
 
6126
6421
  // src/server/service/bot/service.ts
@@ -6189,9 +6484,9 @@ var PiHistoryAdapter = class {
6189
6484
  if (m.role === "system") {
6190
6485
  if (m.sections) {
6191
6486
  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 });
6487
+ for (const [name3, text5] of Object.entries(m.sections)) {
6488
+ sections[name3] = text5 === null ? null : message.content.length;
6489
+ if (text5 !== null) message.content.push({ type: "text", text: text5 });
6195
6490
  }
6196
6491
  pi.sections = sections;
6197
6492
  }
@@ -6328,7 +6623,7 @@ function promptChange(before, after2) {
6328
6623
  break;
6329
6624
  }
6330
6625
  }
6331
- const cut = (text4) => text4.length > TEXT_LIMIT ? `${text4.slice(0, TEXT_LIMIT)}\u2026` : text4;
6626
+ const cut = (text5) => text5.length > TEXT_LIMIT ? `${text5.slice(0, TEXT_LIMIT)}\u2026` : text5;
6332
6627
  if (!tooLong) return { parts, diff: cut(lines.join("\n")), whole: false, next };
6333
6628
  return { parts, diff: cut(changed.map((part) => `@@ ${part.name}
6334
6629
  ${part.text.trim()}`).join("\n\n")), whole: true, next };
@@ -6456,7 +6751,7 @@ import { StringDecoder } from "node:string_decoder";
6456
6751
  var placeholder = (name3) => `{{secret:${name3}}}`;
6457
6752
  var MIN_MASKED = 4;
6458
6753
  var MIN_FRAGMENT = 8;
6459
- var xml = (text4) => text4.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll('"', "&quot;");
6754
+ var xml = (text5) => text5.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll('"', "&quot;");
6460
6755
  function base64Fragments(value2) {
6461
6756
  const bytes = Buffer.from(value2, "utf8"), found = [];
6462
6757
  for (const offset of [0, 1, 2]) {
@@ -6485,8 +6780,8 @@ var Masker = class {
6485
6780
  get empty() {
6486
6781
  return this.pairs.length === 0;
6487
6782
  }
6488
- mask(text4) {
6489
- let result = text4;
6783
+ mask(text5) {
6784
+ let result = text5;
6490
6785
  for (const { form, name: name3 } of this.pairs) if (result.includes(form)) result = result.split(form).join(placeholder(name3));
6491
6786
  return result;
6492
6787
  }
@@ -6568,9 +6863,9 @@ var u16le = (bytes, offset) => at(bytes, offset) + (at(bytes, offset + 1) << 8);
6568
6863
  var u32be = (bytes, offset) => at(bytes, offset) * 16777216 + (at(bytes, offset + 1) << 16) + (at(bytes, offset + 2) << 8) + at(bytes, offset + 3);
6569
6864
  var u32le = (bytes, offset) => at(bytes, offset) + (at(bytes, offset + 1) << 8) + (at(bytes, offset + 2) << 16) + at(bytes, offset + 3) * 16777216;
6570
6865
  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;
6866
+ function ascii(bytes, offset, text5) {
6867
+ if (bytes.length < offset + text5.length) return false;
6868
+ for (let index = 0; index < text5.length; index++) if (bytes[offset + index] !== text5.charCodeAt(index)) return false;
6574
6869
  return true;
6575
6870
  }
6576
6871
 
@@ -6745,7 +7040,7 @@ function relocated(definition, current) {
6745
7040
  try {
6746
7041
  const result = await execute(...args), path = result.details?.fullOutputPath;
6747
7042
  if (typeof path !== "string") return result;
6748
- const target3 = await moveOut(path, env), swap = (text4) => text4?.replaceAll(path, target3);
7043
+ const target3 = await moveOut(path, env), swap = (text5) => text5?.replaceAll(path, target3);
6749
7044
  return {
6750
7045
  ...result,
6751
7046
  content: result.content.map((part) => part.type === "text" ? { ...part, text: swap(part.text) } : part),
@@ -6882,10 +7177,10 @@ var PiSession = class _PiSession {
6882
7177
  let changed = false;
6883
7178
  const content2 = event.content.map((part) => {
6884
7179
  if (part.type !== "text") return part;
6885
- const text4 = masker.mask(part.text);
6886
- if (text4 === part.text) return part;
7180
+ const text5 = masker.mask(part.text);
7181
+ if (text5 === part.text) return part;
6887
7182
  changed = true;
6888
- return { ...part, text: text4 };
7183
+ return { ...part, text: text5 };
6889
7184
  });
6890
7185
  if (!changed) return void 0;
6891
7186
  return { content: content2, ...event.structuredContent !== void 0 ? { structuredContent: masker.maskDeep(event.structuredContent) } : {} };
@@ -7128,7 +7423,7 @@ function overDurable(id, kind, env, options) {
7128
7423
  cwd: exec.cwd,
7129
7424
  ...options.passEnv && exec.env ? { env: { ...exec.env, ...exec.secrets }, inheritEnv: false } : { ...exec.secrets ? { env: exec.secrets } : {}, inheritEnv: true },
7130
7425
  ...exec.timeout ? { timeout: exec.timeout } : {},
7131
- onOutput: (text4) => exec.onData(Buffer.from(text4))
7426
+ onOutput: (text5) => exec.onData(Buffer.from(text5))
7132
7427
  }, context(exec.signal));
7133
7428
  if (result.ok) return { exitCode: result.value.exitCode };
7134
7429
  if (result.error.code === "aborted") throw new Error("aborted");
@@ -7268,6 +7563,10 @@ async function runContainer(spec) {
7268
7563
  "--shm-size",
7269
7564
  "1g",
7270
7565
  ...Object.entries(spec.labels).flatMap(([key, value2]) => ["--label", `${key}=${value2}`]),
7566
+ ...spec.network ? ["--network", spec.network] : [],
7567
+ ...Object.entries(spec.hosts ?? {}).flatMap(([name3, address2]) => ["--add-host", `${name3}:${address2}`]),
7568
+ ...spec.limits?.memory ? memoryFlags(spec.limits.memory, spec.limits.memory) : [],
7569
+ ...spec.limits?.cpus ? ["--cpus", String(spec.limits.cpus)] : [],
7271
7570
  "-v",
7272
7571
  `${spec.workspace}:${COMPUTER_CWD}`,
7273
7572
  "-v",
@@ -7279,6 +7578,28 @@ async function runContainer(spec) {
7279
7578
  "infinity"
7280
7579
  ], 12e4);
7281
7580
  }
7581
+ var memoryFlags = (memory, withSwap) => ["--memory", String(memory), "--memory-swap", String(withSwap)];
7582
+ async function updateLimits(name3, change, machine) {
7583
+ const args = [
7584
+ ...change.memory === void 0 ? [] : change.memory ? memoryFlags(change.memory, change.memory) : memoryFlags(machine.memory, machine.memory * 2),
7585
+ ...change.cpus === void 0 ? [] : ["--cpus", String(change.cpus ?? machine.cpus)]
7586
+ ];
7587
+ if (args.length) await docker(["update", ...args, name3], 6e4);
7588
+ }
7589
+ async function dockerCapacity() {
7590
+ const [cpus, memory] = (await docker(["info", "--format", "{{.NCPU}} {{.MemTotal}}"], 15e3)).split(" ").map(Number);
7591
+ if (!cpus || !memory) throw Object.assign(new Error("docker info gave no CPUs or memory"), { code: "docker_failed" });
7592
+ return { cpus, memory };
7593
+ }
7594
+ async function containerUsage(name3) {
7595
+ const [cpu = "", memory = ""] = (await docker(["stats", "--no-stream", "--format", "{{.CPUPerc}} {{.MemUsage}}", name3], 3e4)).split(" ");
7596
+ return { cpu: parseFloat(cpu) || 0, memory: sizeBytes(memory.split("/")[0] ?? "") };
7597
+ }
7598
+ var UNITS = { b: 1, kib: 2 ** 10, mib: 2 ** 20, gib: 2 ** 30, tib: 2 ** 40, kb: 1e3, mb: 1e6, gb: 1e9, tb: 1e12 };
7599
+ function sizeBytes(text5) {
7600
+ const match = /^\s*([\d.]+)\s*([a-z]+)\s*$/i.exec(text5);
7601
+ return match ? Math.round(parseFloat(match[1]) * (UNITS[match[2].toLowerCase()] ?? 1)) : 0;
7602
+ }
7282
7603
  async function startContainer(name3) {
7283
7604
  await docker(["start", name3], 6e4);
7284
7605
  }
@@ -7308,248 +7629,137 @@ async function syncDaemon(dir) {
7308
7629
  await rename3(temp, target3);
7309
7630
  }
7310
7631
  function containerEnvironment(id, container, log) {
7311
- const connection = new Connection({ command: ["docker", "exec", "-i", container, `${DAEMON_DIR}/pi-env`], onLog: (text4) => log(text4.trim()) });
7632
+ const connection = new Connection({ command: ["docker", "exec", "-i", container, `${DAEMON_DIR}/pi-env`], onLog: (text5) => log(text5.trim()) });
7312
7633
  return overDurable(id, "computer", new RemoteExecutionEnv({ connection, id, cwd: COMPUTER_CWD }), { passEnv: false, close: () => connection.close() });
7313
7634
  }
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 };
7635
+ async function ensureNetwork(name3, labels) {
7636
+ const gateway2 = () => docker(["network", "inspect", "--format", "{{range .IPAM.Config}}{{.Gateway}} {{end}}", name3]).then((out) => out.split(" ").find(Boolean) ?? "");
7637
+ try {
7638
+ return await gateway2();
7639
+ } catch (error) {
7640
+ if (!/not found|no such network/i.test(error.message)) throw error;
7360
7641
  }
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;
7642
+ try {
7643
+ await docker([
7644
+ "network",
7645
+ "create",
7646
+ "--driver",
7647
+ "bridge",
7648
+ "--opt",
7649
+ "com.docker.network.bridge.enable_icc=false",
7650
+ ...Object.entries(labels).flatMap(([key, value2]) => ["--label", `${key}=${value2}`]),
7651
+ name3
7652
+ ]);
7653
+ } catch (error) {
7654
+ if (!/already exists/i.test(error.message)) throw error;
7371
7655
  }
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
- };
7656
+ return gateway2();
7657
+ }
7658
+ async function containerNetworks(label) {
7659
+ const out = await docker(["ps", "-a", "--filter", `label=${label}`, "--format", "{{.Names}} {{.Networks}}"]);
7660
+ return new Map(out.split("\n").filter(Boolean).map((line) => {
7661
+ const [name3, nets = ""] = line.split(" ");
7662
+ return [name3, nets.split(",").filter(Boolean)];
7663
+ }));
7664
+ }
7665
+ async function networkAddresses(network) {
7666
+ const out = await docker(["network", "inspect", "--format", "{{range .Containers}}{{.Name}}={{.IPv4Address}} {{end}}", network]);
7667
+ return new Map(out.split(" ").filter(Boolean).map((entry) => {
7668
+ const [name3, address2 = ""] = entry.split("=");
7669
+ return [name3, address2.split("/")[0]];
7670
+ }));
7671
+ }
7672
+ async function moveToNetwork(container, network) {
7673
+ await docker(["network", "connect", network, container]).catch((error) => {
7674
+ if (!/already exists|already attached/i.test(error.message)) throw error;
7675
+ });
7676
+ await docker(["network", "disconnect", "bridge", container]).catch((error) => {
7677
+ if (!/not connected/i.test(error.message)) throw error;
7678
+ });
7679
+ }
7680
+ async function addHost(container, name3, address2) {
7681
+ await docker(["exec", "-u", "root", container, "sh", "-c", `grep -q '[[:space:]]${name3}$' /etc/hosts || printf '%s\\t%s\\n' '${address2}' '${name3}' >> /etc/hosts`], 3e4);
7682
+ }
7683
+ async function listeningPorts(container) {
7684
+ const out = await docker(["exec", container, "sh", "-c", "cat /proc/net/tcp /proc/net/tcp6 2>/dev/null"], 3e4);
7685
+ const ports = /* @__PURE__ */ new Map();
7686
+ for (const line of out.split("\n")) {
7687
+ const fields = line.trim().split(/\s+/);
7688
+ if (fields[3] !== "0A") continue;
7689
+ const [address2, hex] = (fields[1] ?? "").split(":");
7690
+ if (!address2 || !hex) continue;
7691
+ const port = parseInt(hex, 16), loopback = loopbackAddress(address2);
7692
+ const known = ports.get(port);
7693
+ ports.set(port, { loopbackOnly: (known?.loopbackOnly ?? true) && loopback });
7382
7694
  }
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()];
7695
+ return ports;
7696
+ }
7697
+ function loopbackAddress(hex) {
7698
+ if (hex.length === 8) return hex.slice(6, 8) === "7F";
7699
+ if (hex === "00000000000000000000000001000000") return true;
7700
+ return hex.startsWith("0000000000000000FFFF0000") && hex.slice(30, 32) === "7F";
7701
+ }
7702
+
7703
+ // src/server/service/bot/computers.ts
7704
+ import { createHash as createHash7 } from "node:crypto";
7705
+ import { dirname as dirname5, join as join10 } from "node:path";
7706
+ var SWEEP_MS = 6e4;
7707
+ var HOST_NAME = "host.docker.internal";
7708
+ var MIN_MEMORY = 256 * 2 ** 20;
7709
+ var MIN_CPUS = 0.1;
7710
+ function computerLimits(record3) {
7711
+ const read3 = (value2) => value2 && Number(value2) > 0 ? Number(value2) : null;
7712
+ return { memory: read3(record3.spec.memory), cpus: read3(record3.spec.cpus) };
7713
+ }
7714
+ var Computers = class {
7715
+ constructor(deps) {
7716
+ this.deps = deps;
7717
+ this.dataDir = dirname5(deps.store.path);
7718
+ this.tag = createHash7("sha256").update(this.dataDir).digest("hex").slice(0, 12);
7388
7719
  }
7389
- place(botId, record3, workspacePath) {
7390
- return KINDS[record3.kind].place(record3, botId, workspacePath);
7720
+ docker = null;
7721
+ probing = null;
7722
+ image = null;
7723
+ timer = null;
7724
+ states = /* @__PURE__ */ new Map();
7725
+ preparing = /* @__PURE__ */ new Map();
7726
+ connections = /* @__PURE__ */ new Map();
7727
+ /** Containers set up since they last started: their home checked (`homeAtWorkspace`), and the host's name added. */
7728
+ homes = /* @__PURE__ */ new Map();
7729
+ /** This Jarvy's network and this machine's address on it; null on Docker's default bridge, if it couldn't be made. */
7730
+ network = null;
7731
+ /** Running containers' addresses, by name, as last read. */
7732
+ book = null;
7733
+ /** This machine's CPUs and memory, asked of Docker once. */
7734
+ machine = null;
7735
+ dataDir;
7736
+ /** Tells this Jarvy's containers from those of other data directories on the same Docker. */
7737
+ tag;
7738
+ uid = process.getuid?.() ?? 1e3;
7739
+ gid = process.getgid?.() ?? 1e3;
7740
+ /** Looks for Docker and learns the state of every computer, once; every computer waits for it. */
7741
+ start() {
7742
+ this.probing ??= this.probe();
7743
+ return this.probing;
7391
7744
  }
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) };
7745
+ async probe() {
7746
+ this.docker = await dockerAvailable();
7747
+ if (!this.docker) return;
7748
+ try {
7749
+ await syncDaemon(join10(this.dataDir, "computers", "bin"));
7750
+ } catch (error) {
7751
+ this.docker = false;
7752
+ this.log(`computers disabled: ${error.message}`);
7753
+ return;
7754
+ }
7755
+ await this.joinNetwork().catch((error) => this.log(`sandbox network unavailable, staying on the default bridge: ${error.message}`));
7756
+ await this.sweep().catch((error) => this.log(`computer sweep failed: ${error.message}`));
7757
+ this.timer = setInterval(() => void this.sweep().catch((error) => this.log(`computer sweep failed: ${error.message}`)), SWEEP_MS);
7758
+ this.timer.unref();
7397
7759
  }
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);
7516
- }
7517
- docker = null;
7518
- probing = null;
7519
- image = null;
7520
- timer = null;
7521
- states = /* @__PURE__ */ new Map();
7522
- preparing = /* @__PURE__ */ new Map();
7523
- connections = /* @__PURE__ */ new Map();
7524
- /** Containers whose home was checked since Jarvy started (`homeAtWorkspace`). */
7525
- homes = /* @__PURE__ */ new Map();
7526
- dataDir;
7527
- /** Tells this Jarvy's containers from those of other data directories on the same Docker. */
7528
- tag;
7529
- uid = process.getuid?.() ?? 1e3;
7530
- gid = process.getgid?.() ?? 1e3;
7531
- /** Looks for Docker and learns the state of every computer, once; every computer waits for it. */
7532
- start() {
7533
- this.probing ??= this.probe();
7534
- return this.probing;
7535
- }
7536
- async probe() {
7537
- this.docker = await dockerAvailable();
7538
- if (!this.docker) return;
7539
- try {
7540
- await syncDaemon(join10(this.dataDir, "computers", "bin"));
7541
- } catch (error) {
7542
- this.docker = false;
7543
- this.log(`computers disabled: ${error.message}`);
7544
- return;
7545
- }
7546
- await this.sweep().catch((error) => this.log(`computer sweep failed: ${error.message}`));
7547
- this.timer = setInterval(() => void this.sweep().catch((error) => this.log(`computer sweep failed: ${error.message}`)), SWEEP_MS);
7548
- this.timer.unref();
7549
- }
7550
- /** Whether new computers can be made here; false until `start` found Docker. */
7551
- get available() {
7552
- return this.docker === true;
7760
+ /** Whether new computers can be made here; false until `start` found Docker. */
7761
+ get available() {
7762
+ return this.docker === true;
7553
7763
  }
7554
7764
  status(botId) {
7555
7765
  if (!this.deps.store.environments.computerOf(botId)) return null;
@@ -7560,7 +7770,42 @@ var Computers = class {
7560
7770
  info(botId) {
7561
7771
  const computer = this.deps.store.environments.computerOf(botId), status = this.status(botId);
7562
7772
  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 };
7773
+ return {
7774
+ ...status,
7775
+ container: computer.spec.container,
7776
+ image: computerImage(this.uid, this.gid),
7777
+ workspace: this.workspace(botId),
7778
+ mount: COMPUTER_CWD,
7779
+ limits: computerLimits(computer)
7780
+ };
7781
+ }
7782
+ /** What the sandbox uses now, when it runs, and what this machine has; null for what Docker doesn't answer. */
7783
+ async resources(botId) {
7784
+ const computer = this.need(botId);
7785
+ const usage = this.status(botId)?.state === "running" ? await containerUsage(computer.spec.container).catch(() => null) : null;
7786
+ return { usage, capacity: await this.capacity().catch(() => null) };
7787
+ }
7788
+ /** Sets what the sandbox may use at most (A75): a container that exists changes at once, running or not, and one made
7789
+ * later, after a reset too, starts with them. Each is null for no limit, or from a floor up to this machine's size. */
7790
+ async setLimits(botId, input) {
7791
+ const computer = this.need(botId), machine = await this.capacity();
7792
+ const memory = limit(input.memory, MIN_MEMORY, machine.memory, 2 ** 20), cpus = limit(input.cpus, MIN_CPUS, machine.cpus, 0.01);
7793
+ if (memory === void 0 || cpus === void 0) throw new ValidationError(
7794
+ "Limits are null or a number within this machine's size",
7795
+ "limits_invalid",
7796
+ { memory: Math.floor(machine.memory / 2 ** 30 * 10) / 10, cpus: machine.cpus }
7797
+ );
7798
+ const before = computerLimits(computer), change = { ...memory !== before.memory ? { memory } : {}, ...cpus !== before.cpus ? { cpus } : {} };
7799
+ if (Object.keys(change).length && await containerState(computer.spec.container) !== "missing") await updateLimits(computer.spec.container, change, machine);
7800
+ this.deps.store.environments.setLimits(computer.id, { memory, cpus });
7801
+ this.deps.changed();
7802
+ }
7803
+ capacity() {
7804
+ this.machine ??= dockerCapacity().catch((error) => {
7805
+ this.machine = null;
7806
+ throw error;
7807
+ });
7808
+ return this.machine;
7564
7809
  }
7565
7810
  /** Every Bot has its computer (A68): one restored or made without one, such as the agent Bot on a new installation,
7566
7811
  * gets it now. */
@@ -7609,17 +7854,67 @@ var Computers = class {
7609
7854
  const container = await this.ensure(botId);
7610
7855
  let home = this.homes.get(container);
7611
7856
  if (!home) {
7612
- home = homeAtWorkspace(container).catch((error) => this.log(`home of ${botId} not moved: ${error.message}`));
7857
+ home = this.prepare(container).catch((error) => this.log(`sandbox of ${botId} not set up: ${error.message}`));
7613
7858
  this.homes.set(container, home);
7614
7859
  }
7615
7860
  await home;
7616
7861
  let env = this.connections.get(botId);
7617
7862
  if (!env) {
7618
- env = containerEnvironment(record3.id, container, (text4) => this.log(`[${botId.slice(0, 8)}] ${text4}`));
7863
+ env = containerEnvironment(record3.id, container, (text5) => this.log(`[${botId.slice(0, 8)}] ${text5}`));
7619
7864
  this.connections.set(botId, env);
7620
7865
  }
7621
7866
  return env;
7622
7867
  }
7868
+ /** Sets up a container after it starts: the workspace as its home, and this machine's name for one made before
7869
+ * containers got it at creation. */
7870
+ async prepare(container) {
7871
+ await homeAtWorkspace(container);
7872
+ if (this.network) await addHost(container, HOST_NAME, this.network.gateway);
7873
+ }
7874
+ /** Makes this Jarvy's network and moves the containers still on Docker's default bridge there, running or not. */
7875
+ async joinNetwork() {
7876
+ const name3 = `jarvy-${this.tag.slice(0, 6)}`;
7877
+ this.network = { name: name3, gateway: await ensureNetwork(name3, { "jarvy.data": this.tag }) };
7878
+ const owned = new Set(this.deps.store.environments.computers().map((computer) => computer.spec.container));
7879
+ for (const [container, networks] of await containerNetworks(`jarvy.data=${this.tag}`)) {
7880
+ if (!owned.has(container) || networks.includes(name3)) continue;
7881
+ await moveToNetwork(container, name3).then(() => {
7882
+ this.homes.delete(container);
7883
+ this.log(`moved ${container} to ${name3}`);
7884
+ }).catch((error) => this.log(`moving ${container} to ${name3} failed: ${error.message}`));
7885
+ }
7886
+ }
7887
+ /** The address a Bot's running sandbox has on the network, or null. */
7888
+ async address(botId) {
7889
+ const container = this.deps.store.environments.computerOf(botId)?.spec.container;
7890
+ if (!container || !this.available) return null;
7891
+ return (await this.addresses()).get(container) ?? (await this.addresses(true)).get(container) ?? null;
7892
+ }
7893
+ /** The Bot whose running sandbox has this address, if one of this Jarvy's has it. */
7894
+ async botAt(address2) {
7895
+ if (!this.available) return null;
7896
+ const plain2 = address2.replace(/^::ffff:/, "");
7897
+ const find = (book) => [...book].find(([, value2]) => value2 === plain2)?.[0];
7898
+ const container = find(await this.addresses()) ?? find(await this.addresses(true));
7899
+ return container ? this.deps.store.environments.computers().find((computer) => computer.spec.container === container)?.botId ?? null : null;
7900
+ }
7901
+ /** The ports something listens on in a Bot's running sandbox; null when it isn't running. */
7902
+ async listening(botId) {
7903
+ const container = this.deps.store.environments.computerOf(botId)?.spec.container;
7904
+ if (!container || this.status(botId)?.state !== "running") return null;
7905
+ return listeningPorts(container);
7906
+ }
7907
+ /** How a sandbox names this machine. */
7908
+ get hostName() {
7909
+ return HOST_NAME;
7910
+ }
7911
+ /** Read again when asked fresh, at most once a second, so callers that miss don't each ask Docker. */
7912
+ async addresses(fresh = false) {
7913
+ const age = this.book ? Date.now() - this.book.at : Infinity;
7914
+ if (this.book && (fresh ? age < 1e3 : age < 5e3)) return this.book.byName;
7915
+ this.book = { at: Date.now(), byName: await networkAddresses(this.network?.name ?? "bridge").catch(() => /* @__PURE__ */ new Map()) };
7916
+ return this.book.byName;
7917
+ }
7623
7918
  /** Makes sure the container exists and runs, building the image first when needed; returns its name. */
7624
7919
  ensure(botId) {
7625
7920
  const pending = this.preparing.get(botId);
@@ -7642,9 +7937,15 @@ var Computers = class {
7642
7937
  daemonDir: join10(this.dataDir, "computers", "bin"),
7643
7938
  uid: this.uid,
7644
7939
  gid: this.gid,
7645
- labels: { "jarvy.data": this.tag, "jarvy.bot": botId }
7940
+ labels: { "jarvy.data": this.tag, "jarvy.bot": botId },
7941
+ limits: computerLimits(computer),
7942
+ ...this.network ? { network: this.network.name, hosts: { [HOST_NAME]: this.network.gateway } } : {}
7646
7943
  });
7647
7944
  else if (state === "stopped") await startContainer(container);
7945
+ if (state !== "running") {
7946
+ this.homes.delete(container);
7947
+ this.book = null;
7948
+ }
7648
7949
  this.set(botId, { state: "running" });
7649
7950
  return container;
7650
7951
  } catch (error) {
@@ -7743,11 +8044,221 @@ var Computers = class {
7743
8044
  await this.disconnect(botId);
7744
8045
  continue;
7745
8046
  }
8047
+ if (this.states.get(botId)?.state !== "running") this.homes.delete(computer.spec.container);
7746
8048
  this.set(botId, { state: "running" });
7747
8049
  }
7748
8050
  }
7749
- log(text4) {
7750
- (this.deps.log ?? console.log)(`[computers] ${text4}`);
8051
+ log(text5) {
8052
+ (this.deps.log ?? console.log)(`[computers] ${text5}`);
8053
+ }
8054
+ };
8055
+ function limit(value2, min, max, step) {
8056
+ if (value2 === null) return null;
8057
+ if (typeof value2 !== "number" || !Number.isFinite(value2)) return void 0;
8058
+ const rounded = Math.round(value2 / step) * step;
8059
+ return rounded >= min && rounded <= max ? Number(rounded.toFixed(2)) : void 0;
8060
+ }
8061
+
8062
+ // src/server/service/bot/kinds/sandbox.ts
8063
+ function limits(record3) {
8064
+ const { memory, cpus } = computerLimits(record3);
8065
+ const parts = [memory ? `${Number((memory / 2 ** 30).toFixed(2))} GB of memory` : "", cpus ? `${cpus} ${cpus === 1 ? "CPU" : "CPUs"}` : ""].filter(Boolean);
8066
+ return parts.length ? parts.join(" and ") : null;
8067
+ }
8068
+ var sandbox = {
8069
+ kind: "computer",
8070
+ home: true,
8071
+ grantable: false,
8072
+ open: (record3, botId, _workspacePath, deps) => deps.computers.environment(record3.botId ?? botId, record3),
8073
+ place: (record3, botId) => ({
8074
+ id: record3.id,
8075
+ kind: "sandbox",
8076
+ name: record3.botId === botId ? "your sandbox" : "another Bot's sandbox",
8077
+ directory: COMPUTER_CWD
8078
+ }),
8079
+ ensure: async (record3, botId, deps) => {
8080
+ if (deps.computers.available) await deps.computers.ensure(record3.botId ?? botId);
8081
+ },
8082
+ connects: false,
8083
+ summary: (record3, botId) => record3.botId === botId ? "own sandbox" : "another Bot's sandbox",
8084
+ describe: (record3, botId, deps) => {
8085
+ const most = limits(record3);
8086
+ return { state: deps.computers.status(record3.botId ?? botId)?.state ?? "stopped", ...most ? { limits: `at most ${most}` } : {} };
8087
+ },
8088
+ about: (record3, _botId, deps) => {
8089
+ const most = limits(record3);
8090
+ 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.");
8091
+ },
8092
+ signature: (record3) => record3.id
8093
+ };
8094
+
8095
+ // src/server/service/bot/kinds/ssh.ts
8096
+ var ssh = {
8097
+ kind: "ssh",
8098
+ home: false,
8099
+ grantable: true,
8100
+ open: (record3, botId, _workspacePath, deps) => deps.machines.environment(record3, botId),
8101
+ place: (record3) => ({ id: record3.id, kind: "ssh", name: record3.name, directory: record3.directory ?? record3.spec.home ?? "/" }),
8102
+ connects: true,
8103
+ summary: (record3) => record3.name,
8104
+ describe: (record3, _botId, deps) => ({ address: deps.machines.address(record3), ...record3.description ? { description: record3.description } : {} }),
8105
+ about: (record3, botId, deps) => deps.machines.about(record3, botId),
8106
+ signature: (record3) => `${record3.id}@${record3.directory ?? ""}`
8107
+ };
8108
+
8109
+ // src/server/service/bot/kinds/index.ts
8110
+ var KINDS = { computer: sandbox, ssh };
8111
+
8112
+ // src/server/service/bot/environments.ts
8113
+ var BotEnvironments = class {
8114
+ constructor(store, computers, machines) {
8115
+ this.store = store;
8116
+ this.computers = computers;
8117
+ this.kinds = { computers, machines };
8118
+ }
8119
+ open = /* @__PURE__ */ new Map();
8120
+ kinds;
8121
+ current(botId, workspacePath) {
8122
+ const target3 = this.store.environments.placement(botId).current, signature = this.signature(target3);
8123
+ const open2 = this.open.get(botId);
8124
+ if (open2?.id === signature) return open2.view;
8125
+ if (open2) this.retire(open2);
8126
+ const opened = this.track(signature, KINDS[target3.kind].open(target3, botId, workspacePath, this.kinds));
8127
+ this.open.set(botId, opened);
8128
+ return opened.view;
8129
+ }
8130
+ placement(botId) {
8131
+ const environments = this.store.environments, { current } = environments.placement(botId);
8132
+ const granted = new Set(environments.grants(botId).map((grant) => grant.id));
8133
+ return {
8134
+ current: current.kind,
8135
+ ...current.kind === "ssh" ? { currentName: current.name } : {},
8136
+ computer: this.computers.info(botId),
8137
+ docker: this.computers.available,
8138
+ machines: environments.sshMachines().map((machine) => ({ id: machine.id, name: machine.name, granted: granted.has(machine.id) }))
8139
+ };
8140
+ }
8141
+ /** The environments the Bot may be in: its sandbox, its default, and the ones its owner granted it. */
8142
+ usable(botId) {
8143
+ const environments = this.store.environments;
8144
+ const all = [environments.computerOf(botId), environments.placement(botId).home, ...environments.grants(botId)];
8145
+ return [...new Map(all.filter((record3) => !!record3).map((record3) => [record3.id, record3])).values()];
8146
+ }
8147
+ place(botId, record3, workspacePath) {
8148
+ return KINDS[record3.kind].place(record3, botId, workspacePath);
8149
+ }
8150
+ /** A Bot's environments as other Bots see them in bots.list. */
8151
+ summary(botId) {
8152
+ const name3 = (record3) => KINDS[record3.kind].summary(record3, botId);
8153
+ const { home, current } = this.store.environments.placement(botId);
8154
+ return { default: name3(home), current: name3(current), usable: this.usable(botId).map(name3) };
8155
+ }
8156
+ /** The Bot's default environment (A68): its own sandbox. */
8157
+ default(botId, workspacePath) {
8158
+ return this.place(botId, this.store.environments.placement(botId).home, workspacePath);
8159
+ }
8160
+ /** Where the Bot works now, for the end of each batch. */
8161
+ here(botId, workspacePath) {
8162
+ return this.place(botId, this.store.environments.placement(botId).current, workspacePath);
8163
+ }
8164
+ /** environments.list: where the Bot is and where else it may go. */
8165
+ list(botId, workspacePath) {
8166
+ const { home, current } = this.store.environments.placement(botId);
8167
+ return { current: current.id, environments: this.usable(botId).map((record3) => ({
8168
+ ...this.place(botId, record3, workspacePath),
8169
+ ...record3.id === home.id ? { default: true } : {},
8170
+ ...KINDS[record3.kind].describe(record3, botId, this.kinds)
8171
+ })) };
8172
+ }
8173
+ /** environments.switch: the Bot moves itself, like cd, until it switches back. A sandbox is started first, an SSH
8174
+ * machine connected first; if that fails, the Bot stays where it was. */
8175
+ async switch(botId, environmentId, workspacePath) {
8176
+ const target3 = this.usable(botId).find((record3) => record3.id === environmentId);
8177
+ if (!target3) throw new ChatError(404, "No work environment with this id that you may use; see environments.list");
8178
+ const previous = this.store.environments.placement(botId).current, already = previous.id === target3.id, entry = KINDS[target3.kind];
8179
+ await entry.ensure?.(target3, botId, this.kinds);
8180
+ this.store.environments.setCurrent(botId, target3.id);
8181
+ if (entry.connects && !already) {
8182
+ const place = this.place(botId, target3, workspacePath);
8183
+ try {
8184
+ 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`);
8185
+ } catch (error) {
8186
+ const opened = this.open.get(botId);
8187
+ if (opened) {
8188
+ this.open.delete(botId);
8189
+ this.retire(opened);
8190
+ }
8191
+ this.store.environments.setCurrent(botId, previous.id);
8192
+ throw new ChatError(409, `Couldn't connect to ${target3.name}, so you stay where you were: ${error instanceof Error ? error.message : String(error)}`);
8193
+ }
8194
+ }
8195
+ return {
8196
+ ...this.place(botId, target3, workspacePath),
8197
+ about: entry.about(target3, botId, this.kinds),
8198
+ ...already ? { note: "You were already here." } : {}
8199
+ };
8200
+ }
8201
+ /** The owner grants or takes back an SSH machine (A57); taking it back while the Bot works there sends it home. */
8202
+ grant(botId, environmentId, granted, by, workspacePath) {
8203
+ const environments = this.store.environments, target3 = environments.get(environmentId);
8204
+ if (!target3 || !KINDS[target3.kind].grantable) throw new ChatError(404, "No such environment");
8205
+ const name3 = this.place(botId, target3, workspacePath).name;
8206
+ if (granted) return { changed: environments.grant(target3.id, botId, by), moved: null, name: name3 };
8207
+ const changed = environments.revoke(target3.id, botId), { home, current } = environments.placement(botId);
8208
+ if (current.id !== target3.id || home.id === target3.id) return { changed, moved: null, name: name3 };
8209
+ const from = this.here(botId, workspacePath);
8210
+ environments.setCurrent(botId, home.id);
8211
+ return { changed, moved: { from, to: this.here(botId, workspacePath) }, name: name3 };
8212
+ }
8213
+ async release(botId) {
8214
+ const open2 = this.open.get(botId);
8215
+ this.open.delete(botId);
8216
+ await open2?.env.close();
8217
+ await this.computers.remove(botId);
8218
+ }
8219
+ async close() {
8220
+ const all = [...this.open.values()];
8221
+ this.open.clear();
8222
+ await Promise.all(all.map((open2) => open2.env.close()));
8223
+ await this.computers.close();
8224
+ }
8225
+ /** What an open environment is for: changing a machine's directory reopens it where the owner put it. */
8226
+ signature(record3) {
8227
+ return KINDS[record3.kind].signature(record3);
8228
+ }
8229
+ retire(open2) {
8230
+ open2.retired = true;
8231
+ if (!open2.busy) void open2.env.close();
8232
+ }
8233
+ /** Counts the operations running on an environment, so one the Bot has left closes only when they are done. */
8234
+ track(id, env) {
8235
+ const opened = { id, env, view: env, busy: 0, retired: false };
8236
+ const run2 = async (work) => {
8237
+ opened.busy++;
8238
+ try {
8239
+ return await work();
8240
+ } finally {
8241
+ if (--opened.busy === 0 && opened.retired) void env.close();
8242
+ }
8243
+ };
8244
+ opened.view = {
8245
+ id: env.id,
8246
+ kind: env.kind,
8247
+ local: env.local,
8248
+ cwd: env.cwd,
8249
+ ...env.temp ? { temp: env.temp } : {},
8250
+ exec: (command, options) => run2(() => env.exec(command, options)),
8251
+ readFile: (path) => run2(() => env.readFile(path)),
8252
+ head: (path, bytes) => run2(() => env.head(path, bytes)),
8253
+ readRange: (path, offset, length) => run2(() => env.readRange(path, offset, length)),
8254
+ writeFile: (path, content2) => run2(() => env.writeFile(path, content2)),
8255
+ appendFile: (path, content2) => run2(() => env.appendFile(path, content2)),
8256
+ mkdir: (path) => run2(() => env.mkdir(path)),
8257
+ exists: (path) => run2(() => env.exists(path)),
8258
+ fileSize: (path) => run2(() => env.fileSize(path)),
8259
+ close: () => env.close()
8260
+ };
8261
+ return opened;
7751
8262
  }
7752
8263
  };
7753
8264
 
@@ -7931,23 +8442,23 @@ function run(program, args, stdin = "", env) {
7931
8442
  }
7932
8443
  function classify(error, address2, how = "key") {
7933
8444
  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))
8445
+ const text5 = error instanceof Error ? error.message : String(error), where2 = `${address2.user}@${address2.host}:${address2.port}`;
8446
+ if (error instanceof HostKeyChangedError || /REMOTE HOST IDENTIFICATION HAS CHANGED|Host key verification failed/.test(text5))
7936
8447
  return new SshFault("ssh_host_key_changed", `The host key of ${where2} is not the one accepted`);
7937
8448
  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)) {
8449
+ const denied = /Permission denied \(([^)]*)\)/.exec(text5);
8450
+ if (denied || /Permission denied|Too many authentication failures/.test(text5)) {
7940
8451
  const methods = denied ? denied[1].split(",").map((method) => method.trim()) : [];
7941
8452
  if (how === "password" && methods.length && !methods.some((method) => method === "password" || method === "keyboard-interactive"))
7942
8453
  return new SshFault("ssh_password_refused", `${where2} doesn't accept password logins`);
7943
8454
  if (how === "key") return new SshFault("ssh_key_refused", `${where2} didn't accept Jarvy's key`);
7944
8455
  return new SshFault("ssh_auth_failed", `${where2} refused the password`);
7945
8456
  }
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`);
8457
+ if (/Connection refused|timed out|No route to host|Could not resolve|Network is unreachable|Connection closed|No host key received/i.test(text5))
8458
+ return new SshFault("ssh_unreachable", `${where2} can't be reached: ${text5.split("\n").pop()}`);
8459
+ return new SshFault("ssh_failed", text5.split("\n").slice(-3).join(" ").slice(0, 300) || `ssh ${where2} failed`);
7949
8460
  }
7950
- var quote = (text4) => `'${text4.replaceAll("'", "'\\''")}'`;
8461
+ var quote = (text5) => `'${text5.replaceAll("'", "'\\''")}'`;
7951
8462
 
7952
8463
  // src/server/service/bot/machines.ts
7953
8464
  var HOST = /^(?!-)[A-Za-z0-9._:-]{1,253}$/;
@@ -8097,7 +8608,7 @@ var SshMachines = class {
8097
8608
  this.access(record3),
8098
8609
  record3.directory ?? home,
8099
8610
  posix.join(home, ".cache", "jarvy", botId),
8100
- (text4) => console.error(`[ssh ${record3.name}] ${text4.trim()}`)
8611
+ (text5) => console.error(`[ssh ${record3.name}] ${text5.trim()}`)
8101
8612
  );
8102
8613
  }
8103
8614
  /** What a Bot is told of a machine. */
@@ -8269,29 +8780,29 @@ var HistoryRepository = class {
8269
8780
  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
8781
  return Number(result.lastInsertRowid);
8271
8782
  }
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 };
8783
+ page(before, limit2) {
8784
+ 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);
8785
+ const items = rows3.slice(0, limit2).map((row2) => this.decodeSummary(row2));
8786
+ return { items, nextBefore: rows3.length > limit2 ? items.at(-1).id : null };
8276
8787
  }
8277
- timeline(before, limit) {
8788
+ timeline(before, limit2) {
8278
8789
  const condition = !before ? "" : before.kind === "history" ? "WHERE timestamp<? OR (timestamp=? AND id<?)" : "WHERE timestamp<?";
8279
8790
  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));
8791
+ const rows3 = this.db.prepare(`${SUMMARY} ${condition} ORDER BY timestamp DESC,id DESC LIMIT ?`).all(...cursor2, limit2);
8792
+ return rows3.map((row2) => this.decodeSummary(row2));
8282
8793
  }
8283
8794
  summaries(ids) {
8284
8795
  const query = this.db.prepare(`${SUMMARY} WHERE id=?`);
8285
8796
  return ids.map((id) => {
8286
- const row = query.get(id);
8287
- return row ? this.decodeSummary(row) : null;
8797
+ const row2 = query.get(id);
8798
+ return row2 ? this.decodeSummary(row2) : null;
8288
8799
  });
8289
8800
  }
8290
8801
  details(ids) {
8291
8802
  const query = this.db.prepare("SELECT id,timestamp,role,model,message FROM history WHERE id=?");
8292
8803
  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;
8804
+ const row2 = query.get(id);
8805
+ return row2 ? { ...row2, model: row2.model === null ? null : JSON.parse(row2.model), message: JSON.parse(row2.message) } : null;
8295
8806
  });
8296
8807
  }
8297
8808
  /** Model calls since a time, per provider and model: every assistant record with usage is one call. */
@@ -8302,15 +8813,15 @@ var HistoryRepository = class {
8302
8813
  COALESCE(SUM(json_extract(usage,'$.cost.total')),0) AS cost,COALESCE(MAX(json_extract(usage,'$.cost.total')),0)>0 AS priced
8303
8814
  FROM history WHERE role='assistant' AND usage IS NOT NULL AND model IS NOT NULL AND timestamp>=?
8304
8815
  GROUP BY 1,2 ORDER BY cost DESC,calls DESC`).all(since);
8305
- return rows3.map(({ priced, cost, ...row }) => ({ ...row, cost: priced ? cost : null }));
8816
+ return rows3.map(({ priced, cost, ...row2 }) => ({ ...row2, cost: priced ? cost : null }));
8306
8817
  }
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 };
8818
+ decodeSummary(row2) {
8819
+ const tools = JSON.parse(row2.tools).map(({ kind, name: name3, error }) => error ? { kind, name: name3, error: true } : { kind, name: name3 });
8820
+ return { ...row2, model: row2.model === null ? null : JSON.parse(row2.model), preview: row2.preview.replace(/\s+/g, " ").trim(), tools };
8310
8821
  }
8311
8822
  read(id) {
8312
- const row = this.db.prepare("SELECT * FROM history WHERE id=?").get(id);
8313
- return row ? this.decode(row) : null;
8823
+ const row2 = this.db.prepare("SELECT * FROM history WHERE id=?").get(id);
8824
+ return row2 ? this.decode(row2) : null;
8314
8825
  }
8315
8826
  links(record3) {
8316
8827
  const links = [];
@@ -8328,8 +8839,8 @@ var HistoryRepository = class {
8328
8839
  }
8329
8840
  return links;
8330
8841
  }
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) };
8842
+ decode(row2) {
8843
+ 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
8844
  }
8334
8845
  close() {
8335
8846
  this.db.close();
@@ -8356,11 +8867,11 @@ var Memory = class {
8356
8867
  append(message) {
8357
8868
  return this.history().append(message);
8358
8869
  }
8359
- list(before, limit = 50) {
8360
- return this.history().page(before, limit);
8870
+ list(before, limit2 = 50) {
8871
+ return this.history().page(before, limit2);
8361
8872
  }
8362
- timeline(before, limit) {
8363
- return this.history().timeline(before, limit);
8873
+ timeline(before, limit2) {
8874
+ return this.history().timeline(before, limit2);
8364
8875
  }
8365
8876
  usage(since) {
8366
8877
  return this.history().usage(since);
@@ -8739,7 +9250,7 @@ var BotState = class {
8739
9250
  };
8740
9251
 
8741
9252
  // src/server/service/bot/tools.ts
8742
- import { Type as Type11 } from "typebox";
9253
+ import { Type as Type12 } from "typebox";
8743
9254
  import { Value as Value2 } from "typebox/value";
8744
9255
 
8745
9256
  // src/server/service/bot/tool-errors.ts
@@ -8848,12 +9359,12 @@ import { randomUUID as randomUUID18 } from "node:crypto";
8848
9359
  import { Type as Type2 } from "typebox";
8849
9360
 
8850
9361
  // src/server/service/bot/history-format.ts
8851
- function fence(text4, language = "text") {
9362
+ function fence(text5, language = "text") {
8852
9363
  let length = 3;
8853
- for (const match of text4.matchAll(/`+/g)) length = Math.max(length, match[0].length + 1);
9364
+ for (const match of text5.matchAll(/`+/g)) length = Math.max(length, match[0].length + 1);
8854
9365
  const marker = "`".repeat(length);
8855
9366
  return `${marker}${language}
8856
- ${text4}
9367
+ ${text5}
8857
9368
  ${marker}`;
8858
9369
  }
8859
9370
  var jsonBlock = (value2) => fence(JSON.stringify(value2, null, 2), "json");
@@ -8955,7 +9466,7 @@ var done = (chat) => chat.type === "thread" && chat.status === "done";
8955
9466
  var name2 = (chats) => (memberId) => chats.member(memberId)?.name ?? memberId;
8956
9467
  function chatRows(chats, botId) {
8957
9468
  const sections = new Map(chats.channels.sections(botId).map((section) => [section.id, section.name]));
8958
- const row = (chat) => ({
9469
+ const row2 = (chat) => ({
8959
9470
  id: chat.id,
8960
9471
  type: chat.type,
8961
9472
  title: chat.title,
@@ -8970,7 +9481,7 @@ function chatRows(chats, botId) {
8970
9481
  ...chat.note ? { noticeVersion: chat.note.version } : {},
8971
9482
  updatedAt: iso(chat.updatedAt)
8972
9483
  });
8973
- return [...chats.list(botId).map(row), ...chats.threads.followedChats(botId).map(row)];
9484
+ return [...chats.list(botId).map(row2), ...chats.threads.followedChats(botId).map(row2)];
8974
9485
  }
8975
9486
  function chatDetail(chats, botId, chat) {
8976
9487
  const section = chat.sectionId ? chats.channels.sections(botId).find((item) => item.id === chat.sectionId)?.name : void 0;
@@ -9254,10 +9765,10 @@ async function rows2(context2, messages, signal) {
9254
9765
  signal?.throwIfAborted();
9255
9766
  return messages.map((message) => messageRow(context2.chats, context2.bot, message, listing(message)));
9256
9767
  }
9257
- async function page(context2, chatId, messages, limit, signal) {
9258
- const shown = messages.slice(0, limit);
9768
+ async function page(context2, chatId, messages, limit2, signal) {
9769
+ const shown = messages.slice(0, limit2);
9259
9770
  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 } : {} };
9771
+ return { messages: await rows2(context2, shown, signal), ...messages.length > limit2 ? { hasMore: true } : {} };
9261
9772
  }
9262
9773
  var messageTools = [
9263
9774
  tool(
@@ -9273,7 +9784,7 @@ var messageTools = [
9273
9784
  }, { additionalProperties: false }),
9274
9785
  async (input, context2, { signal }) => {
9275
9786
  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;
9787
+ const limit2 = input.limit ?? 30, modes = [input.after !== void 0, input.around !== void 0, input.unread === true].filter(Boolean).length;
9277
9788
  if (modes > 1) throw new ChatError(400, "Use one of after, around or unread");
9278
9789
  if (input.around !== void 0) {
9279
9790
  const messages2 = chats.around(bot, input.around, input.context ?? 5);
@@ -9282,7 +9793,7 @@ var messageTools = [
9282
9793
  }
9283
9794
  if (input.unread && input.chatId === void 0) {
9284
9795
  const found = [];
9285
- let left = limit, more = false;
9796
+ let left = limit2, more = false;
9286
9797
  for (const chat2 of chats.channels.digest(bot).unread) {
9287
9798
  if (!left) {
9288
9799
  more = true;
@@ -9297,8 +9808,8 @@ var messageTools = [
9297
9808
  }
9298
9809
  if (input.chatId === void 0) throw new ChatError(400, "chatId is required unless unread=true");
9299
9810
  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) };
9811
+ const messages = input.unread ? chats.unreadMessages(bot, chat.id, limit2 + 1) : chats.read(bot, chat.id, input.after ?? 0, limit2 + 1);
9812
+ return { chatId: chat.id, ...await page(context2, chat.id, messages, limit2, signal) };
9302
9813
  }
9303
9814
  ),
9304
9815
  // `mine`: only what this Bot sent; those messages carry the History ID of the turn that sent them (A21).
@@ -9427,10 +9938,10 @@ async function viewFile(file, contents) {
9427
9938
  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
9939
  return { kind: "media", text: head, images: [{ data: (await contents.read(file, PICTURE_LIMIT)).toString("base64"), mimeType: picture }] };
9429
9940
  }
9430
- const text4 = file.size <= TEXT_LIMIT2 ? asText(await contents.read(file, TEXT_LIMIT2)) : null;
9431
- if (text4 !== null) return `${head}
9941
+ const text5 = file.size <= TEXT_LIMIT2 ? asText(await contents.read(file, TEXT_LIMIT2)) : null;
9942
+ if (text5 !== null) return `${head}
9432
9943
 
9433
- ${text4}`;
9944
+ ${text5}`;
9434
9945
  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
9946
  }
9436
9947
  function pictureType(data) {
@@ -9629,29 +10140,81 @@ var taskTools = [
9629
10140
  )
9630
10141
  ];
9631
10142
 
9632
- // src/server/service/bot/tools/agent.ts
10143
+ // src/server/service/bot/tools/ports.ts
9633
10144
  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" })
10145
+ var Port = Type10.Integer({ minimum: 1, maximum: 65535, description: "The port your service listens on in your sandbox" });
10146
+ 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.";
10147
+ var NOTE = {
10148
+ live: "Your service is reachable through the link.",
10149
+ local: LOOPBACK,
10150
+ idle: "Nothing listens on this port in your sandbox yet; the link works once your service listens there on 0.0.0.0.",
10151
+ stopped: "Your sandbox isn't running; the link works once it runs and your service listens there.",
10152
+ unknown: "Whether your service listens there couldn't be checked."
10153
+ };
10154
+ function row(context2, port) {
10155
+ const { fromSandboxes } = context2.scope.ports.links(port.hostPort);
10156
+ return {
10157
+ port: port.port,
10158
+ name: port.name,
10159
+ description: port.description,
10160
+ url: port.urls[0] ?? null,
10161
+ ...port.urls.length > 1 ? { otherUrls: port.urls.slice(1) } : {},
10162
+ ...fromSandboxes ? { fromSandboxes } : {},
10163
+ state: port.state,
10164
+ note: NOTE[port.state]
10165
+ };
10166
+ }
10167
+ var portTools = [
10168
+ tool(
10169
+ "ports.publish",
10170
+ "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.",
10171
+ Type10.Object({
10172
+ port: Port,
10173
+ name: Type10.String({ minLength: 1, maxLength: 60, description: "A short name, in your owner's language, such as the app's name" }),
10174
+ description: Type10.String({ minLength: 1, maxLength: 300, description: "What it is and what to look at there, in your owner's language" })
10175
+ }, { additionalProperties: false }),
10176
+ async (input, context2) => row(context2, await context2.scope.ports.publish(context2.bot, input))
10177
+ ),
10178
+ tool(
10179
+ "ports.list",
10180
+ "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).",
10181
+ None,
10182
+ async (_args, context2) => ({ ports: (await context2.scope.ports.list(context2.bot)).map((port) => row(context2, port)) })
10183
+ ),
10184
+ tool(
10185
+ "ports.unpublish",
10186
+ "Unpublish one of your ports: its link stops working. Your service keeps running in your sandbox.",
10187
+ Type10.Object({ port: Port }, { additionalProperties: false }),
10188
+ ({ port }, context2) => {
10189
+ context2.scope.ports.unpublish(context2.bot, port);
10190
+ return { port, unpublished: true };
10191
+ }
10192
+ )
10193
+ ];
10194
+
10195
+ // src/server/service/bot/tools/agent.ts
10196
+ import { Type as Type11 } from "typebox";
10197
+ var Name2 = Type11.String({ minLength: 1, maxLength: 80, description: "Unique among all members, people and Bots" });
10198
+ var BotName = Type11.String({ minLength: 1, maxLength: 80, description: "The Bot's name, as bots.list shows it" });
10199
+ var Model2 = Type11.Object({
10200
+ connection: Type11.String({ minLength: 1, maxLength: 200, description: "A connection's name or ID from models.list" }),
10201
+ model: Type11.String({ minLength: 1, maxLength: 200, description: "A model's ID or name in that connection" })
9639
10202
  }, { additionalProperties: false });
9640
- var Thinking = Type10.Union(
9641
- ["auto", "off", "minimal", "low", "medium", "high", "xhigh", "max"].map((level) => Type10.Literal(level)),
10203
+ var Thinking = Type11.Union(
10204
+ ["auto", "off", "minimal", "low", "medium", "high", "xhigh", "max"].map((level) => Type11.Literal(level)),
9642
10205
  { description: "How much the model thinks; auto lets this chat platform choose. models.list gives the levels each model has" }
9643
10206
  );
9644
10207
  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)
10208
+ description: Type11.Optional(Type11.String({ maxLength: 1e3, description: "One line that other members see in members.list" })),
10209
+ persona: Type11.Optional(Type11.String({ maxLength: 16e3, description: "Its standing instructions: who it is and how it works (the Persona section of its prompt)" })),
10210
+ model: Type11.Optional(Model2),
10211
+ thinking: Type11.Optional(Thinking)
9649
10212
  };
9650
10213
  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" })
10214
+ askIn: Type11.String({ description: "ID of a chat the owner is in, where the confirmation card goes; usually the chat you were asked in" }),
10215
+ 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" }),
10216
+ confirm: Type11.String({ minLength: 1, maxLength: 40, description: "Label of the button that deletes, in the owner's language" }),
10217
+ cancel: Type11.String({ minLength: 1, maxLength: 40, description: "Label of the button that keeps everything, in the owner's language" })
9655
10218
  };
9656
10219
  var after = "The owner first gets a card in askIn, and nothing is deleted unless they confirm; the result says what they chose.";
9657
10220
  var ops = (context2) => context2.scope.agent;
@@ -9682,7 +10245,7 @@ var agentTools = [
9682
10245
  tool(
9683
10246
  "bots.create",
9684
10247
  "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 }),
10248
+ Type11.Object({ name: Name2, ...Settings }, { additionalProperties: false }),
9686
10249
  async (settings, context2) => {
9687
10250
  const agent = ops(context2);
9688
10251
  const bot = await agent.create(settings);
@@ -9692,7 +10255,7 @@ var agentTools = [
9692
10255
  tool(
9693
10256
  "bots.update",
9694
10257
  "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 }),
10258
+ Type11.Object({ bot: BotName, name: Type11.Optional(Name2), ...Settings }, { additionalProperties: false }),
9696
10259
  async ({ bot, ...settings }, context2) => {
9697
10260
  const result = await ops(context2).update(ops(context2).bot(bot), settings);
9698
10261
  return { name: result.bot.name, changed: result.changed };
@@ -9701,7 +10264,7 @@ var agentTools = [
9701
10264
  tool(
9702
10265
  "bots.start",
9703
10266
  "Start a stopped Bot. A stopped Bot also starts by itself when a message arrives for it.",
9704
- Type10.Object({ bot: BotName }, { additionalProperties: false }),
10267
+ Type11.Object({ bot: BotName }, { additionalProperties: false }),
9705
10268
  async (input, context2) => {
9706
10269
  const bot = ops(context2).bot(input.bot);
9707
10270
  return { name: bot.name, status: await ops(context2).run(bot, "start") };
@@ -9710,7 +10273,7 @@ var agentTools = [
9710
10273
  tool(
9711
10274
  "bots.stop",
9712
10275
  "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 }),
10276
+ Type11.Object({ bot: BotName }, { additionalProperties: false }),
9714
10277
  async (input, context2) => {
9715
10278
  const bot = ops(context2).bot(input.bot);
9716
10279
  return { name: bot.name, status: await ops(context2).run(bot, "stop") };
@@ -9719,7 +10282,7 @@ var agentTools = [
9719
10282
  tool(
9720
10283
  "bots.usage",
9721
10284
  "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 }),
10285
+ Type11.Object({ bot: BotName, days: Type11.Optional(Type11.Integer({ minimum: 1, maximum: 365 })) }, { additionalProperties: false }),
9723
10286
  (input, context2) => {
9724
10287
  const bot = ops(context2).bot(input.bot);
9725
10288
  return { name: bot.name, ...ops(context2).usage(bot, input.days ?? 7) };
@@ -9728,7 +10291,7 @@ var agentTools = [
9728
10291
  tool(
9729
10292
  "bots.delete",
9730
10293
  `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 }),
10294
+ Type11.Object({ bot: BotName, ...Confirm }, { additionalProperties: false }),
9732
10295
  async (input, context2, call) => {
9733
10296
  const agent = ops(context2), bot = agent.bot(input.bot);
9734
10297
  if (bot.id === agent.agentId) throw new ChatError(400, "You can't delete yourself");
@@ -9741,7 +10304,7 @@ var agentTools = [
9741
10304
  tool(
9742
10305
  "chats.delete",
9743
10306
  `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 }),
10307
+ Type11.Object({ chatId: Type11.String({ description: "Channel ID" }), ...Confirm }, { additionalProperties: false }),
9745
10308
  async (input, context2, call) => {
9746
10309
  const chat = channel(context2, input.chatId);
9747
10310
  const declined = await confirmed(context2, input, call);
@@ -9753,7 +10316,7 @@ var agentTools = [
9753
10316
  tool(
9754
10317
  "chats.removeMembers",
9755
10318
  `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 }),
10319
+ 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
10320
  async (input, context2, call) => {
9758
10321
  const chat = channel(context2, input.chatId);
9759
10322
  const members = context2.members(input.members).filter((member) => chat.memberIds.includes(member.id));
@@ -9767,7 +10330,7 @@ var agentTools = [
9767
10330
  tool(
9768
10331
  "sections.delete",
9769
10332
  `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 }),
10333
+ Type11.Object({ section: Type11.String({ minLength: 1, maxLength: 80, description: "The group's name or ID (see sections.list)" }), ...Confirm }, { additionalProperties: false }),
9771
10334
  async (input, context2, call) => {
9772
10335
  const section = context2.section(input.section);
9773
10336
  const declined = await confirmed(context2, input, call);
@@ -9779,11 +10342,11 @@ var agentTools = [
9779
10342
  ];
9780
10343
 
9781
10344
  // 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" }))
10345
+ var EVERY_BOT = [...historyTools, ...chatTools, ...messageTools, ...fileTools, ...environmentTools2, ...portTools, ...secretTools, ...taskTools];
10346
+ var Gateway = Type12.Object({
10347
+ action: Type12.Unsafe({ type: "string", enum: ["list", "describe", "call"] }),
10348
+ tool: Type12.Optional(Type12.String()),
10349
+ args: Type12.Optional(Type12.Unsafe({ type: "object" }))
9787
10350
  }, { additionalProperties: false });
9788
10351
  var Tools = class {
9789
10352
  send;
@@ -9834,13 +10397,13 @@ var welcomeLanguage = (value2) => typeof value2 === "string" && Object.hasOwn(WE
9834
10397
 
9835
10398
  // src/server/service/chat/envelope.ts
9836
10399
  var INVALID_XML = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\ud800-\udfff\ufffe\uffff]/u;
9837
- function escape(value2) {
10400
+ function escape2(value2) {
9838
10401
  if (INVALID_XML.test(value2)) throw new Error("Invalid XML character");
9839
10402
  return value2.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\r/g, "&#13;");
9840
10403
  }
9841
10404
  function attribute(name3, value2) {
9842
10405
  if (!value2.trim()) throw new Error(`Missing ${name3}`);
9843
- return ` ${name3}="${escape(value2).replace(/"/g, "&quot;").replace(/\n/g, "&#10;").replace(/\t/g, "&#9;")}"`;
10406
+ return ` ${name3}="${escape2(value2).replace(/"/g, "&quot;").replace(/\n/g, "&#10;").replace(/\t/g, "&#9;")}"`;
9844
10407
  }
9845
10408
  function remember(map, id, definition) {
9846
10409
  const old = map.get(id);
@@ -9862,7 +10425,7 @@ function encodeNoticeBatch(botId, notices, environment, prompt, unreadNotes) {
9862
10425
  function instructionsLine(change) {
9863
10426
  if (!change) return "";
9864
10427
  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>`;
10428
+ 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
10429
  }
9867
10430
  function noticeTail(notices, chats, members) {
9868
10431
  for (const notice of notices) {
@@ -9881,14 +10444,14 @@ function noticeTail(notices, chats, members) {
9881
10444
  const latest = /* @__PURE__ */ new Map();
9882
10445
  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
10446
  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>`]);
10447
+ 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
10448
  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>`}`);
10449
+ 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
10450
  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);
10451
+ 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
10452
  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("");
10453
+ 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>`]);
10454
+ 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
10455
  }
9893
10456
  function encodeChatBatch(botId, messages, digest, notices, prompt) {
9894
10457
  if (!messages.length) throw new Error("Empty chat batch");
@@ -9903,9 +10466,9 @@ function encodeChatBatch(botId, messages, digest, notices, prompt) {
9903
10466
  const attributes = attribute("id", message.id) + attribute("chat", message.chat.id) + attribute("from", message.sender.id) + attribute("at", at2);
9904
10467
  const reply = message.replyTo;
9905
10468
  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>` : "";
10469
+ const quote2 = reply ? `<reply_to${attribute("message", reply.id)}${attribute("from", reply.sender.id)}>${escape2(reply.text)}</reply_to>` : "";
9907
10470
  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>`;
10471
+ return `<message${attributes}>${quote2}${message.content ? `<text>${escape2(message.content)}</text>` : ""}${files}</message>`;
9909
10472
  });
9910
10473
  const ref = (key, item) => attribute(key, item.chatId) + (chats.has(item.chatId) ? "" : attribute("title", item.title) + attribute("type", item.type));
9911
10474
  let tail = (notices?.length ? noticeTail(notices, chats, members) : "") + instructionsLine(prompt);
@@ -9995,8 +10558,8 @@ var BotInbox = class {
9995
10558
  }
9996
10559
  case "reaction": {
9997
10560
  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 };
10561
+ const text5 = marked ? previewQuotes(marked.content, marked.quotes).slice(0, 120) : "";
10562
+ return { kind: "reaction", id, chat: notice.chat, message: { id: notice.messageId, text: text5 }, actor: notice.actor, emoji: notice.payload.emoji, createdAt };
10000
10563
  }
10001
10564
  // Its conversation was compacted (A73): the open threads it follows, as they are now.
10002
10565
  case "followed": {
@@ -10316,7 +10879,7 @@ When something will take a while, post a short progress note partway through, an
10316
10879
  function workspaceSection(setup) {
10317
10880
  return `## Workspace
10318
10881
  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.
10882
+ 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
10883
 
10321
10884
  ## Work environment
10322
10885
  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 +10904,8 @@ var LIVE_INPUT = 300;
10341
10904
  var LIVE_MESSAGE = 2e3;
10342
10905
 
10343
10906
  // src/server/service/bot/live-input.ts
10344
- function clip(text4, length) {
10345
- return text4.length > length ? `${text4.slice(0, length - 1)}\u2026` : text4;
10907
+ function clip(text5, length) {
10908
+ return text5.length > length ? `${text5.slice(0, length - 1)}\u2026` : text5;
10346
10909
  }
10347
10910
  var field = (value2, length = LIVE_INPUT) => typeof value2 === "string" ? clip(value2, length) : void 0;
10348
10911
  var record2 = (value2) => value2 && typeof value2 === "object" && !Array.isArray(value2) ? value2 : {};
@@ -10396,10 +10959,10 @@ function sentChat(output, chat) {
10396
10959
 
10397
10960
  // src/server/service/bot/live.ts
10398
10961
  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 };
10962
+ function liveOutput(text5) {
10963
+ if (!text5) return null;
10964
+ if (text5.length <= 2 * LIVE_OUTPUT_END) return { head: text5, tail: "", omitted: 0 };
10965
+ return { head: text5.slice(0, LIVE_OUTPUT_END), tail: text5.slice(-LIVE_OUTPUT_END), omitted: text5.length - 2 * LIVE_OUTPUT_END };
10403
10966
  }
10404
10967
  var LiveTurns = class {
10405
10968
  constructor(chat) {
@@ -10456,8 +11019,8 @@ var LiveTurns = class {
10456
11019
  }
10457
11020
  const step = this.steps.get(id);
10458
11021
  if (!step) return;
10459
- const text4 = (step.text ?? "") + event.delta, over = Math.max(0, text4.length - LIVE_TEXT);
10460
- step.text = text4.slice(over);
11022
+ const text5 = (step.text ?? "") + event.delta, over = Math.max(0, text5.length - LIVE_TEXT);
11023
+ step.text = text5.slice(over);
10461
11024
  step.omitted = (step.omitted ?? 0) + over;
10462
11025
  this.queue({ op: "text", id, append: event.delta });
10463
11026
  return;
@@ -10703,6 +11266,222 @@ var AgentOps = class {
10703
11266
  }
10704
11267
  };
10705
11268
 
11269
+ // src/server/infra/net/proxy.ts
11270
+ import { request as httpRequest } from "node:http";
11271
+ import { connect, createServer as createServer2 } from "node:net";
11272
+ var HOP = /* @__PURE__ */ new Set(["connection", "keep-alive", "proxy-connection", "transfer-encoding", "te", "trailer", "upgrade", "proxy-authorization", "proxy-authenticate"]);
11273
+ function cookies(header, keep) {
11274
+ if (!header) return void 0;
11275
+ const kept = header.split(";").map((part) => part.trim()).filter((part) => part && keep(part.split("=")[0].trim()));
11276
+ return kept.length ? kept.join("; ") : void 0;
11277
+ }
11278
+ function requestHeaders(request, policy2, upgrade) {
11279
+ const headers2 = {};
11280
+ for (const [name3, value2] of Object.entries(request.headers)) {
11281
+ if (value2 === void 0 || name3 === "cookie" || name3.startsWith("x-forwarded-")) continue;
11282
+ if (HOP.has(name3) && !(upgrade && (name3 === "upgrade" || name3 === "connection"))) continue;
11283
+ headers2[name3] = value2;
11284
+ }
11285
+ const cookie2 = cookies(request.headers.cookie, policy2.cookie);
11286
+ if (cookie2) headers2.cookie = cookie2;
11287
+ headers2["x-forwarded-for"] = policy2.client.address.replace(/^::ffff:/, "");
11288
+ headers2["x-forwarded-proto"] = policy2.client.secure ? "https" : "http";
11289
+ if (request.headers.host) headers2["x-forwarded-host"] = request.headers.host;
11290
+ return headers2;
11291
+ }
11292
+ function responseHeaders(headers2, policy2) {
11293
+ const out = {};
11294
+ for (const [name3, value2] of Object.entries(headers2)) {
11295
+ if (value2 === void 0 || HOP.has(name3)) continue;
11296
+ if (name3 === "set-cookie") {
11297
+ const kept = (Array.isArray(value2) ? value2 : [value2]).filter((line) => policy2.setCookie(line.split("=")[0].trim()));
11298
+ if (kept.length) out[name3] = kept;
11299
+ continue;
11300
+ }
11301
+ out[name3] = value2;
11302
+ }
11303
+ return out;
11304
+ }
11305
+ function forward(request, response, upstream, policy2, failed) {
11306
+ const outgoing = httpRequest({ host: upstream.host, port: upstream.port, method: request.method, path: request.url, headers: requestHeaders(request, policy2, false) }, (incoming) => {
11307
+ response.writeHead(incoming.statusCode ?? 502, incoming.statusMessage, responseHeaders(incoming.headers, policy2));
11308
+ incoming.pipe(response);
11309
+ incoming.on("error", () => response.destroy());
11310
+ });
11311
+ outgoing.on("error", (error) => {
11312
+ if (response.headersSent) response.destroy();
11313
+ else failed(error);
11314
+ });
11315
+ response.on("close", () => {
11316
+ if (!response.writableFinished) outgoing.destroy();
11317
+ });
11318
+ request.pipe(outgoing);
11319
+ }
11320
+ function forwardUpgrade(request, socket, head, upstream, policy2, failed) {
11321
+ const target3 = connect(upstream.port, upstream.host);
11322
+ let joined = false;
11323
+ target3.once("connect", () => {
11324
+ joined = true;
11325
+ const headers2 = requestHeaders(request, policy2, true);
11326
+ const lines = [`${request.method} ${request.url} HTTP/${request.httpVersion}`];
11327
+ for (const [name3, value2] of Object.entries(headers2)) for (const item of Array.isArray(value2) ? value2 : [value2]) lines.push(`${name3}: ${item}`);
11328
+ target3.write(`${lines.join("\r\n")}\r
11329
+ \r
11330
+ `);
11331
+ if (head.length) target3.write(head);
11332
+ socket.pipe(target3).pipe(socket);
11333
+ });
11334
+ target3.on("error", (error) => {
11335
+ if (joined) socket.destroy();
11336
+ else failed(error);
11337
+ });
11338
+ socket.on("error", () => target3.destroy());
11339
+ socket.on("close", () => target3.destroy());
11340
+ target3.on("close", () => socket.destroy());
11341
+ }
11342
+ function portFree(host, port) {
11343
+ return new Promise((resolve5) => {
11344
+ const server = createServer2();
11345
+ server.once("error", () => resolve5(false));
11346
+ server.listen({ host, port, exclusive: true }, () => server.close(() => resolve5(true)));
11347
+ });
11348
+ }
11349
+
11350
+ // src/server/service/bot/ports.ts
11351
+ var MAX_PER_BOT = 20;
11352
+ var SESSION_COOKIE = /^jarvy_session_/;
11353
+ var policy = (client) => ({ cookie: (name3) => !SESSION_COOKIE.test(name3), setCookie: (name3) => !SESSION_COOKIE.test(name3), client });
11354
+ var wildcard = (host) => host === "0.0.0.0" || host === "::";
11355
+ var PublishedPorts = class {
11356
+ constructor(store, computers, listening) {
11357
+ this.store = store;
11358
+ this.computers = computers;
11359
+ this.listening = listening;
11360
+ }
11361
+ listeners = /* @__PURE__ */ new Set();
11362
+ /** Taking a port is one at a time, so two publishes don't pick the same free one. */
11363
+ taking = Promise.resolve();
11364
+ onChange(listener) {
11365
+ this.listeners.add(listener);
11366
+ return () => this.listeners.delete(listener);
11367
+ }
11368
+ /** Rows changed elsewhere, such as a Bot deleted with its ports. */
11369
+ changed() {
11370
+ for (const listener of this.listeners) listener();
11371
+ }
11372
+ /** Every published port, for the listening. */
11373
+ all() {
11374
+ return this.store.ports.all();
11375
+ }
11376
+ /** A Bot's published ports with what each finds now. */
11377
+ async list(botId) {
11378
+ const records = this.store.ports.list(botId);
11379
+ if (!records.length) return [];
11380
+ const states = await this.states(botId);
11381
+ return records.map((record3) => this.view(record3, states(record3.port)));
11382
+ }
11383
+ /** Publishes a port of the Bot's sandbox, or renames one it published, which keeps its link. */
11384
+ async publish(botId, input) {
11385
+ if (!this.store.environments.computerOf(botId)) throw new RuntimeFault("computer_missing", "This Bot has no sandbox.", 404);
11386
+ const port = input.port;
11387
+ if (!Number.isInteger(port) || port < 1 || port > 65535) throw new ValidationError("A port is 1-65535", "port_invalid");
11388
+ const name3 = text4(input.name, "A name", 60), description = text4(input.description, "A description", 300);
11389
+ const existing = this.store.ports.get(botId, port);
11390
+ 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");
11391
+ const record3 = await this.take(async () => {
11392
+ const hostPort = this.store.ports.get(botId, port)?.hostPort ?? await this.freePort();
11393
+ return this.store.ports.save({ botId, port, name: name3, description, hostPort });
11394
+ });
11395
+ this.changed();
11396
+ return this.view(record3, (await this.states(botId))(record3.port));
11397
+ }
11398
+ unpublish(botId, port) {
11399
+ if (!this.store.ports.remove(botId, port)) throw new ValidationError("That port isn't published", "port_missing");
11400
+ this.changed();
11401
+ }
11402
+ /** The sandbox is reset: what ran there is gone, and so are its links. */
11403
+ unpublishAll(botId) {
11404
+ if (this.store.ports.removeAll(botId)) this.changed();
11405
+ }
11406
+ /** A published port's own port was taken by another program while Jarvy was off: it moves to a free one. */
11407
+ async relocate(record3) {
11408
+ await this.take(async () => this.store.ports.move(record3.botId, record3.port, await this.freePort()));
11409
+ this.changed();
11410
+ }
11411
+ /** Where a request to Jarvy's `hostPort` goes now. */
11412
+ async target(hostPort) {
11413
+ const record3 = this.store.ports.byHostPort(hostPort);
11414
+ if (!record3) return { kind: "missing" };
11415
+ const host = await this.computers.address(record3.botId);
11416
+ return host ? { kind: "open", botId: record3.botId, host, port: record3.port } : { kind: "stopped" };
11417
+ }
11418
+ /** Forwards a request the API layer let through to the sandbox; `failed` when nothing answers there. */
11419
+ forward(request, response, to, client, failed) {
11420
+ forward(request, response, to, policy(client), failed);
11421
+ }
11422
+ forwardUpgrade(request, socket, head, to, client, failed) {
11423
+ forwardUpgrade(request, socket, head, to, policy(client), failed);
11424
+ }
11425
+ /** The Bot whose sandbox a request came from, if it is one of this Jarvy's. */
11426
+ caller(address2) {
11427
+ return this.computers.botAt(address2);
11428
+ }
11429
+ /** Links to it: from other devices (or this machine, when Jarvy listens on it only), and from sandboxes. */
11430
+ links(hostPort) {
11431
+ const where2 = this.listening();
11432
+ if (!where2) return { urls: [], fromSandboxes: null };
11433
+ const url = (scheme, host) => `${scheme}://${host.includes(":") ? `[${host}]` : host}:${hostPort}`;
11434
+ const urls = where2.addresses.length ? where2.addresses.map((host) => url(where2.https ? "https" : "http", host)) : [url("http", "localhost")];
11435
+ const fromSandboxes = wildcard(where2.host) ? url("http", this.computers.hostName) : where2.addresses[0] ? url("http", where2.addresses[0]) : null;
11436
+ return { urls, fromSandboxes };
11437
+ }
11438
+ take(work) {
11439
+ const done2 = this.taking.then(work, work);
11440
+ this.taking = done2.catch(() => void 0);
11441
+ return done2;
11442
+ }
11443
+ view(record3, state) {
11444
+ return {
11445
+ botId: record3.botId,
11446
+ port: record3.port,
11447
+ name: record3.name,
11448
+ description: record3.description,
11449
+ hostPort: record3.hostPort,
11450
+ urls: this.links(record3.hostPort).urls,
11451
+ state,
11452
+ createdAt: record3.createdAt,
11453
+ updatedAt: record3.updatedAt
11454
+ };
11455
+ }
11456
+ async states(botId) {
11457
+ try {
11458
+ const listening = await this.computers.listening(botId);
11459
+ if (!listening) return () => "stopped";
11460
+ return (port) => {
11461
+ const found = listening.get(port);
11462
+ return !found ? "idle" : found.loopbackOnly ? "local" : "live";
11463
+ };
11464
+ } catch {
11465
+ return () => "unknown";
11466
+ }
11467
+ }
11468
+ /** The first port of the range no published port has and nothing listens on, skipping Jarvy's own. */
11469
+ async freePort() {
11470
+ const where2 = this.listening();
11471
+ if (!where2) throw new RuntimeFault("stopping", "Jarvy isn't listening yet.", 503);
11472
+ const taken = this.store.ports.hostPorts();
11473
+ for (let port = where2.range.from; port <= where2.range.to; port++) {
11474
+ if (port === where2.port || taken.has(port)) continue;
11475
+ if (await portFree(where2.host, port)) return port;
11476
+ }
11477
+ throw new ValidationError(`No free port left in ${where2.range.from}-${where2.range.to}`, "ports_exhausted", { from: where2.range.from, to: where2.range.to });
11478
+ }
11479
+ };
11480
+ function text4(value2, label, max) {
11481
+ if (typeof value2 !== "string" || !value2.trim() || value2.length > max || value2.includes("\0")) throw new ValidationError(`${label} must be 1-${max} characters`, "port_invalid");
11482
+ return value2.trim();
11483
+ }
11484
+
10706
11485
  // src/server/service/bot/service.ts
10707
11486
  var BotService = class {
10708
11487
  constructor(deps) {
@@ -10710,6 +11489,7 @@ var BotService = class {
10710
11489
  this.agent = new AgentOps(this, deps.store, deps.chats, deps.deliveries);
10711
11490
  this.computers = new Computers({ store: deps.store, changed: () => this.listChanged() });
10712
11491
  this.computers.adopt(deps.store.bots.list().map((record3) => record3.id));
11492
+ this.ports = new PublishedPorts(deps.store, this.computers, () => deps.listening?.() ?? null);
10713
11493
  this.machines = new SshMachines(deps.store, deps.chats, () => this.listChanged());
10714
11494
  this.environments = new BotEnvironments(deps.store, this.computers, this.machines);
10715
11495
  }
@@ -10719,6 +11499,8 @@ var BotService = class {
10719
11499
  environments;
10720
11500
  /** The owner's SSH machines (A57). */
10721
11501
  machines;
11502
+ /** Ports Bots publish from their sandboxes (A74). */
11503
+ ports;
10722
11504
  closed = false;
10723
11505
  started = false;
10724
11506
  /** What only the agent Bot does (A48). */
@@ -10786,6 +11568,7 @@ var BotService = class {
10786
11568
  },
10787
11569
  secrets: { list: () => this.deps.secrets.listFor(id), prepare: (name3, purpose) => this.deps.secrets.prepareRequest(id, name3, purpose) },
10788
11570
  tasks: this.deps.tasks,
11571
+ ports: this.ports,
10789
11572
  ...id === AGENT_BOT_ID ? { agent: this.agent } : {}
10790
11573
  }),
10791
11574
  agentRuntime: runtime,
@@ -10899,12 +11682,26 @@ var BotService = class {
10899
11682
  this.describe(id);
10900
11683
  if (action === "stop") await this.computers.stop(id);
10901
11684
  else {
10902
- if (action === "reset") await this.computers.reset(id);
11685
+ if (action === "reset") {
11686
+ await this.computers.reset(id);
11687
+ this.ports.unpublishAll(id);
11688
+ }
10903
11689
  void this.computers.ensure(id).catch(() => {
10904
11690
  });
10905
11691
  }
10906
11692
  return this.describe(id);
10907
11693
  }
11694
+ /** What the sandbox may use at most (A75); returns the Bot with them. */
11695
+ async setComputerLimits(id, limits2) {
11696
+ this.describe(id);
11697
+ await this.computers.setLimits(id, limits2);
11698
+ return this.describe(id);
11699
+ }
11700
+ /** What the sandbox uses now, and this machine's size. */
11701
+ computerResources(id) {
11702
+ this.describe(id);
11703
+ return this.computers.resources(id);
11704
+ }
10908
11705
  describe(id) {
10909
11706
  return this.details(this.record(id));
10910
11707
  }
@@ -10912,10 +11709,10 @@ var BotService = class {
10912
11709
  this.describe(id);
10913
11710
  return this.deps.store.runtime.runs(id);
10914
11711
  }
10915
- activity(id, before, limit = 40) {
11712
+ activity(id, before, limit2 = 40) {
10916
11713
  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);
11714
+ const messages = this.deps.chats.sentActivity(this.deps.store.account.userMemberId(), id, before, limit2 + 1);
11715
+ return activityPage(messages, bot.memory.timeline(before, limit2 + 1), limit2);
10919
11716
  }
10920
11717
  activityMessage(id, messageId) {
10921
11718
  this.describe(id);
@@ -11004,6 +11801,7 @@ var BotService = class {
11004
11801
  this.lives.delete(id);
11005
11802
  await this.environments.release(id);
11006
11803
  if (!this.deps.store.bots.delete(id)) throw new RuntimeFault("bot_missing", "Bot not found", 404);
11804
+ this.ports.changed();
11007
11805
  await this.deps.chats.attachments.clean();
11008
11806
  if (!keepFiles) await rm5(dirname10(workspacePath), { recursive: true, force: true });
11009
11807
  }
@@ -11019,6 +11817,9 @@ var where = (moved) => `${moved.to.name}, in ${moved.to.directory}`;
11019
11817
 
11020
11818
  // src/server/service/connection.ts
11021
11819
  var ALLOW_HTTP = "allow_http";
11820
+ var PORT_RANGE = "published_port_range";
11821
+ var DEFAULT_PORT_RANGE = { from: 12600, to: 12699 };
11822
+ var MAX_RANGE = 1e3;
11022
11823
  var ConnectionService = class {
11023
11824
  constructor(store, dataDir) {
11024
11825
  this.store = store;
@@ -11039,10 +11840,37 @@ var ConnectionService = class {
11039
11840
  this.store.settings.set(ALLOW_HTTP, allow ? "1" : null);
11040
11841
  return this.info();
11041
11842
  }
11843
+ portRange() {
11844
+ const [from, to] = (this.store.settings.get(PORT_RANGE) ?? "").split("-").map(Number);
11845
+ return Number.isInteger(from) && Number.isInteger(to) ? { from, to } : DEFAULT_PORT_RANGE;
11846
+ }
11847
+ /** Ports already published keep theirs; new ones take from the new range. */
11848
+ setPortRange(value2) {
11849
+ const range = value2;
11850
+ const from = range?.from, to = range?.to;
11851
+ if (!Number.isInteger(from) || !Number.isInteger(to) || from < 1024 || to > 65535 || from > to || to - from + 1 > MAX_RANGE)
11852
+ throw new ValidationError(`Ports are 1024-65535, from no more than to, at most ${MAX_RANGE} of them`, "port_range_invalid");
11853
+ 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");
11854
+ const same3 = from === DEFAULT_PORT_RANGE.from && to === DEFAULT_PORT_RANGE.to;
11855
+ this.store.settings.set(PORT_RANGE, same3 ? null : `${from}-${to}`);
11856
+ return this.info();
11857
+ }
11042
11858
  info() {
11043
11859
  const listening = this.listening, material = this.material;
11044
11860
  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 };
11861
+ return {
11862
+ https: !!material,
11863
+ allowHttp: this.allowHttp(),
11864
+ addresses,
11865
+ fingerprint: material?.fingerprint ?? null,
11866
+ fingerprintSha1: material?.fingerprintSha1 ?? null,
11867
+ ports: this.portRange()
11868
+ };
11869
+ }
11870
+ /** Null until Jarvy listens. `addresses`: hosts other devices may use, bare. */
11871
+ where() {
11872
+ const listening = this.listening;
11873
+ return listening ? { ...listening, https: !!this.material, addresses: lanAddresses(listening.host), range: this.portRange() } : null;
11046
11874
  }
11047
11875
  /** The root certificate (DER) for devices to install, when serving HTTPS. */
11048
11876
  rootCertificate() {
@@ -11309,7 +12137,8 @@ var Application = class _Application {
11309
12137
  catalog,
11310
12138
  changed: (id) => this.events.publish({ kind: "bot", id, status: this.bots.status(id) }),
11311
12139
  listChanged: () => this.events.publish({ kind: "bots" }),
11312
- factory
12140
+ factory,
12141
+ listening: () => this.connection.where()
11313
12142
  });
11314
12143
  this.auth = new CredentialLogins(store, this.credentials, (id) => this.bots.connectionChanged(id));
11315
12144
  this.connection = new ConnectionService(store, dataDir);
@@ -11541,8 +12370,8 @@ function listenOptions(args) {
11541
12370
  const host = option(args, "--host", "127.0.0.1");
11542
12371
  const port = Number(option(args, "--port", "8080"));
11543
12372
  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}` };
12373
+ const wildcard2 = host === "0.0.0.0" || host === "::";
12374
+ return { host, port, url: wildcard2 || isLoopbackHost(host) ? `http://${wildcard2 ? "localhost" : host}:${port}` : `https://${host}:${port}` };
11546
12375
  }
11547
12376
  async function startDetached(args, dir) {
11548
12377
  const { host, port } = listenOptions(args);
@@ -11615,10 +12444,16 @@ async function main() {
11615
12444
  const { host, port, url } = listenOptions(args);
11616
12445
  const webRoot = join19(dirname11(fileURLToPath2(import.meta.url)), "../web");
11617
12446
  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();
12447
+ const tls = app.connection.prepare(host, port);
12448
+ const server = createJarvyServer(app, webRoot, tls);
12449
+ const published = new PublishedListeners(app, tls, host, port);
12450
+ closeHttp = () => {
12451
+ server.close();
12452
+ published.close();
12453
+ };
11620
12454
  await server.listen(port, host);
11621
12455
  app.start();
12456
+ published.start();
11622
12457
  writePidFile(dir, url);
11623
12458
  console.log(`Jarvy running at ${url} (listening on ${host}, pid ${process.pid})`);
11624
12459
  const notes = reachNotes(host, port, app.connection.allowHttp());
@@ -11631,6 +12466,7 @@ async function main() {
11631
12466
  if (shuttingDown) return;
11632
12467
  shuttingDown = true;
11633
12468
  server.close();
12469
+ published.close();
11634
12470
  void app.close().then(() => {
11635
12471
  removePidFile(dir);
11636
12472
  console.log("Jarvy stopped.");