index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import produce from 'immer'
  6. import type { Emoji, WorkflowToolProviderParameter, WorkflowToolProviderRequest } from '../types'
  7. import cn from '@/utils/classnames'
  8. import Drawer from '@/app/components/base/drawer-plus'
  9. import Input from '@/app/components/base/input'
  10. import Textarea from '@/app/components/base/textarea'
  11. import Button from '@/app/components/base/button'
  12. import Toast from '@/app/components/base/toast'
  13. import EmojiPicker from '@/app/components/base/emoji-picker'
  14. import AppIcon from '@/app/components/base/app-icon'
  15. import MethodSelector from '@/app/components/tools/workflow-tool/method-selector'
  16. import LabelSelector from '@/app/components/tools/labels/selector'
  17. import ConfirmModal from '@/app/components/tools/workflow-tool/confirm-modal'
  18. import Tooltip from '@/app/components/base/tooltip'
  19. type Props = {
  20. isAdd?: boolean
  21. payload: any
  22. onHide: () => void
  23. onRemove?: () => void
  24. onCreate?: (payload: WorkflowToolProviderRequest & { workflow_app_id: string }) => void
  25. onSave?: (payload: WorkflowToolProviderRequest & Partial<{
  26. workflow_app_id: string
  27. workflow_tool_id: string
  28. }>) => void
  29. }
  30. // Add and Edit
  31. const WorkflowToolAsModal: FC<Props> = ({
  32. isAdd,
  33. payload,
  34. onHide,
  35. onRemove,
  36. onSave,
  37. onCreate,
  38. }) => {
  39. const { t } = useTranslation()
  40. const [showEmojiPicker, setShowEmojiPicker] = useState<Boolean>(false)
  41. const [emoji, setEmoji] = useState<Emoji>(payload.icon)
  42. const [label, setLabel] = useState<string>(payload.label)
  43. const [name, setName] = useState(payload.name)
  44. const [description, setDescription] = useState(payload.description)
  45. const [parameters, setParameters] = useState<WorkflowToolProviderParameter[]>(payload.parameters)
  46. const handleParameterChange = (key: string, value: string, index: number) => {
  47. const newData = produce(parameters, (draft: WorkflowToolProviderParameter[]) => {
  48. if (key === 'description')
  49. draft[index].description = value
  50. else
  51. draft[index].form = value
  52. })
  53. setParameters(newData)
  54. }
  55. const [labels, setLabels] = useState<string[]>(payload.labels)
  56. const handleLabelSelect = (value: string[]) => {
  57. setLabels(value)
  58. }
  59. const [privacyPolicy, setPrivacyPolicy] = useState(payload.privacy_policy)
  60. const [showModal, setShowModal] = useState(false)
  61. const isNameValid = (name: string) => {
  62. // when the user has not input anything, no need for a warning
  63. if (name === '')
  64. return true
  65. return /^[a-zA-Z0-9_]+$/.test(name)
  66. }
  67. const onConfirm = () => {
  68. let errorMessage = ''
  69. if (!label)
  70. errorMessage = t('common.errorMsg.fieldRequired', { field: t('tools.createTool.name') })
  71. if (!name)
  72. errorMessage = t('common.errorMsg.fieldRequired', { field: t('tools.createTool.nameForToolCall') })
  73. if (!isNameValid(name))
  74. errorMessage = t('tools.createTool.nameForToolCall') + t('tools.createTool.nameForToolCallTip')
  75. if (errorMessage) {
  76. Toast.notify({
  77. type: 'error',
  78. message: errorMessage,
  79. })
  80. return
  81. }
  82. const requestParams = {
  83. name,
  84. description,
  85. icon: emoji,
  86. label,
  87. parameters: parameters.map(item => ({
  88. name: item.name,
  89. description: item.description,
  90. form: item.form,
  91. })),
  92. labels,
  93. privacy_policy: privacyPolicy,
  94. }
  95. if (!isAdd) {
  96. onSave?.({
  97. ...requestParams,
  98. workflow_tool_id: payload.workflow_tool_id,
  99. })
  100. }
  101. else {
  102. onCreate?.({
  103. ...requestParams,
  104. workflow_app_id: payload.workflow_app_id,
  105. })
  106. }
  107. }
  108. return (
  109. <>
  110. <Drawer
  111. isShow
  112. onHide={onHide}
  113. title={t('workflow.common.workflowAsTool')!}
  114. panelClassName='mt-2 !w-[640px]'
  115. maxWidthClassName='!max-w-[640px]'
  116. height='calc(100vh - 16px)'
  117. headerClassName='!border-b-black/5'
  118. body={
  119. <div className='flex flex-col h-full'>
  120. <div className='grow h-0 overflow-y-auto px-6 py-3 space-y-4'>
  121. {/* name & icon */}
  122. <div>
  123. <div className='py-2 leading-5 text-sm font-medium text-gray-900'>{t('tools.createTool.name')} <span className='ml-1 text-red-500'>*</span></div>
  124. <div className='flex items-center justify-between gap-3'>
  125. <AppIcon size='large' onClick={() => { setShowEmojiPicker(true) }} className='cursor-pointer' iconType='emoji' icon={emoji.content} background={emoji.background} />
  126. <Input
  127. className='grow h-10'
  128. placeholder={t('tools.createTool.toolNamePlaceHolder')!}
  129. value={label}
  130. onChange={e => setLabel(e.target.value)}
  131. />
  132. </div>
  133. </div>
  134. {/* name for tool call */}
  135. <div>
  136. <div className='flex items-center py-2 leading-5 text-sm font-medium text-gray-900'>
  137. {t('tools.createTool.nameForToolCall')} <span className='ml-1 text-red-500'>*</span>
  138. <Tooltip
  139. popupContent={
  140. <div className='w-[180px]'>
  141. {t('tools.createTool.nameForToolCallPlaceHolder')}
  142. </div>
  143. }
  144. />
  145. </div>
  146. <Input
  147. className='h-10'
  148. placeholder={t('tools.createTool.nameForToolCallPlaceHolder')!}
  149. value={name}
  150. onChange={e => setName(e.target.value)}
  151. />
  152. {!isNameValid(name) && (
  153. <div className='text-xs leading-[18px] text-red-500'>{t('tools.createTool.nameForToolCallTip')}</div>
  154. )}
  155. </div>
  156. {/* description */}
  157. <div>
  158. <div className='py-2 leading-5 text-sm font-medium text-gray-900'>{t('tools.createTool.description')}</div>
  159. <Textarea
  160. placeholder={t('tools.createTool.descriptionPlaceholder') || ''}
  161. value={description}
  162. onChange={e => setDescription(e.target.value)}
  163. />
  164. </div>
  165. {/* Tool Input */}
  166. <div>
  167. <div className='py-2 leading-5 text-sm font-medium text-gray-900'>{t('tools.createTool.toolInput.title')}</div>
  168. <div className='rounded-lg border border-gray-200 w-full overflow-x-auto'>
  169. <table className='w-full leading-[18px] text-xs text-gray-700 font-normal'>
  170. <thead className='text-gray-500 uppercase'>
  171. <tr className='border-b border-gray-200'>
  172. <th className="p-2 pl-3 font-medium w-[156px]">{t('tools.createTool.toolInput.name')}</th>
  173. <th className="p-2 pl-3 font-medium w-[102px]">{t('tools.createTool.toolInput.method')}</th>
  174. <th className="p-2 pl-3 font-medium">{t('tools.createTool.toolInput.description')}</th>
  175. </tr>
  176. </thead>
  177. <tbody>
  178. {parameters.map((item, index) => (
  179. <tr key={index} className='border-b last:border-0 border-gray-200'>
  180. <td className="p-2 pl-3 max-w-[156px]">
  181. <div className='text-[13px] leading-[18px]'>
  182. <div title={item.name} className='flex'>
  183. <span className='font-medium text-gray-900 truncate'>{item.name}</span>
  184. <span className='shrink-0 pl-1 text-[#ec4a0a] text-xs leading-[18px]'>{item.required ? t('tools.createTool.toolInput.required') : ''}</span>
  185. </div>
  186. <div className='text-gray-500'>{item.type}</div>
  187. </div>
  188. </td>
  189. <td>
  190. {item.name === '__image' && (
  191. <div className={cn(
  192. 'flex items-center gap-1 min-h-[56px] px-3 py-2 h-9 bg-white cursor-default',
  193. )}>
  194. <div className={cn('grow text-[13px] leading-[18px] text-gray-700 truncate')}>
  195. {t('tools.createTool.toolInput.methodParameter')}
  196. </div>
  197. </div>
  198. )}
  199. {item.name !== '__image' && (
  200. <MethodSelector value={item.form} onChange={value => handleParameterChange('form', value, index)} />
  201. )}
  202. </td>
  203. <td className="p-2 pl-3 text-gray-500 w-[236px]">
  204. <input
  205. type='text'
  206. className='grow text-gray-700 text-[13px] leading-[18px] font-normal bg-white outline-none appearance-none caret-primary-600 placeholder:text-gray-300'
  207. placeholder={t('tools.createTool.toolInput.descriptionPlaceholder')!}
  208. value={item.description}
  209. onChange={e => handleParameterChange('description', e.target.value, index)}
  210. />
  211. </td>
  212. </tr>
  213. ))}
  214. </tbody>
  215. </table>
  216. </div>
  217. </div>
  218. {/* Tags */}
  219. <div>
  220. <div className='py-2 leading-5 text-sm font-medium text-gray-900'>{t('tools.createTool.toolInput.label')}</div>
  221. <LabelSelector value={labels} onChange={handleLabelSelect} />
  222. </div>
  223. {/* Privacy Policy */}
  224. <div>
  225. <div className='py-2 leading-5 text-sm font-medium text-gray-900'>{t('tools.createTool.privacyPolicy')}</div>
  226. <Input
  227. className='h-10'
  228. value={privacyPolicy}
  229. onChange={e => setPrivacyPolicy(e.target.value)}
  230. placeholder={t('tools.createTool.privacyPolicyPlaceholder') || ''} />
  231. </div>
  232. </div>
  233. <div className={cn((!isAdd && onRemove) ? 'justify-between' : 'justify-end', 'mt-2 shrink-0 flex py-4 px-6 rounded-b-[10px] bg-gray-50 border-t border-black/5')} >
  234. {!isAdd && onRemove && (
  235. <Button onClick={onRemove} className='text-red-500 border-red-50 hover:border-red-500'>{t('common.operation.delete')}</Button>
  236. )}
  237. <div className='flex space-x-2 '>
  238. <Button onClick={onHide}>{t('common.operation.cancel')}</Button>
  239. <Button variant='primary' onClick={() => {
  240. if (isAdd)
  241. onConfirm()
  242. else
  243. setShowModal(true)
  244. }}>{t('common.operation.save')}</Button>
  245. </div>
  246. </div>
  247. </div>
  248. }
  249. isShowMask={true}
  250. clickOutsideNotOpen={true}
  251. />
  252. {showEmojiPicker && <EmojiPicker
  253. onSelect={(icon, icon_background) => {
  254. setEmoji({ content: icon, background: icon_background })
  255. setShowEmojiPicker(false)
  256. }}
  257. onClose={() => {
  258. setShowEmojiPicker(false)
  259. }}
  260. />}
  261. {showModal && (
  262. <ConfirmModal
  263. show={showModal}
  264. onClose={() => setShowModal(false)}
  265. onConfirm={onConfirm}
  266. />
  267. )}
  268. </>
  269. )
  270. }
  271. export default React.memo(WorkflowToolAsModal)