Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

app-info.tsx 15KB

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