feat(models): enhance model management with improved dialogs and state handling
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useState, useCallback, useMemo } from 'react';
|
||||
import { useState, useCallback, useMemo, useEffect } from 'react';
|
||||
import { useMessage } from '@/hooks/useSnackbar';
|
||||
import userService from '@/services/user_service';
|
||||
import logger from '@/utils/logger';
|
||||
@@ -11,6 +11,8 @@ import type {
|
||||
import type { ITenantInfo } from '@/interfaces/database/knowledge';
|
||||
import { useLlmList } from '@/hooks/llm-hooks';
|
||||
import type { LlmModelType } from '@/constants/knowledge';
|
||||
import { useUserData } from '@/hooks/useUserData';
|
||||
import type { ISetApiKeyRequestBody } from '@/interfaces/request/llm';
|
||||
|
||||
// 对话框状态管理 hook
|
||||
export const useDialogState = () => {
|
||||
@@ -20,7 +22,9 @@ export const useDialogState = () => {
|
||||
const [initialData, setInitialData] = useState<any>(null);
|
||||
|
||||
const openDialog = useCallback((data?: any, isEdit = false) => {
|
||||
setInitialData(data);
|
||||
if (data != null) {
|
||||
setInitialData(data);
|
||||
}
|
||||
setEditMode(isEdit);
|
||||
setOpen(true);
|
||||
}, []);
|
||||
@@ -55,19 +59,26 @@ export const useApiKeyDialog = () => {
|
||||
|
||||
const submitApiKey = useCallback(async (data: ApiKeyFormData) => {
|
||||
dialogState.setLoading(true);
|
||||
logger.info('提交 API Key:', data);
|
||||
try {
|
||||
await userService.set_api_key({
|
||||
factory_name: factoryName,
|
||||
model_name: '', // 根据实际需求调整
|
||||
// api_key: data.api_key,
|
||||
...data
|
||||
});
|
||||
const params: ISetApiKeyRequestBody = {
|
||||
llm_factory: factoryName,
|
||||
api_key: data.api_key,
|
||||
};
|
||||
|
||||
if (data.base_url && data.base_url.trim() !== '') {
|
||||
params.base_url = data.base_url;
|
||||
}
|
||||
|
||||
if (data.group_id && data.group_id.trim() !== '') {
|
||||
// params.group_id = data.group_id;
|
||||
}
|
||||
|
||||
await userService.set_api_key(params);
|
||||
showMessage.success('API Key 配置成功');
|
||||
dialogState.closeDialog();
|
||||
} catch (error) {
|
||||
logger.error('API Key 配置失败:', error);
|
||||
showMessage.error('API Key 配置失败');
|
||||
throw error;
|
||||
} finally {
|
||||
dialogState.setLoading(false);
|
||||
}
|
||||
@@ -91,8 +102,8 @@ export const useAzureOpenAIDialog = () => {
|
||||
try {
|
||||
// 调用 Azure OpenAI 特定的 API
|
||||
await userService.set_api_key({
|
||||
factory_name: 'AzureOpenAI',
|
||||
model_name: data.deployment_name,
|
||||
llm_factory: 'AzureOpenAI',
|
||||
llm_name: data.deployment_name,
|
||||
api_key: data.api_key,
|
||||
// azure_endpoint: data.azure_endpoint,
|
||||
// api_version: data.api_version,
|
||||
@@ -124,8 +135,8 @@ export const useBedrockDialog = () => {
|
||||
try {
|
||||
// 调用 Bedrock 特定的 API
|
||||
await userService.set_api_key({
|
||||
factory_name: 'Bedrock',
|
||||
model_name: '',
|
||||
llm_factory: 'Bedrock',
|
||||
llm_name: '',
|
||||
api_key: '', // Bedrock 使用 access key
|
||||
// access_key_id: data.access_key_id,
|
||||
// secret_access_key: data.secret_access_key,
|
||||
@@ -158,8 +169,8 @@ export const useOllamaDialog = () => {
|
||||
try {
|
||||
// 调用添加 LLM 的 API
|
||||
await userService.add_llm({
|
||||
factory_name: 'Ollama',
|
||||
model_name: data.model_name,
|
||||
llm_factory: 'Ollama',
|
||||
llm_name: data.model_name,
|
||||
// base_url: data.base_url,
|
||||
});
|
||||
showMessage.success('Ollama 模型添加成功');
|
||||
@@ -188,14 +199,12 @@ export const useDeleteOperations = () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
await userService.delete_llm({
|
||||
factory_name: factoryName,
|
||||
model_name: modelName,
|
||||
llm_factory: factoryName,
|
||||
llm_name: modelName,
|
||||
});
|
||||
showMessage.success('模型删除成功');
|
||||
} catch (error) {
|
||||
logger.error('模型删除失败:', error);
|
||||
showMessage.error('模型删除失败');
|
||||
throw error;
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
@@ -205,13 +214,11 @@ export const useDeleteOperations = () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
await userService.deleteFactory({
|
||||
factory_name: factoryName,
|
||||
llm_factory: factoryName,
|
||||
});
|
||||
showMessage.success('模型工厂删除成功');
|
||||
} catch (error) {
|
||||
logger.error('模型工厂删除失败:', error);
|
||||
showMessage.error('模型工厂删除失败');
|
||||
throw error;
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
@@ -231,6 +238,12 @@ export const useSystemModelSetting = () => {
|
||||
|
||||
const { data: llmList } = useLlmList();
|
||||
|
||||
const { tenantInfo, fetchTenantInfo } = useUserData();
|
||||
|
||||
useEffect(() => {
|
||||
fetchTenantInfo();
|
||||
}, []);
|
||||
|
||||
const getOptionsByModelType = useCallback((modelType: LlmModelType) => {
|
||||
return Object.entries(llmList)
|
||||
.filter(([, value]) =>
|
||||
@@ -278,11 +291,16 @@ export const useSystemModelSetting = () => {
|
||||
|
||||
const submitSystemModelSetting = useCallback(async (data: Partial<ITenantInfo>) => {
|
||||
dialogState.setLoading(true);
|
||||
logger.debug('submitSystemModelSetting data:', data);
|
||||
try {
|
||||
delete data.role;
|
||||
// 这里需要根据实际的 API 接口调整
|
||||
// await userService.setSystemDefaultModel(data);
|
||||
await userService.setTenantInfo({
|
||||
...data,
|
||||
});
|
||||
showMessage.success('系统默认模型设置成功');
|
||||
dialogState.closeDialog();
|
||||
fetchTenantInfo();
|
||||
} catch (error) {
|
||||
logger.error('系统默认模型设置失败:', error);
|
||||
showMessage.error('系统默认模型设置失败');
|
||||
@@ -296,6 +314,7 @@ export const useSystemModelSetting = () => {
|
||||
...dialogState,
|
||||
submitSystemModelSetting,
|
||||
allModelOptions,
|
||||
initialData: tenantInfo,
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user