dsh-mobile-plugin 0.5.117 → 0.5.119

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/mobile/rpc.js CHANGED
@@ -1,3 +1,4 @@
1
+ // 宿主列表仅在同一失效代次内合流,新变更后的读取不能复用旧 RPC。
1
2
  'use strict';
2
3
 
3
4
  /**
@@ -14,6 +15,7 @@
14
15
  */
15
16
 
16
17
  const { MOBILE_PROMPT_RPC_PREFIX } = require('./history-derive.js');
18
+ const { sessionListResponseState } = require('./session-cache.js');
17
19
 
18
20
  // 移动端提示词的 rpcId 约定与 user 消息回溯共用,前缀常量定义放在没有依赖的
19
21
  // history-derive 里(history-derive 要用 clientMessageIdFromSource,而会话模块
@@ -40,8 +42,9 @@ function callSessions(ctx, method, payload, rpcId = 'mobile', signal, options) {
40
42
  if (method !== 'list') return invoke();
41
43
  if (options && options.fresh) return invoke();
42
44
 
45
+ const epoch = sessionListResponseState(ctx).droppedAt || 0;
43
46
  const existing = sessionListInFlight.get(ctx);
44
- if (existing) return existing;
47
+ if (existing && existing.epoch === epoch) return existing.promise;
45
48
 
46
49
  let request;
47
50
  try {
@@ -53,15 +56,15 @@ function callSessions(ctx, method, payload, rpcId = 'mobile', signal, options) {
53
56
  }
54
57
  const tracked = request.then(
55
58
  value => {
56
- if (sessionListInFlight.get(ctx) === tracked) sessionListInFlight.delete(ctx);
59
+ if (sessionListInFlight.get(ctx)?.promise === tracked) sessionListInFlight.delete(ctx);
57
60
  return value;
58
61
  },
59
62
  error => {
60
- if (sessionListInFlight.get(ctx) === tracked) sessionListInFlight.delete(ctx);
63
+ if (sessionListInFlight.get(ctx)?.promise === tracked) sessionListInFlight.delete(ctx);
61
64
  throw error;
62
65
  },
63
66
  );
64
- sessionListInFlight.set(ctx, tracked);
67
+ sessionListInFlight.set(ctx, { epoch, promise: tracked });
65
68
  return tracked;
66
69
  }
67
70
 
@@ -1,3 +1,4 @@
1
+ // 清单变更会断开旧构建的合流引用,后续读取不能继续搭旧快照。
1
2
  // 会话缓存集中保存按 ctx 隔离的状态,持久化修订随服务身份换代。
2
3
  'use strict';
3
4
 
@@ -437,6 +438,9 @@ function writeIdempotencyStateOf(ctx) {
437
438
  * @param {string} [deviceKey] 只清这一台设备的缓存;缺省清掉该 ctx 下所有设备。
438
439
  */
439
440
  function invalidateSessionList(ctx, deviceKey) {
441
+ // 宿主 list 在 ctx 内共享,任一设备写入后都要换掉其底层合流代次。
442
+ const shared = sessionListResponseState(ctx);
443
+ shared.droppedAt = (shared.droppedAt || 0) + 1;
440
444
  const byDevice = sessionListResponseCache.get(ctx);
441
445
  if (!byDevice) return;
442
446
  const keys = deviceKey === undefined ? [...byDevice.keys()] : [deviceKey];
@@ -465,6 +469,8 @@ function invalidateSessionList(ctx, deviceKey) {
465
469
  * @param {string} [deviceKey] 只丢这一台设备的缓存;缺省丢掉该 ctx 下所有设备。
466
470
  */
467
471
  function dropSessionListCache(ctx, deviceKey) {
472
+ const shared = sessionListResponseState(ctx);
473
+ shared.droppedAt = (shared.droppedAt || 0) + 1;
468
474
  const byDevice = sessionListResponseCache.get(ctx);
469
475
  if (!byDevice) return;
470
476
  const keys = deviceKey === undefined ? [...byDevice.keys()] : [deviceKey];
@@ -473,6 +479,8 @@ function dropSessionListCache(ctx, deviceKey) {
473
479
  if (!state) continue;
474
480
  state.value = null;
475
481
  state.updatedAt = 0;
482
+ state.promise = null;
483
+ state.lastRevision = '';
476
484
  state.droppedAt = (state.droppedAt || 0) + 1;
477
485
  }
478
486
  }
@@ -1,3 +1,4 @@
1
+ // 失效前发起的列表构建不得回填缓存或登记条件刷新基准。
1
2
  'use strict';
2
3
 
3
4
  /**
@@ -23,9 +24,9 @@ const { listTitle, deriveMessages, lastMessageOf, promptErrorObject, MODEL_UNAVA
23
24
  const { unwrap } = require('./envelope.js');
24
25
  const { callSessions } = require('./rpc.js');
25
26
  const {
26
- canonicalDeviceKey, sessionListResponseState, invalidateSessionList, deletedSessionIdsOf,
27
+ canonicalDeviceKey, sessionListResponseState, invalidateSessionList, dropSessionListCache, deletedSessionIdsOf,
27
28
  completionGenerationOf, bumpCompletionGeneration,
28
- completionStateOf, sessionModelsStateOf, invalidateSessionModels, sendIdempotencyStateOf,
29
+ completionStateOf, sessionModelsStateOf, invalidateSessionModels, sendIdempotencyStateOf, sessionPreviewStateOf,
29
30
  } = require('./session-cache.js');
30
31
  const { resolveEventAttachments } = require('./attachments.js');
31
32
  const { updateCompletionStates, confirmCompletionFromHistory } = require('./completion.js');
@@ -125,7 +126,11 @@ async function handleSessions(ctx, req, res) {
125
126
  // 但「丢掉」挡不住**已经在飞**的那次构建:它手里是变更之前的数据,落地后会在缓存里
126
127
  // 再活 10 秒。用缓存自带的朝代号兜住(见 session-cache.js 的 dropSessionListCache)。
127
128
  const epoch = cache.droppedAt || 0;
128
- const promise = buildSessionsResponse(ctx, deviceKey, generation, wantFresh, archivedOnly, page);
129
+ const buildOptions = revEligible && rev ? {
130
+ awaitPreviews: false,
131
+ onPreviewsReady: () => dropSessionListCache(ctx, deviceKey),
132
+ } : {};
133
+ const promise = buildSessionsResponse(ctx, deviceKey, generation, wantFresh, archivedOnly, page, buildOptions);
129
134
  cache.promise = promise;
130
135
  try {
131
136
  const response = await promise;
@@ -140,15 +145,15 @@ async function handleSessions(ctx, req, res) {
140
145
  cache.value = response.body;
141
146
  cache.updatedAt = Date.now();
142
147
  }
148
+ if (!archivedOnly) rememberListBuild(cache, response.body, generation);
143
149
  }
144
- if (response.status === 200) rememberListBuild(cache, response.body, completionGenerationOf(ctx, deviceKey));
145
150
  return respond(response.status, response.body);
146
151
  } finally {
147
152
  if (cache.promise === promise) cache.promise = null;
148
153
  }
149
154
  }
150
155
 
151
- async function buildSessionsResponse(ctx, deviceKey = 'legacy', generation = completionGenerationOf(ctx, deviceKey), fresh = false, archivedOnly = false, page = null) {
156
+ async function buildSessionsResponse(ctx, deviceKey = 'legacy', generation = completionGenerationOf(ctx, deviceKey), fresh = false, archivedOnly = false, page = null, buildOptions = {}) {
152
157
  const { value, error } = unwrap(await callSessions(ctx, 'list', {}, 'mobile', undefined, { fresh }));
153
158
  if (error) return { status: 400, body: { ok: false, error } };
154
159
  // 形状不对 = 没取到,不是「你一个会话都没有」。旧实现 `(value.items || [])` 把
@@ -245,7 +250,16 @@ async function buildSessionsResponse(ctx, deviceKey = 'legacy', generation = com
245
250
  (b.summary.running ? 1 : 0) - (a.summary.running ? 1 : 0)
246
251
  || (b.summary.updatedAt || 0) - (a.summary.updatedAt || 0))
247
252
  .slice(0, SESSIONS_PREVIEW_LIMIT);
248
- await Promise.all(previewCandidates.map(async ({ summary: s, base }) => {
253
+ const deferPreviews = buildOptions.awaitPreviews === false;
254
+ if (deferPreviews) {
255
+ for (const { summary: s, base } of previewCandidates) {
256
+ base.lastMessage = sessionPreviewStateOf(ctx).get(s.sessionId)?.lastMessage || null;
257
+ }
258
+ }
259
+ const previewsBefore = deferPreviews ? JSON.stringify(previewCandidates.map(({ base }) => base)) : '';
260
+ const previews = Promise.all(previewCandidates.map(async ({ summary: s, base: listed }) => {
261
+ // 先发清单时只在副本里补预览,不能修改已经返回且算过 revision 的那份行。
262
+ const base = deferPreviews ? { ...listed } : listed;
249
263
  try {
250
264
  const preview = await sessionPreviewFor(ctx, s);
251
265
  const events = preview.events;
@@ -270,7 +284,13 @@ async function buildSessionsResponse(ctx, deviceKey = 'legacy', generation = com
270
284
  } catch {
271
285
  // 预览失败不影响标题与行数据。
272
286
  }
287
+ return base;
273
288
  }));
289
+ if (deferPreviews) {
290
+ void previews.then((completed) => {
291
+ if (previewsBefore !== JSON.stringify(completed)) buildOptions.onPreviewsReady?.();
292
+ }).catch(() => {});
293
+ } else await previews;
274
294
  const allItems = rows.map((r) => r.base);
275
295
  const items = page ? allItems.slice(page.offset, page.offset + page.limit) : allItems;
276
296
  const generatedAt = Date.now();