Skip to content

Commit 1315fc9

Browse files
committed
feat(mobile): add dynamic workflow controls
1 parent 735cc3c commit 1315fc9

16 files changed

Lines changed: 1273 additions & 40 deletions
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { useAtomValue } from "@effect/atom-react";
2+
import type { EnvironmentId, OrchestrationV2Subagent } from "@t3tools/contracts";
3+
import { useState } from "react";
4+
import { View } from "react-native";
5+
6+
import * as Dialog from "../../components/ui/Dialog";
7+
import { IconButton } from "../../components/ui/IconButton";
8+
import { orchestrationEnvironment } from "../../state/orchestration";
9+
import { threadEnvironment } from "../../state/threads";
10+
import { useAtomCommand } from "../../state/use-atom-command";
11+
import {
12+
resolveSubagentRowPresentation,
13+
resolveSubagentStopTarget,
14+
} from "./threadAgentsPresentation";
15+
16+
export 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+
}

‎apps/mobile/src/features/threads/ThreadAgentsSheet.tsx‎

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { useAtomValue } from "@effect/atom-react";
2+
import {
3+
deriveWorkflowGroups,
4+
projectedSubagentsToRuntime,
5+
} from "@t3tools/client-runtime/state/subagentRuntime";
26
import type { ThreadTurnSubagents } from "@t3tools/client-runtime/state/thread-subagents";
37
import {
48
isOrchestrationV2WorkActive,
@@ -11,6 +15,7 @@ import { StackActions, useNavigation, type StaticScreenProps } from "@react-navi
1115
import * as DateTime from "effect/DateTime";
1216
import { Atom } from "effect/reactivity";
1317
import * as Haptics from "expo-haptics";
18+
import { useMemo } from "react";
1419
import { Platform, Pressable, ScrollView, View } from "react-native";
1520
import { ScreenStack } from "react-native-screens";
1621
import { useSafeAreaInsets } from "react-native-safe-area-context";
@@ -23,6 +28,8 @@ import { NativeHeader, NativeScreen } from "../../native/ThemedScreenStack";
2328
import { useEnvironments } from "../../state/environments";
2429
import { SubagentRow } from "./SubagentRow";
2530
import { useVisibleSecondClock } from "./use-visible-second-clock";
31+
import { SubagentStopButton } from "./SubagentStopButton";
32+
import { ThreadWorkflowGroup } from "./ThreadWorkflowGroup";
2633
import { ScreenContent } from "../../components/ui/ScreenContent";
2734
import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
2835
import { subagentThreadTarget } from "./threadAgentsPresentation";
@@ -54,6 +61,16 @@ export function ThreadAgentsSheet({ route }: StaticScreenProps<AgentsTarget>) {
5461
const turn = useThreadTurnSubagents(target);
5562
const subagents = turn?.subagents ?? [];
5663
const hasLiveAgent = (turn?.liveCount ?? 0) > 0;
64+
const workflowGroups = useMemo(
65+
() =>
66+
new Map(
67+
deriveWorkflowGroups(projectedSubagentsToRuntime(subagents)).map((group) => [
68+
group.workflow.id,
69+
group,
70+
]),
71+
),
72+
[subagents],
73+
);
5774

5875
const openChildThread = (child: { environmentId: EnvironmentId; threadId: ThreadId }) => {
5976
void Haptics.selectionAsync();
@@ -88,15 +105,29 @@ export function ThreadAgentsSheet({ route }: StaticScreenProps<AgentsTarget>) {
88105
No agents in this turn.
89106
</Text>
90107
) : (
91-
subagents.map((subagent) => (
92-
<AgentRow
93-
key={subagent.id}
94-
subagent={subagent}
95-
environmentId={target.environmentId}
96-
tickSeconds={hasLiveAgent}
97-
onOpen={openChildThread}
98-
/>
99-
))
108+
subagents.map((subagent) => {
109+
const group = workflowGroups.get(subagent.id);
110+
return group === undefined ? (
111+
<View key={subagent.id} className="border-b border-border">
112+
<AgentRow
113+
subagent={subagent}
114+
environmentId={target.environmentId}
115+
tickSeconds={hasLiveAgent}
116+
onOpen={openChildThread}
117+
/>
118+
</View>
119+
) : (
120+
<ThreadWorkflowGroup
121+
key={subagent.id}
122+
environmentId={target.environmentId}
123+
subagent={subagent}
124+
group={group}
125+
onOpen={(threadId) =>
126+
openChildThread({ environmentId: target.environmentId, threadId })
127+
}
128+
/>
129+
);
130+
})
100131
)}
101132
</ScrollView>
102133
);
@@ -154,7 +185,7 @@ function AgentRow(props: {
154185
);
155186

156187
const row = (
157-
<View className="border-b border-border py-3.5">
188+
<View className="py-3.5">
158189
<SubagentRow
159190
environmentId={props.environmentId}
160191
subagent={subagent}
@@ -164,10 +195,11 @@ function AgentRow(props: {
164195
</View>
165196
);
166197

167-
if (target === null) {
168-
return (
198+
const content =
199+
target === null ? (
169200
<View
170201
accessible
202+
className="min-w-0 flex-1"
171203
accessibilityHint={
172204
subagent.remoteChild === undefined
173205
? "Provider-managed agent. Its work appears in the transcript."
@@ -176,18 +208,22 @@ function AgentRow(props: {
176208
>
177209
{row}
178210
</View>
211+
) : (
212+
<Pressable
213+
accessibilityRole="link"
214+
accessibilityHint="Opens this agent's thread"
215+
onPress={() => props.onOpen(target)}
216+
className="min-w-0 flex-1 active:opacity-70"
217+
>
218+
{row}
219+
</Pressable>
179220
);
180-
}
181221

182222
return (
183-
<Pressable
184-
accessibilityRole="link"
185-
accessibilityHint="Opens this agent's thread"
186-
onPress={() => props.onOpen(target)}
187-
className="active:opacity-70"
188-
>
189-
{row}
190-
</Pressable>
223+
<View className="flex-row items-center">
224+
{content}
225+
<SubagentStopButton environmentId={props.environmentId} subagent={subagent} />
226+
</View>
191227
);
192228
}
193229

0 commit comments

Comments
 (0)