dsh-mobile-plugin 0.5.45 → 0.5.46
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/lib/mirror/pusher.cjs
CHANGED
|
@@ -83,6 +83,21 @@ const AUDIENCE_IDLE_MS = 30_000;
|
|
|
83
83
|
* `MIRROR_SNAPSHOT_TTL_MS`(15s),否则 sidecar 的镜像快照会被判过期。
|
|
84
84
|
*/
|
|
85
85
|
const BG_COLD_ROTATE_MIN_INTERVAL_MS = 5000;
|
|
86
|
+
/**
|
|
87
|
+
* 清单重建的兜底窗:没有事件说「变了」时,最长隔这么久重新问一次宿主。
|
|
88
|
+
*
|
|
89
|
+
* 会话清单的构建是**全目录扫描**(列目录 + 每个会话开文件解压读头部):1800 条会话、
|
|
90
|
+
* 60 个项目目录的机器上单次一百多毫秒。旧实现不管变没变,每 500ms 都整份重建一遍 ——
|
|
91
|
+
* 「内容没变就跳过」那道判重只能省掉推送,省不掉扫描本身。
|
|
92
|
+
*
|
|
93
|
+
* 而变没变,宿主的会话事件流本来就在说(turn/start、turn/end、session/title、
|
|
94
|
+
* session/disposed、以及任何 host/* 帧)。所以现在只有三类时刻才真去重建:
|
|
95
|
+
* ① 从没构建过;② 有事件帧报告过变化(`listDirty`);③ 距上次构建超过这个窗。
|
|
96
|
+
*
|
|
97
|
+
* 取 5s 与冷会话轮转同一个窗口:事件覆盖不到的改动(例如只落了盘、没走会话事件的那类)
|
|
98
|
+
* 最多旧 5 秒,仍远小于 sidecar 的镜像快照 TTL(15s),也不会让镜像被判过期。
|
|
99
|
+
*/
|
|
100
|
+
const BG_LIST_REBUILD_FALLBACK_MS = 5000;
|
|
86
101
|
/**
|
|
87
102
|
* 全量补推(隧道连上那一次、主机级事件帧)之间的最小间隔。
|
|
88
103
|
*
|
|
@@ -787,6 +802,8 @@ function createMirrorPusher(deps) {
|
|
|
787
802
|
return;
|
|
788
803
|
}
|
|
789
804
|
retainSessionSignals(sessionsBody);
|
|
805
|
+
// 这条路径刚刚真的扫过一遍目录:把结果记进缓存,后台 tick 接下来就不必再扫。
|
|
806
|
+
rememberSessionsBody(sessionsBody);
|
|
790
807
|
await pushMirrorPayload({
|
|
791
808
|
kind: 'sync', at: Date.now(),
|
|
792
809
|
activeSessionId: sessionId,
|
|
@@ -873,6 +890,7 @@ function createMirrorPusher(deps) {
|
|
|
873
890
|
return
|
|
874
891
|
}
|
|
875
892
|
retainSessionSignals(sessionsBody);
|
|
893
|
+
rememberSessionsBody(sessionsBody);
|
|
876
894
|
const list = sessionsBody.body.items || [];
|
|
877
895
|
const highPrio = sessionIds
|
|
878
896
|
? list.filter(s => sessionIds.includes(String(s.sessionId)))
|
|
@@ -917,6 +935,41 @@ function createMirrorPusher(deps) {
|
|
|
917
935
|
let lastColdRotateAt = 0;
|
|
918
936
|
// 上一轮后台扫描真正跑起来的时刻(见 IDLE_TICK_INTERVAL_MS 的降频)。
|
|
919
937
|
let lastBgScanAt = 0;
|
|
938
|
+
// 最近一次成功构建出来的会话清单与工作区体,以及构建时刻(见 BG_LIST_REBUILD_FALLBACK_MS)。
|
|
939
|
+
// 缓存的是**整份响应体**:既有 revision 可以判重,也能让「这一轮不用重建」的冷轮转
|
|
940
|
+
// 直接拿它去推,不必再扫一遍目录。
|
|
941
|
+
let cachedSessionsBody = null;
|
|
942
|
+
let cachedWorkspacesBody = null;
|
|
943
|
+
let cachedSessionsAt = 0;
|
|
944
|
+
let cachedWorkspacesAt = 0;
|
|
945
|
+
// 「宿主那边可能变了」:由事件流置起,成功重建清单后清掉。它把「每 500ms 盲目重扫」
|
|
946
|
+
// 换成「事件说变了才重扫」,而兜底窗保证即使漏了事件也不会长期停在旧数据上。
|
|
947
|
+
let listDirty = false;
|
|
948
|
+
|
|
949
|
+
const sessionItemsOf = (body) => (body && body.body && Array.isArray(body.body.items) ? body.body.items : []);
|
|
950
|
+
|
|
951
|
+
/** 记下一份新构建出来的清单(只记成功的;「没取到」绝不能被后续轮次当成权威数据)。 */
|
|
952
|
+
const rememberSessionsBody = (body) => {
|
|
953
|
+
cachedSessionsBody = body;
|
|
954
|
+
cachedSessionsAt = Date.now();
|
|
955
|
+
listDirty = false;
|
|
956
|
+
};
|
|
957
|
+
|
|
958
|
+
/**
|
|
959
|
+
* 工作区体:与清单同一套「脏了才重建」的口径。
|
|
960
|
+
*
|
|
961
|
+
* 复用缓存也照旧过一次 `observeWorkspaces`:工作区信号就是从这里喂给手机的,
|
|
962
|
+
* 复用那一份时同样要保证信号基准是对的(它是幂等的)。守卫失败不覆盖旧缓存。
|
|
963
|
+
*/
|
|
964
|
+
const loadWorkspacesBody = async (stale) => {
|
|
965
|
+
if (!stale && cachedWorkspacesBody) return observeWorkspaces(cachedWorkspacesBody) || cachedWorkspacesBody;
|
|
966
|
+
const body = await workspaces.buildWorkspacesBody(ctx).then(observeWorkspaces);
|
|
967
|
+
if (body && body.ok !== false) {
|
|
968
|
+
cachedWorkspacesBody = body;
|
|
969
|
+
cachedWorkspacesAt = Date.now();
|
|
970
|
+
}
|
|
971
|
+
return body;
|
|
972
|
+
};
|
|
920
973
|
|
|
921
974
|
const processBgQueue = async () => {
|
|
922
975
|
if (bgQueueBusy) return;
|
|
@@ -935,21 +988,46 @@ function createMirrorPusher(deps) {
|
|
|
935
988
|
bgQueueBusy = true;
|
|
936
989
|
let shouldAnnounceRevision = true;
|
|
937
990
|
try {
|
|
938
|
-
const
|
|
939
|
-
//
|
|
940
|
-
//
|
|
941
|
-
|
|
942
|
-
|
|
991
|
+
const now = Date.now();
|
|
992
|
+
// 清单重建判据(见 BG_LIST_REBUILD_FALLBACK_MS):构建会话清单是**全目录扫描**,
|
|
993
|
+
// 旧实现每 500ms 无条件重扫一遍,而「内容没变就跳过」那道判重只能省掉推送、省不掉
|
|
994
|
+
// 扫描。变没变宿主的会话事件流本来就在说,所以这里只在三种时刻真去问宿主:
|
|
995
|
+
// 从没构建过、有事件报告过变化(listDirty)、或距上次构建超过兜底窗。
|
|
996
|
+
const listStale = !cachedSessionsBody || listDirty || now - cachedSessionsAt >= BG_LIST_REBUILD_FALLBACK_MS;
|
|
997
|
+
let sessionsBody;
|
|
998
|
+
if (listStale) {
|
|
999
|
+
sessionsBody = await sessions.buildSessionsResponse(ctx, 'legacy');
|
|
1000
|
+
// 空列表此前只是静默 return,看不出是「真的没会话」还是「没取到」;这里让守卫
|
|
1001
|
+
// 判定并记录理由,别再无声无息。
|
|
1002
|
+
const sessionsBlocked = guardSessions(sessionsBody);
|
|
1003
|
+
if (sessionsBlocked) {
|
|
1004
|
+
// 「没取到」不能进缓存:留着上一份好快照,下一 tick 重试重建。
|
|
1005
|
+
listDirty = true;
|
|
1006
|
+
if (online) recordMirrorPush(false, sessionsBlocked);
|
|
1007
|
+
return;
|
|
1008
|
+
}
|
|
1009
|
+
rememberSessionsBody(sessionsBody);
|
|
1010
|
+
} else {
|
|
1011
|
+
sessionsBody = cachedSessionsBody;
|
|
1012
|
+
}
|
|
943
1013
|
retainSessionSignals(sessionsBody);
|
|
944
1014
|
// 清单内容版本(lib/shared/list-revision.js 的哈希):既是广播判据,也是下面
|
|
945
1015
|
// 「中转镜像里那一版是不是这份」的比对基准 —— 全函数只算这一次。
|
|
946
1016
|
const revision = sessionsRevisionOf(sessionsBody && sessionsBody.body);
|
|
1017
|
+
// 这一轮要不要重扫由上面的 listStale 决定;用缓存时工作区也走缓存(见 loadWorkspacesBody),
|
|
1018
|
+
// 「什么都没变」的那一轮到这里其实只剩几次内存比对,判定统一放在下面的冷轮转门里。
|
|
947
1019
|
if (!online) {
|
|
948
1020
|
// 离线:只广播「清单变了」,不推送也不记推送状态(否则会变成每 tick 一条假故障)。
|
|
949
1021
|
announceSessionsRevision(revision);
|
|
950
1022
|
return;
|
|
951
1023
|
}
|
|
952
|
-
const list = sessionsBody
|
|
1024
|
+
const list = sessionItemsOf(sessionsBody);
|
|
1025
|
+
// 工作区体也是一份重活(含几十 KB 的归档会话 id 列表),判据与清单同一套:
|
|
1026
|
+
// 清单刚重建过、或过了兜底窗才重问宿主,否则复用上一份。
|
|
1027
|
+
const workspacesBody = await loadWorkspacesBody(
|
|
1028
|
+
listStale || !cachedWorkspacesBody || now - cachedWorkspacesAt >= BG_LIST_REBUILD_FALLBACK_MS,
|
|
1029
|
+
);
|
|
1030
|
+
const workspacesRevision = workspacesBody && typeof workspacesBody.revision === 'string' ? workspacesBody.revision : '';
|
|
953
1031
|
if (list.length === 0) {
|
|
954
1032
|
// 空清单**也要推**。守卫放行的空清单是「用户真的清空了」(body.emptyConfirmed
|
|
955
1033
|
// 由 mobile/sessions.js 按 items.length === 0 现算),不推的话中转镜像里那份旧
|
|
@@ -959,8 +1037,7 @@ function createMirrorPusher(deps) {
|
|
|
959
1037
|
// 跨包契约:中转侧对「空覆盖非空」有同口径保护,只认 emptyConfirmed === true 的空
|
|
960
1038
|
// 清单(中转的镜像状态里同一条谓词)。这里推的是 buildSessionsResponse 的原样 body,
|
|
961
1039
|
// 天然带这个字段。
|
|
962
|
-
const
|
|
963
|
-
const wsBlockedEmpty = guardWorkspaces(workspacesBodyEmpty);
|
|
1040
|
+
const wsBlockedEmpty = guardWorkspaces(workspacesBody);
|
|
964
1041
|
if (wsBlockedEmpty) {
|
|
965
1042
|
// 工作区那份快照不可用:别拿一份「没取到」的工作区去覆盖镜像,只广播清单。
|
|
966
1043
|
recordMirrorPush(false, wsBlockedEmpty);
|
|
@@ -971,18 +1048,12 @@ function createMirrorPusher(deps) {
|
|
|
971
1048
|
kind: 'sync', at: Date.now(),
|
|
972
1049
|
activeSessionId: null,
|
|
973
1050
|
sessions: sessionsBody.body,
|
|
974
|
-
workspaces:
|
|
1051
|
+
workspaces: workspacesBody,
|
|
975
1052
|
messages: {}, stats: {}, models: {},
|
|
976
1053
|
});
|
|
977
1054
|
announceSessionsRevision(revision);
|
|
978
1055
|
return;
|
|
979
1056
|
}
|
|
980
|
-
// 门(见 BG_COLD_ROTATE_MIN_INTERVAL_MS):清单与工作区都还停在中转镜像里那一版、
|
|
981
|
-
// 又不到冷会话轮转时间时,这一轮到此为止 —— 省掉统计/模型投影与一个冷会话的 200 条
|
|
982
|
-
// 正文(那才是这个 tick 的大头)。工作区体提前到这里构建,正是为了拿到它的 revision。
|
|
983
|
-
const workspacesBody = await workspaces.buildWorkspacesBody(ctx).then(observeWorkspaces);
|
|
984
|
-
const workspacesRevision = workspacesBody && typeof workspacesBody.revision === 'string' ? workspacesBody.revision : '';
|
|
985
|
-
const now = Date.now();
|
|
986
1057
|
// 清掉已经不在清单里的会话(归档/删除),免得这张表无限长。
|
|
987
1058
|
const hasPendingRetry = sessionPushFailures.size > 0;
|
|
988
1059
|
if (hasPendingRetry) {
|
|
@@ -1086,10 +1157,10 @@ function createMirrorPusher(deps) {
|
|
|
1086
1157
|
const p = frame?.payload;
|
|
1087
1158
|
const type = typeof frame?.type === 'string' ? frame.type : (p && typeof p.type === 'string' ? p.type : '');
|
|
1088
1159
|
if (!type.startsWith('session/') && !type.startsWith('host/')) continue;
|
|
1089
|
-
if (type.startsWith('host/')) { void pushMirror(); continue; }
|
|
1160
|
+
if (type.startsWith('host/')) { listDirty = true; void pushMirror(); continue; }
|
|
1090
1161
|
// session/event 事件本身就是变更发生,按高优先级立即推送
|
|
1091
1162
|
const sessionId = frame?.sessionId || p?.sessionId;
|
|
1092
|
-
if (!sessionId) { void pushMirror(); continue; }
|
|
1163
|
+
if (!sessionId) { listDirty = true; void pushMirror(); continue; }
|
|
1093
1164
|
// assistant/chunk 是逐块流式增量,一轮生成里可达上千条。镜像只需要粗粒度状态:
|
|
1094
1165
|
// 同一轮收尾时的 assistant/message 会把完整消息带进来,手机侧的实时文本由
|
|
1095
1166
|
// session/stream 的 SSE 负责。不跳的话每条增量都要重建五份全量快照并推一次
|
|
@@ -1099,6 +1170,10 @@ function createMirrorPusher(deps) {
|
|
|
1099
1170
|
// 只认 turn/end,其余事件进来一行就退;它自己吞掉所有异常,不拖这条流。
|
|
1100
1171
|
void observeAutoContinue(ctx, sessionId, p?.event);
|
|
1101
1172
|
if (p?.event?.type === 'assistant/chunk') continue;
|
|
1173
|
+
// 到这里说明这一帧真会改清单行(turn/start、turn/end、session/title、
|
|
1174
|
+
// session/disposed …):给后台 tick 留个「脏」标记,它下一轮才去重扫目录。
|
|
1175
|
+
// 放在 chunk 之后 —— 流式增量只改正文、不改清单,不该把整份清单拖着重扫一遍。
|
|
1176
|
+
listDirty = true;
|
|
1102
1177
|
// iOS 推送:turn/end 是「可能刚收尾」的信号,把事实(lastTurnOf 现读)交给
|
|
1103
1178
|
// 通知器判断——只有 completed / failed 且这一轮之前没推过才会真发一帧。
|
|
1104
1179
|
// 标题从镜像快照现查:中转与苹果都只认纯文本,没查到就用会话 id 前缀。
|
|
@@ -307,13 +307,13 @@ function blocksToText(blocks) {
|
|
|
307
307
|
/**
|
|
308
308
|
* 照片说明行的哨兵(U+2063 INVISIBLE SEPARATOR)。
|
|
309
309
|
*
|
|
310
|
-
*
|
|
311
|
-
*
|
|
312
|
-
*
|
|
313
|
-
* 用户看到的是「我发的消息自己变长了」。
|
|
310
|
+
* **只服务老消息**:早先版本发送时会在图片块之后追加一个 text 块告诉模型「这张是什么时候
|
|
311
|
+
* 拍的、多大」(现在不这么发了)。那些消息的 content 里还留着那一行,而 `blocksToText` 把
|
|
312
|
+
* 所有 text 块用换行拼成用户消息的展示文本,重进会话 / 断线补齐时用户气泡里就会凭空长出一行
|
|
313
|
+
* 「[照片 …]」—— 用户看到的是「我发的消息自己变长了」。
|
|
314
314
|
*
|
|
315
|
-
*
|
|
316
|
-
*
|
|
315
|
+
* 当年那一行带了这个不可见前缀,这里在**算用户消息的展示文本时**把带它的整行去掉。字面量与
|
|
316
|
+
* 语义的单一来源是 `app/src/services/viewmodels/photo-caption.ts` 的
|
|
317
317
|
* `PHOTO_CAPTION_SENTINEL`(那边定义了常量与剥离函数,这里只出现同一个字符)。
|
|
318
318
|
*
|
|
319
319
|
* **为什么剥在插件而不是 App**:历史正文是这里拼的,App 拿到的 `item.text` 只是展示出口;
|
|
@@ -240,8 +240,8 @@ function turnMetaOf(events, turn) {
|
|
|
240
240
|
if (event.type === 'turn/start' && typeof event.time === 'number') startedAt = event.time;
|
|
241
241
|
if (event.type === 'user/message') {
|
|
242
242
|
const content = event.data && Array.isArray(event.data.content) ? event.data.content : [];
|
|
243
|
-
// 轮标题取的是「用户说了什么」:用 `userVisibleText` 而不是裸 `blocksToText` ——
|
|
244
|
-
//
|
|
243
|
+
// 轮标题取的是「用户说了什么」:用 `userVisibleText` 而不是裸 `blocksToText` —— 老消息里
|
|
244
|
+
// 留下的照片说明行(现在不再发,哨兵见 `app/src/services/viewmodels/photo-caption.ts`)
|
|
245
245
|
// 混进标题就是时间线上一行「[照片 …]」,而用户其实什么都没写。
|
|
246
246
|
const text = userVisibleText(parseBlocks(content, toolResults)).trim();
|
|
247
247
|
if (text !== '' && title === '') title = text.length > 120 ? `${text.slice(0, 119)}…` : text;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-mobile-plugin",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.46",
|
|
4
4
|
"description": "Mobile API (/api/mobile/v1) and relay client for the DSH mobile app — desktop profile build (dsh 0.2.x, peers resolved by the host)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"icon": "./icon.png",
|