Files
t3code/packages/client-runtime/src/state/providerInstanceDisplay.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

104 lines
3.8 KiB
TypeScript

/**
* How a configured provider instance presents itself in a client: its label,
* its accent color, and whether its icon carries the account badge. Shared by
* web and mobile so both clients name and badge the same instance identically.
*
* @module providerInstanceDisplay
*/
import {
defaultInstanceIdForDriver,
PROVIDER_DISPLAY_NAMES,
type ProviderDriverKind,
type ServerProvider,
} from "@t3tools/contracts";
/**
* Title-case a slug: splits on `_` / `-` and camelCase boundaries, so
* `codex_personal` becomes "Codex Personal" and `myCustomInstance` becomes
* "My Custom Instance".
*/
function humanizeSlug(slug: string): string {
return slug
.replace(/([a-z])([A-Z])/g, "$1 $2")
.replace(/[_-]+/g, " ")
.trim()
.replace(/\b\w/g, (char) => char.toUpperCase());
}
/**
* Resolve an instance's label with a tiered priority:
*
* 1. A snapshot `displayName` that differs from the driver's brand label —
* the server has explicitly named this instance, trust it.
* 2. For non-default instances, a humanized `instanceId` — the server fell
* back to the driver-level label (the same for every instance of that
* kind), so the slug is what keeps "Codex" and "Codex Personal" apart.
* 3. The snapshot's `displayName`, or the brand label from contracts.
*/
export function resolveProviderInstanceDisplayName(
snapshot: Pick<ServerProvider, "instanceId" | "driver" | "displayName">,
): string {
const trimmedSnapshotName = snapshot.displayName?.trim();
const kindLabel = PROVIDER_DISPLAY_NAMES[snapshot.driver] ?? humanizeSlug(snapshot.driver);
if (trimmedSnapshotName && trimmedSnapshotName !== kindLabel) return trimmedSnapshotName;
if (snapshot.instanceId !== defaultInstanceIdForDriver(snapshot.driver)) {
const humanized = humanizeSlug(snapshot.instanceId);
if (humanized.length > 0) return humanized;
}
return trimmedSnapshotName || kindLabel;
}
/**
* Turn a display name into up to two initials for the badge: the first two
* characters of a single word, or the first character of each of the first
* two words. Iterates by code point so an emoji never splits into surrogates.
*/
export function providerInstanceInitials(label: string): string {
const words = label.replace(/[_-]+/g, " ").split(/\s+/u).filter(Boolean);
if (words.length === 0) return "";
if (words.length === 1) return Array.from(words[0]!).slice(0, 2).join("").toUpperCase();
return words
.slice(0, 2)
.map((word) => Array.from(word)[0]?.toUpperCase() ?? "")
.join("");
}
/** Only `#rrggbb` accent colors render; anything else is treated as unset. */
export function normalizeProviderAccentColor(value: string | undefined): string | undefined {
const trimmed = value?.trim();
if (!trimmed) return undefined;
return /^#[0-9a-fA-F]{6}$/u.test(trimmed) ? trimmed : undefined;
}
/**
* Whether an instance's icon carries the account badge: accent color set, or
* several instances sharing a provider so the brand glyph alone is ambiguous.
* ACP agents have distinct glyphs even though they share the registry driver.
* Shared by the composer trigger, the picker rail, and sidebar/thread rows.
*/
export function shouldShowInstanceBadge(
entry: {
readonly driverKind: ProviderDriverKind;
readonly accentColor?: string | undefined;
readonly acpRegistryAgentId?: string | undefined;
},
entries: Iterable<{
readonly driverKind: ProviderDriverKind;
readonly acpRegistryAgentId?: string | undefined;
}>,
): boolean {
if (entry.accentColor) return true;
let sharedProviderCount = 0;
for (const candidate of entries) {
if (candidate.driverKind !== entry.driverKind) continue;
if (
entry.driverKind === "acpRegistry" &&
candidate.acpRegistryAgentId !== entry.acpRegistryAgentId
) {
continue;
}
if (++sharedProviderCount > 1) return true;
}
return false;
}