Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SubagentRow.tsx

186 lines6.8 KB

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