@duke-dsh-plugins/dsh-remote-control 1.3.1 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/host-bridge.mjs +711 -43
- package/market/index.json +10 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -52,7 +52,7 @@ dsh plugin add <本目录> # 或 DSH 的 plugin_manager install_bundle(
|
|
|
52
52
|
| `channel` / `secret` | 首启生成 | 配对信息 |
|
|
53
53
|
| `interceptApprovals` | `true` | 手机在线时代理审批请求 |
|
|
54
54
|
| `interceptQuestions` | `true` | 手机在线时代理用户提问 |
|
|
55
|
-
| `approvalTimeoutMs` | `120000` |
|
|
55
|
+
| `approvalTimeoutMs` | `120000` | 手机独占应答窗口(钳制 10s–600s)。审批超时一律放行桌面答案器;提问在 Host `userQuestions` timed 模式下超时只转为 `continued`,快照仍可恢复。它与 Host ask 工具的 timeout 是两个计时器,建议保持一致 |
|
|
56
56
|
| `push.*` | 关闭 | Push Kit(`appId` / `clientId` / `clientSecret` / `channelId` / `topic` / `notifyTurnEnd`) |
|
|
57
57
|
|
|
58
58
|
## 目录
|
package/lib/host-bridge.mjs
CHANGED
|
@@ -232,6 +232,39 @@ export function queueItemText(content) {
|
|
|
232
232
|
return text;
|
|
233
233
|
}
|
|
234
234
|
|
|
235
|
+
/**
|
|
236
|
+
* Host `inbox` 投影视图 → 插件内部队列项(契约 §5.1/§5.2 v1.3.0)。
|
|
237
|
+
*
|
|
238
|
+
* `values.inbox` 形状为 `{ "next-turn": UserMessage[], "next-step": UserMessage[] }`
|
|
239
|
+
* (Host 的 durable inbox 投影)。这里以消息 `id` 作为唯一身份,`rpcId` 只保留
|
|
240
|
+
* 供旧宿主/旧事件回退使用;没有文本的消息与直播追踪同口径,不占队列展示。
|
|
241
|
+
* @param {unknown} inbox Host inbox 投影视图
|
|
242
|
+
* @returns {Array<{itemId:string,text:string,target:"next-turn"|"next-step",rpcId:string}>}
|
|
243
|
+
*/
|
|
244
|
+
export function queueItemsFromInbox(inbox) {
|
|
245
|
+
const out = [];
|
|
246
|
+
const seen = new Set();
|
|
247
|
+
const push = (target, messages) => {
|
|
248
|
+
if (!Array.isArray(messages)) return;
|
|
249
|
+
for (const message of messages) {
|
|
250
|
+
const itemId = typeof message?.id === "string" ? message.id : "";
|
|
251
|
+
if (itemId === "" || seen.has(itemId)) continue;
|
|
252
|
+
const text = queueItemText(message?.content);
|
|
253
|
+
if (text.trim() === "") continue;
|
|
254
|
+
seen.add(itemId);
|
|
255
|
+
out.push({
|
|
256
|
+
itemId,
|
|
257
|
+
text,
|
|
258
|
+
target,
|
|
259
|
+
rpcId: typeof message?.source?.rpcId === "string" ? message.source.rpcId : "",
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
push("next-turn", inbox?.["next-turn"]);
|
|
264
|
+
push("next-step", inbox?.["next-step"]);
|
|
265
|
+
return out.slice(0, QUEUE_MAX_ITEMS);
|
|
266
|
+
}
|
|
267
|
+
|
|
235
268
|
/**
|
|
236
269
|
* 思考块 → 纯文本(契约 §5.3 / 附录 C)。
|
|
237
270
|
* 只取 type==='reasoning' 的块;普通正文与工具结果**不**进这里。
|
|
@@ -273,6 +306,18 @@ export function reasoningText(blocks) {
|
|
|
273
306
|
* - 任何不认识的形态返回 [](跳过),绝不抛出。
|
|
274
307
|
* @returns {object[]} ChatMessage[]
|
|
275
308
|
*/
|
|
309
|
+
/** v1.3.1:宿主唤醒消息的来源 → 客户端的触发卡类型(对齐桌面端 turnTriggerDetails)。 */
|
|
310
|
+
const WAKE_TRIGGER_KINDS = Object.freeze({
|
|
311
|
+
goal: "goal",
|
|
312
|
+
"agent-message": "agent",
|
|
313
|
+
"team-message": "team",
|
|
314
|
+
"subagent-settled": "subagent",
|
|
315
|
+
webhook: "webhook",
|
|
316
|
+
schedule: "schedule",
|
|
317
|
+
"tool-jobs": "job",
|
|
318
|
+
"cordis-host-runner": "plugin",
|
|
319
|
+
});
|
|
320
|
+
|
|
276
321
|
export function mapHistoryMessages(sessionId, record) {
|
|
277
322
|
const event = record?.event ?? record;
|
|
278
323
|
if (!event || typeof event !== "object") return [];
|
|
@@ -287,10 +332,23 @@ export function mapHistoryMessages(sessionId, record) {
|
|
|
287
332
|
|
|
288
333
|
let role;
|
|
289
334
|
let content;
|
|
335
|
+
let trigger = "";
|
|
290
336
|
if (type === "user/message") {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
337
|
+
const sourceKind = data?.source?.kind;
|
|
338
|
+
if (sourceKind === "user") {
|
|
339
|
+
role = "user";
|
|
340
|
+
content = data.content;
|
|
341
|
+
} else {
|
|
342
|
+
// v1.3.1(§5.3):宿主**唤醒消息**(不是用户手打)——Goal 轮次提示就是这一类
|
|
343
|
+
// (goal-round-driver 写入 source.kind='goal' 的 user/message)。
|
|
344
|
+
// 桌面端把它渲染成「触发卡」(继续执行目标),旧实现直接 return [] →
|
|
345
|
+
// 手机端整段缺失(用户反馈:消息列表里看不到目标消息)。
|
|
346
|
+
const kind = WAKE_TRIGGER_KINDS[sourceKind];
|
|
347
|
+
if (kind === undefined) return [];
|
|
348
|
+
role = "user";
|
|
349
|
+
trigger = kind;
|
|
350
|
+
content = data.content;
|
|
351
|
+
}
|
|
294
352
|
} else {
|
|
295
353
|
role = "assistant";
|
|
296
354
|
content = data?.message?.content;
|
|
@@ -311,10 +369,15 @@ export function mapHistoryMessages(sessionId, record) {
|
|
|
311
369
|
const images = imageBlocksOf(content).map((ref) => imageRefMeta(ref));
|
|
312
370
|
// v1.2.0(§5.11.4):文件块单独成字段(只有名称/大小,不需要读字节)
|
|
313
371
|
const files = fileRefsOfContent(content);
|
|
314
|
-
if (text.trim() !== "" || images.length > 0 || files.length > 0) {
|
|
372
|
+
if (text.trim() !== "" || images.length > 0 || files.length > 0 || trigger !== "") {
|
|
315
373
|
const message = { id: `${sessionId}:${seq}`, role, text, ts, seq };
|
|
316
374
|
if (images.length > 0) message.images = images;
|
|
317
375
|
if (files.length > 0) message.files = files;
|
|
376
|
+
if (trigger !== "") {
|
|
377
|
+
// 唤醒消息的原始注入正文由客户端隐藏(与桌面端一致),只显示触发卡。
|
|
378
|
+
message.text = "";
|
|
379
|
+
message.trigger = trigger;
|
|
380
|
+
}
|
|
318
381
|
out.push(message);
|
|
319
382
|
}
|
|
320
383
|
return out;
|
|
@@ -772,6 +835,87 @@ export function mapReasoningEfforts(rows) {
|
|
|
772
835
|
return efforts;
|
|
773
836
|
}
|
|
774
837
|
|
|
838
|
+
/**
|
|
839
|
+
* v1.3.0:Host todo 行 → wire TodoItem[](只保留 content/status,未知项丢弃)。
|
|
840
|
+
* @param {unknown} raw
|
|
841
|
+
* @returns {Array<{content:string,status:"pending"|"in_progress"|"completed"}>}
|
|
842
|
+
*/
|
|
843
|
+
export function mapTodoItems(raw) {
|
|
844
|
+
if (!Array.isArray(raw)) return [];
|
|
845
|
+
const statuses = new Set(["pending", "in_progress", "completed"]);
|
|
846
|
+
const out = [];
|
|
847
|
+
for (const item of raw) {
|
|
848
|
+
if (!item || typeof item !== "object") continue;
|
|
849
|
+
const content = typeof item.content === "string" ? item.content : "";
|
|
850
|
+
const status = typeof item.status === "string" ? item.status : "";
|
|
851
|
+
if (content === "" || !statuses.has(status)) continue;
|
|
852
|
+
out.push({ content, status });
|
|
853
|
+
}
|
|
854
|
+
return out;
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
/**
|
|
858
|
+
* v1.3.0:Host GoalProjection/GoalView → 扁平 wire GoalWire。
|
|
859
|
+
* 只接受 goal.id 与合法 phase;activation 由调用方按可用性提供。
|
|
860
|
+
* @param {unknown} goal
|
|
861
|
+
* @param {unknown} roundsStarted
|
|
862
|
+
* @param {unknown} createdAt
|
|
863
|
+
* @param {unknown} updatedAt
|
|
864
|
+
* @param {"armed"|"disarmed"|null} activation
|
|
865
|
+
* @returns {object|null}
|
|
866
|
+
*/
|
|
867
|
+
export function mapGoalWire(goal, roundsStarted, createdAt, updatedAt, activation) {
|
|
868
|
+
if (!goal || typeof goal !== "object" || typeof goal.id !== "string" || goal.id === "") return null;
|
|
869
|
+
const phases = new Set(["active", "paused", "blocked", "complete"]);
|
|
870
|
+
if (!phases.has(goal.phase)) return null;
|
|
871
|
+
const out = {
|
|
872
|
+
id: goal.id,
|
|
873
|
+
revision: Number(goal.revision) || 0,
|
|
874
|
+
objective: typeof goal.objective === "string" ? goal.objective : "",
|
|
875
|
+
phase: goal.phase,
|
|
876
|
+
maxGoalRounds: Number(goal.maxGoalRounds) || 0,
|
|
877
|
+
roundsStarted: Number(roundsStarted) || 0,
|
|
878
|
+
createdAt: Number(createdAt) || 0,
|
|
879
|
+
updatedAt: Number(updatedAt) || 0,
|
|
880
|
+
};
|
|
881
|
+
if (goal.blockedReason && typeof goal.blockedReason === "object") {
|
|
882
|
+
const code = typeof goal.blockedReason.code === "string" ? goal.blockedReason.code : "";
|
|
883
|
+
const message = typeof goal.blockedReason.message === "string" ? goal.blockedReason.message : "";
|
|
884
|
+
if (code !== "" && message !== "") out.blockedReason = { code, message };
|
|
885
|
+
}
|
|
886
|
+
if (activation === "armed" || activation === "disarmed") out.activation = activation;
|
|
887
|
+
return out;
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
/**
|
|
891
|
+
* v1.3.0:Host AskUserQuestionItem[] → wire Question[](不泄露 Host 私有字段)。
|
|
892
|
+
* @param {unknown} raw
|
|
893
|
+
* @returns {Array<object>}
|
|
894
|
+
*/
|
|
895
|
+
export function mapWireQuestions(raw) {
|
|
896
|
+
if (!Array.isArray(raw)) return [];
|
|
897
|
+
const out = [];
|
|
898
|
+
for (const q of raw) {
|
|
899
|
+
const id = typeof q?.id === "string" ? q.id : "";
|
|
900
|
+
if (id === "") continue;
|
|
901
|
+
const options = Array.isArray(q?.options)
|
|
902
|
+
? q.options
|
|
903
|
+
.map((o) => ({
|
|
904
|
+
label: typeof o?.label === "string" ? o.label : "",
|
|
905
|
+
description: typeof o?.description === "string" ? o.description : "",
|
|
906
|
+
}))
|
|
907
|
+
.filter((o) => o.label !== "")
|
|
908
|
+
: [];
|
|
909
|
+
out.push({
|
|
910
|
+
id,
|
|
911
|
+
question: typeof q?.question === "string" ? q.question : "",
|
|
912
|
+
multiSelect: q?.multiSelect === true,
|
|
913
|
+
options,
|
|
914
|
+
});
|
|
915
|
+
}
|
|
916
|
+
return out;
|
|
917
|
+
}
|
|
918
|
+
|
|
775
919
|
/**
|
|
776
920
|
* 契约 §5.8 ModelSelection:手机提交的 model → 提交给宿主的
|
|
777
921
|
* {provider, model, reasoningEffort?}(id 不可反拆,只认 provider/model)。
|
|
@@ -902,7 +1046,15 @@ export class HostBridge {
|
|
|
902
1046
|
#modelSelections = new Map(); // sessionId -> {provider, model, reasoningEffort?}(v1.0.8 内存态)
|
|
903
1047
|
// v1.1.7:有效权限的直播兜底;权威查询仍优先走 permissions 持久投影。
|
|
904
1048
|
#permissionSelections = new Map(); // sessionId -> string(含派生态 custom)
|
|
905
|
-
#pending = new Map(); // askId -> { resolve, kind, sessionId, timer, abortHandler, signal }
|
|
1049
|
+
#pending = new Map(); // askId -> { resolve, kind, sessionId, timer, abortHandler, signal, meta }
|
|
1050
|
+
// v1.3.0:Host userQuestions 投影里仍可回答的问题(continued/open),
|
|
1051
|
+
// key = sessionId + "\0" + callId;用于快照、桌面端回答后清理与手机补答路由。
|
|
1052
|
+
#hostQuestions = new Map();
|
|
1053
|
+
// v1.3.0:goal/activation-changed 的进程内状态缓存(投影不含 activation)。
|
|
1054
|
+
#goalActivations = new Map(); // sessionId -> {id,revision,activation}
|
|
1055
|
+
#askQuestionCalls = new Set(); // v1.3.0:进行中的 ask_user_question callId(用于投影对账)
|
|
1056
|
+
#todoSessions = new Set(); // v1.3.0:已确认有 todo 投影的会话(降级时不能把 turn/start 当能力声明)
|
|
1057
|
+
#clientFeatures = new Set(); // v1.3.0:hello.features(旧 App 不声明新语义)
|
|
906
1058
|
#titles = new Map(); // sessionId -> title(turn.end 推送文案用)
|
|
907
1059
|
#pushToken = ""; // 手机 push.register 上报的 token(内存态)
|
|
908
1060
|
// v1.0.9(§5.10):imageId -> 宿主附件引用(image.fetch 用;插件重启后为空)
|
|
@@ -965,6 +1117,29 @@ export class HostBridge {
|
|
|
965
1117
|
ctx.on("approval/request", (req, next) => this.#onApprovalRequest(req, next), { prepend: true });
|
|
966
1118
|
ctx.on("user-questions/request", (req, next) => this.#onQuestionRequest(req, next), { prepend: true });
|
|
967
1119
|
|
|
1120
|
+
// v1.3.0:Goal 进程内 activation(不持久,只能从 Host 事件拿)。
|
|
1121
|
+
this.#on("goal/activation-changed", (payload) => {
|
|
1122
|
+
const sessionId = String(payload?.sessionId ?? "");
|
|
1123
|
+
if (sessionId === "") return;
|
|
1124
|
+
const goal = payload?.goal;
|
|
1125
|
+
if (!goal || typeof goal.id !== "string") {
|
|
1126
|
+
this.#goalActivations.delete(sessionId);
|
|
1127
|
+
return;
|
|
1128
|
+
}
|
|
1129
|
+
const activation = goal.activation === "armed" ? "armed" : "disarmed";
|
|
1130
|
+
this.#goalActivations.set(sessionId, {
|
|
1131
|
+
id: goal.id,
|
|
1132
|
+
revision: Number(goal.revision) || 0,
|
|
1133
|
+
activation,
|
|
1134
|
+
});
|
|
1135
|
+
this.#sendEvent("session.goalActivationChanged", {
|
|
1136
|
+
sessionId,
|
|
1137
|
+
id: goal.id,
|
|
1138
|
+
revision: Number(goal.revision) || 0,
|
|
1139
|
+
activation,
|
|
1140
|
+
});
|
|
1141
|
+
});
|
|
1142
|
+
|
|
968
1143
|
// --- 事件桥接(§5.2)---
|
|
969
1144
|
this.#on("api-session/added", (summary) => {
|
|
970
1145
|
const s = mapSummary(summary);
|
|
@@ -975,6 +1150,10 @@ export class HostBridge {
|
|
|
975
1150
|
const key = String(sessionId);
|
|
976
1151
|
this.#titles.delete(key);
|
|
977
1152
|
this.#permissionSelections.delete(key);
|
|
1153
|
+
this.#goalActivations.delete(key);
|
|
1154
|
+
for (const mapKey of this.#hostQuestions.keys()) {
|
|
1155
|
+
if (mapKey.startsWith(key + "\0")) this.#hostQuestions.delete(mapKey);
|
|
1156
|
+
}
|
|
978
1157
|
// 会话没了,它的待执行列表与交付物历史条目也没有意义了(避免内存泄漏)
|
|
979
1158
|
this.#queues.delete(key);
|
|
980
1159
|
this.#deliveredEntries.delete(key);
|
|
@@ -1074,6 +1253,9 @@ export class HostBridge {
|
|
|
1074
1253
|
this.#flushStream(sessionId);
|
|
1075
1254
|
this.#sendEvent("tool.call", mapped);
|
|
1076
1255
|
}
|
|
1256
|
+
if (event?.data?.name === "ask_user_question" && typeof event?.data?.callId === "string") {
|
|
1257
|
+
this.#askQuestionCalls.add(event.data.callId);
|
|
1258
|
+
}
|
|
1077
1259
|
return;
|
|
1078
1260
|
}
|
|
1079
1261
|
if (type === "tool/result") {
|
|
@@ -1082,6 +1264,53 @@ export class HostBridge {
|
|
|
1082
1264
|
this.#flushStream(sessionId);
|
|
1083
1265
|
this.#sendEvent("tool.result", mapped);
|
|
1084
1266
|
}
|
|
1267
|
+
const callId = String(event?.data?.message?.toolCallId ?? "");
|
|
1268
|
+
let hasCachedQuestion = false;
|
|
1269
|
+
for (const key of this.#hostQuestions.keys()) {
|
|
1270
|
+
if (key.startsWith(sessionId + "\0")) {
|
|
1271
|
+
hasCachedQuestion = true;
|
|
1272
|
+
break;
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
if ((callId !== "" && this.#askQuestionCalls.has(callId)) || hasCachedQuestion) {
|
|
1276
|
+
if (callId !== "") this.#askQuestionCalls.delete(callId);
|
|
1277
|
+
// Host timed 模式:tool/result 落成 pending 后,投影会从 open 变 continued;
|
|
1278
|
+
// 异步对账,绝不阻塞事件流。
|
|
1279
|
+
void this.#sweepHostQuestions(sessionId);
|
|
1280
|
+
}
|
|
1281
|
+
return;
|
|
1282
|
+
}
|
|
1283
|
+
if (type === "todo/write") {
|
|
1284
|
+
this.#todoSessions.add(sessionId);
|
|
1285
|
+
const todos = event?.data?.todos === null ? null : mapTodoItems(event?.data?.todos);
|
|
1286
|
+
this.#sendEvent("session.todosChanged", { sessionId, todos });
|
|
1287
|
+
return;
|
|
1288
|
+
}
|
|
1289
|
+
if (type === "turn/start") {
|
|
1290
|
+
// Host todo 投影语义:下一回合开始清空上一回合清单。
|
|
1291
|
+
// 只有在已确认该会话有 todo 投影时才发;否则没有 tool-todo 的宿主会因为
|
|
1292
|
+
// 普通 turn/start 被客户端误判为「支持 Todo」(capabilities 缺省必须隐藏)。
|
|
1293
|
+
if (this.#todoSessions.has(sessionId)) {
|
|
1294
|
+
this.#sendEvent("session.todosChanged", { sessionId, todos: null });
|
|
1295
|
+
}
|
|
1296
|
+
return;
|
|
1297
|
+
}
|
|
1298
|
+
if (type === "goal/change") {
|
|
1299
|
+
const data = event?.data;
|
|
1300
|
+
const operation = typeof data?.operation === "string" ? data.operation : "";
|
|
1301
|
+
let goal = null;
|
|
1302
|
+
if (operation !== "clear") {
|
|
1303
|
+
goal = mapGoalWire(data?.goal, data?.roundsStarted, data?.createdAt, data?.updatedAt, null);
|
|
1304
|
+
if (goal !== null) {
|
|
1305
|
+
const activation = this.#goalActivations.get(sessionId);
|
|
1306
|
+
if (activation && activation.id === goal.id && activation.revision === goal.revision) {
|
|
1307
|
+
goal.activation = activation.activation;
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
} else {
|
|
1311
|
+
this.#goalActivations.delete(sessionId);
|
|
1312
|
+
}
|
|
1313
|
+
this.#sendEvent("session.goalChanged", { sessionId, goal, operation: operation || "edit" });
|
|
1085
1314
|
return;
|
|
1086
1315
|
}
|
|
1087
1316
|
if (type === "model/selection") {
|
|
@@ -1122,6 +1351,16 @@ export class HostBridge {
|
|
|
1122
1351
|
if (type === "user/message") {
|
|
1123
1352
|
// v1.1.1(§5.2):真人输入被消费(进入某个 turn)→ 从待执行列表里摘掉。
|
|
1124
1353
|
this.#handleUserMessage(sessionId, event);
|
|
1354
|
+
// v1.3.0:迟到回答/桌面回答会以 user-question-reply 写入,对账投影并清卡。
|
|
1355
|
+
if (event?.data?.source?.kind === "user-question-reply") {
|
|
1356
|
+
void this.#sweepHostQuestions(sessionId);
|
|
1357
|
+
}
|
|
1358
|
+
// v1.3.1:Goal 轮次推进 = goal-round-driver 写入 source.kind="goal" 的
|
|
1359
|
+
// user/message,而 Host **只在 goal/change 发事件**(轮次推进不发)→
|
|
1360
|
+
// 不在这里补一次广播,手机端会一直停在上一个轮次。
|
|
1361
|
+
if (event?.data?.source?.kind === "goal") {
|
|
1362
|
+
void this.#emitGoalRound(sessionId);
|
|
1363
|
+
}
|
|
1125
1364
|
return;
|
|
1126
1365
|
}
|
|
1127
1366
|
// 其余事件(turn/*、step/* …)忽略
|
|
@@ -1201,8 +1440,14 @@ export class HostBridge {
|
|
|
1201
1440
|
}
|
|
1202
1441
|
const inserted = Array.isArray(data?.inserted) ? data.inserted : [];
|
|
1203
1442
|
for (const message of inserted) {
|
|
1443
|
+
// v1.3.1:只跟踪**真人输入**。Goal 轮次提示(source.kind="goal")等宿主自动注入
|
|
1444
|
+
// 的唤醒消息不进手机「排队消息」——它们由触发卡表达(「继续执行目标」),
|
|
1445
|
+
// 真进队列只会把注入的原始 prompt 漏出来,还容易看起来像「残留」。
|
|
1446
|
+
const sourceKind = message?.source?.kind;
|
|
1447
|
+
if (sourceKind !== undefined && sourceKind !== "user") continue;
|
|
1204
1448
|
const itemId = typeof message?.id === "string" ? message.id : "";
|
|
1205
1449
|
if (itemId === "") continue;
|
|
1450
|
+
if (items.some((item) => item.itemId === itemId)) continue; // 同一 Host 消息只能占一项
|
|
1206
1451
|
const text = queueItemText(message?.content);
|
|
1207
1452
|
if (text.trim() === "") continue; // 没有文本 → 不是一条可展示的待执行消息
|
|
1208
1453
|
if (items.length >= QUEUE_MAX_ITEMS) {
|
|
@@ -1228,12 +1473,20 @@ export class HostBridge {
|
|
|
1228
1473
|
*/
|
|
1229
1474
|
#handleUserMessage(sessionId, event) {
|
|
1230
1475
|
const data = event?.data;
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1476
|
+
// v1.3.1:出队按 Host 消息身份(data.id)判定,**不再要求 source.kind==='user'**
|
|
1477
|
+
// ——凡进过队列的消息被消费就必须出队(旧实现只认真人来源,任何被跟踪的
|
|
1478
|
+
// 非 user 来源消息都会永久残留,这正是「排队残留」的一类)。
|
|
1234
1479
|
const items = this.#queues.get(sessionId);
|
|
1235
1480
|
if (!items || items.length === 0) return;
|
|
1236
|
-
|
|
1481
|
+
// v1.3.0:Host 消息 id 是队列项的真实身份;子代理初始注入可以**没有 rpcId**
|
|
1482
|
+
// (source.kind='user' 但无 rpcId)。旧实现只认 rpcId → 消费后永远不出队。
|
|
1483
|
+
const itemId = typeof data?.id === "string" ? data.id : "";
|
|
1484
|
+
const rpcId = typeof data?.source?.rpcId === "string" ? data.source.rpcId : "";
|
|
1485
|
+
let index = itemId === "" ? -1 : items.findIndex((item) => item.itemId === itemId);
|
|
1486
|
+
if (index < 0 && rpcId !== "") {
|
|
1487
|
+
// 旧宿主/旧事件缺 id 时保留 rpcId 回退(只对带 rpcId 的项生效)。
|
|
1488
|
+
index = items.findIndex((item) => item.rpcId !== "" && item.rpcId === rpcId);
|
|
1489
|
+
}
|
|
1237
1490
|
if (index < 0) return;
|
|
1238
1491
|
const target = items[index]?.target ?? "";
|
|
1239
1492
|
items.splice(index, 1);
|
|
@@ -1292,16 +1545,325 @@ export class HostBridge {
|
|
|
1292
1545
|
* ——用户会看到「排队列表空着,但桌面端明明排着一条」。
|
|
1293
1546
|
* 故客户端打开会话时拉一次全量快照(与事件同形状)。
|
|
1294
1547
|
*/
|
|
1295
|
-
#rSessionQueue(m) {
|
|
1548
|
+
async #rSessionQueue(m) {
|
|
1296
1549
|
const p = m.payload ?? {};
|
|
1297
1550
|
const sessionId = typeof p.sessionId === "string" ? p.sessionId : "";
|
|
1298
1551
|
if (sessionId === "") {
|
|
1299
1552
|
this.#reply(m, errorOf(m, "bad-params", "sessionId required"));
|
|
1300
1553
|
return;
|
|
1301
1554
|
}
|
|
1555
|
+
// v1.3.0:优先读 Host 的 inbox 持久投影,事件缓存只作旧宿主回退。
|
|
1556
|
+
// 这样即使漏掉 inbox/spliced 或 user/message,回前台仍能对账出真实队列。
|
|
1557
|
+
try {
|
|
1558
|
+
const sc = this.#sc();
|
|
1559
|
+
if (typeof sc.projections === "function") {
|
|
1560
|
+
const projections = await sc.projections({ sessionId }, this.#signal(10_000));
|
|
1561
|
+
const inbox = projections?.values?.inbox;
|
|
1562
|
+
if (inbox !== undefined && inbox !== null && typeof inbox === "object") {
|
|
1563
|
+
const items = queueItemsFromInbox(inbox);
|
|
1564
|
+
this.#queues.set(sessionId, items.map((item) => ({ ...item })));
|
|
1565
|
+
this.#reply(m, resultOf(m, { sessionId, items: this.#queueItemsOf(sessionId) }));
|
|
1566
|
+
return;
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
} catch (e) {
|
|
1570
|
+
this.#log("warn", "inbox projection unavailable; falling back to live queue tracking", {
|
|
1571
|
+
sessionId,
|
|
1572
|
+
error: errText(e),
|
|
1573
|
+
});
|
|
1574
|
+
}
|
|
1302
1575
|
this.#reply(m, resultOf(m, { sessionId, items: this.#queueItemsOf(sessionId) }));
|
|
1303
1576
|
}
|
|
1304
1577
|
|
|
1578
|
+
// ---- v1.3.0:待答交互 / Todo / Goal ------------------------------------
|
|
1579
|
+
|
|
1580
|
+
/** 读一个会话的投影值;失败返回 null(调用方自行降级)。 */
|
|
1581
|
+
async #projectionValues(sessionId) {
|
|
1582
|
+
try {
|
|
1583
|
+
const sc = this.#sc();
|
|
1584
|
+
if (typeof sc.projections !== "function") return null;
|
|
1585
|
+
const projections = await sc.projections({ sessionId }, this.#signal(10_000));
|
|
1586
|
+
const values = projections?.values;
|
|
1587
|
+
return values && typeof values === "object" ? values : null;
|
|
1588
|
+
} catch (e) {
|
|
1589
|
+
this.#log("warn", "projection read failed", { sessionId, error: errText(e) });
|
|
1590
|
+
return null;
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
/** 把一个 Host userQuestions active 行映射成 wire PendingQuestion。 */
|
|
1595
|
+
#hostQuestionWire(sessionId, row) {
|
|
1596
|
+
const callId = typeof row?.callId === "string" ? row.callId : "";
|
|
1597
|
+
if (callId === "") return null;
|
|
1598
|
+
const questions = mapWireQuestions(row?.questions);
|
|
1599
|
+
if (questions.length === 0) return null;
|
|
1600
|
+
const continued = row?.state === "continued";
|
|
1601
|
+
return {
|
|
1602
|
+
askId: callId,
|
|
1603
|
+
sessionId,
|
|
1604
|
+
callId,
|
|
1605
|
+
questions,
|
|
1606
|
+
state: continued ? "continued" : "open",
|
|
1607
|
+
answerable: continued,
|
|
1608
|
+
source: "host",
|
|
1609
|
+
multiSelect: questions.some((q) => q.multiSelect),
|
|
1610
|
+
};
|
|
1611
|
+
}
|
|
1612
|
+
|
|
1613
|
+
/**
|
|
1614
|
+
* 对账 Host userQuestions 投影:把 active 行发布为 question.request,并清掉
|
|
1615
|
+
* 已从投影消失的卡片。`tool/result`、user-question-reply 与快照请求都会触发。
|
|
1616
|
+
* 任何异常只记录,绝不影响瀑布。
|
|
1617
|
+
*/
|
|
1618
|
+
async #sweepHostQuestions(sessionId) {
|
|
1619
|
+
const values = await this.#projectionValues(sessionId);
|
|
1620
|
+
const active = values?.userQuestions?.active;
|
|
1621
|
+
if (!Array.isArray(active)) return;
|
|
1622
|
+
// 已经提交但尚在 Host inbox 等待接纳的迟到回答:桌面 UI 也会暂不显示该卡,
|
|
1623
|
+
// 避免用户回答后回前台又看到一张“可再次回答”的重复卡。
|
|
1624
|
+
const queuedReplies = new Set();
|
|
1625
|
+
const inbox = values?.inbox;
|
|
1626
|
+
for (const target of ["next-step", "next-turn"]) {
|
|
1627
|
+
const messages = inbox?.[target];
|
|
1628
|
+
if (!Array.isArray(messages)) continue;
|
|
1629
|
+
for (const message of messages) {
|
|
1630
|
+
const source = message?.source;
|
|
1631
|
+
if (source?.kind === "user-question-reply" && typeof source.callId === "string") {
|
|
1632
|
+
queuedReplies.add(source.callId);
|
|
1633
|
+
}
|
|
1634
|
+
}
|
|
1635
|
+
}
|
|
1636
|
+
const prefix = sessionId + "\0";
|
|
1637
|
+
const nextKeys = new Set();
|
|
1638
|
+
for (const row of active) {
|
|
1639
|
+
const wire = this.#hostQuestionWire(sessionId, row);
|
|
1640
|
+
if (wire === null) continue;
|
|
1641
|
+
if (wire.state === "continued" && queuedReplies.has(wire.callId)) continue;
|
|
1642
|
+
const key = prefix + wire.callId;
|
|
1643
|
+
nextKeys.add(key);
|
|
1644
|
+
this.#hostQuestions.set(key, wire);
|
|
1645
|
+
// 同一 askId 重发 = 状态更新(客户端替换卡片,不叠加)。
|
|
1646
|
+
// 旧 App 会把重发当新卡,只有声明 session-state-v1 的客户端才接收状态更新。
|
|
1647
|
+
if (this.#clientFeatures.has("session-state-v1")) {
|
|
1648
|
+
this.#sendEvent("question.request", wire, wire.state === "continued");
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
for (const [key, old] of this.#hostQuestions) {
|
|
1652
|
+
if (!key.startsWith(prefix) || nextKeys.has(key)) continue;
|
|
1653
|
+
this.#hostQuestions.delete(key);
|
|
1654
|
+
this.#sendEvent("question.resolved", { askId: old.askId, source: "desktop" });
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
|
|
1658
|
+
/** pending.interactions:插件实时待答 + Host 投影(指定会话时)合并且按 askId 去重。 */
|
|
1659
|
+
async #rPendingInteractions(m) {
|
|
1660
|
+
const p = m.payload ?? {};
|
|
1661
|
+
const sessionId = typeof p.sessionId === "string" ? p.sessionId : "";
|
|
1662
|
+
// 能说新请求本身就是能力声明(旧 App 不会发);幂等补进 feature 集合,
|
|
1663
|
+
// 避免新 App 忘记在 hello 声明时把遗留 continued 卡误当旧客户端。
|
|
1664
|
+
this.#clientFeatures.add("session-state-v1");
|
|
1665
|
+
const questions = [];
|
|
1666
|
+
const approvals = [];
|
|
1667
|
+
for (const [askId, entry] of this.#pending) {
|
|
1668
|
+
if (sessionId !== "" && entry.sessionId !== sessionId) continue;
|
|
1669
|
+
if (entry.kind === "approval") {
|
|
1670
|
+
approvals.push({
|
|
1671
|
+
...entry.meta,
|
|
1672
|
+
askId,
|
|
1673
|
+
sessionId: entry.sessionId,
|
|
1674
|
+
answerable: true,
|
|
1675
|
+
source: "plugin",
|
|
1676
|
+
...(entry.expiresAt ? { expiresAt: entry.expiresAt } : {}),
|
|
1677
|
+
});
|
|
1678
|
+
} else {
|
|
1679
|
+
questions.push({
|
|
1680
|
+
...entry.meta,
|
|
1681
|
+
askId,
|
|
1682
|
+
sessionId: entry.sessionId,
|
|
1683
|
+
state: "open",
|
|
1684
|
+
answerable: true,
|
|
1685
|
+
source: "plugin",
|
|
1686
|
+
...(entry.expiresAt ? { expiresAt: entry.expiresAt } : {}),
|
|
1687
|
+
});
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
if (sessionId !== "") {
|
|
1691
|
+
await this.#sweepHostQuestions(sessionId);
|
|
1692
|
+
for (const wire of this.#hostQuestions.values()) {
|
|
1693
|
+
if (wire.sessionId !== sessionId) continue;
|
|
1694
|
+
if (questions.some((q) => q.askId === wire.askId)) continue;
|
|
1695
|
+
questions.push(wire);
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
this.#reply(m, resultOf(m, { questions, approvals }));
|
|
1699
|
+
}
|
|
1700
|
+
|
|
1701
|
+
/** session.todos:Host `todos` 持久投影(只读)。 */
|
|
1702
|
+
async #rSessionTodos(m) {
|
|
1703
|
+
const p = m.payload ?? {};
|
|
1704
|
+
const sessionId = typeof p.sessionId === "string" ? p.sessionId : "";
|
|
1705
|
+
if (sessionId === "") {
|
|
1706
|
+
this.#reply(m, errorOf(m, "bad-params", "sessionId required"));
|
|
1707
|
+
return;
|
|
1708
|
+
}
|
|
1709
|
+
const values = await this.#projectionValues(sessionId);
|
|
1710
|
+
const supported = values !== null && Object.prototype.hasOwnProperty.call(values, "todos");
|
|
1711
|
+
const todos = !supported || values.todos === null ? null : mapTodoItems(values.todos);
|
|
1712
|
+
if (supported) this.#todoSessions.add(sessionId);
|
|
1713
|
+
else this.#todoSessions.delete(sessionId);
|
|
1714
|
+
this.#reply(m, resultOf(m, { sessionId, todos, supported }));
|
|
1715
|
+
}
|
|
1716
|
+
|
|
1717
|
+
/** Goal 人类操作集合(只暴露 Host GoalService 真正存在的方法)。 */
|
|
1718
|
+
#goalActions(goals) {
|
|
1719
|
+
if (!goals) return [];
|
|
1720
|
+
const wanted = ["create", "edit", "pause", "resume", "complete", "clear"];
|
|
1721
|
+
return wanted.filter((name) => typeof goals[name] === "function");
|
|
1722
|
+
}
|
|
1723
|
+
|
|
1724
|
+
/** 读会话当前 goal + activation(不激活冷会话)。 */
|
|
1725
|
+
async #readGoal(sessionId) {
|
|
1726
|
+
const values = await this.#projectionValues(sessionId);
|
|
1727
|
+
const supported = values !== null && Object.prototype.hasOwnProperty.call(values, "goal");
|
|
1728
|
+
const raw = supported ? values.goal : null;
|
|
1729
|
+
let activation = null;
|
|
1730
|
+
let goal = null;
|
|
1731
|
+
if (raw && typeof raw === "object" && raw.goal && typeof raw.goal.id === "string") {
|
|
1732
|
+
const cached = this.#goalActivations.get(sessionId);
|
|
1733
|
+
if (cached && cached.id === raw.goal.id && cached.revision === raw.goal.revision) {
|
|
1734
|
+
activation = cached.activation;
|
|
1735
|
+
}
|
|
1736
|
+
// 有活 Agent 时从 Host 服务读准确的 activation;绝不为了查询激活冷会话。
|
|
1737
|
+
try {
|
|
1738
|
+
const agents = this.#service("agents");
|
|
1739
|
+
const live = agents && typeof agents.get === "function" ? agents.get(sessionId) : null;
|
|
1740
|
+
const goals = this.#service("goals");
|
|
1741
|
+
if (live && goals && typeof goals.get === "function") {
|
|
1742
|
+
const view = goals.get(live);
|
|
1743
|
+
if (view && view.id === raw.goal.id) activation = view.activation === "armed" ? "armed" : "disarmed";
|
|
1744
|
+
}
|
|
1745
|
+
} catch (e) {
|
|
1746
|
+
this.#log("warn", "goal activation read failed", { sessionId, error: errText(e) });
|
|
1747
|
+
}
|
|
1748
|
+
goal = mapGoalWire(raw.goal, raw.roundsStarted, raw.createdAt, raw.updatedAt, activation);
|
|
1749
|
+
}
|
|
1750
|
+
return { supported, goal, activation };
|
|
1751
|
+
}
|
|
1752
|
+
|
|
1753
|
+
/** v1.3.1:轮次推进(Host 不发 goal/change)→ 重读投影并广播 operation="round"。 */
|
|
1754
|
+
async #emitGoalRound(sessionId) {
|
|
1755
|
+
try {
|
|
1756
|
+
const state = await this.#readGoal(sessionId);
|
|
1757
|
+
if (state.goal === null) return;
|
|
1758
|
+
const goal = state.goal;
|
|
1759
|
+
const cached = this.#goalActivations.get(sessionId);
|
|
1760
|
+
if (cached && cached.id === goal.id && cached.revision === goal.revision) {
|
|
1761
|
+
goal.activation = cached.activation;
|
|
1762
|
+
}
|
|
1763
|
+
this.#sendEvent("session.goalChanged", { sessionId, goal, operation: "round" });
|
|
1764
|
+
} catch (e) {
|
|
1765
|
+
this.#log("warn", "goal round broadcast failed", { sessionId, error: errText(e) });
|
|
1766
|
+
}
|
|
1767
|
+
}
|
|
1768
|
+
|
|
1769
|
+
/** session.goal:读持久投影 + 进程内 activation。 */
|
|
1770
|
+
async #rSessionGoal(m) {
|
|
1771
|
+
const p = m.payload ?? {};
|
|
1772
|
+
const sessionId = typeof p.sessionId === "string" ? p.sessionId : "";
|
|
1773
|
+
if (sessionId === "") {
|
|
1774
|
+
this.#reply(m, errorOf(m, "bad-params", "sessionId required"));
|
|
1775
|
+
return;
|
|
1776
|
+
}
|
|
1777
|
+
const state = await this.#readGoal(sessionId);
|
|
1778
|
+
const goals = this.#service("goals");
|
|
1779
|
+
this.#reply(
|
|
1780
|
+
m,
|
|
1781
|
+
resultOf(m, {
|
|
1782
|
+
sessionId,
|
|
1783
|
+
goal: state.goal,
|
|
1784
|
+
activation: state.activation,
|
|
1785
|
+
supported: state.supported || goals !== null,
|
|
1786
|
+
actions: this.#goalActions(goals),
|
|
1787
|
+
}),
|
|
1788
|
+
);
|
|
1789
|
+
}
|
|
1790
|
+
|
|
1791
|
+
/** session.goalAction:手机人类操作,走 Host GoalService + CAS ref。 */
|
|
1792
|
+
async #rSessionGoalAction(m) {
|
|
1793
|
+
const p = m.payload ?? {};
|
|
1794
|
+
const sessionId = typeof p.sessionId === "string" ? p.sessionId : "";
|
|
1795
|
+
const action = typeof p.action === "string" ? p.action : "";
|
|
1796
|
+
const allowed = ["create", "edit", "pause", "resume", "complete", "clear"];
|
|
1797
|
+
if (sessionId === "" || !allowed.includes(action)) {
|
|
1798
|
+
this.#reply(m, errorOf(m, "bad-params", "sessionId and a valid action required"));
|
|
1799
|
+
return;
|
|
1800
|
+
}
|
|
1801
|
+
const goals = this.#service("goals");
|
|
1802
|
+
if (!goals || typeof goals[action] !== "function") {
|
|
1803
|
+
this.#reply(m, errorOf(m, "service-unavailable", "goals service unavailable"));
|
|
1804
|
+
return;
|
|
1805
|
+
}
|
|
1806
|
+
let agent = null;
|
|
1807
|
+
try {
|
|
1808
|
+
const sc = this.#sc();
|
|
1809
|
+
if (typeof sc.resolveAgent !== "function") throw new Error("sessionController.resolveAgent unavailable");
|
|
1810
|
+
const resolved = await sc.resolveAgent(sessionId);
|
|
1811
|
+
if (resolved?.error) throw resolved.error;
|
|
1812
|
+
agent = resolved?.agent ?? resolved?.session?.agent ?? null;
|
|
1813
|
+
if (!agent) throw new Error(`session "${sessionId}" has no live agent`);
|
|
1814
|
+
} catch (e) {
|
|
1815
|
+
const err = hostErrorOf(e, "goal-agent-unavailable");
|
|
1816
|
+
this.#reply(m, errorOf(m, err.code, err.message));
|
|
1817
|
+
return;
|
|
1818
|
+
}
|
|
1819
|
+
let expected = null;
|
|
1820
|
+
if (action !== "create") {
|
|
1821
|
+
const raw = p.expected;
|
|
1822
|
+
if (!raw || typeof raw.id !== "string" || !Number.isInteger(raw.revision) || raw.revision < 1) {
|
|
1823
|
+
this.#reply(m, errorOf(m, "bad-params", "expected {id, revision} required"));
|
|
1824
|
+
return;
|
|
1825
|
+
}
|
|
1826
|
+
expected = { id: raw.id, revision: raw.revision };
|
|
1827
|
+
}
|
|
1828
|
+
const objective = typeof p.objective === "string" ? p.objective.trim() : "";
|
|
1829
|
+
const maxGoalRounds = p.maxGoalRounds;
|
|
1830
|
+
if ((action === "create" || action === "edit") && objective === "" && action === "create") {
|
|
1831
|
+
this.#reply(m, errorOf(m, "bad-params", "objective required for create"));
|
|
1832
|
+
return;
|
|
1833
|
+
}
|
|
1834
|
+
if (action === "edit" && objective === "" && maxGoalRounds === undefined) {
|
|
1835
|
+
this.#reply(m, errorOf(m, "bad-params", "edit requires objective or maxGoalRounds"));
|
|
1836
|
+
return;
|
|
1837
|
+
}
|
|
1838
|
+
if (maxGoalRounds !== undefined && (!Number.isInteger(maxGoalRounds) || maxGoalRounds < 1)) {
|
|
1839
|
+
this.#reply(m, errorOf(m, "bad-params", "maxGoalRounds must be a positive integer"));
|
|
1840
|
+
return;
|
|
1841
|
+
}
|
|
1842
|
+
try {
|
|
1843
|
+
if (action === "create") {
|
|
1844
|
+
await goals.create(agent, {
|
|
1845
|
+
objective,
|
|
1846
|
+
...(maxGoalRounds === undefined ? {} : { maxGoalRounds }),
|
|
1847
|
+
});
|
|
1848
|
+
} else if (action === "edit") {
|
|
1849
|
+
await goals.edit(agent, expected, {
|
|
1850
|
+
...(objective === "" ? {} : { objective }),
|
|
1851
|
+
...(maxGoalRounds === undefined ? {} : { maxGoalRounds }),
|
|
1852
|
+
});
|
|
1853
|
+
} else if (action === "clear") {
|
|
1854
|
+
await goals.clear(agent, expected);
|
|
1855
|
+
} else {
|
|
1856
|
+
await goals[action](agent, expected);
|
|
1857
|
+
}
|
|
1858
|
+
const state = await this.#readGoal(sessionId);
|
|
1859
|
+
this.#reply(m, resultOf(m, { sessionId, goal: state.goal }));
|
|
1860
|
+
} catch (e) {
|
|
1861
|
+
const err = hostErrorOf(e, "goal-action-failed");
|
|
1862
|
+
this.#log("warn", "goal action failed", { sessionId, action, code: err.code });
|
|
1863
|
+
this.#reply(m, errorOf(m, err.code, err.message));
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
|
|
1305
1867
|
/** 归档集合(宿主没有 Workspace 注册表 → null,表示「不知道」而不是「空」)。 */
|
|
1306
1868
|
#archivedSessionIds() {
|
|
1307
1869
|
const reg = this.#workspaceRegistry();
|
|
@@ -1390,6 +1952,11 @@ export class HostBridge {
|
|
|
1390
1952
|
entry.resolve({ settled: "timeout" });
|
|
1391
1953
|
}
|
|
1392
1954
|
this.#pending.clear();
|
|
1955
|
+
this.#hostQuestions.clear();
|
|
1956
|
+
this.#goalActivations.clear();
|
|
1957
|
+
this.#askQuestionCalls.clear();
|
|
1958
|
+
this.#todoSessions.clear();
|
|
1959
|
+
this.#clientFeatures.clear();
|
|
1393
1960
|
for (const off of this.#disposers) {
|
|
1394
1961
|
try {
|
|
1395
1962
|
off();
|
|
@@ -1896,7 +2463,15 @@ export class HostBridge {
|
|
|
1896
2463
|
case "session.permission":
|
|
1897
2464
|
return await this.#rSessionPermission(m);
|
|
1898
2465
|
case "session.queue":
|
|
1899
|
-
return this.#rSessionQueue(m);
|
|
2466
|
+
return await this.#rSessionQueue(m);
|
|
2467
|
+
case "pending.interactions":
|
|
2468
|
+
return await this.#rPendingInteractions(m);
|
|
2469
|
+
case "session.todos":
|
|
2470
|
+
return await this.#rSessionTodos(m);
|
|
2471
|
+
case "session.goal":
|
|
2472
|
+
return await this.#rSessionGoal(m);
|
|
2473
|
+
case "session.goalAction":
|
|
2474
|
+
return await this.#rSessionGoalAction(m);
|
|
1900
2475
|
case "session.archive":
|
|
1901
2476
|
return await this.#rSessionArchive(m);
|
|
1902
2477
|
case "session.unarchive":
|
|
@@ -1921,7 +2496,7 @@ export class HostBridge {
|
|
|
1921
2496
|
case "approval.answer":
|
|
1922
2497
|
return this.#rApprovalAnswer(m);
|
|
1923
2498
|
case "question.answer":
|
|
1924
|
-
return this.#rQuestionAnswer(m);
|
|
2499
|
+
return await this.#rQuestionAnswer(m);
|
|
1925
2500
|
case "push.register":
|
|
1926
2501
|
return this.#rPushRegister(m);
|
|
1927
2502
|
default:
|
|
@@ -1991,6 +2566,11 @@ export class HostBridge {
|
|
|
1991
2566
|
#rHello(m) {
|
|
1992
2567
|
const p = m.payload ?? {};
|
|
1993
2568
|
if (typeof p.pushToken === "string" && p.pushToken !== "") this.#pushToken = p.pushToken;
|
|
2569
|
+
this.#clientFeatures = new Set(
|
|
2570
|
+
Array.isArray(p.features)
|
|
2571
|
+
? p.features.filter((f) => typeof f === "string" && f !== "").slice(0, 16)
|
|
2572
|
+
: [],
|
|
2573
|
+
);
|
|
1994
2574
|
this.#reply(
|
|
1995
2575
|
m,
|
|
1996
2576
|
resultOf(m, {
|
|
@@ -2535,6 +3115,21 @@ export class HostBridge {
|
|
|
2535
3115
|
// v1.2.0(§5.11.1):宿主具备附件服务(attachments + fileUploads)才声明附件能力;
|
|
2536
3116
|
// 否则**省略该字段**,客户端隐藏附件入口(旧插件也没有该字段)。
|
|
2537
3117
|
if (this.#attachmentHostReady()) capabilities.attachments = attachmentCapabilities();
|
|
3118
|
+
// v1.3.0:新协议能力(字段缺省 = 旧 Host 不支持)。
|
|
3119
|
+
const goals = this.#service("goals");
|
|
3120
|
+
capabilities.todos = { supported: true }; // 具体会话是否有 todo 投影由 session.todos.supported 判定
|
|
3121
|
+
capabilities.goal = { read: true, actions: this.#goalActions(goals) };
|
|
3122
|
+
let hostContinued = false;
|
|
3123
|
+
try {
|
|
3124
|
+
hostContinued = typeof this.#sc().projections === "function";
|
|
3125
|
+
} catch {
|
|
3126
|
+
hostContinued = false;
|
|
3127
|
+
}
|
|
3128
|
+
capabilities.pendingInteractions = {
|
|
3129
|
+
questions: true,
|
|
3130
|
+
approvals: true,
|
|
3131
|
+
hostContinued,
|
|
3132
|
+
};
|
|
2538
3133
|
this.#reply(m, resultOf(m, capabilities));
|
|
2539
3134
|
}
|
|
2540
3135
|
|
|
@@ -3251,21 +3846,74 @@ export class HostBridge {
|
|
|
3251
3846
|
this.#reply(m, resultOf(m, { applied: true }));
|
|
3252
3847
|
}
|
|
3253
3848
|
|
|
3254
|
-
#rQuestionAnswer(m) {
|
|
3849
|
+
async #rQuestionAnswer(m) {
|
|
3255
3850
|
const p = m.payload ?? {};
|
|
3256
3851
|
const askId = typeof p.askId === "string" ? p.askId : "";
|
|
3852
|
+
const answers = p.answers;
|
|
3853
|
+
if (answers === null || typeof answers !== "object" || Array.isArray(answers)) {
|
|
3854
|
+
this.#reply(m, errorOf(m, "bad-answers", "answers must be an object of questionId -> label(s)"));
|
|
3855
|
+
return;
|
|
3856
|
+
}
|
|
3857
|
+
const freeText = typeof p.freeText === "string" ? p.freeText : "";
|
|
3257
3858
|
const entry = askId === "" ? undefined : this.#pending.get(askId);
|
|
3258
|
-
if (entry
|
|
3859
|
+
if (entry !== undefined && entry.kind === "question") {
|
|
3860
|
+
this.#settle(entry, "phone", { answers, freeText });
|
|
3861
|
+
this.#reply(m, resultOf(m, { applied: true }));
|
|
3862
|
+
return;
|
|
3863
|
+
}
|
|
3864
|
+
|
|
3865
|
+
// v1.3.0:超过插件独占窗口后,Host timed 模式仍将问题保持在 continued;
|
|
3866
|
+
// 此时必须走 Host 官方 answer(steer 一条 user-question-reply),禁止伪造消息。
|
|
3867
|
+
let hostQuestion = null;
|
|
3868
|
+
for (const wire of this.#hostQuestions.values()) {
|
|
3869
|
+
if (wire.askId === askId || wire.callId === askId) {
|
|
3870
|
+
hostQuestion = wire;
|
|
3871
|
+
break;
|
|
3872
|
+
}
|
|
3873
|
+
}
|
|
3874
|
+
if (hostQuestion === null || hostQuestion.state !== "continued") {
|
|
3259
3875
|
this.#reply(m, resultOf(m, { applied: false }));
|
|
3260
3876
|
return;
|
|
3261
3877
|
}
|
|
3262
|
-
const
|
|
3263
|
-
|
|
3264
|
-
|
|
3878
|
+
const questions = hostQuestion.questions;
|
|
3879
|
+
const userQuestions = this.#service("userQuestions");
|
|
3880
|
+
if (!userQuestions || typeof userQuestions.answer !== "function") {
|
|
3881
|
+
this.#reply(m, errorOf(m, "service-unavailable", "userQuestions.answer unavailable"));
|
|
3265
3882
|
return;
|
|
3266
3883
|
}
|
|
3267
|
-
|
|
3268
|
-
|
|
3884
|
+
let agent = null;
|
|
3885
|
+
try {
|
|
3886
|
+
const sc = this.#sc();
|
|
3887
|
+
const resolved = await sc.resolveAgent(hostQuestion.sessionId);
|
|
3888
|
+
if (resolved?.error) throw resolved.error;
|
|
3889
|
+
agent = resolved?.agent ?? resolved?.session?.agent ?? null;
|
|
3890
|
+
if (!agent) throw new Error(`session "${hostQuestion.sessionId}" has no live agent`);
|
|
3891
|
+
} catch (e) {
|
|
3892
|
+
const err = hostErrorOf(e, "question-agent-unavailable");
|
|
3893
|
+
this.#reply(m, errorOf(m, err.code, err.message));
|
|
3894
|
+
return;
|
|
3895
|
+
}
|
|
3896
|
+
try {
|
|
3897
|
+
const answer = toAskUserQuestionAnswer(questions, answers, freeText);
|
|
3898
|
+
const accepted =
|
|
3899
|
+
questions.every((q) => answer.answers.some((a) => a.id === q.id)) &&
|
|
3900
|
+
userQuestions.answer(agent, hostQuestion.callId, answer) === true;
|
|
3901
|
+
if (!accepted) {
|
|
3902
|
+
this.#reply(m, resultOf(m, { applied: false }));
|
|
3903
|
+
return;
|
|
3904
|
+
}
|
|
3905
|
+
this.#hostQuestions.delete(hostQuestion.sessionId + "\0" + hostQuestion.callId);
|
|
3906
|
+
this.#sendEvent("question.resolved", { askId: hostQuestion.askId, source: "phone" });
|
|
3907
|
+
this.#reply(m, resultOf(m, { applied: true }));
|
|
3908
|
+
} catch (e) {
|
|
3909
|
+
const err = hostErrorOf(e, "question-answer-failed");
|
|
3910
|
+
this.#log("warn", "continued question answer failed", {
|
|
3911
|
+
sessionId: hostQuestion.sessionId,
|
|
3912
|
+
code: err.code,
|
|
3913
|
+
});
|
|
3914
|
+
// Host 错误码原样给客户端,便于展示 REPLY_QUEUED / BAD_ANSWER。
|
|
3915
|
+
this.#reply(m, errorOf(m, err.code, err.message));
|
|
3916
|
+
}
|
|
3269
3917
|
}
|
|
3270
3918
|
|
|
3271
3919
|
#rPushRegister(m) {
|
|
@@ -3310,16 +3958,16 @@ export class HostBridge {
|
|
|
3310
3958
|
const askId = randomUUID();
|
|
3311
3959
|
const sessionId = String(req?.agent?.session?.id ?? "");
|
|
3312
3960
|
const timeoutMs = Number(this.#cfg.approvalTimeoutMs) || 120_000;
|
|
3961
|
+
const expiresAt = Date.now() + timeoutMs;
|
|
3962
|
+
const meta = {
|
|
3963
|
+
toolName: String(req?.toolName ?? "unknown"),
|
|
3964
|
+
reason: typeof req?.reason === "string" ? req.reason : "",
|
|
3965
|
+
argsPreview: this.#argsPreviewOf(req?.agent, req?.callId),
|
|
3966
|
+
timeoutMs,
|
|
3967
|
+
};
|
|
3313
3968
|
const sent = this.#sendEvent(
|
|
3314
3969
|
"approval.request",
|
|
3315
|
-
{
|
|
3316
|
-
askId,
|
|
3317
|
-
sessionId,
|
|
3318
|
-
toolName: String(req?.toolName ?? "unknown"),
|
|
3319
|
-
reason: typeof req?.reason === "string" ? req.reason : "",
|
|
3320
|
-
argsPreview: this.#argsPreviewOf(req?.agent, req?.callId),
|
|
3321
|
-
timeoutMs,
|
|
3322
|
-
},
|
|
3970
|
+
{ askId, sessionId, ...meta, expiresAt },
|
|
3323
3971
|
true, // urgent:离线时优先补发(契约 §4)
|
|
3324
3972
|
);
|
|
3325
3973
|
if (!sent) {
|
|
@@ -3337,6 +3985,8 @@ export class HostBridge {
|
|
|
3337
3985
|
sessionId,
|
|
3338
3986
|
signal: req?.signal,
|
|
3339
3987
|
timeoutMs,
|
|
3988
|
+
expiresAt,
|
|
3989
|
+
meta,
|
|
3340
3990
|
});
|
|
3341
3991
|
if (outcome.settled === "phone") {
|
|
3342
3992
|
const decision = outcome.decision === "approved" ? "approved" : "rejected";
|
|
@@ -3364,27 +4014,32 @@ export class HostBridge {
|
|
|
3364
4014
|
/** user-questions/request 拦截器(同 §5.5 语义,答案是 AskUserQuestionAnswer)。 */
|
|
3365
4015
|
async #onQuestionRequest(req, next) {
|
|
3366
4016
|
if (!this.#shouldIntercept("question")) return next();
|
|
3367
|
-
|
|
4017
|
+
// v1.3.0:timed 模式带 Host callId;用它作为稳定 askId,双端才能围绕
|
|
4018
|
+
// 同一个问题对象回答/关闭。legacy 没有 wait,仍用随机 id。
|
|
4019
|
+
const callId = typeof req?.wait?.callId === "string" ? req.wait.callId : "";
|
|
4020
|
+
const askId = callId !== "" ? callId : randomUUID();
|
|
3368
4021
|
const sessionId = String(req?.agent?.session?.id ?? "");
|
|
3369
4022
|
const timeoutMs = Number(this.#cfg.approvalTimeoutMs) || 120_000;
|
|
3370
|
-
const
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
}
|
|
3379
|
-
if (questions.length === 0 || questions.some((q) => q.id === "")) return next(); // 形状不对,放行
|
|
4023
|
+
const expiresAt = Date.now() + timeoutMs;
|
|
4024
|
+
const questions = mapWireQuestions(req?.questions);
|
|
4025
|
+
if (questions.length === 0) return next(); // 形状不对,放行
|
|
4026
|
+
const meta = {
|
|
4027
|
+
callId,
|
|
4028
|
+
questions,
|
|
4029
|
+
timeoutMs,
|
|
4030
|
+
multiSelect: questions.some((q) => q.multiSelect),
|
|
4031
|
+
};
|
|
3380
4032
|
const sent = this.#sendEvent(
|
|
3381
4033
|
"question.request",
|
|
3382
4034
|
{
|
|
3383
4035
|
askId,
|
|
3384
4036
|
sessionId,
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
4037
|
+
callId,
|
|
4038
|
+
state: "open",
|
|
4039
|
+
answerable: true,
|
|
4040
|
+
source: "plugin",
|
|
4041
|
+
...meta,
|
|
4042
|
+
expiresAt,
|
|
3388
4043
|
},
|
|
3389
4044
|
true,
|
|
3390
4045
|
);
|
|
@@ -3403,17 +4058,28 @@ export class HostBridge {
|
|
|
3403
4058
|
sessionId,
|
|
3404
4059
|
signal: req?.signal,
|
|
3405
4060
|
timeoutMs,
|
|
4061
|
+
expiresAt,
|
|
4062
|
+
meta,
|
|
3406
4063
|
});
|
|
3407
4064
|
if (outcome.settled === "phone") {
|
|
3408
4065
|
this.#sendEvent("question.resolved", { askId, source: "phone" });
|
|
4066
|
+
this.#hostQuestions.delete(sessionId + "\0" + (callId || askId));
|
|
3409
4067
|
return toAskUserQuestionAnswer(questions, outcome.answers, outcome.freeText);
|
|
3410
4068
|
}
|
|
3411
|
-
|
|
4069
|
+
// legacy:没有 Host 投影,超时后问题真的交给桌面,手机卡片必须结束。
|
|
4070
|
+
// timed:同一请求已有 callId;超时只结束手机独占窗口,随后由投影以
|
|
4071
|
+
// state:'continued' 重新发布,不能提前发 resolved 把卡片删掉。
|
|
4072
|
+
if (callId === "" || !this.#clientFeatures.has("session-state-v1")) {
|
|
4073
|
+
// legacy,或旧 App 不认识 continued:保持旧语义,超时即清卡。
|
|
4074
|
+
this.#sendEvent("question.resolved", { askId, source: "timeout" });
|
|
4075
|
+
}
|
|
3412
4076
|
try {
|
|
3413
4077
|
return await next();
|
|
3414
4078
|
} catch (e) {
|
|
3415
4079
|
this.#log("warn", "next() after question timeout failed", { error: errText(e) });
|
|
3416
4080
|
throw e;
|
|
4081
|
+
} finally {
|
|
4082
|
+
if (callId !== "") void this.#sweepHostQuestions(sessionId);
|
|
3417
4083
|
}
|
|
3418
4084
|
}
|
|
3419
4085
|
|
|
@@ -3422,7 +4088,7 @@ export class HostBridge {
|
|
|
3422
4088
|
* settle 后从 #pending 移除——迟到的手机回答回 applied:false(§5.5)。
|
|
3423
4089
|
* @returns {Promise<{settled:"phone", ...}|{settled:"timeout"}>}
|
|
3424
4090
|
*/
|
|
3425
|
-
#waitForAnswer({ askId, kind, sessionId, signal, timeoutMs }) {
|
|
4091
|
+
#waitForAnswer({ askId, kind, sessionId, signal, timeoutMs, expiresAt, meta }) {
|
|
3426
4092
|
return new Promise((resolve) => {
|
|
3427
4093
|
const entry = {
|
|
3428
4094
|
kind,
|
|
@@ -3431,6 +4097,8 @@ export class HostBridge {
|
|
|
3431
4097
|
timer: null,
|
|
3432
4098
|
signal,
|
|
3433
4099
|
abortHandler: null,
|
|
4100
|
+
expiresAt: Number(expiresAt) || 0,
|
|
4101
|
+
meta: meta && typeof meta === "object" ? meta : {},
|
|
3434
4102
|
};
|
|
3435
4103
|
const finish = (result) => {
|
|
3436
4104
|
if (this.#pending.get(askId) !== entry) return;
|
package/market/index.json
CHANGED
|
@@ -69,6 +69,16 @@
|
|
|
69
69
|
"sortKeys": {}
|
|
70
70
|
},
|
|
71
71
|
"versions": [
|
|
72
|
+
{
|
|
73
|
+
"version": "1.3.2",
|
|
74
|
+
"publishTimestamp": 1794495828,
|
|
75
|
+
"source": {
|
|
76
|
+
"type": "npm"
|
|
77
|
+
},
|
|
78
|
+
"engines": {
|
|
79
|
+
"dsh": "^0.1.5-rc.3 || ^0.2.0-rc.1 || ^0.2.1-rc.0"
|
|
80
|
+
}
|
|
81
|
+
},
|
|
72
82
|
{
|
|
73
83
|
"version": "1.3.1",
|
|
74
84
|
"publishTimestamp": 1794303955,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@duke-dsh-plugins/dsh-remote-control",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.2",
|
|
4
4
|
"description": "Remote-control bridge for DeepSeek Harness: pairs a HarmonyOS phone app with the desktop Host over an end-to-end-encrypted public relay (WebSocket). Lets the phone list/create/switch sessions, send prompts, watch streamed replies, cancel turns, and answer approval and ask-user-question requests from anywhere; pushes Huawei Push Kit notifications (gracefully disabled when AGC is unconfigured) for approvals, questions, and turn completion. The pairing QR lives in the DSH settings panel (\"DSH Pocket\"). Fail-open by design: any relay timeout or fault hands the request back to the desktop answerer.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|