Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { StackActions, useNavigation } from "@react-navigation/native";
3import { scopeThreadRef } from "@t3tools/client-runtime/environment";
4import {
5 resolveProviderInstanceDisplayName,
6 shouldShowInstanceBadge,
7} from "@t3tools/client-runtime/state/provider-instance-display";
8import { summarizeSubagentStatuses } from "@t3tools/client-runtime/state/subagent-display";
9import { isActiveSubagentStatus } from "@t3tools/client-runtime/state/subagentRuntime";
10import type {
11 EnvironmentId,
12 OrchestrationV2Subagent,
13 OrchestrationV2TurnItem,
14 ServerConfig,
15} from "@t3tools/contracts";
16import { Pressable, View, type ColorValue } from "react-native";
17
18import { AppText as Text } from "../../components/ui/AppText";
19import { SymbolView } from "../../components/ui/AppSymbol";
20import { ProviderIcon, ProviderInstanceIcon } from "../../components/ProviderIcon";
21import { cn } from "../../lib/cn";
22import type { ThreadFeedActivity } from "../../lib/threadActivity";
23import { serverEnvironment } from "../../state/server";
24import { environmentThreadDetails } from "../../state/threads";
25import { subagentCardElapsed } from "./subagent-card-presentation";
26import { useEnvironments } from "../../state/environments";
27import { useServerConfigs } from "../../state/entities";
28import { SubagentRow } from "./SubagentRow";
29import { useVisibleSecondClock } from "./use-visible-second-clock";
30import { subagentThreadTarget } from "./threadAgentsPresentation";
31import { WorkLogBlock } from "./work-log-layout";
32
33import { useOpenThreadInspector } from "./thread-inspector-navigation";
34
35type SubagentItem = Extract<OrchestrationV2TurnItem, { type: "subagent" }>;
36type AgentTiming = Pick<OrchestrationV2Subagent, "status" | "startedAt" | "completedAt">;
37
38function SubagentElapsed({ agents }: { readonly agents: ReadonlyArray<AgentTiming> }) {
39 const live = agents.some((agent) => isActiveSubagentStatus(agent.status));
40 const nowMs = useVisibleSecondClock(live);
41 const elapsed = subagentCardElapsed(agents, nowMs);
42 return elapsed ? (
43 <Text className="shrink-0 text-xs tabular-nums text-foreground-muted">{elapsed}</Text>
44 ) : null;
45}
46
47function SubagentAvatar(props: {
48 readonly item: SubagentItem;
49 readonly config: ServerConfig | null | undefined;
50}) {
51 const providers = props.config?.providers ?? [];
52 const provider = providers.find(
53 (candidate) => candidate.instanceId === props.item.providerInstanceId,
54 );
55 // A remote task's own driver is only a "remote" label.
56 const driver =
57 provider?.driver ?? (props.item.remoteChild === undefined ? props.item.driver : undefined);
58 const badgeEntries = providers.map((candidate) => {
59 const rawConfig = props.config?.settings.providerInstances[candidate.instanceId]?.config;
60 const instanceConfig =
61 rawConfig !== null && typeof rawConfig === "object"
62 ? (rawConfig as Readonly<Record<string, unknown>>)
63 : null;
64 const agentId = instanceConfig?.source === "local" ? undefined : instanceConfig?.agentId;
65 return {
66 ...candidate,
67 driverKind: candidate.driver,
68 acpRegistryAgentId: typeof agentId === "string" ? agentId.trim() || undefined : undefined,
69 };
70 });
71 const badgeEntry = badgeEntries.find(
72 (candidate) => candidate.instanceId === props.item.providerInstanceId,
73 );
74 const showBadge =
75 provider !== undefined &&
76 badgeEntry !== undefined &&
77 shouldShowInstanceBadge(badgeEntry, badgeEntries);
78 return (
79 <View
80 accessible={false}
81 className="h-7 w-7 shrink-0 items-center justify-center rounded-full border border-border bg-card"
82 >
83 {showBadge ? (
84 <ProviderInstanceIcon
85 provider={provider.driver}
86 iconUrl={provider.iconUrl}
87 size={15}
88 displayName={resolveProviderInstanceDisplayName(provider)}
89 accentColor={provider.accentColor}
90 showBadge
91 />
92 ) : driver === undefined ? (
93 <SymbolView name="sparkles" size={13} tintColorClassName="accent-icon-muted" />
94 ) : (
95 <ProviderIcon provider={driver} iconUrl={provider?.iconUrl} size={15} />
96 )}
97 </View>
98 );
99}
100
101export function ThreadSubagentGroup(props: {
102 readonly activities: ReadonlyArray<ThreadFeedActivity>;
103 readonly environmentId: EnvironmentId;
104 readonly anchorKey: string;
105 readonly expandedRows: Readonly<Record<string, boolean>>;
106 readonly iconSubtleColor: ColorValue;
107 readonly onToggleRow: (rowId: string, anchorKey: string) => void;
108}) {
109 const config = useAtomValue(serverEnvironment.configValueAtom(props.environmentId));
110 const navigation = useNavigation();
111 const openInspector = useOpenThreadInspector();
112 const members = props.activities.flatMap(({ projectedItem }) =>
113 projectedItem.item.type === "subagent" ? [projectedItem.item] : [],
114 );
115 const liveAgents = useAtomValue(
116 environmentThreadDetails.threadAtom(scopeThreadRef(props.environmentId, members[0]!.threadId)),
117 (thread) => thread?.projection.subagents,
118 );
119 const { presentationById } = useEnvironments();
120 const agents = members.map((item) => {
121 const live = liveAgents?.find((agent) => agent.id === item.subagentId);
122 return {
123 ...item,
124 item,
125 status: live?.status ?? item.status,
126 startedAt: live?.startedAt ?? item.startedAt,
127 completedAt: live?.completedAt ?? item.completedAt,
128 result: live?.result ?? item.result,
129 progress: live?.progress ?? item.progress,
130 model: live?.model ?? null,
131 };
132 });
133 const grouped = agents.length > 1;
134 const label = `${agents.length} subagents`;
135 const summary = summarizeSubagentStatuses(agents.map((agent) => agent.status));
136 const expanded = props.expandedRows[props.anchorKey] ?? false;
137 const serverConfigs = useServerConfigs();
138 // A task in a linked environment runs a provider instance of that environment.
139 const configOf = (item: SubagentItem) =>
140 item.remoteChild === undefined ? config : serverConfigs.get(item.remoteChild.environmentId);
141 return (
142 <WorkLogBlock>
143 {grouped ? (
144 <Pressable
145 accessibilityRole="button"
146 accessibilityLabel={`${label}, ${summary}`}
147 accessibilityState={{ expanded }}
148 onPress={() => props.onToggleRow(props.anchorKey, props.anchorKey)}
149 className="min-h-14 flex-row items-center gap-3 rounded-lg py-2 active:bg-subtle"
150 >
151 <View className="flex-row items-center">
152 {agents.slice(0, 3).map((agent, index) => (
153 <View key={agent.item.id} style={{ marginLeft: index === 0 ? 0 : -7 }}>
154 <SubagentAvatar item={agent.item} config={configOf(agent.item)} />
155 </View>
156 ))}
157 {agents.length > 3 ? (
158 <View className="-ml-2 h-7 w-7 items-center justify-center rounded-full border border-border bg-card">
159 <Text className="text-2xs text-foreground-muted">+{agents.length - 3}</Text>
160 </View>
161 ) : null}
162 </View>
163 <View className="min-w-0 flex-1 gap-0.5">
164 <Text numberOfLines={1} className="font-t3-medium text-sm text-foreground">
165 {label}
166 </Text>
167 <Text
168 numberOfLines={1}
169 className={cn(
170 "text-2xs text-foreground-muted",
171 agents.some((agent) => isActiveSubagentStatus(agent.status))
172 ? "text-adaptive-sky-600-400"
173 : agents.some((agent) => agent.status === "failed") &&
174 "text-adaptive-rose-600-400",
175 )}
176 >
177 {summary}
178 </Text>
179 </View>
180 <SubagentElapsed agents={agents} />
181 <SymbolView
182 name={expanded ? "chevron.up" : "chevron.down"}
183 size={11}
184 tintColor={props.iconSubtleColor}
185 />
186 </Pressable>
187 ) : null}
188 {!grouped || expanded ? (
189 <View className="mb-1 gap-px rounded-xl border border-border bg-card/30 p-1">
190 {agents.map((agent) => {
191 // A task in a linked environment opens there, if this app is connected to it.
192 const target = subagentThreadTarget(agent, props.environmentId, (id) =>
193 presentationById.has(id),
194 );
195 return (
196 <Pressable
197 key={agent.item.id}
198 accessible
199 accessibilityRole={target === null ? undefined : "link"}
200 accessibilityHint={
201 target !== null
202 ? "Opens this agent's thread"
203 : agent.remoteChild !== undefined
204 ? `Runs on ${agent.remoteChild.label}`
205 : "Provider-managed agent"
206 }
207 disabled={target === null}
208 onPress={() => {
209 if (target === null) return;
210 if (
211 openInspector(
212 { environmentId: props.environmentId, threadId: members[0]!.threadId },
213 {
214 type: "thread",
215 environmentId: target.environmentId,
216 threadId: target.threadId,
217 },
218 )
219 )
220 return;
221 navigation.dispatch(
222 StackActions.push("Thread", {
223 environmentId: String(target.environmentId),
224 threadId: String(target.threadId),
225 }),
226 );
227 }}
228 className="rounded-lg px-3 py-3 active:bg-subtle"
229 >
230 <SubagentRow
231 environmentId={props.environmentId}
232 subagent={agent}
233 canOpenRemote={target !== null}
234 elapsed={<SubagentElapsed agents={[agent]} />}
235 />
236 </Pressable>
237 );
238 })}
239 </View>
240 ) : null}
241 </WorkLogBlock>
242 );
243}