Open History to see the last commit
1import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
2import {
3 normalizeProviderAccentColor,
4 resolveProviderInstanceDisplayName,
5 shouldShowInstanceBadge,
6} from "@t3tools/client-runtime/state/provider-instance-display";
7import {
8 resolveSubagentMetadata,
9 subagentCardDetail,
10} from "@t3tools/client-runtime/state/subagent-display";
11import { formatSubagentEffort } from "@t3tools/client-runtime/state/thread-execution";
12import type { EnvironmentId, OrchestrationV2Subagent } from "@t3tools/contracts";
13import type { ReactNode } from "react";
14import { View } from "react-native";
15
16import { AppText as Text } from "../../components/ui/AppText";
17import { SymbolView } from "../../components/ui/AppSymbol";
18import { ProviderIcon, ProviderInstanceIcon } from "../../components/ProviderIcon";
19import { cn } from "../../lib/cn";
20import { useEnvironmentServerConfig, useProject, useThreadShell } from "../../state/entities";
21import { SUBAGENT_TONE_TEXT_CLASS, SubagentStatusDot } from "./SubagentStatusDot";
22import { resolveSubagentRowPresentation } from "./threadAgentsPresentation";
23
24type SubagentRowSubagent = Pick<
25 OrchestrationV2Subagent,
26 | "threadId"
27 | "childThreadId"
28 | "model"
29 | "modelSelection"
30 | "driver"
31 | "providerInstanceId"
32 | "title"
33 | "prompt"
34 | "status"
35 | "progress"
36 | "result"
37 | "remoteChild"
38>;
39
40export function SubagentRow(props: {
41 readonly environmentId: EnvironmentId;
42 readonly subagent: SubagentRowSubagent;
43 readonly elapsed: ReactNode;
44 /** The task runs in a linked environment this app is connected to. */
45 readonly canOpenRemote?: boolean;
46}) {
47 const presentation = resolveSubagentRowPresentation(props.subagent, props.canOpenRemote);
48 const detail = subagentCardDetail(presentation.detail);
49 return (
50 <View className="flex-row gap-3">
51 <View className="h-5 justify-center">
52 <SubagentStatusDot tone={presentation.tone} placement="sheet" />
53 </View>
54 <View className="min-w-0 flex-1 gap-1">
55 <View className="min-h-5 flex-row items-center gap-2">
56 <View className="min-w-0 flex-1 flex-row items-baseline gap-1.5">
57 <Text
58 numberOfLines={1}
59 className="min-w-0 shrink font-t3-medium text-sm text-foreground"
60 >
61 {presentation.title}
62 </Text>
63 <Text
64 accessibilityElementsHidden
65 importantForAccessibility="no"
66 className="shrink-0 text-xs text-foreground-muted"
67 >
68 ·
69 </Text>
70 <Text
71 className={cn(
72 "shrink-0 text-xs font-t3-medium",
73 SUBAGENT_TONE_TEXT_CLASS[presentation.tone],
74 )}
75 >
76 {presentation.statusLabel}
77 </Text>
78 {presentation.runsOn !== null ? (
79 <Text numberOfLines={1} className="shrink text-xs text-foreground-muted">
80 on {presentation.runsOn}
81 </Text>
82 ) : null}
83 </View>
84 {props.elapsed}
85 {presentation.canOpenThread ? (
86 <SymbolView name="chevron.right" size={12} tintColorClassName="accent-icon-subtle" />
87 ) : null}
88 </View>
89 <SubagentMetadata environmentId={props.environmentId} subagent={props.subagent} />
90 {detail ? (
91 <Text
92 numberOfLines={3}
93 className={cn(
94 "text-xs text-foreground-muted",
95 presentation.tone === "failed" && SUBAGENT_TONE_TEXT_CLASS.failed,
96 )}
97 >
98 {detail}
99 </Text>
100 ) : null}
101 </View>
102 </View>
103 );
104}
105
106function SubagentMetadata(props: {
107 readonly environmentId: EnvironmentId;
108 readonly subagent: SubagentRowSubagent;
109}) {
110 const { environmentId, subagent } = props;
111 // A task in a linked environment runs a provider instance of that environment.
112 const config = useEnvironmentServerConfig(subagent.remoteChild?.environmentId ?? environmentId);
113 const providers = (config?.providers ?? []).map((candidate) => {
114 const rawConfig = config?.settings.providerInstances[candidate.instanceId]?.config;
115 const instanceConfig =
116 rawConfig !== null && typeof rawConfig === "object"
117 ? (rawConfig as Readonly<Record<string, unknown>>)
118 : null;
119 const agentId = instanceConfig?.source === "local" ? undefined : instanceConfig?.agentId;
120 return {
121 ...candidate,
122 driverKind: candidate.driver,
123 accentColor: normalizeProviderAccentColor(candidate.accentColor),
124 acpRegistryAgentId: typeof agentId === "string" ? agentId.trim() || undefined : undefined,
125 };
126 });
127 const provider = providers.find(
128 (candidate) => candidate.instanceId === subagent.providerInstanceId,
129 );
130 const showBadge = provider !== undefined && shouldShowInstanceBadge(provider, providers);
131 // A remote task's own driver is only a "remote" label.
132 const driver =
133 provider?.driver ?? (subagent.remoteChild === undefined ? subagent.driver : undefined);
134 const parent = useThreadShell(scopeThreadRef(environmentId, subagent.threadId))?.source;
135 const child = useThreadShell(
136 subagent.childThreadId === null ? null : scopeThreadRef(environmentId, subagent.childThreadId),
137 )?.source;
138 const parentProject = useProject(
139 parent ? scopeProjectRef(environmentId, parent.projectId) : null,
140 );
141 const childProject = useProject(child ? scopeProjectRef(environmentId, child.projectId) : null);
142 const selection = subagent.modelSelection;
143 const { modelLabel, modelSlug, workspace } = resolveSubagentMetadata({
144 model: subagent.model,
145 modelSelection: selection,
146 provider,
147 parentThread: parent,
148 childThread: child,
149 parentProject,
150 childProject,
151 });
152 const effort = formatSubagentEffort({ modelSlug, modelSelection: selection, provider });
153 return (
154 <View className="min-w-0 flex-row items-center gap-1.5">
155 {showBadge ? (
156 <ProviderInstanceIcon
157 provider={provider.driver}
158 iconUrl={provider.iconUrl}
159 size={12}
160 displayName={resolveProviderInstanceDisplayName(provider)}
161 accentColor={provider.accentColor}
162 showBadge
163 />
164 ) : driver === undefined ? (
165 <SymbolView name="robot" size={11} tintColorClassName="accent-icon-muted" />
166 ) : (
167 <ProviderIcon provider={driver} iconUrl={provider?.iconUrl} size={12} />
168 )}
169 <Text className="min-w-0 shrink text-xs text-foreground-muted" numberOfLines={1}>
170 {provider ? `${resolveProviderInstanceDisplayName(provider)} · ` : ""}
171 {modelLabel}
172 {effort ? ` · ${effort}` : ""}
173 </Text>
174 {workspace.map(({ label, value }) => (
175 // Preserve the label against flattening without making it a separate accessibility stop.
176 <View
177 key={label}
178 collapsable={false}
179 accessibilityLabel={`${label}: ${value}`}
180 className="min-w-0 shrink flex-row items-center gap-1.5"
181 >
182 <Text className="text-xs text-foreground-muted">·</Text>
183 <SymbolView
184 name={label === "Branch" ? "arrow.triangle.branch" : "folder"}
185 size={11}
186 tintColorClassName="accent-icon-muted"
187 />
188 <Text className="min-w-0 shrink text-xs text-foreground-muted" numberOfLines={1}>
189 {value}
190 </Text>
191 </View>
192 ))}
193 </View>
194 );
195}