index.tsx 13 KB

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