import * as React from 'react'; import { shallow } from 'zustand/shallow'; import { Box, Button, Grid, IconButton, Input, Modal, ModalClose, ModalDialog, Slider, Stack, Typography } from '@mui/joy'; import KeyboardArrowDownIcon from '@mui/icons-material/KeyboardArrowDown'; import KeyboardArrowUpIcon from '@mui/icons-material/KeyboardArrowUp'; import { Link } from './util/Link'; import { useSettingsStore } from '@/lib/store'; export const isValidOpenAIApiKey = (apiKey?: string) => !!apiKey && apiKey.startsWith('sk-') && apiKey.length > 40; /** * Component that allows the User to modify the application settings, * persisted on the client via localStorage. * * @param {boolean} open Whether the Settings modal is open * @param {() => void} onClose Call this to close the dialog from outside */ export function SettingsModal({ open, onClose }: { open: boolean, onClose: () => void; }) { // state const [showAdvanced, setShowAdvanced] = React.useState(false); // global state const { apiKey, setApiKey, modelTemperature, setModelTemperature, modelMaxTokens, setModelMaxTokens } = useSettingsStore(state => ({ apiKey: state.apiKey, setApiKey: state.setApiKey, modelTemperature: state.modelTemperature, setModelTemperature: state.setModelTemperature, modelMaxTokens: state.modelMaxTokens, setModelMaxTokens: state.setModelMaxTokens, }), shallow); const handleApiKeyChange = (e: React.ChangeEvent) => setApiKey((e.target as HTMLInputElement).value); const handleApiKeyDown = (e: React.KeyboardEvent) => (e.key === 'Enter') && onClose(); const handleTemperatureChange = (event: Event, newValue: number | number[]) => setModelTemperature(newValue as number); const handleMaxTokensChange = (event: Event, newValue: number | number[]) => setModelMaxTokens(newValue as number); const needsApiKey = !!process.env.REQUIRE_USER_API_KEYS; const isValidKey = isValidOpenAIApiKey(apiKey); return ( Settings Personal OpenAI API Key {needsApiKey ? '(required' : '(optional'}, check usage) {needsApiKey ? <>sk-... Create key, then apply to the GPT-4 waitlist : <>if provided, your Key will override the server's} Advanced AI settings setShowAdvanced(!showAdvanced)}> {showAdvanced ? : } {showAdvanced && <> Temperature Max. tokens Adjust only if you're familiar with these settings } ); }