2024-07-17 19:07:34 +08:00
|
|
|
import { useSetModalState, useShowDeleteConfirm } from '@/hooks/common-hooks';
|
2024-03-15 19:35:59 +08:00
|
|
|
import {
|
|
|
|
|
IApiKeySavingParams,
|
|
|
|
|
ISystemModelSettingSavingParams,
|
2024-04-08 19:13:45 +08:00
|
|
|
useAddLlm,
|
2024-05-10 10:38:39 +08:00
|
|
|
useDeleteLlm,
|
2024-03-15 19:35:59 +08:00
|
|
|
useSaveApiKey,
|
|
|
|
|
useSaveTenantInfo,
|
2024-03-18 16:45:01 +08:00
|
|
|
useSelectLlmOptionsByModelType,
|
2024-07-17 19:07:34 +08:00
|
|
|
} from '@/hooks/llm-hooks';
|
2024-07-25 18:53:10 +08:00
|
|
|
import { useFetchTenantInfo } from '@/hooks/user-setting-hooks';
|
2024-04-08 19:13:45 +08:00
|
|
|
import { IAddLlmRequestBody } from '@/interfaces/request/llm';
|
2024-07-25 18:53:10 +08:00
|
|
|
import { useCallback, useState } from 'react';
|
2024-07-17 09:45:10 +08:00
|
|
|
import { ApiKeyPostBody } from '../interface';
|
2024-03-12 18:58:09 +08:00
|
|
|
|
|
|
|
|
type SavingParamsState = Omit<IApiKeySavingParams, 'api_key'>;
|
|
|
|
|
|
|
|
|
|
export const useSubmitApiKey = () => {
|
|
|
|
|
const [savingParams, setSavingParams] = useState<SavingParamsState>(
|
|
|
|
|
{} as SavingParamsState,
|
|
|
|
|
);
|
2024-07-25 18:06:39 +08:00
|
|
|
const { saveApiKey, loading } = useSaveApiKey();
|
2024-03-12 18:58:09 +08:00
|
|
|
const {
|
|
|
|
|
visible: apiKeyVisible,
|
|
|
|
|
hideModal: hideApiKeyModal,
|
|
|
|
|
showModal: showApiKeyModal,
|
|
|
|
|
} = useSetModalState();
|
|
|
|
|
|
|
|
|
|
const onApiKeySavingOk = useCallback(
|
2024-07-17 09:45:10 +08:00
|
|
|
async (postBody: ApiKeyPostBody) => {
|
2024-03-29 09:52:19 +08:00
|
|
|
const ret = await saveApiKey({
|
|
|
|
|
...savingParams,
|
2024-07-17 09:45:10 +08:00
|
|
|
...postBody,
|
2024-03-29 09:52:19 +08:00
|
|
|
});
|
2024-03-12 18:58:09 +08:00
|
|
|
|
2024-03-15 19:35:59 +08:00
|
|
|
if (ret === 0) {
|
2024-03-12 18:58:09 +08:00
|
|
|
hideApiKeyModal();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[hideApiKeyModal, saveApiKey, savingParams],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const onShowApiKeyModal = useCallback(
|
|
|
|
|
(savingParams: SavingParamsState) => {
|
|
|
|
|
setSavingParams(savingParams);
|
|
|
|
|
showApiKeyModal();
|
|
|
|
|
},
|
|
|
|
|
[showApiKeyModal, setSavingParams],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
saveApiKeyLoading: loading,
|
|
|
|
|
initialApiKey: '',
|
2024-03-29 09:52:19 +08:00
|
|
|
llmFactory: savingParams.llm_factory,
|
2024-03-12 18:58:09 +08:00
|
|
|
onApiKeySavingOk,
|
|
|
|
|
apiKeyVisible,
|
|
|
|
|
hideApiKeyModal,
|
|
|
|
|
showApiKeyModal: onShowApiKeyModal,
|
|
|
|
|
};
|
|
|
|
|
};
|
2024-03-15 19:35:59 +08:00
|
|
|
|
|
|
|
|
export const useSubmitSystemModelSetting = () => {
|
2024-07-25 18:53:10 +08:00
|
|
|
const { data: systemSetting } = useFetchTenantInfo();
|
2024-07-25 18:06:39 +08:00
|
|
|
const { saveTenantInfo: saveSystemModelSetting, loading } =
|
|
|
|
|
useSaveTenantInfo();
|
2024-03-15 19:35:59 +08:00
|
|
|
const {
|
|
|
|
|
visible: systemSettingVisible,
|
|
|
|
|
hideModal: hideSystemSettingModal,
|
|
|
|
|
showModal: showSystemSettingModal,
|
|
|
|
|
} = useSetModalState();
|
|
|
|
|
|
|
|
|
|
const onSystemSettingSavingOk = useCallback(
|
|
|
|
|
async (
|
|
|
|
|
payload: Omit<ISystemModelSettingSavingParams, 'tenant_id' | 'name'>,
|
|
|
|
|
) => {
|
|
|
|
|
const ret = await saveSystemModelSetting({
|
|
|
|
|
tenant_id: systemSetting.tenant_id,
|
|
|
|
|
name: systemSetting.name,
|
|
|
|
|
...payload,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (ret === 0) {
|
|
|
|
|
hideSystemSettingModal();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[hideSystemSettingModal, saveSystemModelSetting, systemSetting],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
saveSystemModelSettingLoading: loading,
|
|
|
|
|
onSystemSettingSavingOk,
|
|
|
|
|
systemSettingVisible,
|
|
|
|
|
hideSystemSettingModal,
|
|
|
|
|
showSystemSettingModal,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-25 18:53:10 +08:00
|
|
|
export const useFetchSystemModelSettingOnMount = () => {
|
|
|
|
|
const { data: systemSetting } = useFetchTenantInfo();
|
2024-03-18 16:45:01 +08:00
|
|
|
const allOptions = useSelectLlmOptionsByModelType();
|
|
|
|
|
|
|
|
|
|
return { systemSetting, allOptions };
|
2024-03-15 19:35:59 +08:00
|
|
|
};
|
2024-03-20 11:13:51 +08:00
|
|
|
|
2024-04-08 19:13:45 +08:00
|
|
|
export const useSubmitOllama = () => {
|
2024-04-11 18:17:45 +08:00
|
|
|
const [selectedLlmFactory, setSelectedLlmFactory] = useState<string>('');
|
2024-07-25 18:06:39 +08:00
|
|
|
const { addLlm, loading } = useAddLlm();
|
2024-04-08 19:13:45 +08:00
|
|
|
const {
|
|
|
|
|
visible: llmAddingVisible,
|
|
|
|
|
hideModal: hideLlmAddingModal,
|
|
|
|
|
showModal: showLlmAddingModal,
|
|
|
|
|
} = useSetModalState();
|
|
|
|
|
|
|
|
|
|
const onLlmAddingOk = useCallback(
|
|
|
|
|
async (payload: IAddLlmRequestBody) => {
|
|
|
|
|
const ret = await addLlm(payload);
|
|
|
|
|
if (ret === 0) {
|
|
|
|
|
hideLlmAddingModal();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[hideLlmAddingModal, addLlm],
|
|
|
|
|
);
|
|
|
|
|
|
2024-04-11 18:17:45 +08:00
|
|
|
const handleShowLlmAddingModal = (llmFactory: string) => {
|
|
|
|
|
setSelectedLlmFactory(llmFactory);
|
|
|
|
|
showLlmAddingModal();
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-08 19:13:45 +08:00
|
|
|
return {
|
|
|
|
|
llmAddingLoading: loading,
|
|
|
|
|
onLlmAddingOk,
|
|
|
|
|
llmAddingVisible,
|
|
|
|
|
hideLlmAddingModal,
|
2024-04-11 18:17:45 +08:00
|
|
|
showLlmAddingModal: handleShowLlmAddingModal,
|
|
|
|
|
selectedLlmFactory,
|
2024-04-08 19:13:45 +08:00
|
|
|
};
|
|
|
|
|
};
|
2024-05-10 10:38:39 +08:00
|
|
|
|
2024-05-23 11:15:29 +08:00
|
|
|
export const useSubmitVolcEngine = () => {
|
2024-07-25 18:06:39 +08:00
|
|
|
const { addLlm, loading } = useAddLlm();
|
2024-05-23 11:15:29 +08:00
|
|
|
const {
|
|
|
|
|
visible: volcAddingVisible,
|
|
|
|
|
hideModal: hideVolcAddingModal,
|
|
|
|
|
showModal: showVolcAddingModal,
|
|
|
|
|
} = useSetModalState();
|
|
|
|
|
|
|
|
|
|
const onVolcAddingOk = useCallback(
|
|
|
|
|
async (payload: IAddLlmRequestBody) => {
|
|
|
|
|
const ret = await addLlm(payload);
|
|
|
|
|
if (ret === 0) {
|
|
|
|
|
hideVolcAddingModal();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[hideVolcAddingModal, addLlm],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
volcAddingLoading: loading,
|
|
|
|
|
onVolcAddingOk,
|
|
|
|
|
volcAddingVisible,
|
|
|
|
|
hideVolcAddingModal,
|
2024-07-19 18:36:49 +08:00
|
|
|
showVolcAddingModal,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useSubmitBedrock = () => {
|
2024-07-25 18:06:39 +08:00
|
|
|
const { addLlm, loading } = useAddLlm();
|
2024-07-19 18:36:49 +08:00
|
|
|
const {
|
|
|
|
|
visible: bedrockAddingVisible,
|
|
|
|
|
hideModal: hideBedrockAddingModal,
|
|
|
|
|
showModal: showBedrockAddingModal,
|
|
|
|
|
} = useSetModalState();
|
|
|
|
|
|
|
|
|
|
const onBedrockAddingOk = useCallback(
|
|
|
|
|
async (payload: IAddLlmRequestBody) => {
|
|
|
|
|
const ret = await addLlm(payload);
|
|
|
|
|
if (ret === 0) {
|
|
|
|
|
hideBedrockAddingModal();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[hideBedrockAddingModal, addLlm],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
bedrockAddingLoading: loading,
|
|
|
|
|
onBedrockAddingOk,
|
|
|
|
|
bedrockAddingVisible,
|
|
|
|
|
hideBedrockAddingModal,
|
|
|
|
|
showBedrockAddingModal,
|
2024-05-23 11:15:29 +08:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-10 10:38:39 +08:00
|
|
|
export const useHandleDeleteLlm = (llmFactory: string) => {
|
2024-07-25 18:06:39 +08:00
|
|
|
const { deleteLlm } = useDeleteLlm();
|
2024-05-10 10:38:39 +08:00
|
|
|
const showDeleteConfirm = useShowDeleteConfirm();
|
|
|
|
|
|
|
|
|
|
const handleDeleteLlm = (name: string) => () => {
|
|
|
|
|
showDeleteConfirm({
|
|
|
|
|
onOk: async () => {
|
|
|
|
|
deleteLlm({ llm_factory: llmFactory, llm_name: name });
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return { handleDeleteLlm };
|
|
|
|
|
};
|