dsh-remote-control 1.2.0 → 2.0.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.
@@ -4501,7 +4501,7 @@ var require_websocket = __commonJS({
4501
4501
  var http = __require("http");
4502
4502
  var net = __require("net");
4503
4503
  var tls = __require("tls");
4504
- var { randomBytes: randomBytes5, createHash: createHash3 } = __require("crypto");
4504
+ var { randomBytes: randomBytes5, createHash: createHash4 } = __require("crypto");
4505
4505
  var { Duplex, Readable } = __require("stream");
4506
4506
  var { URL: URL2 } = __require("url");
4507
4507
  var PerMessageDeflate2 = require_permessage_deflate();
@@ -5182,7 +5182,7 @@ var require_websocket = __commonJS({
5182
5182
  abortHandshake(websocket, socket, "Invalid Upgrade header");
5183
5183
  return;
5184
5184
  }
5185
- const digest = createHash3("sha1").update(key + GUID).digest("base64");
5185
+ const digest = createHash4("sha1").update(key + GUID).digest("base64");
5186
5186
  if (res.headers["sec-websocket-accept"] !== digest) {
5187
5187
  abortHandshake(websocket, socket, "Invalid Sec-WebSocket-Accept header");
5188
5188
  return;
@@ -5551,7 +5551,7 @@ var require_websocket_server = __commonJS({
5551
5551
  var EventEmitter = __require("events");
5552
5552
  var http = __require("http");
5553
5553
  var { Duplex } = __require("stream");
5554
- var { createHash: createHash3 } = __require("crypto");
5554
+ var { createHash: createHash4 } = __require("crypto");
5555
5555
  var extension2 = require_extension();
5556
5556
  var PerMessageDeflate2 = require_permessage_deflate();
5557
5557
  var subprotocol2 = require_subprotocol();
@@ -5858,7 +5858,7 @@ var require_websocket_server = __commonJS({
5858
5858
  );
5859
5859
  }
5860
5860
  if (this._state > RUNNING) return abortHandshake(socket, 503);
5861
- const digest = createHash3("sha1").update(key + GUID).digest("base64");
5861
+ const digest = createHash4("sha1").update(key + GUID).digest("base64");
5862
5862
  const headers = [
5863
5863
  "HTTP/1.1 101 Switching Protocols",
5864
5864
  "Upgrade: websocket",
@@ -7486,10 +7486,10 @@ var require_segments = __commonJS({
7486
7486
  const segs = getSegmentsFromString(data, Utils.isKanjiModeEnabled());
7487
7487
  const nodes = buildNodes(segs);
7488
7488
  const graph = buildGraph(nodes, version2);
7489
- const path3 = dijkstra.find_path(graph.map, "start", "end");
7489
+ const path6 = dijkstra.find_path(graph.map, "start", "end");
7490
7490
  const optimizedSegs = [];
7491
- for (let i = 1; i < path3.length - 1; i++) {
7492
- optimizedSegs.push(graph.table[path3[i]].node);
7491
+ for (let i = 1; i < path6.length - 1; i++) {
7492
+ optimizedSegs.push(graph.table[path6[i]].node);
7493
7493
  }
7494
7494
  return exports.fromArray(mergeSegments(optimizedSegs));
7495
7495
  };
@@ -9966,7 +9966,7 @@ var require_png2 = __commonJS({
9966
9966
  });
9967
9967
  png.pack();
9968
9968
  };
9969
- exports.renderToFile = function renderToFile(path3, qrData, options, cb) {
9969
+ exports.renderToFile = function renderToFile(path6, qrData, options, cb) {
9970
9970
  if (typeof cb === "undefined") {
9971
9971
  cb = options;
9972
9972
  options = void 0;
@@ -9977,7 +9977,7 @@ var require_png2 = __commonJS({
9977
9977
  called = true;
9978
9978
  cb.apply(null, args);
9979
9979
  };
9980
- const stream = fs.createWriteStream(path3);
9980
+ const stream = fs.createWriteStream(path6);
9981
9981
  stream.on("error", done);
9982
9982
  stream.on("close", done);
9983
9983
  exports.renderToFileStream(stream, qrData, options);
@@ -10039,14 +10039,14 @@ var require_utf8 = __commonJS({
10039
10039
  }
10040
10040
  return output2;
10041
10041
  };
10042
- exports.renderToFile = function renderToFile(path3, qrData, options, cb) {
10042
+ exports.renderToFile = function renderToFile(path6, qrData, options, cb) {
10043
10043
  if (typeof cb === "undefined") {
10044
10044
  cb = options;
10045
10045
  options = void 0;
10046
10046
  }
10047
10047
  const fs = __require("fs");
10048
10048
  const utf82 = exports.render(qrData, options);
10049
- fs.writeFile(path3, utf82, cb);
10049
+ fs.writeFile(path6, utf82, cb);
10050
10050
  };
10051
10051
  }
10052
10052
  });
@@ -10167,7 +10167,7 @@ var require_svg_tag = __commonJS({
10167
10167
  return str;
10168
10168
  }
10169
10169
  function qrToPath(data, size, margin) {
10170
- let path3 = "";
10170
+ let path6 = "";
10171
10171
  let moveBy = 0;
10172
10172
  let newRow = false;
10173
10173
  let lineLength = 0;
@@ -10178,19 +10178,19 @@ var require_svg_tag = __commonJS({
10178
10178
  if (data[i]) {
10179
10179
  lineLength++;
10180
10180
  if (!(i > 0 && col > 0 && data[i - 1])) {
10181
- path3 += newRow ? svgCmd("M", col + margin, 0.5 + row + margin) : svgCmd("m", moveBy, 0);
10181
+ path6 += newRow ? svgCmd("M", col + margin, 0.5 + row + margin) : svgCmd("m", moveBy, 0);
10182
10182
  moveBy = 0;
10183
10183
  newRow = false;
10184
10184
  }
10185
10185
  if (!(col + 1 < size && data[i + 1])) {
10186
- path3 += svgCmd("h", lineLength);
10186
+ path6 += svgCmd("h", lineLength);
10187
10187
  lineLength = 0;
10188
10188
  }
10189
10189
  } else {
10190
10190
  moveBy++;
10191
10191
  }
10192
10192
  }
10193
- return path3;
10193
+ return path6;
10194
10194
  }
10195
10195
  exports.render = function render(qrData, options, cb) {
10196
10196
  const opts = Utils.getOptions(options);
@@ -10198,10 +10198,10 @@ var require_svg_tag = __commonJS({
10198
10198
  const data = qrData.modules.data;
10199
10199
  const qrcodesize = size + opts.margin * 2;
10200
10200
  const bg = !opts.color.light.a ? "" : "<path " + getColorAttrib(opts.color.light, "fill") + ' d="M0 0h' + qrcodesize + "v" + qrcodesize + 'H0z"/>';
10201
- const path3 = "<path " + getColorAttrib(opts.color.dark, "stroke") + ' d="' + qrToPath(data, size, opts.margin) + '"/>';
10201
+ const path6 = "<path " + getColorAttrib(opts.color.dark, "stroke") + ' d="' + qrToPath(data, size, opts.margin) + '"/>';
10202
10202
  const viewBox = 'viewBox="0 0 ' + qrcodesize + " " + qrcodesize + '"';
10203
10203
  const width = !opts.width ? "" : 'width="' + opts.width + '" height="' + opts.width + '" ';
10204
- const svgTag = '<svg xmlns="http://www.w3.org/2000/svg" ' + width + viewBox + ' shape-rendering="crispEdges">' + bg + path3 + "</svg>\n";
10204
+ const svgTag = '<svg xmlns="http://www.w3.org/2000/svg" ' + width + viewBox + ' shape-rendering="crispEdges">' + bg + path6 + "</svg>\n";
10205
10205
  if (typeof cb === "function") {
10206
10206
  cb(null, svgTag);
10207
10207
  }
@@ -10215,7 +10215,7 @@ var require_svg = __commonJS({
10215
10215
  "../../node_modules/.pnpm/qrcode@1.5.4/node_modules/qrcode/lib/renderer/svg.js"(exports) {
10216
10216
  var svgTagRenderer = require_svg_tag();
10217
10217
  exports.render = svgTagRenderer.render;
10218
- exports.renderToFile = function renderToFile(path3, qrData, options, cb) {
10218
+ exports.renderToFile = function renderToFile(path6, qrData, options, cb) {
10219
10219
  if (typeof cb === "undefined") {
10220
10220
  cb = options;
10221
10221
  options = void 0;
@@ -10223,7 +10223,7 @@ var require_svg = __commonJS({
10223
10223
  const fs = __require("fs");
10224
10224
  const svgTag = exports.render(qrData, options);
10225
10225
  const xmlStr = '<?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' + svgTag;
10226
- fs.writeFile(path3, xmlStr, cb);
10226
+ fs.writeFile(path6, xmlStr, cb);
10227
10227
  };
10228
10228
  }
10229
10229
  });
@@ -10381,8 +10381,8 @@ var require_server = __commonJS({
10381
10381
  cb
10382
10382
  };
10383
10383
  }
10384
- function getTypeFromFilename(path3) {
10385
- return path3.slice((path3.lastIndexOf(".") - 1 >>> 0) + 2).toLowerCase();
10384
+ function getTypeFromFilename(path6) {
10385
+ return path6.slice((path6.lastIndexOf(".") - 1 >>> 0) + 2).toLowerCase();
10386
10386
  }
10387
10387
  function getRendererFromType(type) {
10388
10388
  switch (type) {
@@ -10446,17 +10446,17 @@ var require_server = __commonJS({
10446
10446
  const renderer = getRendererFromType(params.opts.type);
10447
10447
  return render(renderer.renderToBuffer, text, params);
10448
10448
  };
10449
- exports.toFile = function toFile(path3, text, opts, cb) {
10450
- if (typeof path3 !== "string" || !(typeof text === "string" || typeof text === "object")) {
10449
+ exports.toFile = function toFile(path6, text, opts, cb) {
10450
+ if (typeof path6 !== "string" || !(typeof text === "string" || typeof text === "object")) {
10451
10451
  throw new Error("Invalid argument");
10452
10452
  }
10453
10453
  if (arguments.length < 3 && !canPromise()) {
10454
10454
  throw new Error("Too few arguments provided");
10455
10455
  }
10456
10456
  const params = checkParams(text, opts, cb);
10457
- const type = params.opts.type || getTypeFromFilename(path3);
10457
+ const type = params.opts.type || getTypeFromFilename(path6);
10458
10458
  const renderer = getRendererFromType(type);
10459
- const renderToFile = renderer.renderToFile.bind(null, path3);
10459
+ const renderToFile = renderer.renderToFile.bind(null, path6);
10460
10460
  return render(renderToFile, text, params);
10461
10461
  };
10462
10462
  exports.toFileStream = function toFileStream(stream, text, opts) {
@@ -10655,9 +10655,9 @@ var init_mock_kernel = __esm({
10655
10655
  });
10656
10656
 
10657
10657
  // src/index.ts
10658
- import { randomBytes as randomBytes4 } from "node:crypto";
10658
+ import path5 from "node:path";
10659
10659
 
10660
- // ../../node_modules/.pnpm/dsh-remote-wire@1.0.0/node_modules/dsh-remote-wire/dist/src/crypto/bytes.js
10660
+ // ../../node_modules/.pnpm/dsh-remote-wire@1.2.0/node_modules/dsh-remote-wire/dist/src/crypto/bytes.js
10661
10661
  import { Buffer as Buffer2 } from "node:buffer";
10662
10662
  var encoder = new TextEncoder();
10663
10663
  var decoder = new TextDecoder("utf-8", { fatal: false });
@@ -10684,7 +10684,7 @@ function concat(parts) {
10684
10684
  return out;
10685
10685
  }
10686
10686
 
10687
- // ../../node_modules/.pnpm/dsh-remote-wire@1.0.0/node_modules/dsh-remote-wire/dist/src/crypto/keys.js
10687
+ // ../../node_modules/.pnpm/dsh-remote-wire@1.2.0/node_modules/dsh-remote-wire/dist/src/crypto/keys.js
10688
10688
  import { createHash, randomBytes } from "node:crypto";
10689
10689
  var KDF_NAMESPACE = "dsh-rc/v1";
10690
10690
  var UNIT_SEPARATOR = 31;
@@ -10714,7 +10714,7 @@ function randomNonce() {
10714
10714
  return new Uint8Array(randomBytes(NONCE_BYTES));
10715
10715
  }
10716
10716
 
10717
- // ../../node_modules/.pnpm/dsh-remote-wire@1.0.0/node_modules/dsh-remote-wire/dist/src/crypto/record.js
10717
+ // ../../node_modules/.pnpm/dsh-remote-wire@1.2.0/node_modules/dsh-remote-wire/dist/src/crypto/record.js
10718
10718
  var import_tweetnacl = __toESM(require_nacl_fast(), 1);
10719
10719
  var MAC_BYTES = 16;
10720
10720
  var MIN_RECORD_BYTES = NONCE_BYTES + MAC_BYTES;
@@ -10747,7 +10747,7 @@ function open(key, record2) {
10747
10747
  }
10748
10748
  }
10749
10749
 
10750
- // ../../node_modules/.pnpm/dsh-remote-wire@1.0.0/node_modules/dsh-remote-wire/dist/src/identity/pairing.js
10750
+ // ../../node_modules/.pnpm/dsh-remote-wire@1.2.0/node_modules/dsh-remote-wire/dist/src/identity/pairing.js
10751
10751
  import { randomBytes as randomBytes2 } from "node:crypto";
10752
10752
  var URI_PREFIX = "dshr:";
10753
10753
  var TOKEN_RE = /^\d{6}$/;
@@ -11593,10 +11593,10 @@ function mergeDefs(...defs) {
11593
11593
  function cloneDef(schema) {
11594
11594
  return mergeDefs(schema._zod.def);
11595
11595
  }
11596
- function getElementAtPath(obj, path3) {
11597
- if (!path3)
11596
+ function getElementAtPath(obj, path6) {
11597
+ if (!path6)
11598
11598
  return obj;
11599
- return path3.reduce((acc, key) => acc?.[key], obj);
11599
+ return path6.reduce((acc, key) => acc?.[key], obj);
11600
11600
  }
11601
11601
  function promiseAllObject(promisesObj) {
11602
11602
  const keys = Object.keys(promisesObj);
@@ -11936,11 +11936,11 @@ function explicitlyAborted(x, startIndex = 0) {
11936
11936
  }
11937
11937
  return false;
11938
11938
  }
11939
- function prefixIssues(path3, issues) {
11939
+ function prefixIssues(path6, issues) {
11940
11940
  return issues.map((iss) => {
11941
11941
  var _a3;
11942
11942
  (_a3 = iss).path ?? (_a3.path = []);
11943
- iss.path.unshift(path3);
11943
+ iss.path.unshift(path6);
11944
11944
  return iss;
11945
11945
  });
11946
11946
  }
@@ -12390,16 +12390,16 @@ function flattenError(error62, mapper = (issue2) => issue2.message) {
12390
12390
  }
12391
12391
  function formatError(error62, mapper = (issue2) => issue2.message) {
12392
12392
  const fieldErrors = { _errors: [] };
12393
- const processError = (error63, path3 = []) => {
12393
+ const processError = (error63, path6 = []) => {
12394
12394
  for (const issue2 of error63.issues) {
12395
12395
  if (issue2.code === "invalid_union" && issue2.errors.length) {
12396
- issue2.errors.map((issues) => processError({ issues }, [...path3, ...issue2.path]));
12396
+ issue2.errors.map((issues) => processError({ issues }, [...path6, ...issue2.path]));
12397
12397
  } else if (issue2.code === "invalid_key") {
12398
- processError({ issues: issue2.issues }, [...path3, ...issue2.path]);
12398
+ processError({ issues: issue2.issues }, [...path6, ...issue2.path]);
12399
12399
  } else if (issue2.code === "invalid_element") {
12400
- processError({ issues: issue2.issues }, [...path3, ...issue2.path]);
12400
+ processError({ issues: issue2.issues }, [...path6, ...issue2.path]);
12401
12401
  } else {
12402
- const fullpath = [...path3, ...issue2.path];
12402
+ const fullpath = [...path6, ...issue2.path];
12403
12403
  if (fullpath.length === 0) {
12404
12404
  fieldErrors._errors.push(mapper(issue2));
12405
12405
  } else {
@@ -12438,17 +12438,17 @@ function formatError(error62, mapper = (issue2) => issue2.message) {
12438
12438
  }
12439
12439
  function treeifyError(error62, mapper = (issue2) => issue2.message) {
12440
12440
  const result2 = { errors: [] };
12441
- const processError = (error63, path3 = []) => {
12441
+ const processError = (error63, path6 = []) => {
12442
12442
  var _a3;
12443
12443
  for (const issue2 of error63.issues) {
12444
12444
  if (issue2.code === "invalid_union" && issue2.errors.length) {
12445
- issue2.errors.map((issues) => processError({ issues }, [...path3, ...issue2.path]));
12445
+ issue2.errors.map((issues) => processError({ issues }, [...path6, ...issue2.path]));
12446
12446
  } else if (issue2.code === "invalid_key") {
12447
- processError({ issues: issue2.issues }, [...path3, ...issue2.path]);
12447
+ processError({ issues: issue2.issues }, [...path6, ...issue2.path]);
12448
12448
  } else if (issue2.code === "invalid_element") {
12449
- processError({ issues: issue2.issues }, [...path3, ...issue2.path]);
12449
+ processError({ issues: issue2.issues }, [...path6, ...issue2.path]);
12450
12450
  } else {
12451
- const fullpath = [...path3, ...issue2.path];
12451
+ const fullpath = [...path6, ...issue2.path];
12452
12452
  if (fullpath.length === 0) {
12453
12453
  result2.errors.push(mapper(issue2));
12454
12454
  continue;
@@ -12487,8 +12487,8 @@ function treeifyError(error62, mapper = (issue2) => issue2.message) {
12487
12487
  }
12488
12488
  function toDotPath(_path) {
12489
12489
  const segs = [];
12490
- const path3 = _path.map((seg) => typeof seg === "object" ? seg.key : seg);
12491
- for (const seg of path3) {
12490
+ const path6 = _path.map((seg) => typeof seg === "object" ? seg.key : seg);
12491
+ for (const seg of path6) {
12492
12492
  if (typeof seg === "number")
12493
12493
  segs.push(`[${seg}]`);
12494
12494
  else if (typeof seg === "symbol")
@@ -29590,13 +29590,13 @@ function resolveRef(ref, ctx) {
29590
29590
  if (!ref.startsWith("#")) {
29591
29591
  throw new Error("External $ref is not supported, only local refs (#/...) are allowed");
29592
29592
  }
29593
- const path3 = ref.slice(1).split("/").filter(Boolean);
29594
- if (path3.length === 0) {
29593
+ const path6 = ref.slice(1).split("/").filter(Boolean);
29594
+ if (path6.length === 0) {
29595
29595
  return ctx.rootSchema;
29596
29596
  }
29597
29597
  const defsKey = ctx.version === "draft-2020-12" ? "$defs" : "definitions";
29598
- if (path3[0] === defsKey) {
29599
- const key = path3[1] === void 0 ? void 0 : decodeJSONPointerSegment(path3[1]);
29598
+ if (path6[0] === defsKey) {
29599
+ const key = path6[1] === void 0 ? void 0 : decodeJSONPointerSegment(path6[1]);
29600
29600
  if (!key || !ctx.defs[key]) {
29601
29601
  throw new Error(`Reference not found: ${ref}`);
29602
29602
  }
@@ -30445,7 +30445,7 @@ function date4(params) {
30445
30445
  return _coercedDate(ZodDate, params);
30446
30446
  }
30447
30447
 
30448
- // ../../node_modules/.pnpm/dsh-remote-wire@1.0.0/node_modules/dsh-remote-wire/dist/src/wire/frames.js
30448
+ // ../../node_modules/.pnpm/dsh-remote-wire@1.2.0/node_modules/dsh-remote-wire/dist/src/wire/frames.js
30449
30449
  var PAIRING_TOKEN_RE = /^\d{6}$/;
30450
30450
  var nonEmpty = external_exports.string().min(1);
30451
30451
  var pairingToken = external_exports.string().regex(PAIRING_TOKEN_RE);
@@ -30468,8 +30468,7 @@ var helloFrame = external_exports.object({
30468
30468
  });
30469
30469
  var pairBeginFrame = external_exports.object({
30470
30470
  t: external_exports.literal("pair-begin"),
30471
- pairingToken,
30472
- hostLabel: external_exports.string().max(128).optional()
30471
+ pairingToken
30473
30472
  });
30474
30473
  var pairClaimFrame = external_exports.object({
30475
30474
  t: external_exports.literal("pair-begin-client"),
@@ -30605,7 +30604,7 @@ function parseRelayFrameText(raw) {
30605
30604
  }
30606
30605
  }
30607
30606
 
30608
- // ../../node_modules/.pnpm/dsh-remote-wire@1.0.0/node_modules/dsh-remote-wire/dist/src/wire/payloads.js
30607
+ // ../../node_modules/.pnpm/dsh-remote-wire@1.2.0/node_modules/dsh-remote-wire/dist/src/wire/payloads.js
30609
30608
  var SESSION_STATES = [
30610
30609
  "idle",
30611
30610
  "running",
@@ -30670,6 +30669,13 @@ var evPermissionRequest = external_exports.object({
30670
30669
  options: external_exports.array(choiceOption).optional(),
30671
30670
  expiresAt: external_exports.string().optional()
30672
30671
  });
30672
+ var evPermissionResolved = external_exports.object({
30673
+ t: external_exports.literal("ev.permission_resolved"),
30674
+ requestId: nonEmpty2,
30675
+ sessionId: nonEmpty2.optional(),
30676
+ /** 谁收的场。手机只按"要不要收掉这张卡"读它,所以不做必填。 */
30677
+ by: external_exports.enum(["desktop", "cancelled"]).optional()
30678
+ });
30673
30679
  var questionItem = external_exports.object({
30674
30680
  id: nonEmpty2,
30675
30681
  question: nonEmpty2,
@@ -30680,7 +30686,23 @@ var evQuestionRequest = external_exports.object({
30680
30686
  t: external_exports.literal("ev.question_request"),
30681
30687
  sessionId: nonEmpty2.optional(),
30682
30688
  requestId: nonEmpty2,
30683
- questions: external_exports.array(questionItem)
30689
+ questions: external_exports.array(questionItem),
30690
+ /**
30691
+ * 这张提问卡什么时候作废(ISO 字符串)。
30692
+ *
30693
+ * 为什么审批有而提问没有过:主机侧的提问等待本来就带超时(`questionTimeoutMs`,默认 300s),
30694
+ * 但手机上那张卡**没有任何倒计时**——用户看不见自己按的按钮什么时候失效,
30695
+ * 而这违反伞仓 `docs/PRODUCT.md` §3 第 3 条「超时必须可见」。
30696
+ * 与审批那条同一个形状:可选、ISO 字符串、由主机每次发问时现算。
30697
+ */
30698
+ expiresAt: external_exports.string().optional()
30699
+ });
30700
+ var evQuestionResolved = external_exports.object({
30701
+ t: external_exports.literal("ev.question_resolved"),
30702
+ requestId: nonEmpty2,
30703
+ sessionId: nonEmpty2.optional(),
30704
+ /** 谁收的场。手机只按"要不要收掉这张卡"读它,所以不做必填。 */
30705
+ by: external_exports.enum(["desktop", "cancelled"]).optional()
30684
30706
  });
30685
30707
  var evRunState = external_exports.object({
30686
30708
  t: external_exports.literal("ev.run_state"),
@@ -30697,6 +30719,24 @@ var evKeepAwakeState = external_exports.object({
30697
30719
  backend: nonEmpty2,
30698
30720
  reason: external_exports.string().optional()
30699
30721
  });
30722
+ var modelOption = external_exports.object({
30723
+ /** 机器名(回传用),例如 `deepseek-chat`。 */
30724
+ value: nonEmpty2,
30725
+ /** 给人看的名字;缺省时小程序自己截断 `value`。 */
30726
+ label: external_exports.string().optional(),
30727
+ provider: external_exports.string().optional()
30728
+ });
30729
+ var evModel = external_exports.object({
30730
+ t: external_exports.literal("ev.model"),
30731
+ provider: nonEmpty2.optional(),
30732
+ model: nonEmpty2,
30733
+ /** 主机能不能换。false 时小程序**不许**渲染成可点的下拉。 */
30734
+ canSwitch: external_exports.boolean(),
30735
+ /** 候选清单;`canSwitch` 为 true 时至少一项。 */
30736
+ options: external_exports.array(modelOption).optional(),
30737
+ /** 不能切时给手机一句人话,直接显示,不要让小程序自己编措辞。 */
30738
+ reason: external_exports.string().optional()
30739
+ });
30700
30740
  var evResult = external_exports.object({
30701
30741
  t: external_exports.literal("ev.result"),
30702
30742
  cmdId: external_exports.string(),
@@ -30718,9 +30758,12 @@ var evPayload = external_exports.discriminatedUnion("t", [
30718
30758
  evMessageDelta,
30719
30759
  evToolEvent,
30720
30760
  evPermissionRequest,
30761
+ evPermissionResolved,
30721
30762
  evQuestionRequest,
30763
+ evQuestionResolved,
30722
30764
  evRunState,
30723
30765
  evKeepAwakeState,
30766
+ evModel,
30724
30767
  evResult,
30725
30768
  evSessionHistory
30726
30769
  ]);
@@ -30808,14 +30851,17 @@ var PAYLOAD_TYPES = {
30808
30851
  evMessageDelta: "ev.message_delta",
30809
30852
  evToolEvent: "ev.tool_event",
30810
30853
  evPermissionRequest: "ev.permission_request",
30854
+ evPermissionResolved: "ev.permission_resolved",
30811
30855
  evQuestionRequest: "ev.question_request",
30856
+ evQuestionResolved: "ev.question_resolved",
30812
30857
  evRunState: "ev.run_state",
30813
30858
  evKeepAwakeState: "ev.keep_awake_state",
30859
+ evModel: "ev.model",
30814
30860
  evResult: "ev.result",
30815
30861
  evSessionHistory: "ev.session_history"
30816
30862
  };
30817
30863
 
30818
- // ../../node_modules/.pnpm/dsh-remote-wire@1.0.0/node_modules/dsh-remote-wire/dist/src/wire/outbound.js
30864
+ // ../../node_modules/.pnpm/dsh-remote-wire@1.2.0/node_modules/dsh-remote-wire/dist/src/wire/outbound.js
30819
30865
  function sessionChanged(sessions, reason) {
30820
30866
  return { t: PAYLOAD_TYPES.evSessionChanged, sessions, ...reason === void 0 ? {} : { reason } };
30821
30867
  }
@@ -30828,15 +30874,31 @@ function toolEvent(args) {
30828
30874
  function permissionRequest(args) {
30829
30875
  return { t: PAYLOAD_TYPES.evPermissionRequest, ...args };
30830
30876
  }
30877
+ function permissionResolved(args) {
30878
+ return { t: PAYLOAD_TYPES.evPermissionResolved, ...args };
30879
+ }
30831
30880
  function questionRequest(args) {
30832
30881
  return { t: PAYLOAD_TYPES.evQuestionRequest, ...args };
30833
30882
  }
30883
+ function questionResolved(args) {
30884
+ return { t: PAYLOAD_TYPES.evQuestionResolved, ...args };
30885
+ }
30834
30886
  function runState(args) {
30835
30887
  return { t: PAYLOAD_TYPES.evRunState, ...args };
30836
30888
  }
30837
30889
  function keepAwakeState(args) {
30838
30890
  return { t: PAYLOAD_TYPES.evKeepAwakeState, ...args };
30839
30891
  }
30892
+ function model(args) {
30893
+ const payload = { t: PAYLOAD_TYPES.evModel, model: args.model, canSwitch: args.canSwitch };
30894
+ if (args.provider !== void 0)
30895
+ payload.provider = args.provider;
30896
+ if (args.canSwitch && args.options?.length)
30897
+ payload.options = args.options;
30898
+ if (args.reason !== void 0)
30899
+ payload.reason = args.reason;
30900
+ return payload;
30901
+ }
30840
30902
  function result(cmdId, ok, extras = {}) {
30841
30903
  const { message, data } = extras;
30842
30904
  return {
@@ -30858,7 +30920,7 @@ function sessionHistory(args) {
30858
30920
  };
30859
30921
  }
30860
30922
 
30861
- // ../../node_modules/.pnpm/dsh-remote-wire@1.0.0/node_modules/dsh-remote-wire/dist/src/platform/sleep.js
30923
+ // ../../node_modules/.pnpm/dsh-remote-wire@1.2.0/node_modules/dsh-remote-wire/dist/src/platform/sleep.js
30862
30924
  function detectBackend(platform) {
30863
30925
  if (platform === "darwin")
30864
30926
  return "caffeinate";
@@ -31244,6 +31306,25 @@ var HostRuntime = class {
31244
31306
  get stats() {
31245
31307
  return { ...this.outboundCount };
31246
31308
  }
31309
+ /**
31310
+ * 有几件事正挂在手机上等回答,以及**最久的那件已经等了多久**(秒)。
31311
+ *
31312
+ * 为什么这条要出到界面上(而不只是内部状态):一张挂起的审批阻塞着远端一条正在跑的回合,
31313
+ * 而回合阻塞着用户的下班时间。桌面那颗 pill 原来在"已配对 + 有东西在等"时仍然只说
31314
+ * `已配对`——那是这整条链上唯一看得见它的地方,等于没有。判据见伞仓 docs/PRODUCT.md §3。
31315
+ *
31316
+ * `oldestSec` 用 `clock.now()` 而不是 `Date.now()`: FakeClock 要能演"等了 4 分钟",
31317
+ * 否则这条时长在测试里永远是 0,也就永远不会红。
31318
+ */
31319
+ get waiting() {
31320
+ const now = this.clock.now();
31321
+ let oldest = 0;
31322
+ for (const item of this.pending.values()) {
31323
+ const age = Math.max(0, Math.floor((now - item.askedAt) / 1e3));
31324
+ if (age > oldest) oldest = age;
31325
+ }
31326
+ return { count: this.pending.size, oldestSec: oldest };
31327
+ }
31247
31328
  countOutbound(payload, sent) {
31248
31329
  const key = payload.t.replace(/^ev\./, "");
31249
31330
  this.outboundCount[key] = (this.outboundCount[key] ?? 0) + 1;
@@ -31321,8 +31402,40 @@ var HostRuntime = class {
31321
31402
  }
31322
31403
  this.broadcast(sessionChanged(this.sessions, reason));
31323
31404
  this.broadcast(keepAwakeState(this.sleep.snapshot()));
31405
+ this.broadcastModel();
31324
31406
  return this.sessions;
31325
31407
  }
31408
+ /**
31409
+ * 广播当前模型。
31410
+ *
31411
+ * 跟着 `pushSessions` 一起发而不是单独起一条:模型与防休眠都是**全局状态**,
31412
+ * 而 `pushSessions` 已经是"状态变了就推一次"的唯一入口,另开一条推送路径
31413
+ * 必然出现「会话更新了但模型没更新」这种半同步状态。
31414
+ *
31415
+ * **读不到就不发**(而不是发一个 `model: ''`):`ev.model` 的 `model` 是必填的
31416
+ * 非空串,硬塞空串会让手机把「不知道用什么模型」显示成「模型名为空」——
31417
+ * 后者看起来像 bug,前者只是没显示。内核缺 `currentSelection` 时真机上是常态
31418
+ * (见 carrier 的 modelFace 探测)。
31419
+ */
31420
+ broadcastModel() {
31421
+ let selection;
31422
+ try {
31423
+ selection = this.kernel.modelSelection?.();
31424
+ } catch {
31425
+ return;
31426
+ }
31427
+ if (!selection?.model) return;
31428
+ const face = this.kernel.modelOptions?.();
31429
+ this.broadcast(
31430
+ model({
31431
+ model: selection.model,
31432
+ provider: selection.provider,
31433
+ canSwitch: Boolean(face?.canSwitch),
31434
+ options: face?.options,
31435
+ reason: face?.canSwitch ? void 0 : face?.reason ?? "\u4E3B\u673A\u5185\u6838\u672A\u63D0\u4F9B\u5207\u6362\u6A21\u578B\u7684\u80FD\u529B"
31436
+ })
31437
+ );
31438
+ }
31326
31439
  refreshLoop() {
31327
31440
  if (this.stopped) return;
31328
31441
  void this.pushSessions("refresh");
@@ -31336,24 +31449,33 @@ var HostRuntime = class {
31336
31449
  * **没有在线对端就返回 `'decline'`**,让适配器去调平台的 `next()` 把决定权交还桌面 UI;
31337
31450
  * 超时同样返回 `'decline'`(我们已经把卡片发出去了,但手机迟迟没点,
31338
31451
  * 与其替用户决定,不如让桌面去决定)。
31452
+ *
31453
+ * 返回值只说明"手机这一侧算出了什么",**不代表这张卡是手机答掉的**:
31454
+ * 桌面与手机是同时被问的(见 `carrier-services.ts` 的 `participate`),
31455
+ * 所以收尾时要按"有没有真的有人在手机上点过"决定要不要把卡收回去。
31339
31456
  */
31340
31457
  async onApprovalRequest(info) {
31341
31458
  const conversationId = this.pickConversation(info.sessionId);
31342
- if (!conversationId || !this.transport.hasPeer(conversationId)) return "decline";
31459
+ if (!conversationId || !this.transport.hasClient(conversationId)) return "decline";
31343
31460
  const id = `ap_${randomUUID().slice(0, 8)}`;
31344
31461
  this.sleep.hold(id);
31345
31462
  const answered = await new Promise((resolve) => {
31346
31463
  const timer = this.clock.setTimeout(() => {
31347
- this.pending.delete(id);
31348
- resolve("decline");
31464
+ this.settle(id, "decline", "timeout");
31349
31465
  }, this.options.approvalTimeoutMs);
31350
31466
  this.pending.set(id, {
31351
31467
  conversationId,
31352
31468
  sessionId: info.sessionId,
31353
31469
  resolve: (value) => resolve(value === void 0 ? "decline" : value),
31354
- timer
31470
+ timer,
31471
+ kind: "approval",
31472
+ askedAt: this.clock.now()
31355
31473
  });
31356
- info.signal?.addEventListener("abort", () => this.settle(id, "cancelled"), { once: true });
31474
+ info.signal?.addEventListener(
31475
+ "abort",
31476
+ () => this.settle(id, "cancelled", info.signal?.reason === "desktop" ? "desktop" : "withdrawn"),
31477
+ { once: true }
31478
+ );
31357
31479
  this.replyTo(
31358
31480
  conversationId,
31359
31481
  permissionRequest({
@@ -31361,7 +31483,8 @@ var HostRuntime = class {
31361
31483
  sessionId: info.sessionId,
31362
31484
  action: info.action,
31363
31485
  ...info.reason === void 0 ? {} : { reason: info.reason },
31364
- options: APPROVAL_OPTIONS
31486
+ options: APPROVAL_OPTIONS,
31487
+ expiresAt: new Date(Date.now() + this.options.approvalTimeoutMs).toISOString()
31365
31488
  })
31366
31489
  );
31367
31490
  });
@@ -31370,28 +31493,36 @@ var HostRuntime = class {
31370
31493
  }
31371
31494
  async onQuestionRequest(info) {
31372
31495
  const conversationId = this.pickConversation(info.sessionId);
31373
- if (!conversationId || !this.transport.hasPeer(conversationId)) return null;
31496
+ if (!conversationId || !this.transport.hasClient(conversationId)) return null;
31374
31497
  const id = `q_${randomUUID().slice(0, 8)}`;
31375
31498
  this.sleep.hold(id);
31376
31499
  const answer = await new Promise((resolve) => {
31377
31500
  const timer = this.clock.setTimeout(() => {
31378
- this.pending.delete(id);
31379
- resolve(null);
31501
+ this.settle(id, null, "timeout");
31380
31502
  }, this.options.questionTimeoutMs);
31381
31503
  this.pending.set(id, {
31382
31504
  conversationId,
31383
31505
  sessionId: info.sessionId,
31384
31506
  resolve: (value) => resolve(value ?? null),
31385
31507
  timer,
31386
- options: info.questions
31508
+ options: info.questions,
31509
+ kind: "question",
31510
+ askedAt: this.clock.now()
31387
31511
  });
31388
- info.signal?.addEventListener("abort", () => this.settle(id, null), { once: true });
31512
+ info.signal?.addEventListener(
31513
+ "abort",
31514
+ () => this.settle(id, null, info.signal?.reason === "desktop" ? "desktop" : "withdrawn"),
31515
+ { once: true }
31516
+ );
31389
31517
  this.replyTo(
31390
31518
  conversationId,
31391
31519
  questionRequest({
31392
31520
  requestId: id,
31393
31521
  sessionId: info.sessionId,
31394
- questions: info.questions
31522
+ questions: info.questions,
31523
+ // 提问这张卡以前**没有**到期时刻:主机 300 秒就判"没答上",而手机上看不见任何倒计时,
31524
+ // 用户不知道自己按的按钮什么时候作废(伞仓 docs/PRODUCT.md §3 第 3 条)。
31525
+ expiresAt: new Date(Date.now() + this.options.questionTimeoutMs).toISOString()
31395
31526
  })
31396
31527
  );
31397
31528
  });
@@ -31423,13 +31554,45 @@ var HostRuntime = class {
31423
31554
  this.settle(requestId, value);
31424
31555
  return true;
31425
31556
  }
31426
- settle(id, value) {
31557
+ settle(id, value, voidAs) {
31427
31558
  const item = this.pending.get(id);
31428
31559
  if (!item) return;
31429
31560
  this.pending.delete(id);
31430
31561
  this.clock.clearTimeout(item.timer);
31562
+ if (voidAs) this.voidStaleCard(id, item, voidAs);
31431
31563
  item.resolve(value);
31432
31564
  }
31565
+ /**
31566
+ * 让手机上那张已经作废的卡片**当场**收掉:先按 `requestId` 发一条精确帧,
31567
+ * 再补一帧 `ev.run_state` 兜住"手机上装的是老版小程序"这一档。
31568
+ *
31569
+ * 为什么要两条而不是一条:`ev.permission_resolved` / `ev.question_resolved` 是按
31570
+ * `requestId` 收单的(一次答完不会误收别的会话、别的请求那张卡),但**用户手机上现在装的
31571
+ * 那一版还不认这两帧**——小程序的分发是一串 `if (p.t === …)`,认不出来的 `t` 静默忽略。
31572
+ * 而 `ev.run_state` 正好是它早就认的那条"挂着的审批/提问作废"信号
31573
+ * (`pages/chat/chat.js` 的 `_onRunState` 同时清 `pendingPermission` 与 `pendingQuestion`,
31574
+ * 并停掉那条还在走的本地倒数)。所以两条都发:新版手机收到精确帧就精收一张,
31575
+ * 老版手机靠 `run_state` 也能当场收卡,不用等用户重新上传小程序。
31576
+ *
31577
+ * 内核**不会**为"审批/提问被别人答掉了"发状态跳变(这一回合自始至终是 running),
31578
+ * 所以这一帧只能由我们补发;补的是**当前真相**而不是硬编码 `running`——
31579
+ * 手机上那个"思考中"跟着这一帧走,发错方向会一直错到下一次真实跳变。
31580
+ * 读不到真相时退回缓存快照,再退 `idle`:宁可少一个转圈,不能凭空多一个转圈。
31581
+ */
31582
+ voidStaleCard(requestId, item, reason) {
31583
+ const by = reason === "desktop" ? "desktop" : "cancelled";
31584
+ this.replyTo(
31585
+ item.conversationId,
31586
+ item.kind === "approval" ? permissionResolved({ requestId, sessionId: item.sessionId, by }) : questionResolved({ requestId, sessionId: item.sessionId, by })
31587
+ );
31588
+ const emit = (running) => {
31589
+ this.broadcast(runState({ sessionId: item.sessionId, state: running ? "running" : "idle" }));
31590
+ this.log("pending card voided", { requestId, reason, kind: item.kind, state: running ? "running" : "idle" });
31591
+ };
31592
+ void this.kernel.runState(item.sessionId).then((truth) => emit(truth.running)).catch(() => {
31593
+ emit(this.sessions.some((session) => session.id === item.sessionId && session.running === true));
31594
+ });
31595
+ }
31433
31596
  /**
31434
31597
  * 挑一条配对通道把这轮交互发出去。
31435
31598
  *
@@ -31441,7 +31604,7 @@ var HostRuntime = class {
31441
31604
  pickConversation(_sessionId) {
31442
31605
  const ids = this.transport.conversationIds();
31443
31606
  if (ids.length === 0) return void 0;
31444
- return ids.find((id) => this.transport.hasPeer(id)) ?? ids[0];
31607
+ return ids.find((id) => this.transport.hasClient(id)) ?? ids[0];
31445
31608
  }
31446
31609
  log(message, fields) {
31447
31610
  this.options.log?.(message, fields);
@@ -31603,6 +31766,10 @@ var DEFAULT_PRUNE_POLICY = {
31603
31766
  // 一天没有任何收发就认为这条通道不会再被用(手机真回来时会重新扫码,
31604
31767
  // 而且它会先撞上 `unknown_session` → 中文提示,而不是"永远转圈")。
31605
31768
  idleTtlMs: 24 * 3600 * 1e3,
31769
+ // 恢复出来的会话不按上面那条剪:默认给 7 天,与中继的 `DRC_CONV_IDLE_TTL_MS`
31770
+ // (`config.ts` 的 7×24h)逐字对齐 —— 中继忘掉它的那天本来就得重扫,
31771
+ // 主机提前剪只是把同一次重扫提前到用户还没察觉的时间点,反而更难解释。
31772
+ restoredIdleTtlMs: 7 * 24 * 3600 * 1e3,
31606
31773
  maxConversations: 64
31607
31774
  };
31608
31775
  var ConversationBook = class {
@@ -31620,13 +31787,48 @@ var ConversationBook = class {
31620
31787
  kH2C: derivePskKey(args.psk, H2C, args.id),
31621
31788
  psk: args.psk,
31622
31789
  seqHost: 0,
31623
- generation: args.generation,
31624
31790
  createdAt: args.now,
31625
31791
  lastActivityAt: args.now
31626
31792
  };
31627
31793
  this.byId.set(args.id, conversation);
31628
31794
  return conversation;
31629
31795
  }
31796
+ /**
31797
+ * 从落盘记录恢复会话(跨进程续用,见 `shell/pair-store.ts`)。
31798
+ *
31799
+ * 两条刻意的选择:
31800
+ *
31801
+ * - **`clientIds` 一律置空**:那是"此刻谁连着"的瞬时态,进程刚起来时没有人连着主机。
31802
+ * 填了旧值会让主机一启动就往每条会话广播,而手机端还没重连 ——
31803
+ * 那些帧全被中继计成丢帧(`relay.ts` 的 `broadcast` 闸门当初就是为这个加的)。
31804
+ * 手机回来发的第一帧会经 `RelayClient.onEncrypted` 把它重新登记进来。
31805
+ * - **`restored: true`**:让剪枝对这条会话用另一条 TTL,理由见该字段的注释。
31806
+ *
31807
+ * 返回真正恢复出来的会话 id:**同一条 id 重复出现时后者覆盖前者**,
31808
+ * 而一条形状不合的记录整条丢弃(宁可少一条通道,也不要一条解不开的)。
31809
+ */
31810
+ restore(records, now) {
31811
+ const restored = [];
31812
+ for (const record2 of records) {
31813
+ const conversation = this.open({ id: record2.id, psk: record2.psk, now });
31814
+ conversation.seqHost = record2.seqHost;
31815
+ conversation.createdAt = record2.createdAt;
31816
+ conversation.lastActivityAt = record2.lastActivityAt;
31817
+ conversation.restored = true;
31818
+ restored.push(record2.id);
31819
+ }
31820
+ return restored;
31821
+ }
31822
+ /** 导出可落盘的记录(**不含 clientIds**,理由见 `restore`)。 */
31823
+ snapshot() {
31824
+ return [...this.byId.values()].map((conversation) => ({
31825
+ id: conversation.id,
31826
+ psk: conversation.psk,
31827
+ seqHost: conversation.seqHost,
31828
+ createdAt: conversation.createdAt,
31829
+ lastActivityAt: conversation.lastActivityAt
31830
+ }));
31831
+ }
31630
31832
  /** 有实际收发就记一笔活跃(剪枝只看这个时刻)。未知通道返回 false,不顺手建条目。 */
31631
31833
  touch(id, now) {
31632
31834
  const conversation = this.byId.get(id);
@@ -31640,33 +31842,43 @@ var ConversationBook = class {
31640
31842
  has(id) {
31641
31843
  return this.byId.has(id);
31642
31844
  }
31845
+ /**
31846
+ * 这条会话**现在有没有活着的客户端**。
31847
+ *
31848
+ * 与 `has()` 是两件事,而且必须分开:`has()` 是"我手里有这条通道的密钥",
31849
+ * 手机回前台时要用同一个 `convId` 回来,所以密钥**必须留着**(D3);
31850
+ * 但"有密钥"不等于"现在有人能收到"。
31851
+ *
31852
+ * 原来只有 `has()` 一个判据,于是 `hasPeer()` 答的是"有密钥",广播就照着它发——
31853
+ * 真机后果:手机断开之后(会话默认 7 天 TTL)主机每 15 秒仍往这条空会话发
31854
+ * `session_changed` / `model` / `keep_awake_state` 三帧,中继每一帧计一次丢帧,
31855
+ * 实测 45 秒涨 9、`/healthz` 的 `droppedFrames` 因此被这一路噪声主导(5737)。
31856
+ */
31857
+ hasClient(id) {
31858
+ const conversation = this.byId.get(id);
31859
+ return conversation !== void 0 && conversation.clientIds.size > 0;
31860
+ }
31643
31861
  close(id) {
31644
31862
  return this.byId.delete(id);
31645
31863
  }
31646
- /** 中继重启(generation 变化)时把旧会话全部丢掉:它们再也解不开了。 */
31647
- closeBefore(generation) {
31648
- const dropped = [];
31649
- for (const [id, conversation] of [...this.byId]) {
31650
- if (conversation.generation < generation) {
31651
- this.byId.delete(id);
31652
- dropped.push(id);
31653
- }
31654
- }
31655
- return dropped;
31656
- }
31657
31864
  /**
31658
31865
  * 按策略剪枝,返回被剪掉的 id(调用方负责通知中继与上层,见
31659
31866
  * `RelayClient.pruneConversations`)。
31660
31867
  *
31661
- * 两条规则都做,因为它们是两种不同的失控:
31868
+ * 三条规则,因为它们是三种不同的失控:
31662
31869
  * - 空闲超 TTL:一条通道一天没有任何收发,就不会再有人用它了;
31870
+ * - **恢复出来的会话走另一条 TTL**(`restoredIdleTtlMs`):见 `Conversation.restored`
31871
+ * 与 `DEFAULT_PRUNE_POLICY.restoredIdleTtlMs` 的理由 —— 它不该按"新配对却没人用"的
31872
+ * 标准剪掉,否则长假之后主机一启动就把唯一那条通道清了,免扫码重连白做;
31663
31873
  * - 超出硬上界:从**最不活跃**的开始剪。没有这条,一个每天配一次对的用户
31664
31874
  * 会在两周内攒下 260 条密钥,并且每次广播都对它们逐个密封一遍。
31665
31875
  */
31666
31876
  pruneStale(now, policy = DEFAULT_PRUNE_POLICY) {
31667
31877
  const dropped = [];
31878
+ const restoredTtl = policy.restoredIdleTtlMs ?? policy.idleTtlMs;
31668
31879
  for (const [id, conversation] of [...this.byId]) {
31669
- if (now - conversation.lastActivityAt > policy.idleTtlMs) {
31880
+ const ttl = conversation.restored ? restoredTtl : policy.idleTtlMs;
31881
+ if (now - conversation.lastActivityAt > ttl) {
31670
31882
  this.byId.delete(id);
31671
31883
  dropped.push(id);
31672
31884
  }
@@ -31683,6 +31895,25 @@ var ConversationBook = class {
31683
31895
  ids() {
31684
31896
  return [...this.byId.keys()];
31685
31897
  }
31898
+ /**
31899
+ * 当前**真正连着**的客户端数(去重后的 clientId,不是会话数)。
31900
+ *
31901
+ * 为什么必须单独算:一条会话(convId)对应一次配对,而**同一部手机**在
31902
+ * 「解除配对再重新配对」之后会开一条新会话 —— 于是 `size`(会话数)会增长,
31903
+ * 手机却还是那一台。把 `size` 当成"有几部手机"报出去,用户解除配对后看到
31904
+ * 数字反而变大,会以为解配没生效。
31905
+ *
31906
+ * 反过来也算不准:会话被剪掉了但手机还在(`pruneStale`),
31907
+ * 所以这个数只保证"不比会话数更离谱",不保证等于真实在线数 ——
31908
+ * 界面上它只该当"有手机连着没有"的粗判据,不该当精确计数。
31909
+ */
31910
+ clientCount() {
31911
+ const ids = /* @__PURE__ */ new Set();
31912
+ for (const conversation of this.byId.values()) {
31913
+ for (const clientId of conversation.clientIds) ids.add(clientId);
31914
+ }
31915
+ return ids.size;
31916
+ }
31686
31917
  get size() {
31687
31918
  return this.byId.size;
31688
31919
  }
@@ -31849,6 +32080,14 @@ var RelayClient = class {
31849
32080
  options;
31850
32081
  conversations = new ConversationBook();
31851
32082
  socket;
32083
+ /**
32084
+ * **主机侧连接代号**:每收到一次 `hello-ok` 就 +1。
32085
+ *
32086
+ * 它现在**只**喂给 `PairingWindow`("中继的 pending-pair 表是不是清空了"),
32087
+ * 绝不用来清会话 —— 理由见文件头。它递增本身没有权威性(中继不回代次),
32088
+ * 所以配对窗口用它判"换码"是保守正确的:中继真重启时代次一定变了,
32089
+ * 中继没重启时多换一张码的代价只是浪费一个 6 位码。
32090
+ */
31852
32091
  generation = 0;
31853
32092
  attempts = 0;
31854
32093
  stopped = false;
@@ -31862,6 +32101,39 @@ var RelayClient = class {
31862
32101
  get conversationCount() {
31863
32102
  return this.conversations.size;
31864
32103
  }
32104
+ /**
32105
+ * 当前连着的**手机台数**(去重的 clientId)。
32106
+ *
32107
+ * 与 `conversationCount` 分开是因为「解除配对 → 重新配对」会开一条新会话,
32108
+ * 手机却还是那一台:拿会话数当手机数报,用户解完配对看到数字变大,
32109
+ * 会以为解配没生效。
32110
+ */
32111
+ get clientCount() {
32112
+ return this.conversations.clientCount();
32113
+ }
32114
+ /** 本轮已连上的中继代号(`status.json` 与配对窗口都读它)。 */
32115
+ get relayGeneration() {
32116
+ return this.generation;
32117
+ }
32118
+ /**
32119
+ * 恢复上次进程留下的会话(密钥来自 `pair-store`)。
32120
+ *
32121
+ * **恢复出来的会话里 `clientIds` 是空的**(见 `ConversationBook.restore`),所以
32122
+ * `conversationCount > 0` 而 `clientCount === 0` 是这一阶段的正常形态:
32123
+ * 密钥还在、手机还没回来。调用方据此知道**不能**等 `onPeerJoined` 才启动内核订阅
32124
+ * —— 否则主机重启后手机能连上、主机却不订阅任何事件,表现与配对丢失一模一样。
32125
+ */
32126
+ restoreConversations(records) {
32127
+ const restored = this.conversations.restore(records, this.options.clock.now());
32128
+ if (restored.length > 0) {
32129
+ this.options.log("conversations restored from disk", {
32130
+ count: restored.length,
32131
+ // 记一下有没有手机已经登记进来了:恢复阶段应当恒为 0。
32132
+ clients: this.conversations.clientCount()
32133
+ });
32134
+ }
32135
+ return restored;
32136
+ }
31865
32137
  connect() {
31866
32138
  if (this.stopped) return;
31867
32139
  this.closeSocket();
@@ -31936,23 +32208,34 @@ var RelayClient = class {
31936
32208
  this.options.log("pruned stale conversations", { count: dropped.length, remaining: this.conversations.size });
31937
32209
  return dropped;
31938
32210
  }
31939
- /** 向一条配对会话发一条载荷;返回 false = 这条会话已经没了或没人接。 */
32211
+ /** 向一条配对会话发一条载荷;返回 false = 这条会话已经没了,或**现在没有活着的客户端**。 */
31940
32212
  send(conversationId, payload) {
31941
32213
  const conversation = this.conversations.get(conversationId);
31942
32214
  if (!conversation) return false;
32215
+ if (conversation.clientIds.size === 0) return false;
31943
32216
  const record2 = seal(conversation.kH2C, payload);
31944
32217
  conversation.lastActivityAt = this.options.clock.now();
32218
+ this.options.onBookActivity?.();
31945
32219
  return this.raw({ t: "enc", sessionId: conversationId, seq: ++conversation.seqHost, ciphertext: record2.ciphertext });
31946
32220
  }
31947
32221
  broadcast(payload) {
31948
32222
  let sent = 0;
31949
32223
  for (const id of this.conversations.ids()) {
32224
+ if (!this.conversations.hasClient(id)) continue;
31950
32225
  if (this.send(id, payload)) sent += 1;
31951
32226
  }
31952
32227
  return sent;
31953
32228
  }
31954
- hasPeer(conversationId) {
31955
- return this.conversations.has(conversationId);
32229
+ /**
32230
+ * 这条会话**现在**有没有能收到东西的手机。
32231
+ *
32232
+ * 原来这个方法叫 `hasPeer`、答的是"我手里有没有这条通道的密钥"——两者在手机上线时恰好
32233
+ * 同真,所以错误一直藏着:手机断开后审批仍然"发得出去"(发进一条空会话),
32234
+ * 桌面那一半要等满 180 秒才拿到决定权。名字换成 `hasClient` 是为了让下一次
32235
+ * 想写 `conversations.has()` 的人当场看见这两个概念不是一回事。
32236
+ */
32237
+ hasClient(conversationId) {
32238
+ return this.conversations.hasClient(conversationId);
31956
32239
  }
31957
32240
  conversationIds() {
31958
32241
  return this.conversations.ids();
@@ -31978,6 +32261,7 @@ var RelayClient = class {
31978
32261
  this.voided.add(conversationId);
31979
32262
  this.undecryptable.delete(conversationId);
31980
32263
  this.conversations.close(conversationId);
32264
+ this.options.onStructuralChange?.();
31981
32265
  this.raw({ t: "session-leave", sessionId: conversationId });
31982
32266
  this.options.onConversationGone(conversationId);
31983
32267
  }
@@ -32000,18 +32284,24 @@ var RelayClient = class {
32000
32284
  switch (frame.t) {
32001
32285
  case "hello-ok": {
32002
32286
  this.generation += 1;
32003
- const dropped = this.conversations.closeBefore(this.generation);
32004
- for (const id of dropped) this.options.onConversationGone(id);
32005
32287
  this.emitState("online");
32006
32288
  this.raw({ t: "resync", sessionIds: this.conversations.ids() });
32007
- this.options.log("relay online", { generation: this.generation, conversations: this.conversations.size });
32289
+ this.options.log("relay online", {
32290
+ generation: this.generation,
32291
+ conversations: this.conversations.size,
32292
+ clients: this.conversations.clientCount()
32293
+ });
32008
32294
  return;
32009
32295
  }
32010
32296
  case "pair-ready":
32011
32297
  this.options.onPairReady(frame.pairingToken, frame.ttlMs);
32012
32298
  return;
32013
32299
  case "peer-joined": {
32014
- if (!frame.pairingToken) return;
32300
+ if (!frame.pairingToken) {
32301
+ const conversation = this.conversations.get(frame.sessionId);
32302
+ if (conversation && frame.clientId) conversation.clientIds.add(frame.clientId);
32303
+ return;
32304
+ }
32015
32305
  const slot = this.options.lookupPairingSlot(frame.pairingToken);
32016
32306
  if (!slot) {
32017
32307
  this.options.log("peer-joined without a known pairing token", { sessionId: frame.sessionId });
@@ -32020,9 +32310,10 @@ var RelayClient = class {
32020
32310
  this.conversations.open({
32021
32311
  id: frame.sessionId,
32022
32312
  psk: slot.psk,
32023
- generation: this.generation,
32024
32313
  now: this.options.clock.now()
32025
32314
  });
32315
+ this.options.onStructuralChange?.();
32316
+ if (frame.clientId) this.conversations.get(frame.sessionId)?.clientIds.add(frame.clientId);
32026
32317
  this.voided.delete(frame.sessionId);
32027
32318
  this.raw({ t: "resync", sessionIds: this.conversations.ids() });
32028
32319
  this.emitState("online");
@@ -32030,6 +32321,7 @@ var RelayClient = class {
32030
32321
  return;
32031
32322
  }
32032
32323
  case "peer-left":
32324
+ if (frame.clientId) this.conversations.get(frame.sessionId)?.clientIds.delete(frame.clientId);
32033
32325
  this.options.onClientLeft(frame.sessionId, frame.clientId);
32034
32326
  this.emitState("online");
32035
32327
  return;
@@ -32062,6 +32354,7 @@ var RelayClient = class {
32062
32354
  this.voidConversation(frame.sessionId);
32063
32355
  return;
32064
32356
  }
32357
+ if (frame.clientId) conversation.clientIds.add(frame.clientId);
32065
32358
  const records = frame.items ?? (frame.ciphertext === void 0 ? [] : [{ ciphertext: frame.ciphertext }]);
32066
32359
  let decryptedAny = false;
32067
32360
  for (const record2 of records) {
@@ -32080,8 +32373,11 @@ var RelayClient = class {
32080
32373
  decryptedAny = true;
32081
32374
  this.options.onCommand(frame.sessionId, cmd, frame.clientId);
32082
32375
  }
32083
- if (decryptedAny) conversation.lastActivityAt = this.options.clock.now();
32084
- if (decryptedAny) this.undecryptable.delete(frame.sessionId);
32376
+ if (decryptedAny) {
32377
+ conversation.lastActivityAt = this.options.clock.now();
32378
+ this.options.onBookActivity?.();
32379
+ this.undecryptable.delete(frame.sessionId);
32380
+ }
32085
32381
  }
32086
32382
  /** 两次解不开就作废(与手机端"两帧解不开就丢配对"对称,避免一端永久静默)。 */
32087
32383
  noteUndecryptable(conversationId) {
@@ -32118,7 +32414,10 @@ var RelayClient = class {
32118
32414
  this.options.onState({
32119
32415
  relay,
32120
32416
  ...problem === void 0 ? {} : { problem },
32121
- clients: this.conversations.size,
32417
+ // 之前这里写的是 `conversations.size`,把"会话数"顶替成了"客户端数":
32418
+ // 一部手机解配再重配就多一条会话,于是这个字段会**变大** ——
32419
+ // 拿它回答"现在有几台手机连着"会得出相反的结论。
32420
+ clients: this.conversations.clientCount(),
32122
32421
  conversations: this.conversations.size,
32123
32422
  generation: this.generation
32124
32423
  });
@@ -32230,7 +32529,7 @@ var WATERFALL_EVENTS = [
32230
32529
  "tools/post-execute",
32231
32530
  "tools/code-dispatch-log"
32232
32531
  ];
32233
- var WATERFALL_PARTICIPANTS = ["approval/request"];
32532
+ var WATERFALL_PARTICIPANTS = ["approval/request", "user-questions/request"];
32234
32533
  var allowed = new Set(SUBSCRIBABLE_EVENTS);
32235
32534
  var participants = new Set(WATERFALL_PARTICIPANTS);
32236
32535
  function canParticipate(name) {
@@ -32265,11 +32564,45 @@ function guardedSubscribe(on, name, listener, warn) {
32265
32564
  }
32266
32565
 
32267
32566
  // src/platform/carrier-services.ts
32567
+ var PARTICIPATE_OPTIONS = { global: true, prepend: true };
32568
+ var PARTICIPATED_EVENTS = ["approval/request", "user-questions/request"];
32569
+ function fuseSignals(platform, ours) {
32570
+ if (!platform) return ours;
32571
+ const any2 = AbortSignal.any;
32572
+ if (typeof any2 === "function") return any2.call(AbortSignal, [platform, ours]);
32573
+ const fused = new AbortController();
32574
+ for (const source of [platform, ours]) {
32575
+ if (fused.signal.aborted) break;
32576
+ if (source.aborted) {
32577
+ fused.abort(source.reason);
32578
+ break;
32579
+ }
32580
+ source.addEventListener("abort", () => fused.abort(source.reason), { once: true });
32581
+ }
32582
+ return fused.signal;
32583
+ }
32268
32584
  var MAX_TITLE_BATCH_DEFAULT = 25;
32269
32585
  var TITLE_TIMEOUT_DEFAULT = 4e3;
32270
32586
  var HISTORY_TIMEOUT_DEFAULT = 15e3;
32271
32587
  var CREATE_TIMEOUT_DEFAULT = 8e3;
32272
32588
  var LIST_LIMIT_DEFAULT = 100;
32589
+ var MODEL_FACE_METHODS = [
32590
+ "currentSelection",
32591
+ "list",
32592
+ "listModels",
32593
+ "available",
32594
+ "availableModels",
32595
+ "selections",
32596
+ "options",
32597
+ "get",
32598
+ "set",
32599
+ "select",
32600
+ "setSelection",
32601
+ "setDefault",
32602
+ "update"
32603
+ ];
32604
+ var MODEL_LIST_METHODS = /* @__PURE__ */ new Set(["list", "listModels", "available", "availableModels", "selections", "options"]);
32605
+ var MODEL_SET_METHODS = /* @__PURE__ */ new Set(["set", "select", "setSelection", "setDefault", "update"]);
32273
32606
  function createServicesKernel(services, options) {
32274
32607
  const log = (message, fields = {}) => options.log?.(message, fields);
32275
32608
  const titleCache = /* @__PURE__ */ new Map();
@@ -32278,8 +32611,25 @@ function createServicesKernel(services, options) {
32278
32611
  const unmappedEventTypes = /* @__PURE__ */ new Set();
32279
32612
  const injectedUserMessages = /* @__PURE__ */ new Map();
32280
32613
  const listenerErrors = /* @__PURE__ */ new Map();
32281
- let approvalFace = "not-attached";
32282
- let questionsFace = "pending";
32614
+ const faces = {
32615
+ "approval/request": {
32616
+ registered: "not-attached",
32617
+ calls: 0,
32618
+ last: "not-called",
32619
+ desktopVoided: 0,
32620
+ handoff: "unknown"
32621
+ },
32622
+ "user-questions/request": {
32623
+ registered: "not-attached",
32624
+ calls: 0,
32625
+ last: "not-called",
32626
+ desktopVoided: 0,
32627
+ handoff: "unknown"
32628
+ }
32629
+ };
32630
+ let approvalAsked = 0;
32631
+ let approvalDecided = "not-seen";
32632
+ const modelMethods = [];
32283
32633
  const fn = (owner, name) => {
32284
32634
  if (!owner) return void 0;
32285
32635
  const value = owner[name];
@@ -32303,6 +32653,17 @@ function createServicesKernel(services, options) {
32303
32653
  return void 0;
32304
32654
  }
32305
32655
  }
32656
+ function firstLiveAgent() {
32657
+ const list = fn(services.agents, "list");
32658
+ if (list) {
32659
+ try {
32660
+ const all = list.call(services.agents);
32661
+ if (Array.isArray(all) && all.length) return all[0];
32662
+ } catch {
32663
+ }
32664
+ }
32665
+ return void 0;
32666
+ }
32306
32667
  function agentStatus(sessionId) {
32307
32668
  const agent = liveAgent(sessionId);
32308
32669
  const status = agent?.status;
@@ -32467,6 +32828,27 @@ function createServicesKernel(services, options) {
32467
32828
  return void 0;
32468
32829
  }
32469
32830
  }
32831
+ function probeModelFace() {
32832
+ const owner = services.agentDefaultModel;
32833
+ if (!owner) return { hits: [], canList: false, canSet: false };
32834
+ const hits = [];
32835
+ for (const name of MODEL_FACE_METHODS) {
32836
+ if (fn(owner, name)) hits.push(name);
32837
+ }
32838
+ modelMethods.length = 0;
32839
+ modelMethods.push(...hits);
32840
+ return {
32841
+ hits,
32842
+ canList: hits.some((n) => MODEL_LIST_METHODS.has(n)),
32843
+ canSet: hits.some((n) => MODEL_SET_METHODS.has(n))
32844
+ };
32845
+ }
32846
+ function modelFaceSummary() {
32847
+ if (!services.agentDefaultModel) return "absent";
32848
+ const { hits, canList, canSet } = probeModelFace();
32849
+ if (!hits.length) return `none (tried=${MODEL_FACE_METHODS.length})`;
32850
+ return `${canList ? "list" : "no-list"}+${canSet ? "set" : "no-set"} via=${hits.join(",")}`.slice(0, 180);
32851
+ }
32470
32852
  async function sendPrompt(sessionId, text) {
32471
32853
  const agent = liveAgent(sessionId);
32472
32854
  if (!agent) return { ok: false, message: `\u4F1A\u8BDD\u6CA1\u6709\u6D3B\u7684 agent\uFF08agents.get(${sessionId}) \u4E3A\u7A7A\uFF0C\u8BF7\u5148\u6062\u590D\u4F1A\u8BDD\uFF09` };
@@ -32591,6 +32973,8 @@ function createServicesKernel(services, options) {
32591
32973
  if (!sessionId || !type) return;
32592
32974
  seenEventTypes.add(type);
32593
32975
  const data = event?.data ?? {};
32976
+ if (type === "approval/asked") approvalAsked += 1;
32977
+ if (type === "approval/decided") approvalDecided = String(data.outcome ?? "?");
32594
32978
  for (const mapped of sessionEventKernelEvents({ sessionId, type, seq: event?.seq, data })) {
32595
32979
  if (mapped.kind === "title") titleCache.set(sessionId, { title: mapped.title, at: options.clock.now() });
32596
32980
  onEvent(mapped);
@@ -32617,59 +33001,34 @@ function createServicesKernel(services, options) {
32617
33001
  sink = next;
32618
33002
  const disposers = [];
32619
33003
  const on = services.on;
32620
- if (typeof on === "function") {
32621
- const name = "approval/request";
32622
- if (canParticipate(name)) {
32623
- const listener = (...args) => participate(args);
32624
- try {
32625
- const disposer = on.call(services, name, listener);
32626
- approvalFace = "registered";
32627
- disposers.push(() => {
32628
- if (typeof disposer === "function") disposer();
32629
- else if (typeof services.off === "function") services.off(name, listener);
32630
- });
32631
- } catch (error62) {
32632
- approvalFace = `failed: ${messageOf3(error62)}`.slice(0, 80);
32633
- log("approval/request participation failed", { message: messageOf3(error62) });
32634
- }
32635
- } else {
32636
- approvalFace = "refused by guard";
32637
- }
32638
- } else {
32639
- approvalFace = "no on()";
32640
- }
32641
- const questions = services.userQuestions;
32642
- if (!options.takeOverQuestions) {
32643
- questionsFace = "not-taken-over";
32644
- } else if (!questions || typeof questions.registerProvider !== "function") {
32645
- questionsFace = `no registerProvider (keys=${questions ? shapeOf(questions) : "service absent"})`.slice(0, 120);
32646
- }
32647
- if (options.takeOverQuestions && questions && typeof questions.registerProvider === "function") {
32648
- const provider = {
32649
- ask: async (request) => {
32650
- const agent = request.agent;
32651
- const sessionId = String(agent?.session?.id ?? request.sessionId ?? "");
32652
- const items = Array.isArray(request.questions) ? request.questions : [];
32653
- const mapped = items.map((item, index) => mapQuestion(item, index));
32654
- const answer = await sink?.question({
32655
- sessionId,
32656
- questions: mapped,
32657
- ...request.signal ? { signal: request.signal } : {}
32658
- });
32659
- if (!answer) {
32660
- throw new Error("\u624B\u673A\u7AEF\u672A\u5E94\u7B54\u8FD9\u6B21\u63D0\u95EE\uFF08\u672A\u914D\u5BF9\u3001\u4E0D\u5728\u7EBF\u6216\u5DF2\u8D85\u65F6\uFF09");
32661
- }
32662
- return { answers: answer.answers };
32663
- }
33004
+ if (typeof on !== "function") {
33005
+ for (const name of PARTICIPATED_EVENTS) faces[name].registered = "no on()";
33006
+ return () => {
33007
+ sink = void 0;
32664
33008
  };
33009
+ }
33010
+ for (const name of PARTICIPATED_EVENTS) {
33011
+ const face = faces[name];
33012
+ if (!canParticipate(name)) {
33013
+ face.registered = "refused by guard";
33014
+ continue;
33015
+ }
33016
+ const listener = (...args) => participate(name, args);
32665
33017
  try {
32666
- const dispose = questions.registerProvider.call(questions, provider);
32667
- if (typeof dispose === "function") disposers.push(dispose);
32668
- questionsFace = "registered";
32669
- log("question provider taken over from the desktop UI");
33018
+ const disposer = on.call(
33019
+ services,
33020
+ name,
33021
+ listener,
33022
+ PARTICIPATE_OPTIONS
33023
+ );
33024
+ face.registered = "registered";
33025
+ disposers.push(() => {
33026
+ if (typeof disposer === "function") disposer();
33027
+ else if (typeof services.off === "function") services.off(name, listener);
33028
+ });
32670
33029
  } catch (error62) {
32671
- questionsFace = `failed: ${messageOf3(error62)}`.slice(0, 80);
32672
- log("question provider registration failed", { message: messageOf3(error62) });
33030
+ face.registered = `failed: ${messageOf3(error62)}`.slice(0, 80);
33031
+ log(`${name} participation failed`, { message: messageOf3(error62) });
32673
33032
  }
32674
33033
  }
32675
33034
  return () => {
@@ -32682,23 +33041,103 @@ function createServicesKernel(services, options) {
32682
33041
  }
32683
33042
  };
32684
33043
  }
32685
- async function participate(args) {
33044
+ async function participate(event, args) {
33045
+ const isApproval = event === "approval/request";
33046
+ const face = faces[event];
33047
+ face.calls += 1;
32686
33048
  const request = args.find(
32687
- (arg) => arg && typeof arg === "object" && typeof arg.toolName === "string"
33049
+ (arg) => Boolean(arg) && typeof arg === "object" && (isApproval ? typeof arg.toolName === "string" : Array.isArray(arg.questions))
32688
33050
  );
32689
33051
  const next = args.find((arg) => typeof arg === "function");
32690
33052
  const sessionId = String(request?.agent?.session?.id ?? "");
32691
33053
  if (!sink || !sessionId || !servicesPeers()) {
32692
- return String(await next?.() ?? "unavailable");
33054
+ face.last = "handed-back(no phone target)";
33055
+ const value = await next?.();
33056
+ return isApproval ? String(value ?? "unavailable") : value;
33057
+ }
33058
+ const controller = new AbortController();
33059
+ const platform = request?.signal;
33060
+ if (platform) {
33061
+ if (platform.aborted) controller.abort("platform");
33062
+ else platform.addEventListener("abort", () => controller.abort("platform"), { once: true });
33063
+ }
33064
+ const canceller = new AbortController();
33065
+ let handedOff = false;
33066
+ if (request) {
33067
+ try {
33068
+ const fused = fuseSignals(platform, canceller.signal);
33069
+ request.signal = fused;
33070
+ handedOff = request.signal === fused;
33071
+ face.handoff = handedOff ? "fused" : "ignored";
33072
+ } catch (error62) {
33073
+ face.handoff = `failed: ${messageOf3(error62)}`.slice(0, 80);
33074
+ }
32693
33075
  }
32694
- const decision = await sink.approval({
33076
+ const finish = (reason) => {
33077
+ if (!canceller.signal.aborted) canceller.abort(reason);
33078
+ };
33079
+ const desktop = Promise.resolve().then(() => next?.()).then(
33080
+ (value) => (
33081
+ // `undefined` 是"这个参与者没认领"(会把整条链冲掉),不是答案;
33082
+ // `'unavailable'` 是审批那条链末端的"没人可问",也不是答案。
33083
+ value === void 0 || isApproval && value === "unavailable" ? { answered: false, value } : { answered: true, value }
33084
+ ),
33085
+ (error62) => ({ answered: false, error: error62 })
33086
+ );
33087
+ const asked = isApproval ? sink.approval({
32695
33088
  sessionId,
32696
33089
  action: String(request?.toolName ?? "\u5DE5\u5177\u8C03\u7528"),
32697
33090
  ...request?.reason ? { reason: String(request.reason) } : {},
32698
- ...request?.signal ? { signal: request.signal } : {}
33091
+ signal: controller.signal
33092
+ }) : sink.question({
33093
+ sessionId,
33094
+ questions: (request?.questions ?? []).map((item, index) => mapQuestion(item, index)),
33095
+ signal: controller.signal
32699
33096
  });
32700
- if (decision === "decline") return String(await next?.() ?? "unavailable");
32701
- return decision;
33097
+ const phone = asked.then(
33098
+ (value) => (
33099
+ // 每一支都**带着自己是谁**回来:`'rejected'` 这个词手机和桌面都可能给出,
33100
+ // 只比数值就没法知道该收哪一张卡(第一版就是这么错的)。
33101
+ isApproval ? value === "allowed-once" || value === "rejected" ? { answered: true, value } : { answered: false, value } : value === null || value === void 0 ? { answered: false, value } : { answered: true, value }
33102
+ ),
33103
+ // runtime 抛了(未配对、不在线、被撤)同样不算答案。
33104
+ () => ({ answered: false })
33105
+ );
33106
+ const pending = new Promise(() => {
33107
+ });
33108
+ const winner = await Promise.race([
33109
+ phone.then((chain) => chain.answered ? { src: "phone", chain } : pending),
33110
+ desktop.then((chain) => chain.answered ? { src: "desktop", chain } : pending),
33111
+ Promise.all([phone, desktop]).then(([, chain]) => ({ src: "desktop", chain }))
33112
+ ]);
33113
+ if (!winner.chain.answered) {
33114
+ controller.abort("desktop");
33115
+ finish("settled-without-answer");
33116
+ if (isApproval) {
33117
+ face.last = "handed-back(neither answered)";
33118
+ return "unavailable";
33119
+ }
33120
+ face.last = "no-answer";
33121
+ if (winner.chain.error !== void 0) throw winner.chain.error;
33122
+ throw new Error("\u624B\u673A\u7AEF\u672A\u5E94\u7B54\u8FD9\u6B21\u63D0\u95EE\uFF08\u672A\u914D\u5BF9\u3001\u4E0D\u5728\u7EBF\u6216\u5DF2\u8D85\u65F6\uFF09");
33123
+ }
33124
+ if (winner.src === "desktop") controller.abort("desktop");
33125
+ if (winner.src === "phone") {
33126
+ finish("answered-by-phone");
33127
+ if (handedOff) face.desktopVoided += 1;
33128
+ } else {
33129
+ finish("answered-by-desktop");
33130
+ }
33131
+ face.last = `${winner.src === "phone" ? "answered-by-phone" : "answered-by-desktop"}(${describeOutcome(
33132
+ isApproval,
33133
+ winner.chain.value
33134
+ )})`;
33135
+ return winner.chain.value;
33136
+ }
33137
+ function describeOutcome(isApproval, value) {
33138
+ if (isApproval) return String(value);
33139
+ const answers = value?.answers;
33140
+ return Array.isArray(answers) ? `${answers.length} \u9879` : typeof value;
32702
33141
  }
32703
33142
  function servicesPeers() {
32704
33143
  return sink !== void 0;
@@ -32737,6 +33176,22 @@ function createServicesKernel(services, options) {
32737
33176
  // 与发指令/续跑走同一条读取路径:这里原来还有一份实现,而且是 `current()` 裸调用
32738
33177
  // (不带 receiver;真要用 this 的服务方法会当场抛),两份迟早分叉。
32739
33178
  modelSelection: currentSelection,
33179
+ /**
33180
+ * 可切换模型清单。**这一代内核给不出清单也换不了**(`agentDefaultModel` 上只有
33181
+ * `currentSelection`),所以这里报 `canSwitch: false` 并附一句人话。
33182
+ *
33183
+ * 报false 而不是省略这个方法:省略会被 core 当成"只读"但**说不出原因**,
33184
+ * 手机上就只剩一个置灰的模型名,用户不知道是主机不支持还是自己哪里做错了。
33185
+ */
33186
+ modelOptions() {
33187
+ const { hits, canList, canSet } = probeModelFace();
33188
+ return {
33189
+ // 两个都要有才算能切:只有 list 没有 set 时手机上会列出候选却点了没反应,
33190
+ // 那比"换不了"更糟。
33191
+ canSwitch: canList && canSet,
33192
+ reason: canList && canSet ? void 0 : `\u4E3B\u673A\u5185\u6838\u53EA\u63D0\u4F9B\u8BFB\u53D6\uFF08\u547D\u4E2D\uFF1A${hits.join(",") || "\u65E0"}\uFF09`
33193
+ };
33194
+ },
32740
33195
  describe() {
32741
33196
  return {
32742
33197
  carrier: "services",
@@ -32748,19 +33203,47 @@ function createServicesKernel(services, options) {
32748
33203
  // 新建会话只依赖这一条。和 historyFace 同理:真机上"新建没反应"第一件要分清的事
32749
33204
  // 是宿主没有这个能力,还是我们没调对。
32750
33205
  createFace: typeof fn(services.sessionController?.commands, "create") === "function" ? "commands.create" : `absent (keys=${shapeOf(services.sessionController)})`.slice(0, 120),
33206
+ // 第二条换模型的可能路径:命令控制器上自带的方法(`setModel` / `switchModel`…),
33207
+ // 以及活agent 自己暴露的模型字段。前者能列出来就说明这条路通不通。
33208
+ commandsFace: shapeOf(services.sessionController?.commands),
33209
+ agentModelFields: (() => {
33210
+ const first = firstLiveAgent();
33211
+ if (!first) return "no-live-agent";
33212
+ return Object.keys(first).filter((k) => /model|provider|preset/i.test(k)).slice(0, 8).join(",") || "none";
33213
+ })(),
32751
33214
  agents: typeof services.agents === "object",
32752
33215
  agentDefaultModel: typeof services.agentDefaultModel === "object",
33216
+ // 模型面到底有没有"写"的能力(能列可选、能切换)。手机上模型下拉该不该置灰,
33217
+ // 取决于这个而不是取决于"读得到当前值" —— 读得到只够显示一行文字。
33218
+ modelFace: modelFaceSummary(),
32753
33219
  workspaceRegistry: typeof services.workspaceRegistry === "object",
32754
33220
  userQuestions: typeof services.userQuestions === "object",
33221
+ // 这个键**已经不生效**了:提问现在跟审批一样是 waterfall 参与者,两端同弹、
33222
+ // 谁先答谁算,不再有"接管/取代桌面"这回事(那条 `registerProvider` 单提供者
33223
+ // 路线在这一代宿主上根本不存在)。这里照实把配置值报出来,真正的状态看
33224
+ // `questionsFace`。(删键要动 config.ts/index.ts,那两棵树此刻有并行会话在飞的改动。)
32755
33225
  takeOverQuestions: options.takeOverQuestions === true,
33226
+ takeOverQuestionsInert: options.takeOverQuestions === true,
32756
33227
  archivedSessions: archivedIds().size,
32757
33228
  hasOn: typeof services.on === "function",
32758
33229
  // 真机排错的第一现场:内核到底发了哪些事件类型、其中哪些我们没认。
32759
33230
  eventTypes: [...seenEventTypes].slice(-24).join("|"),
32760
33231
  unmappedEventTypes: [...unmappedEventTypes].join("|"),
32761
33232
  injectedUserMessages: [...injectedUserMessages].map(([kind, count]) => `${count}\xD7${kind}`).join("|") || "none",
32762
- approvalFace,
32763
- questionsFace,
33233
+ approvalFace: faces["approval/request"].registered,
33234
+ approvalCalls: faces["approval/request"].calls,
33235
+ approvalLast: faces["approval/request"].last,
33236
+ approvalAsked,
33237
+ approvalDecided,
33238
+ approvalDesktopVoided: faces["approval/request"].desktopVoided,
33239
+ approvalSignalHandoff: faces["approval/request"].handoff,
33240
+ // 提问那一条与审批同构,所以读数也同一套:登记结果 / 被派发几次 / 最后走到哪一步 /
33241
+ // 手机先答时撤销了几次下游 signal / 那份 signal 换没换成功。
33242
+ questionsFace: faces["user-questions/request"].registered,
33243
+ questionsCalls: faces["user-questions/request"].calls,
33244
+ questionsLast: faces["user-questions/request"].last,
33245
+ questionsDesktopVoided: faces["user-questions/request"].desktopVoided,
33246
+ questionsSignalHandoff: faces["user-questions/request"].handoff,
32764
33247
  listenerErrors: [...listenerErrors].slice(0, 4).map(([key, count]) => `${count}\xD7${key}`).join(" | ") || "none"
32765
33248
  };
32766
33249
  }
@@ -32782,17 +33265,19 @@ function sessionEventKernelEvents(input2) {
32782
33265
  switch (type) {
32783
33266
  case "turn/start":
32784
33267
  return [{ kind: "run-state", sessionId, state: "running" }];
32785
- case "assistant/message":
33268
+ case "assistant/message": {
33269
+ const text = stripToolCallXml(textOf(data));
32786
33270
  return [
32787
33271
  {
32788
33272
  kind: "delta",
32789
33273
  sessionId,
32790
33274
  messageId: messageIdOf(data, input2.seq),
32791
- text: textOf(data),
33275
+ text,
32792
33276
  role: "assistant",
32793
33277
  done: true
32794
33278
  }
32795
33279
  ];
33280
+ }
32796
33281
  case "user/message": {
32797
33282
  if (!isHumanUserMessage(data)) return [];
32798
33283
  return [
@@ -32815,8 +33300,8 @@ function sessionEventKernelEvents(input2) {
32815
33300
  callId: String(data.callId ?? data.id ?? `tool_${input2.seq ?? 0}`),
32816
33301
  phase: argsRaw === void 0 || argsRaw === null ? "started" : "args",
32817
33302
  tool: typeof data.name === "string" ? data.name : void 0,
32818
- title: typeof data.title === "string" ? data.title : previewOf(argsRaw),
32819
- ...argsRaw === void 0 ? {} : { argsPreview: previewOf(argsRaw) }
33303
+ title: typeof data.title === "string" ? data.title : argsTitle(argsRaw),
33304
+ ...argsRaw === void 0 ? {} : { argsPreview: argsPreviewOf(argsRaw) }
32820
33305
  }
32821
33306
  ];
32822
33307
  }
@@ -32967,9 +33452,67 @@ function messageIdOf(data, seq) {
32967
33452
  if (typeof id === "string" && id) return id;
32968
33453
  return `msg_${String(seq ?? Date.now())}`;
32969
33454
  }
32970
- function previewOf(value) {
33455
+ var TOOL_CALL_BLOCK = /<tool_call>\s*<function=[\s\S]*?<\/tool_call>/g;
33456
+ var TOOL_CALL_UNCLOSED = /<tool_call>\s*<function=[\s\S]*$/;
33457
+ function stripToolCallXml(text) {
33458
+ const stripped = text.replace(TOOL_CALL_BLOCK, "").replace(TOOL_CALL_UNCLOSED, "");
33459
+ return stripped.trim() === "" ? "" : stripped;
33460
+ }
33461
+ function argsTitle(argsRaw) {
33462
+ const parsed = tryParseJson(argsRaw);
33463
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
33464
+ const record2 = parsed;
33465
+ for (const key of ["description", "prompt", "command", "path", "file_path", "pattern", "query", "url"]) {
33466
+ const value = record2[key];
33467
+ if (typeof value === "string" && value.trim()) return oneLine(value);
33468
+ }
33469
+ }
33470
+ return previewOf(argsRaw);
33471
+ }
33472
+ function argsPreviewOf(argsRaw) {
33473
+ const parsed = tryParseJson(argsRaw);
33474
+ if (parsed !== void 0) {
33475
+ try {
33476
+ return previewOf(JSON.stringify(parsed, null, 2));
33477
+ } catch {
33478
+ }
33479
+ }
33480
+ return previewOf(argsRaw);
33481
+ }
33482
+ function tryParseJson(value) {
33483
+ if (typeof value !== "string" || !value.trim()) return void 0;
33484
+ try {
33485
+ return JSON.parse(value);
33486
+ } catch {
33487
+ return void 0;
33488
+ }
33489
+ }
33490
+ function oneLine(text) {
33491
+ const flat = text.replace(/\s+/g, " ").trim();
33492
+ return flat.length > 120 ? `${flat.slice(0, 120)}\u2026` : flat;
33493
+ }
33494
+ function readableText(value) {
32971
33495
  if (value === void 0 || value === null) return void 0;
32972
- const text = typeof value === "string" ? value : safeJson(value);
33496
+ if (typeof value === "string") return value;
33497
+ if (typeof value === "number" || typeof value === "boolean") return String(value);
33498
+ if (Array.isArray(value)) {
33499
+ const parts = value.map((part) => readableText(part)).filter((part) => typeof part === "string" && part.length > 0);
33500
+ return parts.length > 0 ? parts.join("\n") : void 0;
33501
+ }
33502
+ if (typeof value === "object") {
33503
+ const record2 = value;
33504
+ for (const key of ["content", "text", "output", "stdout", "stderr", "message", "result", "error"]) {
33505
+ if (record2[key] === void 0 || record2[key] === null) continue;
33506
+ const nested = readableText(record2[key]);
33507
+ if (nested !== void 0 && nested !== "") return nested;
33508
+ }
33509
+ if (typeof record2.type === "string" && record2.type !== "text") return void 0;
33510
+ return safeJson(record2);
33511
+ }
33512
+ return safeJson(value);
33513
+ }
33514
+ function previewOf(value) {
33515
+ const text = readableText(value);
32973
33516
  if (!text) return void 0;
32974
33517
  return text.length > 200 ? `${text.slice(0, 200)}\u2026` : text;
32975
33518
  }
@@ -33087,9 +33630,174 @@ var StatusFile = class {
33087
33630
  }
33088
33631
  };
33089
33632
 
33633
+ // src/shell/host-id.ts
33634
+ import { randomBytes as randomBytes4 } from "node:crypto";
33635
+ import { chmodSync, mkdirSync as mkdirSync2, readFileSync, writeFileSync as writeFileSync2 } from "node:fs";
33636
+ import path2 from "node:path";
33637
+ var HOST_ID = /^[A-Za-z0-9][A-Za-z0-9_.-]{0,63}$/;
33638
+ function newHostId() {
33639
+ return `drc-${randomBytes4(6).toString("hex")}`;
33640
+ }
33641
+ function resolveHostId(dir, configured) {
33642
+ if (configured) return configured;
33643
+ if (!dir) return newHostId();
33644
+ const file2 = path2.join(dir, "host-id");
33645
+ let existing;
33646
+ try {
33647
+ existing = readFileSync(file2, "utf8").trim();
33648
+ } catch {
33649
+ existing = void 0;
33650
+ }
33651
+ if (existing !== void 0 && HOST_ID.test(existing)) return existing;
33652
+ const fresh = newHostId();
33653
+ try {
33654
+ mkdirSync2(dir, { recursive: true });
33655
+ writeFileSync2(file2, `${fresh}
33656
+ `, {
33657
+ encoding: "utf8",
33658
+ mode: 384,
33659
+ flag: existing === void 0 ? "wx" : "w"
33660
+ });
33661
+ chmodSync(file2, 384);
33662
+ return fresh;
33663
+ } catch {
33664
+ try {
33665
+ const raced = readFileSync(file2, "utf8").trim();
33666
+ if (HOST_ID.test(raced)) return raced;
33667
+ } catch {
33668
+ }
33669
+ return fresh;
33670
+ }
33671
+ }
33672
+
33673
+ // src/shell/pair-store.ts
33674
+ import { chmodSync as chmodSync2, mkdirSync as mkdirSync3, readFileSync as readFileSync2, renameSync as renameSync2, unlinkSync, writeFileSync as writeFileSync3 } from "node:fs";
33675
+ import { createHash as createHash2 } from "node:crypto";
33676
+ import path3 from "node:path";
33677
+ var storedConversationSchema = external_exports.object({
33678
+ id: external_exports.string().min(1),
33679
+ /** 配对 PSK(base64)。两把方向密钥在恢复时由它现派生,不落盘。 */
33680
+ psk: external_exports.string().min(1),
33681
+ /** host→client 方向的本地序号。中继会重新编号,客户端从不读它(F13),
33682
+ * 但保留它能让恢复后的编号不跳号,便于对拍排查。 */
33683
+ seqHost: external_exports.number().int().nonnegative(),
33684
+ createdAt: external_exports.number().finite().nonnegative(),
33685
+ lastActivityAt: external_exports.number().finite().nonnegative()
33686
+ });
33687
+ var storeSchema = external_exports.object({
33688
+ version: external_exports.literal(1),
33689
+ /** 这个文件属于哪台主机:换了主机身份就不该认(凭据不该跨身份复用)。 */
33690
+ hostId: external_exports.string().min(1),
33691
+ savedAt: external_exports.number().finite().nonnegative(),
33692
+ conversations: external_exports.array(storedConversationSchema)
33693
+ });
33694
+ function safeSegment(hostId) {
33695
+ const cleaned = hostId.replace(/[^A-Za-z0-9._-]/g, "_").slice(0, 48);
33696
+ const tag = createHash2("sha256").update(hostId).digest("hex").slice(0, 8);
33697
+ return `${cleaned || "host"}-${tag}`;
33698
+ }
33699
+ function defaultPairStoreFile(statusFile, hostId) {
33700
+ if (!statusFile) return "";
33701
+ return path3.join(path3.dirname(statusFile), `conversations-${safeSegment(hostId)}.json`);
33702
+ }
33703
+ var PairStore = class {
33704
+ constructor(options) {
33705
+ this.options = options;
33706
+ }
33707
+ options;
33708
+ /** 上一次成功写盘的时刻,供 status.json 回答"这份落盘是什么时候的"。 */
33709
+ savedAt = 0;
33710
+ /** 上次 save 之后发生过任何变化(含只改了 lastActivityAt)。 */
33711
+ dirty = false;
33712
+ get file() {
33713
+ return this.options.file;
33714
+ }
33715
+ get lastSavedAt() {
33716
+ return this.savedAt;
33717
+ }
33718
+ /** 有没有待落盘的变更。status 的 3 秒 tick 用它决定是否真写盘。 */
33719
+ get hasPendingChanges() {
33720
+ return this.dirty;
33721
+ }
33722
+ /** 只标脏,不写盘。高频变动(每次广播都动 `seqHost`)走这条。 */
33723
+ markDirty() {
33724
+ this.dirty = true;
33725
+ }
33726
+ /**
33727
+ * 读回本机的会话记录。
33728
+ *
33729
+ * 三种"读不出来"都归到**空簿 + 一条日志**,绝不抛(红线:插件起不来比丢配对更糟):
33730
+ * 文件不存在(第一次跑)、JSON 坏了(半写/被手改)、形状不认识(版本升级)。
33731
+ * `hostId` 不匹配也算读不出来:那份文件属于另一台主机。
33732
+ */
33733
+ load() {
33734
+ const { file: file2, hostId, log } = this.options;
33735
+ if (!file2) return [];
33736
+ let raw;
33737
+ try {
33738
+ raw = readFileSync2(file2, "utf8");
33739
+ } catch {
33740
+ return [];
33741
+ }
33742
+ const parsed = storeSchema.safeParse(safeJsonParse(raw));
33743
+ if (!parsed.success) {
33744
+ log?.("pair-store unreadable, starting with an empty book", {
33745
+ reason: parsed.error.issues[0]?.message?.slice(0, 80) ?? "shape mismatch"
33746
+ });
33747
+ return [];
33748
+ }
33749
+ if (parsed.data.hostId !== hostId) {
33750
+ log?.("pair-store belongs to another host, starting with an empty book", {
33751
+ fileHostId: parsed.data.hostId.slice(0, 24)
33752
+ });
33753
+ return [];
33754
+ }
33755
+ this.savedAt = parsed.data.savedAt;
33756
+ return parsed.data.conversations;
33757
+ }
33758
+ /**
33759
+ * 写盘。**结构性的变化(新建/作废会话)必须立刻调这个**;
33760
+ * 只改了 `lastActivityAt`/`seqHost` 的场合走 {@link markDirty},由 3 秒 tick 批量落。
33761
+ *
33762
+ * 失败只记日志:写不进盘(目录只读、磁盘满)时,本次运行仍能正常收发,
33763
+ * 只是下次重启要重新扫码 —— 那是可接受的退化,让插件崩掉不是。
33764
+ */
33765
+ save(conversations, now) {
33766
+ const { file: file2, hostId, log } = this.options;
33767
+ if (!file2) return false;
33768
+ const body = JSON.stringify({ version: 1, hostId, savedAt: now, conversations }) + "\n";
33769
+ const tmp = `${file2}.tmp-${process.pid}`;
33770
+ try {
33771
+ mkdirSync3(path3.dirname(file2), { recursive: true });
33772
+ writeFileSync3(tmp, body, { encoding: "utf8", mode: 384 });
33773
+ chmodSync2(tmp, 384);
33774
+ renameSync2(tmp, file2);
33775
+ this.savedAt = now;
33776
+ this.dirty = false;
33777
+ return true;
33778
+ } catch (error62) {
33779
+ log?.("pair-store save failed (pairing will need a rescan after the next restart)", {
33780
+ message: String(error62?.message ?? error62).slice(0, 120)
33781
+ });
33782
+ try {
33783
+ unlinkSync(tmp);
33784
+ } catch {
33785
+ }
33786
+ return false;
33787
+ }
33788
+ }
33789
+ };
33790
+ function safeJsonParse(raw) {
33791
+ try {
33792
+ return JSON.parse(raw);
33793
+ } catch {
33794
+ return void 0;
33795
+ }
33796
+ }
33797
+
33090
33798
  // src/shell/config.ts
33091
33799
  import { homedir } from "node:os";
33092
- import path2 from "node:path";
33800
+ import path4 from "node:path";
33093
33801
  var DEFAULT_CONFIG = {
33094
33802
  enabled: true,
33095
33803
  serverUrl: "ws://127.0.0.1:8787",
@@ -33099,7 +33807,7 @@ var DEFAULT_CONFIG = {
33099
33807
  hostLabel: "dsh-host",
33100
33808
  keepAwake: { enabled: true, idleReleaseSec: 300, keepDisplay: false },
33101
33809
  mockBridge: false,
33102
- statusFile: path2.join(homedir(), ".dsh", "dsh-remote-control", "status.json"),
33810
+ statusFile: path4.join(homedir(), ".dsh", "dsh-remote-control", "status.json"),
33103
33811
  unarchiveOnPrompt: true,
33104
33812
  pairOnStartSec: 0,
33105
33813
  pairTtlMs: 12e4,
@@ -33108,10 +33816,23 @@ var DEFAULT_CONFIG = {
33108
33816
  approvalTimeoutSec: 180,
33109
33817
  listingRefreshSec: 15,
33110
33818
  conversationIdleTtlSec: 86400,
33819
+ // 恢复出来的会话按 7 天剪,与中继的 DRC_CONV_IDLE_TTL_MS 对齐(理由见该字段注释)。
33820
+ restoredIdleTtlSec: 604800,
33821
+ // 密钥簿落盘:默认跟着 statusFile 走(见 `resolvePairStoreFile`)。
33822
+ pairStoreFile: "",
33111
33823
  // 配对的唯一入口。关掉它 = 这台主机**没有**配对入口(`/drc pair` 与文本二维码都在
33112
33824
  // 2026-10-03 删掉了),所以 index.ts 会把它记成一条 warn 而不是安静地什么都不做。
33113
33825
  pill: { enabled: true }
33114
33826
  };
33827
+ var PAIR_STORE_OFF = "off";
33828
+ function resolvePairStoreFile(config2, hostId) {
33829
+ if (config2.pairStoreFile === PAIR_STORE_OFF) return "";
33830
+ const configured = config2.pairStoreFile.trim();
33831
+ if (!configured) return defaultPairStoreFile(config2.statusFile, hostId);
33832
+ if (configured.startsWith("/")) return configured;
33833
+ if (!config2.statusFile) return configured;
33834
+ return path4.join(path4.dirname(config2.statusFile), configured);
33835
+ }
33115
33836
  var loopback = /^wss?:\/\/(localhost|127\.0\.0\.1|\[::1\])(:|\/|$)/;
33116
33837
  var wsUrl = /^wss?:\/\/.+/;
33117
33838
  function envSeconds(value) {
@@ -33196,6 +33917,14 @@ function validateConfig(config2) {
33196
33917
  });
33197
33918
  config2.conversationIdleTtlSec = 86400;
33198
33919
  }
33920
+ if (!secondsSchema.safeParse(config2.restoredIdleTtlSec).success || config2.restoredIdleTtlSec <= 0) {
33921
+ problems.push({
33922
+ level: "warn",
33923
+ field: "restoredIdleTtlSec",
33924
+ message: '\u5FC5\u987B\u662F\u6B63\u6570\u79D2\uFF1B\u8FD9\u91CC\u4E0D\u63A5\u53D7"\u6C38\u4E0D\u526A\u679D"\uFF08\u65E0\u754C\u7684 PSK \u7C3F\u662F\u590D\u6838 \u{1F7E1}8 \u7684\u539F\u59CB\u7F3A\u9677\uFF09\uFF0C\u5DF2\u5939\u56DE 604800'
33925
+ });
33926
+ config2.restoredIdleTtlSec = 604800;
33927
+ }
33199
33928
  if (config2.pairOnStartSec > 0) {
33200
33929
  problems.push({
33201
33930
  level: "warn",
@@ -33221,7 +33950,7 @@ function redact(value) {
33221
33950
  }
33222
33951
 
33223
33952
  // src/pill/routes.ts
33224
- import { createHash as createHash2 } from "node:crypto";
33953
+ import { createHash as createHash3 } from "node:crypto";
33225
33954
 
33226
33955
  // src/platform/qr.ts
33227
33956
  var import_qrcode = __toESM(require_lib(), 1);
@@ -33238,10 +33967,11 @@ async function qrPng(text, options = {}) {
33238
33967
  var PAIR_NEW_ROUTE = "/plugins/dsh-remote-control/pairing/new";
33239
33968
  var PAIR_IMAGE_ROUTE = "/plugins/dsh-remote-control/pairing.png";
33240
33969
  var PAIR_STATUS_ROUTE = "/plugins/dsh-remote-control/status";
33970
+ var PAIR_UNPAIR_ROUTE = "/plugins/dsh-remote-control/unpair";
33241
33971
  var PAIR_MARKER_HEADER = "x-drc-pair";
33242
33972
  var PAIR_MARKER_VALUE = "1";
33243
33973
  function pairingEpoch(qr) {
33244
- return createHash2("sha256").update(qr).digest("hex").slice(0, 16);
33974
+ return createHash3("sha256").update(qr).digest("hex").slice(0, 16);
33245
33975
  }
33246
33976
  async function renderPairingPng(qr) {
33247
33977
  return qrPng(qr);
@@ -33368,20 +34098,44 @@ function pillStatusHandler(deps) {
33368
34098
  json2(response, 200, {
33369
34099
  relay: status.relay,
33370
34100
  paired: status.paired,
33371
- hostLabel: status.hostLabel,
33372
- serverUrl: status.serverUrl
34101
+ serverUrl: status.serverUrl,
34102
+ version: status.version,
34103
+ waiting: status.waiting,
34104
+ waitingOldestSec: status.waitingOldestSec
33373
34105
  });
33374
34106
  } catch (error62) {
33375
34107
  json2(response, 500, { error: String(error62?.message ?? error62).slice(0, 200) });
33376
34108
  }
33377
34109
  };
33378
34110
  }
34111
+ function unpairHandler(deps) {
34112
+ return (request, response) => {
34113
+ if (request.method !== "POST") {
34114
+ json2(response, 405, { error: "method not allowed" });
34115
+ return;
34116
+ }
34117
+ const guard = rejectedBy(request);
34118
+ if (guard) {
34119
+ deps.log("\u89E3\u914D\u8BF7\u6C42\u88AB\u5B88\u536B\u62D2", { guard });
34120
+ json2(response, 403, { error: "request-not-trusted", guard });
34121
+ return;
34122
+ }
34123
+ try {
34124
+ const unpaired = deps.unpair();
34125
+ deps.log("\u7ECF\u72B6\u6001\u680F\u9000\u51FA\u914D\u5BF9", { unpaired });
34126
+ json2(response, 200, { state: "ok", unpaired });
34127
+ } catch (error62) {
34128
+ json2(response, 500, { error: String(error62?.message ?? error62).slice(0, 200) });
34129
+ }
34130
+ };
34131
+ }
33379
34132
  function registerPillRoutes(webServer, deps) {
33380
34133
  const off = [];
33381
34134
  const definitions = [
33382
34135
  { kind: "exact", path: PAIR_NEW_ROUTE, handler: pairNewHandler(deps) },
33383
34136
  { kind: "exact", path: PAIR_IMAGE_ROUTE, handler: pairImageHandler(deps) },
33384
- { kind: "exact", path: PAIR_STATUS_ROUTE, handler: pillStatusHandler(deps) }
34137
+ { kind: "exact", path: PAIR_STATUS_ROUTE, handler: pillStatusHandler(deps) },
34138
+ { kind: "exact", path: PAIR_UNPAIR_ROUTE, handler: unpairHandler(deps) }
33385
34139
  ];
33386
34140
  try {
33387
34141
  for (const definition of definitions) off.push(webServer.register(definition));
@@ -33420,6 +34174,7 @@ function startPill(ctx, deps) {
33420
34174
  ensureFresh: () => deps.ensureFresh(),
33421
34175
  current: () => deps.current(),
33422
34176
  status: () => deps.status(),
34177
+ unpair: () => deps.unpair(),
33423
34178
  log
33424
34179
  });
33425
34180
  probe.routes = "registered";
@@ -33505,6 +34260,9 @@ function startPill(ctx, deps) {
33505
34260
  };
33506
34261
  }
33507
34262
 
34263
+ // src/version.ts
34264
+ var PLUGIN_VERSION = true ? "2.0.0" : "dev";
34265
+
33508
34266
  // src/index.ts
33509
34267
  var SERVICE_NAMES = [
33510
34268
  "sessions",
@@ -33559,7 +34317,7 @@ function applyInner(ctx, injected) {
33559
34317
  const clock = systemClock;
33560
34318
  const oneShot = createOneShotTimers(clock);
33561
34319
  const status = new StatusFile(config2.statusFile, clock);
33562
- const problems = validateConfig(config2);
34320
+ const hostId = resolveHostId(config2.statusFile ? path5.dirname(config2.statusFile) : "", config2.hostId);
33563
34321
  const log = (message, fields = {}) => {
33564
34322
  try {
33565
34323
  process.stdout.write(
@@ -33569,6 +34327,10 @@ function applyInner(ctx, injected) {
33569
34327
  } catch {
33570
34328
  }
33571
34329
  };
34330
+ const pairStoreFile = resolvePairStoreFile(config2, hostId);
34331
+ const pairStore = new PairStore({ file: pairStoreFile, hostId, log });
34332
+ const restoredAtBoot = pairStoreFile ? pairStore.load() : [];
34333
+ const problems = validateConfig(config2);
33572
34334
  for (const problem of problems) {
33573
34335
  log(`config ${problem.level}`, { field: problem.field, message: problem.message });
33574
34336
  }
@@ -33608,8 +34370,8 @@ function applyInner(ctx, injected) {
33608
34370
  broadcast(payload) {
33609
34371
  return relay ? relay.broadcast(payload) : 0;
33610
34372
  },
33611
- hasPeer(conversationId) {
33612
- return relay?.hasPeer(conversationId) ?? false;
34373
+ hasClient(conversationId) {
34374
+ return relay?.hasClient(conversationId) ?? false;
33613
34375
  },
33614
34376
  conversationIds() {
33615
34377
  return relay?.conversationIds() ?? [];
@@ -33624,7 +34386,7 @@ function applyInner(ctx, injected) {
33624
34386
  startedCarrier = port.carrier;
33625
34387
  relay = new RelayClient({
33626
34388
  url: config2.serverUrl,
33627
- hostId: config2.hostId || `h_${randomBytes4(3).toString("hex")}`,
34389
+ hostId,
33628
34390
  label: config2.hostLabel,
33629
34391
  token: config2.hostToken,
33630
34392
  clock,
@@ -33635,6 +34397,9 @@ function applyInner(ctx, injected) {
33635
34397
  pairingWindow.applyServerTtl(token, ttlMs);
33636
34398
  },
33637
34399
  onPeerJoined: (conversationId, pairingToken2) => {
34400
+ for (const id of relay?.conversationIds() ?? []) {
34401
+ if (id !== conversationId) relay?.voidConversation(id);
34402
+ }
33638
34403
  runtime?.start();
33639
34404
  void runtime?.pushSessions("peer-joined");
33640
34405
  if (pairingToken2) {
@@ -33655,7 +34420,23 @@ function applyInner(ctx, injected) {
33655
34420
  },
33656
34421
  // 配对通道长存(D3)之后必须自己剪枝:中继那边的空闲 TTL 是 7 天,
33657
34422
  // 不剪就是无界的 PSK 簿 + 每次广播对废弃通道逐个密封。
33658
- prunePolicy: { idleTtlMs: config2.conversationIdleTtlSec * 1e3, maxConversations: MAX_CONVERSATIONS }
34423
+ // 两条 TTL 分开:新配对的按 24 小时剪,从盘上恢复的按 `restoredIdleTtlSec`
34424
+ // (默认 7 天,与中继对齐)—— 理由见 config.ts 与 keys.ts 的 `Conversation.restored`。
34425
+ prunePolicy: {
34426
+ idleTtlMs: config2.conversationIdleTtlSec * 1e3,
34427
+ restoredIdleTtlMs: config2.restoredIdleTtlSec * 1e3,
34428
+ maxConversations: MAX_CONVERSATIONS
34429
+ },
34430
+ // 结构性变化(新配对 / 作废)立刻落盘。只改了 seqHost 与 lastActivityAt 的场合
34431
+ // 由 status 的 3 秒 tick 批量落(见下面 `pairStore.markDirty()` 那一段)——
34432
+ // 挂在这里等于每广播一次写一次盘。
34433
+ onStructuralChange: () => {
34434
+ if (!pairStoreFile || !relay) return;
34435
+ pairStore.save(relay.conversations.snapshot(), clock.now());
34436
+ },
34437
+ onBookActivity: () => {
34438
+ pairStore.markDirty();
34439
+ }
33659
34440
  });
33660
34441
  runtime = new HostRuntime(port, transport, sleep, clock, {
33661
34442
  listingRefreshMs: config2.listingRefreshSec * 1e3,
@@ -33663,11 +34444,20 @@ function applyInner(ctx, injected) {
33663
34444
  approvalTimeoutMs: config2.approvalTimeoutSec * 1e3,
33664
34445
  log: (message, fields) => log(message, fields)
33665
34446
  });
34447
+ const restoredIds = relay.restoreConversations(restoredAtBoot);
34448
+ if (restoredIds.length > 0) {
34449
+ runtime.start();
34450
+ log("runtime started from restored conversations", { conversations: restoredIds.length });
34451
+ }
33666
34452
  relay.connect();
33667
34453
  pairingWindow.tick();
33668
34454
  status.start(() => {
33669
34455
  pairingWindow.tick();
33670
34456
  relay?.pruneConversations();
34457
+ if (pairStoreFile && pairStore.hasPendingChanges && relay) {
34458
+ pairStore.save(relay.conversations.snapshot(), clock.now());
34459
+ }
34460
+ slots.prune();
33671
34461
  return state();
33672
34462
  });
33673
34463
  };
@@ -33692,17 +34482,46 @@ function applyInner(ctx, injected) {
33692
34482
  carrier: startedCarrier || (config2.mockBridge ? "mock" : "probing"),
33693
34483
  serverUrl: config2.serverUrl,
33694
34484
  hostLabel: config2.hostLabel,
34485
+ // 主机身份。中继日志里认的就是它,所以"手机连不上/丢帧涨"第一件事是看这个值
34486
+ // 在两次重启之间有没有变(变了就是身份没稳住,见 shell/host-id.ts)。
34487
+ hostId,
33695
34488
  relay: relay ? relayState() : "idle",
33696
34489
  relayProblem: relayProblem(),
33697
34490
  conversations: relay?.conversationCount ?? 0,
34491
+ /**
34492
+ * 当前连着的手机台数(去重的 clientId)。
34493
+ *
34494
+ * 为什么必须与 `conversations` 分开报:「解除配对 → 重新配对」会开一条**新会话**,
34495
+ * 而手机还是那一台。所以拿会话数回答"几台手机连着",用户解完配对看到数字反而变大 ——
34496
+ * 那个现象与"我的解配没生效"完全一致,最容易让人把解配按钮当坏的。
34497
+ * `pendingPairs` 是另一个东西:那是**还挂着、还没被用掉的配对码**。
34498
+ */
34499
+ pairedClients: relay?.clientCount ?? 0,
33698
34500
  pendingPairs: slots.size,
34501
+ /**
34502
+ * 密钥簿落盘的排障面。
34503
+ *
34504
+ * `restored` 是"本次启动从盘上恢复了几条会话"——它与 `conversations` 的差值
34505
+ * 就是"落盘没生效"的判据:`conversations > 0 && restored === 0 && pairStore.enabled`
34506
+ * 意味着这些会话是本次运行新配出来的(正常),反过来
34507
+ * `restored > 0` 而 `conversations === 0` 才是异常。
34508
+ *
34509
+ * **`lastSavedAt` 与路径都不含 PSK**:这份快照会被外部脚本读,
34510
+ * 文件路径本身也不是凭据(里面的内容才是)。
34511
+ */
34512
+ pairStore: {
34513
+ enabled: pairStoreFile !== "",
34514
+ file: pairStoreFile || void 0,
34515
+ restored: restoredAtBoot.length,
34516
+ lastSavedAt: pairStoreFile && pairStore.lastSavedAt > 0 ? new Date(pairStore.lastSavedAt).toISOString() : void 0
34517
+ },
33699
34518
  pairing: describeActivePairing(),
33700
34519
  keepAwake: snapshot,
33701
34520
  kernel: kernel?.describe() ?? null,
33702
34521
  // 出站计数:手机上"没收到 X"的第一现场对比点(插件没发 vs 发了但路上丢了)。
33703
34522
  outbound: runtime?.stats ?? null,
33704
34523
  probe,
33705
- // 配对入口(那颗 pill 的三条路由)为什么没起,是"配不了对"唯一的排查入口:
34524
+ // 配对入口(那颗 pill 的四条路由)为什么没起,是"配不了对"唯一的排查入口:
33706
34525
  // 这里直接把软探测的结果带出来。
33707
34526
  pill: pillRoutes?.probe ?? { webServer: "not-started" },
33708
34527
  // token 永远不出现真值;这条有测试锁住。
@@ -33761,13 +34580,21 @@ function applyInner(ctx, injected) {
33761
34580
  return { qr: fresh.qr, token: fresh.token, expiresAt: fresh.expiresAt };
33762
34581
  }
33763
34582
  function pillStatus() {
34583
+ const waiting = runtime?.waiting ?? { count: 0, oldestSec: 0 };
33764
34584
  return {
33765
34585
  relay: relay ? relayState() : "idle",
33766
- paired: relay?.conversationCount ?? 0,
33767
- hostLabel: config2.hostLabel,
33768
- serverUrl: config2.serverUrl
34586
+ paired: relay?.clientCount ?? 0,
34587
+ serverUrl: config2.serverUrl,
34588
+ version: PLUGIN_VERSION,
34589
+ waiting: waiting.count,
34590
+ waitingOldestSec: waiting.oldestSec
33769
34591
  };
33770
34592
  }
34593
+ function unpairAll() {
34594
+ const ids = relay?.conversationIds() ?? [];
34595
+ for (const id of ids) relay?.voidConversation(id);
34596
+ return ids.length;
34597
+ }
33771
34598
  const pairingWindow = new PairingWindow(
33772
34599
  {
33773
34600
  clock,
@@ -34025,10 +34852,11 @@ function applyInner(ctx, injected) {
34025
34852
  try {
34026
34853
  pillRoutes = startPill(ctx, {
34027
34854
  enabled: config2.pill.enabled,
34028
- // 三条路由要的三件事:幂等发码、只读看当前码、读连接状态。
34855
+ // 四条路由要的四件事:幂等发码、只读看当前码、读连接状态、退出配对。
34029
34856
  ensureFresh: () => ensureFreshPairing(),
34030
34857
  current: () => currentPairing(),
34031
34858
  status: () => pillStatus(),
34859
+ unpair: () => unpairAll(),
34032
34860
  log
34033
34861
  });
34034
34862
  } catch (error62) {
@@ -34037,46 +34865,6 @@ function applyInner(ctx, injected) {
34037
34865
  message: String(error62?.message ?? error62).slice(0, 200)
34038
34866
  });
34039
34867
  }
34040
- const commandDefinition = {
34041
- name: "drc",
34042
- description: "DSH \u8FDC\u7A0B\u63A7\u5236\uFF1A\u914D\u5BF9\u8BF7\u70B9\u72B6\u6001\u680F\u7684 dsh-remote-control\uFF1B/drc status \u770B\u8FDE\u63A5\u4E0E\u95EE\u9898\uFF0C/drc unpair \u89E3\u914D",
34043
- input: { hint: "status | unpair" },
34044
- handler: async (invocation) => {
34045
- const argument = String(invocation.rawInput ?? "").trim().toLowerCase();
34046
- if (argument.startsWith("unpair")) {
34047
- for (const id of relay?.conversationIds() ?? []) relay?.voidConversation(id);
34048
- return { kind: "success", text: '\u5DF2\u89E3\u9664\u6240\u6709\u914D\u5BF9\uFF1B\u624B\u673A\u7AEF\u4F1A\u663E\u793A"\u4E3B\u673A\u5DF2\u65AD\u5F00\uFF0C\u8BF7\u91CD\u65B0\u914D\u5BF9"\u3002' };
34049
- }
34050
- return { kind: "success", text: JSON.stringify(state(), null, 2) };
34051
- }
34052
- };
34053
- let commandsTaken = false;
34054
- const takeCommands = (source) => {
34055
- if (commandsTaken || !source || typeof source !== "object") return;
34056
- const holder = source;
34057
- if (typeof holder.register !== "function") return;
34058
- holder.register(commandDefinition);
34059
- commandsTaken = true;
34060
- probe.commands = "registered";
34061
- };
34062
- try {
34063
- takeCommands(ctx.commands);
34064
- } catch {
34065
- }
34066
- try {
34067
- takeCommands(ctx.get?.("commands", true));
34068
- } catch {
34069
- }
34070
- try {
34071
- const child = ctx.inject?.(["commands"], ((...cbArgs) => {
34072
- for (const candidate of [cbArgs[0], ...cbArgs, ctx]) {
34073
- takeCommands(pickFrom(candidate, "commands"));
34074
- if (commandsTaken) break;
34075
- }
34076
- }));
34077
- if (child !== void 0) injectChildren.push(["commands", child]);
34078
- } catch {
34079
- }
34080
34868
  return {
34081
34869
  config: config2,
34082
34870
  state,
@@ -34085,6 +34873,9 @@ function applyInner(ctx, injected) {
34085
34873
  oneShot.cancelAll();
34086
34874
  pillRoutes?.stop();
34087
34875
  status.stop();
34876
+ if (pairStoreFile && relay && pairStore.hasPendingChanges) {
34877
+ pairStore.save(relay.conversations.snapshot(), clock.now());
34878
+ }
34088
34879
  runtime?.stop();
34089
34880
  relay?.stop();
34090
34881
  sleep.stop();