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

ModelMentionChip.tsx

125 lines5.0 KB

1import { useAtomValue } from "@effect/atom-react";
2import type { EnvironmentId, ProviderDriverKind } from "@t3tools/contracts";
3import * as Option from "effect/Option";
4import { Atom } from "effect/reactivity";
5​
6import { brandProviderInstanceEntries, deriveProviderInstanceEntries } from "~/providerInstances";
7import { serverEnvironment } from "~/state/server";
8import { contextChipAccentWithHueOf } from "~/themePalette";
9import { ContextChipPopover } from "../contextChipParts";
10import { ProviderInstanceIcon, providerLogoColor } from "./ProviderInstanceIcon";
11import { getDisplayModelName } from "./providerIconUtils";
12​
13interface ModelMentionPresentation {
14 readonly label: string;
15 readonly providerLabel: string;
16 readonly driverKind: ProviderDriverKind;
17 readonly providerDisplayName: string;
18 readonly acpRegistryAgentId: string | undefined;
19 readonly acpRegistryIconUrl: string | undefined;
21​
22function presentationsEqual(left: ModelMentionPresentation, right: ModelMentionPresentation) {
23 return (
24 left.label === right.label &&
25 left.providerLabel === right.providerLabel &&
26 left.driverKind === right.driverKind &&
27 left.providerDisplayName === right.providerDisplayName &&
28 left.acpRegistryAgentId === right.acpRegistryAgentId &&
29 left.acpRegistryIconUrl === right.acpRegistryIconUrl
30 );
32​
33// Keyed per mention so provider status pushes don't rerender every message that names a model.
34const modelMentionPresentationAtom = Atom.family((key: string) =>
35 Atom.make((get): ModelMentionPresentation | null => {
36 const [environmentId, instanceId, model] = key.split("\u0000") as [string, string, string];
37 if (!environmentId) return null;
38 const config = get(serverEnvironment.configValueAtom(environmentId as EnvironmentId));
39 const provider = config?.providers.find((candidate) => candidate.instanceId === instanceId);
40 const option = provider?.models.find((candidate) => candidate.slug === model);
41 if (!config || !provider || !option) return null;
42 const [entry] = brandProviderInstanceEntries(
43 deriveProviderInstanceEntries([provider]),
44 config.settings,
45 );
46 if (!entry) return null;
47 const next: ModelMentionPresentation = {
48 label: getDisplayModelName(option),
49 providerLabel: option.subProvider ?? entry.displayName,
50 driverKind: entry.driverKind,
51 providerDisplayName: entry.displayName,
52 acpRegistryAgentId: entry.acpRegistryAgentId,
53 acpRegistryIconUrl: entry.acpRegistryIconUrl,
54 };
55 const previous = Option.getOrNull(get.self<ModelMentionPresentation | null>());
56 return previous && presentationsEqual(previous, next) ? previous : next;
57 }).pipe(Atom.withLabel("model-mention-presentation")),
58);
59​
60export function modelMentionKey(instanceId: string, model: string): string {
61 return `${instanceId}\u0000${model}`;
63​
64// Keeps its identity until the environment's models change, so status pushes don't reconcile the composer.
65export const modelMentionKeysAtom = Atom.family((environmentId: string) =>
66 Atom.make((get): ReadonlySet<string> => {
67 const config = environmentId
68 ? get(serverEnvironment.configValueAtom(environmentId as EnvironmentId))
69 : null;
70 const next = new Set(
71 config?.providers.flatMap((provider) =>
72 provider.models.map((option) => modelMentionKey(provider.instanceId, option.slug)),
73 ),
74 );
75 const previous = Option.getOrNull(get.self<ReadonlySet<string>>());
76 return previous && previous.size === next.size && [...next].every((key) => previous.has(key))
77 ? previous
78 : next;
79 }).pipe(Atom.withLabel("model-mention-keys")),
80);
81​
82function logoAccent(driverKind: ProviderDriverKind): string | undefined {
83 const color = providerLogoColor(driverKind);
84 return (color && contextChipAccentWithHueOf("model", color)) || undefined;
86​
87export function ModelMentionChip(props: {
88 environmentId: EnvironmentId | null;
89 instanceId: string;
90 model: string;
91 source: string;
92}) {
93 const presentation = useAtomValue(
94 modelMentionPresentationAtom(
95 `${props.environmentId ?? ""}\u0000${modelMentionKey(props.instanceId, props.model)}`,
96 ),
97 );
98 if (!presentation) return props.source;
99 const accent = logoAccent(presentation.driverKind);
100 return (
101 <ContextChipPopover
102 kind="neutral"
103 accent={accent}
104 icon={
105 <ProviderInstanceIcon
106 driverKind={presentation.driverKind}
107 displayName={presentation.providerDisplayName}
108 acpRegistryAgentId={presentation.acpRegistryAgentId}
109 acpRegistryIconUrl={presentation.acpRegistryIconUrl}
110 className="z-auto"
111 iconClassName="size-[1em]"
112 />
113 }
114 label={presentation.label}
115 accessibleLabel={`Model ${presentation.label}`}
116 copyMarkdown={props.source}
117 >
118 <div className="space-y-1 p-2 text-sm">
119 <p className="font-medium">{presentation.label}</p>
120 <p className="text-muted-foreground">{presentation.providerLabel}</p>
121 <p className="font-mono text-xs text-muted-foreground">{props.source}</p>
122 </div>
123 </ContextChipPopover>
124 );