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 type {
16 EnvironmentId,
17 ModelSelection,
18 OrchestrationV2ModelSwitch,
19 OrchestrationV2ProjectedTurnItem,
20 ProviderInstanceId,
21 ServerProvider,
22} from "@t3tools/contracts";
23import { Atom } from "effect/reactivity";
24import { useMemo } from "react";
25import { Pressable, View, type ColorValue } from "react-native";
26import { AppText as Text } from "../../components/ui/AppText";
27import { SymbolView } from "../../components/ui/AppSymbol";
28import { ProviderIcon, ProviderInstanceIcon } from "../../components/ProviderIcon";
29import { environmentThreadDetails } from "../../state/threads";
30import { serverEnvironment } from "../../state/server";
31import { ThreadContextDivider } from "./thread-context-divider";
32
33type HandoffProvider = ServerProvider & {
34 readonly driverKind: ServerProvider["driver"];
35 readonly acpRegistryAgentId: string | undefined;
36};
37
38function handoffEndpointsAtom(
39 environmentId: EnvironmentId,
40 { item, sourceThreadId }: OrchestrationV2ProjectedTurnItem,
41) {
42 let previous: ReturnType<typeof resolveHandoffEndpoints> | null = null;
43 const ref = scopeThreadRef(environmentId, sourceThreadId);
44 return Atom.make((get) => {
45 if (item.type !== "handoff") return null;
46 const projection = get(environmentThreadDetails.threadAtom(ref))?.projection;
47 const next = resolveHandoffEndpoints(item, projection?.runs ?? []);
48 // Run activity changes frequently; only endpoint changes affect this row.
49 if (JSON.stringify(next) !== JSON.stringify(previous)) previous = next;
50 return previous;
51 });
52}
53
54/** A same-harness model or effort change. Shows only what changed. */
55export function ThreadModelSwitchDivider(props: {
56 environmentId: EnvironmentId;
57 modelSwitch: OrchestrationV2ModelSwitch;
58 iconColor: ColorValue;
59}) {
60 const providers = useAtomValue(serverEnvironment.providersValueAtom(props.environmentId)) ?? [];
61 const change = resolveModelSwitchChange(props.modelSwitch, providers);
62 if (change === null) return null;
63 const describe = (selection: ModelSelection, effort: string | undefined) => {
64 const provider = providers.find((candidate) => candidate.instanceId === selection.instanceId);
65 const model = provider?.models.find((candidate) => candidate.slug === selection.model);
66 const modelName =
67 (model ? resolveModelShortName(model, provider?.driver) : undefined) ??
68 model?.name ??
69 selection.model;
70 return [change.modelChanged ? modelName : null, effort].filter(Boolean).join(" ");
71 };
72 const { from, to } = props.modelSwitch;
73 return (
74 <ThreadContextDivider
75 label={`${describe(from, change.effort?.from)} → ${describe(to, change.effort?.to)}`}
76 iconColor={props.iconColor}
77 />
78 );
79}
80
81export function ThreadHandoffRow(props: {
82 environmentId: EnvironmentId;
83 projectedItem: OrchestrationV2ProjectedTurnItem;
84 iconColor: ColorValue;
85 surfaceColor: string;
86}) {
87 const { item } = props.projectedItem;
88 const endpointsAtom = useMemo(
89 () => handoffEndpointsAtom(props.environmentId, props.projectedItem),
90 [props.environmentId, props.projectedItem],
91 );
92 const endpoints = useAtomValue(endpointsAtom);
93 if (item.type !== "handoff" || endpoints === null) return null;
94 const color = item.status === "failed" ? "#e11d48" : props.iconColor;
95 return (
96 <ThreadHandoffDivider
97 environmentId={props.environmentId}
98 endpoints={endpoints}
99 iconColor={color}
100 surfaceColor={props.surfaceColor}
101 label="Context handoff"
102 failed={item.status === "failed"}
103 />
104 );
105}
106
107export function ThreadHandoffDivider(props: {
108 environmentId: EnvironmentId;
109 endpoints: HandoffEndpoints;
110 iconColor: ColorValue;
111 surfaceColor: string;
112 label: string;
113 failed?: boolean;
114}) {
115 const config = useAtomValue(serverEnvironment.configValueAtom(props.environmentId));
116 const { endpoints } = props;
117 const endpointProps = {
118 providers: (config?.providers ?? []).map((provider) => {
119 const rawConfig = config?.settings.providerInstances[provider.instanceId]?.config;
120 const instanceConfig =
121 rawConfig !== null && typeof rawConfig === "object"
122 ? (rawConfig as Readonly<Record<string, unknown>>)
123 : null;
124 const agentId = instanceConfig?.source === "local" ? undefined : instanceConfig?.agentId;
125 return {
126 ...provider,
127 driverKind: provider.driver,
128 accentColor: normalizeProviderAccentColor(provider.accentColor),
129 acpRegistryAgentId: typeof agentId === "string" ? agentId.trim() || undefined : undefined,
130 };
131 }),
132 surfaceColor: props.surfaceColor,
133 };
134 return (
135 <ThreadContextDivider
136 label={props.label}
137 icon="arrow.left.arrow.right"
138 iconColor={props.iconColor}
139 failed={props.failed}
140 >
141 <View className="flex-row flex-wrap items-center justify-center gap-1.5">
142 {endpoints.from.map((endpoint, index) => (
143 <View
144 key={`${endpoint.instanceId}:${endpoint.model ?? ""}`}
145 className="max-w-full flex-row items-center"
146 >
147 <HandoffEndpoint {...endpoint} {...endpointProps} />
148 {index < endpoints.from.length - 1 ? (
149 <Text accessible={false} className="text-xs text-foreground-muted">
150 ,
151 </Text>
152 ) : null}
153 </View>
154 ))}
155 {endpoints.from.length > 0 ? (
156 <SymbolView name="arrow.right" size={12} tintColor={props.iconColor} />
157 ) : null}
158 <HandoffEndpoint {...endpoints.to} {...endpointProps} />
159 </View>
160 </ThreadContextDivider>
161 );
162}
163
164function HandoffEndpoint(props: {
165 instanceId: ProviderInstanceId;
166 model?: string | undefined;
167 providers: ReadonlyArray<HandoffProvider>;
168 surfaceColor: string;
169}) {
170 const provider = props.providers.find((candidate) => candidate.instanceId === props.instanceId);
171 const model = provider?.models.find((candidate) => candidate.slug === props.model);
172 const label =
173 (model ? resolveModelShortName(model, provider?.driver) : undefined) ??
174 model?.name ??
175 props.model ??
176 (provider ? resolveProviderInstanceDisplayName(provider) : props.instanceId);
177 const showBadge = provider !== undefined && shouldShowInstanceBadge(provider, props.providers);
178 return (
179 <Pressable
180 accessibilityRole="button"
181 accessibilityLabel={label}
182 accessibilityHint="Show provider account"
183 className="min-h-6 max-w-full flex-row items-center justify-center gap-1.5"
184 hitSlop={{ top: 8, bottom: 8 }}
185 onPress={() =>
186 Dialog.alert(
187 label,
188 provider ? resolveProviderInstanceDisplayName(provider) : props.instanceId,
189 )
190 }
191 >
192 {showBadge && provider !== undefined ? (
193 <ProviderInstanceIcon
194 provider={provider.driver}
195 iconUrl={provider.iconUrl}
196 size={14}
197 displayName={resolveProviderInstanceDisplayName(provider)}
198 accentColor={provider.accentColor}
199 showBadge
200 surfaceColor={props.surfaceColor}
201 />
202 ) : (
203 <ProviderIcon
204 provider={provider?.driver ?? props.instanceId}
205 iconUrl={provider?.iconUrl}
206 size={12}
207 />
208 )}
209 <Text numberOfLines={1} className="shrink text-xs text-foreground-muted">
210 {label}
211 </Text>
212 </Pressable>
213 );
214}