TypeScript
fix(mobile): new threads no longer get stuck on Starting (#15870)
nekohasekaid09a9eadf45b
diff --git a/apps/mobile/src/features/threads/ThreadAgentsSheet.tsx b/apps/mobile/src/features/threads/ThreadAgentsSheet.tsxindex 58a8931b84e..6547a4198ee 100644--- a/apps/mobile/src/features/threads/ThreadAgentsSheet.tsx+++ b/apps/mobile/src/features/threads/ThreadAgentsSheet.tsx@@ -9,6 +9,7 @@ import {import { deriveSubagentElapsedMs, formatDuration } from "@t3tools/shared/orchestrationTiming";import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native";import * as DateTime from "effect/DateTime";+import { Atom } from "effect/reactivity";import * as Haptics from "expo-haptics";import { useEffect, useState } from "react";import { Platform, Pressable, ScrollView, View } from "react-native";@@ -28,8 +29,16 @@ const HEADER_SCROLL_EDGE_EFFECTS = nativeHeaderScrollEdgeEffects(Platform.OS, Pltype AgentsTarget = { readonly environmentId: EnvironmentId; readonly threadId: ThreadId };-export function useThreadTurnSubagents(target: AgentsTarget): ThreadTurnSubagents | null {- return useAtomValue(environmentThreadDetails.turnSubagentsAtom(target));+const EMPTY_TURN_SUBAGENTS_ATOM = Atom.make<ThreadTurnSubagents | null>(null).pipe(+ Atom.withLabel("mobile-thread-turn-subagents:empty"),+);++export function useThreadTurnSubagents(target: AgentsTarget | null): ThreadTurnSubagents | null {+ return useAtomValue(+ target === null+ ? EMPTY_TURN_SUBAGENTS_ATOM+ : environmentThreadDetails.turnSubagentsAtom(target),+ );}export function ThreadAgentsSheet({ route }: StaticScreenProps<AgentsTarget>) {diff --git a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx b/apps/mobile/src/features/threads/ThreadDetailScreen.tsxindex 1707debb95e..97663da1ba9 100644--- a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx+++ b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx@@ -26,6 +26,7 @@ import type {ProviderInteractionMode,RuntimeMode,RuntimeRequestId,+ ScopedThreadRef,ServerConfig as T3ServerConfig,ThreadId,UsageLimitsReport,@@ -157,6 +158,11 @@ export interface ThreadDetailScreenProps {readonly setupWorkingStartedAt?: string | null;readonly canOperateThread: boolean;readonly selectedThread: EnvironmentThreadShell;+ /**+ * Read thread details through this, not `selectedThread.id`: it stays null until+ * a queued creation reaches the server; see `useThreadSelection`.+ */+ readonly selectedThreadDetailRef: ScopedThreadRef | null;readonly contentPresentation: ThreadContentPresentation;readonly screenTone: StatusTone;readonly connectionError: string | null;@@ -333,10 +339,7 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Threadconst usesNativeWorkspaceColumns = useNativeWorkspaceColumnsSupported();const navigation = useNavigation();const { session: voiceInputSession } = useGlobalVoiceInput();- const reportedModelSelection = useThreadReportedModelSelection({- environmentId: props.environmentId,- threadId: props.selectedThread.id,- });+ const reportedModelSelection = useThreadReportedModelSelection(props.selectedThreadDetailRef);const deviceState = useEnvironmentQuery(deviceEnvironment.state({ environmentId: props.environmentId, input: {} }),);@@ -414,14 +417,8 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: ThreadthreadId: 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 queuedCount = useThreadQueuedCount(props.selectedThreadDetailRef);+ const turnSubagents = useThreadTurnSubagents(props.selectedThreadDetailRef);const agentsSegment = resolveSubagentPillSegment(turnSubagents);const composerEditorRef = useRef<ComposerEditorHandle>(null);// A provider-native subagent shows status instead of a composer.diff --git a/apps/mobile/src/features/threads/ThreadQueueControl.tsx b/apps/mobile/src/features/threads/ThreadQueueControl.tsxindex e8d81a20a8b..441b1bfe3c6 100644--- a/apps/mobile/src/features/threads/ThreadQueueControl.tsx+++ b/apps/mobile/src/features/threads/ThreadQueueControl.tsx@@ -1,6 +1,7 @@import { type StaticScreenProps, useNavigation } from "@react-navigation/native";import { useAtomValue } from "@effect/atom-react";import type { ChatAttachment, EnvironmentId, RunId, ThreadId } from "@t3tools/contracts";+import { Atom } from "effect/reactivity";import { Image } from "expo-image";import * as Haptics from "expo-haptics";import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";@@ -40,6 +41,9 @@ import { threadDragGapOffset } from "./threadDragGap";const HEADER_SCROLL_EDGE_EFFECTS = nativeHeaderScrollEdgeEffects(Platform.OS, Platform.Version);const REMOVE_ACTION_WIDTH = 76;const THUMBNAIL_LIMIT = 3;+const EMPTY_QUEUED_COUNT_ATOM = Atom.make(0).pipe(+ Atom.withLabel("mobile-thread-queue-count:empty"),+);type QueueTarget = { readonly environmentId: EnvironmentId; readonly threadId: ThreadId };type QueueAction = "steer" | "edit" | "up" | "down" | "remove";@@ -49,8 +53,10 @@ export function useThreadQueueWorkflow(target: QueueTarget) {return useAtomValue(environmentThreadDetails.queueWorkflowAtom(target));}-export function useThreadQueuedCount(target: QueueTarget) {- return useAtomValue(environmentThreadDetails.queuedCountAtom(target));+export function useThreadQueuedCount(target: QueueTarget | null) {+ return useAtomValue(+ target === null ? EMPTY_QUEUED_COUNT_ATOM : environmentThreadDetails.queuedCountAtom(target),+ );}export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {diff --git a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx b/apps/mobile/src/features/threads/ThreadRouteScreen.tsxindex 348f7377724..001dbbd2b0e 100644--- a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx+++ b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx@@ -274,6 +274,7 @@ function ThreadRouteContent(const {selectedThread,selectedThreadCreation,+ selectedThreadDetailRef,selectedThreadProject,selectedEnvironmentConnection,} = useThreadSelection();@@ -988,6 +989,7 @@ function ThreadRouteContent(<ThreadDetailScreencanOperateThread={canOperateThread}selectedThread={selectedThreadWithDraftSettings ?? selectedThread}+ selectedThreadDetailRef={selectedThreadDetailRef}contentPresentation={contentPresentation}screenTone={connectionTone(routeConnectionState)}connectionError={routeConnectionError}diff --git a/apps/mobile/src/state/entities.ts b/apps/mobile/src/state/entities.tsindex b8e42b0cb99..31b19d8574a 100644--- a/apps/mobile/src/state/entities.ts+++ b/apps/mobile/src/state/entities.ts@@ -31,6 +31,9 @@ const EMPTY_THREAD_SHELL_ATOM = Atom.make<EnvironmentThreadShell | null>(null).pconst EMPTY_SERVER_CONFIG_ATOM = Atom.make<ServerConfig | null>(null).pipe(Atom.withLabel("mobile-server-config:empty"),);+const EMPTY_THREAD_ATOM = Atom.make<EnvironmentThread | null>(null).pipe(+ Atom.withLabel("mobile-thread:empty"),+);const EMPTY_THREAD_TITLES: ReadonlyMap<ThreadId, string> = new Map();const EMPTY_THREAD_TITLES_ATOM = Atom.make(EMPTY_THREAD_TITLES).pipe(Atom.withLabel("mobile-thread-titles:empty"),@@ -133,6 +136,9 @@ export function useServerConfigs(): ReadonlyMap<EnvironmentId, ServerConfig> {const selectReportedModelSelection = (thread: EnvironmentThread | null) =>thread === null ? null : deriveReportedModelSelection(thread.projection);-export function useThreadReportedModelSelection(ref: ScopedThreadRef) {- return useAtomValue(environmentThreadDetails.threadAtom(ref), selectReportedModelSelection);+export function useThreadReportedModelSelection(ref: ScopedThreadRef | null) {+ return useAtomValue(+ ref === null ? EMPTY_THREAD_ATOM : environmentThreadDetails.threadAtom(ref),+ selectReportedModelSelection,+ );}diff --git a/apps/mobile/src/state/use-thread-composer-state.ts b/apps/mobile/src/state/use-thread-composer-state.tsindex 623eff1b0a5..10ef09b7b77 100644--- a/apps/mobile/src/state/use-thread-composer-state.ts+++ b/apps/mobile/src/state/use-thread-composer-state.ts@@ -184,6 +184,7 @@ export function useThreadComposerState() {const {selectedThread: selectedThreadShell,selectedThreadCreation,+ selectedThreadDetailRef,selectedEnvironmentRuntime,} = useThreadSelection();const selectedThreadProjection = useSelectedThreadProjection();@@ -311,12 +312,9 @@ export function useThreadComposerState() {// Steering needs a live provider turn the adapter can interrupt; the queue// workflow already derives that from the session's capabilities.const queueWorkflow = useAtomValue(- selectedThreadShell === null+ selectedThreadDetailRef === null? EMPTY_QUEUE_WORKFLOW_ATOM- : environmentThreadDetails.queueWorkflowAtom({- environmentId: selectedThreadShell.environmentId,- threadId: selectedThreadShell.id,- }),+ : environmentThreadDetails.queueWorkflowAtom(selectedThreadDetailRef),);const canSteerActiveTurn = queueWorkflow?.canPromoteToSteer === true;const queuedRunEdit = useQueuedRunEdit(selectedThreadKey);diff --git a/apps/mobile/src/state/use-thread-selection.ts b/apps/mobile/src/state/use-thread-selection.tsindex 3624caebb07..51b88c748e4 100644--- a/apps/mobile/src/state/use-thread-selection.ts+++ b/apps/mobile/src/state/use-thread-selection.ts@@ -158,7 +158,8 @@ function useResolvedThreadSelection(params: ThreadSelectionRouteParams | undefinreturn message === null ? null : { message, outcome };}, [creationOutcome, queuedMessagesByThreadKey, selectedThreadKey]);// Until the creation is delivered the server has no thread to subscribe- // to; subscribing anyway would retry "not found" for the whole setup.+ // to. Every detail reader must use this ref, not the route id: an earlier+ // read gets a definitive not-found, which the thread state keeps as deleted.const selectedThreadDetailRef =selectedThreadShell !== null ||pendingCreation === null ||