import { heartbeatMessageBody } from '../../ui/heartbeat-message' import { ChatImageView } from '../../ui/chat-image' import { ArrowUp, Bot, Check, CircleCheck, ChevronDown, ChevronRight, ChevronUp, Code2, FilePenLine, FileSearch, ListTree, RefreshCw, ShieldAlert, Sparkles, Terminal, Wrench, User, X } from 'lucide-react-native' import { memo, useEffect, useMemo, useRef, useState } from 'react' import { AccessibilityInfo, ActivityIndicator, Animated, Modal, Pressable, ScrollView, StyleSheet, Text, TextInput, useWindowDimensions, View, } from 'react-native' import { Alert } from '../../ui/alert' import * as Clipboard from 'expo-clipboard' import { triggerLightHaptic } from '../../ui/haptics' import Svg, { Path } from 'react-native-svg' import { strings as zhCN } from '../../locales/i18n' import { foldHistory, hasVisibleMessageText } from '../../state/event-reducer' import { readToolHistoryDetail, useAppStore } from '../../state/store' import { formatQuoteDraftSnippet } from '../../ui/quote-helpers' import { IconChecklistOutline, IconCheckOutline, IconCopyOutline, IconDatabaseOutline } from '../../ui/dsh-icons' import type { UiArtifact } from '../../state/ui-artifacts' import type { ApprovalActivity, ChatImage, ChatItem, ChatMessage, QuestionActivity, ToolActivity, ToolDisplayDetail, } from '../../types' import { Button, IconButton } from '../../ui/components' import { FISH_LOGO_PATH, FISH_LOGO_VIEWBOX } from '../../ui/fish-logo' import { NativeMarkdown, useMarkdownLink } from '../../ui/markdown' import { parseMessageQuotes, QuoteMarkIcon, QuotePreview } from '../../ui/quote-preview' import { PresentedFileGroup } from '../../ui/file-card' import { useTheme } from '../../ui/theme-context' import { useThemedStyles } from '../../ui/use-themed-styles' import { DARK_CODE_PALETTE, highlightCode, LIGHT_CODE_PALETTE } from '../../ui/syntax-highlight' import { parseBotReplyPayload } from '../bot/replies' import { BotReplies } from '../bot/reply-view' import type { BotObject } from '../../services/bot-catalog' import type { ProcessActivity, ProcessSegment } from '../chat-process' import { processSummary, processDetail, processSegments, toolActivity, toolDisplayName } from '../chat-process' import { MessageActions } from '../message-actions' import { IntelligentUiCard } from '../intelligent-ui-card' import { createStyles } from './styles' /** Only activity ranges fold; completing a turn must never hide spoken replies. */ export const ChatProcessGroup = memo(function ChatProcessGroup({ items, artifacts, updatedArtifacts, availableUiIds, busyAction, onApproval, onQuestion }: { items: ChatItem[] artifacts: UiArtifact[] updatedArtifacts: UiArtifact[] availableUiIds: ReadonlySet busyAction?: string onApproval: (itemId: string, outcome: 'allowed-once' | 'rejected') => Promise onQuestion: (itemId: string, selected: Record, custom?: Record) => Promise }) { const { colors } = useTheme() const styles = useThemedStyles(createStyles) const backend = useAppStore(state => state.selectedSession?.backend) const last = items.at(-1) const codexClosed = backend === 'codex' && !items.some(isActiveChatItem) && last?.kind === 'message' && last.role === 'assistant' const end = items.find(item => item.turnEnd !== undefined)?.turnEnd ?? (codexClosed ? { reason: 'completed' as const, time: 0 } : undefined) const segments = useMemo(() => processSegments(items).flatMap((segment): (ProcessSegment | { kind: 'botReplies'; key: string; rows: BotObject[]; groupRelay: boolean })[] => { if (segment.kind === 'response') return [segment] const result: (ProcessSegment | { kind: 'botReplies'; key: string; rows: BotObject[]; groupRelay: boolean })[] = [] let pending: ChatItem[] = [] for (const item of segment.items) { const payload = item.kind === 'tool' && item.state === 'finished' ? parseBotReplyPayload(item.resultDetail?.text) : undefined if (!payload?.rows.length) { pending.push(item); continue } const groupRelay = item.kind === 'tool' && (item.toolKey ?? item.toolName) === 'group_relay' // A whole group relay is conversation content, not a visible tool step. // A partially recovered or Host-truncated round keeps its expandable tool // diagnostics: the visible replies are only a prefix of that round. if (!groupRelay || !payload.complete || payload.truncated) pending.push(item) if (pending.length) result.push({ kind: 'activity', key: `activity:${pending[0]!.id}`, items: pending }) pending = [] result.push({ kind: 'botReplies', key: `bot-replies:${item.id}`, rows: payload.rows, groupRelay }) } if (pending.length) result.push({ kind: 'activity', key: `activity:${pending[0]!.id}`, items: pending }) return result }), [items]) const unavailableUi = items.filter(item => item.kind === 'tool' && item.state === 'finished' && (item.toolKey === 'ui_render' || item.toolKey === 'ui_render_widget' || item.toolKey === 'ui_compose') && !availableUiIds.has(item.id) && (typeof item.uiResultMeta !== 'object' || item.uiResultMeta === null || (item.uiResultMeta as { ok?: unknown }).ok !== false)) const hasGroupRelayReplies = segments.some(segment => segment.kind === 'botReplies' && segment.groupRelay) const hasToolSummary = segments.some(segment => segment.kind === 'activity' && segment.items.some(item => item.kind === 'tool')) const label = end?.reason === 'stopped' ? zhCN.chatProcess.stopped : end?.reason === 'failed' ? zhCN.chat.failed : zhCN.chat.completed const onFilePress = useMarkdownLink() const editedFiles = useMemo(() => { const files: { path: string; name?: string; action?: 'created' | 'edited' }[] = [] const seen = new Set() for (const item of items) { if (item.kind !== 'tool' || item.state !== 'finished') continue const name = (item.toolKey ?? item.toolName).toLowerCase() if (!item.arguments) continue try { const args = JSON.parse(item.arguments) as Record let path: string | undefined let action: 'created' | 'edited' = 'edited' if (name === 'write' || name === 'write_file') { path = (args.file_path || args.path || args.filename) as string | undefined action = 'created' } else if (name === 'edit' || name === 'edit_file' || name === 'patch') { path = (args.file_path || args.path || args.filename) as string | undefined action = 'edited' } else if (name === 'str_replace_editor') { const cmd = args.command as string | undefined if (cmd === 'view') continue // Read-only command does not modify files path = (args.path || args.file_path) as string | undefined action = cmd === 'create' ? 'created' : 'edited' } if (typeof path === 'string' && path.trim() && !seen.has(path.trim())) { seen.add(path.trim()) files.push({ path: path.trim(), action }) } } catch {} } return files }, [items]) const lastResponseIndex = segments.map(s => s.kind).lastIndexOf('response') return {end !== undefined && !(end.reason === 'completed' && (hasGroupRelayReplies || hasToolSummary)) && {label}{end.reason !== 'completed' && end.detail !== undefined ? ` · ${end.detail}` : ''} } {segments.map((segment, index) => segment.kind === 'response' ? : segment.kind === 'botReplies' ? : )} {lastResponseIndex === -1 && } {lastResponseIndex === -1 && artifacts.map(ui => )} {updatedArtifacts.map(ui => {ui.title || 'Interactive UI'} updated to version {ui.version}; see its original card above.)} {unavailableUi.map(item => Interactive UI could not be restored from this Host result. Check the original tool output and history metadata.)} }, (prev, next) => { // Every field below is a reference that streaming rebuilds, so compare their // contents: a group whose text did not change keeps its rendered subtree. if (prev.busyAction !== next.busyAction) return false if (prev.onApproval !== next.onApproval || prev.onQuestion !== next.onQuestion) return false if (prev.items !== next.items) { if (prev.items.length !== next.items.length) return false for (let index = 0; index < prev.items.length; index += 1) { if (prev.items[index] !== next.items[index]) return false } } if (prev.artifacts !== next.artifacts) return false if (prev.updatedArtifacts !== next.updatedArtifacts) return false const before = prev.availableUiIds const after = next.availableUiIds if (before !== after) { if (before.size !== after.size) return false for (const id of before) { if (!after.has(id)) return false } } return true }) export const ChatActivityGroup = memo(function ChatActivityGroup({ items, closed, dense = false, busyAction, onApproval, onQuestion }: { items: ChatItem[] closed: boolean dense?: boolean busyAction?: string onApproval: (itemId: string, outcome: 'allowed-once' | 'rejected') => Promise onQuestion: (itemId: string, selected: Record, custom?: Record) => Promise }) { const { colors } = useTheme() const styles = useThemedStyles(createStyles) const { height } = useWindowDimensions() const [open, setOpen] = useState(false) const body = useRef(null) const pending = items.some(item => (item.kind === 'approval' || item.kind === 'question') && item.outcome === undefined) const expanded = pending || open const tools = items.filter((item): item is ToolActivity => item.kind === 'tool') const latestTool = tools.at(-1) const activeTool = closed ? undefined : tools.findLast(item => item.state === 'running') const runningReasoning = closed ? undefined : items.findLast(item => item.kind === 'message' && item.streamingPhase === 'reasoning') const running = activeTool !== undefined || runningReasoning !== undefined const counts = new Map() for (const tool of tools) counts.set(toolActivity(tool), (counts.get(toolActivity(tool)) ?? 0) + 1) const labels = zhCN.chatProcess.processActivity const completedSummary = tools.length > 0 && !running && closed const label = completedSummary ? `${zhCN.chat.completed} · ${tools.length} ${zhCN.chatProcess.toolCalls}` : tools.length > 0 ? processSummary(counts) : pending ? labels.questions.running : running ? labels[activeTool === undefined ? 'thinking' : toolActivity(activeTool)].running : processSummary(counts) const primary = activeTool ? toolActivity(activeTool) : [...counts].sort((a, b) => b[1] - a[1])[0]?.[0] const ActivityIcon = completedSummary ? CircleCheck : primary === 'commands' || primary === 'code' ? Terminal : primary === 'read' || primary === 'search' || primary === 'readImage' ? FileSearch : primary === 'edit' || primary === 'write' ? FilePenLine : primary === 'plan' ? ListTree : tools.length > 0 ? Wrench : Sparkles const detail = latestTool !== undefined ? processDetail(latestTool) : runningReasoning && runningReasoning.kind === 'message' ? compactActivityText(runningReasoning.reasoning)?.slice(-160) : undefined return setOpen(value => !value)} hitSlop={{ top: 7, bottom: 7 }} style={({ pressed }) => [styles.processHeader, dense && { minHeight: 32 }, pressed && styles.reasoningHeaderPressed]}> {label} {expanded ? : } {(latestTool !== undefined || running) && !expanded && {latestTool === undefined || (!closed && latestTool.state === 'running') ? : latestTool.state === 'failed' ? : } {latestTool ? toolDisplayName(latestTool) : labels.thinking.running}{detail ? ` · ${detail}` : ''} } {expanded && { if (running || pending) body.current?.scrollToEnd({ animated: false }) }} contentContainerStyle={styles.processBody}> {items.map(item => item.kind === 'message' && item.role === 'assistant' && !hasVisibleMessageText(item.text) && !item.images?.length ? : )} } }) export const ChatItemView = memo(function ChatItemView({ item, busyAction, compact, onApproval, onQuestion, artifacts, onSendPrompt, userBubble }: { item: ChatItem busyAction?: string compact: boolean /** Paint the user's own message as a bubble on the inverse surface (group chats). */ userBubble?: boolean onApproval: (itemId: string, outcome: 'allowed-once' | 'rejected') => Promise onQuestion: (itemId: string, selected: Record, custom?: Record) => Promise artifacts?: UiArtifact[] onSendPrompt?: (text: string) => Promise }) { if (item.kind === 'approval') return if (item.kind === 'question') return if (item.kind === 'tool') return const hasArtifacts = artifacts !== undefined && artifacts.length > 0 if (!compact && item.role === 'assistant' && !hasArtifacts && !hasVisibleMessageText(item.text) && !item.images?.length && hasVisibleMessageText(item.reasoning ?? '')) return return }) /** Retry affordance for a kept failed/unconfirmed row, with guarded resend. */ function UserRowResend({ item, status }: { item: ChatMessage; status: string }) { const { colors } = useTheme() const styles = useThemedStyles(createStyles) const busyAction = useAppStore(state => state.busyAction) const missingImages = (item.images?.length ?? 0) > 0 && (item.promptImages?.length ?? 0) === 0 const disabled = busyAction !== undefined || missingImages const retry = () => { const state = useAppStore.getState() const backend = state.sessions.find(row => row.sessionId === item.sessionId)?.backend ?? state.selectedSession?.backend const resend = () => { void state.retrySendMessage(item.id) } // Harness dedupes by request rpcId (official hasPromptRequest), so only // the carriers without request ids need the double-execution warning. if (item.sendState === 'unconfirmed' && backend !== undefined && backend !== 'harness') { Alert.alert(zhCN.chat.sendUnconfirmed, zhCN.chat.resendConfirm, [ { text: zhCN.common.cancel, style: 'cancel' }, { text: zhCN.chat.resend, onPress: resend }, ]) } else resend() } return ( [styles.resendRow, pressed && !disabled && styles.resendRowPressed, disabled && styles.resendRowDisabled]} > {missingImages ? zhCN.chat.resendImageMissing : `${status} · ${zhCN.chat.resend}`} ) } function HeartbeatMessage({ text, body }: { text: string; body: string }) { const [expanded, setExpanded] = useState(false) const { colors } = useTheme() const styles = useThemedStyles(createStyles) return setExpanded(value => !value)} style={styles.reasoningHeader}> {expanded ? zhCN.chat.heartbeatCollapse : zhCN.chat.heartbeatExpand} {expanded ? : } {expanded && {text}} } /** * In-place free text selection on assistant prose (Figure 2 / IMG_1488). * Allows the user to freely select any snippet using native handles. * When text is selected, floating callout menu [询问智能体] [拷贝] appears * right above the selection. */ function AssistantMessageCallout({ item }: { item: ChatMessage }) { const styles = useThemedStyles(createStyles) const body = heartbeatMessageBody(item.text, 'assistant') return ( {body === undefined ? : } ) } function MessageBubble({ item, compact, artifacts, editedFiles, onFilePress, onSendPrompt, userBubble }: { item: ChatMessage compact: boolean userBubble?: boolean artifacts?: UiArtifact[] editedFiles?: { path: string; name?: string; action?: 'created' | 'edited' }[] onFilePress?: (path: string) => void onSendPrompt?: (text: string) => Promise }) { const { colors, appearance } = useTheme() const styles = useThemedStyles(createStyles) const user = item.role === 'user' const remote = item.role === 'assistant' const showReasoning = !compact && remote && hasVisibleMessageText(item.reasoning ?? '') const showText = hasVisibleMessageText(item.text) const showImages = item.images !== undefined && item.images.length > 0 const showStreaming = item.streaming === true && item.streamingPhase !== 'reasoning' const hasArtifacts = artifacts !== undefined && artifacts.length > 0 // Harness renders assistant prose directly on the conversation canvas. if (remote) { return ( {showReasoning && } {showImages && } {showText && ( )} {showStreaming && ( )} {artifacts?.map(ui => ( ))} {editedFiles?.length && onFilePress ? : null} {!item.streaming && (showText || hasArtifacts) && } ) } if (user) { const undelivered = item.sendState === 'failed' || item.sendState === 'unconfirmed' const status = item.sendError ?? (item.sendState === 'failed' ? zhCN.chat.sendFailed : zhCN.chat.sendUnconfirmed) const heartbeatBody = heartbeatMessageBody(item.text, 'user') const { quotes, body } = parseMessageQuotes(item.text) const hasBubbleText = heartbeatBody !== undefined || quotes.length > 0 || hasVisibleMessageText(body) const solidText = colors.onPrimary ?? colors.white const isSolid = !userBubble && appearance.bubbleStyle === 'solid' const bubblePalette = { ...colors, ink: colors.onInverse, muted: colors.onInverse, subtle: colors.onInverse, surfaceStrong: colors.inverse } const solidPalette = { ...colors, ink: solidText, muted: solidText, subtle: solidText, primary: solidText, surfaceStrong: 'rgba(255,255,255,0.18)' } const softBubblePalette = { ...colors, surfaceStrong: colors.border } const userBubbleStyle = userBubble ? styles.userBubble : !hasBubbleText ? styles.userMediaOnly : appearance.bubbleStyle === 'tinted' ? styles.userTintedBubble : appearance.bubbleStyle === 'solid' ? styles.userSolidBubble : appearance.bubbleStyle === 'minimal' ? styles.userMinimalBubble : styles.userSoftBubble const quoteTint = userBubble ? colors.onInverse : isSolid ? solidText : colors.primary return {showImages && } {heartbeatBody !== undefined ? : quotes.length > 0 ? ( {quotes.map((quoteText, index) => ( “{formatQuoteDraftSnippet(quoteText, 16)}” ))} {hasVisibleMessageText(body) && ( {body} )} ) : ( hasVisibleMessageText(body) && ( ) )} {item.sendState === 'canceled' ? {zhCN.chat.sendCanceled} : undelivered ? : } {/* Failed rows keep their actions: losing copy would be a regression. */} {undelivered && item.sendState !== 'canceled' && } } return ( {user ? ( ) : ( )} {user ? zhCN.chat.you : zhCN.chat.system} {showImages && } {showText && } {showStreaming && } ) } function ReasoningDisclosure({ item }: { item: ChatMessage }) { const { colors } = useTheme() const styles = useThemedStyles(createStyles) const [expanded, setExpanded] = useState(false) const active = item.streamingPhase === 'reasoning' const label = active ? zhCN.chat.reasoningActive : zhCN.chat.reasoning const preview = compactActivityText(item.reasoning) ?? '' const actionLabel = expanded ? zhCN.chat.reasoningCollapse : zhCN.chat.reasoningExpand return ( setExpanded(value => !value)} style={({ pressed }) => [styles.reasoningHeader, pressed && styles.reasoningHeaderPressed]} > {label} · {preview} {expanded ? : } {expanded && } ) } function ToolRow({ item: original, compact }: { item: ToolActivity; compact: boolean }) { const [loaded, setLoaded] = useState() const [loadingDetail, setLoadingDetail] = useState(false) const [detailError, setDetailError] = useState() const detailAbort = useRef(undefined) const item = loaded ?? original useEffect(() => {setLoaded(undefined); setLoadingDetail(false); setDetailError(undefined); return () => detailAbort.current?.abort()}, [original.remoteDetail?.detailId]) const loadDetail = async () => { if (loadingDetail || loaded !== undefined || original.remoteDetail === undefined) return const controller = new AbortController(); detailAbort.current = controller setLoadingDetail(true); setDetailError(undefined) try { const record = await readToolHistoryDetail(original.remoteDetail, controller.signal) as {event: import('../../types').NativeSessionEvent; view?: import('../../types').HistoryEntry['view']} if (controller.signal.aborted) return const full = foldHistory([record], original.sessionId).find(row => row.kind === 'tool' && row.id === original.id) if (full?.kind !== 'tool') throw new Error(zhCN.comfort.toolInvalid) setLoaded({...original, ...full, callDetail: original.callDetail, remoteDetail: undefined}) } catch (error) {if (!controller.signal.aborted) setDetailError(error instanceof Error ? error.message : zhCN.comfort.toolUnavailable)} finally {if (!controller.signal.aborted) setLoadingDetail(false)} } const { colors } = useTheme() const styles = useThemedStyles(createStyles) const [expanded, setExpanded] = useState(false) const stateText = item.state === 'running' ? zhCN.status.running : item.state === 'failed' ? zhCN.chat.failed : zhCN.chat.completed const detail = processDetail(item) const name = toolDisplayName(item) const hasDetail = (item.callDetail !== undefined || item.resultDetail !== undefined || item.remoteDetail !== undefined) return ( {if (!expanded) void loadDetail(); setExpanded(value => !value)}} style={({ pressed }) => [styles.toolRow, pressed && hasDetail && styles.toolRowPressed]} > {item.toolName === 'todo_write' || (item.toolKey ?? '').endsWith('todo_write') ? ( ) : /command|bash|shell|命令|终端/i.test(item.toolName) ? ( ) : ( )} {name} {detail !== undefined && ·} {detail !== undefined && {detail}} {item.state === 'running' && ( )} {item.state === 'failed' && {stateText}} {hasDetail && (expanded ? : )} {item.images !== undefined && item.images.length > 0 && } {expanded && hasDetail && ( {loadingDetail && {zhCN.comfort.toolLoading}} {detailError !== undefined && void loadDetail()}>{detailError} · {zhCN.common.retry}} {item.callDetail !== undefined && } {item.resultDetail !== undefined && } )} ) } export function ChatImages({ images, alignEnd = false, tool = false }: { images: ChatImage[]; alignEnd?: boolean; tool?: boolean }) { const styles = useThemedStyles(createStyles) return ( {images.map((image, index) => )} ) } function ToolDetailView({ label, detail }: { label: string; detail: ToolDisplayDetail }) { const { scheme, appearance } = useTheme() const styles = useThemedStyles(createStyles) const useDarkCode = appearance.codeTheme === 'dark' || (appearance.codeTheme === 'auto' && scheme === 'dark') const palette = useDarkCode ? DARK_CODE_PALETTE : LIGHT_CODE_PALETTE const trimmed = detail.text.trimStart() const likelyJson = detail.format !== 'markdown' && (trimmed.startsWith('{') || trimmed.startsWith('[')) const tokens = useMemo( () => (detail.format === 'markdown' ? [] : highlightCode(detail.text, likelyJson ? 'json' : undefined)), [detail.format, detail.text, likelyJson], ) return ( {label} {detail.format === 'markdown' ? : ( {tokens.map((token, index) => ( token.kind === 'plain' ? token.text : {token.text} ))} )} {detail.truncated && {zhCN.chat.toolTruncated}} ) } function compactActivityText(value: string | undefined): string | undefined { if (value === undefined) return undefined const compact = value.replace(/\\[nrt]/g, ' ').replace(/\s+/g, ' ').trim() return compact.length === 0 ? undefined : compact } function ApprovalCard({ item, busy, onRespond }: { item: ApprovalActivity busy: boolean onRespond: (itemId: string, outcome: 'allowed-once' | 'rejected') => Promise }) { const { colors } = useTheme() const styles = useThemedStyles(createStyles) if (item.outcome !== undefined) { const denied = item.outcome === 'rejected' || item.outcome === 'cancelled' || item.outcome === 'unavailable' const handledElsewhere = item.outcome === 'unavailable' return ( {denied && !handledElsewhere ? : } {handledElsewhere ? zhCN.chat.approvalHandled : denied ? zhCN.chat.denied : zhCN.chat.allowedOnce} ) } return ( {zhCN.chat.permissionTitle} {zhCN.chat.hostOperation(item.toolName)} {item.reason !== undefined && ( {item.reason} )} {zhCN.chat.permissionScope}