File size: 8,950 Bytes
246d201 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
import { useLocation } from "react-router";
import { useTranslation } from "react-i18next";
import React from "react";
import posthog from "posthog-js";
import { I18nKey } from "#/i18n/declaration";
import { organizeModelsAndProviders } from "#/utils/organize-models-and-providers";
import { getDefaultSettings, Settings } from "#/services/settings";
import { extractModelAndProvider } from "#/utils/extract-model-and-provider";
import { DangerModal } from "../confirmation-modals/danger-modal";
import { extractSettings, saveSettingsView } from "#/utils/settings-utils";
import { useEndSession } from "#/hooks/use-end-session";
import { ModalButton } from "../../buttons/modal-button";
import { AdvancedOptionSwitch } from "../../inputs/advanced-option-switch";
import { AgentInput } from "../../inputs/agent-input";
import { APIKeyInput } from "../../inputs/api-key-input";
import { BaseUrlInput } from "../../inputs/base-url-input";
import { ConfirmationModeSwitch } from "../../inputs/confirmation-mode-switch";
import { CustomModelInput } from "../../inputs/custom-model-input";
import { SecurityAnalyzerInput } from "../../inputs/security-analyzers-input";
import { ModalBackdrop } from "../modal-backdrop";
import { ModelSelector } from "./model-selector";
import { RuntimeSizeSelector } from "./runtime-size-selector";
import { useConfig } from "#/hooks/query/use-config";
import { useCurrentSettings } from "#/context/settings-context";
interface SettingsFormProps {
disabled?: boolean;
settings: Settings;
models: string[];
agents: string[];
securityAnalyzers: string[];
onClose: () => void;
}
export function SettingsForm({
disabled,
settings,
models,
agents,
securityAnalyzers,
onClose,
}: SettingsFormProps) {
const { saveUserSettings } = useCurrentSettings();
const endSession = useEndSession();
const { data: config } = useConfig();
const location = useLocation();
const { t } = useTranslation();
const formRef = React.useRef<HTMLFormElement>(null);
const advancedAlreadyInUse = React.useMemo(() => {
if (models.length > 0) {
const organizedModels = organizeModelsAndProviders(models);
const { provider, model } = extractModelAndProvider(
settings.LLM_MODEL || "",
);
const isKnownModel =
provider in organizedModels &&
organizedModels[provider].models.includes(model);
const isUsingSecurityAnalyzer = !!settings.SECURITY_ANALYZER;
const isUsingConfirmationMode = !!settings.CONFIRMATION_MODE;
const isUsingBaseUrl = !!settings.LLM_BASE_URL;
const isUsingCustomModel = !!settings.LLM_MODEL && !isKnownModel;
return (
isUsingSecurityAnalyzer ||
isUsingConfirmationMode ||
isUsingBaseUrl ||
isUsingCustomModel
);
}
return false;
}, [settings, models]);
const [showAdvancedOptions, setShowAdvancedOptions] =
React.useState(advancedAlreadyInUse);
const [confirmResetDefaultsModalOpen, setConfirmResetDefaultsModalOpen] =
React.useState(false);
const [confirmEndSessionModalOpen, setConfirmEndSessionModalOpen] =
React.useState(false);
const resetOngoingSession = () => {
if (location.pathname.startsWith("/conversations/")) {
endSession();
}
};
const handleFormSubmission = async (formData: FormData) => {
const keys = Array.from(formData.keys());
const isUsingAdvancedOptions = keys.includes("use-advanced-options");
const newSettings = extractSettings(formData);
saveSettingsView(isUsingAdvancedOptions ? "advanced" : "basic");
await saveUserSettings(newSettings);
onClose();
resetOngoingSession();
posthog.capture("settings_saved", {
LLM_MODEL: newSettings.LLM_MODEL,
LLM_API_KEY: newSettings.LLM_API_KEY ? "SET" : "UNSET",
REMOTE_RUNTIME_RESOURCE_FACTOR:
newSettings.REMOTE_RUNTIME_RESOURCE_FACTOR,
});
};
const handleConfirmResetSettings = async () => {
await saveUserSettings(getDefaultSettings());
onClose();
resetOngoingSession();
posthog.capture("settings_reset");
};
const handleConfirmEndSession = () => {
const formData = new FormData(formRef.current ?? undefined);
handleFormSubmission(formData);
};
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
const formData = new FormData(event.currentTarget);
if (location.pathname.startsWith("/conversations/")) {
setConfirmEndSessionModalOpen(true);
} else {
handleFormSubmission(formData);
}
};
const isSaasMode = config?.APP_MODE === "saas";
return (
<div>
<form
ref={formRef}
data-testid="settings-form"
className="flex flex-col gap-6"
onSubmit={handleSubmit}
>
<div className="flex flex-col gap-2">
<AdvancedOptionSwitch
isDisabled={!!disabled}
showAdvancedOptions={showAdvancedOptions}
setShowAdvancedOptions={setShowAdvancedOptions}
/>
{showAdvancedOptions && (
<>
<CustomModelInput
isDisabled={!!disabled}
defaultValue={settings.LLM_MODEL}
/>
<BaseUrlInput
isDisabled={!!disabled}
defaultValue={settings.LLM_BASE_URL}
/>
</>
)}
{!showAdvancedOptions && (
<ModelSelector
isDisabled={disabled}
models={organizeModelsAndProviders(models)}
currentModel={settings.LLM_MODEL}
/>
)}
<APIKeyInput
isDisabled={!!disabled}
isSet={settings.LLM_API_KEY === "SET"}
/>
{showAdvancedOptions && (
<>
<AgentInput
isDisabled={!!disabled}
defaultValue={settings.AGENT}
agents={agents}
/>
{isSaasMode && (
<RuntimeSizeSelector
isDisabled={!!disabled}
defaultValue={settings.REMOTE_RUNTIME_RESOURCE_FACTOR}
/>
)}
<SecurityAnalyzerInput
isDisabled={!!disabled}
defaultValue={settings.SECURITY_ANALYZER}
securityAnalyzers={securityAnalyzers}
/>
<ConfirmationModeSwitch
isDisabled={!!disabled}
defaultSelected={settings.CONFIRMATION_MODE}
/>
</>
)}
</div>
<div className="flex flex-col gap-2">
<div className="flex gap-2">
<ModalButton
testId="save-settings-button"
disabled={disabled}
type="submit"
text={t(I18nKey.BUTTON$SAVE)}
className="bg-[#4465DB] w-full"
/>
<ModalButton
text={t(I18nKey.BUTTON$CLOSE)}
className="bg-[#737373] w-full"
onClick={onClose}
/>
</div>
<ModalButton
disabled={disabled}
text={t(I18nKey.BUTTON$RESET_TO_DEFAULTS)}
variant="text-like"
className="text-danger self-start"
onClick={() => {
setConfirmResetDefaultsModalOpen(true);
}}
/>
</div>
</form>
{confirmResetDefaultsModalOpen && (
<ModalBackdrop>
<DangerModal
testId="reset-defaults-modal"
title={t(I18nKey.MODAL$CONFIRM_RESET_TITLE)}
description={t(I18nKey.MODAL$CONFIRM_RESET_MESSAGE)}
buttons={{
danger: {
text: t(I18nKey.BUTTON$RESET_TO_DEFAULTS),
onClick: handleConfirmResetSettings,
},
cancel: {
text: t(I18nKey.BUTTON$CANCEL),
onClick: () => setConfirmResetDefaultsModalOpen(false),
},
}}
/>
</ModalBackdrop>
)}
{confirmEndSessionModalOpen && (
<ModalBackdrop>
<DangerModal
title={t(I18nKey.MODAL$END_SESSION_TITLE)}
description={t(I18nKey.MODAL$END_SESSION_MESSAGE)}
buttons={{
danger: {
text: t(I18nKey.BUTTON$END_SESSION),
onClick: handleConfirmEndSession,
},
cancel: {
text: t(I18nKey.BUTTON$CANCEL),
onClick: () => setConfirmEndSessionModalOpen(false),
},
}}
/>
</ModalBackdrop>
)}
</div>
);
}
|