@marktrue/dsh-mobile-remote 3.2.4-alpha.11 → 3.2.4-alpha.12

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/CHANGELOG.md CHANGED
@@ -1,5 +1,31 @@
1
1
  # Changelog
2
2
 
3
+ ## v3.2.4-alpha.12(build 44,issues #78 / #79)— 图片与折叠 hotfix
4
+
5
+ > 插件 `3.2.4-alpha.12` / App `3.2.4+44` / 载体 `3.2.4-alpha.12-app`。
6
+
7
+ ### 图片加载修复(#78,P0)
8
+
9
+ - 修复 `/m/api/attachment`:DSH `session.attachment` RPC 新 schema 要求 `{sessionId, attachment:{完整引用}}`;改为从会话原始事件解析完整 image ref 后再调用,用户附图/read_image/历史图恢复正常。
10
+ - 非法 id/缺失 ref/存储缺失 → 404 `attachment-not-found`;LRU 缓存 + 宿主拒绝时清缓存重扫,不按路径读文件、不信任客户端字段。
11
+ - 真实 HTTP 验证:用户附图 200、read_image 200、不存在 404。
12
+
13
+ ### 插话折叠修复(#79,P1)
14
+
15
+ - 修复「思考/运行中插入消息后该轮永久无法折叠」:`forceExpanded` 仅保留「未结束 / aborted / interrupted / error」。
16
+ - 轮内插话改为**默认展开但可手动折叠**:`afterAnswer` 档回答刚结束保持展开;`onNewMessage` 档仍按轮后新消息自动收起。
17
+ - 运行中/异常轮仍硬强制展开;两档时机、手动覆盖、组折叠矩阵不变。
18
+
19
+ ### 文档
20
+
21
+ - ADR 0027 §2 / ADR 0028 §4 同步插话折叠语义。
22
+
23
+ ### 验证
24
+
25
+ - flutter test 1221 全绿;timeline-contract 179、account-usage 15/15、Kotlin 门禁通过;attachment-routes 11/11。
26
+ - 真机验收:alpha.12 重点验证图片加载、插话后折叠、折叠视觉/滚动回归。
27
+
28
+
3
29
  ## v3.2.4-alpha.11(build 43,issues #73 / #74 / #75 / #76 / #77)— 真机验收修复
4
30
 
5
31
  > 插件 `3.2.4-alpha.11` / App `3.2.4+43` / 载体 `3.2.4-alpha.11-app`。
package/lib/index.js CHANGED
@@ -1769,6 +1769,94 @@ function readUpdateManifest(dir, basePath = "/m", origin = "") {
1769
1769
  return { ok: true, manifest, pluginServed, dir };
1770
1770
  }
1771
1771
 
