Skip to content

armgit / cindy

Public Imported from GitHub
0

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

revert: 撤销 #3029 回退提交

Lizib53e8f30ac6f
diff --git a/apps/mobile/app/sessions/[sessionId].tsx b/apps/mobile/app/sessions/[sessionId].tsx
index 80fb6b60bf..49ce81c8ba 100644
--- a/apps/mobile/app/sessions/[sessionId].tsx
+++ b/apps/mobile/app/sessions/[sessionId].tsx
@@ -34,6 +34,7 @@ import {
} from 'expo-audio';
import {
addScreenshotListener,
+ renderConversationShareHtmlToPng,
} from 'xdt-screenshot-monitor';
import { useFocusEffect, useLocalSearchParams, useNavigation, useRouter } from 'expo-router';
import { forwardRef, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore, type ReactNode, type RefObject, type SetStateAction } from 'react';
@@ -100,6 +101,7 @@ import {
type ShareableMessageViewport,
} from '@/session/MessageRenderer';
import {
+ bundledAssetToDataUri,
cleanupConversationSharePngTemps,
deleteConversationSharePngTemp,
writeConversationSharePngTemp,
@@ -109,6 +111,7 @@ import {
type ConversationShareSvgHandle,
} from '@/session/ConversationShareSvg';
import {
+ buildConversationShareHtml,
type ConversationShareMessage,
type ConversationShareWebViewColors,
} from '@/session/conversationShareWebViewHtml';
@@ -610,6 +613,15 @@ const REOPEN_MESSAGE_WINDOW_LIMITS = [20, 10, 5, 1] as const;
// 覆盖 settling 窗口上限(10s)之后仍无任何在途证据的场景。
const TAIL_RETRY_HIDE_TIMEOUT_MS = 15_000;
const SCREENSHOT_SHARE_ACTIVATION_DEBOUNCE_MS = 1_200;
+const nativeConversationShareAvailable = Platform.OS === 'ios';
+
+// 原生 WKWebView 只能稳定读取 data URI;SVG 兜底直接使用同一组 bundle asset。
+// eslint-disable-next-line @typescript-eslint/no-require-imports
+const shareCharacterAsset = require('../../assets/share/cindy-share-character.jpg');
+// eslint-disable-next-line @typescript-eslint/no-require-imports
+const shareLogoLightAsset = require('../../assets/login/login-wordmark.png');
+// eslint-disable-next-line @typescript-eslint/no-require-imports
+const shareLogoDarkAsset = require('../../assets/login/login-wordmark-dark.png');
/**
* 排队消息「复用 composer 编辑」的会话内状态:clientId 定位队列条目,
@@ -1004,6 +1016,9 @@ export default function SessionScreen() {
const shareOperationSeqRef = useRef(0);
const [conversationShareBusy, setConversationShareBusy] = useState(false);
const [shareSelectionTriggeredByScreenshot, setShareSelectionTriggeredByScreenshot] = useState(false);
+ const [shareCharacterSrc, setShareCharacterSrc] = useState<string | null>(null);
+ const [shareLogoSrc, setShareLogoSrc] = useState<string | null>(null);
+ const shareLogoModeRef = useRef<string | null>(null);
// chat-text-quote:待随下一条消息发送的选中文字引用(全局 store,消息流选区
// 按钮 / 文件预览页写入;发送时拼进正文,命中本地命令时保留)。
const quotes = useSessionQuotes(sessionId);
@@ -1083,6 +1098,28 @@ export default function SessionScreen() {
};
}, [sessionId]),
);
+ useEffect(() => {
+ if (!nativeConversationShareAvailable || !shareSelectionActive) return undefined;
+ let cancelled = false;
+ const logoNeedsLoad = shareLogoModeRef.current !== mode || !shareLogoSrc;
+ void Promise.all([
+ shareCharacterSrc
+ ? Promise.resolve(shareCharacterSrc)
+ : bundledAssetToDataUri(shareCharacterAsset, 'image/jpeg'),
+ logoNeedsLoad
+ ? bundledAssetToDataUri(
+ mode === 'dark' ? shareLogoDarkAsset : shareLogoLightAsset,
+ 'image/png',
+ )
+ : Promise.resolve(shareLogoSrc),
+ ]).then(([character, logo]) => {
+ if (cancelled) return;
+ shareLogoModeRef.current = mode;
+ setShareCharacterSrc(character);
+ setShareLogoSrc(logo);
+ });
+ return () => { cancelled = true; };
+ }, [mode, shareCharacterSrc, shareLogoSrc, shareSelectionActive]);
const [composerFocused, setComposerFocused] = useState(false);
const [composerInputContentHeight, setComposerInputContentHeight] = useState(COMPOSER_INPUT_SINGLE_LINE_CONTENT_HEIGHT);
const [voiceDraftCaretFrame, setVoiceDraftCaretFrame] = useState({ left: 0, top: 0 });
@@ -5999,6 +6036,30 @@ export default function SessionScreen() {
textTertiary: colors.textTertiary,
dark: mode === 'dark',
}), [colors, mode]);
+ const conversationShareHtml = useMemo(() => {
+ if (
+ !nativeConversationShareAvailable
+ || !shareSelectionActive
+ || selectedShareMessages.length === 0
+ ) return '';
+ return buildConversationShareHtml({
+ allShareableIds,
+ characterSrc: shareCharacterSrc ?? undefined,
+ colors: conversationShareColors,
+ contentWidth: windowDimensions.width,
+ logoSrc: shareLogoModeRef.current === mode ? shareLogoSrc ?? undefined : undefined,
+ selectedMessages: selectedShareMessages,
+ });
+ }, [
+ allShareableIds,
+ conversationShareColors,
+ mode,
+ selectedShareMessages,
+ shareCharacterSrc,
+ shareLogoSrc,
+ shareSelectionActive,
+ windowDimensions.width,
+ ]);
const enterShareSelection = useCallback((clientId: string) => {
Keyboard.dismiss();
setShareSelectionTriggeredByScreenshot(false);
@@ -6011,10 +6072,30 @@ export default function SessionScreen() {
shareSelectionStore.exit();
}, []);
const exportConversationSharePng = useCallback(async () => {
+ const nativeShareAssetsReady = Boolean(
+ nativeConversationShareAvailable
+ && shareCharacterSrc
+ && shareLogoSrc
+ && shareLogoModeRef.current === mode,
+ );
+ if (conversationShareHtml && nativeShareAssetsReady) {
+ try {
+ const nativeBase64 = await renderConversationShareHtmlToPng({
+ html: conversationShareHtml,
+ width: windowDimensions.width,
+ });
+ if (nativeBase64) {
+ console.info('[conversation-share] native webview export succeeded');
+ return nativeBase64;
+ }
+ } catch (error) {
+ console.warn('[conversation-share] native webview export failed; falling back to svg', error);
+ }
+ }
const svg = conversationShareSvgRef.current;
if (!svg) throw new Error('conversation share svg renderer is unavailable');
return svg.exportPng();
- }, []);
+ }, [conversationShareHtml, mode, shareCharacterSrc, shareLogoSrc, windowDimensions.width]);
const shareSelectedConversation = useCallback(async () => {
if (
conversationShareBusy
@@ -6044,8 +6125,8 @@ export default function SessionScreen() {
const sharing = await import('expo-sharing');
if (!isShareOperationActive()) return;
await sharing.shareAsync(localUri, { mimeType: 'image/png' });
- // shareAsync 返回时系统扩展仍可能继续读取该 URL;只要系统分享调用成功,
- // 即使当前选择操作已失活,也要把文件留给下一次有界清理。
+ // shareAsync 成功后系统扩展仍可能读取该 URL;即使当前操作随即失活,
+ // 也必须交给下一次有界清理,不能在 finally 中提前删除。
shareCompleted = true;
if (!isShareOperationActive()) return;
setShareSelectionTriggeredByScreenshot(false);
@@ -6055,8 +6136,8 @@ export default function SessionScreen() {
console.warn('[conversation-share] failed to generate or open share image', error);
Alert.alert(t('session.screen.shareFailedTitle'), t('session.screen.shareImageFailed'));
} finally {
- // 失败、取消或分享调用前失活时立即清理;成功产物留给系统扩展,
- // 并由下一次分享开始前的有界清理回收。
+ // shareAsync 返回后,iOS 分享扩展仍可能继续读取该 URL。成功写入的文件留在
+ // cache 目录交给下一次有界清理;失败、取消或中途失活则立即删除当前产物。
if (!shareCompleted && localUri) await deleteConversationSharePngTemp(localUri);
if (shareOperationSeqRef.current === operationSeq) setConversationShareBusy(false);
}
diff --git a/apps/mobile/src/__tests__/conversationShareWebViewHtml.test.ts b/apps/mobile/src/__tests__/conversationShareWebViewHtml.test.ts
index ba0c58c674..3af2fed142 100644
--- a/apps/mobile/src/__tests__/conversationShareWebViewHtml.test.ts
+++ b/apps/mobile/src/__tests__/conversationShareWebViewHtml.test.ts
@@ -149,7 +149,7 @@ describe('buildConversationShareHtml 富内容导出', () => {
expect(html).toContain(`alt="${i18n.t('message.renderer.imageFallbackTitle')}"`);
});
- it('限制原生 renderer 源尺寸,使用 SVG 主路径并安全保留已分享 PNG', () => {
+ it('限制原生与降级 renderer 的完整源尺寸,并安全保留已分享 PNG', () => {
const nativeSource = readFileSync(
resolve(
process.cwd(),
@@ -167,6 +167,17 @@ describe('buildConversationShareHtml 富内容导出', () => {
).replace(/\r\n/g, '\n');
expect(nativeSource).toContain('conversationShareMaxSourcePixels');
+ expect(nativeSource).toContain('UIWindow(windowScene: windowScene)');
+ expect(nativeSource).toContain('hostingWindow.rootViewController = viewController');
+ expect(nativeSource).toContain('hostingWindow?.isHidden = true');
+ expect(nativeSource).toContain('no active window scene');
+ expect(nativeSource).toContain('UIWindow.Level.normal.rawValue + 1');
+ expect(nativeSource).toContain('hostingWindow.alpha = 0.01');
+ expect(nativeSource).toContain('waitForWebContentPaint(webView)');
+ expect(nativeSource).toContain('requestAnimationFrame(resolve)');
+ expect(nativeSource).toContain('merged.hasVisibleVariation');
+ expect(nativeSource).toContain('Conversation share PNG is blank.');
+ expect(nativeSource).toContain('format.scale = 1');
expect(nativeSource).toContain(
'captureWidth * captureHeight <= conversationShareMaxSourcePixels',
);
@@ -179,16 +190,40 @@ describe('buildConversationShareHtml 富内容导出', () => {
expect(webViewSource).toContain(
'files.slice(SHARE_PNG_RETAIN_COUNT, SHARE_PNG_RETAIN_COUNT + SHARE_PNG_CLEANUP_BATCH)',
);
+ expect(sessionSource).toContain('deleteConversationSharePngTemp');
expect(sessionSource).toContain('cleanupConversationSharePngTemps');
+ expect(sessionSource).toContain('cache 目录交给下一次有界清理');
+ expect(sessionSource).toContain('if (!shareCompleted && localUri)');
+ expect(sessionSource).toContain('<ConversationShareSvg');
expect(sessionSource).toContain(
- 'if (!shareCompleted && localUri) await deleteConversationSharePngTemp(localUri);',
+ "nativeConversationShareAvailable = Platform.OS === 'ios'",
);
expect(sessionSource).toContain(
- "await sharing.shareAsync(localUri, { mimeType: 'image/png' });\n // shareAsync 返回时系统扩展仍可能继续读取该 URL;只要系统分享调用成功,\n // 即使当前选择操作已失活,也要把文件留给下一次有界清理。\n shareCompleted = true;\n if (!isShareOperationActive()) return;",
+ 'if (!nativeConversationShareAvailable || !shareSelectionActive) return undefined;',
);
- expect(sessionSource).toContain('<ConversationShareSvg');
+ expect(sessionSource).toContain(
+ '!nativeConversationShareAvailable\n || !shareSelectionActive',
+ );
+ expect(sessionSource).toContain(
+ 'nativeConversationShareAvailable\n && shareCharacterSrc',
+ );
+ expect(sessionSource).toContain('renderConversationShareHtmlToPng({');
+ expect(sessionSource).toContain('nativeShareAssetsReady');
+ expect(sessionSource).toContain('native webview export succeeded');
+ expect(sessionSource).toContain('falling back to svg');
+ expect(sessionSource).not.toContain('OTA webview export failed; falling back to svg');
expect(sessionSource).toContain('return svg.exportPng();');
- expect(sessionSource).not.toContain('renderConversationShareHtmlToPng({');
+ const shareAsyncIndex = sessionSource.indexOf(
+ "await sharing.shareAsync(localUri, { mimeType: 'image/png' });",
+ );
+ const shareCompletedIndex = sessionSource.indexOf('shareCompleted = true;', shareAsyncIndex);
+ const postShareActiveCheckIndex = sessionSource.indexOf(
+ 'if (!isShareOperationActive()) return;',
+ shareAsyncIndex,
+ );
+ expect(shareAsyncIndex).toBeGreaterThanOrEqual(0);
+ expect(shareCompletedIndex).toBeGreaterThan(shareAsyncIndex);
+ expect(shareCompletedIndex).toBeLessThan(postShareActiveCheckIndex);
});
it('使用 Mobile 获批的克制页脚尺寸', () => {