Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

thread-handoff-row.tsx

114 lines4.3 KB

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