Open History to see the last commit
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());
14}
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;
27}
28
29export function providerInstanceInitials(label: string): string {
30 const words = label.replace(/[_-]+/g, " ").split(/\s+/u).filter(Boolean);
31 if (words.length === 0) return "";
32 if (words.length === 1) return Array.from(words[0]!).slice(0, 2).join("").toUpperCase();
33 return words
34 .slice(0, 2)
35 .map((word) => Array.from(word)[0]?.toUpperCase() ?? "")
36 .join("");
37}
38
39export function normalizeProviderAccentColor(value: string | undefined): string | undefined {
40 const trimmed = value?.trim();
41 if (!trimmed) return undefined;
42 return /^#[0-9a-fA-F]{6}$/u.test(trimmed) ? trimmed : undefined;
43}
44
45export function shouldShowInstanceBadge(
46 entry: {
47 readonly driverKind: ProviderDriverKind;
48 readonly accentColor?: string | undefined;
49 readonly acpRegistryAgentId?: string | undefined;
50 },
51 entries: Iterable<{
52 readonly driverKind: ProviderDriverKind;
53 readonly acpRegistryAgentId?: string | undefined;
54 }>,
55): boolean {
56 if (entry.accentColor) return true;
57 let sharedProviderCount = 0;
58 for (const candidate of entries) {
59 if (candidate.driverKind !== entry.driverKind) continue;
60 if (
61 entry.driverKind === "acpRegistry" &&
62 candidate.acpRegistryAgentId !== entry.acpRegistryAgentId
63 ) {
64 continue;
65 }
66 if (++sharedProviderCount > 1) return true;
67 }
68 return false;
69}