@@ -4,7 +4,6 @@ import { ThreadMarkdownVideo, ThreadMediaVisibleContext } from "./ThreadMarkdown
44import { useMarkdownImageSource } from "../../native/useMarkdownImageSource" ;
55import { enrichedContextLinkPresentation } from "../../lib/enrichedLinkPresentation" ;
66import { ThreadContextDivider } from "./thread-context-divider" ;
7- import { ThreadForkSheet } from "./ThreadForkSheet" ;
87import { ThreadHandoffDivider , ThreadHandoffRow } from "./thread-handoff-row" ;
98import { SecretRequestCard } from "./SecretRequestCard" ;
109import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator" ;
@@ -18,9 +17,12 @@ import { KeyboardAwareLegendList } from "@legendapp/list/keyboard";
1817import { useViewabilityAmount , type LegendListRef } from "@legendapp/list/react-native" ;
1918import { scopeThreadRef } from "@t3tools/client-runtime/environment" ;
2019import type { HandoffEndpoints } from "@t3tools/client-runtime/handoff" ;
21- import { forkWorkspacePreparingAtom } from "@t3tools/client-runtime/worktree-setup" ;
2220import { resolveUserMessagePresentation } from "@t3tools/client-runtime/user-message" ;
23- import { canForkProjectedAssistantItem } from "@t3tools/client-runtime/state/thread-workflows" ;
21+ import {
22+ canForkProjectedAssistantItem ,
23+ threadForkWorkspaceChoices ,
24+ } from "@t3tools/client-runtime/state/thread-workflows" ;
25+ import { forkWorkspacePreparingAtom } from "@t3tools/client-runtime/worktree-setup" ;
2426import type { WorkflowOpening } from "@t3tools/client-runtime/state/subagentRuntime" ;
2527import {
2628 type OrchestrationMessageContext ,
@@ -31,6 +33,7 @@ import {
3133 type EnvironmentId ,
3234 type MessageId ,
3335 type OrchestrationV2ProjectedTurnItem ,
36+ type OrchestrationV2ThreadLaunchWorkspaceStrategy ,
3437 type RunId ,
3538} from "@t3tools/contracts" ;
3639import { renderAssistantCitationsAsText } from "@t3tools/shared/assistantCitations" ;
@@ -292,6 +295,7 @@ export interface ThreadFeedProps {
292295 readonly threadId : ThreadId ;
293296 readonly threadTitle : string ;
294297 readonly workspaceRoot ?: string | null ;
298+ readonly isGitRepo ?: boolean ;
295299 readonly feed : ReadonlyArray < ThreadFeedEntry > ;
296300 readonly pendingHandoff ?: HandoffEndpoints | null ;
297301 readonly contentPresentation : ThreadContentPresentation ;
@@ -364,7 +368,7 @@ function UserMessageCopyButton(props: {
364368function AssistantForkButton ( props : {
365369 readonly environmentId : EnvironmentId ;
366370 readonly threadId : ThreadId ;
367- readonly workspaceRoot : string | null ;
371+ readonly isGitRepo : boolean ;
368372 readonly iconColor : ColorValue ;
369373 readonly projectedItem : OrchestrationV2ProjectedTurnItem ;
370374 readonly sourceTitle : string ;
@@ -377,7 +381,6 @@ function AssistantForkButton(props: {
377381 const forkFromRun = useAtomCommand ( threadEnvironment . forkFromRun , "fork from response" ) ;
378382 const navigation = useNavigation ( ) ;
379383 const [ busy , setBusy ] = useState ( false ) ;
380- const [ choosingWorkspace , setChoosingWorkspace ] = useState ( false ) ;
381384 const canFork = canForkProjectedAssistantItem ( {
382385 projectedItem : props . projectedItem ,
383386 capabilities : support . providerSession ?. capabilities ,
@@ -386,112 +389,101 @@ function AssistantForkButton(props: {
386389
387390 if ( ! canFork || runId === null ) return null ;
388391
392+ const fork = async ( workspaceStrategy : OrchestrationV2ThreadLaunchWorkspaceStrategy ) => {
393+ const targetThreadId = ThreadId . make ( uuidv4 ( ) ) ;
394+ const preparingAtom = forkWorkspacePreparingAtom (
395+ scopedThreadKey ( props . environmentId , targetThreadId ) ,
396+ ) ;
397+ const preparesWorktree = workspaceStrategy . type === "worktree" ;
398+ const openFork = ( ) =>
399+ navigation . navigate ( "Thread" , {
400+ environmentId : props . environmentId ,
401+ threadId : targetThreadId ,
402+ } ) ;
403+ if ( preparesWorktree ) appAtomRegistry . set ( preparingAtom , true ) ;
404+ const pendingResult = forkFromRun ( {
405+ environmentId : props . environmentId ,
406+ input : {
407+ sourceThreadId : props . projectedItem . sourceThreadId ,
408+ targetThreadId,
409+ runId,
410+ title : `${ props . sourceTitle } fork` ,
411+ creationSource : "mobile" ,
412+ workspaceStrategy,
413+ } ,
414+ } ) . finally ( ( ) => appAtomRegistry . set ( preparingAtom , false ) ) ;
415+ // The request settles once a new worktree is ready. Open the fork as soon as
416+ // its thread exists so it shows setup progress meanwhile.
417+ const openedEarly =
418+ preparesWorktree &&
419+ ( await Promise . race ( [
420+ waitForThreadShell ( props . environmentId , targetThreadId ) ,
421+ pendingResult . then ( ( ) => false ) ,
422+ ] ) ) ;
423+ if ( openedEarly ) openFork ( ) ;
424+ const result = await pendingResult ;
425+ if ( result . _tag !== "Success" ) {
426+ // The server deletes a fork whose worktree could not be prepared.
427+ const state = navigation . getState ( ) ;
428+ const focused = state ?. routes [ state . index ] ?. params ;
429+ if ( openedEarly && focused && "threadId" in focused && focused . threadId === targetThreadId ) {
430+ navigation . navigate ( "Thread" , {
431+ environmentId : props . environmentId ,
432+ threadId : props . threadId ,
433+ } ) ;
434+ }
435+ return ;
436+ }
437+ if ( openedEarly ) return ;
438+ const targetThreadReady = await waitForThreadShell ( props . environmentId , targetThreadId ) ;
439+ if ( ! targetThreadReady ) {
440+ Dialog . alert (
441+ "Fork created" ,
442+ "Its thread data did not reach this client. Reconnect and try opening it from the thread list." ,
443+ ) ;
444+ return ;
445+ }
446+ openFork ( ) ;
447+ } ;
448+
389449 return (
390- < >
391- < Pressable
392- accessibilityRole = "button"
393- accessibilityLabel = "Fork from this response"
394- disabled = { busy }
395- onPress = { ( ) => {
396- void Haptics . selectionAsync ( ) ;
397- setChoosingWorkspace ( true ) ;
398- } }
399- className = "h-7 w-7 items-center justify-center disabled:opacity-40"
400- >
401- { busy ? (
402- < AppActivityIndicator size = "small" />
403- ) : (
404- < SymbolView
405- name = "arrow.triangle.branch"
406- size = { 13 }
407- tintColor = { props . iconColor }
408- type = "monochrome"
409- />
410- ) }
411- </ Pressable >
412- { choosingWorkspace ? (
413- < ThreadForkSheet
414- environmentId = { props . environmentId }
415- threadId = { props . threadId }
416- workspaceRoot = { props . workspaceRoot }
417- onClose = { ( ) => setChoosingWorkspace ( false ) }
418- onSelect = { ( workspaceStrategy ) => {
419- setChoosingWorkspace ( false ) ;
420- const targetThreadId = ThreadId . make ( uuidv4 ( ) ) ;
421- const preparingAtom = forkWorkspacePreparingAtom (
422- scopedThreadKey ( props . environmentId , targetThreadId ) ,
423- ) ;
424- const preparesWorktree = workspaceStrategy . type === "worktree" ;
425- const openFork = ( ) =>
426- navigation . navigate ( "Thread" , {
427- environmentId : props . environmentId ,
428- threadId : targetThreadId ,
429- } ) ;
430- setBusy ( true ) ;
431- void Haptics . selectionAsync ( ) ;
432- if ( preparesWorktree ) appAtomRegistry . set ( preparingAtom , true ) ;
433- const pendingResult = forkFromRun ( {
434- environmentId : props . environmentId ,
435- input : {
436- sourceThreadId : props . projectedItem . sourceThreadId ,
437- targetThreadId,
438- runId,
439- title : `${ props . sourceTitle } fork` ,
440- creationSource : "mobile" ,
441- workspaceStrategy,
442- } ,
443- } ) . finally ( ( ) => appAtomRegistry . set ( preparingAtom , false ) ) ;
444- void ( async ( ) => {
445- // The fork request settles only once its new worktree is ready.
446- // Open the fork as soon as its thread exists so it shows setup
447- // progress meanwhile, like a new thread started in a new worktree.
448- const openedEarly =
449- preparesWorktree &&
450- ( await Promise . race ( [
451- waitForThreadShell ( props . environmentId , targetThreadId ) ,
452- pendingResult . then ( ( ) => false ) ,
453- ] ) ) ;
454- if ( openedEarly ) openFork ( ) ;
455- const result = await pendingResult ;
456- if ( result . _tag !== "Success" ) {
457- // The server deletes a fork whose worktree could not be prepared.
458- if ( openedEarly ) {
459- const state = navigation . getState ( ) ;
460- const focused = state ?. routes [ state . index ] ;
461- if (
462- focused ?. name === "Thread" &&
463- focused . params !== undefined &&
464- "threadId" in focused . params &&
465- focused . params . threadId === targetThreadId &&
466- "environmentId" in focused . params &&
467- focused . params . environmentId === props . environmentId
468- ) {
469- navigation . navigate ( "Thread" , {
470- environmentId : props . environmentId ,
471- threadId : props . threadId ,
472- } ) ;
473- }
474- }
475- return ;
476- }
477- if ( openedEarly ) return ;
478- const targetThreadReady = await waitForThreadShell (
479- props . environmentId ,
480- targetThreadId ,
481- ) ;
482- if ( ! targetThreadReady ) {
483- Dialog . alert (
484- "Fork created" ,
485- "Its thread data did not reach this client. Reconnect and try opening it from the thread list." ,
486- ) ;
487- return ;
488- }
489- openFork ( ) ;
490- } ) ( ) . finally ( ( ) => setBusy ( false ) ) ;
491- } }
450+ < Pressable
451+ accessibilityRole = "button"
452+ accessibilityLabel = "Fork from this response"
453+ disabled = { busy }
454+ onPress = { ( ) => {
455+ const thread = appAtomRegistry . get (
456+ environmentThreadShells . threadShellAtom (
457+ scopeThreadRef ( props . environmentId , props . threadId ) ,
458+ ) ,
459+ ) ;
460+ if ( ! thread ) return ;
461+ void Haptics . selectionAsync ( ) ;
462+ const choices = threadForkWorkspaceChoices ( { ...thread , isGitRepo : props . isGitRepo } ) ;
463+ Dialog . alert ( "Fork thread from here" , "Choose where to continue from this response." , [
464+ ...choices . map ( ( choice ) => ( {
465+ text : choice . label ,
466+ onPress : ( ) => {
467+ setBusy ( true ) ;
468+ void fork ( choice . workspaceStrategy ) . finally ( ( ) => setBusy ( false ) ) ;
469+ } ,
470+ } ) ) ,
471+ { text : "Cancel" , style : "cancel" } ,
472+ ] ) ;
473+ } }
474+ className = "h-7 w-7 items-center justify-center disabled:opacity-40"
475+ >
476+ { busy ? (
477+ < AppActivityIndicator size = "small" />
478+ ) : (
479+ < SymbolView
480+ name = "arrow.triangle.branch"
481+ size = { 13 }
482+ tintColor = { props . iconColor }
483+ type = "monochrome"
492484 />
493- ) : null }
494- </ >
485+ ) }
486+ </ Pressable >
495487 ) ;
496488}
497489
@@ -1040,6 +1032,7 @@ function renderFeedEntry(
10401032 | "onEditPendingMessage"
10411033 | "threadId"
10421034 | "workspaceRoot"
1035+ | "isGitRepo"
10431036 > & {
10441037 readonly copiedRowId : string | null ;
10451038 readonly expandedWorkRows : Record < string , boolean > ;
@@ -1487,7 +1480,7 @@ function renderFeedEntry(
14871480 < AssistantForkButton
14881481 environmentId = { props . environmentId }
14891482 threadId = { props . threadId }
1490- workspaceRoot = { props . workspaceRoot ?? null }
1483+ isGitRepo = { props . isGitRepo ?? false }
14911484 iconColor = { iconSubtleColor }
14921485 projectedItem = { message . projectedItem }
14931486 sourceTitle = { props . threadTitle }
@@ -2456,6 +2449,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
24562449 threadTitle : props . threadTitle ,
24572450 skills : props . skills ,
24582451 workspaceRoot : props . workspaceRoot ,
2452+ isGitRepo : props . isGitRepo ,
24592453 workflowOpening,
24602454 } ) }
24612455 { props . worktreeSetup && info . index === setupAnchorIndex ? (
0 commit comments