Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): 点开自己发出的图片时先用本地图垫底,不再黑屏重新加载 (#5820)

Dash502da07f4b9c
diff --git a/apps/mobile/src/__tests__/imageLightboxInteraction.test.tsx b/apps/mobile/src/__tests__/imageLightboxInteraction.test.tsx
index 32cbbc5462..f4a344b677 100644
--- a/apps/mobile/src/__tests__/imageLightboxInteraction.test.tsx
+++ b/apps/mobile/src/__tests__/imageLightboxInteraction.test.tsx
@@ -1137,6 +1137,23 @@ describe("chrome toggle, loading and video pages", () => {
expect(runtime.nodes.get("message.imageLightboxAnnotateButton").disabled).toBe(false);
});
+ it("backs a sent image with its local copy from the first frame when no remote thumbnail is cached", async () => {
+ const url = "xdt-image://cache/sent.png";
+ const image = {
+ key: "sent", title: "Sent", url, previewUri: "file:///sent-local.jpg",
+ payload: { kind: "media", media: { kind: "image", url, previewable: false } },
+ } as ImageLightboxProps["images"][number];
+ // 气泡用的是本地图,从没取过远程缩略图:缓存查询必然落空,原图仍在路上。
+ const resolver = vi.fn((media: { thumbnail?: boolean }) => (media.thumbnail
+ ? Promise.reject(new Error("not cached"))
+ : new Promise(() => undefined)));
+ await act(async () => {
+ mount({ images: [image], initialUrl: url, onResolveRemoteMedia: resolver as never });
+ });
+ expect(runtime.nodes.get("message.imageLightboxPreviewLayer").source.uri).toBe("file:///sent-local.jpg");
+ expect(runtime.nodes.has("message.imageLightboxRetryButton")).toBe(false);
+ });
+
it("unlocks paging when the original fails after the placeholder was zoomed", async () => {
const url = "xdt-image://cache/broken.png";
const image = {
diff --git a/apps/mobile/src/__tests__/messageGallery.test.ts b/apps/mobile/src/__tests__/messageGallery.test.ts
index 2189541f60..b12e211152 100644
--- a/apps/mobile/src/__tests__/messageGallery.test.ts
+++ b/apps/mobile/src/__tests__/messageGallery.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it } from 'vitest';
+import { describe, expect, it, vi } from 'vitest';
import {
collectMobileMessageGalleryImages,
galleryImageIndexForPayload,
@@ -10,6 +10,12 @@ import type { RemoteMessage } from '@/session/types';
import { mobileMarkdownImageUrlForWorkdir, mobileMarkdownManagedImagePreviewUrl } from '@/session/messageMarkdown';
import { buildMediaPayload } from '@/session/messagePayload';
+const durableThumbs = vi.hoisted(() => new Map<string, string>());
+vi.mock('@/session/sentAttachmentThumbStore', async (importOriginal) => ({
+ ...(await importOriginal<typeof import('@/session/sentAttachmentThumbStore')>()),
+ getSentAttachmentThumbUri: (ref: string) => durableThumbs.get(ref) ?? null,
+}));
+
function messageItem(key: string, message: Partial<MobileMessageRenderItem & { message: unknown }>['message']): MobileMessageRenderItem {
return {
type: 'message',
@@ -432,4 +438,41 @@ describe('message gallery', () => {
'https://example.com/other.png',
]);
});
+ it('carries the sent local image as the viewer backdrop once the host has rewritten the url', () => {
+ const calls: unknown[][] = [];
+ const gallery = collectMobileMessageGalleryImages([
+ messageItem('m-sent', { attachments: [
+ { kind: 'file', name: 'notes.md', path: '/repo/notes.md', previewable: false },
+ { kind: 'image', name: 'a.png', uri: 'xdt-image://cache/a.png', previewable: false },
+ { kind: 'image', name: 'b.png', uri: 'xdt-image://cache/b.png', previewable: false, sha256: 'sha-b' },
+ ] }),
+ ], undefined, undefined, undefined, (...args) => {
+ calls.push(args);
+ return args[1] === 1
+ ? { attachmentId: 'att-b', name: 'b.png', sourceRef: 'cindy-oss-attach://b', sha256: 'sha-b', uri: 'file:///local/b.jpg' }
+ : undefined;
+ });
+ // 预览按「第几张图片附件」对位,与气泡 AttachmentStrip 同口径(文件附件不占位)。
+ expect(calls.map((args) => args.slice(0, 3))).toEqual([['m-sent', 0, 'a.png'], ['m-sent', 1, 'b.png']]);
+ expect(gallery.map((image) => [image.url, image.previewUri])).toEqual([
+ ['xdt-image://cache/a.png', undefined],
+ ['xdt-image://cache/b.png', 'file:///local/b.jpg'],
+ ]);
+ });
+
+ it('prefers the persisted local copy over the outbox file that is cleaned up after delivery', () => {
+ durableThumbs.set('cindy-oss-attach://b', 'file:///documents/sent-attachment-thumbs/b.jpg');
+ try {
+ const gallery = collectMobileMessageGalleryImages([
+ messageItem('m-sent', { attachments: [
+ { kind: 'image', name: 'b.png', uri: 'xdt-image://cache/b.png', previewable: false },
+ ] }),
+ ], undefined, undefined, undefined, () => (
+ { attachmentId: 'att-b', name: 'b.png', sourceRef: 'cindy-oss-attach://b', uri: 'file:///outbox/b.jpg' }
+ ));
+ expect(gallery[0].previewUri).toBe('file:///documents/sent-attachment-thumbs/b.jpg');
+ } finally {
+ durableThumbs.clear();
+ }
+ });
});
diff --git a/apps/mobile/src/session/ImageLightbox.tsx b/apps/mobile/src/session/ImageLightbox.tsx
index 422d7a22ff..f272192b37 100644
--- a/apps/mobile/src/session/ImageLightbox.tsx
+++ b/apps/mobile/src/session/ImageLightbox.tsx
@@ -546,12 +546,13 @@ export const ImageLightbox = memo(function ImageLightbox({
}, [resolveMap]);
/**
- * 该页的垫底缩略图。与原图地址相同时返回 null:磁盘缓存命中会让缩略图与
- * 原图落到同一个 file://(缩不动的图按裸键落盘),此时垫同一张图纯属多解码
- * 一份,且原图本就秒出。
+ * 该页的垫底缩略图。手机本地已有的同图(自己发出的图)优先:同步可用,打开第一帧
+ * 就有画面;其次是列表已取过的远程缩略图。与原图地址相同时返回 null:磁盘缓存
+ * 命中会让缩略图与原图落到同一个 file://(缩不动的图按裸键落盘),此时垫同一张图
+ * 纯属多解码一份,且原图本就秒出。
*/
const previewUriFor = useCallback((image: MobileMessageGalleryImage): string | null => {
- const preview = previewMap[image.url];
+ const preview = image.previewUri ?? previewMap[image.url];
if (!preview) return null;
return preview === displayUriFor(image) ? null : preview;
}, [previewMap, displayUriFor]);
diff --git a/apps/mobile/src/session/MessageRenderer.tsx b/apps/mobile/src/session/MessageRenderer.tsx
index ffaf57fd50..fac75a677b 100644
--- a/apps/mobile/src/session/MessageRenderer.tsx
+++ b/apps/mobile/src/session/MessageRenderer.tsx
@@ -1677,6 +1677,7 @@ export function MessageRenderer({
chatFilePathContext?.workdir,
chatFilePathContext?.remoteHostId,
chatFilePathContext?.sessionId,
+ getSentImagePreview,
)
: null),
// eslint-disable-next-line react-hooks/exhaustive-deps -- sentThumbsVersion 是 collect 内部读的全局 store 的失效信号
@@ -1684,6 +1685,7 @@ export function MessageRenderer({
chatFilePathContext?.remoteHostId,
chatFilePathContext?.sessionId,
chatFilePathContext?.workdir,
+ getSentImagePreview,
imageLightboxOpen,
listData,
sentThumbsVersion,
@@ -1702,7 +1704,8 @@ export function MessageRenderer({
return p.key === n.key
&& p.url === n.url
&& p.payload.media.url === n.payload.media.url
- && p.payload.media.previewable === n.payload.media.previewable;
+ && p.payload.media.previewable === n.payload.media.previewable
+ && p.previewUri === n.previewUri;
})) {
return prev;
}
diff --git a/apps/mobile/src/session/messageGallery.ts b/apps/mobile/src/session/messageGallery.ts
index e391681d62..05a3154f6b 100644
--- a/apps/mobile/src/session/messageGallery.ts
+++ b/apps/mobile/src/session/messageGallery.ts
@@ -9,7 +9,8 @@ import type { MessagePayload } from '@/session/messagePayload';
import { buildAttachmentPayload, buildMediaPayload, payloadMediaKindLabel } from '@/session/messagePayload';
import type { MobileMessageRenderItem, MobileWorkChildItem } from '@/session/messageRenderModel';
import { logUnhandledRenderItem } from '@/session/assertNever';
-import { applySentAttachmentThumbOverlay } from '@/session/sentAttachmentThumbStore';
+import { applySentAttachmentThumbOverlay, getSentAttachmentThumbUri } from '@/session/sentAttachmentThumbStore';
+import type { GetSentMessageImagePreview } from '@/session/sentMessageImagePreviews';
import { i18n } from '@/i18n';
import { svgAttachmentForDisplay } from '@/session/messageAttachments';
@@ -30,6 +31,11 @@ export interface MobileMessageGalleryImage {
* 非聊天来源(composer 托盘 / 文件浏览器)不填,调用方自行决定集合范围。
*/
groupKey?: string;
+ /**
+ * 手机本地已有的同图(发送时留下的预览 / 本地缩略兜底),即气泡正在显示的那张。
+ * 查看器拿它从打开那一刻垫底,原图取到前不再黑屏转圈。
+ */
+ previewUri?: string;
}
/** 可进全屏查看器翻页的媒体:图片与视频;音频仍走通用查看器。 */
@@ -42,6 +48,7 @@ export function collectMobileMessageGalleryImages(
workdir?: string,
remoteHostId?: string,
sessionId?: string,
+ getSentImagePreview?: GetSentMessageImagePreview,
): MobileMessageGalleryImage[] {
const images: MobileMessageGalleryImage[] = [];
// 去重按「组内」进行(键 = groupKey + url):翻页组是单条消息,跨消息出现的
@@ -51,13 +58,18 @@ export function collectMobileMessageGalleryImages(
// 当前顶层 render item 的 key:一条消息(气泡 / 一轮 work_group)= 一个翻页组。
let currentGroupKey = '';
- const push = (key: string, title: string, payload: Extract<MessagePayload, { kind: 'media' }>) => {
+ const push = (
+ key: string,
+ title: string,
+ payload: Extract<MessagePayload, { kind: 'media' }>,
+ previewUri?: string | null,
+ ) => {
const url = payload.media.url.trim();
if (!url || !isLightboxMediaKind(payload.media.kind)) return;
const dedupeKey = `${currentGroupKey}::${url}`;
if (seenUrls.has(dedupeKey)) return;
seenUrls.add(dedupeKey);
- images.push({ key, title, url, payload, groupKey: currentGroupKey });
+ images.push({ key, title, url, payload, groupKey: currentGroupKey, ...(previewUri ? { previewUri } : {}) });
};
// includeBodyImages: 只有 body 真正经 MarkdownBody 渲染的 message 气泡才收正文 Markdown 图片;
@@ -69,15 +81,30 @@ export function collectMobileMessageGalleryImages(
);
const visitMessage = (message: NormalizedRemoteMessage, keyPrefix: string, includeBodyImages: boolean) => {
+ const clientId = message.source.clientId ?? message.source.id;
+ // 与 AttachmentStrip 同口径:本地发送预览按「第几张图片附件」对位。
+ let imageIndex = -1;
message.attachments?.forEach((rawAttachment, index) => {
const displayAttachment = svgAttachmentForDisplay(rawAttachment, workdir, message.key, remoteHostId, sessionId);
+ if (displayAttachment.kind === 'image') imageIndex += 1;
// 图片附件,以及按 MIME 识别成视频的文件附件(AttachmentStrip 点开同样是 media payload)。
if (displayAttachment.kind === 'image' && !displayAttachment.uri) return;
// 与 AttachmentStrip 同源 overlay(cindy-oss-attach:// → 本地缩略兜底):气泡
// 点开时 initialUrl 是替换后的 file://,图集条目必须同步替换才能匹配进翻页组。
const attachment = applySentAttachmentThumbOverlay(displayAttachment);
const payload = buildAttachmentPayload(attachment);
- if (payload.kind === 'media') push(`${keyPrefix}:attachment:${index}`, attachment.name, payload);
+ if (payload.kind !== 'media') return;
+ // 与气泡 MediaPreview 同源取本地图:被控端物化后 url 已换成远程引用,
+ // 但手机本地仍留着发送时的那张,查看器用它垫底。持久副本优先:发送预览多指向
+ // 发件箱上传文件,消息送达后即被清理(气泡靠加载失败再换持久副本兜住);
+ // 只有持久副本尚未写好的发送窗口才用它。
+ const localPreview = attachment.kind === 'image' && clientId
+ ? getSentImagePreview?.(clientId, imageIndex, displayAttachment.name, undefined, displayAttachment.sha256, displayAttachment.uri)
+ : undefined;
+ const previewUri = attachment.kind === 'image' && displayAttachment.uri
+ ? getSentAttachmentThumbUri(localPreview?.sourceRef ?? displayAttachment.uri) ?? localPreview?.uri
+ : null;
+ push(`${keyPrefix}:attachment:${index}`, attachment.name, payload, previewUri);
});
message.media?.forEach((media, index) => pushMedia(media, `${keyPrefix}:media:${index}`));
// 正文 Markdown 图片(![](url) / 安全 <img>)也纳入图集,点开后可与附件图片一起横滑翻页。