Open History to see the last commit
1import type { ModelOption, ProviderGroup } from "../../lib/modelOptions";
2import type { ModelSelection, ProviderInstanceId } from "@t3tools/contracts";
3
4export function modelFavoriteKey(provider: ProviderInstanceId, model: string): string {
5 return `${provider}:${model}`;
6}
7
8export function favoritesFirst(
9 models: ReadonlyArray<ModelOption>,
10 favoriteKeys: ReadonlySet<string>,
11): ReadonlyArray<ModelOption> {
12 const favorites: ModelOption[] = [];
13 const others: ModelOption[] = [];
14 for (const model of models) {
15 (favoriteKeys.has(model.key) ? favorites : others).push(model);
16 }
17 return [...favorites, ...others];
18}
19
20export function modelMatchesCatalogQuery(input: {
21 readonly model: ModelOption;
22 readonly providerLabel: string;
23 readonly query: string;
24}): boolean {
25 const query = input.query.trim().toLocaleLowerCase();
26 if (query.length === 0) {
27 return true;
28 }
29
30 return [
31 input.model.label,
32 input.model.subtitle,
33 input.model.selection.model,
34 input.providerLabel,
35 ].some((value) => value.toLocaleLowerCase().includes(query));
36}
37
38export function favoriteCatalogModels(
39 providerGroups: ReadonlyArray<ProviderGroup>,
40 favoriteKeys: ReadonlySet<string>,
41): ReadonlyArray<ModelOption> {
42 if (favoriteKeys.size === 0) {
43 return [];
44 }
45 return providerGroups.flatMap((group) =>
46 group.models.filter(
47 (model) =>
48 favoriteKeys.has(model.key) &&
49 !group.subProviders?.some(
50 (subProvider) =>
51 subProvider.name === model.subtitle && subProvider.unavailableReason !== undefined,
52 ),
53 ),
54 );
55}
56
57export function visibleCatalogModels(input: {
58 readonly models: ReadonlyArray<ModelOption>;
59 readonly providerLabel: string;
60 readonly query: string;
61 readonly favoriteKeys: ReadonlySet<string>;
62 readonly favoritesOnly: boolean;
63 readonly showLegacy: boolean;
64 readonly isDisplayed: (model: ModelOption) => boolean;
65}): ReadonlyArray<ModelOption> {
66 const catalogModels =
67 input.showLegacy || input.favoritesOnly || input.query.trim().length > 0
68 ? input.models
69 : input.models.filter(
70 (model) =>
71 !model.isLegacy || input.isDisplayed(model) || input.favoriteKeys.has(model.key),
72 );
73 return favoritesFirst(
74 catalogModels.filter(
75 (model) =>
76 (!input.favoritesOnly || input.favoriteKeys.has(model.key)) &&
77 modelMatchesCatalogQuery({
78 model,
79 providerLabel: input.providerLabel,
80 query: input.query,
81 }),
82 ),
83 input.favoriteKeys,
84 );
85}
86
87export function pendingModelAfterPress(input: {
88 readonly current: ModelOption | null;
89 readonly pressed: ModelOption;
90 readonly pressedIsApplied: boolean;
91 readonly restoreSelection?: (selection: ModelSelection) => ModelSelection;
92}): ModelOption | null {
93 if (input.pressedIsApplied) {
94 return null;
95 }
96 if (input.current?.key === input.pressed.key) {
97 return input.current;
98 }
99 return input.restoreSelection
100 ? { ...input.pressed, selection: input.restoreSelection(input.pressed.selection) }
101 : input.pressed;
102}
103
104export function canCommitPendingModel(
105 pending: ModelOption,
106 groups: ReadonlyArray<ProviderGroup>,
107): boolean {
108 return groups.some((group) =>
109 group.models.some(
110 (model) => model.key === pending.key && !model.isUnavailable && !model.isHidden,
111 ),
112 );
113}
114
115export function providerSectionIsCollapsed(input: {
116 readonly defaultExpanded: boolean;
117 readonly hasExpansionOverride: boolean;
118 readonly isNarrowed: boolean;
119}): boolean {
120 if (input.isNarrowed) {
121 return false;
122 }
123 return input.defaultExpanded ? input.hasExpansionOverride : !input.hasExpansionOverride;
124}