Open History to see the last commit
1import { useMemo } from "react";
2
3import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
4import {
5 normalizeProviderAccentColor,
6 resolveProviderInstanceDisplayName,
7 shouldShowInstanceBadge,
8} from "@t3tools/client-runtime/state/provider-instance-display";
9import type { EnvironmentId, ProviderDriverKind, ServerConfig } from "@t3tools/contracts";
10
11/** What a thread row needs to draw the provider glyph and its account badge. */
12export interface ThreadRowProviderInstance {
13 readonly driverKind: ProviderDriverKind;
14 readonly displayName: string;
15 readonly accentColor?: string | undefined;
16 readonly showBadge: boolean;
17}
18
19/**
20 * Resolve the provider instance a thread runs on, scoped to the thread's own
21 * environment: default instance ids are the driver slug, so the same id
22 * names a different account on every server.
23 */
24export function resolveThreadProviderInstance(
25 serverConfigs: ReadonlyMap<EnvironmentId, ServerConfig>,
26 thread: EnvironmentThreadShell,
27): ThreadRowProviderInstance | null {
28 const providers = serverConfigs.get(thread.environmentId)?.providers ?? [];
29 const instanceId = thread.session?.providerInstanceId ?? thread.modelSelection.instanceId;
30 const snapshot = providers.find((provider) => provider.instanceId === instanceId);
31 if (!snapshot) return null;
32 const entry = {
33 driverKind: snapshot.driver,
34 displayName: resolveProviderInstanceDisplayName(snapshot),
35 accentColor: normalizeProviderAccentColor(snapshot.accentColor),
36 };
37 return {
38 ...entry,
39 showBadge: shouldShowInstanceBadge(
40 entry,
41 providers.map((provider) => ({ driverKind: provider.driver })),
42 ),
43 };
44}
45
46/**
47 * Builds a resolver handing out reference-stable `ThreadRowProviderInstance`
48 * objects. `resolveThreadProviderInstance` builds a fresh object per call,
49 * which breaks the memoized row's props comparison on every parent render —
50 * the result only depends on (environment, instance id), so one cache per
51 * server-config generation keeps each row's `providerInstance` prop stable
52 * until the instance behind the row actually changes.
53 */
54export function createThreadRowProviderInstanceResolver(
55 serverConfigs: ReadonlyMap<EnvironmentId, ServerConfig>,
56): (thread: EnvironmentThreadShell) => ThreadRowProviderInstance | null {
57 const cache = new Map<string, ThreadRowProviderInstance | null>();
58 return (thread) => {
59 const instanceId = thread.session?.providerInstanceId ?? thread.modelSelection.instanceId;
60 const cacheKey = `${thread.environmentId}|${instanceId ?? ""}`;
61 const cached = cache.get(cacheKey);
62 if (cached !== undefined) return cached;
63 const resolved = resolveThreadProviderInstance(serverConfigs, thread);
64 cache.set(cacheKey, resolved);
65 return resolved;
66 };
67}
68
69/** List-scoped wrapper: one cache per server-config generation. */
70export function useThreadRowProviderInstanceResolver(
71 serverConfigs: ReadonlyMap<EnvironmentId, ServerConfig>,
72): (thread: EnvironmentThreadShell) => ThreadRowProviderInstance | null {
73 return useMemo(() => createThreadRowProviderInstanceResolver(serverConfigs), [serverConfigs]);
74}