Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions apps/mobile/src/components/ProviderIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Image } from "expo-image";
import { Defs, LinearGradient, Path, Stop, Svg } from "react-native-svg";
import { View } from "react-native";
import { providerInstanceInitials } from "@t3tools/client-runtime/state/provider-instance-display";
import {
normalizeProviderAccentColor,
providerAccentForegroundColor,
providerInstanceInitials,
} from "@t3tools/client-runtime/state/provider-instance-display";
import { useId, useState } from "react";
import { resolveOfficialAcpRegistryIconUrl } from "@t3tools/contracts";
import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
Expand Down Expand Up @@ -156,6 +160,7 @@ export function ProviderInstanceIcon(props: {
readonly showBadge?: boolean;
readonly surfaceColor: string;
}) {
const accentColor = normalizeProviderAccentColor(props.accentColor);
return (
<View style={{ position: "relative" }}>
<View style={{ opacity: 0.6 }}>
Expand All @@ -169,26 +174,26 @@ export function ProviderInstanceIcon(props: {
style={{ position: "absolute", right: -3, bottom: -3, width: 32, alignItems: "flex-end" }}
>
<View
className={props.accentColor ? undefined : "bg-card"}
className={accentColor ? undefined : "bg-card"}
style={{
height: 12,
minWidth: 12,
paddingHorizontal: 2,
borderRadius: 999,
borderWidth: 1,
borderColor: props.surfaceColor,
backgroundColor: props.accentColor,
backgroundColor: accentColor,
alignItems: "center",
justifyContent: "center",
}}
>
<Text
className={props.accentColor ? undefined : "text-foreground-muted"}
className={accentColor ? undefined : "text-foreground-muted"}
style={{
fontSize: 7,
fontWeight: "600",
lineHeight: 9,
color: props.accentColor ? "#ffffff" : undefined,
color: accentColor ? providerAccentForegroundColor(accentColor) : undefined,
}}
>
{providerInstanceInitials(props.displayName)}
Expand Down
20 changes: 14 additions & 6 deletions apps/web/src/components/chat/ProviderInstanceIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { type CSSProperties, memo } from "react";

import { providerInstanceInitials } from "@t3tools/client-runtime/state/provider-instance-display";
import {
normalizeProviderAccentColor,
providerAccentForegroundColor,
providerInstanceInitials,
} from "@t3tools/client-runtime/state/provider-instance-display";

import { ProviderDriverKind } from "@t3tools/contracts";
import { AntigravityIcon, ClaudeAI, Icon, OpenAI } from "../Icons";
Expand Down Expand Up @@ -71,8 +75,12 @@ export const ProviderInstanceIcon = memo(function ProviderInstanceIcon(props: {
const Icon = PROVIDER_ICON_BY_PROVIDER[props.driverKind] ?? null;
const packageIcon = providerClients.get(props.driverKind)?.icon;
const indicatorBackground = props.indicatorBackground ?? "var(--card)";
const accentStyle = props.accentColor
? ({ "--provider-accent": props.accentColor } as CSSProperties)
const accentColor = normalizeProviderAccentColor(props.accentColor);
const accentStyle = accentColor
? ({
"--provider-accent": accentColor,
"--provider-accent-foreground": providerAccentForegroundColor(accentColor),
} as CSSProperties)
: undefined;
const badgeContent = props.badgeContent ?? "initials";
const iconClassName = cn(props.size === "compact" && "size-3.5", props.iconClassName);
Expand All @@ -90,7 +98,7 @@ export const ProviderInstanceIcon = memo(function ProviderInstanceIcon(props: {
props.className,
)}
style={accentStyle}
data-provider-accent-color={props.accentColor}
data-provider-accent-color={accentColor}
>
{isAcpRegistry ? (
<AcpRegistryAgentIcon
Expand Down Expand Up @@ -125,8 +133,8 @@ export const ProviderInstanceIcon = memo(function ProviderInstanceIcon(props: {
<span
className={cn(
"pointer-events-none absolute right-0 bottom-0 z-10 flex h-3.5 min-w-3.5 items-center justify-center rounded-full border px-0.5 text-4xs font-semibold leading-none shadow-sm",
props.accentColor
? "bg-(--provider-accent) text-white"
accentColor
? "bg-(--provider-accent) text-(--provider-accent-foreground)"
: "bg-card text-muted-foreground",
props.size === "compact" &&
"right-[-0.1875rem] bottom-[-0.1875rem] h-3 min-w-3 px-0.5 text-5xs",
Expand Down
11 changes: 11 additions & 0 deletions packages/client-runtime/src/state/providerInstanceDisplay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, expect, it } from "vite-plus/test";

import {
normalizeProviderAccentColor,
providerAccentForegroundColor,
providerInstanceInitials,
resolveProviderInstanceDisplayName,
shouldShowInstanceBadge,
Expand Down Expand Up @@ -89,6 +90,16 @@ describe("normalizeProviderAccentColor", () => {
});
});

describe("providerAccentForegroundColor", () => {
it.each([
["#b5f23d", "#000000"],
["#2563eb", "#ffffff"],
["#777777", "#000000"],
])("chooses the higher WCAG contrast foreground for %s", (accent, foreground) => {
expect(providerAccentForegroundColor(accent)).toBe(foreground);
});
});

describe("shouldShowInstanceBadge", () => {
it("hides badges for distinct ACP agents sharing the registry driver", () => {
const mistral = { driverKind: acpRegistry, acpRegistryAgentId: "mistral-vibe", enabled: true };
Expand Down
12 changes: 12 additions & 0 deletions packages/client-runtime/src/state/providerInstanceDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ export function normalizeProviderAccentColor(value: string | undefined): string
return /^#[0-9a-fA-F]{6}$/u.test(trimmed) ? trimmed : undefined;
}

export function providerAccentForegroundColor(accentColor: string) {
const linearChannel = (offset: number) => {
const value = Number.parseInt(accentColor.slice(offset, offset + 2), 16) / 255;
return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
};
const luminance =
0.2126 * linearChannel(1) + 0.7152 * linearChannel(3) + 0.0722 * linearChannel(5);
const blackContrast = (luminance + 0.05) / 0.05;
const whiteContrast = 1.05 / (luminance + 0.05);
return blackContrast >= whiteContrast ? "#000000" : "#ffffff";
}

export function shouldShowInstanceBadge(
entry: {
readonly driverKind: ProviderDriverKind;
Expand Down
Loading