1772
+ /** DSH `session.attachment` 请求 schema 允许的媒体类型(strict enum,issue #78)。 */
1773
+ const ATTACHMENT_MEDIA_TYPES = Object.freeze(["image/png", "image/jpeg", "image/webp", "image/gif"]);
1774
+
1775
+ /** attachmentId 基本形状校验:不透明存储 id(允许 `sha256:` 等),拒绝空白/控制字符。 */
1776
+ export function attachmentIdValid(value) {
1777
+ return typeof value === "string"
1778
+ && value.length > 0
1779
+ && value.length <= 512
1780
+ && !/[\s\u0000-\u001f\u007f]/.test(value);
1781
+ }
1782
+
1783
+ /**
1784
+ * 从原始 attachment ref 提取 `session.attachment` strict schema 允许的完整引用
1785
+ * (issue #78):新 DSH schema 只接受 `{attachmentId,mediaType,bytes,width,height,name?,
1786
+ * originalDimensions?}`,且 mediaType 是 png/jpeg/webp/gif 枚举。字段缺失/非法一律
1787
+ * fail-closed 返回 null(绝不把客户端字段或半截引用发给网关)。
1788
+ * @param {unknown} value 原始 attachment 对象。
1789
+ * @returns {{attachmentId:string,mediaType:string,bytes:number,width:number,height:number,name?:string,originalDimensions?:{width:number,height:number}}|null}
1790
+ */
1791
+ export function attachmentRefFrom(value) {
1792
+ if (!value || typeof value !== "object" || Array.isArray(value)) return null;
1793
+ const attachmentId = typeof value.attachmentId === "string" ? value.attachmentId : "";
1794
+ if (!attachmentIdValid(attachmentId)) return null;
1795
+ const mediaType = typeof value.mediaType === "string" ? value.mediaType : "";
1796
+ if (!ATTACHMENT_MEDIA_TYPES.includes(mediaType)) return null;
1797
+ const bytes = value.bytes;
1798
+ const width = value.width;
1799
+ const height = value.height;
1800
+ if (!Number.isFinite(bytes) || bytes <= 0) return null;
1801
+ if (!Number.isFinite(width) || width <= 0) return null;
1802
+ if (!Number.isFinite(height) || height <= 0) return null;
1803
+ const ref = { attachmentId, mediaType, bytes, width, height };
1804
+ if (typeof value.name === "string" && value.name !== "") {
1805
+ // name 可选;保持为纯展示字段(不拼接本地路径),并去掉控制字符。
1806
+ const name = value.name.replace(/[\u0000-\u001f\u007f]/g, "").trim();
1807
+ if (name !== "") ref.name = name.slice(0, 512);
1808
+ }
1809
+ const dimensions = value.originalDimensions;
1810
+ if (dimensions && typeof dimensions === "object"
1811
+ && Number.isFinite(dimensions.width) && dimensions.width > 0
1812
+ && Number.isFinite(dimensions.height) && dimensions.height > 0) {
1813
+ ref.originalDimensions = { width: dimensions.width, height: dimensions.height };
1814
+ }
1815
+ return ref;
1816
+ }
1817
+
1818
+ /**
1819
+ * 按 attachmentId 从会话原始事件里找到完整 image attachment ref(issue #78)。
1820
+ * 服务端投影的 `images[]` 只有元数据、没有 bytes;`session.attachment` 新 schema
1821
+ * 需要完整 ref,因此必须回到**原始事件**(user/message、assistant/message、
1822
+ * tool/result 的 content blocks)里按 id 查找。找不到/字段不完整返回 null(不猜)。
1823
+ * @param {any[]} events 会话原始事件。
1824
+ * @param {string} attachmentId 目标 id。
1825
+ * @returns {object|null} 经 attachmentRefFrom 校验后的完整 ref。
1826
+ */
1827
+ export function extractAttachmentRef(events, attachmentId) {
1828
+ if (!Array.isArray(events) || !attachmentIdValid(attachmentId)) return null;
1829
+ let found = null;
1830
+ const seen = new Set();
1831
+ const consider = (value) => {
1832
+ if (found || !value || typeof value !== "object") return;
1833
+ const ref = attachmentRefFrom(value);
1834
+ if (ref && ref.attachmentId === attachmentId) found = ref;
1835
+ };
1836
+ const visit = (value, depth) => {
1837
+ if (found || depth > 12 || value === null || value === undefined) return;
1838
+ if (Array.isArray(value)) {
1839
+ for (const entry of value) visit(entry, depth + 1);
1840
+ return;
1841
+ }
1842
+ if (typeof value !== "object") return;
1843
+ if (seen.has(value)) return;
1844
+ seen.add(value);
1845
+ // image block:attachment 在嵌套对象里;images[]/message.content 里的 flat ref 也接受。
1846
+ if (value.type === "image" && value.attachment) consider(value.attachment);
1847
+ consider(value);
1848
+ for (const key of Object.keys(value)) {
1849
+ const child = value[key];
1850
+ if (child !== null && typeof child === "object") visit(child, depth + 1);
1851
+ }
1852
+ };
1853
+ for (const event of events) {
1854
+ if (event !== null && typeof event === "object") visit(event.data ?? event, 0);
1855
+ if (found) break;
1856
+ }
1857
+ return found;
1858
+ }
1859
+
1772
1860
  /** image attachment 引用 → 移动端图片元数据(仅引用不含字节;渲染时 App 按 attachmentId 走 /attachment 端点拉取)。 */
