@michael-joseph-miller/ant-bot 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/server.js CHANGED
@@ -18,7 +18,7 @@ import {
18
18
  SettingsSchema,
19
19
  UpdateBotRequest,
20
20
  UpdateConnectorRequest
21
- } from "./chunk-I3GHIX3G.js";
21
+ } from "./chunk-BKF7CUVN.js";
22
22
  import {
23
23
  findWebDist,
24
24
  nodeLocateDeps,
@@ -38,7 +38,7 @@ import {
38
38
 
39
39
  // daemon/src/api/server.ts
40
40
  import path11 from "node:path";
41
- import { fileURLToPath as fileURLToPath2 } from "node:url";
41
+ import { fileURLToPath as fileURLToPath3 } from "node:url";
42
42
  import { createRequire } from "node:module";
43
43
  import Fastify from "fastify";
44
44
  import cors from "@fastify/cors";
@@ -47,6 +47,7 @@ import websocket from "@fastify/websocket";
47
47
  import fastifyStatic from "@fastify/static";
48
48
 
49
49
  // daemon/src/app.ts
50
+ import fs8 from "node:fs";
50
51
  import path8 from "node:path";
51
52
 
52
53
  // daemon/src/db/db.ts
@@ -184,6 +185,19 @@ CREATE TABLE bot_connectors (
184
185
  enabled INTEGER NOT NULL DEFAULT 1,
185
186
  PRIMARY KEY (bot_id, connector_id)
186
187
  );
188
+ `
189
+ },
190
+ {
191
+ version: 3,
192
+ name: "connector-kind-and-health",
193
+ // Built-in connectors (served by the daemon) alongside custom ones, and the last verdict a
194
+ // check or a turn reached — so the screen shows a connector's real state instead of a toast
195
+ // that has already vanished.
196
+ up: `
197
+ ALTER TABLE connectors ADD COLUMN kind TEXT NOT NULL DEFAULT 'custom';
198
+ ALTER TABLE connectors ADD COLUMN last_status TEXT;
199
+ ALTER TABLE connectors ADD COLUMN last_error TEXT;
200
+ ALTER TABLE connectors ADD COLUMN checked_at INTEGER;
187
201
  `
188
202
  }
189
203
  ];
@@ -257,10 +271,10 @@ function migrate(db, opts = {}) {
257
271
  });
258
272
  try {
259
273
  run();
260
- } catch (err) {
274
+ } catch (err2) {
261
275
  throw new MigrationError(
262
276
  "MIGRATION_FAILED",
263
- `migration ${m.version} (${m.name}) failed: ${err.message}` + (backupPath ? `. The pre-migration database was saved to ${backupPath}` : "")
277
+ `migration ${m.version} (${m.name}) failed: ${err2.message}` + (backupPath ? `. The pre-migration database was saved to ${backupPath}` : "")
264
278
  );
265
279
  }
266
280
  applied.push({ version: m.version, name: m.name });
@@ -365,8 +379,12 @@ var toConnector = (r) => ({
365
379
  id: r.id,
366
380
  name: r.name,
367
381
  description: r.description,
382
+ kind: r.kind === "builtin" ? "builtin" : "custom",
368
383
  config: ConnectorConfigSchema.parse(JSON.parse(r.config_json)),
369
384
  enabled: b(r.enabled),
385
+ lastStatus: r.last_status ?? null,
386
+ lastError: r.last_error ?? null,
387
+ checkedAt: r.checked_at ?? null,
370
388
  createdAt: r.created_at
371
389
  });
372
390
  var toRoutine = (r) => ({
@@ -756,10 +774,17 @@ var Store = class {
756
774
  createConnector(c) {
757
775
  const id = newId();
758
776
  this.db.prepare(
759
- `INSERT INTO connectors (id,name,description,config_json,enabled,created_at) VALUES (?,?,?,?,?,?)`
760
- ).run(id, c.name, c.description ?? "", JSON.stringify(c.config), i(c.enabled, true), now());
777
+ `INSERT INTO connectors (id,name,description,config_json,enabled,kind,created_at) VALUES (?,?,?,?,?,?,?)`
778
+ ).run(id, c.name, c.description ?? "", JSON.stringify(c.config), i(c.enabled, true), c.kind ?? "custom", now());
761
779
  return this.getConnector(id);
762
780
  }
781
+ /** Record the latest verdict. Written by both `check` and the turn's own mount report. */
782
+ setConnectorStatus(id, status, error = null) {
783
+ this.db.prepare(`UPDATE connectors SET last_status=?, last_error=?, checked_at=? WHERE id=?`).run(status, error, now(), id);
784
+ }
785
+ setConnectorStatusByName(name, status, error = null) {
786
+ this.db.prepare(`UPDATE connectors SET last_status=?, last_error=?, checked_at=? WHERE name=?`).run(status, error, now(), name);
787
+ }
763
788
  getConnector(id) {
764
789
  const r = this.db.prepare(`SELECT * FROM connectors WHERE id=?`).get(id);
765
790
  return r ? toConnector(r) : null;
@@ -1042,7 +1067,12 @@ var BUILTIN_RULES = [
1042
1067
  { kind: "allow", toolPattern: "Glob", inputPattern: "", scopeNote: "Listing files is safe", builtin: true, enabled: true },
1043
1068
  { kind: "allow", toolPattern: "Grep", inputPattern: "", scopeNote: "Searching files is safe", builtin: true, enabled: true },
1044
1069
  { kind: "allow", toolPattern: "TodoWrite", inputPattern: "", scopeNote: "Planning scratchpad", builtin: true, enabled: true },
1045
- { kind: "allow", toolPattern: "Bash", inputPattern: "^\\s*(git\\s+(status|diff|log|show|branch)|ls|pwd|cat|head|tail|wc|echo|date|which|grep|find|rg)\\b", scopeNote: "Read-only shell inspection", builtin: true, enabled: true }
1070
+ { kind: "allow", toolPattern: "Bash", inputPattern: "^\\s*(git\\s+(status|diff|log|show|branch)|ls|pwd|cat|head|tail|wc|echo|date|which|grep|find|rg)\\b", scopeNote: "Read-only shell inspection", builtin: true, enabled: true },
1071
+ // The one place a seeded `mcp__*` rule is right: these tool names are ant-bot's own (the gmail
1072
+ // connector is served by the daemon), fixed, and fully qualified — so they cannot match a
1073
+ // third-party server's tool by alias, and they are the two Gmail actions that leave the machine.
1074
+ { kind: "require", toolPattern: "mcp__gmail__send_message", inputPattern: "", scopeNote: "Sending email", builtin: true, enabled: true },
1075
+ { kind: "require", toolPattern: "mcp__gmail__create_draft", inputPattern: "", scopeNote: "Creating an email draft", builtin: true, enabled: true }
1046
1076
  ];
1047
1077
  function seedBuiltinRules(store) {
1048
1078
  const key = (r) => `${r.kind}\0${r.toolPattern}\0${r.inputPattern}`;
@@ -1183,8 +1213,8 @@ var PermissionGateway = class {
1183
1213
  if (verdict.verdict === "deny_suggested")
1184
1214
  return this.askHuman({ ...args, reason: `Auto review flagged this: ${verdict.reason}` });
1185
1215
  return this.askHuman({ ...args, reason: verdict.reason });
1186
- } catch (err) {
1187
- log3.warn("auto review failed; falling back to human approval", err);
1216
+ } catch (err2) {
1217
+ log3.warn("auto review failed; falling back to human approval", err2);
1188
1218
  }
1189
1219
  }
1190
1220
  return this.askHuman({
@@ -1293,6 +1323,18 @@ import { query as query2 } from "@anthropic-ai/claude-agent-sdk";
1293
1323
 
1294
1324
  // daemon/src/agent/session.ts
1295
1325
  import { query } from "@anthropic-ai/claude-agent-sdk";
1326
+
1327
+ // daemon/src/agent/runtime.ts
1328
+ var ClaudeRuntime = class {
1329
+ name = "claude";
1330
+ mountConnectors(connectors) {
1331
+ const out = {};
1332
+ for (const [name, c] of Object.entries(connectors)) out[name] = { ...c, alwaysLoad: true };
1333
+ return out;
1334
+ }
1335
+ };
1336
+
1337
+ // daemon/src/agent/session.ts
1296
1338
  var log4 = logger("agent");
1297
1339
  function resolveModel(tier) {
1298
1340
  return tier;
@@ -1305,6 +1347,7 @@ function buildEnv(settings, base = process.env) {
1305
1347
  }
1306
1348
  return env;
1307
1349
  }
1350
+ var runtime = new ClaudeRuntime();
1308
1351
  async function* runTurn(req) {
1309
1352
  const abort = req.abortController ?? new AbortController();
1310
1353
  const options = {
@@ -1316,7 +1359,14 @@ async function* runTurn(req) {
1316
1359
  includePartialMessages: true,
1317
1360
  permissionMode: "default",
1318
1361
  canUseTool: req.canUseTool,
1319
- mcpServers: req.mcpServers,
1362
+ mcpServers: {
1363
+ ...req.mcpServers ?? {},
1364
+ ...runtime.mountConnectors(req.connectors ?? {})
1365
+ },
1366
+ // ant-bot is the MCP host. The SDK mounts exactly what is passed here and nothing else — not
1367
+ // ~/.claude.json, not plugins, not claude.ai connectors. A bot's tools cannot change because
1368
+ // of something configured outside ant-bot, and swapping the runtime later swaps only this.
1369
+ strictMcpConfig: true,
1320
1370
  env: buildEnv(req.settings),
1321
1371
  // Do not inherit the user's own Claude Code project settings into bot turns.
1322
1372
  settingSources: [],
@@ -1324,23 +1374,40 @@ async function* runTurn(req) {
1324
1374
  };
1325
1375
  if (req.resumeSessionId) options.resume = req.resumeSessionId;
1326
1376
  if (req.skillPluginPath) {
1327
- options.plugins = [{ type: "local", path: req.skillPluginPath }];
1377
+ options.plugins = [{ type: "local", path: req.skillPluginPath, skipMcpDiscovery: true }];
1328
1378
  options.skills = req.enabledSkills ?? [];
1329
1379
  }
1380
+ const pending = [];
1381
+ options.onElicitation = async (request) => {
1382
+ if (request.mode === "url" && request.url) {
1383
+ pending.push({ kind: "signin", signin: { serverName: request.serverName, url: request.url } });
1384
+ return { action: "accept" };
1385
+ }
1386
+ log4.warn(`declined a ${request.mode ?? "form"} elicitation from "${request.serverName}": ${request.message}`);
1387
+ return { action: "decline" };
1388
+ };
1330
1389
  let q;
1331
1390
  try {
1332
1391
  q = query({ prompt: req.prompt, options });
1333
- } catch (err) {
1334
- yield { kind: "error", message: err instanceof Error ? err.message : String(err) };
1392
+ } catch (err2) {
1393
+ yield { kind: "error", message: err2 instanceof Error ? err2.message : String(err2) };
1335
1394
  return;
1336
1395
  }
1337
1396
  const seenToolIds = /* @__PURE__ */ new Set();
1338
1397
  try {
1339
1398
  for await (const msg of q) {
1399
+ while (pending.length) yield pending.shift();
1340
1400
  const m = msg;
1341
1401
  switch (m.type) {
1342
1402
  case "system":
1343
1403
  if (m.subtype === "init" && m.session_id) yield { kind: "session", sessionId: m.session_id };
1404
+ if (m.subtype === "elicitation_complete" && typeof m.mcp_server_name === "string") {
1405
+ try {
1406
+ await q.reconnectMcpServer(m.mcp_server_name);
1407
+ } catch (err2) {
1408
+ log4.warn(`reconnect of "${m.mcp_server_name}" after sign-in failed: ${err2.message}`);
1409
+ }
1410
+ }
1344
1411
  if (m.subtype === "init" && Array.isArray(m.mcp_servers)) {
1345
1412
  yield { kind: "mcp_status", mcpStatus: m.mcp_servers };
1346
1413
  }
@@ -1395,13 +1462,13 @@ async function* runTurn(req) {
1395
1462
  break;
1396
1463
  }
1397
1464
  }
1398
- } catch (err) {
1465
+ } catch (err2) {
1399
1466
  if (abort.signal.aborted) {
1400
1467
  yield { kind: "error", message: "Interrupted." };
1401
1468
  return;
1402
1469
  }
1403
- log4.error("turn failed", err);
1404
- yield { kind: "error", message: err instanceof Error ? err.message : String(err) };
1470
+ log4.error("turn failed", err2);
1471
+ yield { kind: "error", message: err2 instanceof Error ? err2.message : String(err2) };
1405
1472
  }
1406
1473
  }
1407
1474
 
@@ -1484,8 +1551,8 @@ var NullAutoReviewer = class {
1484
1551
  function makeAutoReviewer(getSettings, cwd) {
1485
1552
  try {
1486
1553
  return new HaikuAutoReviewer(getSettings, cwd);
1487
- } catch (err) {
1488
- log5.warn("falling back to null reviewer", err);
1554
+ } catch (err2) {
1555
+ log5.warn("falling back to null reviewer", err2);
1489
1556
  return new NullAutoReviewer();
1490
1557
  }
1491
1558
  }
@@ -1748,8 +1815,8 @@ ${lines.join("\n")}` }] };
1748
1815
  text: `Installed: ${names}.${note} A skill must still be assigned to you in Bot settings before you can invoke it \u2014 ask the human to enable it, then retry.`
