dsh-plugin-mesh 0.5.7 → 0.5.9

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.
@@ -146,6 +146,8 @@ const {
146
146
  UpdateLink,
147
147
  UpdateLinkView,
148
148
  FeedbackLink,
149
+ DemoOverlay,
150
+ ApprovalBar,
149
151
  } = mod.__test;
150
152
 
151
153
  const noop = () => {};
@@ -1143,14 +1145,133 @@ test("检查更新:没点之前就是一个小字,点了先说「检查中
1143
1145
  const idle = textOf(UpdateLink({ push: noop })).join(" ");
1144
1146
  assert.ok(idle.includes("检查更新"), idle);
1145
1147
  assert.ok(!idle.includes("已是最新"), "没查过就不能有任何结论");
1146
- const checking = textOf(UpdateLinkView({ phase: "checking", report: null, onCheck: noop, onCopy: noop })).join(" ");
1148
+ const checking = textOf(UpdateLinkView({ phase: "checking", report: null, install: null, onCheck: noop })).join(" ");
1147
1149
  assert.ok(checking.includes("检查中"), checking);
1148
1150
  });
1149
1151
 
1150
- test("有更新:旁边多一个字「新版本 v0.6.0」,命令不铺在页面上", () => {
1151
- const txt = textOf(UpdateLinkView({ phase: "done", report: updateReport, onCheck: noop, onCopy: noop })).join(" ");
1152
+ test("有更新:一行小字「新版本 v0.6.0」,不铺命令、不占卡片", () => {
1153
+ const txt = textOf(
1154
+ UpdateLinkView({ phase: "done", report: updateReport, install: null, onCheck: noop, onInstall: noop }),
1155
+ ).join(" ");
1152
1156
  assert.ok(txt.includes("新版本 v0.6.0"), txt);
1153
- assert.ok(!txt.includes("curl"), "升级命令点一下才复制,不占版面");
1157
+ assert.ok(!txt.includes("curl"), "升级命令不再铺在页面上");
1158
+ });
1159
+
1160
+ test("一键安装:第一次点只是确认,第二次点才真的开始装", () => {
1161
+ let installed = 0;
1162
+ const view = (install) =>
1163
+ UpdateLinkView({
1164
+ phase: "done",
1165
+ report: updateReport,
1166
+ install,
1167
+ onCheck: noop,
1168
+ onInstall: () => {
1169
+ installed += 1;
1170
+ },
1171
+ });
1172
+ const findInstallBtn = (el) => {
1173
+ let btn = null;
1174
+ walk(el, (n) => {
1175
+ if (n.type === "button" && textOf(n).join("").includes("确认安装")) btn = n;
1176
+ });
1177
+ return btn;
1178
+ };
1179
+ const armed = view({ state: "armed", error: "" });
1180
+ const btn = findInstallBtn(armed);
1181
+ assert.ok(btn, "确认态要有一个「确认安装 v0.6.0」按钮");
1182
+ btn.props.onClick();
1183
+ assert.equal(installed, 1, "点击确认后才调用安装");
1184
+ });
1185
+
1186
+ test("安装中 / 安装失败:状态写在字面上,失败不悄悄回到「有新版本」", () => {
1187
+ const running = textOf(
1188
+ UpdateLinkView({ phase: "done", report: updateReport, install: { state: "running" }, onCheck: noop, onInstall: noop }),
1189
+ ).join(" ");
1190
+ assert.ok(running.includes("安装中"), running);
1191
+
1192
+ let retried = 0;
1193
+ const failed = UpdateLinkView({
1194
+ phase: "done",
1195
+ report: updateReport,
1196
+ install: { state: "failed", error: "sha512 校验失败" },
1197
+ onCheck: noop,
1198
+ onInstall: () => {
1199
+ retried += 1;
1200
+ },
1201
+ });
1202
+ const txt = textOf(failed).join(" ");
1203
+ assert.ok(txt.includes("安装失败"), txt);
1204
+ assert.ok(!txt.includes("新版本 v0.6.0"), "失败时不能又显示成普通的新版本入口");
1205
+ let failBtn = null;
1206
+ walk(failed, (n) => {
1207
+ if (n.type === "button" && textOf(n).join("").includes("安装失败")) failBtn = n;
1208
+ });
1209
+ assert.ok(failBtn, "失败后要能点一下重试");
1210
+ assert.ok(String(failBtn.props.title).includes("sha512 校验失败"), String(failBtn.props.title));
1211
+ failBtn.props.onClick();
1212
+ assert.equal(retried, 1);
1213
+ });
1214
+
1215
+ test("已安装待重启:只给「重启生效」,不再给安装入口", () => {
1216
+ let restarted = 0;
1217
+ const report = {
1218
+ ...updateReport,
1219
+ diskVersion: "0.6.0",
1220
+ versionDrift: true,
1221
+ pendingUpgrade: { version: "0.6.0", previous: "0.5.6", at: "", restartRequired: true, reloadRequired: false },
1222
+ };
1223
+ const el = UpdateLinkView({
1224
+ phase: "done",
1225
+ report,
1226
+ install: null,
1227
+ onCheck: noop,
1228
+ onInstall: noop,
1229
+ onRestart: () => {
1230
+ restarted += 1;
1231
+ },
1232
+ });
1233
+ const txt = textOf(el).join(" ");
1234
+ assert.ok(txt.includes("已安装 v0.6.0"), txt);
1235
+ assert.ok(txt.includes("重启生效"), txt);
1236
+ assert.ok(!txt.includes("新版本 v0.6.0"), "装好之后不该再提示再装一次");
1237
+ let btn = null;
1238
+ walk(el, (n) => {
1239
+ if (n.type === "button" && textOf(n).join("").includes("重启生效")) btn = n;
1240
+ });
1241
+ assert.ok(btn, "缺少「重启生效」按钮");
1242
+ btn.props.onClick();
1243
+ assert.equal(restarted, 1);
1244
+ });
1245
+
1246
+ test("只改了界面:给「重载界面」而不是重启", () => {
1247
+ let reloaded = 0;
1248
+ const report = {
1249
+ ...updateReport,
1250
+ diskVersion: "0.6.0",
1251
+ versionDrift: true,
1252
+ pendingUpgrade: { version: "0.6.0", previous: "0.5.6", at: "", restartRequired: false, reloadRequired: true },
1253
+ };
1254
+ const el = UpdateLinkView({
1255
+ phase: "done",
1256
+ report,
1257
+ install: null,
1258
+ onCheck: noop,
1259
+ onInstall: noop,
1260
+ onReload: () => {
1261
+ reloaded += 1;
1262
+ },
1263
+ });
1264
+ const txt = textOf(el).join(" ");
1265
+ assert.ok(txt.includes("已安装 v0.6.0"), txt);
1266
+ assert.ok(txt.includes("重载界面"), txt);
1267
+ assert.ok(!txt.includes("重启生效"), "只改界面时不该要求重启");
1268
+ let btn = null;
1269
+ walk(el, (n) => {
1270
+ if (n.type === "button" && textOf(n).join("").includes("重载界面")) btn = n;
1271
+ });
1272
+ assert.ok(btn, "缺少「重载界面」按钮");
1273
+ btn.props.onClick();
1274
+ assert.equal(reloaded, 1);
1154
1275
  });
1155
1276
 
1156
1277
  test("检查失败 / 无法比较:各多一个字说明,绝不说「已是最新」", () => {
@@ -1158,8 +1279,8 @@ test("检查失败 / 无法比较:各多一个字说明,绝不说「已是
1158
1279
  UpdateLinkView({
1159
1280
  phase: "done",
1160
1281
  report: { ok: false, current: "0.5.6", error: "检查更新失败:registry.npmjs.org 超时(5000ms)" },
1282
+ install: null,
1161
1283
  onCheck: noop,
1162
- onCopy: noop,
1163
1284
  }),
1164
1285
  ).join(" ");
1165
1286
  assert.ok(failed.includes("检查失败"), failed);
@@ -1170,42 +1291,64 @@ test("检查失败 / 无法比较:各多一个字说明,绝不说「已是
1170
1291
  UpdateLinkView({
1171
1292
  phase: "done",
1172
1293
  report: { ...updateReport, latest: "2026.1", comparable: false, hasUpdate: false },
1294
+ install: null,
1173
1295
  onCheck: noop,
1174
- onCopy: noop,
1175
1296
  }),
1176
1297
  ).join(" ");
1177
1298
  assert.ok(incomparable.includes("无法比较"), incomparable);
1178
1299
  assert.ok(!incomparable.includes("已是最新"));
1179
1300
  });
1180
1301
 
1181
- test("点「新版本 v0.6.0」复制升级命令;点「检查更新」重查(不是跳页)", () => {
1182
- let copied = "";
1302
+ test("点「检查更新」触发重查(不是跳页)", () => {
1183
1303
  let checked = 0;
1184
1304
  const el = UpdateLinkView({
1185
1305
  phase: "done",
1186
1306
  report: updateReport,
1307
+ install: null,
1187
1308
  onCheck: () => {
1188
1309
  checked += 1;
1189
1310
  },
1190
- onCopy: (text) => {
1191
- copied = text;
1192
- },
1311
+ onInstall: noop,
1193
1312
  });
1194
- let updateBtn = null;
1195
1313
  let checkBtn = null;
1196
1314
  walk(el, (n) => {
1197
- if (n.type !== "button") return;
1198
- const label = textOf(n).join("");
1199
- if (label.includes("新版本")) updateBtn = n;
1200
- if (label === "检查更新") checkBtn = n;
1315
+ if (n.type === "button" && textOf(n).join("") === "检查更新") checkBtn = n;
1201
1316
  });
1202
- assert.ok(updateBtn, "要有一个「新版本」按钮");
1203
- assert.ok(checkBtn, "要有一个「检查更新」按钮");
1204
- updateBtn.props.onClick();
1317
+ assert.ok(checkBtn, "缺少「检查更新」按钮");
1205
1318
  checkBtn.props.onClick();
1206
- assert.equal(copied, updateReport.upgradeCommand);
1207
1319
  assert.equal(checked, 1);
1208
1320
  });
1209
1321
 
1322
+ test("陌生设备首次派活:确认条说清「谁」和「要干什么」,并给出允许/拒绝", () => {
1323
+ const el = ApprovalBar({
1324
+ items: [{ id: "t-1", fromMachine: "hos", from: "hos/peng", task: "把插件升级到 0.5.8" }],
1325
+ busy: false,
1326
+ onDecide: noop,
1327
+ });
1328
+ const txt = textOf(el).join(" ");
1329
+ assert.ok(txt.includes("hos"), txt);
1330
+ assert.ok(txt.includes("把插件升级到 0.5.8"), txt);
1331
+ assert.ok(txt.includes("允许") && txt.includes("拒绝"), txt);
1332
+ let allow = null;
1333
+ walk(el, (n) => {
1334
+ if (n.type === "button" && textOf(n).join("") === "允许") allow = n;
1335
+ });
1336
+ assert.ok(allow, "缺少「允许」按钮");
1337
+ });
1338
+
1339
+ test("没有待确认项时确认条不渲染", () => {
1340
+ assert.equal(ApprovalBar({ items: [], onDecide: noop }), null);
1341
+ });
1342
+
1343
+ test("演示:单机也能看见完整闭环,且明说这是演示、不花 token", () => {
1344
+ const el = DemoOverlay({ open: true, onClose: noop });
1345
+ const txt = textOf(el).join(" ");
1346
+ assert.ok(txt.includes("演示"), txt);
1347
+ assert.ok(txt.includes("报一下你的系统和负载"), txt);
1348
+ assert.ok(txt.includes("结果回到这里"), txt);
1349
+ assert.ok(txt.includes("不花 token"), txt);
1350
+ assert.equal(DemoOverlay({ open: false, onClose: noop }), null);
1351
+ });
1352
+
1210
1353
  console.log(`\n${failed === 0 ? "全部通过" : "有失败"}:${passed} 通过 / ${failed} 失败\n`);
1211
1354
  process.exit(failed ? 1 : 0);
@@ -0,0 +1,152 @@
1
+ /**
2
+ * 陌生设备第一次派活时的「确认一下」闸门 —— 纯函数,便于单测。
3
+ *
4
+ * 为什么要有它:默认零配置意味着同一网段里任何连得上 45917 的机器都能让本机
5
+ * 开一个 agent 会话跑东西。对"自己家三台电脑"这是方便,对"公司网络 / 合租 /
6
+ * 咖啡厅"就是敞着门。原来的答案是"你去开配对码",但那是个**设置项**,没人会
7
+ * 主动打开 —— 而恐惧是装之前就有的,不是用完才有的。
8
+ *
9
+ * 所以默认改成:陌生设备第一次派活 → 本机弹一次确认;你点过「允许」之后,
10
+ * 这台机器才不再问你。配对码(requirePairing)比它更强,开了配对码就说明
11
+ * 你已经用共享密钥表达过信任,不再多问一遍。
12
+ *
13
+ * 规则写在这里而不是埋进 3000 行的 index.js:这是安全相关判定,写错了会
14
+ * "看起来拦住了、实际放行",而这种错误没人会为它写测试。拆出来,单测它。
15
+ */
16
+
17
+ /** 一次批准记录最多留多少条:设备是有限的,留太多只会让 state.json 变脏。 */
18
+ export const MAX_APPROVED_PEERS = 50;
19
+
20
+ /**
21
+ * 从一条入站任务里取出"这是谁"。
22
+ *
23
+ * 识别优先级:机器名 > 来源地址。**机器名优先**,因为它稳定、可读,用户在
24
+ * 确认框里看到的就是这个名字;地址只作为兜底(旧版本对端、或机器名没填)。
25
+ *
26
+ * @param {object} task `/mesh/v1/task` 落下来的任务对象
27
+ * @returns {{machine: string, address: string, key: string}}
28
+ */
29
+ export function peerOfTask(task = {}) {
30
+ let machine = String(task?.fromMachine || "").trim() || String(task?.from || "").split("/")[0].trim();
31
+ // "unknown" 是旧版本对端没填名字时的占位符,不能当成一台叫 unknown 的机器 ——
32
+ // 否则所有老对端会共用一张信任记录。
33
+ if (machine.toLowerCase() === "unknown") machine = "";
34
+ const address = String(task?.sourceAddress || "").trim();
35
+ return { machine, address, key: (machine || address).toLowerCase() };
36
+ }
37
+
38
+ /** 把磁盘上读出来的批准列表归一化 —— 坏数据不能让闸门炸掉。 */
39
+ export function normalizeApproved(list) {
40
+ if (!Array.isArray(list)) return [];
41
+ return list
42
+ .filter((x) => x && typeof x === "object")
43
+ .map((x) => ({
44
+ machine: String(x.machine ?? "").trim(),
45
+ address: String(x.address ?? "").trim(),
46
+ at: typeof x.at === "string" ? x.at : "",
47
+ }))
48
+ .filter((x) => x.machine || x.address)
49
+ .slice(-MAX_APPROVED_PEERS);
50
+ }
51
+
52
+ /**
53
+ * 这台设备是否已经被允许过。
54
+ *
55
+ * **有机器名就只认机器名**;没有名字(旧版本对端)才退回地址。
56
+ * 不能"任一命中":tailnet 反代下多台对端的 sourceAddress 都是 127.0.0.1,
57
+ * 按地址匹配会让允许一台 = 允许全部 —— 闸门形同虚设(本地联调时实测到了)。
58
+ * 机器名大小写不敏感(Windows 上尤其重要)。
59
+ *
60
+ * @param {Array} approved state.json 里的 approvedPeers
61
+ * @param {{machine: string, address: string}} peer peerOfTask 的返回值
62
+ */
63
+ export function isApproved(approved, peer) {
64
+ const list = normalizeApproved(approved);
65
+ // 调用方可以只传 {machine, address},不必先过 peerOfTask;
66
+ // 认不出是谁才返回 false(宁可多问一次,也不默认放行)。
67
+ const machine = String(peer?.machine || "").trim().toLowerCase();
68
+ if (machine && machine !== "unknown") {
69
+ return list.some((x) => x.machine && x.machine.toLowerCase() === machine);
70
+ }
71
+ const address = String(peer?.address || "").trim();
72
+ if (!address) return false;
73
+ return list.some((x) => !x.machine && x.address === address);
74
+ }
75
+
76
+ /** 追加一条批准记录(幂等;超过上限丢最旧的)。 */
77
+ export function approvePeer(approved, peer, at = new Date().toISOString()) {
78
+ const machineKey = String(peer?.machine || "").trim().toLowerCase();
79
+ const addressKey = String(peer?.address || "").trim();
80
+ const list = normalizeApproved(approved).filter((x) => {
81
+ // 和 isApproved 同一套规则:有机器名只按机器名去重,没名字才按地址。
82
+ if (machineKey) return !(x.machine && x.machine.toLowerCase() === machineKey);
83
+ return !(!x.machine && addressKey && x.address === addressKey);
84
+ });
85
+ const entry = {
86
+ machine: String(peer?.machine ?? "").trim(),
87
+ address: String(peer?.address ?? "").trim(),
88
+ at,
89
+ };
90
+ if (!entry.machine && !entry.address) return normalizeApproved(approved);
91
+ return [...list, entry].slice(-MAX_APPROVED_PEERS);
92
+ }
93
+
94
+ /**
95
+ * 撤销一条批准(按机器名或地址,大小写不敏感)。
96
+ *
97
+ * @returns {Array} 新列表;没命中时原样返回
98
+ */
99
+ export function revokePeer(approved, target) {
100
+ const t = String(target ?? "").trim().toLowerCase();
101
+ if (!t) return normalizeApproved(approved);
102
+ return normalizeApproved(approved).filter(
103
+ (x) => x.machine.toLowerCase() !== t && String(x.address).toLowerCase() !== t,
104
+ );
105
+ }
106
+
107
+ /**
108
+ * 这条任务要不要先问用户。
109
+ *
110
+ * 只看三件事:配置开关、配对码是否已生效、这台设备是否已被允许。
111
+ * **不要**在这里判断"是不是自己派给自己"——那属于路由层,不属于信任层。
112
+ *
113
+ * @param {object} input
114
+ * @param {object} input.config 插件配置(读 requireApproval)
115
+ * @param {Array} input.approved state.json 的 approvedPeers
116
+ * @param {object} input.peer peerOfTask 的返回值
117
+ * @param {boolean} input.pairingRequired pairingRequired(config) 的结果
118
+ * @returns {boolean}
119
+ */
120
+ export function needsApproval({ config = {}, approved = [], peer = {}, pairingRequired = false }) {
121
+ // 显式 false 才关;缺省按开(schema 默认也是 true)。
122
+ if (config?.requireApproval === false) return false;
123
+ // 配对码是比"点一次允许"更强的信任动作:开了它就不再重复问。
124
+ if (pairingRequired) return false;
125
+ if (!peer || !(peer.machine || peer.address)) return true;
126
+ return !isApproved(approved, peer);
127
+ }
128
+
129
+ /**
130
+ * 这条入站任务是不是在「回答问题」而不是派新活?
131
+ *
132
+ * 判据和 handleInboundTask 里的一致:同一台机器 + 同一个 thread 上,我们确实
133
+ * 往外派过任务。回执**必须绕过**首次确认闸门,否则对方把结果派回来时会被本机
134
+ * 拦下,整条回路卡死在"等允许"。
135
+ *
136
+ * 抽成纯函数是为了能单测这条绕过 —— 写错了是静默灾难:要么结果回不来,
137
+ * 要么陌生机器能借一个旧 thread 名绕过闸门。
138
+ *
139
+ * @param {object} task 入站任务
140
+ * @param {Array} outbound 本机发出的任务记录
141
+ */
142
+ export function isReplyToOutbound(task = {}, outbound = []) {
143
+ const thread = String(task?.thread || "").trim();
144
+ if (!thread) return false;
145
+ const machine = String(task?.fromMachine || String(task?.from || "").split("/")[0] || "").trim();
146
+ if (!machine) return false;
147
+ return (Array.isArray(outbound) ? outbound : []).some(
148
+ (o) =>
149
+ String(o?.toMachine || "").trim() === machine &&
150
+ String(o?.thread || "").trim() === thread,
151
+ );
152
+ }
@@ -0,0 +1,109 @@
1
+ /**
2
+ * 聊天窗口「@ 一台在线设备」的纯逻辑(宿主侧)。
3
+ *
4
+ * 用户在 composer 里打 `@`,菜单把**在线的 mesh 机器**列出来(client/client.js
5
+ * 注册的那个 @ source);选中后插入 `@机器名 `。发送时宿主在
6
+ * `agent/pre-step` 里把消息开头的 @目标 认出来,把正文直接派发到那些机器 ——
7
+ * 于是「在聊天窗口 @ 一台机器」就等于「把这句话交给那台机器」。
8
+ *
9
+ * 为什么只认**开头**的 @:
10
+ * · 正文中间的 @ 通常是「提到某台机器」或者让本机 agent 自己用 mesh_run,
11
+ * 自动派发会在这种地方误伤;
12
+ * · 文件引用的序列化形式也是 `@path`(见 @deepseek-ai/dsh-client-ui-reference),
13
+ * 而开头的 @路径 不会被误认成机器名(名字必须精确命中已知机器)。
14
+ *
15
+ * 本模块不读文件、不联网、不碰 ctx,全部单测覆盖(lib/_selftest-at-mention.mjs)。
16
+ */
17
+
18
+ /** 一条消息最多认几个派发目标:再多就不是派活了,是广播。 */
19
+ export const AT_MENTION_LIMIT = 8;
20
+
21
+ /**
22
+ * 机器名是否允许出现在 mention 里。
23
+ *
24
+ * 名字由用户起(validateMachineName 允许空格、禁止斜杠与控制字符),所以这里
25
+ * 只挡掉「会破坏 mention 语法」的字符:换行、@ 本身。@ 在名字里会让
26
+ * `@@名字` 这种写法歧义。
27
+ *
28
+ * @param {unknown} name
29
+ * @returns {boolean}
30
+ */
31
+ export function isMentionableName(name) {
32
+ const value = String(name ?? "").trim();
33
+ if (!value) return false;
34
+ // eslint-disable-next-line no-control-regex
35
+ return !/[@\r\n\t\x00-\x1f\x7f]/.test(value);
36
+ }
37
+
38
+ /**
39
+ * 从消息开头认出连续的 `@机器名`。
40
+ *
41
+ * 机器名可以含空格(用户起的名字),所以不能按空白切词 —— 这里按「已知名字
42
+ * 最长优先」逐个试,命中后要求下一个字符是空白或结尾(整词边界)。匹配对大小写
43
+ * 不敏感,返回的名字始终取已知列表里的规范写法。
44
+ *
45
+ * @param {string} text 消息原文(通常就是 composer 里那段文本)。
46
+ * @param {string[]} names 当前已知的机器名(含离线;在线与否由调用方再判)。
47
+ * @returns {{ mentions: { machine: string, raw: string }[], body: string }}
48
+ * `mentions` 是按出现顺序认出的目标;`body` 是去掉这些 @目标 后的任务正文。
49
+ */
50
+ export function matchLeadingMentions(text, names) {
51
+ const source = typeof text === "string" ? text : "";
52
+ const known = [...new Set((Array.isArray(names) ? names : []).map((n) => String(n ?? "").trim()).filter(isMentionableName))].sort(
53
+ (a, b) => b.length - a.length,
54
+ );
55
+
56
+ const mentions = [];
57
+ let cursor = 0;
58
+ const skipSpace = () => {
59
+ while (cursor < source.length && /\s/u.test(source[cursor])) cursor += 1;
60
+ };
61
+ skipSpace();
62
+ while (mentions.length < AT_MENTION_LIMIT && source[cursor] === "@") {
63
+ const nameStart = cursor + 1;
64
+ let hit = null;
65
+ for (const name of known) {
66
+ if (source.slice(nameStart, nameStart + name.length).toLocaleLowerCase() !== name.toLocaleLowerCase()) continue;
67
+ const after = nameStart + name.length;
68
+ if (after < source.length && !/\s/u.test(source[after])) continue;
69
+ hit = { machine: name, raw: source.slice(cursor, after) };
70
+ break;
71
+ }
72
+ if (!hit) break;
73
+ mentions.push(hit);
74
+ cursor = nameStart + hit.machine.length;
75
+ skipSpace();
76
+ }
77
+
78
+ return { mentions, body: source.slice(cursor).trim() };
79
+ }
80
+
81
+ /**
82
+ * 生成这一轮要给模型看的新消息正文。
83
+ *
84
+ * 派发成功时告诉 agent「已经发出去了,别在本机再做一遍」(少了这句话,agent 会
85
+ * 把正文当成本地任务直接执行 —— `/mesh/api/send` 那条路径踩过一次);失败时
86
+ * 把原因说清楚,并明确「没有在本机执行」,让 agent 转告用户、等用户决定。
87
+ *
88
+ * @param {{ body: string, results: { machine: string, id?: string, error?: string }[] }} input
89
+ * @returns {string}
90
+ */
91
+ export function buildAtPrompt({ body, results }) {
92
+ const text = String(body ?? "").trim();
93
+ const lines = [text || "(这条消息只有 @ 设备名,没有正文)", ""];
94
+ for (const item of Array.isArray(results) ? results : []) {
95
+ const machine = String(item?.machine ?? "").trim();
96
+ if (item?.id) {
97
+ lines.push(
98
+ `[Mesh 聊天窗口派发 → ${machine}/${item.id}] 已发出。现在什么都不用做:不要调用任何工具、不要回派,` +
99
+ `回一句「已派发给 ${machine},等待回执」即可。`,
100
+ );
101
+ } else {
102
+ lines.push(
103
+ `[Mesh 未能派发 @${machine}:${item?.error || "未知原因"}] 这条任务没有发出,也没有在本机执行。` +
104
+ "请把原因直接告诉用户,等用户决定下一步(不要自己动手执行这条任务)。",
105
+ );
106
+ }
107
+ }
108
+ return lines.join("\n");
109
+ }