credential-panel.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import type { FC } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import type { ModelProvider } from '../declarations'
  4. import {
  5. ConfigurationMethodEnum,
  6. CustomConfigurationStatusEnum,
  7. PreferredProviderTypeEnum,
  8. } from '../declarations'
  9. import {
  10. useUpdateModelList,
  11. useUpdateModelProviders,
  12. } from '../hooks'
  13. import PrioritySelector from './priority-selector'
  14. import PriorityUseTip from './priority-use-tip'
  15. import { UPDATE_MODEL_PROVIDER_CUSTOM_MODEL_LIST } from './index'
  16. import Indicator from '@/app/components/header/indicator'
  17. import { Settings01 } from '@/app/components/base/icons/src/vender/line/general'
  18. import Button from '@/app/components/base/button'
  19. import { changeModelProviderPriority } from '@/service/common'
  20. import { useToastContext } from '@/app/components/base/toast'
  21. import { useEventEmitterContextContext } from '@/context/event-emitter'
  22. type CredentialPanelProps = {
  23. provider: ModelProvider
  24. onSetup: () => void
  25. }
  26. const CredentialPanel: FC<CredentialPanelProps> = ({
  27. provider,
  28. onSetup,
  29. }) => {
  30. const { t } = useTranslation()
  31. const { notify } = useToastContext()
  32. const { eventEmitter } = useEventEmitterContextContext()
  33. const updateModelList = useUpdateModelList()
  34. const updateModelProviders = useUpdateModelProviders()
  35. const customConfig = provider.custom_configuration
  36. const systemConfig = provider.system_configuration
  37. const priorityUseType = provider.preferred_provider_type
  38. const isCustomConfigured = customConfig.status === CustomConfigurationStatusEnum.active
  39. const configurateMethods = provider.configurate_methods
  40. const handleChangePriority = async (key: PreferredProviderTypeEnum) => {
  41. const res = await changeModelProviderPriority({
  42. url: `/workspaces/current/model-providers/${provider.provider}/preferred-provider-type`,
  43. body: {
  44. preferred_provider_type: key,
  45. },
  46. })
  47. if (res.result === 'success') {
  48. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  49. updateModelProviders()
  50. configurateMethods.forEach((method) => {
  51. if (method === ConfigurationMethodEnum.predefinedModel)
  52. provider.supported_model_types.forEach(modelType => updateModelList(modelType))
  53. })
  54. eventEmitter?.emit({
  55. type: UPDATE_MODEL_PROVIDER_CUSTOM_MODEL_LIST,
  56. payload: provider.provider,
  57. } as any)
  58. }
  59. }
  60. return (
  61. <>
  62. {
  63. provider.provider_credential_schema && (
  64. <div className='shrink-0 relative ml-1 p-1 w-[112px] rounded-lg bg-white/[0.3] border-[0.5px] border-black/5'>
  65. <div className='flex items-center justify-between mb-1 pt-1 pl-2 pr-[7px] h-5 text-xs font-medium text-gray-500'>
  66. API-KEY
  67. <Indicator color={isCustomConfigured ? 'green' : 'gray'} />
  68. </div>
  69. <div className='flex items-center gap-0.5'>
  70. <Button
  71. className='grow'
  72. size='small'
  73. onClick={onSetup}
  74. >
  75. <Settings01 className='mr-1 w-3 h-3' />
  76. {t('common.operation.setup')}
  77. </Button>
  78. {
  79. systemConfig.enabled && isCustomConfigured && (
  80. <PrioritySelector
  81. value={priorityUseType}
  82. onSelect={handleChangePriority}
  83. />
  84. )
  85. }
  86. </div>
  87. {
  88. priorityUseType === PreferredProviderTypeEnum.custom && systemConfig.enabled && (
  89. <PriorityUseTip />
  90. )
  91. }
  92. </div>
  93. )
  94. }
  95. {
  96. systemConfig.enabled && isCustomConfigured && !provider.provider_credential_schema && (
  97. <div className='ml-1'>
  98. <PrioritySelector
  99. value={priorityUseType}
  100. onSelect={handleChangePriority}
  101. />
  102. </div>
  103. )
  104. }
  105. </>
  106. )
  107. }
  108. export default CredentialPanel