Skip to content

Commit 54edab5

Browse files
authored
Fix the save/discard/revert flow for Prompt Settings (#4623)
Add support for the save/discard flow for support prompt setting page Normally when you edit things on the settings pages, the save button lights up, allowing you to discard your changes. Currently the prompts page doesn't support this flow- the prompts are immediately saved when they change. With this change, we use the normal cachedState system in the SettingView, allowing users to dicard changes to their prompts like any other setting. This removed the need for the resetSupportPrompt event since we send the entire state of the support prompts (same as before). Test plan: * Manually verified prompts can be saved/discarded for different types of support prompts.
1 parent 69ffa43 commit 54edab5

File tree

5 files changed

+38
-49
lines changed

5 files changed

+38
-49
lines changed

src/core/webview/webviewMessageHandler.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -829,13 +829,12 @@ export const webviewMessageHandler = async (
829829
break
830830
case "updateSupportPrompt":
831831
try {
832-
if (Object.keys(message?.values ?? {}).length === 0) {
832+
if (!message?.values) {
833833
return
834834
}
835835

836-
const existingPrompts = getGlobalState("customSupportPrompts") ?? {}
837-
const updatedPrompts = { ...existingPrompts, ...message.values }
838-
await updateGlobalState("customSupportPrompts", updatedPrompts)
836+
// Replace all prompts with the new values from the cached state
837+
await updateGlobalState("customSupportPrompts", message.values)
839838
await provider.postStateToWebview()
840839
} catch (error) {
841840
provider.log(
@@ -844,24 +843,6 @@ export const webviewMessageHandler = async (
844843
vscode.window.showErrorMessage(t("common:errors.update_support_prompt"))
845844
}
846845
break
847-
case "resetSupportPrompt":
848-
try {
849-
if (!message?.text) {
850-
return
851-
}
852-
853-
const existingPrompts = getGlobalState("customSupportPrompts") ?? {}
854-
const updatedPrompts = { ...existingPrompts }
855-
updatedPrompts[message.text] = undefined
856-
await updateGlobalState("customSupportPrompts", updatedPrompts)
857-
await provider.postStateToWebview()
858-
} catch (error) {
859-
provider.log(
860-
`Error reset support prompt: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
861-
)
862-
vscode.window.showErrorMessage(t("common:errors.reset_support_prompt"))
863-
}
864-
break
865846
case "updatePrompt":
866847
if (message.promptMode && message.customPrompt !== undefined) {
867848
const existingPrompts = getGlobalState("customModePrompts") ?? {}

src/shared/WebviewMessage.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ export interface WebviewMessage {
118118
| "mode"
119119
| "updatePrompt"
120120
| "updateSupportPrompt"
121-
| "resetSupportPrompt"
122121
| "getSystemPrompt"
123122
| "copySystemPrompt"
124123
| "systemPrompt"

webview-ui/src/components/settings/PromptsSettings.tsx

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ import { SectionHeader } from "./SectionHeader"
1111
import { Section } from "./Section"
1212
import { MessageSquare } from "lucide-react"
1313

14-
const PromptsSettings = () => {
15-
const { t } = useAppTranslation()
14+
interface PromptsSettingsProps {
15+
customSupportPrompts: Record<string, string | undefined>
16+
setCustomSupportPrompts: (prompts: Record<string, string | undefined>) => void
17+
}
1618

17-
const { customSupportPrompts, listApiConfigMeta, enhancementApiConfigId, setEnhancementApiConfigId } =
18-
useExtensionState()
19+
const PromptsSettings = ({ customSupportPrompts, setCustomSupportPrompts }: PromptsSettingsProps) => {
20+
const { t } = useAppTranslation()
21+
const { listApiConfigMeta, enhancementApiConfigId, setEnhancementApiConfigId } = useExtensionState()
1922

2023
const [testPrompt, setTestPrompt] = useState("")
2124
const [isEnhancing, setIsEnhancing] = useState(false)
@@ -37,19 +40,14 @@ const PromptsSettings = () => {
3740
}, [])
3841

3942
const updateSupportPrompt = (type: SupportPromptType, value: string | undefined) => {
40-
vscode.postMessage({
41-
type: "updateSupportPrompt",
42-
values: {
43-
[type]: value,
44-
},
45-
})
43+
const updatedPrompts = { ...customSupportPrompts, [type]: value }
44+
setCustomSupportPrompts(updatedPrompts)
4645
}
4746

4847
const handleSupportReset = (type: SupportPromptType) => {
49-
vscode.postMessage({
50-
type: "resetSupportPrompt",
51-
text: type,
52-
})
48+
const updatedPrompts = { ...customSupportPrompts }
49+
delete updatedPrompts[type]
50+
setCustomSupportPrompts(updatedPrompts)
5351
}
5452

5553
const getSupportPromptValue = (type: SupportPromptType): string => {

webview-ui/src/components/settings/SettingsView.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ const SettingsView = forwardRef<SettingsViewRef, SettingsViewProps>(({ onDone, t
171171
customCondensingPrompt,
172172
codebaseIndexConfig,
173173
codebaseIndexModels,
174+
customSupportPrompts,
174175
} = cachedState
175176

176177
const apiConfiguration = useMemo(() => cachedState.apiConfiguration ?? {}, [cachedState.apiConfiguration])
@@ -242,6 +243,17 @@ const SettingsView = forwardRef<SettingsViewRef, SettingsViewProps>(({ onDone, t
242243
})
243244
}, [])
244245

246+
const setCustomSupportPromptsField = useCallback((prompts: Record<string, string | undefined>) => {
247+
setCachedState((prevState) => {
248+
if (JSON.stringify(prevState.customSupportPrompts) === JSON.stringify(prompts)) {
249+
return prevState
250+
}
251+
252+
setChangeDetected(true)
253+
return { ...prevState, customSupportPrompts: prompts }
254+
})
255+
}, [])
256+
245257
const isSettingValid = !errorMessage
246258

247259
const handleSubmit = () => {
@@ -299,6 +311,7 @@ const SettingsView = forwardRef<SettingsViewRef, SettingsViewProps>(({ onDone, t
299311
vscode.postMessage({ type: "alwaysAllowSubtasks", bool: alwaysAllowSubtasks })
300312
vscode.postMessage({ type: "condensingApiConfigId", text: condensingApiConfigId || "" })
301313
vscode.postMessage({ type: "updateCondensingPrompt", text: customCondensingPrompt || "" })
314+
vscode.postMessage({ type: "updateSupportPrompt", values: customSupportPrompts || {} })
302315
vscode.postMessage({ type: "upsertApiConfiguration", text: currentApiConfigName, apiConfiguration })
303316
vscode.postMessage({ type: "telemetrySetting", text: telemetrySetting })
304317
vscode.postMessage({ type: "codebaseIndexConfig", values: codebaseIndexConfig })
@@ -653,7 +666,12 @@ const SettingsView = forwardRef<SettingsViewRef, SettingsViewProps>(({ onDone, t
653666
)}
654667

655668
{/* Prompts Section */}
656-
{activeTab === "prompts" && <PromptsSettings />}
669+
{activeTab === "prompts" && (
670+
<PromptsSettings
671+
customSupportPrompts={customSupportPrompts || {}}
672+
setCustomSupportPrompts={setCustomSupportPromptsField}
673+
/>
674+
)}
657675

658676
{/* Experimental Section */}
659677
{activeTab === "experimental" && (

webview-ui/src/context/ExtensionStateContext.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,29 +123,22 @@ export interface ExtensionStateContextType extends ExtensionState {
123123
export const ExtensionStateContext = createContext<ExtensionStateContextType | undefined>(undefined)
124124

125125
export const mergeExtensionState = (prevState: ExtensionState, newState: ExtensionState) => {
126-
const {
127-
customModePrompts: prevCustomModePrompts,
128-
customSupportPrompts: prevCustomSupportPrompts,
129-
experiments: prevExperiments,
130-
...prevRest
131-
} = prevState
126+
const { customModePrompts: prevCustomModePrompts, experiments: prevExperiments, ...prevRest } = prevState
132127

133128
const {
134129
apiConfiguration,
135130
customModePrompts: newCustomModePrompts,
136-
customSupportPrompts: newCustomSupportPrompts,
131+
customSupportPrompts,
137132
experiments: newExperiments,
138133
...newRest
139134
} = newState
140135

141136
const customModePrompts = { ...prevCustomModePrompts, ...newCustomModePrompts }
142-
const customSupportPrompts = { ...prevCustomSupportPrompts, ...newCustomSupportPrompts }
143137
const experiments = { ...prevExperiments, ...newExperiments }
144138
const rest = { ...prevRest, ...newRest }
145139

146-
// Note that we completely replace the previous apiConfiguration object with
147-
// a new one since the state that is broadcast is the entire apiConfiguration
148-
// and therefore merging is not necessary.
140+
// Note that we completely replace the previous apiConfiguration and customSupportPrompts objects
141+
// with new ones since the state that is broadcast is the entire objects so merging is not necessary.
149142
return { ...rest, apiConfiguration, customModePrompts, customSupportPrompts, experiments }
150143
}
151144

0 commit comments

Comments
 (0)