Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import type { EnvironmentId, OrchestrationV2Subagent } from "@t3tools/contracts";
3import { useState } from "react";
4import { View } from "react-native";
5
6import * as Dialog from "../../components/ui/Dialog";
7import { IconButton } from "../../components/ui/IconButton";
8import { orchestrationEnvironment } from "../../state/orchestration";
9import { threadEnvironment } from "../../state/threads";
10import { useAtomCommand } from "../../state/use-atom-command";
11import {
12 resolveSubagentRowPresentation,
13 resolveSubagentStopTarget,
14} from "./threadAgentsPresentation";
15
16export function SubagentStopButton(props: {
17 readonly environmentId: EnvironmentId;
18 readonly subagent: OrchestrationV2Subagent;
19 readonly className?: string;
20}) {
21 const target = resolveSubagentStopTarget(props.subagent);
22 const canStop = useAtomValue(
23 orchestrationEnvironment.stopWorkflow.permissionAtom(props.environmentId),
24 );
25 const stopWorkflow = useAtomCommand(orchestrationEnvironment.stopWorkflow);
26 const interruptTurn = useAtomCommand(threadEnvironment.interruptTurn);
27 const [stopping, setStopping] = useState(false);
28
29 if (target === null || !canStop) return null;
30
31 const workflow = target.kind === "workflow";
32 const label =
33 props.subagent.workflow?.name ?? resolveSubagentRowPresentation(props.subagent).title;
34 const stop = async () => {
35 setStopping(true);
36 const result = await (
37 target.kind === "workflow"
38 ? stopWorkflow({ environmentId: props.environmentId, input: target.input })
39 : interruptTurn({ environmentId: props.environmentId, input: target.input })
40 ).finally(() => setStopping(false));
41 if (result._tag === "Failure") {
42 Dialog.alert(workflow ? "Could not stop workflow" : "Could not stop agent");
43 }
44 };
45
46 return (
47 <View className={props.className}>
48 <IconButton
49 icon="stop.fill"
50 variant="danger"
51 accessibilityLabel={`${stopping ? "Stopping" : "Stop"} ${workflow ? "workflow" : "agent"} ${label}`}
52 disabled={stopping}
53 onPress={() =>
54 Dialog.confirm({
55 title: workflow ? "Stop workflow?" : "Stop agent?",
56 message: workflow ? `Every running agent in ${label} stops.` : `Stop ${label}?`,
57 confirmText: "Stop",
58 destructive: true,
59 onConfirm: () => void stop(),
60 })
61 }
62 />
63 </View>
64 );
65}