1749
1816
  }]
1750
1817
  };
1751
- } catch (err) {
1752
- const e = err;
1818
+ } catch (err2) {
1819
+ const e = err2;
1753
1820
  if (e.name === "MultipleSkillsError" && Array.isArray(e.names)) {
1754
1821
  const listed = e.names.slice(0, 40).join(", ");
1755
1822
  const more = e.names.length > 40 ? `, and ${e.names.length - 40} more` : "";
@@ -1782,8 +1849,8 @@ Pick the one you need and install just it, e.g. ${args.source.replace(/^https?:\
1782
1849
  return { content: [{ type: "text", text: `No skill with slug "${args.slug}". Installed: ${known || "none"}.` }] };
1783
1850
  }
1784
1851
  return { content: [{ type: "text", text: `Removed "${args.slug}"${res.name ? ` (${res.name})` : ""}.` }] };
1785
- } catch (err) {
1786
- return { content: [{ type: "text", text: `Remove failed: ${err.message}` }] };
1852
+ } catch (err2) {
1853
+ return { content: [{ type: "text", text: `Remove failed: ${err2.message}` }] };
1787
1854
  }
1788
1855
  }
1789
1856
  ),
@@ -1834,13 +1901,12 @@ Pick the one you need and install just it, e.g. ${args.source.replace(/^https?:\
1834
1901
  isGroup
1835
1902
  });
1836
1903
  let text = "";
1837
- let ok = true;
1904
+ let ok2 = true;
1838
1905
  let errorMessage = "";
1839
1906
  const toolCards = /* @__PURE__ */ new Map();
1840
1907
  const mcpServers = { antbot: this.buildToolServer(bot, job.threadId, job.hops) };
1841
1908
  const browser = this.deps.browserTools?.(bot.id);
1842
1909
  if (browser) mcpServers.browser = browser;
