panel.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import type { FC } from 'react'
  2. import React, { useMemo } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import Split from '../_base/components/split'
  5. import type { ToolNodeType } from './types'
  6. import useConfig from './use-config'
  7. import InputVarList from './components/input-var-list'
  8. import Button from '@/app/components/base/button'
  9. import Field from '@/app/components/workflow/nodes/_base/components/field'
  10. import type { NodePanelProps } from '@/app/components/workflow/types'
  11. import Form from '@/app/components/header/account-setting/model-provider-page/model-modal/Form'
  12. import ConfigCredential from '@/app/components/tools/setting/build-in/config-credentials'
  13. import Loading from '@/app/components/base/loading'
  14. import BeforeRunForm from '@/app/components/workflow/nodes/_base/components/before-run-form'
  15. import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
  16. import ResultPanel from '@/app/components/workflow/run/result-panel'
  17. import { useToolIcon } from '@/app/components/workflow/hooks'
  18. import { useLogs } from '@/app/components/workflow/run/hooks'
  19. import formatToTracingNodeList from '@/app/components/workflow/run/utils/format-log'
  20. const i18nPrefix = 'workflow.nodes.tool'
  21. const Panel: FC<NodePanelProps<ToolNodeType>> = ({
  22. id,
  23. data,
  24. }) => {
  25. const { t } = useTranslation()
  26. const {
  27. readOnly,
  28. inputs,
  29. toolInputVarSchema,
  30. setInputVar,
  31. handleOnVarOpen,
  32. filterVar,
  33. toolSettingSchema,
  34. toolSettingValue,
  35. setToolSettingValue,
  36. currCollection,
  37. isShowAuthBtn,
  38. showSetAuth,
  39. showSetAuthModal,
  40. hideSetAuthModal,
  41. handleSaveAuth,
  42. isLoading,
  43. isShowSingleRun,
  44. hideSingleRun,
  45. singleRunForms,
  46. runningStatus,
  47. handleRun,
  48. handleStop,
  49. runResult,
  50. outputSchema,
  51. } = useConfig(id, data)
  52. const toolIcon = useToolIcon(data)
  53. const logsParams = useLogs()
  54. const nodeInfo = useMemo(() => {
  55. if (!runResult)
  56. return null
  57. return formatToTracingNodeList([runResult], t)[0]
  58. }, [runResult, t])
  59. if (isLoading) {
  60. return <div className='flex h-[200px] items-center justify-center'>
  61. <Loading />
  62. </div>
  63. }
  64. return (
  65. <div className='pt-2'>
  66. {!readOnly && isShowAuthBtn && (
  67. <>
  68. <div className='px-4'>
  69. <Button
  70. variant='primary'
  71. className='w-full'
  72. onClick={showSetAuthModal}
  73. >
  74. {t(`${i18nPrefix}.toAuthorize`)}
  75. </Button>
  76. </div>
  77. </>
  78. )}
  79. {!isShowAuthBtn && <>
  80. <div className='px-4 space-y-4'>
  81. {toolInputVarSchema.length > 0 && (
  82. <Field
  83. title={t(`${i18nPrefix}.inputVars`)}
  84. >
  85. <InputVarList
  86. readOnly={readOnly}
  87. nodeId={id}
  88. schema={toolInputVarSchema as any}
  89. value={inputs.tool_parameters}
  90. onChange={setInputVar}
  91. filterVar={filterVar}
  92. isSupportConstantValue
  93. onOpen={handleOnVarOpen}
  94. />
  95. </Field>
  96. )}
  97. {toolInputVarSchema.length > 0 && toolSettingSchema.length > 0 && (
  98. <Split />
  99. )}
  100. <Form
  101. className='space-y-4'
  102. itemClassName='!py-0'
  103. fieldLabelClassName='!text-[13px] !font-semibold !text-gray-700 uppercase'
  104. value={toolSettingValue}
  105. onChange={setToolSettingValue}
  106. formSchemas={toolSettingSchema as any}
  107. isEditMode={false}
  108. showOnVariableMap={{}}
  109. validating={false}
  110. inputClassName='!bg-gray-50'
  111. readonly={readOnly}
  112. />
  113. </div>
  114. </>}
  115. {showSetAuth && (
  116. <ConfigCredential
  117. collection={currCollection!}
  118. onCancel={hideSetAuthModal}
  119. onSaved={handleSaveAuth}
  120. isHideRemoveBtn
  121. />
  122. )}
  123. <div>
  124. <OutputVars>
  125. <>
  126. <VarItem
  127. name='text'
  128. type='String'
  129. description={t(`${i18nPrefix}.outputVars.text`)}
  130. />
  131. <VarItem
  132. name='files'
  133. type='Array[File]'
  134. description={t(`${i18nPrefix}.outputVars.files.title`)}
  135. />
  136. <VarItem
  137. name='json'
  138. type='Array[Object]'
  139. description={t(`${i18nPrefix}.outputVars.json`)}
  140. />
  141. {outputSchema.map(outputItem => (
  142. <VarItem
  143. key={outputItem.name}
  144. name={outputItem.name}
  145. type={outputItem.type}
  146. description={outputItem.description}
  147. />
  148. ))}
  149. </>
  150. </OutputVars>
  151. </div>
  152. {isShowSingleRun && (
  153. <BeforeRunForm
  154. nodeName={inputs.title}
  155. nodeType={inputs.type}
  156. toolIcon={toolIcon}
  157. onHide={hideSingleRun}
  158. forms={singleRunForms}
  159. runningStatus={runningStatus}
  160. onRun={handleRun}
  161. onStop={handleStop}
  162. {...logsParams}
  163. result={<ResultPanel {...runResult} showSteps={false} {...logsParams} nodeInfo={nodeInfo} />}
  164. />
  165. )}
  166. </div>
  167. )
  168. }
  169. export default React.memo(Panel)