@@ -25,6 +25,7 @@ import {
2525import { useLoadBalancedEnvironment } from "../hooks/useLoadBalancedEnvironment" ;
2626import { useScratchProject } from "../hooks/useScratchProject" ;
2727import { useStableCallback } from "../hooks/useStableCallback" ;
28+ import { useThreadTerminalActions } from "./terminal/useThreadTerminalActions" ;
2829import { usePendingThreadRequests } from "./chat/usePendingThreadRequests" ;
2930import { isScratchProject } from "@t3tools/client-runtime/state/projects" ;
3031import { visibleThreadPullRequests } from "@t3tools/shared/threadPullRequests" ;
@@ -76,7 +77,6 @@ import {
7677 resolveEnvironmentMachineKind ,
7778 resolveProjectProviderInstanceEnabled ,
7879 RuntimeMode ,
79- TerminalOpenInput ,
8080 type WorktreeSetupSnapshot ,
8181} from "@t3tools/contracts" ;
8282import { type EnvironmentConnectionPresentation } from "@t3tools/client-runtime/connection" ;
@@ -125,11 +125,7 @@ import {
125125 resolvePromptInjectedEffort ,
126126 resolveSelectableModel ,
127127} from "@t3tools/shared/model" ;
128- import {
129- projectScriptCwd ,
130- projectScriptRuntimeEnv ,
131- resolveProjectScripts ,
132- } from "@t3tools/shared/projectScripts" ;
128+ import { projectScriptCwd , resolveProjectScripts } from "@t3tools/shared/projectScripts" ;
133129import { CHAT_LIST_ANCHOR_OFFSET } from "@t3tools/shared/chatList" ;
134130import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork" ;
135131import {
@@ -140,7 +136,7 @@ import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
140136import { sourceControlRepositorySelector } from "@t3tools/shared/sourceControl" ;
141137import { truncate } from "@t3tools/shared/String" ;
142138import { resolveThreadReferenceCopyTarget } from "@t3tools/shared/threadReference" ;
143- import { nextTerminalId , resolveTerminalSessionLabel } from "@t3tools/shared/terminalLabels" ;
139+ import { resolveTerminalSessionLabel } from "@t3tools/shared/terminalLabels" ;
144140import { Debouncer } from "@tanstack/react-pacer" ;
145141import { useAtomValue } from "@effect/atom-react" ;
146142import { Atom } from "effect/reactivity" ;
@@ -400,7 +396,6 @@ import {
400396 primaryServerKeybindingsAtom ,
401397 serverEnvironment ,
402398} from "../state/server" ;
403- import { terminalEnvironment } from "../state/terminal" ;
404399import { threadEnvironment } from "../state/threads" ;
405400import { workspacePreparationRetryRunIds } from "@t3tools/client-runtime/state/turn-item-presentation" ;
406401import { resolveProviderSkillsForCwd } from "@t3tools/client-runtime/providerSkills" ;
@@ -1025,9 +1020,6 @@ export default function ChatView(props: ChatViewProps) {
10251020 const removeKeybinding = useAtomCommand ( serverEnvironment . removeKeybinding , {
10261021 reportFailure : false ,
10271022 } ) ;
1028- const openTerminal = useAtomCommand ( terminalEnvironment . open , "terminal open" ) ;
1029- const writeTerminal = useAtomCommand ( terminalEnvironment . write , "terminal write" ) ;
1030- const closeTerminalMutation = useAtomCommand ( terminalEnvironment . close , "terminal close" ) ;
10311023 const createThread = useOrchestrationCommand ( threadEnvironment . create , { reportFailure : false } ) ;
10321024 const deleteThread = useOrchestrationCommand ( threadEnvironment . delete , { reportFailure : false } ) ;
10331025 const updateThreadMetadata = useOrchestrationCommand ( threadEnvironment . updateMetadata , {
@@ -1602,6 +1594,7 @@ export default function ChatView(props: ChatViewProps) {
16021594 return labels ;
16031595 } , [ activeThreadKnownSessions ] ) ;
16041596 const activeThreadRef = useActiveThreadRef ( activeThread ) ;
1597+ const terminalActions = useThreadTerminalActions ( activeThreadRef ) ;
16051598 const activeThreadKey = activeThreadRef ? scopedThreadKey ( activeThreadRef ) : null ;
16061599 const activeEnvironmentServerBrowser = useEnvironmentSupportsServerBrowser (
16071600 activeThreadRef ?. environmentId ?? null ,
@@ -1690,18 +1683,7 @@ export default function ChatView(props: ChatViewProps) {
16901683 ) ,
16911684 [ rightPanelState . surfaces ] ,
16921685 ) ;
1693- const allocatableActiveTerminalIds = useMemo (
1694- ( ) => [ ...new Set ( [ ...activeKnownTerminalIds , ...panelTerminalIds ] ) ] ,
1695- [ activeKnownTerminalIds , panelTerminalIds ] ,
1696- ) ;
16971686 const canReuseTerminal = activeThreadKnownSessionsRaw !== null ;
1698- const allocateTerminalId = ( ) =>
1699- nextTerminalId (
1700- allocatableActiveTerminalIds ,
1701- canReuseTerminal && readEnvironmentScope ( environmentId , AuthTerminalReadScope )
1702- ? undefined
1703- : randomUUID ( ) ,
1704- ) ;
17051687 const previewPanelOpen = activeRightPanelKind === "preview" && browserAvailable ;
17061688 const rightPanelOpen = rightPanelState . isOpen ;
17071689 const { active : panelAnimationsActive , durationMs : panelAnimationDurationMs } =
@@ -3871,119 +3853,40 @@ export default function ChatView(props: ChatViewProps) {
38713853 storeEnsureTerminal ( activeThreadRef , existingTerminalId , { open : true , active : true } ) ;
38723854 return ;
38733855 }
3874- if ( ! activeThreadId || ! activeProject ) {
3875- return ;
3876- }
3877- const cwdForOpen = gitCwd ?? activeProject . workspaceRoot ;
3878- if ( ! cwdForOpen ) {
3879- return ;
3880- }
3881- const terminalId = allocateTerminalId ( ) ;
3882- storeEnsureTerminal ( activeThreadRef , terminalId , { open : true } ) ;
3883- void openTerminal ( {
3884- environmentId,
3885- input : {
3886- threadId : activeThreadId ,
3887- terminalId,
3888- cwd : cwdForOpen ,
3889- ...( activeThreadWorktreePath != null ? { worktreePath : activeThreadWorktreePath } : { } ) ,
3890- env : projectScriptRuntimeEnv ( {
3891- project : { cwd : activeProject . workspaceRoot } ,
3892- worktreePath : activeThreadWorktreePath ,
3893- } ) ,
3894- } ,
3895- } ) ;
3856+ void terminalActions . launchTerminal ( {
3857+ place : ( terminalId ) => storeEnsureTerminal ( activeThreadRef , terminalId , { open : true } ) ,
3858+ } ) ?. result ;
38963859 return ;
38973860 }
38983861 setTerminalOpen ( nextOpen ) ;
38993862 } ;
39003863 const toggleTerminalVisibilityStable = useStableCallback ( toggleTerminalVisibility ) ;
39013864 const splitTerminal = ( direction : "horizontal" | "vertical" = "horizontal" ) => {
3902- if (
3903- ! hasTerminalWriteAccess ( ) ||
3904- ! activeThreadRef ||
3905- hasReachedSplitLimit ||
3906- ! activeThreadId ||
3907- ! activeProject
3908- ) {
3909- return ;
3910- }
3911- const cwdForOpen = gitCwd ?? activeProject . workspaceRoot ;
3912- if ( ! cwdForOpen ) {
3913- return ;
3914- }
3915- const terminalId = allocateTerminalId ( ) ;
3916- if ( direction === "vertical" ) {
3917- storeSplitTerminalVertical ( activeThreadRef , terminalId ) ;
3918- } else {
3919- storeSplitTerminal ( activeThreadRef , terminalId ) ;
3920- }
3921- setTerminalFocusRequestId ( ( value ) => value + 1 ) ;
3922- void openTerminal ( {
3923- environmentId,
3924- input : {
3925- threadId : activeThreadId ,
3926- terminalId,
3927- cwd : cwdForOpen ,
3928- ...( activeThreadWorktreePath != null ? { worktreePath : activeThreadWorktreePath } : { } ) ,
3929- env : projectScriptRuntimeEnv ( {
3930- project : { cwd : activeProject . workspaceRoot } ,
3931- worktreePath : activeThreadWorktreePath ,
3932- } ) ,
3933- } ,
3865+ if ( ! activeThreadRef || hasReachedSplitLimit ) return ;
3866+ const launched = terminalActions . launchTerminal ( {
3867+ place : ( terminalId ) =>
3868+ direction === "vertical"
3869+ ? storeSplitTerminalVertical ( activeThreadRef , terminalId )
3870+ : storeSplitTerminal ( activeThreadRef , terminalId ) ,
39343871 } ) ;
3872+ if ( ! launched ) return ;
3873+ setTerminalFocusRequestId ( ( value ) => value + 1 ) ;
3874+ void launched . result ;
39353875 } ;
39363876 const splitTerminalStable = useStableCallback ( splitTerminal ) ;
39373877 const createNewTerminal = ( ) => {
3938- if ( ! hasTerminalWriteAccess ( ) || ! activeThreadRef || ! activeThreadId || ! activeProject ) {
3939- return ;
3940- }
3941- const cwdForOpen = gitCwd ?? activeProject . workspaceRoot ;
3942- if ( ! cwdForOpen ) {
3943- return ;
3944- }
3945- const terminalId = allocateTerminalId ( ) ;
3946- storeNewTerminal ( activeThreadRef , terminalId ) ;
3947- setTerminalFocusRequestId ( ( value ) => value + 1 ) ;
3948- void openTerminal ( {
3949- environmentId,
3950- input : {
3951- threadId : activeThreadId ,
3952- terminalId,
3953- cwd : cwdForOpen ,
3954- ...( activeThreadWorktreePath != null ? { worktreePath : activeThreadWorktreePath } : { } ) ,
3955- env : projectScriptRuntimeEnv ( {
3956- project : { cwd : activeProject . workspaceRoot } ,
3957- worktreePath : activeThreadWorktreePath ,
3958- } ) ,
3959- } ,
3878+ if ( ! activeThreadRef ) return ;
3879+ const launched = terminalActions . launchTerminal ( {
3880+ place : ( terminalId ) => storeNewTerminal ( activeThreadRef , terminalId ) ,
39603881 } ) ;
3882+ if ( ! launched ) return ;
3883+ setTerminalFocusRequestId ( ( value ) => value + 1 ) ;
3884+ void launched . result ;
39613885 } ;
39623886 const createNewTerminalStable = useStableCallback ( createNewTerminal ) ;
39633887 const closeTerminal = ( terminalId : string ) => {
3964- if ( ! hasTerminalWriteAccess ( ) || ! activeThreadId || ! activeThreadRef ) return ;
3965- const fallbackExitWrite = ( ) =>
3966- writeTerminal ( {
3967- environmentId,
3968- input : { threadId : activeThreadId , terminalId, data : "exit\n" } ,
3969- } ) ;
3970- void ( async ( ) => {
3971- const closeResult = await closeTerminalMutation ( {
3972- environmentId,
3973- input : {
3974- threadId : activeThreadId ,
3975- terminalId,
3976- deleteHistory : true ,
3977- } ,
3978- } ) ;
3979- if (
3980- closeResult . _tag === "Failure" &&
3981- ! isAtomCommandInterrupted ( closeResult ) &&
3982- hasTerminalWriteAccess ( )
3983- ) {
3984- await fallbackExitWrite ( ) ;
3985- }
3986- } ) ( ) ;
3888+ if ( ! hasTerminalWriteAccess ( ) || ! activeThreadRef ) return ;
3889+ terminalActions . closeTerminalSession ( terminalId ) ;
39873890 storeCloseTerminal ( activeThreadRef , terminalId ) ;
39883891 setTerminalFocusRequestId ( ( value ) => value + 1 ) ;
39893892 } ;
@@ -4026,39 +3929,20 @@ export default function ChatView(props: ChatViewProps) {
40263929 }
40273930 setTerminalFocusRequestId ( ( value ) => value + 1 ) ;
40283931
4029- const runtimeEnv = projectScriptRuntimeEnv ( {
4030- project : {
4031- cwd : activeProject . workspaceRoot ,
4032- } ,
4033- worktreePath : targetWorktreePath ,
3932+ const launched = terminalActions . launchTerminal ( {
3933+ place : ( terminalId ) =>
3934+ shouldCreateNewTerminal
3935+ ? storeNewTerminal ( activeThreadRef , terminalId )
3936+ : storeSetActiveTerminal ( activeThreadRef , terminalId ) ,
3937+ ...( shouldCreateNewTerminal
3938+ ? { size : { cols : SCRIPT_TERMINAL_COLS , rows : SCRIPT_TERMINAL_ROWS } }
3939+ : { terminalId : baseTerminalId } ) ,
3940+ location : { cwd : targetCwd , worktreePath : targetWorktreePath } ,
40343941 ...( options ?. env ? { extraEnv : options . env } : { } ) ,
40353942 } ) ;
4036- const targetTerminalId = shouldCreateNewTerminal ? allocateTerminalId ( ) : baseTerminalId ;
4037- const openTerminalInput : TerminalOpenInput = shouldCreateNewTerminal
4038- ? {
4039- threadId : activeThreadId ,
4040- terminalId : targetTerminalId ,
4041- cwd : targetCwd ,
4042- ...( targetWorktreePath !== null ? { worktreePath : targetWorktreePath } : { } ) ,
4043- env : runtimeEnv ,
4044- cols : SCRIPT_TERMINAL_COLS ,
4045- rows : SCRIPT_TERMINAL_ROWS ,
4046- }
4047- : {
4048- threadId : activeThreadId ,
4049- terminalId : targetTerminalId ,
4050- cwd : targetCwd ,
4051- ...( targetWorktreePath !== null ? { worktreePath : targetWorktreePath } : { } ) ,
4052- env : runtimeEnv ,
4053- } ;
4054-
4055- if ( shouldCreateNewTerminal ) {
4056- storeNewTerminal ( activeThreadRef , targetTerminalId ) ;
4057- } else {
4058- storeSetActiveTerminal ( activeThreadRef , targetTerminalId ) ;
4059- }
4060-
4061- const openResult = await openTerminal ( { environmentId, input : openTerminalInput } ) ;
3943+ if ( ! launched ) return ;
3944+ const targetTerminalId = launched . terminalId ;
3945+ const openResult = await launched . result ;
40623946 if ( openResult . _tag === "Failure" ) {
40633947 if ( ! isAtomCommandInterrupted ( openResult ) ) {
40643948 const error = squashAtomCommandFailure ( openResult ) ;
@@ -4070,15 +3954,11 @@ export default function ChatView(props: ChatViewProps) {
40703954 return ;
40713955 }
40723956
4073- if ( ! hasTerminalWriteAccess ( ) ) return ;
4074- const writeResult = await writeTerminal ( {
4075- environmentId,
4076- input : {
4077- threadId : activeThreadId ,
4078- terminalId : targetTerminalId ,
4079- data : `${ script . command } \r` ,
4080- } ,
4081- } ) ;
3957+ const writeResult = await terminalActions . writeToTerminal (
3958+ targetTerminalId ,
3959+ `${ script . command } \r` ,
3960+ ) ;
3961+ if ( ! writeResult ) return ;
40823962 if ( writeResult . _tag === "Failure" ) {
40833963 if ( ! isAtomCommandInterrupted ( writeResult ) ) {
40843964 const error = squashAtomCommandFailure ( writeResult ) ;
@@ -4846,56 +4726,34 @@ export default function ChatView(props: ChatViewProps) {
48464726 } ;
48474727 const togglePreviewPanelStable = useStableCallback ( togglePreviewPanel ) ;
48484728 const addTerminalSurface = ( ) => {
4849- if ( ! hasTerminalWriteAccess ( ) || ! activeThreadRef || ! activeThreadId || ! activeProject ) return ;
4850- const cwd = gitCwd ?? activeProject . workspaceRoot ;
4851- const terminalId = allocateTerminalId ( ) ;
4852- useRightPanelStore . getState ( ) . openTerminal ( activeThreadRef , terminalId ) ;
4853- setTerminalFocusRequestId ( ( value ) => value + 1 ) ;
4854- void openTerminal ( {
4855- environmentId : activeThreadRef . environmentId ,
4856- input : {
4857- threadId : activeThreadId ,
4858- terminalId,
4859- cwd,
4860- ...( activeThreadWorktreePath != null ? { worktreePath : activeThreadWorktreePath } : { } ) ,
4861- env : projectScriptRuntimeEnv ( {
4862- project : { cwd : activeProject . workspaceRoot } ,
4863- worktreePath : activeThreadWorktreePath ,
4864- } ) ,
4865- } ,
4729+ if ( ! activeThreadRef ) return ;
4730+ const launched = terminalActions . launchTerminal ( {
4731+ place : ( terminalId ) =>
4732+ useRightPanelStore . getState ( ) . openTerminal ( activeThreadRef , terminalId ) ,
48664733 } ) ;
4734+ if ( ! launched ) return ;
4735+ setTerminalFocusRequestId ( ( value ) => value + 1 ) ;
4736+ void launched . result ;
48674737 } ;
48684738 const addTerminalSurfaceStable = useStableCallback ( addTerminalSurface ) ;
48694739 const splitPanelTerminal = ( direction : "horizontal" | "vertical" = "horizontal" ) => {
48704740 if (
4871- ! hasTerminalWriteAccess ( ) ||
48724741 ! activeThreadRef ||
4873- ! activeThreadId ||
4874- ! activeProject ||
48754742 activeRightPanelSurface ?. kind !== "terminal" ||
48764743 activeRightPanelSurface . terminalIds . length >= MAX_TERMINALS_PER_GROUP
48774744 ) {
48784745 return ;
48794746 }
4880- const terminalId = allocateTerminalId ( ) ;
4881- const cwd = gitCwd ?? activeProject . workspaceRoot ;
4882- useRightPanelStore
4883- . getState ( )
4884- . splitTerminal ( activeThreadRef , activeRightPanelSurface . id , terminalId , direction ) ;
4885- setTerminalFocusRequestId ( ( value ) => value + 1 ) ;
4886- void openTerminal ( {
4887- environmentId : activeThreadRef . environmentId ,
4888- input : {
4889- threadId : activeThreadId ,
4890- terminalId,
4891- cwd,
4892- ...( activeThreadWorktreePath != null ? { worktreePath : activeThreadWorktreePath } : { } ) ,
4893- env : projectScriptRuntimeEnv ( {
4894- project : { cwd : activeProject . workspaceRoot } ,
4895- worktreePath : activeThreadWorktreePath ,
4896- } ) ,
4897- } ,
4747+ const surfaceId = activeRightPanelSurface . id ;
4748+ const launched = terminalActions . launchTerminal ( {
4749+ place : ( terminalId ) =>
4750+ useRightPanelStore
4751+ . getState ( )
4752+ . splitTerminal ( activeThreadRef , surfaceId , terminalId , direction ) ,
48984753 } ) ;
4754+ if ( ! launched ) return ;
4755+ setTerminalFocusRequestId ( ( value ) => value + 1 ) ;
4756+ void launched . result ;
48994757 } ;
49004758 const splitPanelTerminalStable = useStableCallback ( splitPanelTerminal ) ;
49014759 const splitPanelTerminalVertical = ( ) => {
@@ -4917,10 +4775,7 @@ export default function ChatView(props: ChatViewProps) {
49174775 activeRightPanelSurface ?. kind !== "terminal"
49184776 )
49194777 return ;
4920- void closeTerminalMutation ( {
4921- environmentId : activeThreadRef . environmentId ,
4922- input : { threadId : activeThreadRef . threadId , terminalId, deleteHistory : true } ,
4923- } ) ;
4778+ terminalActions . closeTerminalSession ( terminalId ) ;
49244779 storeCloseTerminal ( activeThreadRef , terminalId ) ;
49254780 useRightPanelStore
49264781 . getState ( )
@@ -5004,10 +4859,7 @@ export default function ChatView(props: ChatViewProps) {
50044859 ) {
50054860 for ( const terminalId of surface . terminalIds ) {
50064861 storeCloseTerminal ( activeThreadRef , terminalId ) ;
5007- void closeTerminalMutation ( {
5008- environmentId : activeThreadRef . environmentId ,
5009- input : { threadId : activeThreadRef . threadId , terminalId, deleteHistory : true } ,
5010- } ) ;
4862+ terminalActions . closeTerminalSession ( terminalId ) ;
50114863 }
50124864 }
50134865 }
0 commit comments