You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

panel.tsx 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import type { FC } from 'react'
  2. import React 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 ToolForm from './components/tool-form'
  8. import Field from '@/app/components/workflow/nodes/_base/components/field'
  9. import type { NodePanelProps } from '@/app/components/workflow/types'
  10. import Loading from '@/app/components/base/loading'
  11. import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
  12. import StructureOutputItem from '@/app/components/workflow/nodes/_base/components/variable/object-child-tree-panel/show'
  13. import { useStore } from '@/app/components/workflow/store'
  14. import { wrapStructuredVarItem } from '@/app/components/workflow/utils/tool'
  15. import useMatchSchemaType, { getMatchedSchemaType } from '../_base/components/variable/use-match-schema-type'
  16. const i18nPrefix = 'workflow.nodes.tool'
  17. const Panel: FC<NodePanelProps<ToolNodeType>> = ({
  18. id,
  19. data,
  20. }) => {
  21. const { t } = useTranslation()
  22. const {
  23. readOnly,
  24. inputs,
  25. toolInputVarSchema,
  26. setInputVar,
  27. toolSettingSchema,
  28. toolSettingValue,
  29. setToolSettingValue,
  30. currCollection,
  31. isShowAuthBtn,
  32. isLoading,
  33. outputSchema,
  34. hasObjectOutput,
  35. currTool,
  36. } = useConfig(id, data)
  37. const [collapsed, setCollapsed] = React.useState(false)
  38. const pipelineId = useStore(s => s.pipelineId)
  39. const setShowInputFieldPanel = useStore(s => s.setShowInputFieldPanel)
  40. const { schemaTypeDefinitions } = useMatchSchemaType()
  41. if (isLoading) {
  42. return (
  43. <div className='flex h-[200px] items-center justify-center'>
  44. <Loading />
  45. </div>
  46. )
  47. }
  48. return (
  49. <div className='pt-2'>
  50. {!isShowAuthBtn && (
  51. <div className='relative'>
  52. {toolInputVarSchema.length > 0 && (
  53. <Field
  54. className='px-4'
  55. title={t(`${i18nPrefix}.inputVars`)}
  56. >
  57. <ToolForm
  58. readOnly={readOnly}
  59. nodeId={id}
  60. schema={toolInputVarSchema as any}
  61. value={inputs.tool_parameters}
  62. onChange={setInputVar}
  63. currentProvider={currCollection}
  64. currentTool={currTool}
  65. showManageInputField={!!pipelineId}
  66. onManageInputField={() => setShowInputFieldPanel?.(true)}
  67. />
  68. </Field>
  69. )}
  70. {toolInputVarSchema.length > 0 && toolSettingSchema.length > 0 && (
  71. <Split className='mt-1' />
  72. )}
  73. {toolSettingSchema.length > 0 && (
  74. <>
  75. <OutputVars
  76. title={t(`${i18nPrefix}.settings`)}
  77. collapsed={collapsed}
  78. onCollapse={setCollapsed}
  79. >
  80. <ToolForm
  81. readOnly={readOnly}
  82. nodeId={id}
  83. schema={toolSettingSchema as any}
  84. value={toolSettingValue}
  85. onChange={setToolSettingValue}
  86. />
  87. </OutputVars>
  88. <Split />
  89. </>
  90. )}
  91. </div>
  92. )}
  93. <div>
  94. <OutputVars>
  95. <>
  96. <VarItem
  97. name='text'
  98. type='string'
  99. description={t(`${i18nPrefix}.outputVars.text`)}
  100. isIndent={hasObjectOutput}
  101. />
  102. <VarItem
  103. name='files'
  104. type='array[file]'
  105. description={t(`${i18nPrefix}.outputVars.files.title`)}
  106. isIndent={hasObjectOutput}
  107. />
  108. <VarItem
  109. name='json'
  110. type='array[object]'
  111. description={t(`${i18nPrefix}.outputVars.json`)}
  112. isIndent={hasObjectOutput}
  113. />
  114. {outputSchema.map((outputItem) => {
  115. const schemaType = getMatchedSchemaType(outputItem.value, schemaTypeDefinitions)
  116. return (
  117. <div key={outputItem.name}>
  118. {outputItem.value?.type === 'object' ? (
  119. <StructureOutputItem
  120. rootClassName='code-sm-semibold text-text-secondary'
  121. payload={wrapStructuredVarItem(outputItem, schemaType)}
  122. />
  123. ) : (
  124. <VarItem
  125. name={outputItem.name}
  126. // eslint-disable-next-line sonarjs/no-nested-template-literals
  127. type={`${outputItem.type.toLocaleLowerCase()}${schemaType ? ` (${schemaType})` : ''}`}
  128. description={outputItem.description}
  129. isIndent={hasObjectOutput}
  130. />
  131. )}
  132. </div>
  133. )
  134. })}
  135. </>
  136. </OutputVars>
  137. </div>
  138. </div>
  139. )
  140. }
  141. export default React.memo(Panel)