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.

get-automatic-res.tsx 10KB

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