Skip to content

Commit e0ab895

Browse files
committed
feat(mobile): show dynamic workflows in the Agents sheet
1 parent 5753207 commit e0ab895

9 files changed

Lines changed: 511 additions & 68 deletions

File tree

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

Lines changed: 46 additions & 11 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,
@@ -10,7 +14,7 @@ import { deriveSubagentElapsedMs, formatDuration } from "@t3tools/shared/orchest
1014
import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native";
1115
import * as DateTime from "effect/DateTime";
1216
import * as Haptics from "expo-haptics";
13-
import { useEffect, useState } from "react";
17+
import { useEffect, useMemo, useState } from "react";
1418
import { Platform, Pressable, ScrollView, View } from "react-native";
1519
import { Screen, ScreenStack, ScreenStackHeaderConfig } from "react-native-screens";
1620
import { useSafeAreaInsets } from "react-native-safe-area-context";
@@ -21,6 +25,7 @@ import { useUniwindTheme } from "../../lib/useUniwindTheme";
2125
import { environmentThreadDetails } from "../../state/threads";
2226
import { NativeStackScreenOptions, nativeHeaderScrollEdgeEffects } from "../../native/StackHeader";
2327
import { SubagentRow } from "./SubagentRow";
28+
import { ThreadWorkflowGroup } from "./ThreadWorkflowGroup";
2429
import { MaterialScreenContent } from "../../components/MaterialScreenContent";
2530
import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
2631

