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.

index.tsx 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import type { FC } from 'react'
  2. import {
  3. memo,
  4. useCallback,
  5. useMemo,
  6. useRef,
  7. } from 'react'
  8. import { RiCloseLine } from '@remixicon/react'
  9. import { useTranslation } from 'react-i18next'
  10. import type {
  11. CustomConfigurationModelFixedFields,
  12. ModelProvider,
  13. } from '../declarations'
  14. import {
  15. ConfigurationMethodEnum,
  16. FormTypeEnum,
  17. } from '../declarations'
  18. import {
  19. useLanguage,
  20. } from '../hooks'
  21. import Button from '@/app/components/base/button'
  22. import { Lock01 } from '@/app/components/base/icons/src/vender/solid/security'
  23. import { LinkExternal02 } from '@/app/components/base/icons/src/vender/line/general'
  24. import {
  25. PortalToFollowElem,
  26. PortalToFollowElemContent,
  27. } from '@/app/components/base/portal-to-follow-elem'
  28. import Confirm from '@/app/components/base/confirm'
  29. import { useAppContext } from '@/context/app-context'
  30. import AuthForm from '@/app/components/base/form/form-scenarios/auth'
  31. import type {
  32. FormRefObject,
  33. FormSchema,
  34. } from '@/app/components/base/form/types'
  35. import { useModelFormSchemas } from '../model-auth/hooks'
  36. import type {
  37. Credential,
  38. CustomModel,
  39. } from '../declarations'
  40. import Loading from '@/app/components/base/loading'
  41. import {
  42. useAuth,
  43. useCredentialData,
  44. } from '@/app/components/header/account-setting/model-provider-page/model-auth/hooks'
  45. import ModelIcon from '@/app/components/header/account-setting/model-provider-page/model-icon'
  46. import Badge from '@/app/components/base/badge'
  47. import { useRenderI18nObject } from '@/hooks/use-i18n'
  48. type ModelModalProps = {
  49. provider: ModelProvider
  50. configurateMethod: ConfigurationMethodEnum
  51. currentCustomConfigurationModelFixedFields?: CustomConfigurationModelFixedFields
  52. onCancel: () => void
  53. onSave: () => void
  54. model?: CustomModel
  55. credential?: Credential
  56. isModelCredential?: boolean
  57. }
  58. const ModelModal: FC<ModelModalProps> = ({
  59. provider,
  60. configurateMethod,
  61. currentCustomConfigurationModelFixedFields,
  62. onCancel,
  63. onSave,
  64. model,
  65. credential,
  66. isModelCredential,
  67. }) => {
  68. const renderI18nObject = useRenderI18nObject()
  69. const providerFormSchemaPredefined = configurateMethod === ConfigurationMethodEnum.predefinedModel
  70. const {
  71. isLoading,
  72. credentialData,
  73. } = useCredentialData(provider, providerFormSchemaPredefined, isModelCredential, credential, model)
  74. const {
  75. handleSaveCredential,
  76. handleConfirmDelete,
  77. deleteCredentialId,
  78. closeConfirmDelete,
  79. openConfirmDelete,
  80. doingAction,
  81. } = useAuth(provider, configurateMethod, currentCustomConfigurationModelFixedFields, isModelCredential, onSave)
  82. const {
  83. credentials: formSchemasValue,
  84. } = credentialData as any
  85. const { isCurrentWorkspaceManager } = useAppContext()
  86. const isEditMode = !!formSchemasValue && isCurrentWorkspaceManager
  87. const { t } = useTranslation()
  88. const language = useLanguage()
  89. const {
  90. formSchemas,
  91. formValues,
  92. } = useModelFormSchemas(provider, providerFormSchemaPredefined, formSchemasValue, credential, model)
  93. const formRef = useRef<FormRefObject>(null)
  94. const handleSave = useCallback(async () => {
  95. const {
  96. isCheckValidated,
  97. values,
  98. } = formRef.current?.getFormValues({
  99. needCheckValidatedValues: true,
  100. needTransformWhenSecretFieldIsPristine: true,
  101. }) || { isCheckValidated: false, values: {} }
  102. if (!isCheckValidated)
  103. return
  104. const {
  105. __authorization_name__,
  106. __model_name,
  107. __model_type,
  108. ...rest
  109. } = values
  110. if (__model_name && __model_type) {
  111. handleSaveCredential({
  112. credential_id: credential?.credential_id,
  113. credentials: rest,
  114. name: __authorization_name__,
  115. model: __model_name,
  116. model_type: __model_type,
  117. })
  118. }
  119. else {
  120. handleSaveCredential({
  121. credential_id: credential?.credential_id,
  122. credentials: rest,
  123. name: __authorization_name__,
  124. })
  125. }
  126. }, [handleSaveCredential, credential?.credential_id, model])
  127. const modalTitle = useMemo(() => {
  128. if (!providerFormSchemaPredefined && !model) {
  129. return (
  130. <div className='flex items-center'>
  131. <ModelIcon
  132. className='mr-2 h-10 w-10 shrink-0'
  133. iconClassName='h-10 w-10'
  134. provider={provider}
  135. />
  136. <div>
  137. <div className='system-xs-medium-uppercase text-text-tertiary'>{t('common.modelProvider.auth.apiKeyModal.addModel')}</div>
  138. <div className='system-md-semibold text-text-primary'>{renderI18nObject(provider.label)}</div>
  139. </div>
  140. </div>
  141. )
  142. }
  143. let label = t('common.modelProvider.auth.apiKeyModal.title')
  144. if (model)
  145. label = t('common.modelProvider.auth.addModelCredential')
  146. return (
  147. <div className='title-2xl-semi-bold text-text-primary'>
  148. {label}
  149. </div>
  150. )
  151. }, [providerFormSchemaPredefined, t, model, renderI18nObject])
  152. const modalDesc = useMemo(() => {
  153. if (providerFormSchemaPredefined) {
  154. return (
  155. <div className='system-xs-regular mt-1 text-text-tertiary'>
  156. {t('common.modelProvider.auth.apiKeyModal.desc')}
  157. </div>
  158. )
  159. }
  160. return null
  161. }, [providerFormSchemaPredefined, t])
  162. const modalModel = useMemo(() => {
  163. if (model) {
  164. return (
  165. <div className='mt-2 flex items-center'>
  166. <ModelIcon
  167. className='mr-2 h-4 w-4 shrink-0'
  168. provider={provider}
  169. modelName={model.model}
  170. />
  171. <div className='system-md-regular mr-1 text-text-secondary'>{model.model}</div>
  172. <Badge>{model.model_type}</Badge>
  173. </div>
  174. )
  175. }
  176. return null
  177. }, [model, provider])
  178. return (
  179. <PortalToFollowElem open>
  180. <PortalToFollowElemContent className='z-[60] h-full w-full'>
  181. <div className='fixed inset-0 flex items-center justify-center bg-black/[.25]'>
  182. <div className='relative w-[640px] rounded-2xl bg-components-panel-bg shadow-xl'>
  183. <div
  184. className='absolute right-5 top-5 flex h-8 w-8 cursor-pointer items-center justify-center'
  185. onClick={onCancel}
  186. >
  187. <RiCloseLine className='h-4 w-4 text-text-tertiary' />
  188. </div>
  189. <div className='px-6 pt-6'>
  190. <div className='pb-3'>
  191. {modalTitle}
  192. {modalDesc}
  193. {modalModel}
  194. </div>
  195. <div className='max-h-[calc(100vh-320px)] overflow-y-auto'>
  196. {
  197. isLoading && (
  198. <div className='flex items-center justify-center'>
  199. <Loading />
  200. </div>
  201. )
  202. }
  203. {
  204. !isLoading && (
  205. <AuthForm
  206. formSchemas={formSchemas.map((formSchema) => {
  207. return {
  208. ...formSchema,
  209. name: formSchema.variable,
  210. showRadioUI: formSchema.type === FormTypeEnum.radio,
  211. }
  212. }) as FormSchema[]}
  213. defaultValues={formValues}
  214. inputClassName='justify-start'
  215. ref={formRef}
  216. />
  217. )
  218. }
  219. </div>
  220. <div className='sticky bottom-0 -mx-2 mt-2 flex flex-wrap items-center justify-between gap-y-2 bg-components-panel-bg px-2 pb-6 pt-4'>
  221. {
  222. (provider.help && (provider.help.title || provider.help.url))
  223. ? (
  224. <a
  225. href={provider.help?.url[language] || provider.help?.url.en_US}
  226. target='_blank' rel='noopener noreferrer'
  227. className='inline-flex items-center text-xs text-primary-600'
  228. onClick={e => !provider.help.url && e.preventDefault()}
  229. >
  230. {provider.help.title?.[language] || provider.help.url[language] || provider.help.title?.en_US || provider.help.url.en_US}
  231. <LinkExternal02 className='ml-1 h-3 w-3' />
  232. </a>
  233. )
  234. : <div />
  235. }
  236. <div>
  237. {
  238. isEditMode && (
  239. <Button
  240. variant='warning'
  241. size='large'
  242. className='mr-2'
  243. onClick={() => openConfirmDelete(credential, model)}
  244. >
  245. {t('common.operation.remove')}
  246. </Button>
  247. )
  248. }
  249. <Button
  250. size='large'
  251. className='mr-2'
  252. onClick={onCancel}
  253. >
  254. {t('common.operation.cancel')}
  255. </Button>
  256. <Button
  257. size='large'
  258. variant='primary'
  259. onClick={handleSave}
  260. disabled={isLoading || doingAction}
  261. >
  262. {t('common.operation.save')}
  263. </Button>
  264. </div>
  265. </div>
  266. </div>
  267. <div className='border-t-[0.5px] border-t-divider-regular'>
  268. <div className='flex items-center justify-center rounded-b-2xl bg-background-section-burn py-3 text-xs text-text-tertiary'>
  269. <Lock01 className='mr-1 h-3 w-3 text-text-tertiary' />
  270. {t('common.modelProvider.encrypted.front')}
  271. <a
  272. className='mx-1 text-text-accent'
  273. target='_blank' rel='noopener noreferrer'
  274. href='https://pycryptodome.readthedocs.io/en/latest/src/cipher/oaep.html'
  275. >
  276. PKCS1_OAEP
  277. </a>
  278. {t('common.modelProvider.encrypted.back')}
  279. </div>
  280. </div>
  281. </div>
  282. {
  283. deleteCredentialId && (
  284. <Confirm
  285. isShow
  286. title={t('common.modelProvider.confirmDelete')}
  287. isDisabled={doingAction}
  288. onCancel={closeConfirmDelete}
  289. onConfirm={handleConfirmDelete}
  290. />
  291. )
  292. }
  293. </div>
  294. </PortalToFollowElemContent>
  295. </PortalToFollowElem>
  296. )
  297. }
  298. export default memo(ModelModal)