Open History to see the last commit
1import { useEffect } from "react";
2
3import {
4 useClientSettingsHydrated,
5 usePrimarySettings,
6 useUpdatePrimarySettings,
7} from "./hooks/useSettings";
8import { resolvePlanAgentHealPatch } from "./modelSelection";
9
10/**
11 * Heals persisted text-generation model selections that still reference the
12 * opencode "plan" agent. The dropdown hides the option while legacy plan mode
13 * is off, but the toggle handler only runs when the setting flips; users who
14 * already have plan mode off with a stored "plan" selection need this pass
15 * whenever the settings load.
16 */
17export function PlanAgentSelectionHeal() {
18 const planModeEnabled = usePrimarySettings((settings) => settings.planModeEnabled);
19 const textGenerationModelSelection = usePrimarySettings(
20 (settings) => settings.textGenerationModelSelection,
21 );
22 const sourceControlWriterModelSelection = usePrimarySettings(
23 (settings) => settings.sourceControlWriterModelSelection,
24 );
25 const settingsHydrated = useClientSettingsHydrated();
26 const updateSettings = useUpdatePrimarySettings();
27
28 useEffect(() => {
29 // planModeEnabled reads as false until client settings hydrate, so never
30 // heal before then: we would strip a stored plan selection from a user
31 // whose plan mode is actually on.
32 if (!settingsHydrated) {
33 return;
34 }
35 const patch = resolvePlanAgentHealPatch({
36 planModeEnabled,
37 textGenerationModelSelection,
38 sourceControlWriterModelSelection,
39 });
40 if (patch) {
41 updateSettings(patch);
42 }
43 }, [
44 planModeEnabled,
45 settingsHydrated,
46 textGenerationModelSelection,
47 sourceControlWriterModelSelection,
48 updateSettings,
49 ]);
50
51 return null;
52}