| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- import { useSetModalState, useShowDeleteConfirm } from '@/hooks/common-hooks';
- import {
- IApiKeySavingParams,
- ISystemModelSettingSavingParams,
- useAddLlm,
- useDeleteLlm,
- useSaveApiKey,
- useSaveTenantInfo,
- useSelectLlmOptionsByModelType,
- } from '@/hooks/llm-hooks';
- import { useFetchTenantInfo } from '@/hooks/user-setting-hooks';
- import { IAddLlmRequestBody } from '@/interfaces/request/llm';
- import { useCallback, useState } from 'react';
- import { ApiKeyPostBody } from '../interface';
-
- type SavingParamsState = Omit<IApiKeySavingParams, 'api_key'>;
-
- export const useSubmitApiKey = () => {
- const [savingParams, setSavingParams] = useState<SavingParamsState>(
- {} as SavingParamsState,
- );
- const { saveApiKey, loading } = useSaveApiKey();
- const {
- visible: apiKeyVisible,
- hideModal: hideApiKeyModal,
- showModal: showApiKeyModal,
- } = useSetModalState();
-
- const onApiKeySavingOk = useCallback(
- async (postBody: ApiKeyPostBody) => {
- const ret = await saveApiKey({
- ...savingParams,
- ...postBody,
- });
-
- if (ret === 0) {
- hideApiKeyModal();
- }
- },
- [hideApiKeyModal, saveApiKey, savingParams],
- );
-
- const onShowApiKeyModal = useCallback(
- (savingParams: SavingParamsState) => {
- setSavingParams(savingParams);
- showApiKeyModal();
- },
- [showApiKeyModal, setSavingParams],
- );
-
- return {
- saveApiKeyLoading: loading,
- initialApiKey: '',
- llmFactory: savingParams.llm_factory,
- onApiKeySavingOk,
- apiKeyVisible,
- hideApiKeyModal,
- showApiKeyModal: onShowApiKeyModal,
- };
- };
-
- export const useSubmitSystemModelSetting = () => {
- const { data: systemSetting } = useFetchTenantInfo();
- const { saveTenantInfo: saveSystemModelSetting, loading } =
- useSaveTenantInfo();
- 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,
- };
- };
-
- export const useFetchSystemModelSettingOnMount = () => {
- const { data: systemSetting } = useFetchTenantInfo();
- const allOptions = useSelectLlmOptionsByModelType();
-
- return { systemSetting, allOptions };
- };
-
- export const useSubmitOllama = () => {
- const [selectedLlmFactory, setSelectedLlmFactory] = useState<string>('');
- const { addLlm, loading } = useAddLlm();
- 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],
- );
-
- const handleShowLlmAddingModal = (llmFactory: string) => {
- setSelectedLlmFactory(llmFactory);
- showLlmAddingModal();
- };
-
- return {
- llmAddingLoading: loading,
- onLlmAddingOk,
- llmAddingVisible,
- hideLlmAddingModal,
- showLlmAddingModal: handleShowLlmAddingModal,
- selectedLlmFactory,
- };
- };
-
- export const useSubmitVolcEngine = () => {
- const { addLlm, loading } = useAddLlm();
- 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,
- showVolcAddingModal,
- };
- };
-
- export const useSubmitBedrock = () => {
- const { addLlm, loading } = useAddLlm();
- 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,
- };
- };
-
- export const useHandleDeleteLlm = (llmFactory: string) => {
- const { deleteLlm } = useDeleteLlm();
- const showDeleteConfirm = useShowDeleteConfirm();
-
- const handleDeleteLlm = (name: string) => () => {
- showDeleteConfirm({
- onOk: async () => {
- deleteLlm({ llm_factory: llmFactory, llm_name: name });
- },
- });
- };
-
- return { handleDeleteLlm };
- };
|