Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(media): retain and play chat videos inline
Chris26e273e6880c
diff --git a/apps/desktop/src/main/device-link/__tests__/filePeer.test.ts b/apps/desktop/src/main/device-link/__tests__/filePeer.test.tsindex dd4a3a4bae..72fdb42b06 100644--- a/apps/desktop/src/main/device-link/__tests__/filePeer.test.ts+++ b/apps/desktop/src/main/device-link/__tests__/filePeer.test.ts@@ -371,6 +371,15 @@ describe('authorized file peer source', () => {url: 'xdt-file://local/?path=/test',})) as { ticket: string; size: number; version: string };}+ // Unit regression: an authorized path without a MIME was sent as octet-stream,+ // so the mobile player rejected a completely downloaded MP4.+ it.each([['.mp4', 'video/mp4'], ['.MP4', 'video/mp4'], ['mp4', 'video/mp4'], ['.webm', 'video/webm'], ['.html', 'text/html']])(+ 'returns playable direct-file metadata for extension %s', async (uploadExtHint, mimeType) => {+ mock.resolve.mockResolvedValue({ absPath: file, uploadExtHint, maxBytes: 100 });+ const { connection } = await connect();+ await expect(open(connection)).resolves.toMatchObject({ mimeType, size: 5 });+ },+ );const read = (connection: string, ticket: string, offset: number) =>mock.handlers.get('file-peer:host:read')!({}, connection, ticket, offset);it('keeps negotiated connections until release, isolates transport failure and still expires stalled files', async () => {diff --git a/apps/desktop/src/main/device-link/filePeer.ts b/apps/desktop/src/main/device-link/filePeer.tsindex 54702d64c2..801af18c4b 100644--- a/apps/desktop/src/main/device-link/filePeer.ts+++ b/apps/desktop/src/main/device-link/filePeer.ts@@ -34,6 +34,7 @@ import { FILE_PEER_LOCAL, type FilePeerCommand } from '../../shared/filePeer';import { DesktopCaptureWindow } from '../remote-desktop/captureWindow';import { loadDesktopIceServers } from '../remote-desktop/iceConfig';import { resolveAuthorizedMedia } from './mediaFetch';+import { mimeOf } from './mediaMime';import { readDeviceLinkSettings } from './settings-store';import { captureDataOwnerBroadcastScope, isDataOwnerBroadcastScopeCurrent } from './broadcast-tap';import { captureFilePeerScope, invalidateFilePeerScopes } from './filePeerScope';@@ -495,23 +496,10 @@ async function handleFilePeerRequest(if (!stat.isFile() || stat.size > (authorized.maxBytes ?? FILE_PEER_MAX_BYTES))throw new Error('FILE_PEER_SIZE');const ticket = randomUUID();- const ext = (authorized.uploadExtHint ?? path.extname(authorized.absPath)).toLowerCase();- const mimeType =- authorized.mimeType ??- (- {- '.html': 'text/html',- '.htm': 'text/html',- '.png': 'image/png',- '.jpg': 'image/jpeg',- '.jpeg': 'image/jpeg',- '.webp': 'image/webp',- '.svg': 'image/svg+xml',- '.gif': 'image/gif',- '.pdf': 'application/pdf',- } as Record<string, string>- )[ext] ??- 'application/octet-stream';+ const ext = (authorized.uploadExtHint ?? path.extname(authorized.absPath)).replace(/^\./, '').toLowerCase();+ // Keep direct downloads aligned with mediaFetch, including video/audio.+ const mimeType = authorized.mimeType ??+ (ext === 'html' || ext === 'htm' ? 'text/html' : mimeOf(ext));const source: Source = {connection: r.connection,file,diff --git a/apps/desktop/src/main/device-link/mediaMime.ts b/apps/desktop/src/main/device-link/mediaMime.tsnew file mode 100644index 0000000000..b4499971ff--- /dev/null+++ b/apps/desktop/src/main/device-link/mediaMime.ts@@ -0,0 +1,28 @@+/** Shared extension fallback for authorized direct files and OSS media exports. */+const MIME_BY_EXT: Record<string, string> = {+ png: 'image/png',+ jpg: 'image/jpeg',+ jpeg: 'image/jpeg',+ gif: 'image/gif',+ webp: 'image/webp',+ svg: 'image/svg+xml',+ bmp: 'image/bmp',+ ico: 'image/x-icon',+ mp4: 'video/mp4',+ webm: 'video/webm',+ mov: 'video/quicktime',+ mkv: 'video/x-matroska',+ mp3: 'audio/mpeg',+ wav: 'audio/wav',+ ogg: 'audio/ogg',+ m4a: 'audio/mp4',+ pdf: 'application/pdf',+ txt: 'text/plain',+ json: 'application/json',+};++/** Bare lowercase extension; an authoritative MIME from the resolver takes precedence. */+export function mimeOf(ext: string): string {+ return MIME_BY_EXT[ext] ?? 'application/octet-stream';+}+export { MIME_BY_EXT };diff --git a/apps/desktop/src/main/device-link/mediaTransfer.ts b/apps/desktop/src/main/device-link/mediaTransfer.tsindex a0a58ff2e4..4e95f330f8 100644--- a/apps/desktop/src/main/device-link/mediaTransfer.ts+++ b/apps/desktop/src/main/device-link/mediaTransfer.ts@@ -39,6 +39,8 @@ import { sharedTaskMediaId } from './sharedTaskMediaContext.js';import { describeErrorChain } from '../utils/errorChain.js';import { createLogger } from '../logger.js';import { sourceFileReadError, withSourceFileHandleContext } from './sourceFileError';+import { mimeOf, MIME_BY_EXT } from './mediaMime';+export { mimeOf } from './mediaMime';const log = createLogger('device-link:mediaTransfer');@@ -55,44 +57,12 @@ const PRESIGN_PUT_PATH = '/api/device-link/media/presign-put';const PRESIGN_GET_PATH = '/api/device-link/media/presign-get';const DELETE_PATH = '/api/device-link/media';-/**- * device-link 媒体关心的 ext→mime(覆盖图片/视频/音频/常见文档)。- * 仅作 OSS 存储 Content-Type 的 best-effort 兜底;入方向权威 mime 来自被控端- * cache-store resolver(readFile 返回),不依赖本表。未知 → octet-stream。- */-const MIME_BY_EXT: Record<string, string> = {- png: 'image/png',- jpg: 'image/jpeg',- jpeg: 'image/jpeg',- gif: 'image/gif',- webp: 'image/webp',- svg: 'image/svg+xml',- bmp: 'image/bmp',- ico: 'image/x-icon',- mp4: 'video/mp4',- webm: 'video/webm',- mov: 'video/quicktime',- mkv: 'video/x-matroska',- mp3: 'audio/mpeg',- wav: 'audio/wav',- ogg: 'audio/ogg',- m4a: 'audio/mp4',- pdf: 'application/pdf',- txt: 'text/plain',- json: 'application/json',-};-/** 从文件路径取裸扩展名(无点、小写),无扩展名 → 'bin'。 */function extOf(localPath: string): string {const e = path.extname(localPath).replace(/^\.+/, '').toLowerCase();return e || 'bin';}-/** ext → mime,未知回落 application/octet-stream。 */-export function mimeOf(ext: string): string {- return MIME_BY_EXT[ext] ?? 'application/octet-stream';-}-interface PresignPutResponse {putUrl: string;key: string;diff --git a/apps/desktop/src/main/security/__tests__/csp.test.ts b/apps/desktop/src/main/security/__tests__/csp.test.tsindex 8dd3b19f0d..545cf02f1a 100644--- a/apps/desktop/src/main/security/__tests__/csp.test.ts+++ b/apps/desktop/src/main/security/__tests__/csp.test.ts@@ -75,7 +75,9 @@ describe('buildContentSecurityPolicy — shared invariants', () => {for (const scheme of ['xdt-image:', 'xdt-file:', 'xdt-video:', 'xdt-model:', 'cindy-remote-media:', 'cindy-media:']) {expect(d['img-src']).toContain(scheme);}- for (const scheme of ['xdt-audio:', 'xdt-video:', 'cindy-remote-media:', 'cindy-media:']) {+ // Local MP4 answers resolve to xdt-file, not the generated-video store.+ // Missing this source blocks playback before the file handler sees a request.+ for (const scheme of ['xdt-file:', 'xdt-audio:', 'xdt-video:', 'cindy-remote-media:', 'cindy-media:']) {expect(d['media-src']).toContain(scheme);}// data: / blob: needed for canvas, object URLs, inline assets.diff --git a/apps/desktop/src/main/security/csp.ts b/apps/desktop/src/main/security/csp.tsindex 96e0c2b324..0590d7b3f8 100644--- a/apps/desktop/src/main/security/csp.ts+++ b/apps/desktop/src/main/security/csp.ts@@ -55,6 +55,8 @@ const CUSTOM_IMG_SCHEMES = ['cindy-media:',];const CUSTOM_MEDIA_SCHEMES = [+ // Task-file video uses the same guarded local-file handler as image previews.+ 'xdt-file:','xdt-audio:','xdt-video:','xdt-image:',diff --git a/apps/desktop/src/renderer/components/chat/ChatVideoView.tsx b/apps/desktop/src/renderer/components/chat/ChatVideoView.tsxindex 84b37f862f..910d5a7be6 100644--- a/apps/desktop/src/renderer/components/chat/ChatVideoView.tsx+++ b/apps/desktop/src/renderer/components/chat/ChatVideoView.tsx@@ -1,25 +1,9 @@-/**- * ChatVideoView- * ---------------------------------------------------------------------------- * 聊天流里视频渲染的统一组件,与 ChatImageView 对位:art video tools- * (seedance / 未来 kling/luma) 的产物视频都走这里。封面 + 点击播放,大图- * 走 VideoLightbox。- *- * 行为契约:- * - 默认显示首帧封面(<video preload="metadata">,chromium 自取首帧),- * 上盖一个半透明大 Play 按钮;<video> 本身**不挂 controls**——- * 原位永远是封面而不是迷你播放器。- * - 点击 → 打开 VideoLightbox 全屏播放(controls + autoPlay + loop)。- * - 失败降级 → ImageMissingPlaceholder 同款样式,文案换"视频不可用"。- * - 右键(仅 xdt-video://):复制视频 / 打开视频所在目录。复制走 OS 原生- * 文件剪贴板(Win/Mac),粘贴出来是文件本体,不是字节流。- *- * variant 的尺寸 / 边框沿用与 image 同款,以后调样式只用动一处。- */+/** Chat-stream video player. Native controls keep play, pause and seeking in+ * the message; fullscreen remains the browser player's optional action. */import { CHAT_FOCUS_CLASS } from './chatChrome';-import { useRef, useState } from 'react';-import { Play, FolderOpen, VideoOff, Copy } from 'lucide-react';+import { useEffect, useRef, useState } from 'react';+import { FolderOpen, VideoOff, Copy } from 'lucide-react';import { useTranslation } from 'react-i18next';import { cn } from '@/lib/utils';import { toast } from '@/lib/toast';@@ -30,7 +14,6 @@ import {DropdownMenuTrigger,} from '@/components/ui/dropdown-menu';import { Tooltip } from '@/components/ui/tooltip';-import { VideoLightbox } from './VideoLightbox';import { useRemoteMediaUrl } from '@/hooks/useRemoteMediaUrl';export type ChatVideoVariant = 'tool-output';@@ -64,14 +47,37 @@ const VARIANT_STYLES: Record<export function ChatVideoView({ src, filename, variant, sessionId }: ChatVideoViewProps) {const { t } = useTranslation();- const [lightboxOpen, setLightboxOpen] = useState(false);- const previewTriggerRef = useRef<HTMLDivElement>(null);- const [errored, setErrored] = useState(false);+ const videoRef = useRef<HTMLVideoElement>(null);+ const [failedSrc, setFailedSrc] = useState<string | null>(null);const [menuPos, setMenuPos] = useState<{ x: number; y: number } | null>(null);// 远程会话改写到 cindy-remote-media://(OSS range 流式);本地原样。下游统一用 displaySrc。const displaySrc = useRemoteMediaUrl(src, sessionId);+ useEffect(() => { setFailedSrc(null); }, [displaySrc]);- if (errored) {+ // Navigation and hidden/recycled rows must not leave an invisible player running.+ useEffect(() => {+ const video = videoRef.current;+ if (!video) return;+ const observer = new IntersectionObserver(([entry]) => {+ if (!entry.isIntersecting) video.pause();+ });+ const onVisibility = () => { if (document.hidden) video.pause(); };+ observer.observe(video);+ document.addEventListener('visibilitychange', onVisibility);+ return () => { observer.disconnect(); document.removeEventListener('visibilitychange', onVisibility); video.pause(); };+ }, [displaySrc, failedSrc]);++ // A transient failure should recover when the window/network becomes available;+ // source patches also get a fresh player instead of inheriting an old failure.+ useEffect(() => {+ if (failedSrc !== displaySrc) return;+ const retry = () => setFailedSrc(null);+ window.addEventListener('online', retry);+ window.addEventListener('focus', retry);+ return () => { window.removeEventListener('online', retry); window.removeEventListener('focus', retry); };+ }, [displaySrc, failedSrc]);++ if (failedSrc === displaySrc) {return <VideoMissingPlaceholder filename={filename} />;}@@ -99,28 +105,9 @@ export function ChatVideoView({ src, filename, variant, sessionId }: ChatVideoVireturn (<>- {/* 容器 wraps <video> 以叠加 Play 按钮,本身就是点击区。- <video> preload=metadata 让 chromium 自动渲染首帧作为封面,- NOT 挂 controls — 用户一点就走 lightbox 大图播放。 */}<div- ref={previewTriggerRef}- // self-start 与 ChatImageView tool-output 同理:防止 MessageStream 的- // flex-col 包裹层把容器拉宽到超过 <video> 实际宽度(点击区/边框错位)。- className={cn(- 'relative inline-block self-start rounded-xl cursor-pointer hover:opacity-90 transition-opacity motion-reduce:transition-none',- CHAT_FOCUS_CLASS,- )}- role="button"- aria-label={filename}- tabIndex={0}- onKeyDown={(event) => {- if (event.key === 'Enter' || event.key === ' ') {- event.preventDefault();- event.currentTarget.click();- }- }}+ className="relative inline-block self-start min-w-0 rounded-xl"style={{ maxWidth: style.maxWidth, maxHeight: style.maxHeight }}- onClick={() => setLightboxOpen(true)}onContextMenu={(e) => {if (!canRevealInFolder) return;e.preventDefault();@@ -129,36 +116,17 @@ export function ChatVideoView({ src, filename, variant, sessionId }: ChatVideoVi}}><video+ key={displaySrc}+ ref={videoRef}src={displaySrc}- // 关键:preload metadata 让 chromium 拉够元信息+首帧,但不预加载整段。+ aria-label={filename}preload="metadata"- // muted 让浏览器允许在更多策略下自动取首帧(部分自动播放策略- // 与 muted 强相关;我们这里没 autoplay,但 muted 让首帧抽取更稳)。- muted- // playsInline 防止部分场景下被吞成全屏播放器。+ controlsplaysInlinestyle={style}- className={cn(className, 'block pointer-events-none')}- // 不挂 controls — 原位是封面状态。- onError={() => setErrored(true)}+ className={cn(className, 'block', CHAT_FOCUS_CLASS)}+ onError={() => setFailedSrc(displaySrc)}/>- {/* 大 Play 按钮浮层 — 居中半透明圆 + lucide play 图标。- 点击落到外层 div,不需要这层自己处理 click。 */}- <div- className="absolute inset-0 flex items-center justify-center pointer-events-none"- aria-hidden- >- <div- className={cn(- 'flex items-center justify-center',- 'w-14 h-14 rounded-full',- 'bg-black/55 backdrop-blur-sm',- 'shadow-[var(--shadow-menu)]',- )}- >- <Play className="w-7 h-7 text-white fill-white" />- </div>- </div></div>{canRevealInFolder ? (<DropdownMenu@@ -193,15 +161,6 @@ export function ChatVideoView({ src, filename, variant, sessionId }: ChatVideoVi</DropdownMenuContent></DropdownMenu>) : null}- {lightboxOpen && (- <VideoLightbox- src={displaySrc}- onClose={() => {- setLightboxOpen(false);- previewTriggerRef.current?.focus({ preventScroll: true });- }}- />- )}</>);}diff --git a/apps/desktop/src/renderer/components/chat/__tests__/ChatVideoView.test.tsx b/apps/desktop/src/renderer/components/chat/__tests__/ChatVideoView.test.tsxnew file mode 100644index 0000000000..000d10713f--- /dev/null+++ b/apps/desktop/src/renderer/components/chat/__tests__/ChatVideoView.test.tsx@@ -0,0 +1,49 @@+// @vitest-environment jsdom+// Tier 1: the production message player must expose inline transport controls,+// recover from a failed source patch, and stop when its message leaves view.+// Markdown renderer tests mock this component, so they cannot catch these bugs.+import { afterEach, beforeEach, expect, it, vi } from 'vitest';+import { cleanup, fireEvent, render } from '@testing-library/react';+import { Tooltip } from '@/components/ui/tooltip';+import { ChatVideoView } from '../ChatVideoView';+vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key }) }));+vi.mock('@/hooks/useRemoteMediaUrl', () => ({ useRemoteMediaUrl: (url: string) => url }));+let visibility: IntersectionObserverCallback;+let pause: ReturnType<typeof vi.spyOn>;+beforeEach(() => {+ vi.stubGlobal('IntersectionObserver', class {+ constructor(callback: IntersectionObserverCallback) { visibility = callback; }+ observe() {} disconnect() {}+ });+ pause = vi.spyOn(HTMLMediaElement.prototype, 'pause').mockImplementation(() => {});+});+afterEach(() => { cleanup(); vi.restoreAllMocks(); vi.unstubAllGlobals(); });+const props = { filename: 'Clip', variant: 'tool-output' as const, src: 'xdt-file://local/?path=%2Fclip.mp4' };+it('provides inline browser transport controls without a click-to-lightbox wrapper', () => {+ const view = render(<ChatVideoView {...props} />, { wrapper: Tooltip.Provider });+ const video = view.container.querySelector('video')!;+ expect(video.controls).toBe(true);+ expect(video.playsInline).toBe(true);+ fireEvent.click(video);+ expect(view.container.querySelectorAll('video')).toHaveLength(1);+ expect(view.queryByRole('dialog')).toBeNull();+ expect(view.queryByRole('button', { name: 'Clip' })).toBeNull();+});+it('a patched source recovers from a previous decoding error', () => {+ const view = render(<ChatVideoView {...props} />, { wrapper: Tooltip.Provider });+ fireEvent.error(view.container.querySelector('video')!);+ expect(view.container.querySelector('video')).toBeNull();+ view.rerender(<ChatVideoView {...props} src="https://media.test/recovered.mp4" />);+ expect(view.container.querySelector('video')?.src).toBe('https://media.test/recovered.mp4');+});+it('retries a failed source when the network returns', () => {+ const view = render(<ChatVideoView {...props} />, { wrapper: Tooltip.Provider });+ fireEvent.error(view.container.querySelector('video')!);+ fireEvent(window, new Event('online'));+ expect(view.container.querySelector('video')?.src).toBe(props.src);+});+it('pauses video when its message leaves view', () => {+ render(<ChatVideoView {...props} />, { wrapper: Tooltip.Provider });+ visibility([{ isIntersecting: false }] as IntersectionObserverEntry[], {} as IntersectionObserver);+ expect(pause).toHaveBeenCalledOnce();+});diff --git a/apps/mobile/app/sessions/[sessionId].tsx b/apps/mobile/app/sessions/[sessionId].tsxindex e278716589..d361fdd94d 100644--- a/apps/mobile/app/sessions/[sessionId].tsx+++ b/apps/mobile/app/sessions/[sessionId].tsx@@ -1988,6 +1988,12 @@ function SessionScreenContent({ beforeSessionSwitchRef, newSessionActionRef }: S}const resolved = await resolveMobileRemoteMedia(media, {fetchRemoteMedia: deps.maker.fetchRemoteMedia,+ ...(diskCache ? { retainLocalVideo: async (video: MobileResolvedRemoteMedia) => {+ const stored = await diskCache.store(bareDiskSource, video.url, video.mimeType, video.size);+ const copy = stored ? await diskCache.lookup(bareDiskSource) : null;+ if (!copy) throw new Error('VIDEO_CACHE_FAILED');+ return { ...video, url: copy.uri, expiresAt: REMOTE_MEDIA_NEVER_EXPIRES };+ } } : {}),presignGet: (ossKey) => deps.auth.apiFetch<MobileRemoteMediaPresignResult>('/api/device-link/media/presign-get',{ baseUrl: DEVICE_LINK_API_BASE_URL, method: 'POST', body: { key: ossKey } },diff --git a/apps/mobile/src/__tests__/NativeVideoPlayer.test.tsx b/apps/mobile/src/__tests__/NativeVideoPlayer.test.tsxindex 2abfb02974..d256052bb3 100644--- a/apps/mobile/src/__tests__/NativeVideoPlayer.test.tsx+++ b/apps/mobile/src/__tests__/NativeVideoPlayer.test.tsx@@ -16,6 +16,7 @@ function createPlayer() {duration: 0,bufferedPosition: -1,pause: vi.fn(),+ play: vi.fn(),addListener(event: string, listener: Listener) {if (!listeners.has(event)) listeners.set(event, new Set());listeners.get(event)!.add(listener);@@ -48,10 +49,11 @@ vi.mock('expo', () => ({},}));vi.mock('expo-video', () => ({- useVideoPlayer: () => {+ useVideoPlayer: (_url: string, setup?: (player: any) => void) => {const [player] = useState(() => {const next = createPlayer();h.players.push(next);+ setup?.(next);return next;});return player;@@ -76,13 +78,14 @@ import { NativeVideoPlayer } from '@/session/NativeVideoPlayer';let container: HTMLDivElement;let root: Root;-function render(props: { visible: boolean; onError?: (detail: string) => void }) {+function render(props: { visible: boolean; autoPlay?: boolean; onError?: (detail: string) => void }) {act(() => {root.render(el(NativeVideoPlayer, {onError: props.onError ?? vi.fn(),testID: 'player',url: 'https://example.test/movie.mp4',visible: props.visible,+ autoPlay: props.autoPlay,}));});return h.players[h.players.length - 1];@@ -104,6 +107,14 @@ afterEach(() => {});describe('NativeVideoPlayer', () => {+ it('starts inline playback only after an explicit play action on a visible message', () => {+ const player = render({ visible: true, autoPlay: true });+ expect(player.play).toHaveBeenCalledOnce();+ render({ visible: false, autoPlay: true });+ expect(player.pause).toHaveBeenCalledOnce();+ render({ visible: true, autoPlay: true });+ expect(player.play).toHaveBeenCalledOnce();+ });it('shows buffered percent only after a sustained loading state and hides it on resume', () => {const player = render({ visible: true });player.duration = 100;diff --git a/apps/mobile/src/__tests__/inlineVideoPlayer.test.tsx b/apps/mobile/src/__tests__/inlineVideoPlayer.test.tsxnew file mode 100644index 0000000000..a0369a4e6c--- /dev/null+++ b/apps/mobile/src/__tests__/inlineVideoPlayer.test.tsx@@ -0,0 +1,86 @@+// @vitest-environment jsdom+// Tier 1: message video previously opened a viewer instead of an inline player.+// Unlike NativeVideoPlayer tests, this exercises click-to-resolve, cancellation+// and retry through the production message adapter, without fetching on mount.+import { act, createElement as el } from 'react';+import { createRoot, type Root } from 'react-dom/client';+import { afterEach, beforeEach, expect, it, vi } from 'vitest';+import { InlineVideoPlayer } from '@/session/InlineVideoPlayer';+const h = vi.hoisted(() => ({ player: null as any }));+vi.mock('react-native', () => ({+ View: ({ children }: any) => el('div', null, children),+ Pressable: ({ children, onPress, disabled, testID }: any) => el('button', { onClick: onPress, disabled, 'data-testid': testID }, children),+ ActivityIndicator: () => el('span', null, 'loading'),+ AppState: { currentState: 'active', addEventListener: () => ({ remove() {} }) },+}));+vi.mock('expo-router', () => ({ useIsFocused: () => true }));+vi.mock('@/session/NativeVideoPlayer', () => ({ NativeVideoPlayer: (props: any) => { h.player = props; return el('video', { src: props.url, style: Object.assign({}, ...props.style) }); } }));+vi.mock('@/session/remoteMedia', () => ({ isDirectPreviewableMediaUrl: (url: string) => /^https:/.test(url) }));+vi.mock('@/components/AppText', () => ({ Text: ({ children }: any) => el('span', null, children) }));+vi.mock('lucide-react-native', () => ({ Play: () => null }));+vi.mock('@/theme', () => ({ useTheme: () => ({ colors: {} }) }));+vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key }) }));+let root: Root;+let container: HTMLDivElement;+const url = 'xdt-file://open?path=%2Foutside%2Fclip.mp4';+const result = { url: 'https://media.test/clip.mp4', mimeType: 'video/mp4', previewable: true, ossKey: '', size: 12, expiresAt: '9999-12-31' };+function render(resolve: any, visible = true, source = url) {+ act(() => root.render(el(InlineVideoPlayer, { url: source, label: 'Clip', width: 300, visible, resolve })));+}+async function click() { await act(async () => container.querySelector('button')!.click()); }+beforeEach(() => { Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true }); container = document.createElement('div'); root = createRoot(container); h.player = null; });+afterEach(() => act(() => root.unmount()));++it('fetches only on play and mounts the resolved video in the message with native autoplay', async () => {+ const resolve = vi.fn().mockResolvedValue(result);+ render(resolve);+ expect(resolve).not.toHaveBeenCalled();+ await click();+ expect(resolve).toHaveBeenCalledWith({ kind: 'video', url, previewable: false }, expect.objectContaining({ front: true }));+ expect(container.querySelector('video')?.src).toBe(result.url);+ expect(h.player.autoPlay).toBe(true);+ render(resolve, false);+ expect(h.player.visible).toBe(false);+});+// Unit regression: the original 1146×848 recording was boxed into 16:9 with+// heavy black bars. Native-size changes must fit the picture without cropping;+// portrait clips must stay bounded. Earlier adapter tests did not cover sizing.+it('fits native video dimensions and bounds portrait height without stretching', async () => {+ render(vi.fn().mockResolvedValue(result)); await click();+ act(() => h.player.onVideoSize({ width: 1146, height: 848 }));+ const video = container.querySelector('video')!;+ expect(parseFloat(video.style.width)).toBeCloseTo(300);+ expect(parseFloat(video.style.height)).toBeCloseTo(300 * 848 / 1146);+ act(() => h.player.onVideoSize({ width: 1080, height: 1920 }));+ expect(parseFloat(video.style.height)).toBe(400);+ expect(parseFloat(video.style.width)).toBe(225);+});+it('retries failed playback through the resolver with a fresh source', async () => {+ const resolve = vi.fn().mockResolvedValue(result);+ render(resolve); await click();+ act(() => h.player.onError('expired'));+ expect(container.querySelector('video')).toBeNull();+ expect(container.textContent).toContain('message.renderer.retryPreview');+ await click();+ expect(resolve.mock.calls[1][1].forceRefresh).toBe(true);+ expect(container.querySelector('video')).not.toBeNull();+});+it('cancels a hidden row and ignores its late resolved file', async () => {+ let finish!: (value: typeof result) => void;+ const resolve = vi.fn((_media: unknown, _options: { signal: AbortSignal }) => new Promise(r => { finish = r; }));+ render(resolve); await click();+ render(resolve, false);+ expect(resolve.mock.calls[0][1].signal.aborted).toBe(true);+ await act(async () => finish(result));+ expect(container.querySelector('video')).toBeNull();+});+it('rejects a non-video response and plays direct video without remote file access', async () => {+ const resolve = vi.fn().mockResolvedValue({ ...result, mimeType: 'text/html', previewable: false });+ render(resolve); await click();+ expect(container.querySelector('video')).toBeNull();+ expect(container.textContent).toContain('message.renderer.retryPreview');+ act(() => root.render(null));+ render(resolve, true, result.url); await click();+ expect(resolve).toHaveBeenCalledTimes(1);+ expect(container.querySelector('video')?.src).toBe(result.url);+});diff --git a/apps/mobile/src/__tests__/remoteMedia.test.ts b/apps/mobile/src/__tests__/remoteMedia.test.tsindex 73050a71cb..459c44431a 100644--- a/apps/mobile/src/__tests__/remoteMedia.test.ts+++ b/apps/mobile/src/__tests__/remoteMedia.test.ts@@ -12,6 +12,27 @@ import {} from "@/session/remoteMedia";describe("mobile remote media", () => {+ // Regression: video streaming forced OSS even when authenticated peer bytes+ // were available. Retention must finish before the staging lease is released.+ it.each([false, true])('owns peer video playback beyond its staging lifetime (copy fails=%s)', async (fails) => {+ const { recordPeerMedia } = await import('@/device-link/peerFileRegistry');+ const staged = { ossKey: '', mimeType: 'video/mp4', size: 3 };+ const dispose = vi.fn();+ recordPeerMedia(staged, 'file:///staging/clip.mp4', dispose);+ const fetchRemoteMedia = vi.fn(async () => staged);+ const retainLocalVideo = vi.fn(async (media: import('@/session/remoteMedia').MobileResolvedRemoteMedia) => {+ expect(dispose).not.toHaveBeenCalled();+ if (fails) throw new Error('disk full');+ return { ...media, url: 'file:///retained/clip.mp4', expiresAt: REMOTE_MEDIA_NEVER_EXPIRES };+ });+ const result = resolveMobileRemoteMedia({ kind: 'video', url: 'xdt-file://open?path=/clip.mp4' }, {+ fetchRemoteMedia, presignGet: vi.fn(), retainLocalVideo,+ });+ if (fails) await expect(result).rejects.toThrow('disk full');+ else await expect(result).resolves.toMatchObject({ url: 'file:///retained/clip.mp4', previewable: true });+ expect(fetchRemoteMedia).toHaveBeenCalledWith('xdt-file://open?path=/clip.mp4', expect.objectContaining({ stream: false, revalidate: true }));+ expect(dispose).toHaveBeenCalledOnce();+ });it('retains a background fallback and releases its staging copy and cloud object', async () => {const { recordPeerMedia } = await import('@/device-link/peerFileRegistry');const local = { ossKey: '', mimeType: 'application/pdf', size: 3 };diff --git a/apps/mobile/src/__tests__/remoteMediaDiskCache.test.ts b/apps/mobile/src/__tests__/remoteMediaDiskCache.test.tsindex b625e2503a..add24a8971 100644--- a/apps/mobile/src/__tests__/remoteMediaDiskCache.test.ts+++ b/apps/mobile/src/__tests__/remoteMediaDiskCache.test.ts@@ -36,6 +36,19 @@ const URL_B = 'xdt-image://cache/b.png';const URL_C = 'xdt-image://cache/c.png';describe('remoteMediaDiskCache', () => {+ // Unit regression: a downloaded MP4 retained as .img is rejected by AVPlayer.+ // Exercise the real cache write/lookup, not the MIME table in isolation.+ it.each([['video/mp4', 'mp4'], ['video/quicktime', 'mov'], ['video/webm', 'webm']])(+ 'retains playable filenames for %s through history reopen', async (mimeType, ext) => {+ const { io } = memoryIO();+ const cache = createRemoteMediaDiskCache(io);+ await cache.store('video-history', 'file:///staging/video', mimeType, 1000);+ const reopened = createRemoteMediaDiskCache(io);+ const hit = await reopened.lookup('video-history');+ expect(hit?.uri.endsWith(`.${ext}`)).toBe(true);+ expect(hit).toMatchObject({ mimeType, size: 1000 });+ },+ );it('preserves the previous version when replacement storage is unavailable', async () => {const h = memoryIO();let available = 10_000;diff --git a/apps/mobile/src/session/InlineVideoPlayer.tsx b/apps/mobile/src/session/InlineVideoPlayer.tsxnew file mode 100644index 0000000000..2101e8055f--- /dev/null+++ b/apps/mobile/src/session/InlineVideoPlayer.tsx@@ -0,0 +1,82 @@+import { useEffect, useRef, useState } from 'react';+import { ActivityIndicator, AppState, Pressable, View } from 'react-native';+import { useIsFocused } from 'expo-router';+import { useTranslation } from 'react-i18next';+import { Play } from 'lucide-react-native';+import { Text } from '@/components/AppText';+import { useTheme } from '@/theme';+import { iconSize, iconStroke, lineHeight, radius, spacing, typeScale } from '@/theme/tokens';+import { NativeVideoPlayer } from './NativeVideoPlayer';+import { useMessageHistoryActive } from './messageHistoryActivity';+import { isDirectPreviewableMediaUrl, type ResolveRemoteMediaFn } from './remoteMedia';++/** Resolve on explicit play, including files outside the task directory. The+ * existing Host file service still validates origin, path and media type. */+export function InlineVideoPlayer({ url, label, width, visible, resolve }: {+ url: string;+ label: string;+ width: number;+ visible: boolean;+ resolve?: ResolveRemoteMediaFn;+}) {+ const { t } = useTranslation();+ const { colors } = useTheme();+ const focused = useIsFocused();+ const historyActive = useMessageHistoryActive();+ const [foreground, setForeground] = useState(AppState.currentState === 'active');+ const [source, setSource] = useState<string | null>(null);+ const [videoSize, setVideoSize] = useState<{ width: number; height: number } | null>(null);+ const [loading, setLoading] = useState(false);+ const [failed, setFailed] = useState(false);+ const pending = useRef<AbortController | null>(null);+ const active = visible && focused && historyActive && foreground;+ useEffect(() => {+ const subscription = AppState.addEventListener('change', state => setForeground(state === 'active'));+ return () => { subscription.remove(); pending.current?.abort(); };+ }, []);+ useEffect(() => {+ if (!active) { pending.current?.abort(); pending.current = null; setLoading(false); }+ }, [active]);++ const play = async () => {+ if (pending.current) return;+ const controller = new AbortController();+ pending.current = controller;+ setLoading(true);+ const forceRefresh = failed;+ setFailed(false);+ try {+ let next = url;+ if (!isDirectPreviewableMediaUrl(url)) {+ if (!resolve) throw new Error('No media resolver');+ const media = await resolve({ kind: 'video', url, previewable: false },+ { front: true, forceRefresh, signal: controller.signal });+ if (!media.previewable || !media.mimeType.startsWith('video/')) throw new Error('Not video');+ next = media.url;+ }+ if (!controller.signal.aborted) setSource(next);+ } catch {+ if (!controller.signal.aborted) setFailed(true);+ } finally {+ if (pending.current === controller) { pending.current = null; setLoading(false); }+ }+ };+ const ratio = videoSize ? videoSize.width / videoSize.height : 16 / 9;+ // Fit the actual picture, rather than putting every movie in a black 16:9 box.+ // Tall clips shrink proportionally instead of consuming the entire message list.+ const height = Math.min(width / ratio, width * 4 / 3);+ const frame = { width: Math.min(width, height * ratio), height, borderRadius: radius.container, overflow: 'hidden' as const };+ if (source) return <NativeVideoPlayer key={source} url={source} visible={active} autoPlay+ onVideoSize={setVideoSize}+ style={[frame, { flex: 0, backgroundColor: colors.surface }]} testID="message.inlineVideo"+ onError={() => { setSource(null); setFailed(true); }} />;+ return <Pressable accessibilityRole="button" accessibilityLabel={`${t('message.richAnswer.playVideo')} ${label}`}+ disabled={loading} onPress={() => { void play(); }} testID="message.inlineVideoPlay"+ style={[frame, { alignItems: 'center', justifyContent: 'center', gap: spacing.sm, backgroundColor: colors.surface }]}>+ {loading ? <ActivityIndicator color={colors.textSecondary} />+ : <Play size={iconSize.lg} strokeWidth={iconStroke.regular} color={colors.textPrimary} />}+ <View><Text style={{ color: colors.textSecondary, fontSize: typeScale.bodySmall, lineHeight: lineHeight.bodySmall }}>+ {t(loading ? 'message.renderer.loading' : failed ? 'message.renderer.retryPreview' : 'message.richAnswer.playVideo')}+ </Text></View>+ </Pressable>;+}diff --git a/apps/mobile/src/session/MessageRenderer.tsx b/apps/mobile/src/session/MessageRenderer.tsxindex 5813ff8ae2..656461903d 100644--- a/apps/mobile/src/session/MessageRenderer.tsx+++ b/apps/mobile/src/session/MessageRenderer.tsx@@ -1,4 +1,5 @@import { VideoSitePreview } from './VideoSitePreview';+import { InlineVideoPlayer } from './InlineVideoPlayer';import { youtubeVideo } from '@cindy/maker-shared/rich-answer';import { richVisualStyle, splitRichAnswer, richAnswerText, richMediaSource, richMarkdown, richString, type RichNode } from '@cindy/maker-shared/rich-answer';import { RichAnswer } from './RichAnswer';@@ -10019,13 +10020,15 @@ export function MarkdownBody(props: Parameters<typeof MarkdownBodyBase>[0]) {function RichAnswerMedia({ node, body }: { node: RichNode; body: Parameters<typeof MarkdownBodyBase>[0] }) {const origin = useContext(ChatFilePathContext);const resolver = useContext(MarkdownRemoteMediaContext);+ const visible = useContext(MessageHeavyContentVisibilityContext);const [width, setWidth] = useState(0);const raw = richMediaSource(node);if (!raw || (node.kind !== 'image' && node.kind !== 'video' && node.kind !== 'audio')) return null;if (node.kind === 'video' && youtubeVideo(raw)) return <VideoSitePreview src={raw} title={richString(node, 'alt', 'YouTube')} />;const direct = /^(?:https?|xdt-audio|xdt-video|cindy-media):\/\//.test(raw);- const url = direct ? raw : mobileMarkdownManagedImagePreviewUrl(raw, origin?.workdir, body.markdownImageCacheKey, origin?.remoteHostId, origin?.sessionId);- // Outside-workdir files retain the existing explicit-open path; never fetch them automatically.+ const resolveUrl = node.kind === 'video' ? mobileMarkdownImageUrlForWorkdir : mobileMarkdownManagedImagePreviewUrl;+ const url = direct ? raw : resolveUrl(raw, origin?.workdir, body.markdownImageCacheKey, origin?.remoteHostId, origin?.sessionId);+ // Images outside workdir retain explicit-open. Video resolves only on explicit play.if (!url) return <MarkdownBodyBase {...body} text={richMarkdown(node) ?? ''} streaming={false} />;const label = richString(node, 'alt', node.kind);const media: NormalizedToolMedia = { kind: node.kind, url, title: label, previewable: isDirectPreviewableMediaUrl(url) };@@ -10044,7 +10047,9 @@ function RichAnswerMedia({ node, body }: { node: RichNode; body: Parameters<typeimagePreviewHeight: imageHeight,mediaPreviewWidth: Math.min(body.layout.mediaPreviewWidth, width) };return <View style={{ minWidth: 0 }} onLayout={event => setWidth(event.nativeEvent.layout.width)}>- {width > 0 ? <MediaPreview key={previewKey} layout={layout} media={media} label={label} onResolveRemoteMedia={resolver}+ {width > 0 && node.kind === 'video' ? <InlineVideoPlayer key={previewKey} url={url} label={label}+ width={width} visible={visible} resolve={resolver} />+ : width > 0 ? <MediaPreview key={previewKey} layout={layout} media={media} label={label} onResolveRemoteMedia={resolver}usePreviewState={useState}onOpen={body.onOpenPayload ? () => body.onOpenPayload!(buildMediaPayload(media, label)) : undefined} /> : null}</View>;diff --git a/apps/mobile/src/session/NativeVideoPlayer.tsx b/apps/mobile/src/session/NativeVideoPlayer.tsxindex 036398c5fe..3c098660ed 100644--- a/apps/mobile/src/session/NativeVideoPlayer.tsx+++ b/apps/mobile/src/session/NativeVideoPlayer.tsx@@ -34,6 +34,8 @@ export function NativeVideoPlayer({testID,url,visible,+ autoPlay = false,+ onVideoSize,}: {onError(detail: string): void;style?: StyleProp<ViewStyle>;@@ -41,10 +43,14 @@ export function NativeVideoPlayer({url: string;/** 所在页是否为当前可见页:可见 → 失活沿暂停一次。 */visible: boolean;+ /** Only true after an explicit play action in the message stream. */+ autoPlay?: boolean;+ /** Intrinsic display size, available once the native track renders. */+ onVideoSize?: (size: { width: number; height: number }) => void;}) {const { t } = useTranslation();const liquidGlass = useLiquidGlassAvailable();- const player = useVideoPlayer(url);+ const player = useVideoPlayer(url, instance => { if (autoPlay && visible) instance.play(); });const { status, error } = useEvent(player, "statusChange", {status: player.status,});@@ -112,6 +118,11 @@ export function NativeVideoPlayer({contentFit="contain"fullscreenOptions={{ enable: true, orientation: "default" }}nativeControls+ onFirstFrameRender={() => {+ const size = player.videoTrack?.size;+ if (size && Number.isFinite(size.width) && Number.isFinite(size.height)+ && size.width > 0 && size.height > 0) onVideoSize?.(size);+ }}player={player}style={StyleSheet.absoluteFill}/>diff --git a/apps/mobile/src/session/remoteMedia.ts b/apps/mobile/src/session/remoteMedia.tsindex 19bbc25fbf..21a9557581 100644--- a/apps/mobile/src/session/remoteMedia.ts+++ b/apps/mobile/src/session/remoteMedia.ts@@ -69,6 +69,8 @@ export interface MobileRemoteMediaFetchOptions {export interface MobileRemoteMediaResolverDeps {findLocalImage?(url: string): Promise<{ uri: string; mimeType: string; size: number } | null>;+ /** Retain completed video bytes before releasing the peer's short-lived staging file. */+ retainLocalVideo?(media: MobileResolvedRemoteMedia): Promise<MobileResolvedRemoteMedia>;fetchRemoteMedia(url: string,opts?: MobileRemoteMediaFetchOptions,@@ -192,6 +194,15 @@ export async function resolveMobileRemoteMedia(deps: MobileRemoteMediaResolverDeps,opts?: MobileRemoteMediaResolveOptions,): Promise<MobileResolvedRemoteMedia> {+ const retainVideo = media.kind === 'video' ? deps.retainLocalVideo : undefined;+ // Complete-file reads can use authenticated peer transport. Playback must own+ // a durable copy first: peer staging files expire after five minutes.+ const stream = retainVideo ? false : opts?.stream;+ const retain = async (resolved: MobileResolvedRemoteMedia) => {+ if (!retainVideo) return resolved;+ try { return await retainVideo(resolved); }+ finally { releasePeerMedia(resolved.url); }+ };if (media.kind === 'image' && !opts?.skipCache) {// Shared by task history, group chat and file viewers. Keeping this here// avoids fixing thumbnails in one screen while original-image reads still go online.@@ -208,19 +219,21 @@ export async function resolveMobileRemoteMedia(throw new Error(i18n.t("composer.attachments.notFetchableMedia"));}const fetchOpts = {- ...(opts?.revalidate ? { revalidate: true } : {}),+ // Old peer caches may carry octet-stream for a video. Revalidate metadata,+ // not bytes: a matching version still reuses the downloaded local file.+ ...(opts?.revalidate || media.kind === 'video' ? { revalidate: true } : {}),...(opts?.beforeFetch ? { beforeFetch: opts.beforeFetch } : {}),- ...(opts?.stream === false ? { retainBackground: async (result: MobileRemoteMediaFetchResult, isCurrent?: () => boolean) => {+ ...(stream === false ? { retainBackground: async (result: MobileRemoteMediaFetchResult, isCurrent?: () => boolean) => {if (!result.ossKey || isCurrent?.() === false) return;try {const signed = await deps.presignGet(result.ossKey);const copy = await result.cacheDownload?.(signed.getUrl);const uri = copy && peerMediaUri(copy);if (uri) releasePeerMedia(uri);- } finally { if (isCurrent?.() !== false) opts.discardBackgroundOssKey?.(result.ossKey); }+ } finally { if (isCurrent?.() !== false) opts?.discardBackgroundOssKey?.(result.ossKey); }} } : {}),...(opts?.onProgress ? { onProgress: opts.onProgress } : {}),- ...(opts?.stream !== undefined ? { stream: opts.stream } : {}),+ ...(stream !== undefined ? { stream } : {}),...(opts?.onOssKey ? { onDiscardOssKey: opts.onOssKey } : {}),...(opts?.signal ? { signal: opts.signal } : {}),...(opts?.skipCache ? { skipCache: true } : {}),@@ -232,14 +245,14 @@ export async function resolveMobileRemoteMedia();const local = peerMediaUri(fetched);if (local)- return {+ return retain({url: local,ossKey: "",size: fetched.size,mimeType: fetched.mimeType,expiresAt: peerMediaExpiry(fetched)!,previewable: canPreviewResolvedRemoteMedia(media.kind, fetched.mimeType),- };+ });// inline 回包:字节已随 invoke 帧到手,无 OSS 对象,跳过 presign。// 缩略图给 data:image;小音视频若仍以内联回包到达,也走 data URI,由播放器直接打开。// 图片宿主(会话屏)会把字节落盘并换成 file://。@@ -280,11 +293,11 @@ export async function resolveMobileRemoteMedia(}const downloaded = await fetched.cacheDownload?.(signed.getUrl);const downloadedUri = downloaded && peerMediaUri(downloaded);- if (downloadedUri) return {+ if (downloadedUri) return retain({url: downloadedUri, ossKey: fetched.ossKey, mimeType: fetched.mimeType, size: fetched.size,expiresAt: peerMediaExpiry(downloaded!)!,previewable: canPreviewResolvedRemoteMedia(media.kind, fetched.mimeType),- };+ });if (media.kind !== "image")mobileDebugLog("debug", "files", "remote media presigned", {kind: media.kind,diff --git a/apps/mobile/src/session/remoteMediaDiskCache.ts b/apps/mobile/src/session/remoteMediaDiskCache.tsindex 438aa5755b..0c2b96aac5 100644--- a/apps/mobile/src/session/remoteMediaDiskCache.ts+++ b/apps/mobile/src/session/remoteMediaDiskCache.ts@@ -378,6 +378,11 @@ export function extOfMime(mimeType: string): string {if (mimeType === 'image/webp') return 'webp';if (mimeType === 'image/svg+xml') return 'svg';if (mimeType === 'image/bmp') return 'bmp';+ // AVPlayer uses the local file suffix to select its media demuxer.+ if (mimeType === 'video/mp4') return 'mp4';+ if (mimeType === 'video/quicktime') return 'mov';+ if (mimeType === 'video/webm') return 'webm';+ if (mimeType === 'video/x-matroska') return 'mkv';return 'img';}diff --git a/docs/rich-answers.md b/docs/rich-answers.mdindex 266c149606..478182cd35 100644--- a/docs/rich-answers.md+++ b/docs/rich-answers.md@@ -101,6 +101,17 @@ iPhone 原生测试版回放真实结果,已检查任务、伙伴、流式到## 网站视频(2026-10-18)+文件视频直接嵌在消息流中,提供播放、暂停和拖动进度;全屏只是可选操作。+Desktop 复用受控媒体协议与浏览器播放控件,`xdt-file:` 同时列入 `media-src`,+本地文件仍由原协议核验路径与类型。Mobile 在点击播放后通过既有远程媒体取件服务+解析源地址,再挂载原生播放器;目录外文件同样只在显式点击后取件,不自动预取。+离开消息/页面或应用进入后台时暂停,回到页面不自动续播;失败可重新取件。+历史消息沿原地址重开,不重写原文件,不公开上传媒体,也不新增原生依赖。+直连取件与 OSS 复用媒体 MIME 映射;旧端返回泛型二进制时,仅在授权元数据的版本与+字节数完全一致时补全音视频类型,不靠消息 URL 猜类型。手机视频取件先验证元数据,+相同版本仍复用已有字节;播放前持有缓存副本,保留 `.mp4` / `.mov` 等媒体扩展名,+不把视频落成图片后缀,也不让播放器依赖会过期的直连暂存文件。+`video.src` 识别 YouTube watch、youtu.be、shorts、live 与 embed 链接,从核验的视频 ID生成官方 `youtube.com/embed` 地址;保留起播时间,不传任意查询参数。旧 nocookie 输入仍可解析。点击封面后装载,原视频链接始终可用。外部媒体文件、图片与其它网站链接行为不变。diff --git a/packages/device-link/src/__tests__/fileAccess.test.ts b/packages/device-link/src/__tests__/fileAccess.test.tsindex 4d24fe27ec..6f6072d9ce 100644--- a/packages/device-link/src/__tests__/fileAccess.test.ts+++ b/packages/device-link/src/__tests__/fileAccess.test.ts@@ -8,6 +8,20 @@ const metadata = {transferRequired: true,};describe("shared file read policy", () => {+ // Unit: old peers omit video MIME mappings; preserve both authorization and the+ // WeakMap-bound staging lease when reconciling metadata with downloaded bytes.+ it.each(['same', 'version', 'size', 'unversioned', 'explicit-type'] as const)(+ 'reconciles generic peer media only for the authorized version: %s', async mode => {+ const prepared = { ...metadata, version: 'a'.repeat(64), mimeType: 'video/mp4' };+ const direct = { ossKey: '', size: prepared.size + (mode === 'size' ? 1 : 0),+ version: mode === 'unversioned' ? undefined : (mode === 'version' ? 'b' : 'a').repeat(64),+ mimeType: mode === 'explicit-type' ? 'text/html' : 'application/octet-stream' };+ const expectedMime = mode === 'same' ? prepared.mimeType : direct.mimeType;+ const result = await readDeviceFile({ prepare: async () => prepared, peer: async () => direct, fallback: vi.fn() });+ expect(result).toBe(direct);+ expect(result.mimeType).toBe(expectedMime);+ },+ );it.each(['cancelled', 'superseded'])('reopens from cache and refreshes independently of a %s read', async mode => {const cached = { ...metadata, version: 'a'.repeat(64), inlineBase64: 'YQ==' };const fresh = { ...cached, version: 'b'.repeat(64), transferRequired: false, inlineBase64: 'Yg==' };diff --git a/packages/device-link/src/fileAccess.ts b/packages/device-link/src/fileAccess.tsindex 218d824aaf..89f55cea36 100644--- a/packages/device-link/src/fileAccess.ts+++ b/packages/device-link/src/fileAccess.ts@@ -106,7 +106,15 @@ export async function readDeviceFile<T extends DeviceFileResult>(options: {const direct = needsFallback ? null : await options.peer(prepared);if (direct && !active()) return discardCancelled(direct);else assertActive();- if (direct) return direct;+ if (direct) {+ // Older hosts' peer endpoint omits media MIME mappings. Use the authorized+ // metadata only for exactly the same version/size; never infer from a URL.+ // Preserve object identity: adapters attach staging leases through WeakMaps.+ if (direct.mimeType === 'application/octet-stream' && validFileVersion(prepared.version) &&+ direct.version === prepared.version && direct.size === prepared.size &&+ /^(audio|video)\//i.test(prepared.mimeType)) direct.mimeType = prepared.mimeType;+ return direct;+ }const result = await options.fallback();if (!active()) return discardCancelled(result);if (!mediaStream && !result.version && validFileVersion(prepared.version) && options.revalidate) {