@@ -4,14 +4,19 @@ import {
44 scopeThreadRef ,
55 scopedThreadKey ,
66} from "@t3tools/client-runtime/environment" ;
7- import { settlePromise , squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime" ;
7+ import {
8+ isAtomCommandInterrupted ,
9+ settlePromise ,
10+ squashAtomCommandFailure ,
11+ } from "@t3tools/client-runtime/state/runtime" ;
812import { canSnooze , threadWokeAt } from "@t3tools/client-runtime/state/thread-settled" ;
913import { threadRuntimeCanArchive } from "@t3tools/client-runtime/state/models" ;
1014import {
1115 AuthOrchestrationOperateScope ,
1216 AuthSourceControlWriteScope ,
1317 EnvironmentAuthorizationError ,
1418 EnvironmentId ,
19+ ProjectId ,
1520 type ScopedThreadRef ,
1621 ThreadId ,
1722 sessionGrantsScope ,
@@ -27,7 +32,7 @@ import { useComposerDraftStore } from "../composerDraftStore";
2732import { environmentSession , readEnvironmentScope } from "../state/session" ;
2833import { appAtomRegistry } from "../rpc/atomRegistry" ;
2934import { environmentServerConfigsAtom } from "../state/server" ;
30- import { isScratchProject } from "@t3tools/client-runtime/state/projects" ;
35+ import { isScratchProject , threadMoveDestinations } from "@t3tools/client-runtime/state/projects" ;
3136import { orchestrationEnvironment } from "../state/orchestration" ;
3237import { projectEnvironment } from "../state/projects" ;
3338import { threadEnvironment } from "../state/threads" ;
@@ -41,6 +46,7 @@ import {
4146 readEnvironmentSupportsAutoSettleOptOut ,
4247 readEnvironmentThreadRefs ,
4348 readProject ,
49+ readProjects ,
4450 readThreadShell ,
4551 readThreadShells ,
4652} from "../state/entities" ;
@@ -260,6 +266,20 @@ export function canTurnThreadIntoProject(threadRef: ScopedThreadRef): boolean {
260266 return project !== null && isScratchProject ( project , config . scratchWorkspaceRoot ) ;
261267}
262268
269+ /** Projects in the thread's environment it can move to, on a server that supports moving. */
270+ export function threadMoveDestinationsFor (
271+ threadRef : ScopedThreadRef ,
272+ ) : ReadonlyArray < { readonly id : ProjectId ; readonly label : string } > {
273+ const thread = readThreadShell ( threadRef ) ;
274+ const config = appAtomRegistry . get ( environmentServerConfigsAtom ) . get ( threadRef . environmentId ) ;
275+ if ( ! thread || config ?. environment . capabilities . threadProjectMove !== true ) return [ ] ;
276+ return threadMoveDestinations (
277+ { ...thread , worktreePath : thread . worktreePath ?? null } ,
278+ readProjects ( ) ,
279+ config . scratchWorkspaceRoot ,
280+ ) . map ( ( project ) => ( { id : project . id , label : project . title } ) ) ;
281+ }
282+
263283function threadOperationFailure ( target : ScopedThreadRef ) {
264284 return readEnvironmentScope ( target . environmentId , AuthOrchestrationOperateScope )
265285 ? null
@@ -314,6 +334,9 @@ export function useThreadActions() {
314334 const createProjectFromThread = useAtomCommand ( projectEnvironment . createFromThread , {
315335 reportFailure : false ,
316336 } ) ;
337+ const moveThreadMutation = useAtomCommand ( projectEnvironment . moveThread , {
338+ reportFailure : false ,
339+ } ) ;
317340 const stopThreadSession = useOrchestrationCommand ( threadEnvironment . stopSession ) ;
318341 const removeWorktree = useAtomCommand ( vcsEnvironment . removeWorktree , {
319342 reportFailure : false ,
@@ -970,5 +993,28 @@ export function useThreadActions() {
970993 environmentId : target . environmentId ,
971994 input : { threadId : target . threadId } ,
972995 } ) ,
996+ moveThreadToProject : async ( target : ScopedThreadRef , projectId : ProjectId ) => {
997+ const result = await moveThreadMutation ( {
998+ environmentId : target . environmentId ,
999+ input : { threadId : target . threadId , projectId } ,
1000+ } ) ;
1001+ if ( result . _tag === "Success" ) {
1002+ const project = readProject ( { environmentId : target . environmentId , projectId } ) ;
1003+ toastManager . add ( {
1004+ type : "success" ,
1005+ title : "Thread moved" ,
1006+ description : project ? `Its next turn runs in ${ project . title } .` : undefined ,
1007+ } ) ;
1008+ } else if ( ! isAtomCommandInterrupted ( result ) ) {
1009+ const error = squashAtomCommandFailure ( result ) ;
1010+ toastManager . add (
1011+ stackedThreadToast ( {
1012+ type : "error" ,
1013+ title : "Could not move thread" ,
1014+ description : error instanceof Error ? error . message : "An error occurred." ,
1015+ } ) ,
1016+ ) ;
1017+ }
1018+ } ,
9731019 } ;
9741020}
0 commit comments