@@ -42,6 +42,8 @@ interface LLMConfigFormProps {
4242function LLMConfigForm ( { open, config, onSave, onCancel } : LLMConfigFormProps ) {
4343 const [ form ] = Form . useForm ( )
4444 const [ loading , setLoading ] = useState ( false )
45+ const [ modelLoading , setModelLoading ] = useState ( false )
46+ const [ models , setModels ] = useState < string [ ] > ( [ ] )
4547 const { message } = App . useApp ( )
4648 const { settings } = useSettingsStore ( )
4749
@@ -53,6 +55,7 @@ function LLMConfigForm({ open, config, onSave, onCancel }: LLMConfigFormProps) {
5355 } else {
5456 form . resetFields ( )
5557 }
58+ setModels ( [ ] )
5659 }
5760 } , [ open , config , form ] )
5861
@@ -79,31 +82,59 @@ function LLMConfigForm({ open, config, onSave, onCancel }: LLMConfigFormProps) {
7982 }
8083 }
8184
85+ const fetchModels = async ( ) => {
86+ try {
87+ const apiHost = form . getFieldValue ( 'apiHost' )
88+ const apiKey = form . getFieldValue ( 'apiKey' )
89+
90+ if ( ! apiHost || ! apiKey ) {
91+ message . warning ( '请先输入 API Host 和 API Key' )
92+ return
93+ }
94+
95+ setModelLoading ( true )
96+ const result = await window . api . ai . getModels ( {
97+ id : 'temp' ,
98+ name : 'temp' ,
99+ apiHost,
100+ apiKey,
101+ modelName : '' ,
102+ createdAt : Date . now ( )
103+ } )
104+
105+ if ( result . success && result . models ) {
106+ setModels ( result . models )
107+ message . success ( `成功获取 ${ result . models . length } 个模型` )
108+ } else {
109+ message . error ( result . error || '获取模型列表失败' )
110+ setModels ( [ ] )
111+ }
112+ } catch ( error ) {
113+ message . error ( '获取模型列表失败' )
114+ setModels ( [ ] )
115+ } finally {
116+ setModelLoading ( false )
117+ }
118+ }
119+
82120 const testConnection = async ( ) => {
83121 try {
84122 const values = await form . validateFields ( )
85123 setLoading ( true )
86124
87- const tempConfig : LLMConfig = {
125+ const result = await window . api . ai . testConnection ( {
88126 id : 'temp' ,
89127 name : values . name ,
90128 apiHost : values . apiHost ,
91129 apiKey : values . apiKey ,
92130 modelName : values . modelName ,
93131 createdAt : Date . now ( )
94- }
95-
96- const defaultModelConfig =
97- settings . modelConfigs . find ( ( c ) => c . id === settings . defaultModelConfigId ) ||
98- settings . modelConfigs [ 0 ]
99- const aiService = createAIService ( tempConfig , defaultModelConfig )
100- const isConnected = await aiService . testConnection ( )
132+ } )
101133
102- console . log ( 'isConnected' , isConnected )
103- if ( isConnected ) {
134+ if ( result . success ) {
104135 message . success ( '连接测试成功' )
105136 } else {
106- message . error ( '连接测试失败,请检查配置' )
137+ message . error ( result . error || '连接测试失败,请检查配置' )
107138 }
108139 } catch ( error ) {
109140 message . error ( '连接测试失败,请检查配置' )
@@ -167,8 +198,32 @@ function LLMConfigForm({ open, config, onSave, onCancel }: LLMConfigFormProps) {
167198 name = "modelName"
168199 label = "模型名称"
169200 rules = { [ { required : true , message : '请输入模型名称' } ] }
201+ extra = {
202+ < Button
203+ type = "link"
204+ size = "small"
205+ onClick = { fetchModels }
206+ loading = { modelLoading }
207+ style = { { padding : 0 } }
208+ >
209+ 从服务器获取模型列表
210+ </ Button >
211+ }
170212 >
171- < Input placeholder = "gpt-4" />
213+ { models . length > 0 ? (
214+ < Select
215+ placeholder = "选择或输入模型名称"
216+ showSearch
217+ allowClear
218+ loading = { modelLoading }
219+ options = { models . map ( ( model ) => ( { label : model , value : model } ) ) }
220+ filterOption = { ( input , option ) =>
221+ ( option ?. label ?? '' ) . toLowerCase ( ) . includes ( input . toLowerCase ( ) )
222+ }
223+ />
224+ ) : (
225+ < Input placeholder = "请输入模型名称,例如: gpt-4" />
226+ ) }
172227 </ Form . Item >
173228
174229 < Form . Item
0 commit comments