@@ -12,8 +12,13 @@ import {
1212 requestOlderThreadTurns ,
1313 threadHasOlderTurns ,
1414} from "@t3tools/client-runtime/state/threads" ;
15+ import { findThreadHandoffs } from "@t3tools/client-runtime/state/thread-handoff" ;
16+ import {
17+ isAtomCommandInterrupted ,
18+ squashAtomCommandFailure ,
19+ } from "@t3tools/client-runtime/state/runtime" ;
1520import { projectScriptCwd , projectScriptRuntimeEnv } from "@t3tools/shared/projectScripts" ;
16- import { Platform , ScrollView , View } from "react-native" ;
21+ import { Alert , Platform , ScrollView , View } from "react-native" ;
1722import { useSafeAreaInsets } from "react-native-safe-area-context" ;
1823import { useWorkspaceState } from "../../state/workspace" ;
1924import { useEnvironmentQuery } from "../../state/query" ;
@@ -27,6 +32,7 @@ import {
2732} from "../../components/AndroidScreenHeader" ;
2833import { LoadingScreen } from "../../components/LoadingScreen" ;
2934import { scopedThreadKey } from "../../lib/scopedEntities" ;
35+ import { uuidv4 } from "../../lib/uuid" ;
3036import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass" ;
3137import { connectionTone } from "../connection/connectionTone" ;
3238
@@ -62,6 +68,7 @@ import { useSelectedThreadGitState } from "../../state/use-selected-thread-git-s
6268import { useSelectedThreadRequests } from "../../state/use-selected-thread-requests" ;
6369import { useSelectedThreadWorktree } from "../../state/use-selected-thread-worktree" ;
6470import { useThreadComposerState } from "../../state/use-thread-composer-state" ;
71+ import { seedComposerDraftHandoffPrompt } from "../../state/use-composer-drafts" ;
6572import { threadEnvironment } from "../../state/threads" ;
6673import { projectThreadContentPresentation } from "./threadContentPresentation" ;
6774import {
@@ -214,7 +221,14 @@ function ThreadRouteContent(
214221 const gitActions = useSelectedThreadGitActions ( ) ;
215222 const requests = useSelectedThreadRequests ( ) ;
216223 const interruptThreadTurn = useAtomCommand ( threadEnvironment . interruptTurn , "thread interrupt" ) ;
224+ const acceptThreadHandoff = useAtomCommand ( threadEnvironment . acceptHandoff , {
225+ reportFailure : false ,
226+ } ) ;
227+ const dismissThreadHandoff = useAtomCommand ( threadEnvironment . dismissHandoff , {
228+ reportFailure : false ,
229+ } ) ;
217230 const navigation = useNavigation ( ) ;
231+ const [ handoffActionId , setHandoffActionId ] = useState < string | null > ( null ) ;
218232 const params = props . route . params ;
219233 const environmentIdRaw = firstRouteParam ( params . environmentId ) ;
220234 const environmentId = environmentIdRaw ? EnvironmentId . make ( environmentIdRaw ) : null ;
@@ -295,6 +309,15 @@ function ThreadRouteContent(
295309 : null ,
296310 [ composer . interactionMode , composer . modelSelection , composer . runtimeMode , selectedThread ] ,
297311 ) ;
312+ const availableThreadHandoffs = useMemo (
313+ ( ) =>
314+ selectedThreadDetail
315+ ? findThreadHandoffs ( selectedThreadDetail ) . filter (
316+ ( handoff ) => handoff . state === "available" ,
317+ )
318+ : [ ] ,
319+ [ selectedThreadDetail ] ,
320+ ) ;
298321
299322 /* ─── Native header theming ──────────────────────────────────────── */
300323 const usesNativeHeaderGlass = NATIVE_LIQUID_GLASS_SUPPORTED ;
@@ -496,6 +519,69 @@ function ThreadRouteContent(
496519 } ,
497520 } ) ;
498521 } , [ interruptThreadTurn , selectedThread ] ) ;
522+ const handleOpenHandoff = useCallback (
523+ async ( handoff : ReturnType < typeof findThreadHandoffs > [ number ] ) => {
524+ if ( ! selectedThread || handoffActionId !== null ) {
525+ return ;
526+ }
527+ const targetThreadId = ThreadId . make ( uuidv4 ( ) ) ;
528+ setHandoffActionId ( handoff . handoffId ) ;
529+ const result = await acceptThreadHandoff ( {
530+ environmentId : selectedThread . environmentId ,
531+ input : {
532+ threadId : selectedThread . id ,
533+ handoffId : handoff . handoffId ,
534+ targetThreadId,
535+ } ,
536+ } ) ;
537+ if ( result . _tag === "Failure" ) {
538+ if ( ! isAtomCommandInterrupted ( result ) ) {
539+ const error = squashAtomCommandFailure ( result ) ;
540+ Alert . alert (
541+ "Could not open handoff thread" ,
542+ error instanceof Error ? error . message : "Please try again." ,
543+ ) ;
544+ }
545+ setHandoffActionId ( null ) ;
546+ return ;
547+ }
548+ seedComposerDraftHandoffPrompt (
549+ scopedThreadKey ( selectedThread . environmentId , targetThreadId ) ,
550+ handoff . handoffId ,
551+ handoff . prompt ,
552+ ) ;
553+ navigation . navigate ( "Thread" , {
554+ environmentId : String ( selectedThread . environmentId ) ,
555+ threadId : String ( targetThreadId ) ,
556+ } ) ;
557+ setHandoffActionId ( null ) ;
558+ } ,
559+ [ acceptThreadHandoff , handoffActionId , navigation , selectedThread ] ,
560+ ) ;
561+ const handleDismissHandoff = useCallback (
562+ async ( handoff : ReturnType < typeof findThreadHandoffs > [ number ] ) => {
563+ if ( ! selectedThread || handoffActionId !== null ) {
564+ return ;
565+ }
566+ setHandoffActionId ( handoff . handoffId ) ;
567+ const result = await dismissThreadHandoff ( {
568+ environmentId : selectedThread . environmentId ,
569+ input : {
570+ threadId : selectedThread . id ,
571+ handoffId : handoff . handoffId ,
572+ } ,
573+ } ) ;
574+ if ( result . _tag === "Failure" && ! isAtomCommandInterrupted ( result ) ) {
575+ const error = squashAtomCommandFailure ( result ) ;
576+ Alert . alert (
577+ "Could not dismiss handoff" ,
578+ error instanceof Error ? error . message : "Please try again." ,
579+ ) ;
580+ }
581+ setHandoffActionId ( null ) ;
582+ } ,
583+ [ dismissThreadHandoff , handoffActionId , selectedThread ] ,
584+ ) ;
499585
500586 const handleOpenTerminal = useCallback (
501587 ( nextTerminalId ?: string | null ) => {
@@ -808,6 +894,10 @@ function ThreadRouteContent(
808894 onSelectUserInputOption = { requests . onSelectUserInputOption }
809895 onChangeUserInputCustomAnswer = { requests . onChangeUserInputCustomAnswer }
810896 onSubmitUserInput = { requests . onSubmitUserInput }
897+ availableHandoffs = { availableThreadHandoffs }
898+ handoffActionId = { handoffActionId }
899+ onOpenHandoff = { handleOpenHandoff }
900+ onDismissHandoff = { handleDismissHandoff }
811901 />
812902 </ View >
813903 </ >
0 commit comments