Compare commits

...
Author SHA1 Message Date
Cursor AgentandJulius Marminge ae4b630800 Stabilize chat project grouping settings
Co-authored-by: Julius Marminge <[email protected]>
2026-06-02 17:12:15 +00:00
Cursor AgentandJulius Marminge 027d366e98 Optimize chat settings subscriptions
Co-authored-by: Julius Marminge <[email protected]>
2026-06-02 17:01:49 +00:00
Cursor AgentandJulius Marminge 60cf698a43 fix: restore missing settings variable in ChatView
The settings variable was accidentally removed during settings optimization,
causing a ReferenceError at runtime in the onProviderModelSelect callback.
This restores the variable to fix the react-scan-harness page.

Co-authored-by: Julius Marminge <[email protected]>
2026-06-02 16:36:40 +00:00
3 changed files with 144 additions and 33 deletions

No files matched your search

+29 -11
View File
@@ -96,6 +96,7 @@ import {
type TurnDiffSummary,
} from "../types";
import { useTheme } from "../hooks/useTheme";
import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings";
import { useTurnDiffSummaries } from "../hooks/useTurnDiffSummaries";
import { useCommandPaletteStore } from "../commandPaletteStore";
import { buildTemporaryWorktreeBranchName } from "@t3tools/shared/git";
@@ -120,10 +121,7 @@ import { getProviderModelCapabilities, resolveSelectableProvider } from "../prov
import { useSettings } from "../hooks/useSettings";
import { resolveAppModelSelectionForInstance } from "../modelSelection";
import { isTerminalFocused } from "../lib/terminalFocus";
import {
deriveLogicalProjectKeyFromSettings,
selectProjectGroupingSettings,
} from "../logicalProject";
import { deriveLogicalProjectKeyFromSettings } from "../logicalProject";
import {
reconnectSavedEnvironment,
useSavedEnvironmentRegistryStore,
@@ -792,12 +790,23 @@ export default function ChatView(props: ChatViewProps) {
const activeThreadLastVisitedAt = useUiStateStore((store) =>
routeKind === "server" ? store.threadLastVisitedAtById[routeThreadKey] : undefined,
);
const settings = useSettings();
const timestampFormat = useSettings((settings) => settings.timestampFormat);
const autoOpenPlanSidebar = useSettings((settings) => settings.autoOpenPlanSidebar);
const providerSettings = useSettings((settings) => settings.providers);
const providerInstancesSettings = useSettings((settings) => settings.providerInstances);
const providerModelPreferences = useSettings((settings) => settings.providerModelPreferences);
const modelSettings = useMemo(
() => ({
...DEFAULT_UNIFIED_SETTINGS,
providers: providerSettings,
providerInstances: providerInstancesSettings,
providerModelPreferences,
}),
[providerInstancesSettings, providerModelPreferences, providerSettings],
);
const setStickyComposerModelSelection = useComposerDraftStore(
(store) => store.setStickyModelSelection,
);
const timestampFormat = settings.timestampFormat;
const autoOpenPlanSidebar = settings.autoOpenPlanSidebar;
const navigate = useNavigate();
const rawSearch = useSearch({
strict: false,
@@ -1149,7 +1158,17 @@ export default function ChatView(props: ChatViewProps) {
},
[],
);
const projectGroupingSettings = useSettings(selectProjectGroupingSettings);
const sidebarProjectGroupingMode = useSettings((settings) => settings.sidebarProjectGroupingMode);
const sidebarProjectGroupingOverrides = useSettings(
(settings) => settings.sidebarProjectGroupingOverrides,
);
const projectGroupingSettings = useMemo(
() => ({
sidebarProjectGroupingMode,
sidebarProjectGroupingOverrides,
}),
[sidebarProjectGroupingMode, sidebarProjectGroupingOverrides],
);
const logicalProjectEnvironments = useMemo(() => {
if (!activeProject) return [];
const logicalKey = deriveLogicalProjectKeyFromSettings(activeProject, projectGroupingSettings);
@@ -3621,7 +3640,7 @@ export default function ChatView(props: ChatViewProps) {
}
const resolvedModel = resolveAppModelSelectionForInstance(
instanceId,
settings,
modelSettings,
providerStatuses,
model,
);
@@ -3647,7 +3666,7 @@ export default function ChatView(props: ChatViewProps) {
setComposerDraftModelSelection,
setStickyComposerModelSelection,
providerStatuses,
settings,
modelSettings,
],
);
const onEnvModeChange = useCallback(
@@ -3870,7 +3889,6 @@ export default function ChatView(props: ChatViewProps) {
activeThreadModelSelection={activeThread?.modelSelection}
activeThreadActivities={activeThread?.activities}
resolvedTheme={resolvedTheme}
settings={settings}
keybindings={keybindings}
terminalOpen={Boolean(terminalUiState.terminalOpen)}
gitCwd={gitCwd}
+17 -6
View File
@@ -100,7 +100,7 @@ import {
type ProviderInstanceEntry,
} from "../../providerInstances";
import { type AppModelOption, getAppModelOptionsForInstance } from "../../modelSelection";
import type { UnifiedSettings } from "@t3tools/contracts/settings";
import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings";
import type { SessionPhase, Thread } from "../../types";
import type { PendingUserInputDraftAnswer } from "../../pendingUserInput";
import type { PendingApproval, PendingUserInput } from "../../session-logic";
@@ -108,6 +108,7 @@ import { deriveLatestContextWindowSnapshot } from "../../lib/contextWindow";
import { formatProviderSkillDisplayName } from "../../providerSkillPresentation";
import { searchProviderSkills } from "../../providerSkillSearch";
import { useMediaQuery } from "../../hooks/useMediaQuery";
import { useSettings } from "../../hooks/useSettings";
const IMAGE_SIZE_LIMIT_LABEL = `${Math.round(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES / (1024 * 1024))}MB`;
@@ -434,7 +435,6 @@ export interface ChatComposerProps {
// Misc
resolvedTheme: "light" | "dark";
settings: UnifiedSettings;
keybindings: ResolvedKeybindingsConfig;
terminalOpen: boolean;
gitCwd: string | null;
@@ -524,7 +524,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
activeThreadModelSelection,
activeThreadActivities,
resolvedTheme,
settings,
keybindings,
terminalOpen,
gitCwd,
@@ -561,6 +560,18 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
const composerImages = composerDraft.images;
const composerTerminalContexts = composerDraft.terminalContexts;
const nonPersistedComposerImageIds = composerDraft.nonPersistedImageIds;
const providerSettings = useSettings((settings) => settings.providers);
const providerInstancesSettings = useSettings((settings) => settings.providerInstances);
const providerModelPreferences = useSettings((settings) => settings.providerModelPreferences);
const modelSettings = useMemo(
() => ({
...DEFAULT_UNIFIED_SETTINGS,
providers: providerSettings,
providerInstances: providerInstancesSettings,
providerModelPreferences,
}),
[providerInstancesSettings, providerModelPreferences, providerSettings],
);
const setComposerDraftPrompt = useComposerDraftStore((store) => store.setPrompt);
const addComposerDraftImage = useComposerDraftStore((store) => store.addImage);
@@ -696,7 +707,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
selectedInstanceId,
threadModelSelection: activeThreadModelSelection,
projectModelSelection: activeProjectDefaultModelSelection,
settings,
settings: modelSettings,
});
// Resolve the active instance's snapshot by `instanceId` so a custom
@@ -759,10 +770,10 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
>(() => {
const out = new Map<ProviderInstanceId, ReadonlyArray<AppModelOption>>();
for (const entry of providerInstanceEntries) {
out.set(entry.instanceId, getAppModelOptionsForInstance(settings, entry));
out.set(entry.instanceId, getAppModelOptionsForInstance(modelSettings, entry));
}
return out;
}, [providerInstanceEntries, settings]);
}, [modelSettings, providerInstanceEntries]);
const selectedModelForPickerWithCustomFallback = useMemo(() => {
const currentOptions = modelOptionsByInstance.get(selectedInstanceId) ?? [];
return currentOptions.some((option) => option.slug === selectedModelForPicker)
+98 -16
View File
@@ -9,8 +9,12 @@
* write. The hook transparently routes reads/writes to the correct backing
* store.
*/
import { useCallback, useMemo, useSyncExternalStore } from "react";
import { ServerSettings, type ServerSettingsPatch } from "@t3tools/contracts";
import { useCallback, useRef, useSyncExternalStore } from "react";
import {
DEFAULT_SERVER_SETTINGS,
ServerSettings,
type ServerSettingsPatch,
} from "@t3tools/contracts";
import {
type ClientSettingsPatch,
type ClientSettings,
@@ -21,7 +25,7 @@ import {
import { ensureLocalApi } from "~/localApi";
import * as Struct from "effect/Struct";
import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings";
import { applySettingsUpdated, getServerConfig, useServerSettings } from "~/rpc/serverState";
import { applySettingsUpdated, getServerConfig, onServerConfigUpdated } from "~/rpc/serverState";
const CLIENT_SETTINGS_PERSISTENCE_ERROR_SCOPE = "[CLIENT_SETTINGS]";
@@ -30,6 +34,9 @@ const clientSettingsHydrationListeners = new Set<() => void>();
let clientSettingsSnapshot = DEFAULT_CLIENT_SETTINGS;
let clientSettingsHydrated = false;
let clientSettingsHydrationPromise: Promise<void> | null = null;
let unifiedServerSettingsSnapshot = DEFAULT_SERVER_SETTINGS;
let unifiedClientSettingsSnapshot = DEFAULT_CLIENT_SETTINGS;
let unifiedSettingsSnapshot = DEFAULT_UNIFIED_SETTINGS;
function emitClientSettingsChange() {
for (const listener of clientSettingsListeners) {
@@ -47,6 +54,28 @@ function getClientSettingsSnapshot(): ClientSettings {
return clientSettingsSnapshot;
}
function getServerSettingsSnapshot(): ServerSettings {
return getServerConfig()?.settings ?? DEFAULT_SERVER_SETTINGS;
}
function getUnifiedSettingsSnapshot(): UnifiedSettings {
const serverSettings = getServerSettingsSnapshot();
const clientSettings = getClientSettingsSnapshot();
if (
serverSettings === unifiedServerSettingsSnapshot &&
clientSettings === unifiedClientSettingsSnapshot
) {
return unifiedSettingsSnapshot;
}
unifiedServerSettingsSnapshot = serverSettings;
unifiedClientSettingsSnapshot = clientSettings;
unifiedSettingsSnapshot = {
...serverSettings,
...clientSettings,
};
return unifiedSettingsSnapshot;
}
function replaceClientSettingsSnapshot(settings: ClientSettings): void {
clientSettingsSnapshot = settings;
emitClientSettingsChange();
@@ -68,6 +97,17 @@ function subscribeClientSettings(listener: () => void): () => void {
};
}
function subscribeUnifiedSettings(listener: () => void): () => void {
const unsubscribeClient = subscribeClientSettings(listener);
const unsubscribeServer = onServerConfigUpdated(() => {
listener();
});
return () => {
unsubscribeClient();
unsubscribeServer();
};
}
function getClientSettingsHydratedSnapshot(): boolean {
return clientSettingsHydrated;
}
@@ -168,22 +208,61 @@ export function useClientSettingsHydrated(): boolean {
}
export function useSettings<T = UnifiedSettings>(selector?: (s: UnifiedSettings) => T): T {
const serverSettings = useServerSettings();
const clientSettings = useSyncExternalStore(
subscribeClientSettings,
getClientSettingsSnapshot,
() => DEFAULT_CLIENT_SETTINGS,
const selectorRef = useRef(selector);
selectorRef.current = selector;
const lastSourceRef = useRef<UnifiedSettings | null>(null);
const lastSelectorRef = useRef<typeof selector | undefined>(undefined);
const lastSelectedRef = useRef<T | null>(null);
const hasSelectedRef = useRef(false);
const selectSnapshot = useCallback((settings: UnifiedSettings): T => {
const currentSelector = selectorRef.current;
if (
hasSelectedRef.current &&
lastSourceRef.current === settings &&
lastSelectorRef.current === currentSelector
) {
return lastSelectedRef.current as T;
}
const selected = currentSelector ? currentSelector(settings) : (settings as T);
lastSourceRef.current = settings;
lastSelectorRef.current = currentSelector;
if (hasSelectedRef.current && Object.is(lastSelectedRef.current, selected)) {
return lastSelectedRef.current as T;
}
hasSelectedRef.current = true;
lastSelectedRef.current = selected;
return selected;
}, []);
const getSelectedSnapshot = useCallback((): T => {
return selectSnapshot(getUnifiedSettingsSnapshot());
}, [selectSnapshot]);
const getServerSelectedSnapshot = useCallback((): T => {
return selectSnapshot(DEFAULT_UNIFIED_SETTINGS);
}, [selectSnapshot]);
const subscribeSelectedSettings = useCallback(
(listener: () => void): (() => void) =>
subscribeUnifiedSettings(() => {
const hadSelected = hasSelectedRef.current;
const previousSelected = lastSelectedRef.current;
const nextSelected = selectSnapshot(getUnifiedSettingsSnapshot());
if (!hadSelected || !Object.is(previousSelected, nextSelected)) {
listener();
}
}),
[selectSnapshot],
);
const merged = useMemo<UnifiedSettings>(
() => ({
...serverSettings,
...clientSettings,
}),
[clientSettings, serverSettings],
return useSyncExternalStore(
subscribeSelectedSettings,
getSelectedSnapshot,
getServerSelectedSnapshot,
);
return useMemo(() => (selector ? selector(merged) : (merged as T)), [merged, selector]);
}
/**
@@ -227,6 +306,9 @@ export function __resetClientSettingsPersistenceForTests(): void {
clientSettingsSnapshot = DEFAULT_CLIENT_SETTINGS;
clientSettingsHydrated = false;
clientSettingsHydrationPromise = null;
unifiedServerSettingsSnapshot = DEFAULT_SERVER_SETTINGS;
unifiedClientSettingsSnapshot = DEFAULT_CLIENT_SETTINGS;
unifiedSettingsSnapshot = DEFAULT_UNIFIED_SETTINGS;
clientSettingsListeners.clear();
clientSettingsHydrationListeners.clear();
}