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

providerInstanceDisplay.ts

98 lines3.7 KB

1import {
2 defaultInstanceIdForDriver,
3 PROVIDER_DISPLAY_NAMES,
4 type ProviderDriverKind,
5 type ServerProvider,
6} from "@t3tools/contracts";
7​
8function humanizeSlug(slug: string): string {
9 return slug
10 .replace(/([a-z])([A-Z])/g, "$1 $2")
11 .replace(/[_-]+/g, " ")
12 .trim()
13 .replace(/\b\w/g, (char) => char.toUpperCase());
15​
16export function resolveProviderInstanceDisplayName(
17 snapshot: Pick<ServerProvider, "instanceId" | "driver" | "displayName">,
18): string {
19 const trimmedSnapshotName = snapshot.displayName?.trim();
20 const kindLabel = PROVIDER_DISPLAY_NAMES[snapshot.driver] ?? humanizeSlug(snapshot.driver);
21 if (trimmedSnapshotName && trimmedSnapshotName !== kindLabel) return trimmedSnapshotName;
22 if (snapshot.instanceId !== defaultInstanceIdForDriver(snapshot.driver)) {
23 const humanized = humanizeSlug(snapshot.instanceId);
24 if (humanized.length > 0) return humanized;
25 }
26 return trimmedSnapshotName || kindLabel;
28​
29// A letter with the combining marks after it, which NFC cannot always fold into one character.
30const glyphs = (word: string) => word.match(/\P{M}\p{M}*/gu) ?? [];
31​
32export function providerInstanceInitials(label: string): string {
33 // Punctuation separates words so "Codex (hub)" reads CH, not C(. An apostrophe
34 // inside a word stays ("Theo's Codex" is TC); a quoting one does not
35 // ("Codex 'hub'" is CH). A single camel-case word splits at its humps, so
36 // "ClaudeProx" is CP, but "ClaudeProx Work" keeps its own words and is CW.
37 const toWords = (text: string) =>
38 text
39 .split(/\s+/u)
40 .map((word) => word.replace(/^['’]+|['’]+$/gu, ""))
41 .filter(Boolean);
42 let words = toWords(label.normalize("NFC").replace(/[^\P{P}'’]+/gu, " "));
43 if (words.length === 1) words = toWords(words[0]!.replace(/(\p{Ll})(\p{Lu})/gu, "$1 $2"));
44 if (words.length === 0) return "";
45 if (words.length === 1) return glyphs(words[0]!).slice(0, 2).join("").toUpperCase();
46 return words
47 .slice(0, 2)
48 .map((word) => glyphs(word)[0]?.toUpperCase() ?? "")
49 .join("");
51​
52export function normalizeProviderAccentColor(value: string | undefined): string | undefined {
53 const trimmed = value?.trim();
54 if (!trimmed) return undefined;
55 return /^#[0-9a-fA-F]{6}$/u.test(trimmed) ? trimmed : undefined;
57​
58export function providerAccentForegroundColor(accentColor: string) {
59 const linearChannel = (offset: number) => {
60 const value = Number.parseInt(accentColor.slice(offset, offset + 2), 16) / 255;
61 return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
62 };
63 const luminance =
64 0.2126 * linearChannel(1) + 0.7152 * linearChannel(3) + 0.0722 * linearChannel(5);
65 const blackContrast = (luminance + 0.05) / 0.05;
66 const whiteContrast = 1.05 / (luminance + 0.05);
67 return blackContrast >= whiteContrast ? "#000000" : "#ffffff";
69​
70export function shouldShowInstanceBadge(
71 entry: {
72 readonly driverKind: ProviderDriverKind;
73 readonly accentColor?: string | undefined;
74 readonly acpRegistryAgentId?: string | undefined;
75 readonly enabled: boolean;
76 },
77 entries: Iterable<{
78 readonly driverKind: ProviderDriverKind;
79 readonly acpRegistryAgentId?: string | undefined;
80 readonly enabled: boolean;
81 }>,
82): boolean {
83 if (normalizeProviderAccentColor(entry.accentColor)) return true;
84 let sharedProviderCount = 0;
85 for (const candidate of entries) {
86 if (candidate.driverKind !== entry.driverKind) continue;
87 // Historical threads still need to distinguish accounts that are all disabled.
88 if (entry.enabled && !candidate.enabled) continue;
89 if (
90 entry.driverKind === "acpRegistry" &&
91 candidate.acpRegistryAgentId !== entry.acpRegistryAgentId
92 ) {
93 continue;
94 }
95 if (++sharedProviderCount > 1) return true;
96 }
97 return false;