index.tsx 13 KB

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