@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 95 lines 3.54 KB
Raw
1 import {
2 defaultInstanceIdForDriver,
3 PROVIDER_DISPLAY_NAMES,
4 type ProviderDriverKind,
5 type ServerProvider,
6 } from "@t3tools/contracts";
7
8 function 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
16 export 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
29 export function providerInstanceInitials(label: string): string {
30 // Punctuation separates words so "Codex (hub)" reads CH, not C(. An apostrophe
31 // inside a word stays ("Theo's Codex" is TC); a quoting one does not
32 // ("Codex 'hub'" is CH). A single camel-case word splits at its humps, so
33 // "ClaudeProx" is CP, but "ClaudeProx Work" keeps its own words and is CW.
34 const toWords = (text: string) =>
35 text
36 .split(/\s+/u)
37 .map((word) => word.replace(/^['’]+|['’]+$/gu, ""))
38 .filter(Boolean);
39 let words = toWords(label.replace(/[^\P{P}'’]+/gu, " "));
40 if (words.length === 1) words = toWords(words[0]!.replace(/(\p{Ll})(\p{Lu})/gu, "$1 $2"));
41 if (words.length === 0) return "";
42 if (words.length === 1) return Array.from(words[0]!).slice(0, 2).join("").toUpperCase();
43 return words
44 .slice(0, 2)
45 .map((word) => Array.from(word)[0]?.toUpperCase() ?? "")
46 .join("");
47 }
48
49 export function normalizeProviderAccentColor(value: string | undefined): string | undefined {
50 const trimmed = value?.trim();
51 if (!trimmed) return undefined;
52 return /^#[0-9a-fA-F]{6}$/u.test(trimmed) ? trimmed : undefined;
53 }
54
55 export function providerAccentForegroundColor(accentColor: string) {
56 const linearChannel = (offset: number) => {
57 const value = Number.parseInt(accentColor.slice(offset, offset + 2), 16) / 255;
58 return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
59 };
60 const luminance =
61 0.2126 * linearChannel(1) + 0.7152 * linearChannel(3) + 0.0722 * linearChannel(5);
62 const blackContrast = (luminance + 0.05) / 0.05;
63 const whiteContrast = 1.05 / (luminance + 0.05);
64 return blackContrast >= whiteContrast ? "#000000" : "#ffffff";
65 }
66
67 export function shouldShowInstanceBadge(
68 entry: {
69 readonly driverKind: ProviderDriverKind;
70 readonly accentColor?: string | undefined;
71 readonly acpRegistryAgentId?: string | undefined;
72 readonly enabled: boolean;
73 },
74 entries: Iterable<{
75 readonly driverKind: ProviderDriverKind;
76 readonly acpRegistryAgentId?: string | undefined;
77 readonly enabled: boolean;
78 }>,
79 ): boolean {
80 if (normalizeProviderAccentColor(entry.accentColor)) return true;
81 let sharedProviderCount = 0;
82 for (const candidate of entries) {
83 if (candidate.driverKind !== entry.driverKind) continue;
84 // Historical threads still need to distinguish accounts that are all disabled.
85 if (entry.enabled && !candidate.enabled) continue;
86 if (
87 entry.driverKind === "acpRegistry" &&
88 candidate.acpRegistryAgentId !== entry.acpRegistryAgentId
89 ) {
90 continue;
91 }
92 if (++sharedProviderCount > 1) return true;
93 }
94 return false;
95 }