From 591dc52ce70cd13c838ff794b77cc29d4abfb2ff Mon Sep 17 00:00:00 2001 From: QQ Date: Tue, 17 Nov 2026 08:28:47 +0800 Subject: [PATCH] Import apps/android/src/screens/chat/composer.tsx --- apps/android/src/screens/chat/composer.tsx | 382 ++++++++++++--------- 1 file changed, 216 insertions(+), 166 deletions(-) diff --git a/apps/android/src/screens/chat/composer.tsx b/apps/android/src/screens/chat/composer.tsx index 4314ae4..dd6f13d 100644 --- a/apps/android/src/screens/chat/composer.tsx +++ b/apps/android/src/screens/chat/composer.tsx @@ -1,19 +1,17 @@ -import { ArrowUp, ChevronDown, CircleStop, Plus, Zap } from 'lucide-react-native' -import { useMemo } from 'react' +import { ArrowUp, CircleStop, Plus, X, Zap } from 'lucide-react-native' +import { useMemo, useState, type RefObject } from 'react' import { ActivityIndicator, Pressable, Text, TextInput, View } from 'react-native' -import Svg, { Path } from 'react-native-svg' import { strings } from '../../locales/i18n' import type { RemoteSession } from '../../types' -import { FISH_LOGO_PATH, FISH_LOGO_VIEWBOX } from '../../ui/fish-logo' -import { MentionPopover, splitDraftSegments } from '../../ui/mention-popover' +import { MentionPopover, splitDraftSegments, type MentionGroup } from '../../ui/mention-popover' import { formatQuoteDraftSnippet } from '../../ui/quote-helpers' import { QuoteMarkIcon } from '../../ui/quote-preview' import { useTheme } from '../../ui/theme-context' import { useThemedStyles } from '../../ui/use-themed-styles' -import { ReplyStatusDots } from './messages' import { ExpandCornerIcon } from '../../ui/composer-expand-icons' +import { triggerLightHaptic } from '../../ui/haptics' +import { nextStackedOverflowLength, shouldShowExpandCorner, shouldUseStackedComposer } from './composer-gate' import { createStyles } from './styles' -import type { useChatMentions } from './use-chat-mentions' type BackendChatTextKeys = Record< 'generating' | 'messageLabel' | 'placeholder' | 'policyHint', @@ -21,7 +19,7 @@ type BackendChatTextKeys = Record< > // Resolve these keys during rendering so language changes use the current catalog. -const BACKEND_CHAT_TEXT_KEYS: Partial> & { harness: BackendChatTextKeys } = { +export const BACKEND_CHAT_TEXT_KEYS: Partial> & { harness: BackendChatTextKeys } = { harness: { generating: 'generating', messageLabel: 'messageLabel', placeholder: 'placeholder', policyHint: 'policyHint' }, codex: { generating: 'codexGenerating', messageLabel: 'codexMessageLabel', placeholder: 'codexPlaceholder', policyHint: 'codexPolicyHint' }, cursor: { generating: 'cursorGenerating', messageLabel: 'cursorMessageLabel', placeholder: 'cursorPlaceholder', policyHint: 'cursorPolicyHint' }, @@ -35,15 +33,31 @@ const BACKEND_CHAT_TEXT_KEYS: Partial> & { h /** Soft hint threshold; the composer accepts longer input instead of silently truncating. */ const LONG_DRAFT_HINT = 12_000 +/** Shared input adapter so both Agent chat and DS-Bot reuse the same composer shell. */ +export type ComposerInputBridge = { + composerInputRef?: RefObject + mentionType: string | null + mentionGroups: MentionGroup[] + selection?: { start: number; end: number } + closeMention: () => void + onChangeText: (value: string) => void + onSelectionChange: (event: { nativeEvent: { selection: { start: number; end: number } } }) => void +} + type Props = { - backend: RemoteSession['backend'] + backend?: RemoteSession['backend'] draft: string quotes?: string[] onRemoveQuote?: (index: number) => void - input: ReturnType + input: ComposerInputBridge + accessibilityLabel?: string + placeholder?: string + highlightMentionTokens?: boolean replyActive: boolean stopping: boolean - compactRunning: boolean + sending?: boolean + /** A tracked Host command (for example `/compact`) is running. */ + commandRunning?: boolean hasComposerContent: boolean connected: boolean /** Text editing is local, so a dropped carrier must not lock this field. */ @@ -52,197 +66,233 @@ type Props = { sendDisabled: boolean compactComposer: boolean composerInputMaxHeight: number - model: { available: boolean; label: string | undefined; selecting: boolean; onOpen: () => void } - promptsPickerOpen: boolean - onExpand: () => void onOpenExpand?: () => void onOpenActions: () => void - onOpenPrompts: () => void + onOpenPrompts?: () => void onSubmit: () => void onStop: () => void } -export function ChatComposer({ backend, draft, quotes, onRemoveQuote, input, replyActive, stopping, compactRunning, +export function ChatComposer({ backend, draft, quotes, onRemoveQuote, input, accessibilityLabel, placeholder, + highlightMentionTokens = true, replyActive, stopping, sending = false, commandRunning, hasComposerContent, connected, editable, composerDisabled, sendDisabled, compactComposer, - composerInputMaxHeight, model, promptsPickerOpen, onExpand, onOpenExpand, onOpenActions, onOpenPrompts, onSubmit, onStop }: Props) { + composerInputMaxHeight, onOpenExpand, onOpenActions, onOpenPrompts, onSubmit, onStop }: Props) { const { colors } = useTheme() const styles = useThemedStyles(createStyles) const { composerInputRef, mentionType, mentionGroups, selection, closeMention, onChangeText, onSelectionChange } = input - const draftSegments = useMemo(() => splitDraftSegments(draft), [draft]) + const draftSegments = useMemo(() => (highlightMentionTokens ? splitDraftSegments(draft) : []), [draft, highlightMentionTokens]) + const hasTokens = useMemo( + () => highlightMentionTokens && draftSegments.some(segment => segment.token), + [draftSegments, highlightMentionTokens], + ) + const inputChildren = useMemo( + () => (hasTokens + ? ( + + {draftSegments.map((segment, index) => ( + {segment.text} + ))} + + ) + : undefined), + [colors.ink, colors.primary, draftSegments, hasTokens], + ) const backendTextKeys = BACKEND_CHAT_TEXT_KEYS[backend ?? 'harness'] ?? BACKEND_CHAT_TEXT_KEYS.harness - const replyStatusLabel = stopping ? strings.chat.stopping : strings.chat[backendTextKeys.generating] + const resolvedLabel = accessibilityLabel ?? strings.chat[backendTextKeys.messageLabel] + const resolvedPlaceholder = placeholder ?? strings.chat[backendTextKeys.placeholder] + const [contentHeight, setContentHeight] = useState(0) + const [stackedOverflowLength, setStackedOverflowLength] = useState(null) + const hasQuotes = quotes !== undefined && quotes.length > 0 + const effectiveContentHeight = draft.length === 0 ? 0 : contentHeight + const effectiveOverflowLength = draft.length === 0 ? null : stackedOverflowLength + const isStacked = shouldUseStackedComposer({ + draft, + hasQuotes, + overflowLength: effectiveOverflowLength, + }) + const showExpand = isStacked && onOpenExpand !== undefined && shouldShowExpandCorner({ + draft, + contentHeight: effectiveContentHeight, + }) + const rightInlineReserve = 40 + + (onOpenPrompts !== undefined ? 40 : 0) + + ((replyActive || commandRunning) && hasComposerContent ? 34 : 0) return <> - {replyActive && ( - - - {replyStatusLabel} - {!stopping && } - - )} {mentionType !== null && ( )} - {compactRunning ? - [styles.plusButton, pressed && styles.plusPressed]}> - - - - {strings.sessionActivity.composeRunning} - - [styles.stopButton, pressed && !stopping && styles.stopPressed]}> - {stopping ? : } - - : - {draft.length > 0 && !compactRunning && onOpenExpand !== undefined && ( - [styles.composerExpandButton, pressed && styles.plusPressed]} - > - - + + {hasQuotes && ( + + {quotes.map((quoteText, index) => ( + onRemoveQuote?.(index)} + hitSlop={4} + style={styles.composerQuotePill} + > + + + “{formatQuoteDraftSnippet(quoteText, 18)}” + + + + ))} + )} - - {quotes?.map((quoteText, index) => ( + {/* Keep Child 0 (TextInput) and Child 1 (action bar) in fixed React tree positions so inline <-> stacked promotion never unmounts the input or drops IME focus. */} + + + { + const nextHeight = event.nativeEvent.contentSize.height + setContentHeight(nextHeight) + setStackedOverflowLength(prev => nextStackedOverflowLength({ + draft, + contentHeight: nextHeight, + currentOverflowLength: prev, + })) + }} + onKeyPress={({ nativeEvent }) => { + if (nativeEvent.key === 'Backspace' && draft.length === 0 && quotes && quotes.length > 0) { + onRemoveQuote?.(quotes.length - 1) + } + }} + selection={selection} + placeholder={resolvedPlaceholder} + placeholderTextColor={colors.muted} + multiline + editable={editable} + selectionColor={colors.accent} + cursorColor={colors.accent} + children={inputChildren} + /> + + onRemoveQuote?.(index)} - style={styles.composerInlineQuoteBadge} + accessibilityLabel={strings.chat.moreActions} + accessibilityState={{ disabled: composerDisabled }} + disabled={composerDisabled} + onPress={() => { + triggerLightHaptic() + onOpenActions() + }} + hitSlop={6} + style={({ pressed }) => [styles.plusButton, pressed && styles.plusPressed, composerDisabled && styles.plusDisabled]} > - - - “{formatQuoteDraftSnippet(quoteText, 14)}” - + - ))} - {/* Native attributed text keeps references blue without an opaque Android value layer. */} - 0 && !compactRunning && onOpenExpand !== undefined && { paddingRight: 32 }]} - onChangeText={onChangeText} - onSelectionChange={onSelectionChange} - onKeyPress={({ nativeEvent }) => { - if (nativeEvent.key === 'Backspace' && draft.length === 0 && quotes && quotes.length > 0) { - onRemoveQuote?.(quotes.length - 1) - } - }} - - selection={selection} - placeholder={strings.chat[backendTextKeys.placeholder]} - placeholderTextColor={colors.muted} - multiline - editable={editable} - selectionColor={colors.accent} - cursorColor={colors.accent} - > - {draftSegments.map((segment, index) => {segment.text})} - - - - [styles.plusButton, pressed && styles.plusPressed, composerDisabled && styles.plusDisabled]} - > - - - [styles.plusButton, pressed && styles.plusPressed, composerDisabled && styles.plusDisabled]} - > - - - - {model.available && ( - - {model.label} - {model.selecting ? : } - - )} - {replyActive && !hasComposerContent - ? [styles.stopButton, pressed && !stopping && styles.stopPressed, stopping && styles.sendDisabled]} - > - {stopping - ? - : } - - : <> - {/* With text in the composer the primary action is the send - (queueing) button — tapping where Send lives must never stop - the running task (official InputBar behaviour). Stop stays - available as its own secondary button. */} - {replyActive && ( + + {onOpenPrompts !== undefined && ( [styles.composerShortcutButton, pressed && styles.plusPressed, composerDisabled && styles.plusDisabled]} + > + + + )} + {(replyActive || commandRunning) && !hasComposerContent + ? [styles.stopButtonSmall, pressed && !stopping && styles.stopPressed, stopping && styles.sendDisabled]} + onPress={() => { + triggerLightHaptic() + onStop() + }} + hitSlop={6} + style={({ pressed }) => [styles.stopButton, pressed && !stopping && styles.stopPressed, stopping && styles.sendDisabled]} > {stopping - ? - : } + ? + : } - )} - [styles.sendButton, pressed && styles.sendPressed, sendDisabled && styles.sendDisabled]} - > - - - } + : <> + {/* With text in the composer the primary action is the send + (queueing) button — tapping where Send lives must never stop + the running task (official InputBar behaviour). Stop stays + available as its own secondary button. */} + {(replyActive || commandRunning) && ( + { + triggerLightHaptic() + onStop() + }} + hitSlop={6} + style={({ pressed }) => [styles.stopButtonSmall, pressed && !stopping && styles.stopPressed, stopping && styles.sendDisabled]} + > + {stopping + ? + : } + + )} + [styles.sendButton, pressed && styles.sendPressed, !sending && sendDisabled && styles.sendDisabled]} + > + {sending + ? + : } + + } + + + {showExpand && ( + [styles.composerExpandCorner, pressed && styles.plusPressed]} + > + + + )} - } + {draft.length > LONG_DRAFT_HINT && ( {strings.chat.draftLong(draft.length)} )} {/* The draft survives a carrier drop; say so instead of silently swallowing an offline send tap (the primary action stays disabled). */} - {!connected && draft.length > 0 && ( - {strings.chat.offlineSendBlocked} - )} - {/* Landscape phones leave almost no space above the IME once the - composer is visible; keep the privacy note for roomier windows. */} - {!compactComposer && ( - - {strings.chat[backendTextKeys.policyHint]} - + {!connected && (draft.length > 0 || hasComposerContent) && ( + {strings.chat.offlineSendBlocked} )} }