The best way to control Claude Code, Codex and more from any computer or phone

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