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.1KB

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