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 } from "@t3tools/contracts";
10
11import type { ThreadListProvider } from "../../state/thread-list-environments";
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 providers: ReadonlyArray<ThreadListProvider> | undefined,
26 thread: EnvironmentThreadShell,
27): ThreadRowProviderInstance | null {
28 if (providers === undefined) return null;
29 const instanceId = thread.runtime?.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, enabled: snapshot.enabled },
41 providers.map((provider) => ({ driverKind: provider.driver, enabled: provider.enabled })),
42 ),
43 };
44}
45
46/**
47 * `resolveThreadProviderInstance` builds a fresh object per call, which breaks
48 * the memoized row's props comparison on every parent render — the result only
49 * depends on (environment, instance id), so one cache per provider-list
50 * generation keeps each row's `providerInstance` prop stable until the instance
51 * behind the row actually changes.
52 */
53export function createThreadRowProviderInstanceResolver(
54 providersByEnvironmentId: ReadonlyMap<EnvironmentId, ReadonlyArray<ThreadListProvider>>,
55): (thread: EnvironmentThreadShell) => ThreadRowProviderInstance | null {
56 const cache = new Map<string, ThreadRowProviderInstance | null>();
57 return (thread) => {
58 const instanceId = thread.runtime?.providerInstanceId ?? thread.modelSelection.instanceId;
59 const cacheKey = `${thread.environmentId}|${instanceId ?? ""}`;
60 const cached = cache.get(cacheKey);
61 if (cached !== undefined) return cached;
62 const resolved = resolveThreadProviderInstance(
63 providersByEnvironmentId.get(thread.environmentId),
64 thread,
65 );
66 cache.set(cacheKey, resolved);
67 return resolved;
68 };
69}
70
71export function useThreadRowProviderInstanceResolver(
72 providersByEnvironmentId: ReadonlyMap<EnvironmentId, ReadonlyArray<ThreadListProvider>>,
73): (thread: EnvironmentThreadShell) => ThreadRowProviderInstance | null {
74 return useMemo(
75 () => createThreadRowProviderInstanceResolver(providersByEnvironmentId),
76 [providersByEnvironmentId],
77 );
78}