dsh-mobile-plugin 0.5.49 → 0.5.50

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.
@@ -239,6 +239,19 @@ function reset() {
239
239
  SESSIONS.clear();
240
240
  }
241
241
 
242
+ /**
243
+ * 只从盘上那份对象文件头量宽高,**不问宿主、不搬字节**(批量那一档用)。
244
+ *
245
+ * 量不出来(对象文件不在 / 不是位图)回 null,调用方退回问宿主那条路。这条比 `refFor`
246
+ * 便宜的地方在于:它连「和盘上字节对得上的引用」都不拼 —— 宿主那条校验只在真要取字节
247
+ * 或要宿主自己量的时候才用得上。
248
+ */
249
+ function localDims(attachmentId) {
250
+ const probed = probeObject(attachmentId);
251
+ if (!probed) return null;
252
+ return { width: probed.width, height: probed.height };
253
+ }
254
+
242
255
  module.exports = {
243
256
  MAX_IDS_PER_SESSION,
244
257
  MAX_SESSIONS,
@@ -247,6 +260,7 @@ module.exports = {
247
260
  remember,
248
261
  lookup,
249
262
  refFor,
263
+ localDims,
250
264
  openObject,
251
265
  streamIfRemembered,
252
266
  reset,
@@ -113,7 +113,12 @@ function monitorItem(summary, state, fact) {
113
113
  * `{ error }` —— 两种都由调用方照 `/sessions` 的口径回,别在这里编一个空清单:空清单会被
114
114
  * 原生当成「所有会话都没了」,基线一清,下一轮就为所有正在跑的会话重新报一次完成。
115
115
  */
116
- async function buildMonitorState(ctx, deviceKey, extraItems = null) {
116
+ /**
117
+ * @param {string|null} scopeSessionId 只回这一条会话(对话页对账用);null = 整份视图(后台提醒用)。
118
+ *
119
+ * **收窄只发生在出 item 那一步**:完成态台账仍按整份清单推进 —— 见下面那句注释。
120
+ */
121
+ async function buildMonitorState(ctx, deviceKey, extraItems = null, scopeSessionId = null) {
117
122
  const { value, error } = unwrap(await callSessions(ctx, 'list', {}, 'mobile'));
118
123
  if (error) return { error };
119
124
  const rawSummaries = Array.isArray(value) ? value : (value && Array.isArray(value.items) ? value.items : null);
@@ -203,7 +208,15 @@ async function buildMonitorState(ctx, deviceKey, extraItems = null) {
203
208
  // 按 id 排序后再算指纹:宿主是按 updatedAt 排的,聊一句话顺序就换,指纹会跟着每轮都变,
204
209
  // 条件刷新等于白做。监控视图不关心顺序。
205
210
  items.sort((a, b) => (a.sessionId < b.sessionId ? -1 : a.sessionId > b.sessionId ? 1 : 0));
206
- return { items, revision: stableRevision(items) };
211
+ /**
212
+ * 收窄到一条会话(`?sessionId=`,对话页对账用)。
213
+ *
214
+ * **只影响回什么,不影响台账**:上面那些按整份清单跑的(完成态、未定结局的补确认、指纹)
215
+ * 一个字不动 —— `updateCompletionStates` 的「不在列表里就丢」是拿完整清单定的,收窄放在
216
+ * 它之后,免得一次单条请求把别条会话的状态清掉。
217
+ */
218
+ const scoped = scopeSessionId ? items.filter((item) => item.sessionId === scopeSessionId) : items;
219
+ return { items: scoped, revision: stableRevision(scoped) };
207
220
  }
208
221
 
209
222
  /** 一次挂起的上限。手机侧每次挂 20 秒,再长没有意义:断线重连比占着一条连接便宜。 */
@@ -241,10 +254,10 @@ function waitForChange(timeoutMs, res) {
241
254
  }
242
255
 
243
256
  /** 算一次监控视图,把「宿主出错 / 形状异常」也收成一个可下发的响应。 */
244
- async function buildMonitorView(ctx, deviceKey, extraItems = null) {
257
+ async function buildMonitorView(ctx, deviceKey, extraItems = null, scopeSessionId = null) {
245
258
  let built;
246
259
  try {
247
- built = await buildMonitorState(ctx, deviceKey, extraItems);
260
+ built = await buildMonitorState(ctx, deviceKey, extraItems, scopeSessionId);
248
261
  } catch (error) {
249
262
  return {
250
263
  status: 500,
@@ -272,16 +285,22 @@ async function buildMonitorView(ctx, deviceKey, extraItems = null) {
272
285
  *
273
286
  * 只挂一次:醒来之后不管变没变都如实回(变没变由 revision 说了算),由手机自己决定
274
287
  * 要不要再挂。这样这里没有循环、没有重试,断线也不会在宿主侧留下一个转不停的请求。
288
+ *
289
+ * `sessionId` 是可选的收窄:对话页「这张卡还在不在」的对账只关心自己那一条,带上它就只回
290
+ * 这一条(台账照旧按整份清单推进,见 buildMonitorState)。
275
291
  */
276
292
  async function handleMonitor(ctx, req, res, extraItems = null) {
277
293
  const deviceKey = canonicalDeviceKey(req);
278
294
  const url = new URL(req.url, 'http://x');
279
295
  const rev = (url.searchParams.get('rev') || '').trim();
280
296
  const waitSeconds = Number(url.searchParams.get('wait') || 0) || 0;
297
+ // 只回这一条会话(见 buildMonitorState 的同名参数):对话页对账只要自己那一格,
298
+ // 不必为了一格卡把整台电脑的会话状态搬一遍。
299
+ const scopeSessionId = (url.searchParams.get('sessionId') || '').trim() || null;
281
300
  // 没有 rev 就不挂:那不是「确认一下有没有变」,而是「给我一份完整状态」,立刻回。
282
301
  const waitMs = rev ? Math.max(0, Math.min(WAIT_MAX_MS, Math.round(waitSeconds * 1000))) : 0;
283
302
 
284
- const first = await buildMonitorView(ctx, deviceKey, extraItems);
303
+ const first = await buildMonitorView(ctx, deviceKey, extraItems, scopeSessionId);
285
304
  if (first.status !== 200) {
286
305
  return sendJson(res, first.status, first.body);
287
306
  }
@@ -298,7 +317,7 @@ async function handleMonitor(ctx, req, res, extraItems = null) {
298
317
  await waitForChange(waitMs, res);
299
318
  // 手机在这段时间里断了(切网 / 进程被杀):别为一个没人收的响应再算一遍。
300
319
  if (res.destroyed || res.writableEnded) return;
301
- const again = await buildMonitorView(ctx, deviceKey, extraItems);
320
+ const again = await buildMonitorView(ctx, deviceKey, extraItems, scopeSessionId);
302
321
  const body = again.status === 200 ? (unchangedListBody(again.body, rev) || again.body) : again.body;
303
322
  // 头已经出去了,不能再走 sendJson(它会再写一次头):这里只补正文。
304
323
  try { res.end(JSON.stringify(body)); } catch { /* 连接已断开 */ }
@@ -320,6 +320,11 @@ async function handleScheduleCatalog(ctx, _req, res) {
320
320
  * 走 `catalog()` 再按 sessionId 筛,而不是宿主的 `list({sessionId})`:后者只回还在跑的,
321
321
  * 且行上没有 sessionId / status(见文件头)。`state` 因此由行上的 status 推出来,
322
322
  * 已结束的任务才能如实显示成「已结束」。
323
+ *
324
+ * `?brief=1` 是页头那只闹钟用的轻量档:只要「几条活动任务 + 最近要跑的那条 id」,
325
+ * 不把每条任务的正文本、规则、交付记录整份搬回去。**筛选与判定完全同一套**(同一个
326
+ * `normalizeScheduleEntries` + 同一个 active 判据),只是出参少了两样 —— 两处各写一份
327
+ * 判定迟早会给出两个数(角标说 2、任务页说 3)。
323
328
  */
324
329
  async function handleScheduleList(ctx, req, res) {
325
330
  const url = new URL(req.url, 'http://x');
@@ -340,11 +345,21 @@ async function handleScheduleList(ctx, req, res) {
340
345
 
341
346
  // 先按目录整体收敛(保住 status → state 的推导),再筛这条会话的。
342
347
  const all = normalizeScheduleEntries(value, undefined, undefined);
343
- sendJson(res, 200, {
344
- ok: true,
345
- records: all.filter((entry) => entry.sessionId === sessionId),
346
- serverNow: Date.now(),
347
- });
348
+ const records = all.filter((entry) => entry.sessionId === sessionId);
349
+ if (queryValue(url, 'brief') === '1') {
350
+ const active = records
351
+ .filter((entry) => entry.state === 'active')
352
+ // 与 App 侧同一条排序口径:下次运行最近的在前,没有 scheduledAt 的垫底。
353
+ .sort((a, b) => (Date.parse(a.scheduledAt ?? '') || Infinity) - (Date.parse(b.scheduledAt ?? '') || Infinity));
354
+ return sendJson(res, 200, {
355
+ ok: true,
356
+ brief: true,
357
+ count: active.length,
358
+ ...(active[0]?.id === undefined ? {} : { firstTaskId: active[0].id }),
359
+ serverNow: Date.now(),
360
+ });
361
+ }
362
+ sendJson(res, 200, { ok: true, records, serverNow: Date.now() });
348
363
  }
349
364
 
350
365
  /** 把查询串里的 limit 收成 1–100 的整数;非法返回 0。 */
@@ -278,6 +278,13 @@ async function handleFork(ctx, req, res) {
278
278
  */
279
279
  async function handleSessionAttachment(ctx, req, res) {
280
280
  const body = await readJsonBody(req);
281
+ // 批量宽高走同一条路由(`attachmentIds`):一次一屏,不再逐张发(见 handleAttachmentDimsBatch)。
282
+ if (body && Array.isArray(body.attachmentIds)) {
283
+ if (typeof body.sessionId !== 'string' || !body.sessionId) {
284
+ return sendJson(res, 400, { ok: false, error: { code: 'bad-request', message: '需要 { sessionId, attachmentIds }' } });
285
+ }
286
+ return handleAttachmentDimsBatch(ctx, body, res);
287
+ }
281
288
  if (!body || typeof body.sessionId !== 'string' || !body.sessionId
282
289
  || typeof body.attachmentId !== 'string' || !body.attachmentId) {
283
290
  return sendJson(res, 400, { ok: false, error: { code: 'bad-request', message: '需要 { sessionId, attachmentId }' } });
@@ -325,4 +332,52 @@ async function handleSessionAttachment(ctx, req, res) {
325
332
  });
326
333
  }
327
334
 
328
- module.exports = { singleTextOf, executeCommand, promptOnce, handleSend, handleCommands, handleSessionRead, handleSessionAttachment, handleStop, handleRename, handleFork };
335
+ /**
336
+ * 一次问一批附件图的宽高:`{ sessionId, attachmentIds: [...] }` → `{ ok, dims: { <id>: {width, height} } }`。
337
+ *
338
+ * 为什么要有这一档:老会话里缺宽高的图一屏能有十几张,逐张各发一次请求,走中转时每一发
339
+ * 都是两秒起步;而这一档**先看盘上那份对象文件的文件头**,绝大多数图连宿主都不用问,
340
+ * 整屏一次往返就齐了。
341
+ *
342
+ * 三条口径:
343
+ * - **先盘上、后宿主**:`attachmentStore.localDims` 直接读文件头;量不出来的(对象不在 /
344
+ * 没登记过的老 id)才退回问宿主,仍是一张一次,但那是少数;
345
+ * - **量不出来的就是没有**:不进结果表,App 那边按兜底盒画 —— 与单张那档的 404 同一个语义,
346
+ * 只是批量里不能用一次失败毁掉整批;
347
+ * - **条数封顶**:`DIMS_BATCH_MAX` 与 App 侧「一屏最多补几张」同一量级,多出来的等下一批。
348
+ */
349
+ const DIMS_BATCH_MAX = 64;
350
+
351
+ async function handleAttachmentDimsBatch(ctx, body, res) {
352
+ const ids = Array.isArray(body.attachmentIds)
353
+ ? [...new Set(body.attachmentIds.filter((id) => typeof id === 'string' && id))]
354
+ : [];
355
+ if (ids.length === 0) {
356
+ return sendJson(res, 400, { ok: false, error: { code: 'bad-request', message: '需要 { sessionId, attachmentIds }' } });
357
+ }
358
+ const dims = {};
359
+ const misses = [];
360
+ for (const attachmentId of ids.slice(0, DIMS_BATCH_MAX)) {
361
+ const local = attachmentStore.localDims(attachmentId);
362
+ if (local) dims[attachmentId] = local;
363
+ else misses.push(attachmentId);
364
+ }
365
+ // 盘上量不出来的:照单张那条老路问一次宿主(拼不出引用 / 宿主读不到就跳过这张)。
366
+ for (const attachmentId of misses) {
367
+ const ref = attachmentStore.refFor(body.sessionId, attachmentId);
368
+ if (!ref) continue;
369
+ try {
370
+ const { value, error } = unwrap(await callSessions(ctx, 'attachment', { sessionId: body.sessionId, attachment: ref }));
371
+ if (error) continue;
372
+ const data = value && typeof value.data === 'string' ? value.data : '';
373
+ if (!data) continue;
374
+ const size = imageSizeOf(Buffer.from(data, 'base64'));
375
+ if (size && size.width > 0 && size.height > 0) dims[attachmentId] = { width: size.width, height: size.height };
376
+ } catch {
377
+ // 这一张量不到就当没有:整批照旧回,别让一张坏图拖垮一屏。
378
+ }
379
+ }
380
+ sendJson(res, 200, { ok: true, dims });
381
+ }
382
+
383
+ module.exports = { singleTextOf, executeCommand, promptOnce, handleSend, handleCommands, handleSessionRead, handleSessionAttachment, handleAttachmentDimsBatch, DIMS_BATCH_MAX, handleStop, handleRename, handleFork };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-mobile-plugin",
3
- "version": "0.5.49",
3
+ "version": "0.5.50",
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",