Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import type { ThreadTurnSubagents } from "@t3tools/client-runtime/state/thread-subagents";
3import {
4 isOrchestrationV2WorkActive,
5 type EnvironmentId,
6 type OrchestrationV2Subagent,
7 type ThreadId,
8} from "@t3tools/contracts";
9import { deriveSubagentElapsedMs, formatDuration } from "@t3tools/shared/orchestrationTiming";
10import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native";
11import * as DateTime from "effect/DateTime";
12import { Atom } from "effect/reactivity";
13import * as Haptics from "expo-haptics";
14import { useEffect, useState } from "react";
15import { Platform, Pressable, ScrollView, View } from "react-native";
16import { ScreenStack } from "react-native-screens";
17import { useSafeAreaInsets } from "react-native-safe-area-context";
18
19import { AndroidSheetHeader } from "../../components/ui/AndroidScreenHeader";
20import { AppText as Text } from "../../components/ui/AppText";
21import { environmentThreadDetails } from "../../state/threads";
22import { NativeStackScreenOptions, nativeHeaderScrollEdgeEffects } from "../../native/StackHeader";
23import { NativeHeader, NativeScreen } from "../../native/ThemedScreenStack";
24import { SubagentRow } from "./SubagentRow";
25import { ScreenContent } from "../../components/ui/ScreenContent";
26import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
27
28const HEADER_SCROLL_EDGE_EFFECTS = nativeHeaderScrollEdgeEffects(Platform.OS, Platform.Version);
29
30type AgentsTarget = { readonly environmentId: EnvironmentId; readonly threadId: ThreadId };
31
32const EMPTY_TURN_SUBAGENTS_ATOM = Atom.make<ThreadTurnSubagents | null>(null).pipe(
33 Atom.withLabel("mobile-thread-turn-subagents:empty"),
34);
35
36export function useThreadTurnSubagents(target: AgentsTarget | null): ThreadTurnSubagents | null {
37 return useAtomValue(
38 target === null
39 ? EMPTY_TURN_SUBAGENTS_ATOM
40 : environmentThreadDetails.turnSubagentsAtom(target),
41 );
42}
43
44export function ThreadAgentsSheet({ route }: StaticScreenProps<AgentsTarget>) {
45 const { layout } = useAdaptiveWorkspaceLayout();
46 const target = route.params;
47 const navigation = useNavigation();
48 const insets = useSafeAreaInsets();
49 const turn = useThreadTurnSubagents(target);
50 const subagents = turn?.subagents ?? [];
51 const hasLiveAgent = (turn?.liveCount ?? 0) > 0;
52
53 const openChildThread = (childThreadId: ThreadId) => {
54 void Haptics.selectionAsync();
55 // Replace rather than push: the sheet is a leaf, and the child thread
56 // belongs in the workspace stack where Home's back button expects it.
57 navigation.dispatch(
58 StackActions.replace("Thread", {
59 environmentId: target.environmentId,
60 threadId: childThreadId,
61 }),
62 );
63 };
64
65 const content = (
66 <ScrollView
67 nestedScrollEnabled
68 className="flex-1"
69 // The iOS header is translucent and floats over this view; UIKit has to
70 // inset the content or the first row sits underneath the title.
71 contentInsetAdjustmentBehavior={Platform.OS === "ios" ? "automatic" : "never"}
72 contentContainerClassName="px-5 pb-6"
73 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 16) + 8 }}
74 >
75 {subagents.length === 0 ? (
76 <Text className="pt-6 text-center text-sm text-foreground-muted">
77 No agents in this turn.
78 </Text>
79 ) : (
80 subagents.map((subagent) => (
81 <AgentRow
82 key={subagent.id}
83 subagent={subagent}
84 environmentId={target.environmentId}
85 tickSeconds={hasLiveAgent}
86 onOpen={openChildThread}
87 />
88 ))
89 )}
90 </ScrollView>
91 );
92
93 if (Platform.OS === "ios") {
94 // A plain formSheet screen never renders a stack header, so it comes from
95 // a nested native stack inside the sheet (same shape as the git sheet).
96 return (
97 <View collapsable={false} className="flex-1 bg-sheet">
98 <ScreenStack style={{ flex: 1 }}>
99 <NativeScreen
100 activityState={2}
101 enabled
102 isNativeStack
103 screenId="thread-agents-sheet-native"
104 scrollEdgeEffects={HEADER_SCROLL_EDGE_EFFECTS}
105 className="flex-1 bg-sheet"
106 >
107 {content}
108 <NativeHeader
109 backgroundColor="rgba(0,0,0,0)"
110 tintColorClassName="accent-foreground"
111 hideBackButton
112 hideShadow={false}
113 title="Agents"
114 titleColorClassName="accent-foreground"
115 titleFontSize={18}
116 titleFontWeight="800"
117 translucent
118 />
119 </NativeScreen>
120 </ScreenStack>
121 </View>
122 );
123 }
124
125 return (
126 <View collapsable={false} className="flex-1 bg-sheet">
127 <NativeStackScreenOptions options={{ sheetCornerRadius: 28 }} />
128 <AndroidSheetHeader title="Agents" onBack={() => navigation.goBack()} />
129 <ScreenContent insetHorizontal={layout.usesSplitView}>{content}</ScreenContent>
130 </View>
131 );
132}
133
134function AgentRow(props: {
135 readonly environmentId: EnvironmentId;
136 readonly subagent: OrchestrationV2Subagent;
137 readonly tickSeconds: boolean;
138 readonly onOpen: (childThreadId: ThreadId) => void;
139}) {
140 const { subagent } = props;
141 const childThreadId = subagent.childThreadId;
142
143 const row = (
144 <View className="border-b border-border py-3.5">
145 <SubagentRow
146 environmentId={props.environmentId}
147 subagent={subagent}
148 elapsed={<AgentElapsed subagent={subagent} tickSeconds={props.tickSeconds} />}
149 />
150 </View>
151 );
152
153 if (childThreadId === null) {
154 return (
155 <View
156 accessible
157 accessibilityHint="Provider-managed agent. Its work appears in the transcript."
158 >
159 {row}
160 </View>
161 );
162 }
163
164 return (
165 <Pressable
166 accessibilityRole="link"
167 accessibilityHint="Opens this agent's thread"
168 onPress={() => props.onOpen(childThreadId)}
169 className="active:opacity-70"
170 >
171 {row}
172 </Pressable>
173 );
174}
175
176function AgentElapsed(props: {
177 readonly subagent: OrchestrationV2Subagent;
178 readonly tickSeconds: boolean;
179}) {
180 const elapsed = useSubagentElapsed(props.subagent, props.tickSeconds);
181 return elapsed === null ? null : (
182 <Text className="shrink-0 text-xs tabular-nums text-foreground-muted">{elapsed}</Text>
183 );
184}
185
186function useSubagentElapsed(
187 subagent: Pick<OrchestrationV2Subagent, "status" | "startedAt" | "completedAt">,
188 tickSeconds: boolean,
189): string | null {
190 const [nowMs, setNowMs] = useState(() => Date.now());
191 const running = isOrchestrationV2WorkActive(subagent.status);
192 useEffect(() => {
193 if (!tickSeconds || !running) return;
194 const intervalId = setInterval(() => setNowMs(Date.now()), 1_000);
195 return () => clearInterval(intervalId);
196 }, [running, tickSeconds]);
197 const elapsedMs = deriveSubagentElapsedMs(
198 {
199 status: subagent.status,
200 startedAt: subagent.startedAt === null ? null : DateTime.formatIso(subagent.startedAt),
201 completedAt: subagent.completedAt === null ? null : DateTime.formatIso(subagent.completedAt),
202 },
203 nowMs,
204 );
205 return elapsedMs === null || elapsedMs === 0 ? null : formatDuration(elapsedMs);
206}