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