import React, { useEffect } from 'react'; import { Dialog, DialogTitle, DialogContent, DialogActions, Button, TextField, Box, Typography, IconButton, InputAdornment, FormControl, InputLabel, Select, MenuItem, CircularProgress, FormHelperText, Link, } from '@mui/material'; import { Visibility, VisibilityOff } from '@mui/icons-material'; import { Controller, useForm } from 'react-hook-form'; import type { IAddLlmRequestBody } from '@/interfaces/request/llm'; // AWS Bedrock 支持的区域列表 export const BEDROCK_REGIONS = [ 'us-east-2', 'us-east-1', 'us-west-1', 'us-west-2', 'af-south-1', 'ap-east-1', 'ap-south-2', 'ap-southeast-3', 'ap-southeast-5', 'ap-southeast-4', 'ap-south-1', 'ap-northeast-3', 'ap-northeast-2', 'ap-southeast-1', 'ap-southeast-2', 'ap-east-2', 'ap-southeast-7', 'ap-northeast-1', 'ca-central-1', 'ca-west-1', 'eu-central-1', 'eu-west-1', 'eu-west-2', 'eu-south-1', 'eu-west-3', 'eu-south-2', 'eu-north-1', 'eu-central-2', 'il-central-1', 'mx-central-1', 'me-south-1', 'me-central-1', 'sa-east-1', 'us-gov-east-1', 'us-gov-west-1', ]; // 模型类型选项 const MODEL_TYPE_OPTIONS = [ { value: 'chat', label: 'Chat' }, { value: 'embedding', label: 'Embedding' }, ]; // 表单数据接口 export interface BedrockFormData extends IAddLlmRequestBody { bedrock_ak: string; bedrock_sk: string; bedrock_region: string; } // 对话框 Props 接口 export interface BedrockDialogProps { open: boolean; onClose: () => void; onSubmit: (data: BedrockFormData) => void; loading: boolean; initialData?: BedrockFormData; editMode?: boolean; } /** * AWS Bedrock 配置对话框 */ function BedrockDialog ({ open, onClose, onSubmit, loading, initialData, editMode = false, }: BedrockDialogProps) { const [showAccessKey, setShowAccessKey] = React.useState(false); const [showSecretKey, setShowSecretKey] = React.useState(false); const { control, handleSubmit, reset, formState: { errors }, } = useForm({ defaultValues: { model_type: 'chat', llm_name: '', bedrock_ak: '', bedrock_sk: '', bedrock_region: 'us-east-1', max_tokens: 4096, llm_factory: 'Bedrock', }, }); // 当对话框打开或初始数据变化时重置表单 useEffect(() => { if (open) { reset({ model_type: 'chat', llm_name: '', bedrock_ak: '', bedrock_sk: '', bedrock_region: 'us-east-1', max_tokens: 4096, llm_factory: initialData?.llm_factory || 'Bedrock', }); } }, [open, initialData, reset]); const handleFormSubmit = (data: BedrockFormData) => { onSubmit(data); }; const toggleShowAccessKey = () => { setShowAccessKey(!showAccessKey); }; const toggleShowSecretKey = () => { setShowSecretKey(!showSecretKey); }; const docInfo = { url: 'https://console.aws.amazon.com/', text: '如何集成 Bedrock', }; return ( {editMode ? '编辑' : '添加'} LLM {/* 模型类型 */} ( * 模型类型 {errors.model_type && ( {errors.model_type.message} )} )} /> {/* 模型名称 */} ( )} /> {/* ACCESS KEY */} ( {showAccessKey ? : } ), }} /> )} /> {/* SECRET KEY */} ( {showSecretKey ? : } ), }} /> )} /> {/* AWS Region */} ( * AWS Region {errors.bedrock_region && ( {errors.bedrock_region.message} )} )} /> {/* 最大token数 */} ( field.onChange(Number(e.target.value))} /> )} /> {docInfo.text} ); }; export default BedrockDialog;