Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 3 additions & 22 deletions src/core/webview/webviewMessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,13 +825,12 @@ export const webviewMessageHandler = async (
break
case "updateSupportPrompt":
try {
if (Object.keys(message?.values ?? {}).length === 0) {
if (!message?.values) {
return
}

const existingPrompts = getGlobalState("customSupportPrompts") ?? {}
const updatedPrompts = { ...existingPrompts, ...message.values }
await updateGlobalState("customSupportPrompts", updatedPrompts)
// Replace all prompts with the new values from the cached state
await updateGlobalState("customSupportPrompts", message.values)
await provider.postStateToWebview()
} catch (error) {
provider.log(
Expand All @@ -840,24 +839,6 @@ export const webviewMessageHandler = async (
vscode.window.showErrorMessage(t("common:errors.update_support_prompt"))
}
break
case "resetSupportPrompt":
try {
if (!message?.text) {
return
}

const existingPrompts = getGlobalState("customSupportPrompts") ?? {}
const updatedPrompts = { ...existingPrompts }
updatedPrompts[message.text] = undefined
await updateGlobalState("customSupportPrompts", updatedPrompts)
await provider.postStateToWebview()
} catch (error) {
provider.log(
`Error reset support prompt: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
)
vscode.window.showErrorMessage(t("common:errors.reset_support_prompt"))
}
break
case "updatePrompt":
if (message.promptMode && message.customPrompt !== undefined) {
const existingPrompts = getGlobalState("customModePrompts") ?? {}
Expand Down
1 change: 0 additions & 1 deletion src/shared/WebviewMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ export interface WebviewMessage {
| "mode"
| "updatePrompt"
| "updateSupportPrompt"
| "resetSupportPrompt"
| "getSystemPrompt"
| "copySystemPrompt"
| "systemPrompt"
Expand Down
26 changes: 12 additions & 14 deletions webview-ui/src/components/settings/PromptsSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import { SectionHeader } from "./SectionHeader"
import { Section } from "./Section"
import { MessageSquare } from "lucide-react"

const PromptsSettings = () => {
const { t } = useAppTranslation()
interface PromptsSettingsProps {
customSupportPrompts: Record<string, string | undefined>
setCustomSupportPrompts: (prompts: Record<string, string | undefined>) => void
}

const { customSupportPrompts, listApiConfigMeta, enhancementApiConfigId, setEnhancementApiConfigId } =
useExtensionState()
const PromptsSettings = ({ customSupportPrompts, setCustomSupportPrompts }: PromptsSettingsProps) => {
const { t } = useAppTranslation()
const { listApiConfigMeta, enhancementApiConfigId, setEnhancementApiConfigId } = useExtensionState()

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

const updateSupportPrompt = (type: SupportPromptType, value: string | undefined) => {
vscode.postMessage({
type: "updateSupportPrompt",
values: {
[type]: value,
},
})
const updatedPrompts = { ...customSupportPrompts, [type]: value }
setCustomSupportPrompts(updatedPrompts)
}

const handleSupportReset = (type: SupportPromptType) => {
vscode.postMessage({
type: "resetSupportPrompt",
text: type,
})
const updatedPrompts = { ...customSupportPrompts }
delete updatedPrompts[type]
setCustomSupportPrompts(updatedPrompts)
}

const getSupportPromptValue = (type: SupportPromptType): string => {
Expand Down
20 changes: 19 additions & 1 deletion webview-ui/src/components/settings/SettingsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ const SettingsView = forwardRef<SettingsViewRef, SettingsViewProps>(({ onDone, t
customCondensingPrompt,
codebaseIndexConfig,
codebaseIndexModels,
customSupportPrompts,
} = cachedState

const apiConfiguration = useMemo(() => cachedState.apiConfiguration ?? {}, [cachedState.apiConfiguration])
Expand Down Expand Up @@ -241,6 +242,17 @@ const SettingsView = forwardRef<SettingsViewRef, SettingsViewProps>(({ onDone, t
})
}, [])

const setCustomSupportPromptsField = useCallback((prompts: Record<string, string | undefined>) => {
setCachedState((prevState) => {
if (JSON.stringify(prevState.customSupportPrompts) === JSON.stringify(prompts)) {
return prevState
}

setChangeDetected(true)
return { ...prevState, customSupportPrompts: prompts }
})
}, [])

const isSettingValid = !errorMessage

const handleSubmit = () => {
Expand Down Expand Up @@ -297,6 +309,7 @@ const SettingsView = forwardRef<SettingsViewRef, SettingsViewProps>(({ onDone, t
vscode.postMessage({ type: "alwaysAllowSubtasks", bool: alwaysAllowSubtasks })
vscode.postMessage({ type: "condensingApiConfigId", text: condensingApiConfigId || "" })
vscode.postMessage({ type: "updateCondensingPrompt", text: customCondensingPrompt || "" })
vscode.postMessage({ type: "updateSupportPrompt", values: customSupportPrompts || {} })
vscode.postMessage({ type: "upsertApiConfiguration", text: currentApiConfigName, apiConfiguration })
vscode.postMessage({ type: "telemetrySetting", text: telemetrySetting })
vscode.postMessage({ type: "codebaseIndexConfig", values: codebaseIndexConfig })
Expand Down Expand Up @@ -650,7 +663,12 @@ const SettingsView = forwardRef<SettingsViewRef, SettingsViewProps>(({ onDone, t
)}

{/* Prompts Section */}
{activeTab === "prompts" && <PromptsSettings />}
{activeTab === "prompts" && (
<PromptsSettings
customSupportPrompts={customSupportPrompts || {}}
setCustomSupportPrompts={setCustomSupportPromptsField}
/>
)}

{/* Experimental Section */}
{activeTab === "experimental" && (
Expand Down
15 changes: 4 additions & 11 deletions webview-ui/src/context/ExtensionStateContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,29 +123,22 @@ export interface ExtensionStateContextType extends ExtensionState {
export const ExtensionStateContext = createContext<ExtensionStateContextType | undefined>(undefined)

export const mergeExtensionState = (prevState: ExtensionState, newState: ExtensionState) => {
const {
customModePrompts: prevCustomModePrompts,
customSupportPrompts: prevCustomSupportPrompts,
experiments: prevExperiments,
...prevRest
} = prevState
const { customModePrompts: prevCustomModePrompts, experiments: prevExperiments, ...prevRest } = prevState

const {
apiConfiguration,
customModePrompts: newCustomModePrompts,
customSupportPrompts: newCustomSupportPrompts,
customSupportPrompts,
experiments: newExperiments,
...newRest
} = newState

const customModePrompts = { ...prevCustomModePrompts, ...newCustomModePrompts }
const customSupportPrompts = { ...prevCustomSupportPrompts, ...newCustomSupportPrompts }
const experiments = { ...prevExperiments, ...newExperiments }
const rest = { ...prevRest, ...newRest }

// Note that we completely replace the previous apiConfiguration object with
// a new one since the state that is broadcast is the entire apiConfiguration
// and therefore merging is not necessary.
// Note that we completely replace the previous apiConfiguration and customSupportPrompts objects
// with new ones since the state that is broadcast is the entire objects so merging is not necessary.
return { ...rest, apiConfiguration, customModePrompts, customSupportPrompts, experiments }
}

Expand Down
Loading