1773
1861
  function imageMetaOf(ref) {
1774
1862
  if (!ref || typeof ref !== "object" || !ref.attachmentId) return null;
@@ -6946,6 +7034,48 @@ export function apply(ctx, config, runtime = {}) {
6946
7034
  // 队列有界 + 截止时间从请求到达时算起(issue #25 评审 P1):见 createTurnOutlineQueue。
6947
7035
  const turnOutlineQueue = createTurnOutlineQueue();
6948
7036
 
7037
+ // issue #78:/attachment 每次请求都要从会话原始事件里按 id 找完整 ref;同一张图常被
7038
+ // 缩略图/查看器/重试重复拉取,用进程内小 LRU 缓存已校验的 ref(只缓存完整合法 ref,
7039
+ // 解析失败不缓存,避免污染)。
7040
+ const attachmentRefCache = new Map();
7041
+ const ATTACHMENT_REF_CACHE_LIMIT = 64;
7042
+ const rememberAttachmentRef = (key, ref) => {
7043
+ attachmentRefCache.delete(key);
7044
+ attachmentRefCache.set(key, ref);
7045
+ while (attachmentRefCache.size > ATTACHMENT_REF_CACHE_LIMIT) {
7046
+ const oldest = attachmentRefCache.keys().next().value;
7047
+ attachmentRefCache.delete(oldest);
7048
+ }
7049
+ };
7050
+ /** 取会话原始事件:live 会话走 snapshotEvents;休眠会话走 readSession(含 seeded 降级)。 */
7051
+ const attachmentEventsOf = async (sessionId) => {
7052
+ const sessions = ctx.get("sessions");
7053
+ let live;
7054
+ try { live = sessions?.get?.(sessionId); } catch { live = undefined; }
7055
+ if (live) {
7056
+ const events = eventsOf(live);
7057
+ if (events.length > 0) return events;
7058
+ }
7059
+ const query = ctx.get("sessionQuery");
7060
+ if (!query || typeof query.readSession !== "function") return null;
7061
+ const loaded = await loadDormantSession(query, sessionId);
7062
+ return loaded?.ok ? eventsOf(loaded.snapshot) : null;
7063
+ };
7064
+ /**
7065
+ * 解析 attachmentId → 严格 schema 的完整 ref;缓存命中直接返回,未命中扫事件。
7066
+ * 解析失败返回 null(调用方 404,不按路径读文件、不猜)。
7067
+ */
7068
+ const resolveAttachmentRef = async (sessionId, attachmentId) => {
7069
+ const key = `${sessionId}\u0000${attachmentId}`;
7070
+ const cached = attachmentRefCache.get(key);
7071
+ if (cached) return { ref: cached, cached: true, key };
7072
+ let events = null;
7073
+ try { events = await attachmentEventsOf(sessionId); } catch { events = null; }
7074
+ const ref = extractAttachmentRef(events ?? [], attachmentId);
7075
+ if (!ref) return null;
7076
+ rememberAttachmentRef(key, ref);
7077
+ return { ref, cached: false, key };
7078
+ };
6949
7079
  const handleApi = async (req, res, url, rest) => {
6950
7080
  if (!hostAllowed(req)) {
6951
7081
  error(res, 403, "host-not-allowed");
@@ -7496,14 +7626,32 @@ export function apply(ctx, config, runtime = {}) {
7496
7626
  const sessionId = url.searchParams.get("sessionId");
7497
7627
  const attachmentId = url.searchParams.get("attachmentId");
7498
7628
  if (!sessionId || !attachmentId) return error(res, 400, "bad-request", "sessionId/attachmentId required");
7629
+ // 非法 id 直接 404:不扫事件、不把客户端字段当 ref(issue #78)。
7630
+ if (!attachmentIdValid(attachmentId)) return error(res, 404, "attachment-not-found");
7631
+ let resolved = await resolveAttachmentRef(sessionId, attachmentId);
7632
+ if (!resolved) return error(res, 404, "attachment-not-found");
7499
7633
  try {
7500
- const value = await apiRpc("session.attachment", { sessionId, attachmentId }, 30_000);
7501
- const ref = value?.attachment;
7502
- if (!ref || typeof value?.data !== "string" || value.data === "") return error(res, 404, "attachment-not-found");
7634
+ let value;
7635
+ try {
7636
+ // 新 DSH schema:{sessionId, attachment:{attachmentId,mediaType,bytes,width,height,name?}}
7637
+ value = await apiRpc("session.attachment", { sessionId, attachment: resolved.ref }, 30_000);
7638
+ } catch (err) {
7639
+ // 缓存可能过期/宿主侧引用已变化:清掉缓存重扫一次,仍失败才按原错误返回。
7640
+ if (!resolved.cached) throw err;
7641
+ attachmentRefCache.delete(resolved.key);
7642
+ resolved = await resolveAttachmentRef(sessionId, attachmentId);
7643
+ if (!resolved) return error(res, 404, "attachment-not-found");
7644
+ value = await apiRpc("session.attachment", { sessionId, attachment: resolved.ref }, 30_000);
7645
+ }
7646
+ // 响应仍带 attachment 与 base64 data;优先信宿主回显的 ref,缺失时用请求 ref。
7647
+ const ref = value?.attachment && typeof value.attachment === "object" ? value.attachment : resolved.ref;
7648
+ if (typeof value?.data !== "string" || value.data === "") return error(res, 404, "attachment-not-found");
7503
7649
  const buf = Buffer.from(value.data, "base64");
7650
+ if (buf.length === 0) return error(res, 404, "attachment-not-found");
7651
+ const contentType = ATTACHMENT_MEDIA_TYPES.includes(ref.mediaType) ? ref.mediaType : resolved.ref.mediaType;
7504
7652
  guardRes(res);
7505
7653
  res.writeHead(200, {
7506
- "content-type": typeof ref.mediaType === "string" ? ref.mediaType : "image/jpeg",
7654
+ "content-type": contentType,
7507
7655
  "content-length": buf.length,
7508
7656
  "cache-control": "private, max-age=3600",
7509
7657
  // v3.0.0(热修 04):与 sendJson 同策略——图片取完即断,不留下可被复用的半关连接
@@ -7517,6 +7665,9 @@ export function apply(ctx, config, runtime = {}) {
7517
7665
  });
7518
7666
  res.end(buf);
7519
7667
  } catch (err) {
7668
+ // 调用前 ref 已经过严格校验;宿主仍报 attachment-invalid 基本是对象已不存在,
7669
+ // 对客户端按 404 attachment-not-found 处理(不暴露存储路径/内部错误)。
7670
+ if (err?.code === "session/attachment-invalid") return error(res, 404, "attachment-not-found");
7520
7671
  return error(res, ...rpcError(err, "attachment-failed"));
7521
7672
  }
7522
7673
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marktrue/dsh-mobile-remote",
3
- "version": "3.2.4-alpha.11",
3
+ "version": "3.2.4-alpha.12",
4
4
  "description": "DSH Mobile:把 DeepSeek Harness 变成随身控制台——手机远程发消息派活、看进度、收通知、审批决策、管理会话;配套 Flutter App(当前 Android 端,iOS 未开发)",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -52,7 +52,7 @@
52
52
  "qrcode"
53
53
  ],
54
54
  "optionalDependencies": {
55
- "@marktrue/dsh-mobile-remote-app": "npm:@marktrue/dsh-mobile-remote@3.2.4-alpha.11-app"
55
+ "@marktrue/dsh-mobile-remote-app": "npm:@marktrue/dsh-mobile-remote@3.2.4-alpha.12-app"
56
56
  },
57
57
  "peerDependencies": {
58
58
  "@deepseek-ai/dsh-credentials": ">=0.1.0-rc.6 <=0.2.2 || >=0.2.0-0 <0.2.0 || >=0.2.1-0 <0.2.1 || >=0.2.2-0 <0.2.2",