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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. 'use client'
  2. import { useCallback, useEffect, useRef, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useRouter } from 'next/navigation'
  5. import { useContext } from 'use-context-selector'
  6. import { RiArrowRightLine, RiArrowRightSLine, RiCommandLine, RiCornerDownLeftLine, RiExchange2Fill } from '@remixicon/react'
  7. import Link from 'next/link'
  8. import { useDebounceFn, useKeyPress } from 'ahooks'
  9. import Image from 'next/image'
  10. import AppIconPicker from '../../base/app-icon-picker'
  11. import type { AppIconSelection } from '../../base/app-icon-picker'
  12. import Button from '@/app/components/base/button'
  13. import Divider from '@/app/components/base/divider'
  14. import cn from '@/utils/classnames'
  15. import { basePath } from '@/utils/var'
  16. import { useAppContext } from '@/context/app-context'
  17. import { useProviderContext } from '@/context/provider-context'
  18. import { ToastContext } from '@/app/components/base/toast'
  19. import type { AppMode } from '@/types/app'
  20. import { createApp } from '@/service/apps'
  21. import Input from '@/app/components/base/input'
  22. import Textarea from '@/app/components/base/textarea'
  23. import AppIcon from '@/app/components/base/app-icon'
  24. import AppsFull from '@/app/components/billing/apps-full-in-dialog'
  25. import { BubbleTextMod, ChatBot, ListSparkle, Logic } from '@/app/components/base/icons/src/vender/solid/communication'
  26. import { NEED_REFRESH_APP_LIST_KEY } from '@/config'
  27. import { getRedirection } from '@/utils/app-redirection'
  28. import FullScreenModal from '@/app/components/base/fullscreen-modal'
  29. import useTheme from '@/hooks/use-theme'
  30. import { useDocLink } from '@/context/i18n'
  31. type CreateAppProps = {
  32. onSuccess: () => void
  33. onClose: () => void
  34. onCreateFromTemplate?: () => void
  35. defaultAppMode?: AppMode
  36. }
  37. function CreateApp({ onClose, onSuccess, onCreateFromTemplate, defaultAppMode }: CreateAppProps) {
  38. const { t } = useTranslation()
  39. const { push } = useRouter()
  40. const { notify } = useContext(ToastContext)
  41. const [appMode, setAppMode] = useState<AppMode>(defaultAppMode || 'advanced-chat')
  42. const [appIcon, setAppIcon] = useState<AppIconSelection>({ type: 'emoji', icon: '🤖', background: '#FFEAD5' })
  43. const [showAppIconPicker, setShowAppIconPicker] = useState(false)
  44. const [name, setName] = useState('')
  45. const [description, setDescription] = useState('')
  46. const [isAppTypeExpanded, setIsAppTypeExpanded] = useState(false)
  47. const { plan, enableBilling } = useProviderContext()
  48. const isAppsFull = (enableBilling && plan.usage.buildApps >= plan.total.buildApps)
  49. const { isCurrentWorkspaceEditor } = useAppContext()
  50. const isCreatingRef = useRef(false)
  51. useEffect(() => {
  52. if (appMode === 'chat' || appMode === 'agent-chat' || appMode === 'completion')
  53. setIsAppTypeExpanded(true)
  54. }, [appMode])
  55. const onCreate = useCallback(async () => {
  56. if (!appMode) {
  57. notify({ type: 'error', message: t('app.newApp.appTypeRequired') })
  58. return
  59. }
  60. if (!name.trim()) {
  61. notify({ type: 'error', message: t('app.newApp.nameNotEmpty') })
  62. return
  63. }
  64. if (isCreatingRef.current)
  65. return
  66. isCreatingRef.current = true
  67. try {
  68. const app = await createApp({
  69. name,
  70. description,
  71. icon_type: appIcon.type,
  72. icon: appIcon.type === 'emoji' ? appIcon.icon : appIcon.fileId,
  73. icon_background: appIcon.type === 'emoji' ? appIcon.background : undefined,
  74. mode: appMode,
  75. })
  76. notify({ type: 'success', message: t('app.newApp.appCreated') })
  77. onSuccess()
  78. onClose()
  79. localStorage.setItem(NEED_REFRESH_APP_LIST_KEY, '1')
  80. getRedirection(isCurrentWorkspaceEditor, app, push)
  81. }
  82. catch (e: any) {
  83. notify({
  84. type: 'error',
  85. message: e.message || t('app.newApp.appCreateFailed'),
  86. })
  87. }
  88. isCreatingRef.current = false
  89. }, [name, notify, t, appMode, appIcon, description, onSuccess, onClose, push, isCurrentWorkspaceEditor])
  90. const { run: handleCreateApp } = useDebounceFn(onCreate, { wait: 300 })
  91. useKeyPress(['meta.enter', 'ctrl.enter'], () => {
  92. if (isAppsFull)
  93. return
  94. handleCreateApp()
  95. })
  96. return <>
  97. <div className='flex h-full justify-center overflow-y-auto overflow-x-hidden'>
  98. <div className='flex flex-1 shrink-0 justify-end'>
  99. <div className='px-10'>
  100. <div className='h-6 w-full 2xl:h-[139px]' />
  101. <div className='pb-6 pt-1'>
  102. <span className='title-2xl-semi-bold text-text-primary'>{t('app.newApp.startFromBlank')}</span>
  103. </div>
  104. <div className='mb-2 leading-6'>
  105. <span className='system-sm-semibold text-text-secondary'>{t('app.newApp.chooseAppType')}</span>
  106. </div>
  107. <div className='flex w-[660px] flex-col gap-4'>
  108. <div>
  109. <div className='flex flex-row gap-2'>
  110. <AppTypeCard
  111. active={appMode === 'workflow'}
  112. title={t('app.types.workflow')}
  113. description={t('app.newApp.workflowShortDescription')}
  114. icon={<div className='flex h-6 w-6 items-center justify-center rounded-md bg-components-icon-bg-indigo-solid'>
  115. <RiExchange2Fill className='h-4 w-4 text-components-avatar-shape-fill-stop-100' />
  116. </div>}
  117. onClick={() => {
  118. setAppMode('workflow')
  119. }} />
  120. <AppTypeCard
  121. active={appMode === 'advanced-chat'}
  122. title={t('app.types.advanced')}
  123. description={t('app.newApp.advancedShortDescription')}
  124. icon={<div className='flex h-6 w-6 items-center justify-center rounded-md bg-components-icon-bg-blue-light-solid'>
  125. <BubbleTextMod className='h-4 w-4 text-components-avatar-shape-fill-stop-100' />
  126. </div>}
  127. onClick={() => {
  128. setAppMode('advanced-chat')
  129. }} />
  130. </div>
  131. </div>
  132. <div>
  133. <div className='mb-2 flex items-center'>
  134. <button type="button"
  135. className='flex cursor-pointer items-center border-0 bg-transparent p-0'
  136. onClick={() => setIsAppTypeExpanded(!isAppTypeExpanded)}
  137. >
  138. <span className='system-2xs-medium-uppercase text-text-tertiary'>{t('app.newApp.forBeginners')}</span>
  139. <RiArrowRightSLine className={`ml-1 h-4 w-4 text-text-tertiary transition-transform ${isAppTypeExpanded ? 'rotate-90' : ''}`} />
  140. </button>
  141. </div>
  142. {isAppTypeExpanded && (
  143. <div className='flex flex-row gap-2'>
  144. <AppTypeCard
  145. active={appMode === 'chat'}
  146. title={t('app.types.chatbot')}
  147. description={t('app.newApp.chatbotShortDescription')}
  148. icon={<div className='flex h-6 w-6 items-center justify-center rounded-md bg-components-icon-bg-blue-solid'>
  149. <ChatBot className='h-4 w-4 text-components-avatar-shape-fill-stop-100' />
  150. </div>}
  151. onClick={() => {
  152. setAppMode('chat')
  153. }} />
  154. <AppTypeCard
  155. active={appMode === 'agent-chat'}
  156. title={t('app.types.agent')}
  157. description={t('app.newApp.agentShortDescription')}
  158. icon={<div className='flex h-6 w-6 items-center justify-center rounded-md bg-components-icon-bg-violet-solid'>
  159. <Logic className='h-4 w-4 text-components-avatar-shape-fill-stop-100' />
  160. </div>}
  161. onClick={() => {
  162. setAppMode('agent-chat')
  163. }} />
  164. <AppTypeCard
  165. active={appMode === 'completion'}
  166. title={t('app.newApp.completeApp')}
  167. description={t('app.newApp.completionShortDescription')}
  168. icon={<div className='flex h-6 w-6 items-center justify-center rounded-md bg-components-icon-bg-teal-solid'>
  169. <ListSparkle className='h-4 w-4 text-components-avatar-shape-fill-stop-100' />
  170. </div>}
  171. onClick={() => {
  172. setAppMode('completion')
  173. }} />
  174. </div>
  175. )}
  176. </div>
  177. <Divider style={{ margin: 0 }} />
  178. <div className='flex items-center space-x-3'>
  179. <div className='flex-1'>
  180. <div className='mb-1 flex h-6 items-center'>
  181. <label className='system-sm-semibold text-text-secondary'>{t('app.newApp.captionName')}</label>
  182. </div>
  183. <Input
  184. value={name}
  185. onChange={e => setName(e.target.value)}
  186. placeholder={t('app.newApp.appNamePlaceholder') || ''}
  187. />
  188. </div>
  189. <AppIcon
  190. iconType={appIcon.type}
  191. icon={appIcon.type === 'emoji' ? appIcon.icon : appIcon.fileId}
  192. background={appIcon.type === 'emoji' ? appIcon.background : undefined}
  193. imageUrl={appIcon.type === 'image' ? appIcon.url : undefined}
  194. size='xxl' className='cursor-pointer rounded-2xl'
  195. onClick={() => { setShowAppIconPicker(true) }}
  196. />
  197. {showAppIconPicker && <AppIconPicker
  198. onSelect={(payload) => {
  199. setAppIcon(payload)
  200. setShowAppIconPicker(false)
  201. }}
  202. onClose={() => {
  203. setShowAppIconPicker(false)
  204. }}
  205. />}
  206. </div>
  207. <div>
  208. <div className='mb-1 flex h-6 items-center'>
  209. <label className='system-sm-semibold text-text-secondary'>{t('app.newApp.captionDescription')}</label>
  210. <span className='system-xs-regular ml-1 text-text-tertiary'>({t('app.newApp.optional')})</span>
  211. </div>
  212. <Textarea
  213. className='resize-none'
  214. placeholder={t('app.newApp.appDescriptionPlaceholder') || ''}
  215. value={description}
  216. onChange={e => setDescription(e.target.value)}
  217. />
  218. </div>
  219. </div>
  220. {isAppsFull && <AppsFull className='mt-4' loc='app-create' />}
  221. <div className='flex items-center justify-between pb-10 pt-5'>
  222. <div className='system-xs-regular flex cursor-pointer items-center gap-1 text-text-tertiary' onClick={onCreateFromTemplate}>
  223. <span>{t('app.newApp.noIdeaTip')}</span>
  224. <div className='p-[1px]'>
  225. <RiArrowRightLine className='h-3.5 w-3.5' />
  226. </div>
  227. </div>
  228. <div className='flex gap-2'>
  229. <Button onClick={onClose}>{t('app.newApp.Cancel')}</Button>
  230. <Button disabled={isAppsFull || !name} className='gap-1' variant="primary" onClick={handleCreateApp}>
  231. <span>{t('app.newApp.Create')}</span>
  232. <div className='flex gap-0.5'>
  233. <RiCommandLine size={14} className='system-kbd rounded-sm bg-components-kbd-bg-white p-0.5' />
  234. <RiCornerDownLeftLine size={14} className='system-kbd rounded-sm bg-components-kbd-bg-white p-0.5' />
  235. </div>
  236. </Button>
  237. </div>
  238. </div>
  239. </div>
  240. </div>
  241. <div className='relative flex h-full flex-1 shrink justify-start overflow-hidden'>
  242. <div className='absolute left-0 right-0 top-0 h-6 border-b border-b-divider-subtle 2xl:h-[139px]'></div>
  243. <div className='max-w-[760px] border-x border-x-divider-subtle'>
  244. <div className='h-6 2xl:h-[139px]' />
  245. <AppPreview mode={appMode} />
  246. <div className='absolute left-0 right-0 border-b border-b-divider-subtle'></div>
  247. <div className='flex h-[448px] w-[664px] items-center justify-center' style={{ background: 'repeating-linear-gradient(135deg, transparent, transparent 2px, rgba(16,24,40,0.04) 4px,transparent 3px, transparent 6px)' }}>
  248. <AppScreenShot show={appMode === 'chat'} mode='chat' />
  249. <AppScreenShot show={appMode === 'advanced-chat'} mode='advanced-chat' />
  250. <AppScreenShot show={appMode === 'agent-chat'} mode='agent-chat' />
  251. <AppScreenShot show={appMode === 'completion'} mode='completion' />
  252. <AppScreenShot show={appMode === 'workflow'} mode='workflow' />
  253. </div>
  254. <div className='absolute left-0 right-0 border-b border-b-divider-subtle'></div>
  255. </div>
  256. </div>
  257. </div>
  258. </>
  259. }
  260. type CreateAppDialogProps = CreateAppProps & {
  261. show: boolean
  262. }
  263. const CreateAppModal = ({ show, onClose, onSuccess, onCreateFromTemplate, defaultAppMode }: CreateAppDialogProps) => {
  264. return (
  265. <FullScreenModal
  266. overflowVisible
  267. closable
  268. open={show}
  269. onClose={onClose}
  270. >
  271. <CreateApp onClose={onClose} onSuccess={onSuccess} onCreateFromTemplate={onCreateFromTemplate} defaultAppMode={defaultAppMode} />
  272. </FullScreenModal>
  273. )
  274. }
  275. export default CreateAppModal
  276. type AppTypeCardProps = {
  277. icon: React.JSX.Element
  278. title: string
  279. description: string
  280. active: boolean
  281. onClick: () => void
  282. }
  283. function AppTypeCard({ icon, title, description, active, onClick }: AppTypeCardProps) {
  284. return <div
  285. className={
  286. cn(`relative box-content h-[84px] w-[191px] cursor-pointer rounded-xl
  287. border-[0.5px] border-components-option-card-option-border
  288. bg-components-panel-on-panel-item-bg p-3 shadow-xs hover:shadow-md`, active
  289. ? 'shadow-md outline outline-[1.5px] outline-components-option-card-option-selected-border'
  290. : '')
  291. }
  292. onClick={onClick}
  293. >
  294. {icon}
  295. <div className='system-sm-semibold mb-0.5 mt-2 text-text-secondary'>{title}</div>
  296. <div className='system-xs-regular line-clamp-2 text-text-tertiary' title={description}>{description}</div>
  297. </div>
  298. }
  299. function AppPreview({ mode }: { mode: AppMode }) {
  300. const { t } = useTranslation()
  301. const docLink = useDocLink()
  302. const modeToPreviewInfoMap = {
  303. 'chat': {
  304. title: t('app.types.chatbot'),
  305. description: t('app.newApp.chatbotUserDescription'),
  306. link: docLink('/guides/application-orchestrate/chatbot-application'),
  307. },
  308. 'advanced-chat': {
  309. title: t('app.types.advanced'),
  310. description: t('app.newApp.advancedUserDescription'),
  311. link: docLink('/guides/workflow/README', {
  312. 'zh-Hans': '/guides/workflow/readme',
  313. 'ja-JP': '/guides/workflow/concepts',
  314. }),
  315. },
  316. 'agent-chat': {
  317. title: t('app.types.agent'),
  318. description: t('app.newApp.agentUserDescription'),
  319. link: docLink('/guides/application-orchestrate/agent'),
  320. },
  321. 'completion': {
  322. title: t('app.newApp.completeApp'),
  323. description: t('app.newApp.completionUserDescription'),
  324. link: docLink('/guides/application-orchestrate/text-generator', {
  325. 'zh-Hans': '/guides/application-orchestrate/readme',
  326. 'ja-JP': '/guides/application-orchestrate/README',
  327. }),
  328. },
  329. 'workflow': {
  330. title: t('app.types.workflow'),
  331. description: t('app.newApp.workflowUserDescription'),
  332. link: docLink('/guides/workflow/README', {
  333. 'zh-Hans': '/guides/workflow/readme',
  334. 'ja-JP': '/guides/workflow/concepts',
  335. }),
  336. },
  337. }
  338. const previewInfo = modeToPreviewInfoMap[mode]
  339. return <div className='px-8 py-4'>
  340. <h4 className='system-sm-semibold-uppercase text-text-secondary'>{previewInfo.title}</h4>
  341. <div className='system-xs-regular mt-1 min-h-8 max-w-96 text-text-tertiary'>
  342. <span>{previewInfo.description}</span>
  343. {previewInfo.link && <Link target='_blank' href={previewInfo.link} className='ml-1 text-text-accent'>{t('app.newApp.learnMore')}</Link>}
  344. </div>
  345. </div>
  346. }
  347. function AppScreenShot({ mode, show }: { mode: AppMode; show: boolean }) {
  348. const { theme } = useTheme()
  349. const modeToImageMap = {
  350. 'chat': 'Chatbot',
  351. 'advanced-chat': 'Chatflow',
  352. 'agent-chat': 'Agent',
  353. 'completion': 'TextGenerator',
  354. 'workflow': 'Workflow',
  355. }
  356. return <picture>
  357. <source media="(resolution: 1x)" srcSet={`${basePath}/screenshots/${theme}/${modeToImageMap[mode]}.png`} />
  358. <source media="(resolution: 2x)" srcSet={`${basePath}/screenshots/${theme}/${modeToImageMap[mode]}@2x.png`} />
  359. <source media="(resolution: 3x)" srcSet={`${basePath}/screenshots/${theme}/${modeToImageMap[mode]}@3x.png`} />
  360. <Image className={show ? '' : 'hidden'}
  361. src={`${basePath}/screenshots/${theme}/${modeToImageMap[mode]}.png`}
  362. alt='App Screen Shot'
  363. width={664} height={448} />
  364. </picture>
  365. }