@@ -36,9 +36,13 @@ import {
3636 resolveUserMessageFileCards ,
3737 resolveUserMessagePresentation ,
3838} from "@t3tools/client-runtime/user-message" ;
39- import { canForkProjectedAssistantItem } from "@t3tools/client-runtime/state/thread-workflows" ;
39+ import {
40+ canForkProjectedAssistantItem ,
41+ threadForkWorkspaceChoices ,
42+ } from "@t3tools/client-runtime/state/thread-workflows" ;
4043import type { ThreadCheckpointSummary } from "@t3tools/client-runtime/state/thread-checkpoints" ;
4144import { formatDiffCount } from "@t3tools/client-runtime/diff-count" ;
45+ import { forkWorkspacePreparingAtom } from "@t3tools/client-runtime/worktree-setup" ;
4246import type { WorkflowOpening } from "@t3tools/client-runtime/state/subagentRuntime" ;
4347import {
4448 AuthFilesystemReadScope ,
@@ -51,6 +55,7 @@ import {
5155 type EnvironmentId ,
5256 type MessageId ,
5357 type OrchestrationV2ProjectedTurnItem ,
58+ type OrchestrationV2ThreadLaunchWorkspaceStrategy ,
5459 type RunId ,
5560 type ScheduledTaskId ,
5661 type ScopedThreadRef ,
@@ -347,6 +352,7 @@ export interface ThreadFeedProps {
347352 readonly detailRef : ScopedThreadRef | null ;
348353 readonly threadTitle : string ;
349354 readonly workspaceRoot ?: string | null ;
355+ readonly isGitRepo ?: boolean ;
350356 readonly feed : ReadonlyArray < ThreadFeedEntry > ;
351357 readonly pendingHandoff ?: HandoffEndpoints | null ;
352358 readonly contentPresentation : ThreadContentPresentation ;
@@ -420,6 +426,8 @@ function UserMessageCopyButton(props: {
420426
421427function AssistantForkButton ( props : {
422428 readonly environmentId : EnvironmentId ;
429+ readonly threadId : ThreadId ;
430+ readonly isGitRepo : boolean ;
423431 readonly iconColor : ColorValue ;
424432 readonly buttonSize : number ;
425433 readonly iconSize : number ;
@@ -433,6 +441,7 @@ function AssistantForkButton(props: {
433441 } ) ;
434442 const forkFromRun = useAtomCommand ( threadEnvironment . forkFromRun , "fork from response" ) ;
435443 const selectDetail = useSelectWorkspaceDetail ( ) ;
444+ const navigation = useNavigation ( ) ;
436445 const [ busy , setBusy ] = useState ( false ) ;
437446 const canFork = canForkProjectedAssistantItem ( {
438447 projectedItem : props . projectedItem ,
@@ -442,41 +451,93 @@ function AssistantForkButton(props: {
442451
443452 if ( ! canFork || runId === null ) return null ;
444453
454+ const fork = async ( workspaceStrategy : OrchestrationV2ThreadLaunchWorkspaceStrategy ) => {
455+ const targetThreadId = ThreadId . make ( uuidv4 ( ) ) ;
456+ const preparingAtom = forkWorkspacePreparingAtom (
457+ scopedThreadKey ( props . environmentId , targetThreadId ) ,
458+ ) ;
459+ const preparesWorktree = workspaceStrategy . type === "worktree" ;
460+ const stillShowing = ( threadId : ThreadId ) => {
461+ const state = navigation . getState ( ) ;
462+ const route = state ?. routes [ state . index ] ;
463+ const params = route ?. params ;
464+ return (
465+ route ?. name === "Thread" && params && "threadId" in params && params . threadId === threadId
466+ ) ;
467+ } ;
468+ const openFork = ( ) =>
469+ selectDetail ( {
470+ name : "Thread" ,
471+ params : { environmentId : props . environmentId , threadId : targetThreadId } ,
472+ } ) ;
473+ if ( preparesWorktree ) appAtomRegistry . set ( preparingAtom , true ) ;
474+ const pendingResult = forkFromRun ( {
475+ environmentId : props . environmentId ,
476+ input : {
477+ sourceThreadId : props . projectedItem . sourceThreadId ,
478+ targetThreadId,
479+ runId,
480+ title : `${ props . sourceTitle } fork` ,
481+ creationSource : "mobile" ,
482+ workspaceStrategy,
483+ } ,
484+ } ) . finally ( ( ) => appAtomRegistry . set ( preparingAtom , false ) ) ;
485+ // The request settles once a new worktree is ready. Open the fork as soon as
486+ // its thread exists so it shows setup progress meanwhile.
487+ const openedEarly =
488+ preparesWorktree &&
489+ ( await Promise . race ( [
490+ waitForThreadShell ( props . environmentId , targetThreadId ) ,
491+ pendingResult . then ( ( ) => false ) ,
492+ ] ) ) ;
493+ const navigatedEarly = openedEarly && stillShowing ( props . threadId ) ;
494+ if ( navigatedEarly ) openFork ( ) ;
495+ const result = await pendingResult ;
496+ if ( result . _tag !== "Success" ) {
497+ if ( navigatedEarly && stillShowing ( targetThreadId ) )
498+ selectDetail ( {
499+ name : "Thread" ,
500+ params : { environmentId : props . environmentId , threadId : props . threadId } ,
501+ } ) ;
502+ return ;
503+ }
504+ if ( navigatedEarly || ! stillShowing ( props . threadId ) ) return ;
505+ const targetThreadReady = await waitForThreadShell ( props . environmentId , targetThreadId ) ;
506+ if ( ! stillShowing ( props . threadId ) ) return ;
507+ if ( ! targetThreadReady ) {
508+ Dialog . alert (
509+ "Fork created" ,
510+ "Its thread data did not reach this client. Reconnect and try opening it from the thread list." ,
511+ ) ;
512+ return ;
513+ }
514+ openFork ( ) ;
515+ } ;
516+
445517 return (
446518 < Pressable
447519 accessibilityRole = "button"
448520 accessibilityLabel = "Fork from this response"
449521 disabled = { busy }
450522 onPress = { ( ) => {
451- const targetThreadId = ThreadId . make ( uuidv4 ( ) ) ;
452- setBusy ( true ) ;
523+ const thread = appAtomRegistry . get (
524+ environmentThreadShells . threadShellAtom (
525+ scopeThreadRef ( props . environmentId , props . threadId ) ,
526+ ) ,
527+ ) ;
528+ if ( ! thread ) return ;
453529 void Haptics . selectionAsync ( ) ;
454- void forkFromRun ( {
455- environmentId : props . environmentId ,
456- input : {
457- sourceThreadId : props . projectedItem . sourceThreadId ,
458- targetThreadId,
459- runId,
460- title : `${ props . sourceTitle } fork` ,
461- creationSource : "mobile" ,
462- } ,
463- } )
464- . then ( async ( result ) => {
465- if ( result . _tag !== "Success" ) return ;
466- const targetThreadReady = await waitForThreadShell ( props . environmentId , targetThreadId ) ;
467- if ( ! targetThreadReady ) {
468- Dialog . alert (
469- "Fork created" ,
470- "Its thread data did not reach this client. Reconnect and try opening it from the thread list." ,
471- ) ;
472- return ;
473- }
474- selectDetail ( {
475- name : "Thread" ,
476- params : { environmentId : props . environmentId , threadId : targetThreadId } ,
477- } ) ;
478- } )
479- . finally ( ( ) => setBusy ( false ) ) ;
530+ const choices = threadForkWorkspaceChoices ( { ...thread , isGitRepo : props . isGitRepo } ) ;
531+ Dialog . alert ( "Fork thread from here" , "Choose where to continue from this response." , [
532+ ...choices . map ( ( choice ) => ( {
533+ text : choice . label ,
534+ onPress : ( ) => {
535+ setBusy ( true ) ;
536+ void fork ( choice . workspaceStrategy ) . finally ( ( ) => setBusy ( false ) ) ;
537+ } ,
538+ } ) ) ,
539+ { text : "Cancel" , style : "cancel" } ,
540+ ] ) ;
480541 } }
481542 className = "h-7 w-7 items-center justify-center disabled:opacity-40"
482543 style = {
@@ -1584,6 +1645,7 @@ function renderFeedEntry(
15841645 | "onEditPendingMessage"
15851646 | "threadId"
15861647 | "workspaceRoot"
1648+ | "isGitRepo"
15871649 > & {
15881650 readonly copiedRowId : string | null ;
15891651 readonly expandedWorkRows : Record < string , boolean > ;
@@ -2210,6 +2272,8 @@ function renderFeedEntry(
22102272 { message . projectedItem ? (
22112273 < AssistantForkButton
22122274 environmentId = { props . environmentId }
2275+ threadId = { props . threadId }
2276+ isGitRepo = { props . isGitRepo ?? false }
22132277 iconColor = { iconSubtleColor }
22142278 buttonSize = { props . replyActionSize . button }
22152279 iconSize = { props . replyActionSize . icon }
@@ -3554,6 +3618,7 @@ export const ThreadFeed = memo(function ThreadFeed({
35543618 threadTitle : props . threadTitle ,
35553619 skills : props . skills ,
35563620 workspaceRoot : props . workspaceRoot ,
3621+ isGitRepo : props . isGitRepo ,
35573622 workflowOpening,
35583623 } ) }
35593624 { props . worktreeSetup && info . index === setupAnchorIndex ? (
0 commit comments