You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hooks.ts 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { useSetModalState, useShowDeleteConfirm } from '@/hooks/commonHooks';
  2. import {
  3. IApiKeySavingParams,
  4. ISystemModelSettingSavingParams,
  5. useAddLlm,
  6. useDeleteLlm,
  7. useFetchLlmList,
  8. useSaveApiKey,
  9. useSaveTenantInfo,
  10. useSelectLlmOptionsByModelType,
  11. } from '@/hooks/llmHooks';
  12. import { useOneNamespaceEffectsLoading } from '@/hooks/storeHooks';
  13. import {
  14. useFetchTenantInfo,
  15. useSelectTenantInfo,
  16. } from '@/hooks/userSettingHook';
  17. import { IAddLlmRequestBody } from '@/interfaces/request/llm';
  18. import { useCallback, useEffect, useState } from 'react';
  19. import { ApiKeyPostBody } from '../interface';
  20. type SavingParamsState = Omit<IApiKeySavingParams, 'api_key'>;
  21. export const useSubmitApiKey = () => {
  22. const [savingParams, setSavingParams] = useState<SavingParamsState>(
  23. {} as SavingParamsState,
  24. );
  25. const saveApiKey = useSaveApiKey();
  26. const {
  27. visible: apiKeyVisible,
  28. hideModal: hideApiKeyModal,
  29. showModal: showApiKeyModal,
  30. } = useSetModalState();
  31. const onApiKeySavingOk = useCallback(
  32. async (postBody: ApiKeyPostBody) => {
  33. const ret = await saveApiKey({
  34. ...savingParams,
  35. ...postBody,
  36. });
  37. if (ret === 0) {
  38. hideApiKeyModal();
  39. }
  40. },
  41. [hideApiKeyModal, saveApiKey, savingParams],
  42. );
  43. const onShowApiKeyModal = useCallback(
  44. (savingParams: SavingParamsState) => {
  45. setSavingParams(savingParams);
  46. showApiKeyModal();
  47. },
  48. [showApiKeyModal, setSavingParams],
  49. );
  50. const loading = useOneNamespaceEffectsLoading('settingModel', [
  51. 'set_api_key',
  52. ]);
  53. return {
  54. saveApiKeyLoading: loading,
  55. initialApiKey: '',
  56. llmFactory: savingParams.llm_factory,
  57. onApiKeySavingOk,
  58. apiKeyVisible,
  59. hideApiKeyModal,
  60. showApiKeyModal: onShowApiKeyModal,
  61. };
  62. };
  63. export const useSubmitSystemModelSetting = () => {
  64. const systemSetting = useSelectTenantInfo();
  65. const loading = useOneNamespaceEffectsLoading('settingModel', [
  66. 'set_tenant_info',
  67. ]);
  68. const saveSystemModelSetting = useSaveTenantInfo();
  69. const {
  70. visible: systemSettingVisible,
  71. hideModal: hideSystemSettingModal,
  72. showModal: showSystemSettingModal,
  73. } = useSetModalState();
  74. const onSystemSettingSavingOk = useCallback(
  75. async (
  76. payload: Omit<ISystemModelSettingSavingParams, 'tenant_id' | 'name'>,
  77. ) => {
  78. const ret = await saveSystemModelSetting({
  79. tenant_id: systemSetting.tenant_id,
  80. name: systemSetting.name,
  81. ...payload,
  82. });
  83. if (ret === 0) {
  84. hideSystemSettingModal();
  85. }
  86. },
  87. [hideSystemSettingModal, saveSystemModelSetting, systemSetting],
  88. );
  89. return {
  90. saveSystemModelSettingLoading: loading,
  91. onSystemSettingSavingOk,
  92. systemSettingVisible,
  93. hideSystemSettingModal,
  94. showSystemSettingModal,
  95. };
  96. };
  97. export const useFetchSystemModelSettingOnMount = (visible: boolean) => {
  98. const systemSetting = useSelectTenantInfo();
  99. const allOptions = useSelectLlmOptionsByModelType();
  100. const fetchLlmList = useFetchLlmList();
  101. const fetchTenantInfo = useFetchTenantInfo();
  102. useEffect(() => {
  103. if (visible) {
  104. fetchLlmList();
  105. fetchTenantInfo();
  106. }
  107. }, [fetchLlmList, fetchTenantInfo, visible]);
  108. return { systemSetting, allOptions };
  109. };
  110. export const useSelectModelProvidersLoading = () => {
  111. const loading = useOneNamespaceEffectsLoading('settingModel', [
  112. 'my_llm',
  113. 'factories_list',
  114. ]);
  115. return loading;
  116. };
  117. export const useSubmitOllama = () => {
  118. const loading = useOneNamespaceEffectsLoading('settingModel', ['add_llm']);
  119. const [selectedLlmFactory, setSelectedLlmFactory] = useState<string>('');
  120. const addLlm = useAddLlm();
  121. const {
  122. visible: llmAddingVisible,
  123. hideModal: hideLlmAddingModal,
  124. showModal: showLlmAddingModal,
  125. } = useSetModalState();
  126. const onLlmAddingOk = useCallback(
  127. async (payload: IAddLlmRequestBody) => {
  128. const ret = await addLlm(payload);
  129. if (ret === 0) {
  130. hideLlmAddingModal();
  131. }
  132. },
  133. [hideLlmAddingModal, addLlm],
  134. );
  135. const handleShowLlmAddingModal = (llmFactory: string) => {
  136. setSelectedLlmFactory(llmFactory);
  137. showLlmAddingModal();
  138. };
  139. return {
  140. llmAddingLoading: loading,
  141. onLlmAddingOk,
  142. llmAddingVisible,
  143. hideLlmAddingModal,
  144. showLlmAddingModal: handleShowLlmAddingModal,
  145. selectedLlmFactory,
  146. };
  147. };
  148. export const useSubmitVolcEngine = () => {
  149. const loading = useOneNamespaceEffectsLoading('settingModel', ['add_llm']);
  150. const [selectedVolcFactory, setSelectedVolcFactory] = useState<string>('');
  151. const addLlm = useAddLlm();
  152. const {
  153. visible: volcAddingVisible,
  154. hideModal: hideVolcAddingModal,
  155. showModal: showVolcAddingModal,
  156. } = useSetModalState();
  157. const onVolcAddingOk = useCallback(
  158. async (payload: IAddLlmRequestBody) => {
  159. const ret = await addLlm(payload);
  160. if (ret === 0) {
  161. hideVolcAddingModal();
  162. }
  163. },
  164. [hideVolcAddingModal, addLlm],
  165. );
  166. const handleShowVolcAddingModal = (llmFactory: string) => {
  167. setSelectedVolcFactory(llmFactory);
  168. showVolcAddingModal();
  169. };
  170. return {
  171. volcAddingLoading: loading,
  172. onVolcAddingOk,
  173. volcAddingVisible,
  174. hideVolcAddingModal,
  175. showVolcAddingModal: handleShowVolcAddingModal,
  176. selectedVolcFactory,
  177. };
  178. };
  179. export const useHandleDeleteLlm = (llmFactory: string) => {
  180. const deleteLlm = useDeleteLlm();
  181. const showDeleteConfirm = useShowDeleteConfirm();
  182. const handleDeleteLlm = (name: string) => () => {
  183. showDeleteConfirm({
  184. onOk: async () => {
  185. deleteLlm({ llm_factory: llmFactory, llm_name: name });
  186. },
  187. });
  188. };
  189. return { handleDeleteLlm };
  190. };