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 visibleCatalogModels(input: {
39 readonly models: ReadonlyArray<ModelOption>;
40 readonly providerLabel: string;
41 readonly query: string;
42 readonly favoriteKeys: ReadonlySet<string>;
43 readonly favoritesOnly: boolean;
44 readonly showLegacy: boolean;
45 readonly isDisplayed: (model: ModelOption) => boolean;
46}): ReadonlyArray<ModelOption> {
47 const catalogModels =
48 input.showLegacy || input.favoritesOnly || input.query.trim().length > 0
49 ? input.models
50 : input.models.filter(
51 (model) =>
52 !model.isLegacy || input.isDisplayed(model) || input.favoriteKeys.has(model.key),
53 );
54 return favoritesFirst(
55 catalogModels.filter(
56 (model) =>
57 (!input.favoritesOnly || input.favoriteKeys.has(model.key)) &&
58 modelMatchesCatalogQuery({
59 model,
60 providerLabel: input.providerLabel,
61 query: input.query,
62 }),
63 ),
64 input.favoriteKeys,
65 );
66}
67
68export function pendingModelAfterPress(input: {
69 readonly current: ModelOption | null;
70 readonly pressed: ModelOption;
71 readonly pressedIsApplied: boolean;
72 readonly restoreSelection?: (selection: ModelSelection) => ModelSelection;
73}): ModelOption | null {
74 if (input.pressedIsApplied) {
75 return null;
76 }
77 if (input.current?.key === input.pressed.key) {
78 return input.current;
79 }
80 return input.restoreSelection
81 ? { ...input.pressed, selection: input.restoreSelection(input.pressed.selection) }
82 : input.pressed;
83}
84
85export function canCommitPendingModel(
86 pending: ModelOption,
87 groups: ReadonlyArray<ProviderGroup>,
88): boolean {
89 return groups.some((group) =>
90 group.models.some(
91 (model) => model.key === pending.key && !model.isUnavailable && !model.isHidden,
92 ),
93 );
94}
95
96export function providerSectionIsCollapsed(input: {
97 readonly defaultExpanded: boolean;
98 readonly hasExpansionOverride: boolean;
99 readonly isNarrowed: boolean;
100}): boolean {
101 if (input.isNarrowed) {
102 return false;
103 }
104 return input.defaultExpanded ? input.hasExpansionOverride : !input.hasExpansionOverride;
105}