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

thread-provider-instance.ts

43 lines1.6 KB

1import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
2import {
3 normalizeProviderAccentColor,
4 resolveProviderInstanceDisplayName,
5 shouldShowInstanceBadge,
6} from "@t3tools/client-runtime/state/provider-instance-display";
7import type { ProviderDriverKind } from "@t3tools/contracts";
8​
9import type { ThreadListProvider } from "../../state/thread-list-environments";
10/** What a thread row needs to draw the provider glyph and its account badge. */
11export interface ThreadRowProviderInstance {
12 readonly driverKind: ProviderDriverKind;
13 readonly displayName: string;
14 readonly accentColor?: string | undefined;
15 readonly showBadge: boolean;
17​
18/**
19 * Resolve the provider instance a thread runs on, scoped to the thread's own
20 * environment: default instance ids are the driver slug, so the same id
21 * names a different account on every server.
22 */
23export function resolveThreadProviderInstance(
24 providers: ReadonlyArray<ThreadListProvider> | undefined,
25 thread: EnvironmentThreadShell,
26): ThreadRowProviderInstance | null {
27 if (providers === undefined) return null;
28 const instanceId = thread.runtime?.providerInstanceId ?? thread.modelSelection.instanceId;
29 const snapshot = providers.find((provider) => provider.instanceId === instanceId);
30 if (!snapshot) return null;
31 const entry = {
32 driverKind: snapshot.driver,
33 displayName: resolveProviderInstanceDisplayName(snapshot),
34 accentColor: normalizeProviderAccentColor(snapshot.accentColor),
35 };
36 return {
37 ...entry,
38 showBadge: shouldShowInstanceBadge(
39 entry,
40 providers.map((provider) => ({ driverKind: provider.driver })),
41 ),
42 };