@@ -156,8 +156,10 @@ import {
156156import { appAtomRegistry } from "../../state/atom-registry" ;
157157import { threadStreamHaptics } from "./threadStreamHaptics" ;
158158import { useAtomCommand } from "../../state/use-atom-command" ;
159+ import { orchestrationEnvironment } from "../../state/orchestration" ;
159160import { useDelayedStatus } from "../../lib/useDelayedStatus" ;
160161import type { PendingUserInputDraftAnswer } from "@t3tools/client-runtime/state/pending-user-input" ;
162+ import type { RunStopPresentation } from "@t3tools/client-runtime/state/thread-execution" ;
161163import type {
162164 PendingApproval ,
163165 PendingUserInput ,
@@ -339,6 +341,9 @@ const threadVisitTracker = createThreadVisitTracker({
339341
340342// Past the composer's 180 ms height animation.
341343const COMPOSER_HEIGHT_SETTLE_MS = 220 ;
344+ const NO_RUN_STOP_ATOM = Atom . make < RunStopPresentation | null > ( null ) . pipe (
345+ Atom . withLabel ( "mobile-thread-run-stop:empty" ) ,
346+ ) ;
342347
343348// Anchors, the submitted message, follow state and pending scrolls belong to
344349// one thread, so the body remounts when the thread changes.
@@ -528,6 +533,43 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) {
528533 const unsettleThread = useAtomCommand ( threadEnvironment . unsettle , { reportFailure : false } ) ;
529534 const updateMetadata = useAtomCommand ( threadEnvironment . updateMetadata , { reportFailure : false } ) ;
530535 const visitThread = useAtomCommand ( threadEnvironment . visit , { reportFailure : false } ) ;
536+ const anyRunStop = useAtomValue (
537+ props . selectedThreadDetailRef === null
538+ ? NO_RUN_STOP_ATOM
539+ : environmentThreadDetails . runStopAtom ( props . selectedThreadDetailRef ) ,
540+ ) ;
541+ // A background Stop matters only while that work is still pending.
542+ const runStop =
543+ anyRunStop ?. background && props . selectedThread . pendingBackgroundTasks . length === 0
544+ ? null
545+ : anyRunStop ;
546+ const forceStopThread = useAtomCommand ( orchestrationEnvironment . forceStopThread , {
547+ reportFailure : false ,
548+ } ) ;
549+ const canForceStop =
550+ useAtomValue ( orchestrationEnvironment . forceStopThread . permissionAtom ( props . environmentId ) ) &&
551+ props . serverConfig ?. environment . capabilities . threadForceStop === true ;
552+ const [ forceStoppingKey , setForceStoppingKey ] = useState < string | null > ( null ) ;
553+ const forceStop = async ( ) => {
554+ const threadKey = selectedThreadKey ;
555+ setForceStoppingKey ( threadKey ) ;
556+ const result = await forceStopThread ( {
557+ environmentId : props . environmentId ,
558+ input : { threadId : props . selectedThread . id } ,
559+ } ) ;
560+ setForceStoppingKey ( ( current ) => ( current === threadKey ? null : current ) ) ;
561+ if ( result . _tag === "Failure" ) {
562+ if ( ! isAtomCommandInterrupted ( result ) ) Dialog . alert ( "Could not force stop the agent" ) ;
563+ return ;
564+ }
565+ const others = result . value . sharedWithThreadIds . length ;
566+ if ( result . value . outcome === "shared" ) {
567+ Dialog . alert (
568+ "Turn stopped; the agent process kept running" ,
569+ `It is shared with ${ others } other working ${ others === 1 ? "thread" : "threads" } . Stop those to free it.` ,
570+ ) ;
571+ }
572+ } ;
531573 const [ statusRequest , setStatusRequest ] = useState < { threadKey : string } | null > ( null ) ;
532574 const statusPending = statusRequest ?. threadKey === selectedThreadKey ;
533575 const [ lifecycleNow , setLifecycleNow ] = useState ( ( ) => new Date ( ) . toISOString ( ) ) ;
@@ -647,6 +689,31 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) {
647689 } ,
648690 } ;
649691 }
692+ if ( runStop !== null && contentPresentationKind === "ready" ) {
693+ const unresponsive = runStop . state === "unresponsive" ;
694+ return {
695+ kind : "stop" ,
696+ unresponsive,
697+ label : unresponsive ? "Not responding to Stop" : "Stopping…" ,
698+ action :
699+ unresponsive && canForceStop
700+ ? {
701+ label : forceStoppingKey === selectedThreadKey ? "Force stopping..." : "Force stop" ,
702+ accessibilityLabel : "Force stop the agent" ,
703+ disabled : forceStoppingKey === selectedThreadKey ,
704+ onPress : ( ) =>
705+ Dialog . confirm ( {
706+ title : "Force stop the agent?" ,
707+ message :
708+ "It has not answered Stop. Force stop kills its processes and ends the turn." ,
709+ confirmText : "Force stop" ,
710+ destructive : true ,
711+ onConfirm : ( ) => void forceStop ( ) ,
712+ } ) ,
713+ }
714+ : null ,
715+ } ;
716+ }
650717 if ( props . isCompacting && contentPresentationKind === "ready" ) {
651718 return { kind : "compacting" } ;
652719 }
0 commit comments