Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
revert: 完整回退 #3029 原生 WebView 分享导出
Lizi29a52203ef0d
diff --git a/apps/mobile/app/sessions/[sessionId].tsx b/apps/mobile/app/sessions/[sessionId].tsxindex 49ce81c8ba..7a8dce78b7 100644--- a/apps/mobile/app/sessions/[sessionId].tsx+++ b/apps/mobile/app/sessions/[sessionId].tsx@@ -34,7 +34,6 @@ 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';@@ -101,7 +100,6 @@ import {type ShareableMessageViewport,} from '@/session/MessageRenderer';import {- bundledAssetToDataUri,cleanupConversationSharePngTemps,deleteConversationSharePngTemp,writeConversationSharePngTemp,@@ -111,7 +109,6 @@ import {type ConversationShareSvgHandle,} from '@/session/ConversationShareSvg';import {- buildConversationShareHtml,type ConversationShareMessage,type ConversationShareWebViewColors,} from '@/session/conversationShareWebViewHtml';@@ -613,15 +610,6 @@ 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 定位队列条目,@@ -1016,9 +1004,6 @@ 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);@@ -1098,28 +1083,6 @@ 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 });@@ -6036,30 +5999,6 @@ 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);@@ -6072,30 +6011,10 @@ 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 (conversationShareBusydiff --git a/apps/mobile/modules/xdt-screenshot-monitor/ios/XdtScreenshotMonitorModule.swift b/apps/mobile/modules/xdt-screenshot-monitor/ios/XdtScreenshotMonitorModule.swiftindex bca472c7a8..a489072e2d 100644--- a/apps/mobile/modules/xdt-screenshot-monitor/ios/XdtScreenshotMonitorModule.swift+++ b/apps/mobile/modules/xdt-screenshot-monitor/ios/XdtScreenshotMonitorModule.swift@@ -6,7 +6,6 @@ private let onScreenshot = "onScreenshot"private let conversationShareRenderTimeout: TimeInterval = 20private let conversationShareMaxOutputPixels: CGFloat = 12_000_000private let conversationShareMaxSourcePixels: CGFloat = 12_000_000-private let conversationShareViewportHeight: CGFloat = 760public class XdtScreenshotMonitorModule: Module {private var screenshotObserver: NSObjectProtocol?@@ -88,7 +87,6 @@ private final class ConversationShareHtmlRenderer: NSObject, WKNavigationDelegatprivate var completed = falseprivate var timeoutWorkItem: DispatchWorkItem?private var webView: WKWebView?- private var hostingWindow: UIWindow?init(html: String,@@ -103,48 +101,13 @@ private final class ConversationShareHtmlRenderer: NSObject, WKNavigationDelegat}func start() {- guard let windowScene = UIApplication.shared.connectedScenes- .compactMap({ $0 as? UIWindowScene })- .first(where: { $0.activationState == .foregroundActive || $0.activationState == .foregroundInactive })- else {- finish(.failure(ConversationShareRenderError("Conversation share renderer has no active window scene.")))- return- }-let configuration = WKWebViewConfiguration()configuration.websiteDataStore = .nonPersistent()- let viewportHeight = min(conversationShareViewportHeight, max(1, width * 2))- let webView = WKWebView(- frame: CGRect(x: 0, y: 0, width: width, height: viewportHeight),- configuration: configuration- )- webView.isOpaque = false- webView.scrollView.isScrollEnabled = true- webView.scrollView.showsVerticalScrollIndicator = false- webView.scrollView.showsHorizontalScrollIndicator = false- webView.backgroundColor = .clear-- // WKWebView 的离屏 snapshot 仍需要挂在可见的 UIKit window 层级中。- // 不把它挂到业务页面,避免导出期间改变用户当前页面的布局或焦点。- let hostingWindow = UIWindow(windowScene: windowScene)- hostingWindow.frame = CGRect(x: 0, y: 0, width: width, height: viewportHeight)- // 放到主窗口上方才能让 WebKit 进入可合成状态;极低透明度避免导出时闪屏。- // takeSnapshot 直接读取 WKWebView 内容,不会继承 hostingWindow 的透明度。- hostingWindow.windowLevel = UIWindow.Level(rawValue: UIWindow.Level.normal.rawValue + 1)- hostingWindow.backgroundColor = .clear- hostingWindow.alpha = 0.01- hostingWindow.isUserInteractionEnabled = false- let viewController = UIViewController()- viewController.view.backgroundColor = .clear- viewController.view.frame = hostingWindow.bounds- webView.frame = viewController.view.bounds- viewController.view.addSubview(webView)- hostingWindow.rootViewController = viewController- hostingWindow.isHidden = false-+ let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: width, height: 1), configuration: configuration)webView.navigationDelegate = self+ webView.isOpaque = false+ webView.scrollView.isScrollEnabled = falseself.webView = webView- self.hostingWindow = hostingWindowlet timeout = DispatchWorkItem { [weak self] inself?.finish(.failure(ConversationShareRenderError("Conversation share rendering timed out.")))@@ -224,171 +187,28 @@ private final class ConversationShareHtmlRenderer: NSObject, WKNavigationDelegatself.finish(.failure(ConversationShareRenderError("Conversation share content is too large.")))return}- guard captureWidth <= webView.bounds.width + 1 else {- self.finish(.failure(ConversationShareRenderError("Conversation share width changed unexpectedly.")))- return- }- let viewportHeight = webView.bounds.height+ webView.frame = CGRect(x: 0, y: 0, width: captureWidth, height: captureHeight)webView.setNeedsLayout()webView.layoutIfNeeded()- self.hostingWindow?.rootViewController?.view.setNeedsLayout()- self.hostingWindow?.rootViewController?.view.layoutIfNeeded()let requestedScale = max(0.25, self.scale)let maxScale = sqrt(conversationShareMaxOutputPixels / max(1, captureWidth * captureHeight))let effectiveScale = min(requestedScale, maxScale)let snapshot = WKSnapshotConfiguration()- snapshot.rect = webView.bounds+ snapshot.rect = CGRect(x: 0, y: 0, width: captureWidth, height: captureHeight)snapshot.snapshotWidth = NSNumber(value: Double(captureWidth * effectiveScale))snapshot.afterScreenUpdates = true- // 长页面不能用超出 WKWebView.bounds 的 rect 一次截图;按固定视口滚动分片,- // 每片的 rect 始终位于 bounds 内,再在原生侧拼接成完整 PNG。- CATransaction.flush()- self.captureTiles(- webView: webView,- snapshot: snapshot,- contentWidth: captureWidth,- contentHeight: captureHeight,- viewportHeight: viewportHeight,- effectiveScale: effectiveScale- ) { [weak self] result in- guard let self else { return }- switch result {- case .success(let image):- guard let data = image.pngData(), !data.isEmpty else {- self.finish(.failure(ConversationShareRenderError("Conversation share PNG is empty.")))- return- }- self.finish(.success(data.base64EncodedString()))- case .failure(let error):- self.finish(.failure(error))- }- }- }- }-- private func captureTiles(- webView: WKWebView,- snapshot: WKSnapshotConfiguration,- contentWidth: CGFloat,- contentHeight: CGFloat,- viewportHeight: CGFloat,- effectiveScale: CGFloat,- completion: @escaping (Result<UIImage, Error>) -> Void- ) {- let outputSize = CGSize(- width: contentWidth * effectiveScale,- height: contentHeight * effectiveScale- )- guard outputSize.width > 0, outputSize.height > 0 else {- completion(.failure(ConversationShareRenderError("Conversation share output is empty.")))- return- }- let tileOffsets: [CGFloat] = {- if contentHeight <= viewportHeight { return [0] }- var offsets = stride(from: CGFloat(0), through: contentHeight - viewportHeight, by: viewportHeight).map { $0 }- let lastOffset = contentHeight - viewportHeight- if offsets.last != lastOffset { offsets.append(lastOffset) }- return offsets- }()- captureTile(- index: 0,- offsets: tileOffsets,- webView: webView,- snapshot: snapshot,- effectiveScale: effectiveScale,- outputSize: outputSize,- tiles: [],- completion: completion- )- }-- private func captureTile(- index: Int,- offsets: [CGFloat],- webView: WKWebView,- snapshot: WKSnapshotConfiguration,- effectiveScale: CGFloat,- outputSize: CGSize,- tiles: [(offset: CGFloat, image: UIImage)],- completion: @escaping (Result<UIImage, Error>) -> Void- ) {- guard !completed else { return }- guard !offsets.isEmpty else {- completion(.failure(ConversationShareRenderError("Conversation share produced no tiles.")))- return- }- guard index < offsets.count else {- let format = UIGraphicsImageRendererFormat()- // outputSize 已包含 effectiveScale;renderer 再采用屏幕 scale 会把像素数额外- // 放大 4~9 倍,长图会在合成阶段超时或触发内存压力。- format.scale = 1- let renderer = UIGraphicsImageRenderer(size: outputSize, format: format)- let merged = renderer.image { _ in- for tile in tiles {- tile.image.draw(in: CGRect(- x: 0,- y: tile.offset * effectiveScale,- width: outputSize.width,- height: snapshot.rect.height * effectiveScale- ))- }- }- guard merged.hasVisibleVariation else {- completion(.failure(ConversationShareRenderError("Conversation share PNG is blank.")))- return- }- completion(.success(merged))- return- }- let offset = offsets[index]- webView.scrollView.setContentOffset(CGPoint(x: 0, y: offset), animated: false)- waitForWebContentPaint(webView) { [weak self, weak webView] result in- guard let self, let webView else { return }- guard case .success = result else {- if case .failure(let error) = result { completion(.failure(error)) }- return- }- webView.takeSnapshot(with: snapshot) { [weak self] image, error in- guard let self else { return }+ webView.takeSnapshot(with: snapshot) { image, error inif let error {- completion(.failure(error))+ self.finish(.failure(error))return}- guard let image else {- completion(.failure(ConversationShareRenderError("Conversation share tile is empty.")))+ guard let data = image?.pngData(), !data.isEmpty else {+ self.finish(.failure(ConversationShareRenderError("Conversation share PNG is empty.")))return}- self.captureTile(- index: index + 1,- offsets: offsets,- webView: webView,- snapshot: snapshot,- effectiveScale: effectiveScale,- outputSize: outputSize,- tiles: tiles + [(offset: offset, image: image)],- completion: completion- )- }- }- }-- private func waitForWebContentPaint(- _ webView: WKWebView,- completion: @escaping (Result<Void, Error>) -> Void- ) {- let script = """- return await new Promise((resolve) => {- requestAnimationFrame(() => requestAnimationFrame(resolve));- });- """- webView.callAsyncJavaScript(script, arguments: [:], in: nil, in: .page) { result in- switch result {- case .success:- completion(.success(()))- case .failure(let error):- completion(.failure(error))+ self.finish(.success(data.base64EncodedString()))}}}@@ -416,56 +236,11 @@ private final class ConversationShareHtmlRenderer: NSObject, WKNavigationDelegattimeoutWorkItem = nilwebView?.stopLoading()webView?.navigationDelegate = nil- webView?.removeFromSuperview()- hostingWindow?.isHidden = true- hostingWindow?.rootViewController = nil- hostingWindow = nilwebView = nilcompletion(result)}}-private extension UIImage {- var hasVisibleVariation: Bool {- guard let cgImage else { return false }- let sampleWidth = 64- let sampleHeight = 64- let bytesPerPixel = 4- let bytesPerRow = sampleWidth * bytesPerPixel- var pixels = [UInt8](repeating: 0, count: sampleHeight * bytesPerRow)- guard let context = CGContext(- data: &pixels,- width: sampleWidth,- height: sampleHeight,- bitsPerComponent: 8,- bytesPerRow: bytesPerRow,- space: CGColorSpaceCreateDeviceRGB(),- bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue- ) else {- return false- }- context.interpolationQuality = .low- context.draw(cgImage, in: CGRect(x: 0, y: 0, width: sampleWidth, height: sampleHeight))-- var minimum = [Int](repeating: 255, count: 3)- var maximum = [Int](repeating: 0, count: 3)- var hasOpaquePixel = false- for offset in stride(from: 0, to: pixels.count, by: bytesPerPixel) {- guard pixels[offset + 3] > 8 else { continue }- hasOpaquePixel = true- for channel in 0..<3 {- let value = Int(pixels[offset + channel])- minimum[channel] = min(minimum[channel], value)- maximum[channel] = max(maximum[channel], value)- }- }- guard hasOpaquePixel else { return false }- return zip(minimum, maximum).contains { lower, upper in- upper - lower >= 4- }- }-}-private struct ConversationShareRenderError: LocalizedError {let message: Stringdiff --git a/apps/mobile/src/__tests__/conversationShareWebViewHtml.test.ts b/apps/mobile/src/__tests__/conversationShareWebViewHtml.test.tsindex 3af2fed142..58ea6898a5 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 的完整源尺寸,并安全保留已分享 PNG', () => {+ it('完整回退原生增强,并安全保留已分享 PNG', () => {const nativeSource = readFileSync(resolve(process.cwd(),@@ -164,20 +164,9 @@ describe('buildConversationShareHtml 富内容导出', () => {const sessionSource = readFileSync(resolve(process.cwd(), 'app/sessions/[sessionId].tsx'),'utf8',- ).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',);@@ -190,29 +179,12 @@ 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(- "nativeConversationShareAvailable = Platform.OS === 'ios'",- );- expect(sessionSource).toContain(- 'if (!nativeConversationShareAvailable || !shareSelectionActive) return undefined;',- );- 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('nativeConversationShareAvailable');+ expect(sessionSource).not.toContain('renderConversationShareHtmlToPng({');const shareAsyncIndex = sessionSource.indexOf("await sharing.shareAsync(localUri, { mimeType: 'image/png' });",);