Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

panel.tsx 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. import StructureOutputItem from '@/app/components/workflow/nodes/_base/components/variable/object-child-tree-panel/show'
  21. import { Type } from '../llm/types'
  22. const i18nPrefix = 'workflow.nodes.tool'
  23. const Panel: FC<NodePanelProps<ToolNodeType>> = ({
  24. id,
  25. data,
  26. }) => {
  27. const { t } = useTranslation()
  28. const {
  29. readOnly,
  30. inputs,
  31. toolInputVarSchema,
  32. setInputVar,
  33. handleOnVarOpen,
  34. filterVar,
  35. toolSettingSchema,
  36. toolSettingValue,
  37. setToolSettingValue,
  38. currCollection,
  39. isShowAuthBtn,
  40. showSetAuth,
  41. showSetAuthModal,
  42. hideSetAuthModal,
  43. handleSaveAuth,
  44. isLoading,
  45. isShowSingleRun,
  46. hideSingleRun,
  47. singleRunForms,
  48. runningStatus,
  49. handleRun,
  50. handleStop,
  51. runResult,
  52. outputSchema,
  53. hasObjectOutput,
  54. } = useConfig(id, data)
  55. const toolIcon = useToolIcon(data)
  56. const logsParams = useLogs()
  57. const nodeInfo = useMemo(() => {
  58. if (!runResult)
  59. return null
  60. return formatToTracingNodeList([runResult], t)[0]
  61. }, [runResult, t])
  62. if (isLoading) {
  63. return <div className='flex h-[200px] items-center justify-center'>
  64. <Loading />
  65. </div>
  66. }
  67. return (
  68. <div className='pt-2'>
  69. {!readOnly && isShowAuthBtn && (
  70. <>
  71. <div className='px-4'>
  72. <Button
  73. variant='primary'
  74. className='w-full'
  75. onClick={showSetAuthModal}
  76. >
  77. {t(`${i18nPrefix}.toAuthorize`)}
  78. </Button>
  79. </div>
  80. </>
  81. )}
  82. {!isShowAuthBtn && <>
  83. <div className='space-y-4 px-4'>
  84. {toolInputVarSchema.length > 0 && (
  85. <Field
  86. title={t(`${i18nPrefix}.inputVars`)}
  87. >
  88. <InputVarList
  89. readOnly={readOnly}
  90. nodeId={id}
  91. schema={toolInputVarSchema as any}
  92. value={inputs.tool_parameters}
  93. onChange={setInputVar}
  94. filterVar={filterVar}
  95. isSupportConstantValue
  96. onOpen={handleOnVarOpen}
  97. />
  98. </Field>
  99. )}
  100. {toolInputVarSchema.length > 0 && toolSettingSchema.length > 0 && (
  101. <Split />
  102. )}
  103. <Form
  104. className='space-y-4'
  105. itemClassName='!py-0'
  106. fieldLabelClassName='!text-[13px] !font-semibold !text-text-secondary uppercase'
  107. value={toolSettingValue}
  108. onChange={setToolSettingValue}
  109. formSchemas={toolSettingSchema as any}
  110. isEditMode={false}
  111. showOnVariableMap={{}}
  112. validating={false}
  113. // inputClassName='!bg-gray-50'
  114. readonly={readOnly}
  115. />
  116. </div>
  117. </>}
  118. {showSetAuth && (
  119. <ConfigCredential
  120. collection={currCollection!}
  121. onCancel={hideSetAuthModal}
  122. onSaved={handleSaveAuth}
  123. isHideRemoveBtn
  124. />
  125. )}
  126. <div>
  127. <OutputVars>
  128. <>
  129. <VarItem
  130. name='text'
  131. type='string'
  132. description={t(`${i18nPrefix}.outputVars.text`)}
  133. isIndent={hasObjectOutput}
  134. />
  135. <VarItem
  136. name='files'
  137. type='array[file]'
  138. description={t(`${i18nPrefix}.outputVars.files.title`)}
  139. isIndent={hasObjectOutput}
  140. />
  141. <VarItem
  142. name='json'
  143. type='array[object]'
  144. description={t(`${i18nPrefix}.outputVars.json`)}
  145. isIndent={hasObjectOutput}
  146. />
  147. {outputSchema.map(outputItem => (
  148. <div key={outputItem.name}>
  149. {outputItem.value?.type === 'object' ? (
  150. <StructureOutputItem
  151. rootClassName='code-sm-semibold text-text-secondary'
  152. payload={{
  153. schema: {
  154. type: Type.object,
  155. properties: {
  156. [outputItem.name]: outputItem.value,
  157. },
  158. additionalProperties: false,
  159. },
  160. }} />
  161. ) : (
  162. <VarItem
  163. name={outputItem.name}
  164. type={outputItem.type.toLocaleLowerCase()}
  165. description={outputItem.description}
  166. isIndent={hasObjectOutput}
  167. />
  168. )}
  169. </div>
  170. ))}
  171. </>
  172. </OutputVars>
  173. </div>
  174. {isShowSingleRun && (
  175. <BeforeRunForm
  176. nodeName={inputs.title}
  177. nodeType={inputs.type}
  178. toolIcon={toolIcon}
  179. onHide={hideSingleRun}
  180. forms={singleRunForms}
  181. runningStatus={runningStatus}
  182. onRun={handleRun}
  183. onStop={handleStop}
  184. {...logsParams}
  185. result={<ResultPanel {...runResult} showSteps={false} {...logsParams} nodeInfo={nodeInfo} />}
  186. />
  187. )}
  188. </div>
  189. )
  190. }
  191. export default React.memo(Panel)