|
| 1 | +import * as React from 'react'; |
| 2 | +import { useTranslation } from 'react-i18next'; |
| 3 | +import { |
| 4 | + Button, |
| 5 | + ExpandableSection, |
| 6 | + FormGroup, |
| 7 | + FormSelect, |
| 8 | + FormSelectOption, |
| 9 | + NumberInput, |
| 10 | + TextInput, |
| 11 | + Title, |
| 12 | +} from '@patternfly/react-core'; |
| 13 | + |
| 14 | +import { LLMProviderK8s } from '../../models/proposal'; |
| 15 | + |
| 16 | +type AgentFormProps = { |
| 17 | + providers: LLMProviderK8s[]; |
| 18 | + onSubmit: (name: string, spec: Record<string, unknown>) => Promise<void>; |
| 19 | + onCancel: () => void; |
| 20 | +}; |
| 21 | + |
| 22 | +const AgentForm: React.FC<AgentFormProps> = ({ providers, onSubmit, onCancel }) => { |
| 23 | + const { t } = useTranslation('plugin__lightspeed-agentic-console-plugin'); |
| 24 | + |
| 25 | + const [name, setName] = React.useState(''); |
| 26 | + const [providerName, setProviderName] = React.useState(''); |
| 27 | + const [model, setModel] = React.useState(''); |
| 28 | + |
| 29 | + React.useEffect(() => { |
| 30 | + if (!providerName && providers.length > 0) { |
| 31 | + setProviderName(providers[0].metadata.name); |
| 32 | + } |
| 33 | + }, [providers, providerName]); |
| 34 | + const [maxTurns, setMaxTurns] = React.useState(100); |
| 35 | + const [analysisSeconds, setAnalysisSeconds] = React.useState(300); |
| 36 | + const [executionSeconds, setExecutionSeconds] = React.useState(600); |
| 37 | + const [verificationSeconds, setVerificationSeconds] = React.useState(300); |
| 38 | + const [chatSeconds, setChatSeconds] = React.useState(120); |
| 39 | + const [showTimeouts, setShowTimeouts] = React.useState(false); |
| 40 | + const [submitting, setSubmitting] = React.useState(false); |
| 41 | + const [error, setError] = React.useState(''); |
| 42 | + |
| 43 | + const buildSpec = (): Record<string, unknown> => { |
| 44 | + const spec: Record<string, unknown> = { |
| 45 | + llmProvider: { name: providerName }, |
| 46 | + model, |
| 47 | + maxTurns, |
| 48 | + }; |
| 49 | + if (showTimeouts) { |
| 50 | + spec.timeouts = { |
| 51 | + analysisSeconds, |
| 52 | + executionSeconds, |
| 53 | + verificationSeconds, |
| 54 | + chatSeconds, |
| 55 | + }; |
| 56 | + } |
| 57 | + return spec; |
| 58 | + }; |
| 59 | + |
| 60 | + const handleSubmit = async () => { |
| 61 | + setSubmitting(true); |
| 62 | + setError(''); |
| 63 | + try { |
| 64 | + await onSubmit(name, buildSpec()); |
| 65 | + } catch (err) { |
| 66 | + setError(err instanceof Error ? err.message : String(err)); |
| 67 | + setSubmitting(false); |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + const isValid = (): boolean => { |
| 72 | + return !!name && !!providerName && !!model; |
| 73 | + }; |
| 74 | + |
| 75 | + const clampedNumberInput = ( |
| 76 | + value: number, |
| 77 | + min: number, |
| 78 | + max: number, |
| 79 | + setter: (v: number) => void, |
| 80 | + ) => ( |
| 81 | + <NumberInput |
| 82 | + value={value} |
| 83 | + min={min} |
| 84 | + max={max} |
| 85 | + onMinus={() => setter(Math.max(min, value - 30))} |
| 86 | + onPlus={() => setter(Math.min(max, value + 30))} |
| 87 | + onChange={(e) => { |
| 88 | + const val = Number((e.target as HTMLInputElement).value); |
| 89 | + if (val >= min && val <= max) setter(val); |
| 90 | + }} |
| 91 | + /> |
| 92 | + ); |
| 93 | + |
| 94 | + return ( |
| 95 | + <div className="ols-plugin__config-form-section"> |
| 96 | + <Title headingLevel="h3">{t('Create Agent')}</Title> |
| 97 | + |
| 98 | + {error && <p className="ols-plugin__config-error-text">{error}</p>} |
| 99 | + |
| 100 | + <FormGroup label={t('Name')} isRequired fieldId="agent-name"> |
| 101 | + <TextInput id="agent-name" value={name} onChange={(_e, v) => setName(v)} isRequired placeholder="default" /> |
| 102 | + </FormGroup> |
| 103 | + |
| 104 | + <FormGroup label={t('LLM Provider')} isRequired fieldId="agent-provider"> |
| 105 | + <FormSelect |
| 106 | + id="agent-provider" |
| 107 | + value={providerName} |
| 108 | + onChange={(_e, v) => setProviderName(v)} |
| 109 | + > |
| 110 | + {providers.length ? ( |
| 111 | + providers.map((p) => ( |
| 112 | + <FormSelectOption key={p.metadata.name} value={p.metadata.name} label={p.metadata.name} /> |
| 113 | + )) |
| 114 | + ) : ( |
| 115 | + <FormSelectOption value="" label={t('No providers available')} isDisabled /> |
| 116 | + )} |
| 117 | + </FormSelect> |
| 118 | + </FormGroup> |
| 119 | + |
| 120 | + <FormGroup label={t('Model')} isRequired fieldId="agent-model"> |
| 121 | + <TextInput |
| 122 | + id="agent-model" |
| 123 | + value={model} |
| 124 | + onChange={(_e, v) => setModel(v)} |
| 125 | + isRequired |
| 126 | + placeholder="claude-opus-4-6" |
| 127 | + /> |
| 128 | + </FormGroup> |
| 129 | + |
| 130 | + <FormGroup label={t('Max Turns')} fieldId="agent-max-turns"> |
| 131 | + {clampedNumberInput(maxTurns, 1, 500, setMaxTurns)} |
| 132 | + </FormGroup> |
| 133 | + |
| 134 | + <ExpandableSection |
| 135 | + toggleText={showTimeouts ? t('Hide Timeouts') : t('Show Timeouts')} |
| 136 | + isExpanded={showTimeouts} |
| 137 | + onToggle={(_e, expanded) => setShowTimeouts(expanded)} |
| 138 | + > |
| 139 | + <FormGroup label={t('Analysis (seconds)')} fieldId="agent-timeout-analysis"> |
| 140 | + {clampedNumberInput(analysisSeconds, 1, 3600, setAnalysisSeconds)} |
| 141 | + </FormGroup> |
| 142 | + <FormGroup label={t('Execution (seconds)')} fieldId="agent-timeout-execution"> |
| 143 | + {clampedNumberInput(executionSeconds, 1, 3600, setExecutionSeconds)} |
| 144 | + </FormGroup> |
| 145 | + <FormGroup label={t('Verification (seconds)')} fieldId="agent-timeout-verification"> |
| 146 | + {clampedNumberInput(verificationSeconds, 1, 3600, setVerificationSeconds)} |
| 147 | + </FormGroup> |
| 148 | + <FormGroup label={t('Chat (seconds)')} fieldId="agent-timeout-chat"> |
| 149 | + {clampedNumberInput(chatSeconds, 1, 600, setChatSeconds)} |
| 150 | + </FormGroup> |
| 151 | + </ExpandableSection> |
| 152 | + |
| 153 | + <div className="ols-plugin__config-form-actions"> |
| 154 | + <Button variant="primary" onClick={handleSubmit} isLoading={submitting} isDisabled={!isValid() || submitting}> |
| 155 | + {t('Create')} |
| 156 | + </Button> |
| 157 | + <Button variant="link" onClick={onCancel} isDisabled={submitting}> |
| 158 | + {t('Cancel')} |
| 159 | + </Button> |
| 160 | + </div> |
| 161 | + </div> |
| 162 | + ); |
| 163 | +}; |
| 164 | + |
| 165 | +export default AgentForm; |
0 commit comments