@@ -30,7 +30,6 @@ import {
3030 projectScriptCwd ,
3131 projectScriptReservesPort ,
3232 projectScriptRuntimeEnv ,
33- projectScriptTerminalId ,
3433 resolveProjectScripts ,
3534} from "@t3tools/shared/projectScripts" ;
3635import { Platform , ScrollView , View } from "react-native" ;
@@ -297,7 +296,7 @@ function ThreadRouteContent(
297296 const gitActions = useSelectedThreadGitActions ( ) ;
298297 const requests = useSelectedThreadRequests ( ) ;
299298 const interruptThreadTurn = useAtomCommand ( threadEnvironment . interruptTurn , "thread interrupt" ) ;
300- const getScriptPorts = useAtomCommand ( projectEnvironment . getScriptPorts , "assign script port " ) ;
299+ const runScript = useAtomCommand ( projectEnvironment . runScript , "run project script " ) ;
301300 const loadEarlierHistory = useAtomCommand ( threadEnvironment . loadEarlierHistory , {
302301 label : "load earlier thread history" ,
303302 reportFailure : false ,
@@ -364,6 +363,15 @@ function ThreadRouteContent(
364363 const threadId = firstRouteParam ( params . threadId ) ;
365364 const routeThreadIdentity =
366365 environmentIdRaw !== null && threadId !== null ? `${ environmentIdRaw } :${ threadId } ` : null ;
366+ const focusedThreadIdentityRef = useRef < string | null > ( null ) ;
367+ useFocusEffect (
368+ useCallback ( ( ) => {
369+ focusedThreadIdentityRef . current = routeThreadIdentity ;
370+ return ( ) => {
371+ focusedThreadIdentityRef . current = null ;
372+ } ;
373+ } , [ routeThreadIdentity ] ) ,
374+ ) ;
367375 const [ inspectorSelection , setInspectorSelection ] = useState < ThreadInspectorSelection | null > (
368376 ( ) => ( props . renderInspector ? { routeThreadIdentity, mode : "route" } : null ) ,
369377 ) ;
@@ -455,10 +463,6 @@ function ThreadRouteContent(
455463 environmentId : selectedThread ?. environmentId ?? null ,
456464 threadId : selectedThread ?. id ?? null ,
457465 } ) ;
458- const { sessions : knownEnvironmentTerminalSessions } = useKnownTerminalSessions ( {
459- environmentId : selectedThread ?. environmentId ?? null ,
460- threadId : null ,
461- } ) ;
462466 const terminalMenuSessions = useMemo (
463467 ( ) =>
464468 buildTerminalMenuSessions ( {
@@ -729,44 +733,26 @@ function ThreadRouteContent(
729733 project : { cwd : selectedThreadProject . workspaceRoot } ,
730734 worktreePath : preferredWorktreePath ,
731735 } ) ;
732- let port : number | undefined ;
733736 if ( projectScriptReservesPort ( script ) ) {
734- const result = await getScriptPorts ( {
737+ const result = await runScript ( {
735738 environmentId : selectedThread . environmentId ,
736- input : { cwd, scriptIds : [ script . id ] } ,
739+ input : {
740+ threadId : selectedThread . id ,
741+ projectId : selectedThreadProject . id ,
742+ worktreePath : preferredWorktreePath ,
743+ scriptId : script . id ,
744+ } ,
737745 } ) ;
746+ if ( focusedThreadIdentityRef . current !== routeThreadIdentity ) return ;
738747 if ( result . _tag !== "Success" ) {
739748 if ( ! isAtomCommandInterrupted ( result ) ) {
740749 Dialog . alert ( "Couldn't run script" , String ( squashAtomCommandFailure ( result ) ) ) ;
741750 }
742751 return ;
743752 }
744- port = result . value . ports [ script . id ] ;
745- }
746- const targetTerminalId =
747- port !== undefined
748- ? projectScriptTerminalId ( port )
749- : resolveProjectScriptTerminalId ( {
750- existingTerminalIds : terminalMenuSessions . map ( ( session ) => session . terminalId ) ,
751- ...( knownTerminalSessions === null ||
752- ! readEnvironmentScope ( selectedThread . environmentId , AuthTerminalReadScope )
753- ? { uniqueSuffix : uuidv4 ( ) }
754- : { } ) ,
755- hasRunningTerminal : terminalMenuSessions . some (
756- ( session ) => session . status === "running" || session . status === "starting" ,
757- ) ,
758- } ) ;
759- const runningScriptTerminal =
760- port === undefined
761- ? undefined
762- : knownEnvironmentTerminalSessions ?. find (
763- ( session ) =>
764- session . target . terminalId === targetTerminalId &&
765- ( session . state . status === "starting" || session . state . hasRunningSubprocess ) ,
766- ) ;
767- if ( runningScriptTerminal ) {
768- if ( runningScriptTerminal . target . threadId === selectedThread . id ) {
769- handleOpenTerminal ( targetTerminalId ) ;
753+ if ( result . value . status === "no-script" ) return ;
754+ if ( result . value . threadId === selectedThread . id ) {
755+ handleOpenTerminal ( result . value . terminalId ) ;
770756 } else {
771757 Dialog . alert (
772758 "Already running" ,
@@ -775,10 +761,19 @@ function ThreadRouteContent(
775761 }
776762 return ;
777763 }
764+ const targetTerminalId = resolveProjectScriptTerminalId ( {
765+ existingTerminalIds : terminalMenuSessions . map ( ( session ) => session . terminalId ) ,
766+ ...( knownTerminalSessions === null ||
767+ ! readEnvironmentScope ( selectedThread . environmentId , AuthTerminalReadScope )
768+ ? { uniqueSuffix : uuidv4 ( ) }
769+ : { } ) ,
770+ hasRunningTerminal : terminalMenuSessions . some (
771+ ( session ) => session . status === "running" || session . status === "starting" ,
772+ ) ,
773+ } ) ;
778774 const env = projectScriptRuntimeEnv ( {
779775 project : { cwd : selectedThreadProject . workspaceRoot } ,
780776 worktreePath : preferredWorktreePath ,
781- ...( port !== undefined ? { extraEnv : { T3CODE_PREVIEW_PORT : String ( port ) } } : { } ) ,
782777 } ) ;
783778 stagePendingTerminalLaunch ( {
784779 target : {
@@ -807,15 +802,15 @@ function ThreadRouteContent(
807802 } ) ;
808803 } ,
809804 [
810- getScriptPorts ,
805+ runScript ,
811806 handleOpenTerminal ,
812807 navigation ,
808+ routeThreadIdentity ,
813809 selectedThread ,
814810 selectedThreadDetailWorktreePath ,
815811 selectedThreadProject ,
816812 terminalMenuSessions ,
817813 knownTerminalSessions ,
818- knownEnvironmentTerminalSessions ,
819814 ] ,
820815 ) ;
821816 const threadGitControlProps = {
0 commit comments