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

thread-settings-sheet-state.ts

91 lines2.9 KB

1import type { ModelOption, ProviderGroup } from "../../lib/modelOptions";
2import type { ProviderInstanceId } from "@t3tools/contracts";
3​
4export type ModelFavorite = {
5 readonly provider: ProviderInstanceId;
6 readonly model: string;
7};
8​
9export function modelFavoriteKey(provider: ProviderInstanceId, model: string): string {
10 return `${provider}:${model}`;
12​
13export function toggleModelFavorite(
14 favorites: ReadonlyArray<ModelFavorite>,
15 option: ModelOption,
16): ReadonlyArray<ModelFavorite> {
17 const provider = option.selection.instanceId;
18 const model = option.selection.model;
19 return favorites.some((favorite) => favorite.provider === provider && favorite.model === model)
20 ? favorites.filter((favorite) => favorite.provider !== provider || favorite.model !== model)
21 : [...favorites, { provider, model }];
23​
24/** Keep catalog order within each group when favorites move to the front. */
25export function favoritesFirst(
26 models: ReadonlyArray<ModelOption>,
27 favoriteKeys: ReadonlySet<string>,
28): ReadonlyArray<ModelOption> {
29 const favorites: ModelOption[] = [];
30 const others: ModelOption[] = [];
31 for (const model of models) {
32 (favoriteKeys.has(model.key) ? favorites : others).push(model);
33 }
34 return [...favorites, ...others];
36​
37/** Match the terms a user can actually see or recognize in the model picker. */
38export function modelMatchesCatalogQuery(input: {
39 readonly model: ModelOption;
40 readonly providerLabel: string;
41 readonly query: string;
42}): boolean {
43 const query = input.query.trim().toLocaleLowerCase();
44 if (query.length === 0) {
45 return true;
46 }
47​
48 return [
49 input.model.label,
50 input.model.subtitle,
51 input.model.selection.model,
52 input.providerLabel,
53 ].some((value) => value.toLocaleLowerCase().includes(query));
55​
56/** Preserve staged provider options when the highlighted model is tapped again. */
57export function pendingModelAfterPress(input: {
58 readonly current: ModelOption | null;
59 readonly pressed: ModelOption;
60 readonly pressedIsApplied: boolean;
61}): ModelOption | null {
62 if (input.pressedIsApplied) {
63 return null;
64 }
65 return input.current?.key === input.pressed.key ? input.current : input.pressed;
67​
68/** A model can disappear while the picker is open. */
69export function canCommitPendingModel(
70 pending: ModelOption,
71 groups: ReadonlyArray<ProviderGroup>,
72): boolean {
73 return groups.some((group) =>
74 group.models.some((model) => model.key === pending.key && !model.isUnavailable),
75 );
77​
78/**
79 * Primary and selected providers start open; all other catalogs start closed.
80 * A user's disclosure tap inverts that default until the picker is dismissed.
81 */
82export function providerSectionIsCollapsed(input: {
83 readonly defaultExpanded: boolean;
84 readonly hasExpansionOverride: boolean;
85 readonly isNarrowed: boolean;
86}): boolean {
87 if (input.isNarrowed) {
88 return false;
89 }
90 return input.defaultExpanded ? input.hasExpansionOverride : !input.hasExpansionOverride;