Co-authored-by: T3 Code Test <[email protected]> Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1388 lines
59 KiB
TypeScript
1388 lines
59 KiB
TypeScript
import { useAtomValue } from "@effect/atom-react";
|
|
import { useThreadReportedModelSelection } from "../../state/entities";
|
|
import { UsageLimitRecoveryCard } from "./UsageLimitRecoveryCard";
|
|
import { useNavigation } from "@react-navigation/native";
|
|
import type { WorktreeSetupCardProps } from "./worktree-setup-card";
|
|
import type { ComposerTextPaste } from "../../native/T3ComposerEditor.types";
|
|
import { type EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
|
|
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
|
|
import type {
|
|
CodexFeedbackSubmission,
|
|
EnvironmentThreadStatus,
|
|
} from "@t3tools/client-runtime/state/threads";
|
|
import { useKeyboardChatComposerInset, useKeyboardScrollToEnd } from "@legendapp/list/keyboard";
|
|
import { resolveProviderSkillsForCwd } from "@t3tools/client-runtime/providerSkills";
|
|
import type { LegendListRef } from "@legendapp/list/react-native";
|
|
import { HeaderHeightContext } from "@react-navigation/elements";
|
|
import type {
|
|
EnvironmentId,
|
|
MessageId,
|
|
ModelSelection,
|
|
ProviderApprovalDecision,
|
|
ProviderInteractionMode,
|
|
RuntimeMode,
|
|
RuntimeRequestId,
|
|
ServerConfig as T3ServerConfig,
|
|
ThreadId,
|
|
UsageLimitsReport,
|
|
} from "@t3tools/contracts";
|
|
import {
|
|
appendCodexArtifactTemplateUsePrompt,
|
|
type CodexArtifactTemplate,
|
|
} from "@t3tools/client-runtime/codex-artifact-templates";
|
|
import type { ThreadUserInputQuestion } from "@t3tools/client-runtime/state/thread-requests";
|
|
import { presentPendingBackgroundWork } from "@t3tools/client-runtime/state/thread-execution";
|
|
import { resolveSubagentPillSegment } from "@t3tools/client-runtime/state/thread-subagents";
|
|
import {
|
|
formatModelSelectionEffort,
|
|
type ProviderSubagentStatus,
|
|
} from "@t3tools/client-runtime/state/thread-execution";
|
|
import { formatModelSlugName, resolveSelectableModel } from "@t3tools/shared/model";
|
|
import { isProviderNativeSubagentThread } from "@t3tools/contracts";
|
|
import type { QueuedRunEdit } from "../../state/queued-run-edit";
|
|
import type { FollowUpBehavior } from "../../lib/followUpBehavior";
|
|
import type { ActiveTurnComposerAction } from "@t3tools/client-runtime/state/composer-dispatch";
|
|
import * as Haptics from "expo-haptics";
|
|
import {
|
|
memo,
|
|
useCallback,
|
|
useContext,
|
|
useEffect,
|
|
useLayoutEffect,
|
|
useMemo,
|
|
useRef,
|
|
useState,
|
|
} from "react";
|
|
import {
|
|
Alert,
|
|
AppState,
|
|
Keyboard,
|
|
Platform,
|
|
useWindowDimensions,
|
|
View,
|
|
type GestureResponderEvent,
|
|
type ViewInstance,
|
|
} from "react-native";
|
|
import {
|
|
KeyboardController,
|
|
KeyboardStickyView,
|
|
useKeyboardState,
|
|
} from "react-native-keyboard-controller";
|
|
import Animated, {
|
|
Easing,
|
|
FadeInDown,
|
|
FadeOut,
|
|
ReduceMotion,
|
|
useAnimatedReaction,
|
|
useAnimatedStyle,
|
|
useDerivedValue,
|
|
useSharedValue,
|
|
withTiming,
|
|
} from "react-native-reanimated";
|
|
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
|
import { useWorkspaceContentWidth } from "../layout/workspace-content-width";
|
|
import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
|
|
import { collectProviderUsageLimits } from "@t3tools/shared/usageLimits";
|
|
import type { ComposerEditorHandle } from "../../components/ComposerEditor";
|
|
import type { StatusTone } from "../../components/StatusPill";
|
|
import type { DraftComposerAttachment } from "../../lib/composerImages";
|
|
import { RenderErrorBoundary, RenderFailureView } from "../../components/RenderErrorBoundary";
|
|
import { CHAT_CONTENT_MAX_WIDTH, type LayoutVariant } from "../../lib/layout";
|
|
import { editPendingThreadMessage } from "../../state/edit-pending-thread-message";
|
|
import { deviceEnvironment } from "../../state/device";
|
|
import { useEnvironmentQuery } from "../../state/query";
|
|
import { threadDevicePreviews } from "../devices/threadDevicePreviews";
|
|
import type { QueuedThreadMessage } from "../../state/thread-outbox-model";
|
|
import { scopedThreadKey } from "../../lib/scopedEntities";
|
|
import {
|
|
clearThreadComposerError,
|
|
threadComposerErrorsAtom,
|
|
} from "../../state/thread-composer-error";
|
|
import { threadEnvironment } from "../../state/threads";
|
|
import { useAtomCommand } from "../../state/use-atom-command";
|
|
import { useDelayedStatus } from "../../lib/useDelayedStatus";
|
|
import type {
|
|
PendingApproval,
|
|
PendingUserInput,
|
|
PendingUserInputDraftAnswer,
|
|
ThreadFeedEntry,
|
|
ThreadFeedLatestRun,
|
|
} from "../../lib/threadActivity";
|
|
import { PendingApprovalCard } from "./PendingApprovalCard";
|
|
import { ComposerErrorNotice } from "./ComposerErrorNotice";
|
|
import { ComposerFeedback } from "./ComposerFeedback";
|
|
import { ComposerUsageLimits } from "./ComposerUsageLimits";
|
|
import { PendingUserInputCard } from "./PendingUserInputCard";
|
|
import { ProviderSubagentBar } from "./ProviderSubagentBar";
|
|
import { ThreadCreationFailedCard } from "./ThreadCreationFailedCard";
|
|
import {
|
|
FLOATING_WORKING_CONTROL_COVERAGE,
|
|
FloatingWorkingControl,
|
|
} from "./floating-working-control";
|
|
import { connectionFloatingStatus, type FloatingWorkingStatus } from "./floating-working-status";
|
|
import {
|
|
derivePendingUserInputMaxHeight,
|
|
ESTIMATED_KEYBOARD_HEIGHT,
|
|
USER_INPUT_TOGGLE_DURATION_MS,
|
|
} from "./pendingUserInputLayout";
|
|
import {
|
|
COMPOSER_COLLAPSED_CHROME,
|
|
COMPOSER_EXPANDED_CHROME,
|
|
COMPOSER_LAYOUT_TRANSITION,
|
|
COMPOSER_TRANSITION_DURATION_MS,
|
|
ThreadComposer,
|
|
} from "./ThreadComposer";
|
|
import { ThreadFeed, type ThreadFeedHistoryControls } from "./ThreadFeed";
|
|
import { useThreadTurnSubagents } from "./ThreadAgentsSheet";
|
|
import { ComposerQueuedEditBanner } from "./ComposerQueuedEdit";
|
|
import { useThreadQueuedCount } from "./ThreadQueueControl";
|
|
import type { ThreadContentPresentation } from "./threadContentPresentation";
|
|
import { resolveThreadFeedSubmissionAnchor } from "./thread-feed-live-follow";
|
|
import { useGlobalVoiceInput } from "../voice-input/VoiceInputProvider";
|
|
|
|
export interface ThreadDetailScreenProps {
|
|
readonly worktreeSetup?: WorktreeSetupCardProps | null;
|
|
readonly setupWorkingStartedAt?: string | null;
|
|
readonly selectedThread: EnvironmentThreadShell;
|
|
readonly contentPresentation: ThreadContentPresentation;
|
|
readonly screenTone: StatusTone;
|
|
readonly connectionError: string | null;
|
|
readonly environmentLabel: string | null;
|
|
readonly feedbackSubmissions: ReadonlyArray<CodexFeedbackSubmission>;
|
|
readonly onDismissFeedback: (id: MessageId) => void;
|
|
readonly selectedThreadFeed: ReadonlyArray<ThreadFeedEntry>;
|
|
readonly activityRun: ThreadFeedLatestRun | null;
|
|
readonly activeWorkStartedAt: string | null;
|
|
/** The live work is a provider-native subagent's runless root turn. */
|
|
readonly runlessWorkActive?: boolean;
|
|
/** Set on a provider-native subagent thread, which shows status instead of a composer. */
|
|
readonly providerSubagentStatus?: ProviderSubagentStatus | null;
|
|
readonly isCompacting: boolean;
|
|
/**
|
|
* The server has not created this thread yet. "preparing" runs while the
|
|
* queued creation is delivered (a worktree may be checking out); "failed"
|
|
* is a rejected creation whose content went back to the project draft.
|
|
*/
|
|
readonly creationState:
|
|
| { readonly kind: "preparing"; readonly preparingWorktree: boolean }
|
|
| { readonly kind: "failed"; readonly reason: string; readonly onEditTask: () => void }
|
|
| null;
|
|
readonly activePendingApproval: PendingApproval | null;
|
|
readonly respondingApprovalId: RuntimeRequestId | null;
|
|
readonly activePendingUserInput: PendingUserInput | null;
|
|
readonly activePendingUserInputDrafts: Record<string, PendingUserInputDraftAnswer>;
|
|
readonly activePendingUserInputAnswers: Record<string, string | ReadonlyArray<string>> | null;
|
|
readonly respondingUserInputId: RuntimeRequestId | null;
|
|
readonly draftMessage: string;
|
|
readonly draftAttachments: ReadonlyArray<DraftComposerAttachment>;
|
|
readonly connectionStateLabel: EnvironmentConnectionPhase;
|
|
/** Message sync status for the selected thread (drives the composer status pill). */
|
|
readonly threadSyncStatus?: EnvironmentThreadStatus;
|
|
/** Progressive history controls for oversized mobile thread opens. */
|
|
readonly historyControls?: ThreadFeedHistoryControls;
|
|
readonly activeThreadBusy: boolean;
|
|
readonly canStopThread: boolean;
|
|
/** Set while a queued message is open in the composer for editing. */
|
|
readonly queuedRunEdit: QueuedRunEdit | null;
|
|
readonly composerDraftKey: string | null;
|
|
readonly followUpBehavior: FollowUpBehavior;
|
|
readonly canSteerActiveTurn: boolean;
|
|
readonly isSavingQueuedEdit: boolean;
|
|
readonly onCancelQueuedRunEdit: () => void;
|
|
readonly onRemoveQueuedEditAttachment: (attachmentId: string) => void;
|
|
readonly environmentId: EnvironmentId;
|
|
readonly projectWorkspaceRoot: string | null;
|
|
readonly threadCwd: string | null;
|
|
readonly selectedThreadQueueCount: number;
|
|
readonly queuedMessages: ReadonlyArray<QueuedThreadMessage>;
|
|
readonly dispatchingMessageId: MessageId | null;
|
|
readonly serverConfig: T3ServerConfig | null;
|
|
readonly layoutVariant?: LayoutVariant;
|
|
readonly usesAutomaticContentInsets?: boolean;
|
|
readonly onHeaderMaterialVisibilityChange?: (visible: boolean) => void;
|
|
readonly onOpenConnectionEditor: () => void;
|
|
readonly onChangeDraftMessage: (value: string) => void;
|
|
readonly onPickDraftMedia: () => Promise<void>;
|
|
readonly onPickDraftFiles: () => Promise<void>;
|
|
readonly onNativePasteImages: (uris: ReadonlyArray<string>) => Promise<void>;
|
|
readonly onNativePasteText: (paste: ComposerTextPaste) => Promise<void>;
|
|
readonly onRemoveDraftImage: (imageId: string) => void;
|
|
readonly onStopThread: () => void;
|
|
readonly onSendMessage: (followUp?: ActiveTurnComposerAction) => Promise<MessageId | null>;
|
|
readonly onReconnectEnvironment: () => void;
|
|
/** Whether the model picker may offer providers other than this thread's. */
|
|
readonly canSwitchThreadProvider: boolean;
|
|
readonly onUpdateThreadModelSelection: (modelSelection: ModelSelection) => void;
|
|
readonly onUpdateThreadRuntimeMode: (runtimeMode: RuntimeMode) => void;
|
|
readonly onUpdateThreadInteractionMode: (interactionMode: ProviderInteractionMode) => void;
|
|
readonly onRespondToApproval: (
|
|
requestId: RuntimeRequestId,
|
|
decision: ProviderApprovalDecision,
|
|
) => Promise<unknown>;
|
|
readonly onSelectUserInputOption: (
|
|
requestId: RuntimeRequestId,
|
|
question: ThreadUserInputQuestion,
|
|
value: string,
|
|
) => void;
|
|
readonly onChangeUserInputCustomAnswer: (
|
|
requestId: RuntimeRequestId,
|
|
questionId: string,
|
|
customAnswer: string,
|
|
) => void;
|
|
readonly onSubmitUserInput: () => Promise<unknown>;
|
|
readonly onDismissUserInput: () => Promise<unknown>;
|
|
readonly showContent?: boolean;
|
|
}
|
|
|
|
function latestStreamingAssistantMessage(
|
|
feed: ReadonlyArray<ThreadFeedEntry>,
|
|
): { readonly id: string; readonly textLength: number } | null {
|
|
for (let index = feed.length - 1; index >= 0; index -= 1) {
|
|
const entry = feed[index];
|
|
if (entry?.type !== "message") {
|
|
continue;
|
|
}
|
|
if (entry.message.role !== "assistant" || !entry.message.streaming) {
|
|
continue;
|
|
}
|
|
return {
|
|
id: entry.message.id,
|
|
textLength: entry.message.text.length,
|
|
};
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
function useStreamingHaptics(threadId: ThreadId, feed: ReadonlyArray<ThreadFeedEntry>) {
|
|
const lastStreamingAssistantRef = useRef<{
|
|
readonly id: string;
|
|
readonly textLength: number;
|
|
} | null>(null);
|
|
const lastStreamHapticAtRef = useRef(0);
|
|
const hydratedRef = useRef(false);
|
|
const previousThreadIdRef = useRef(threadId);
|
|
|
|
useEffect(() => {
|
|
if (previousThreadIdRef.current !== threadId) {
|
|
previousThreadIdRef.current = threadId;
|
|
hydratedRef.current = false;
|
|
}
|
|
|
|
const latestStreamingMessage = latestStreamingAssistantMessage(feed);
|
|
|
|
if (!hydratedRef.current) {
|
|
hydratedRef.current = true;
|
|
lastStreamingAssistantRef.current = latestStreamingMessage;
|
|
return;
|
|
}
|
|
|
|
if (!latestStreamingMessage) {
|
|
lastStreamingAssistantRef.current = null;
|
|
return;
|
|
}
|
|
|
|
const previousStreamingMessage = lastStreamingAssistantRef.current;
|
|
lastStreamingAssistantRef.current = latestStreamingMessage;
|
|
|
|
const isNewStream = previousStreamingMessage?.id !== latestStreamingMessage.id;
|
|
const textGrew =
|
|
previousStreamingMessage?.id === latestStreamingMessage.id &&
|
|
latestStreamingMessage.textLength > previousStreamingMessage.textLength;
|
|
|
|
if (!isNewStream && !textGrew) {
|
|
return;
|
|
}
|
|
|
|
const now = Date.now();
|
|
if (!isNewStream && now - lastStreamHapticAtRef.current < 320) {
|
|
return;
|
|
}
|
|
|
|
lastStreamHapticAtRef.current = now;
|
|
void Haptics.selectionAsync();
|
|
}, [threadId, feed]);
|
|
}
|
|
|
|
const USER_INPUT_TOGGLE_TIMING = {
|
|
duration: USER_INPUT_TOGGLE_DURATION_MS,
|
|
easing: Easing.out(Easing.cubic),
|
|
};
|
|
|
|
export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: ThreadDetailScreenProps) {
|
|
const navigation = useNavigation();
|
|
const { session: voiceInputSession } = useGlobalVoiceInput();
|
|
const reportedModelSelection = useThreadReportedModelSelection({
|
|
environmentId: props.environmentId,
|
|
threadId: props.selectedThread.id,
|
|
});
|
|
const deviceState = useEnvironmentQuery(
|
|
deviceEnvironment.state({ environmentId: props.environmentId, input: {} }),
|
|
);
|
|
const devicePreviews = useMemo(
|
|
() => threadDevicePreviews(deviceState.data, props.selectedThread.id),
|
|
[deviceState.data, props.selectedThread.id],
|
|
);
|
|
const openDevicePreview = useCallback(() => {
|
|
Keyboard.dismiss();
|
|
navigation.navigate("ThreadDevicePreview", {
|
|
environmentId: props.environmentId,
|
|
threadId: props.selectedThread.id,
|
|
});
|
|
}, [navigation, props.environmentId, props.selectedThread.id]);
|
|
const insets = useSafeAreaInsets();
|
|
const isKeyboardVisible = useKeyboardState((state) => state.isVisible);
|
|
const liveKeyboardHeight = useKeyboardState((state) => state.height);
|
|
// Android can swallow the IME hide callbacks when the app is backgrounded
|
|
// mid keyboard-hide (the reported repro: send — which blurs and starts the
|
|
// hide — then Home within a second). The keyboard library's height AND
|
|
// visibility then stay frozen open, so gating the sticky translation on
|
|
// visibility alone still strands the composer after resume. Quarantine the
|
|
// translation on every Android resume instead; any sign of a live keyboard
|
|
// stream — an owned input gaining focus, or any visibility/height movement —
|
|
// lifts it. A healthy resume sees no visual difference (the translation is
|
|
// already zero while the keyboard is closed).
|
|
const [keyboardStateSuspect, setKeyboardStateSuspect] = useState(false);
|
|
useEffect(() => {
|
|
if (Platform.OS !== "android") {
|
|
return;
|
|
}
|
|
const subscription = AppState.addEventListener("change", (state) => {
|
|
if (state === "active") {
|
|
setKeyboardStateSuspect(true);
|
|
}
|
|
});
|
|
return () => {
|
|
subscription.remove();
|
|
};
|
|
}, []);
|
|
useEffect(() => {
|
|
setKeyboardStateSuspect(false);
|
|
}, [isKeyboardVisible, liveKeyboardHeight]);
|
|
const handleOwnedInputFocusChange = useCallback((focused: boolean) => {
|
|
if (focused) {
|
|
setKeyboardStateSuspect(false);
|
|
}
|
|
}, []);
|
|
const windowHeight = useWindowDimensions().height;
|
|
const navigationHeaderHeight = useContext(HeaderHeightContext) || insets.top + 44;
|
|
const agentLabel = `${props.selectedThread.modelSelection.instanceId} agent`;
|
|
const selectedThreadKey = scopedThreadKey(props.environmentId, props.selectedThread.id);
|
|
const composerError = useAtomValue(threadComposerErrorsAtom)[selectedThreadKey]?.message ?? null;
|
|
const queuedCount = useThreadQueuedCount({
|
|
environmentId: props.environmentId,
|
|
threadId: props.selectedThread.id,
|
|
});
|
|
const turnSubagents = useThreadTurnSubagents({
|
|
environmentId: props.environmentId,
|
|
threadId: props.selectedThread.id,
|
|
});
|
|
const agentsSegment = resolveSubagentPillSegment(turnSubagents);
|
|
const composerEditorRef = useRef<ComposerEditorHandle>(null);
|
|
// A provider-native subagent shows status instead of a composer.
|
|
const isProviderSubagent = isProviderNativeSubagentThread(props.selectedThread.source);
|
|
// Entering edit mode from the queue sheet should land in a ready composer,
|
|
// not require a second tap on a composer already holding the message.
|
|
const editingRunId = props.queuedRunEdit?.runId ?? null;
|
|
useEffect(() => {
|
|
if (editingRunId === null) return;
|
|
const frame = requestAnimationFrame(() => composerEditorRef.current?.focus());
|
|
return () => cancelAnimationFrame(frame);
|
|
}, [editingRunId]);
|
|
const draftMessageRef = useRef(props.draftMessage);
|
|
draftMessageRef.current = props.draftMessage;
|
|
const composerOverlayRef = useRef<ViewInstance>(null);
|
|
const listRef = useRef<LegendListRef>(null);
|
|
const feedTouchStartRef = useRef<{ pageX: number; pageY: number } | null>(null);
|
|
const selectedThreadKeyRef = useRef(selectedThreadKey);
|
|
const lastScrolledSubmittedMessageIdRef = useRef<MessageId | null>(null);
|
|
const [composerExpanded, setComposerExpanded] = useState(false);
|
|
const [composerFocused, setComposerFocused] = useState(false);
|
|
const handleComposerFocusChange = useCallback(
|
|
(focused: boolean) => {
|
|
setComposerFocused(focused);
|
|
handleOwnedInputFocusChange(focused);
|
|
},
|
|
[handleOwnedInputFocusChange],
|
|
);
|
|
const [anchorMessageId, setAnchorMessageId] = useState<MessageId | null>(null);
|
|
const [submittedMessageId, setSubmittedMessageId] = useState<MessageId | null>(null);
|
|
const [endFollowEnabled, setEndFollowEnabled] = useState(true);
|
|
// Android keys the safe-area padding on keyboard visibility (#5988): the
|
|
// back gesture closes the keyboard while the editor stays focused, and a
|
|
// focus-keyed inset would leave the toolbar under the gesture bar. iOS must
|
|
// NOT use visibility — it only flips on keyboardDidHide, after the hide
|
|
// animation, so the composer would ride down flush to the screen edge and
|
|
// then snap up into the inset. On iOS blur precedes the hide, so the
|
|
// focus-keyed inset is already in place while the composer rides down.
|
|
// Dictation keeps that focus while the composer switches to its compact pill.
|
|
const composerBottomInset = (
|
|
Platform.OS === "android" ? isKeyboardVisible : composerExpanded || composerFocused
|
|
)
|
|
? 0
|
|
: Math.max(insets.bottom, 12);
|
|
const contentPresentationKind = props.contentPresentation.kind;
|
|
// The raw sync status enters "synchronizing" on every full fetch, cached or
|
|
// not. Whether messages are already on screen decides the pill label: no
|
|
// data yet → "Loading messages", cached data reconciling → "Syncing".
|
|
const realThreadSyncLabel = (() => {
|
|
switch (props.threadSyncStatus) {
|
|
case "empty":
|
|
case "cached":
|
|
case "synchronizing":
|
|
if (contentPresentationKind === "ready") {
|
|
return "Syncing messages...";
|
|
}
|
|
return contentPresentationKind === "loading" ? "Loading messages..." : null;
|
|
default:
|
|
return null;
|
|
}
|
|
})();
|
|
// Opening a running thread resyncs for a few frames. The pill shows the
|
|
// sync label only when the sync lasts, so it does not flash before the timer.
|
|
const threadSyncLabel = useDelayedStatus(selectedThreadKey, realThreadSyncLabel);
|
|
// One floating pill above the composer: it reads the connection phase while
|
|
// disconnected, the sync state while messages load, then the working timer
|
|
// once the feed is settled.
|
|
// The shell's roster is the server's post-settlement view of what still runs.
|
|
const pendingBackgroundWork = presentPendingBackgroundWork(
|
|
props.selectedThread.pendingBackgroundTasks,
|
|
);
|
|
const floatingStatus = ((): FloatingWorkingStatus | null => {
|
|
const connectionStatus = connectionFloatingStatus({
|
|
connectionError: props.connectionError,
|
|
connectionState: props.connectionStateLabel,
|
|
environmentLabel: props.environmentLabel,
|
|
onReconnect: props.onReconnectEnvironment,
|
|
});
|
|
if (connectionStatus !== null) {
|
|
return connectionStatus;
|
|
}
|
|
if (props.activePendingApproval !== null || props.activePendingUserInput !== null) {
|
|
return null;
|
|
}
|
|
if (props.creationState?.kind === "preparing") {
|
|
// The setup header already reports progress in the feed.
|
|
if (props.worktreeSetup) return null;
|
|
return {
|
|
kind: "preparing",
|
|
label: props.creationState.preparingWorktree ? "Setting up worktree…" : "Starting…",
|
|
};
|
|
}
|
|
if (props.creationState?.kind === "failed") {
|
|
return null;
|
|
}
|
|
if (threadSyncLabel !== null) {
|
|
return { kind: "syncing", label: threadSyncLabel };
|
|
}
|
|
if (props.isCompacting && contentPresentationKind === "ready") {
|
|
return { kind: "compacting" };
|
|
}
|
|
if (props.activeWorkStartedAt !== null && contentPresentationKind === "ready") {
|
|
return { kind: "working", startedAt: props.activeWorkStartedAt };
|
|
}
|
|
if (pendingBackgroundWork !== null && contentPresentationKind === "ready") {
|
|
return {
|
|
kind: "background",
|
|
label: pendingBackgroundWork.title,
|
|
accessibilityLabel: `${pendingBackgroundWork.title}: ${pendingBackgroundWork.items
|
|
.map((item) => item.label)
|
|
.join(", ")}`,
|
|
waiting: pendingBackgroundWork.waiting,
|
|
};
|
|
}
|
|
return null;
|
|
})();
|
|
const showWorkingControl = floatingStatus !== null;
|
|
// Connection and working status occupy the same space. Keep the feed inset
|
|
// stable when reconnecting hands off to syncing and then to a running turn.
|
|
const showFloatingStatus =
|
|
showWorkingControl ||
|
|
queuedCount > 0 ||
|
|
agentsSegment !== null ||
|
|
devicePreviews.length > 0 ||
|
|
props.connectionStateLabel !== "connected" ||
|
|
props.queuedMessages.length > 0 ||
|
|
props.selectedThreadFeed.some(
|
|
(entry) => "acknowledged" in entry && entry.acknowledged === true,
|
|
);
|
|
const selectedThreadFeed = props.selectedThreadFeed;
|
|
const hasCompactableConversation =
|
|
selectedThreadFeed.some(
|
|
(entry) =>
|
|
entry.type === "message" &&
|
|
entry.message.role === "user" &&
|
|
((entry.message.attachments?.length ?? 0) > 0 ||
|
|
entry.message.text.trim().toLowerCase() !== "/compact"),
|
|
) ||
|
|
(props.historyControls?.hasMoreHistory === true &&
|
|
props.selectedThread.latestUserMessageAt !== null);
|
|
const composerChrome = composerExpanded ? COMPOSER_EXPANDED_CHROME : COMPOSER_COLLAPSED_CHROME;
|
|
const composerOverlapHeight = composerChrome + composerBottomInset;
|
|
// While a user-input request is pending, the questionnaire owns the
|
|
// composer slot outright: expanded it is the full card, collapsed it is a
|
|
// composer-style bar in the same place (with its own stop control). The
|
|
// composer never mounts into the transition, which keeps the collapse and
|
|
// keyboard animations coherent. Collapse state is keyed by request id so a
|
|
// new request re-expands automatically.
|
|
const [collapsedUserInputRequestId, setCollapsedUserInputRequestId] =
|
|
useState<RuntimeRequestId | null>(null);
|
|
const activeUserInputRequestId = props.activePendingUserInput?.requestId ?? null;
|
|
// The open /usage-limits panel for this thread, model and turn. Only the open
|
|
// moment is stored: the rows read live provider data, so a redeemed reset
|
|
// credit or refreshed probe shows through. Anything that spends quota closes
|
|
// it: a new turn from any source, or the agent resuming after an approval or
|
|
// answered question.
|
|
const [usageLimitsPanel, setUsageLimitsPanel] = useState<{
|
|
readonly key: string;
|
|
readonly threadKey: string;
|
|
readonly now: number;
|
|
} | null>(null);
|
|
// A pending approval or question is part of the key: once it is answered,
|
|
// from this client or any other, the agent resumes and spends quota.
|
|
const usageLimitsKey = [
|
|
selectedThreadKey,
|
|
props.selectedThread.modelSelection.instanceId,
|
|
props.selectedThread.latestRun?.runId ?? "",
|
|
props.activePendingApproval?.requestId ?? props.activePendingUserInput?.requestId ?? "",
|
|
].join(":");
|
|
// Drop the snapshot as soon as the key changes so it cannot resurface stale.
|
|
if (usageLimitsPanel !== null && usageLimitsPanel.key !== usageLimitsKey) {
|
|
setUsageLimitsPanel(null);
|
|
}
|
|
const usageLimitsReport = useMemo(
|
|
() =>
|
|
usageLimitsPanel !== null && usageLimitsPanel.key === usageLimitsKey
|
|
? collectProviderUsageLimits(
|
|
props.selectedThread.modelSelection.instanceId,
|
|
props.serverConfig?.providers ?? [],
|
|
props.serverConfig?.usageLimitSources ?? [],
|
|
usageLimitsPanel.now,
|
|
)
|
|
: null,
|
|
[
|
|
props.selectedThread.modelSelection.instanceId,
|
|
props.serverConfig,
|
|
usageLimitsKey,
|
|
usageLimitsPanel,
|
|
],
|
|
);
|
|
const showUsageLimits = useCallback(
|
|
(report: UsageLimitsReport | null) =>
|
|
setUsageLimitsPanel(
|
|
report === null
|
|
? null
|
|
: {
|
|
key: usageLimitsKey,
|
|
threadKey: selectedThreadKey,
|
|
now: Date.parse(report.createdAt),
|
|
},
|
|
),
|
|
[selectedThreadKey, usageLimitsKey],
|
|
);
|
|
const dismissUsageLimits = useCallback(() => setUsageLimitsPanel(null), []);
|
|
// A send may resolve after navigating away, so only the originating
|
|
// thread's panel is cleared; a panel opened elsewhere in the meantime stays.
|
|
const clearUsageLimitsFor = useCallback(
|
|
(threadKey: string) =>
|
|
setUsageLimitsPanel((current) =>
|
|
current !== null && current.threadKey === threadKey ? null : current,
|
|
),
|
|
[],
|
|
);
|
|
const userInputCollapsed =
|
|
activeUserInputRequestId !== null && collapsedUserInputRequestId === activeUserInputRequestId;
|
|
// The card's height RESERVES keyboard space at all times instead of
|
|
// tracking the keyboard: transforms (the sticky translation) apply
|
|
// same-frame on the UI thread while layout props lag a Yoga pass behind,
|
|
// so any height that follows the keyboard flashes the card over the nav
|
|
// header on the way up. With a constant height the keyboard transition is
|
|
// pure translation — frame-perfect by construction — and the resting card
|
|
// stays compact over the transcript. Before the first open the reserve is
|
|
// an estimate; once a real height is known the card corrects once,
|
|
// discretely.
|
|
const [lastKnownKeyboardHeight, setLastKnownKeyboardHeight] = useState(0);
|
|
useEffect(() => {
|
|
if (liveKeyboardHeight > 0 && liveKeyboardHeight !== lastKnownKeyboardHeight) {
|
|
setLastKnownKeyboardHeight(liveKeyboardHeight);
|
|
}
|
|
}, [lastKnownKeyboardHeight, liveKeyboardHeight]);
|
|
const pendingUserInputMaxHeight = derivePendingUserInputMaxHeight({
|
|
windowHeight,
|
|
keyboardHeight:
|
|
lastKnownKeyboardHeight > 0 ? lastKnownKeyboardHeight : ESTIMATED_KEYBOARD_HEIGHT,
|
|
navigationHeaderHeight,
|
|
// The questionnaire owns the composer slot, so only the composer's
|
|
// bottom inset still overlaps.
|
|
composerOverlapHeight: composerBottomInset,
|
|
});
|
|
const estimatedOverlayHeight = composerOverlapHeight;
|
|
// The overlay's measured height includes the home-indicator inset (the
|
|
// composer pads it), but contentInsetAdjustmentBehavior="automatic" makes
|
|
// UIKit add the safe-area bottom to the content inset AGAIN — leaving a
|
|
// dead strip between the resting content and the composer. Report the
|
|
// overlay height minus the safe area; UIKit adds it back, and ThreadFeed
|
|
// hands LegendList the same delta via contentInsetEndStaticAdjustment so
|
|
// its end-scroll math matches the real resting position.
|
|
const nativeInsetOvercount =
|
|
props.usesAutomaticContentInsets === true && Platform.OS === "ios" ? insets.bottom : 0;
|
|
const { contentInsetEndAdjustment, onComposerLayout } = useKeyboardChatComposerInset(
|
|
listRef,
|
|
composerOverlayRef,
|
|
Math.max(0, estimatedOverlayHeight - nativeInsetOvercount),
|
|
-nativeInsetOvercount,
|
|
Platform.OS === "ios" ? COMPOSER_TRANSITION_DURATION_MS : 0,
|
|
);
|
|
// The expanded questionnaire is an absolute overlay on iOS, so it never
|
|
// changes the measured overlay height (that constancy is what keeps the
|
|
// feed from snapping on collapse/expand). The toggle choreography runs on
|
|
// SHARED VALUES set directly in the tap handler — one JS hop, then the
|
|
// card's rise/sink and the feed's end-inset glide animate in lockstep on
|
|
// the UI thread, keyboard-style, instead of waiting on React mount +
|
|
// onLayout + state round trips. Coverage (how far the card extends above
|
|
// the bar) is measured straight into a shared value by the card's
|
|
// onLayout, with no re-render.
|
|
const userInputCardProgress = useSharedValue(1);
|
|
const userInputInsetProgress = useSharedValue(1);
|
|
const userInputCardCoverage = useSharedValue(0);
|
|
const floatingControlCoverage = useSharedValue(
|
|
showFloatingStatus ? FLOATING_WORKING_CONTROL_COVERAGE : 0,
|
|
);
|
|
useEffect(() => {
|
|
floatingControlCoverage.value = withTiming(
|
|
showFloatingStatus ? FLOATING_WORKING_CONTROL_COVERAGE : 0,
|
|
{ duration: 180, reduceMotion: ReduceMotion.System },
|
|
);
|
|
}, [floatingControlCoverage, showFloatingStatus]);
|
|
// Android renders the expanded card in-flow (it cannot hit-test the iOS
|
|
// overlay outside the bar's bounds), so its measured overlay height already
|
|
// includes the card — the coverage extra is iOS-only.
|
|
const userInputCoverageApplies = Platform.OS === "ios" && activeUserInputRequestId !== null;
|
|
const combinedContentInsetEndAdjustment = useSharedValue(
|
|
Math.max(0, estimatedOverlayHeight - nativeInsetOvercount),
|
|
);
|
|
useAnimatedReaction(
|
|
() =>
|
|
contentInsetEndAdjustment.value +
|
|
floatingControlCoverage.value +
|
|
(userInputCoverageApplies ? userInputInsetProgress.value * userInputCardCoverage.value : 0),
|
|
(value) => {
|
|
combinedContentInsetEndAdjustment.value = value;
|
|
},
|
|
[userInputCoverageApplies],
|
|
);
|
|
// The floating control is anchored to the bar's top edge, so ride it up
|
|
// with the expanded card instead of drawing it over the questions.
|
|
const floatingControlLift = useDerivedValue(
|
|
() =>
|
|
userInputCoverageApplies ? userInputCardProgress.value * userInputCardCoverage.value : 0,
|
|
[userInputCoverageApplies],
|
|
);
|
|
const { freeze, scrollMessageToEnd } = useKeyboardScrollToEnd({ listRef });
|
|
const endFollowEnabledRef = useRef(true);
|
|
endFollowEnabledRef.current = endFollowEnabled;
|
|
const overlayRepinTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
const previousWorkingControlStateRef = useRef({
|
|
threadKey: selectedThreadKey,
|
|
visible: false,
|
|
});
|
|
// The list's own corrections for these inset changes drift on short
|
|
// content (and the error compounds across toggles), so deterministically
|
|
// re-pin the end once a toggle settles: a no-op when the resting position
|
|
// is already right, corrective when it is not. Follow state is re-checked
|
|
// inside the callback — the user may grab the list during the settle
|
|
// window, and yanking them back would override a live gesture.
|
|
const scheduleOverlayRepin = useCallback(
|
|
(delayMs: number) => {
|
|
if (overlayRepinTimerRef.current !== null) {
|
|
clearTimeout(overlayRepinTimerRef.current);
|
|
}
|
|
overlayRepinTimerRef.current = setTimeout(() => {
|
|
overlayRepinTimerRef.current = null;
|
|
if (!endFollowEnabledRef.current) {
|
|
return;
|
|
}
|
|
void scrollMessageToEnd({ animated: false, closeKeyboard: false }).catch(() => {
|
|
freeze.set(false);
|
|
});
|
|
}, delayMs);
|
|
},
|
|
[freeze, scrollMessageToEnd],
|
|
);
|
|
useEffect(
|
|
() => () => {
|
|
if (overlayRepinTimerRef.current !== null) {
|
|
clearTimeout(overlayRepinTimerRef.current);
|
|
}
|
|
},
|
|
[],
|
|
);
|
|
useEffect(() => {
|
|
const previous = previousWorkingControlStateRef.current;
|
|
const threadChanged = previous.threadKey !== selectedThreadKey;
|
|
const visibilityChanged = previous.visible !== showFloatingStatus;
|
|
previousWorkingControlStateRef.current = {
|
|
threadKey: selectedThreadKey,
|
|
visible: showFloatingStatus,
|
|
};
|
|
if ((!threadChanged && !visibilityChanged) || (threadChanged && !showFloatingStatus)) {
|
|
return;
|
|
}
|
|
// LegendList applies the larger inset but does not re-anchor short
|
|
// followed conversations when this floating coverage changes after the
|
|
// initial load. Re-pin after the finite inset transition; the callback
|
|
// checks follow state again so a user who scrolled up stays put.
|
|
scheduleOverlayRepin(230);
|
|
}, [scheduleOverlayRepin, selectedThreadKey, showFloatingStatus]);
|
|
const handleToggleUserInputCollapsed = useCallback(() => {
|
|
if (activeUserInputRequestId === null) {
|
|
return;
|
|
}
|
|
if (userInputCollapsed) {
|
|
// Expanding: card and feed glide start NOW, on the UI thread.
|
|
userInputCardProgress.value = withTiming(1, USER_INPUT_TOGGLE_TIMING);
|
|
userInputInsetProgress.value = withTiming(1, USER_INPUT_TOGGLE_TIMING);
|
|
setCollapsedUserInputRequestId(null);
|
|
scheduleOverlayRepin(USER_INPUT_TOGGLE_DURATION_MS + 50);
|
|
} else {
|
|
// Collapsing hides the custom-answer inputs; release the keyboard with
|
|
// them instead of leaving it up over a dead responder.
|
|
Keyboard.dismiss();
|
|
userInputCardProgress.value = withTiming(0, USER_INPUT_TOGGLE_TIMING);
|
|
// Instant: the sinking card still covers the strip being revealed, and
|
|
// animating the inset downward is what drifted the short-content end
|
|
// anchor.
|
|
userInputInsetProgress.value = 0;
|
|
setCollapsedUserInputRequestId(activeUserInputRequestId);
|
|
scheduleOverlayRepin(60);
|
|
}
|
|
}, [
|
|
activeUserInputRequestId,
|
|
scheduleOverlayRepin,
|
|
userInputCardProgress,
|
|
userInputCollapsed,
|
|
userInputInsetProgress,
|
|
]);
|
|
useEffect(() => {
|
|
// A new request always arrives expanded.
|
|
userInputCardProgress.value = 1;
|
|
userInputInsetProgress.value = 1;
|
|
}, [activeUserInputRequestId, userInputCardProgress, userInputInsetProgress]);
|
|
const showContent = props.showContent ?? true;
|
|
const layoutVariant = props.layoutVariant ?? "compact";
|
|
const isSplitLayout = layoutVariant === "split";
|
|
const contentMaxWidth = isSplitLayout ? CHAT_CONTENT_MAX_WIDTH : undefined;
|
|
const providerSubagentProvider = props.serverConfig?.providers.find(
|
|
(provider) => provider.instanceId === props.selectedThread.modelSelection.instanceId,
|
|
);
|
|
// Providers can report a dated id or alias (claude-haiku-4-5-20251001).
|
|
const providerSubagentModelSlug = providerSubagentProvider
|
|
? resolveSelectableModel(
|
|
providerSubagentProvider.driver,
|
|
props.selectedThread.modelSelection.model,
|
|
providerSubagentProvider.models,
|
|
)
|
|
: null;
|
|
const providerSubagentCatalogModel = providerSubagentProvider?.models.find(
|
|
(model) => model.slug === providerSubagentModelSlug,
|
|
);
|
|
const workspaceContentWidth = useWorkspaceContentWidth();
|
|
// Clearing animated width can retain the unfolded width after Android resumes folded.
|
|
// Assign both layouts explicitly so the dock always follows its current parent.
|
|
const composerWidthStyle = useAnimatedStyle(() =>
|
|
isSplitLayout && workspaceContentWidth !== null
|
|
? { width: workspaceContentWidth.value }
|
|
: { width: "100%" },
|
|
);
|
|
const selectedInstanceId = props.selectedThread.modelSelection.instanceId;
|
|
useStreamingHaptics(props.selectedThread.id, props.selectedThreadFeed);
|
|
const selectedProviderSkills = useMemo(() => {
|
|
const provider = props.serverConfig?.providers.find(
|
|
(candidate) => candidate.instanceId === selectedInstanceId,
|
|
);
|
|
return provider
|
|
? resolveProviderSkillsForCwd(provider, props.threadCwd ?? props.projectWorkspaceRoot)
|
|
: [];
|
|
}, [props.projectWorkspaceRoot, props.serverConfig, props.threadCwd, selectedInstanceId]);
|
|
|
|
useLayoutEffect(() => {
|
|
selectedThreadKeyRef.current = selectedThreadKey;
|
|
// A replaced or unmounted native editor may not emit a blur event.
|
|
setComposerFocused(false);
|
|
}, [selectedThreadKey, showContent]);
|
|
|
|
const visitThread = useAtomCommand(threadEnvironment.visit, { reportFailure: false });
|
|
const lastDispatchedVisitRef = useRef<string | null>(null);
|
|
const lastVisitDispatchRef = useRef({ threadKey: selectedThreadKey, at: 0 });
|
|
const selectedThreadId = props.selectedThread.id;
|
|
const selectedThreadUpdatedAt = props.selectedThread.updatedAt;
|
|
const selectedThreadLastVisitedAt = props.selectedThread.lastVisitedAt;
|
|
const selectedThreadCompletedAt = props.selectedThread.latestRun?.completedAt;
|
|
useEffect(() => {
|
|
// Records the server-side visited watermark while the thread is on
|
|
// screen (mirror of web ChatView), so the "Done" marker clears on every
|
|
// device. Field absent → the server predates visited tracking.
|
|
if (!showContent || selectedThreadLastVisitedAt === undefined) return;
|
|
const threadUpdatedAtMs = Date.parse(selectedThreadUpdatedAt);
|
|
if (Number.isNaN(threadUpdatedAtMs)) return;
|
|
const lastVisitedAtMs = selectedThreadLastVisitedAt
|
|
? Date.parse(selectedThreadLastVisitedAt)
|
|
: NaN;
|
|
if (!Number.isNaN(lastVisitedAtMs) && lastVisitedAtMs >= threadUpdatedAtMs) return;
|
|
// Dedupe per watermark — the effect re-runs before the command echo lands.
|
|
const dispatchKey = `${selectedThreadKey}:${selectedThreadUpdatedAt}`;
|
|
if (lastDispatchedVisitRef.current === dispatchKey) return;
|
|
const dispatch = () => {
|
|
lastDispatchedVisitRef.current = dispatchKey;
|
|
lastVisitDispatchRef.current = { threadKey: selectedThreadKey, at: Date.now() };
|
|
void visitThread({
|
|
environmentId: props.environmentId,
|
|
input: { threadId: selectedThreadId, visitedAt: selectedThreadUpdatedAt },
|
|
});
|
|
};
|
|
// Completion clears unread state immediately; streaming watermarks use the
|
|
// same ten-second trailing throttle as web, keeping the newest update.
|
|
const completedAtMs = selectedThreadCompletedAt ? Date.parse(selectedThreadCompletedAt) : NaN;
|
|
const hasUnseenCompletion =
|
|
!Number.isNaN(completedAtMs) &&
|
|
(Number.isNaN(lastVisitedAtMs) || completedAtMs > lastVisitedAtMs);
|
|
const previous = lastVisitDispatchRef.current;
|
|
const elapsed = Date.now() - previous.at;
|
|
if (previous.threadKey !== selectedThreadKey || hasUnseenCompletion || elapsed >= 10_000) {
|
|
dispatch();
|
|
return;
|
|
}
|
|
const timer = setTimeout(dispatch, 10_000 - elapsed);
|
|
return () => clearTimeout(timer);
|
|
}, [
|
|
props.environmentId,
|
|
selectedThreadId,
|
|
selectedThreadKey,
|
|
selectedThreadLastVisitedAt,
|
|
selectedThreadCompletedAt,
|
|
selectedThreadUpdatedAt,
|
|
showContent,
|
|
visitThread,
|
|
]);
|
|
|
|
useEffect(() => {
|
|
setAnchorMessageId(null);
|
|
setSubmittedMessageId(null);
|
|
lastScrolledSubmittedMessageIdRef.current = null;
|
|
setEndFollowEnabled(true);
|
|
freeze.set(false);
|
|
}, [freeze, selectedThreadKey]);
|
|
|
|
useEffect(() => {
|
|
if (
|
|
submittedMessageId === null ||
|
|
anchorMessageId !== submittedMessageId ||
|
|
lastScrolledSubmittedMessageIdRef.current === submittedMessageId ||
|
|
contentPresentationKind !== "ready" ||
|
|
(!selectedThreadFeed.some(
|
|
(entry) => entry.type === "message" && entry.id === submittedMessageId,
|
|
) &&
|
|
!props.queuedMessages.some((message) => message.messageId === submittedMessageId))
|
|
) {
|
|
return;
|
|
}
|
|
|
|
const targetThreadKey = selectedThreadKey;
|
|
const frame = requestAnimationFrame(() => {
|
|
if (selectedThreadKeyRef.current !== targetThreadKey) {
|
|
return;
|
|
}
|
|
lastScrolledSubmittedMessageIdRef.current = submittedMessageId;
|
|
// Wait for the keyboard dismissal (started by blur() on send) to finish
|
|
// before scrolling: scrollMessageToEnd freezes keyboard-driven inset
|
|
// updates while it runs, and a close event swallowed by that freeze
|
|
// leaves the keyboard padding permanently applied — overshooting the
|
|
// anchor and leaving a phantom bottom inset once the reply streams in.
|
|
void KeyboardController.dismiss()
|
|
.then(() => {
|
|
if (
|
|
selectedThreadKeyRef.current !== targetThreadKey ||
|
|
lastScrolledSubmittedMessageIdRef.current !== submittedMessageId
|
|
) {
|
|
return;
|
|
}
|
|
return scrollMessageToEnd({ animated: true, closeKeyboard: false });
|
|
})
|
|
.catch(() => {
|
|
if (
|
|
selectedThreadKeyRef.current !== targetThreadKey ||
|
|
lastScrolledSubmittedMessageIdRef.current !== submittedMessageId
|
|
) {
|
|
return;
|
|
}
|
|
lastScrolledSubmittedMessageIdRef.current = null;
|
|
freeze.set(false);
|
|
});
|
|
});
|
|
return () => cancelAnimationFrame(frame);
|
|
}, [
|
|
anchorMessageId,
|
|
submittedMessageId,
|
|
freeze,
|
|
contentPresentationKind,
|
|
props.queuedMessages,
|
|
selectedThreadFeed,
|
|
scrollMessageToEnd,
|
|
selectedThreadKey,
|
|
]);
|
|
|
|
const handleSendMessage = useCallback(
|
|
async (followUp?: ActiveTurnComposerAction) => {
|
|
const targetThreadKey = selectedThreadKey;
|
|
const hasUserMessage = selectedThreadFeed.some(
|
|
(entry) => entry.type === "message" && entry.message.role === "user",
|
|
);
|
|
const messageId = await props.onSendMessage(followUp);
|
|
if (messageId === null || selectedThreadKeyRef.current !== targetThreadKey) {
|
|
return messageId;
|
|
}
|
|
|
|
// A sent message makes the snapshot stale; a refused send leaves it in place.
|
|
clearUsageLimitsFor(targetThreadKey);
|
|
|
|
setSubmittedMessageId(messageId);
|
|
setAnchorMessageId(
|
|
resolveThreadFeedSubmissionAnchor({
|
|
currentAnchorMessageId: anchorMessageId,
|
|
submittedMessageId: messageId,
|
|
hasStartedTurn: props.selectedThread.latestRun !== null,
|
|
hasUserMessage,
|
|
queuedMessageCount: props.selectedThreadQueueCount,
|
|
}),
|
|
);
|
|
composerEditorRef.current?.blur();
|
|
return messageId;
|
|
},
|
|
[
|
|
anchorMessageId,
|
|
clearUsageLimitsFor,
|
|
props.onSendMessage,
|
|
props.selectedThread.latestRun,
|
|
props.selectedThreadQueueCount,
|
|
selectedThreadFeed,
|
|
selectedThreadKey,
|
|
],
|
|
);
|
|
|
|
const handleEditPendingMessage = useCallback(async (message: QueuedThreadMessage) => {
|
|
try {
|
|
if (
|
|
(await editPendingThreadMessage(message)) &&
|
|
selectedThreadKeyRef.current === scopedThreadKey(message.environmentId, message.threadId)
|
|
) {
|
|
composerEditorRef.current?.focus();
|
|
}
|
|
} catch (error) {
|
|
Alert.alert(
|
|
"Could not edit message",
|
|
error instanceof Error ? error.message : "Please try again.",
|
|
);
|
|
}
|
|
}, []);
|
|
|
|
const collapseComposer = useCallback(() => {
|
|
composerEditorRef.current?.blur();
|
|
}, []);
|
|
|
|
const handleUseArtifactTemplate = useCallback(
|
|
(template: CodexArtifactTemplate) => {
|
|
const currentDraft = draftMessageRef.current;
|
|
const nextDraft = appendCodexArtifactTemplateUsePrompt(currentDraft, template);
|
|
if (nextDraft !== currentDraft) {
|
|
draftMessageRef.current = nextDraft;
|
|
props.onChangeDraftMessage(nextDraft);
|
|
}
|
|
requestAnimationFrame(() => {
|
|
composerEditorRef.current?.focus();
|
|
composerEditorRef.current?.setSelection({ start: nextDraft.length, end: nextDraft.length });
|
|
});
|
|
},
|
|
[props.onChangeDraftMessage],
|
|
);
|
|
|
|
const handleScrollToEnd = useCallback(() => {
|
|
void Haptics.selectionAsync();
|
|
void scrollMessageToEnd({ animated: true, closeKeyboard: false }).catch(() => {
|
|
freeze.set(false);
|
|
});
|
|
}, [freeze, scrollMessageToEnd]);
|
|
|
|
const showScrollToEndButton = contentPresentationKind === "ready" && !endFollowEnabled;
|
|
const { themeAppearance } = useAppearancePreferences();
|
|
const isDarkMode = themeAppearance === "dark";
|
|
|
|
const handleFeedTouchStart = useCallback((event: GestureResponderEvent) => {
|
|
feedTouchStartRef.current = {
|
|
pageX: event.nativeEvent.pageX,
|
|
pageY: event.nativeEvent.pageY,
|
|
};
|
|
}, []);
|
|
|
|
const handleFeedTouchMove = useCallback((event: GestureResponderEvent) => {
|
|
const start = feedTouchStartRef.current;
|
|
if (!start) {
|
|
return;
|
|
}
|
|
const deltaX = event.nativeEvent.pageX - start.pageX;
|
|
const deltaY = event.nativeEvent.pageY - start.pageY;
|
|
if (Math.hypot(deltaX, deltaY) > 8) {
|
|
feedTouchStartRef.current = null;
|
|
}
|
|
}, []);
|
|
|
|
const handleFeedTouchEnd = useCallback(() => {
|
|
if (feedTouchStartRef.current) {
|
|
collapseComposer();
|
|
}
|
|
feedTouchStartRef.current = null;
|
|
}, [collapseComposer]);
|
|
|
|
const handleFeedTouchCancel = useCallback(() => {
|
|
feedTouchStartRef.current = null;
|
|
}, []);
|
|
|
|
return (
|
|
<View className="flex-1">
|
|
{showContent ? (
|
|
<View
|
|
style={{ flex: 1 }}
|
|
onTouchStart={handleFeedTouchStart}
|
|
onTouchMove={handleFeedTouchMove}
|
|
onTouchEnd={handleFeedTouchEnd}
|
|
onTouchCancel={handleFeedTouchCancel}
|
|
>
|
|
<View
|
|
pointerEvents="none"
|
|
className={
|
|
Platform.OS === "android"
|
|
? "absolute inset-0 bg-thread-canvas"
|
|
: "absolute inset-0 bg-screen"
|
|
}
|
|
/>
|
|
<RenderErrorBoundary
|
|
key={selectedThreadKey}
|
|
resetKeys={[props.threadCwd]}
|
|
renderFallback={(fallback) => (
|
|
<RenderFailureView
|
|
{...fallback}
|
|
title="The conversation couldn't be displayed"
|
|
bottomInset={estimatedOverlayHeight}
|
|
/>
|
|
)}
|
|
>
|
|
<ThreadFeed
|
|
environmentId={props.environmentId}
|
|
threadId={props.selectedThread.id}
|
|
workspaceRoot={props.threadCwd}
|
|
feed={props.selectedThreadFeed}
|
|
worktreeSetup={props.worktreeSetup}
|
|
setupWorkingStartedAt={props.setupWorkingStartedAt}
|
|
queuedMessages={props.queuedMessages}
|
|
dispatchingMessageId={props.dispatchingMessageId}
|
|
// A native subagent has no composer to edit a pending message in;
|
|
// Cancel on the edit banner would discard it.
|
|
onEditPendingMessage={isProviderSubagent ? null : handleEditPendingMessage}
|
|
contentPresentation={props.contentPresentation}
|
|
agentLabel={agentLabel}
|
|
threadTitle={props.selectedThread.title}
|
|
latestRun={props.activityRun}
|
|
activeWorkStartedAt={props.activeWorkStartedAt}
|
|
runlessWorkActive={props.runlessWorkActive ?? false}
|
|
listRef={listRef}
|
|
freeze={freeze}
|
|
anchorMessageId={anchorMessageId}
|
|
submittedMessageId={submittedMessageId}
|
|
contentInsetEndAdjustment={combinedContentInsetEndAdjustment}
|
|
contentTopInset={0}
|
|
contentBottomInset={
|
|
estimatedOverlayHeight +
|
|
(showFloatingStatus ? FLOATING_WORKING_CONTROL_COVERAGE : 0)
|
|
}
|
|
contentMaxWidth={contentMaxWidth}
|
|
historyControls={props.historyControls}
|
|
layoutVariant={layoutVariant}
|
|
usesAutomaticContentInsets={props.usesAutomaticContentInsets}
|
|
onHeaderMaterialVisibilityChange={props.onHeaderMaterialVisibilityChange}
|
|
onEndFollowEnabledChange={setEndFollowEnabled}
|
|
skills={selectedProviderSkills}
|
|
onUseArtifactTemplate={handleUseArtifactTemplate}
|
|
/>
|
|
</RenderErrorBoundary>
|
|
</View>
|
|
) : (
|
|
<View className="flex-1" />
|
|
)}
|
|
|
|
{/* Floating composer — sticks to keyboard via KeyboardStickyView */}
|
|
{showContent ? (
|
|
<KeyboardStickyView
|
|
// iOS emits a native animated height target on both will-show and
|
|
// will-hide, so stay subscribed for the full transition. Android
|
|
// retains its background/resume stale-state quarantine.
|
|
enabled={Platform.OS === "ios" || (isKeyboardVisible && !keyboardStateSuspect)}
|
|
pointerEvents="box-none"
|
|
style={{ position: "absolute", bottom: 0, left: 0, right: 0, top: 0 }}
|
|
offset={{ closed: 0, opened: 0 }}
|
|
>
|
|
{/* The fixed sticky host gives this bottom-anchored child a stable
|
|
coordinate space. Its top and height can then animate together
|
|
instead of the auto-sized host jumping to Yoga's destination. */}
|
|
<Animated.View
|
|
layout={COMPOSER_LAYOUT_TRANSITION}
|
|
pointerEvents="box-none"
|
|
style={[{ position: "absolute", bottom: 0, left: 0 }, composerWidthStyle]}
|
|
>
|
|
{/* No paddingTop here: the overlay's measured height becomes the
|
|
list's bottom inset, so any padding above the pill/composer
|
|
pushes the resting content floor up by the same amount. */}
|
|
<View ref={composerOverlayRef} onLayout={onComposerLayout} className="w-full">
|
|
<FloatingWorkingControl
|
|
colorScheme={isDarkMode ? "dark" : "light"}
|
|
status={floatingStatus}
|
|
lift={floatingControlLift}
|
|
devicePreview={
|
|
devicePreviews.length > 0
|
|
? { count: devicePreviews.length, onPress: openDevicePreview }
|
|
: null
|
|
}
|
|
showScrollToEnd={showScrollToEndButton}
|
|
onScrollToEnd={handleScrollToEnd}
|
|
agents={agentsSegment}
|
|
onOpenAgents={() => {
|
|
Keyboard.dismiss();
|
|
navigation.navigate("ThreadAgents", {
|
|
environmentId: props.environmentId,
|
|
threadId: props.selectedThread.id,
|
|
});
|
|
}}
|
|
queuedCount={queuedCount}
|
|
onOpenQueue={() => {
|
|
Keyboard.dismiss();
|
|
navigation.navigate("ThreadQueue", {
|
|
environmentId: props.environmentId,
|
|
threadId: props.selectedThread.id,
|
|
});
|
|
}}
|
|
/>
|
|
<View className="w-full self-center" style={{ maxWidth: contentMaxWidth }}>
|
|
{props.queuedRunEdit !== null ? (
|
|
<Animated.View
|
|
className="shrink-0"
|
|
entering={FadeInDown.duration(180)}
|
|
exiting={FadeOut.duration(120)}
|
|
>
|
|
<ComposerQueuedEditBanner
|
|
saving={props.isSavingQueuedEdit}
|
|
onCancel={() => {
|
|
voiceInputSession.cancel(props.composerDraftKey);
|
|
props.onCancelQueuedRunEdit();
|
|
}}
|
|
/>
|
|
</Animated.View>
|
|
) : null}
|
|
<UsageLimitRecoveryCard
|
|
key={props.selectedThread.latestRun?.runId}
|
|
thread={props.selectedThread}
|
|
environmentId={props.environmentId}
|
|
/>
|
|
{props.feedbackSubmissions.map((submission) => (
|
|
<ComposerFeedback
|
|
key={submission.id}
|
|
submission={submission}
|
|
onDismiss={() => props.onDismissFeedback(submission.id)}
|
|
/>
|
|
))}
|
|
{composerError !== null ? (
|
|
<Animated.View
|
|
className="shrink-0"
|
|
entering={FadeInDown.duration(180)}
|
|
exiting={FadeOut.duration(120)}
|
|
>
|
|
<ComposerErrorNotice
|
|
message={composerError}
|
|
onDismiss={() => clearThreadComposerError(selectedThreadKey)}
|
|
/>
|
|
</Animated.View>
|
|
) : null}
|
|
{usageLimitsReport && activeUserInputRequestId === null ? (
|
|
<Animated.View
|
|
className="shrink-0 px-4 pb-3"
|
|
entering={FadeInDown.duration(220)}
|
|
exiting={FadeOut.duration(140)}
|
|
>
|
|
<ComposerUsageLimits
|
|
report={usageLimitsReport}
|
|
environmentId={props.environmentId}
|
|
onClose={dismissUsageLimits}
|
|
/>
|
|
</Animated.View>
|
|
) : null}
|
|
{props.creationState?.kind === "failed" ? (
|
|
<Animated.View
|
|
className="shrink-0 px-4"
|
|
style={{ paddingBottom: composerBottomInset }}
|
|
entering={FadeInDown.duration(220)}
|
|
exiting={FadeOut.duration(140)}
|
|
>
|
|
<ThreadCreationFailedCard
|
|
reason={props.creationState.reason}
|
|
onEditTask={props.creationState.onEditTask}
|
|
/>
|
|
</Animated.View>
|
|
) : null}
|
|
{props.activePendingApproval || props.activePendingUserInput ? (
|
|
<Animated.View
|
|
className="shrink-0 gap-3 px-4 pb-3"
|
|
// The questionnaire replaces the composer, so it must pad
|
|
// the home indicator the composer normally covers.
|
|
style={
|
|
activeUserInputRequestId !== null
|
|
? { paddingBottom: composerBottomInset }
|
|
: undefined
|
|
}
|
|
entering={FadeInDown.duration(220)}
|
|
exiting={FadeOut.duration(140)}
|
|
>
|
|
{props.activePendingApproval ? (
|
|
<PendingApprovalCard
|
|
approval={props.activePendingApproval}
|
|
respondingApprovalId={props.respondingApprovalId}
|
|
onRespond={props.onRespondToApproval}
|
|
/>
|
|
) : null}
|
|
{props.activePendingUserInput ? (
|
|
<PendingUserInputCard
|
|
pendingUserInput={props.activePendingUserInput}
|
|
maxHeight={pendingUserInputMaxHeight}
|
|
collapsed={userInputCollapsed}
|
|
onToggleCollapsed={handleToggleUserInputCollapsed}
|
|
onStopThread={props.onStopThread}
|
|
cardProgress={userInputCardProgress}
|
|
cardCoverage={userInputCardCoverage}
|
|
onInputFocusChange={handleOwnedInputFocusChange}
|
|
drafts={props.activePendingUserInputDrafts}
|
|
answers={props.activePendingUserInputAnswers}
|
|
respondingUserInputId={props.respondingUserInputId}
|
|
onSelectOption={props.onSelectUserInputOption}
|
|
onChangeCustomAnswer={props.onChangeUserInputCustomAnswer}
|
|
onSubmit={props.onSubmitUserInput}
|
|
onDismiss={props.onDismissUserInput}
|
|
/>
|
|
) : null}
|
|
</Animated.View>
|
|
) : null}
|
|
</View>
|
|
|
|
{/* Hidden (not unmounted) while a user-input request owns the
|
|
composer slot, so composer drafts and editor state survive.
|
|
A rejected creation has no thread to send to; the failure card
|
|
owns the slot instead. */}
|
|
<View
|
|
style={
|
|
activeUserInputRequestId !== null || props.creationState?.kind === "failed"
|
|
? { display: "none" }
|
|
: undefined
|
|
}
|
|
>
|
|
{isProviderSubagent ? (
|
|
<View
|
|
className="self-center px-3 pt-1.5"
|
|
style={{
|
|
width: "100%",
|
|
maxWidth: contentMaxWidth,
|
|
paddingBottom: composerBottomInset + 6,
|
|
}}
|
|
>
|
|
<ProviderSubagentBar
|
|
provider={providerSubagentProvider ?? null}
|
|
modelLabel={
|
|
providerSubagentCatalogModel?.name ??
|
|
formatModelSlugName(props.selectedThread.modelSelection.model)
|
|
}
|
|
effortLabel={formatModelSelectionEffort(
|
|
props.selectedThread.modelSelection,
|
|
providerSubagentProvider?.models,
|
|
reportedModelSelection,
|
|
)}
|
|
status={props.providerSubagentStatus ?? null}
|
|
onOpenParent={
|
|
props.selectedThread.lineage.parentThreadId === null
|
|
? null
|
|
: () =>
|
|
navigation.navigate("Thread", {
|
|
environmentId: String(props.environmentId),
|
|
threadId: String(props.selectedThread.lineage.parentThreadId),
|
|
})
|
|
}
|
|
/>
|
|
</View>
|
|
) : (
|
|
<>
|
|
<ThreadComposer
|
|
reportedModelSelection={reportedModelSelection}
|
|
editorRef={composerEditorRef}
|
|
draftMessage={props.draftMessage}
|
|
draftAttachments={props.draftAttachments}
|
|
placeholder="Ask the repo agent, or run a command…"
|
|
contentMaxWidth={contentMaxWidth}
|
|
connectionState={props.connectionStateLabel}
|
|
environmentLabel={props.environmentLabel}
|
|
selectedThread={props.selectedThread}
|
|
hasCompactableConversation={hasCompactableConversation && !props.isCompacting}
|
|
serverConfig={props.serverConfig}
|
|
queueCount={props.selectedThreadQueueCount}
|
|
activeThreadBusy={props.activeThreadBusy}
|
|
canStopThread={props.canStopThread}
|
|
environmentId={props.environmentId}
|
|
projectCwd={props.threadCwd ?? props.projectWorkspaceRoot}
|
|
// Follow-ups typed during setup wait in the draft: queueing
|
|
// them against a thread id the server may still reject
|
|
// would strand them in the outbox.
|
|
sendBlockedReason={
|
|
props.creationState?.kind === "preparing" ? "Starting the task…" : null
|
|
}
|
|
draftKey={props.composerDraftKey ?? undefined}
|
|
followUpBehavior={props.followUpBehavior}
|
|
canSteerActiveTurn={props.canSteerActiveTurn}
|
|
queuedEdit={
|
|
props.queuedRunEdit === null
|
|
? null
|
|
: {
|
|
existingAttachments: props.queuedRunEdit.existingAttachments,
|
|
saving: props.isSavingQueuedEdit,
|
|
onRemoveExistingAttachment: props.onRemoveQueuedEditAttachment,
|
|
}
|
|
}
|
|
bottomInset={composerBottomInset}
|
|
onChangeDraftMessage={props.onChangeDraftMessage}
|
|
onPickDraftMedia={props.onPickDraftMedia}
|
|
onPickDraftFiles={props.onPickDraftFiles}
|
|
onNativePasteImages={props.onNativePasteImages}
|
|
onNativePasteText={props.onNativePasteText}
|
|
onRemoveDraftImage={props.onRemoveDraftImage}
|
|
onStopThread={props.onStopThread}
|
|
onSendMessage={handleSendMessage}
|
|
onShowUsageLimits={showUsageLimits}
|
|
canSwitchProvider={props.canSwitchThreadProvider}
|
|
onUpdateModelSelection={props.onUpdateThreadModelSelection}
|
|
onUpdateRuntimeMode={props.onUpdateThreadRuntimeMode}
|
|
onUpdateInteractionMode={props.onUpdateThreadInteractionMode}
|
|
onExpandedChange={setComposerExpanded}
|
|
onEditorFocusChange={handleComposerFocusChange}
|
|
/>
|
|
</>
|
|
)}
|
|
</View>
|
|
</View>
|
|
</Animated.View>
|
|
</KeyboardStickyView>
|
|
) : null}
|
|
</View>
|
|
);
|
|
});
|