Skip to content
Source
Sign inSign up

modelShortName.ts

71 lines1.8 KB

1const FAMILY_PREFIXES = ["Claude ", "GPT-", "GPT ", "Gemini ", "Muse ", "Grok "] as const;
2​
3// Words that describe a variant within a family rather than name a model. Off the
4// family's native provider there is no icon saying which family "4 Pro" belongs to.
5const VARIANT_WORDS = new Set([
6 "pro",
7 "flash",
8 "mini",
9 "max",
10 "lite",
11 "nano",
12 "ultra",
13 "plus",
14 "fast",
15 "turbo",
16 "preview",
17 "latest",
18 "thinking",
19 "reasoning",
20 "build",
21 "code",
22 "image",
23 "vision",
24 "audio",
25 "live",
26 "custom",
27 "tools",
28 "exp",
29 "experimental",
30 "chat",
31 "tts",
32 "stt",
33 "non",
34 "transcribe",
35]);
36​
37const VERSION = /^v?\d+(?:\.\d+)*[a-z]?$/i;
38​
39const VENDOR_PREFIX = /^[A-Z][\w.&\- ]{0,24}: (?=\S)/;
40​
41export function modelShortName(
42 name: string,
43 nativeFamilies: ReadonlyArray<string>,
44): string | undefined {
45 const unprefixed = name.replace(VENDOR_PREFIX, "");
46 const short =
47 withoutFamily(unprefixed, nativeFamilies) ?? (namesModel(unprefixed) ? unprefixed : name);
48 return short === name ? undefined : short;
50​
51function withoutFamily(name: string, nativeFamilies: ReadonlyArray<string>): string | undefined {
52 const prefix = FAMILY_PREFIXES.find((candidate) => name.startsWith(candidate));
53 if (!prefix) return undefined;
54 const rest = name.slice(prefix.length).trim();
55 const keep = nativeFamilies.includes(prefix)
56 ? nonVersionWords(rest).length > 0
57 : namesModel(rest);
58 if (!keep) return undefined;
59 return /\s/.test(rest) ? rest : rest.replaceAll("-", " ");
61​
62function nonVersionWords(text: string): ReadonlyArray<string> {
63 return text
64 .replace(/\(.*?\)/g, " ")
65 .split(/[\s-]+/)
66 .filter((word) => word.length > 0 && !VERSION.test(word));
68​
69function namesModel(text: string): boolean {
70 return nonVersionWords(text).some((word) => !VARIANT_WORDS.has(word.toLowerCase()));