Open History to see the last commit
1import * as Dialog from "../../components/ui/Dialog";
2import { useAtomValue } from "@effect/atom-react";
3import { scopeThreadRef } from "@t3tools/client-runtime/environment";
4import {
5 resolveHandoffEndpoints,
6 resolveModelSwitchChange,
7 type HandoffEndpoints,
8} from "@t3tools/client-runtime/handoff";
9import { resolveModelShortName } from "@t3tools/client-runtime/provider-clients";
10import {
11 normalizeProviderAccentColor,
12 resolveProviderInstanceDisplayName,
13 shouldShowInstanceBadge,
14} from "@t3tools/client-runtime/state/provider-instance-display";
15import { resolveEnvironmentMachineKind } from "@t3tools/contracts";
16import type {
17 EnvironmentId,
18 ModelSelection,
19 OrchestrationV2ModelSwitch,
20 OrchestrationV2ProjectedTurnItem,
21 ProviderInstanceId,
22 ServerProvider,
23} from "@t3tools/contracts";
24import { Atom } from "effect/reactivity";
25import { useGT } from "gt-react-native";
26import { useMemo } from "react";
27import { Pressable, View, type ColorValue } from "react-native";
28import { AppText as Text } from "../../components/ui/AppText";
29import { SymbolView } from "../../components/ui/AppSymbol";
30import { ProviderIcon, ProviderInstanceIcon } from "../../components/ProviderIcon";
31import { environmentThreadDetails } from "../../state/threads";
32import { EnvironmentMachineSymbol } from "../../components/EnvironmentMachineSymbol";
33import { useEnvironments } from "../../state/environments";
34import { serverEnvironment } from "../../state/server";
35import { ThreadContextDivider } from "./thread-context-divider";
36
37type HandoffProvider = ServerProvider & {
38 readonly driverKind: ServerProvider["driver"];
39 readonly acpRegistryAgentId: string | undefined;
40};
41
42function handoffEndpointsAtom(
43 environmentId: EnvironmentId,
44 { item, sourceThreadId }: OrchestrationV2ProjectedTurnItem,
45) {
46 let previous: ReturnType<typeof resolveHandoffEndpoints> | null = null;
47 const ref = scopeThreadRef(environmentId, sourceThreadId);
48 return Atom.make((get) => {
49 if (item.type !== "handoff") return null;
50 const projection = get(environmentThreadDetails.threadAtom(ref))?.projection;
51 const next = resolveHandoffEndpoints(item, projection?.runs ?? []);
52 // Run activity changes frequently; only endpoint changes affect this row.
53 if (JSON.stringify(next) !== JSON.stringify(previous)) previous = next;
54 return previous;
55 });
56}
57
58/**
59 * Where a thread that moved from another machine picks up; what is above it happened there.
60 * Drawn as a context handoff is, with the two machines in place of the two models.
61 */
62export function ThreadMovedDivider(props: {
63 fromEnvironmentId: EnvironmentId | undefined;
64 toEnvironmentId: EnvironmentId;
65 iconColor: ColorValue;
66}) {
67 const gt = useGT();
68 const { environments } = useEnvironments();
69 const find = (id: EnvironmentId | undefined) =>
70 environments.find((environment) => environment.environmentId === id);
71 const from = find(props.fromEnvironmentId);
72 const to = find(props.toEnvironmentId);
73 const endpoint = (environment: NonNullable<typeof from>) => (
74 <View className="max-w-full flex-row items-center gap-1.5">
75 <EnvironmentMachineSymbol
76 kind={resolveEnvironmentMachineKind(environment.serverConfig)}
77 size={12}
78 tintColorClassName="accent-foreground-muted"
79 />
80 <Text numberOfLines={1} className="shrink font-t3-medium text-xs text-foreground-muted">
81 {environment.label}
82 </Text>
83 </View>
84 );
85 const arrives = to !== undefined && to.environmentId !== from?.environmentId ? to : null;
86 return (
87 // Imported messages above have no footer row, so the divider brings its own space.
88 <View className="mt-3">
89 <ThreadContextDivider
90 icon="arrow.up.right"
91 iconColor={props.iconColor}
92 label={from !== undefined && arrives === null ? gt`Thread moved from` : gt`Thread moved`}
93 >
94 <View className="flex-row flex-wrap items-center justify-center gap-1.5">
95 {from === undefined ? (
96 <Text className="text-xs text-foreground-muted">{gt`from another machine`}</Text>
97 ) : (
98 endpoint(from)
99 )}
100 {arrives !== null ? (
101 <>
102 <SymbolView name="arrow.right" size={12} tintColor={props.iconColor} />
103 {endpoint(arrives)}
104 </>
105 ) : null}
106 </View>
107 </ThreadContextDivider>
108 </View>
109 );
110}
111
112/** A same-harness model or effort change. Shows only what changed. */
113export function ThreadModelSwitchDivider(props: {
114 environmentId: EnvironmentId;
115 modelSwitch: OrchestrationV2ModelSwitch;
116 iconColor: ColorValue;
117}) {
118 const providers = useAtomValue(serverEnvironment.providersValueAtom(props.environmentId)) ?? [];
119 const change = resolveModelSwitchChange(props.modelSwitch, providers);
120 if (change === null) return null;
121 const describe = (selection: ModelSelection, effort: string | undefined) => {
122 const provider = providers.find((candidate) => candidate.instanceId === selection.instanceId);
123 const model = provider?.models.find((candidate) => candidate.slug === selection.model);
124 const modelName =
125 (model ? resolveModelShortName(model, provider?.driver) : undefined) ??
126 model?.name ??
127 selection.model;
128 return [change.modelChanged ? modelName : null, effort].filter(Boolean).join(" ");
129 };
130 const { from, to } = props.modelSwitch;
131 return (
132 <ThreadContextDivider
133 label={`${describe(from, change.effort?.from)} → ${describe(to, change.effort?.to)}`}
134 iconColor={props.iconColor}
135 />
136 );
137}
138
139export function ThreadHandoffRow(props: {
140 environmentId: EnvironmentId;
141 projectedItem: OrchestrationV2ProjectedTurnItem;
142 iconColor: ColorValue;
143 surfaceColor: string;
144}) {
145 const gt = useGT();
146 const { item } = props.projectedItem;
147 const endpointsAtom = useMemo(
148 () => handoffEndpointsAtom(props.environmentId, props.projectedItem),
149 [props.environmentId, props.projectedItem],
150 );
151 const endpoints = useAtomValue(endpointsAtom);
152 if (item.type !== "handoff" || endpoints === null) return null;
153 const color = item.status === "failed" ? "#e11d48" : props.iconColor;
154 return (
155 <ThreadHandoffDivider
156 environmentId={props.environmentId}
157 endpoints={endpoints}
158 iconColor={color}
159 surfaceColor={props.surfaceColor}
160 label={gt`Context handoff`}
161 failed={item.status === "failed"}
162 />
163 );
164}
165
166export function ThreadHandoffDivider(props: {
167 environmentId: EnvironmentId;
168 endpoints: HandoffEndpoints;
169 iconColor: ColorValue;
170 surfaceColor: string;
171 label: string;
172 failed?: boolean;
173}) {
174 const config = useAtomValue(serverEnvironment.configValueAtom(props.environmentId));
175 const { endpoints } = props;
176 const endpointProps = {
177 providers: (config?.providers ?? []).map((provider) => {
178 const rawConfig = config?.settings.providerInstances[provider.instanceId]?.config;
179 const instanceConfig =
180 rawConfig !== null && typeof rawConfig === "object"
181 ? (rawConfig as Readonly<Record<string, unknown>>)
182 : null;
183 const agentId = instanceConfig?.source === "local" ? undefined : instanceConfig?.agentId;
184 return {
185 ...provider,
186 driverKind: provider.driver,
187 accentColor: normalizeProviderAccentColor(provider.accentColor),
188 acpRegistryAgentId: typeof agentId === "string" ? agentId.trim() || undefined : undefined,
189 };
190 }),
191 surfaceColor: props.surfaceColor,
192 };
193 return (
194 <ThreadContextDivider
195 label={props.label}
196 icon="arrow.left.arrow.right"
197 iconColor={props.iconColor}
198 failed={props.failed}
199 >
200 <View className="flex-row flex-wrap items-center justify-center gap-1.5">
201 {endpoints.from.map((endpoint, index) => (
202 <View
203 key={`${endpoint.instanceId}:${endpoint.model ?? ""}`}
204 className="max-w-full flex-row items-center"
205 >
206 <HandoffEndpoint {...endpoint} {...endpointProps} />
207 {index < endpoints.from.length - 1 ? (
208 <Text accessible={false} className="text-xs text-foreground-muted">
209 ,
210 </Text>
211 ) : null}
212 </View>
213 ))}
214 {endpoints.from.length > 0 ? (
215 <SymbolView name="arrow.right" size={12} tintColor={props.iconColor} />
216 ) : null}
217 <HandoffEndpoint {...endpoints.to} {...endpointProps} />
218 </View>
219 </ThreadContextDivider>
220 );
221}
222
223function HandoffEndpoint(props: {
224 instanceId: ProviderInstanceId;
225 model?: string | undefined;
226 providers: ReadonlyArray<HandoffProvider>;
227 surfaceColor: string;
228}) {
229 const gt = useGT();
230 const provider = props.providers.find((candidate) => candidate.instanceId === props.instanceId);
231 const model = provider?.models.find((candidate) => candidate.slug === props.model);
232 const label =
233 (model ? resolveModelShortName(model, provider?.driver) : undefined) ??
234 model?.name ??
235 props.model ??
236 (provider ? resolveProviderInstanceDisplayName(provider) : props.instanceId);
237 const showBadge = provider !== undefined && shouldShowInstanceBadge(provider, props.providers);
238 return (
239 <Pressable
240 accessibilityRole="button"
241 accessibilityLabel={label}
242 accessibilityHint={gt`Show provider account`}
243 className="min-h-6 max-w-full flex-row items-center justify-center gap-1.5"
244 hitSlop={{ top: 8, bottom: 8 }}
245 onPress={() =>
246 Dialog.alert(
247 label,
248 provider ? resolveProviderInstanceDisplayName(provider) : props.instanceId,
249 )
250 }
251 >
252 {showBadge && provider !== undefined ? (
253 <ProviderInstanceIcon
254 provider={provider.driver}
255 iconUrl={provider.iconUrl}
256 size={14}
257 displayName={resolveProviderInstanceDisplayName(provider)}
258 accentColor={provider.accentColor}
259 showBadge
260 surfaceColor={props.surfaceColor}
261 />
262 ) : (
263 <ProviderIcon
264 provider={provider?.driver ?? props.instanceId}
265 iconUrl={provider?.iconUrl}
266 size={12}
267 />
268 )}
269 <Text numberOfLines={1} className="shrink text-xs text-foreground-muted">
270 {label}
271 </Text>
272 </Pressable>
273 );
274}