Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

app-info.tsx 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. import { useTranslation } from 'react-i18next'
  2. import { useRouter } from 'next/navigation'
  3. import { useContext } from 'use-context-selector'
  4. import React, { useCallback, useState } from 'react'
  5. import {
  6. RiDeleteBinLine,
  7. RiEditLine,
  8. RiEqualizer2Line,
  9. RiExchange2Line,
  10. RiFileCopy2Line,
  11. RiFileDownloadLine,
  12. RiFileUploadLine,
  13. } from '@remixicon/react'
  14. import AppIcon from '../base/app-icon'
  15. import cn from '@/utils/classnames'
  16. import { useStore as useAppStore } from '@/app/components/app/store'
  17. import { ToastContext } from '@/app/components/base/toast'
  18. import { useAppContext } from '@/context/app-context'
  19. import { useProviderContext } from '@/context/provider-context'
  20. import { copyApp, deleteApp, exportAppConfig, updateAppInfo } from '@/service/apps'
  21. import type { DuplicateAppModalProps } from '@/app/components/app/duplicate-modal'
  22. import type { CreateAppModalProps } from '@/app/components/explore/create-app-modal'
  23. import { NEED_REFRESH_APP_LIST_KEY } from '@/config'
  24. import { getRedirection } from '@/utils/app-redirection'
  25. import type { EnvironmentVariable } from '@/app/components/workflow/types'
  26. import { fetchWorkflowDraft } from '@/service/workflow'
  27. import ContentDialog from '@/app/components/base/content-dialog'
  28. import Button from '@/app/components/base/button'
  29. import CardView from '@/app/(commonLayout)/app/(appDetailLayout)/[appId]/overview/cardView'
  30. import Divider from '../base/divider'
  31. import type { Operation } from './app-operations'
  32. import AppOperations from './app-operations'
  33. import dynamic from 'next/dynamic'
  34. const SwitchAppModal = dynamic(() => import('@/app/components/app/switch-app-modal'), {
  35. ssr: false,
  36. })
  37. const CreateAppModal = dynamic(() => import('@/app/components/explore/create-app-modal'), {
  38. ssr: false,
  39. })
  40. const DuplicateAppModal = dynamic(() => import('@/app/components/app/duplicate-modal'), {
  41. ssr: false,
  42. })
  43. const Confirm = dynamic(() => import('@/app/components/base/confirm'), {
  44. ssr: false,
  45. })
  46. const UpdateDSLModal = dynamic(() => import('@/app/components/workflow/update-dsl-modal'), {
  47. ssr: false,
  48. })
  49. const DSLExportConfirmModal = dynamic(() => import('@/app/components/workflow/dsl-export-confirm-modal'), {
  50. ssr: false,
  51. })
  52. export type IAppInfoProps = {
  53. expand: boolean
  54. onlyShowDetail?: boolean
  55. openState?: boolean
  56. onDetailExpand?: (expand: boolean) => void
  57. }
  58. const AppInfo = ({ expand, onlyShowDetail = false, openState = false, onDetailExpand }: IAppInfoProps) => {
  59. const { t } = useTranslation()
  60. const { notify } = useContext(ToastContext)
  61. const { replace } = useRouter()
  62. const { onPlanInfoChanged } = useProviderContext()
  63. const appDetail = useAppStore(state => state.appDetail)
  64. const setAppDetail = useAppStore(state => state.setAppDetail)
  65. const [open, setOpen] = useState(openState)
  66. const [showEditModal, setShowEditModal] = useState(false)
  67. const [showDuplicateModal, setShowDuplicateModal] = useState(false)
  68. const [showConfirmDelete, setShowConfirmDelete] = useState(false)
  69. const [showSwitchModal, setShowSwitchModal] = useState<boolean>(false)
  70. const [showImportDSLModal, setShowImportDSLModal] = useState<boolean>(false)
  71. const [secretEnvList, setSecretEnvList] = useState<EnvironmentVariable[]>([])
  72. const onEdit: CreateAppModalProps['onConfirm'] = useCallback(async ({
  73. name,
  74. icon_type,
  75. icon,
  76. icon_background,
  77. description,
  78. use_icon_as_answer_icon,
  79. max_active_requests,
  80. }) => {
  81. if (!appDetail)
  82. return
  83. try {
  84. const app = await updateAppInfo({
  85. appID: appDetail.id,
  86. name,
  87. icon_type,
  88. icon,
  89. icon_background,
  90. description,
  91. use_icon_as_answer_icon,
  92. max_active_requests,
  93. })
  94. setShowEditModal(false)
  95. notify({
  96. type: 'success',
  97. message: t('app.editDone'),
  98. })
  99. setAppDetail(app)
  100. }
  101. catch {
  102. notify({ type: 'error', message: t('app.editFailed') })
  103. }
  104. }, [appDetail, notify, setAppDetail, t])
  105. const onCopy: DuplicateAppModalProps['onConfirm'] = async ({ name, icon_type, icon, icon_background }) => {
  106. if (!appDetail)
  107. return
  108. try {
  109. const newApp = await copyApp({
  110. appID: appDetail.id,
  111. name,
  112. icon_type,
  113. icon,
  114. icon_background,
  115. mode: appDetail.mode,
  116. })
  117. setShowDuplicateModal(false)
  118. notify({
  119. type: 'success',
  120. message: t('app.newApp.appCreated'),
  121. })
  122. localStorage.setItem(NEED_REFRESH_APP_LIST_KEY, '1')
  123. onPlanInfoChanged()
  124. getRedirection(true, newApp, replace)
  125. }
  126. catch {
  127. notify({ type: 'error', message: t('app.newApp.appCreateFailed') })
  128. }
  129. }
  130. const onExport = async (include = false) => {
  131. if (!appDetail)
  132. return
  133. try {
  134. const { data } = await exportAppConfig({
  135. appID: appDetail.id,
  136. include,
  137. })
  138. const a = document.createElement('a')
  139. const file = new Blob([data], { type: 'application/yaml' })
  140. a.href = URL.createObjectURL(file)
  141. a.download = `${appDetail.name}.yml`
  142. a.click()
  143. }
  144. catch {
  145. notify({ type: 'error', message: t('app.exportFailed') })
  146. }
  147. }
  148. const exportCheck = async () => {
  149. if (!appDetail)
  150. return
  151. if (appDetail.mode !== 'workflow' && appDetail.mode !== 'advanced-chat') {
  152. onExport()
  153. return
  154. }
  155. try {
  156. const workflowDraft = await fetchWorkflowDraft(`/apps/${appDetail.id}/workflows/draft`)
  157. const list = (workflowDraft.environment_variables || []).filter(env => env.value_type === 'secret')
  158. if (list.length === 0) {
  159. onExport()
  160. return
  161. }
  162. setSecretEnvList(list)
  163. }
  164. catch {
  165. notify({ type: 'error', message: t('app.exportFailed') })
  166. }
  167. }
  168. const onConfirmDelete = useCallback(async () => {
  169. if (!appDetail)
  170. return
  171. try {
  172. await deleteApp(appDetail.id)
  173. notify({ type: 'success', message: t('app.appDeleted') })
  174. onPlanInfoChanged()
  175. setAppDetail()
  176. replace('/apps')
  177. }
  178. catch (e: any) {
  179. notify({
  180. type: 'error',
  181. message: `${t('app.appDeleteFailed')}${'message' in e ? `: ${e.message}` : ''}`,
  182. })
  183. }
  184. setShowConfirmDelete(false)
  185. }, [appDetail, notify, onPlanInfoChanged, replace, setAppDetail, t])
  186. const { isCurrentWorkspaceEditor } = useAppContext()
  187. if (!appDetail)
  188. return null
  189. const operations = [
  190. {
  191. id: 'edit',
  192. title: t('app.editApp'),
  193. icon: <RiEditLine />,
  194. onClick: () => {
  195. setOpen(false)
  196. onDetailExpand?.(false)
  197. setShowEditModal(true)
  198. },
  199. },
  200. {
  201. id: 'duplicate',
  202. title: t('app.duplicate'),
  203. icon: <RiFileCopy2Line />,
  204. onClick: () => {
  205. setOpen(false)
  206. onDetailExpand?.(false)
  207. setShowDuplicateModal(true)
  208. },
  209. },
  210. {
  211. id: 'export',
  212. title: t('app.export'),
  213. icon: <RiFileDownloadLine />,
  214. onClick: exportCheck,
  215. },
  216. (appDetail.mode !== 'agent-chat' && (appDetail.mode === 'advanced-chat' || appDetail.mode === 'workflow')) ? {
  217. id: 'import',
  218. title: t('workflow.common.importDSL'),
  219. icon: <RiFileUploadLine />,
  220. onClick: () => {
  221. setOpen(false)
  222. onDetailExpand?.(false)
  223. setShowImportDSLModal(true)
  224. },
  225. } : undefined,
  226. (appDetail.mode !== 'agent-chat' && (appDetail.mode === 'completion' || appDetail.mode === 'chat')) ? {
  227. id: 'switch',
  228. title: t('app.switch'),
  229. icon: <RiExchange2Line />,
  230. onClick: () => {
  231. setOpen(false)
  232. onDetailExpand?.(false)
  233. setShowSwitchModal(true)
  234. },
  235. } : undefined,
  236. ].filter((op): op is Operation => Boolean(op))
  237. return (
  238. <div>
  239. {!onlyShowDetail && (
  240. <button
  241. onClick={() => {
  242. if (isCurrentWorkspaceEditor)
  243. setOpen(v => !v)
  244. }}
  245. className='block w-full'
  246. >
  247. <div className={cn('flex rounded-lg', expand ? 'flex-col gap-2 p-2 pb-2.5' : 'items-start justify-center gap-1 p-1', open && 'bg-state-base-hover', isCurrentWorkspaceEditor && 'cursor-pointer hover:bg-state-base-hover')}>
  248. <div className={`flex items-center self-stretch ${expand ? 'justify-between' : 'flex-col gap-1'}`}>
  249. <AppIcon
  250. size={expand ? 'large' : 'small'}
  251. iconType={appDetail.icon_type}
  252. icon={appDetail.icon}
  253. background={appDetail.icon_background}
  254. imageUrl={appDetail.icon_url}
  255. />
  256. <div className='flex items-center justify-center rounded-md p-0.5'>
  257. <div className='flex h-5 w-5 items-center justify-center'>
  258. <RiEqualizer2Line className='h-4 w-4 text-text-tertiary' />
  259. </div>
  260. </div>
  261. </div>
  262. {
  263. expand && (
  264. <div className='flex flex-col items-start gap-1'>
  265. <div className='flex w-full'>
  266. <div className='system-md-semibold truncate text-text-secondary'>{appDetail.name}</div>
  267. </div>
  268. <div className='system-2xs-medium-uppercase text-text-tertiary'>{appDetail.mode === 'advanced-chat' ? t('app.types.advanced') : appDetail.mode === 'agent-chat' ? t('app.types.agent') : appDetail.mode === 'chat' ? t('app.types.chatbot') : appDetail.mode === 'completion' ? t('app.types.completion') : t('app.types.workflow')}</div>
  269. </div>
  270. )
  271. }
  272. </div>
  273. </button>
  274. )}
  275. <ContentDialog
  276. show={onlyShowDetail ? openState : open}
  277. onClose={() => {
  278. setOpen(false)
  279. onDetailExpand?.(false)
  280. }}
  281. className='absolute bottom-2 left-2 top-2 flex w-[420px] flex-col rounded-2xl !p-0'
  282. >
  283. <div className='flex shrink-0 flex-col items-start justify-center gap-3 self-stretch p-4'>
  284. <div className='flex items-center gap-3 self-stretch'>
  285. <AppIcon
  286. size="large"
  287. iconType={appDetail.icon_type}
  288. icon={appDetail.icon}
  289. background={appDetail.icon_background}
  290. imageUrl={appDetail.icon_url}
  291. />
  292. <div className='flex w-full grow flex-col items-start justify-center'>
  293. <div className='system-md-semibold w-full truncate text-text-secondary'>{appDetail.name}</div>
  294. <div className='system-2xs-medium-uppercase text-text-tertiary'>{appDetail.mode === 'advanced-chat' ? t('app.types.advanced') : appDetail.mode === 'agent-chat' ? t('app.types.agent') : appDetail.mode === 'chat' ? t('app.types.chatbot') : appDetail.mode === 'completion' ? t('app.types.completion') : t('app.types.workflow')}</div>
  295. </div>
  296. </div>
  297. {/* description */}
  298. {appDetail.description && (
  299. <div className='system-xs-regular overflow-wrap-anywhere max-h-[105px] w-full max-w-full overflow-y-auto whitespace-normal break-words text-text-tertiary'>{appDetail.description}</div>
  300. )}
  301. {/* operations */}
  302. <AppOperations
  303. gap={4}
  304. operations={operations}
  305. />
  306. </div>
  307. <CardView
  308. appId={appDetail.id}
  309. isInPanel={true}
  310. className='flex flex-1 flex-col gap-2 overflow-auto px-2 py-1'
  311. />
  312. <Divider />
  313. <div className='flex min-h-fit shrink-0 flex-col items-start justify-center gap-3 self-stretch border-t-[0.5px] border-divider-subtle p-2'>
  314. <Button
  315. size={'medium'}
  316. variant={'ghost'}
  317. className='gap-0.5'
  318. onClick={() => {
  319. setOpen(false)
  320. onDetailExpand?.(false)
  321. setShowConfirmDelete(true)
  322. }}
  323. >
  324. <RiDeleteBinLine className='h-4 w-4 text-text-tertiary' />
  325. <span className='system-sm-medium text-text-tertiary'>{t('common.operation.deleteApp')}</span>
  326. </Button>
  327. </div>
  328. </ContentDialog>
  329. {showSwitchModal && (
  330. <SwitchAppModal
  331. inAppDetail
  332. show={showSwitchModal}
  333. appDetail={appDetail}
  334. onClose={() => setShowSwitchModal(false)}
  335. onSuccess={() => setShowSwitchModal(false)}
  336. />
  337. )}
  338. {showEditModal && (
  339. <CreateAppModal
  340. isEditModal
  341. appName={appDetail.name}
  342. appIconType={appDetail.icon_type}
  343. appIcon={appDetail.icon}
  344. appIconBackground={appDetail.icon_background}
  345. appIconUrl={appDetail.icon_url}
  346. appDescription={appDetail.description}
  347. appMode={appDetail.mode}
  348. appUseIconAsAnswerIcon={appDetail.use_icon_as_answer_icon}
  349. max_active_requests={appDetail.max_active_requests ?? null}
  350. show={showEditModal}
  351. onConfirm={onEdit}
  352. onHide={() => setShowEditModal(false)}
  353. />
  354. )}
  355. {showDuplicateModal && (
  356. <DuplicateAppModal
  357. appName={appDetail.name}
  358. icon_type={appDetail.icon_type}
  359. icon={appDetail.icon}
  360. icon_background={appDetail.icon_background}
  361. icon_url={appDetail.icon_url}
  362. show={showDuplicateModal}
  363. onConfirm={onCopy}
  364. onHide={() => setShowDuplicateModal(false)}
  365. />
  366. )}
  367. {showConfirmDelete && (
  368. <Confirm
  369. title={t('app.deleteAppConfirmTitle')}
  370. content={t('app.deleteAppConfirmContent')}
  371. isShow={showConfirmDelete}
  372. onConfirm={onConfirmDelete}
  373. onCancel={() => setShowConfirmDelete(false)}
  374. />
  375. )}
  376. {showImportDSLModal && (
  377. <UpdateDSLModal
  378. onCancel={() => setShowImportDSLModal(false)}
  379. onBackup={exportCheck}
  380. />
  381. )}
  382. {secretEnvList.length > 0 && (
  383. <DSLExportConfirmModal
  384. envList={secretEnvList}
  385. onConfirm={onExport}
  386. onClose={() => setSecretEnvList([])}
  387. />
  388. )}
  389. </div>
  390. )
  391. }
  392. export default React.memo(AppInfo)