panel.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import type { FC } from 'react'
  2. import React from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import RemoveEffectVarConfirm from '../_base/components/remove-effect-var-confirm'
  5. import useConfig from './use-config'
  6. import type { CodeNodeType } from './types'
  7. import { CodeLanguage } from './types'
  8. import { extractFunctionParams, extractReturnType } from './code-parser'
  9. import VarList from '@/app/components/workflow/nodes/_base/components/variable/var-list'
  10. import OutputVarList from '@/app/components/workflow/nodes/_base/components/variable/output-var-list'
  11. import AddButton from '@/app/components/base/button/add-button'
  12. import Field from '@/app/components/workflow/nodes/_base/components/field'
  13. import Split from '@/app/components/workflow/nodes/_base/components/split'
  14. import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor'
  15. import TypeSelector from '@/app/components/workflow/nodes/_base/components/selector'
  16. import { type NodePanelProps } from '@/app/components/workflow/types'
  17. import BeforeRunForm from '@/app/components/workflow/nodes/_base/components/before-run-form'
  18. import ResultPanel from '@/app/components/workflow/run/result-panel'
  19. const i18nPrefix = 'workflow.nodes.code'
  20. const codeLanguages = [
  21. {
  22. label: 'Python3',
  23. value: CodeLanguage.python3,
  24. },
  25. {
  26. label: 'JavaScript',
  27. value: CodeLanguage.javascript,
  28. },
  29. ]
  30. const Panel: FC<NodePanelProps<CodeNodeType>> = ({
  31. id,
  32. data,
  33. }) => {
  34. const { t } = useTranslation()
  35. const {
  36. readOnly,
  37. inputs,
  38. outputKeyOrders,
  39. handleCodeAndVarsChange,
  40. handleVarListChange,
  41. handleAddVariable,
  42. handleRemoveVariable,
  43. handleCodeChange,
  44. handleCodeLanguageChange,
  45. handleVarsChange,
  46. handleAddOutputVariable,
  47. filterVar,
  48. isShowRemoveVarConfirm,
  49. hideRemoveVarConfirm,
  50. onRemoveVarConfirm,
  51. // single run
  52. isShowSingleRun,
  53. hideSingleRun,
  54. runningStatus,
  55. handleRun,
  56. handleStop,
  57. runResult,
  58. varInputs,
  59. inputVarValues,
  60. setInputVarValues,
  61. } = useConfig(id, data)
  62. const handleGeneratedCode = (value: string) => {
  63. const params = extractFunctionParams(value, inputs.code_language)
  64. const codeNewInput = params.map((p) => {
  65. return {
  66. variable: p,
  67. value_selector: [],
  68. }
  69. })
  70. const returnTypes = extractReturnType(value, inputs.code_language)
  71. handleCodeAndVarsChange(value, codeNewInput, returnTypes)
  72. }
  73. return (
  74. <div className='mt-2'>
  75. <div className='px-4 pb-4 space-y-4'>
  76. <Field
  77. title={t(`${i18nPrefix}.inputVars`)}
  78. operations={
  79. !readOnly ? <AddButton onClick={handleAddVariable} /> : undefined
  80. }
  81. >
  82. <VarList
  83. readonly={readOnly}
  84. nodeId={id}
  85. list={inputs.variables}
  86. onChange={handleVarListChange}
  87. filterVar={filterVar}
  88. isSupportFileVar={false}
  89. />
  90. </Field>
  91. <Split />
  92. <CodeEditor
  93. isInNode
  94. readOnly={readOnly}
  95. title={
  96. <TypeSelector
  97. options={codeLanguages}
  98. value={inputs.code_language}
  99. onChange={handleCodeLanguageChange}
  100. />
  101. }
  102. language={inputs.code_language}
  103. value={inputs.code}
  104. onChange={handleCodeChange}
  105. onGenerated={handleGeneratedCode}
  106. showCodeGenerator={true}
  107. />
  108. </div>
  109. <Split />
  110. <div className='px-4 pt-4 pb-2'>
  111. <Field
  112. title={t(`${i18nPrefix}.outputVars`)}
  113. operations={
  114. <AddButton onClick={handleAddOutputVariable} />
  115. }
  116. >
  117. <OutputVarList
  118. readonly={readOnly}
  119. outputs={inputs.outputs}
  120. outputKeyOrders={outputKeyOrders}
  121. onChange={handleVarsChange}
  122. onRemove={handleRemoveVariable}
  123. />
  124. </Field>
  125. </div>
  126. {
  127. isShowSingleRun && (
  128. <BeforeRunForm
  129. nodeName={inputs.title}
  130. onHide={hideSingleRun}
  131. forms={[
  132. {
  133. inputs: varInputs,
  134. values: inputVarValues,
  135. onChange: setInputVarValues,
  136. },
  137. ]}
  138. runningStatus={runningStatus}
  139. onRun={handleRun}
  140. onStop={handleStop}
  141. result={<ResultPanel {...runResult} showSteps={false} />}
  142. />
  143. )
  144. }
  145. <RemoveEffectVarConfirm
  146. isShow={isShowRemoveVarConfirm}
  147. onCancel={hideRemoveVarConfirm}
  148. onConfirm={onRemoveVarConfirm}
  149. />
  150. </div >
  151. )
  152. }
  153. export default React.memo(Panel)