Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

app-info.tsx 14KB

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