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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <div className='flex h-[200px] items-center justify-center'>
  43. <Loading />
  44. </div>
  45. }
  46. return (
  47. <div className='pt-2'>
  48. {!isShowAuthBtn && (
  49. <div className='relative'>
  50. {toolInputVarSchema.length > 0 && (
  51. <Field
  52. className='px-4'
  53. title={t(`${i18nPrefix}.inputVars`)}
  54. >
  55. <ToolForm
  56. readOnly={readOnly}
  57. nodeId={id}
  58. schema={toolInputVarSchema as any}
  59. value={inputs.tool_parameters}
  60. onChange={setInputVar}
  61. currentProvider={currCollection}
  62. currentTool={currTool}
  63. showManageInputField={!!pipelineId}
  64. onManageInputField={() => setShowInputFieldPanel?.(true)}
  65. />
  66. </Field>
  67. )}
  68. {toolInputVarSchema.length > 0 && toolSettingSchema.length > 0 && (
  69. <Split className='mt-1' />
  70. )}
  71. {toolSettingSchema.length > 0 && (
  72. <>
  73. <OutputVars
  74. title={t(`${i18nPrefix}.settings`)}
  75. collapsed={collapsed}
  76. onCollapse={setCollapsed}
  77. >
  78. <ToolForm
  79. readOnly={readOnly}
  80. nodeId={id}
  81. schema={toolSettingSchema as any}
  82. value={toolSettingValue}
  83. onChange={setToolSettingValue}
  84. />
  85. </OutputVars>
  86. <Split />
  87. </>
  88. )}
  89. </div>
  90. )}
  91. <div>
  92. <OutputVars>
  93. <>
  94. <VarItem
  95. name='text'
  96. type='string'
  97. description={t(`${i18nPrefix}.outputVars.text`)}
  98. isIndent={hasObjectOutput}
  99. />
  100. <VarItem
  101. name='files'
  102. type='array[file]'
  103. description={t(`${i18nPrefix}.outputVars.files.title`)}
  104. isIndent={hasObjectOutput}
  105. />
  106. <VarItem
  107. name='json'
  108. type='array[object]'
  109. description={t(`${i18nPrefix}.outputVars.json`)}
  110. isIndent={hasObjectOutput}
  111. />
  112. {outputSchema.map((outputItem) => {
  113. const schemaType = getMatchedSchemaType(outputItem.value, schemaTypeDefinitions)
  114. return (
  115. <div key={outputItem.name}>
  116. {outputItem.value?.type === 'object' ? (
  117. <StructureOutputItem
  118. rootClassName='code-sm-semibold text-text-secondary'
  119. payload={wrapStructuredVarItem(outputItem, schemaType)}
  120. />
  121. ) : (
  122. <VarItem
  123. name={outputItem.name}
  124. // eslint-disable-next-line sonarjs/no-nested-template-literals
  125. type={`${outputItem.type.toLocaleLowerCase()}${schemaType ? ` (${schemaType})` : ''}`}
  126. description={outputItem.description}
  127. isIndent={hasObjectOutput}
  128. />
  129. )}
  130. </div>
  131. )
  132. })}
  133. </>
  134. </OutputVars>
  135. </div>
  136. </div>
  137. )
  138. }
  139. export default React.memo(Panel)