feat(mobile): add favorites to model picker (#12231)
This commit is contained in:
1 parent
dd9528a974
commit
298f8c95cd
7 files changed
+358
-62
No files matched your search
@@ -82,6 +82,8 @@ import IconServer from "@tabler/icons-react-native/IconServer";
|
||||
import IconSettings from "@tabler/icons-react-native/IconSettings";
|
||||
import IconSparkles from "@tabler/icons-react-native/IconSparkles";
|
||||
import IconStack2 from "@tabler/icons-react-native/IconStack2";
|
||||
import IconStar from "@tabler/icons-react-native/IconStar";
|
||||
import IconStarFilled from "@tabler/icons-react-native/IconStarFilled";
|
||||
import IconStethoscope from "@tabler/icons-react-native/IconStethoscope";
|
||||
import IconSun from "@tabler/icons-react-native/IconSun";
|
||||
import IconTerminal2 from "@tabler/icons-react-native/IconTerminal2";
|
||||
@@ -180,6 +182,8 @@ const ANDROID_ICON_BY_SF_SYMBOL: Partial<Record<SFSymbol, Icon>> = {
|
||||
"square.and.pencil": IconEdit,
|
||||
"square.grid.2x2": IconApps,
|
||||
"square.split.2x1": IconLayoutColumns,
|
||||
star: IconStar,
|
||||
"star.fill": IconStarFilled,
|
||||
"sun.max": IconSun,
|
||||
"stop.fill": IconPlayerStopFilled,
|
||||
terminal: IconTerminal2,
|
||||
|
||||
@@ -7,6 +7,7 @@ import type {
|
||||
RuntimeMode,
|
||||
} from "@t3tools/contracts";
|
||||
import type { LegendListRenderItemProps } from "@legendapp/list/react-native";
|
||||
import { useAtomSet, useAtomValue } from "@effect/atom-react";
|
||||
import { AnimatedLegendList } from "@legendapp/list/reanimated";
|
||||
import { HeaderHeightContext } from "@react-navigation/elements";
|
||||
import {
|
||||
@@ -20,6 +21,7 @@ import {
|
||||
type NativeStackNavigationProp,
|
||||
} from "@react-navigation/native-stack";
|
||||
import * as Haptics from "expo-haptics";
|
||||
import { AsyncResult } from "effect/unstable/reactivity";
|
||||
import {
|
||||
createContext,
|
||||
use,
|
||||
@@ -46,6 +48,7 @@ import type { ModelOption, ProviderGroup } from "../../lib/modelOptions";
|
||||
import { applyProviderOptionSelection } from "../../lib/providerOptions";
|
||||
import { resolveProviderOptionDescriptors } from "../../lib/providerOptions";
|
||||
import { useUniwindTheme } from "../../lib/useUniwindTheme";
|
||||
import type { Preferences } from "../../persistence/mobile-preferences";
|
||||
import {
|
||||
NativeHeaderToolbar,
|
||||
NativeStackScreenOptions,
|
||||
@@ -53,6 +56,7 @@ import {
|
||||
} from "../../native/StackHeader";
|
||||
import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";
|
||||
import { serverEnvironment } from "../../state/server";
|
||||
import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
|
||||
import { useAtomCommand } from "../../state/use-atom-command";
|
||||
import { useNewTaskFlow } from "./new-task-flow-provider";
|
||||
import { MaterialScreenContent } from "../../components/MaterialScreenContent";
|
||||
@@ -68,9 +72,12 @@ import {
|
||||
import { RUNTIME_MODE_CHOICES, selectableChoices } from "./thread-settings-options";
|
||||
import {
|
||||
canCommitPendingModel,
|
||||
favoritesFirst,
|
||||
modelFavoriteKey,
|
||||
modelMatchesCatalogQuery,
|
||||
pendingModelAfterPress,
|
||||
providerSectionIsCollapsed,
|
||||
toggleModelFavorite,
|
||||
} from "./thread-settings-sheet-state";
|
||||
|
||||
/**
|
||||
@@ -101,74 +108,103 @@ const THREAD_SETTINGS_HEADER_SCROLL_EDGE_EFFECTS = nativeHeaderScrollEdgeEffects
|
||||
Platform.OS,
|
||||
Platform.Version,
|
||||
);
|
||||
const EMPTY_MODEL_FAVORITES: ReadonlyArray<{
|
||||
readonly provider: ProviderInstanceId;
|
||||
readonly model: string;
|
||||
}> = [];
|
||||
const FAVORITES_PROVIDER_FILTER = "@favorites";
|
||||
function ModelRow(props: {
|
||||
readonly option: ModelOption;
|
||||
readonly selected: boolean;
|
||||
readonly onPress: () => void;
|
||||
readonly isFavorite: boolean;
|
||||
readonly favoritesLoaded: boolean;
|
||||
readonly onToggleFavorite: () => void;
|
||||
readonly isFirst: boolean;
|
||||
readonly isLast: boolean;
|
||||
}) {
|
||||
const selectedMaterialRow = Platform.OS === "android" && props.selected;
|
||||
return (
|
||||
<Pressable
|
||||
accessibilityLabel={[props.option.label, props.option.subtitle].filter(Boolean).join(", ")}
|
||||
accessibilityRole="radio"
|
||||
accessibilityState={{
|
||||
checked: props.selected,
|
||||
disabled: props.option.isUnavailable === true,
|
||||
}}
|
||||
disabled={props.option.isUnavailable}
|
||||
onPress={props.onPress}
|
||||
style={Platform.OS === "android" ? { minHeight: 56, paddingVertical: 12 } : undefined}
|
||||
<View
|
||||
style={Platform.OS === "android" ? { minHeight: 56 } : undefined}
|
||||
className={cn(
|
||||
"mx-4 min-h-11 flex-row items-center gap-2 bg-card px-4 py-2 active:bg-subtle",
|
||||
"mx-4 min-h-11 flex-row items-center gap-2 bg-card px-4",
|
||||
selectedMaterialRow && "bg-thread-selected",
|
||||
props.isFirst && "rounded-t-2xl",
|
||||
props.isLast ? "rounded-b-2xl" : "border-b border-border-subtle",
|
||||
)}
|
||||
>
|
||||
{Platform.OS === "android" ? <MaterialRadioIndicator selected={props.selected} /> : null}
|
||||
<View className="min-w-0 flex-1">
|
||||
<View className="flex-row items-center gap-2">
|
||||
<Text
|
||||
className="min-w-0 shrink text-base font-t3-medium text-foreground"
|
||||
numberOfLines={Platform.OS === "android" ? 2 : 1}
|
||||
>
|
||||
{props.option.label}
|
||||
</Text>
|
||||
{props.option.isDefault ? (
|
||||
<View className="rounded-md bg-subtle-strong px-1.5 py-0.5">
|
||||
<Text className="text-3xs font-t3-bold text-foreground-muted">Default</Text>
|
||||
</View>
|
||||
) : null}
|
||||
{props.option.isLegacy ? (
|
||||
<View className="rounded-md bg-subtle px-1.5 py-0.5">
|
||||
<Text className="text-3xs font-t3-bold text-foreground-muted">Legacy</Text>
|
||||
</View>
|
||||
) : null}
|
||||
{props.option.isUnavailable ? (
|
||||
<Text className="text-xs text-foreground">Unavailable</Text>
|
||||
<Pressable
|
||||
accessibilityLabel={[props.option.label, props.option.subtitle].filter(Boolean).join(", ")}
|
||||
accessibilityRole="radio"
|
||||
accessibilityState={{
|
||||
checked: props.selected,
|
||||
disabled: props.option.isUnavailable === true,
|
||||
}}
|
||||
className="min-h-11 min-w-0 flex-1 flex-row items-center gap-2 active:opacity-70"
|
||||
disabled={props.option.isUnavailable}
|
||||
onPress={props.onPress}
|
||||
>
|
||||
{Platform.OS === "android" ? <MaterialRadioIndicator selected={props.selected} /> : null}
|
||||
<View className="min-w-0 flex-1">
|
||||
<View className="flex-row items-center gap-2">
|
||||
<Text
|
||||
className="min-w-0 shrink text-base font-t3-medium text-foreground"
|
||||
numberOfLines={Platform.OS === "android" ? 2 : 1}
|
||||
>
|
||||
{props.option.label}
|
||||
</Text>
|
||||
{props.option.isDefault ? (
|
||||
<View className="rounded-md bg-subtle-strong px-1.5 py-0.5">
|
||||
<Text className="text-3xs font-t3-bold text-foreground-muted">Default</Text>
|
||||
</View>
|
||||
) : null}
|
||||
{props.option.isLegacy ? (
|
||||
<View className="rounded-md bg-subtle px-1.5 py-0.5">
|
||||
<Text className="text-3xs font-t3-bold text-foreground-muted">Legacy</Text>
|
||||
</View>
|
||||
) : null}
|
||||
{props.option.isUnavailable ? (
|
||||
<Text className="text-xs text-foreground">Unavailable</Text>
|
||||
) : null}
|
||||
</View>
|
||||
{props.option.subtitle ? (
|
||||
<Text
|
||||
className="text-xs text-foreground-muted"
|
||||
numberOfLines={Platform.OS === "android" ? 2 : 1}
|
||||
>
|
||||
{props.option.subtitle}
|
||||
</Text>
|
||||
) : null}
|
||||
</View>
|
||||
{props.option.subtitle ? (
|
||||
<Text
|
||||
className="text-xs text-foreground-muted"
|
||||
numberOfLines={Platform.OS === "android" ? 2 : 1}
|
||||
>
|
||||
{props.option.subtitle}
|
||||
</Text>
|
||||
{props.selected && Platform.OS !== "android" ? (
|
||||
<SymbolView
|
||||
name="checkmark"
|
||||
size={16}
|
||||
tintColorClassName="accent-icon"
|
||||
type="monochrome"
|
||||
weight="semibold"
|
||||
/>
|
||||
) : null}
|
||||
</View>
|
||||
{props.selected && Platform.OS !== "android" ? (
|
||||
</Pressable>
|
||||
<Pressable
|
||||
accessibilityLabel={`${props.isFavorite ? "Remove from" : "Add to"} favorites: ${
|
||||
props.option.providerLabel
|
||||
}, ${props.option.label}`}
|
||||
accessibilityRole="button"
|
||||
accessibilityState={{ disabled: !props.favoritesLoaded, selected: props.isFavorite }}
|
||||
className="min-h-11 min-w-11 items-center justify-center"
|
||||
disabled={!props.favoritesLoaded}
|
||||
onPress={props.onToggleFavorite}
|
||||
>
|
||||
<SymbolView
|
||||
name="checkmark"
|
||||
size={16}
|
||||
tintColorClassName="accent-icon"
|
||||
name={props.isFavorite ? "star.fill" : "star"}
|
||||
size={18}
|
||||
tintColorClassName={props.isFavorite ? "accent-icon" : "accent-icon-subtle"}
|
||||
type="monochrome"
|
||||
weight="semibold"
|
||||
/>
|
||||
) : null}
|
||||
</Pressable>
|
||||
</Pressable>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -384,6 +420,9 @@ type ThreadSettingsSessionValue = {
|
||||
readonly environmentId: EnvironmentId | null;
|
||||
readonly providerInstanceId?: ProviderInstanceId;
|
||||
readonly providerGroups: ReadonlyArray<ProviderGroup>;
|
||||
readonly favoriteKeys: ReadonlySet<string>;
|
||||
readonly favoritesLoaded: boolean;
|
||||
readonly toggleFavorite: (option: ModelOption) => void;
|
||||
readonly runtimeMode: RuntimeMode;
|
||||
readonly onUpdateRuntimeMode: (mode: RuntimeMode) => void;
|
||||
readonly displayedDescriptors: ReadonlyArray<ProviderOptionDescriptor>;
|
||||
@@ -410,6 +449,32 @@ const ThreadSettingsSessionContext = createContext<ThreadSettingsSessionValue |
|
||||
function ThreadSettingsSessionProvider(
|
||||
props: ThreadSettingsSessionProps & { readonly children: ReactNode },
|
||||
) {
|
||||
const preferences = useAtomValue(mobilePreferencesAtom);
|
||||
const savePreferences = useAtomSet(updateMobilePreferencesAtom);
|
||||
const favoritesLoaded = AsyncResult.isSuccess(preferences);
|
||||
const modelFavorites = favoritesLoaded
|
||||
? (preferences.value.modelFavorites ?? EMPTY_MODEL_FAVORITES)
|
||||
: EMPTY_MODEL_FAVORITES;
|
||||
const favoriteKeys = useMemo(
|
||||
() =>
|
||||
new Set(
|
||||
modelFavorites.map((favorite) => modelFavoriteKey(favorite.provider, favorite.model)),
|
||||
),
|
||||
[modelFavorites],
|
||||
);
|
||||
const toggleFavorite = useCallback(
|
||||
(option: ModelOption) => {
|
||||
if (!favoritesLoaded) return;
|
||||
void Haptics.selectionAsync();
|
||||
savePreferences((current: Preferences) => ({
|
||||
modelFavorites: toggleModelFavorite(
|
||||
current.modelFavorites ?? EMPTY_MODEL_FAVORITES,
|
||||
option,
|
||||
),
|
||||
}));
|
||||
},
|
||||
[favoritesLoaded, savePreferences],
|
||||
);
|
||||
const [showLegacyToggle, setShowLegacyToggle] = useState(false);
|
||||
const [providerFilter, setProviderFilter] = useState<string | null>(null);
|
||||
const [searchQuery, setSearchQuery] = useState("");
|
||||
@@ -515,6 +580,8 @@ function ThreadSettingsSessionProvider(
|
||||
runtimeMode: props.runtimeMode,
|
||||
onUpdateRuntimeMode: props.onUpdateRuntimeMode,
|
||||
displayedDescriptors,
|
||||
favoriteKeys,
|
||||
favoritesLoaded,
|
||||
providerExpansionOverrides,
|
||||
hasLegacyModels,
|
||||
pendingModel,
|
||||
@@ -530,11 +597,14 @@ function ThreadSettingsSessionProvider(
|
||||
setSearchQuery,
|
||||
setShowLegacy: setShowLegacyToggle,
|
||||
toggleProvider,
|
||||
toggleFavorite,
|
||||
}),
|
||||
[
|
||||
applyOptionChange,
|
||||
commitPendingModel,
|
||||
displayedDescriptors,
|
||||
favoriteKeys,
|
||||
favoritesLoaded,
|
||||
providerExpansionOverrides,
|
||||
hasLegacyModels,
|
||||
isApplied,
|
||||
@@ -550,6 +620,7 @@ function ThreadSettingsSessionProvider(
|
||||
searchQuery,
|
||||
showLegacyToggle,
|
||||
toggleProvider,
|
||||
toggleFavorite,
|
||||
],
|
||||
);
|
||||
|
||||
@@ -616,6 +687,9 @@ function ThreadSettingsModelListRow(props: {
|
||||
isFirst={props.isFirst}
|
||||
isLast={props.isLast}
|
||||
onPress={onPress}
|
||||
isFavorite={session.favoriteKeys.has(props.option.key)}
|
||||
favoritesLoaded={session.favoritesLoaded}
|
||||
onToggleFavorite={() => session.toggleFavorite(props.option)}
|
||||
option={props.option}
|
||||
selected={session.isDisplayed(props.option)}
|
||||
/>
|
||||
@@ -649,19 +723,35 @@ function useThreadSettingsCatalogItems(
|
||||
return useMemo(
|
||||
() =>
|
||||
session.providerGroups.flatMap((group) => {
|
||||
if (session.providerFilter !== null && group.providerKey !== session.providerFilter) {
|
||||
if (
|
||||
session.providerFilter !== null &&
|
||||
session.providerFilter !== FAVORITES_PROVIDER_FILTER &&
|
||||
group.providerKey !== session.providerFilter
|
||||
) {
|
||||
return [];
|
||||
}
|
||||
const driver = group.models[0]?.providerDriver ?? group.providerKey;
|
||||
const catalogModels = session.showLegacy
|
||||
? group.models
|
||||
: group.models.filter((model) => !model.isLegacy || session.isDisplayed(model));
|
||||
const visibleModels = catalogModels.filter((model) =>
|
||||
modelMatchesCatalogQuery({
|
||||
model,
|
||||
providerLabel: group.providerLabel,
|
||||
query: session.searchQuery,
|
||||
}),
|
||||
const catalogModels =
|
||||
session.showLegacy || session.providerFilter === FAVORITES_PROVIDER_FILTER
|
||||
? group.models
|
||||
: group.models.filter(
|
||||
(model) =>
|
||||
!model.isLegacy ||
|
||||
session.isDisplayed(model) ||
|
||||
session.favoriteKeys.has(model.key),
|
||||
);
|
||||
const visibleModels = favoritesFirst(
|
||||
catalogModels.filter(
|
||||
(model) =>
|
||||
(session.providerFilter !== FAVORITES_PROVIDER_FILTER ||
|
||||
session.favoriteKeys.has(model.key)) &&
|
||||
modelMatchesCatalogQuery({
|
||||
model,
|
||||
providerLabel: group.providerLabel,
|
||||
query: session.searchQuery,
|
||||
}),
|
||||
),
|
||||
session.favoriteKeys,
|
||||
);
|
||||
if (visibleModels.length === 0) {
|
||||
return [];
|
||||
@@ -705,6 +795,7 @@ function useThreadSettingsCatalogItems(
|
||||
[
|
||||
session.isApplied,
|
||||
session.isDisplayed,
|
||||
session.favoriteKeys,
|
||||
session.providerExpansionOverrides,
|
||||
session.providerFilter,
|
||||
session.providerGroups,
|
||||
@@ -834,7 +925,12 @@ function ThreadSettingsMainContent(props: {
|
||||
content = (
|
||||
<View className="items-center px-8 py-14">
|
||||
<Text className="text-center text-sm text-foreground-muted">
|
||||
{hasActiveCatalogFilter ? "No matching models" : "No available models"}
|
||||
{session.providerFilter === FAVORITES_PROVIDER_FILTER &&
|
||||
session.searchQuery.trim().length === 0
|
||||
? "No favorite models"
|
||||
: hasActiveCatalogFilter
|
||||
? "No matching models"
|
||||
: "No available models"}
|
||||
</Text>
|
||||
</View>
|
||||
);
|
||||
@@ -857,7 +953,13 @@ function ThreadSettingsMainContent(props: {
|
||||
</Animated.View>
|
||||
);
|
||||
},
|
||||
[animationsReady, hasActiveCatalogFilter, props.onOpenSubmenu],
|
||||
[
|
||||
animationsReady,
|
||||
hasActiveCatalogFilter,
|
||||
props.onOpenSubmenu,
|
||||
session.providerFilter,
|
||||
session.searchQuery,
|
||||
],
|
||||
);
|
||||
|
||||
return (
|
||||
@@ -918,6 +1020,38 @@ function ThreadSettingsMainContent(props: {
|
||||
/>
|
||||
) : null}
|
||||
</View>
|
||||
<View className="flex-row gap-2 pt-3">
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
accessibilityState={{
|
||||
selected: session.providerFilter !== FAVORITES_PROVIDER_FILTER,
|
||||
}}
|
||||
className={cn(
|
||||
"rounded-full px-4 py-2",
|
||||
session.providerFilter !== FAVORITES_PROVIDER_FILTER
|
||||
? "bg-subtle-strong"
|
||||
: "bg-subtle",
|
||||
)}
|
||||
onPress={() => session.setProviderFilter(null)}
|
||||
>
|
||||
<Text className="text-sm font-t3-medium text-foreground">All</Text>
|
||||
</Pressable>
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
accessibilityState={{
|
||||
selected: session.providerFilter === FAVORITES_PROVIDER_FILTER,
|
||||
}}
|
||||
className={cn(
|
||||
"rounded-full px-4 py-2",
|
||||
session.providerFilter === FAVORITES_PROVIDER_FILTER
|
||||
? "bg-subtle-strong"
|
||||
: "bg-subtle",
|
||||
)}
|
||||
onPress={() => session.setProviderFilter(FAVORITES_PROVIDER_FILTER)}
|
||||
>
|
||||
<Text className="text-sm font-t3-medium text-foreground">Favorites</Text>
|
||||
</Pressable>
|
||||
</View>
|
||||
</View>
|
||||
) : null}
|
||||
</>
|
||||
@@ -1077,6 +1211,15 @@ function ThreadSettingsModelsScreen() {
|
||||
state: session.providerFilter === null ? ("on" as const) : ("off" as const),
|
||||
onPress: () => session.setProviderFilter(null),
|
||||
},
|
||||
{
|
||||
type: "action" as const,
|
||||
title: "Favorites",
|
||||
state:
|
||||
session.providerFilter === FAVORITES_PROVIDER_FILTER
|
||||
? ("on" as const)
|
||||
: ("off" as const),
|
||||
onPress: () => session.setProviderFilter(FAVORITES_PROVIDER_FILTER),
|
||||
},
|
||||
...session.providerGroups.map((group) => ({
|
||||
type: "action" as const,
|
||||
title: group.providerLabel,
|
||||
@@ -1213,6 +1356,12 @@ function ThreadSettingsModelsScreen() {
|
||||
>
|
||||
All providers
|
||||
</NativeHeaderToolbar.MenuAction>
|
||||
<NativeHeaderToolbar.MenuAction
|
||||
isOn={session.providerFilter === FAVORITES_PROVIDER_FILTER}
|
||||
onPress={() => session.setProviderFilter(FAVORITES_PROVIDER_FILTER)}
|
||||
>
|
||||
Favorites
|
||||
</NativeHeaderToolbar.MenuAction>
|
||||
{session.providerGroups.map((group) => (
|
||||
<NativeHeaderToolbar.MenuAction
|
||||
key={group.providerKey}
|
||||
|
||||
@@ -5,8 +5,11 @@ import { ProviderInstanceId, type ProviderOptionSelection } from "@t3tools/contr
|
||||
import type { ModelOption } from "../../lib/modelOptions";
|
||||
import {
|
||||
canCommitPendingModel,
|
||||
favoritesFirst,
|
||||
modelFavoriteKey,
|
||||
modelMatchesCatalogQuery,
|
||||
pendingModelAfterPress,
|
||||
toggleModelFavorite,
|
||||
} from "./thread-settings-sheet-state";
|
||||
|
||||
function modelOption(
|
||||
@@ -32,6 +35,46 @@ function modelOption(
|
||||
}
|
||||
|
||||
describe("thread settings sheet state", () => {
|
||||
it("keeps favorites in catalog order ahead of other models", () => {
|
||||
const models = [
|
||||
modelOption("first"),
|
||||
modelOption("second"),
|
||||
modelOption("third"),
|
||||
modelOption("fourth"),
|
||||
];
|
||||
const favorites = new Set([models[2]!.key, models[0]!.key]);
|
||||
|
||||
expect(favoritesFirst(models, favorites).map((model) => model.selection.model)).toEqual([
|
||||
"first",
|
||||
"third",
|
||||
"second",
|
||||
"fourth",
|
||||
]);
|
||||
expect(models.map((model) => model.selection.model)).toEqual([
|
||||
"first",
|
||||
"second",
|
||||
"third",
|
||||
"fourth",
|
||||
]);
|
||||
});
|
||||
|
||||
it("adds and removes favorites for one provider instance", () => {
|
||||
const codexModel = modelOption("shared");
|
||||
const otherProvider = ProviderInstanceId.make("codex_personal");
|
||||
const personalModel = {
|
||||
...codexModel,
|
||||
key: modelFavoriteKey(otherProvider, "shared"),
|
||||
selection: { ...codexModel.selection, instanceId: otherProvider },
|
||||
};
|
||||
const favorites = toggleModelFavorite([], codexModel);
|
||||
|
||||
expect(toggleModelFavorite(favorites, personalModel)).toEqual([
|
||||
{ provider: ProviderInstanceId.make("codex"), model: "shared" },
|
||||
{ provider: otherProvider, model: "shared" },
|
||||
]);
|
||||
expect(toggleModelFavorite(favorites, codexModel)).toEqual([]);
|
||||
});
|
||||
|
||||
it("matches visible model and provider terms", () => {
|
||||
const model = modelOption("gpt-next");
|
||||
|
||||
|
||||
@@ -1,4 +1,38 @@
|
||||
import type { ModelOption, ProviderGroup } from "../../lib/modelOptions";
|
||||
import type { ProviderInstanceId } from "@t3tools/contracts";
|
||||
|
||||
export type ModelFavorite = {
|
||||
readonly provider: ProviderInstanceId;
|
||||
readonly model: string;
|
||||
};
|
||||
|
||||
export function modelFavoriteKey(provider: ProviderInstanceId, model: string): string {
|
||||
return `${provider}:${model}`;
|
||||
}
|
||||
|
||||
export function toggleModelFavorite(
|
||||
favorites: ReadonlyArray<ModelFavorite>,
|
||||
option: ModelOption,
|
||||
): ReadonlyArray<ModelFavorite> {
|
||||
const provider = option.selection.instanceId;
|
||||
const model = option.selection.model;
|
||||
return favorites.some((favorite) => favorite.provider === provider && favorite.model === model)
|
||||
? favorites.filter((favorite) => favorite.provider !== provider || favorite.model !== model)
|
||||
: [...favorites, { provider, model }];
|
||||
}
|
||||
|
||||
/** Keep catalog order within each group when favorites move to the front. */
|
||||
export function favoritesFirst(
|
||||
models: ReadonlyArray<ModelOption>,
|
||||
favoriteKeys: ReadonlySet<string>,
|
||||
): ReadonlyArray<ModelOption> {
|
||||
const favorites: ModelOption[] = [];
|
||||
const others: ModelOption[] = [];
|
||||
for (const model of models) {
|
||||
(favoriteKeys.has(model.key) ? favorites : others).push(model);
|
||||
}
|
||||
return [...favorites, ...others];
|
||||
}
|
||||
|
||||
/** Match the terms a user can actually see or recognize in the model picker. */
|
||||
export function modelMatchesCatalogQuery(input: {
|
||||
|
||||
@@ -5,7 +5,7 @@ import * as Option from "effect/Option";
|
||||
import * as Ref from "effect/Ref";
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as Semaphore from "effect/Semaphore";
|
||||
import type { SidebarProjectGroupingMode } from "@t3tools/contracts";
|
||||
import type { ProviderInstanceId, SidebarProjectGroupingMode } from "@t3tools/contracts";
|
||||
import type { ComposerEnterBehavior } from "../lib/composerEnterBehavior";
|
||||
import { MOBILE_THEME_IDS, type MobileThemeId, type MobileThemeMode } from "../lib/mobileTheme";
|
||||
import * as MobileDatabase from "./mobile-database";
|
||||
@@ -43,6 +43,11 @@ export interface Preferences {
|
||||
readonly legacyThreadListEnabled?: boolean;
|
||||
/** Device-local counterpart of desktop's `planModeEnabled` legacy flag. */
|
||||
readonly planModeEnabled?: boolean;
|
||||
/** Model favorites belong to this device, like the web client setting. */
|
||||
readonly modelFavorites?: ReadonlyArray<{
|
||||
readonly provider: ProviderInstanceId;
|
||||
readonly model: string;
|
||||
}>;
|
||||
/** Fresh keys reset both shelves to collapsed when users update. */
|
||||
readonly threadListSettledShelfExpanded?: boolean;
|
||||
readonly threadListSnoozedShelfExpanded?: boolean;
|
||||
@@ -104,6 +109,7 @@ function sanitizePreferences(parsed: Preferences): Preferences {
|
||||
projectGroupingMode?: SidebarProjectGroupingMode;
|
||||
legacyThreadListEnabled?: boolean;
|
||||
planModeEnabled?: boolean;
|
||||
modelFavorites?: Preferences["modelFavorites"];
|
||||
threadListSettledShelfExpanded?: boolean;
|
||||
threadListSnoozedShelfExpanded?: boolean;
|
||||
} = {};
|
||||
@@ -176,6 +182,17 @@ function sanitizePreferences(parsed: Preferences): Preferences {
|
||||
if (typeof parsed.planModeEnabled === "boolean") {
|
||||
preferences.planModeEnabled = parsed.planModeEnabled;
|
||||
}
|
||||
if (Array.isArray(parsed.modelFavorites)) {
|
||||
preferences.modelFavorites = parsed.modelFavorites.filter(
|
||||
(favorite) =>
|
||||
typeof favorite === "object" &&
|
||||
favorite !== null &&
|
||||
typeof favorite.provider === "string" &&
|
||||
favorite.provider.length > 0 &&
|
||||
typeof favorite.model === "string" &&
|
||||
favorite.model.trim().length > 0,
|
||||
);
|
||||
}
|
||||
if (typeof parsed.threadListSettledShelfExpanded === "boolean") {
|
||||
preferences.threadListSettledShelfExpanded = parsed.threadListSettledShelfExpanded;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import * as Layer from "effect/Layer";
|
||||
import * as Option from "effect/Option";
|
||||
import { AsyncResult, Atom, AtomRegistry } from "effect/unstable/reactivity";
|
||||
import { vi } from "vite-plus/test";
|
||||
import { ProviderInstanceId } from "@t3tools/contracts";
|
||||
|
||||
vi.mock("expo-secure-store", () => ({
|
||||
getItemAsync: vi.fn(),
|
||||
@@ -124,6 +125,45 @@ describe("mobile preferences state", () => {
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("keeps both favorites when updates are sent before a render", () =>
|
||||
Effect.gen(function* () {
|
||||
let persisted: Preferences = { modelFavorites: [] };
|
||||
const state = makePreferencesState({
|
||||
load: Effect.succeed(persisted),
|
||||
savePatch: () => Effect.die(new Error("Favorite updates must use a transform.")),
|
||||
update: (transform) =>
|
||||
Effect.sync(() => {
|
||||
persisted = { ...persisted, ...transform(persisted) };
|
||||
return persisted;
|
||||
}),
|
||||
});
|
||||
const registry = AtomRegistry.make();
|
||||
const unmountPreferences = registry.mount(state.preferencesAtom);
|
||||
const unmountUpdate = registry.mount(state.updatePreferencesAtom);
|
||||
yield* AtomRegistry.getResult(registry, state.preferencesAtom, { suspendOnWaiting: true });
|
||||
|
||||
const provider = ProviderInstanceId.make("codex");
|
||||
registry.set(state.updatePreferencesAtom, (current) => ({
|
||||
modelFavorites: [...(current.modelFavorites ?? []), { provider, model: "astra" }],
|
||||
}));
|
||||
registry.set(state.updatePreferencesAtom, (current) => ({
|
||||
modelFavorites: [...(current.modelFavorites ?? []), { provider, model: "sol" }],
|
||||
}));
|
||||
yield* AtomRegistry.getResult(registry, state.updatePreferencesAtom, {
|
||||
suspendOnWaiting: true,
|
||||
});
|
||||
|
||||
expect(persisted.modelFavorites).toEqual([
|
||||
{ provider, model: "astra" },
|
||||
{ provider, model: "sol" },
|
||||
]);
|
||||
|
||||
unmountUpdate();
|
||||
unmountPreferences();
|
||||
registry.dispose();
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("falls back to empty preferences when secure storage cannot be read", () =>
|
||||
Effect.gen(function* () {
|
||||
const state = makePreferencesState({
|
||||
|
||||
@@ -15,6 +15,8 @@ interface OptimisticPreferences {
|
||||
readonly versions: Partial<Record<keyof Preferences, number>>;
|
||||
}
|
||||
|
||||
type PreferencesUpdate = Partial<Preferences> | ((current: Preferences) => Partial<Preferences>);
|
||||
|
||||
/**
|
||||
* Owns the device preference blob for the lifetime of the app registry.
|
||||
* Optimistic patches are kept separately so writes made while persistence is
|
||||
@@ -57,7 +59,12 @@ export function createMobilePreferencesState(runtime: Atom.AtomRuntime<MobilePre
|
||||
|
||||
const updatePreferencesAtom = runtime
|
||||
.fn(
|
||||
(patch: Partial<Preferences>, get) => {
|
||||
(update: PreferencesUpdate, get) => {
|
||||
const currentPreferences = get(preferencesAtom);
|
||||
const patch =
|
||||
typeof update === "function"
|
||||
? update(AsyncResult.isSuccess(currentPreferences) ? currentPreferences.value : {})
|
||||
: update;
|
||||
const version = ++nextPatchVersion;
|
||||
const current = get(optimisticPatchAtom);
|
||||
const versions = { ...current.versions };
|
||||
@@ -69,7 +76,9 @@ export function createMobilePreferencesState(runtime: Atom.AtomRuntime<MobilePre
|
||||
versions,
|
||||
});
|
||||
return MobilePreferencesStore.pipe(
|
||||
Effect.flatMap((store) => store.savePatch(patch)),
|
||||
Effect.flatMap((store) =>
|
||||
typeof update === "function" ? store.update(update) : store.savePatch(patch),
|
||||
),
|
||||
Effect.tap((saved) =>
|
||||
Effect.sync(() => {
|
||||
get.set(confirmedPreferencesAtom, saved);
|
||||
|
||||
Reference in new issue
Block a user