dsh-mobile-plugin 0.5.118 → 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.
@@ -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();