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