diff --git a/apps/android/src/screens/chat-trajectory.tsx b/apps/android/src/screens/chat-trajectory.tsx new file mode 100644 index 0000000..7bf0917 --- /dev/null +++ b/apps/android/src/screens/chat-trajectory.tsx @@ -0,0 +1,155 @@ +import { memo, useMemo, useState, type ReactNode } from 'react' +import { ActivityIndicator, FlatList, Keyboard, Pressable, ScrollView, StyleSheet, Text, TextInput, View } from 'react-native' +import { Bot, ChevronDown, ChevronRight, Layers, Search, Settings2, User, Wrench, X } from 'lucide-react-native' +import type { ChatImage, ChatItem, ChatMessage } from '../types' +import { strings } from '../locales/i18n' +import { IconButton } from '../ui/components' +import { NativeMarkdown } from '../ui/markdown' +import { radius, spacing, type, type ThemeColors } from '../ui/theme' +import { useResponsiveLayout } from '../ui/responsive' +import { useTheme } from '../ui/theme-context' +import { useThemedStyles } from '../ui/use-themed-styles' +import { MessageActions, messageClock, messageTime } from './message-actions' + +type Category = 'all' | 'system' | 'user' | 'context' | 'assistant' | 'tool' +const CATEGORIES = ['all', 'system', 'user', 'context', 'assistant', 'tool'] as const +const CATEGORY_ICONS = { system: Settings2, user: User, context: Layers, assistant: Bot, tool: Wrench } +function category(item: ChatItem): Exclude { + if (item.kind !== 'message') return 'tool' + return item.context ? 'context' : item.role +} +function content(item: ChatItem): string { + if (item.kind === 'message') return `${item.text}\n${item.reasoning ?? ''}` + if (item.kind === 'tool') return `${item.toolName}\n${item.arguments ?? ''}\n${item.summary ?? ''}\n${item.callDetail?.text ?? ''}\n${item.resultDetail?.text ?? ''}` + if (item.kind === 'approval') return `${item.toolName}\n${item.reason ?? ''}` + return item.questions.map(question => question.question).join('\n') +} + +/** Keeps the real message order; only the disclosure changes what is rendered. */ +export function ChatTrajectory({ items, renderItem, renderImages, hasMore, loading, loadOlder }: { + items: ChatItem[] + renderItem: (item: ChatItem) => ReactNode + renderImages: (images: ChatImage[]) => ReactNode + hasMore: boolean + loading: boolean + loadOlder: () => void +}) { + const { colors } = useTheme() + const styles = useThemedStyles(createStyles) + const { contentMaxWidth } = useResponsiveLayout() + const [filter, setFilter] = useState('all') + const [query, setQuery] = useState('') + const rows = useMemo(() => { + const search = query.trim().toLocaleLowerCase() + return items.filter(item => (filter === 'all' || category(item) === filter) + && `${content(item)}\n${item.turn ?? ''}`.toLocaleLowerCase().includes(search)) + }, [items, filter, query]) + return + + + Keyboard.dismiss()} style={styles.searchInput} /> + {query.length > 0 && setQuery('')} />} + + + + {CATEGORIES.map(key => setFilter(key)} style={({ pressed }) => [styles.filter, key === filter && styles.filterSelected, pressed && styles.pressed]}> + {strings.trajectory[key]} + )} + + + item.id} keyboardShouldPersistTaps="handled" keyboardDismissMode="on-drag" + onScrollBeginDrag={() => Keyboard.dismiss()} initialNumToRender={6} maxToRenderPerBatch={6} + ListHeaderComponent={ + {strings.trajectory.loadedOnly} + {hasMore && [styles.loadOlder, pressed && styles.pressed]}> + {loading ? : {strings.chat.older}} + } + } + ListEmptyComponent={{strings.trajectory.empty}} + contentContainerStyle={styles.list} renderItem={({ item }) => } /> + +} + +const TrajectoryEntry = memo(function TrajectoryEntry({ item, renderItem, renderImages }: { + item: ChatItem; renderItem: (item: ChatItem) => ReactNode; renderImages: (images: ChatImage[]) => ReactNode +}) { + const { colors } = useTheme() + const styles = useThemedStyles(createStyles) + const [expanded, setExpanded] = useState(false) + const kind = category(item) + const Icon = CATEGORY_ICONS[kind] + const message = item.kind === 'message' ? item : undefined + const time = messageTime(item) + const label = strings.trajectory[kind] + const preview = message === undefined ? '' : (message.text.trim() || message.reasoning?.trim() + || ((message.images?.length ?? 0) > 0 ? strings.chat.unnamedImage : strings.trajectory.noText)) + .replace(/^#{1,6}\s+/gm, '').replace(/\*\*|`/g, '').trim() + const meta = + + {label} + {item.turn !== undefined && {strings.trajectory.turn} {item.turn}} + {time !== undefined && {messageClock(time)}} + {message !== undefined && (expanded ? : )} + + return + {message === undefined ? <>{meta}{renderItem(item)} : <> + setExpanded(value => !value)} + style={({ pressed }) => [styles.messageHeader, pressed && styles.pressed]}> + {meta} + {!expanded && {preview}} + {!expanded && (message.images?.length ?? 0) > 0 && {strings.trajectory.attachments(message.images!.length)}} + + {expanded && } + } + +}) + +function TrajectoryMessage({ item, renderImages }: { item: ChatMessage; renderImages: (images: ChatImage[]) => ReactNode }) { + const styles = useThemedStyles(createStyles) + return + {(item.reasoning?.trim().length ?? 0) > 0 && + {strings.chat.reasoning} + + } + {(item.images?.length ?? 0) > 0 && renderImages(item.images!)} + {item.text.trim().length > 0 && } + {!item.context && item.role !== 'system' && !item.streaming && item.text.length > 0 && } + +} + +const createStyles = (colors: ThemeColors) => StyleSheet.create({ + root: { flex: 1, minHeight: 0 }, + search: { flexDirection: 'row', alignItems: 'center', gap: spacing.xs, marginHorizontal: spacing.lg, marginTop: spacing.sm, + paddingLeft: spacing.sm, paddingRight: spacing.xxs, minHeight: 48, borderRadius: radius.md, backgroundColor: colors.surfaceStrong }, + searchInput: { ...type.small, flex: 1, minWidth: 0, minHeight: 48, paddingVertical: spacing.sm, color: colors.ink }, + filterBar: { borderBottomWidth: StyleSheet.hairlineWidth, borderBottomColor: colors.separator }, + filters: { paddingHorizontal: spacing.lg, gap: spacing.md }, + filter: { minHeight: 48, paddingHorizontal: spacing.xxs, alignItems: 'center', justifyContent: 'center', borderBottomWidth: 2, borderBottomColor: 'transparent' }, + filterSelected: { borderBottomColor: colors.primary }, + filterText: { ...type.small, color: colors.muted }, + filterTextSelected: { color: colors.primary, fontWeight: '600' }, + pressed: { backgroundColor: colors.surfaceStrong }, + list: { paddingHorizontal: spacing.lg, paddingBottom: spacing.xl, flexGrow: 1 }, + listHeader: { paddingTop: spacing.sm, paddingBottom: spacing.xxs }, + hint: { ...type.caption, color: colors.muted }, + loadOlder: { minHeight: 48, alignItems: 'center', justifyContent: 'center', marginTop: spacing.xs }, + loadOlderText: { ...type.small, color: colors.primary }, + entry: { paddingVertical: spacing.md, borderBottomWidth: StyleSheet.hairlineWidth, borderBottomColor: colors.separator }, + meta: { flexDirection: 'row', alignItems: 'center', gap: spacing.xs, minHeight: 24, marginBottom: spacing.xs }, + role: { ...type.caption, color: colors.muted }, + turn: { ...type.caption, color: colors.muted, flexShrink: 1 }, + time: { ...type.caption, color: colors.muted, marginLeft: 'auto', fontVariant: ['tabular-nums'] }, + messageHeader: { minHeight: 48 }, + preview: { ...type.small, color: colors.ink }, + attachmentHint: { ...type.caption, color: colors.muted, marginTop: spacing.xs }, + messageBody: { gap: spacing.sm }, + reasoning: { gap: spacing.xs }, + empty: { flex: 1, alignItems: 'center', justifyContent: 'center', gap: spacing.sm, paddingVertical: spacing.xxxl }, + emptyText: { ...type.small, color: colors.muted, textAlign: 'center' }, +})