Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(mobile): 图片标注修复失败重试、误触、崩溃等问题并改用共享核心
Dashc4baf892e5e6
diff --git a/apps/mobile/app/files/[sessionId].tsx b/apps/mobile/app/files/[sessionId].tsxindex ea91041de6..a7d269c11e 100644--- a/apps/mobile/app/files/[sessionId].tsx+++ b/apps/mobile/app/files/[sessionId].tsx@@ -549,6 +549,9 @@ export default function RemoteFileBrowserScreen() {displayUri,strokes,mimeType: context.mimeType,+ ...(context.naturalWidth && context.naturalHeight+ ? { naturalWidth: context.naturalWidth, naturalHeight: context.naturalHeight }+ : {}),});setLightbox(null);router.navigate({diff --git a/apps/mobile/app/sessions/[sessionId].tsx b/apps/mobile/app/sessions/[sessionId].tsxindex a7b0954430..8116030a15 100644--- a/apps/mobile/app/sessions/[sessionId].tsx+++ b/apps/mobile/app/sessions/[sessionId].tsx@@ -3938,6 +3938,7 @@ export default function SessionScreen() {submission.displayUri,submission.strokes,submission.mimeType,+ { naturalWidth: submission.naturalWidth, naturalHeight: submission.naturalHeight },);} catch {// 失败(槽满 / 读源失败 / 烧录失败,Alert 已由标注管线弹出)回投@@ -5444,6 +5445,7 @@ export default function SessionScreen() {// 生效,读 state 会拿到「入队前」旧值绕过上限(review P1)。getRemainingAttachmentSlots: () =>MOBILE_MAX_ATTACHMENTS - attachmentsRef.current.length - getPendingUploadCount(),+ getAttachment: (attachmentId) => attachmentsRef.current.find((item) => item.id === attachmentId),});composerAnnotationsRef.current = composerAnnotations;diff --git a/apps/mobile/app/sessions/new.tsx b/apps/mobile/app/sessions/new.tsxindex 750315fa8c..c545d8f25d 100644--- a/apps/mobile/app/sessions/new.tsx+++ b/apps/mobile/app/sessions/new.tsx@@ -3972,6 +3972,7 @@ export default function NewRemoteSessionScreen() {// 生效,读 state 会拿到「入队前」旧值绕过上限(review P1)。getRemainingAttachmentSlots: () =>MOBILE_MAX_ATTACHMENTS - attachmentsRef.current.length - getPendingUploadCount(),+ getAttachment: (attachmentId) => attachmentsRef.current.find((item) => item.id === attachmentId),});composerAnnotationsRef.current = composerAnnotations;diff --git a/apps/mobile/src/__tests__/annotationBurnInQueue.test.ts b/apps/mobile/src/__tests__/annotationBurnInQueue.test.tsnew file mode 100644index 0000000000..62310b3344--- /dev/null+++ b/apps/mobile/src/__tests__/annotationBurnInQueue.test.ts@@ -0,0 +1,231 @@+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import {+ BURN_IN_TIMEOUT_MS,+ WEBVIEW_READY_TIMEOUT_MS,+ createAnnotationBurnInQueue,+ type AnnotationBurnInQueue,+} from '@/session/annotationBurnInQueue';++interface FakeHost {+ mounted: number | null;+ mounts: number[];+ unmounts: number;+ injected: Array<{ key: number; request: Record<string, unknown> }>;+ injectOk: boolean;+}++function setup(): { queue: AnnotationBurnInQueue; host: FakeHost } {+ const host: FakeHost = { mounted: null, mounts: [], unmounts: 0, injected: [], injectOk: true };+ const queue = createAnnotationBurnInQueue({+ mount: (key) => {+ host.mounted = key;+ host.mounts.push(key);+ },+ unmount: () => {+ host.mounted = null;+ host.unmounts += 1;+ },+ inject: (key, script) => {+ if (!host.injectOk || host.mounted !== key) return false;+ const json = script.slice('window.__xdtBurnIn('.length, script.lastIndexOf('); true;'));+ host.injected.push({ key, request: JSON.parse(json) as Record<string, unknown> });+ return true;+ },+ });+ return { queue, host };+}++const input = { base64: 'QUJD', mimeType: 'image/png', strokes: [{ points: [{ x: 0.1, y: 0.2 }] }] };+const ready = (queue: AnnotationBurnInQueue, key: number) =>+ queue.handleMessage(key, JSON.stringify({ ready: true }));+const reply = (queue: AnnotationBurnInQueue, key: number, id: unknown, extra: Record<string, unknown> = {}) =>+ queue.handleMessage(key, JSON.stringify({+ id, ok: true, base64: 'b3V0', mimeType: 'image/png', width: 10, height: 20, ...extra,+ }));++/** 让 promise 的落定回调跑完。 */+async function settled<T>(promise: Promise<T>): Promise<{ value?: T; error?: Error }> {+ let outcome: { value?: T; error?: Error } | null = null;+ promise.then((value) => { outcome = { value }; }, (error: Error) => { outcome = { error }; });+ await Promise.resolve();+ await Promise.resolve();+ if (!outcome) throw new Error('promise still pending');+ return outcome;+}+async function isPending(promise: Promise<unknown>): Promise<boolean> {+ let done = false;+ promise.then(() => { done = true; }, () => { done = true; });+ await Promise.resolve();+ await Promise.resolve();+ return !done;+}++beforeEach(() => { vi.useFakeTimers(); });+afterEach(() => { vi.useRealTimers(); });++describe('annotation burn-in queue', () => {+ it('mounts on demand, dispatches after ready, and unmounts when idle', async () => {+ const { queue, host } = setup();+ const job = queue.burnIn({ ...input, strokeSpace: { width: 4032, height: 3024 } });+ expect(host.mounts).toEqual([1]);+ expect(host.injected).toHaveLength(0); // 未 ready 不注入+ ready(queue, 1);+ expect(host.injected).toHaveLength(1);+ expect(host.injected[0].request).toMatchObject({+ id: 'burn-1',+ mimeType: 'image/png',+ strokeSpace: { width: 4032, height: 3024 },+ });+ reply(queue, 1, 'burn-1');+ expect((await settled(job)).value).toEqual({+ base64: 'b3V0', mimeType: 'image/png', width: 10, height: 20,+ });+ expect(host.mounted).toBeNull();+ expect(host.unmounts).toBe(1);+ });++ it('remounts with a fresh key after an idle unmount so readiness is always re-established', async () => {+ const { queue, host } = setup();+ const first = queue.burnIn(input);+ ready(queue, 1);+ reply(queue, 1, 'burn-1');+ await settled(first);+ // 卸载后立刻再来一张:宿主可能把 unmount + mount 批处理合并,新 key 保证真实重挂载。+ const second = queue.burnIn(input);+ expect(host.mounts).toEqual([1, 2]);+ expect(host.injected).toHaveLength(1); // 旧 ready 不复用+ ready(queue, 1); // 旧实例的迟到 ready 被丢弃+ expect(host.injected).toHaveLength(1);+ ready(queue, 2);+ expect(host.injected.at(-1)?.key).toBe(2);+ reply(queue, 2, 'burn-2');+ expect((await settled(second)).value?.base64).toBe('b3V0');+ });++ it('fails queued jobs when the webview never becomes ready, then retries on a new mount', async () => {+ const { queue, host } = setup();+ const a = queue.burnIn(input);+ const b = queue.burnIn(input);+ vi.advanceTimersByTime(WEBVIEW_READY_TIMEOUT_MS - 1);+ expect(await isPending(a)).toBe(true);+ vi.advanceTimersByTime(1);+ expect((await settled(a)).error?.message).toMatch(/failed to initialize/);+ expect((await settled(b)).error?.message).toMatch(/failed to initialize/);+ expect(host.mounted).toBeNull();+ const c = queue.burnIn(input);+ expect(host.mounts).toEqual([1, 2]);+ ready(queue, 2);+ reply(queue, 2, 'burn-3');+ expect((await settled(c)).value).toBeDefined();+ });++ it('times out only the running job and continues the queue on a fresh webview', async () => {+ const { queue, host } = setup();+ const a = queue.burnIn(input);+ const b = queue.burnIn(input);+ ready(queue, 1);+ expect(host.injected.map((i) => i.request.id)).toEqual(['burn-1']);+ // 排队任务不计时:第一张跑满预算前 b 仍在等。+ vi.advanceTimersByTime(BURN_IN_TIMEOUT_MS - 1);+ expect(await isPending(b)).toBe(true);+ vi.advanceTimersByTime(1);+ expect((await settled(a)).error?.message).toMatch(/timed out/);+ expect(host.mounts).toEqual([1, 2]); // 卡住的实例被换掉+ reply(queue, 1, 'burn-1'); // 超时后到达的旧回包丢弃+ expect(await isPending(b)).toBe(true);+ ready(queue, 2);+ expect(host.injected.at(-1)).toMatchObject({ key: 2, request: { id: 'burn-2' } });+ reply(queue, 2, 'burn-2');+ expect((await settled(b)).value).toBeDefined();+ });++ it('fails the running job immediately when the webview process dies and remounts for the rest', async () => {+ const { queue, host } = setup();+ const a = queue.burnIn(input);+ const b = queue.burnIn(input);+ ready(queue, 1);+ queue.handleProcessGone(1, 'render process gone');+ expect((await settled(a)).error?.message).toMatch(/terminated: render process gone/);+ expect(await isPending(b)).toBe(true);+ expect(host.mounts).toEqual([1, 2]);+ queue.handleProcessGone(1, 'late event from old instance'); // 旧 key 忽略+ expect(await isPending(b)).toBe(true);+ ready(queue, 2);+ reply(queue, 2, 'burn-2');+ expect((await settled(b)).value).toBeDefined();+ // 没有剩余任务时不再保持挂载。+ expect(host.mounted).toBeNull();+ // 崩溃后仍未派发前的任务也会被消耗,反复崩溃不会无限重挂载。+ const c = queue.burnIn(input);+ queue.handleProcessGone(3, 'crash before ready');+ expect((await settled(c)).error).toBeDefined();+ expect(host.mounted).toBeNull();+ });++ it('treats an unavailable webview at dispatch time like a crash instead of waiting for the timeout', async () => {+ const { queue, host } = setup();+ host.injectOk = false;+ const a = queue.burnIn(input);+ ready(queue, 1);+ expect((await settled(a)).error?.message).toMatch(/webview unavailable/);+ });++ it('ignores stale or foreign responses', async () => {+ const { queue } = setup();+ const a = queue.burnIn(input);+ ready(queue, 1);+ reply(queue, 1, 'burn-999');+ queue.handleMessage(1, 'not json');+ queue.handleMessage(1, JSON.stringify({ foo: 1 }));+ expect(await isPending(a)).toBe(true);+ queue.handleMessage(1, JSON.stringify({ id: 'burn-1', ok: false, error: 'image decode failed' }));+ expect((await settled(a)).error?.message).toMatch(/image decode failed/);+ });++ it('rejects everything on unmount and can be used again afterwards', async () => {+ const { queue, host } = setup();+ const a = queue.burnIn(input);+ const b = queue.burnIn(input);+ ready(queue, 1);+ queue.dispose();+ expect((await settled(a)).error?.message).toMatch(/host unmounted/);+ expect((await settled(b)).error?.message).toMatch(/host unmounted/);+ vi.advanceTimersByTime(BURN_IN_TIMEOUT_MS * 2); // 计时器已清理,不会二次落定+ const c = queue.burnIn(input);+ expect(host.mounts.at(-1)).toBe(2);+ ready(queue, 2);+ reply(queue, 2, 'burn-3');+ expect((await settled(c)).value).toBeDefined();+ });++ it('keeps a prewarmed webview mounted across jobs and unmounts after release', async () => {+ const { queue, host } = setup();+ const release = queue.acquireWarm();+ expect(host.mounts).toEqual([1]);+ // 预热期间不计 ready 超时。+ vi.advanceTimersByTime(WEBVIEW_READY_TIMEOUT_MS * 3);+ expect(host.mounted).toBe(1);+ ready(queue, 1);+ const a = queue.burnIn(input);+ expect(host.injected).toHaveLength(1); // 已 ready,立即派发+ reply(queue, 1, 'burn-1');+ await settled(a);+ expect(host.mounted).toBe(1); // 仍在标注模式:不卸载+ release();+ release(); // 幂等+ expect(host.mounted).toBeNull();+ expect(host.unmounts).toBe(1);+ });++ it('keeps an in-flight job alive when the prewarm holder releases early', async () => {+ const { queue, host } = setup();+ const release = queue.acquireWarm();+ ready(queue, 1);+ const a = queue.burnIn(input);+ release();+ expect(host.mounted).toBe(1);+ reply(queue, 1, 'burn-1');+ expect((await settled(a)).value).toBeDefined();+ expect(host.mounted).toBeNull();+ });+});diff --git a/apps/mobile/src/__tests__/imageAnnotationModel.test.ts b/apps/mobile/src/__tests__/imageAnnotationModel.test.tsindex 7ca0ca17c8..2f109fd42a 100644--- a/apps/mobile/src/__tests__/imageAnnotationModel.test.ts+++ b/apps/mobile/src/__tests__/imageAnnotationModel.test.ts@@ -1,6 +1,19 @@import { describe, expect, it } from 'vitest';+import {+ ANNOTATION_CANVAS_SCRIPT,+ annotationStrokeWidth as sharedAnnotationStrokeWidth,+} from '@cindy/maker-shared/image-annotation';import {ANNOTATION_MAX_BURN_DIMENSION,+ ANNOTATION_MIN_POINT_SCREEN_PX,+ ANNOTATION_MULTI_TOUCH_DISCARD_SCREEN_PX,+ MIN_POINT_DISTANCE_RATIO,+ annotationBurnCanvasSize,+ annotationMinPointDistanceForRect,+ annotationStrokesEqual,+ isAnnotationBurnSourceResultUsable,+ planAnnotationBurnSource,+ shouldDiscardInterruptedStroke,ANNOTATION_OUTLINE_COLOR,ANNOTATION_STROKE_COLOR,annotationBaseRect,@@ -91,12 +104,115 @@ describe('shouldAppendAnnotationPoint(点距抑制)', () => {});});-describe('annotationStrokeWidth(与桌面同公式:0.5% 短边,4~24px)', () => {+describe('annotationStrokeWidth(共享核心公式)', () => {it('小图走下限、大图走上限、中等图按比例', () => {expect(annotationStrokeWidth(200, 200)).toBe(4);expect(annotationStrokeWidth(10000, 10000)).toBe(24);expect(annotationStrokeWidth(2000, 3000)).toBe(10);});++ it('是共享核心的薄 re-export(超长截图同样加粗)', () => {+ expect(annotationStrokeWidth).toBe(sharedAnnotationStrokeWidth);+ expect(annotationStrokeWidth(1000, 8000)).toBe(sharedAnnotationStrokeWidth(1000, 8000));+ });+});++describe('normalizeAnnotationPoint 量化(共享核心)', () => {+ it('坐标量化到 4 位小数', () => {+ expect(normalizeAnnotationPoint(1, 0, { left: 0, top: 0, width: 3, height: 1 })).toEqual({ x: 0.3333, y: 0 });+ });+});++describe('annotationMinPointDistanceForRect(屏幕像素阈值)', () => {+ it('按显示矩形长边把 1.5 屏幕像素换算成归一化距离', () => {+ expect(annotationMinPointDistanceForRect({ width: 400, height: 300 }))+ .toBeCloseTo(ANNOTATION_MIN_POINT_SCREEN_PX / 400);+ // 放大 4 倍时阈值随之变细,保住放大作画的精度。+ expect(annotationMinPointDistanceForRect({ width: 1600, height: 1200 })!)+ .toBeLessThan(MIN_POINT_DISTANCE_RATIO);+ });++ it('矩形非法时退回共享默认阈值(undefined)', () => {+ expect(annotationMinPointDistanceForRect({ width: 0, height: 0 })).toBeUndefined();+ expect(shouldAppendAnnotationPoint(+ { points: [{ x: 0.5, y: 0.5 }] },+ { x: 0.5015, y: 0.5 },+ annotationMinPointDistanceForRect({ width: 0, height: 0 }),+ )).toBe(false);+ });+});++describe('shouldDiscardInterruptedStroke(捏合起手误触)', () => {+ it('手势被第二根手指取消且路径很短才丢弃', () => {+ expect(shouldDiscardInterruptedStroke(false, 0)).toBe(true);+ expect(shouldDiscardInterruptedStroke(false, ANNOTATION_MULTI_TOUCH_DISCARD_SCREEN_PX - 0.1)).toBe(true);+ expect(shouldDiscardInterruptedStroke(false, ANNOTATION_MULTI_TOUCH_DISCARD_SCREEN_PX)).toBe(false);+ // 单指点按 / 正常结束的笔画永远保留。+ expect(shouldDiscardInterruptedStroke(true, 0)).toBe(false);+ });+});++describe('annotationStrokesEqual', () => {+ it('逐条按引用比较', () => {+ const a = { points: [{ x: 0, y: 0 }] };+ const b = { points: [{ x: 0, y: 0 }] };+ expect(annotationStrokesEqual([a], [a])).toBe(true);+ expect(annotationStrokesEqual([], [])).toBe(true);+ expect(annotationStrokesEqual([a], [b])).toBe(false);+ expect(annotationStrokesEqual([a], [])).toBe(false);+ });+});++describe('planAnnotationBurnSource(烧录前预处理)', () => {+ it('小图 / 尺寸未知:原样交给 WebView(既有路径)', () => {+ expect(planAnnotationBurnSource({ mimeType: 'image/jpeg', platformOS: 'ios', naturalWidth: 2048, naturalHeight: 1536 })).toBeNull();+ expect(planAnnotationBurnSource({ mimeType: 'image/png', platformOS: 'android' })).toBeNull();+ });++ it('超大 JPEG 按上传口径预缩,中间产物保持 JPEG 最高质量,笔迹空间 = 未预缩时的 canvas', () => {+ expect(planAnnotationBurnSource({ mimeType: 'image/jpeg', platformOS: 'ios', naturalWidth: 4032, naturalHeight: 3024 })).toEqual({+ resize: { width: 2048 },+ format: 'jpeg',+ compress: 1,+ strokeSpace: { width: 4032, height: 3024 },+ });+ // 超过 4096 的图,笔迹空间与既有 WebView 安全钳一致。+ expect(planAnnotationBurnSource({ mimeType: 'image/jpeg', platformOS: 'ios', naturalWidth: 6000, naturalHeight: 8000 })?.strokeSpace)+ .toEqual(annotationBurnCanvasSize(6000, 8000));+ expect(annotationBurnCanvasSize(6000, 8000)).toEqual({ width: 3072, height: 4096 });+ });++ it('PNG / WebP 截图预缩为无损 PNG(不改输出格式)', () => {+ expect(planAnnotationBurnSource({ mimeType: 'image/png', platformOS: 'ios', naturalWidth: 1290, naturalHeight: 2796 }))+ .toMatchObject({ resize: { height: 2048 }, format: 'png' });+ expect(planAnnotationBurnSource({ mimeType: 'image/webp', platformOS: 'android', naturalWidth: 3000, naturalHeight: 1000 }))+ .toMatchObject({ resize: { width: 2048 }, format: 'png' });+ });++ it('Android 上 HEIC / AVIF 即使是小图也先转码为 PNG;iOS 小图维持原路径', () => {+ expect(planAnnotationBurnSource({ mimeType: 'image/heic', platformOS: 'android', naturalWidth: 800, naturalHeight: 600 }))+ .toEqual({ resize: null, format: 'png', compress: 1, strokeSpace: null });+ expect(planAnnotationBurnSource({ mimeType: 'image/avif', platformOS: 'android' }))+ .toMatchObject({ resize: null, format: 'png' });+ expect(planAnnotationBurnSource({ mimeType: 'image/heic', platformOS: 'ios', naturalWidth: 800, naturalHeight: 600 })).toBeNull();+ });++ it('未知 / 矢量 / 动图不交给 manipulator', () => {+ for (const mimeType of ['image/svg+xml', 'image/gif', 'image/bmp', 'application/octet-stream']) {+ expect(planAnnotationBurnSource({ mimeType, platformOS: 'android', naturalWidth: 9000, naturalHeight: 9000 })).toBeNull();+ }+ });++ it('预缩产物必须与提示尺寸同宽高比、不超上限,否则回退原路径', () => {+ const plan = planAnnotationBurnSource({ mimeType: 'image/jpeg', platformOS: 'ios', naturalWidth: 4032, naturalHeight: 3024 })!;+ const hint = { width: 4032, height: 3024 };+ expect(isAnnotationBurnSourceResultUsable(plan, { width: 2048, height: 1536 }, hint)).toBe(true);+ expect(isAnnotationBurnSourceResultUsable(plan, { width: 2048, height: 2731 }, hint)).toBe(false); // 方向对不上+ expect(isAnnotationBurnSourceResultUsable(plan, { width: 4032, height: 3024 }, hint)).toBe(false); // 没缩+ expect(isAnnotationBurnSourceResultUsable(plan, { width: 0, height: 0 }, hint)).toBe(false);+ const transcode = planAnnotationBurnSource({ mimeType: 'image/heic', platformOS: 'android' })!;+ expect(isAnnotationBurnSourceResultUsable(transcode, { width: 800, height: 600 }, {})).toBe(true);+ });});describe('annotationStrokeToSvgPath', () => {@@ -120,6 +236,16 @@ describe('annotationStrokeToSvgPath', () => {});describe('烧录 WebView 协议', () => {+ it('HTML 内联共享核心的 canvas 脚本并调用它,不再维护本地副本', () => {+ const html = buildAnnotationBurnInHtml();+ expect(html).toContain(ANNOTATION_CANVAS_SCRIPT);+ expect(html).toContain('cindyDrawAnnotationStrokes(ctx, request.strokes');+ expect(html).not.toContain('function strokeWidthFor');+ expect(html).not.toContain('function drawPass(ctx, strokes');+ // 编码策略只在 mobileImagePreprocess 维护一份,WebView 不做任何上传定稿。+ expect(html).not.toContain('finalize');+ });+it('HTML 内嵌与模型层一致的视觉参数与安全钳', () => {const html = buildAnnotationBurnInHtml();expect(html).toContain(ANNOTATION_STROKE_COLOR);@@ -229,3 +355,81 @@ describe('annotationBurnedFileName / canAnnotateImageMime', () => {expect(canAnnotateImageMime(undefined)).toBe(true);});});++describe('烧录 WebView 脚本(fake DOM 执行)', () => {+ interface FakeRun {+ posts: Array<Record<string, unknown>>;+ ops: unknown[][];+ dataUrlCalls: Array<[string, number | undefined]>;+ }+ function runBurn(+ request: Record<string, unknown>,+ image: { width: number; height: number },+ encodedBytes: (mime: string, quality: number | undefined) => number,+ ): FakeRun {+ const run: FakeRun = { posts: [], ops: [], dataUrlCalls: [] };+ const html = buildAnnotationBurnInHtml();+ const code = [...html.matchAll(/<script>([\s\S]*?)<\/script>/g)].map((m) => m[1]).join('\n');+ const ctx = new Proxy({} as Record<string, unknown>, {+ get: (_target, key: string) => (...args: unknown[]) => { run.ops.push([key, ...args]); },+ set: (_target, key: string, value) => { run.ops.push([`set:${key}`, value]); return true; },+ });+ const canvas = {+ width: 0,+ height: 0,+ getContext: () => ctx,+ toDataURL: (mime: string, quality?: number) => {+ run.dataUrlCalls.push([mime, quality]);+ const bytes = encodedBytes(mime, quality);+ return `data:${mime};base64,${'A'.repeat(Math.ceil(bytes / 3) * 4)}`;+ },+ };+ class FakeImage {+ onload: (() => void) | null = null;+ onerror: (() => void) | null = null;+ naturalWidth = image.width;+ naturalHeight = image.height;+ width = image.width;+ height = image.height;+ set src(_value: string) { this.onload?.(); }+ }+ const window: Record<string, unknown> = {+ ReactNativeWebView: { postMessage: (raw: string) => run.posts.push(JSON.parse(raw)) },+ };+ const document = { createElement: () => canvas };+ // eslint-disable-next-line @typescript-eslint/no-implied-eval+ new Function('window', 'document', 'Image', code)(window, document, FakeImage);+ (window.__xdtBurnIn as (r: unknown) => void)(request);+ return run;+ }+ const strokes = [{ points: [{ x: 0.1, y: 0.1 }, { x: 0.5, y: 0.5 }] }];++ it('编码与既有行为一致:JPEG 源 0.92 JPEG、其余 PNG,输出 canvas 尺寸', () => {+ const jpeg = runBurn({ id: 'b1', base64: 'x', mimeType: 'image/jpeg', strokes }, { width: 4032, height: 3024 }, () => 3);+ expect(jpeg.dataUrlCalls).toEqual([['image/jpeg', 0.92]]);+ expect(jpeg.posts.at(-1)).toMatchObject({ id: 'b1', ok: true, mimeType: 'image/jpeg', width: 4032, height: 3024 });+ const png = runBurn({ id: 'b2', base64: 'x', mimeType: 'image/webp', strokes }, { width: 5000, height: 2500 }, () => 3);+ expect(png.dataUrlCalls).toEqual([['image/png', 0.92]]);+ expect(png.posts.at(-1)).toMatchObject({ mimeType: 'image/png', width: 4096, height: 2048 });+ });++ it('预缩源在未预缩的逻辑空间重放笔迹:线宽按逻辑尺寸计算,再等比缩到实际 canvas', () => {+ const run = runBurn(+ { id: 'b5', base64: 'x', mimeType: 'image/jpeg', strokes, strokeSpace: { width: 4032, height: 3024 } },+ { width: 2048, height: 1536 },+ () => 1,+ );+ const scale = run.ops.find((op) => op[0] === 'scale');+ expect(scale?.[1]).toBeCloseTo(2048 / 4032);+ expect(scale?.[2]).toBeCloseTo(1536 / 3024);+ const widths = run.ops.filter((op) => op[0] === 'set:lineWidth').map((op) => op[1]);+ const logicalWidth = annotationStrokeWidth(4032, 3024);+ expect(widths).toEqual([Math.round(logicalWidth * 1.8), logicalWidth]);+ expect(run.ops.find((op) => op[0] === 'moveTo')).toEqual(['moveTo', 0.1 * 4032, 0.1 * 3024]);+ // 不传 strokeSpace:直接在 canvas 尺寸重放(与既有行为一致)。+ const plain = runBurn({ id: 'b6', base64: 'x', mimeType: 'image/png', strokes }, { width: 800, height: 600 }, () => 1);+ expect(plain.ops.some((op) => op[0] === 'scale')).toBe(false);+ expect(plain.ops.filter((op) => op[0] === 'set:lineWidth').map((op) => op[1]))+ .toEqual([Math.round(annotationStrokeWidth(800, 600) * 1.8), annotationStrokeWidth(800, 600)]);+ });+});diff --git a/apps/mobile/src/__tests__/imageLightboxInteraction.test.tsx b/apps/mobile/src/__tests__/imageLightboxInteraction.test.tsxindex a1dca5d9cc..c6b65347f6 100644--- a/apps/mobile/src/__tests__/imageLightboxInteraction.test.tsx+++ b/apps/mobile/src/__tests__/imageLightboxInteraction.test.tsx@@ -25,6 +25,7 @@ const runtime = vi.hoisted(() => ({dimensions: { width: 400, height: 800 },insets: { top: 40, bottom: 20, left: 0, right: 0 },gesture: null as GestureNode | null,+ alerts: [] as Array<[string, string, Array<{ text: string; style?: string; onPress?: () => void }>]>,}));vi.mock("expo-router", () => ({@@ -62,6 +63,7 @@ vi.mock("react-native", async () => {Text: view("Text"),ActivityIndicator: view("ActivityIndicator"),StatusBar: () => null,+ Alert: { alert: (...args: any[]) => runtime.alerts.push(args as any) },Image: view("Image"),Platform: { OS: "android" },StyleSheet: {@@ -191,6 +193,7 @@ beforeEach(() => {runtime.immediate = false;runtime.dimensions = { width: 400, height: 800 };runtime.insets = { top: 40, bottom: 20, left: 0, right: 0 };+ runtime.alerts = [];root = createRoot(document.createElement("div"));});afterEach(() => act(() => root.unmount()));@@ -528,7 +531,11 @@ describe("image viewer actions", () => {const { props } = mount({annotation: { allowDirectSubmit: true, submitLabel: "Send", onSubmit },});- if (annotating) press("message.imageLightboxAnnotateButton");+ if (annotating) {+ // 画笔要等自然尺寸就位才可用(坐标换算基准)。+ act(() => runtime.nodes.get("Image").onLoad({ source: { width: 400, height: 800 } }));+ press("message.imageLightboxAnnotateButton");+ }const submitId = annotating? "message.imageLightboxAnnotationSubmit": "message.imageLightboxSendToChatButton";@@ -569,3 +576,159 @@ describe("image viewer actions", () => {expect(runtime.nodes.get("FlatList").scrollEnabled).toBe(true);});});++describe("annotation drawing", () => {+ const drawGesture = () => gestures().find((g) => g.kind === "Pan" && g.options.minDistance === 0)!;+ const load = () => act(() => runtime.nodes.get("Image").onLoad({ source: { width: 400, height: 800 } }));+ // 标注模式锁翻页:FlatList 每次渲染都会重新登记,比「节点是否存在」更可靠(卸载节点不会从登记表移除)。+ const annotating = () => runtime.nodes.get("FlatList").scrollEnabled === false;+ const submittedStrokes = async (onSubmit: ReturnType<typeof vi.fn>) => {+ press("message.imageLightboxAnnotationSubmit");+ await act(async () => { await Promise.resolve(); });+ return onSubmit.mock.calls.at(-1)?.[2];+ };++ it("disables the pen until the image size is known, then enters annotate mode", () => {+ mount({ annotation: { submitLabel: "Send", onSubmit: vi.fn() } });+ expect(runtime.nodes.get("message.imageLightboxAnnotateButton").disabled).toBe(true);+ press("message.imageLightboxAnnotateButton");+ expect(annotating()).toBe(false);+ load();+ expect(runtime.nodes.get("message.imageLightboxAnnotateButton").disabled).toBe(false);+ press("message.imageLightboxAnnotateButton");+ expect(annotating()).toBe(true);+ });++ it("lets the host keep the pen disabled (pending replacement)", () => {+ mount({ annotation: { submitLabel: "Save", onSubmit: vi.fn(), canAnnotate: () => false } });+ load();+ expect(runtime.nodes.get("message.imageLightboxAnnotateButton").disabled).toBe(true);+ });++ it("drops the stray dot left when a second finger starts a pinch, but keeps taps and real strokes", async () => {+ const onSubmit = vi.fn(() => new Promise<void>(() => undefined));+ mount({ annotation: { submitLabel: "Send", onSubmit } });+ load();+ press("message.imageLightboxAnnotateButton");+ // 捏合起手:第一根手指落下开始一笔,第二根手指落下让画笔手势被取消。+ fire(drawGesture(), "onStart", { x: 100, y: 200 });+ fire(drawGesture(), "onUpdate", { x: 104, y: 203 });+ fire(drawGesture(), "onFinalize", {}, false);+ expect(runtime.nodes.get("message.imageLightboxAnnotationUndo").disabled).toBe(true);+ // 单指点按(正常结束)= 圆点,保留。+ fire(drawGesture(), "onStart", { x: 200, y: 400 });+ fire(drawGesture(), "onFinalize", {}, true);+ // 画了一段后第二根手指落下:保留已画的半笔。+ fire(drawGesture(), "onStart", { x: 100, y: 100 });+ fire(drawGesture(), "onUpdate", { x: 140, y: 100 });+ fire(drawGesture(), "onFinalize", {}, false);+ const strokes = await submittedStrokes(onSubmit);+ expect(strokes).toEqual([+ { points: [{ x: 0.5, y: 0.5 }] },+ { points: [{ x: 0.25, y: 0.125 }, { x: 0.35, y: 0.125 }] },+ ]);+ });++ it("ignores drawing before the image size is known", async () => {+ const onSubmit = vi.fn(() => new Promise<void>(() => undefined));+ mount({ annotation: { submitLabel: "Send", onSubmit }, autoAnnotate: true });+ fire(drawGesture(), "onStart", { x: 100, y: 200 });+ fire(drawGesture(), "onFinalize", {}, true);+ expect(await submittedStrokes(onSubmit)).toEqual([]);+ });++ it("samples points by screen distance so zoomed drawing keeps precision", async () => {+ const onSubmit = vi.fn(() => new Promise<void>(() => undefined));+ mount({ annotation: { submitLabel: "Send", onSubmit } });+ load();+ press("message.imageLightboxAnnotateButton");+ fire(drawGesture(), "onStart", { x: 100, y: 200 });+ // 显示矩形 400×800:阈值 = 1.5 / 800(长边),纵向 1px 不记录、2px 记录。+ fire(drawGesture(), "onUpdate", { x: 100, y: 201 });+ fire(drawGesture(), "onUpdate", { x: 100, y: 202 });+ fire(drawGesture(), "onFinalize", {}, true);+ expect(await submittedStrokes(onSubmit)).toEqual([+ { points: [{ x: 0.25, y: 0.25 }, { x: 0.25, y: 0.2525 }] },+ ]);+ });++ it("asks before discarding changed strokes and exits immediately when unchanged", () => {+ mount({ annotation: { submitLabel: "Send", onSubmit: vi.fn() } });+ load();+ press("message.imageLightboxAnnotateButton");+ press("message.imageLightboxAnnotationCancel");+ expect(runtime.alerts).toHaveLength(0);+ expect(annotating()).toBe(false);++ press("message.imageLightboxAnnotateButton");+ fire(drawGesture(), "onStart", { x: 100, y: 200 });+ fire(drawGesture(), "onFinalize", {}, true);+ press("message.imageLightboxAnnotationCancel");+ expect(runtime.alerts).toHaveLength(1);+ const [title, , buttons] = runtime.alerts[0];+ expect(title).toBe("message.lightbox.discardAnnotationTitle");+ expect(buttons.map((b) => b.style)).toEqual(["cancel", "destructive"]);+ act(() => buttons[0].onPress?.()); // 继续标注+ expect(annotating()).toBe(true);+ // Android 返回键走同一确认。+ act(() => runtime.nodes.get("Modal").onRequestClose());+ expect(runtime.alerts).toHaveLength(2);+ act(() => runtime.alerts[1][2][1].onPress?.()); // 放弃+ expect(annotating()).toBe(false);+ // 笔迹已恢复:再次进入标注时没有可撤销的笔迹。+ press("message.imageLightboxAnnotateButton");+ expect(runtime.nodes.get("message.imageLightboxAnnotationUndo").disabled).toBe(true);+ });++ it("does not ask when the only change was undone back to the existing strokes", () => {+ const existing = [{ points: [{ x: 0.1, y: 0.1 }] }];+ mount({ annotation: { submitLabel: "Save", onSubmit: vi.fn(), initialStrokesFor: () => existing } });+ load();+ press("message.imageLightboxAnnotateButton");+ fire(drawGesture(), "onStart", { x: 100, y: 200 });+ fire(drawGesture(), "onFinalize", {}, true);+ press("message.imageLightboxAnnotationUndo");+ press("message.imageLightboxAnnotationCancel");+ expect(runtime.alerts).toHaveLength(0);+ expect(annotating()).toBe(false);+ });++ it("autoAnnotate opens in annotate mode and discarding closes the viewer", () => {+ const { props } = mount({ annotation: { submitLabel: "Send", onSubmit: vi.fn() }, autoAnnotate: true });+ expect(runtime.nodes.has("message.imageLightboxAnnotationCancel")).toBe(true);+ press("message.imageLightboxAnnotationCancel");+ expect(props.onClose).toHaveBeenCalledTimes(1);+ });++ it("autoAnnotate confirms before closing when strokes were drawn", () => {+ const { props } = mount({ annotation: { submitLabel: "Send", onSubmit: vi.fn() }, autoAnnotate: true });+ load();+ fire(drawGesture(), "onStart", { x: 100, y: 200 });+ fire(drawGesture(), "onFinalize", {}, true);+ act(() => runtime.nodes.get("message.imageLightbox").onAccessibilityEscape());+ expect(props.onClose).not.toHaveBeenCalled();+ act(() => runtime.alerts[0][2][1].onPress?.());+ expect(props.onClose).toHaveBeenCalledTimes(1);+ });++ it("prewarms the host while annotating and releases it on exit", () => {+ const release = vi.fn();+ const prewarm = vi.fn(() => release);+ mount({ annotation: { submitLabel: "Send", onSubmit: vi.fn(), prewarm } });+ load();+ expect(prewarm).not.toHaveBeenCalled();+ press("message.imageLightboxAnnotateButton");+ expect(prewarm).toHaveBeenCalledTimes(1);+ press("message.imageLightboxAnnotationCancel");+ expect(release).toHaveBeenCalledTimes(1);+ });++ it("passes the decoded image size with the submission", async () => {+ const onSubmit = vi.fn((..._args: unknown[]) => new Promise<void>(() => undefined));+ mount({ annotation: { submitLabel: "Send", allowDirectSubmit: true, onSubmit } });+ load();+ press("message.imageLightboxSendToChatButton");+ await act(async () => { await Promise.resolve(); });+ expect(onSubmit.mock.calls[0][3]).toMatchObject({ naturalWidth: 400, naturalHeight: 800 });+ });+});diff --git a/apps/mobile/src/__tests__/mobileLocalAttachmentUpload.test.ts b/apps/mobile/src/__tests__/mobileLocalAttachmentUpload.test.tsindex 56732a66f9..d46d24e946 100644--- a/apps/mobile/src/__tests__/mobileLocalAttachmentUpload.test.ts+++ b/apps/mobile/src/__tests__/mobileLocalAttachmentUpload.test.ts@@ -946,3 +946,89 @@ describe('in-flight 取消(abort 传递)', () => {});});++describe('onAbandoned(自生成输入文件的生命周期回收)', () => {+ it('上传失败不算放弃:失败卡可重试;移除失败卡才回调一次', async () => {+ const abandoned = vi.fn();+ let attempts = 0;+ const { deps, pendingSnapshots, uploaded } = makeDeps({+ upload: (c) => {+ attempts += 1;+ return attempts === 1 ? Promise.reject(new Error('network down')) : Promise.resolve(attachmentFor(c.name));+ },+ });+ const controller = createMobileLocalAttachmentUploadController(deps);+ controller.enqueue([{ ...candidate('a.jpg'), onAbandoned: abandoned }], { token: 't' });+ await controller.waitForIdle();+ expect(abandoned).not.toHaveBeenCalled();+ const failedCard = pendingSnapshots.at(-1)?.[0];+ controller.retry(failedCard!.localId, { token: 't2' });+ await controller.waitForIdle();+ expect(uploaded).toHaveLength(1);+ expect(abandoned).not.toHaveBeenCalled(); // 成功走 onUploaded++ const second = vi.fn();+ const failing = makeDeps({ upload: () => Promise.reject(new Error('network down')) });+ const other = createMobileLocalAttachmentUploadController(failing.deps);+ other.enqueue([{ ...candidate('b.jpg'), onAbandoned: second }], { token: 't' });+ await other.waitForIdle();+ other.remove(failing.pendingSnapshots.at(-1)![0]!.localId);+ other.remove(failing.pendingSnapshots.at(-1)?.[0]?.localId ?? 'none');+ expect(second).toHaveBeenCalledTimes(1);+ });++ it('in-flight 被移除:等任务落定后回调(不在传输读取文件期间回收)', async () => {+ const gate = gatedUpload();+ const abandoned = vi.fn();+ const { deps } = makeDeps({ upload: gate.upload });+ const controller = createMobileLocalAttachmentUploadController(deps);+ controller.enqueue([{ ...candidate('a.jpg'), onAbandoned: abandoned }], { token: 't' });+ await flush();+ controller.remove('local-attachment-upload-1');+ expect(abandoned).not.toHaveBeenCalled();+ gate.release('a.jpg');+ await flush();+ expect(abandoned).toHaveBeenCalledTimes(1);+ });++ it('removeAll / dispose / 退屏后入队 都视为放弃', async () => {+ const a = vi.fn();+ const b = vi.fn();+ const { deps } = makeDeps({ upload: () => new Promise(() => undefined) });+ const controller = createMobileLocalAttachmentUploadController(deps);+ controller.enqueue([+ { ...candidate('a.jpg'), onAbandoned: a },+ { ...candidate('b.jpg'), onAbandoned: b },+ { ...candidate('c.jpg'), onAbandoned: b },+ ], { token: 't' });+ controller.removeAll();+ await flush();+ expect(b).toHaveBeenCalled(); // 排队中的 c 立即回调+ const d = vi.fn();+ controller.dispose();+ controller.enqueue([{ ...candidate('d.jpg'), onAbandoned: d }], { token: 't' });+ expect(d).toHaveBeenCalledTimes(1);+ });++ it('交接给持久发件箱:提交后才回调(文件已复制),回滚不回调', async () => {+ const committed = vi.fn();+ const rolledBack = vi.fn();+ const { deps } = makeDeps({ upload: () => new Promise(() => undefined) });+ const controller = createMobileLocalAttachmentUploadController(deps);+ controller.enqueue([{ ...candidate('a.jpg'), onAbandoned: rolledBack }], { token: new Promise(() => undefined) });+ const first = controller.beginHandoff();+ await first.prepare();+ first.release(false);+ await flush();+ expect(rolledBack).not.toHaveBeenCalled();+ controller.removeAll();+ const other = createMobileLocalAttachmentUploadController(makeDeps({ upload: () => new Promise(() => undefined) }).deps);+ other.enqueue([{ ...candidate('b.jpg'), onAbandoned: committed }], { token: new Promise(() => undefined) });+ const handoff = other.beginHandoff();+ await handoff.prepare();+ expect(committed).not.toHaveBeenCalled();+ handoff.release(true);+ await flush();+ expect(committed).toHaveBeenCalledTimes(1);+ });+});diff --git a/apps/mobile/src/__tests__/useComposerImageAnnotations.test.tsx b/apps/mobile/src/__tests__/useComposerImageAnnotations.test.tsxnew file mode 100644index 0000000000..e041ec2c57--- /dev/null+++ b/apps/mobile/src/__tests__/useComposerImageAnnotations.test.tsx@@ -0,0 +1,347 @@+// @vitest-environment jsdom+import { act } from 'react';+import { createRoot, type Root } from 'react-dom/client';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import type { MobileLocalAttachmentUploadCandidate } from '@/session/mobileLocalAttachmentUpload';+import type { RemoteSerializedAttachment } from '@/session/types';+import {+ useComposerImageAnnotations,+ type UseComposerImageAnnotationsOptions,+ type UseComposerImageAnnotationsResult,+} from '@/session/useComposerImageAnnotations';++const runtime = vi.hoisted(() => ({+ platform: 'ios',+ deleted: [] as string[],+ written: [] as string[],+ alerts: [] as unknown[][],+ manipulations: [] as Array<{ uri: string; resize?: unknown; save?: Record<string, unknown> }>,+ manipulatorResult: null as null | { width: number; height: number },+ manipulatorError: null as Error | null,+ manipulatorSeq: 0,+ burnIn: null as unknown as ReturnType<typeof vi.fn>,+ acquireWarm: null as unknown as ReturnType<typeof vi.fn>,+}));++vi.mock('react-native', () => ({+ Alert: { alert: (...args: unknown[]) => runtime.alerts.push(args) },+ Platform: { get OS() { return runtime.platform; } },+}));+vi.mock('react-i18next', async (importOriginal) => {+ const t = (key: string) => key;+ return { ...await importOriginal<typeof import('react-i18next')>(), useTranslation: () => ({ t }) };+});+vi.mock('expo-file-system/legacy', () => ({+ cacheDirectory: 'file:///cache/',+ EncodingType: { Base64: 'base64' },+ makeDirectoryAsync: async () => undefined,+ getInfoAsync: async () => ({ exists: true, size: 1234 }),+ // 头部嗅探读不出魔数 → 以调用方给的 mime 为准。+ readAsStringAsync: async (_uri: string, options?: { length?: number }) => (options?.length ? '' : 'QUJD'),+ writeAsStringAsync: async (uri: string) => { runtime.written.push(uri); },+ copyAsync: async () => undefined,+ downloadAsync: async () => ({ status: 200 }),+ deleteAsync: async (uri: string) => { runtime.deleted.push(uri); },+}));+vi.mock('@/session/AnnotationBurnInWebView', () => ({+ useAnnotationBurnIn: () => ({ burnIn: runtime.burnIn, acquireWarm: runtime.acquireWarm, host: null }),+}));+vi.mock('expo-image-manipulator', () => ({+ SaveFormat: { JPEG: 'jpeg', PNG: 'png' },+ ImageManipulator: {+ manipulate: (uri: string) => {+ const record: { uri: string; resize?: unknown; save?: Record<string, unknown> } = { uri };+ runtime.manipulations.push(record);+ const context = {+ resize: (size: unknown) => { record.resize = size; return context; },+ renderAsync: async () => {+ if (runtime.manipulatorError) throw runtime.manipulatorError;+ return {+ release: () => undefined,+ saveAsync: async (options: Record<string, unknown>) => {+ record.save = options;+ runtime.manipulatorSeq += 1;+ const size = runtime.manipulatorResult ?? { width: 800, height: 600 };+ return { uri: `file:///cache/ImageManipulator/out-${runtime.manipulatorSeq}`, ...size };+ },+ };+ },+ release: () => undefined,+ };+ return context;+ },+ },+}));++let root: Root;+beforeEach(() => {+ Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });+ runtime.platform = 'ios';+ runtime.deleted = [];+ runtime.written = [];+ runtime.alerts = [];+ runtime.manipulations = [];+ runtime.manipulatorResult = null;+ runtime.manipulatorError = null;+ runtime.manipulatorSeq = 0;+ runtime.burnIn = vi.fn(async (input: { mimeType: string }) => ({+ base64: 'b3V0',+ mimeType: input.mimeType === 'image/jpeg' ? 'image/jpeg' : 'image/png',+ width: 800,+ height: 600,+ }));+ runtime.acquireWarm = vi.fn(() => () => undefined);+ root = createRoot(document.createElement('div'));+});+afterEach(() => {+ act(() => root.unmount());+ vi.useRealTimers();+});++function mountHook(overrides: Partial<UseComposerImageAnnotationsOptions> = {}) {+ const options: UseComposerImageAnnotationsOptions = {+ getAccessToken: vi.fn(async () => 'token'),+ enqueueUploads: vi.fn(),+ removeAttachment: vi.fn(),+ getRemainingAttachmentSlots: vi.fn(() => 5),+ getAttachment: vi.fn(() => undefined),+ ...overrides,+ };+ let latest: UseComposerImageAnnotationsResult | null = null;+ function Harness() {+ latest = useComposerImageAnnotations(options);+ return null;+ }+ act(() => root.render(<Harness />));+ const enqueued = () => (options.enqueueUploads as ReturnType<typeof vi.fn>).mock.calls+ .map((call) => (call[0] as MobileLocalAttachmentUploadCandidate[])[0]);+ return { options, api: () => latest!, enqueued };+}++const image = (key: string) => ({ key, url: key, title: key, payload: { kind: 'media', media: { kind: 'image', url: key, previewable: true } } }) as never;+const attachment = (id: string, extra: Partial<RemoteSerializedAttachment> = {}) =>+ ({ id, name: `${id}.png`, category: 'image', ...extra }) as RemoteSerializedAttachment;+const STROKES = [{ points: [{ x: 0.1, y: 0.1 }, { x: 0.4, y: 0.4 }] }];++async function chatSubmit(+ api: UseComposerImageAnnotationsResult,+ uri: string,+ strokes = STROKES,+ context: Record<string, unknown> = { mimeType: 'image/jpeg' },+) {+ await act(async () => { await api.chatAnnotation.onSubmit(image('chat'), uri, strokes, context); });+}+async function traySubmit(+ api: UseComposerImageAnnotationsResult,+ key: string,+ uri: string,+ strokes = STROKES,+ context: Record<string, unknown> = { mimeType: 'image/png' },+) {+ await act(async () => { await api.trayAnnotation.onSubmit(image(key), uri, strokes, context); });+}++describe('generated file lifecycle', () => {+ it('keeps the files of a failed upload for retry and deletes them only when the card is abandoned', async () => {+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });+ const { api, enqueued } = mountHook();+ await chatSubmit(api(), 'file:///photos/a.jpg');+ const candidate = enqueued()[0];+ expect(candidate.uri).toMatch(/^file:\/\/\/cache\/annotation-burned\/annotated-/);+ expect(candidate.annotation?.sourceUri).toMatch(/^file:\/\/\/cache\/annotation-src\/src-/);+ // 上传失败后卡片停留很久:文件不能被计时器回收(重试会重新读 candidate.uri)。+ vi.advanceTimersByTime(60 * 60 * 1000);+ expect(runtime.deleted).toEqual([]);+ candidate.onAbandoned?.();+ await act(async () => undefined);+ expect(runtime.deleted.sort()).toEqual([candidate.uri, candidate.annotation!.sourceUri].sort());+ candidate.onAbandoned?.(); // 幂等+ expect(runtime.deleted).toHaveLength(2);+ });++ it('moves files under the attachment on success; later abandonment is a no-op and removal deletes them', async () => {+ const { api, enqueued } = mountHook();+ await chatSubmit(api(), 'file:///photos/a.jpg');+ const candidate = enqueued()[0];+ const decorated = api().decorateUploadedAttachment(attachment('att-1'), candidate);+ expect(decorated.annotated).toBe(true);+ candidate.onAbandoned?.();+ expect(runtime.deleted).toEqual([]);+ act(() => api().forgetAttachment('att-1'));+ await act(async () => undefined);+ expect(runtime.deleted.sort()).toEqual([candidate.uri, candidate.annotation!.sourceUri].sort());+ });++ it('forgetAllAttachments leaves still-pending uploads to their own lifecycle', async () => {+ const { api, enqueued } = mountHook();+ await chatSubmit(api(), 'file:///photos/a.jpg');+ act(() => api().forgetAllAttachments());+ expect(runtime.deleted).toEqual([]);+ enqueued()[0].onAbandoned?.();+ expect(runtime.deleted).toHaveLength(2);+ });++ it('cleans up the private copies when the submission fails before reaching the upload queue', async () => {+ runtime.burnIn.mockRejectedValueOnce(new Error('annotation burn-in timed out'));+ const { api, options } = mountHook();+ await expect(Promise.resolve(+ api().chatAnnotation.onSubmit(image('chat'), 'file:///photos/a.jpg', STROKES, { mimeType: 'image/jpeg' }),+ )).rejects.toThrow(/timed out/);+ expect(options.enqueueUploads).not.toHaveBeenCalled();+ expect(runtime.alerts).toHaveLength(1);+ expect(runtime.deleted).toHaveLength(1);+ expect(runtime.deleted[0]).toMatch(/annotation-src\/src-/);+ });+});++describe('re-edit replacement', () => {+ async function seedAnnotated(hook: ReturnType<typeof mountHook>, id: string) {+ await chatSubmit(hook.api(), 'file:///photos/a.jpg');+ const candidate = hook.enqueued().at(-1)!;+ hook.api().decorateUploadedAttachment(attachment(id), candidate);+ return candidate;+ }++ it('blocks a second edit while the replacement is pending, then replaces in order', async () => {+ const attachments = new Map<string, RemoteSerializedAttachment>();+ const hook = mountHook({ getAttachment: (id) => attachments.get(id) });+ const original = await seedAnnotated(hook, 'A');+ attachments.set('A', attachment('A', { annotated: true }));+ expect(hook.api().trayAnnotation.canAnnotate?.(image('A'))).toBe(true);+ await traySubmit(hook.api(), 'A', hook.api().trayImageSourceUri('A', 'preview'));+ const replacement = hook.enqueued().at(-1)!;+ expect(replacement.replacesAttachmentId).toBe('A');+ // 替换未落定:画笔禁用,且直接提交也被拒绝(不会产生第二份替换)。+ expect(hook.api().trayAnnotation.canAnnotate?.(image('A'))).toBe(false);+ await expect(Promise.resolve(+ hook.api().trayAnnotation.onSubmit(image('A'), 'x', STROKES, {}),+ )).rejects.toThrow('composer.attachments.replacementPending');+ expect(hook.enqueued()).toHaveLength(2);+ // 替换上传成功:此刻才移除旧附件;共享的源副本保留,旧烧录图删除。+ const decorated = hook.api().decorateUploadedAttachment(attachment('B'), replacement);+ expect(decorated.annotated).toBe(true);+ expect(hook.options.removeAttachment).toHaveBeenCalledWith('A');+ expect(runtime.deleted).toEqual([original.uri]);+ expect(replacement.annotation?.sourceUri).toBe(original.annotation?.sourceUri);+ expect(hook.api().trayImageSourceUri('B', 'preview')).toBe(original.annotation?.sourceUri);+ });++ it('re-enables editing when a pending replacement is abandoned without touching the original files', async () => {+ const attachments = new Map<string, RemoteSerializedAttachment>();+ const hook = mountHook({ getAttachment: (id) => attachments.get(id) });+ const original = await seedAnnotated(hook, 'A');+ attachments.set('A', attachment('A', { annotated: true }));+ await traySubmit(hook.api(), 'A', hook.api().trayImageSourceUri('A', 'preview'));+ const replacement = hook.enqueued().at(-1)!;+ replacement.onAbandoned?.();+ expect(hook.api().trayAnnotation.canAnnotate?.(image('A'))).toBe(true);+ // 只删替换自己的烧录图;与 A 共享的源副本仍被 A 引用。+ expect(runtime.deleted).toEqual([replacement.uri]);+ expect(runtime.deleted).not.toContain(original.annotation?.sourceUri);+ });++ it('does not let a replacement bypass the attachment limit once its target is gone', async () => {+ const hook = mountHook({ getRemainingAttachmentSlots: () => 0, getAttachment: () => undefined });+ await expect(Promise.resolve(+ hook.api().trayAnnotation.onSubmit(image('ghost'), 'file:///photos/a.png', STROKES, { mimeType: 'image/png' }),+ )).rejects.toThrow('composer.upload.maxAttachments');+ expect(hook.options.enqueueUploads).not.toHaveBeenCalled();+ // 目标仍在:替换不占新槽位。+ const present = mountHook({ getRemainingAttachmentSlots: () => 0, getAttachment: (id) => attachment(id) });+ await traySubmit(present.api(), 'A', 'file:///photos/a.png');+ expect(present.enqueued()[0].replacesAttachmentId).toBe('A');+ });++ it('keeps the annotated flag when re-saving an already-burned image whose edit data was lost', async () => {+ const hook = mountHook({ getAttachment: (id) => attachment(id, { annotated: true }) });+ await traySubmit(hook.api(), 'R', 'file:///staged/r.png', []);+ const candidate = hook.enqueued()[0];+ expect(runtime.burnIn).not.toHaveBeenCalled();+ expect(candidate.annotation).toMatchObject({ strokes: [], baseAnnotated: true });+ expect(hook.api().decorateUploadedAttachment(attachment('R2'), candidate).annotated).toBe(true);+ // 之后再次保存(已有内存真相但底图仍是烧录图):标仍保留。+ await traySubmit(hook.api(), 'R2', hook.api().trayImageSourceUri('R2', 'preview'), []);+ const again = hook.enqueued()[1];+ expect(again.annotation).toMatchObject({ strokes: [], baseAnnotated: true });+ expect(hook.api().decorateUploadedAttachment(attachment('R3'), again).annotated).toBe(true);+ // 普通底图撤光笔迹 = 恢复原图,不打标。+ const plain = mountHook({ getAttachment: (id) => attachment(id) });+ await traySubmit(plain.api(), 'P', 'file:///staged/p.png', []);+ expect(plain.enqueued()[0].annotation).toBeUndefined();+ });+});++describe('burn source preparation', () => {+ it('transcodes HEIC with the native manipulator on Android before burning, and falls back when it fails', async () => {+ runtime.platform = 'android';+ const hook = mountHook();+ await chatSubmit(hook.api(), 'file:///photos/x.heic', [], { mimeType: 'image/heic' });+ expect(runtime.manipulations).toHaveLength(1);+ expect(runtime.manipulations[0].resize).toBeUndefined();+ expect(runtime.manipulations[0].save).toEqual({ format: 'png', compress: 1 });+ expect(runtime.burnIn).toHaveBeenLastCalledWith(expect.objectContaining({ mimeType: 'image/png', strokes: [] }));+ expect(runtime.burnIn.mock.calls[0][0]).not.toHaveProperty('strokeSpace');+ expect(runtime.deleted).toContain('file:///cache/ImageManipulator/out-1'); // 中间产物烧完即删+ expect(hook.enqueued()[0].mimeType).toBe('image/png');+ expect(hook.enqueued()[0].annotation).toBeUndefined();+ expect(hook.enqueued()[0].name).toMatch(/^image-\d+\.png$/);++ runtime.manipulatorError = new Error('unsupported');+ await chatSubmit(hook.api(), 'file:///photos/y.heic', [], { mimeType: 'image/heic' });+ expect(runtime.burnIn).toHaveBeenLastCalledWith(expect.objectContaining({ mimeType: 'image/heic' }));+ });++ it('keeps the existing WebView path for HEIC on iOS', async () => {+ const hook = mountHook();+ await chatSubmit(hook.api(), 'file:///photos/x.heic', [], { mimeType: 'image/heic', naturalWidth: 800, naturalHeight: 600 });+ expect(runtime.manipulations).toHaveLength(0);+ expect(runtime.burnIn).toHaveBeenLastCalledWith(expect.objectContaining({ mimeType: 'image/heic' }));+ });++ it('pre-downscales large photos and burns strokes in the original stroke space', async () => {+ runtime.manipulatorResult = { width: 2048, height: 1536 };+ runtime.burnIn.mockResolvedValueOnce({ base64: 'b3V0', mimeType: 'image/jpeg', width: 2048, height: 1536 });+ const hook = mountHook();+ await chatSubmit(hook.api(), 'file:///photos/big.jpg', STROKES, { mimeType: 'image/jpeg', naturalWidth: 4032, naturalHeight: 3024 });+ expect(runtime.manipulations[0]).toMatchObject({ resize: { width: 2048 }, save: { format: 'jpeg', compress: 1 } });+ expect(runtime.burnIn).toHaveBeenLastCalledWith(expect.objectContaining({+ mimeType: 'image/jpeg',+ strokeSpace: { width: 4032, height: 3024 },+ }));+ expect(runtime.burnIn.mock.calls[0][0]).not.toHaveProperty('finalize');+ const candidate = hook.enqueued()[0];+ // 烧录产物照旧交给上传前处理(尺寸 / 字节数带上,不跳过 preprocess)。+ expect(candidate).toMatchObject({ mimeType: 'image/jpeg', width: 2048, height: 1536, size: 1234 });+ expect(candidate.skipPreprocess).toBeUndefined();+ // 再编辑真相仍指向未缩的原图副本。+ expect(candidate.annotation?.sourceUri).toMatch(/annotation-src\/src-/);+ });++ it('falls back to the original bytes when the manipulator result does not match the viewer size', async () => {+ runtime.manipulatorResult = { width: 1536, height: 2048 };+ runtime.burnIn.mockResolvedValueOnce({ base64: 'b3V0', mimeType: 'image/jpeg', width: 4032, height: 3024 });+ const hook = mountHook();+ await chatSubmit(hook.api(), 'file:///photos/big.jpg', STROKES, { mimeType: 'image/jpeg', naturalWidth: 4032, naturalHeight: 3024 });+ const request = runtime.burnIn.mock.calls[0][0];+ expect(request).not.toHaveProperty('strokeSpace');+ expect(runtime.deleted).toContain('file:///cache/ImageManipulator/out-1');+ expect(hook.enqueued()[0].skipPreprocess).toBeUndefined();+ expect(hook.enqueued()[0]).toMatchObject({ width: 4032, height: 3024 });+ });++ it('forwards the viewer size on direct (stroke-free) forwarding so preprocess can downscale', async () => {+ const hook = mountHook();+ await chatSubmit(hook.api(), 'file:///photos/shot.png', [], { mimeType: 'image/png', naturalWidth: 3000, naturalHeight: 2000 });+ expect(runtime.burnIn).not.toHaveBeenCalled();+ expect(hook.enqueued()[0]).toMatchObject({ mimeType: 'image/png', width: 3000, height: 2000 });+ expect(hook.enqueued()[0].skipPreprocess).toBeUndefined();+ await chatSubmit(hook.api(), 'file:///photos/shot2.png', [], { mimeType: 'image/png' });+ expect(hook.enqueued()[1].width).toBeUndefined();+ });++ it('exposes burn-in prewarm on both lightbox configs', () => {+ const hook = mountHook();+ expect(hook.api().chatAnnotation.prewarm).toBe(runtime.acquireWarm);+ expect(hook.api().trayAnnotation.prewarm).toBe(runtime.acquireWarm);+ });+});diff --git a/apps/mobile/src/i18n/locales/en/composer.json b/apps/mobile/src/i18n/locales/en/composer.jsonindex 0103352456..aa442130c7 100644--- a/apps/mobile/src/i18n/locales/en/composer.json+++ b/apps/mobile/src/i18n/locales/en/composer.json@@ -19,6 +19,7 @@"imageNotAnnotatable": "This image can't be annotated or forwarded.","downloadOriginalFailed": "Could not download the original image (HTTP {{status}}).","annotationSaveFailed": "Could not save annotation",+ "replacementPending": "The previous save of this image is still uploading. Edit it again after it finishes.","tryAgain": "Try again.","sendToChat": "Send to Chat","save": "Save",diff --git a/apps/mobile/src/i18n/locales/en/message.json b/apps/mobile/src/i18n/locales/en/message.jsonindex 93845c78db..3184aadab6 100644--- a/apps/mobile/src/i18n/locales/en/message.json+++ b/apps/mobile/src/i18n/locales/en/message.json@@ -159,7 +159,11 @@"loadFailed": "Image failed to load","retryLoadImage": "Retry loading image","retry": "Retry",- "closeImage": "Close image"+ "closeImage": "Close image",+ "discardAnnotationTitle": "Discard Annotation?",+ "discardAnnotationBody": "Your unsaved strokes will be lost.",+ "continueAnnotating": "Keep Annotating",+ "discardAnnotation": "Discard"},"queue": {"continueSystemInstruction": "Continue the unfinished task (system instruction)",diff --git a/apps/mobile/src/i18n/locales/ja/composer.json b/apps/mobile/src/i18n/locales/ja/composer.jsonindex aa4dae295e..09b376c49d 100644--- a/apps/mobile/src/i18n/locales/ja/composer.json+++ b/apps/mobile/src/i18n/locales/ja/composer.json@@ -19,6 +19,7 @@"imageNotAnnotatable": "この画像は注釈や転送に対応していません。","downloadOriginalFailed": "元の画像をダウンロードできませんでした(HTTP {{status}})。","annotationSaveFailed": "注釈を保存できませんでした",+ "replacementPending": "この画像の前回の保存はまだアップロード中です。完了してから編集してください。","tryAgain": "もう一度お試しください。","sendToChat": "チャットに送信","save": "保存",diff --git a/apps/mobile/src/i18n/locales/ja/message.json b/apps/mobile/src/i18n/locales/ja/message.jsonindex c2a5b24528..068d7826c2 100644--- a/apps/mobile/src/i18n/locales/ja/message.json+++ b/apps/mobile/src/i18n/locales/ja/message.json@@ -159,7 +159,11 @@"loadFailed": "画像の読み込みに失敗","retryLoadImage": "画像の読み込みを再試行","retry": "再試行",- "closeImage": "画像を閉じる"+ "closeImage": "画像を閉じる",+ "discardAnnotationTitle": "注釈を破棄しますか?",+ "discardAnnotationBody": "保存されていない描画は失われます。",+ "continueAnnotating": "注釈を続ける",+ "discardAnnotation": "破棄"},"queue": {"continueSystemInstruction": "未完了のタスクを続行(システム指令)",diff --git a/apps/mobile/src/i18n/locales/ko/composer.json b/apps/mobile/src/i18n/locales/ko/composer.jsonindex 0dc903524b..0ef65d2c96 100644--- a/apps/mobile/src/i18n/locales/ko/composer.json+++ b/apps/mobile/src/i18n/locales/ko/composer.json@@ -19,6 +19,7 @@"imageNotAnnotatable": "이 이미지는 주석이나 전달을 지원하지 않습니다.","downloadOriginalFailed": "원본 이미지를 다운로드하지 못했습니다(HTTP {{status}}).","annotationSaveFailed": "주석을 저장하지 못했습니다",+ "replacementPending": "이 이미지의 이전 저장이 아직 업로드 중입니다. 완료된 후 다시 편집하세요.","tryAgain": "다시 시도하세요.","sendToChat": "대화로 보내기","save": "저장",diff --git a/apps/mobile/src/i18n/locales/ko/message.json b/apps/mobile/src/i18n/locales/ko/message.jsonindex 254fd4cef5..afc6deb749 100644--- a/apps/mobile/src/i18n/locales/ko/message.json+++ b/apps/mobile/src/i18n/locales/ko/message.json@@ -159,7 +159,11 @@"loadFailed": "이미지 로드 실패","retryLoadImage": "이미지 로드 재시도","retry": "재시도",- "closeImage": "이미지 닫기"+ "closeImage": "이미지 닫기",+ "discardAnnotationTitle": "주석을 버릴까요?",+ "discardAnnotationBody": "저장하지 않은 획은 사라집니다.",+ "continueAnnotating": "계속 주석 달기",+ "discardAnnotation": "버리기"},"queue": {"continueSystemInstruction": "완료되지 않은 작업 계속(시스템 지시)",diff --git a/apps/mobile/src/i18n/locales/zh-CN/composer.json b/apps/mobile/src/i18n/locales/zh-CN/composer.jsonindex c3e03f9f7f..60c3285dbd 100644--- a/apps/mobile/src/i18n/locales/zh-CN/composer.json+++ b/apps/mobile/src/i18n/locales/zh-CN/composer.json@@ -19,6 +19,7 @@"imageNotAnnotatable": "这张图片暂不支持标注或转发。","downloadOriginalFailed": "下载原图失败(HTTP {{status}})。","annotationSaveFailed": "标注保存失败",+ "replacementPending": "这张图片上一次的保存仍在上传,完成后再编辑。","tryAgain": "请重试。","sendToChat": "发送到对话","save": "保存",diff --git a/apps/mobile/src/i18n/locales/zh-CN/message.json b/apps/mobile/src/i18n/locales/zh-CN/message.jsonindex a0c44eb5d1..a5e68b37ca 100644--- a/apps/mobile/src/i18n/locales/zh-CN/message.json+++ b/apps/mobile/src/i18n/locales/zh-CN/message.json@@ -159,7 +159,11 @@"loadFailed": "图片加载失败","retryLoadImage": "重试加载图片","retry": "重试",- "closeImage": "关闭图片"+ "closeImage": "关闭图片",+ "discardAnnotationTitle": "放弃标注?",+ "discardAnnotationBody": "未保存的笔迹将会丢失。",+ "continueAnnotating": "继续标注",+ "discardAnnotation": "放弃标注"},"queue": {"continueSystemInstruction": "继续未完成的任务(系统指令)",diff --git a/apps/mobile/src/i18n/locales/zh-TW/composer.json b/apps/mobile/src/i18n/locales/zh-TW/composer.jsonindex 603a65304b..6c892c0c2a 100644--- a/apps/mobile/src/i18n/locales/zh-TW/composer.json+++ b/apps/mobile/src/i18n/locales/zh-TW/composer.json@@ -19,6 +19,7 @@"imageNotAnnotatable": "這張圖片暫不支援標註或轉發。","downloadOriginalFailed": "下載原圖失敗(HTTP {{status}})。","annotationSaveFailed": "標註儲存失敗",+ "replacementPending": "這張圖片上一次的儲存仍在上傳,完成後再編輯。","tryAgain": "請重試。","sendToChat": "傳送到對話","save": "儲存",diff --git a/apps/mobile/src/i18n/locales/zh-TW/message.json b/apps/mobile/src/i18n/locales/zh-TW/message.jsonindex f1f6f852b8..f35fe632b5 100644--- a/apps/mobile/src/i18n/locales/zh-TW/message.json+++ b/apps/mobile/src/i18n/locales/zh-TW/message.json@@ -159,7 +159,11 @@"loadFailed": "圖片載入失敗","retryLoadImage": "重試載入圖片","retry": "重試",- "closeImage": "關閉圖片"+ "closeImage": "關閉圖片",+ "discardAnnotationTitle": "放棄標註?",+ "discardAnnotationBody": "未儲存的筆跡將會遺失。",+ "continueAnnotating": "繼續標註",+ "discardAnnotation": "放棄標註"},"queue": {"continueSystemInstruction": "繼續未完成的任務(系統指令)",diff --git a/apps/mobile/src/session/AnnotationBurnInWebView.tsx b/apps/mobile/src/session/AnnotationBurnInWebView.tsxindex d06d2c15d7..4e9ed9124c 100644--- a/apps/mobile/src/session/AnnotationBurnInWebView.tsx+++ b/apps/mobile/src/session/AnnotationBurnInWebView.tsx@@ -2,185 +2,82 @@* AnnotationBurnInWebView — 标注烧录的隐藏 WebView host(手机版)。** RN 没有 DOM canvas,烧录(原图 + 矢量笔迹 → 位图)在一个 1x1 隐藏 WebView- * 里完成:canvas 重放算法与桌面版逐行对应(见 imageAnnotationModel 的+ * 里完成:canvas 重放脚本内联跨端共享核心(见 imageAnnotationModel 的* buildAnnotationBurnInHtml),纯 JS 方案零新增原生依赖,可随 OTA 热更。** useAnnotationBurnIn() 返回 promise 风格的 burnIn API 与 host 元素;host 按需- * 挂载(有任务才渲染 WebView,空闲零开销),任务串行执行,页面把 host 挂在- * 任意稳定位置即可。+ * 挂载(有任务或被预热时才渲染 WebView,空闲零开销),任务串行执行,页面把+ * host 挂在任意稳定位置即可。队列、超时、崩溃恢复与预热见 annotationBurnInQueue。*/import { useCallback, useEffect, useMemo, useRef, useState, type ReactElement } from 'react';import { StyleSheet, View } from 'react-native';import { WebView, type WebViewMessageEvent } from 'react-native-webview';import {- buildAnnotationBurnInHtml,- buildAnnotationBurnInInvocation,- parseAnnotationBurnInMessage,- type AnnotationStroke,-} from '@/session/imageAnnotationModel';+ createAnnotationBurnInQueue,+ type AnnotationBurnInInput,+ type AnnotationBurnInResult,+} from '@/session/annotationBurnInQueue';+import { buildAnnotationBurnInHtml } from '@/session/imageAnnotationModel';-export interface AnnotationBurnInInput {- /** 原图字节(纯 base64,无 data: 前缀)。 */- base64: string;- mimeType: string;- strokes: readonly AnnotationStroke[];-}--export interface AnnotationBurnInResult {- base64: string;- mimeType: string;- /** 烧录输出的像素尺寸(0 = WebView 回包缺失):供上传前降采样决策使用。 */- width: number;- height: number;-}--/** 单次烧录超时:大图解码 + 编码在低端机上也应远低于此,超时视为失败降级。 */-const BURN_IN_TIMEOUT_MS = 30_000;-/**- * WebView 挂载 → ready 回包的兜底超时:WebView 加载失败 / JS 早退 / 低内存被杀- * 时永远等不到 ready,任务级超时(注入时刻才起表)覆盖不到这段——不兜底的话- * burnIn promise 永不 settle,lightbox 卡在提交转圈且取消也被禁用(review P2)。- */-const WEBVIEW_READY_TIMEOUT_MS = 10_000;--interface BurnInJob {- id: string;- input: AnnotationBurnInInput;- resolve: (result: AnnotationBurnInResult) => void;- reject: (error: Error) => void;- timer: ReturnType<typeof setTimeout> | null;-}+export type { AnnotationBurnInInput, AnnotationBurnInResult } from '@/session/annotationBurnInQueue';export interface UseAnnotationBurnInResult {- /** 烧录一张图;失败(解码/编码/超时)reject,调用方自行降级。 */+ /** 烧录一张图;失败(解码/编码/超时/WebView 崩溃)reject,调用方自行降级。 */burnIn: (input: AnnotationBurnInInput) => Promise<AnnotationBurnInResult>;- /** 挂到页面任意稳定位置;无任务时为 null。 */+ /**+ * 预热:进入标注模式时调用,提前挂载 WebView,提交时省掉冷启动;返回的释放+ * 函数(幂等)在退出标注 / 关闭查看器时调用,空闲后照常卸载。+ */+ acquireWarm: () => () => void;+ /** 挂到页面任意稳定位置;无任务且未预热时为 null。 */host: ReactElement | null;}export function useAnnotationBurnIn(): UseAnnotationBurnInResult {- // hasJobs 只负责驱动 host 挂载/卸载;任务真相全在 ref(onMessage 回调闭包稳定)。- const [hasJobs, setHasJobs] = useState(false);- const queueRef = useRef<BurnInJob[]>([]);- const readyRef = useRef(false);+ // hostKey 只驱动 WebView 挂载/卸载(并作为 React key 保证每次都是全新实例);+ // 任务真相全在 queue 里。+ const [hostKey, setHostKey] = useState<number | null>(null);const webViewRef = useRef<WebView | null>(null);- const seqRef = useRef(0);-- // 前置声明打破 settleActive ↔ dispatchJob 的循环引用(经 ref 调用)。- const dispatchJobRef = useRef<(job: BurnInJob) => void>(() => undefined);-- /** WebView 起不来(见 WEBVIEW_READY_TIMEOUT_MS):全队失败 + 卸载,下次任务重新挂载重试。 */- const failAllPending = useCallback((error: Error) => {- const jobs = queueRef.current.splice(0);- for (const job of jobs) {- if (job.timer) clearTimeout(job.timer);- job.reject(error);- }- readyRef.current = false;- setHasJobs(false);- }, []);-- useEffect(() => {- if (!hasJobs || readyRef.current) return undefined;- const timer = setTimeout(() => {- // ready 已到则 no-op(effect 不因 ready 重跑,靠回调时刻的 ref 判断)。- if (!readyRef.current && queueRef.current.length > 0) {- failAllPending(new Error('annotation burn-in webview failed to initialize'));- }- }, WEBVIEW_READY_TIMEOUT_MS);- return () => clearTimeout(timer);- }, [hasJobs, failAllPending]);+ const mountedKeyRef = useRef<number | null>(null);++ const queue = useMemo(() => createAnnotationBurnInQueue({+ mount: (key) => {+ mountedKeyRef.current = key;+ setHostKey(key);+ },+ unmount: () => {+ mountedKeyRef.current = null;+ setHostKey(null);+ },+ inject: (key, script) => {+ const webView = webViewRef.current;+ if (mountedKeyRef.current !== key || !webView) return false;+ webView.injectJavaScript(script);+ return true;+ },+ }), []);// host 卸载(页面退出/失焦销毁)时在飞与排队任务全部显式 reject:不兜底的话- // burnIn promise 永不 settle,而信箱已被 drain,调用方的 catch 回投也不会跑,- // 用户笔迹静默丢失(review P2)。reject 后走既有失败链路(Alert / 信箱回投)。- useEffect(() => () => {- failAllPending(new Error('annotation burn-in host unmounted'));- }, [failAllPending]);-- const settleActive = useCallback((settle: (job: BurnInJob) => void) => {- const job = queueRef.current.shift();- if (!job) return;- if (job.timer) clearTimeout(job.timer);- settle(job);- const next = queueRef.current[0];- if (next && readyRef.current) {- dispatchJobRef.current(next);- } else if (!next) {- readyRef.current = false;- setHasJobs(false); // 卸载 WebView,下次任务重新挂载 + ready- }- }, []);-- /**- * 把队首 job 注入 WebView 并**此刻**起表:超时预算只覆盖真正在跑的任务,- * 排队中的任务不计时——否则第一张大图会吃掉后续任务的预算,多图/信箱- * 批量提交被误杀(review P2)。超时的 job 必为队首(仅被 dispatch 的有 timer)。- */- const dispatchJob = useCallback((job: BurnInJob) => {- job.timer = setTimeout(() => {- settleActive((j) => j.reject(new Error('annotation burn-in timed out')));- }, BURN_IN_TIMEOUT_MS);- webViewRef.current?.injectJavaScript(buildAnnotationBurnInInvocation({- id: job.id,- base64: job.input.base64,- mimeType: job.input.mimeType,- strokes: job.input.strokes,- }));- }, [settleActive]);- dispatchJobRef.current = dispatchJob;-- const handleMessage = useCallback((event: WebViewMessageEvent) => {- const message = parseAnnotationBurnInMessage(event.nativeEvent.data);- if (!message) return;- if ('ready' in message) {- readyRef.current = true;- const job = queueRef.current[0];- if (job) dispatchJob(job);- return;- }- const active = queueRef.current[0];- if (!active || message.id !== active.id) return; // 过期回包(超时后到达)丢弃- if (message.ok) {- settleActive((job) => job.resolve({- base64: message.base64,- mimeType: message.mimeType,- width: message.width,- height: message.height,- }));- } else {- settleActive((job) => job.reject(new Error(`annotation burn-in failed: ${message.error}`)));- }- }, [settleActive, dispatchJob]);+ // burnIn promise 永不 settle,调用方的失败链路(Alert / 信箱回投)不会跑。+ useEffect(() => () => queue.dispose(), [queue]);- const burnIn = useCallback((input: AnnotationBurnInInput): Promise<AnnotationBurnInResult> => {- return new Promise<AnnotationBurnInResult>((resolve, reject) => {- seqRef.current += 1;- const job: BurnInJob = {- id: `burn-${seqRef.current}`,- input,- resolve,- reject,- timer: null, // 计时在 dispatchJob(注入时刻)才启动,排队不占预算- };- queueRef.current.push(job);- if (queueRef.current.length === 1) {- if (readyRef.current) {- dispatchJob(job);- } else {- setHasJobs(true); // 触发挂载;ready 回包后自动开跑- }- }- });- }, [dispatchJob]);+ const burnIn = useCallback((input: AnnotationBurnInInput) => queue.burnIn(input), [queue]);+ const acquireWarm = useCallback(() => queue.acquireWarm(), [queue]);const host = useMemo<ReactElement | null>(() => {- if (!hasJobs) return null;+ if (hostKey === null) return null;+ const key = hostKey;+ const onMessage = (event: WebViewMessageEvent) => queue.handleMessage(key, event.nativeEvent.data);return (<View pointerEvents="none" style={styles.hidden}><WebViewjavaScriptEnabled- onMessage={handleMessage}+ key={key}+ onContentProcessDidTerminate={() => queue.handleProcessGone(key, 'content process terminated')}+ onError={() => queue.handleProcessGone(key, 'load error')}+ onMessage={onMessage}+ // Android 渲染进程被杀:提供该回调同时避免宿主 App 随之崩溃;实例作废后换新。+ onRenderProcessGone={() => queue.handleProcessGone(key, 'render process gone')}originWhitelist={['*']}ref={webViewRef}scrollEnabled={false}@@ -190,9 +87,9 @@ export function useAnnotationBurnIn(): UseAnnotationBurnInResult {/></View>);- }, [hasJobs, handleMessage]);+ }, [hostKey, queue]);- return { burnIn, host };+ return { burnIn, acquireWarm, host };}const styles = StyleSheet.create({diff --git a/apps/mobile/src/session/ImageLightbox.tsx b/apps/mobile/src/session/ImageLightbox.tsxindex c5891e7cad..5266176ac8 100644--- a/apps/mobile/src/session/ImageLightbox.tsx+++ b/apps/mobile/src/session/ImageLightbox.tsx@@ -19,6 +19,7 @@ import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';import { useTranslation } from 'react-i18next';import {ActivityIndicator,+ Alert,FlatList,Modal,Platform,@@ -74,15 +75,18 @@ import {} from '@/session/imageLightboxModel';import {ANNOTATION_OUTLINE_COLOR,- ANNOTATION_OUTLINE_WIDTH_RATIO,ANNOTATION_STROKE_COLOR,annotationBaseRect,annotationDisplayRect,+ annotationMinPointDistanceForRect,+ annotationOutlineWidth,+ annotationStrokesEqual,annotationStrokeToSvgPath,annotationStrokeWidth,canAnnotateImageMime,normalizeAnnotationPoint,shouldAppendAnnotationPoint,+ shouldDiscardInterruptedStroke,type AnnotationStroke,} from '@/session/imageAnnotationModel';@@ -131,13 +135,24 @@ export interface ImageLightboxAnnotationConfig {image: MobileMessageGalleryImage,displayUri: string,strokes: AnnotationStroke[],- context: { mimeType?: string },+ /** naturalWidth / naturalHeight:查看器已解码的图片尺寸(未加载完成时缺省)。 */+ context: { mimeType?: string; naturalWidth?: number; naturalHeight?: number },) => void | Promise<void>;/*** 某页的既有笔迹(托盘带标注图再编辑):打开/翻页时叠加显示,进入标注模式* 可继续画或撤销。不提供 = 全部从空白开始。*/initialStrokesFor?: (image: MobileMessageGalleryImage) => readonly AnnotationStroke[] | undefined;+ /**+ * 某页此刻能否进入标注(可选;缺省可以)。返回 false 时画笔按钮置灰禁用+ * (如托盘附件的替换上传尚未落定)。+ */+ canAnnotate?: (image: MobileMessageGalleryImage) => boolean;+ /**+ * 进入标注模式时调用(可选):宿主据此提前准备提交所需资源(烧录 WebView+ * 预热)。返回的释放函数在退出标注 / 关闭查看器时调用。+ */+ prewarm?: () => () => void;}export interface ImageLightboxProps {@@ -167,6 +182,11 @@ export interface ImageLightboxProps {showFileHeader?: boolean;/** 圈点标注(可选):见 {@link ImageLightboxAnnotationConfig}。 */annotation?: ImageLightboxAnnotationConfig;+ /**+ * 打开即进入标注模式(需同时提供 annotation;对齐桌面 autoAnnotate)。+ * 为标注而开的临时图(如图表导出)用:放弃标注(取消 / 返回)直接关闭查看器。+ */+ autoAnnotate?: boolean;}// memo:父层(消息列表)在流式回复期间每 token 重渲染,props 全部引用稳定@@ -181,6 +201,7 @@ export const ImageLightbox = memo(function ImageLightbox({extraActions,showFileHeader,annotation,+ autoAnnotate = false,}: ImageLightboxProps) {const { t } = useTranslation();const { width, height } = useWindowDimensions();@@ -205,7 +226,9 @@ export const ImageLightbox = memo(function ImageLightbox({}, []);// ---- 圈点标注状态(仅活跃页;笔迹归一化存储,显示/烧录共用同一映射)----- const [isAnnotating, setIsAnnotating] = useState(false);+ // autoAnnotate 只作初值:打开即进标注(对齐桌面)。+ const autoAnnotateActive = autoAnnotate && !!annotation;+ const [isAnnotating, setIsAnnotating] = useState(autoAnnotateActive);const [annotationSubmitting, setAnnotationSubmitting] = useState(false);const submittingRef = useRef(false);const sharingRef = useRef(false);@@ -213,10 +236,20 @@ export const ImageLightbox = memo(function ImageLightbox({const onCloseRef = useRef(onClose);onCloseRef.current = onClose;const [strokes, setStrokes] = useState<AnnotationStroke[]>([]);+ // 取消 / 返回回调经 ref 读最新笔迹与文案:它们进入手势 deps(onRequestClose),+ // 不能因每次作画 / 父层重渲染而让整张手势图重建。+ const strokesRef = useRef(strokes);+ strokesRef.current = strokes;+ const tRef = useRef(t);+ tRef.current = t;const [draftStroke, setDraftStroke] = useState<AnnotationStroke | null>(null);/** url → 图片自然尺寸(LightboxPage onLoad 上报;overlay 与坐标换算的基准)。 */const [naturalSizes, setNaturalSizes] = useState<Record<string, { width: number; height: number }>>({});const draftStrokeRef = useRef<AnnotationStroke | null>(null);+ /** 进行中一笔的屏幕路径长度(容器坐标,px)与上一个原始触点:判定捏合误触。 */+ const draftScreenLengthRef = useRef(0);+ const draftLastScreenPointRef = useRef<{ x: number; y: number } | null>(null);+ const discardConfirmOpenRef = useRef(false);const activeImageForDraw = images[activeIndex] ?? null;const activeNaturalSize = activeImageForDraw ? naturalSizes[activeImageForDraw.key] ?? null : null;@@ -240,12 +273,18 @@ export const ImageLightbox = memo(function ImageLightbox({const initial = image ? annotation?.initialStrokesFor?.(image) : undefined;setStrokes(initial ? [...initial] : []);draftStrokeRef.current = null;+ draftLastScreenPointRef.current = null;+ draftScreenLengthRef.current = 0;setDraftStroke(null);// annotation 语义上只在打开时取一次快照,翻页时按新页(key)重取// eslint-disable-next-line react-hooks/exhaustive-deps}, [activeIndex, activeImageKeyForStrokes]);- /** 画笔落点(容器坐标 + 当页 transform 状态,LightboxPage 手势经 runOnJS 上抛)。 */+ /**+ * 画笔落点(容器坐标 + 当页 transform 状态,LightboxPage 手势经 runOnJS 上抛)。+ * end 带手势是否正常结束:第二根手指落下会让单指画笔手势被取消,此时屏幕+ * 路径很短的半笔是捏合起手的误触(点 / 小短线),丢弃;点按与真正的笔画照常落笔。+ */const handleDrawPoint = useCallback((phase: 'start' | 'move' | 'end',pointX: number,@@ -253,13 +292,17 @@ export const ImageLightbox = memo(function ImageLightbox({translateX: number,translateY: number,scale: number,+ gestureSucceeded = true,) => {if (submittingRef.current) return;if (phase === 'end') {const draft = draftStrokeRef.current;+ const screenLength = draftScreenLengthRef.current;draftStrokeRef.current = null;+ draftLastScreenPointRef.current = null;+ draftScreenLengthRef.current = 0;setDraftStroke(null);- if (draft && draft.points.length > 0) {+ if (draft && draft.points.length > 0 && !shouldDiscardInterruptedStroke(gestureSucceeded, screenLength)) {setStrokes((prev) => [...prev, draft]);}return;@@ -272,30 +315,94 @@ export const ImageLightbox = memo(function ImageLightbox({if (!point) return;if (phase === 'start') {draftStrokeRef.current = { points: [point] };+ draftLastScreenPointRef.current = { x: pointX, y: pointY };+ draftScreenLengthRef.current = 0;setDraftStroke(draftStrokeRef.current);return;}const draft = draftStrokeRef.current;if (!draft) return;- if (!shouldAppendAnnotationPoint(draft, point)) return;- draftStrokeRef.current = { points: [...draft.points, point] };+ const lastScreen = draftLastScreenPointRef.current;+ if (lastScreen) draftScreenLengthRef.current += Math.hypot(pointX - lastScreen.x, pointY - lastScreen.y);+ draftLastScreenPointRef.current = { x: pointX, y: pointY };+ // 最小点距按屏幕像素换算:放大作画保精度,1x 不逐事件记录。+ if (!shouldAppendAnnotationPoint(draft, point, annotationMinPointDistanceForRect(rect))) return;+ // 原地追加(O(1)),只换外层引用触发重渲染;落笔 / 提交后草稿引用即被置空,+ // 已提交的笔迹不会再被改动。+ draft.points.push(point);+ draftStrokeRef.current = { points: draft.points };setDraftStroke(draftStrokeRef.current);}, [activeNaturalSize, width, height]);const undoLastStroke = useCallback(() => {setStrokes((prev) => prev.slice(0, -1));draftStrokeRef.current = null;+ draftLastScreenPointRef.current = null;+ draftScreenLengthRef.current = 0;setDraftStroke(null);}, []);- const exitAnnotationMode = useCallback(() => {+ /**+ * 放弃标注:恢复到该页既有笔迹并退出标注模式。autoAnnotate 宿主(为标注而开+ * 的临时图)留在查看器没有意义,直接关闭(对齐桌面)。+ */+ const discardAnnotation = useCallback(() => {+ if (autoAnnotateActive) {+ onCloseRef.current();+ return;+ }const image = images[activeIndex];const initial = image ? annotation?.initialStrokesFor?.(image) : undefined;setStrokes(initial ? [...initial] : []);draftStrokeRef.current = null;+ draftLastScreenPointRef.current = null;+ draftScreenLengthRef.current = 0;setDraftStroke(null);setIsAnnotating(false);- }, [activeIndex, images, annotation]);+ }, [activeIndex, images, annotation, autoAnnotateActive]);++ /**+ * 取消 / 返回:笔迹与打开时一致则直接放弃(与以往完全相同);有未保存的改动+ * 先确认,避免误触丢掉整张图的圈点。+ */+ const exitAnnotationMode = useCallback(() => {+ if (submittingRef.current) return;+ const image = images[activeIndex];+ const baseline = (image ? annotation?.initialStrokesFor?.(image) : undefined) ?? EMPTY_STROKES;+ const changed = !!draftStrokeRef.current?.points.length+ || !annotationStrokesEqual(strokesRef.current, baseline);+ if (!changed) {+ discardAnnotation();+ return;+ }+ if (discardConfirmOpenRef.current) return;+ discardConfirmOpenRef.current = true;+ const settle = () => { discardConfirmOpenRef.current = false; };+ const t = tRef.current;+ Alert.alert(+ t('message.lightbox.discardAnnotationTitle'),+ t('message.lightbox.discardAnnotationBody'),+ [+ { text: t('message.lightbox.continueAnnotating'), style: 'cancel', onPress: settle },+ {+ text: t('message.lightbox.discardAnnotation'),+ style: 'destructive',+ onPress: () => {+ settle();+ discardAnnotation();+ },+ },+ ],+ { cancelable: true, onDismiss: settle },+ );+ }, [activeIndex, images, annotation, discardAnnotation]);++ // 标注模式期间让宿主预热提交资源(烧录 WebView);退出 / 关闭时释放。+ const prewarmAnnotation = annotation?.prewarm;+ useEffect(() => {+ if (!isAnnotating || !prewarmAnnotation) return undefined;+ return prewarmAnnotation();+ }, [isAnnotating, prewarmAnnotation]);// 每 url 只自动强制重取一次(Image 加载失败自愈),防 onError↔重取死循环;// 重试按钮的显式 forceRefresh 不受此限制。@@ -453,6 +560,15 @@ export const ImageLightbox = memo(function ImageLightbox({&& canAnnotateImageMime(activeMimeType)&& !activeLooksGif&& !activeLooksSvg;+ // 画笔要等图片自然尺寸就位(坐标换算基准),否则进入标注后作画静默无效;+ // 宿主也可暂时禁止(canAnnotate)。不满足时按钮置灰禁用,与撤销的禁用态同观感。+ const annotateEnabled = annotateVisible+ && !!activeNaturalSize+ && (!activeImage || !annotation?.canAnnotate || annotation.canAnnotate(activeImage));+ const enterAnnotationMode = useCallback(() => {+ if (!annotateEnabled || submittingRef.current || sharingRef.current) return;+ setIsAnnotating(true);+ }, [annotateEnabled]);// 独立直发(发送到对话):不要求可标注——gif 等不可画的图同样能转发。const directSubmitVisible = !!annotation?.allowDirectSubmit && !!activeImage && !!activeUri;@@ -470,6 +586,9 @@ export const ImageLightbox = memo(function ImageLightbox({void Promise.resolve().then(() => annotation.onSubmit(activeImage, activeUri, submittedStrokes, {mimeType: activeMimeType,+ ...(activeNaturalSize+ ? { naturalWidth: activeNaturalSize.width, naturalHeight: activeNaturalSize.height }+ : {}),})).then(() => onCloseRef.current()).catch(() => {@@ -479,12 +598,12 @@ export const ImageLightbox = memo(function ImageLightbox({submittingRef.current = false;setAnnotationSubmitting(false);});- }, [annotation, activeImage, activeUri, activeMimeType, strokes]);+ }, [annotation, activeImage, activeUri, activeMimeType, activeNaturalSize, strokes]);// Android 物理返回键触发 Modal.onRequestClose,不受手势层/按钮层的// isAnnotating 禁用覆盖(review 发现:会绕开"标注模式中关闭均禁用"保护- // 直接整体关闭 lightbox,未保存笔迹丢失)。标注中改为退出标注模式,- // 提交中忽略返回键,非标注态才走原始关闭。+ // 直接整体关闭 lightbox,未保存笔迹丢失)。标注中改为退出标注模式(有改动+ // 先确认;autoAnnotate 放弃即关闭),提交中忽略返回键,非标注态才走原始关闭。const handleRequestClose = useCallback(() => {if (submittingRef.current) return;if (isAnnotating) {@@ -683,14 +802,19 @@ export const ImageLightbox = memo(function ImageLightbox({{annotateVisible ? (<PressableaccessibilityLabel={t('message.lightbox.annotateImage')}- disabled={annotationSubmitting || sharing}+ accessibilityState={{ disabled: !annotateEnabled }}+ disabled={annotationSubmitting || sharing || !annotateEnabled}hitSlop={8}- onPress={() => setIsAnnotating(true)}+ onPress={enterAnnotationMode}style={styles.actionItem}testID="message.imageLightboxAnnotateButton">- <Pen color="#ffffff" size={iconSize.action} strokeWidth={iconStroke.regular} />- <Text style={styles.actionLabel}>{t('message.lightbox.annotate')}</Text>+ <Pen+ color={annotateEnabled ? '#ffffff' : 'rgba(255,255,255,0.35)'}+ size={iconSize.action}+ strokeWidth={iconStroke.regular}+ />+ <Text style={[styles.actionLabel, !annotateEnabled && styles.actionLabelDisabled]}>{t('message.lightbox.annotate')}</Text></Pressable>) : null}{extraActions.map((action) => (@@ -739,14 +863,19 @@ export const ImageLightbox = memo(function ImageLightbox({{annotateVisible ? (<PressableaccessibilityLabel={t('message.lightbox.annotateImage')}- disabled={annotationSubmitting || sharing}+ accessibilityState={{ disabled: !annotateEnabled }}+ disabled={annotationSubmitting || sharing || !annotateEnabled}hitSlop={8}- onPress={() => setIsAnnotating(true)}+ onPress={enterAnnotationMode}style={styles.actionItem}testID="message.imageLightboxAnnotateButton">- <Pen color="#ffffff" size={iconSize.action} strokeWidth={iconStroke.regular} />- <Text style={styles.actionLabel}>+ <Pen+ color={annotateEnabled ? '#ffffff' : 'rgba(255,255,255,0.35)'}+ size={iconSize.action}+ strokeWidth={iconStroke.regular}+ />+ <Text style={[styles.actionLabel, !annotateEnabled && styles.actionLabelDisabled]}>{t('message.lightbox.annotate')}</Text></Pressable>@@ -846,7 +975,8 @@ const LightboxPage = memo(function LightboxPage({naturalSize: { width: number; height: number } | null;/** 捏合/平移开始与结束时通知父层开关 chrome 触摸。 */onChromeBusy(busy: boolean): void;- /** 画笔事件(容器坐标 + 当页 transform 快照),UI 线程经 runOnJS 上抛。 */+ /** 画笔事件(容器坐标 + 当页 transform 快照),UI 线程经 runOnJS 上抛;+ * end 附带手势是否正常结束(第二根手指落下会让画笔手势被取消)。 */onDrawPoint: (phase: 'start' | 'move' | 'end',pointX: number,@@ -854,6 +984,7 @@ const LightboxPage = memo(function LightboxPage({translateX: number,translateY: number,scale: number,+ gestureSucceeded?: boolean,) => void;/** 原生 Image 加载失败(悬空 key 404 等)→ 上抛做一次性 forceRefresh 自愈。* 带 image 参数的稳定引用;是否接线由本页按 retryable 判定。 */@@ -1227,7 +1358,8 @@ const LightboxPage = memo(function LightboxPage({});// 画笔:单指跟手采点(worklet 只搬运坐标 + transform 快照,归一化在 JS 侧- // 纯函数完成);第二根手指落下时本手势自然结束,已画的半笔照常落笔。+ // 纯函数完成);第二根手指落下时本手势被取消(success=false):已画出一段的+ // 半笔照常落笔,屏幕路径极短的(捏合起手误触的点 / 小短线)由 JS 侧丢弃。const panDraw = Gesture.Pan().enabled(annotating && !interactionDisabled).maxPointers(1)@@ -1238,8 +1370,8 @@ const LightboxPage = memo(function LightboxPage({.onUpdate((event) => {runOnJS(onDrawPoint)('move', event.x, event.y, translateX.value, translateY.value, scale.value);})- .onFinalize(() => {- runOnJS(onDrawPoint)('end', 0, 0, 0, 0, 1);+ .onFinalize((_event, success) => {+ runOnJS(onDrawPoint)('end', 0, 0, 0, 0, 1, success);});return Gesture.Simultaneous(@@ -1285,14 +1417,43 @@ const LightboxPage = memo(function LightboxPage({() => (naturalSize ? annotationBaseRect(width, height, naturalSize.width, naturalSize.height) : null),[naturalSize, width, height],);- const overlayStrokes = useMemo<readonly AnnotationStroke[]>(- () => (annotationDraftStroke ? [...annotationStrokes, annotationDraftStroke] : annotationStrokes),- [annotationStrokes, annotationDraftStroke],- );- const overlayVisible = !!uri && !!annotationBase && !!naturalSize && overlayStrokes.length > 0;+ const overlayVisible = !!uri && !!annotationBase && !!naturalSize+ && (annotationStrokes.length > 0 || !!annotationDraftStroke);const overlayStrokeWidth = naturalSize? annotationStrokeWidth(naturalSize.width, naturalSize.height): 0;+ const overlayOutlineWidth = annotationOutlineWidth(overlayStrokeWidth);+ // 已落笔迹的两层 path 元素按(笔迹, 尺寸)记忆:作画时每个 move 只重算进行中+ // 那一笔,已有笔迹的 path 字符串与元素引用不变,React 直接跳过它们。+ const committedOutlinePaths = useMemo(() => (naturalSize+ ? annotationStrokes.map((stroke, index) => (+ <Path+ d={annotationStrokeToSvgPath(stroke, naturalSize.width, naturalSize.height)}+ fill="none"+ key={`outline-${index}`}+ stroke={ANNOTATION_OUTLINE_COLOR}+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth={overlayOutlineWidth}+ />+ ))+ : null), [annotationStrokes, naturalSize, overlayOutlineWidth]);+ const committedStrokePaths = useMemo(() => (naturalSize+ ? annotationStrokes.map((stroke, index) => (+ <Path+ d={annotationStrokeToSvgPath(stroke, naturalSize.width, naturalSize.height)}+ fill="none"+ key={`stroke-${index}`}+ stroke={ANNOTATION_STROKE_COLOR}+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth={overlayStrokeWidth}+ />+ ))+ : null), [annotationStrokes, naturalSize, overlayStrokeWidth]);+ const draftPathD = naturalSize && annotationDraftStroke+ ? annotationStrokeToSvgPath(annotationDraftStroke, naturalSize.width, naturalSize.height)+ : null;return (<View style={{ height, width }}>@@ -1368,28 +1529,30 @@ const LightboxPage = memo(function LightboxPage({viewBox={`0 0 ${naturalSize.width} ${naturalSize.height}`}width="100%">- {overlayStrokes.map((stroke, index) => (+ {committedOutlinePaths}+ {draftPathD ? (<Path- d={annotationStrokeToSvgPath(stroke, naturalSize.width, naturalSize.height)}+ d={draftPathD}fill="none"- key={`outline-${index}`}+ key="outline-draft"stroke={ANNOTATION_OUTLINE_COLOR}strokeLinecap="round"strokeLinejoin="round"- strokeWidth={Math.round(overlayStrokeWidth * ANNOTATION_OUTLINE_WIDTH_RATIO)}+ strokeWidth={overlayOutlineWidth}/>- ))}- {overlayStrokes.map((stroke, index) => (+ ) : null}+ {committedStrokePaths}+ {draftPathD ? (<Path- d={annotationStrokeToSvgPath(stroke, naturalSize.width, naturalSize.height)}+ d={draftPathD}fill="none"- key={`stroke-${index}`}+ key="stroke-draft"stroke={ANNOTATION_STROKE_COLOR}strokeLinecap="round"strokeLinejoin="round"strokeWidth={overlayStrokeWidth}/>- ))}+ ) : null}</Svg></Animated.View>) : null}diff --git a/apps/mobile/src/session/MessageRenderer.tsx b/apps/mobile/src/session/MessageRenderer.tsxindex 85b5471c6c..87ee7d3b77 100644--- a/apps/mobile/src/session/MessageRenderer.tsx+++ b/apps/mobile/src/session/MessageRenderer.tsx@@ -7022,8 +7022,10 @@ function MessagePayloadModal({</View>{annotatePayload && annotateImages ? (// 嵌套标注层:必须渲染在本 Modal 的 children 内(见 annotatePayload 注释)。+ // 入口本身就是「标注」:打开即进标注模式;放弃标注直接回图表(对齐桌面)。<ImageLightboxannotation={annotateLightboxAnnotation}+ autoAnnotateimages={annotateImages}initialUrl={annotatePayload.media.url}onClose={closeAnnotatePayload}diff --git a/apps/mobile/src/session/annotationBurnInQueue.ts b/apps/mobile/src/session/annotationBurnInQueue.tsnew file mode 100644index 0000000000..e940b7e2d1--- /dev/null+++ b/apps/mobile/src/session/annotationBurnInQueue.ts@@ -0,0 +1,276 @@+/**+ * annotationBurnInQueue.ts — 标注烧录 WebView 的任务队列与生命周期(纯逻辑)。+ * ---------------------------------------------------------------------------+ * AnnotationBurnInWebView 的 React hook 只负责把 WebView 挂上 / 摘下;任务串行、+ * 就绪握手、超时、进程崩溃与预热全部在这里,依赖注入 host 与计时器,node 可单测。+ *+ * 不变量:+ * - 每次(重新)挂载都换一个新的 sessionKey。宿主以它作 WebView 的 React key,+ * 「卸载后立刻再挂载」被批处理合并时也一定是一次真实的重挂载,必然重新发+ * ready——不会出现旧实现里 ready 已被清零、WebView 却没重建、任务永远等不到+ * ready 的情况。旧 session 的迟到消息 / 崩溃事件按 key 丢弃。+ * - 任务超时只覆盖真正注入 WebView 的队首任务(排队不计时);ready 超时只在+ * 有任务等待时计时(预热期间不计)。+ * - WebView 进程被系统杀掉 / 加载失败时,队首任务立即失败(不再干等 30s),+ * 剩余任务在一个新挂载的 WebView 上继续;每次崩溃至少消耗一个任务,不会+ * 无限重挂载。+ * - 没有任务、也没有预热持有者时卸载 WebView(空闲零开销)。+ */+import {+ buildAnnotationBurnInInvocation,+ parseAnnotationBurnInMessage,+ type AnnotationStroke,+} from '@/session/imageAnnotationModel';++export interface AnnotationBurnInInput {+ /** 原图字节(纯 base64,无 data: 前缀)。 */+ base64: string;+ mimeType: string;+ strokes: readonly AnnotationStroke[];+ /** 见 AnnotationBurnInRequest.strokeSpace。 */+ strokeSpace?: { width: number; height: number };+}++export interface AnnotationBurnInResult {+ base64: string;+ mimeType: string;+ /** 烧录输出的像素尺寸(0 = WebView 回包缺失):供上传前降采样决策使用。 */+ width: number;+ height: number;+}++/** 单次烧录超时:大图解码 + 编码在低端机上也应远低于此,超时视为失败降级。 */+export const BURN_IN_TIMEOUT_MS = 30_000;+/**+ * WebView 挂载 → ready 回包的兜底超时:WebView 加载失败 / JS 早退 / 低内存被杀+ * 时永远等不到 ready,任务级超时(注入时刻才起表)覆盖不到这段。+ */+export const WEBVIEW_READY_TIMEOUT_MS = 10_000;++export interface AnnotationBurnInQueueHost {+ /** 以新的 sessionKey 挂载(或重挂载)WebView。 */+ mount(sessionKey: number): void;+ /** 卸载 WebView。 */+ unmount(): void;+ /** 向 sessionKey 对应的 WebView 注入脚本;WebView 不可用时返回 false。 */+ inject(sessionKey: number, script: string): boolean;+}++export interface AnnotationBurnInQueueOptions {+ readyTimeoutMs?: number;+ jobTimeoutMs?: number;+ setTimer?: (fn: () => void, ms: number) => unknown;+ clearTimer?: (timer: unknown) => void;+}++export interface AnnotationBurnInQueue {+ burnIn(input: AnnotationBurnInInput): Promise<AnnotationBurnInResult>;+ /** 预热:持有期间 WebView 保持挂载(空闲也不卸载);返回幂等的释放函数。 */+ acquireWarm(): () => void;+ handleMessage(sessionKey: number, raw: string): void;+ /** WebView 进程终止 / 渲染进程消失 / 加载失败。 */+ handleProcessGone(sessionKey: number, reason: string): void;+ /** 宿主卸载:全部任务显式 reject 并清理计时器(之后仍可重新使用)。 */+ dispose(): void;+}++interface BurnInJob {+ id: string;+ input: AnnotationBurnInInput;+ resolve: (result: AnnotationBurnInResult) => void;+ reject: (error: Error) => void;+ /** 注入 WebView 时才起表;非 null = 已派发(必为队首)。 */+ timer: unknown;+ dispatched: boolean;+}++export function createAnnotationBurnInQueue(+ host: AnnotationBurnInQueueHost,+ options: AnnotationBurnInQueueOptions = {},+): AnnotationBurnInQueue {+ const readyTimeoutMs = options.readyTimeoutMs ?? WEBVIEW_READY_TIMEOUT_MS;+ const jobTimeoutMs = options.jobTimeoutMs ?? BURN_IN_TIMEOUT_MS;+ const setTimer = options.setTimer ?? ((fn: () => void, ms: number) => setTimeout(fn, ms));+ const clearTimer = options.clearTimer ?? ((timer: unknown) => clearTimeout(timer as ReturnType<typeof setTimeout>));++ const queue: BurnInJob[] = [];+ let jobSeq = 0;+ let keySeq = 0;+ /** 当前挂载的 WebView session;null = 未挂载。 */+ let sessionKey: number | null = null;+ let ready = false;+ let readyTimer: unknown = null;+ let warmCount = 0;++ function clearReadyTimer(): void {+ if (readyTimer !== null) clearTimer(readyTimer);+ readyTimer = null;+ }++ function mountFresh(): void {+ keySeq += 1;+ sessionKey = keySeq;+ ready = false;+ clearReadyTimer();+ host.mount(keySeq);+ }++ function unmount(): void {+ clearReadyTimer();+ ready = false;+ if (sessionKey === null) return;+ sessionKey = null;+ host.unmount();+ }++ function ensureMounted(): void {+ if (sessionKey === null) mountFresh();+ }++ function armReadyTimer(): void {+ if (ready || readyTimer !== null || queue.length === 0 || sessionKey === null) return;+ readyTimer = setTimer(() => {+ readyTimer = null;+ if (ready || queue.length === 0) return;+ failAllQueued(new Error('annotation burn-in webview failed to initialize'));+ // 起不来的 WebView 不再复用:预热中则换一个新实例(无任务不计时,不会循环),+ // 否则卸载,下次任务重新挂载重试。+ if (warmCount > 0) mountFresh();+ else unmount();+ }, readyTimeoutMs);+ }++ function failAllQueued(error: Error): void {+ const jobs = queue.splice(0);+ for (const job of jobs) {+ if (job.timer !== null) clearTimer(job.timer);+ job.reject(error);+ }+ }++ /** 队首任务落定后:继续派发下一个,或在空闲时卸载。 */+ function afterSettle(): void {+ if (queue.length > 0) {+ if (ready) dispatchHead();+ else armReadyTimer();+ return;+ }+ if (warmCount === 0) unmount();+ }++ /** WebView 不再可信(崩溃 / 超时):有后续需求则换新实例,否则卸载。 */+ function replaceWebView(): void {+ if (queue.length > 0 || warmCount > 0) {+ mountFresh();+ armReadyTimer();+ } else {+ unmount();+ }+ }++ function takeHead(job: BurnInJob): boolean {+ if (queue[0] !== job) return false;+ queue.shift();+ if (job.timer !== null) clearTimer(job.timer);+ job.timer = null;+ return true;+ }++ function dispatchHead(): void {+ const job = queue[0];+ if (!job || job.dispatched || !ready || sessionKey === null) return;+ job.dispatched = true;+ const key = sessionKey;+ job.timer = setTimer(() => {+ if (!takeHead(job)) return;+ job.reject(new Error('annotation burn-in timed out'));+ // 超时的 WebView 可能仍卡在解码大图上,后续任务在新实例上跑。+ replaceWebView();+ }, jobTimeoutMs);+ let injected = false;+ try {+ injected = host.inject(key, buildAnnotationBurnInInvocation({+ id: job.id,+ base64: job.input.base64,+ mimeType: job.input.mimeType,+ strokes: job.input.strokes,+ ...(job.input.strokeSpace ? { strokeSpace: job.input.strokeSpace } : {}),+ }));+ } catch {+ injected = false;+ }+ if (!injected) handleProcessGone(key, 'webview unavailable');+ }++ function handleProcessGone(key: number, reason: string): void {+ if (key !== sessionKey) return; // 旧实例的迟到事件+ ready = false;+ clearReadyTimer();+ const head = queue[0];+ if (head && takeHead(head)) {+ head.reject(new Error(`annotation burn-in webview terminated: ${reason}`));+ }+ replaceWebView();+ }++ return {+ burnIn(input) {+ return new Promise<AnnotationBurnInResult>((resolve, reject) => {+ jobSeq += 1;+ queue.push({ id: `burn-${jobSeq}`, input, resolve, reject, timer: null, dispatched: false });+ ensureMounted();+ if (ready) dispatchHead();+ else armReadyTimer();+ });+ },++ acquireWarm() {+ warmCount += 1;+ ensureMounted();+ let released = false;+ return () => {+ if (released) return;+ released = true;+ warmCount = Math.max(0, warmCount - 1);+ if (warmCount === 0 && queue.length === 0) unmount();+ };+ },++ handleMessage(key, raw) {+ if (key !== sessionKey) return; // 旧实例的迟到消息+ const message = parseAnnotationBurnInMessage(raw);+ if (!message) return;+ if ('ready' in message) {+ ready = true;+ clearReadyTimer();+ dispatchHead();+ return;+ }+ const active = queue[0];+ // 过期回包(超时后到达 / 非队首)丢弃。+ if (!active || !active.dispatched || message.id !== active.id) return;+ takeHead(active);+ if (message.ok) {+ active.resolve({+ base64: message.base64,+ mimeType: message.mimeType,+ width: message.width,+ height: message.height,+ });+ } else {+ active.reject(new Error(`annotation burn-in failed: ${message.error}`));+ }+ afterSettle();+ },++ handleProcessGone,++ dispose() {+ clearReadyTimer();+ failAllQueued(new Error('annotation burn-in host unmounted'));+ warmCount = 0;+ ready = false;+ sessionKey = null;+ },+ };+}diff --git a/apps/mobile/src/session/composerAttachmentInbox.ts b/apps/mobile/src/session/composerAttachmentInbox.tsindex 97d33d795f..cab9536707 100644--- a/apps/mobile/src/session/composerAttachmentInbox.ts+++ b/apps/mobile/src/session/composerAttachmentInbox.ts@@ -19,6 +19,9 @@ export interface ComposerAnnotationSubmission {displayUri: string;strokes: AnnotationStroke[];mimeType?: string;+ /** lightbox 已解码的图片尺寸(可选):供烧录预缩 / 上传降采样决策。 */+ naturalWidth?: number;+ naturalHeight?: number;/*** 处理失败后的回投次数(消费方维护):提交失败(槽满 / 读源失败 / 烧录失败)* 时把 submission 回投信箱等下次 focus 重试,用户画的笔迹不静默丢;达到上限diff --git a/apps/mobile/src/session/imageAnnotationModel.ts b/apps/mobile/src/session/imageAnnotationModel.tsindex d221d6e1eb..bc65d634b0 100644--- a/apps/mobile/src/session/imageAnnotationModel.ts+++ b/apps/mobile/src/session/imageAnnotationModel.ts@@ -1,32 +1,47 @@/*** imageAnnotationModel.ts — 图片圈点标注的纯函数层(手机版)。* ---------------------------------------------------------------------------- * 与桌面版 lightboxAnnotations.ts(PR #792)同构:笔迹一律存**归一化坐标**- * (0..1,相对图片自然尺寸),显示(SVG overlay)与烧录(WebView canvas)共用- * 同一映射,所见即所得。视觉参数(红 #FF3B30 + 白描边、线宽 0.5% 短边)与- * 桌面完全一致,保证同一张标注图在两端观感一致。+ * 笔迹一律存**归一化坐标**(0..1,相对图片自然尺寸),显示(SVG overlay)与+ * 烧录(WebView canvas)共用同一映射,所见即所得。视觉参数、线宽公式、归一化与+ * 采点过滤、SVG path 与 canvas 重放算法全部来自跨端共享核心+ * `@cindy/maker-shared/image-annotation`(与桌面同一份实现)。** 手机版特有的部分:* - contain 布局计算(RN 没有 getBoundingClientRect,显示矩形要从容器尺寸、* 自然尺寸与 lightbox 的 translate/scale 状态推导);- * - WebView 烧录 HTML 生成(RN 无 DOM canvas,烧录在隐藏 WebView 里重放,- * 算法与桌面 drawStrokesOnCanvas 逐行对应)。+ * - WebView 烧录 HTML 生成与协议(RN 无 DOM canvas,烧录在隐藏 WebView 里+ * 重放,重放脚本内联共享核心的 ANNOTATION_CANVAS_SCRIPT);+ * - 烧录前的源图预处理规划(Android 解不了的格式先转码、超大图先预缩);+ * - mime 嗅探与直传白名单。* 全部无副作用,node 可单测;React 组件只做事件采集与状态管理。*/-/** 一条手绘笔迹:归一化坐标点序列(0..1,相对图片自然尺寸)。 */-export interface AnnotationStroke {- points: Array<{ x: number; y: number }>;-}--/** 标注笔迹主色(红,语义豁免色系;与桌面 ANNOTATION_STROKE_COLOR 一致)。 */-export const ANNOTATION_STROKE_COLOR = '#FF3B30';-export const ANNOTATION_OUTLINE_COLOR = 'rgba(255,255,255,0.9)';+import {+ ANNOTATION_CANVAS_SCRIPT,+ annotationStrokeToSvgPath,+ annotationStrokeWidth,+ annotationOutlineWidth,+ normalizeAnnotationPoint as normalizeSharedAnnotationPoint,+ shouldAppendAnnotationPoint,+ type AnnotationStroke,+} from '@cindy/maker-shared/image-annotation';+import { MOBILE_IMAGE_UPLOAD_MAX_LONG_EDGE } from '@/session/mobileImagePreprocess';-/** 白描边相对红线的宽度倍率(桌面同值)。 */-export const ANNOTATION_OUTLINE_WIDTH_RATIO = 1.8;-/** 采集时的最小点距(归一化):小于该距离的 move 点丢弃,抑制点数爆炸。 */-export const MIN_POINT_DISTANCE_RATIO = 0.002;+// 视觉参数与纯函数的唯一真相源是跨端共享核心(@cindy/maker-shared/image-annotation);+// 这里只做薄 re-export,既有调用点与测试不必改导入路径。+export {+ ANNOTATION_OUTLINE_COLOR,+ ANNOTATION_OUTLINE_WIDTH_RATIO,+ ANNOTATION_STROKE_COLOR,+ MIN_POINT_DISTANCE_RATIO,+} from '@cindy/maker-shared/image-annotation';+export {+ annotationOutlineWidth,+ annotationStrokeToSvgPath,+ annotationStrokeWidth,+ shouldAppendAnnotationPoint,+};+export type { AnnotationStroke };/*** 烧录 canvas 的边长上限:iOS WKWebView 的 canvas 有约 16MP 的硬限制,超限@@ -35,10 +50,6 @@ export const MIN_POINT_DISTANCE_RATIO = 0.002;*/export const ANNOTATION_MAX_BURN_DIMENSION = 4096;-function clamp01(v: number): number {- return Math.min(1, Math.max(0, v));-}-/** 图片显示矩形(容器坐标系,px)。 */export interface AnnotationDisplayRect {left: number;@@ -97,62 +108,80 @@ export function annotationDisplayRect(/*** 触点(容器坐标)→ 归一化图片坐标。越界点钳制到边缘(画到图外时贴边),- * 与桌面 normalizePoint 语义一致。+ * 坐标量化到 4 位小数——实现见共享核心,这里只收窄参数类型。*/export function normalizeAnnotationPoint(pointX: number,pointY: number,rect: AnnotationDisplayRect,): { x: number; y: number } | null {- if (rect.width <= 0 || rect.height <= 0) return null;- return {- x: clamp01((pointX - rect.left) / rect.width),- y: clamp01((pointY - rect.top) / rect.height),- };+ return normalizeSharedAnnotationPoint(pointX, pointY, rect);}+/** 采点的屏幕像素阈值:移动不足该距离的 move 事件丢弃。 */+export const ANNOTATION_MIN_POINT_SCREEN_PX = 1.5;+/**- * 是否应把新点追加进笔迹:与上一点距离(归一化)超过阈值才收,- * 抑制高频 touch move 造成的点数爆炸。首点恒收。+ * 按当前显示矩形把屏幕像素阈值换算成归一化最小点距(传给+ * shouldAppendAnnotationPoint)。用长边换算:任一方向上相距超过+ * {@link ANNOTATION_MIN_POINT_SCREEN_PX} 屏幕像素的点都不会被丢——放大作画时+ * 阈值随之变细保住精度,1x 下不再逐事件记录。矩形非法时退回共享默认阈值。*/-export function shouldAppendAnnotationPoint(- stroke: AnnotationStroke,- point: { x: number; y: number },- minDistance = MIN_POINT_DISTANCE_RATIO,-): boolean {- const last = stroke.points[stroke.points.length - 1];- if (!last) return true;- return Math.hypot(point.x - last.x, point.y - last.y) >= minDistance;+export function annotationMinPointDistanceForRect(+ rect: Pick<AnnotationDisplayRect, 'width' | 'height'>,+ screenPx = ANNOTATION_MIN_POINT_SCREEN_PX,+): number | undefined {+ const longEdge = Math.max(rect.width, rect.height);+ if (!(longEdge > 0) || !Number.isFinite(longEdge)) return undefined;+ return screenPx / longEdge;}/**- * 烧录/显示用线宽(像素,相对图片自然尺寸):0.5% 短边,4px 下限、24px 上限。- * 与桌面 annotationStrokeWidth 同公式。+ * 进行中笔迹的屏幕路径长度阈值:第二根手指落下导致单指画笔手势被取消时,+ * 短于它的半笔视为捏合起手的误触(点 / 小短线)丢弃;单指点按(手势正常结束)+ * 与真正的笔画不受影响。+ */+export const ANNOTATION_MULTI_TOUCH_DISCARD_SCREEN_PX = 12;++/**+ * 画笔手势结束时是否丢弃进行中的一笔:仅当手势非正常结束(第二根手指落下使+ * 单指画笔手势被取消)且屏幕路径长度短于阈值。*/-export function annotationStrokeWidth(naturalWidth: number, naturalHeight: number): number {- const base = Math.min(naturalWidth, naturalHeight) * 0.005;- return Math.min(24, Math.max(4, Math.round(base)));+export function shouldDiscardInterruptedStroke(+ gestureSucceeded: boolean,+ screenPathLength: number,+): boolean {+ return !gestureSucceeded && screenPathLength < ANNOTATION_MULTI_TOUCH_DISCARD_SCREEN_PX;}/**- * 归一化笔迹 → SVG path `d`(映射到 width×height 像素空间)。- * 单点笔迹(点按)画一个极短线段,配合 round linecap 呈现为圆点。+ * 两组笔迹是否与基线一致(放弃确认用):逐条按引用比较——笔迹一旦落下就是+ * 不可变对象,新画 / 撤销都会改变数组成员或长度。*/-export function annotationStrokeToSvgPath(- stroke: AnnotationStroke,- width: number,- height: number,-): string {- const pts = stroke.points;- if (pts.length === 0) return '';- const fmt = (p: { x: number; y: number }) =>- `${(p.x * width).toFixed(1)} ${(p.y * height).toFixed(1)}`;- if (pts.length === 1) {- const x = pts[0].x * width;- const y = pts[0].y * height;- return `M ${x.toFixed(1)} ${y.toFixed(1)} L ${(x + 0.1).toFixed(1)} ${y.toFixed(1)}`;+export function annotationStrokesEqual(+ a: readonly AnnotationStroke[],+ b: readonly AnnotationStroke[],+): boolean {+ if (a.length !== b.length) return false;+ for (let i = 0; i < a.length; i++) {+ if (a[i] !== b[i]) return false;}- return `M ${fmt(pts[0])} ${pts.slice(1).map((p) => `L ${fmt(p)}`).join(' ')}`;+ return true;+}++/**+ * 烧录 canvas 尺寸(与 WebView 内的安全钳同公式):长边超过+ * {@link ANNOTATION_MAX_BURN_DIMENSION} 时等比缩到上限内。+ */+export function annotationBurnCanvasSize(+ naturalWidth: number,+ naturalHeight: number,+): { width: number; height: number } {+ const cap = Math.min(1, ANNOTATION_MAX_BURN_DIMENSION / Math.max(naturalWidth, naturalHeight));+ return {+ width: Math.max(1, Math.round(naturalWidth * cap)),+ height: Math.max(1, Math.round(naturalHeight * cap)),+ };}/** 烧录任务输入(RN → WebView)。 */@@ -163,6 +192,12 @@ export interface AnnotationBurnInRequest {base64: string;mimeType: string;strokes: readonly AnnotationStroke[];+ /**+ * 笔迹线宽 / 坐标所在的逻辑像素空间(可选)。源图已被预缩时传入「未预缩时+ * 的 canvas 尺寸」,WebView 在该空间内按共享算法重放后等比缩到实际 canvas,+ * 线宽相对图片的比例与未预缩时一致。缺省 = 实际 canvas 尺寸。+ */+ strokeSpace?: { width: number; height: number };}/** 烧录回包(WebView → RN,JSON 字符串经 postMessage)。 */@@ -208,47 +243,36 @@ export function buildAnnotationBurnInInvocation(request: AnnotationBurnInRequest}/**- * 烧录 WebView 的宿主 HTML。canvas 重放算法与桌面 drawStrokesOnCanvas 逐行- * 对应:round cap/join、两遍绘制(先全部白描边、再全部红线,保证交叉处不出现- * 白边压红线的断裂),单点画极短线段呈圆点。JPEG 源保持 JPEG(照片转 PNG 体积- * 爆炸),其余输出 PNG。+ * 烧录 WebView 的宿主 HTML。笔迹重放直接内联共享核心的+ * ANNOTATION_CANVAS_SCRIPT(cindyDrawAnnotationStrokes:round cap/join、先全部+ * 白描边再全部红线、单点画极短线段呈圆点),与 SVG 预览 / 桌面烧录同一实现。+ * JPEG 源保持 JPEG(照片转 PNG 体积爆炸),其余输出 PNG。*/export function buildAnnotationBurnInHtml(): string {return `<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width"></head><body><script>+${ANNOTATION_CANVAS_SCRIPT}+</script>+<script>(function () {'use strict';- var STROKE_COLOR = ${JSON.stringify(ANNOTATION_STROKE_COLOR)};- var OUTLINE_COLOR = ${JSON.stringify(ANNOTATION_OUTLINE_COLOR)};- var OUTLINE_RATIO = ${ANNOTATION_OUTLINE_WIDTH_RATIO};var MAX_DIMENSION = ${ANNOTATION_MAX_BURN_DIMENSION};function post(msg) {window.ReactNativeWebView.postMessage(JSON.stringify(msg));}- function strokeWidthFor(width, height) {- var base = Math.min(width, height) * 0.005;- return Math.min(24, Math.max(4, Math.round(base)));- }-- function drawPass(ctx, strokes, width, height, color, lineWidth) {- ctx.strokeStyle = color;- ctx.lineWidth = lineWidth;- for (var s = 0; s < strokes.length; s++) {- var pts = strokes[s].points;- if (!pts || pts.length === 0) continue;- ctx.beginPath();- ctx.moveTo(pts[0].x * width, pts[0].y * height);- if (pts.length === 1) {- ctx.lineTo(pts[0].x * width + 0.1, pts[0].y * height);- } else {- for (var i = 1; i < pts.length; i++) {- ctx.lineTo(pts[i].x * width, pts[i].y * height);- }- }- ctx.stroke();+ function drawStrokes(ctx, request, width, height) {+ var space = request.strokeSpace;+ if (space && space.width > 0 && space.height > 0 && (space.width !== width || space.height !== height)) {+ // 源图已预缩:在未预缩的逻辑空间内重放(线宽按逻辑尺寸计算),再等比缩到实际 canvas。+ ctx.save();+ ctx.scale(width / space.width, height / space.height);+ cindyDrawAnnotationStrokes(ctx, request.strokes, space.width, space.height);+ ctx.restore();+ } else {+ cindyDrawAnnotationStrokes(ctx, request.strokes, width, height);}}@@ -275,11 +299,7 @@ export function buildAnnotationBurnInHtml(): string {return;}ctx.drawImage(img, 0, 0, width, height);- ctx.lineCap = 'round';- ctx.lineJoin = 'round';- var strokeWidth = strokeWidthFor(width, height);- drawPass(ctx, request.strokes, width, height, OUTLINE_COLOR, Math.round(strokeWidth * OUTLINE_RATIO));- drawPass(ctx, request.strokes, width, height, STROKE_COLOR, strokeWidth);+ drawStrokes(ctx, request, width, height);var outMime = request.mimeType === 'image/jpeg' ? 'image/jpeg' : 'image/png';var dataUrl = canvas.toDataURL(outMime, 0.92);var comma = dataUrl.indexOf(',');@@ -404,3 +424,91 @@ export function canAnnotateImageMime(mimeType: string | undefined): boolean {if (!lower.startsWith('image/')) return false;return lower !== 'image/gif' && lower !== 'image/svg+xml';}++/** Android 系统 WebView 解不了(或不稳定)的位图格式:烧录前先用原生 manipulator 转码。 */+const ANDROID_WEBVIEW_UNDECODABLE_MIMES = new Set([+ 'image/heic',+ 'image/heif',+ 'image/heic-sequence',+ 'image/heif-sequence',+ 'image/avif',+]);+/** 烧录前允许交给原生 manipulator 预缩的格式(未知 / 矢量 / 动图一律走原路径)。 */+const MANIPULATOR_PRESCALE_MIMES = new Set([+ 'image/jpeg',+ 'image/png',+ 'image/webp',+ ...ANDROID_WEBVIEW_UNDECODABLE_MIMES,+]);++/** 烧录前的源图预处理方案(expo-image-manipulator 执行)。 */+export interface AnnotationBurnSourcePlan {+ /** 等比缩放目标(只给长边一维);null = 只转码不缩尺寸。 */+ resize: { width: number } | { height: number } | null;+ /** 中间产物格式:JPEG 源保持 JPEG(最高质量),其余一律无损 PNG(保透明)。 */+ format: 'jpeg' | 'png';+ compress: number;+ /**+ * 预缩后笔迹重放的逻辑空间(= 未预缩时 WebView 会用的 canvas 尺寸),+ * 保证线宽相对图片的比例与未预缩时一致;只转码不缩时为 null。+ */+ strokeSpace: { width: number; height: number } | null;+}++/**+ * 决定烧录前要不要先把源图交给原生 manipulator 处理。返回 null = 原样交给+ * WebView(与既有路径完全一致)。+ *+ * - Android 系统 WebView 解不了 HEIC / HEIF / AVIF:先转成 WebView 能解的格式,+ * 否则用户画完才得到解码失败。+ * - 已知尺寸长边超过上传上限(2048)的大图:先按上传同口径缩到上限,WebView+ * 只需解码 / 注入 / 烧录一张小图(内存与注入体积按像素数下降)。+ * 尺寸未知时不预缩(不为探测尺寸额外解码),维持原路径。+ */+export function planAnnotationBurnSource(input: {+ mimeType: string;+ platformOS: string;+ naturalWidth?: number | null;+ naturalHeight?: number | null;+}): AnnotationBurnSourcePlan | null {+ const mime = input.mimeType.trim().toLowerCase();+ if (!MANIPULATOR_PRESCALE_MIMES.has(mime)) return null;+ const width = positiveDimension(input.naturalWidth);+ const height = positiveDimension(input.naturalHeight);+ const transcode = input.platformOS === 'android' && ANDROID_WEBVIEW_UNDECODABLE_MIMES.has(mime);+ const prescale = width > 0 && height > 0 && Math.max(width, height) > MOBILE_IMAGE_UPLOAD_MAX_LONG_EDGE;+ if (!transcode && !prescale) return null;+ return {+ resize: prescale+ ? (width >= height ? { width: MOBILE_IMAGE_UPLOAD_MAX_LONG_EDGE } : { height: MOBILE_IMAGE_UPLOAD_MAX_LONG_EDGE })+ : null,+ format: mime === 'image/jpeg' ? 'jpeg' : 'png',+ compress: 1,+ strokeSpace: prescale ? annotationBurnCanvasSize(width, height) : null,+ };+}++/**+ * 校验 manipulator 预缩产物:尺寸有效、长边不超上限、宽高比与提示尺寸一致+ * (提示来自 lightbox 解码尺寸;方向或尺寸对不上说明提示不可信,放弃预缩回退+ * 原路径,绝不带着错误的线宽空间烧录)。+ */+export function isAnnotationBurnSourceResultUsable(+ plan: AnnotationBurnSourcePlan,+ result: { width: number; height: number },+ hint: { width?: number | null; height?: number | null },+): boolean {+ if (!(result.width > 0) || !(result.height > 0)) return false;+ if (!plan.resize) return true;+ if (Math.max(result.width, result.height) > MOBILE_IMAGE_UPLOAD_MAX_LONG_EDGE) return false;+ const hintWidth = positiveDimension(hint.width);+ const hintHeight = positiveDimension(hint.height);+ if (!hintWidth || !hintHeight) return false;+ const expected = hintWidth / hintHeight;+ const actual = result.width / result.height;+ return Math.abs(actual - expected) / expected <= 0.02;+}++function positiveDimension(value: number | null | undefined): number {+ return typeof value === 'number' && Number.isFinite(value) && value > 0 ? Math.round(value) : 0;+}diff --git a/apps/mobile/src/session/mobileLocalAttachmentUpload.ts b/apps/mobile/src/session/mobileLocalAttachmentUpload.tsindex c11f46a876..8daa46bbb9 100644--- a/apps/mobile/src/session/mobileLocalAttachmentUpload.ts+++ b/apps/mobile/src/session/mobileLocalAttachmentUpload.ts@@ -69,6 +69,8 @@ export interface MobileLocalAttachmentUploadCandidate {strokes: AnnotationStroke[];sourceUri: string;sourceMimeType: string;+ /** sourceUri 本身已是带笔迹的烧录图(再编辑真相丢失后的再编辑):无新笔迹也仍是标注图。 */+ baseAnnotated?: boolean;};/*** 再编辑保存的替换语义:本 candidate 上传**成功后**宿主才移除该旧附件@@ -76,6 +78,13 @@ export interface MobileLocalAttachmentUploadCandidate {* 提前删会让用户新旧两头空(review P1)。*/replacesAttachmentId?: string;+ /**+ * 任务被**确定放弃**时回调(至多一次):托盘 / outbox 移除、整体丢弃、退屏、+ * 交接给持久发件箱后,或入队时作用域已失效。上传失败**不**回调——失败卡仍在+ * 托盘可重试,重试会重新读取 candidate.uri。上传成功走 onUploaded,也不回调。+ * 供自行生成了输入文件的调用方(标注烧录图等)按生命周期回收;管线不消费。+ */+ onAbandoned?: () => void;}/** 托盘 pending 卡的渲染数据(image → 缩略卡;file → 文件名 chip)。 */@@ -241,6 +250,8 @@ interface UploadTask {prepared?: Promise<MobileLocalAttachmentUploadCandidate>;handoff?: { promise: Promise<void>; resolve(): void };delivering?: boolean;+ /** onAbandoned 已回调(保证至多一次)。 */+ abandonNotified?: boolean;resolvedSource?: MobileLocalAttachmentUploadCandidate;outcome: Promise<TaskOutcome>;resolveOutcome: (outcome: TaskOutcome) => void;@@ -290,11 +301,24 @@ export function createMobileLocalAttachmentUploadController(}}+ function notifyAbandoned(task: UploadTask): void {+ if (task.abandonNotified) return;+ task.abandonNotified = true;+ try {+ task.candidate.onAbandoned?.();+ } catch {+ // 调用方的回收失败不能影响管线。+ }+ }+function cleanupTaskLocalUris(task: UploadTask, keepOriginal: boolean): void {if (task.handoff) {void task.handoff.promise.then(() => cleanupTaskLocalUris(task, keepOriginal));return;}+ // keepOriginal=false 只出现在任务被放弃的路径(remove / removeAll / dispose /+ // 交接提交 / 丢弃结算);失败保留卡片走 keepOriginal=true,不算放弃。+ if (!keepOriginal) notifyAbandoned(task);if (keepOriginal) task.prepared = undefined;const cleanup = task.candidate.cleanupLocalUris;if (!cleanup) return;@@ -516,7 +540,13 @@ export function createMobileLocalAttachmentUploadController(};},enqueue(candidates, opts) {- if (disposed || candidates.length === 0) return;+ if (disposed) {+ for (const candidate of candidates) {+ try { candidate.onAbandoned?.(); } catch { /* 调用方回收失败不影响管线 */ }+ }+ return;+ }+ if (candidates.length === 0) return;for (const candidate of candidates) {seq += 1;const localId = `local-attachment-upload-${seq}`;diff --git a/apps/mobile/src/session/useComposerImageAnnotations.ts b/apps/mobile/src/session/useComposerImageAnnotations.tsindex ad31b9e913..e16cbf5474 100644--- a/apps/mobile/src/session/useComposerImageAnnotations.ts+++ b/apps/mobile/src/session/useComposerImageAnnotations.ts@@ -16,19 +16,24 @@* 笔迹仍是唯一事实源——原图 + 笔迹在本地保留,再编辑时重放,重存时重新烧录。** 源文件寿命:标注/转发的源图统一复制进本 hook 私有的 annotation-src 缓存目录- * (聊天图的磁盘缓存受 LRU 管辖,直接引用会在再编辑窗口内被清理);附件移除 /- * 发送清空时 best-effort 删除。+ * (聊天图的磁盘缓存受 LRU 管辖,直接引用会在再编辑窗口内被清理)。生成文件的+ * 回收跟随上传生命周期:上传成功后挂到附件名下,随附件移除 / 发送清空 / 被替换+ * 删除;上传失败时失败卡仍在托盘、可重试,文件原样保留;只有任务被确定放弃+ * (失败卡 / 在途卡被移除、整体丢弃、退屏、交接给发件箱)才删除。*/import { useCallback, useMemo, useRef } from 'react';import { useTranslation } from 'react-i18next';-import { Alert } from 'react-native';+import { Alert, Platform } from 'react-native';import * as FileSystem from 'expo-file-system/legacy';import { useAnnotationBurnIn } from '@/session/AnnotationBurnInWebView';import {annotationBurnedFileName,imageMimeForUriFallback,+ isAnnotationBurnSourceResultUsable,isDirectSendableImageMime,+ planAnnotationBurnSource,sniffImageMimeFromBase64,+ type AnnotationBurnSourcePlan,type AnnotationStroke,} from '@/session/imageAnnotationModel';import { MOBILE_MAX_ATTACHMENTS, MOBILE_MAX_ATTACHMENT_BYTES } from '@/session/attachments';@@ -44,6 +49,8 @@ interface AnnotationEditMeta {/** 未烧录原图(annotation-src 私有副本,不受磁盘缓存 LRU 影响)。 */sourceUri: string;sourceMimeType: string;+ /** sourceUri 本身已是带笔迹的烧录图(见 candidate.annotation.baseAnnotated)。 */+ baseAnnotated?: boolean;}export interface UseComposerImageAnnotationsOptions {@@ -62,6 +69,17 @@ export interface UseComposerImageAnnotationsOptions {* 不占新槽位,不检查。*/getRemainingAttachmentSlots: () => number;+ /**+ * 按 id 读取托盘里的已上传附件(同步真源)。再编辑时据此判断替换目标是否仍在+ * 托盘、以及底图本身是否已是标注烧录图(再编辑真相丢失时保住 annotated 标)。+ */+ getAttachment?: (attachmentId: string) => RemoteSerializedAttachment | undefined;+}++/** lightbox 已解码的源图尺寸(可选提示,用于预缩 / 上传降采样决策)。 */+export interface AnnotationSourceSizeHint {+ naturalWidth?: number;+ naturalHeight?: number;}export interface UseComposerImageAnnotationsResult {@@ -86,6 +104,7 @@ export interface UseComposerImageAnnotationsResult {displayUri: string,strokes: AnnotationStroke[],mimeType?: string,+ sizeHint?: AnnotationSourceSizeHint,) => Promise<void>;/** 附件被移除时清理再编辑真相与源图副本。 */forgetAttachment: (attachmentId: string) => void;@@ -97,13 +116,42 @@ export interface UseComposerImageAnnotationsResult {const ANNOTATION_SRC_DIR = 'annotation-src';/** 烧录产物落盘目录。 */const ANNOTATION_BURNED_DIR = 'annotation-burned';++/** 入队后尚未落定的上传登记(candidate.uri → 本次生成的文件)。 */+interface PendingUploadRegistration {+ files: string[];+ /** 再编辑替换的目标附件:登记存在期间该附件不可再次编辑(防双重替换)。 */+ replacesAttachmentId?: string;+}+/**- * 上传入队后等多久没被 decorateUploadedAttachment "认领"就视为放弃(上传失败 /- * 被取消,onFailed 回调链路不传 candidate,拿不到精确的失败信号,review P2)。- * 远超正常上传耗时(乐观管线并发上限 2,单张图不会跑这么久),避免误删还在- * 传的文件。+ * 烧录前的源图预处理(expo-image-manipulator,已是本 App 既有原生依赖):+ * 按 plan 转码 / 预缩并落盘。动态 import 保证 node 单测可导入本模块。*/-const PENDING_UPLOAD_SWEEP_MS = 3 * 60 * 1000;+async function runAnnotationBurnSourcePlan(+ uri: string,+ plan: AnnotationBurnSourcePlan,+): Promise<{ uri: string; width: number; height: number }> {+ const { ImageManipulator, SaveFormat } = await import('expo-image-manipulator');+ const context = ImageManipulator.manipulate(uri);+ if (plan.resize) context.resize(plan.resize);+ const rendered = await context.renderAsync();+ try {+ const saved = await rendered.saveAsync({+ compress: plan.compress,+ format: plan.format === 'jpeg' ? SaveFormat.JPEG : SaveFormat.PNG,+ });+ return { uri: saved.uri, width: saved.width, height: saved.height };+ } finally {+ // render 结果持有原生纹理,显式释放(与 mobileImagePreprocess 同模式)。+ rendered.release();+ context.release();+ }+}++function positiveHintDimension(value: number | undefined): number | undefined {+ return typeof value === 'number' && Number.isFinite(value) && value > 0 ? Math.round(value) : undefined;+}function extForMime(mimeType: string): string {const lower = mimeType.toLowerCase();@@ -122,7 +170,7 @@ export function useComposerImageAnnotations(options: UseComposerImageAnnotationsOptions,): UseComposerImageAnnotationsResult {const { t } = useTranslation();- const { burnIn, host } = useAnnotationBurnIn();+ const { burnIn, acquireWarm, host } = useAnnotationBurnIn();const metaRef = useRef<Map<string, AnnotationEditMeta>>(new Map());/*** attachmentId → 本 hook 为该附件生成的缓存文件(烧录图 / 源图副本)。@@ -130,6 +178,12 @@ export function useComposerImageAnnotations(* (review P2);再编辑替换的新旧附件会共享源副本,删除前做引用检查。*/const generatedFilesRef = useRef<Map<string, string[]>>(new Map());+ /**+ * candidate.uri → 已入队、尚未上传成功的生成文件登记。上传成功时迁到+ * attachment.id 名下;失败时原样保留(失败卡可重试,重试读的就是这些文件);+ * 只有上传控制器确认任务被放弃(onAbandoned)才删除。+ */+ const pendingUploadsRef = useRef<Map<string, PendingUploadRegistration>>(new Map());// 回调经 ref 转发:config 对象保持稳定引用,lightbox 打开期间不重建。const optionsRef = useRef(options);optionsRef.current = options;@@ -154,7 +208,10 @@ export function useComposerImageAnnotations([srcDir, burnedDir],);- /** 删除不再被任何登记(或 extraKeep)引用的生成文件(best-effort)。 */+ /**+ * 删除不再被任何登记(附件名下 / 在途上传 / extraKeep)引用的生成文件+ * (best-effort)。在途上传(含失败待重试)仍引用的文件绝不删除。+ */const deleteUnreferencedFiles = useCallback((files: readonly string[],extraKeep: readonly string[] = [],@@ -169,10 +226,69 @@ export function useComposerImageAnnotations(}}}+ if (!referenced) {+ for (const registration of pendingUploadsRef.current.values()) {+ if (registration.files.includes(file)) {+ referenced = true;+ break;+ }+ }+ }if (!referenced) void FileSystem.deleteAsync(file, { idempotent: true }).catch(() => undefined);}}, []);+ /** 该附件是否有尚未落定的再编辑替换(上传中或失败待重试)。 */+ const hasPendingReplacement = useCallback((attachmentId: string) => {+ for (const registration of pendingUploadsRef.current.values()) {+ if (registration.replacesAttachmentId === attachmentId) return true;+ }+ return false;+ }, []);++ /**+ * 烧录源准备:Android WebView 解不了的格式先转码、已知超大图先按上传口径预缩+ * (见 planAnnotationBurnSource)。manipulator 失败或产物与提示尺寸对不上时+ * 回退原图(即既有路径)。tempUri = 仅供本次烧录的中间产物,烧完即删。+ */+ const prepareBurnSource = useCallback(async (+ source: { fileUri: string; mimeType: string },+ sizeHint: AnnotationSourceSizeHint | undefined,+ ): Promise<{+ fileUri: string;+ mimeType: string;+ strokeSpace?: { width: number; height: number };+ tempUri?: string;+ }> => {+ const original = { fileUri: source.fileUri, mimeType: source.mimeType };+ const hint = {+ width: positiveHintDimension(sizeHint?.naturalWidth),+ height: positiveHintDimension(sizeHint?.naturalHeight),+ };+ const plan = planAnnotationBurnSource({+ mimeType: source.mimeType,+ platformOS: Platform.OS,+ naturalWidth: hint.width,+ naturalHeight: hint.height,+ });+ if (!plan) return original;+ try {+ const result = await runAnnotationBurnSourcePlan(source.fileUri, plan);+ if (!isAnnotationBurnSourceResultUsable(plan, result, hint)) {+ void FileSystem.deleteAsync(result.uri, { idempotent: true }).catch(() => undefined);+ return original;+ }+ return {+ fileUri: result.uri,+ mimeType: plan.format === 'jpeg' ? 'image/jpeg' : 'image/png',+ ...(plan.strokeSpace ? { strokeSpace: plan.strokeSpace } : {}),+ tempUri: result.uri,+ };+ } catch {+ return original;+ }+ }, []);+/*** 源副本收尾(全部来源统一过):* - mime 以字节魔数为准(png/jpeg/gif/webp),扩展名 / Content-Type / hint 都@@ -253,27 +369,72 @@ export function useComposerImageAnnotations(strokes: AnnotationStroke[],mimeTypeHint: string | undefined,replaceAttachmentId: string | null,+ sizeHint?: AnnotationSourceSizeHint,): Promise<void> => {const opts = optionsRef.current;+ /** 本次提交新生成、尚未交给上传管线的文件:提交失败时回收。 */+ const createdFiles: string[] = [];+ let handedToUpload = false;try {- if (!replaceAttachmentId && opts.getRemainingAttachmentSlots() <= 0) {+ let replaceId = replaceAttachmentId;+ if (replaceId && hasPendingReplacement(replaceId)) {+ // 同一附件已有未落定的替换(上传中 / 失败待重试):再替换一次会让两份+ // 新图都留在托盘(第二次替换找不到旧附件可删)并越过附件上限。+ throw new Error(t('composer.attachments.replacementPending'));+ }+ const replacedAttachment = replaceId && opts.getAttachment ? opts.getAttachment(replaceId) : undefined;+ if (replaceId && opts.getAttachment && !replacedAttachment) {+ // 替换目标已不在托盘:按新增处理(占新槽位,受上限约束)。+ replaceId = null;+ }+ if (!replaceId && opts.getRemainingAttachmentSlots() <= 0) {throw new Error(t('composer.upload.maxAttachments', { count: MOBILE_MAX_ATTACHMENTS }));}- const replacedMeta = replaceAttachmentId ? metaRef.current.get(replaceAttachmentId) : undefined;+ const replacedMeta = replaceId ? metaRef.current.get(replaceId) : undefined;+ // 再编辑真相丢失(恢复草稿 / 排队编辑载入等)时底图本身就是带红线的烧录图:+ // 即使本次没有新笔迹,产物仍是标注图,annotated 标不能丢(之后再次编辑同样)。+ const baseIsAnnotatedBurn = !!replaceId && (replacedMeta+ ? replacedMeta.baseAnnotated === true+ : replacedAttachment?.annotated === true);+ const sourceInputUri = replacedMeta?.sourceUri ?? displayUri;const source = await materializeSource(- replacedMeta?.sourceUri ?? displayUri,+ sourceInputUri,replacedMeta?.sourceMimeType ?? mimeTypeHint,);+ if (source.fileUri !== sourceInputUri) createdFiles.push(source.fileUri);+ const hintWidth = positiveHintDimension(sizeHint?.naturalWidth);+ const hintHeight = positiveHintDimension(sizeHint?.naturalHeight);+ const annotation = strokes.length > 0 || baseIsAnnotatedBurn+ ? {+ strokes: strokes.map((s) => ({ points: [...s.points] })),+ sourceUri: source.fileUri,+ sourceMimeType: source.mimeType,+ ...(baseIsAnnotatedBurn ? { baseAnnotated: true } : {}),+ }+ : undefined;let candidate: MobileLocalAttachmentUploadCandidate;// 非直传白名单(bmp / heic 等能显示但管线不收的格式):即使无笔迹也走// 烧录通道——空笔迹烧录 = 光栅化为 PNG,对齐桌面「字节可达 + 发送时// 光栅化」模型(PR #792),否则这类图转发会被上传层类型白名单拒收。const mustRasterize = !isDirectSendableImageMime(source.mimeType);if (strokes.length > 0 || mustRasterize) {- const base64 = await FileSystem.readAsStringAsync(source.fileUri, {- encoding: FileSystem.EncodingType.Base64,- });- const burned = await burnIn({ base64, mimeType: source.mimeType, strokes });+ const burnSource = await prepareBurnSource(source, sizeHint);+ let burned;+ try {+ const base64 = await FileSystem.readAsStringAsync(burnSource.fileUri, {+ encoding: FileSystem.EncodingType.Base64,+ });+ burned = await burnIn({+ base64,+ mimeType: burnSource.mimeType,+ strokes,+ ...(burnSource.strokeSpace ? { strokeSpace: burnSource.strokeSpace } : {}),+ });+ } finally {+ if (burnSource.tempUri) {+ void FileSystem.deleteAsync(burnSource.tempUri, { idempotent: true }).catch(() => undefined);+ }+ }await ensureDir(burnedDir);const name = strokes.length > 0? annotationBurnedFileName(burned.mimeType, nextFileTag())@@ -282,6 +443,7 @@ export function useComposerImageAnnotations(await FileSystem.writeAsStringAsync(burnedUri, burned.base64, {encoding: FileSystem.EncodingType.Base64,});+ createdFiles.push(burnedUri);const burnedInfo = await FileSystem.getInfoAsync(burnedUri).catch(() => null);candidate = {kind: 'image',@@ -294,15 +456,7 @@ export function useComposerImageAnnotations(height: burned.height > 0 ? burned.height : undefined,mimeType: burned.mimeType,// 纯光栅化(无笔迹)不带标注元数据:托盘不显示画笔角标、不打 annotated 标。- ...(strokes.length > 0- ? {- annotation: {- strokes: strokes.map((s) => ({ points: [...s.points] })),- sourceUri: source.fileUri,- sourceMimeType: source.mimeType,- },- }- : {}),+ ...(annotation ? { annotation } : {}),};} else {const ext = extForMime(source.mimeType);@@ -310,29 +464,33 @@ export function useComposerImageAnnotations(kind: 'image',uri: source.fileUri,name: `image-${nextFileTag()}.${ext}`,- // 直传源无解码尺寸,至少带上真实字节数让 preprocess 的重编码判断生效。+ // 直传源至少带上真实字节数让 preprocess 的重编码判断生效;lightbox 已解码+ // 的尺寸一并带上,超大图与相册选图一样按长边上限降采样。size: source.size,+ ...(hintWidth && hintHeight ? { width: hintWidth, height: hintHeight } : {}),mimeType: source.mimeType,+ ...(annotation ? { annotation } : {}),};}- if (replaceAttachmentId) candidate.replacesAttachmentId = replaceAttachmentId;- // 入队前先按 candidate.uri(强唯一)登记本次生成的文件(review P2):- // 上传成功后 decorateUploadedAttachment 会把这份登记迁到 attachment.id- // 下;上传失败 / 被取消时没有 attachment.id 可挂,onFailed 回调链路也不- // 传 candidate,拿不到这里的登记——用超时兜底清理,给上传留足够窗口后- // 若始终没被 claimed 视为已放弃,删除对应文件。- const ownedFiles = [candidate.uri, candidate.annotation?.sourceUri]- .filter((uri): uri is string => !!uri && isHookGeneratedFile(uri));- if (ownedFiles.length > 0) {- generatedFilesRef.current.set(candidate.uri, ownedFiles);- setTimeout(() => {- const stillPending = generatedFilesRef.current.get(candidate.uri);- if (stillPending === ownedFiles) {- generatedFilesRef.current.delete(candidate.uri);- deleteUnreferencedFiles(ownedFiles);- }- }, PENDING_UPLOAD_SWEEP_MS);- }+ if (replaceId) candidate.replacesAttachmentId = replaceId;+ // 入队前先按 candidate.uri(强唯一)登记本次生成的文件:上传成功后+ // decorateUploadedAttachment 把登记迁到 attachment.id 下;上传失败时失败卡+ // 仍可重试,登记与文件原样保留;任务被确定放弃时由上传控制器回调+ // onAbandoned 删除(替换场景与旧附件共享的源副本经引用检查保留)。+ const ownedFiles = [...new Set([candidate.uri, candidate.annotation?.sourceUri]+ .filter((uri): uri is string => !!uri && isHookGeneratedFile(uri)))];+ const registration: PendingUploadRegistration = {+ files: ownedFiles,+ ...(replaceId ? { replacesAttachmentId: replaceId } : {}),+ };+ const registrationKey = candidate.uri;+ pendingUploadsRef.current.set(registrationKey, registration);+ candidate.onAbandoned = () => {+ if (pendingUploadsRef.current.get(registrationKey) !== registration) return;+ pendingUploadsRef.current.delete(registrationKey);+ deleteUnreferencedFiles(registration.files);+ };+ handedToUpload = true;opts.enqueueUploads([candidate], { token: opts.getAccessToken() });// 旧附件此刻不动:替换在上传成功回调(decorateUploadedAttachment)里执行,// 失败时旧附件与其再编辑真相原样保留,用户可重试。@@ -341,19 +499,32 @@ export function useComposerImageAnnotations(// lightbox 停留在标注模式,用户可重试或放弃。Alert.alert(t('composer.attachments.annotationSaveFailed'), err instanceof Error && err.message ? err.message : t('composer.attachments.tryAgain'));throw err;+ } finally {+ // 未交给上传管线就失败:本次新建的副本 / 烧录图无人引用,立即回收+ // (与既有附件共享的源副本经引用检查保留)。+ if (!handedToUpload && createdFiles.length > 0) deleteUnreferencedFiles(createdFiles);}- }, [materializeSource, burnIn, burnedDir, nextFileTag, isHookGeneratedFile, deleteUnreferencedFiles, t]);+ }, [+ materializeSource,+ prepareBurnSource,+ burnIn,+ burnedDir,+ nextFileTag,+ isHookGeneratedFile,+ deleteUnreferencedFiles,+ hasPendingReplacement,+ t,+ ]);const decorateUploadedAttachment = useCallback((attachment: RemoteSerializedAttachment,candidate: MobileLocalAttachmentUploadCandidate,): RemoteSerializedAttachment => {- // 接手 submitAnnotation 入队前按 candidate.uri 登记的生成文件(review P2 的- // 超时兜底注册);这里是正常路径——上传成功了,把临时登记迁到 attachment.id- // 下,不再依赖超时清理。查不到临时登记(理论不会发生,兜底)才重新计算。- const pending = generatedFilesRef.current.get(candidate.uri);- generatedFilesRef.current.delete(candidate.uri);- const ownedFiles = pending ?? [candidate.uri, candidate.annotation?.sourceUri]+ // 接手 submitAnnotation 入队前按 candidate.uri 登记的生成文件:上传成功了,+ // 把在途登记迁到 attachment.id 下。查不到登记(非本 hook 的上传)才重新计算。+ const pending = pendingUploadsRef.current.get(candidate.uri);+ pendingUploadsRef.current.delete(candidate.uri);+ const ownedFiles = pending?.files ?? [candidate.uri, candidate.annotation?.sourceUri].filter((uri): uri is string => !!uri && isHookGeneratedFile(uri));if (candidate.replacesAttachmentId) {// 再编辑替换:新图上传成功,此刻才移除旧附件(见 candidate 字段注释)。@@ -374,6 +545,7 @@ export function useComposerImageAnnotations(strokes: candidate.annotation.strokes,sourceUri: candidate.annotation.sourceUri,sourceMimeType: candidate.annotation.sourceMimeType,+ ...(candidate.annotation.baseAnnotated ? { baseAnnotated: true } : {}),});return { ...attachment, annotated: true };}, [isHookGeneratedFile, deleteUnreferencedFiles]);@@ -382,16 +554,20 @@ export function useComposerImageAnnotations(submitLabel: t('composer.attachments.sendToChat'),// 一级直发按钮(对齐桌面):不画笔迹也能把历史图转发进 composer 托盘。allowDirectSubmit: true,+ prewarm: acquireWarm,onSubmit: (_image, displayUri, strokes, context) =>- submitAnnotation(displayUri, strokes, context.mimeType, null),- }), [submitAnnotation, t]);+ submitAnnotation(displayUri, strokes, context.mimeType, null, context),+ }), [submitAnnotation, acquireWarm, t]);const trayAnnotation = useMemo<ImageLightboxAnnotationConfig>(() => ({submitLabel: t('composer.attachments.save'),initialStrokesFor: (image) => metaRef.current.get(image.key)?.strokes,+ // 替换上传未落定(上传中 / 失败待重试)的附件不可再编辑,防双重替换。+ canAnnotate: (image) => !hasPendingReplacement(image.key),+ prewarm: acquireWarm,onSubmit: (image, displayUri, strokes, context) =>- submitAnnotation(displayUri, strokes, context.mimeType, image.key),- }), [submitAnnotation, t]);+ submitAnnotation(displayUri, strokes, context.mimeType, image.key, context),+ }), [submitAnnotation, hasPendingReplacement, acquireWarm, t]);/*** 信箱消费入口:其它路由(文件浏览器 lightbox 画笔)投递的标注提交,由@@ -399,8 +575,8 @@ export function useComposerImageAnnotations(* 烧录 / 上传 / annotated 链路。*/const submitExternalAnnotation = useCallback(- (displayUri: string, strokes: AnnotationStroke[], mimeType?: string) =>- submitAnnotation(displayUri, strokes, mimeType, null),+ (displayUri: string, strokes: AnnotationStroke[], mimeType?: string, sizeHint?: AnnotationSourceSizeHint) =>+ submitAnnotation(displayUri, strokes, mimeType, null, sizeHint),[submitAnnotation],);@@ -419,6 +595,8 @@ export function useComposerImageAnnotations(}, [deleteUnreferencedFiles]);const forgetAllAttachments = useCallback(() => {+ // 只清附件名下的文件;在途上传(含失败待重试)的文件由上传生命周期回收+ // (放弃时 onAbandoned),这里删会让仍在跑 / 可重试的任务读不到源文件。const allFiles = [...generatedFilesRef.current.values()].flat();generatedFilesRef.current.clear();metaRef.current.clear();diff --git a/apps/mobile/src/session/useMobileLocalAttachments.ts b/apps/mobile/src/session/useMobileLocalAttachments.tsindex 08716b8ca7..2c1bd991a8 100644--- a/apps/mobile/src/session/useMobileLocalAttachments.ts+++ b/apps/mobile/src/session/useMobileLocalAttachments.ts@@ -295,6 +295,8 @@ export function useMobileLocalAttachments(getToken: () => optionsRef.current.getAccessToken(),});if (candidate.cleanupLocalUris) void candidate.cleanupLocalUris(localUris).catch(() => undefined);+ // 结果被拒收 = 任务被放弃:让自行生成输入文件的调用方回收(见 onAbandoned)。+ try { candidate.onAbandoned?.(); } catch { /* 回收失败不影响管线 */ }return;}// 发送后气泡的本地缩略图兜底:消息里持久化的是 cindy-oss-attach:// 中转引用,@@ -329,6 +331,7 @@ export function useMobileLocalAttachments(uploadedSourcesRef.current.set(attachment.id, {...candidate, uri: stageUri, name: attachment.name, mimeType: attachment.mimeType,size: attachment.size, resolve: undefined, skipPreprocess: true, cleanupLocalUris: undefined,+ onAbandoned: undefined,});optionsRef.current.onUploaded(attachment, deliveredCandidate, localId);if (candidate.cleanupLocalUris) {@@ -380,7 +383,13 @@ export function useMobileLocalAttachments(candidates: readonly MobileLocalAttachmentUploadCandidate[],opts: { token: string | Promise<string | null> },) => {- if (!isAttachmentScopeActive()) return;+ if (!isAttachmentScopeActive()) {+ // 作用域已失效,任务不会入队:视同放弃,让自行生成输入文件的调用方回收。+ for (const candidate of candidates) {+ try { candidate.onAbandoned?.(); } catch { /* 回收失败不影响管线 */ }+ }+ return;+ }controller.enqueue(candidates.map((candidate) => ({...candidate,