Files
t3code/apps/mobile/src/features/threads/ThreadSettingsSheet.tsx
T

1386 lines
47 KiB
TypeScript

import type {
EnvironmentId,
ModelSelection,
ProviderInstanceId,
ProviderOptionDescriptor,
ProviderOptionSelection,
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 {
getProviderOptionCurrentLabel,
getProviderOptionCurrentValue,
getProviderOptionDescriptors,
} from "@t3tools/shared/model";
import { useNavigation, useRoute, type RouteProp } from "@react-navigation/native";
import {
createNativeStackNavigator,
type NativeStackNavigationProp,
} from "@react-navigation/native-stack";
import * as Haptics from "expo-haptics";
import { AsyncResult } from "effect/unstable/reactivity";
import {
createContext,
use,
useCallback,
useEffect,
useMemo,
useState,
type ReactNode,
} from "react";
import {
Alert,
Platform,
Pressable,
RefreshControl,
ScrollView,
TextInput,
View,
} from "react-native";
import Animated, { FadeIn, FadeOut, LinearTransition } from "react-native-reanimated";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { SymbolView } from "../../components/AppSymbol";
import { AppText as Text } from "../../components/AppText";
import { AndroidScreenHeader } from "../../components/AndroidScreenHeader";
import { AndroidAnchoredMenu } from "../../components/AndroidAnchoredMenu";
import { MaterialButton } from "../../components/MaterialButton";
import { MaterialIconButton } from "../../components/MaterialIconButton";
import { ProviderIcon } from "../../components/ProviderIcon";
import { ThemedSwitch } from "../../components/ThemedSwitch";
import { cn } from "../../lib/cn";
import type { ModelOption, ProviderGroup } from "../../lib/modelOptions";
import { applyProviderOptionSelection } from "../../lib/providerOptions";
import { resolveProviderOptionDescriptors } from "../../lib/providerOptions";
import { useUniwindTheme } from "../../lib/useUniwindTheme";
import {
NativeHeaderToolbar,
NativeStackScreenOptions,
nativeHeaderScrollEdgeEffects,
} 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";
import {
createProviderCatalogRefreshRunner,
providerCatalogRefreshError,
} from "./provider-catalog-refresh";
import {
createNativeMailSearchToolbarItem,
NATIVE_MAIL_SEARCH_TOOLBAR_CONTENT_INSET,
NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED,
} from "../layout/native-mail-search-toolbar";
import { ModelRow, ChoiceRow } from "./ThreadSettingsRows";
import { RUNTIME_MODE_CHOICES, selectableChoices } from "./thread-settings-options";
import {
canCommitPendingModel,
favoritesFirst,
modelFavoriteKey,
modelMatchesCatalogQuery,
pendingModelAfterPress,
providerSectionIsCollapsed,
toggleModelFavorite,
} from "./thread-settings-sheet-state";
/**
* Everyday harnesses start expanded; every other provider (OpenRouter catalogs
* and friends) starts folded so a 300-model catalog cannot bury the list. All
* provider headers remain user-collapsible.
*/
const PRIMARY_PROVIDER_DRIVERS: ReadonlySet<string> = new Set([
"claudeAgent",
"codex",
"antigravity",
]);
/**
* Keep measured row changes stable, but let catalog mutations use the list's
* native bounds so a filtered catalog that underflows returns to the top.
*/
const THREAD_SETTINGS_MAINTAIN_VISIBLE_CONTENT_POSITION = {
data: false,
size: true,
} as const;
const THREAD_SETTINGS_CATALOG_LAYOUT_TRANSITION = LinearTransition.duration(180);
const THREAD_SETTINGS_CATALOG_ENTER_TRANSITION = FadeIn.duration(140);
const THREAD_SETTINGS_CATALOG_EXIT_TRANSITION = FadeOut.duration(120);
const THREAD_SETTINGS_OPTIONS_LAYOUT_TRANSITION = LinearTransition.duration(180);
const THREAD_SETTINGS_OPTION_ENTER_TRANSITION = FadeIn.duration(140);
const THREAD_SETTINGS_OPTION_EXIT_TRANSITION = FadeOut.duration(100);
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";
/** Provider catalog header with its harness logo and disclosure state. */
function ProviderHeader(props: {
readonly driver: string | undefined;
readonly label: string;
readonly collapsible: boolean;
readonly collapsed: boolean;
readonly modelCount: number;
readonly onToggle: () => void;
}) {
const content = (
<>
<ProviderIcon provider={props.driver} size={15} />
<Text className="text-sm font-t3-medium text-foreground-muted">{props.label}</Text>
{props.collapsible ? (
<>
<View className="flex-1" />
{props.collapsed ? (
<Text className="text-2xs font-t3-medium text-foreground-muted">
{props.modelCount}
</Text>
) : null}
<SymbolView
name={props.collapsed ? "chevron.down" : "chevron.up"}
size={12}
tintColorClassName="accent-icon-subtle"
type="monochrome"
/>
</>
) : null}
</>
);
if (props.collapsible) {
return (
<Pressable
accessibilityLabel={`${props.label}, ${props.modelCount} models`}
accessibilityRole="button"
accessibilityState={{ expanded: !props.collapsed }}
className="mx-4 mt-1 min-h-11 flex-row items-center gap-2 rounded-xl px-1 pt-2 active:opacity-60 android:min-h-12"
onPress={props.onToggle}
>
{content}
</Pressable>
);
}
return (
<View accessibilityRole="header" className="mx-4 min-h-9 flex-row items-center gap-2 px-1 pt-1">
{content}
</View>
);
}
/** Compact row that opens a single-choice submenu panel. */
function DisclosureRow(props: {
readonly label: string;
readonly value: string | undefined;
readonly onPress: () => void;
readonly isLast?: boolean;
}) {
return (
<Pressable
accessibilityRole="button"
onPress={props.onPress}
className={cn(
"min-h-11 flex-row items-center gap-2 bg-card px-4 py-2 active:bg-subtle android:min-h-14",
!props.isLast && "border-b border-border-subtle",
)}
>
<Text className="text-sm font-t3-medium text-foreground">{props.label}</Text>
<View className="flex-1" />
{props.value ? (
<Text className="text-sm text-foreground-muted" numberOfLines={1}>
{props.value}
</Text>
) : null}
<SymbolView
name="chevron.right"
size={12}
tintColorClassName="accent-icon-subtle"
type="monochrome"
/>
</Pressable>
);
}
function SwitchRow(props: {
readonly label: string;
readonly value: boolean;
readonly onValueChange: (value: boolean) => void;
readonly isLast?: boolean;
}) {
return (
<View
className={cn(
"min-h-11 flex-row items-center justify-between bg-card px-4 py-1",
!props.isLast && "border-b border-border-subtle",
)}
>
<Text className="text-sm font-t3-medium text-foreground">{props.label}</Text>
<ThemedSwitch
accessibilityLabel={props.label}
onValueChange={props.onValueChange}
value={props.value}
/>
</View>
);
}
type ThreadSettingsSubmenuPage =
| { readonly kind: "descriptor"; readonly id: string }
| { readonly kind: "runtime" };
type ThreadSettingsSessionProps = {
readonly environmentId: EnvironmentId | null;
readonly providerInstanceId?: ProviderInstanceId;
readonly providerGroups: ReadonlyArray<ProviderGroup>;
readonly selectedModel: ModelSelection | null;
readonly onSelectModel: (option: ModelOption) => void;
readonly optionDescriptors: ReadonlyArray<ProviderOptionDescriptor>;
readonly onUpdateOptionSelections: (selections: ReadonlyArray<ProviderOptionSelection>) => void;
readonly runtimeMode: RuntimeMode;
readonly onUpdateRuntimeMode: (mode: RuntimeMode) => void;
};
export type ExistingThreadSettingsRouteSession = ThreadSettingsSessionProps & {
readonly ownerId: string;
};
type ExistingThreadSettingsRouteContextValue = {
readonly session: ExistingThreadSettingsRouteSession | null;
readonly present: (session: ExistingThreadSettingsRouteSession) => void;
readonly clear: (ownerId: string) => void;
};
const ExistingThreadSettingsRouteContext =
createContext<ExistingThreadSettingsRouteContextValue | null>(null);
/** Bridges the active thread's settings state into the root native sheet route. */
export function ExistingThreadSettingsRouteProvider(props: { readonly children: ReactNode }) {
const [session, setSession] = useState<ExistingThreadSettingsRouteSession | null>(null);
const present = useCallback((nextSession: ExistingThreadSettingsRouteSession) => {
setSession(nextSession);
}, []);
const clear = useCallback((ownerId: string) => {
setSession((current) => (current?.ownerId === ownerId ? null : current));
}, []);
const value = useMemo(() => ({ session, present, clear }), [clear, present, session]);
return (
<ExistingThreadSettingsRouteContext.Provider value={value}>
{props.children}
</ExistingThreadSettingsRouteContext.Provider>
);
}
export function useExistingThreadSettingsRoutePresentation() {
const value = use(ExistingThreadSettingsRouteContext);
if (!value) {
throw new Error(
"useExistingThreadSettingsRoutePresentation must be used inside ExistingThreadSettingsRouteProvider.",
);
}
return value;
}
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>;
readonly providerExpansionOverrides: ReadonlySet<string>;
readonly hasLegacyModels: boolean;
readonly pendingModel: ModelOption | null;
readonly providerFilter: string | null;
readonly searchQuery: string;
readonly showLegacy: boolean;
readonly applyOptionChange: (id: string, value: string | boolean) => void;
readonly commitPendingModel: () => boolean;
readonly isApplied: (option: ModelOption) => boolean;
readonly isDisplayed: (option: ModelOption) => boolean;
readonly pressModel: (option: ModelOption) => void;
readonly setProviderFilter: (providerKey: string | null) => void;
readonly setSearchQuery: (query: string) => void;
readonly setShowLegacy: (showLegacy: boolean) => void;
readonly toggleProvider: (providerKey: string) => void;
};
const ThreadSettingsSessionContext = createContext<ThreadSettingsSessionValue | null>(null);
/** Owns the staged model and option state for one picker presentation. */
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({
transform: (current) => ({
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("");
const [providerExpansionOverrides, setProviderExpansionOverrides] = useState<ReadonlySet<string>>(
() => new Set(),
);
const [pendingModel, setPendingModel] = useState<ModelOption | null>(null);
const isApplied = useCallback(
(option: ModelOption) =>
option.selection.instanceId === props.selectedModel?.instanceId &&
option.selection.model === props.selectedModel.model,
[props.selectedModel],
);
// The list highlights the staged pick; Save turns it into the applied one.
const isDisplayed = useCallback(
(option: ModelOption) => (pendingModel ? option.key === pendingModel.key : isApplied(option)),
[isApplied, pendingModel],
);
// While a model is staged, the settings rows describe and edit the staged
// model's options (kept on its pending selection); Save applies model and
// options together. Otherwise they edit the applied selection directly.
const displayedDescriptors = useMemo(
() =>
pendingModel
? pendingModel.capabilities
? getProviderOptionDescriptors({
caps: pendingModel.capabilities,
selections: pendingModel.selection.options,
})
: []
: props.optionDescriptors,
[pendingModel, props.optionDescriptors],
);
const hasLegacyModels = useMemo(
() => props.providerGroups.some((group) => group.models.some((model) => model.isLegacy)),
[props.providerGroups],
);
const commitPendingModel = useCallback(() => {
if (pendingModel) {
if (!canCommitPendingModel(pendingModel, props.providerGroups)) {
Alert.alert(
"Model unavailable",
"Set up this provider on web or desktop, or select another model.",
);
return false;
}
void Haptics.selectionAsync();
props.onSelectModel(pendingModel);
}
return true;
}, [pendingModel, props.onSelectModel, props.providerGroups]);
const applyOptionChange = useCallback(
(id: string, value: string | boolean) => {
const next = applyProviderOptionSelection(displayedDescriptors, { id, value });
if (!next) {
return;
}
if (pendingModel) {
setPendingModel({
...pendingModel,
selection: { ...pendingModel.selection, options: next },
});
} else {
props.onUpdateOptionSelections(next);
}
},
[displayedDescriptors, pendingModel, props.onUpdateOptionSelections],
);
const toggleProvider = useCallback((providerKey: string) => {
setProviderExpansionOverrides((current) => {
const next = new Set(current);
if (!next.delete(providerKey)) {
next.add(providerKey);
}
return next;
});
}, []);
const pressModel = useCallback(
(option: ModelOption) => {
void Haptics.selectionAsync();
setPendingModel((current) =>
pendingModelAfterPress({
current,
pressed: option,
pressedIsApplied: isApplied(option),
}),
);
},
[isApplied],
);
const value = useMemo<ThreadSettingsSessionValue>(
() => ({
environmentId: props.environmentId,
providerInstanceId: props.providerInstanceId,
providerGroups: props.providerGroups,
runtimeMode: props.runtimeMode,
onUpdateRuntimeMode: props.onUpdateRuntimeMode,
displayedDescriptors,
favoriteKeys,
favoritesLoaded,
providerExpansionOverrides,
hasLegacyModels,
pendingModel,
providerFilter,
searchQuery,
showLegacy: showLegacyToggle,
applyOptionChange,
commitPendingModel,
isApplied,
isDisplayed,
pressModel,
setProviderFilter,
setSearchQuery,
setShowLegacy: setShowLegacyToggle,
toggleProvider,
toggleFavorite,
}),
[
applyOptionChange,
commitPendingModel,
displayedDescriptors,
favoriteKeys,
favoritesLoaded,
providerExpansionOverrides,
hasLegacyModels,
isApplied,
isDisplayed,
props.environmentId,
props.providerInstanceId,
pendingModel,
pressModel,
providerFilter,
props.onUpdateRuntimeMode,
props.providerGroups,
props.runtimeMode,
searchQuery,
showLegacyToggle,
toggleProvider,
toggleFavorite,
],
);
return (
<ThreadSettingsSessionContext.Provider value={value}>
{props.children}
</ThreadSettingsSessionContext.Provider>
);
}
function useThreadSettingsSession() {
const value = use(ThreadSettingsSessionContext);
if (!value) {
throw new Error("useThreadSettingsSession must be used inside ThreadSettingsSessionProvider.");
}
return value;
}
type ThreadSettingsProviderCatalog = {
readonly key: string;
readonly driver: string | undefined;
readonly label: string;
readonly collapsible: boolean;
readonly collapsed: boolean;
readonly modelCount: number;
readonly models: ReadonlyArray<ModelOption>;
};
type ThreadSettingsCatalogItem =
| {
readonly kind: "provider";
readonly key: string;
readonly provider: ThreadSettingsProviderCatalog;
}
| {
readonly kind: "model";
readonly key: string;
readonly option: ModelOption;
readonly isFirst: boolean;
readonly isLast: boolean;
}
| {
readonly kind: "empty";
readonly key: "empty";
}
| {
readonly kind: "options";
readonly key: "options";
};
function ThreadSettingsModelListRow(props: {
readonly option: ModelOption;
readonly isFirst: boolean;
readonly isLast: boolean;
}) {
const session = useThreadSettingsSession();
const onPress = useCallback(
() => session.pressModel(props.option),
[props.option, session.pressModel],
);
return (
<ModelRow
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)}
/>
);
}
function ThreadSettingsProviderListHeader(props: {
readonly provider: ThreadSettingsProviderCatalog;
}) {
const session = useThreadSettingsSession();
const onToggle = useCallback(
() => session.toggleProvider(props.provider.key),
[props.provider.key, session.toggleProvider],
);
return (
<ProviderHeader
collapsible={props.provider.collapsible}
collapsed={props.provider.collapsed}
driver={props.provider.driver}
label={props.provider.label}
modelCount={props.provider.modelCount}
onToggle={onToggle}
/>
);
}
function useThreadSettingsCatalogItems(
session: ThreadSettingsSessionValue,
): ReadonlyArray<ThreadSettingsCatalogItem> {
return useMemo(
() =>
session.providerGroups.flatMap((group) => {
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 || 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 [];
}
const isPrimary = driver !== undefined && PRIMARY_PROVIDER_DRIVERS.has(driver);
// Staging a model must not change disclosure state. The applied model
// stays stable for the lifetime of this picker (Save closes it), so it
// is safe to use as the initial selected-provider default.
const containsAppliedSelection = group.models.some(session.isApplied);
const isNarrowed = session.providerFilter !== null || session.searchQuery.trim().length > 0;
const collapsible = !isNarrowed;
const collapsed = providerSectionIsCollapsed({
defaultExpanded: isPrimary || containsAppliedSelection,
hasExpansionOverride: session.providerExpansionOverrides.has(group.providerKey),
isNarrowed,
});
const provider: ThreadSettingsProviderCatalog = {
key: group.providerKey,
driver,
label: group.providerLabel,
collapsible,
collapsed,
modelCount: visibleModels.length,
models: collapsed ? [] : visibleModels,
};
return [
{
kind: "provider" as const,
key: `provider:${group.providerKey}`,
provider,
},
...provider.models.map((option, index) => ({
kind: "model" as const,
key: `model:${option.key}`,
option,
isFirst: index === 0,
isLast: index === provider.models.length - 1,
})),
];
}),
[
session.isApplied,
session.isDisplayed,
session.favoriteKeys,
session.providerExpansionOverrides,
session.providerFilter,
session.providerGroups,
session.searchQuery,
session.showLegacy,
],
);
}
function ThreadSettingsOptionsItem(props: {
readonly animationsReady: boolean;
readonly onOpenSubmenu: (submenu: ThreadSettingsSubmenuPage) => void;
}) {
const insets = useSafeAreaInsets();
const session = useThreadSettingsSession();
const bottomToolbarInset =
Platform.OS === "ios" && NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED
? NATIVE_MAIL_SEARCH_TOOLBAR_CONTENT_INSET
: 0;
return (
<View style={{ paddingBottom: insets.bottom + bottomToolbarInset + 12 }}>
<Text className="px-5 pb-2 pt-2 text-sm font-t3-medium text-foreground-muted">Options</Text>
<Animated.View
className="mx-4 overflow-hidden rounded-2xl bg-card"
layout={THREAD_SETTINGS_OPTIONS_LAYOUT_TRANSITION}
>
{session.displayedDescriptors.map((descriptor) => {
if (descriptor.type === "select") {
return (
<Animated.View
key={descriptor.id}
entering={
props.animationsReady ? THREAD_SETTINGS_OPTION_ENTER_TRANSITION : undefined
}
exiting={props.animationsReady ? THREAD_SETTINGS_OPTION_EXIT_TRANSITION : undefined}
layout={THREAD_SETTINGS_OPTIONS_LAYOUT_TRANSITION}
>
<DisclosureRow
label={descriptor.label}
value={getProviderOptionCurrentLabel(descriptor)}
onPress={() => props.onOpenSubmenu({ kind: "descriptor", id: descriptor.id })}
/>
</Animated.View>
);
}
return (
<Animated.View
key={descriptor.id}
entering={props.animationsReady ? THREAD_SETTINGS_OPTION_ENTER_TRANSITION : undefined}
exiting={props.animationsReady ? THREAD_SETTINGS_OPTION_EXIT_TRANSITION : undefined}
layout={THREAD_SETTINGS_OPTIONS_LAYOUT_TRANSITION}
>
<SwitchRow
label={descriptor.label}
value={descriptor.currentValue ?? false}
onValueChange={(value) => session.applyOptionChange(descriptor.id, value)}
/>
</Animated.View>
);
})}
<Animated.View layout={THREAD_SETTINGS_OPTIONS_LAYOUT_TRANSITION}>
<DisclosureRow
isLast
label="Runtime"
value={
RUNTIME_MODE_CHOICES.find((choice) => choice.mode === session.runtimeMode)?.label
}
onPress={() => props.onOpenSubmenu({ kind: "runtime" })}
/>
</Animated.View>
</Animated.View>
{Platform.OS !== "ios" && session.hasLegacyModels ? (
<>
<Text className="px-5 pb-2 pt-7 text-sm font-t3-medium text-foreground-muted">
Catalog
</Text>
<View className="mx-4 overflow-hidden rounded-2xl bg-card">
<SwitchRow
isLast
label="Legacy models"
onValueChange={session.setShowLegacy}
value={session.showLegacy}
/>
</View>
</>
) : null}
</View>
);
}
/** One native scroll owner for the model catalog and its related settings. */
function ThreadSettingsMainContent(props: {
readonly onOpenSubmenu: (submenu: ThreadSettingsSubmenuPage) => void;
}) {
const session = useThreadSettingsSession();
const refreshProvidersCommand = useAtomCommand(serverEnvironment.refreshProviders, {
reportFailure: false,
});
const refreshProviderCatalog = useMemo(
() => createProviderCatalogRefreshRunner(refreshProvidersCommand),
[refreshProvidersCommand],
);
const [isRefreshingProviders, setIsRefreshingProviders] = useState(false);
const refreshProviders = useCallback(() => {
if (!session.environmentId || isRefreshingProviders) return;
setIsRefreshingProviders(true);
void refreshProviderCatalog(session.environmentId).then((result) => {
setIsRefreshingProviders(false);
const error = providerCatalogRefreshError(result);
if (error) Alert.alert("Could not refresh models", error);
});
}, [isRefreshingProviders, refreshProviderCatalog, session.environmentId]);
const catalogItems = useThreadSettingsCatalogItems(session);
const [animationsReady, setAnimationsReady] = useState(false);
const hasActiveCatalogFilter =
session.providerFilter !== null || session.searchQuery.trim().length > 0;
const listItems = useMemo<ReadonlyArray<ThreadSettingsCatalogItem>>(
() => [
...(catalogItems.length === 0 ? ([{ kind: "empty", key: "empty" }] as const) : catalogItems),
{ kind: "options", key: "options" },
],
[catalogItems],
);
const renderCatalogItem = useCallback(
(itemProps: LegendListRenderItemProps<ThreadSettingsCatalogItem>) => {
const item = itemProps.item;
let content: ReactNode;
if (item.kind === "provider") {
content = <ThreadSettingsProviderListHeader provider={item.provider} />;
} else if (item.kind === "model") {
content = (
<ThreadSettingsModelListRow
isFirst={item.isFirst}
isLast={item.isLast}
option={item.option}
/>
);
} else if (item.kind === "empty") {
content = (
<View className="items-center px-8 py-14">
<Text className="text-center text-sm text-foreground-muted">
{session.providerFilter === FAVORITES_PROVIDER_FILTER &&
session.searchQuery.trim().length === 0
? "No favorite models"
: hasActiveCatalogFilter
? "No matching models"
: "No available models"}
</Text>
</View>
);
} else {
content = (
<ThreadSettingsOptionsItem
animationsReady={animationsReady}
onOpenSubmenu={props.onOpenSubmenu}
/>
);
}
return (
<Animated.View
key={item.key}
entering={animationsReady ? THREAD_SETTINGS_CATALOG_ENTER_TRANSITION : undefined}
exiting={animationsReady ? THREAD_SETTINGS_CATALOG_EXIT_TRANSITION : undefined}
>
{content}
</Animated.View>
);
},
[
animationsReady,
hasActiveCatalogFilter,
props.onOpenSubmenu,
session.providerFilter,
session.searchQuery,
],
);
return (
<AnimatedLegendList
alwaysBounceVertical
automaticallyAdjustsScrollIndicatorInsets
className="flex-1 bg-sheet"
style={
Platform.OS === "android"
? { width: "100%", maxWidth: 720, alignSelf: "center" }
: undefined
}
contentContainerStyle={{ paddingTop: 4 }}
contentInsetAdjustmentBehavior="automatic"
data={listItems}
estimatedItemSize={Platform.OS === "android" ? 56 : 48}
extraData={animationsReady}
getItemType={(item) => item.kind}
itemLayoutAnimation={THREAD_SETTINGS_CATALOG_LAYOUT_TRANSITION}
keyExtractor={(item) => item.key}
keyboardDismissMode="on-drag"
keyboardShouldPersistTaps="handled"
maintainVisibleContentPosition={THREAD_SETTINGS_MAINTAIN_VISIBLE_CONTENT_POSITION}
ListHeaderComponent={
<>
{Platform.OS === "android" ? (
<View className="px-4 pb-2 pt-3">
<View
className="flex-row items-center rounded-full bg-input px-2"
style={{ minHeight: 56 }}
>
<View pointerEvents="none" className="px-2">
<SymbolView
name="magnifyingglass"
size={24}
tintColorClassName="accent-icon-subtle"
/>
</View>
<TextInput
accessibilityLabel="Find a model"
autoCapitalize="none"
autoCorrect={false}
className="min-w-0 flex-1 px-2 py-0 text-base text-foreground"
style={{ minHeight: 56, includeFontPadding: false, textAlignVertical: "center" }}
onChangeText={session.setSearchQuery}
placeholder="Find a model"
placeholderTextColorClassName="accent-placeholder"
selectionColorClassName="accent-focus/32"
cursorColorClassName="accent-focus"
selectionHandleColorClassName="accent-focus"
value={session.searchQuery}
/>
{session.searchQuery.length > 0 ? (
<MaterialIconButton
accessibilityLabel="Clear model search"
icon="xmark"
onPress={() => session.setSearchQuery("")}
/>
) : null}
</View>
</View>
) : null}
</>
}
recycleItems
refreshControl={
session.environmentId ? (
<RefreshControl refreshing={isRefreshingProviders} onRefresh={refreshProviders} />
) : undefined
}
onLoad={() => setAnimationsReady(true)}
renderItem={renderCatalogItem}
showsVerticalScrollIndicator={false}
/>
);
}
/** Compact choice page pushed by the picker navigator. */
function ThreadSettingsChoiceContent(props: {
readonly submenu: ThreadSettingsSubmenuPage;
readonly onSelected: () => void;
}) {
const insets = useSafeAreaInsets();
const session = useThreadSettingsSession();
const descriptorId = props.submenu.kind === "descriptor" ? props.submenu.id : null;
const activeDescriptor =
descriptorId !== null
? session.displayedDescriptors.find(
(descriptor) => descriptor.type === "select" && descriptor.id === descriptorId,
)
: undefined;
const submenuContent =
props.submenu.kind === "runtime"
? {
rows: RUNTIME_MODE_CHOICES.map((choice) => ({
id: choice.mode,
label: choice.label,
description: choice.description,
selected: choice.mode === session.runtimeMode,
onPress: () => {
void Haptics.selectionAsync();
session.onUpdateRuntimeMode(choice.mode);
props.onSelected();
},
})),
}
: activeDescriptor?.type === "select"
? {
rows: selectableChoices(activeDescriptor).map((choice) => ({
id: choice.id,
label: choice.label,
description: undefined,
selected: choice.id === getProviderOptionCurrentValue(activeDescriptor),
onPress: () => {
void Haptics.selectionAsync();
session.applyOptionChange(activeDescriptor.id, choice.id);
props.onSelected();
},
})),
}
: null;
if (!submenuContent) {
return <View className="flex-1 bg-sheet" />;
}
return (
<ScrollView
className="flex-1 bg-sheet"
style={
Platform.OS === "android"
? { width: "100%", maxWidth: 720, alignSelf: "center" }
: undefined
}
contentContainerStyle={{
paddingBottom: insets.bottom + 12,
paddingHorizontal: 16,
paddingTop: 16,
}}
contentInsetAdjustmentBehavior="automatic"
showsVerticalScrollIndicator={false}
>
<View className="overflow-hidden rounded-2xl bg-card">
{submenuContent.rows.map((row, index) => (
<ChoiceRow
key={row.id}
description={row.description}
isLast={index === submenuContent.rows.length - 1}
label={row.label}
selected={row.selected}
onPress={row.onPress}
/>
))}
</View>
</ScrollView>
);
}
type ThreadSettingsPickerStackParams = {
ThreadSettingsModels: undefined;
ThreadSettingsChoice: ThreadSettingsSubmenuPage & { readonly title: string };
};
type ThreadSettingsPickerPresentation = {
readonly onClose: () => void;
};
const ThreadSettingsPickerStack = createNativeStackNavigator<ThreadSettingsPickerStackParams>();
const ThreadSettingsPickerPresentationContext =
createContext<ThreadSettingsPickerPresentation | null>(null);
function useThreadSettingsPickerPresentation() {
const value = use(ThreadSettingsPickerPresentationContext);
if (!value) {
throw new Error(
"useThreadSettingsPickerPresentation must be used inside ThreadSettingsPickerNavigator.",
);
}
return value;
}
function ThreadSettingsModelsScreen() {
const session = useThreadSettingsSession();
const presentation = useThreadSettingsPickerPresentation();
const navigation = useNavigation<NativeStackNavigationProp<ThreadSettingsPickerStackParams>>();
const usesNativeMailSearchToolbar = Platform.OS === "ios" && NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED;
const hasCustomCatalogFilter = session.providerFilter !== null || session.showLegacy;
const commitAndClose = useCallback(() => {
if (!session.commitPendingModel()) return;
presentation.onClose();
}, [presentation, session]);
const providerFilters = useMemo(
() => [
{ id: "all-providers", title: "All providers", value: null },
{ id: "favorites", title: "Favorites", value: FAVORITES_PROVIDER_FILTER },
...session.providerGroups.map((group) => ({
id: `provider:${group.providerKey}`,
title: group.providerLabel,
value: group.providerKey,
})),
],
[session.providerGroups],
);
const filterMenu = useMemo(
() => ({
title: "Model filters",
items: [
{
type: "submenu" as const,
title: "Provider",
items: providerFilters.map((filter) => ({
type: "action" as const,
title: filter.title,
state: session.providerFilter === filter.value ? ("on" as const) : ("off" as const),
onPress: () => session.setProviderFilter(filter.value),
})),
},
...(session.hasLegacyModels
? [
{
type: "action" as const,
title: "Show legacy models",
state: session.showLegacy ? ("on" as const) : ("off" as const),
onPress: () => session.setShowLegacy(!session.showLegacy),
},
]
: []),
],
}),
[providerFilters, session],
);
return (
<>
{Platform.OS === "android" ? (
<AndroidScreenHeader
trailing={
<View className="flex-row items-center">
<AndroidAnchoredMenu
title="Model filters"
actions={[
{
title: "Provider",
subactions: providerFilters.map((filter) => ({
id: filter.id,
title: filter.title,
state: session.providerFilter === filter.value ? "on" : "off",
})),
},
...(session.hasLegacyModels
? [
{
id: "show-legacy",
title: "Show legacy models",
state: session.showLegacy ? ("on" as const) : ("off" as const),
},
]
: []),
]}
onPressAction={({ nativeEvent }) => {
if (nativeEvent.event === "show-legacy") {
session.setShowLegacy(!session.showLegacy);
} else {
const filter = providerFilters.find((item) => item.id === nativeEvent.event);
if (filter) session.setProviderFilter(filter.value);
}
}}
>
{(open) => (
<MaterialIconButton
accessibilityLabel="Filter models"
icon="line.3.horizontal.decrease"
selected={hasCustomCatalogFilter}
variant={hasCustomCatalogFilter ? "tonal" : "standard"}
onPress={open}
/>
)}
</AndroidAnchoredMenu>
{session.pendingModel ? (
<MaterialButton label="Save" tone="text" onPress={commitAndClose} />
) : null}
</View>
}
onBack={presentation.onClose}
title="Thread settings"
hideBottomBorder
/>
) : null}
<NativeStackScreenOptions
optionsVersion={[
session.providerFilter,
session.providerGroups.map((group) => group.providerKey),
session.showLegacy,
]}
options={{
unstable_headerToolbarItems: usesNativeMailSearchToolbar
? () => [
createNativeMailSearchToolbarItem({
filterButtonId: "thread-settings-model-filter",
filterMenu,
filterSystemImageName: hasCustomCatalogFilter
? "line.3.horizontal.decrease.circle.fill"
: "line.3.horizontal.decrease",
onSearchTextChange: session.setSearchQuery,
placeholder: "Find a model",
searchTextChangeId: "thread-settings-model-search-text",
showsSearchDismissButton: true,
}),
]
: undefined,
headerShown: Platform.OS !== "android",
headerSearchBarOptions:
Platform.OS === "ios" && !usesNativeMailSearchToolbar
? {
autoCapitalize: "none",
hideNavigationBar: false,
obscureBackground: false,
onCancelButtonPress: () => session.setSearchQuery(""),
onChangeText: (event) => session.setSearchQuery(event.nativeEvent.text),
placeholder: "Find a model",
}
: undefined,
}}
/>
<MaterialScreenContent>
<ThreadSettingsMainContent
onOpenSubmenu={(submenu) => {
const title =
submenu.kind === "runtime"
? "Runtime"
: (session.displayedDescriptors.find(
(descriptor) => descriptor.type === "select" && descriptor.id === submenu.id,
)?.label ?? "Option");
navigation.navigate("ThreadSettingsChoice", { ...submenu, title });
}}
/>
</MaterialScreenContent>
<NativeHeaderToolbar placement="left">
<NativeHeaderToolbar.Button
accessibilityLabel="Cancel thread settings"
label="Cancel"
onPress={presentation.onClose}
/>
</NativeHeaderToolbar>
<NativeHeaderToolbar placement="right">
<NativeHeaderToolbar.Button
accessibilityLabel={session.pendingModel ? "Save thread settings" : "Done"}
label={session.pendingModel ? "Save" : "Done"}
onPress={commitAndClose}
/>
</NativeHeaderToolbar>
{Platform.OS === "ios" && !usesNativeMailSearchToolbar ? (
<NativeHeaderToolbar placement="bottom">
<NativeHeaderToolbar.Menu
accessibilityLabel="Filter models"
icon={
hasCustomCatalogFilter
? "line.3.horizontal.decrease.circle.fill"
: "line.3.horizontal.decrease.circle"
}
separateBackground
title="Model filters"
>
<NativeHeaderToolbar.Menu title="Provider">
<NativeHeaderToolbar.Label>Provider</NativeHeaderToolbar.Label>
<NativeHeaderToolbar.MenuAction
isOn={session.providerFilter === null}
onPress={() => session.setProviderFilter(null)}
>
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}
isOn={session.providerFilter === group.providerKey}
onPress={() => session.setProviderFilter(group.providerKey)}
>
{group.providerLabel}
</NativeHeaderToolbar.MenuAction>
))}
</NativeHeaderToolbar.Menu>
{session.hasLegacyModels ? (
<NativeHeaderToolbar.MenuAction
isOn={session.showLegacy}
onPress={() => session.setShowLegacy(!session.showLegacy)}
>
Show legacy models
</NativeHeaderToolbar.MenuAction>
) : null}
</NativeHeaderToolbar.Menu>
</NativeHeaderToolbar>
) : null}
</>
);
}
function ThreadSettingsChoiceScreen() {
const navigation = useNavigation<NativeStackNavigationProp<ThreadSettingsPickerStackParams>>();
const route = useRoute<RouteProp<ThreadSettingsPickerStackParams, "ThreadSettingsChoice">>();
return (
<>
<NativeStackScreenOptions options={{ headerShown: Platform.OS !== "android" }} />
{Platform.OS === "android" ? (
<AndroidScreenHeader
title={route.params.title}
onBack={() => navigation.goBack()}
hideBottomBorder
/>
) : null}
<MaterialScreenContent>
<ThreadSettingsChoiceContent
submenu={route.params}
onSelected={() => navigation.goBack()}
/>
</MaterialScreenContent>
</>
);
}
function ThreadSettingsPickerNavigator(props: ThreadSettingsPickerPresentation) {
const theme = useUniwindTheme();
const solidSheetBackground = theme["--color-sheet-solid"];
const foreground = theme["--color-foreground"];
const presentation = useMemo(
() => ({
onClose: props.onClose,
}),
[props.onClose],
);
return (
<ThreadSettingsPickerPresentationContext.Provider value={presentation}>
<ThreadSettingsPickerStack.Navigator
initialRouteName="ThreadSettingsModels"
screenOptions={{
animation: Platform.OS === "android" ? "default" : "slide_from_right",
contentStyle: { backgroundColor: solidSheetBackground },
gestureEnabled: true,
headerBackButtonDisplayMode: "minimal",
headerBackTitle: "",
headerShown: Platform.OS !== "android",
headerShadowVisible: false,
headerStyle: {
backgroundColor: NATIVE_LIQUID_GLASS_SUPPORTED ? "transparent" : solidSheetBackground,
},
headerTransparent: NATIVE_LIQUID_GLASS_SUPPORTED,
headerTintColor: foreground,
headerTitleStyle: { fontSize: 17, fontWeight: "700" },
scrollEdgeEffects: NATIVE_LIQUID_GLASS_SUPPORTED
? THREAD_SETTINGS_HEADER_SCROLL_EDGE_EFFECTS
: undefined,
}}
>
<ThreadSettingsPickerStack.Screen
name="ThreadSettingsModels"
component={ThreadSettingsModelsScreen}
options={{ headerBackVisible: false, title: "Thread settings" }}
/>
<ThreadSettingsPickerStack.Screen
name="ThreadSettingsChoice"
component={ThreadSettingsChoiceScreen}
options={({ route }) => ({ title: route.params.title })}
/>
</ThreadSettingsPickerStack.Navigator>
</ThreadSettingsPickerPresentationContext.Provider>
);
}
/** Existing-thread model picker hosted by the root RNS form-sheet route. */
export function ExistingThreadSettingsRouteScreen() {
const navigation = useNavigation<NativeStackNavigationProp<Record<string, object | undefined>>>();
const presentation = useExistingThreadSettingsRoutePresentation();
const session = presentation.session;
useEffect(() => {
if (session) {
return;
}
navigation.goBack();
}, [navigation, session]);
if (!session) {
return <View className="flex-1 bg-sheet" />;
}
const { ownerId: _ownerId, ...settings } = session;
return (
<ThreadSettingsSessionProvider {...settings}>
<ThreadSettingsPickerNavigator onClose={() => navigation.goBack()} />
</ThreadSettingsSessionProvider>
);
}
/**
* Native stack hosted by the New Task navigator's form-sheet route. Keeping
* the sheet presentation in RNS gives UIKit ownership of nested dismissal,
* while Reasoning and Runtime remain regular pushes inside this navigator.
*/
export function NewTaskThreadSettingsRouteScreen() {
const flow = useNewTaskFlow();
const navigation = useNavigation<NativeStackNavigationProp<Record<string, object | undefined>>>();
const optionDescriptors = useMemo(
() =>
resolveProviderOptionDescriptors({
capabilities: flow.selectedModelOption?.capabilities,
selections: flow.selectedModel?.options,
}),
[flow.selectedModel?.options, flow.selectedModelOption?.capabilities],
);
return (
<ThreadSettingsSessionProvider
environmentId={flow.selectedEnvironmentId}
providerGroups={flow.providerGroups}
selectedModel={flow.selectedModel}
onSelectModel={(option) => flow.setSelectedModelKey(option.key, option.selection.options)}
optionDescriptors={optionDescriptors}
onUpdateOptionSelections={flow.setSelectedModelOptions}
runtimeMode={flow.runtimeMode}
onUpdateRuntimeMode={flow.setRuntimeMode}
>
<ThreadSettingsPickerNavigator onClose={() => navigation.goBack()} />
</ThreadSettingsSessionProvider>
);
}