1843
- if (connectors) Object.assign(mcpServers, connectors.servers);
1844
1910
  try {
1845
1911
  for await (const ev of runTurn({
1846
1912
  prompt: job.prompt,
@@ -1851,6 +1917,9 @@ Pick the one you need and install just it, e.g. ${args.source.replace(/^https?:\
1851
1917
  settings,
1852
1918
  abortController: abort,
1853
1919
  mcpServers,
1920
+ // Names are validated against RESERVED_CONNECTOR_NAMES, so these cannot clobber the two
1921
+ // in-process servers above. Mounted by the runtime adapter, not here.
1922
+ connectors: connectors?.servers ?? {},
1854
1923
  skillPluginPath: this.deps.skillPluginPath?.(),
1855
1924
  // Skill names come from SKILL.md frontmatter, which is what the SDK matches on.
1856
1925
  enabledSkills: botSkills.map((s) => s.name),
@@ -1888,17 +1957,17 @@ Pick the one you need and install just it, e.g. ${args.source.replace(/^https?:\
1888
1957
  if (ev.kind === "text" && ev.text) text += ev.text;
1889
1958
  if (ev.kind === "done") {
1890
1959
  if (ev.text && !text.trim()) text = ev.text;
1891
- ok = !ev.isError;
1960
+ ok2 = !ev.isError;
1892
1961
  }
1893
1962
  if (ev.kind === "error") {
1894
- ok = false;
1963
+ ok2 = false;
1895
1964
  errorMessage = ev.message ?? "Unknown error";
1896
1965
  }
1897
1966
  }
1898
- } catch (err) {
1899
- ok = false;
1900
- errorMessage = err instanceof Error ? err.message : String(err);
1901
- log6.error("turn crashed", err);
1967
+ } catch (err2) {
1968
+ ok2 = false;
1969
+ errorMessage = err2 instanceof Error ? err2.message : String(err2);
1970
+ log6.error("turn crashed", err2);
1902
1971
  } finally {
1903
1972
  this.running.delete(job.botId);
1904
1973
  }
@@ -1912,7 +1981,7 @@ Pick the one you need and install just it, e.g. ${args.source.replace(/^https?:\
1912
1981
  const interrupted = abort.signal.aborted;
1913
1982
  this.setState(job.botId, interrupted ? "interrupted" : "idle", "unread");
1914
1983
  if (interrupted) this.setState(job.botId, "idle", "unread");
1915
- job.onDone?.(final || errorMessage, ok && !interrupted);
1984
+ job.onDone?.(final || errorMessage, ok2 && !interrupted);
1916
1985
  void this.drain();
1917
1986
  }
1918
1987
  async applyEvent(ev, ctx) {
@@ -1926,6 +1995,10 @@ Pick the one you need and install just it, e.g. ${args.source.replace(/^https?:\
1926
1995
  // simply behaves as though the connector were never assigned. Surfacing the SDK's own
1927
1996
  // verdict is the difference between "my bot ignores my connector" and a stated reason.
1928
1997
  case "mcp_status": {
1998
+ for (const m of ev.mcpStatus ?? []) {
1999
+ if (m.name === "antbot" || m.name === "browser") continue;
2000
+ store.setConnectorStatusByName(m.name, m.status, m.error ?? null);
2001
+ }
1929
2002
  const bad = (ev.mcpStatus ?? []).filter((m) => m.status !== "connected");
1930
2003
  if (!bad.length) break;
1931
2004
  for (const m of bad) {
@@ -1941,6 +2014,16 @@ Pick the one you need and install just it, e.g. ${args.source.replace(/^https?:\
1941
2014
  });
1942
2015
  break;
1943
2016
  }
2017
+ // Mid-turn sign-in: the link goes into the thread as a card, where the human is looking,
2018
+ // and it persists — a toast would be gone before they came back from the browser.
2019
+ case "signin": {
2020
+ if (!ev.signin) break;
2021
+ const card = { type: "signin", serverName: ev.signin.serverName, url: ev.signin.url };
2022
+ const idx = store.appendCard(msgId, card);
2023
+ bus.publish({ type: "message.card", threadId: job.threadId, botId: bot.id, messageId: msgId, card, cardIndex: idx });
2024
+ store.setConnectorStatusByName(ev.signin.serverName, "needs-sign-in", null);
2025
+ break;
2026
+ }
1944
2027
  case "text":
1945
2028
  if (ev.text) {
1946
2029
  const cur = store.getMessage(msgId);
@@ -2009,7 +2092,7 @@ function describeMcpStatus(status) {
2009
2092
  case "needs-auth":
2010
2093
  return "needs authentication \u2014 the server rejected the credentials it was given (or was given none)";
2011
2094
  case "failed":
2012
- return "failed to start \u2014 check the command or URL with `antbot connector test`";
2095
+ return "failed to start \u2014 run `antbot mcp check <name>` to see why";
2013
2096
  case "pending":
2014
2097
  return "did not finish connecting in time";
2015
2098
  case "disabled":
@@ -2222,8 +2305,8 @@ async function discoverAuth(mcpUrl, headers = {}) {
2222
2305
  signal: AbortSignal.timeout(FETCH_TIMEOUT_MS)
2223
2306
  });
2224
2307
  challenge = res.headers.get("www-authenticate");
2225
- } catch (err) {
2226
- throw new OAuthError(`Could not reach ${mcpUrl}: ${err.message}`);
2308
+ } catch (err2) {
2309
+ throw new OAuthError(`Could not reach ${mcpUrl}: ${err2.message}`);
2227
2310
  }
2228
2311
  const found = await firstResourceMetadata(mcpUrl, challenge);
2229
2312
  if (!found) {
@@ -2310,13 +2393,16 @@ var clientSecretName = (connectorName) => `antbot:oauth-client:${connectorName}`
2310
2393
  var redirectUri = (port) => `http://127.0.0.1:${port}/api/connectors/oauth/callback`;
2311
2394
  var LOGIN_TTL_MS = 10 * 60 * 1e3;
2312
2395
  var ConnectorAuthService = class {
2313
- constructor(secrets, port) {
2396
+ constructor(secrets, portOf) {
2314
2397
  this.secrets = secrets;
2315
- this.port = port;
2398
+ this.portOf = portOf;
2316
2399
  }
2317
2400
  secrets;
2318
- port;
2401
+ portOf;
2319
2402
  pending = /* @__PURE__ */ new Map();
2403
+ get port() {
2404
+ return this.portOf();
2405
+ }
2320
2406
  /** Has this connector been signed in? Names only — never reads a value to answer. */
2321
2407
  isAuthorized(connectorName) {
2322
2408
  return this.secrets.list().includes(tokenSecretName(connectorName));
@@ -2342,8 +2428,8 @@ var ConnectorAuthService = class {
2342
2428
  async forgetClient(connectorName) {
2343
2429
  await this.secrets.remove(clientSecretName(connectorName));
2344
2430
  }
2345
- async readClient(connectorName) {
2346
- const key = clientSecretName(connectorName);
2431
+ async readClient(clientKey) {
2432
+ const key = clientSecretName(clientKey);
2347
2433
  const found = (await this.secrets.resolve([key])).get(key);
2348
2434
  if (!found) return null;
2349
2435
  try {
@@ -2364,49 +2450,76 @@ var ConnectorAuthService = class {
2364
2450
  throw new OAuthError("Sign-in applies to http and sse connectors; a stdio server takes its credentials in env.");
2365
2451
  }
2366
2452
  const discovery = await discoverAuth(connector.config.url);
2453
+ const authorizeUrl = await this.beginLoginWith(
2454
+ {
2455
+ connectorId: connector.id,
2456
+ connectorName: connector.name,
2457
+ clientKey: connector.name,
2458
+ authorizationEndpoint: discovery.authServer.authorizationEndpoint,
2459
+ tokenEndpoint: discovery.authServer.tokenEndpoint,
2460
+ registrationEndpoint: discovery.authServer.registrationEndpoint,
2461
+ resource: discovery.resource.resource,
2462
+ scopes: opts.scopes?.length ? opts.scopes : discovery.resource.scopesSupported,
2463
+ // Without these Google issues no refresh token, and the connector dies in an hour. Harmless
2464
+ // for providers that ignore them.
2465
+ extras: { access_type: "offline", prompt: "consent" }
2466
+ },
2467
+ opts
2468
+ );
2469
+ return { authorizeUrl, discovery };
2470
+ }
2471
+ /**
2472
+ * Begin a sign-in against a known authorization server. Used directly by built-in connectors,
2473
+ * whose provider endpoints are fixed and whose client credentials are shared under one
2474
+ * `clientKey` — one Google client serves Gmail, Calendar and Drive.
2475
+ */
2476
+ async beginLoginWith(target, opts = {}) {
2367
2477
  const redirect = redirectUri(this.port);
2368
- const remembered = await this.readClient(connector.name);
2478
+ const remembered = await this.readClient(target.clientKey);
2369
2479
  let clientId = opts.clientId ?? remembered?.clientId;
2370
2480
  let clientSecret = opts.clientSecret ?? (opts.clientId ? void 0 : remembered?.clientSecret);
2371
- if (!clientId && discovery.authServer.registrationEndpoint) {
2372
- const registered = await registerClient(discovery.authServer.registrationEndpoint, redirect);
2481
+ if (!clientId && target.registrationEndpoint) {
2482
+ const registered = await registerClient(target.registrationEndpoint, redirect);
2373
2483
  clientId = registered?.clientId;
2374
2484
  clientSecret = registered?.clientSecret;
2375
2485
  }
2376
2486
  if (!clientId) {
2487
+ const who = target.providerName ?? new URL(target.authorizationEndpoint).host;
2377
2488
  throw new OAuthError(
2378
- `${new URL(discovery.authServer.authorizationEndpoint).host} does not support automatic app registration, so it needs a client ID you create yourself. Register one with that provider, add "${redirect}" as an authorised redirect URI, and pass the client ID with --client-id.`
2489
+ `${who} does not support automatic app registration, so it needs a client ID you create yourself. Register one with that provider, add "${redirect}" as an authorised redirect URI, and pass the client ID with --client-id.`
2379
2490
  );
2380
2491
  }
2381
2492
  if (opts.clientId || opts.clientSecret || !remembered) {
2382
- await this.secrets.set(clientSecretName(connector.name), JSON.stringify({ clientId, clientSecret }));
2493
+ await this.secrets.set(clientSecretName(target.clientKey), JSON.stringify({ clientId, clientSecret }));
2383
2494
  }
2384
2495
  const pkce = createPkce();
2385
2496
  const state = crypto2.randomBytes(16).toString("base64url");
2386
2497
  this.pending.set(state, {
2387
- connectorId: connector.id,
2388
- connectorName: connector.name,
2498
+ connectorId: target.connectorId,
2499
+ connectorName: target.connectorName,
2389
2500
  verifier: pkce.verifier,
2390
2501
  clientId,
2391
2502
  clientSecret,
2392
- tokenEndpoint: discovery.authServer.tokenEndpoint,
2393
- resource: discovery.resource.resource,
2503
+ tokenEndpoint: target.tokenEndpoint,
2504
+ resource: target.resource,
2394
2505
  redirectUri: redirect,
2395
2506
  startedAt: Date.now()
2396
2507
  });
2397
2508
  this.sweep();
2398
- const authorizeUrl = buildAuthorizeUrl({
2399
- authorizationEndpoint: discovery.authServer.authorizationEndpoint,
2509
+ return buildAuthorizeUrl({
2510
+ authorizationEndpoint: target.authorizationEndpoint,
2400
2511
  clientId,
2401
2512
  redirectUri: redirect,
2402
- scopes: opts.scopes?.length ? opts.scopes : discovery.resource.scopesSupported,
2513
+ scopes: target.scopes,
2403
2514
  state,
2404
2515
  challenge: pkce.challenge,
2405
- resource: discovery.resource.resource,
2406
- // Without these Google issues no refresh token, and the connector dies in an hour.
2407
- extra: { access_type: "offline", prompt: "consent" }
2516
+ resource: target.resource,
2517
+ extra: target.extras
2408
2518
  });
2409
- return { authorizeUrl, discovery };
2519
+ }
2520
+ /** Whether client credentials are on file for a key (a connector name or a provider key). */
2521
+ hasClient(clientKey) {
2522
+ return this.secrets.list().includes(clientSecretName(clientKey));
2410
2523
  }
2411
2524
  /** Finish a sign-in from the redirect. Returns the connector that was authorised. */
2412
2525
  async completeLogin(state, code) {
@@ -2424,14 +2537,14 @@ var ConnectorAuthService = class {
2424
2537
  redirectUri: p.redirectUri,
2425
2538
  resource: p.resource
2426
2539
  });
2427
- } catch (err) {
2428
- const message = err.message;
2540
+ } catch (err2) {
2541
+ const message = err2.message;
2429
2542
  if (/client_secret/i.test(message)) {
2430
2543
  throw new OAuthError(
2431
2544
  `This provider requires a client secret as well as a client ID. Add the secret from the same OAuth client (in Google's console: the client's "Client secret") and sign in again.`
2432
2545
  );
2433
2546
  }
2434
- throw err;
2547
+ throw err2;
2435
2548
  }
2436
2549
  await this.write(p.connectorName, tokens);
2437
2550
  log7.info(`connector "${p.connectorName}" signed in`);
@@ -2449,8 +2562,8 @@ var ConnectorAuthService = class {
2449
2562
  try {
2450
2563
  tokens = await refreshTokens(tokens);
2451
2564
  await this.write(connectorName, tokens);
2452
- } catch (err) {
2453
- log7.warn(`could not refresh tokens for "${connectorName}": ${err.message}`);
2565
+ } catch (err2) {
2566
+ log7.warn(`could not refresh tokens for "${connectorName}": ${err2.message}`);
2454
2567
  return null;
2455
2568
  }
2456
2569
  }
@@ -2462,6 +2575,532 @@ var ConnectorAuthService = class {
2462
2575
  }
2463
2576
  };
2464
2577
 
2578
+ // daemon/src/connectors/builtin/service.ts
2579
+ import crypto3 from "node:crypto";
2580
+
2581
+ // daemon/src/connectors/builtin/gmail.ts
2582
+ import { z as z2 } from "zod";
2583
+
2584
+ // daemon/src/connectors/builtin/mcpServer.ts
2585
+ var MCP_PROTOCOL_VERSION = "2025-06-18";
2586
+ var RPC = {
2587
+ PARSE_ERROR: -32700,
2588
+ INVALID_REQUEST: -32600,
2589
+ METHOD_NOT_FOUND: -32601,
2590
+ INVALID_PARAMS: -32602
2591
+ };
2592
+ var err = (id, code, message) => ({ jsonrpc: "2.0", id, error: { code, message } });
2593
+ var ok = (id, result) => ({ jsonrpc: "2.0", id, result });
2594
+ var toolError = (text) => ({ content: [{ type: "text", text }], isError: true });
2595
+ var toolText = (text) => ({ content: [{ type: "text", text }] });
2596
+ async function handleMcpRequest(body, server, ctx) {
2597
+ const req = body;
2598
+ if (!req || typeof req !== "object" || req.jsonrpc !== "2.0" || typeof req.method !== "string") {
2599
+ return err(null, RPC.INVALID_REQUEST, "Expected a JSON-RPC 2.0 request");
2600
+ }
2601
+ const id = req.id ?? null;
2602
+ if (req.id === void 0) return null;
2603
+ switch (req.method) {
2604
+ case "initialize": {
2605
+ const asked = String(req.params?.protocolVersion ?? MCP_PROTOCOL_VERSION);
2606
+ return ok(id, {
2607
+ // Echo the client's version when it is one we can serve; the surface is small enough that
2608
+ // every revision since 2024-11-05 is compatible for these three methods.
2609
+ protocolVersion: asked,
2610
+ capabilities: { tools: {} },
2611
+ serverInfo: { name: server.name, version: server.version }
2612
+ });
2613
+ }
2614
+ case "ping":
2615
+ return ok(id, {});
2616
+ case "tools/list":
2617
+ return ok(id, {
2618
+ tools: server.tools.map((t) => ({ name: t.name, description: t.description, inputSchema: t.inputSchema }))
2619
+ });
2620
+ case "tools/call": {
2621
+ const name = String(req.params?.name ?? "");
2622
+ const tool2 = server.tools.find((t) => t.name === name);
2623
+ if (!tool2) return err(id, RPC.INVALID_PARAMS, `Unknown tool: ${name}`);
2624
+ const parsed = tool2.parse.safeParse(req.params?.arguments ?? {});
2625
+ if (!parsed.success) {
2626
+ return err(id, RPC.INVALID_PARAMS, `Invalid arguments for ${name}: ${parsed.error.issues[0]?.message ?? "invalid"}`);
2627
+ }
2628
+ let context;
2629
+ try {
2630
+ context = await ctx();
2631
+ } catch (e) {
2632
+ return ok(id, toolError(e.message));
2633
+ }
2634
+ try {
2635
+ return ok(id, await tool2.handler(parsed.data, context));
2636
+ } catch (e) {
2637
+ return ok(id, toolError(`${name} failed: ${e.message}`));
2638
+ }
2639
+ }
2640
+ default:
2641
+ return err(id, RPC.METHOD_NOT_FOUND, `Method not supported: ${req.method}`);
2642
+ }
2643
+ }
2644
+
2645
+ // daemon/src/connectors/builtin/gmail.ts
2646
+ var API = "https://gmail.googleapis.com/gmail/v1/users/me";
2647
+ async function gmail(fetchFn, ctx, path12, init = {}) {
2648
+ const res = await fetchFn(`${API}${path12}`, {
2649
+ ...init,
2650
+ headers: { Authorization: `Bearer ${ctx.accessToken}`, "content-type": "application/json", ...init.headers ?? {} }
2651
+ });
2652
+ const text = await res.text();
2653
+ let body = null;
2654
+ try {
2655
+ body = text ? JSON.parse(text) : null;
2656
+ } catch {
2657
+ }
2658
+ if (!res.ok) {
2659
+ const msg = body?.error?.message ?? `HTTP ${res.status}`;
2660
+ const hint = res.status === 401 || res.status === 403 ? " \u2014 sign in to the gmail connector again" : "";
2661
+ return { ok: false, error: `Gmail: ${msg}${hint}` };
2662
+ }
2663
+ return { ok: true, body };
2664
+ }
2665
+ function summarizeMessage(m) {
2666
+ const headers = {};
2667
+ for (const h of m?.payload?.headers ?? []) {
2668
+ const k = String(h.name).toLowerCase();
2669
+ if (["from", "to", "cc", "subject", "date"].includes(k)) headers[k] = String(h.value);
2670
+ }
2671
+ return {
2672
+ id: m?.id,
2673
+ threadId: m?.threadId,
2674
+ labelIds: m?.labelIds ?? [],
2675
+ snippet: m?.snippet ?? "",
2676
+ ...headers,
2677
+ body: extractText(m?.payload) ?? ""
2678
+ };
2679
+ }
2680
+ function extractText(payload) {
2681
+ if (!payload) return null;
2682
+ const decode = (data) => Buffer.from(data.replace(/-/g, "+").replace(/_/g, "/"), "base64").toString("utf8");
2683
+ if (payload.mimeType === "text/plain" && payload.body?.data) return decode(payload.body.data);
2684
+ if (Array.isArray(payload.parts)) {
2685
+ for (const p of payload.parts) {
2686
+ const t = extractText(p);
2687
+ if (t) return t;
2688
+ }
2689
+ }
2690
+ if (payload.mimeType === "text/html" && payload.body?.data) {
2691
+ return decode(payload.body.data).replace(/<style[\s\S]*?<\/style>/gi, "").replace(/<[^>]+>/g, " ").replace(/\s+/g, " ").trim();
2692
+ }
2693
+ return null;
2694
+ }
2695
+ function buildRawMessage(m) {
2696
+ const lines = [
2697
+ `To: ${m.to}`,
2698
+ ...m.cc ? [`Cc: ${m.cc}`] : [],
2699
+ `Subject: ${m.subject}`,
2700
+ ...m.inReplyTo ? [`In-Reply-To: ${m.inReplyTo}`, `References: ${m.inReplyTo}`] : [],
2701
+ "Content-Type: text/plain; charset=utf-8",
2702
+ "MIME-Version: 1.0",
2703
+ "",
2704
+ m.body
2705
+ ];
2706
+ return Buffer.from(lines.join("\r\n"), "utf8").toString("base64url");
2707
+ }
2708
+ var json = (v) => toolText(JSON.stringify(v, null, 2));
2709
+ function gmailTools(fetchFn = fetch) {
2710
+ return [
2711
+ {
2712
+ name: "search_threads",
2713
+ description: 'Search the mailbox with Gmail query syntax (e.g. "is:unread", "from:alice newer_than:7d"). Returns thread ids with a snippet of the latest message.',
2714
+ inputSchema: {
2715
+ type: "object",
2716
+ properties: { query: { type: "string" }, maxResults: { type: "integer", minimum: 1, maximum: 50 } },
2717
+ required: ["query"]
2718
+ },
2719
+ parse: z2.object({ query: z2.string().min(1), maxResults: z2.number().int().min(1).max(50).default(10) }),
2720
+ handler: async (a, ctx) => {
2721
+ const r = await gmail(fetchFn, ctx, `/threads?q=${encodeURIComponent(a.query)}&maxResults=${a.maxResults}`);
2722
+ if (!r.ok) return toolError(r.error);
2723
+ return json({ threads: (r.body.threads ?? []).map((t) => ({ id: t.id, snippet: t.snippet })), estimate: r.body.resultSizeEstimate });
2724
+ }
2725
+ },
2726
+ {
2727
+ name: "get_thread",
2728
+ description: "Read every message in a thread: from, to, subject, date and a plain-text body.",
2729
+ inputSchema: { type: "object", properties: { threadId: { type: "string" } }, required: ["threadId"] },
2730
+ parse: z2.object({ threadId: z2.string().min(1) }),
2731
+ handler: async (a, ctx) => {
2732
+ const r = await gmail(fetchFn, ctx, `/threads/${encodeURIComponent(a.threadId)}?format=full`);
2733
+ if (!r.ok) return toolError(r.error);
2734
+ return json({ id: r.body.id, messages: (r.body.messages ?? []).map(summarizeMessage) });
2735
+ }
2736
+ },
2737
+ {
2738
+ name: "get_message",
2739
+ description: "Read one message by id.",
2740
+ inputSchema: { type: "object", properties: { messageId: { type: "string" } }, required: ["messageId"] },
2741
+ parse: z2.object({ messageId: z2.string().min(1) }),
2742
+ handler: async (a, ctx) => {
2743
+ const r = await gmail(fetchFn, ctx, `/messages/${encodeURIComponent(a.messageId)}?format=full`);
2744
+ if (!r.ok) return toolError(r.error);
2745
+ return json(summarizeMessage(r.body));
2746
+ }
2747
+ },
2748
+ {
2749
+ name: "list_labels",
2750
+ description: "List the mailbox labels (INBOX, SENT, user labels\u2026) with their ids.",
2751
+ inputSchema: { type: "object", properties: {} },
2752
+ parse: z2.object({}),
2753
+ handler: async (_a, ctx) => {
2754
+ const r = await gmail(fetchFn, ctx, "/labels");
2755
+ if (!r.ok) return toolError(r.error);
2756
+ return json({ labels: (r.body.labels ?? []).map((l) => ({ id: l.id, name: l.name, type: l.type })) });
2757
+ }
2758
+ },
2759
+ {
2760
+ name: "create_draft",
2761
+ description: "Create a draft email. Nothing is sent. Set inReplyTo to a message id to draft a reply in that thread.",
2762
+ inputSchema: {
2763
+ type: "object",
2764
+ properties: { to: { type: "string" }, subject: { type: "string" }, body: { type: "string" }, cc: { type: "string" }, inReplyTo: { type: "string" } },
2765
+ required: ["to", "subject", "body"]
2766
+ },
2767
+ parse: z2.object({ to: z2.string().min(1), subject: z2.string(), body: z2.string(), cc: z2.string().optional(), inReplyTo: z2.string().optional() }),
2768
+ handler: async (a, ctx) => {
2769
+ const r = await gmail(fetchFn, ctx, "/drafts", { method: "POST", body: JSON.stringify({ message: { raw: buildRawMessage(a) } }) });
2770
+ if (!r.ok) return toolError(r.error);
2771
+ return json({ draftId: r.body.id, messageId: r.body.message?.id });
2772
+ }
2773
+ },
2774
+ {
2775
+ name: "send_message",
2776
+ description: "Send an email immediately. This is consequential and asks the human for approval.",
2777
+ inputSchema: {
2778
+ type: "object",
2779
+ properties: { to: { type: "string" }, subject: { type: "string" }, body: { type: "string" }, cc: { type: "string" }, inReplyTo: { type: "string" } },
2780
+ required: ["to", "subject", "body"]
2781
+ },
2782
+ parse: z2.object({ to: z2.string().min(1), subject: z2.string(), body: z2.string(), cc: z2.string().optional(), inReplyTo: z2.string().optional() }),
2783
+ handler: async (a, ctx) => {
2784
+ const r = await gmail(fetchFn, ctx, "/messages/send", { method: "POST", body: JSON.stringify({ raw: buildRawMessage(a) }) });
2785
+ if (!r.ok) return toolError(r.error);
2786
+ return json({ sent: true, messageId: r.body.id, threadId: r.body.threadId });
2787
+ }
2788
+ }
2789
+ ];
2790
+ }
2791
+
2792
+ // daemon/src/connectors/builtin/catalog.ts
2793
+ var GOOGLE = {
2794
+ key: "google",
2795
+ displayName: "Google",
2796
+ authorizationEndpoint: "https://accounts.google.com/o/oauth2/v2/auth",
2797
+ tokenEndpoint: "https://oauth2.googleapis.com/token",
2798
+ authorizeExtras: { access_type: "offline", prompt: "consent" },
2799
+ dynamicRegistration: false,
2800
+ setupSteps: [
2801
+ "Open console.cloud.google.com \u2192 APIs & Services \u2192 Credentials \u2192 Create credentials \u2192 OAuth client ID.",
2802
+ 'Application type: Web application. Under "Authorised redirect URIs" add exactly: {redirectUri}',
2803
+ "Enable the Gmail API for the project (APIs & Services \u2192 Library).",
2804
+ "Copy the Client ID and Client secret it shows you. One client works for every Google connector."
2805
+ ]
2806
+ };
2807
+ var BUILTIN_CATALOG = {
2808
+ gmail: {
2809
+ name: "gmail",
2810
+ displayName: "Gmail",
2811
+ description: "Read, search and draft email in the signed-in Gmail account.",
2812
+ provider: GOOGLE,
2813
+ // modify covers read + draft + send; asking for less means a second consent screen later.
2814
+ scopes: ["https://www.googleapis.com/auth/gmail.modify"],
2815
+ tools: gmailTools
2816
+ }
2817
+ };
2818
+
2819
+ // daemon/src/connectors/builtin/service.ts
2820
+ var BuiltinService = class {
2821
+ constructor(auth, portOf, version) {
2822
+ this.auth = auth;
2823
+ this.portOf = portOf;
2824
+ this.version = version;
2825
+ }
2826
+ auth;
2827
+ portOf;
2828
+ version;
2829
+ /** Rotates every boot. Checked on every `/mcp/<name>` request. */
2830
+ bearer = crypto3.randomBytes(24).toString("base64url");
2831
+ get port() {
2832
+ return this.portOf();
2833
+ }
2834
+ get(name) {
2835
+ return BUILTIN_CATALOG[name];
2836
+ }
2837
+ /** The config a built-in connector's row stores: the daemon's own endpoint, nothing secret. */
2838
+ rowConfig(name) {
2839
+ return { transport: "http", url: `http://127.0.0.1:${this.port}/mcp/${name}`, headers: {} };
2840
+ }
2841
+ /** What actually gets mounted: the row's config plus this boot's bearer. */
2842
+ mountConfig(connector) {
2843
+ return {
2844
+ type: "http",
2845
+ url: `http://127.0.0.1:${this.port}/mcp/${connector.name}`,
2846
+ headers: { Authorization: `Bearer ${this.bearer}` }
2847
+ };
2848
+ }
2849
+ authorized(name) {
2850
+ return this.auth?.isAuthorized(name) ?? false;
2851
+ }
2852
+ /** Constant-time compare so the bearer cannot be guessed a byte at a time. */
2853
+ checkBearer(header) {
2854
+ const given = (header ?? "").replace(/^Bearer\s+/i, "");
2855
+ const a = Buffer.from(given);
2856
+ const b2 = Buffer.from(this.bearer);
2857
+ return a.length === b2.length && crypto3.timingSafeEqual(a, b2);
2858
+ }
2859
+ /** Serve one MCP request for a built-in connector. */
2860
+ async handle(name, body) {
2861
+ const def = this.get(name);
2862
+ if (!def) return { jsonrpc: "2.0", id: null, error: { code: -32601, message: `No built-in connector named ${name}` } };
2863
+ return handleMcpRequest(body, { name: def.name, version: this.version, tools: def.tools() }, async () => {
2864
+ const hdr = await this.auth?.authHeader(name);
2865
+ if (!hdr) {
2866
+ throw new Error(
2867
+ `${def.displayName} is not signed in. Sign in on the Connectors screen or with \`antbot mcp login ${name}\`.`
2868
+ );
2869
+ }
2870
+ return { accessToken: hdr.Authorization.replace(/^Bearer\s+/, "") };
2871
+ });
2872
+ }
2873
+ /** Start the provider sign-in for a built-in connector. Returns the URL to open. */
2874
+ async beginLogin(connector, opts = {}) {
2875
+ const def = this.get(connector.name);
2876
+ if (!def) throw new Error(`No built-in connector named ${connector.name}`);
2877
+ if (!this.auth) throw new Error("Secrets backend unavailable, so a sign-in cannot be stored.");
2878
+ const p = def.provider;
2879
+ return this.auth.beginLoginWith(
2880
+ {
2881
+ connectorId: connector.id,
2882
+ connectorName: connector.name,
2883
+ clientKey: p.key,
2884
+ authorizationEndpoint: p.authorizationEndpoint,
2885
+ tokenEndpoint: p.tokenEndpoint,
2886
+ scopes: def.scopes,
2887
+ extras: p.authorizeExtras,
2888
+ providerName: p.displayName
2889
+ },
2890
+ opts
2891
+ );
2892
+ }
2893
+ };
2894
+
2895
+ // daemon/src/bots/mcpProbe.ts
2896
+ import { spawn } from "node:child_process";
2897
+ var log8 = logger("mcp-probe");
2898
+ var PROTOCOL_VERSION = "2025-06-18";
2899
+ var DEFAULT_TIMEOUT_MS = 1e4;
2900
+ var MAX_DESCRIPTION = 200;
2901
+ function parseToolsResult(result) {
2902
+ const tools = result?.tools;
2903
+ if (!Array.isArray(tools)) return [];
2904
+ return tools.filter((t) => typeof t === "object" && t !== null).map((t) => ({
2905
+ name: String(t.name ?? ""),
2906
+ description: String(t.description ?? "").slice(0, MAX_DESCRIPTION)
2907
+ })).filter((t) => t.name.length > 0);
2908
+ }
2909
+ var rpc = (id, method, params) => `${JSON.stringify({ jsonrpc: "2.0", id, method, ...params ? { params } : {} })}
2910
+ `;
2911
+ var notify = (method) => `${JSON.stringify({ jsonrpc: "2.0", method })}
2912
+ `;
2913
+ var failed = (error) => ({ ok: false, tools: [], error });
2914
+ async function probeStdio(cfg, timeoutMs) {
2915
+ return new Promise((resolve) => {
2916
+ let child;
2917
+ try {
2918
+ child = spawn(cfg.command, cfg.args ?? [], {
2919
+ // The server's own env plus the connector's — a connector that needs PATH still gets it.
2920
+ env: { ...process.env, ...cfg.env ?? {} },
2921
+ stdio: ["pipe", "pipe", "pipe"]
2922
+ });
2923
+ } catch (err2) {
2924
+ return resolve(failed(err2.message));
2925
+ }
2926
+ let settled = false;
2927
+ let stderr = "";
2928
+ let buffer = "";
2929
+ const finish = (r) => {
2930
+ if (settled) return;
2931
+ settled = true;
2932
+ clearTimeout(timer);
2933
+ try {
2934
+ child.kill("SIGKILL");
2935
+ } catch {
2936
+ }
2937
+ resolve(r);
2938
+ };
2939
+ const timer = setTimeout(
2940
+ () => finish(failed(`timed out after ${timeoutMs}ms${stderr ? `: ${stderr.trim().slice(0, 200)}` : ""}`)),
2941
+ timeoutMs
2942
+ );
2943
+ child.on("error", (err2) => finish(failed(err2.message)));
2944
+ child.on(
2945
+ "exit",
2946
+ (code) => finish(failed(`server exited with code ${code}${stderr ? `: ${stderr.trim().slice(0, 200)}` : ""}`))
2947
+ );
2948
+ child.stderr?.on("data", (d) => {
2949
+ stderr += d.toString();
2950
+ });
2951
+ child.stdout?.on("data", (d) => {
2952
+ buffer += d.toString();
2953
+ const lines = buffer.split("\n");
2954
+ buffer = lines.pop() ?? "";
2955
+ for (const line of lines) {
2956
+ if (!line.trim()) continue;
2957
+ let msg;
2958
+ try {
2959
+ msg = JSON.parse(line);
2960
+ } catch {
2961
+ continue;
2962
+ }
2963
+ if (msg.id === 1) {
2964
+ try {
2965
+ child.stdin?.write(notify("notifications/initialized"));
2966
+ child.stdin?.write(rpc(2, "tools/list"));
2967
+ } catch (err2) {
2968
+ finish(failed(err2.message));
2969
+ }
2970
+ } else if (msg.id === 2) {
2971
+ if (msg.error) return finish(failed(JSON.stringify(msg.error).slice(0, 200)));
2972
+ finish({ ok: true, tools: parseToolsResult(msg.result) });
2973
+ }
2974
+ }
2975
+ });
2976
+ try {
2977
+ child.stdin?.write(
2978
+ rpc(1, "initialize", {
2979
+ protocolVersion: PROTOCOL_VERSION,
2980
+ capabilities: {},
2981
+ clientInfo: { name: "ant-bot", version: "1.0.0" }
2982
+ })
2983
+ );
2984
+ } catch (err2) {
2985
+ finish(failed(err2.message));
2986
+ }
2987
+ });
2988
+ }
2989
+ async function probeHttp(cfg, timeoutMs) {
2990
+ const ac = new AbortController();
2991
+ const timer = setTimeout(() => ac.abort(), timeoutMs);
2992
+ const base = {
2993
+ "content-type": "application/json",
2994
+ accept: "application/json, text/event-stream",
2995
+ ...cfg.headers ?? {}
2996
+ };
2997
+ const readBody = async (res) => {
2998
+ const text = await res.text();
2999
+ const line = text.split("\n").find((l) => l.startsWith("data:"));
3000
+ try {
3001
+ return JSON.parse(line ? line.slice(5).trim() : text);
3002
+ } catch {
3003
+ return null;
3004
+ }
3005
+ };
3006
+ try {
3007
+ const initRes = await fetch(cfg.url, {
3008
+ method: "POST",
3009
+ signal: ac.signal,
3010
+ headers: base,
3011
+ body: rpc(1, "initialize", {
3012
+ protocolVersion: PROTOCOL_VERSION,
3013
+ capabilities: {},
3014
+ clientInfo: { name: "ant-bot", version: "1.0.0" }
3015
+ })
3016
+ });
3017
+ if (!initRes.ok) return failed(`initialize returned HTTP ${initRes.status}`);
3018
+ const session = initRes.headers.get("mcp-session-id");
3019
+ const withSession = session ? { ...base, "mcp-session-id": session } : base;
3020
+ await readBody(initRes);
3021
+ await fetch(cfg.url, { method: "POST", signal: ac.signal, headers: withSession, body: notify("notifications/initialized") }).catch(() => void 0);
3022
+ const listRes = await fetch(cfg.url, {
3023
+ method: "POST",
3024
+ signal: ac.signal,
3025
+ headers: withSession,
3026
+ body: rpc(2, "tools/list")
3027
+ });
3028
+ if (!listRes.ok) return failed(`tools/list returned HTTP ${listRes.status}`);
3029
+ const body = await readBody(listRes);
3030
+ if (body?.error) return failed(JSON.stringify(body.error).slice(0, 200));
3031
+ return { ok: true, tools: parseToolsResult(body?.result) };
3032
+ } catch (err2) {
3033
+ const e = err2;
3034
+ return failed(e.name === "AbortError" ? `timed out after ${timeoutMs}ms` : e.message);
3035
+ } finally {
3036
+ clearTimeout(timer);
3037
+ }
3038
+ }
3039
+ async function probeConnector(config, opts = {}) {
3040
+ const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
3041
+ const type = config.type;
3042
+ try {
3043
+ if (type === "stdio") {
3044
+ return await probeStdio(config, timeoutMs);
3045
+ }
3046
+ if (type === "http") {
3047
+ return await probeHttp(config, timeoutMs);
3048
+ }
3049
+ if (type === "sse") return failed("testing is not supported for sse connectors \u2014 assign it to a bot and run a turn");
3050
+ return failed(`unknown transport: ${String(type)}`);
3051
+ } catch (err2) {
3052
+ log8.warn("probe threw", err2);
3053
+ return failed(err2.message);
3054
+ }
3055
+ }
3056
+
3057
+ // daemon/src/connectors/check.ts
3058
+ function decideCheck(signals) {
3059
+ if (signals.builtinProvider) {
3060
+ return signals.builtinSignedIn ? { status: "ready", tools: signals.probe?.tools ?? [], provider: signals.builtinProvider.name } : {
3061
+ status: "needs-sign-in",
3062
+ selfRegistration: signals.builtinProvider.dynamicRegistration,
3063
+ provider: signals.builtinProvider.name,
3064
+ tools: signals.probe?.tools ?? []
3065
+ };
3066
+ }
3067
+ if (signals.missingSecrets.length) {
3068
+ return { status: "needs-credential", tools: [], detail: `missing secret(s): ${signals.missingSecrets.join(", ")}` };
3069
+ }
3070
+ if (signals.challenge === "auth") {
3071
+ const as = signals.discovery?.authServer;
3072
+ return {
3073
+ status: "needs-sign-in",
3074
+ selfRegistration: Boolean(as?.registrationEndpoint),
3075
+ provider: as ? new URL(as.authorizationEndpoint).host : void 0,
3076
+ tools: signals.probe?.tools ?? []
3077
+ };
3078
+ }
3079
+ if (signals.challenge === "unreachable" || signals.probe && !signals.probe.ok) {
3080
+ return { status: "unreachable", tools: [], detail: signals.probe?.error };
3081
+ }
3082
+ return { status: "ready", tools: signals.probe?.tools ?? [] };
3083
+ }
3084
+ async function gatherCustomSignals(connector, mounted, missingSecrets) {
3085
+ if (missingSecrets.length || !mounted) return { probe: null, challenge: "none", missingSecrets };
3086
+ const probe = await probeConnector(mounted, { timeoutMs: 8e3 });
3087
+ if (mounted.type === "stdio") return { probe, challenge: probe.ok ? "none" : "unreachable", missingSecrets };
3088
+ if (!probe.ok) return { probe, challenge: "unreachable", missingSecrets };
3089
+ try {
3090
+ const discovery = await discoverAuth(mounted.url, mounted.headers);
3091
+ return { probe, challenge: "auth", discovery, missingSecrets };
3092
+ } catch (err2) {
3093
+ const msg = err2.message;
3094
+ if (err2 instanceof OAuthError && /did not advertise an authorization server/.test(msg)) {
3095
+ return { probe, challenge: "auth", discovery: null, missingSecrets };
3096
+ }
3097
+ return { probe, challenge: "none", missingSecrets };
3098
+ }
3099
+ }
3100
+
3101
+ // daemon/src/app.ts
3102
+ import { fileURLToPath } from "node:url";
3103
+
2465
3104
  // daemon/src/config/config.ts
2466
3105
  import fs6 from "node:fs";
2467
3106
  import { parse as parseToml, stringify as stringifyToml } from "smol-toml";
@@ -2538,11 +3177,11 @@ function writeConfig(cfg) {
2538
3177
  // daemon/src/permissions/secrets.ts
2539
3178
  import fs7 from "node:fs";
2540
3179
  import path7 from "node:path";
2541
- import crypto3 from "node:crypto";
3180
+ import crypto4 from "node:crypto";
2542
3181
  import { execFile } from "node:child_process";
2543
3182
  import { promisify } from "node:util";
2544
3183
  var exec = promisify(execFile);
2545
- var log8 = logger("secrets");
3184
+ var log9 = logger("secrets");
2546
3185
  var SERVICE = "ant-bot";
2547
3186
  var SecretToolBackend = class {
2548
3187
  name = "libsecret (secret-tool)";
@@ -2551,7 +3190,7 @@ var SecretToolBackend = class {
2551
3190
  const p = execFile(
2552
3191
  "secret-tool",
2553
3192
  ["store", "--label", `${SERVICE}: ${key}`, "service", SERVICE, "account", key],
2554
- (err) => err ? reject(err) : resolve()
3193
+ (err2) => err2 ? reject(err2) : resolve()
2555
3194
  );
2556
3195
  p.stdin?.end(value);
2557
3196
  });
@@ -2609,7 +3248,7 @@ var EncryptedFileBackend = class {
2609
3248
  key() {
2610
3249
  if (!fs7.existsSync(this.keyFile)) {
2611
3250
  fs7.mkdirSync(path7.dirname(this.keyFile), { recursive: true });
2612
- fs7.writeFileSync(this.keyFile, crypto3.randomBytes(32), { mode: 384 });
3251
+ fs7.writeFileSync(this.keyFile, crypto4.randomBytes(32), { mode: 384 });
2613
3252
  }
2614
3253
  return fs7.readFileSync(this.keyFile);
2615
3254
  }
@@ -2620,7 +3259,7 @@ var EncryptedFileBackend = class {
2620
3259
  const key = this.key();
2621
3260
  const out = {};
2622
3261
  for (const [k, v] of Object.entries(raw)) {
2623
- const d = crypto3.createDecipheriv("aes-256-gcm", key, Buffer.from(v.iv, "base64"));
3262
+ const d = crypto4.createDecipheriv("aes-256-gcm", key, Buffer.from(v.iv, "base64"));
2624
3263
  d.setAuthTag(Buffer.from(v.tag, "base64"));
2625
3264
  out[k] = Buffer.concat([d.update(Buffer.from(v.data, "base64")), d.final()]).toString("utf8");
2626
3265
  }
@@ -2633,8 +3272,8 @@ var EncryptedFileBackend = class {
2633
3272
  const key = this.key();
2634
3273
  const out = {};
2635
3274
  for (const [k, v] of Object.entries(values)) {
2636
- const iv = crypto3.randomBytes(12);
2637
- const c = crypto3.createCipheriv("aes-256-gcm", key, iv);
3275
+ const iv = crypto4.randomBytes(12);
3276
+ const c = crypto4.createCipheriv("aes-256-gcm", key, iv);
2638
3277
  const data = Buffer.concat([c.update(v, "utf8"), c.final()]);
2639
3278
  out[k] = { iv: iv.toString("base64"), tag: c.getAuthTag().toString("base64"), data: data.toString("base64") };
2640
3279
  }
@@ -2663,13 +3302,13 @@ async function pickBackend(fallbackFile) {
2663
3302
  try {
2664
3303
  await exec(bin, args);
2665
3304
  return true;
2666
- } catch (err) {
2667
- return err.code !== "ENOENT";
3305
+ } catch (err2) {
3306
+ return err2.code !== "ENOENT";
2668
3307
  }
2669
3308
  };
2670
3309
  if (process.platform === "darwin" && await has("security", ["-h"])) return new MacKeychainBackend();
2671
3310
  if (process.platform === "linux" && await has("secret-tool", ["--version"])) return new SecretToolBackend();
2672
- log8.warn("no system keychain available; using the encrypted-file fallback");
3311
+ log9.warn("no system keychain available; using the encrypted-file fallback");
2673
3312
  return new EncryptedFileBackend(fallbackFile);
2674
3313
  }
2675
3314
  var SecretsService = class {
@@ -2741,12 +3380,12 @@ var SecretsService = class {
2741
3380
  };
2742
3381
 
2743
3382
  // daemon/src/app.ts
2744
- var log9 = logger("app");
3383
+ var log10 = logger("app");
2745
3384
  async function optionalImport(name, load) {
2746
3385
  try {
2747
3386
  return await load();
2748
- } catch (err) {
2749
- log9.warn(`${name} module could not be loaded`, err.message);
3387
+ } catch (err2) {
3388
+ log10.warn(`${name} module could not be loaded`, err2.message);
2750
3389
  return null;
2751
3390
  }
2752
3391
  }
@@ -2775,7 +3414,9 @@ async function createApp(opts = {}) {
2775
3414
  manager: void 0,
2776
3415
  lastUserActivity: { at: Date.now() },
2777
3416
  shutdown: async () => {
2778
- }
3417
+ },
3418
+ mountConnector: async () => null,
3419
+ checkConnector: async () => ({ status: "unreachable", tools: [] })
2779
3420
  };
2780
3421
  app.manager = new BotManager({
2781
3422
  store,
@@ -2820,41 +3461,84 @@ async function createApp(opts = {}) {
2820
3461
  */
2821
3462
  connectorServers: async (botId) => {
2822
3463
  const assigned = store.listBotConnectors(botId);
2823
- if (!assigned.length) return { servers: {}, mounted: [] };
2824
- const available = new Set(app.secrets?.list() ?? []);
2825
- const { mount, skipped } = planConnectorMount(assigned, available);
2826
- for (const s of skipped) {
2827
- log9.warn(`connector "${s.connector.name}" not mounted \u2014 missing secret(s): ${s.missing.join(", ")}`);
2828
- }
2829
3464
  const servers = {};
2830
3465
  const mounted = [];
2831
- for (const connector of mount) {
2832
- const refs = extractSecretRefs(connector.config);
2833
- try {
2834
- const secrets = refs.length ? await app.secrets.resolve(refs) : /* @__PURE__ */ new Map();
2835
- const built = buildMcpServerConfig(connector, secrets);
2836
- const auth = await app.connectorAuth?.authHeader(connector.name);
2837
- if (auth && built.headers && !("Authorization" in built.headers)) {
2838
- built.headers = { ...built.headers, ...auth };
2839
- }
2840
- servers[connector.name] = built;
2841
- mounted.push({ name: connector.name, description: connector.description });
2842
- } catch (err) {
2843
- log9.warn(`connector "${connector.name}" not mounted`, err.message);
2844
- }
3466
+ for (const connector of assigned) {
3467
+ const built = await app.mountConnector(connector);
3468
+ if (!built) continue;
3469
+ servers[connector.name] = built;
3470
+ mounted.push({ name: connector.name, description: connector.description });
2845
3471
  }
2846
3472
  return { servers, mounted };
2847
3473
  }
2848
3474
  });
3475
+ app.mountConnector = async (connector) => {
3476
+ if (connector.kind === "builtin") {
3477
+ if (!app.builtin?.get(connector.name)) return null;
3478
+ return app.builtin.mountConfig(connector);
3479
+ }
3480
+ const available = new Set(app.secrets?.list() ?? []);
3481
+ const { skipped } = planConnectorMount([connector], available);
3482
+ if (skipped.length) {
3483
+ const missing = skipped[0].missing;
3484
+ log10.warn(`connector "${connector.name}" not mounted \u2014 missing secret(s): ${missing.join(", ")}`);
3485
+ store.setConnectorStatus(connector.id, "needs-credential", `missing secret(s): ${missing.join(", ")}`);
3486
+ return null;
3487
+ }
3488
+ try {
3489
+ const refs = extractSecretRefs(connector.config);
3490
+ const secrets = refs.length ? await app.secrets.resolve(refs) : /* @__PURE__ */ new Map();
3491
+ const built = buildMcpServerConfig(connector, secrets);
3492
+ const auth = await app.connectorAuth?.authHeader(connector.name);
3493
+ if (auth && built.type !== "stdio" && !("Authorization" in built.headers)) {
3494
+ built.headers = { ...built.headers, ...auth };
3495
+ }
3496
+ return built;
3497
+ } catch (err2) {
3498
+ log10.warn(`connector "${connector.name}" not mounted`, err2.message);
3499
+ store.setConnectorStatus(connector.id, "needs-credential", err2.message);
3500
+ return null;
3501
+ }
3502
+ };
3503
+ app.checkConnector = async (connector) => {
3504
+ let verdict;
3505
+ if (connector.kind === "builtin") {
3506
+ const def = app.builtin?.get(connector.name);
3507
+ const tools = def ? def.tools().map((t) => ({ name: t.name, description: t.description })) : [];
3508
+ verdict = decideCheck({
3509
+ probe: def ? { ok: true, tools } : null,
3510
+ challenge: "none",
3511
+ missingSecrets: [],
3512
+ builtinSignedIn: app.builtin?.authorized(connector.name) ?? false,
3513
+ builtinProvider: def ? { name: def.provider.displayName, dynamicRegistration: def.provider.dynamicRegistration } : void 0
3514
+ });
3515
+ } else {
3516
+ const available = new Set(app.secrets?.list() ?? []);
3517
+ const missing = computeMissingSecrets(connector, available);
3518
+ const mounted = missing.length ? null : await app.mountConnector(connector);
3519
+ verdict = decideCheck(await gatherCustomSignals(connector, mounted, missing));
3520
+ }
3521
+ store.setConnectorStatus(connector.id, verdict.status, verdict.detail ?? null);
3522
+ return verdict;
3523
+ };
2849
3524
  try {
2850
3525
  app.secrets = new SecretsService(
2851
3526
  await pickBackend(cfg.paths.secrets),
2852
3527
  `${cfg.paths.secrets}.index`
2853
3528
  );
2854
- log9.info(`secrets backend: ${app.secrets.backendName}`);
2855
- app.connectorAuth = new ConnectorAuthService(app.secrets, cfg.port);
2856
- } catch (err) {
2857
- log9.warn("secrets backend unavailable", err.message);
3529
+ log10.info(`secrets backend: ${app.secrets.backendName}`);
3530
+ app.connectorAuth = new ConnectorAuthService(app.secrets, () => app.cfg.port);
3531
+ } catch (err2) {
3532
+ log10.warn("secrets backend unavailable", err2.message);
3533
+ }
3534
+ app.builtin = new BuiltinService(
3535
+ app.connectorAuth,
3536
+ () => app.cfg.port,
3537
+ readPackageVersion(path8.dirname(fileURLToPath(import.meta.url)), (p) => fs8.existsSync(p), (p) => fs8.readFileSync(p, "utf8"))
3538
+ );
3539
+ try {
3540
+ } catch (err2) {
3541
+ log10.warn("secrets backend unavailable", err2.message);
2858
3542
  }
2859
3543
  await wireSkills(app);
2860
3544
  await wireBrowser(app);
@@ -2880,12 +3564,12 @@ async function wireSkills(app) {
2880
3564
  const mod = await optionalImport("skills", () => import("./skills-JUHWFBD6.js"));
2881
3565
  const pluginMod = await optionalImport("skill plugin", () => import("./plugin-WYUCG6F7.js"));
2882
3566
  const Ctor = mod?.SkillStore ?? mod?.default;
2883
- if (!Ctor) return void log9.warn("skills subsystem unavailable: no SkillStore export");
3567
+ if (!Ctor) return void log10.warn("skills subsystem unavailable: no SkillStore export");
2884
3568
  const pluginRoot = app.cfg.paths.skills;
2885
3569
  if (pluginMod?.ensureSkillPlugin) {
2886
3570
  pluginMod.ensureSkillPlugin(pluginRoot);
2887
3571
  const moved = pluginMod.migrateLegacyLayout?.(pluginRoot) ?? [];
2888
- if (moved.length) log9.info(`migrated ${moved.length} skill(s) into the plugin layout: ${moved.join(", ")}`);
3572
+ if (moved.length) log10.info(`migrated ${moved.length} skill(s) into the plugin layout: ${moved.join(", ")}`);
2889
3573
  app.skillPluginPath = pluginRoot;
2890
3574
  }
2891
3575
  const filesDir = pluginMod?.skillFilesDir?.(pluginRoot) ?? pluginRoot;
@@ -2898,30 +3582,30 @@ async function wireSkills(app) {
2898
3582
  const installed = took("install");
2899
3583
  const updated = took("update");
2900
3584
  const kept = [...took("skip-modified"), ...took("skip-foreign")];
2901
- if (installed.length) log9.info(`installed ${installed.length} bundled skill(s): ${installed.join(", ")}`);
2902
- if (updated.length) log9.info(`updated ${updated.length} bundled skill(s): ${updated.join(", ")}`);
2903
- if (kept.length) log9.info(`left ${kept.length} locally-modified skill(s) alone: ${kept.join(", ")}`);
3585
+ if (installed.length) log10.info(`installed ${installed.length} bundled skill(s): ${installed.join(", ")}`);
3586
+ if (updated.length) log10.info(`updated ${updated.length} bundled skill(s): ${updated.join(", ")}`);
3587
+ if (kept.length) log10.info(`left ${kept.length} locally-modified skill(s) alone: ${kept.join(", ")}`);
2904
3588
  const written = [...installed, ...updated, ...took("adopt")];
2905
3589
  const renamed = app.skills?.refreshFromDisk?.(written) ?? [];
2906
- if (renamed.length) log9.info(`refreshed metadata for ${renamed.length} skill(s): ${renamed.join(", ")}`);
3590
+ if (renamed.length) log10.info(`refreshed metadata for ${renamed.length} skill(s): ${renamed.join(", ")}`);
2907
3591
  } catch (e) {
2908
- log9.warn("bundled skills not synced", e.message);
3592
+ log10.warn("bundled skills not synced", e.message);
2909
3593
  }
2910
3594
  }
2911
3595
  app.skills.syncFromDisk?.();
2912
3596
  const fixed = app.skills.reconcile?.();
2913
- if (fixed?.repaired.length) log9.info(`repaired ${fixed.repaired.length} skill path(s): ${fixed.repaired.join(", ")}`);
2914
- if (fixed?.removed.length) log9.info(`dropped ${fixed.removed.length} skill row(s) with no files on disk`);
2915
- log9.info(`skills ready (${app.store.listSkills().length} registered)`);
2916
- } catch (err) {
2917
- log9.warn("skills subsystem unavailable", err.message);
3597
+ if (fixed?.repaired.length) log10.info(`repaired ${fixed.repaired.length} skill path(s): ${fixed.repaired.join(", ")}`);
3598
+ if (fixed?.removed.length) log10.info(`dropped ${fixed.removed.length} skill row(s) with no files on disk`);
3599
+ log10.info(`skills ready (${app.store.listSkills().length} registered)`);
3600
+ } catch (err2) {
3601
+ log10.warn("skills subsystem unavailable", err2.message);
2918
3602
  }
2919
3603
  }
2920
3604
  async function wireBrowser(app) {
2921
3605
  try {
2922
3606
  const mod = await optionalImport("browser", () => import("./browser-NOWM4S6C.js"));
2923
3607
  const Ctor = mod?.BrowserService ?? mod?.default;
2924
- if (!Ctor) return void log9.warn("browser subsystem unavailable: no BrowserService export");
3608
+ if (!Ctor) return void log10.warn("browser subsystem unavailable: no BrowserService export");
2925
3609
  const svc = new Ctor({ profileDir: app.cfg.paths.browserProfile, bus: app.bus, headless: true });
2926
3610
  let toolsMod = null;
2927
3611
  toolsMod = await optionalImport("browser tools", () => import("./tools-YNE7ZRPR.js"));
@@ -2936,16 +3620,16 @@ async function wireBrowser(app) {
2936
3620
  return s;
2937
3621
  };
2938
3622
  app.browser = svc;
2939
- log9.info("browser computer service ready");
2940
- } catch (err) {
2941
- log9.warn("browser subsystem unavailable", err.message);
3623
+ log10.info("browser computer service ready");
3624
+ } catch (err2) {
3625
+ log10.warn("browser subsystem unavailable", err2.message);
2942
3626
  }
2943
3627
  }
2944
3628
  async function wireScheduler(app) {
2945
3629
  try {
2946
3630
  const mod = await optionalImport("scheduler", () => import("./scheduler-VZVHQWGD.js"));
2947
3631
  const Ctor = mod?.Scheduler ?? mod?.default;
2948
- if (!Ctor) return void log9.warn("scheduler subsystem unavailable: no Scheduler export");
3632
+ if (!Ctor) return void log10.warn("scheduler subsystem unavailable: no Scheduler export");
2949
3633
  app.scheduler = new Ctor({
2950
3634
  store: app.store,
2951
3635
  bus: app.bus,
@@ -2953,9 +3637,9 @@ async function wireScheduler(app) {
2953
3637
  getSettings: app.getSettings
2954
3638
  });
2955
3639
  app.scheduler.start?.();
2956
- log9.info(`scheduler started (${app.store.listRoutines().filter((r) => r.enabled).length} active routines)`);
2957
- } catch (err) {
2958
- log9.warn("scheduler subsystem unavailable", err.message);
3640
+ log10.info(`scheduler started (${app.store.listRoutines().filter((r) => r.enabled).length} active routines)`);
3641
+ } catch (err2) {
3642
+ log10.warn("scheduler subsystem unavailable", err2.message);
2959
3643
  }
2960
3644
  }
2961
3645
  function drainMailbox(app) {
@@ -2987,7 +3671,7 @@ function workspaceRelative(root, p) {
2987
3671
 
2988
3672
  // daemon/src/bots/groups.ts
2989
3673
  import { query as query3 } from "@anthropic-ai/claude-agent-sdk";
2990
- var log10 = logger("groups");
3674
+ var log11 = logger("groups");
2991
3675
  async function routeGroupMessage(args) {
2992
3676
  const { text, members, mentionBotIds, mentionEveryone, settings, cwd } = args;
2993
3677
  if (mentionEveryone) return members;
@@ -3025,8 +3709,8 @@ Which single teammate should own this? Reply with only the slug.`,
3025
3709
  const slug = out.trim().toLowerCase().replace(/[^a-z0-9-]/g, "");
3026
3710
  const found = members.find((m) => m.slug === slug);
3027
3711
  if (found) return [found];
3028
- } catch (err) {
3029
- log10.warn("group router failed; defaulting to first member", err);
3712
+ } catch (err2) {
3713
+ log11.warn("group router failed; defaulting to first member", err2);
3030
3714
  }
3031
3715
  return members.slice(0, 1);
3032
3716
  }
@@ -3037,13 +3721,13 @@ function parseMentions(text, members) {
3037
3721
  }
3038
3722
 
3039
3723
  // daemon/src/api/routes-core.ts
3040
- import fs8 from "node:fs";
3724
+ import fs9 from "node:fs";
3041
3725
  import path9 from "node:path";
3042
- import { fileURLToPath } from "node:url";
3726
+ import { fileURLToPath as fileURLToPath2 } from "node:url";
3043
3727
  var SERVER_VERSION = readPackageVersion(
3044
- path9.dirname(fileURLToPath(import.meta.url)),
3045
- (p) => fs8.existsSync(p),
3046
- (p) => fs8.readFileSync(p, "utf8")
3728
+ path9.dirname(fileURLToPath2(import.meta.url)),
3729
+ (p) => fs9.existsSync(p),
3730
+ (p) => fs9.readFileSync(p, "utf8")
3047
3731
  );
3048
3732
  function registerCoreRoutes(f, app) {
3049
3733
  const { store, bus, manager } = app;
@@ -3066,9 +3750,9 @@ function registerCoreRoutes(f, app) {
3066
3750
  if (!parsed.success) return reply.code(400).send({ error: parsed.error.issues[0]?.message ?? "Invalid body" });
3067
3751
  try {
3068
3752
  return store.createBot(parsed.data);
3069
- } catch (err) {
3070
- if (err instanceof LimitError) return reply.code(409).send({ error: err.message, code: err.code });
3071
- throw err;
3753
+ } catch (err2) {
3754
+ if (err2 instanceof LimitError) return reply.code(409).send({ error: err2.message, code: err2.code });
3755
+ throw err2;
3072
3756
  }
3073
3757
  });
3074
3758
  f.get("/api/bots/:id", async (req, reply) => {
@@ -3104,9 +3788,9 @@ function registerCoreRoutes(f, app) {
3104
3788
  try {
3105
3789
  const copy = store.duplicateBot(req.params.id);
3106
3790
  return copy ?? reply.code(404).send({ error: "No such bot" });
3107
- } catch (err) {
3108
- if (err instanceof LimitError) return reply.code(409).send({ error: err.message, code: err.code });
3109
- throw err;
3791
+ } catch (err2) {
3792
+ if (err2 instanceof LimitError) return reply.code(409).send({ error: err2.message, code: err2.code });
3793
+ throw err2;
3110
3794
  }
3111
3795
  });
3112
3796
  f.post("/api/bots/:id/stop", async (req) => ({
@@ -3159,9 +3843,9 @@ function registerCoreRoutes(f, app) {
3159
3843
  return reply.code(400).send({ error: "One or more bots do not exist" });
3160
3844
  const title = parsed.data.title || members.map((m) => m.name).join(", ");
3161
3845
  return store.createThread({ ...parsed.data, title });
3162
- } catch (err) {
3163
- if (err instanceof LimitError) return reply.code(409).send({ error: err.message, code: err.code });
3164
- throw err;
3846
+ } catch (err2) {
3847
+ if (err2 instanceof LimitError) return reply.code(409).send({ error: err2.message, code: err2.code });
3848
+ throw err2;
3165
3849
  }
3166
3850
  });
3167
3851
  f.get("/api/threads/:id", async (req, reply) => {
@@ -3202,9 +3886,9 @@ function registerCoreRoutes(f, app) {
3202
3886
  if (parsed.data.attachmentIds?.length) {
3203
3887
  try {
3204
3888
  store.attachToMessage(parsed.data.attachmentIds, msg.id);
3205
- } catch (err) {
3206
- if (err instanceof LimitError) return reply.code(409).send({ error: err.message, code: err.code });
3207
- throw err;
3889
+ } catch (err2) {
3890
+ if (err2 instanceof LimitError) return reply.code(409).send({ error: err2.message, code: err2.code });
3891
+ throw err2;
3208
3892
  }
3209
3893
  }
3210
3894
  bus.publish({ type: "message.created", threadId: thread.id, botId: null, message: store.getMessage(msg.id) });
@@ -3236,178 +3920,8 @@ ${attachments.map((a) => `- ${a.name} \u2192 ${a.path}`).join("\n")}` : "";
3236
3920
  }
3237
3921
 
3238
3922
  // daemon/src/api/routes-ops.ts
3239
- import fs9 from "node:fs";
3923
+ import fs10 from "node:fs";
3240
3924
  import path10 from "node:path";
3241
-
3242
- // daemon/src/bots/mcpProbe.ts
3243
- import { spawn } from "node:child_process";
3244
- var log11 = logger("mcp-probe");
3245
- var PROTOCOL_VERSION = "2025-06-18";
3246
- var DEFAULT_TIMEOUT_MS = 1e4;
3247
- var MAX_DESCRIPTION = 200;
3248
- function parseToolsResult(result) {
3249
- const tools = result?.tools;
3250
- if (!Array.isArray(tools)) return [];
3251
- return tools.filter((t) => typeof t === "object" && t !== null).map((t) => ({
3252
- name: String(t.name ?? ""),
3253
- description: String(t.description ?? "").slice(0, MAX_DESCRIPTION)
3254
- })).filter((t) => t.name.length > 0);
3255
- }
3256
- var rpc = (id, method, params) => `${JSON.stringify({ jsonrpc: "2.0", id, method, ...params ? { params } : {} })}
3257
- `;
3258
- var notify = (method) => `${JSON.stringify({ jsonrpc: "2.0", method })}
3259
- `;
3260
- var failed = (error) => ({ ok: false, tools: [], error });
3261
- async function probeStdio(cfg, timeoutMs) {
3262
- return new Promise((resolve) => {
3263
- let child;
3264
- try {
3265
- child = spawn(cfg.command, cfg.args ?? [], {
3266
- // The server's own env plus the connector's — a connector that needs PATH still gets it.
3267
- env: { ...process.env, ...cfg.env ?? {} },
3268
- stdio: ["pipe", "pipe", "pipe"]
3269
- });
3270
- } catch (err) {
3271
- return resolve(failed(err.message));
3272
- }
3273
- let settled = false;
3274
- let stderr = "";
3275
- let buffer = "";
3276
- const finish = (r) => {
3277
- if (settled) return;
3278
- settled = true;
3279
- clearTimeout(timer);
3280
- try {
3281
- child.kill("SIGKILL");
3282
- } catch {
3283
- }
3284
- resolve(r);
3285
- };
3286
- const timer = setTimeout(
3287
- () => finish(failed(`timed out after ${timeoutMs}ms${stderr ? `: ${stderr.trim().slice(0, 200)}` : ""}`)),
3288
- timeoutMs
3289
- );
3290
- child.on("error", (err) => finish(failed(err.message)));
3291
- child.on(
3292
- "exit",
3293
- (code) => finish(failed(`server exited with code ${code}${stderr ? `: ${stderr.trim().slice(0, 200)}` : ""}`))
3294
- );
3295
- child.stderr?.on("data", (d) => {
3296
- stderr += d.toString();
3297
- });
3298
- child.stdout?.on("data", (d) => {
3299
- buffer += d.toString();
3300
- const lines = buffer.split("\n");
3301
- buffer = lines.pop() ?? "";
3302
- for (const line of lines) {
3303
- if (!line.trim()) continue;
3304
- let msg;
3305
- try {
3306
- msg = JSON.parse(line);
3307
- } catch {
3308
- continue;
3309
- }
3310
- if (msg.id === 1) {
3311
- try {
3312
- child.stdin?.write(notify("notifications/initialized"));
3313
- child.stdin?.write(rpc(2, "tools/list"));
3314
- } catch (err) {
3315
- finish(failed(err.message));
3316
- }
3317
- } else if (msg.id === 2) {
3318
- if (msg.error) return finish(failed(JSON.stringify(msg.error).slice(0, 200)));
3319
- finish({ ok: true, tools: parseToolsResult(msg.result) });
3320
- }
3321
- }
3322
- });
3323
- try {
3324
- child.stdin?.write(
3325
- rpc(1, "initialize", {
3326
- protocolVersion: PROTOCOL_VERSION,
3327
- capabilities: {},
3328
- clientInfo: { name: "ant-bot", version: "1.0.0" }
3329
- })
3330
- );
3331
- } catch (err) {
3332
- finish(failed(err.message));
3333
- }
3334
- });
3335
- }
3336
- async function probeHttp(cfg, timeoutMs) {
3337
- const ac = new AbortController();
3338
- const timer = setTimeout(() => ac.abort(), timeoutMs);
3339
- const base = {
3340
- "content-type": "application/json",
3341
- accept: "application/json, text/event-stream",
3342
- ...cfg.headers ?? {}
3343
- };
3344
- const readBody = async (res) => {
3345
- const text = await res.text();
3346
- const line = text.split("\n").find((l) => l.startsWith("data:"));
3347
- try {
3348
- return JSON.parse(line ? line.slice(5).trim() : text);
3349
- } catch {
3350
- return null;
3351
- }
3352
- };
3353
- try {
3354
- const initRes = await fetch(cfg.url, {
3355
- method: "POST",
3356
- signal: ac.signal,
3357
- headers: base,
3358
- body: rpc(1, "initialize", {
3359
- protocolVersion: PROTOCOL_VERSION,
3360
- capabilities: {},
3361
- clientInfo: { name: "ant-bot", version: "1.0.0" }
3362
- })
3363
- });
3364
- if (!initRes.ok) return failed(`initialize returned HTTP ${initRes.status}`);
3365
- const session = initRes.headers.get("mcp-session-id");
3366
- const withSession = session ? { ...base, "mcp-session-id": session } : base;
3367
- await readBody(initRes);
3368
- await fetch(cfg.url, { method: "POST", signal: ac.signal, headers: withSession, body: notify("notifications/initialized") }).catch(() => void 0);
3369
- const listRes = await fetch(cfg.url, {
3370
- method: "POST",
3371
- signal: ac.signal,
3372
- headers: withSession,
3373
- body: rpc(2, "tools/list")
3374
- });
3375
- if (!listRes.ok) return failed(`tools/list returned HTTP ${listRes.status}`);
3376
- const body = await readBody(listRes);
3377
- if (body?.error) return failed(JSON.stringify(body.error).slice(0, 200));
3378
- return { ok: true, tools: parseToolsResult(body?.result) };
3379
- } catch (err) {
3380
- const e = err;
3381
- return failed(e.name === "AbortError" ? `timed out after ${timeoutMs}ms` : e.message);
3382
- } finally {
3383
- clearTimeout(timer);
3384
- }
3385
- }
3386
- async function probeConnector(config, opts = {}) {
3387
- const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
3388
- const type = config.type;
3389
- try {
3390
- if (type === "stdio") {
3391
- return await probeStdio(config, timeoutMs);
3392
- }
3393
- if (type === "http") {
3394
- const result = await probeHttp(config, timeoutMs);
3395
- const headers = config.headers ?? {};
3396
- const authed = Object.keys(headers).some((h) => /^(authorization|x-api-key|api-key)$/i.test(h));
3397
- if (result.ok && !authed) {
3398
- result.authHint = "This server was reached without any credential. If it needs one, the tools will list here but a bot will still see none \u2014 add an Authorization header, e.g. {{secret:NAME}}.";
3399
- }
3400
- return result;
3401
- }
3402
- if (type === "sse") return failed("testing is not supported for sse connectors \u2014 assign it to a bot and run a turn");
3403
- return failed(`unknown transport: ${String(type)}`);
3404
- } catch (err) {
3405
- log11.warn("probe threw", err);
3406
- return failed(err.message);
3407
- }
3408
- }
3409
-
3410
- // daemon/src/api/routes-ops.ts
3411
3925
  function registerOpsRoutes(f, app) {
3412
3926
  const { store, gateway, bus } = app;
3413
3927
  f.get("/api/approvals", async () => store.listPendingApprovals());
@@ -3441,35 +3955,74 @@ function registerOpsRoutes(f, app) {
3441
3955
  store.deleteRule(rule.id);
3442
3956
  return { ok: true };
3443
3957
  });
3444
- f.get("/api/connectors", async () => {
3445
- const available = new Set(app.secrets?.list() ?? []);
3446
- return store.listConnectors().map((c) => ({
3447
- ...c,
3448
- missingSecrets: computeMissingSecrets(c, available),
3449
- // Names only — knowing a connector is signed in never requires reading its token.
3450
- signedIn: app.connectorAuth?.isAuthorized(c.name) ?? false
3451
- }));
3958
+ const describe = (c) => ({
3959
+ ...c,
3960
+ missingSecrets: computeMissingSecrets(c, new Set(app.secrets?.list() ?? [])),
3961
+ // Names only — knowing a connector is signed in never requires reading its token.
3962
+ signedIn: app.connectorAuth?.isAuthorized(c.name) ?? false
3452
3963
  });
3964
+ f.get("/api/connectors", async () => store.listConnectors().map(describe));
3965
+ f.get(
3966
+ "/api/connectors/catalog",
3967
+ async () => Object.values(BUILTIN_CATALOG).map((b2) => ({
3968
+ name: b2.name,
3969
+ displayName: b2.displayName,
3970
+ description: b2.description,
3971
+ provider: b2.provider.displayName,
3972
+ needsClientCredentials: !b2.provider.dynamicRegistration,
3973
+ setupSteps: b2.provider.setupSteps.map((step) => step.replace("{redirectUri}", redirectUri(app.cfg.port)))
3974
+ }))
3975
+ );
3453
3976
  f.post("/api/connectors", async (req, reply) => {
3454
3977
  const parsed = CreateConnectorRequest.safeParse(req.body);
3455
3978
  if (!parsed.success) return reply.code(400).send({ error: parsed.error.issues[0]?.message ?? "Invalid body" });
3456
- if (store.getConnectorByName(parsed.data.name)) {
3457
- return reply.code(409).send({ error: `A connector named "${parsed.data.name}" already exists` });
3979
+ const body = parsed.data;
3980
+ if (store.getConnectorByName(body.name)) {
3981
+ return reply.code(409).send({ error: `A connector named "${body.name}" already exists` });
3458
3982
  }
3459
- return store.createConnector(parsed.data);
3983
+ let created;
3984
+ if (body.builtin) {
3985
+ const def = app.builtin?.get(body.builtin);
3986
+ if (!def) return reply.code(400).send({ error: `No built-in connector named "${body.builtin}"` });
3987
+ if (body.name !== def.name) return reply.code(400).send({ error: `The built-in ${def.name} connector must be named "${def.name}"` });
3988
+ created = store.createConnector({
3989
+ name: def.name,
3990
+ description: body.description || def.description,
3991
+ config: app.builtin.rowConfig(def.name),
3992
+ kind: "builtin",
3993
+ enabled: body.enabled
3994
+ });
3995
+ } else {
3996
+ created = store.createConnector({ name: body.name, description: body.description, config: body.config, enabled: body.enabled });
3997
+ }
3998
+ for (const botId of body.botIds ?? []) {
3999
+ if (!store.getBot(botId)) continue;
4000
+ const current = store.listBotConnectors(botId).map((c) => c.id);
4001
+ store.setBotConnectors(botId, [.../* @__PURE__ */ new Set([...current, created.id])]);
4002
+ }
4003
+ const check = await app.checkConnector(created);
4004
+ return { ...describe(store.getConnector(created.id)), check };
3460
4005
  });
3461
4006
  f.patch("/api/connectors/:id", async (req, reply) => {
3462
4007
  const parsed = UpdateConnectorRequest.safeParse(req.body);
3463
4008
  if (!parsed.success) return reply.code(400).send({ error: "Invalid body" });
3464
- const updated = store.updateConnector(req.params.id, parsed.data);
3465
- if (!updated) return reply.code(404).send({ error: "No such connector" });
3466
- return updated;
4009
+ const existing = store.getConnector(req.params.id);
4010
+ if (!existing) return reply.code(404).send({ error: "No such connector" });
4011
+ if (existing.kind === "builtin" && parsed.data.config) return reply.code(400).send({ error: "A built-in connector has no editable config" });
4012
+ return describe(store.updateConnector(req.params.id, parsed.data));
3467
4013
  });
3468
4014
  f.delete("/api/connectors/:id", async (req, reply) => {
3469
- if (!store.getConnector(req.params.id)) return reply.code(404).send({ error: "No such connector" });
4015
+ const c = store.getConnector(req.params.id);
4016
+ if (!c) return reply.code(404).send({ error: "No such connector" });
4017
+ await app.connectorAuth?.signOut(c.name);
3470
4018
  store.deleteConnector(req.params.id);
3471
4019
  return { ok: true };
3472
4020
  });
4021
+ f.post("/api/connectors/:id/check", async (req, reply) => {
4022
+ const connector = store.getConnector(req.params.id);
4023
+ if (!connector) return reply.code(404).send({ error: "No such connector" });
4024
+ return app.checkConnector(connector);
4025
+ });
3473
4026
  f.post(
3474
4027
  "/api/connectors/:id/login",
3475
4028
  async (req, reply) => {
@@ -3477,10 +4030,10 @@ function registerOpsRoutes(f, app) {
3477
4030
  if (!connector) return reply.code(404).send({ error: "No such connector" });
3478
4031
  if (!app.connectorAuth) return reply.code(503).send({ error: "Secrets backend unavailable, so sign-in cannot be stored" });
3479
4032
  try {
3480
- const { authorizeUrl } = await app.connectorAuth.beginLogin(connector, req.body ?? {});
4033
+ const authorizeUrl = connector.kind === "builtin" ? await app.builtin.beginLogin(connector, req.body ?? {}) : (await app.connectorAuth.beginLogin(connector, req.body ?? {})).authorizeUrl;
3481
4034
  return { authorizeUrl };
3482
- } catch (err) {
3483
- return reply.code(400).send({ error: err.message });
4035
+ } catch (err2) {
4036
+ return reply.code(400).send({ error: err2.message });
3484
4037
  }
3485
4038
  }
3486
4039
  );
@@ -3488,56 +4041,39 @@ function registerOpsRoutes(f, app) {
3488
4041
  const connector = store.getConnector(req.params.id);
3489
4042
  if (!connector) return reply.code(404).send({ error: "No such connector" });
3490
4043
  await app.connectorAuth?.signOut(connector.name);
4044
+ store.setConnectorStatus(connector.id, "needs-sign-in", null);
3491
4045
  return { ok: true };
3492
4046
  });
3493
4047
  f.get(
3494
4048
  "/api/connectors/oauth/callback",
3495
4049
  async (req, reply) => {
3496
- const page = (title, detail, ok) => `<!doctype html><meta charset=utf-8><title>${title}</title>
4050
+ const page = (title, detail, ok2) => `<!doctype html><meta charset=utf-8><title>${title}</title>
3497
4051
  <body style="font-family:system-ui;background:#0b0d10;color:#e6e8eb;padding:3rem;max-width:40rem">
3498
- <h1 style="color:${ok ? "#4ade80" : "#f87171"}">${title}</h1><p>${detail}</p>
4052
+ <h1 style="color:${ok2 ? "#4ade80" : "#f87171"}">${title}</h1><p>${detail}</p>
3499
4053
  <p style="color:#9aa4b2">You can close this tab and return to ant-bot.</p>`;
3500
4054
  const { code, state, error, error_description: desc } = req.query;
3501
- if (error) {
3502
- return reply.type("text/html").send(page("Sign-in failed", `${error}: ${desc ?? ""}`, false));
3503
- }
3504
- if (!code || !state) {
3505
- return reply.type("text/html").send(page("Sign-in failed", "The provider did not return a code.", false));
3506
- }
3507
- if (!app.connectorAuth) {
3508
- return reply.type("text/html").send(page("Sign-in failed", "The secrets backend is unavailable.", false));
3509
- }
4055
+ if (error) return reply.type("text/html").send(page("Sign-in failed", `${error}: ${desc ?? ""}`, false));
4056
+ if (!code || !state) return reply.type("text/html").send(page("Sign-in failed", "The provider did not return a code.", false));
4057
+ if (!app.connectorAuth) return reply.type("text/html").send(page("Sign-in failed", "The secrets backend is unavailable.", false));
3510
4058
  try {
3511
- const { connectorName } = await app.connectorAuth.completeLogin(state, code);
3512
- bus.publish({
3513
- type: "notify",
3514
- botId: null,
3515
- threadId: null,
3516
- title: "Connector signed in",
3517
- body: `${connectorName} is now authorised.`,
3518
- level: "info"
3519
- });
4059
+ const { connectorId, connectorName } = await app.connectorAuth.completeLogin(state, code);
4060
+ const row = store.getConnector(connectorId);
4061
+ if (row) await app.checkConnector(row);
4062
+ bus.publish({ type: "notify", botId: null, threadId: null, title: "Connector signed in", body: `${connectorName} is now authorised.`, level: "info" });
3520
4063
  return reply.type("text/html").send(page("Signed in", `<b>${connectorName}</b> is now authorised.`, true));
3521
- } catch (err) {
3522
- return reply.type("text/html").send(page("Sign-in failed", err.message, false));
4064
+ } catch (err2) {
4065
+ return reply.type("text/html").send(page("Sign-in failed", err2.message, false));
3523
4066
  }
3524
4067
  }
3525
4068
  );
3526
- f.post("/api/connectors/:id/test", async (req, reply) => {
3527
- const connector = store.getConnector(req.params.id);
3528
- if (!connector) return reply.code(404).send({ error: "No such connector" });
3529
- const refs = extractSecretRefs(connector.config);
3530
- const missing = computeMissingSecrets(connector, new Set(app.secrets?.list() ?? []));
3531
- if (missing.length) {
3532
- return { ok: false, tools: [], error: `missing secret(s): ${missing.join(", ")}` };
3533
- }
3534
- try {
3535
- const secrets = refs.length && app.secrets ? await app.secrets.resolve(refs) : /* @__PURE__ */ new Map();
3536
- return await probeConnector(buildMcpServerConfig(connector, secrets));
3537
- } catch (err) {
3538
- return { ok: false, tools: [], error: err.message };
3539
- }
4069
+ f.post("/mcp/:name", async (req, reply) => {
4070
+ if (!app.builtin) return reply.code(503).send({ error: "Built-in connectors unavailable" });
4071
+ if (!app.builtin.checkBearer(req.headers.authorization)) return reply.code(401).send({ error: "Unauthorized" });
4072
+ const res = await app.builtin.handle(req.params.name, req.body);
4073
+ if (res === null) return reply.code(202).send();
4074
+ return res;
3540
4075
  });
4076
+ f.delete("/mcp/:name", async () => ({ ok: true }));
3541
4077
  f.get("/api/skills", async () => store.listSkills());
3542
4078
  f.post("/api/skills", async (req, reply) => {
3543
4079
  const parsed = CreateSkillRequest.safeParse(req.body);
@@ -3559,8 +4095,8 @@ function registerOpsRoutes(f, app) {
3559
4095
  replaced: i2.replaced
3560
4096
  }))
3561
4097
  };
3562
- } catch (err) {
3563
- return reply.code(400).send({ error: err.message });
4098
+ } catch (err2) {
4099
+ return reply.code(400).send({ error: err2.message });
3564
4100
  }
3565
4101
  });
3566
4102
  f.get("/api/skills/:id", async (req, reply) => {
@@ -3604,9 +4140,9 @@ function registerOpsRoutes(f, app) {
3604
4140
  const routine = store.createRoutine({ ...parsed.data, timezone: parsed.data.timezone ?? app.getSettings().timezone });
3605
4141
  app.scheduler?.reload?.(routine.id);
3606
4142
  return routine;
3607
- } catch (err) {
3608
- if (err instanceof LimitError) return reply.code(409).send({ error: err.message, code: err.code });
3609
- throw err;
4143
+ } catch (err2) {
4144
+ if (err2 instanceof LimitError) return reply.code(409).send({ error: err2.message, code: err2.code });
4145
+ throw err2;
3610
4146
  }
3611
4147
  });
3612
4148
  f.patch("/api/routines/:id", async (req, reply) => {
@@ -3637,8 +4173,8 @@ function registerOpsRoutes(f, app) {
3637
4173
  const buf = await part.toBuffer();
3638
4174
  const safe = String(part.filename ?? "file").replace(/[^a-zA-Z0-9._-]/g, "_");
3639
4175
  const dest = path10.join(app.cfg.paths.attachments, `${Date.now()}-${safe}`);
3640
- fs9.mkdirSync(path10.dirname(dest), { recursive: true });
3641
- fs9.writeFileSync(dest, buf);
4176
+ fs10.mkdirSync(path10.dirname(dest), { recursive: true });
4177
+ fs10.writeFileSync(dest, buf);
3642
4178
  try {
3643
4179
  return store.createAttachment({
3644
4180
  messageId: null,
@@ -3647,16 +4183,16 @@ function registerOpsRoutes(f, app) {
3647
4183
  mime: part.mimetype ?? "application/octet-stream",
3648
4184
  bytes: buf.byteLength
3649
4185
  });
3650
- } catch (err) {
3651
- fs9.unlinkSync(dest);
3652
- if (err instanceof LimitError) return reply.code(413).send({ error: err.message, code: err.code });
3653
- throw err;
4186
+ } catch (err2) {
4187
+ fs10.unlinkSync(dest);
4188
+ if (err2 instanceof LimitError) return reply.code(413).send({ error: err2.message, code: err2.code });
4189
+ throw err2;
3654
4190
  }
3655
4191
  });
3656
4192
  f.get("/api/attachments/:id", async (req, reply) => {
3657
4193
  const a = store.getAttachment(req.params.id);
3658
- if (!a || !fs9.existsSync(a.path)) return reply.code(404).send({ error: "No such attachment" });
3659
- return reply.type(a.mime).send(fs9.createReadStream(a.path));
4194
+ if (!a || !fs10.existsSync(a.path)) return reply.code(404).send({ error: "No such attachment" });
4195
+ return reply.type(a.mime).send(fs10.createReadStream(a.path));
3660
4196
  });
3661
4197
  f.get("/api/usage", async () => {
3662
4198
  const rows = store.listUsage(0);
@@ -3723,12 +4259,12 @@ function registerOpsRoutes(f, app) {
3723
4259
  const root = app.cfg.paths.workspace;
3724
4260
  const target = workspaceRelative(root, req.query.path ?? ".");
3725
4261
  if (!target) return reply.code(400).send({ error: "Path is outside the workspace" });
3726
- if (!fs9.existsSync(target)) return [];
3727
- return fs9.readdirSync(target, { withFileTypes: true }).map((d) => {
4262
+ if (!fs10.existsSync(target)) return [];
4263
+ return fs10.readdirSync(target, { withFileTypes: true }).map((d) => {
3728
4264
  const full = path10.join(target, d.name);
3729
4265
  let bytes = 0;
3730
4266
  try {
3731
- bytes = d.isFile() ? fs9.statSync(full).size : 0;
4267
+ bytes = d.isFile() ? fs10.statSync(full).size : 0;
3732
4268
  } catch {
3733
4269
  }
3734
4270
  return { name: d.name, path: path10.relative(root, full), dir: d.isDirectory(), bytes };
@@ -3737,7 +4273,7 @@ function registerOpsRoutes(f, app) {
3737
4273
  f.get("/api/workspace/file", async (req, reply) => {
3738
4274
  const root = app.cfg.paths.workspace;
3739
4275
  const target = workspaceRelative(root, req.query.path ?? "");
3740
- if (!target || !fs9.existsSync(target) || !fs9.statSync(target).isFile())
4276
+ if (!target || !fs10.existsSync(target) || !fs10.statSync(target).isFile())
3741
4277
  return reply.code(404).send({ error: "No such file" });
3742
4278
  const ext = path10.extname(target).toLowerCase();
3743
4279
  const mime = {
@@ -3753,14 +4289,14 @@ function registerOpsRoutes(f, app) {
3753
4289
  ".pdf": "application/pdf",
3754
4290
  ".html": "text/html"
3755
4291
  };
3756
- return reply.type(mime[ext] ?? "application/octet-stream").send(fs9.createReadStream(target));
4292
+ return reply.type(mime[ext] ?? "application/octet-stream").send(fs10.createReadStream(target));
3757
4293
  });
3758
4294
  f.get("/api/computer/status", async () => {
3759
4295
  if (!app.browser?.status) return { available: false, reason: "Browser service not built", mode: "host", headless: true, pages: [] };
3760
4296
  try {
3761
4297
  return await app.browser.status();
3762
- } catch (err) {
3763
- return { available: false, reason: err.message, mode: "host", headless: true, pages: [] };
4298
+ } catch (err2) {
4299
+ return { available: false, reason: err2.message, mode: "host", headless: true, pages: [] };
3764
4300
  }
3765
4301
  });
3766
4302
  f.post("/api/computer/takeover", async (req, reply) => {
@@ -3779,7 +4315,7 @@ var log12 = logger("server");
3779
4315
  var require_ = createRequire(import.meta.url);
3780
4316
  function resolveWebDist() {
3781
4317
  return findWebDist(
3782
- nodeLocateDeps(path11.dirname(fileURLToPath2(import.meta.url)), (spec) => {
4318
+ nodeLocateDeps(path11.dirname(fileURLToPath3(import.meta.url)), (spec) => {
3783
4319
  try {
3784
4320
  return require_.resolve(spec);
3785
4321
  } catch {
@@ -3845,9 +4381,9 @@ async function startServer(opts = {}) {
3845
4381
  } catch {
3846
4382
  }
3847
4383
  });
3848
- } catch (err) {
4384
+ } catch (err2) {
3849
4385
  try {
3850
- socket.send(JSON.stringify({ type: "error", message: err.message }));
4386
+ socket.send(JSON.stringify({ type: "error", message: err2.message }));
3851
4387
  } catch {
3852
4388
  }
3853
4389
  socket.close();
@@ -3861,10 +4397,10 @@ async function startServer(opts = {}) {
3861
4397
  } catch {
3862
4398
  return;
3863
4399
  }
3864
- const fail = (err) => {
4400
+ const fail = (err2) => {
3865
4401
  try {
3866
4402
  if (socket.readyState === 1)
3867
- socket.send(JSON.stringify({ type: "input-error", message: err.message }));
4403
+ socket.send(JSON.stringify({ type: "input-error", message: err2.message }));
3868
4404
  } catch {
3869
4405
  }
3870
4406
  };
@@ -3908,14 +4444,15 @@ async function startServer(opts = {}) {
3908
4444
  });
3909
4445
  }
3910
4446
  }
3911
- fastify.setErrorHandler((err, _req, reply) => {
3912
- log12.error("request failed", err);
3913
- const e = err;
4447
+ fastify.setErrorHandler((err2, _req, reply) => {
4448
+ log12.error("request failed", err2);
4449
+ const e = err2;
3914
4450
  const code = e.statusCode && e.statusCode >= 400 ? e.statusCode : 500;
3915
4451
  reply.code(code).send({ error: e.message ?? "Internal error" });
3916
4452
  });
3917
4453
  const port = opts.port ?? app.cfg.port;
3918
4454
  const host = opts.host ?? app.cfg.host;
4455
+ app.cfg.port = port;
3919
4456
  await fastify.listen({ port, host });
3920
4457
  const url = `http://${host}:${port}`;
3921
4458
  const delivered = drainMailbox(app);