fix: sync model favorites and visibility across clients (#16816)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
40 files changed
+1461
-245
No files matched your search
@@ -53,6 +53,7 @@ import IconDotsVertical from "@tabler/icons-react-native/IconDotsVertical";
|
||||
import IconDotsCircleHorizontal from "@tabler/icons-react-native/IconDotsCircleHorizontal";
|
||||
import IconEdit from "@tabler/icons-react-native/IconEdit";
|
||||
import IconExternalLink from "@tabler/icons-react-native/IconExternalLink";
|
||||
import IconEyeOff from "@tabler/icons-react-native/IconEyeOff";
|
||||
import IconEye from "@tabler/icons-react-native/IconEye";
|
||||
import IconFileText from "@tabler/icons-react-native/IconFileText";
|
||||
import IconFilter from "@tabler/icons-react-native/IconFilter";
|
||||
@@ -177,6 +178,7 @@ const ANDROID_ICON_BY_SF_SYMBOL = {
|
||||
"exclamationmark.triangle": IconAlertTriangle,
|
||||
"exclamationmark.circle": IconAlertCircle,
|
||||
eye: IconEye,
|
||||
"eye.slash": IconEyeOff,
|
||||
folder: IconFolder,
|
||||
"folder.badge.plus": IconFolderPlus,
|
||||
"folder.fill": IconFolder,
|
||||
|
||||
@@ -675,6 +675,7 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
|
||||
ownerId: settingsOwnerId,
|
||||
environmentId: props.environmentId,
|
||||
providerInstanceId: currentModelSelection.instanceId,
|
||||
...(lockedProviderInstanceId ? { lockedProviderInstanceId } : {}),
|
||||
providerGroups: threadProviderGroups,
|
||||
selectedModel: currentModelSelection,
|
||||
reportedModelSelection: props.reportedModelSelection,
|
||||
@@ -700,6 +701,7 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
|
||||
props.onUpdateRuntimeMode,
|
||||
providerOptionDescriptors,
|
||||
settingsOwnerId,
|
||||
lockedProviderInstanceId,
|
||||
threadProviderGroups,
|
||||
],
|
||||
);
|
||||
|
||||
@@ -12,6 +12,8 @@ export type ModelRowProps = {
|
||||
readonly isFavorite: boolean;
|
||||
readonly favoritesLoaded: boolean;
|
||||
readonly onToggleFavorite: () => void;
|
||||
readonly onToggleHidden?: () => void;
|
||||
readonly hiddenUpdatePending?: boolean;
|
||||
readonly isFirst: boolean;
|
||||
readonly isLast: boolean;
|
||||
};
|
||||
@@ -52,10 +54,10 @@ export function ModelRowContent(
|
||||
accessibilityRole="radio"
|
||||
accessibilityState={{
|
||||
checked: props.selected,
|
||||
disabled: props.option.isUnavailable === true,
|
||||
disabled: props.option.isUnavailable === true || props.option.isHidden === true,
|
||||
}}
|
||||
className="min-h-11 min-w-0 flex-1 flex-row items-center gap-2 active:opacity-70"
|
||||
disabled={props.option.isUnavailable}
|
||||
disabled={props.option.isUnavailable || props.option.isHidden}
|
||||
onPress={props.onPress}
|
||||
>
|
||||
{props.leadingSelection}
|
||||
@@ -77,6 +79,9 @@ export function ModelRowContent(
|
||||
<Text className="text-3xs font-t3-bold text-foreground-muted">Legacy</Text>
|
||||
</View>
|
||||
) : null}
|
||||
{props.option.isHidden ? (
|
||||
<Text className="text-xs text-foreground-muted">Hidden</Text>
|
||||
) : null}
|
||||
{props.option.isUnavailable ? (
|
||||
<Text className="text-xs text-foreground">Unavailable</Text>
|
||||
) : null}
|
||||
@@ -92,6 +97,23 @@ export function ModelRowContent(
|
||||
</View>
|
||||
{props.trailingSelection}
|
||||
</Pressable>
|
||||
{props.onToggleHidden ? (
|
||||
<Pressable
|
||||
accessibilityLabel={`${props.option.isHidden ? "Show" : "Hide"} model: ${props.option.providerLabel}, ${props.option.label}`}
|
||||
accessibilityRole="button"
|
||||
accessibilityState={{ disabled: props.hiddenUpdatePending === true }}
|
||||
className="min-h-11 min-w-11 items-center justify-center"
|
||||
disabled={props.hiddenUpdatePending}
|
||||
onPress={props.onToggleHidden}
|
||||
>
|
||||
<SymbolView
|
||||
name={props.option.isHidden ? "eye.slash" : "eye"}
|
||||
size={18}
|
||||
tintColorClassName={props.option.isHidden ? "accent-icon-subtle" : "accent-icon"}
|
||||
type="monochrome"
|
||||
/>
|
||||
</Pressable>
|
||||
) : null}
|
||||
<Pressable
|
||||
accessibilityLabel={`${props.isFavorite ? "Remove from" : "Add to"} favorites: ${
|
||||
props.option.providerLabel
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
import { createV5StackNavigator as createNativeStackNavigator } from "../../native/createV5StackNavigator";
|
||||
import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
|
||||
import {
|
||||
modelPreferencesMigrationPatch,
|
||||
resolveModelPreferences,
|
||||
} from "@t3tools/shared/serverSettings";
|
||||
import type {
|
||||
EnvironmentId,
|
||||
ModelSelection,
|
||||
@@ -6,9 +11,10 @@ import type {
|
||||
ProviderOptionDescriptor,
|
||||
ProviderOptionSelection,
|
||||
RuntimeMode,
|
||||
ServerSettings,
|
||||
} from "@t3tools/contracts";
|
||||
import type { LegendListRenderItemProps } from "@legendapp/list/react-native";
|
||||
import { useAtomSet, useAtomValue } from "@effect/atom-react";
|
||||
import { RegistryContext, useAtomValue } from "@effect/atom-react";
|
||||
import { AnimatedLegendList } from "@legendapp/list/reanimated";
|
||||
import {
|
||||
getProviderOptionCurrentLabel,
|
||||
@@ -25,6 +31,7 @@ import {
|
||||
useCallback,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
useState,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
@@ -49,7 +56,12 @@ 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 {
|
||||
buildModelOptions,
|
||||
groupByProvider,
|
||||
type ModelOption,
|
||||
type ProviderGroup,
|
||||
} from "../../lib/modelOptions";
|
||||
import { applyProviderOptionSelection } from "../../lib/providerOptions";
|
||||
import { resolveProviderOptionDescriptors } from "../../lib/providerOptions";
|
||||
import { useUniwindTheme } from "../../lib/useUniwindTheme";
|
||||
@@ -62,7 +74,7 @@ import {
|
||||
import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";
|
||||
import { ChatGptSharingStatus } from "./ChatGptSharingStatus";
|
||||
import { environmentServerConfigsAtom, serverEnvironment } from "../../state/server";
|
||||
import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
|
||||
import { mobilePreferencesAtom } from "../../state/preferences";
|
||||
import { useAtomCommand } from "../../state/use-atom-command";
|
||||
import { useNewTaskFlow } from "./new-task-flow-provider";
|
||||
import { MaterialScreenContent } from "../../components/MaterialScreenContent";
|
||||
@@ -88,7 +100,6 @@ import {
|
||||
modelMatchesCatalogQuery,
|
||||
pendingModelAfterPress,
|
||||
providerSectionIsCollapsed,
|
||||
toggleModelFavorite,
|
||||
} from "./thread-settings-sheet-state";
|
||||
import { formatProviderUpdateRequiredNotice } from "@t3tools/client-runtime/providerUpdateRequiredModels";
|
||||
|
||||
@@ -242,6 +253,7 @@ type ThreadSettingsSubmenuPage =
|
||||
type ThreadSettingsSessionProps = {
|
||||
readonly environmentId: EnvironmentId | null;
|
||||
readonly providerInstanceId?: ProviderInstanceId;
|
||||
readonly lockedProviderInstanceId?: ProviderInstanceId;
|
||||
readonly providerGroups: ReadonlyArray<ProviderGroup>;
|
||||
readonly selectedModel: ModelSelection | null;
|
||||
readonly reportedModelSelection?: ModelSelection | null;
|
||||
@@ -300,6 +312,10 @@ type ThreadSettingsSessionValue = {
|
||||
readonly favoriteKeys: ReadonlySet<string>;
|
||||
readonly favoritesLoaded: boolean;
|
||||
readonly toggleFavorite: (option: ModelOption) => void;
|
||||
readonly toggleHidden: (option: ModelOption) => void;
|
||||
readonly pendingHiddenKeys: ReadonlySet<string>;
|
||||
readonly manageHidden: boolean;
|
||||
readonly setManageHidden: (value: boolean) => void;
|
||||
readonly runtimeMode: RuntimeMode;
|
||||
readonly runtimeModeChoices: ReturnType<typeof runtimeModeChoicesForSupportedModes>;
|
||||
readonly onUpdateRuntimeMode: (mode: RuntimeMode) => void;
|
||||
@@ -329,12 +345,46 @@ const ThreadSettingsSessionContext = createContext<ThreadSettingsSessionValue |
|
||||
function ThreadSettingsSessionProvider(
|
||||
props: ThreadSettingsSessionProps & { readonly children: ReactNode },
|
||||
) {
|
||||
const registry = use(RegistryContext);
|
||||
const preferences = useAtomValue(mobilePreferencesAtom);
|
||||
const savePreferences = useAtomSet(updateMobilePreferencesAtom);
|
||||
const favoritesLoaded = AsyncResult.isSuccess(preferences);
|
||||
const modelFavorites = favoritesLoaded
|
||||
const configs = useAtomValue(environmentServerConfigsAtom);
|
||||
const config = props.environmentId ? configs.get(props.environmentId) : undefined;
|
||||
const settings = config?.settings;
|
||||
const [manageHidden, setManageHidden] = useState(false);
|
||||
const pendingHidden = useRef(new Set<string>());
|
||||
const pendingFavorites = useRef(
|
||||
new Map<string, { favorite: boolean; settings: ServerSettings; pending: boolean }>(),
|
||||
);
|
||||
const [pendingHiddenKeys, setPendingHiddenKeys] = useState<ReadonlySet<string>>(() => new Set());
|
||||
const providerGroups = useMemo(
|
||||
() =>
|
||||
manageHidden && config
|
||||
? groupByProvider(
|
||||
buildModelOptions(config, props.selectedModel, props.lockedProviderInstanceId, true),
|
||||
)
|
||||
: props.providerGroups,
|
||||
[
|
||||
config,
|
||||
manageHidden,
|
||||
props.providerGroups,
|
||||
props.selectedModel,
|
||||
props.lockedProviderInstanceId,
|
||||
],
|
||||
);
|
||||
const legacyFavorites = AsyncResult.isSuccess(preferences)
|
||||
? (preferences.value.modelFavorites ?? EMPTY_MODEL_FAVORITES)
|
||||
: EMPTY_MODEL_FAVORITES;
|
||||
const favoritesLoaded =
|
||||
settings !== undefined && (settings.favorites !== null || AsyncResult.isSuccess(preferences));
|
||||
const modelFavorites = resolveModelPreferences(settings ?? DEFAULT_SERVER_SETTINGS, {
|
||||
favorites: legacyFavorites,
|
||||
}).favorites;
|
||||
const saveSettings = useAtomCommand(serverEnvironment.updateSettings, "model favorites update");
|
||||
useEffect(() => {
|
||||
if (!props.environmentId || !settings || !AsyncResult.isSuccess(preferences)) return;
|
||||
const patch = modelPreferencesMigrationPatch(settings, { favorites: legacyFavorites });
|
||||
if (patch) void saveSettings({ environmentId: props.environmentId, input: { patch } });
|
||||
}, [legacyFavorites, preferences, props.environmentId, saveSettings, settings]);
|
||||
const favoriteKeys = useMemo(
|
||||
() =>
|
||||
new Set(
|
||||
@@ -343,19 +393,105 @@ function ThreadSettingsSessionProvider(
|
||||
[modelFavorites],
|
||||
);
|
||||
const toggleFavorite = useCallback(
|
||||
(option: ModelOption) => {
|
||||
if (!favoritesLoaded) return;
|
||||
async (option: ModelOption) => {
|
||||
if (!favoritesLoaded || !props.environmentId) return;
|
||||
const currentSettings = registry
|
||||
.get(environmentServerConfigsAtom)
|
||||
.get(props.environmentId)?.settings;
|
||||
if (!currentSettings) return;
|
||||
const favorites = resolveModelPreferences(currentSettings, {
|
||||
favorites: legacyFavorites,
|
||||
}).favorites;
|
||||
const key = JSON.stringify([
|
||||
props.environmentId,
|
||||
option.selection.instanceId,
|
||||
option.selection.model,
|
||||
]);
|
||||
const previous = pendingFavorites.current.get(key);
|
||||
const previousFavorite =
|
||||
previous && (previous.pending || previous.settings.favorites === currentSettings.favorites)
|
||||
? previous.favorite
|
||||
: favorites.some(
|
||||
(favorite) =>
|
||||
favorite.provider === option.selection.instanceId &&
|
||||
favorite.model === option.selection.model,
|
||||
);
|
||||
const pending = { favorite: !previousFavorite, settings: currentSettings, pending: true };
|
||||
pendingFavorites.current.set(key, pending);
|
||||
void Haptics.selectionAsync();
|
||||
savePreferences({
|
||||
transform: (current) => ({
|
||||
modelFavorites: toggleModelFavorite(
|
||||
current.modelFavorites ?? EMPTY_MODEL_FAVORITES,
|
||||
option,
|
||||
),
|
||||
}),
|
||||
try {
|
||||
const result = await saveSettings({
|
||||
environmentId: props.environmentId,
|
||||
input: {
|
||||
patch: {
|
||||
setModelFavorites: [
|
||||
{
|
||||
provider: option.selection.instanceId,
|
||||
model: option.selection.model,
|
||||
favorite: pending.favorite,
|
||||
},
|
||||
],
|
||||
...(currentSettings.favorites === null
|
||||
? { migrateModelPreferences: { favorites } }
|
||||
: {}),
|
||||
},
|
||||
},
|
||||
});
|
||||
if (
|
||||
AsyncResult.isSuccess(result) &&
|
||||
registry.get(environmentServerConfigsAtom).get(props.environmentId)?.settings
|
||||
.favorites === currentSettings.favorites
|
||||
) {
|
||||
pending.pending = false;
|
||||
}
|
||||
} finally {
|
||||
if (pending.pending && pendingFavorites.current.get(key) === pending) {
|
||||
if (
|
||||
previous &&
|
||||
!previous.pending &&
|
||||
registry.get(environmentServerConfigsAtom).get(props.environmentId)?.settings
|
||||
.favorites === previous.settings.favorites
|
||||
) {
|
||||
pendingFavorites.current.set(key, previous);
|
||||
} else {
|
||||
pendingFavorites.current.delete(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
[favoritesLoaded, legacyFavorites, props.environmentId, registry, saveSettings],
|
||||
);
|
||||
const toggleHidden = useCallback(
|
||||
(option: ModelOption) => {
|
||||
if (
|
||||
!settings ||
|
||||
!props.environmentId ||
|
||||
!option.canHide ||
|
||||
pendingHidden.current.has(option.key)
|
||||
)
|
||||
return;
|
||||
pendingHidden.current.add(option.key);
|
||||
setPendingHiddenKeys(new Set(pendingHidden.current));
|
||||
void Haptics.selectionAsync();
|
||||
void saveSettings({
|
||||
environmentId: props.environmentId,
|
||||
input: {
|
||||
patch: {
|
||||
setModelsHidden: [
|
||||
{
|
||||
provider: option.selection.instanceId,
|
||||
model: option.selection.model,
|
||||
hidden: !option.isHidden,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
}).then(() => {
|
||||
pendingHidden.current.delete(option.key);
|
||||
setPendingHiddenKeys(new Set(pendingHidden.current));
|
||||
});
|
||||
},
|
||||
[favoritesLoaded, savePreferences],
|
||||
[props.environmentId, saveSettings, settings],
|
||||
);
|
||||
const [showLegacyToggle, setShowLegacyToggle] = useState(false);
|
||||
const [providerFilter, setProviderFilter] = useState<string | null>(null);
|
||||
@@ -473,7 +609,11 @@ function ThreadSettingsSessionProvider(
|
||||
() => ({
|
||||
environmentId: props.environmentId,
|
||||
providerInstanceId: props.providerInstanceId,
|
||||
providerGroups: props.providerGroups,
|
||||
providerGroups,
|
||||
manageHidden,
|
||||
setManageHidden,
|
||||
toggleHidden,
|
||||
pendingHiddenKeys,
|
||||
runtimeMode: compatibleRuntimeMode,
|
||||
runtimeModeChoices,
|
||||
onUpdateRuntimeMode: props.onUpdateRuntimeMode,
|
||||
@@ -518,7 +658,10 @@ function ThreadSettingsSessionProvider(
|
||||
pressModel,
|
||||
providerFilter,
|
||||
props.onUpdateRuntimeMode,
|
||||
props.providerGroups,
|
||||
providerGroups,
|
||||
manageHidden,
|
||||
toggleHidden,
|
||||
pendingHiddenKeys,
|
||||
runtimeModeChoices,
|
||||
searchQuery,
|
||||
showLegacyToggle,
|
||||
@@ -599,6 +742,12 @@ function ThreadSettingsModelListRow(props: {
|
||||
isFavorite={session.favoriteKeys.has(props.option.key)}
|
||||
favoritesLoaded={session.favoritesLoaded}
|
||||
onToggleFavorite={() => session.toggleFavorite(props.option)}
|
||||
onToggleHidden={
|
||||
session.manageHidden && props.option.canHide
|
||||
? () => session.toggleHidden(props.option)
|
||||
: undefined
|
||||
}
|
||||
hiddenUpdatePending={session.pendingHiddenKeys.has(props.option.key)}
|
||||
option={props.option}
|
||||
selected={session.isDisplayed(props.option)}
|
||||
/>
|
||||
@@ -642,7 +791,9 @@ function useThreadSettingsCatalogItems(
|
||||
}
|
||||
const driver = group.models[0]?.providerDriver ?? group.providerKey;
|
||||
const catalogModels =
|
||||
session.showLegacy || session.providerFilter === FAVORITES_PROVIDER_FILTER
|
||||
session.manageHidden ||
|
||||
session.showLegacy ||
|
||||
session.providerFilter === FAVORITES_PROVIDER_FILTER
|
||||
? group.models
|
||||
: group.models.filter(
|
||||
(model) =>
|
||||
@@ -726,6 +877,7 @@ function useThreadSettingsCatalogItems(
|
||||
session.providerGroups,
|
||||
session.searchQuery,
|
||||
session.showLegacy,
|
||||
session.manageHidden,
|
||||
],
|
||||
);
|
||||
}
|
||||
@@ -806,21 +958,23 @@ function ThreadSettingsOptionsItem(props: {
|
||||
</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-grouped-card">
|
||||
<SwitchRow
|
||||
isLast
|
||||
label="Legacy models"
|
||||
onValueChange={session.setShowLegacy}
|
||||
value={session.showLegacy}
|
||||
/>
|
||||
</View>
|
||||
</>
|
||||
) : null}
|
||||
<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-grouped-card">
|
||||
<SwitchRow
|
||||
label="Manage hidden models"
|
||||
value={session.manageHidden}
|
||||
onValueChange={session.setManageHidden}
|
||||
isLast={Platform.OS === "ios" || !session.hasLegacyModels}
|
||||
/>
|
||||
{Platform.OS !== "ios" && session.hasLegacyModels ? (
|
||||
<SwitchRow
|
||||
isLast
|
||||
label="Legacy models"
|
||||
onValueChange={session.setShowLegacy}
|
||||
value={session.showLegacy}
|
||||
/>
|
||||
) : null}
|
||||
</View>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -6,10 +6,8 @@ import type { ModelOption } from "../../lib/modelOptions";
|
||||
import {
|
||||
canCommitPendingModel,
|
||||
favoritesFirst,
|
||||
modelFavoriteKey,
|
||||
modelMatchesCatalogQuery,
|
||||
pendingModelAfterPress,
|
||||
toggleModelFavorite,
|
||||
} from "./thread-settings-sheet-state";
|
||||
|
||||
function modelOption(
|
||||
@@ -58,23 +56,6 @@ describe("thread settings sheet state", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
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");
|
||||
|
||||
@@ -144,6 +125,15 @@ describe("thread settings sheet state", () => {
|
||||
).toBe(pressed);
|
||||
});
|
||||
|
||||
it("cannot select a hidden model exposed only for visibility management", () => {
|
||||
const pending = modelOption("gpt-next");
|
||||
expect(
|
||||
canCommitPendingModel(pending, [
|
||||
{ providerKey: "codex", providerLabel: "Codex", models: [{ ...pending, isHidden: true }] },
|
||||
]),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("cannot save a staged model after sign-out removes it from the catalog", () => {
|
||||
const pending = modelOption("gemini-native");
|
||||
const group = { providerKey: "codex", providerLabel: "Codex", models: [pending] };
|
||||
|
||||
@@ -1,26 +1,10 @@
|
||||
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>,
|
||||
@@ -71,7 +55,9 @@ export function canCommitPendingModel(
|
||||
groups: ReadonlyArray<ProviderGroup>,
|
||||
): boolean {
|
||||
return groups.some((group) =>
|
||||
group.models.some((model) => model.key === pending.key && !model.isUnavailable),
|
||||
group.models.some(
|
||||
(model) => model.key === pending.key && !model.isUnavailable && !model.isHidden,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { ProviderInstanceId, type ModelSelection, type ServerConfig } from "@t3tools/contracts";
|
||||
import {
|
||||
DEFAULT_SERVER_SETTINGS,
|
||||
ProviderInstanceId,
|
||||
type ModelSelection,
|
||||
type ServerConfig,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
import {
|
||||
buildModelOptions,
|
||||
@@ -504,3 +509,90 @@ describe("mobile model options", () => {
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("synchronized mobile model visibility", () => {
|
||||
const provider = ProviderInstanceId.make("codex_work");
|
||||
const other = ProviderInstanceId.make("codex_personal");
|
||||
const models = [
|
||||
{ slug: "hidden", name: "Hidden", isCustom: false, capabilities: null },
|
||||
{ slug: "first", name: "First", isCustom: false, capabilities: null },
|
||||
{ slug: "second", name: "Second", isCustom: false, capabilities: null },
|
||||
{ slug: "custom", name: "Custom", isCustom: true, capabilities: null },
|
||||
];
|
||||
const config = {
|
||||
settings: {
|
||||
...DEFAULT_SERVER_SETTINGS,
|
||||
providerModelPreferences: {
|
||||
[provider]: {
|
||||
hiddenModels: ["hidden", "custom", "missing"],
|
||||
modelOrder: ["second", "first"],
|
||||
},
|
||||
},
|
||||
},
|
||||
providers: [provider, other].map((instanceId) => ({
|
||||
instanceId,
|
||||
driver: "codex",
|
||||
enabled: true,
|
||||
installed: true,
|
||||
auth: { status: "authenticated" },
|
||||
models,
|
||||
})),
|
||||
} as unknown as ServerConfig;
|
||||
|
||||
it("applies server visibility and ordering independently to each provider instance", () => {
|
||||
const options = buildModelOptions(config, null);
|
||||
expect(
|
||||
options
|
||||
.filter((option) => option.providerKey === provider)
|
||||
.map((option) => option.selection.model),
|
||||
).toEqual(["second", "first", "custom"]);
|
||||
expect(
|
||||
options
|
||||
.filter((option) => option.providerKey === other)
|
||||
.map((option) => option.selection.model),
|
||||
).toEqual(["hidden", "first", "second", "custom"]);
|
||||
});
|
||||
|
||||
it("exposes hidden built-ins for management without offering custom visibility controls", () => {
|
||||
const options = buildModelOptions(
|
||||
config,
|
||||
{ instanceId: provider, model: "hidden" },
|
||||
provider,
|
||||
true,
|
||||
);
|
||||
expect(options.map((option) => option.selection.model)).toEqual([
|
||||
"second",
|
||||
"first",
|
||||
"hidden",
|
||||
"custom",
|
||||
]);
|
||||
expect(options.find((option) => option.selection.model === "hidden")).toMatchObject({
|
||||
isHidden: true,
|
||||
canHide: true,
|
||||
});
|
||||
expect(options.find((option) => option.selection.model === "custom")).toMatchObject({
|
||||
isHidden: false,
|
||||
canHide: false,
|
||||
});
|
||||
});
|
||||
|
||||
it.each(["hidden", "missing"])("does not resurrect the hidden fallback %s", (model) => {
|
||||
expect(
|
||||
buildModelOptions(config, { instanceId: provider, model }, provider).map(
|
||||
(option) => option.selection.model,
|
||||
),
|
||||
).toEqual(["second", "first", "custom"]);
|
||||
});
|
||||
|
||||
it("keeps custom models visible and restores built-in models when the server clears hidden models", () => {
|
||||
expect(
|
||||
buildModelOptions(config, { instanceId: provider, model: "custom" }, provider).map(
|
||||
(option) => option.selection.model,
|
||||
),
|
||||
).toContain("custom");
|
||||
const cleared = { ...config, settings: { ...config.settings, providerModelPreferences: {} } };
|
||||
expect(
|
||||
buildModelOptions(cleared, null, provider).map((option) => option.selection.model),
|
||||
).toEqual(["hidden", "first", "second", "custom"]);
|
||||
});
|
||||
});
|
||||
@@ -22,6 +22,8 @@ export type ModelOption = {
|
||||
readonly isDefault: boolean;
|
||||
readonly isLegacy: boolean;
|
||||
readonly isUnavailable?: boolean;
|
||||
readonly isHidden?: boolean;
|
||||
readonly canHide?: boolean;
|
||||
readonly capabilities: ModelCapabilities | null;
|
||||
readonly selection: ModelSelection;
|
||||
readonly providerUpdateRequired?: ProviderUpdateRequired;
|
||||
@@ -167,6 +169,7 @@ export function buildModelOptions(
|
||||
config: T3ServerConfig | null | undefined,
|
||||
fallbackModelSelection: ModelSelection | null,
|
||||
providerInstanceId?: ModelSelection["instanceId"],
|
||||
includeHidden = false,
|
||||
): ReadonlyArray<ModelOption> {
|
||||
const options = new Map<string, ModelOption>();
|
||||
|
||||
@@ -185,7 +188,15 @@ export function buildModelOptions(
|
||||
const updateRequired = provider.updateRequiredModels?.length
|
||||
? { driver: provider.driver, updateRequiredModels: provider.updateRequiredModels }
|
||||
: undefined;
|
||||
for (const model of provider.models) {
|
||||
const preferences = config?.settings?.providerModelPreferences?.[provider.instanceId];
|
||||
const hiddenModels = new Set(preferences?.hiddenModels ?? []);
|
||||
const order = new Map((preferences?.modelOrder ?? []).map((slug, index) => [slug, index]));
|
||||
const models = provider.models.filter(
|
||||
(model) => includeHidden || model.isCustom || !hiddenModels.has(model.slug),
|
||||
);
|
||||
if (order.size > 0)
|
||||
models.sort((a, b) => (order.get(a.slug) ?? Infinity) - (order.get(b.slug) ?? Infinity));
|
||||
for (const model of models) {
|
||||
const key = `${provider.instanceId}:${model.slug}`;
|
||||
options.set(key, {
|
||||
key,
|
||||
@@ -198,6 +209,8 @@ export function buildModelOptions(
|
||||
? {}
|
||||
: { supportedRuntimeModes: provider.supportedRuntimeModes }),
|
||||
...(provider.iconUrl ? { providerIconUrl: provider.iconUrl } : {}),
|
||||
isHidden: !model.isCustom && hiddenModels.has(model.slug),
|
||||
canHide: !model.isCustom,
|
||||
isDefault: model.isDefault === true,
|
||||
isLegacy: model.isLegacy === true,
|
||||
...(updateRequired ? { providerUpdateRequired: updateRequired } : {}),
|
||||
@@ -215,7 +228,14 @@ export function buildModelOptions(
|
||||
|
||||
if (
|
||||
fallbackModelSelection &&
|
||||
(providerInstanceId === undefined || fallbackModelSelection.instanceId === providerInstanceId)
|
||||
(providerInstanceId === undefined ||
|
||||
fallbackModelSelection.instanceId === providerInstanceId) &&
|
||||
(!config?.settings?.providerModelPreferences?.[
|
||||
fallbackModelSelection.instanceId
|
||||
]?.hiddenModels.includes(fallbackModelSelection.model) ||
|
||||
config?.providers
|
||||
.find((provider) => provider.instanceId === fallbackModelSelection.instanceId)
|
||||
?.models.find((model) => model.slug === fallbackModelSelection.model)?.isCustom)
|
||||
) {
|
||||
const key = `${fallbackModelSelection.instanceId}:${fallbackModelSelection.model}`;
|
||||
const existing = options.get(key);
|
||||
|
||||
@@ -46,7 +46,7 @@ export interface Preferences {
|
||||
readonly planModeEnabled?: boolean;
|
||||
/** Device-local counterpart of web's `sidebarWorkingShelfEnabled` beta. */
|
||||
readonly workingShelfEnabled?: boolean;
|
||||
/** Model favorites belong to this device, like the web client setting. */
|
||||
/** @deprecated Retained to migrate favorites to the connected environment. */
|
||||
readonly modelFavorites?: ReadonlyArray<{
|
||||
readonly provider: ProviderInstanceId;
|
||||
readonly model: string;
|
||||
|
||||
@@ -20,6 +20,7 @@ import * as ProviderAdapterRegistry from "../orchestration-v2/ProviderAdapterReg
|
||||
import * as ProviderRegistry from "../provider/ProviderRegistry.ts";
|
||||
import * as ProjectService from "../project/ProjectService.ts";
|
||||
import * as ScheduledTaskService from "../scheduledTasks/ScheduledTaskService.ts";
|
||||
import * as ServerSettings from "../serverSettings.ts";
|
||||
import * as SecretRequests from "../secrets/SecretRequests.ts";
|
||||
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
|
||||
import type * as McpInvocationContext from "./McpInvocationContext.ts";
|
||||
@@ -146,6 +147,7 @@ it("readThread prefers activity-run status over a newer cancelled queued run", a
|
||||
? Effect.succeed(projection)
|
||||
: Effect.die(`unexpected thread ${input.threadId}`),
|
||||
} satisfies Partial<ThreadManagementService.ThreadManagementService["Service"]>),
|
||||
ServerSettings.layerTest(),
|
||||
Layer.mock(ProviderRegistry.ProviderRegistry)({
|
||||
getProviders: Effect.succeed([]),
|
||||
} satisfies Partial<ProviderRegistry.ProviderRegistry["Service"]>),
|
||||
@@ -211,6 +213,7 @@ it("readThread prefers waiting activity status over a newer cancelled queued run
|
||||
? Effect.succeed(projection)
|
||||
: Effect.die(`unexpected thread ${input.threadId}`),
|
||||
} satisfies Partial<ThreadManagementService.ThreadManagementService["Service"]>),
|
||||
ServerSettings.layerTest(),
|
||||
Layer.mock(ProviderRegistry.ProviderRegistry)({
|
||||
getProviders: Effect.succeed([]),
|
||||
} satisfies Partial<ProviderRegistry.ProviderRegistry["Service"]>),
|
||||
@@ -324,6 +327,7 @@ it("taskStatus returns task.providerInstanceId rather than the driver kind", asy
|
||||
return Effect.die(`unexpected thread ${threadId}`);
|
||||
},
|
||||
} satisfies Partial<ThreadManagementService.ThreadManagementService["Service"]>),
|
||||
ServerSettings.layerTest(),
|
||||
Layer.mock(ProviderRegistry.ProviderRegistry)({
|
||||
getProviders: Effect.succeed([]),
|
||||
} satisfies Partial<ProviderRegistry.ProviderRegistry["Service"]>),
|
||||
@@ -447,6 +451,7 @@ it("readThread and sendToThread reach threads in other projects", async () => {
|
||||
delivery: "started",
|
||||
} as unknown as ThreadManagementService.ThreadManagementSendResult),
|
||||
} satisfies Partial<ThreadManagementService.ThreadManagementService["Service"]>),
|
||||
ServerSettings.layerTest(),
|
||||
Layer.mock(ProviderRegistry.ProviderRegistry)({
|
||||
getProviders: Effect.succeed([]),
|
||||
} satisfies Partial<ProviderRegistry.ProviderRegistry["Service"]>),
|
||||
|
||||
@@ -34,6 +34,7 @@ import * as ProviderRegistry from "../provider/ProviderRegistry.ts";
|
||||
import { buildUnavailableProviderSnapshot } from "../provider/unavailableProviderSnapshot.ts";
|
||||
import * as ProjectService from "../project/ProjectService.ts";
|
||||
import * as ScheduledTaskService from "../scheduledTasks/ScheduledTaskService.ts";
|
||||
import * as ServerSettings from "../serverSettings.ts";
|
||||
import * as SecretRequests from "../secrets/SecretRequests.ts";
|
||||
import type { McpInvocationScope } from "./McpInvocationContext.ts";
|
||||
import { idleThreadProjection, liveThreadShell } from "./McpToolAccess.testkit.ts";
|
||||
@@ -128,6 +129,7 @@ describe("OrchestratorMcpService", () => {
|
||||
),
|
||||
),
|
||||
}),
|
||||
ServerSettings.layerTest(),
|
||||
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
|
||||
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
|
||||
list: () => Effect.succeed([]),
|
||||
@@ -227,6 +229,7 @@ describe("OrchestratorMcpService", () => {
|
||||
: Effect.succeed(awaitsRestart),
|
||||
dispatch: () => Ref.update(dispatched, (count) => count + 1).pipe(Effect.as({} as never)),
|
||||
}),
|
||||
ServerSettings.layerTest(),
|
||||
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
|
||||
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
|
||||
list: () => Effect.succeed([]),
|
||||
@@ -311,6 +314,7 @@ describe("OrchestratorMcpService", () => {
|
||||
Effect.as({} as never),
|
||||
),
|
||||
}),
|
||||
ServerSettings.layerTest(),
|
||||
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
|
||||
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
|
||||
list: () => Effect.succeed([]),
|
||||
@@ -387,6 +391,7 @@ describe("OrchestratorMcpService", () => {
|
||||
Effect.andThen(Effect.fail(new Error("simulated stop failure") as never)),
|
||||
),
|
||||
}),
|
||||
ServerSettings.layerTest(),
|
||||
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
|
||||
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
|
||||
list: () => Effect.succeed([]),
|
||||
@@ -471,6 +476,7 @@ describe("OrchestratorMcpService", () => {
|
||||
),
|
||||
stopDelegatedTasks: () => Effect.void,
|
||||
}),
|
||||
ServerSettings.layerTest(),
|
||||
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
|
||||
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
|
||||
list: () => Effect.succeed([]),
|
||||
@@ -557,6 +563,7 @@ describe("OrchestratorMcpService", () => {
|
||||
),
|
||||
stopDelegatedTasks: () => Effect.void,
|
||||
}),
|
||||
ServerSettings.layerTest(),
|
||||
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
|
||||
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
|
||||
list: () => Effect.succeed([]),
|
||||
@@ -697,6 +704,7 @@ describe("OrchestratorMcpService", () => {
|
||||
yield* Ref.set(stoppedBelow, true);
|
||||
}),
|
||||
}),
|
||||
ServerSettings.layerTest(),
|
||||
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
|
||||
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
|
||||
list: () => Effect.succeed([]),
|
||||
@@ -927,6 +935,7 @@ describe("OrchestratorMcpService provider resolution", () => {
|
||||
Layer.mock(ThreadManagementService.ThreadManagementService)({
|
||||
getThreadRecords: () => Effect.succeed(parentProjection([])),
|
||||
}),
|
||||
ServerSettings.layerTest(),
|
||||
Layer.mock(ProviderRegistry.ProviderRegistry)({
|
||||
getProviders: Effect.sync(() => providers),
|
||||
}),
|
||||
@@ -1016,6 +1025,51 @@ describe("OrchestratorMcpService provider resolution", () => {
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("leaves models the user hid out of the capability catalog", () =>
|
||||
Effect.gen(function* () {
|
||||
const codex = providerSnapshot({
|
||||
instanceId: codexInstanceId,
|
||||
driver: ProviderDriverKind.make("codex"),
|
||||
model: "gpt-5.4",
|
||||
});
|
||||
const layerDependencies = Layer.mergeAll(
|
||||
NodeServices.layer,
|
||||
Layer.mock(ThreadManagementService.ThreadManagementService)({
|
||||
getThreadRecords: () => Effect.succeed(parentProjection([])),
|
||||
}),
|
||||
ServerSettings.layerTest({
|
||||
providerModelPreferences: {
|
||||
[codexInstanceId]: { hiddenModels: ["gpt-5.4-mini", "my-model"], modelOrder: [] },
|
||||
},
|
||||
}),
|
||||
providerRegistryLayer([
|
||||
{
|
||||
...codex,
|
||||
models: [
|
||||
...codex.models,
|
||||
{ slug: "gpt-5.4-mini", name: "Mini", isCustom: false, capabilities: null },
|
||||
// Custom models cannot be hidden, matching the client pickers.
|
||||
{ slug: "my-model", name: "Mine", isCustom: true, capabilities: null },
|
||||
],
|
||||
},
|
||||
]),
|
||||
adapterRegistryLayer([codexInstanceId]),
|
||||
Layer.mock(ProjectService.ProjectService)({}),
|
||||
Layer.mock(SecretRequests.SecretRequests)({}),
|
||||
Layer.mock(ScheduledTaskService.ScheduledTaskService)({}),
|
||||
);
|
||||
|
||||
yield* Effect.gen(function* () {
|
||||
const service = yield* OrchestratorMcpService.OrchestratorMcpService;
|
||||
const capabilities = yield* service.capabilities(scope);
|
||||
assert.deepEqual(
|
||||
capabilities.providers[0]?.models.map((model) => model.id),
|
||||
["gpt-5.4", "my-model"],
|
||||
);
|
||||
}).pipe(Effect.provide(OrchestratorMcpService.layer.pipe(Layer.provide(layerDependencies))));
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect(
|
||||
"delegates to an Antigravity instance whose adapter resolves through the registry",
|
||||
() =>
|
||||
@@ -1070,6 +1124,7 @@ describe("OrchestratorMcpService provider resolution", () => {
|
||||
} as never),
|
||||
),
|
||||
}),
|
||||
ServerSettings.layerTest(),
|
||||
Layer.mock(ProviderRegistry.ProviderRegistry)({
|
||||
getProviders: Effect.succeed([
|
||||
providerSnapshot({
|
||||
@@ -1167,6 +1222,7 @@ describe("OrchestratorMcpService provider resolution", () => {
|
||||
} as never),
|
||||
),
|
||||
}),
|
||||
ServerSettings.layerTest(),
|
||||
Layer.mock(ProviderRegistry.ProviderRegistry)({
|
||||
getProviders: Effect.succeed([
|
||||
providerSnapshot({
|
||||
@@ -1221,6 +1277,7 @@ describe("OrchestratorMcpService provider resolution", () => {
|
||||
Layer.mock(ThreadManagementService.ThreadManagementService)({
|
||||
getThreadRecords: () => Effect.succeed(parentProjection([])),
|
||||
}),
|
||||
ServerSettings.layerTest(),
|
||||
providerRegistryLayer([
|
||||
providerSnapshot({
|
||||
instanceId: codexInstanceId,
|
||||
@@ -1329,6 +1386,7 @@ describe("OrchestratorMcpService provider resolution", () => {
|
||||
} as never),
|
||||
),
|
||||
}),
|
||||
ServerSettings.layerTest(),
|
||||
Layer.mock(ProviderRegistry.ProviderRegistry)({
|
||||
getProviders: Effect.succeed([codex, missingCli]),
|
||||
refreshInstance: () =>
|
||||
@@ -1480,6 +1538,7 @@ describe("OrchestratorMcpService provider resolution", () => {
|
||||
} as never),
|
||||
),
|
||||
}),
|
||||
ServerSettings.layerTest(),
|
||||
providerRegistryLayer([
|
||||
providerSnapshot({
|
||||
instanceId: codexInstanceId,
|
||||
@@ -1632,6 +1691,7 @@ describe("OrchestratorMcpService provider resolution", () => {
|
||||
getThreadShell: (threadId) =>
|
||||
Effect.succeed(threadId === boundThreadId ? boundThread : null),
|
||||
}),
|
||||
ServerSettings.layerTest(),
|
||||
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
|
||||
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
|
||||
list: () => Effect.succeed([]),
|
||||
@@ -1700,6 +1760,7 @@ describe("OrchestratorMcpService provider resolution", () => {
|
||||
// Its turn ended: no run is active.
|
||||
getThreadRecords: () => Effect.succeed(idleThreadProjection(shell)),
|
||||
}),
|
||||
ServerSettings.layerTest(),
|
||||
Layer.mock(ProviderRegistry.ProviderRegistry)({
|
||||
getProviders: Effect.succeed([]),
|
||||
}),
|
||||
@@ -1789,6 +1850,7 @@ describe("OrchestratorMcpService provider resolution", () => {
|
||||
),
|
||||
),
|
||||
}),
|
||||
ServerSettings.layerTest(),
|
||||
Layer.mock(ProviderRegistry.ProviderRegistry)({
|
||||
getProviders: Effect.succeed([]),
|
||||
}),
|
||||
|
||||
@@ -85,6 +85,7 @@ import { isSnoozed } from "../orchestration-v2/ThreadSettlementService.ts";
|
||||
import * as ProjectService from "../project/ProjectService.ts";
|
||||
import * as ProviderRegistry from "../provider/ProviderRegistry.ts";
|
||||
import * as ScheduledTaskService from "../scheduledTasks/ScheduledTaskService.ts";
|
||||
import * as ServerSettings from "../serverSettings.ts";
|
||||
import {
|
||||
clientRuntimeModeCeiling,
|
||||
type McpInvocationScope,
|
||||
@@ -901,6 +902,7 @@ const make = Effect.gen(function* () {
|
||||
const providerAdapters = yield* ProviderAdapterRegistry.ProviderAdapterRegistryV2;
|
||||
const scheduledTasks = yield* ScheduledTaskService.ScheduledTaskService;
|
||||
const projects = yield* ProjectService.ProjectService;
|
||||
const serverSettings = yield* ServerSettings.ServerSettingsService;
|
||||
|
||||
/** A caller-named project, which must exist before anything is recorded against it. */
|
||||
const requireProject = (projectId: ProjectId) =>
|
||||
@@ -1851,6 +1853,11 @@ const make = Effect.gen(function* () {
|
||||
const { parent, limits } = yield* loadCaller(scope);
|
||||
const providers = yield* loadProviders;
|
||||
const orchestrationCapableInstanceIds = yield* loadOrchestrationCapableInstanceIds();
|
||||
// Models the user hid from pickers stay out of the catalog agents choose from.
|
||||
const modelPreferences = yield* serverSettings.getSettings.pipe(
|
||||
Effect.map((settings) => settings.providerModelPreferences ?? {}),
|
||||
Effect.mapError(() => failure("orchestration_error", "Unable to read server settings.")),
|
||||
);
|
||||
return {
|
||||
parentThreadId: parent?.thread.id ?? null,
|
||||
inheritedProviderInstanceId: parent?.thread.modelSelection.instanceId ?? null,
|
||||
@@ -1867,13 +1874,19 @@ const make = Effect.gen(function* () {
|
||||
driverKind: provider.driver,
|
||||
displayName: provider?.displayName ?? null,
|
||||
models:
|
||||
provider?.models.map((model) => ({
|
||||
id: model.slug,
|
||||
label: model.name ?? null,
|
||||
...(model.capabilities?.optionDescriptors === undefined
|
||||
? {}
|
||||
: { options: model.capabilities.optionDescriptors }),
|
||||
})) ?? [],
|
||||
provider?.models
|
||||
.filter(
|
||||
(model) =>
|
||||
model.isCustom ||
|
||||
!modelPreferences[provider.instanceId]?.hiddenModels.includes(model.slug),
|
||||
)
|
||||
.map((model) => ({
|
||||
id: model.slug,
|
||||
label: model.name ?? null,
|
||||
...(model.capabilities?.optionDescriptors === undefined
|
||||
? {}
|
||||
: { options: model.capabilities.optionDescriptors }),
|
||||
})) ?? [],
|
||||
...(provider.supportedRuntimeModes === undefined ||
|
||||
provider.supportedRuntimeModes.length === 0
|
||||
? {}
|
||||
@@ -2594,4 +2607,5 @@ export const layer: Layer.Layer<
|
||||
| ScheduledTaskService.ScheduledTaskService
|
||||
| ProjectService.ProjectService
|
||||
| SecretRequests.SecretRequests
|
||||
| ServerSettings.ServerSettingsService
|
||||
> = Layer.effect(OrchestratorMcpService, make);
|
||||
@@ -70,6 +70,7 @@ import {
|
||||
import * as ProviderRegistryMock from "../provider/testUtils/providerRegistryMock.ts";
|
||||
import * as ProjectService from "../project/ProjectService.ts";
|
||||
import * as ScheduledTaskService from "../scheduledTasks/ScheduledTaskService.ts";
|
||||
import * as ServerSettings from "../serverSettings.ts";
|
||||
import * as SecretRequests from "../secrets/SecretRequests.ts";
|
||||
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
|
||||
import * as McpHttpServer from "./McpHttpServer.ts";
|
||||
@@ -680,6 +681,7 @@ describe("orchestrator MCP toolkit", () => {
|
||||
Layer.provideMerge(layerOrchestration),
|
||||
Layer.provide(layerRegistry),
|
||||
Layer.provide(layerProviderRegistry),
|
||||
Layer.provide(ServerSettings.layerTest()),
|
||||
Layer.provide(layerScheduledTaskStub),
|
||||
Layer.provide(Layer.mock(ThreadSearch.ThreadSearch)({})),
|
||||
Layer.provide(
|
||||
@@ -3825,6 +3827,7 @@ describe("orchestrator MCP toolkit", () => {
|
||||
),
|
||||
),
|
||||
Layer.provide(layerProviderRegistry),
|
||||
Layer.provide(ServerSettings.layerTest()),
|
||||
Layer.provide(layerUnusedScheduledTaskStub),
|
||||
Layer.provide(Layer.mock(ProjectService.ProjectService)({})),
|
||||
Layer.provideMerge(
|
||||
|
||||
@@ -33,6 +33,7 @@ import * as ProjectService from "../project/ProjectService.ts";
|
||||
import * as ProviderRegistry from "../provider/ProviderRegistry.ts";
|
||||
import * as ScheduledTaskService from "../scheduledTasks/ScheduledTaskService.ts";
|
||||
import * as SecretRequests from "../secrets/SecretRequests.ts";
|
||||
import * as ServerSettings from "../serverSettings.ts";
|
||||
import type { McpInvocationScope } from "./McpInvocationContext.ts";
|
||||
import * as OrchestratorMcpService from "./OrchestratorMcpService.ts";
|
||||
|
||||
@@ -142,6 +143,7 @@ it.effect("task_cancel ends a delegated child's background native subagent", ()
|
||||
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
|
||||
Layer.mock(ProjectService.ProjectService)({}),
|
||||
Layer.mock(SecretRequests.SecretRequests)({}),
|
||||
ServerSettings.layerTest(),
|
||||
Layer.mock(ScheduledTaskService.ScheduledTaskService)({}),
|
||||
NodeServices.layer,
|
||||
),
|
||||
|
||||
@@ -42,6 +42,7 @@ import * as ProjectService from "../../project/ProjectService.ts";
|
||||
import * as ProviderRegistry from "../../provider/ProviderRegistry.ts";
|
||||
import * as SecretRequests from "../../secrets/SecretRequests.ts";
|
||||
import * as ScheduledTaskService from "../../scheduledTasks/ScheduledTaskService.ts";
|
||||
import * as ServerSettings from "../../serverSettings.ts";
|
||||
import * as McpHttpServer from "../McpHttpServer.ts";
|
||||
import * as McpInvocationContext from "../McpInvocationContext.ts";
|
||||
import * as McpToolAccessTestkit from "../McpToolAccess.testkit.ts";
|
||||
@@ -581,6 +582,7 @@ it.effect("refuses act-as-caller tools to a client caller", () =>
|
||||
Layer.provideMerge(McpServer.McpServer.layer),
|
||||
Layer.provide(NodeCrypto.layer),
|
||||
Layer.provide(Layer.mock(ThreadManagement.ThreadManagementService)({})),
|
||||
Layer.provide(ServerSettings.layerTest()),
|
||||
Layer.provide(Layer.mock(ProviderRegistry.ProviderRegistry)({})),
|
||||
Layer.provide(Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({})),
|
||||
Layer.provide(Layer.mock(ScheduledTaskService.ScheduledTaskService)({})),
|
||||
@@ -619,6 +621,7 @@ it.effect("a caller cannot rewrite a scheduled task that runs above its own mode
|
||||
Layer.provideMerge(McpServer.McpServer.layer),
|
||||
Layer.provide(NodeCrypto.layer),
|
||||
Layer.provide(Layer.mock(ThreadManagement.ThreadManagementService)({})),
|
||||
Layer.provide(ServerSettings.layerTest()),
|
||||
Layer.provide(Layer.mock(ProviderRegistry.ProviderRegistry)({})),
|
||||
Layer.provide(Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({})),
|
||||
Layer.provide(
|
||||
@@ -716,6 +719,7 @@ it.effect.each(["t3_thread_interrupt", "t3_workflow_stop"])(
|
||||
interruptThread: () => Effect.die("interrupt must not dispatch above the ceiling"),
|
||||
}),
|
||||
),
|
||||
Layer.provide(ServerSettings.layerTest()),
|
||||
Layer.provide(Layer.mock(ProviderRegistry.ProviderRegistry)({})),
|
||||
Layer.provide(Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({})),
|
||||
Layer.provide(Layer.mock(ScheduledTaskService.ScheduledTaskService)({})),
|
||||
@@ -807,6 +811,7 @@ it.effect("discovers workflow stop targets and requires orchestration capability
|
||||
}),
|
||||
}),
|
||||
),
|
||||
Layer.provide(ServerSettings.layerTest()),
|
||||
Layer.provide(Layer.mock(ProviderRegistry.ProviderRegistry)({})),
|
||||
Layer.provide(Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({})),
|
||||
Layer.provide(Layer.mock(ScheduledTaskService.ScheduledTaskService)({})),
|
||||
|
||||
@@ -98,6 +98,88 @@ const recordProviderUsage = (provider: string, instanceId: string | null = provi
|
||||
});
|
||||
|
||||
it.layer(NodeServices.layer)("server settings", (it) => {
|
||||
it.effect("serializes concurrent model preference edits without cancelling duplicate adds", () =>
|
||||
Effect.gen(function* () {
|
||||
const service = yield* ServerSettingsModule.ServerSettingsService;
|
||||
const provider = ProviderInstanceId.make("codex_work");
|
||||
yield* Effect.all(
|
||||
[
|
||||
service.updateSettings({
|
||||
setModelFavorites: [{ provider, model: "sol", favorite: true }],
|
||||
}),
|
||||
service.updateSettings({
|
||||
setModelFavorites: [{ provider, model: "sol", favorite: true }],
|
||||
}),
|
||||
service.updateSettings({
|
||||
setModelFavorites: [{ provider, model: "luna", favorite: true }],
|
||||
}),
|
||||
service.updateSettings({ setModelsHidden: [{ provider, model: "astra", hidden: true }] }),
|
||||
service.updateSettings({ setModelsHidden: [{ provider, model: "luna", hidden: true }] }),
|
||||
service.updateSettings({
|
||||
setProviderModelOrder: { provider, modelOrder: ["luna", "sol"] },
|
||||
}),
|
||||
],
|
||||
{ concurrency: "unbounded" },
|
||||
);
|
||||
const settings = yield* service.getSettings;
|
||||
assert.deepEqual(settings.favorites?.map((entry) => entry.model).sort(), ["luna", "sol"]);
|
||||
assert.deepEqual(settings.providerModelPreferences?.[provider]?.hiddenModels.toSorted(), [
|
||||
"astra",
|
||||
"luna",
|
||||
]);
|
||||
assert.deepEqual(settings.providerModelPreferences?.[provider]?.modelOrder, ["luna", "sol"]);
|
||||
}).pipe(Effect.provide(layerServerSettings())),
|
||||
);
|
||||
it.effect("persists and broadcasts model preferences to other connected clients", () =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
const service = yield* ServerSettingsModule.ServerSettingsService;
|
||||
const config = yield* ServerConfig.ServerConfig;
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
const provider = ProviderInstanceId.make("codex_work");
|
||||
const patch = {
|
||||
favorites: [{ provider, model: "sol" }],
|
||||
providerModelPreferences: {
|
||||
[provider]: { hiddenModels: ["astra"], modelOrder: ["sol"] },
|
||||
},
|
||||
};
|
||||
const changes = yield* service.subscribeChanges;
|
||||
yield* service.updateSettings(patch);
|
||||
const received = Option.getOrThrow(yield* Stream.runHead(changes));
|
||||
assert.deepEqual(received.favorites, patch.favorites);
|
||||
assert.deepEqual(received.providerModelPreferences, patch.providerModelPreferences);
|
||||
const saved = yield* decodeServerSettingsJson(
|
||||
yield* fs.readFileString(config.settingsPath),
|
||||
);
|
||||
assert.deepEqual(saved.favorites, patch.favorites);
|
||||
assert.deepEqual(saved.providerModelPreferences, patch.providerModelPreferences);
|
||||
const hidden = yield* service.updateSettings({
|
||||
setModelsHidden: [{ provider, model: "sol", hidden: true }],
|
||||
});
|
||||
assert.deepEqual(hidden.providerModelPreferences?.[provider], {
|
||||
hiddenModels: ["astra", "sol"],
|
||||
modelOrder: ["sol"],
|
||||
});
|
||||
const shown = yield* service.updateSettings({
|
||||
setModelsHidden: [{ provider, model: "astra", hidden: false }],
|
||||
});
|
||||
assert.deepEqual(shown.providerModelPreferences?.[provider], {
|
||||
hiddenModels: ["sol"],
|
||||
modelOrder: ["sol"],
|
||||
});
|
||||
assert.deepEqual(
|
||||
(yield* decodeServerSettingsJson(yield* fs.readFileString(config.settingsPath)))
|
||||
.providerModelPreferences,
|
||||
shown.providerModelPreferences,
|
||||
);
|
||||
yield* service.updateSettings({ favorites: [], providerModelPreferences: {} });
|
||||
// An older device's delayed seed cannot undo a clear, even after persistence.
|
||||
const migrated = yield* service.updateSettings({ migrateModelPreferences: patch });
|
||||
assert.deepEqual(migrated.favorites, []);
|
||||
assert.deepEqual(migrated.providerModelPreferences, {});
|
||||
}),
|
||||
).pipe(Effect.provide(layerServerSettings())),
|
||||
);
|
||||
it.effect("migrates saved token delivery to paragraph buffering without resetting settings", () =>
|
||||
Effect.gen(function* () {
|
||||
const config = yield* ServerConfig.ServerConfig;
|
||||
|
||||
@@ -5524,6 +5524,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
|
||||
/>
|
||||
) : null}
|
||||
<ProviderModelPicker
|
||||
environmentId={environmentId}
|
||||
compact={false}
|
||||
isComposerOwned
|
||||
disabled={providerCatalogPending || isSendBusy}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
ANTIGRAVITY_DEFAULT_MODEL,
|
||||
type ProviderInstanceId,
|
||||
type EnvironmentId,
|
||||
type ProviderDriverKind,
|
||||
type ResolvedKeybindingsConfig,
|
||||
} from "@t3tools/contracts";
|
||||
@@ -37,7 +38,10 @@ import {
|
||||
resolveShortcutCommand,
|
||||
shortcutLabelForCommand,
|
||||
} from "../../keybindings";
|
||||
import { useClientSettings, useUpdateClientSettings } from "~/hooks/useSettings";
|
||||
import {
|
||||
useEnvironmentModelPreferences,
|
||||
useToggleEnvironmentModelFavorite,
|
||||
} from "~/hooks/useSettings";
|
||||
import { cn } from "~/lib/utils";
|
||||
import { getVirtualizedScrollFadeClassName } from "../ui/scroll-area";
|
||||
import { TooltipProvider } from "../ui/tooltip";
|
||||
@@ -153,6 +157,7 @@ function ModelListSeparator() {
|
||||
}
|
||||
|
||||
export const ModelPickerContent = memo(function ModelPickerContent(props: {
|
||||
environmentId: EnvironmentId | null;
|
||||
/** The instance currently selected in the composer (combobox "value"). */
|
||||
activeInstanceId: ProviderInstanceId;
|
||||
model: string;
|
||||
@@ -202,7 +207,7 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
|
||||
const modelListRef = useRef<LegendListRef | null>(null);
|
||||
const pickerContentRef = useRef<HTMLDivElement>(null);
|
||||
const highlightedModelKeyRef = useRef<string | null>(null);
|
||||
const favorites = useClientSettings((s) => s.favorites ?? []);
|
||||
const { favorites } = useEnvironmentModelPreferences(props.environmentId);
|
||||
const activeEntry = props.instanceEntries.find(
|
||||
(entry) => entry.instanceId === props.activeInstanceId,
|
||||
);
|
||||
@@ -276,8 +281,8 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
|
||||
);
|
||||
const serverKeybindings = useAtomValue(primaryServerKeybindingsAtom);
|
||||
const keybindings = providedKeybindings ?? serverKeybindings;
|
||||
const updateSettings = useUpdateClientSettings();
|
||||
const navigate = useNavigate();
|
||||
const toggleFavorite = useToggleEnvironmentModelFavorite(props.environmentId);
|
||||
|
||||
const focusSearchInput = useCallback(() => {
|
||||
searchInputRef.current?.focus({ preventScroll: true });
|
||||
@@ -652,20 +657,6 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
|
||||
],
|
||||
);
|
||||
|
||||
const toggleFavorite = useCallback(
|
||||
(instanceId: ProviderInstanceId, model: string) => {
|
||||
const newFavorites = [...favorites];
|
||||
const index = newFavorites.findIndex((f) => f.provider === instanceId && f.model === model);
|
||||
if (index >= 0) {
|
||||
newFavorites.splice(index, 1);
|
||||
} else {
|
||||
newFavorites.push({ provider: instanceId, model });
|
||||
}
|
||||
updateSettings({ favorites: newFavorites });
|
||||
},
|
||||
[favorites, updateSettings],
|
||||
);
|
||||
|
||||
const modelJumpCommandByKey = useMemo(() => {
|
||||
const mapping = new Map<
|
||||
string,
|
||||
|
||||
@@ -40,6 +40,7 @@ function renderPicker(input: {
|
||||
const entry = providerEntry(input.instanceId, input.driver);
|
||||
return renderToStaticMarkup(
|
||||
<ProviderModelPicker
|
||||
environmentId={null}
|
||||
activeInstanceId={instanceId}
|
||||
model={input.model}
|
||||
lockedProvider={null}
|
||||
@@ -161,6 +162,7 @@ describe("ProviderModelPicker", () => {
|
||||
const activeEntry = providerEntry("codex_personal", "codex");
|
||||
const markup = renderToStaticMarkup(
|
||||
<ProviderModelPicker
|
||||
environmentId={null}
|
||||
activeInstanceId={activeEntry.instanceId}
|
||||
model="gpt-5"
|
||||
lockedProvider={null}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
ANTIGRAVITY_DEFAULT_MODEL,
|
||||
type ProviderInstanceId,
|
||||
type EnvironmentId,
|
||||
type ProviderDriverKind,
|
||||
type ResolvedKeybindingsConfig,
|
||||
} from "@t3tools/contracts";
|
||||
@@ -27,6 +28,7 @@ import { useComposerMenuProps } from "./composerEventScope";
|
||||
import { shortcutLabelForCommand } from "../../keybindings";
|
||||
|
||||
export const ProviderModelPicker = memo(function ProviderModelPicker(props: {
|
||||
environmentId: EnvironmentId | null;
|
||||
/**
|
||||
* The instance currently selected in the composer. Drives the trigger
|
||||
* icon, label and the default-highlighted combobox row.
|
||||
@@ -291,6 +293,7 @@ export const ProviderModelPicker = memo(function ProviderModelPicker(props: {
|
||||
padding="none"
|
||||
>
|
||||
<ModelPickerContent
|
||||
environmentId={props.environmentId}
|
||||
activeInstanceId={activeInstanceId}
|
||||
model={props.model}
|
||||
{...(props.selectedModels !== undefined ? { selectedModels: props.selectedModels } : {})}
|
||||
|
||||
@@ -8,6 +8,7 @@ import { createModelSelection } from "@t3tools/shared/model";
|
||||
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
|
||||
import { useNavigate } from "@tanstack/react-router";
|
||||
|
||||
import { mergeEnvironmentSettings } from "../../hooks/useSettings";
|
||||
import { getCustomModelOptionsByInstance } from "../../modelSelection";
|
||||
import {
|
||||
applyProviderInstanceSettings,
|
||||
@@ -106,7 +107,7 @@ export function ProjectDefaultsSettings({ category }: { category: ProjectSetting
|
||||
candidate.settings,
|
||||
).find((option) => option.instanceId === instanceId);
|
||||
const options = getCustomModelOptionsByInstance(
|
||||
{ ...settings, ...candidate.settings },
|
||||
mergeEnvironmentSettings(candidate.settings, settings),
|
||||
config.providers,
|
||||
).get(instanceId);
|
||||
if (
|
||||
@@ -158,6 +159,7 @@ export function ProjectDefaultsSettings({ category }: { category: ProjectSetting
|
||||
selection && activeEntry ? (
|
||||
<div className="flex min-w-0 flex-wrap items-center justify-end gap-1.5">
|
||||
<ProviderModelPicker
|
||||
environmentId={representative?.environmentId ?? null}
|
||||
activeInstanceId={selection.instanceId}
|
||||
model={selection.model}
|
||||
lockedProvider={null}
|
||||
|
||||
@@ -118,6 +118,7 @@ describe("deriveProviderModelsForDisplay", () => {
|
||||
modelOrder: [],
|
||||
onHiddenModelsChange: () => undefined,
|
||||
onFavoriteModelsChange: () => undefined,
|
||||
onToggleFavorite: () => undefined,
|
||||
onModelOrderChange: () => undefined,
|
||||
}),
|
||||
);
|
||||
@@ -157,6 +158,7 @@ describe("deriveProviderModelsForDisplay", () => {
|
||||
modelOrder: [],
|
||||
onHiddenModelsChange: () => undefined,
|
||||
onFavoriteModelsChange: () => undefined,
|
||||
onToggleFavorite: () => undefined,
|
||||
onModelOrderChange: () => undefined,
|
||||
} as const;
|
||||
|
||||
|
||||
@@ -503,8 +503,12 @@ interface ProviderInstanceCardProps {
|
||||
readonly hiddenModels: ReadonlyArray<string>;
|
||||
readonly favoriteModels: ReadonlyArray<string>;
|
||||
readonly modelOrder: ReadonlyArray<string>;
|
||||
readonly onHiddenModelsChange: (next: ReadonlyArray<string>) => void;
|
||||
readonly onHiddenModelsChange: (
|
||||
next: ReadonlyArray<string>,
|
||||
affectedModels?: ReadonlyArray<string>,
|
||||
) => void;
|
||||
readonly onFavoriteModelsChange: (next: ReadonlyArray<string>) => void;
|
||||
readonly onToggleFavorite: (model: string) => void;
|
||||
readonly onModelOrderChange: (next: ReadonlyArray<string>) => void;
|
||||
readonly onRunUpdate?: (() => void) | undefined;
|
||||
readonly onInstallRecommended?: (() => void) | undefined;
|
||||
@@ -561,6 +565,7 @@ export function ProviderInstanceCard({
|
||||
modelOrder,
|
||||
onHiddenModelsChange,
|
||||
onFavoriteModelsChange,
|
||||
onToggleFavorite,
|
||||
onModelOrderChange,
|
||||
onRunUpdate,
|
||||
onInstallRecommended,
|
||||
@@ -1173,8 +1178,8 @@ export function ProviderInstanceCard({
|
||||
<SettingsSection title="Models">
|
||||
<div className="px-3 py-3 sm:px-4">
|
||||
<p className="mb-3 text-xs text-muted-foreground">
|
||||
Favorites, visibility, and ordering are saved on this device. Custom models are saved
|
||||
on the selected environment.
|
||||
Model preferences and custom models are saved on the selected environment and shared
|
||||
across connected devices.
|
||||
</p>
|
||||
<ProviderModelsSection
|
||||
canManageCustomModels={!readOnly}
|
||||
@@ -1189,6 +1194,7 @@ export function ProviderInstanceCard({
|
||||
onChange={updateCustomModels}
|
||||
onHiddenModelsChange={onHiddenModelsChange}
|
||||
onFavoriteModelsChange={onFavoriteModelsChange}
|
||||
onToggleFavorite={onToggleFavorite}
|
||||
onModelOrderChange={onModelOrderChange}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -145,8 +145,12 @@ interface ProviderModelsSectionProps {
|
||||
* `providerInstances[id].config`).
|
||||
*/
|
||||
readonly onChange: (next: ReadonlyArray<CustomModelDefinition>) => void;
|
||||
readonly onHiddenModelsChange: (next: ReadonlyArray<string>) => void;
|
||||
readonly onHiddenModelsChange: (
|
||||
next: ReadonlyArray<string>,
|
||||
affectedModels?: ReadonlyArray<string>,
|
||||
) => void;
|
||||
readonly onFavoriteModelsChange: (next: ReadonlyArray<string>) => void;
|
||||
readonly onToggleFavorite: (model: string) => void;
|
||||
readonly onModelOrderChange: (next: ReadonlyArray<string>) => void;
|
||||
}
|
||||
|
||||
@@ -174,6 +178,7 @@ export function ProviderModelsSection({
|
||||
onChange,
|
||||
onHiddenModelsChange,
|
||||
onFavoriteModelsChange,
|
||||
onToggleFavorite,
|
||||
onModelOrderChange,
|
||||
}: ProviderModelsSectionProps) {
|
||||
const [input, setInput] = useState("");
|
||||
@@ -282,16 +287,13 @@ export function ProviderModelsSection({
|
||||
if (!canWritePreferences || hidden === hiddenModelSet.has(slug)) return;
|
||||
onHiddenModelsChange(
|
||||
hidden ? [...hiddenModels, slug] : hiddenModels.filter((model) => model !== slug),
|
||||
[slug],
|
||||
);
|
||||
};
|
||||
|
||||
const handleToggleFavorite = (slug: string) => {
|
||||
if (!canWritePreferences) return;
|
||||
if (favoriteModelSet.has(slug)) {
|
||||
onFavoriteModelsChange(favoriteModels.filter((model) => model !== slug));
|
||||
return;
|
||||
}
|
||||
onFavoriteModelsChange([...favoriteModels, slug]);
|
||||
onToggleFavorite(slug);
|
||||
};
|
||||
|
||||
// Rows only trade places with a neighbour in the same group (favorites,
|
||||
@@ -533,7 +535,10 @@ export function ProviderModelsSection({
|
||||
size="xs"
|
||||
variant="ghost-muted"
|
||||
onClick={() =>
|
||||
onHiddenModelsChange(nextHiddenModelsForBulkToggle(models, hiddenModels))
|
||||
onHiddenModelsChange(
|
||||
nextHiddenModelsForBulkToggle(models, hiddenModels),
|
||||
builtInModels.map((model) => model.slug),
|
||||
)
|
||||
}
|
||||
>
|
||||
{allBuiltInModelsHidden ? "Enable all" : "Disable all"}
|
||||
|
||||
@@ -108,6 +108,8 @@ vi.mock("../../hooks/useSettings", () => ({
|
||||
settingsState.updateEnvironmentIds.push(environmentId);
|
||||
return settingsState.updateSettings;
|
||||
},
|
||||
useUpdateEnvironmentModelPreferences: () => settingsState.updateSettings,
|
||||
useToggleEnvironmentModelFavorite: () => () => undefined,
|
||||
usePersistEnvironmentProviderInstanceMutation: (environmentId: EnvironmentId) => {
|
||||
settingsState.mutationEnvironmentIds.push(environmentId);
|
||||
return settingsState.mutateProviderInstance;
|
||||
@@ -344,16 +346,19 @@ describe("EnvironmentProviderSettings routing", () => {
|
||||
});
|
||||
|
||||
it.each([
|
||||
["onFavoriteModelsChange", { favorites: [{ provider: codexId, model: "chosen" }] }],
|
||||
[
|
||||
"onFavoriteModelsChange",
|
||||
{ setModelFavorites: [{ provider: codexId, model: "chosen", favorite: true }] },
|
||||
],
|
||||
[
|
||||
"onHiddenModelsChange",
|
||||
{ providerModelPreferences: { [codexId]: { hiddenModels: ["chosen"], modelOrder: [] } } },
|
||||
{ setModelsHidden: [{ provider: codexId, model: "chosen", hidden: true }] },
|
||||
],
|
||||
[
|
||||
"onModelOrderChange",
|
||||
{ providerModelPreferences: { [codexId]: { hiddenModels: [], modelOrder: ["chosen"] } } },
|
||||
{ setProviderModelOrder: { provider: codexId, modelOrder: ["chosen"] } },
|
||||
],
|
||||
])("saves %s on this device without changing the selected server", (action, expected) => {
|
||||
])("saves %s on the selected environment for every connected client", (action, expected) => {
|
||||
atoms.providers = [provider()];
|
||||
const panel = renderPanel();
|
||||
const editor = visitElements(
|
||||
@@ -363,8 +368,8 @@ describe("EnvironmentProviderSettings routing", () => {
|
||||
expect(editor).not.toBeNull();
|
||||
if (!editor) throw new Error("Provider editor was not rendered");
|
||||
(editor.props[action] as (models: string[]) => void)(["chosen"]);
|
||||
expect(settingsState.updateClientSettings).toHaveBeenCalledExactlyOnceWith(expected);
|
||||
expect(settingsState.updateSettings).not.toHaveBeenCalled();
|
||||
expect(settingsState.updateSettings).toHaveBeenCalledExactlyOnceWith(expected);
|
||||
expect(settingsState.updateClientSettings).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not substitute another account when the requested instance was removed", () => {
|
||||
|
||||
@@ -40,8 +40,9 @@ import { isDesktopLocalConnectionTarget } from "../../connection/desktopLocal";
|
||||
import {
|
||||
useEnvironmentSettings,
|
||||
usePersistEnvironmentProviderInstanceMutation,
|
||||
useUpdateClientSettings,
|
||||
useUpdateEnvironmentSettings,
|
||||
useUpdateEnvironmentModelPreferences,
|
||||
useToggleEnvironmentModelFavorite,
|
||||
} from "../../hooks/useSettings";
|
||||
import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon";
|
||||
import { cn } from "../../lib/utils";
|
||||
@@ -123,20 +124,12 @@ import {
|
||||
resolveSelectedProviderEnvironmentId,
|
||||
} from "./ProviderSettingsPanel.logic";
|
||||
|
||||
function withoutProviderInstanceKey<V>(
|
||||
record: Readonly<Record<ProviderInstanceId, V>> | undefined,
|
||||
key: ProviderInstanceId,
|
||||
): Record<ProviderInstanceId, V> {
|
||||
const next = { ...record } as Record<ProviderInstanceId, V>;
|
||||
delete next[key];
|
||||
return next;
|
||||
}
|
||||
|
||||
function withoutProviderInstanceFavorites(
|
||||
favorites: ReadonlyArray<{ readonly provider: ProviderInstanceId; readonly model: string }>,
|
||||
instanceId: ProviderInstanceId,
|
||||
) {
|
||||
return favorites.filter((favorite) => favorite.provider !== instanceId);
|
||||
function modelListChanges(current: ReadonlyArray<string>, next: ReadonlyArray<string>) {
|
||||
const currentSet = new Set(current);
|
||||
const nextSet = new Set(next.map((model) => model.trim()).filter(Boolean));
|
||||
return [...new Set([...currentSet, ...nextSet])]
|
||||
.filter((model) => currentSet.has(model) !== nextSet.has(model))
|
||||
.map((model) => ({ model, selected: nextSet.has(model) }));
|
||||
}
|
||||
|
||||
function providerConfigString(config: unknown, key: string): string | null {
|
||||
@@ -565,8 +558,9 @@ export function EnvironmentProviderSettings({
|
||||
const canWriteSettings = useEnvironmentScope(environmentId, AuthSettingsWriteScope);
|
||||
const canRefreshProviders = useEnvironmentScope(environmentId, AuthOrchestrationReadScope);
|
||||
const updateSettings = useUpdateEnvironmentSettings(environmentId);
|
||||
const updateModelPreferences = useUpdateEnvironmentModelPreferences(environmentId);
|
||||
const toggleModelFavorite = useToggleEnvironmentModelFavorite(environmentId);
|
||||
const persistProviderInstance = usePersistEnvironmentProviderInstanceMutation(environmentId);
|
||||
const updateClientSettings = useUpdateClientSettings();
|
||||
const serverProviders =
|
||||
useAtomValue(serverEnvironment.providersValueAtom(environmentId)) ?? EMPTY_SERVER_PROVIDERS;
|
||||
const projects = useProjects().filter((project) => project.environmentId === environmentId);
|
||||
@@ -873,50 +867,6 @@ export function EnvironmentProviderSettings({
|
||||
}
|
||||
};
|
||||
|
||||
const updateProviderModelPreferences = (
|
||||
instanceId: ProviderInstanceId,
|
||||
next: {
|
||||
readonly hiddenModels: ReadonlyArray<string>;
|
||||
readonly modelOrder: ReadonlyArray<string>;
|
||||
},
|
||||
) => {
|
||||
const hiddenModels = [...new Set(next.hiddenModels.filter((slug) => slug.trim().length > 0))];
|
||||
const modelOrder = [...new Set(next.modelOrder.filter((slug) => slug.trim().length > 0))];
|
||||
const rest = withoutProviderInstanceKey(settings.providerModelPreferences, instanceId);
|
||||
updateClientSettings({
|
||||
providerModelPreferences:
|
||||
hiddenModels.length === 0 && modelOrder.length === 0
|
||||
? rest
|
||||
: {
|
||||
...rest,
|
||||
[instanceId]: {
|
||||
hiddenModels,
|
||||
modelOrder,
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const updateProviderFavoriteModels = (
|
||||
instanceId: ProviderInstanceId,
|
||||
nextFavoriteModels: ReadonlyArray<string>,
|
||||
) => {
|
||||
const favoriteModels = [
|
||||
...new Set(
|
||||
Arr.filterMap(nextFavoriteModels, (slug) => {
|
||||
const trimmedSlug = slug.trim();
|
||||
return trimmedSlug.length > 0 ? Result.succeed(trimmedSlug) : Result.failVoid;
|
||||
}),
|
||||
),
|
||||
];
|
||||
updateClientSettings({
|
||||
favorites: [
|
||||
...withoutProviderInstanceFavorites(settings.favorites ?? [], instanceId),
|
||||
...favoriteModels.map((model) => ({ provider: instanceId, model })),
|
||||
],
|
||||
});
|
||||
};
|
||||
|
||||
const resetDefaultInstance = async (driverKind: ProviderDriverKind) => {
|
||||
const result = await persistProviderInstance({
|
||||
operation: "remove",
|
||||
@@ -1072,19 +1022,34 @@ export function EnvironmentProviderSettings({
|
||||
hiddenModels={modelPreferences.hiddenModels}
|
||||
favoriteModels={favoriteModels}
|
||||
modelOrder={modelPreferences.modelOrder}
|
||||
onHiddenModelsChange={(hiddenModels) =>
|
||||
updateProviderModelPreferences(row.instanceId, {
|
||||
...modelPreferences,
|
||||
hiddenModels,
|
||||
})
|
||||
}
|
||||
onFavoriteModelsChange={(next) => updateProviderFavoriteModels(row.instanceId, next)}
|
||||
onModelOrderChange={(modelOrder) =>
|
||||
updateProviderModelPreferences(row.instanceId, {
|
||||
...modelPreferences,
|
||||
modelOrder,
|
||||
})
|
||||
}
|
||||
onHiddenModelsChange={(next, affectedModels) => {
|
||||
const hidden = new Set(next);
|
||||
const models =
|
||||
affectedModels ??
|
||||
modelListChanges(modelPreferences.hiddenModels, next).map((change) => change.model);
|
||||
void updateModelPreferences({
|
||||
setModelsHidden: models.map((model) => ({
|
||||
provider: row.instanceId,
|
||||
model,
|
||||
hidden: hidden.has(model),
|
||||
})),
|
||||
});
|
||||
}}
|
||||
onToggleFavorite={(model) => {
|
||||
void toggleModelFavorite(row.instanceId, model);
|
||||
}}
|
||||
onFavoriteModelsChange={(next) => {
|
||||
void updateModelPreferences({
|
||||
setModelFavorites: modelListChanges(favoriteModels, next).map(
|
||||
({ model, selected }) => ({ provider: row.instanceId, model, favorite: selected }),
|
||||
),
|
||||
});
|
||||
}}
|
||||
onModelOrderChange={(modelOrder) => {
|
||||
void updateModelPreferences({
|
||||
setProviderModelOrder: { provider: row.instanceId, modelOrder },
|
||||
});
|
||||
}}
|
||||
onInstallRecommended={
|
||||
!readOnly &&
|
||||
liveProvider?.compatibilityAdvisory?.message &&
|
||||
|
||||
@@ -1122,6 +1122,7 @@ function ScheduledTaskEditorDialog({
|
||||
|
||||
<Field label="Model">
|
||||
<ProviderModelPicker
|
||||
environmentId={environmentId}
|
||||
disabled={saving || !connected}
|
||||
activeInstanceId={activeInstanceId}
|
||||
model={activeModel}
|
||||
|
||||
@@ -3259,6 +3259,7 @@ export function GeneralSettingsPanel() {
|
||||
) : (
|
||||
<div className="flex flex-wrap items-center justify-end gap-1.5">
|
||||
<ProviderModelPicker
|
||||
environmentId={environmentId}
|
||||
activeInstanceId={textGenInstanceId}
|
||||
model={textGenModel}
|
||||
lockedProvider={null}
|
||||
|
||||
@@ -301,6 +301,7 @@ export function SourceControlWritingSettingsSection() {
|
||||
) : null}
|
||||
{usesDedicatedModel && canEnableDedicatedModel ? (
|
||||
<ProviderModelPicker
|
||||
environmentId={environmentId}
|
||||
activeInstanceId={activeSelection.instanceId}
|
||||
model={activeSelection.model}
|
||||
lockedProvider={null}
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
EnvironmentAuthorizationError,
|
||||
EnvironmentId,
|
||||
ProjectId,
|
||||
ProviderInstanceId,
|
||||
type ServerSettings,
|
||||
} from "@t3tools/contracts";
|
||||
import { describe, expect, it, vi } from "vite-plus/test";
|
||||
@@ -644,3 +645,16 @@ describe("partial object patches at project scope", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it("routes legacy model preference keys exclusively to the selected environment", () => {
|
||||
const patch = {
|
||||
favorites: [{ provider: ProviderInstanceId.make("codex"), model: "sol" }],
|
||||
providerModelPreferences: {},
|
||||
};
|
||||
const plan = planScopedSettingsPatch(named, environments, patch);
|
||||
expect(plan.clientPatch).toEqual({});
|
||||
expect(plan.hasClientWrite).toBe(false);
|
||||
expect(plan.serverWrites).toEqual([
|
||||
{ environmentId: server.environmentId, label: server.label, patch },
|
||||
]);
|
||||
});
|
||||
@@ -216,7 +216,7 @@ export function planScopedSettingsPatch(
|
||||
patch: ScopedSettingsPatch,
|
||||
) {
|
||||
const clientPatch = Object.fromEntries(
|
||||
Object.entries(patch).filter(([key]) => CLIENT_KEYS.has(key)),
|
||||
Object.entries(patch).filter(([key]) => CLIENT_KEYS.has(key) && !SERVER_KEYS.has(key)),
|
||||
) as ClientSettingsPatch;
|
||||
const serverPatch = Object.fromEntries(
|
||||
Object.entries(patch).filter(([key]) => SERVER_KEYS.has(key)),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { ProviderInstanceId, UnifiedSettings } from "@t3tools/contracts";
|
||||
import { useCallback } from "react";
|
||||
|
||||
import { mergeEnvironmentSettings } from "../../hooks/useSettings";
|
||||
import { getCustomModelOptionsByInstance } from "../../modelSelection";
|
||||
import {
|
||||
applyProviderInstanceSettings,
|
||||
@@ -36,7 +37,7 @@ export function useScopedModelDisabledReason(
|
||||
candidate.settings,
|
||||
).find((option) => option.instanceId === instanceId);
|
||||
const options = getCustomModelOptionsByInstance(
|
||||
{ ...settings, ...candidate.settings },
|
||||
mergeEnvironmentSettings(candidate.settings, settings),
|
||||
config.providers,
|
||||
).get(instanceId);
|
||||
if (
|
||||
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
mergeEnvironmentSettings,
|
||||
persistClientSettingsPatch,
|
||||
useClientSettings,
|
||||
useMigrateModelPreferences,
|
||||
} from "../../hooks/useSettings";
|
||||
import { serverEnvironment } from "../../state/server";
|
||||
import { useAtomCommand } from "../../state/use-atom-command";
|
||||
@@ -42,6 +43,7 @@ export function useScopedSettings<T = UnifiedSettings>(
|
||||
const { target } = useSettingsScope();
|
||||
const clientSettings = useClientSettings();
|
||||
const serverSettings = target?.settings ?? DEFAULT_SERVER_SETTINGS;
|
||||
useMigrateModelPreferences(target?.environmentId ?? null, target?.settings ?? null);
|
||||
const settings = useMemo(
|
||||
() => mergeEnvironmentSettings(serverSettings, clientSettings),
|
||||
[clientSettings, serverSettings],
|
||||
|
||||
@@ -0,0 +1,252 @@
|
||||
import { RegistryContext } from "@effect/atom-react";
|
||||
import {
|
||||
DEFAULT_SERVER_SETTINGS,
|
||||
EnvironmentId,
|
||||
ProviderInstanceId,
|
||||
type ServerSettings,
|
||||
type ServerSettingsPatch,
|
||||
} from "@t3tools/contracts";
|
||||
import * as Cause from "effect/Cause";
|
||||
import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
|
||||
import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings";
|
||||
import { act } from "react";
|
||||
import { create, type ReactTestRenderer } from "react-test-renderer";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
|
||||
|
||||
const state = vi.hoisted(() => ({
|
||||
settings: new Map<EnvironmentId, Atom.Writable<ServerSettings | null>>(),
|
||||
persist: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/state/server", () => ({
|
||||
serverEnvironment: {
|
||||
settingsValueAtom: (id: EnvironmentId) => state.settings.get(id)!,
|
||||
updateSettings: Symbol("updateSettings"),
|
||||
},
|
||||
primaryServerSettingsAtom: undefined,
|
||||
}));
|
||||
vi.mock("~/state/use-atom-command", () => ({ useAtomCommand: () => state.persist }));
|
||||
vi.mock("~/localApi", () => ({
|
||||
ensureLocalApi: () => ({ persistence: { getClientSettings: async () => null } }),
|
||||
}));
|
||||
|
||||
import { useToggleEnvironmentModelFavorite } from "./useSettings";
|
||||
|
||||
const primaryId = EnvironmentId.make("primary");
|
||||
const remoteId = EnvironmentId.make("remote");
|
||||
const provider = ProviderInstanceId.make("codex_work");
|
||||
const model = "gpt-6.1-sol";
|
||||
const initialSettings = {
|
||||
...DEFAULT_SERVER_SETTINGS,
|
||||
favorites: [],
|
||||
providerModelPreferences: {},
|
||||
} satisfies ServerSettings;
|
||||
let registry: AtomRegistry.AtomRegistry;
|
||||
let renderer: ReactTestRenderer | undefined;
|
||||
let durable: Map<EnvironmentId, ServerSettings>;
|
||||
let writes: Array<{
|
||||
environmentId: EnvironmentId;
|
||||
patch: ServerSettingsPatch;
|
||||
finish: (success?: boolean) => void;
|
||||
}>;
|
||||
|
||||
function FavoriteEditor({ environmentId }: { environmentId: EnvironmentId }) {
|
||||
const toggle = useToggleEnvironmentModelFavorite(environmentId);
|
||||
return <button onClick={() => toggle(provider, model)}>Toggle favorite</button>;
|
||||
}
|
||||
|
||||
function editor(environmentId: EnvironmentId) {
|
||||
return (
|
||||
<RegistryContext.Provider value={registry}>
|
||||
<FavoriteEditor environmentId={environmentId} />
|
||||
</RegistryContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
async function mount(environmentId = primaryId) {
|
||||
await act(() => {
|
||||
renderer = create(editor(environmentId));
|
||||
});
|
||||
}
|
||||
|
||||
function click(): Promise<void> {
|
||||
return renderer!.root.findByType("button").props.onClick();
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
|
||||
registry = AtomRegistry.make();
|
||||
state.settings.clear();
|
||||
durable = new Map();
|
||||
writes = [];
|
||||
for (const id of [primaryId, remoteId]) {
|
||||
state.settings.set(id, Atom.make<ServerSettings | null>(initialSettings));
|
||||
durable.set(id, initialSettings);
|
||||
}
|
||||
state.persist.mockReset().mockImplementation(
|
||||
({
|
||||
environmentId,
|
||||
input,
|
||||
}: {
|
||||
environmentId: EnvironmentId;
|
||||
input: { patch: ServerSettingsPatch };
|
||||
}) =>
|
||||
new Promise((resolve) => {
|
||||
writes.push({
|
||||
environmentId,
|
||||
patch: input.patch,
|
||||
finish: (success = true) => {
|
||||
if (!success) {
|
||||
resolve(AsyncResult.failure(Cause.fail(new Error("save failed"))));
|
||||
return;
|
||||
}
|
||||
const settings = applyServerSettingsPatch(durable.get(environmentId)!, input.patch);
|
||||
durable.set(environmentId, settings);
|
||||
resolve(AsyncResult.success(settings));
|
||||
},
|
||||
});
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await act(() => renderer?.unmount());
|
||||
renderer = undefined;
|
||||
registry.dispose();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe("environment favorite toggles", () => {
|
||||
it.each([false, true])(
|
||||
"toggles twice before any server echo when initially favorited=%s",
|
||||
async (initiallyFavorited) => {
|
||||
const favorites = initiallyFavorited ? [{ provider, model }] : [];
|
||||
const settings = { ...initialSettings, favorites };
|
||||
registry.set(state.settings.get(primaryId)!, settings);
|
||||
durable.set(primaryId, settings);
|
||||
await mount();
|
||||
const first = click();
|
||||
const second = click();
|
||||
// Preparing the patch is asynchronous even when migration has already completed.
|
||||
await act(async () => {});
|
||||
expect(writes.map((write) => write.patch.setModelFavorites?.[0]?.favorite)).toEqual([
|
||||
!initiallyFavorited,
|
||||
initiallyFavorited,
|
||||
]);
|
||||
writes[0]!.finish();
|
||||
await first;
|
||||
writes[1]!.finish();
|
||||
await second;
|
||||
expect(durable.get(primaryId)!.favorites).toEqual(favorites);
|
||||
},
|
||||
);
|
||||
|
||||
it("keeps the newest pending intent when an earlier write completes", async () => {
|
||||
await mount();
|
||||
const first = click();
|
||||
const second = click();
|
||||
await act(async () => {});
|
||||
writes[0]!.finish();
|
||||
await first;
|
||||
const third = click();
|
||||
await act(async () => {});
|
||||
expect(writes.map((write) => write.patch.setModelFavorites?.[0]?.favorite)).toEqual([
|
||||
true,
|
||||
false,
|
||||
true,
|
||||
]);
|
||||
writes[1]!.finish();
|
||||
await second;
|
||||
writes[2]!.finish();
|
||||
await third;
|
||||
expect(durable.get(primaryId)!.favorites).toEqual([{ provider, model }]);
|
||||
});
|
||||
|
||||
it("clears failed intent so a retry still adds the favorite", async () => {
|
||||
await mount();
|
||||
const failed = click();
|
||||
await act(async () => {});
|
||||
writes[0]!.finish(false);
|
||||
await failed;
|
||||
const retry = click();
|
||||
await act(async () => {});
|
||||
writes[1]!.finish();
|
||||
await retry;
|
||||
expect(durable.get(primaryId)!.favorites).toEqual([{ provider, model }]);
|
||||
});
|
||||
|
||||
it("toggles the acknowledged choice when the RPC reply precedes its server echo", async () => {
|
||||
await mount();
|
||||
const first = click();
|
||||
await act(async () => {});
|
||||
writes[0]!.finish();
|
||||
await first;
|
||||
expect(registry.get(state.settings.get(primaryId)!)!.favorites).toEqual([]);
|
||||
const second = click();
|
||||
await act(async () => {});
|
||||
writes[1]!.finish();
|
||||
await second;
|
||||
expect(durable.get(primaryId)!.favorites).toEqual([]);
|
||||
});
|
||||
|
||||
it("uses a later remote change instead of a cached acknowledged choice", async () => {
|
||||
await mount();
|
||||
const first = click();
|
||||
await act(async () => {});
|
||||
writes[0]!.finish();
|
||||
await first;
|
||||
// Another client removed the favorite after this client's add was acknowledged.
|
||||
const remoteSettings = { ...initialSettings, favorites: [] };
|
||||
durable.set(primaryId, remoteSettings);
|
||||
await act(() => registry.set(state.settings.get(primaryId)!, remoteSettings));
|
||||
const second = click();
|
||||
await act(async () => {});
|
||||
writes[1]!.finish();
|
||||
await second;
|
||||
expect(durable.get(primaryId)!.favorites).toEqual([{ provider, model }]);
|
||||
});
|
||||
|
||||
it("preserves an acknowledged add when a later remove fails before its echo", async () => {
|
||||
await mount();
|
||||
const first = click();
|
||||
await act(async () => {});
|
||||
writes[0]!.finish();
|
||||
await first;
|
||||
const failed = click();
|
||||
await act(async () => {});
|
||||
writes[1]!.finish(false);
|
||||
await failed;
|
||||
const retry = click();
|
||||
await act(async () => {});
|
||||
writes[2]!.finish();
|
||||
await retry;
|
||||
expect(durable.get(primaryId)!.favorites).toEqual([]);
|
||||
});
|
||||
|
||||
it("does not reuse another environment's pending intent for the same model", async () => {
|
||||
await mount();
|
||||
const primary = click();
|
||||
await act(() => renderer!.update(editor(remoteId)));
|
||||
const remote = click();
|
||||
await act(async () => {});
|
||||
writes[0]!.finish();
|
||||
await primary;
|
||||
writes[1]!.finish();
|
||||
await remote;
|
||||
expect(durable.get(primaryId)!.favorites).toEqual([{ provider, model }]);
|
||||
expect(durable.get(remoteId)!.favorites).toEqual([{ provider, model }]);
|
||||
});
|
||||
|
||||
it("reads the latest server state even when the click callback predates its echo", async () => {
|
||||
await mount();
|
||||
const previousClick = renderer!.root.findByType("button").props.onClick as () => Promise<void>;
|
||||
const updated = { ...initialSettings, favorites: [{ provider, model }] };
|
||||
durable.set(primaryId, updated);
|
||||
await act(() => registry.set(state.settings.get(primaryId)!, updated));
|
||||
const clicked = previousClick();
|
||||
await act(async () => {});
|
||||
writes[0]!.finish();
|
||||
await clicked;
|
||||
expect(durable.get(primaryId)!.favorites).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -23,8 +23,10 @@ import {
|
||||
mergeEnvironmentSettings,
|
||||
persistClientSettingsPatch,
|
||||
persistClientSettingsUpdate,
|
||||
prepareModelPreferencesPatch,
|
||||
resolveEnvironmentIdentificationMode,
|
||||
} from "./useSettings";
|
||||
import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings";
|
||||
|
||||
beforeEach(() => {
|
||||
persistenceMocks.getClientSettings.mockReset().mockResolvedValue(null);
|
||||
@@ -45,6 +47,44 @@ describe("client settings hydration", () => {
|
||||
const onboardingCompletedAt = "2026-09-05T12:00:00.000Z";
|
||||
const complete = (current: ClientSettings) => ({ ...current, onboardingCompletedAt });
|
||||
|
||||
it("waits for saved model preferences before applying the first server edit", async () => {
|
||||
const provider = ProviderInstanceId.make("codex_work");
|
||||
let finishRead!: (settings: ClientSettings) => void;
|
||||
persistenceMocks.getClientSettings.mockReturnValueOnce(
|
||||
new Promise((resolve) => {
|
||||
finishRead = resolve;
|
||||
}),
|
||||
);
|
||||
const patch = prepareModelPreferencesPatch(DEFAULT_SERVER_SETTINGS, {
|
||||
setModelFavorites: [{ provider, model: "new", favorite: true }],
|
||||
setModelsHidden: [{ provider, model: "new", hidden: true }],
|
||||
});
|
||||
expect(getClientSettings()).toBe(DEFAULT_CLIENT_SETTINGS);
|
||||
finishRead({
|
||||
...savedSettings,
|
||||
providerModelPreferences: {
|
||||
[provider]: { hiddenModels: ["saved-hidden"], modelOrder: ["saved-order"] },
|
||||
},
|
||||
});
|
||||
const edited = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, await patch);
|
||||
expect(edited.favorites).toEqual([...savedSettings.favorites, { provider, model: "new" }]);
|
||||
expect(edited.providerModelPreferences?.[provider]).toEqual({
|
||||
hiddenModels: ["saved-hidden", "new"],
|
||||
modelOrder: ["saved-order"],
|
||||
});
|
||||
});
|
||||
|
||||
it("does not prepare a model preference write after a failed legacy read", async () => {
|
||||
const failure = new Error("storage unavailable");
|
||||
vi.spyOn(console, "error").mockImplementation(() => undefined);
|
||||
persistenceMocks.getClientSettings.mockRejectedValueOnce(failure);
|
||||
await expect(
|
||||
prepareModelPreferencesPatch(DEFAULT_SERVER_SETTINGS, {
|
||||
setProviderModelOrder: { provider: ProviderInstanceId.make("codex_work"), modelOrder: [] },
|
||||
}),
|
||||
).rejects.toBe(failure);
|
||||
});
|
||||
|
||||
it("rejects completion after a failed read and preserves saved preferences on retry", async () => {
|
||||
const failure = new Error("storage unavailable");
|
||||
vi.spyOn(console, "error").mockImplementation(() => undefined);
|
||||
@@ -451,3 +491,20 @@ describe("onboarding completion persistence", () => {
|
||||
expect(persist).toHaveBeenLastCalledWith(completedSettings);
|
||||
});
|
||||
});
|
||||
|
||||
it("uses the environment's model preferences after migration, including empty lists", () => {
|
||||
const client = {
|
||||
...DEFAULT_CLIENT_SETTINGS,
|
||||
favorites: [{ provider: ProviderInstanceId.make("codex_work"), model: "old" }],
|
||||
providerModelPreferences: { codex_work: { hiddenModels: ["old"], modelOrder: [] } },
|
||||
};
|
||||
expect(mergeEnvironmentSettings(DEFAULT_SERVER_SETTINGS, client).favorites).toEqual(
|
||||
client.favorites,
|
||||
);
|
||||
const merged = mergeEnvironmentSettings(
|
||||
{ ...DEFAULT_SERVER_SETTINGS, favorites: [], providerModelPreferences: {} },
|
||||
client,
|
||||
);
|
||||
expect(merged.favorites).toEqual([]);
|
||||
expect(merged.providerModelPreferences).toEqual({});
|
||||
});
|
||||
@@ -9,14 +9,15 @@
|
||||
* access is intentionally named as such so environment-sensitive consumers
|
||||
* cannot silently read the wrong server's settings.
|
||||
*/
|
||||
import { useCallback, useMemo, useSyncExternalStore } from "react";
|
||||
import { useAtomValue } from "@effect/atom-react";
|
||||
import { useCallback, useEffect, useMemo, useRef, useSyncExternalStore, use } from "react";
|
||||
import { RegistryContext, useAtomValue } from "@effect/atom-react";
|
||||
import {
|
||||
DEFAULT_SERVER_SETTINGS,
|
||||
AuthSettingsWriteScope,
|
||||
requiredScopesForServerSettingsPatch,
|
||||
type EnvironmentId,
|
||||
type ProviderInstanceMutation,
|
||||
type ProviderInstanceId,
|
||||
ServerSettings,
|
||||
type ServerSettingsPatch,
|
||||
sessionGrantsScope,
|
||||
@@ -46,6 +47,10 @@ import {
|
||||
subscribeToThemePreview,
|
||||
themeAllowsSidebarArtwork,
|
||||
} from "~/themePalette";
|
||||
import {
|
||||
modelPreferencesMigrationPatch,
|
||||
resolveModelPreferences,
|
||||
} from "@t3tools/shared/serverSettings";
|
||||
import * as Struct from "effect/Struct";
|
||||
import * as Option from "effect/Option";
|
||||
import { AsyncResult, Atom } from "effect/reactivity";
|
||||
@@ -318,7 +323,38 @@ export function mergeEnvironmentSettings(
|
||||
): UnifiedSettings {
|
||||
// Decode drops retired client keys, but older untyped persistence adapters
|
||||
// can still return them. Server-owned values must always win.
|
||||
return { ...clientSettings, ...serverSettings };
|
||||
return {
|
||||
...clientSettings,
|
||||
...serverSettings,
|
||||
...resolveModelPreferences(serverSettings, clientSettings),
|
||||
};
|
||||
}
|
||||
|
||||
const pendingModelPreferencesMigrations = new Set<EnvironmentId>();
|
||||
|
||||
/** Seed saved client preferences once without overwriting another client's edits. */
|
||||
export function useMigrateModelPreferences(
|
||||
environmentId: EnvironmentId | null,
|
||||
settings: ServerSettings | null,
|
||||
) {
|
||||
const clientSettings = useClientSettingsValue();
|
||||
const hydrated = useClientSettingsHydrated();
|
||||
const migrate = useAtomCommand(serverEnvironment.updateSettings, { reportFailure: false });
|
||||
useEffect(() => {
|
||||
if (
|
||||
!environmentId ||
|
||||
!settings ||
|
||||
!hydrated ||
|
||||
pendingModelPreferencesMigrations.has(environmentId)
|
||||
)
|
||||
return;
|
||||
const patch = modelPreferencesMigrationPatch(settings, clientSettings);
|
||||
if (!patch) return;
|
||||
pendingModelPreferencesMigrations.add(environmentId);
|
||||
void migrate({ environmentId, input: { patch } }).finally(() =>
|
||||
pendingModelPreferencesMigrations.delete(environmentId),
|
||||
);
|
||||
}, [clientSettings, environmentId, hydrated, migrate, settings]);
|
||||
}
|
||||
|
||||
function useMergedSettings<T>(
|
||||
@@ -391,12 +427,135 @@ export function useLegacySidebarEnabled(): boolean {
|
||||
return settingsHydrated && legacySidebarEnabled;
|
||||
}
|
||||
|
||||
/** Hydrate legacy preferences before a mutation can claim either server-owned list. */
|
||||
export async function prepareModelPreferencesPatch(
|
||||
settings: ServerSettings,
|
||||
patch: ServerSettingsPatch,
|
||||
): Promise<ServerSettingsPatch> {
|
||||
if (settings.favorites === null || settings.providerModelPreferences === null) {
|
||||
await ensureClientSettingsHydrated();
|
||||
}
|
||||
return { ...modelPreferencesMigrationPatch(settings, getClientSettings()), ...patch };
|
||||
}
|
||||
|
||||
export function useUpdateEnvironmentModelPreferences(environmentId: EnvironmentId | null) {
|
||||
const settings = useAtomValue(
|
||||
environmentId ? serverEnvironment.settingsValueAtom(environmentId) : noEnvironmentSettingsAtom,
|
||||
);
|
||||
const save = useAtomCommand(serverEnvironment.updateSettings, "model preferences update");
|
||||
return useCallback(
|
||||
async (patch: ServerSettingsPatch) => {
|
||||
if (!environmentId || !settings) return;
|
||||
let prepared: ServerSettingsPatch;
|
||||
try {
|
||||
prepared = await prepareModelPreferencesPatch(settings, patch);
|
||||
} catch {
|
||||
toastManager.add({
|
||||
type: "error",
|
||||
title: "Model preferences not saved",
|
||||
description: "Could not load saved model preferences. Try again.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
return save({ environmentId, input: { patch: prepared } });
|
||||
},
|
||||
[environmentId, save, settings],
|
||||
);
|
||||
}
|
||||
|
||||
/** Send the desired state so two clients adding a favorite do not cancel each other. */
|
||||
export function useToggleEnvironmentModelFavorite(environmentId: EnvironmentId | null) {
|
||||
const registry = use(RegistryContext);
|
||||
const pendingFavorites = useRef(
|
||||
new Map<string, { favorite: boolean; settings: ServerSettings; pending: boolean }>(),
|
||||
);
|
||||
const save = useUpdateEnvironmentModelPreferences(environmentId);
|
||||
return useCallback(
|
||||
async (provider: ProviderInstanceId, model: string) => {
|
||||
if (!environmentId) return;
|
||||
const settingsAtom = serverEnvironment.settingsValueAtom(environmentId);
|
||||
const settings = registry.get(settingsAtom);
|
||||
if (!settings) return;
|
||||
if (settings.favorites === null) {
|
||||
try {
|
||||
await ensureClientSettingsHydrated();
|
||||
} catch {
|
||||
toastManager.add({
|
||||
type: "error",
|
||||
title: "Favorites not saved",
|
||||
description: "Could not load saved model preferences. Try again.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
const currentSettings = registry.get(settingsAtom) ?? settings;
|
||||
const favorites = resolveModelPreferences(currentSettings, getClientSettings()).favorites;
|
||||
const key = JSON.stringify([environmentId, provider, model]);
|
||||
const previous = pendingFavorites.current.get(key);
|
||||
// Keep the acknowledged choice until the config stream catches up with the RPC reply.
|
||||
const previousFavorite =
|
||||
previous && (previous.pending || previous.settings.favorites === currentSettings.favorites)
|
||||
? previous.favorite
|
||||
: favorites.some(
|
||||
(favorite) => favorite.provider === provider && favorite.model === model,
|
||||
);
|
||||
const pending = {
|
||||
favorite: !previousFavorite,
|
||||
settings: currentSettings,
|
||||
pending: true,
|
||||
};
|
||||
pendingFavorites.current.set(key, pending);
|
||||
try {
|
||||
const result = await save({
|
||||
setModelFavorites: [{ provider, model, favorite: pending.favorite }],
|
||||
});
|
||||
if (
|
||||
result &&
|
||||
AsyncResult.isSuccess(result) &&
|
||||
registry.get(settingsAtom)?.favorites === currentSettings.favorites
|
||||
) {
|
||||
pending.pending = false;
|
||||
}
|
||||
} finally {
|
||||
if (pending.pending && pendingFavorites.current.get(key) === pending) {
|
||||
if (
|
||||
previous &&
|
||||
!previous.pending &&
|
||||
registry.get(settingsAtom)?.favorites === previous.settings.favorites
|
||||
) {
|
||||
pendingFavorites.current.set(key, previous);
|
||||
} else {
|
||||
pendingFavorites.current.delete(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
[environmentId, registry, save],
|
||||
);
|
||||
}
|
||||
|
||||
const noEnvironmentSettingsAtom = Atom.make<ServerSettings | null>(null);
|
||||
|
||||
/** Model pickers may render before a settings target has been selected. */
|
||||
export function useEnvironmentModelPreferences(environmentId: EnvironmentId | null) {
|
||||
const settings = useAtomValue(
|
||||
environmentId ? serverEnvironment.settingsValueAtom(environmentId) : noEnvironmentSettingsAtom,
|
||||
);
|
||||
const legacy = useClientSettingsValue();
|
||||
useMigrateModelPreferences(environmentId, settings);
|
||||
return useMemo(
|
||||
() => resolveModelPreferences(settings ?? DEFAULT_SERVER_SETTINGS, legacy),
|
||||
[legacy, settings],
|
||||
);
|
||||
}
|
||||
|
||||
/** Read current settings for one environment, merged with client-local preferences. */
|
||||
export function useEnvironmentSettings<T = UnifiedSettings>(
|
||||
environmentId: EnvironmentId,
|
||||
selector?: (settings: UnifiedSettings) => T,
|
||||
): T {
|
||||
const serverSettings = useAtomValue(serverEnvironment.settingsValueAtom(environmentId));
|
||||
useMigrateModelPreferences(environmentId, serverSettings);
|
||||
return useMergedSettings(serverSettings ?? DEFAULT_SERVER_SETTINGS, selector);
|
||||
}
|
||||
|
||||
@@ -419,7 +578,13 @@ export function usePersistEnvironmentProviderInstanceMutation(environmentId: Env
|
||||
export function usePrimarySettings<T = UnifiedSettings>(
|
||||
selector?: (settings: UnifiedSettings) => T,
|
||||
): T {
|
||||
return useMergedSettings(useAtomValue(primaryServerSettingsAtom), selector);
|
||||
const environment = usePrimaryEnvironment();
|
||||
const settings = useAtomValue(primaryServerSettingsAtom);
|
||||
useMigrateModelPreferences(
|
||||
environment?.environmentId ?? null,
|
||||
environment?.serverConfig?.settings ?? null,
|
||||
);
|
||||
return useMergedSettings(settings, selector);
|
||||
}
|
||||
|
||||
export const PRIMARY_SETTINGS_UNAVAILABLE_MESSAGE =
|
||||
@@ -618,7 +783,7 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
|
||||
return updateSettings;
|
||||
}
|
||||
|
||||
export function useUpdateEnvironmentSettings(environmentId: EnvironmentId) {
|
||||
export function useUpdateEnvironmentSettings(environmentId: EnvironmentId | null) {
|
||||
return useUpdateSettingsTarget(environmentId);
|
||||
}
|
||||
|
||||
|
||||
@@ -1080,3 +1080,34 @@ describe("requiredScopesForProjectMutation", () => {
|
||||
).toEqual(["orchestration:operate", "settings:write"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("server model preferences", () => {
|
||||
it("decodes a visibility update scoped to a provider instance and model", () => {
|
||||
const patch = { setModelsHidden: [{ provider: "codex_work", model: "sol", hidden: false }] };
|
||||
expect(decodeServerSettingsPatch(patch)).toEqual(patch);
|
||||
expect(() =>
|
||||
decodeServerSettingsPatch({ setModelsHidden: [{ provider: "codex_work", model: "sol" }] }),
|
||||
).toThrow();
|
||||
});
|
||||
it("distinguishes unmigrated lists from deliberately cleared lists", () => {
|
||||
expect(decodeServerSettings({})).toMatchObject({
|
||||
favorites: null,
|
||||
providerModelPreferences: null,
|
||||
});
|
||||
const input = { favorites: [], providerModelPreferences: {} };
|
||||
expect(encodeServerSettings(decodeServerSettings(input))).toMatchObject(input);
|
||||
expect(decodeServerSettingsPatch(input)).toEqual(input);
|
||||
});
|
||||
|
||||
it("round trips favorites and hidden models keyed by provider instance", () => {
|
||||
const input = {
|
||||
favorites: [{ provider: "codex_work", model: "sol" }],
|
||||
providerModelPreferences: { codex_work: { hiddenModels: ["astra"], modelOrder: ["sol"] } },
|
||||
};
|
||||
expect(encodeServerSettings(decodeServerSettings(input))).toMatchObject(input);
|
||||
expect(decodeServerSettingsPatch(input)).toEqual(input);
|
||||
expect(decodeServerSettingsPatch({ migrateModelPreferences: input })).toEqual({
|
||||
migrateModelPreferences: input,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -299,6 +299,25 @@ export const DiffColorScheme = Schema.Literals(["red-green", "blue-orange"]);
|
||||
export const ChatWidth = Schema.Literals(["comfortable", "wide", "full"]);
|
||||
export type ChatWidth = typeof ChatWidth.Type;
|
||||
|
||||
/** Preferences shared by clients connected to the same environment. */
|
||||
export const ModelFavorite = Schema.Struct({
|
||||
// The historical field name is retained; default instance ids also decode old provider-kind favorites.
|
||||
provider: ProviderInstanceId,
|
||||
model: TrimmedNonEmptyString,
|
||||
});
|
||||
export const ModelFavorites = Schema.Array(ModelFavorite);
|
||||
export const ProviderModelPreferences = Schema.Record(
|
||||
ProviderInstanceId,
|
||||
Schema.Struct({
|
||||
hiddenModels: Schema.Array(Schema.String).pipe(Schema.withDecodingDefault(Effect.succeed([]))),
|
||||
modelOrder: Schema.Array(Schema.String).pipe(Schema.withDecodingDefault(Effect.succeed([]))),
|
||||
}),
|
||||
);
|
||||
export type ModelPreferences = {
|
||||
readonly favorites: typeof ModelFavorites.Type;
|
||||
readonly providerModelPreferences: typeof ProviderModelPreferences.Type;
|
||||
};
|
||||
|
||||
export const ClientSettingsSchema = Schema.Struct({
|
||||
notificationMode: NotificationMode.pipe(
|
||||
Schema.withDecodingDefault(Effect.succeed("off" as const)),
|
||||
@@ -413,31 +432,11 @@ export const ClientSettingsSchema = Schema.Struct({
|
||||
persistComposerContextStrip: Schema.Boolean.pipe(
|
||||
Schema.withDecodingDefault(Effect.succeed(false)),
|
||||
),
|
||||
// Model favorites. Historically keyed by provider kind, now
|
||||
// widened to `ProviderInstanceId` so users can favorite a specific model
|
||||
// on a custom provider instance (e.g. "Codex Personal · gpt-5") without
|
||||
// the UI collapsing it into the same bucket as the default Codex. The
|
||||
// widening is backward-compatible by construction: prior provider-kind
|
||||
// strings satisfy the `ProviderInstanceId` slug schema, so previously
|
||||
// persisted favorites decode unchanged and continue to point at the
|
||||
// default instance for their kind (because `defaultInstanceIdForDriver(kind)`
|
||||
// uses the same slug). The field name is kept as `provider` for storage
|
||||
// stability; new call sites should treat the value as an instance id.
|
||||
favorites: Schema.Array(
|
||||
Schema.Struct({
|
||||
provider: ProviderInstanceId,
|
||||
model: TrimmedNonEmptyString,
|
||||
}),
|
||||
).pipe(Schema.withDecodingDefault(Effect.succeed([]))),
|
||||
providerModelPreferences: Schema.Record(
|
||||
ProviderInstanceId,
|
||||
Schema.Struct({
|
||||
hiddenModels: Schema.Array(Schema.String).pipe(
|
||||
Schema.withDecodingDefault(Effect.succeed([])),
|
||||
),
|
||||
modelOrder: Schema.Array(Schema.String).pipe(Schema.withDecodingDefault(Effect.succeed([]))),
|
||||
}),
|
||||
).pipe(Schema.withDecodingDefault(Effect.succeed({}))),
|
||||
// Legacy client values retained for migration to the connected environment.
|
||||
favorites: ModelFavorites.pipe(Schema.withDecodingDefault(Effect.succeed([]))),
|
||||
providerModelPreferences: ProviderModelPreferences.pipe(
|
||||
Schema.withDecodingDefault(Effect.succeed({})),
|
||||
),
|
||||
pullRequestMergeMethodOverrides: Schema.Record(
|
||||
TrimmedNonEmptyString,
|
||||
PullRequestMergeMethod,
|
||||
@@ -1060,6 +1059,12 @@ export const StorageCleanupSettings = Schema.Struct({
|
||||
export type StorageCleanupSettings = typeof StorageCleanupSettings.Type;
|
||||
|
||||
export const ServerSettings = Schema.Struct({
|
||||
// Null means no client has migrated this preference yet; an empty value is
|
||||
// authoritative so another device cannot resurrect cleared preferences.
|
||||
favorites: Schema.NullOr(ModelFavorites).pipe(Schema.withDecodingDefault(Effect.succeed(null))),
|
||||
providerModelPreferences: Schema.NullOr(ProviderModelPreferences).pipe(
|
||||
Schema.withDecodingDefault(Effect.succeed(null)),
|
||||
),
|
||||
worktreeCleanup: WorktreeCleanup.pipe(Schema.withDecodingDefault(Effect.succeed(null))),
|
||||
storageCleanup: StorageCleanupSettings.pipe(
|
||||
Schema.withDecodingDefault(Effect.succeed(Schema.decodeSync(StorageCleanupSettings)({}))),
|
||||
@@ -1384,7 +1389,7 @@ export class ServerSettingsError extends Schema.TaggedError<ServerSettingsError>
|
||||
|
||||
// ── Unified type ─────────────────────────────────────────────────────
|
||||
|
||||
export type UnifiedSettings = ServerSettings & ClientSettings;
|
||||
export type UnifiedSettings = Omit<ServerSettings, keyof ModelPreferences> & ClientSettings;
|
||||
export const DEFAULT_UNIFIED_SETTINGS: UnifiedSettings = {
|
||||
...DEFAULT_SERVER_SETTINGS,
|
||||
...DEFAULT_CLIENT_SETTINGS,
|
||||
@@ -1399,6 +1404,24 @@ const ModelSelectionPatch = Schema.Struct({
|
||||
});
|
||||
|
||||
export const ServerSettingsPatch = Schema.Struct({
|
||||
setModelFavorites: Schema.optionalKey(
|
||||
Schema.Array(Schema.Struct({ ...ModelFavorite.fields, favorite: Schema.Boolean })),
|
||||
),
|
||||
setModelsHidden: Schema.optionalKey(
|
||||
Schema.Array(Schema.Struct({ ...ModelFavorite.fields, hidden: Schema.Boolean })),
|
||||
),
|
||||
setProviderModelOrder: Schema.optionalKey(
|
||||
Schema.Struct({ provider: ProviderInstanceId, modelOrder: Schema.Array(TrimmedString) }),
|
||||
),
|
||||
favorites: Schema.optionalKey(ModelFavorites),
|
||||
providerModelPreferences: Schema.optionalKey(ProviderModelPreferences),
|
||||
// Applied under the settings service's write lock, only while still unset.
|
||||
migrateModelPreferences: Schema.optionalKey(
|
||||
Schema.Struct({
|
||||
favorites: Schema.optionalKey(ModelFavorites),
|
||||
providerModelPreferences: Schema.optionalKey(ProviderModelPreferences),
|
||||
}),
|
||||
),
|
||||
worktreeCleanup: Schema.optionalKey(
|
||||
Schema.NullOr(
|
||||
Schema.Union([
|
||||
@@ -1606,27 +1629,8 @@ export const ClientSettingsPatch = Schema.Struct({
|
||||
fontFamilyTerminal: Schema.optionalKey(FontFamilyPreference),
|
||||
fontSmoothing: Schema.optionalKey(Schema.Boolean),
|
||||
persistComposerContextStrip: Schema.optionalKey(Schema.Boolean),
|
||||
favorites: Schema.optionalKey(
|
||||
Schema.Array(
|
||||
Schema.Struct({
|
||||
provider: ProviderInstanceId,
|
||||
model: TrimmedNonEmptyString,
|
||||
}),
|
||||
),
|
||||
),
|
||||
providerModelPreferences: Schema.optionalKey(
|
||||
Schema.Record(
|
||||
ProviderInstanceId,
|
||||
Schema.Struct({
|
||||
hiddenModels: Schema.Array(Schema.String).pipe(
|
||||
Schema.withDecodingDefault(Effect.succeed([])),
|
||||
),
|
||||
modelOrder: Schema.Array(Schema.String).pipe(
|
||||
Schema.withDecodingDefault(Effect.succeed([])),
|
||||
),
|
||||
}),
|
||||
),
|
||||
),
|
||||
favorites: Schema.optionalKey(ModelFavorites),
|
||||
providerModelPreferences: Schema.optionalKey(ProviderModelPreferences),
|
||||
pullRequestMergeMethodOverrides: Schema.optionalKey(
|
||||
Schema.Record(TrimmedNonEmptyString, PullRequestMergeMethod),
|
||||
),
|
||||
|
||||
@@ -13,6 +13,8 @@ import { createModelSelection } from "./model.ts";
|
||||
import { resolveProjectScripts, projectScriptsInheritDefaults } from "./projectScripts.ts";
|
||||
import {
|
||||
applyServerSettingsPatch,
|
||||
modelPreferencesMigrationPatch,
|
||||
resolveModelPreferences,
|
||||
isModelSelectionProviderEnabled,
|
||||
parsePersistedServerObservabilitySettings,
|
||||
resolveSourceControlWriterModelSelection,
|
||||
@@ -823,3 +825,175 @@ describe("worktreesDirectory", () => {
|
||||
expect(back.previousWorktreesDirectories).toEqual(["/b"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("synchronized model preferences", () => {
|
||||
const provider = ProviderInstanceId.make("codex_work");
|
||||
const legacy = {
|
||||
favorites: [{ provider, model: "sol" }],
|
||||
providerModelPreferences: { [provider]: { hiddenModels: ["astra"], modelOrder: ["sol"] } },
|
||||
};
|
||||
|
||||
it("keeps concurrent adds and removes of the same favorite idempotent", () => {
|
||||
const add = { setModelFavorites: [{ provider, model: "sol", favorite: true }] };
|
||||
const first = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, add);
|
||||
expect(applyServerSettingsPatch(first, add).favorites).toEqual([{ provider, model: "sol" }]);
|
||||
const remove = { setModelFavorites: [{ provider, model: "sol", favorite: false }] };
|
||||
const removed = applyServerSettingsPatch(first, remove);
|
||||
expect(applyServerSettingsPatch(removed, remove).favorites).toEqual([]);
|
||||
});
|
||||
|
||||
it("preserves unrelated models and providers during bulk visibility and order edits", () => {
|
||||
const otherProvider = ProviderInstanceId.make("claude_work");
|
||||
const initial = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
|
||||
providerModelPreferences: {
|
||||
...legacy.providerModelPreferences,
|
||||
[otherProvider]: { hiddenModels: ["opus"], modelOrder: ["sonnet", "opus"] },
|
||||
},
|
||||
});
|
||||
const mobile = applyServerSettingsPatch(initial, {
|
||||
setModelsHidden: [{ provider, model: "luna", hidden: true }],
|
||||
});
|
||||
const web = applyServerSettingsPatch(mobile, {
|
||||
setModelsHidden: [
|
||||
{ provider, model: "astra", hidden: false },
|
||||
{ provider, model: "sol", hidden: true },
|
||||
],
|
||||
setProviderModelOrder: { provider, modelOrder: ["sol", "luna", "astra"] },
|
||||
});
|
||||
expect(web.providerModelPreferences?.[provider]).toEqual({
|
||||
hiddenModels: ["luna", "sol"],
|
||||
modelOrder: ["sol", "luna", "astra"],
|
||||
});
|
||||
expect(web.providerModelPreferences?.[otherProvider]).toEqual(
|
||||
initial.providerModelPreferences?.[otherProvider],
|
||||
);
|
||||
expect(
|
||||
applyServerSettingsPatch(web, {
|
||||
setProviderModelOrder: { provider, modelOrder: [] },
|
||||
}).providerModelPreferences?.[provider],
|
||||
).toEqual({ hiddenModels: ["luna", "sol"], modelOrder: [] });
|
||||
});
|
||||
|
||||
it("migrates saved lists independently without claiming empty legacy lists", () => {
|
||||
expect(modelPreferencesMigrationPatch(DEFAULT_SERVER_SETTINGS, {})).toBeNull();
|
||||
const patch = modelPreferencesMigrationPatch(DEFAULT_SERVER_SETTINGS, legacy)!;
|
||||
const migrated = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, patch);
|
||||
expect(resolveModelPreferences(migrated)).toEqual(legacy);
|
||||
expect(modelPreferencesMigrationPatch(migrated, legacy)).toBeNull();
|
||||
const favoritesOnly = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { favorites: [] });
|
||||
expect(modelPreferencesMigrationPatch(favoritesOnly, legacy)).toEqual({
|
||||
migrateModelPreferences: { providerModelPreferences: legacy.providerModelPreferences },
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps explicit clears when a delayed migration arrives from another device", () => {
|
||||
const staleMigration = modelPreferencesMigrationPatch(DEFAULT_SERVER_SETTINGS, legacy)!;
|
||||
const cleared = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
|
||||
favorites: [],
|
||||
providerModelPreferences: {},
|
||||
});
|
||||
expect(
|
||||
resolveModelPreferences(applyServerSettingsPatch(cleared, staleMigration), legacy),
|
||||
).toEqual({
|
||||
favorites: [],
|
||||
providerModelPreferences: {},
|
||||
});
|
||||
});
|
||||
|
||||
it("replaces visibility records so showing every model clears removed entries", () => {
|
||||
const migrated = applyServerSettingsPatch(
|
||||
DEFAULT_SERVER_SETTINGS,
|
||||
modelPreferencesMigrationPatch(DEFAULT_SERVER_SETTINGS, legacy)!,
|
||||
);
|
||||
const cleared = applyServerSettingsPatch(migrated, { providerModelPreferences: {} });
|
||||
expect(cleared.providerModelPreferences).toEqual({});
|
||||
expect(cleared.favorites).toEqual(legacy.favorites);
|
||||
});
|
||||
|
||||
it("sets favorites without losing changes from other clients", () => {
|
||||
const first = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
|
||||
setModelFavorites: [{ provider, model: "sol", favorite: true }],
|
||||
});
|
||||
const second = applyServerSettingsPatch(first, {
|
||||
setModelFavorites: [{ provider, model: "astra", favorite: true }],
|
||||
migrateModelPreferences: { favorites: [] },
|
||||
});
|
||||
expect(second.favorites).toEqual([
|
||||
{ provider, model: "sol" },
|
||||
{ provider, model: "astra" },
|
||||
]);
|
||||
const removed = applyServerSettingsPatch(second, {
|
||||
setModelFavorites: [{ provider, model: "sol", favorite: false }],
|
||||
});
|
||||
expect(removed.favorites).toEqual([{ provider, model: "astra" }]);
|
||||
expect(
|
||||
applyServerSettingsPatch(removed, {
|
||||
setModelFavorites: [{ provider, model: "astra", favorite: false }],
|
||||
}).favorites,
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it("sets visibility against the latest state without replacing other models or their order", () => {
|
||||
const initial = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
|
||||
providerModelPreferences: legacy.providerModelPreferences,
|
||||
});
|
||||
const first = applyServerSettingsPatch(initial, {
|
||||
setModelsHidden: [{ provider, model: "sol", hidden: true }],
|
||||
});
|
||||
const second = applyServerSettingsPatch(first, {
|
||||
setModelsHidden: [{ provider, model: "luna", hidden: true }],
|
||||
});
|
||||
expect(second.providerModelPreferences?.[provider]).toEqual({
|
||||
hiddenModels: ["astra", "sol", "luna"],
|
||||
modelOrder: ["sol"],
|
||||
});
|
||||
const restored = applyServerSettingsPatch(second, {
|
||||
setModelsHidden: [{ provider, model: "sol", hidden: false }],
|
||||
});
|
||||
expect(restored.providerModelPreferences?.[provider]).toEqual({
|
||||
hiddenModels: ["astra", "luna"],
|
||||
modelOrder: ["sol"],
|
||||
});
|
||||
expect(
|
||||
applyServerSettingsPatch(restored, {
|
||||
setModelsHidden: [{ provider, model: "sol", hidden: false }],
|
||||
}).providerModelPreferences,
|
||||
).toEqual(restored.providerModelPreferences);
|
||||
expect(
|
||||
applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
|
||||
setModelsHidden: [{ provider, model: "sol", hidden: true }],
|
||||
}).providerModelPreferences?.[provider]?.hiddenModels,
|
||||
).toEqual(["sol"]);
|
||||
});
|
||||
|
||||
it("keeps the same model on different provider instances as separate favorites", () => {
|
||||
const otherProvider = ProviderInstanceId.make("codex_personal");
|
||||
const first = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
|
||||
setModelFavorites: [{ provider, model: "sol", favorite: true }],
|
||||
});
|
||||
const second = applyServerSettingsPatch(first, {
|
||||
setModelFavorites: [{ provider: otherProvider, model: "sol", favorite: true }],
|
||||
});
|
||||
expect(second.favorites).toEqual([
|
||||
{ provider, model: "sol" },
|
||||
{ provider: otherProvider, model: "sol" },
|
||||
]);
|
||||
expect(
|
||||
applyServerSettingsPatch(second, {
|
||||
setModelFavorites: [{ provider, model: "sol", favorite: false }],
|
||||
}).favorites,
|
||||
).toEqual([{ provider: otherProvider, model: "sol" }]);
|
||||
});
|
||||
|
||||
it("keeps explicit edits when the same patch also carries a legacy migration", () => {
|
||||
const edited = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
|
||||
...modelPreferencesMigrationPatch(DEFAULT_SERVER_SETTINGS, legacy),
|
||||
favorites: [],
|
||||
providerModelPreferences: {},
|
||||
});
|
||||
expect(resolveModelPreferences(edited, legacy)).toEqual({
|
||||
favorites: [],
|
||||
providerModelPreferences: {},
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { ModelPreferences } from "@t3tools/contracts/settings";
|
||||
import {
|
||||
isProviderAvailable,
|
||||
isUnconfiguredDefaultInstanceEnabled,
|
||||
@@ -260,6 +261,11 @@ export function applyServerSettingsPatch(
|
||||
const patch = translateLegacyProjectOverridePatch(current, rawPatch);
|
||||
const selectionPatch = patch.textGenerationModelSelection;
|
||||
const {
|
||||
migrateModelPreferences,
|
||||
setModelFavorites,
|
||||
setModelsHidden,
|
||||
setProviderModelOrder,
|
||||
providerModelPreferences,
|
||||
automaticGitFetchInterval,
|
||||
providerHealthRefreshInterval,
|
||||
backgroundActivityProfile,
|
||||
@@ -314,9 +320,57 @@ export function applyServerSettingsPatch(
|
||||
},
|
||||
}
|
||||
: undefined;
|
||||
const initialFavorites =
|
||||
patch.favorites ?? current.favorites ?? migrateModelPreferences?.favorites ?? null;
|
||||
const favorites =
|
||||
setModelFavorites?.reduce((current, change) => {
|
||||
const exists = current.some(
|
||||
(favorite) => favorite.provider === change.provider && favorite.model === change.model,
|
||||
);
|
||||
return change.favorite
|
||||
? exists
|
||||
? current
|
||||
: [...current, { provider: change.provider, model: change.model }]
|
||||
: current.filter(
|
||||
(favorite) => favorite.provider !== change.provider || favorite.model !== change.model,
|
||||
);
|
||||
}, initialFavorites ?? []) ?? initialFavorites;
|
||||
const initialModelPreferences =
|
||||
providerModelPreferences ??
|
||||
current.providerModelPreferences ??
|
||||
migrateModelPreferences?.providerModelPreferences ??
|
||||
null;
|
||||
let nextModelPreferences = initialModelPreferences;
|
||||
for (const change of setModelsHidden ?? []) {
|
||||
const preferences = nextModelPreferences?.[change.provider] ?? {
|
||||
hiddenModels: [],
|
||||
modelOrder: [],
|
||||
};
|
||||
const hiddenModels = preferences.hiddenModels.filter((model) => model !== change.model);
|
||||
if (change.hidden) hiddenModels.push(change.model);
|
||||
nextModelPreferences = {
|
||||
...nextModelPreferences,
|
||||
[change.provider]: { ...preferences, hiddenModels },
|
||||
};
|
||||
}
|
||||
if (setProviderModelOrder) {
|
||||
const preferences = nextModelPreferences?.[setProviderModelOrder.provider] ?? {
|
||||
hiddenModels: [],
|
||||
modelOrder: [],
|
||||
};
|
||||
nextModelPreferences = {
|
||||
...nextModelPreferences,
|
||||
[setProviderModelOrder.provider]: {
|
||||
...preferences,
|
||||
modelOrder: [...setProviderModelOrder.modelOrder],
|
||||
},
|
||||
};
|
||||
}
|
||||
const next = deepMerge(current, patchForMerge);
|
||||
const nextWithReplacementsBase = {
|
||||
...next,
|
||||
favorites,
|
||||
providerModelPreferences: nextModelPreferences,
|
||||
...(worktreeCleanupPatch === undefined
|
||||
? {}
|
||||
: {
|
||||
@@ -459,3 +513,39 @@ export function applyServerSettingsPatch(
|
||||
textGenerationModelSelection: createModelSelection(instanceId, model, options),
|
||||
};
|
||||
}
|
||||
|
||||
/** Resolve legacy local preferences only until the environment has a saved value. */
|
||||
export function resolveModelPreferences(
|
||||
settings: Pick<ServerSettings, "favorites" | "providerModelPreferences">,
|
||||
legacy: Partial<ModelPreferences> = {},
|
||||
): ModelPreferences {
|
||||
return {
|
||||
favorites: settings.favorites ?? legacy.favorites ?? [],
|
||||
providerModelPreferences:
|
||||
settings.providerModelPreferences ?? legacy.providerModelPreferences ?? {},
|
||||
};
|
||||
}
|
||||
|
||||
/** Empty legacy values do not claim ownership before another client can migrate its saved list. */
|
||||
export function modelPreferencesMigrationPatch(
|
||||
settings: Pick<ServerSettings, "favorites" | "providerModelPreferences">,
|
||||
legacy: Partial<ModelPreferences>,
|
||||
): ServerSettingsPatch | null {
|
||||
const favorites =
|
||||
settings.favorites === null && (legacy.favorites?.length ?? 0) > 0
|
||||
? legacy.favorites
|
||||
: undefined;
|
||||
const providerModelPreferences =
|
||||
settings.providerModelPreferences === null &&
|
||||
Object.keys(legacy.providerModelPreferences ?? {}).length > 0
|
||||
? legacy.providerModelPreferences
|
||||
: undefined;
|
||||
return favorites === undefined && providerModelPreferences === undefined
|
||||
? null
|
||||
: {
|
||||
migrateModelPreferences: {
|
||||
...(favorites === undefined ? {} : { favorites }),
|
||||
...(providerModelPreferences === undefined ? {} : { providerModelPreferences }),
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user