@@ -41,6 +46,16 @@ export function ThreadAgentsSheet({ route }: StaticScreenProps<AgentsTarget>) {
4146
const turn = useThreadTurnSubagents(target);
4247
const subagents = turn?.subagents ?? [];
4348
const hasLiveAgent = (turn?.liveCount ?? 0) > 0;
49+
const workflowGroups = useMemo(
50+
() =>
51+
new Map(
52+
deriveWorkflowGroups(projectedSubagentsToRuntime(subagents)).map((group) => [
53+
group.workflow.id,
54+
group,
55+
]),
56+
),
57+
[subagents],
58+
);
4459

4560
const openChildThread = (childThreadId: ThreadId) => {
4661
void Haptics.selectionAsync();
@@ -69,15 +84,35 @@ export function ThreadAgentsSheet({ route }: StaticScreenProps<AgentsTarget>) {
6984
No agents in this turn.
7085
</Text>
7186
) : (
72-
subagents.map((subagent) => (
73-
<AgentRow
74-
key={subagent.id}
75-
subagent={subagent}
76-
environmentId={target.environmentId}
77-
tickSeconds={hasLiveAgent}
78-
onOpen={openChildThread}
79-
/>
80-
))
87+
subagents.map((subagent) => {
88+
const group = workflowGroups.get(subagent.id);
89+
const row = (
90+
<AgentRow
91+
subagent={
92+
subagent.workflow?.name === undefined
93+
? subagent
94+
: { ...subagent, title: subagent.workflow.name }
95+
}
96+
environmentId={target.environmentId}
97+
tickSeconds={hasLiveAgent}
98+
onOpen={openChildThread}
99+
/>
100+
);
101+
return group === undefined ? (
102+
<View key={subagent.id} className="border-b border-border">
103+
{row}
104+
</View>
105+
) : (
106+
<ThreadWorkflowGroup
107+
key={subagent.id}
108+
environmentId={target.environmentId}
109+
coordinator={subagent}
110+
group={group}
111+
header={row}
112+
onOpen={openChildThread}
113+
/>
114+
);
115+
})
81116
)}
82117
</ScrollView>
83118
);
@@ -135,7 +170,7 @@ function AgentRow(props: {
135170
const childThreadId = subagent.childThreadId;
136171

137172
const row = (
138-
<View className="border-b border-border py-3.5">
173+
<View className="py-3.5">
139174
<SubagentRow
140175
environmentId={props.environmentId}
141176
subagent={subagent}
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
import { useAtomValue } from "@effect/atom-react";
2+
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
3+
import {
4+
isActiveSubagentStatus,
5+
workflowPhaseElapsed,
6+
workflowPhaseStatus,
7+
type RuntimeSubagent,
8+
type SubagentWorkflowGroup,
9+
} from "@t3tools/client-runtime/state/subagentRuntime";
10+
import {
11+
NodeId,
12+
ThreadId,
13+
type EnvironmentId,
14+
type OrchestrationV2Subagent,
15+
} from "@t3tools/contracts";
16+
import { deriveSubagentElapsedMs, formatDuration } from "@t3tools/shared/orchestrationTiming";
17+
import { useState, type ReactNode } from "react";
18+
import { Alert, Pressable, View } from "react-native";
19+
20+
import { AppActivityIndicator } from "../../components/AppActivityIndicator";
21+
import { AppText as Text } from "../../components/AppText";
22+
import { SymbolView } from "../../components/AppSymbol";
23+
import { cn } from "../../lib/cn";
24+
import { useThreadShell } from "../../state/entities";
25+
import { orchestrationEnvironment } from "../../state/orchestration";
26+
import { useAtomCommand } from "../../state/use-atom-command";
27+
import { subagentCardDetail } from "./subagent-card-presentation";
28+
import { SUBAGENT_TONE_TEXT_CLASS, SubagentStatusDot } from "./SubagentStatusDot";
29+
import { resolveSubagentRowPresentation, workflowGroupSummary } from "./threadAgentsPresentation";
30+
import { useVisibleSecondClock } from "./use-visible-second-clock";
31+
32+
export function ThreadWorkflowGroup(props: {
33+
readonly environmentId: EnvironmentId;
34+
readonly coordinator: OrchestrationV2Subagent;
35+
readonly group: SubagentWorkflowGroup;
36+
readonly header: ReactNode;
37+
readonly onOpen: (threadId: ThreadId) => void;
38+
}) {
39+
const { coordinator, group } = props;
40+
const [expanded, setExpanded] = useState(() => isActiveSubagentStatus(group.workflow.status));
41+
const [phaseOpen, setPhaseOpen] = useState<ReadonlyMap<number, boolean>>(() => new Map());
42+
const label = group.workflow.workflowName ?? group.workflow.title;
43+
const summary = workflowGroupSummary(group);
44+
const phases = group.phases.filter((phase) => phase.members.length > 0);
45+
46+
const canOperate = useAtomValue(
47+
orchestrationEnvironment.stopWorkflow.permissionAtom(props.environmentId),
48+
);
49+
const stopWorkflow = useAtomCommand(orchestrationEnvironment.stopWorkflow);
50+
const [stopping, setStopping] = useState(false);
51+
const canStop =
52+
canOperate &&
53+
group.workflow.status === "running" &&
54+
coordinator.origin === "provider_native" &&
55+
coordinator.driver === "claudeAgent";
56+
const confirmStop = () =>
57+
Alert.alert("Stop workflow?", `Every running agent in ${label} stops.`, [
58+
{ text: "Cancel", style: "cancel" },
59+
{
60+
text: "Stop",
61+
style: "destructive",
62+
onPress: async () => {
63+
setStopping(true);
64+
const result = await stopWorkflow({
65+
environmentId: props.environmentId,
66+
input: { threadId: coordinator.threadId, subagentId: NodeId.make(coordinator.id) },
67+
});
68+
setStopping(false);
69+
if (result._tag === "Failure") Alert.alert("Could not stop workflow");
70+
},
71+
},
72+
]);
73+
74+
return (
75+
<View className="border-b border-border pb-2">
76+
{props.header}
77+
<View className="-mt-2 flex-row items-center gap-2">
78+
<Pressable
79+
accessibilityRole="button"
80+
accessibilityLabel={`${expanded ? "Collapse" : "Expand"} ${label}, ${summary}`}
81+
accessibilityState={{ expanded }}
82+
onPress={() => setExpanded(!expanded)}
83+
className="min-h-11 min-w-0 flex-1 flex-row items-center gap-3 rounded-lg active:opacity-70"
84+
>
85+
<View className="w-2 items-center">
86+
<SymbolView
87+
name={expanded ? "chevron.down" : "chevron.right"}
88+
size={11}
89+
tintColorClassName="accent-icon-subtle"
90+
/>
91+
</View>
92+
<Text numberOfLines={1} className="min-w-0 shrink text-xs text-foreground-muted">
93+
{summary}
94+
</Text>
95+
</Pressable>
96+
{canStop ? (
97+
<Pressable
98+
accessibilityRole="button"
99+
accessibilityLabel={`Stop workflow ${label}`}
100+
accessibilityState={{ disabled: stopping, busy: stopping }}
101+
disabled={stopping}
102+
hitSlop={8}
103+
onPress={confirmStop}
104+
className="min-h-9 flex-row items-center gap-1.5 rounded-full bg-subtle px-3 active:opacity-70 disabled:opacity-50"
105+
>
106+
{stopping ? (
107+
<AppActivityIndicator size="small" />
108+
) : (
109+
<SymbolView
110+
name="stop.fill"
111+
size={10}
112+
tintColorClassName="accent-danger-foreground"
113+
/>
114+
)}
115+
<Text className="font-t3-medium text-xs text-danger-foreground">Stop</Text>
116+
</Pressable>
117+
) : null}
118+
</View>
119+
{expanded ? (
120+
<View className="ml-[3px] border-l border-border pl-3.5">
121+
{phases.map((phase) => {
122+
const open = phaseOpen.get(phase.index) ?? phase.state === "running";
123+
const status = workflowPhaseStatus(phase);
124+
const count = `${phase.settledCount}/${phase.members.length} ${phase.members.length === 1 ? "agent" : "agents"}`;
125+
return (
126+
<View key={phase.index}>
127+
<Pressable
128+
accessibilityRole="button"
129+
accessibilityLabel={`${phase.title} phase, ${status}, ${count} done`}
130+
accessibilityState={{ expanded: open }}
131+
onPress={() =>
132+
setPhaseOpen((current) => new Map(current).set(phase.index, !open))
133+
}
134+
className="min-h-11 flex-row items-center gap-2 active:opacity-70"
135+
>
136+
<SymbolView
137+
name={open ? "chevron.down" : "chevron.right"}
138+
size={10}
139+
tintColorClassName="accent-icon-subtle"
140+
/>
141+
<Text
142+
numberOfLines={1}
143+
className={cn(
144+
"min-w-0 shrink font-t3-medium text-xs",
145+
phase.state === "running" ? "text-foreground" : "text-foreground-muted",
146+
)}
147+
>
148+
{phase.title}
149+
</Text>
150+
{status === "failed" ? (
151+
<Text
152+
className={cn("text-2xs font-t3-medium", SUBAGENT_TONE_TEXT_CLASS.failed)}
153+
>
154+
Failed
155+
</Text>
156+
) : null}
157+
<View className="flex-1" />
158+
<Text className="text-2xs tabular-nums text-foreground-muted">{count}</Text>
159+
<WorkflowElapsed timing={workflowPhaseElapsed(phase)} />
160+
</Pressable>
161+
{open
162+
? phase.members.map((member) => (
163+
<WorkflowMemberRow
164+
key={member.id}
165+
environmentId={props.environmentId}
166+
member={member}
167+
onOpen={props.onOpen}
168+
/>
169+
))
170+
: null}
171+
</View>
172+
);
173+
})}
174+
{group.unphasedMembers.map((member) => (
175+
<WorkflowMemberRow
176+
key={member.id}
177+
environmentId={props.environmentId}
178+
member={member}
179+
onOpen={props.onOpen}
180+
/>
181+
))}
182+
</View>
183+
) : null}
184+
</View>
185+
);
186+
}
187+
188+
function WorkflowMemberRow(props: {
189+
readonly environmentId: EnvironmentId;
190+
readonly member: RuntimeSubagent;
191+
readonly onOpen: (threadId: ThreadId) => void;
192+
}) {
193+
const { member } = props;
194+
const threadId = member.childThreadId === null ? null : ThreadId.make(member.childThreadId);
195+
const shell = useThreadShell(
196+
threadId === null ? null : scopeThreadRef(props.environmentId, threadId),
197+
);
198+
const canOpen = threadId !== null && shell !== null;
199+
const presentation = resolveSubagentRowPresentation({
200+
title: member.title,
201+
prompt: "",
202+
status: member.status,
203+
progress: member.progress ?? undefined,
204+
result: member.result ?? member.error,
205+
childThreadId: threadId,
206+
});
207+
const detail = subagentCardDetail(presentation.detail);
208+
return (
209+
<Pressable
210+
accessibilityRole={canOpen ? "link" : undefined}
211+
accessibilityLabel={`${presentation.title}, ${presentation.statusLabel}`}
212+
accessibilityHint={canOpen ? "Opens this agent's thread" : "Its thread is not available yet"}
213+
disabled={!canOpen}
214+
onPress={() => threadId !== null && props.onOpen(threadId)}
215+
className="min-h-11 flex-row gap-2.5 py-2 active:opacity-70"
216+
>
217+
<View className="h-5 justify-center">
218+
<SubagentStatusDot tone={presentation.tone} />
219+
</View>
220+
<View className="min-w-0 flex-1 gap-0.5">
221+
<View className="min-h-5 flex-row items-center gap-2">
222+
<Text numberOfLines={1} className="min-w-0 flex-1 text-sm text-foreground">
223+
{presentation.title}
224+
</Text>
225+
<Text className={cn("shrink-0 text-xs", SUBAGENT_TONE_TEXT_CLASS[presentation.tone])}>
226+
{presentation.statusLabel}
227+
</Text>
228+
<WorkflowElapsed timing={member} />
229+
{canOpen ? (
230+
<SymbolView name="chevron.right" size={12} tintColorClassName="accent-icon-subtle" />
231+
) : null}
232+
</View>
233+
{detail ? (
234+
<Text numberOfLines={2} className="text-xs text-foreground-muted">
235+
{detail}
236+
</Text>
237+
) : null}
238+
</View>
239+
</Pressable>
240+
);
241+
}
242+
243+
function WorkflowElapsed({
244+
timing,
245+
}: {
246+
readonly timing: Parameters<typeof deriveSubagentElapsedMs>[0];
247+
}) {
248+
const nowMs = useVisibleSecondClock(isActiveSubagentStatus(timing.status));
249+
const elapsedMs = deriveSubagentElapsedMs(timing, nowMs);
250+
return elapsedMs === null || elapsedMs === 0 ? null : (
251+
<Text className="shrink-0 text-xs tabular-nums text-foreground-muted">
252+
{formatDuration(elapsedMs)}
253+
</Text>
254+
);
255+
}

‎apps/mobile/src/features/threads/threadAgentsPresentation.test.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { resolveSubagentRowPresentation } from "./threadAgentsPresentation";
3+
import { resolveSubagentRowPresentation, workflowGroupSummary } from "./threadAgentsPresentation";
44

55
const base = {
66
title: null,
@@ -76,3 +76,20 @@ describe("resolveSubagentRowPresentation", () => {
7676
expect(row.statusLabel).toBe("Failed");
7777
});
7878
});
79+
80+
type Group = Parameters<typeof workflowGroupSummary>[0];
81+
82+
const members = (...statuses: ReadonlyArray<string>) => statuses.map((status) => ({ status }));
83+
84+
describe("workflowGroupSummary", () => {
85+
it("summarizes phases and settled agents", () => {
86+
const group = {
87+
phases: [{ members: members("completed", "failed") }, { members: [] }],
88+
unphasedMembers: members("running"),
89+
} as unknown as Group;
90+
expect(workflowGroupSummary(group)).toBe("1 phase · 2/3 agents done");
91+
expect(workflowGroupSummary({ phases: [], unphasedMembers: [] } as unknown as Group)).toBe(
92+
"No agents yet",
93+
);
94+
});
95+
});

0 commit comments

Comments
 (0)