@duke-dsh-plugins/dsh-remote-control 1.3.1 → 1.3.3
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 +738 -45
- package/market/index.json +20 -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;
|
|
@@ -525,11 +588,31 @@ export function mapToolResultEvent(sessionId, event) {
|
|
|
525
588
|
};
|
|
526
589
|
}
|
|
527
590
|
|
|
591
|
+
/**
|
|
592
|
+
* 事件是否属于「对话记录(transcript)」来源(2026-11-13 修复)。
|
|
593
|
+
*
|
|
594
|
+
* 宿主在 `compaction/prune` 时把超长的工具结果改写成 head+tail,并 **append 一条新的
|
|
595
|
+
* `tool/result`**(`surfaceOp:{op:'replace',startSeq,endSeq}` + `sourceEventSeqs:[原 seq]`)。
|
|
596
|
+
* 那条新事件是**模型面(model-only)的替换副本**:只有结果,**没有配对的 `tool/call`**;
|
|
597
|
+
* 桌面端同样只把 `surfaceOp === "append"` 的事件当对话记录(`dsh-client-ui-chat` 的
|
|
598
|
+
* `isAppendSurfaceEvent` 注释原话:replacement copies stay model-only)。
|
|
599
|
+
* `tool/call` 不带 `surfaceOp`(undefined)= append。
|
|
600
|
+
*
|
|
601
|
+
* `mapHistoryMessages` 一直按这条口径过滤(见其 `event.surfaceOp !== "append"`),
|
|
602
|
+
* `mapHistorySteps` 曾经漏掉 → prune 的替换副本被当成一次新调用,`name` 恒为空,
|
|
603
|
+
* 手机端渲染成兜底「工具」,还带着一个错位的 `seq` 与老页里的同名步骤重复。
|
|
604
|
+
*/
|
|
605
|
+
function isTranscriptEvent(event) {
|
|
606
|
+
const op = event?.surfaceOp;
|
|
607
|
+
return op === undefined || op === null || op === "append";
|
|
608
|
+
}
|
|
609
|
+
|
|
528
610
|
/**
|
|
529
611
|
* 契约 §5.7 StepRecord 提取:session.history 的 page.records → steps[]。
|
|
530
612
|
*
|
|
531
613
|
* 规则:
|
|
532
|
-
* - 只看 type==='tool/call' 与 'tool/result'
|
|
614
|
+
* - 只看 type==='tool/call' 与 'tool/result' 的记录;**非 append 的 surface 替换副本跳过**
|
|
615
|
+
* (prune 重写的事件只有结果、没有 call,当成步骤就是一条无名行);
|
|
533
616
|
* - 按 callId 配对:一次调用合并成一条(call 的字段 + result 的状态/文本);
|
|
534
617
|
* - seq:优先取 tool/call 事件的 seq;只有 tool/result 时取它的 seq;
|
|
535
618
|
* - status:tool/result 的 isError 决定 ok/error;只有 call 没有 result 时 running;
|
|
@@ -550,6 +633,8 @@ export function mapHistorySteps(records) {
|
|
|
550
633
|
if (!event || typeof event !== "object") continue;
|
|
551
634
|
const type = event.type;
|
|
552
635
|
if (type !== "tool/call" && type !== "tool/result") continue;
|
|
636
|
+
// 替换副本(compaction/prune 的 surface 重写)不是对话事件:跳过。
|
|
637
|
+
if (type === "tool/result" && !isTranscriptEvent(event)) continue;
|
|
553
638
|
const data = event.data;
|
|
554
639
|
if (!data || typeof data !== "object") continue;
|
|
555
640
|
const ts = eventTime(event);
|
|
@@ -772,6 +857,87 @@ export function mapReasoningEfforts(rows) {
|
|
|
772
857
|
return efforts;
|
|
773
858
|
}
|
|
774
859
|
|
|
860
|
+
/**
|
|
861
|
+
* v1.3.0:Host todo 行 → wire TodoItem[](只保留 content/status,未知项丢弃)。
|
|
862
|
+
* @param {unknown} raw
|
|
863
|
+
* @returns {Array<{content:string,status:"pending"|"in_progress"|"completed"}>}
|
|
864
|
+
*/
|
|
865
|
+
export function mapTodoItems(raw) {
|
|
866
|
+
if (!Array.isArray(raw)) return [];
|
|
867
|
+
const statuses = new Set(["pending", "in_progress", "completed"]);
|
|
868
|
+
const out = [];
|
|
869
|
+
for (const item of raw) {
|
|
870
|
+
if (!item || typeof item !== "object") continue;
|
|
871
|
+
const content = typeof item.content === "string" ? item.content : "";
|
|
872
|
+
const status = typeof item.status === "string" ? item.status : "";
|
|
873
|
+
if (content === "" || !statuses.has(status)) continue;
|
|
874
|
+
out.push({ content, status });
|
|
875
|
+
}
|
|
876
|
+
return out;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
/**
|
|
880
|
+
* v1.3.0:Host GoalProjection/GoalView → 扁平 wire GoalWire。
|
|
881
|
+
* 只接受 goal.id 与合法 phase;activation 由调用方按可用性提供。
|
|
882
|
+
* @param {unknown} goal
|
|
883
|
+
* @param {unknown} roundsStarted
|
|
884
|
+
* @param {unknown} createdAt
|
|
885
|
+
* @param {unknown} updatedAt
|
|
886
|
+
* @param {"armed"|"disarmed"|null} activation
|
|
887
|
+
* @returns {object|null}
|
|
888
|
+
*/
|
|
889
|
+
export function mapGoalWire(goal, roundsStarted, createdAt, updatedAt, activation) {
|
|
890
|
+
if (!goal || typeof goal !== "object" || typeof goal.id !== "string" || goal.id === "") return null;
|
|
891
|
+
const phases = new Set(["active", "paused", "blocked", "complete"]);
|
|
892
|
+
if (!phases.has(goal.phase)) return null;
|
|
893
|
+
const out = {
|
|
894
|
+
id: goal.id,
|
|
895
|
+
revision: Number(goal.revision) || 0,
|
|
896
|
+
objective: typeof goal.objective === "string" ? goal.objective : "",
|
|
897
|
+
phase: goal.phase,
|
|
898
|
+
maxGoalRounds: Number(goal.maxGoalRounds) || 0,
|
|
899
|
+
roundsStarted: Number(roundsStarted) || 0,
|
|
900
|
+
createdAt: Number(createdAt) || 0,
|
|
901
|
+
updatedAt: Number(updatedAt) || 0,
|
|
902
|
+
};
|
|
903
|
+
if (goal.blockedReason && typeof goal.blockedReason === "object") {
|
|
904
|
+
const code = typeof goal.blockedReason.code === "string" ? goal.blockedReason.code : "";
|
|
905
|
+
const message = typeof goal.blockedReason.message === "string" ? goal.blockedReason.message : "";
|
|
906
|
+
if (code !== "" && message !== "") out.blockedReason = { code, message };
|
|
907
|
+
}
|
|
908
|
+
if (activation === "armed" || activation === "disarmed") out.activation = activation;
|
|
909
|
+
return out;
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
/**
|
|
913
|
+
* v1.3.0:Host AskUserQuestionItem[] → wire Question[](不泄露 Host 私有字段)。
|
|
914
|
+
* @param {unknown} raw
|
|
915
|
+
* @returns {Array<object>}
|
|
916
|
+
*/
|
|
917
|
+
export function mapWireQuestions(raw) {
|
|
918
|
+
if (!Array.isArray(raw)) return [];
|
|
919
|
+
const out = [];
|
|
920
|
+
for (const q of raw) {
|
|
921
|
+
const id = typeof q?.id === "string" ? q.id : "";
|
|
922
|
+
if (id === "") continue;
|
|
923
|
+
const options = Array.isArray(q?.options)
|
|
924
|
+
? q.options
|
|
925
|
+
.map((o) => ({
|
|
926
|
+
label: typeof o?.label === "string" ? o.label : "",
|
|
927
|
+
description: typeof o?.description === "string" ? o.description : "",
|
|
928
|
+
}))
|
|
929
|
+
.filter((o) => o.label !== "")
|
|
930
|
+
: [];
|
|
931
|
+
out.push({
|
|
932
|
+
id,
|
|
933
|
+
question: typeof q?.question === "string" ? q.question : "",
|
|
934
|
+
multiSelect: q?.multiSelect === true,
|
|
935
|
+
options,
|
|
936
|
+
});
|
|
937
|
+
}
|
|
938
|
+
return out;
|
|
939
|
+
}
|
|
940
|
+
|
|
775
941
|
/**
|
|
776
942
|
* 契约 §5.8 ModelSelection:手机提交的 model → 提交给宿主的
|
|
777
943
|
* {provider, model, reasoningEffort?}(id 不可反拆,只认 provider/model)。
|
|
@@ -902,7 +1068,15 @@ export class HostBridge {
|
|
|
902
1068
|
#modelSelections = new Map(); // sessionId -> {provider, model, reasoningEffort?}(v1.0.8 内存态)
|
|
903
1069
|
// v1.1.7:有效权限的直播兜底;权威查询仍优先走 permissions 持久投影。
|
|
904
1070
|
#permissionSelections = new Map(); // sessionId -> string(含派生态 custom)
|
|
905
|
-
#pending = new Map(); // askId -> { resolve, kind, sessionId, timer, abortHandler, signal }
|
|
1071
|
+
#pending = new Map(); // askId -> { resolve, kind, sessionId, timer, abortHandler, signal, meta }
|
|
1072
|
+
// v1.3.0:Host userQuestions 投影里仍可回答的问题(continued/open),
|
|
1073
|
+
// key = sessionId + "\0" + callId;用于快照、桌面端回答后清理与手机补答路由。
|
|
1074
|
+
#hostQuestions = new Map();
|
|
1075
|
+
// v1.3.0:goal/activation-changed 的进程内状态缓存(投影不含 activation)。
|
|
1076
|
+
#goalActivations = new Map(); // sessionId -> {id,revision,activation}
|
|
1077
|
+
#askQuestionCalls = new Set(); // v1.3.0:进行中的 ask_user_question callId(用于投影对账)
|
|
1078
|
+
#todoSessions = new Set(); // v1.3.0:已确认有 todo 投影的会话(降级时不能把 turn/start 当能力声明)
|
|
1079
|
+
#clientFeatures = new Set(); // v1.3.0:hello.features(旧 App 不声明新语义)
|
|
906
1080
|
#titles = new Map(); // sessionId -> title(turn.end 推送文案用)
|
|
907
1081
|
#pushToken = ""; // 手机 push.register 上报的 token(内存态)
|
|
908
1082
|
// v1.0.9(§5.10):imageId -> 宿主附件引用(image.fetch 用;插件重启后为空)
|
|
@@ -965,6 +1139,29 @@ export class HostBridge {
|
|
|
965
1139
|
ctx.on("approval/request", (req, next) => this.#onApprovalRequest(req, next), { prepend: true });
|
|
966
1140
|
ctx.on("user-questions/request", (req, next) => this.#onQuestionRequest(req, next), { prepend: true });
|
|
967
1141
|
|
|
1142
|
+
// v1.3.0:Goal 进程内 activation(不持久,只能从 Host 事件拿)。
|
|
1143
|
+
this.#on("goal/activation-changed", (payload) => {
|
|
1144
|
+
const sessionId = String(payload?.sessionId ?? "");
|
|
1145
|
+
if (sessionId === "") return;
|
|
1146
|
+
const goal = payload?.goal;
|
|
1147
|
+
if (!goal || typeof goal.id !== "string") {
|
|
1148
|
+
this.#goalActivations.delete(sessionId);
|
|
1149
|
+
return;
|
|
1150
|
+
}
|
|
1151
|
+
const activation = goal.activation === "armed" ? "armed" : "disarmed";
|
|
1152
|
+
this.#goalActivations.set(sessionId, {
|
|
1153
|
+
id: goal.id,
|
|
1154
|
+
revision: Number(goal.revision) || 0,
|
|
1155
|
+
activation,
|
|
1156
|
+
});
|
|
1157
|
+
this.#sendEvent("session.goalActivationChanged", {
|
|
1158
|
+
sessionId,
|
|
1159
|
+
id: goal.id,
|
|
1160
|
+
revision: Number(goal.revision) || 0,
|
|
1161
|
+
activation,
|
|
1162
|
+
});
|
|
1163
|
+
});
|
|
1164
|
+
|
|
968
1165
|
// --- 事件桥接(§5.2)---
|
|
969
1166
|
this.#on("api-session/added", (summary) => {
|
|
970
1167
|
const s = mapSummary(summary);
|
|
@@ -975,6 +1172,10 @@ export class HostBridge {
|
|
|
975
1172
|
const key = String(sessionId);
|
|
976
1173
|
this.#titles.delete(key);
|
|
977
1174
|
this.#permissionSelections.delete(key);
|
|
1175
|
+
this.#goalActivations.delete(key);
|
|
1176
|
+
for (const mapKey of this.#hostQuestions.keys()) {
|
|
1177
|
+
if (mapKey.startsWith(key + "\0")) this.#hostQuestions.delete(mapKey);
|
|
1178
|
+
}
|
|
978
1179
|
// 会话没了,它的待执行列表与交付物历史条目也没有意义了(避免内存泄漏)
|
|
979
1180
|
this.#queues.delete(key);
|
|
980
1181
|
this.#deliveredEntries.delete(key);
|
|
@@ -1074,14 +1275,67 @@ export class HostBridge {
|
|
|
1074
1275
|
this.#flushStream(sessionId);
|
|
1075
1276
|
this.#sendEvent("tool.call", mapped);
|
|
1076
1277
|
}
|
|
1278
|
+
if (event?.data?.name === "ask_user_question" && typeof event?.data?.callId === "string") {
|
|
1279
|
+
this.#askQuestionCalls.add(event.data.callId);
|
|
1280
|
+
}
|
|
1077
1281
|
return;
|
|
1078
1282
|
}
|
|
1079
1283
|
if (type === "tool/result") {
|
|
1080
|
-
|
|
1284
|
+
// 2026-11-13:surface 替换副本(prune 重写)是模型面事件,桌面端也不把它当
|
|
1285
|
+
// 对话事件 → **不桥接**。否则手机端会多出一条没有名字的步骤(callId 没有
|
|
1286
|
+
// 配对的 tool/call 可查),并把用户已经看到的结果文本换成剪枝版。
|
|
1287
|
+
const mapped = isTranscriptEvent(event) ? mapToolResultEvent(sessionId, event) : null;
|
|
1081
1288
|
if (mapped !== null) {
|
|
1082
1289
|
this.#flushStream(sessionId);
|
|
1083
1290
|
this.#sendEvent("tool.result", mapped);
|
|
1084
1291
|
}
|
|
1292
|
+
const callId = String(event?.data?.message?.toolCallId ?? "");
|
|
1293
|
+
let hasCachedQuestion = false;
|
|
1294
|
+
for (const key of this.#hostQuestions.keys()) {
|
|
1295
|
+
if (key.startsWith(sessionId + "\0")) {
|
|
1296
|
+
hasCachedQuestion = true;
|
|
1297
|
+
break;
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1300
|
+
if ((callId !== "" && this.#askQuestionCalls.has(callId)) || hasCachedQuestion) {
|
|
1301
|
+
if (callId !== "") this.#askQuestionCalls.delete(callId);
|
|
1302
|
+
// Host timed 模式:tool/result 落成 pending 后,投影会从 open 变 continued;
|
|
1303
|
+
// 异步对账,绝不阻塞事件流。
|
|
1304
|
+
void this.#sweepHostQuestions(sessionId);
|
|
1305
|
+
}
|
|
1306
|
+
return;
|
|
1307
|
+
}
|
|
1308
|
+
if (type === "todo/write") {
|
|
1309
|
+
this.#todoSessions.add(sessionId);
|
|
1310
|
+
const todos = event?.data?.todos === null ? null : mapTodoItems(event?.data?.todos);
|
|
1311
|
+
this.#sendEvent("session.todosChanged", { sessionId, todos });
|
|
1312
|
+
return;
|
|
1313
|
+
}
|
|
1314
|
+
if (type === "turn/start") {
|
|
1315
|
+
// Host todo 投影语义:下一回合开始清空上一回合清单。
|
|
1316
|
+
// 只有在已确认该会话有 todo 投影时才发;否则没有 tool-todo 的宿主会因为
|
|
1317
|
+
// 普通 turn/start 被客户端误判为「支持 Todo」(capabilities 缺省必须隐藏)。
|
|
1318
|
+
if (this.#todoSessions.has(sessionId)) {
|
|
1319
|
+
this.#sendEvent("session.todosChanged", { sessionId, todos: null });
|
|
1320
|
+
}
|
|
1321
|
+
return;
|
|
1322
|
+
}
|
|
1323
|
+
if (type === "goal/change") {
|
|
1324
|
+
const data = event?.data;
|
|
1325
|
+
const operation = typeof data?.operation === "string" ? data.operation : "";
|
|
1326
|
+
let goal = null;
|
|
1327
|
+
if (operation !== "clear") {
|
|
1328
|
+
goal = mapGoalWire(data?.goal, data?.roundsStarted, data?.createdAt, data?.updatedAt, null);
|
|
1329
|
+
if (goal !== null) {
|
|
1330
|
+
const activation = this.#goalActivations.get(sessionId);
|
|
1331
|
+
if (activation && activation.id === goal.id && activation.revision === goal.revision) {
|
|
1332
|
+
goal.activation = activation.activation;
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
} else {
|
|
1336
|
+
this.#goalActivations.delete(sessionId);
|
|
1337
|
+
}
|
|
1338
|
+
this.#sendEvent("session.goalChanged", { sessionId, goal, operation: operation || "edit" });
|
|
1085
1339
|
return;
|
|
1086
1340
|
}
|
|
1087
1341
|
if (type === "model/selection") {
|
|
@@ -1122,6 +1376,16 @@ export class HostBridge {
|
|
|
1122
1376
|
if (type === "user/message") {
|
|
1123
1377
|
// v1.1.1(§5.2):真人输入被消费(进入某个 turn)→ 从待执行列表里摘掉。
|
|
1124
1378
|
this.#handleUserMessage(sessionId, event);
|
|
1379
|
+
// v1.3.0:迟到回答/桌面回答会以 user-question-reply 写入,对账投影并清卡。
|
|
1380
|
+
if (event?.data?.source?.kind === "user-question-reply") {
|
|
1381
|
+
void this.#sweepHostQuestions(sessionId);
|
|
1382
|
+
}
|
|
1383
|
+
// v1.3.1:Goal 轮次推进 = goal-round-driver 写入 source.kind="goal" 的
|
|
1384
|
+
// user/message,而 Host **只在 goal/change 发事件**(轮次推进不发)→
|
|
1385
|
+
// 不在这里补一次广播,手机端会一直停在上一个轮次。
|
|
1386
|
+
if (event?.data?.source?.kind === "goal") {
|
|
1387
|
+
void this.#emitGoalRound(sessionId);
|
|
1388
|
+
}
|
|
1125
1389
|
return;
|
|
1126
1390
|
}
|
|
1127
1391
|
// 其余事件(turn/*、step/* …)忽略
|
|
@@ -1201,8 +1465,14 @@ export class HostBridge {
|
|
|
1201
1465
|
}
|
|
1202
1466
|
const inserted = Array.isArray(data?.inserted) ? data.inserted : [];
|
|
1203
1467
|
for (const message of inserted) {
|
|
1468
|
+
// v1.3.1:只跟踪**真人输入**。Goal 轮次提示(source.kind="goal")等宿主自动注入
|
|
1469
|
+
// 的唤醒消息不进手机「排队消息」——它们由触发卡表达(「继续执行目标」),
|
|
1470
|
+
// 真进队列只会把注入的原始 prompt 漏出来,还容易看起来像「残留」。
|
|
1471
|
+
const sourceKind = message?.source?.kind;
|
|
1472
|
+
if (sourceKind !== undefined && sourceKind !== "user") continue;
|
|
1204
1473
|
const itemId = typeof message?.id === "string" ? message.id : "";
|
|
1205
1474
|
if (itemId === "") continue;
|
|
1475
|
+
if (items.some((item) => item.itemId === itemId)) continue; // 同一 Host 消息只能占一项
|
|
1206
1476
|
const text = queueItemText(message?.content);
|
|
1207
1477
|
if (text.trim() === "") continue; // 没有文本 → 不是一条可展示的待执行消息
|
|
1208
1478
|
if (items.length >= QUEUE_MAX_ITEMS) {
|
|
@@ -1228,12 +1498,20 @@ export class HostBridge {
|
|
|
1228
1498
|
*/
|
|
1229
1499
|
#handleUserMessage(sessionId, event) {
|
|
1230
1500
|
const data = event?.data;
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1501
|
+
// v1.3.1:出队按 Host 消息身份(data.id)判定,**不再要求 source.kind==='user'**
|
|
1502
|
+
// ——凡进过队列的消息被消费就必须出队(旧实现只认真人来源,任何被跟踪的
|
|
1503
|
+
// 非 user 来源消息都会永久残留,这正是「排队残留」的一类)。
|
|
1234
1504
|
const items = this.#queues.get(sessionId);
|
|
1235
1505
|
if (!items || items.length === 0) return;
|
|
1236
|
-
|
|
1506
|
+
// v1.3.0:Host 消息 id 是队列项的真实身份;子代理初始注入可以**没有 rpcId**
|
|
1507
|
+
// (source.kind='user' 但无 rpcId)。旧实现只认 rpcId → 消费后永远不出队。
|
|
1508
|
+
const itemId = typeof data?.id === "string" ? data.id : "";
|
|
1509
|
+
const rpcId = typeof data?.source?.rpcId === "string" ? data.source.rpcId : "";
|
|
1510
|
+
let index = itemId === "" ? -1 : items.findIndex((item) => item.itemId === itemId);
|
|
1511
|
+
if (index < 0 && rpcId !== "") {
|
|
1512
|
+
// 旧宿主/旧事件缺 id 时保留 rpcId 回退(只对带 rpcId 的项生效)。
|
|
1513
|
+
index = items.findIndex((item) => item.rpcId !== "" && item.rpcId === rpcId);
|
|
1514
|
+
}
|
|
1237
1515
|
if (index < 0) return;
|
|
1238
1516
|
const target = items[index]?.target ?? "";
|
|
1239
1517
|
items.splice(index, 1);
|
|
@@ -1292,16 +1570,325 @@ export class HostBridge {
|
|
|
1292
1570
|
* ——用户会看到「排队列表空着,但桌面端明明排着一条」。
|
|
1293
1571
|
* 故客户端打开会话时拉一次全量快照(与事件同形状)。
|
|
1294
1572
|
*/
|
|
1295
|
-
#rSessionQueue(m) {
|
|
1573
|
+
async #rSessionQueue(m) {
|
|
1296
1574
|
const p = m.payload ?? {};
|
|
1297
1575
|
const sessionId = typeof p.sessionId === "string" ? p.sessionId : "";
|
|
1298
1576
|
if (sessionId === "") {
|
|
1299
1577
|
this.#reply(m, errorOf(m, "bad-params", "sessionId required"));
|
|
1300
1578
|
return;
|
|
1301
1579
|
}
|
|
1580
|
+
// v1.3.0:优先读 Host 的 inbox 持久投影,事件缓存只作旧宿主回退。
|
|
1581
|
+
// 这样即使漏掉 inbox/spliced 或 user/message,回前台仍能对账出真实队列。
|
|
1582
|
+
try {
|
|
1583
|
+
const sc = this.#sc();
|
|
1584
|
+
if (typeof sc.projections === "function") {
|
|
1585
|
+
const projections = await sc.projections({ sessionId }, this.#signal(10_000));
|
|
1586
|
+
const inbox = projections?.values?.inbox;
|
|
1587
|
+
if (inbox !== undefined && inbox !== null && typeof inbox === "object") {
|
|
1588
|
+
const items = queueItemsFromInbox(inbox);
|
|
1589
|
+
this.#queues.set(sessionId, items.map((item) => ({ ...item })));
|
|
1590
|
+
this.#reply(m, resultOf(m, { sessionId, items: this.#queueItemsOf(sessionId) }));
|
|
1591
|
+
return;
|
|
1592
|
+
}
|
|
1593
|
+
}
|
|
1594
|
+
} catch (e) {
|
|
1595
|
+
this.#log("warn", "inbox projection unavailable; falling back to live queue tracking", {
|
|
1596
|
+
sessionId,
|
|
1597
|
+
error: errText(e),
|
|
1598
|
+
});
|
|
1599
|
+
}
|
|
1302
1600
|
this.#reply(m, resultOf(m, { sessionId, items: this.#queueItemsOf(sessionId) }));
|
|
1303
1601
|
}
|
|
1304
1602
|
|
|
1603
|
+
// ---- v1.3.0:待答交互 / Todo / Goal ------------------------------------
|
|
1604
|
+
|
|
1605
|
+
/** 读一个会话的投影值;失败返回 null(调用方自行降级)。 */
|
|
1606
|
+
async #projectionValues(sessionId) {
|
|
1607
|
+
try {
|
|
1608
|
+
const sc = this.#sc();
|
|
1609
|
+
if (typeof sc.projections !== "function") return null;
|
|
1610
|
+
const projections = await sc.projections({ sessionId }, this.#signal(10_000));
|
|
1611
|
+
const values = projections?.values;
|
|
1612
|
+
return values && typeof values === "object" ? values : null;
|
|
1613
|
+
} catch (e) {
|
|
1614
|
+
this.#log("warn", "projection read failed", { sessionId, error: errText(e) });
|
|
1615
|
+
return null;
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
|
|
1619
|
+
/** 把一个 Host userQuestions active 行映射成 wire PendingQuestion。 */
|
|
1620
|
+
#hostQuestionWire(sessionId, row) {
|
|
1621
|
+
const callId = typeof row?.callId === "string" ? row.callId : "";
|
|
1622
|
+
if (callId === "") return null;
|
|
1623
|
+
const questions = mapWireQuestions(row?.questions);
|
|
1624
|
+
if (questions.length === 0) return null;
|
|
1625
|
+
const continued = row?.state === "continued";
|
|
1626
|
+
return {
|
|
1627
|
+
askId: callId,
|
|
1628
|
+
sessionId,
|
|
1629
|
+
callId,
|
|
1630
|
+
questions,
|
|
1631
|
+
state: continued ? "continued" : "open",
|
|
1632
|
+
answerable: continued,
|
|
1633
|
+
source: "host",
|
|
1634
|
+
multiSelect: questions.some((q) => q.multiSelect),
|
|
1635
|
+
};
|
|
1636
|
+
}
|
|
1637
|
+
|
|
1638
|
+
/**
|
|
1639
|
+
* 对账 Host userQuestions 投影:把 active 行发布为 question.request,并清掉
|
|
1640
|
+
* 已从投影消失的卡片。`tool/result`、user-question-reply 与快照请求都会触发。
|
|
1641
|
+
* 任何异常只记录,绝不影响瀑布。
|
|
1642
|
+
*/
|
|
1643
|
+
async #sweepHostQuestions(sessionId) {
|
|
1644
|
+
const values = await this.#projectionValues(sessionId);
|
|
1645
|
+
const active = values?.userQuestions?.active;
|
|
1646
|
+
if (!Array.isArray(active)) return;
|
|
1647
|
+
// 已经提交但尚在 Host inbox 等待接纳的迟到回答:桌面 UI 也会暂不显示该卡,
|
|
1648
|
+
// 避免用户回答后回前台又看到一张“可再次回答”的重复卡。
|
|
1649
|
+
const queuedReplies = new Set();
|
|
1650
|
+
const inbox = values?.inbox;
|
|
1651
|
+
for (const target of ["next-step", "next-turn"]) {
|
|
1652
|
+
const messages = inbox?.[target];
|
|
1653
|
+
if (!Array.isArray(messages)) continue;
|
|
1654
|
+
for (const message of messages) {
|
|
1655
|
+
const source = message?.source;
|
|
1656
|
+
if (source?.kind === "user-question-reply" && typeof source.callId === "string") {
|
|
1657
|
+
queuedReplies.add(source.callId);
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
const prefix = sessionId + "\0";
|
|
1662
|
+
const nextKeys = new Set();
|
|
1663
|
+
for (const row of active) {
|
|
1664
|
+
const wire = this.#hostQuestionWire(sessionId, row);
|
|
1665
|
+
if (wire === null) continue;
|
|
1666
|
+
if (wire.state === "continued" && queuedReplies.has(wire.callId)) continue;
|
|
1667
|
+
const key = prefix + wire.callId;
|
|
1668
|
+
nextKeys.add(key);
|
|
1669
|
+
this.#hostQuestions.set(key, wire);
|
|
1670
|
+
// 同一 askId 重发 = 状态更新(客户端替换卡片,不叠加)。
|
|
1671
|
+
// 旧 App 会把重发当新卡,只有声明 session-state-v1 的客户端才接收状态更新。
|
|
1672
|
+
if (this.#clientFeatures.has("session-state-v1")) {
|
|
1673
|
+
this.#sendEvent("question.request", wire, wire.state === "continued");
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
for (const [key, old] of this.#hostQuestions) {
|
|
1677
|
+
if (!key.startsWith(prefix) || nextKeys.has(key)) continue;
|
|
1678
|
+
this.#hostQuestions.delete(key);
|
|
1679
|
+
this.#sendEvent("question.resolved", { askId: old.askId, source: "desktop" });
|
|
1680
|
+
}
|
|
1681
|
+
}
|
|
1682
|
+
|
|
1683
|
+
/** pending.interactions:插件实时待答 + Host 投影(指定会话时)合并且按 askId 去重。 */
|
|
1684
|
+
async #rPendingInteractions(m) {
|
|
1685
|
+
const p = m.payload ?? {};
|
|
1686
|
+
const sessionId = typeof p.sessionId === "string" ? p.sessionId : "";
|
|
1687
|
+
// 能说新请求本身就是能力声明(旧 App 不会发);幂等补进 feature 集合,
|
|
1688
|
+
// 避免新 App 忘记在 hello 声明时把遗留 continued 卡误当旧客户端。
|
|
1689
|
+
this.#clientFeatures.add("session-state-v1");
|
|
1690
|
+
const questions = [];
|
|
1691
|
+
const approvals = [];
|
|
1692
|
+
for (const [askId, entry] of this.#pending) {
|
|
1693
|
+
if (sessionId !== "" && entry.sessionId !== sessionId) continue;
|
|
1694
|
+
if (entry.kind === "approval") {
|
|
1695
|
+
approvals.push({
|
|
1696
|
+
...entry.meta,
|
|
1697
|
+
askId,
|
|
1698
|
+
sessionId: entry.sessionId,
|
|
1699
|
+
answerable: true,
|
|
1700
|
+
source: "plugin",
|
|
1701
|
+
...(entry.expiresAt ? { expiresAt: entry.expiresAt } : {}),
|
|
1702
|
+
});
|
|
1703
|
+
} else {
|
|
1704
|
+
questions.push({
|
|
1705
|
+
...entry.meta,
|
|
1706
|
+
askId,
|
|
1707
|
+
sessionId: entry.sessionId,
|
|
1708
|
+
state: "open",
|
|
1709
|
+
answerable: true,
|
|
1710
|
+
source: "plugin",
|
|
1711
|
+
...(entry.expiresAt ? { expiresAt: entry.expiresAt } : {}),
|
|
1712
|
+
});
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
if (sessionId !== "") {
|
|
1716
|
+
await this.#sweepHostQuestions(sessionId);
|
|
1717
|
+
for (const wire of this.#hostQuestions.values()) {
|
|
1718
|
+
if (wire.sessionId !== sessionId) continue;
|
|
1719
|
+
if (questions.some((q) => q.askId === wire.askId)) continue;
|
|
1720
|
+
questions.push(wire);
|
|
1721
|
+
}
|
|
1722
|
+
}
|
|
1723
|
+
this.#reply(m, resultOf(m, { questions, approvals }));
|
|
1724
|
+
}
|
|
1725
|
+
|
|
1726
|
+
/** session.todos:Host `todos` 持久投影(只读)。 */
|
|
1727
|
+
async #rSessionTodos(m) {
|
|
1728
|
+
const p = m.payload ?? {};
|
|
1729
|
+
const sessionId = typeof p.sessionId === "string" ? p.sessionId : "";
|
|
1730
|
+
if (sessionId === "") {
|
|
1731
|
+
this.#reply(m, errorOf(m, "bad-params", "sessionId required"));
|
|
1732
|
+
return;
|
|
1733
|
+
}
|
|
1734
|
+
const values = await this.#projectionValues(sessionId);
|
|
1735
|
+
const supported = values !== null && Object.prototype.hasOwnProperty.call(values, "todos");
|
|
1736
|
+
const todos = !supported || values.todos === null ? null : mapTodoItems(values.todos);
|
|
1737
|
+
if (supported) this.#todoSessions.add(sessionId);
|
|
1738
|
+
else this.#todoSessions.delete(sessionId);
|
|
1739
|
+
this.#reply(m, resultOf(m, { sessionId, todos, supported }));
|
|
1740
|
+
}
|
|
1741
|
+
|
|
1742
|
+
/** Goal 人类操作集合(只暴露 Host GoalService 真正存在的方法)。 */
|
|
1743
|
+
#goalActions(goals) {
|
|
1744
|
+
if (!goals) return [];
|
|
1745
|
+
const wanted = ["create", "edit", "pause", "resume", "complete", "clear"];
|
|
1746
|
+
return wanted.filter((name) => typeof goals[name] === "function");
|
|
1747
|
+
}
|
|
1748
|
+
|
|
1749
|
+
/** 读会话当前 goal + activation(不激活冷会话)。 */
|
|
1750
|
+
async #readGoal(sessionId) {
|
|
1751
|
+
const values = await this.#projectionValues(sessionId);
|
|
1752
|
+
const supported = values !== null && Object.prototype.hasOwnProperty.call(values, "goal");
|
|
1753
|
+
const raw = supported ? values.goal : null;
|
|
1754
|
+
let activation = null;
|
|
1755
|
+
let goal = null;
|
|
1756
|
+
if (raw && typeof raw === "object" && raw.goal && typeof raw.goal.id === "string") {
|
|
1757
|
+
const cached = this.#goalActivations.get(sessionId);
|
|
1758
|
+
if (cached && cached.id === raw.goal.id && cached.revision === raw.goal.revision) {
|
|
1759
|
+
activation = cached.activation;
|
|
1760
|
+
}
|
|
1761
|
+
// 有活 Agent 时从 Host 服务读准确的 activation;绝不为了查询激活冷会话。
|
|
1762
|
+
try {
|
|
1763
|
+
const agents = this.#service("agents");
|
|
1764
|
+
const live = agents && typeof agents.get === "function" ? agents.get(sessionId) : null;
|
|
1765
|
+
const goals = this.#service("goals");
|
|
1766
|
+
if (live && goals && typeof goals.get === "function") {
|
|
1767
|
+
const view = goals.get(live);
|
|
1768
|
+
if (view && view.id === raw.goal.id) activation = view.activation === "armed" ? "armed" : "disarmed";
|
|
1769
|
+
}
|
|
1770
|
+
} catch (e) {
|
|
1771
|
+
this.#log("warn", "goal activation read failed", { sessionId, error: errText(e) });
|
|
1772
|
+
}
|
|
1773
|
+
goal = mapGoalWire(raw.goal, raw.roundsStarted, raw.createdAt, raw.updatedAt, activation);
|
|
1774
|
+
}
|
|
1775
|
+
return { supported, goal, activation };
|
|
1776
|
+
}
|
|
1777
|
+
|
|
1778
|
+
/** v1.3.1:轮次推进(Host 不发 goal/change)→ 重读投影并广播 operation="round"。 */
|
|
1779
|
+
async #emitGoalRound(sessionId) {
|
|
1780
|
+
try {
|
|
1781
|
+
const state = await this.#readGoal(sessionId);
|
|
1782
|
+
if (state.goal === null) return;
|
|
1783
|
+
const goal = state.goal;
|
|
1784
|
+
const cached = this.#goalActivations.get(sessionId);
|
|
1785
|
+
if (cached && cached.id === goal.id && cached.revision === goal.revision) {
|
|
1786
|
+
goal.activation = cached.activation;
|
|
1787
|
+
}
|
|
1788
|
+
this.#sendEvent("session.goalChanged", { sessionId, goal, operation: "round" });
|
|
1789
|
+
} catch (e) {
|
|
1790
|
+
this.#log("warn", "goal round broadcast failed", { sessionId, error: errText(e) });
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
/** session.goal:读持久投影 + 进程内 activation。 */
|
|
1795
|
+
async #rSessionGoal(m) {
|
|
1796
|
+
const p = m.payload ?? {};
|
|
1797
|
+
const sessionId = typeof p.sessionId === "string" ? p.sessionId : "";
|
|
1798
|
+
if (sessionId === "") {
|
|
1799
|
+
this.#reply(m, errorOf(m, "bad-params", "sessionId required"));
|
|
1800
|
+
return;
|
|
1801
|
+
}
|
|
1802
|
+
const state = await this.#readGoal(sessionId);
|
|
1803
|
+
const goals = this.#service("goals");
|
|
1804
|
+
this.#reply(
|
|
1805
|
+
m,
|
|
1806
|
+
resultOf(m, {
|
|
1807
|
+
sessionId,
|
|
1808
|
+
goal: state.goal,
|
|
1809
|
+
activation: state.activation,
|
|
1810
|
+
supported: state.supported || goals !== null,
|
|
1811
|
+
actions: this.#goalActions(goals),
|
|
1812
|
+
}),
|
|
1813
|
+
);
|
|
1814
|
+
}
|
|
1815
|
+
|
|
1816
|
+
/** session.goalAction:手机人类操作,走 Host GoalService + CAS ref。 */
|
|
1817
|
+
async #rSessionGoalAction(m) {
|
|
1818
|
+
const p = m.payload ?? {};
|
|
1819
|
+
const sessionId = typeof p.sessionId === "string" ? p.sessionId : "";
|
|
1820
|
+
const action = typeof p.action === "string" ? p.action : "";
|
|
1821
|
+
const allowed = ["create", "edit", "pause", "resume", "complete", "clear"];
|
|
1822
|
+
if (sessionId === "" || !allowed.includes(action)) {
|
|
1823
|
+
this.#reply(m, errorOf(m, "bad-params", "sessionId and a valid action required"));
|
|
1824
|
+
return;
|
|
1825
|
+
}
|
|
1826
|
+
const goals = this.#service("goals");
|
|
1827
|
+
if (!goals || typeof goals[action] !== "function") {
|
|
1828
|
+
this.#reply(m, errorOf(m, "service-unavailable", "goals service unavailable"));
|
|
1829
|
+
return;
|
|
1830
|
+
}
|
|
1831
|
+
let agent = null;
|
|
1832
|
+
try {
|
|
1833
|
+
const sc = this.#sc();
|
|
1834
|
+
if (typeof sc.resolveAgent !== "function") throw new Error("sessionController.resolveAgent unavailable");
|
|
1835
|
+
const resolved = await sc.resolveAgent(sessionId);
|
|
1836
|
+
if (resolved?.error) throw resolved.error;
|
|
1837
|
+
agent = resolved?.agent ?? resolved?.session?.agent ?? null;
|
|
1838
|
+
if (!agent) throw new Error(`session "${sessionId}" has no live agent`);
|
|
1839
|
+
} catch (e) {
|
|
1840
|
+
const err = hostErrorOf(e, "goal-agent-unavailable");
|
|
1841
|
+
this.#reply(m, errorOf(m, err.code, err.message));
|
|
1842
|
+
return;
|
|
1843
|
+
}
|
|
1844
|
+
let expected = null;
|
|
1845
|
+
if (action !== "create") {
|
|
1846
|
+
const raw = p.expected;
|
|
1847
|
+
if (!raw || typeof raw.id !== "string" || !Number.isInteger(raw.revision) || raw.revision < 1) {
|
|
1848
|
+
this.#reply(m, errorOf(m, "bad-params", "expected {id, revision} required"));
|
|
1849
|
+
return;
|
|
1850
|
+
}
|
|
1851
|
+
expected = { id: raw.id, revision: raw.revision };
|
|
1852
|
+
}
|
|
1853
|
+
const objective = typeof p.objective === "string" ? p.objective.trim() : "";
|
|
1854
|
+
const maxGoalRounds = p.maxGoalRounds;
|
|
1855
|
+
if ((action === "create" || action === "edit") && objective === "" && action === "create") {
|
|
1856
|
+
this.#reply(m, errorOf(m, "bad-params", "objective required for create"));
|
|
1857
|
+
return;
|
|
1858
|
+
}
|
|
1859
|
+
if (action === "edit" && objective === "" && maxGoalRounds === undefined) {
|
|
1860
|
+
this.#reply(m, errorOf(m, "bad-params", "edit requires objective or maxGoalRounds"));
|
|
1861
|
+
return;
|
|
1862
|
+
}
|
|
1863
|
+
if (maxGoalRounds !== undefined && (!Number.isInteger(maxGoalRounds) || maxGoalRounds < 1)) {
|
|
1864
|
+
this.#reply(m, errorOf(m, "bad-params", "maxGoalRounds must be a positive integer"));
|
|
1865
|
+
return;
|
|
1866
|
+
}
|
|
1867
|
+
try {
|
|
1868
|
+
if (action === "create") {
|
|
1869
|
+
await goals.create(agent, {
|
|
1870
|
+
objective,
|
|
1871
|
+
...(maxGoalRounds === undefined ? {} : { maxGoalRounds }),
|
|
1872
|
+
});
|
|
1873
|
+
} else if (action === "edit") {
|
|
1874
|
+
await goals.edit(agent, expected, {
|
|
1875
|
+
...(objective === "" ? {} : { objective }),
|
|
1876
|
+
...(maxGoalRounds === undefined ? {} : { maxGoalRounds }),
|
|
1877
|
+
});
|
|
1878
|
+
} else if (action === "clear") {
|
|
1879
|
+
await goals.clear(agent, expected);
|
|
1880
|
+
} else {
|
|
1881
|
+
await goals[action](agent, expected);
|
|
1882
|
+
}
|
|
1883
|
+
const state = await this.#readGoal(sessionId);
|
|
1884
|
+
this.#reply(m, resultOf(m, { sessionId, goal: state.goal }));
|
|
1885
|
+
} catch (e) {
|
|
1886
|
+
const err = hostErrorOf(e, "goal-action-failed");
|
|
1887
|
+
this.#log("warn", "goal action failed", { sessionId, action, code: err.code });
|
|
1888
|
+
this.#reply(m, errorOf(m, err.code, err.message));
|
|
1889
|
+
}
|
|
1890
|
+
}
|
|
1891
|
+
|
|
1305
1892
|
/** 归档集合(宿主没有 Workspace 注册表 → null,表示「不知道」而不是「空」)。 */
|
|
1306
1893
|
#archivedSessionIds() {
|
|
1307
1894
|
const reg = this.#workspaceRegistry();
|
|
@@ -1390,6 +1977,11 @@ export class HostBridge {
|
|
|
1390
1977
|
entry.resolve({ settled: "timeout" });
|
|
1391
1978
|
}
|
|
1392
1979
|
this.#pending.clear();
|
|
1980
|
+
this.#hostQuestions.clear();
|
|
1981
|
+
this.#goalActivations.clear();
|
|
1982
|
+
this.#askQuestionCalls.clear();
|
|
1983
|
+
this.#todoSessions.clear();
|
|
1984
|
+
this.#clientFeatures.clear();
|
|
1393
1985
|
for (const off of this.#disposers) {
|
|
1394
1986
|
try {
|
|
1395
1987
|
off();
|
|
@@ -1896,7 +2488,15 @@ export class HostBridge {
|
|
|
1896
2488
|
case "session.permission":
|
|
1897
2489
|
return await this.#rSessionPermission(m);
|
|
1898
2490
|
case "session.queue":
|
|
1899
|
-
return this.#rSessionQueue(m);
|
|
2491
|
+
return await this.#rSessionQueue(m);
|
|
2492
|
+
case "pending.interactions":
|
|
2493
|
+
return await this.#rPendingInteractions(m);
|
|
2494
|
+
case "session.todos":
|
|
2495
|
+
return await this.#rSessionTodos(m);
|
|
2496
|
+
case "session.goal":
|
|
2497
|
+
return await this.#rSessionGoal(m);
|
|
2498
|
+
case "session.goalAction":
|
|
2499
|
+
return await this.#rSessionGoalAction(m);
|
|
1900
2500
|
case "session.archive":
|
|
1901
2501
|
return await this.#rSessionArchive(m);
|
|
1902
2502
|
case "session.unarchive":
|
|
@@ -1921,7 +2521,7 @@ export class HostBridge {
|
|
|
1921
2521
|
case "approval.answer":
|
|
1922
2522
|
return this.#rApprovalAnswer(m);
|
|
1923
2523
|
case "question.answer":
|
|
1924
|
-
return this.#rQuestionAnswer(m);
|
|
2524
|
+
return await this.#rQuestionAnswer(m);
|
|
1925
2525
|
case "push.register":
|
|
1926
2526
|
return this.#rPushRegister(m);
|
|
1927
2527
|
default:
|
|
@@ -1991,6 +2591,11 @@ export class HostBridge {
|
|
|
1991
2591
|
#rHello(m) {
|
|
1992
2592
|
const p = m.payload ?? {};
|
|
1993
2593
|
if (typeof p.pushToken === "string" && p.pushToken !== "") this.#pushToken = p.pushToken;
|
|
2594
|
+
this.#clientFeatures = new Set(
|
|
2595
|
+
Array.isArray(p.features)
|
|
2596
|
+
? p.features.filter((f) => typeof f === "string" && f !== "").slice(0, 16)
|
|
2597
|
+
: [],
|
|
2598
|
+
);
|
|
1994
2599
|
this.#reply(
|
|
1995
2600
|
m,
|
|
1996
2601
|
resultOf(m, {
|
|
@@ -2535,6 +3140,21 @@ export class HostBridge {
|
|
|
2535
3140
|
// v1.2.0(§5.11.1):宿主具备附件服务(attachments + fileUploads)才声明附件能力;
|
|
2536
3141
|
// 否则**省略该字段**,客户端隐藏附件入口(旧插件也没有该字段)。
|
|
2537
3142
|
if (this.#attachmentHostReady()) capabilities.attachments = attachmentCapabilities();
|
|
3143
|
+
// v1.3.0:新协议能力(字段缺省 = 旧 Host 不支持)。
|
|
3144
|
+
const goals = this.#service("goals");
|
|
3145
|
+
capabilities.todos = { supported: true }; // 具体会话是否有 todo 投影由 session.todos.supported 判定
|
|
3146
|
+
capabilities.goal = { read: true, actions: this.#goalActions(goals) };
|
|
3147
|
+
let hostContinued = false;
|
|
3148
|
+
try {
|
|
3149
|
+
hostContinued = typeof this.#sc().projections === "function";
|
|
3150
|
+
} catch {
|
|
3151
|
+
hostContinued = false;
|
|
3152
|
+
}
|
|
3153
|
+
capabilities.pendingInteractions = {
|
|
3154
|
+
questions: true,
|
|
3155
|
+
approvals: true,
|
|
3156
|
+
hostContinued,
|
|
3157
|
+
};
|
|
2538
3158
|
this.#reply(m, resultOf(m, capabilities));
|
|
2539
3159
|
}
|
|
2540
3160
|
|
|
@@ -3251,21 +3871,74 @@ export class HostBridge {
|
|
|
3251
3871
|
this.#reply(m, resultOf(m, { applied: true }));
|
|
3252
3872
|
}
|
|
3253
3873
|
|
|
3254
|
-
#rQuestionAnswer(m) {
|
|
3874
|
+
async #rQuestionAnswer(m) {
|
|
3255
3875
|
const p = m.payload ?? {};
|
|
3256
3876
|
const askId = typeof p.askId === "string" ? p.askId : "";
|
|
3877
|
+
const answers = p.answers;
|
|
3878
|
+
if (answers === null || typeof answers !== "object" || Array.isArray(answers)) {
|
|
3879
|
+
this.#reply(m, errorOf(m, "bad-answers", "answers must be an object of questionId -> label(s)"));
|
|
3880
|
+
return;
|
|
3881
|
+
}
|
|
3882
|
+
const freeText = typeof p.freeText === "string" ? p.freeText : "";
|
|
3257
3883
|
const entry = askId === "" ? undefined : this.#pending.get(askId);
|
|
3258
|
-
if (entry
|
|
3884
|
+
if (entry !== undefined && entry.kind === "question") {
|
|
3885
|
+
this.#settle(entry, "phone", { answers, freeText });
|
|
3886
|
+
this.#reply(m, resultOf(m, { applied: true }));
|
|
3887
|
+
return;
|
|
3888
|
+
}
|
|
3889
|
+
|
|
3890
|
+
// v1.3.0:超过插件独占窗口后,Host timed 模式仍将问题保持在 continued;
|
|
3891
|
+
// 此时必须走 Host 官方 answer(steer 一条 user-question-reply),禁止伪造消息。
|
|
3892
|
+
let hostQuestion = null;
|
|
3893
|
+
for (const wire of this.#hostQuestions.values()) {
|
|
3894
|
+
if (wire.askId === askId || wire.callId === askId) {
|
|
3895
|
+
hostQuestion = wire;
|
|
3896
|
+
break;
|
|
3897
|
+
}
|
|
3898
|
+
}
|
|
3899
|
+
if (hostQuestion === null || hostQuestion.state !== "continued") {
|
|
3259
3900
|
this.#reply(m, resultOf(m, { applied: false }));
|
|
3260
3901
|
return;
|
|
3261
3902
|
}
|
|
3262
|
-
const
|
|
3263
|
-
|
|
3264
|
-
|
|
3903
|
+
const questions = hostQuestion.questions;
|
|
3904
|
+
const userQuestions = this.#service("userQuestions");
|
|
3905
|
+
if (!userQuestions || typeof userQuestions.answer !== "function") {
|
|
3906
|
+
this.#reply(m, errorOf(m, "service-unavailable", "userQuestions.answer unavailable"));
|
|
3265
3907
|
return;
|
|
3266
3908
|
}
|
|
3267
|
-
|
|
3268
|
-
|
|
3909
|
+
let agent = null;
|
|
3910
|
+
try {
|
|
3911
|
+
const sc = this.#sc();
|
|
3912
|
+
const resolved = await sc.resolveAgent(hostQuestion.sessionId);
|
|
3913
|
+
if (resolved?.error) throw resolved.error;
|
|
3914
|
+
agent = resolved?.agent ?? resolved?.session?.agent ?? null;
|
|
3915
|
+
if (!agent) throw new Error(`session "${hostQuestion.sessionId}" has no live agent`);
|
|
3916
|
+
} catch (e) {
|
|
3917
|
+
const err = hostErrorOf(e, "question-agent-unavailable");
|
|
3918
|
+
this.#reply(m, errorOf(m, err.code, err.message));
|
|
3919
|
+
return;
|
|
3920
|
+
}
|
|
3921
|
+
try {
|
|
3922
|
+
const answer = toAskUserQuestionAnswer(questions, answers, freeText);
|
|
3923
|
+
const accepted =
|
|
3924
|
+
questions.every((q) => answer.answers.some((a) => a.id === q.id)) &&
|
|
3925
|
+
userQuestions.answer(agent, hostQuestion.callId, answer) === true;
|
|
3926
|
+
if (!accepted) {
|
|
3927
|
+
this.#reply(m, resultOf(m, { applied: false }));
|
|
3928
|
+
return;
|
|
3929
|
+
}
|
|
3930
|
+
this.#hostQuestions.delete(hostQuestion.sessionId + "\0" + hostQuestion.callId);
|
|
3931
|
+
this.#sendEvent("question.resolved", { askId: hostQuestion.askId, source: "phone" });
|
|
3932
|
+
this.#reply(m, resultOf(m, { applied: true }));
|
|
3933
|
+
} catch (e) {
|
|
3934
|
+
const err = hostErrorOf(e, "question-answer-failed");
|
|
3935
|
+
this.#log("warn", "continued question answer failed", {
|
|
3936
|
+
sessionId: hostQuestion.sessionId,
|
|
3937
|
+
code: err.code,
|
|
3938
|
+
});
|
|
3939
|
+
// Host 错误码原样给客户端,便于展示 REPLY_QUEUED / BAD_ANSWER。
|
|
3940
|
+
this.#reply(m, errorOf(m, err.code, err.message));
|
|
3941
|
+
}
|
|
3269
3942
|
}
|
|
3270
3943
|
|
|
3271
3944
|
#rPushRegister(m) {
|
|
@@ -3310,16 +3983,16 @@ export class HostBridge {
|
|
|
3310
3983
|
const askId = randomUUID();
|
|
3311
3984
|
const sessionId = String(req?.agent?.session?.id ?? "");
|
|
3312
3985
|
const timeoutMs = Number(this.#cfg.approvalTimeoutMs) || 120_000;
|
|
3986
|
+
const expiresAt = Date.now() + timeoutMs;
|
|
3987
|
+
const meta = {
|
|
3988
|
+
toolName: String(req?.toolName ?? "unknown"),
|
|
3989
|
+
reason: typeof req?.reason === "string" ? req.reason : "",
|
|
3990
|
+
argsPreview: this.#argsPreviewOf(req?.agent, req?.callId),
|
|
3991
|
+
timeoutMs,
|
|
3992
|
+
};
|
|
3313
3993
|
const sent = this.#sendEvent(
|
|
3314
3994
|
"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
|
-
},
|
|
3995
|
+
{ askId, sessionId, ...meta, expiresAt },
|
|
3323
3996
|
true, // urgent:离线时优先补发(契约 §4)
|
|
3324
3997
|
);
|
|
3325
3998
|
if (!sent) {
|
|
@@ -3337,6 +4010,8 @@ export class HostBridge {
|
|
|
3337
4010
|
sessionId,
|
|
3338
4011
|
signal: req?.signal,
|
|
3339
4012
|
timeoutMs,
|
|
4013
|
+
expiresAt,
|
|
4014
|
+
meta,
|
|
3340
4015
|
});
|
|
3341
4016
|
if (outcome.settled === "phone") {
|
|
3342
4017
|
const decision = outcome.decision === "approved" ? "approved" : "rejected";
|
|
@@ -3364,27 +4039,32 @@ export class HostBridge {
|
|
|
3364
4039
|
/** user-questions/request 拦截器(同 §5.5 语义,答案是 AskUserQuestionAnswer)。 */
|
|
3365
4040
|
async #onQuestionRequest(req, next) {
|
|
3366
4041
|
if (!this.#shouldIntercept("question")) return next();
|
|
3367
|
-
|
|
4042
|
+
// v1.3.0:timed 模式带 Host callId;用它作为稳定 askId,双端才能围绕
|
|
4043
|
+
// 同一个问题对象回答/关闭。legacy 没有 wait,仍用随机 id。
|
|
4044
|
+
const callId = typeof req?.wait?.callId === "string" ? req.wait.callId : "";
|
|
4045
|
+
const askId = callId !== "" ? callId : randomUUID();
|
|
3368
4046
|
const sessionId = String(req?.agent?.session?.id ?? "");
|
|
3369
4047
|
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(); // 形状不对,放行
|
|
4048
|
+
const expiresAt = Date.now() + timeoutMs;
|
|
4049
|
+
const questions = mapWireQuestions(req?.questions);
|
|
4050
|
+
if (questions.length === 0) return next(); // 形状不对,放行
|
|
4051
|
+
const meta = {
|
|
4052
|
+
callId,
|
|
4053
|
+
questions,
|
|
4054
|
+
timeoutMs,
|
|
4055
|
+
multiSelect: questions.some((q) => q.multiSelect),
|
|
4056
|
+
};
|
|
3380
4057
|
const sent = this.#sendEvent(
|
|
3381
4058
|
"question.request",
|
|
3382
4059
|
{
|
|
3383
4060
|
askId,
|
|
3384
4061
|
sessionId,
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
4062
|
+
callId,
|
|
4063
|
+
state: "open",
|
|
4064
|
+
answerable: true,
|
|
4065
|
+
source: "plugin",
|
|
4066
|
+
...meta,
|
|
4067
|
+
expiresAt,
|
|
3388
4068
|
},
|
|
3389
4069
|
true,
|
|
3390
4070
|
);
|
|
@@ -3403,17 +4083,28 @@ export class HostBridge {
|
|
|
3403
4083
|
sessionId,
|
|
3404
4084
|
signal: req?.signal,
|
|
3405
4085
|
timeoutMs,
|
|
4086
|
+
expiresAt,
|
|
4087
|
+
meta,
|
|
3406
4088
|
});
|
|
3407
4089
|
if (outcome.settled === "phone") {
|
|
3408
4090
|
this.#sendEvent("question.resolved", { askId, source: "phone" });
|
|
4091
|
+
this.#hostQuestions.delete(sessionId + "\0" + (callId || askId));
|
|
3409
4092
|
return toAskUserQuestionAnswer(questions, outcome.answers, outcome.freeText);
|
|
3410
4093
|
}
|
|
3411
|
-
|
|
4094
|
+
// legacy:没有 Host 投影,超时后问题真的交给桌面,手机卡片必须结束。
|
|
4095
|
+
// timed:同一请求已有 callId;超时只结束手机独占窗口,随后由投影以
|
|
4096
|
+
// state:'continued' 重新发布,不能提前发 resolved 把卡片删掉。
|
|
4097
|
+
if (callId === "" || !this.#clientFeatures.has("session-state-v1")) {
|
|
4098
|
+
// legacy,或旧 App 不认识 continued:保持旧语义,超时即清卡。
|
|
4099
|
+
this.#sendEvent("question.resolved", { askId, source: "timeout" });
|
|
4100
|
+
}
|
|
3412
4101
|
try {
|
|
3413
4102
|
return await next();
|
|
3414
4103
|
} catch (e) {
|
|
3415
4104
|
this.#log("warn", "next() after question timeout failed", { error: errText(e) });
|
|
3416
4105
|
throw e;
|
|
4106
|
+
} finally {
|
|
4107
|
+
if (callId !== "") void this.#sweepHostQuestions(sessionId);
|
|
3417
4108
|
}
|
|
3418
4109
|
}
|
|
3419
4110
|
|
|
@@ -3422,7 +4113,7 @@ export class HostBridge {
|
|
|
3422
4113
|
* settle 后从 #pending 移除——迟到的手机回答回 applied:false(§5.5)。
|
|
3423
4114
|
* @returns {Promise<{settled:"phone", ...}|{settled:"timeout"}>}
|
|
3424
4115
|
*/
|
|
3425
|
-
#waitForAnswer({ askId, kind, sessionId, signal, timeoutMs }) {
|
|
4116
|
+
#waitForAnswer({ askId, kind, sessionId, signal, timeoutMs, expiresAt, meta }) {
|
|
3426
4117
|
return new Promise((resolve) => {
|
|
3427
4118
|
const entry = {
|
|
3428
4119
|
kind,
|
|
@@ -3431,6 +4122,8 @@ export class HostBridge {
|
|
|
3431
4122
|
timer: null,
|
|
3432
4123
|
signal,
|
|
3433
4124
|
abortHandler: null,
|
|
4125
|
+
expiresAt: Number(expiresAt) || 0,
|
|
4126
|
+
meta: meta && typeof meta === "object" ? meta : {},
|
|
3434
4127
|
};
|
|
3435
4128
|
const finish = (result) => {
|
|
3436
4129
|
if (this.#pending.get(askId) !== entry) return;
|
package/market/index.json
CHANGED
|
@@ -69,6 +69,26 @@
|
|
|
69
69
|
"sortKeys": {}
|
|
70
70
|
},
|
|
71
71
|
"versions": [
|
|
72
|
+
{
|
|
73
|
+
"version": "1.3.3",
|
|
74
|
+
"publishTimestamp": 1794529053,
|
|
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
|
+
},
|
|
82
|
+
{
|
|
83
|
+
"version": "1.3.2",
|
|
84
|
+
"publishTimestamp": 1794495828,
|
|
85
|
+
"source": {
|
|
86
|
+
"type": "npm"
|
|
87
|
+
},
|
|
88
|
+
"engines": {
|
|
89
|
+
"dsh": "^0.1.5-rc.3 || ^0.2.0-rc.1 || ^0.2.1-rc.0"
|
|
90
|
+
}
|
|
91
|
+
},
|
|
72
92
|
{
|
|
73
93
|
"version": "1.3.1",
|
|
74
94
|
"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.3",
|
|
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",
|