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