| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import type { FC } from 'react' | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | import React, { useMemo } from 'react' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							|  |  |  | import Split from '../_base/components/split' | 
					
						
							|  |  |  | import type { ToolNodeType } from './types' | 
					
						
							|  |  |  | import useConfig from './use-config' | 
					
						
							|  |  |  | import InputVarList from './components/input-var-list' | 
					
						
							|  |  |  | import Button from '@/app/components/base/button' | 
					
						
							|  |  |  | import Field from '@/app/components/workflow/nodes/_base/components/field' | 
					
						
							|  |  |  | import type { NodePanelProps } from '@/app/components/workflow/types' | 
					
						
							|  |  |  | import Form from '@/app/components/header/account-setting/model-provider-page/model-modal/Form' | 
					
						
							|  |  |  | import ConfigCredential from '@/app/components/tools/setting/build-in/config-credentials' | 
					
						
							|  |  |  | import Loading from '@/app/components/base/loading' | 
					
						
							|  |  |  | import BeforeRunForm from '@/app/components/workflow/nodes/_base/components/before-run-form' | 
					
						
							|  |  |  | import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars' | 
					
						
							|  |  |  | import ResultPanel from '@/app/components/workflow/run/result-panel' | 
					
						
							| 
									
										
										
										
											2024-12-20 15:44:37 +08:00
										 |  |  | import { useToolIcon } from '@/app/components/workflow/hooks' | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | import { useLogs } from '@/app/components/workflow/run/hooks' | 
					
						
							|  |  |  | import formatToTracingNodeList from '@/app/components/workflow/run/utils/format-log' | 
					
						
							| 
									
										
										
										
											2025-04-18 16:53:43 +08:00
										 |  |  | import StructureOutputItem from '@/app/components/workflow/nodes/_base/components/variable/object-child-tree-panel/show' | 
					
						
							|  |  |  | import { Type } from '../llm/types' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const i18nPrefix = 'workflow.nodes.tool' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const Panel: FC<NodePanelProps<ToolNodeType>> = ({ | 
					
						
							|  |  |  |   id, | 
					
						
							|  |  |  |   data, | 
					
						
							|  |  |  | }) => { | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const { | 
					
						
							|  |  |  |     readOnly, | 
					
						
							|  |  |  |     inputs, | 
					
						
							|  |  |  |     toolInputVarSchema, | 
					
						
							|  |  |  |     setInputVar, | 
					
						
							|  |  |  |     handleOnVarOpen, | 
					
						
							|  |  |  |     filterVar, | 
					
						
							|  |  |  |     toolSettingSchema, | 
					
						
							|  |  |  |     toolSettingValue, | 
					
						
							|  |  |  |     setToolSettingValue, | 
					
						
							|  |  |  |     currCollection, | 
					
						
							|  |  |  |     isShowAuthBtn, | 
					
						
							|  |  |  |     showSetAuth, | 
					
						
							|  |  |  |     showSetAuthModal, | 
					
						
							|  |  |  |     hideSetAuthModal, | 
					
						
							|  |  |  |     handleSaveAuth, | 
					
						
							|  |  |  |     isLoading, | 
					
						
							|  |  |  |     isShowSingleRun, | 
					
						
							|  |  |  |     hideSingleRun, | 
					
						
							|  |  |  |     singleRunForms, | 
					
						
							|  |  |  |     runningStatus, | 
					
						
							|  |  |  |     handleRun, | 
					
						
							|  |  |  |     handleStop, | 
					
						
							|  |  |  |     runResult, | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     outputSchema, | 
					
						
							| 
									
										
										
										
											2025-04-18 16:53:43 +08:00
										 |  |  |     hasObjectOutput, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   } = useConfig(id, data) | 
					
						
							| 
									
										
										
										
											2024-12-20 15:44:37 +08:00
										 |  |  |   const toolIcon = useToolIcon(data) | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |   const logsParams = useLogs() | 
					
						
							|  |  |  |   const nodeInfo = useMemo(() => { | 
					
						
							|  |  |  |     if (!runResult) | 
					
						
							|  |  |  |       return null | 
					
						
							|  |  |  |     return formatToTracingNodeList([runResult], t)[0] | 
					
						
							|  |  |  |   }, [runResult, t]) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (isLoading) { | 
					
						
							|  |  |  |     return <div className='flex h-[200px] items-center justify-center'> | 
					
						
							|  |  |  |       <Loading /> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							| 
									
										
										
										
											2024-12-11 14:21:38 +08:00
										 |  |  |     <div className='pt-2'> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       {!readOnly && isShowAuthBtn && ( | 
					
						
							|  |  |  |         <> | 
					
						
							| 
									
										
										
										
											2024-12-11 14:21:38 +08:00
										 |  |  |           <div className='px-4'> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             <Button | 
					
						
							| 
									
										
										
										
											2024-06-19 14:13:16 +08:00
										 |  |  |               variant='primary' | 
					
						
							| 
									
										
										
										
											2024-06-21 14:17:45 +08:00
										 |  |  |               className='w-full' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |               onClick={showSetAuthModal} | 
					
						
							|  |  |  |             > | 
					
						
							| 
									
										
										
										
											2025-05-27 16:40:11 +08:00
										 |  |  |               {t(`${i18nPrefix}.authorize`)} | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             </Button> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         </> | 
					
						
							|  |  |  |       )} | 
					
						
							|  |  |  |       {!isShowAuthBtn && <> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |         <div className='space-y-4 px-4'> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           {toolInputVarSchema.length > 0 && ( | 
					
						
							|  |  |  |             <Field | 
					
						
							|  |  |  |               title={t(`${i18nPrefix}.inputVars`)} | 
					
						
							|  |  |  |             > | 
					
						
							|  |  |  |               <InputVarList | 
					
						
							|  |  |  |                 readOnly={readOnly} | 
					
						
							|  |  |  |                 nodeId={id} | 
					
						
							|  |  |  |                 schema={toolInputVarSchema as any} | 
					
						
							|  |  |  |                 value={inputs.tool_parameters} | 
					
						
							|  |  |  |                 onChange={setInputVar} | 
					
						
							|  |  |  |                 filterVar={filterVar} | 
					
						
							|  |  |  |                 isSupportConstantValue | 
					
						
							|  |  |  |                 onOpen={handleOnVarOpen} | 
					
						
							|  |  |  |               /> | 
					
						
							|  |  |  |             </Field> | 
					
						
							|  |  |  |           )} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           {toolInputVarSchema.length > 0 && toolSettingSchema.length > 0 && ( | 
					
						
							|  |  |  |             <Split /> | 
					
						
							|  |  |  |           )} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           <Form | 
					
						
							|  |  |  |             className='space-y-4' | 
					
						
							|  |  |  |             itemClassName='!py-0' | 
					
						
							| 
									
										
										
										
											2025-04-10 17:15:48 +08:00
										 |  |  |             fieldLabelClassName='!text-[13px] !font-semibold !text-text-secondary uppercase' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             value={toolSettingValue} | 
					
						
							|  |  |  |             onChange={setToolSettingValue} | 
					
						
							|  |  |  |             formSchemas={toolSettingSchema as any} | 
					
						
							|  |  |  |             isEditMode={false} | 
					
						
							|  |  |  |             showOnVariableMap={{}} | 
					
						
							|  |  |  |             validating={false} | 
					
						
							| 
									
										
										
										
											2025-04-10 17:15:48 +08:00
										 |  |  |             // inputClassName='!bg-gray-50'
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             readonly={readOnly} | 
					
						
							|  |  |  |           /> | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |       </>} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       {showSetAuth && ( | 
					
						
							|  |  |  |         <ConfigCredential | 
					
						
							|  |  |  |           collection={currCollection!} | 
					
						
							|  |  |  |           onCancel={hideSetAuthModal} | 
					
						
							|  |  |  |           onSaved={handleSaveAuth} | 
					
						
							|  |  |  |           isHideRemoveBtn | 
					
						
							|  |  |  |         /> | 
					
						
							|  |  |  |       )} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 14:21:38 +08:00
										 |  |  |       <div> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         <OutputVars> | 
					
						
							|  |  |  |           <> | 
					
						
							|  |  |  |             <VarItem | 
					
						
							|  |  |  |               name='text' | 
					
						
							| 
									
										
										
										
											2025-04-18 16:53:43 +08:00
										 |  |  |               type='string' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |               description={t(`${i18nPrefix}.outputVars.text`)} | 
					
						
							| 
									
										
										
										
											2025-04-18 16:53:43 +08:00
										 |  |  |               isIndent={hasObjectOutput} | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             /> | 
					
						
							|  |  |  |             <VarItem | 
					
						
							|  |  |  |               name='files' | 
					
						
							| 
									
										
										
										
											2025-04-18 16:53:43 +08:00
										 |  |  |               type='array[file]' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |               description={t(`${i18nPrefix}.outputVars.files.title`)} | 
					
						
							| 
									
										
										
										
											2025-04-18 16:53:43 +08:00
										 |  |  |               isIndent={hasObjectOutput} | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             /> | 
					
						
							| 
									
										
										
										
											2024-07-04 16:15:56 +08:00
										 |  |  |             <VarItem | 
					
						
							|  |  |  |               name='json' | 
					
						
							| 
									
										
										
										
											2025-04-18 16:53:43 +08:00
										 |  |  |               type='array[object]' | 
					
						
							| 
									
										
										
										
											2024-07-04 16:15:56 +08:00
										 |  |  |               description={t(`${i18nPrefix}.outputVars.json`)} | 
					
						
							| 
									
										
										
										
											2025-04-18 16:53:43 +08:00
										 |  |  |               isIndent={hasObjectOutput} | 
					
						
							| 
									
										
										
										
											2024-07-04 16:15:56 +08:00
										 |  |  |             /> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             {outputSchema.map(outputItem => ( | 
					
						
							| 
									
										
										
										
											2025-04-18 16:53:43 +08:00
										 |  |  |               <div key={outputItem.name}> | 
					
						
							|  |  |  |                 {outputItem.value?.type === 'object' ? ( | 
					
						
							|  |  |  |                   <StructureOutputItem | 
					
						
							|  |  |  |                     rootClassName='code-sm-semibold text-text-secondary' | 
					
						
							|  |  |  |                     payload={{ | 
					
						
							|  |  |  |                       schema: { | 
					
						
							|  |  |  |                         type: Type.object, | 
					
						
							|  |  |  |                         properties: { | 
					
						
							|  |  |  |                           [outputItem.name]: outputItem.value, | 
					
						
							|  |  |  |                         }, | 
					
						
							|  |  |  |                         additionalProperties: false, | 
					
						
							|  |  |  |                       }, | 
					
						
							|  |  |  |                     }} /> | 
					
						
							|  |  |  |                 ) : ( | 
					
						
							|  |  |  |                   <VarItem | 
					
						
							|  |  |  |                     name={outputItem.name} | 
					
						
							|  |  |  |                     type={outputItem.type.toLocaleLowerCase()} | 
					
						
							|  |  |  |                     description={outputItem.description} | 
					
						
							|  |  |  |                     isIndent={hasObjectOutput} | 
					
						
							|  |  |  |                   /> | 
					
						
							|  |  |  |                 )} | 
					
						
							|  |  |  |               </div> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             ))} | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           </> | 
					
						
							|  |  |  |         </OutputVars> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       {isShowSingleRun && ( | 
					
						
							|  |  |  |         <BeforeRunForm | 
					
						
							|  |  |  |           nodeName={inputs.title} | 
					
						
							| 
									
										
										
										
											2024-12-20 15:44:37 +08:00
										 |  |  |           nodeType={inputs.type} | 
					
						
							|  |  |  |           toolIcon={toolIcon} | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           onHide={hideSingleRun} | 
					
						
							|  |  |  |           forms={singleRunForms} | 
					
						
							|  |  |  |           runningStatus={runningStatus} | 
					
						
							|  |  |  |           onRun={handleRun} | 
					
						
							|  |  |  |           onStop={handleStop} | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |           {...logsParams} | 
					
						
							|  |  |  |           result={<ResultPanel {...runResult} showSteps={false} {...logsParams} nodeInfo={nodeInfo} />} | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         /> | 
					
						
							|  |  |  |       )} | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default React.memo(Panel) |