From 56af7b0d43a5c2e2dc38950d9c12322946b5e5a6 Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 14:15:50 +0800 Subject: [PATCH] Import apps/android/src/screens/intelligent-ui-card.tsx --- .../src/screens/intelligent-ui-card.tsx | 262 ++++++++++++++++++ 1 file changed, 262 insertions(+) create mode 100644 apps/android/src/screens/intelligent-ui-card.tsx diff --git a/apps/android/src/screens/intelligent-ui-card.tsx b/apps/android/src/screens/intelligent-ui-card.tsx new file mode 100644 index 0000000..271df24 --- /dev/null +++ b/apps/android/src/screens/intelligent-ui-card.tsx @@ -0,0 +1,262 @@ +/** + * Android's OPTIONAL UI surface. The trusted top-level HTML only hosts an + * audited local bundle; arbitrary model-generated HTML runs inside the + * bundle's sandboxed widget iframe, never as this page's document. + */ +import { useCallback, useEffect, useMemo, useRef, useState } from 'react' +import { Alert, Share, StyleSheet, Text, View } from 'react-native' +import * as Crypto from 'expo-crypto' +import WebView from 'react-native-webview' +import type { WebViewMessageEvent } from 'react-native-webview' +import { useAppStore } from '../state/store' +import type { UiArtifact } from '../state/ui-artifacts' +import { parseUiBridgeMessage, UiActionGuard } from '../state/ui-action-guard' +import { useTheme } from '../ui/theme-context' +import { INTELLIGENT_UI_RENDERER_CODE } from '../generated/intelligent-ui-renderer-code' + +const MAX_PROMPT = 4000 +const MAX_FRAME_HEIGHT = 4000 + +function documentFor(token: string): string { + // This is a trusted local bootstrap, not Host-supplied markup. Never place + // the artifact itself in HTML: it enters via a JSON argument after load. + const code = INTELLIGENT_UI_RENDERER_CODE.replace(/<\/script/gi, '<\\/script') + const mobileStyles = [ + 'html,body{padding:0;margin:0;overflow-x:hidden;background:transparent;-webkit-tap-highlight-color:transparent}', + '.diui-remote-title{display:none!important}', + '.diui-remote{padding:2px 0 6px;font-size:13px}', + '.diui-compose{display:flex;flex-direction:column;gap:10px;padding:2px 0 8px}', + '.diui-compose .diui-md{font-size:13px;line-height:1.5}', + '.diui-table-wrap{-webkit-overflow-scrolling:touch;touch-action:pan-x pan-y;overscroll-behavior-x:contain;border-radius:8px}', + '.diui-table th,.diui-table td{padding:8px 12px;font-size:13px}', + '.diui-table th{white-space:nowrap;font-weight:600}', + '.diui-button,.diui-choice-submit,.diui-table-tools button{min-height:36px;padding:6px 14px;border-radius:8px;touch-action:manipulation;font-size:13px}', + '.diui-table-tools{gap:8px;margin-bottom:8px}', + '.diui-table-tools input[type=search]{height:36px;padding:0 10px;font-size:13px;border-radius:8px}', + '.diui-choice-option{padding:12px 14px;border-radius:10px}', + '.diui-choice-submit{width:100%;justify-content:center;min-height:42px;font-weight:600;font-size:14px}', + '.diui-choice-hint{font-size:11px;line-height:1.4}', + '.diui-stat{padding:12px 14px;border-radius:10px}', + ].join('') + return '' + + '' + + '
' + + '' + + '' +} + +export function IntelligentUiCard({ artifact, onSendPrompt }: { artifact: UiArtifact; onSendPrompt?: (text: string) => Promise }) { + const { colors, scheme } = useTheme() + const token = useMemo(() => Crypto.randomUUID(), [artifact.hostId, artifact.sessionId, artifact.uiId]) + const html = useMemo(() => documentFor(token), [token]) + const source = useMemo(() => ({ html }), [html]) + const ref = useRef(null) + const ready = useRef(false) + const actions = useRef(new UiActionGuard()) + const mounted = useRef(false) + const [height, setHeight] = useState(180) + const [error, setError] = useState() + const current = useRef(artifact) + current.current = artifact + const scopeIsActive = () => { + const state = useAppStore.getState() + return state.selectedDevice?.deviceId === current.current.hostId + && state.selectedSession?.sessionId === current.current.sessionId + && (state.selectedSession?.backend === undefined || state.selectedSession.backend === 'harness') + && state.connection.phase === 'connected' + } + useEffect(() => { + mounted.current = true + const unsubscribe = useAppStore.subscribe(() => { + if (!scopeIsActive()) { + const revoked = actions.current.invalidate() + if (revoked && ready.current) { + reply(revoked.id, false, 'Session changed or disconnected; delivery unconfirmed. Inspect the session before retrying') + } + } + }) + return () => { + mounted.current = false + ready.current = false + actions.current.invalidate() + unsubscribe() + } + }, []) + const postArtifact = useCallback(() => { + // Android's offline policy is stricter than an optional Host CDN policy. + const value = current.current + const data = JSON.stringify({ ...value, policy: { + ...value.policy, allowNetwork: false, cdn: [], openLinkOrigins: [], + } }).replace(/ { if (ready.current) postArtifact() }, [artifact, postArtifact]) + useEffect(() => { + if (ready.current) ref.current?.injectJavaScript('window.__diuiSetTheme && window.__diuiSetTheme(' + JSON.stringify(scheme) + ');true;') + }, [scheme]) + const reply = (id: number, ok: boolean, error?: string) => { + ref.current?.injectJavaScript('window.__diuiReply && window.__diuiReply(' + + JSON.stringify(id) + ',' + JSON.stringify(ok) + ',' + JSON.stringify(error ?? '') + ');true;') + } + const handle = (event: WebViewMessageEvent) => { + if (!mounted.current) return + const msg = parseUiBridgeMessage(event.nativeEvent.data, token) + if (!msg) return + if (msg.type === 'ready') { + ready.current = true + postArtifact() + ref.current?.injectJavaScript('window.__diuiSetTheme && window.__diuiSetTheme(' + JSON.stringify(scheme) + ');' + + 'window.__diuiSetLocale && window.__diuiSetLocale("zh-CN");true;') + return + } + if (msg.type === 'error' && typeof msg.message === 'string') { + setError(msg.message.slice(0, 240)) + return + } + if (msg.type === 'download' && typeof msg.text === 'string') { + const name = typeof msg.name === 'string' && msg.name.trim() ? msg.name.trim() : 'data.txt' + void Share.share({ title: name, message: msg.text }) + return + } + if (msg.type === 'resize' && typeof msg.height === 'number' && Number.isFinite(msg.height)) { + const maxH = typeof current.current.policy?.maxHeight === 'number' + ? Math.max(100, Math.min(4000, current.current.policy.maxHeight)) + : MAX_FRAME_HEIGHT + setHeight(Math.min(maxH, Math.max(100, Math.ceil(msg.height) + 8))) + return + } + if (msg.type !== 'prompt' || !Number.isSafeInteger(msg.id) || typeof msg.text !== 'string') return + const id = msg.id as number + const text = msg.text.trim() + if (text.length === 0 || text.length > MAX_PROMPT + || !ready.current || !scopeIsActive()) { + reply(id, false, 'UI action is unavailable, invalid or rate-limited') + return + } + const ticket = actions.current.admit(id, Date.now()) + if (!ticket) { + reply(id, false, 'UI action is unavailable, invalid or rate-limited') + return + } + // A widget's arbitrary JS cannot prove a real gesture. Require a native + // confirmation even for declarative actions; never auto-prompt on load. + // Show the exact text we will submit. Android's native message dialog + // scrolls long content; never hide a suffix of a model-directed action. + Alert.alert('Send UI action?', text, [ + { text: 'Cancel', style: 'cancel', onPress: () => { + if (!actions.current.isCurrent(ticket)) return + actions.current.finish(ticket) + reply(id, false, 'Cancelled') + } }, + { text: 'Send', onPress: () => { + if (!mounted.current || !actions.current.isCurrent(ticket)) return + if (!scopeIsActive()) { + actions.current.finish(ticket) + reply(id, false, 'Session changed or disconnected') + return + } + // The native button callback is one-shot as well. Keep the gate busy + // until delivery settles, but a second callback cannot send again. + const now = useAppStore.getState() + if (!actions.current.submit(ticket)) return + if (onSendPrompt) { + void onSendPrompt(text).then(ok => { + if (!mounted.current || !actions.current.isCurrent(ticket)) return + reply(id, ok, ok ? undefined : 'Prompt was not accepted') + }).catch(e => { + if (!mounted.current || !actions.current.isCurrent(ticket)) return + reply(id, false, e instanceof Error ? e.message : 'Prompt failed') + }).finally(() => { actions.current.finish(ticket) }) + } else { + void now.sendMessage(text).then(outcome => { + if (!mounted.current || !actions.current.isCurrent(ticket)) return + reply(id, outcome === 'sent', outcome === 'kept' + ? 'Delivery unconfirmed; inspect the session before retrying' + : 'Prompt was not accepted') + }).catch(e => { + if (!mounted.current || !actions.current.isCurrent(ticket)) return + reply(id, false, e instanceof Error ? e.message : 'Prompt failed') + }).finally(() => { actions.current.finish(ticket) }) + } + } }, + ], { cancelable: false }) + } + + if (!INTELLIGENT_UI_RENDERER_CODE) return ( + + {artifact.title || 'Intelligent UI'} + Interactive UI requires the optional offline renderer package. The Host content was not executed. + + ) + return + + ◇ + + {artifact.title || 'Intelligent UI'} + + + + {error !== undefined + ? Could not display UI: {error} + : = (typeof artifact.policy?.maxHeight === 'number' ? Math.max(100, Math.min(4000, artifact.policy.maxHeight)) : MAX_FRAME_HEIGHT)} + nestedScrollEnabled={true} + javaScriptEnabled + domStorageEnabled={true} + javaScriptCanOpenWindowsAutomatically={false} + setSupportMultipleWindows={false} + allowFileAccess={false} + allowUniversalAccessFromFileURLs={false} + mixedContentMode="never" + onShouldStartLoadWithRequest={request => + request.url === 'about:blank' || request.url === 'about:srcdoc'} + onMessage={handle} + onLoadStart={() => { ready.current = false; actions.current.invalidate() }} + onLoadEnd={() => { if (!ready.current) postArtifact() }} + onError={() => { + ready.current = false + actions.current.invalidate() + setError('WebView failed to load') + }} + onRenderProcessGone={() => { + ready.current = false + actions.current.invalidate() + setError('WebView process stopped') + }} + />} + + +} +const styles = StyleSheet.create({ + card: { borderWidth: StyleSheet.hairlineWidth, borderRadius: 12, marginVertical: 6, overflow: 'hidden' }, + header: { flexDirection: 'row', alignItems: 'center', paddingHorizontal: 12, paddingVertical: 8, borderBottomWidth: StyleSheet.hairlineWidth, gap: 6 }, + titleIcon: { fontSize: 13, fontWeight: '700' }, + title: { fontSize: 13, fontWeight: '600', flex: 1 }, + content: { padding: 4 }, +})