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