Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 分享图包含聊天里的图片 (#6041)
Dash3a23df264583
diff --git a/apps/desktop/src/renderer/__tests__/shareConversationImage.test.ts b/apps/desktop/src/renderer/__tests__/shareConversationImage.test.tsindex 7aff4850fa..2224279ea3 100644--- a/apps/desktop/src/renderer/__tests__/shareConversationImage.test.ts+++ b/apps/desktop/src/renderer/__tests__/shareConversationImage.test.ts@@ -13,12 +13,14 @@ vi.mock('@/lib/annotationBurnIn', () => ({const {SHARE_EXCLUDE_ATTR,SHARE_MESSAGE_ATTR,+ SHARE_PENDING_IMAGE_ATTR,SHARE_SESSION_ATTR,SHARE_SOURCE_ATTR,ShareImageTooLargeError,appendShareMessages,assertShareImageReadableSize,buildShareImageFooter,+ buildShareStageMotionFreeze,expandScrollableBlocks,inlineCloneImages,prepareShareMessageClone,@@ -126,6 +128,23 @@ describe('prepareShareMessageClone', () => {.firstElementChild as HTMLElement;expect(prepareShareMessageClone(node)).not.toBeNull();});++ it('流里仍在加载的图片照常进图:去掉加载占位,恢复显示', () => {+ const node = root(`+ <div ${SHARE_MESSAGE_ATTR}="media-1">+ <span ${SHARE_EXCLUDE_ATTR} role="status"></span>+ <img ${SHARE_PENDING_IMAGE_ATTR} src="cindy-media://shot.png" style="max-width: 480px; display: none;" />+ </div>+ `).firstElementChild as HTMLElement;+ const clone = prepareShareMessageClone(node);+ const img = clone?.querySelector('img');+ expect(clone?.querySelector('[role="status"]')).toBeNull();+ expect(img?.hasAttribute(SHARE_PENDING_IMAGE_ATTR)).toBe(false);+ expect(img?.style.display).toBe('');+ expect(img?.style.maxWidth).toBe('480px');+ // 原节点不受影响,流里照旧等加载完成。+ expect(node.querySelector('img')?.style.display).toBe('none');+ });});describe('appendShareMessages', () => {@@ -481,6 +500,15 @@ describe('queryShareableMessageIds', () => {});});+describe('buildShareStageMotionFreeze', () => {+ it('只作用于导出 stage,关掉入场动画与过渡,避免图片按动画中途的透明度入图', () => {+ const css = buildShareStageMotionFreeze().textContent ?? '';+ expect(css).toContain('[data-share-export] *');+ expect(css).toContain('animation:none!important');+ expect(css).toContain('transition:none!important');+ });+});+describe('buildShareImageFooter', () => {it('形象与 logo 横向锁定,不附加网址', () => {const footer = buildShareImageFooter({diff --git a/apps/desktop/src/renderer/components/chat/ChatLoadingImage.tsx b/apps/desktop/src/renderer/components/chat/ChatLoadingImage.tsxindex daee35f5ca..e1cd47f71c 100644--- a/apps/desktop/src/renderer/components/chat/ChatLoadingImage.tsx+++ b/apps/desktop/src/renderer/components/chat/ChatLoadingImage.tsx@@ -1,6 +1,7 @@import { useState, type ComponentProps } from 'react';import { useTranslation } from 'react-i18next';import { Spinner } from '@/components/ui/spinner';+import { SHARE_EXCLUDE_ATTR, SHARE_PENDING_IMAGE_ATTR } from '@/lib/shareConversationImage';import { cn } from '@/lib/utils';/** Keep a visible frame until the browser has received and decoded the image. */@@ -16,6 +17,7 @@ function ImageLoad({ onLoad, style, className, ...props }: ComponentProps<'img'><>{!loaded && (<span+ {...{ [SHARE_EXCLUDE_ATTR]: '' }}role="status"aria-label={t('chat.imageMissing.loading')}className="inline-flex min-h-[180px] w-[280px] max-w-full self-start items-center justify-center rounded-xl bg-[var(--surface-chip)] text-[var(--text-tertiary)]"@@ -25,6 +27,8 @@ function ImageLoad({ onLoad, style, className, ...props }: ComponentProps<'img'>)}<img{...props}+ // 分享图自己读图片字节,不必等这里加载完:克隆时去掉占位、显示这张图。+ {...(loaded ? {} : { [SHARE_PENDING_IMAGE_ATTR]: '' })}className={cn(className,loaded && 'motion-safe:animate-fade-in',diff --git a/apps/desktop/src/renderer/components/chat/MessageStream.tsx b/apps/desktop/src/renderer/components/chat/MessageStream.tsxindex 3027f3b1cd..34233249b7 100644--- a/apps/desktop/src/renderer/components/chat/MessageStream.tsx+++ b/apps/desktop/src/renderer/components/chat/MessageStream.tsx@@ -127,9 +127,17 @@ import {saveSessionScroll,type SessionScrollSnapshot,} from '@/lib/sessionScrollStore';-import { SHARE_MESSAGE_ATTR, SHARE_SESSION_ATTR } from '@/lib/shareConversationImage';+import {+ SHARE_EXCLUDE_ATTR,+ SHARE_MESSAGE_ATTR,+ SHARE_SESSION_ATTR,+} from '@/lib/shareConversationImage';import { ShareMessageCheckbox } from './ShareMessageCheckbox';-import { isShareableMessage, useShareSelectionActive } from './shareSelectionStore';+import {+ isShareableMessage,+ isShareableToolMedia,+ useShareSelectionActive,+} from './shareSelectionStore';import { parsePluginInstanceId, pluginStoragePart } from '../../../shared/pluginIdentity';import { isValidGhostId } from '../../../shared/ghost';import { isValidPluginNamespace } from '@cindy/plugin-protocol';@@ -2556,9 +2564,14 @@ function ToolMediaList({ items, sessionId }: { items: ToolMediaItem[]; sessionId</div>);}+ // 视频 / 音频画不进静态分享图,克隆时剔除;同一行的图片照常进图。if (m.kind === 'video') {return (- <div key={mediaKey} className="flex flex-col gap-1.5">+ <div+ key={mediaKey}+ {...{ [SHARE_EXCLUDE_ATTR]: '' }}+ className="flex flex-col gap-1.5"+ ><ChatVideoViewsrc={m.url}filename={`tool-video-${i + 1}`}@@ -2576,12 +2589,15 @@ function ToolMediaList({ items, sessionId }: { items: ToolMediaItem[]; sessionId// Track must be present (extractToolResultMedia synthesises// an empty one if only xdt_audio_urls came in defensively).if (m.audioTrack) {- if (m.audioTrack.kind === 'sound_effect') {- return (- <ChatSoundEffectCard key={mediaKey} track={m.audioTrack} sessionId={sessionId} />- );- }- return <ChatAudioCard key={mediaKey} track={m.audioTrack} sessionId={sessionId} />;+ return (+ <div key={mediaKey} {...{ [SHARE_EXCLUDE_ATTR]: '' }} className="contents">+ {m.audioTrack.kind === 'sound_effect' ? (+ <ChatSoundEffectCard track={m.audioTrack} sessionId={sessionId} />+ ) : (+ <ChatAudioCard track={m.audioTrack} sessionId={sessionId} />+ )}+ </div>+ );}return null;})}@@ -2589,6 +2605,39 @@ function ToolMediaList({ items, sessionId }: { items: ToolMediaItem[]; sessionId);}+/**+ * 工具产物行的分享壳:含图片时与消息行一样挂分享定位属性和复选框,否则 AI 截图、+ * 出图等渲染在气泡外的图片永远选不进分享图。shareId 取 render item key,与消息+ * clientId 不同形,不会撞号。复选框只在选择模式渲染,进出选择模式不重挂媒体。+ */+function ShareableToolMedia({+ shareId,+ renderItemKey,+ items,+ sessionId,+ selectionActive,+}: {+ shareId: string;+ renderItemKey?: string;+ items: ToolMediaItem[];+ sessionId?: string;+ selectionActive: boolean;+}) {+ const shareable = Boolean(sessionId) && isShareableToolMedia(items);+ return (+ <div+ {...(renderItemKey ? { 'data-render-item-key': renderItemKey } : {})}+ {...(shareable && sessionId+ ? { [SHARE_SESSION_ATTR]: sessionId, [SHARE_MESSAGE_ATTR]: shareId }+ : {})}+ className={cn('flex flex-col gap-2', shareable && 'relative')}+ >+ {shareable && selectionActive ? <ShareMessageCheckbox clientId={shareId} /> : null}+ <ToolMediaList items={items} sessionId={sessionId} />+ </div>+ );+}+function renderWorkGroupChild(item: Exclude<WorkChildItem, ToolSegmentRenderItem>,props: {@@ -6438,7 +6487,12 @@ export function MessageStream({sessionId={sessionId}/>{item.media && item.media.length > 0 ? (- <ToolMediaList items={item.media} sessionId={sessionId} />+ <ShareableToolMedia+ shareId={`${item.key}:media`}+ items={item.media}+ sessionId={sessionId}+ selectionActive={shareSelectionActive}+ />) : null}</div>);@@ -6448,13 +6502,14 @@ export function MessageStream({// tool-result-media: 跳出 tool_segment 折叠卡片,渲染体在// ToolMediaList(与 ghost_card 回锚媒体共用单一来源)。return (- <div+ <ShareableToolMediakey={item.key}- data-render-item-key={item.key}- className="flex flex-col gap-2"- >- <ToolMediaList items={item.items} sessionId={sessionId} />- </div>+ renderItemKey={item.key}+ shareId={item.key}+ items={item.items}+ sessionId={sessionId}+ selectionActive={shareSelectionActive}+ />);}diff --git a/apps/desktop/src/renderer/components/chat/__tests__/shareSelectionStore.test.ts b/apps/desktop/src/renderer/components/chat/__tests__/shareSelectionStore.test.tsindex e92a5efad6..f6931276ac 100644--- a/apps/desktop/src/renderer/components/chat/__tests__/shareSelectionStore.test.ts+++ b/apps/desktop/src/renderer/components/chat/__tests__/shareSelectionStore.test.ts@@ -1,11 +1,27 @@import { beforeEach, describe, expect, it } from 'vitest';-import { isShareableMessage, shareSelectionStore } from '../shareSelectionStore';+import {+ isShareableMessage,+ isShareableToolMedia,+ shareSelectionStore,+} from '../shareSelectionStore';beforeEach(() => {shareSelectionStore.reset();});+describe('isShareableToolMedia', () => {+ it('含图片的工具产物行可选(AI 截图 / 出图要能进分享图)', () => {+ expect(isShareableToolMedia([{ kind: 'image' }])).toBe(true);+ expect(isShareableToolMedia([{ kind: 'video' }, { kind: 'image' }])).toBe(true);+ });++ it('只有视频 / 音频的行不可选(画不进静态图)', () => {+ expect(isShareableToolMedia([{ kind: 'video' }, { kind: 'audio' }])).toBe(false);+ expect(isShareableToolMedia([])).toBe(false);+ });+});+describe('isShareableMessage', () => {it('只认正文对话:user / assistant', () => {expect(isShareableMessage({ role: 'user', clientId: 'x' })).toBe(true);diff --git a/apps/desktop/src/renderer/components/chat/shareSelectionStore.ts b/apps/desktop/src/renderer/components/chat/shareSelectionStore.tsindex 61791fb9ed..a1e1ef36ce 100644--- a/apps/desktop/src/renderer/components/chat/shareSelectionStore.ts+++ b/apps/desktop/src/renderer/components/chat/shareSelectionStore.ts@@ -46,6 +46,15 @@ export function isShareableMessage(message: ShareableMessageLike): boolean {return true;}+/**+ * 工具产物行(AI 截图 / 出图 / 插件回锚媒体)是否可被勾选:含图片才可选。+ * 这些图渲染在消息气泡之外的独立行,不单独开放勾选就永远进不了分享图。+ * 视频 / 音频画不进静态图,只剩它们的行没有可分享的内容。+ */+export function isShareableToolMedia(items: readonly { kind: string }[]): boolean {+ return items.some((item) => item.kind === 'image');+}+/** 选择模式所属会话;null = 未进入选择模式。 */let activeSessionId: string | null = null;const selected = new Set<string>();diff --git a/apps/desktop/src/renderer/lib/shareConversationImage.ts b/apps/desktop/src/renderer/lib/shareConversationImage.tsindex af434aad97..8182eff54c 100644--- a/apps/desktop/src/renderer/lib/shareConversationImage.ts+++ b/apps/desktop/src/renderer/lib/shareConversationImage.ts@@ -46,6 +46,11 @@ export const SHARE_EXCLUDE_ATTR = 'data-share-exclude';* 发给别人既无意义又可能泄露身份;正文保留。*/export const SHARE_SOURCE_ATTR = 'data-share-source';+/**+ * 流里尚未加载完、暂时隐藏的图片(ChatLoadingImage)。分享管线自己读图片字节,+ * 克隆时恢复显示,不能因为流里还在转圈就让图从产物里消失。+ */+export const SHARE_PENDING_IMAGE_ATTR = 'data-share-image-pending';/*** 克隆体里必须清掉的锚点属性:离屏容器挂在 document 内,这些 data 属性会让@@ -127,6 +132,14 @@ export function stripMessageSources(root: HTMLElement): void {root.querySelectorAll(`[${SHARE_SOURCE_ATTR}]`).forEach((el) => el.remove());}+/** 恢复流里仍在加载、被隐藏的图片(见 SHARE_PENDING_IMAGE_ATTR)。 */+export function revealPendingImages(root: HTMLElement): void {+ root.querySelectorAll<HTMLElement>(`[${SHARE_PENDING_IMAGE_ATTR}]`).forEach((el) => {+ el.removeAttribute(SHARE_PENDING_IMAGE_ATTR);+ el.style.removeProperty('display');+ });+}+/** 清掉会污染全局 querySelector 的锚点属性(见 CLONE_STRIPPED_ATTRS 注释)。 */export function stripCloneAnchors(root: HTMLElement): void {for (const attr of CLONE_STRIPPED_ATTRS) {@@ -151,6 +164,7 @@ export function prepareShareMessageClone(node: HTMLElement): HTMLElement | nullconst clone = node.cloneNode(true) as HTMLElement;stripInteractiveElements(clone);stripMessageSources(clone);+ revealPendingImages(clone);expandCollapsedMessages(clone);stripCloneAnchors(clone);if ((clone.textContent ?? '').trim().length > 0) return clone;@@ -462,6 +476,20 @@ export function buildShareImageGapMarker(): HTMLElement {return marker;}+/**+ * 冻结 stage 内的入场动画。克隆体挂进 document 时,图片淡入、消息浮入等动画会+ * 从头重播,html-to-image 按动画中途的 computed style 内联。2026-10-14 实测:聊天+ * 图片带 ChatLoadingImage 的 `animate-fade-in` 时,产物里整块留白、图片不见;+ * 冻结后正常。样式表挂在 host 上、不在 stage 内,不会被序列化进产物。+ */+export function buildShareStageMotionFreeze(): HTMLStyleElement {+ const style = document.createElement('style');+ style.textContent =+ '[data-share-export],[data-share-export] *,[data-share-export] *::before,' ++ '[data-share-export] *::after{animation:none!important;transition:none!important}';+ return style;+}+/*** 同源资源 → data URL。页脚 logo 是打包 asset,html-to-image 本可自行内联,* 但预先转掉能把「产物里 logo 空白」这类不确定性彻底消掉。@@ -574,6 +602,7 @@ export async function buildShareImageBlob({host.style.top = '0';host.style.zIndex = '-1';host.style.pointerEvents = 'none';+ host.appendChild(buildShareStageMotionFreeze());const stage = document.createElement('div');stage.setAttribute('data-share-export', '');