feat(llm-config): add generic configuration dialog for LLM providers
This commit is contained in:
@@ -13,7 +13,9 @@ 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';
|
||||
import type { ISetApiKeyRequestBody, IAddLlmRequestBody } from '@/interfaces/request/llm';
|
||||
import type { ConfigFormItem, ConfigurationFormData, DocLinkConfig } from '../components/Dialog/ConfigurationDialog';
|
||||
import { getLLMConfig } from '../components/Dialog/llmConfigs';
|
||||
|
||||
// 对话框状态管理 hook
|
||||
export const useDialogState = () => {
|
||||
@@ -45,6 +47,68 @@ export const useDialogState = () => {
|
||||
openDialog,
|
||||
closeDialog,
|
||||
};
|
||||
}
|
||||
|
||||
// 通用配置对话框管理
|
||||
export const useConfigurationDialog = (onSuccess?: () => void) => {
|
||||
const dialogState = useDialogState();
|
||||
const showMessage = useMessage();
|
||||
const [llmFactory, setLlmFactory] = useState('');
|
||||
const [config, setConfig] = useState<{
|
||||
formItems: ConfigFormItem[];
|
||||
docLink?: DocLinkConfig;
|
||||
title: string;
|
||||
defaultValues: Record<string, any>;
|
||||
} | null>(null);
|
||||
|
||||
const openConfigurationDialog = useCallback((factory: string, data?: Partial<ConfigurationFormData>, isEdit = false) => {
|
||||
setLlmFactory(factory);
|
||||
const llmConfig = getLLMConfig(factory);
|
||||
setConfig(llmConfig);
|
||||
|
||||
// 合并默认值和传入的数据
|
||||
const mergedData = {
|
||||
...llmConfig.defaultValues,
|
||||
...data,
|
||||
};
|
||||
|
||||
dialogState.openDialog(mergedData, isEdit);
|
||||
}, [dialogState]);
|
||||
|
||||
const submitConfiguration = useCallback(async (data: Partial<ConfigurationFormData>) => {
|
||||
dialogState.setLoading(true);
|
||||
logger.info('提交配置:', data);
|
||||
try {
|
||||
// 构建请求参数
|
||||
const params: Partial<IAddLlmRequestBody> = {
|
||||
...data,
|
||||
llm_factory: llmFactory,
|
||||
};
|
||||
|
||||
await userService.add_llm(params);
|
||||
showMessage.success(`${config?.title || llmFactory} 配置成功`);
|
||||
dialogState.closeDialog();
|
||||
|
||||
// 调用成功回调
|
||||
if (onSuccess) {
|
||||
onSuccess();
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(`${config?.title || llmFactory} 配置失败:`, error);
|
||||
showMessage.error(`${config?.title || llmFactory} 配置失败`);
|
||||
throw error;
|
||||
} finally {
|
||||
dialogState.setLoading(false);
|
||||
}
|
||||
}, [llmFactory, config, dialogState, showMessage, onSuccess]);
|
||||
|
||||
return {
|
||||
...dialogState,
|
||||
llmFactory,
|
||||
config,
|
||||
openConfigurationDialog,
|
||||
submitConfiguration,
|
||||
};
|
||||
};
|
||||
|
||||
// API Key 对话框管理
|
||||
@@ -352,33 +416,22 @@ export const useModelDialogs = (onSuccess?: () => void) => {
|
||||
const azureDialog = useAzureOpenAIDialog();
|
||||
const bedrockDialog = useBedrockDialog();
|
||||
const ollamaDialog = useOllamaDialog();
|
||||
const configurationDialog = useConfigurationDialog(onSuccess);
|
||||
const systemDialog = useSystemModelSetting(onSuccess);
|
||||
const deleteOps = useDeleteOperations(onSuccess);
|
||||
|
||||
// 根据工厂类型打开对应的对话框
|
||||
const openFactoryDialog = useCallback((factoryName: string, data?: any, isEdit = false) => {
|
||||
switch (factoryName.toLowerCase()) {
|
||||
case 'azureopenai':
|
||||
azureDialog.openDialog(data, isEdit);
|
||||
break;
|
||||
case 'bedrock':
|
||||
bedrockDialog.openDialog(data, isEdit);
|
||||
break;
|
||||
case 'ollama':
|
||||
ollamaDialog.openDialog(data, isEdit);
|
||||
break;
|
||||
default:
|
||||
// 默认使用 API Key 对话框
|
||||
apiKeyDialog.openApiKeyDialog(factoryName, data, isEdit);
|
||||
break;
|
||||
}
|
||||
}, [apiKeyDialog, azureDialog, bedrockDialog, ollamaDialog]);
|
||||
// 使用通用的 ConfigurationDialog 替代特定的 Dialog
|
||||
configurationDialog.openConfigurationDialog(factoryName, data, isEdit);
|
||||
}, [configurationDialog]);
|
||||
|
||||
return {
|
||||
apiKeyDialog,
|
||||
azureDialog,
|
||||
bedrockDialog,
|
||||
ollamaDialog,
|
||||
configurationDialog,
|
||||
systemDialog,
|
||||
deleteOps,
|
||||
openFactoryDialog,
|
||||
|
||||
Reference in New Issue
Block a user