|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import React from 'react'; |
| 4 | +import { Endpoint, ModelEndpointStatus } from '@/types'; |
| 5 | +import { |
| 6 | + Button, |
| 7 | + Form, |
| 8 | + FormGroup, |
| 9 | + FormHelperText, |
| 10 | + HelperText, |
| 11 | + HelperTextItem, |
| 12 | + Modal, |
| 13 | + ModalBody, |
| 14 | + ModalFooter, |
| 15 | + ModalHeader, |
| 16 | + ModalVariant, |
| 17 | + Popover, |
| 18 | + TextInput, |
| 19 | + ValidatedOptions |
| 20 | +} from '@patternfly/react-core'; |
| 21 | +import { ExclamationCircleIcon, OutlinedQuestionCircleIcon } from '@patternfly/react-icons'; |
| 22 | +import { fetchEndpointStatus } from '@/components/Chat/modelService'; |
| 23 | + |
| 24 | +const removeTrailingSlash = (inputUrl: string): string => { |
| 25 | + if (inputUrl.slice(-1) === '/') { |
| 26 | + return inputUrl.slice(0, -1); |
| 27 | + } |
| 28 | + return inputUrl; |
| 29 | +}; |
| 30 | + |
| 31 | +const validateUrl = (link: string): boolean => { |
| 32 | + try { |
| 33 | + new URL(link); |
| 34 | + return true; |
| 35 | + } catch (e) { |
| 36 | + return false; |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +interface Props { |
| 41 | + endpoint: Endpoint; |
| 42 | + onClose: (endpoint?: Endpoint) => void; |
| 43 | +} |
| 44 | + |
| 45 | +const EditEndpointModal: React.FC<Props> = ({ endpoint, onClose }) => { |
| 46 | + const [endpointName, setEndpointName] = React.useState<string>(endpoint.name || ''); |
| 47 | + const [endpointDescription, setEndpointDescription] = React.useState<string>(endpoint.description || ''); |
| 48 | + const [url, setUrl] = React.useState<string>(endpoint.url || ''); |
| 49 | + const [modelName, setModelName] = React.useState<string>(endpoint.modelName || ''); |
| 50 | + const [modelDescription, setModelDescription] = React.useState<string>(endpoint.modelDescription || ''); |
| 51 | + const [apiKey, setApiKey] = React.useState<string>(endpoint.apiKey || ''); |
| 52 | + const [nameTouched, setNameTouched] = React.useState<boolean>(); |
| 53 | + const [urlTouched, setUrlTouched] = React.useState<boolean>(); |
| 54 | + const [modelNameTouched, setModelNameTouched] = React.useState<boolean>(); |
| 55 | + const [apiKeyTouched, setApiKeyTouched] = React.useState<boolean>(); |
| 56 | + |
| 57 | + const validName = endpointName.trim().length > 0; |
| 58 | + const validUrl = validateUrl(url); |
| 59 | + const validModelName = modelName.trim().length > 0; |
| 60 | + const validApiKey = apiKey.trim().length > 0; |
| 61 | + |
| 62 | + const isValid = validName && validUrl && validModelName && validApiKey; |
| 63 | + |
| 64 | + return ( |
| 65 | + <Modal |
| 66 | + variant={ModalVariant.medium} |
| 67 | + isOpen |
| 68 | + onClose={() => onClose()} |
| 69 | + aria-labelledby="endpoint-modal-title" |
| 70 | + aria-describedby="endpoint-body-variant" |
| 71 | + > |
| 72 | + <ModalHeader |
| 73 | + title={endpoint?.id ? `Edit ${endpoint.name}` : 'Add a custom model endpoint'} |
| 74 | + labelId="endpoint-modal-title" |
| 75 | + description={ |
| 76 | + endpoint?.id |
| 77 | + ? 'Update the model endpoint details below.' |
| 78 | + : 'Add a custom model endpoint to interact with and test a fine-tuned model using the chat interface.' |
| 79 | + } |
| 80 | + /> |
| 81 | + <ModalBody> |
| 82 | + <Form> |
| 83 | + <FormGroup label="Endpoint name" isRequired fieldId="endpointName"> |
| 84 | + <TextInput |
| 85 | + isRequired |
| 86 | + type="text" |
| 87 | + id="endpointName" |
| 88 | + name="endpointName" |
| 89 | + value={endpointName} |
| 90 | + onChange={(_, value) => setEndpointName(value)} |
| 91 | + placeholder="Enter name" |
| 92 | + onBlur={() => setNameTouched(true)} |
| 93 | + /> |
| 94 | + {nameTouched && !validName ? ( |
| 95 | + <FormHelperText> |
| 96 | + <HelperText> |
| 97 | + <HelperTextItem icon={<ExclamationCircleIcon />} variant={ValidatedOptions.error}> |
| 98 | + Required field |
| 99 | + </HelperTextItem> |
| 100 | + </HelperText> |
| 101 | + </FormHelperText> |
| 102 | + ) : null} |
| 103 | + </FormGroup> |
| 104 | + <FormGroup label="Endpoint description" fieldId="endpointDescription"> |
| 105 | + <TextInput |
| 106 | + type="text" |
| 107 | + id="endpointDescription" |
| 108 | + name="endpointDescription" |
| 109 | + value={endpointDescription} |
| 110 | + onChange={(_, value) => setEndpointDescription(value)} |
| 111 | + placeholder="Enter description" |
| 112 | + /> |
| 113 | + </FormGroup> |
| 114 | + <FormGroup |
| 115 | + label="URL" |
| 116 | + isRequired |
| 117 | + fieldId="url" |
| 118 | + labelHelp={ |
| 119 | + <Popover |
| 120 | + headerContent="Which URL do I use?" |
| 121 | + bodyContent="This should be the full endpoint of what you want to use for chat inference. For example, with OpenAI this would be: `https://api.openai.com/v1/chat/completions` (IE. it should include the path)." |
| 122 | + > |
| 123 | + <Button variant="link" isInline aria-label="More info"> |
| 124 | + <OutlinedQuestionCircleIcon /> |
| 125 | + </Button> |
| 126 | + </Popover> |
| 127 | + } |
| 128 | + > |
| 129 | + <TextInput |
| 130 | + isRequired |
| 131 | + type="text" |
| 132 | + id="url" |
| 133 | + name="url" |
| 134 | + value={url} |
| 135 | + onChange={(_, value) => setUrl(value)} |
| 136 | + placeholder="Enter URL" |
| 137 | + onBlur={() => setUrlTouched(true)} |
| 138 | + /> |
| 139 | + {urlTouched && !validUrl ? ( |
| 140 | + <FormHelperText> |
| 141 | + <HelperText> |
| 142 | + <HelperTextItem icon={<ExclamationCircleIcon />} variant={ValidatedOptions.error}> |
| 143 | + Please enter a valid URL. |
| 144 | + </HelperTextItem> |
| 145 | + </HelperText> |
| 146 | + </FormHelperText> |
| 147 | + ) : null} |
| 148 | + </FormGroup> |
| 149 | + <FormGroup label="Model name" isRequired fieldId="modelName"> |
| 150 | + <TextInput |
| 151 | + isRequired |
| 152 | + type="text" |
| 153 | + id="modelName" |
| 154 | + name="modelName" |
| 155 | + value={modelName} |
| 156 | + onChange={(_, value) => setModelName(value)} |
| 157 | + placeholder="Enter model name" |
| 158 | + onBlur={() => setModelNameTouched(true)} |
| 159 | + /> |
| 160 | + {modelNameTouched && !validModelName ? ( |
| 161 | + <FormHelperText> |
| 162 | + <HelperText> |
| 163 | + <HelperTextItem icon={<ExclamationCircleIcon />} variant={ValidatedOptions.error}> |
| 164 | + Required field |
| 165 | + </HelperTextItem> |
| 166 | + </HelperText> |
| 167 | + </FormHelperText> |
| 168 | + ) : null} |
| 169 | + </FormGroup> |
| 170 | + <FormGroup label="Model description" fieldId="modelDescription"> |
| 171 | + <TextInput |
| 172 | + type="text" |
| 173 | + id="modelDescription" |
| 174 | + name="modelDescription" |
| 175 | + value={modelDescription} |
| 176 | + onChange={(_, value) => setModelDescription(value)} |
| 177 | + placeholder="Enter description" |
| 178 | + /> |
| 179 | + </FormGroup> |
| 180 | + <FormGroup |
| 181 | + label="API Key" |
| 182 | + isRequired |
| 183 | + fieldId="apiKey" |
| 184 | + labelHelp={ |
| 185 | + <Popover headerContent="What is an API Key?" bodyContent="An API key is a unique identifier used to authenticate requests to an API."> |
| 186 | + <Button variant="link" isInline aria-label="More info"> |
| 187 | + <OutlinedQuestionCircleIcon /> |
| 188 | + </Button> |
| 189 | + </Popover> |
| 190 | + } |
| 191 | + > |
| 192 | + <TextInput |
| 193 | + isRequired |
| 194 | + type="password" |
| 195 | + id="apiKey" |
| 196 | + name="apiKey" |
| 197 | + value={apiKey} |
| 198 | + onChange={(_, value) => setApiKey(value)} |
| 199 | + placeholder="Enter API key" |
| 200 | + onBlur={() => setApiKeyTouched(true)} |
| 201 | + /> |
| 202 | + {apiKeyTouched && !validApiKey ? ( |
| 203 | + <FormHelperText> |
| 204 | + <HelperText> |
| 205 | + <HelperTextItem icon={<ExclamationCircleIcon />} variant={ValidatedOptions.error}> |
| 206 | + Required field |
| 207 | + </HelperTextItem> |
| 208 | + </HelperText> |
| 209 | + </FormHelperText> |
| 210 | + ) : null} |
| 211 | + </FormGroup> |
| 212 | + </Form> |
| 213 | + </ModalBody> |
| 214 | + <ModalFooter> |
| 215 | + <Button |
| 216 | + key="save" |
| 217 | + variant="primary" |
| 218 | + isDisabled={!isValid} |
| 219 | + onClick={async () => { |
| 220 | + const updatedUrl = removeTrailingSlash(url); |
| 221 | + const newEndpoint = { |
| 222 | + id: endpoint.id, |
| 223 | + name: endpointName, |
| 224 | + description: endpointDescription, |
| 225 | + url: updatedUrl, |
| 226 | + modelName: modelName, |
| 227 | + modelDescription: modelDescription, |
| 228 | + apiKey: apiKey, |
| 229 | + status: ModelEndpointStatus.unknown, |
| 230 | + enabled: true |
| 231 | + }; |
| 232 | + newEndpoint.status = await fetchEndpointStatus(newEndpoint); |
| 233 | + onClose(newEndpoint); |
| 234 | + }} |
| 235 | + > |
| 236 | + {endpoint.id ? 'Save' : 'Add'} |
| 237 | + </Button> |
| 238 | + <Button key="cancel" variant="link" onClick={() => onClose()}> |
| 239 | + Cancel |
| 240 | + </Button> |
| 241 | + </ModalFooter> |
| 242 | + </Modal> |
| 243 | + ); |
| 244 | +}; |
| 245 | + |
| 246 | +export default EditEndpointModal; |
0 commit comments