|
| 1 | +import { VSCodeCheckbox } from "@vscode/webview-ui-toolkit/react" |
| 2 | +import { useEffect, useState } from "react" |
| 3 | +import { useAppTranslation } from "@/i18n/TranslationContext" |
| 4 | +import { useDebounce } from "react-use" |
| 5 | + |
| 6 | +import { Slider } from "@/components/ui" |
| 7 | + |
| 8 | +interface MaxContextWindowControlProps { |
| 9 | + value: number | undefined | null |
| 10 | + onChange: (value: number | undefined | null) => void |
| 11 | + maxValue?: number // Some providers like OpenAI use 0-2 range. |
| 12 | +} |
| 13 | + |
| 14 | +export const MaxContextWindowControl = ({ value, onChange, maxValue = 1000000 }: MaxContextWindowControlProps) => { |
| 15 | + const { t } = useAppTranslation() |
| 16 | + const [isCustomTemperature, setIsCustomTemperature] = useState(value !== undefined) |
| 17 | + const [inputValue, setInputValue] = useState(value) |
| 18 | + |
| 19 | + useDebounce(() => onChange(inputValue), 50, [onChange, inputValue]) |
| 20 | + |
| 21 | + // Sync internal state with prop changes when switching profiles. |
| 22 | + useEffect(() => { |
| 23 | + const hasCustomTemperature = value !== undefined && value !== null |
| 24 | + setIsCustomTemperature(hasCustomTemperature) |
| 25 | + setInputValue(value) |
| 26 | + }, [value]) |
| 27 | + |
| 28 | + return ( |
| 29 | + <> |
| 30 | + <div> |
| 31 | + <VSCodeCheckbox |
| 32 | + checked={isCustomTemperature} |
| 33 | + onChange={(e: any) => { |
| 34 | + const isChecked = e.target.checked |
| 35 | + setIsCustomTemperature(isChecked) |
| 36 | + |
| 37 | + if (!isChecked) { |
| 38 | + setInputValue(null) // Unset the temperature, note that undefined is unserializable. |
| 39 | + } else { |
| 40 | + setInputValue(value ?? 0) // Use the value from apiConfiguration, if set. |
| 41 | + } |
| 42 | + }}> |
| 43 | + <label className="block font-medium mb-1">{t("settings:maxContextWindow.useCustom")}</label> |
| 44 | + </VSCodeCheckbox> |
| 45 | + <div className="text-sm text-vscode-descriptionForeground mt-1"> |
| 46 | + {t("settings:maxContextWindow.description")} |
| 47 | + </div> |
| 48 | + </div> |
| 49 | + |
| 50 | + {isCustomTemperature && ( |
| 51 | + <div className="flex flex-col gap-3 pl-3 border-l-2 border-vscode-button-background"> |
| 52 | + <div> |
| 53 | + <div className="flex items-center gap-2"> |
| 54 | + <Slider |
| 55 | + min={0} |
| 56 | + max={maxValue} |
| 57 | + step={1} |
| 58 | + value={[inputValue ?? 0]} |
| 59 | + onValueChange={([value]) => setInputValue(value)} |
| 60 | + /> |
| 61 | + <span className="w-10">{inputValue}</span> |
| 62 | + </div> |
| 63 | + <div className="text-vscode-descriptionForeground text-sm mt-1"> |
| 64 | + {t("settings:maxContextWindow.rangeDescription")} |
| 65 | + </div> |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + )} |
| 69 | + </> |
| 70 | + ) |
| 71 | +} |
0 commit comments