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