|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import { |
| 3 | + Dialog, |
| 4 | + DialogTitle, |
| 5 | + DialogContent, |
| 6 | + DialogActions, |
| 7 | + TextField, |
| 8 | + Button, |
| 9 | + Box, |
| 10 | + Alert, |
| 11 | + FormControlLabel, |
| 12 | + Switch |
| 13 | +} from '@mui/material'; |
| 14 | + |
| 15 | +/** |
| 16 | + * ModelEditModal Component |
| 17 | + * |
| 18 | + * Modal for editing an existing model's configuration |
| 19 | + */ |
| 20 | +export default function ModelEditModal({ open, onClose, onSave, model }) { |
| 21 | + const [formData, setFormData] = useState({ |
| 22 | + display_name: '', |
| 23 | + context_length: 4096, |
| 24 | + cost_per_1m_input_tokens: 0, |
| 25 | + cost_per_1m_output_tokens: 0, |
| 26 | + enabled: true |
| 27 | + }); |
| 28 | + const [error, setError] = useState(''); |
| 29 | + const [loading, setLoading] = useState(false); |
| 30 | + |
| 31 | + // Populate form when model changes or dialog opens |
| 32 | + useEffect(() => { |
| 33 | + if (model && open) { |
| 34 | + setFormData({ |
| 35 | + display_name: model.display_name || model.name || '', |
| 36 | + context_length: model.context_length || 4096, |
| 37 | + cost_per_1m_input_tokens: model.cost_per_1m_input_tokens ?? (model.cost_per_input_token ? model.cost_per_input_token * 1_000_000 : 0), |
| 38 | + cost_per_1m_output_tokens: model.cost_per_1m_output_tokens ?? (model.cost_per_output_token ? model.cost_per_output_token * 1_000_000 : 0), |
| 39 | + enabled: model.enabled !== false && model.status !== 'inactive' |
| 40 | + }); |
| 41 | + setError(''); |
| 42 | + } |
| 43 | + }, [model, open]); |
| 44 | + |
| 45 | + if (!open) return null; |
| 46 | + |
| 47 | + const handleChange = (field) => (event) => { |
| 48 | + setFormData({ |
| 49 | + ...formData, |
| 50 | + [field]: event.target.value |
| 51 | + }); |
| 52 | + }; |
| 53 | + |
| 54 | + const handleSwitchChange = (field) => (event) => { |
| 55 | + setFormData({ |
| 56 | + ...formData, |
| 57 | + [field]: event.target.checked |
| 58 | + }); |
| 59 | + }; |
| 60 | + |
| 61 | + const handleSubmit = async () => { |
| 62 | + setError(''); |
| 63 | + setLoading(true); |
| 64 | + |
| 65 | + try { |
| 66 | + if (!formData.display_name) { |
| 67 | + throw new Error('Display name is required'); |
| 68 | + } |
| 69 | + |
| 70 | + await onSave(model, { |
| 71 | + display_name: formData.display_name, |
| 72 | + context_length: parseInt(formData.context_length, 10) || 4096, |
| 73 | + cost_per_1m_input_tokens: parseFloat(formData.cost_per_1m_input_tokens) || 0, |
| 74 | + cost_per_1m_output_tokens: parseFloat(formData.cost_per_1m_output_tokens) || 0, |
| 75 | + enabled: formData.enabled |
| 76 | + }); |
| 77 | + onClose(); |
| 78 | + } catch (err) { |
| 79 | + setError(err.message || 'Failed to update model'); |
| 80 | + } finally { |
| 81 | + setLoading(false); |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + const handleClose = () => { |
| 86 | + setError(''); |
| 87 | + onClose(); |
| 88 | + }; |
| 89 | + |
| 90 | + return ( |
| 91 | + <Dialog open={open} onClose={handleClose} maxWidth="sm" fullWidth> |
| 92 | + <DialogTitle>Edit Model: {model?.name || ''}</DialogTitle> |
| 93 | + <DialogContent> |
| 94 | + <Box sx={{ display: 'flex', flexDirection: 'column', gap: 2, mt: 1 }}> |
| 95 | + {error && <Alert severity="error">{error}</Alert>} |
| 96 | + |
| 97 | + <TextField |
| 98 | + label="Display Name" |
| 99 | + value={formData.display_name} |
| 100 | + onChange={handleChange('display_name')} |
| 101 | + required |
| 102 | + fullWidth |
| 103 | + helperText="The name shown in the UI" |
| 104 | + /> |
| 105 | + |
| 106 | + <TextField |
| 107 | + label="Context Length (tokens)" |
| 108 | + value={formData.context_length} |
| 109 | + onChange={handleChange('context_length')} |
| 110 | + type="number" |
| 111 | + fullWidth |
| 112 | + helperText="Maximum context window in tokens" |
| 113 | + /> |
| 114 | + |
| 115 | + <TextField |
| 116 | + label="Cost per 1M Input Tokens ($)" |
| 117 | + value={formData.cost_per_1m_input_tokens} |
| 118 | + onChange={handleChange('cost_per_1m_input_tokens')} |
| 119 | + type="number" |
| 120 | + inputProps={{ step: '0.01' }} |
| 121 | + fullWidth |
| 122 | + helperText="e.g., 0.15 for $0.15 per 1M input tokens (0 = free)" |
| 123 | + /> |
| 124 | + |
| 125 | + <TextField |
| 126 | + label="Cost per 1M Output Tokens ($)" |
| 127 | + value={formData.cost_per_1m_output_tokens} |
| 128 | + onChange={handleChange('cost_per_1m_output_tokens')} |
| 129 | + type="number" |
| 130 | + inputProps={{ step: '0.01' }} |
| 131 | + fullWidth |
| 132 | + helperText="e.g., 0.60 for $0.60 per 1M output tokens (0 = free)" |
| 133 | + /> |
| 134 | + |
| 135 | + <FormControlLabel |
| 136 | + control={ |
| 137 | + <Switch |
| 138 | + checked={formData.enabled} |
| 139 | + onChange={handleSwitchChange('enabled')} |
| 140 | + /> |
| 141 | + } |
| 142 | + label="Enabled" |
| 143 | + /> |
| 144 | + </Box> |
| 145 | + </DialogContent> |
| 146 | + <DialogActions> |
| 147 | + <Button onClick={handleClose}>Cancel</Button> |
| 148 | + <Button onClick={handleSubmit} variant="contained" disabled={loading}> |
| 149 | + {loading ? 'Saving...' : 'Save Changes'} |
| 150 | + </Button> |
| 151 | + </DialogActions> |
| 152 | + </Dialog> |
| 153 | + ); |
| 154 | +} |
0 commit comments