get-automatic-res.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useCallback } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import { useBoolean } from 'ahooks'
  6. import {
  7. RiDatabase2Line,
  8. RiFileExcel2Line,
  9. RiGitCommitLine,
  10. RiNewspaperLine,
  11. RiPresentationLine,
  12. RiRoadMapLine,
  13. RiTerminalBoxLine,
  14. RiTranslate,
  15. RiUser2Line,
  16. } from '@remixicon/react'
  17. import cn from 'classnames'
  18. import s from './style.module.css'
  19. import Modal from '@/app/components/base/modal'
  20. import Button from '@/app/components/base/button'
  21. import Toast from '@/app/components/base/toast'
  22. import { generateRule } from '@/service/debug'
  23. import ConfigPrompt from '@/app/components/app/configuration/config-prompt'
  24. import type { Model } from '@/types/app'
  25. import { AppType } from '@/types/app'
  26. import ConfigVar from '@/app/components/app/configuration/config-var'
  27. import OpeningStatement from '@/app/components/app/configuration/features/chat-group/opening-statement'
  28. import GroupName from '@/app/components/app/configuration/base/group-name'
  29. import Loading from '@/app/components/base/loading'
  30. import Confirm from '@/app/components/base/confirm'
  31. // type
  32. import type { AutomaticRes } from '@/service/debug'
  33. import { Generator } from '@/app/components/base/icons/src/vender/other'
  34. export type IGetAutomaticResProps = {
  35. mode: AppType
  36. model: Model
  37. isShow: boolean
  38. onClose: () => void
  39. onFinished: (res: AutomaticRes) => void
  40. isInLLMNode?: boolean
  41. }
  42. const TryLabel: FC<{
  43. Icon: any
  44. text: string
  45. onClick: () => void
  46. }> = ({ Icon, text, onClick }) => {
  47. return (
  48. <div
  49. className='mt-2 mr-1 shrink-0 flex h-7 items-center px-2 bg-gray-100 rounded-lg cursor-pointer'
  50. onClick={onClick}
  51. >
  52. <Icon className='w-4 h-4 text-gray-500'></Icon>
  53. <div className='ml-1 text-xs font-medium text-gray-700'>{text}</div>
  54. </div>
  55. )
  56. }
  57. const GetAutomaticRes: FC<IGetAutomaticResProps> = ({
  58. mode,
  59. model,
  60. isShow,
  61. onClose,
  62. isInLLMNode,
  63. onFinished,
  64. }) => {
  65. const { t } = useTranslation()
  66. const tryList = [
  67. {
  68. icon: RiTerminalBoxLine,
  69. key: 'pythonDebugger',
  70. },
  71. {
  72. icon: RiTranslate,
  73. key: 'translation',
  74. },
  75. {
  76. icon: RiPresentationLine,
  77. key: 'meetingTakeaways',
  78. },
  79. {
  80. icon: RiNewspaperLine,
  81. key: 'writingsPolisher',
  82. },
  83. {
  84. icon: RiUser2Line,
  85. key: 'professionalAnalyst',
  86. },
  87. {
  88. icon: RiFileExcel2Line,
  89. key: 'excelFormulaExpert',
  90. },
  91. {
  92. icon: RiRoadMapLine,
  93. key: 'travelPlanning',
  94. },
  95. {
  96. icon: RiDatabase2Line,
  97. key: 'SQLSorcerer',
  98. },
  99. {
  100. icon: RiGitCommitLine,
  101. key: 'GitGud',
  102. },
  103. ]
  104. const [instruction, setInstruction] = React.useState<string>('')
  105. const handleChooseTemplate = useCallback((key: string) => {
  106. return () => {
  107. const template = t(`appDebug.generate.template.${key}.instruction`)
  108. setInstruction(template)
  109. }
  110. }, [t])
  111. const isValid = () => {
  112. if (instruction.trim() === '') {
  113. Toast.notify({
  114. type: 'error',
  115. message: t('common.errorMsg.fieldRequired', {
  116. field: t('appDebug.generate.instruction'),
  117. }),
  118. })
  119. return false
  120. }
  121. return true
  122. }
  123. const [isLoading, { setTrue: setLoadingTrue, setFalse: setLoadingFalse }] = useBoolean(false)
  124. const [res, setRes] = React.useState<AutomaticRes | null>(null)
  125. const renderLoading = (
  126. <div className='w-0 grow flex flex-col items-center justify-center h-full space-y-3'>
  127. <Loading />
  128. <div className='text-[13px] text-gray-400'>{t('appDebug.generate.loading')}</div>
  129. </div>
  130. )
  131. const renderNoData = (
  132. <div className='w-0 grow flex flex-col items-center px-8 justify-center h-full space-y-3'>
  133. <Generator className='w-14 h-14 text-gray-300' />
  134. <div className='leading-5 text-center text-[13px] font-normal text-gray-400'>
  135. <div>{t('appDebug.generate.noDataLine1')}</div>
  136. <div>{t('appDebug.generate.noDataLine2')}</div>
  137. </div>
  138. </div>
  139. )
  140. const onGenerate = async () => {
  141. if (!isValid())
  142. return
  143. if (isLoading)
  144. return
  145. setLoadingTrue()
  146. try {
  147. const { error, ...res } = await generateRule({
  148. instruction,
  149. model_config: model,
  150. no_variable: !!isInLLMNode,
  151. })
  152. setRes(res)
  153. if (error) {
  154. Toast.notify({
  155. type: 'error',
  156. message: error,
  157. })
  158. }
  159. }
  160. finally {
  161. setLoadingFalse()
  162. }
  163. }
  164. const [showConfirmOverwrite, setShowConfirmOverwrite] = React.useState(false)
  165. const isShowAutoPromptResPlaceholder = () => {
  166. return !isLoading && !res
  167. }
  168. return (
  169. <Modal
  170. isShow={isShow}
  171. onClose={onClose}
  172. className='!p-0 min-w-[1140px]'
  173. closable
  174. >
  175. <div className='flex h-[680px] flex-wrap'>
  176. <div className='w-[570px] shrink-0 p-6 h-full overflow-y-auto border-r border-gray-100'>
  177. <div className='mb-8'>
  178. <div className={`leading-[28px] text-lg font-bold ${s.textGradient}`}>{t('appDebug.generate.title')}</div>
  179. <div className='mt-1 text-[13px] font-normal text-gray-500'>{t('appDebug.generate.description')}</div>
  180. </div>
  181. <div >
  182. <div className='flex items-center'>
  183. <div className='mr-3 shrink-0 leading-[18px] text-xs font-semibold text-gray-500 uppercase'>{t('appDebug.generate.tryIt')}</div>
  184. <div className='grow h-px' style={{
  185. background: 'linear-gradient(to right, rgba(243, 244, 246, 1), rgba(243, 244, 246, 0))',
  186. }}></div>
  187. </div>
  188. <div className='flex flex-wrap'>
  189. {tryList.map(item => (
  190. <TryLabel
  191. key={item.key}
  192. Icon={item.icon}
  193. text={t(`appDebug.generate.template.${item.key}.name`)}
  194. onClick={handleChooseTemplate(item.key)}
  195. />
  196. ))}
  197. </div>
  198. </div>
  199. {/* inputs */}
  200. <div className='mt-6'>
  201. <div className='text-[0px]'>
  202. <div className='mb-2 leading-5 text-sm font-medium text-gray-900'>{t('appDebug.generate.instruction')}</div>
  203. <textarea className="w-full h-[200px] overflow-y-auto px-3 py-2 text-sm bg-gray-50 rounded-lg" placeholder={t('appDebug.generate.instructionPlaceHolder') as string} value={instruction} onChange={e => setInstruction(e.target.value)} />
  204. </div>
  205. <div className='mt-5 flex justify-end'>
  206. <Button
  207. className='flex space-x-1'
  208. variant='primary'
  209. onClick={onGenerate}
  210. disabled={isLoading}
  211. >
  212. <Generator className='w-4 h-4 text-white' />
  213. <span className='text-xs font-semibold text-white'>{t('appDebug.generate.generate')}</span>
  214. </Button>
  215. </div>
  216. </div>
  217. </div>
  218. {(!isLoading && res) && (
  219. <div className='w-0 grow p-6 pb-0 h-full'>
  220. <div className='shrink-0 mb-3 leading-[160%] text-base font-semibold text-gray-800'>{t('appDebug.generate.resTitle')}</div>
  221. <div className={cn('max-h-[555px] overflow-y-auto', !isInLLMNode && 'pb-2')}>
  222. <ConfigPrompt
  223. mode={mode}
  224. promptTemplate={res?.prompt || ''}
  225. promptVariables={[]}
  226. readonly
  227. noTitle={isInLLMNode}
  228. gradientBorder
  229. editorHeight={isInLLMNode ? 524 : 0}
  230. noResize={isInLLMNode}
  231. />
  232. {!isInLLMNode && (
  233. <>
  234. {(res?.variables?.length && res?.variables?.length > 0)
  235. ? (
  236. <ConfigVar
  237. promptVariables={res?.variables.map(key => ({ key, name: key, type: 'string', required: true })) || []}
  238. readonly
  239. />
  240. )
  241. : ''}
  242. {(mode !== AppType.completion && res?.opening_statement) && (
  243. <div className='mt-7'>
  244. <GroupName name={t('appDebug.feature.groupChat.title')} />
  245. <OpeningStatement
  246. value={res?.opening_statement || ''}
  247. readonly
  248. />
  249. </div>
  250. )}
  251. </>
  252. )}
  253. </div>
  254. <div className='flex justify-end py-4 bg-white'>
  255. <Button onClick={onClose}>{t('common.operation.cancel')}</Button>
  256. <Button variant='primary' className='ml-2' onClick={() => {
  257. setShowConfirmOverwrite(true)
  258. }}>{t('appDebug.generate.apply')}</Button>
  259. </div>
  260. </div>
  261. )}
  262. {isLoading && renderLoading}
  263. {isShowAutoPromptResPlaceholder() && renderNoData}
  264. {showConfirmOverwrite && (
  265. <Confirm
  266. title={t('appDebug.generate.overwriteTitle')}
  267. content={t('appDebug.generate.overwriteMessage')}
  268. isShow={showConfirmOverwrite}
  269. onConfirm={() => {
  270. setShowConfirmOverwrite(false)
  271. onFinished(res!)
  272. }}
  273. onCancel={() => setShowConfirmOverwrite(false)}
  274. />
  275. )}
  276. </div>
  277. </Modal>
  278. )
  279. }
  280. export default React.memo(GetAutomaticRes)