panel.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import type { FC } from 'react'
  2. import { memo, useMemo } from 'react'
  3. import type { NodePanelProps } from '../../types'
  4. import type { AgentNodeType } from './types'
  5. import Field from '../_base/components/field'
  6. import { AgentStrategy } from '../_base/components/agent-strategy'
  7. import useConfig from './use-config'
  8. import { useTranslation } from 'react-i18next'
  9. import OutputVars, { VarItem } from '../_base/components/output-vars'
  10. import type { StrategyParamItem } from '@/app/components/plugins/types'
  11. import type { CredentialFormSchema } from '@/app/components/header/account-setting/model-provider-page/declarations'
  12. import BeforeRunForm from '@/app/components/workflow/nodes/_base/components/before-run-form'
  13. import ResultPanel from '@/app/components/workflow/run/result-panel'
  14. import formatTracing from '@/app/components/workflow/run/utils/format-log'
  15. import { useLogs } from '@/app/components/workflow/run/hooks'
  16. import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form'
  17. import { toType } from '@/app/components/tools/utils/to-form-schema'
  18. import { useStore } from '../../store'
  19. const i18nPrefix = 'workflow.nodes.agent'
  20. export function strategyParamToCredientialForm(param: StrategyParamItem): CredentialFormSchema {
  21. return {
  22. ...param as any,
  23. variable: param.name,
  24. show_on: [],
  25. type: toType(param.type),
  26. }
  27. }
  28. const AgentPanel: FC<NodePanelProps<AgentNodeType>> = (props) => {
  29. const {
  30. inputs,
  31. setInputs,
  32. currentStrategy,
  33. formData,
  34. onFormChange,
  35. availableNodesWithParent,
  36. availableVars,
  37. isShowSingleRun,
  38. hideSingleRun,
  39. runningStatus,
  40. handleRun,
  41. handleStop,
  42. runResult,
  43. runInputData,
  44. setRunInputData,
  45. varInputs,
  46. outputSchema,
  47. } = useConfig(props.id, props.data)
  48. const { t } = useTranslation()
  49. const nodeInfo = useMemo(() => {
  50. if (!runResult)
  51. return
  52. return formatTracing([runResult], t)[0]
  53. }, [runResult, t])
  54. const logsParams = useLogs()
  55. const singleRunForms = (() => {
  56. const forms: FormProps[] = []
  57. if (varInputs.length > 0) {
  58. forms.push(
  59. {
  60. label: t(`${i18nPrefix}.singleRun.variable`)!,
  61. inputs: varInputs,
  62. values: runInputData,
  63. onChange: setRunInputData,
  64. },
  65. )
  66. }
  67. return forms
  68. })()
  69. const resetEditor = useStore(s => s.setControlPromptEditorRerenderKey)
  70. return <div className='my-2'>
  71. <Field title={t('workflow.nodes.agent.strategy.label')} className='px-4 py-2' tooltip={t('workflow.nodes.agent.strategy.tooltip')} >
  72. <AgentStrategy
  73. strategy={inputs.agent_strategy_name ? {
  74. agent_strategy_provider_name: inputs.agent_strategy_provider_name!,
  75. agent_strategy_name: inputs.agent_strategy_name!,
  76. agent_strategy_label: inputs.agent_strategy_label!,
  77. agent_output_schema: inputs.output_schema,
  78. plugin_unique_identifier: inputs.plugin_unique_identifier!,
  79. } : undefined}
  80. onStrategyChange={(strategy) => {
  81. setInputs({
  82. ...inputs,
  83. agent_strategy_provider_name: strategy?.agent_strategy_provider_name,
  84. agent_strategy_name: strategy?.agent_strategy_name,
  85. agent_strategy_label: strategy?.agent_strategy_label,
  86. output_schema: strategy!.agent_output_schema,
  87. plugin_unique_identifier: strategy!.plugin_unique_identifier,
  88. agent_parameters: {},
  89. })
  90. resetEditor(Date.now())
  91. }}
  92. formSchema={currentStrategy?.parameters?.map(strategyParamToCredientialForm) || []}
  93. formValue={formData}
  94. onFormValueChange={onFormChange}
  95. nodeOutputVars={availableVars}
  96. availableNodes={availableNodesWithParent}
  97. nodeId={props.id}
  98. />
  99. </Field>
  100. <div>
  101. <OutputVars>
  102. <VarItem
  103. name='text'
  104. type='String'
  105. description={t(`${i18nPrefix}.outputVars.text`)}
  106. />
  107. <VarItem
  108. name='files'
  109. type='Array[File]'
  110. description={t(`${i18nPrefix}.outputVars.files.title`)}
  111. />
  112. <VarItem
  113. name='json'
  114. type='Array[Object]'
  115. description={t(`${i18nPrefix}.outputVars.json`)}
  116. />
  117. {outputSchema.map(({ name, type, description }) => (
  118. <VarItem
  119. key={name}
  120. name={name}
  121. type={type}
  122. description={description}
  123. />
  124. ))}
  125. </OutputVars>
  126. </div>
  127. {
  128. isShowSingleRun && (
  129. <BeforeRunForm
  130. nodeName={inputs.title}
  131. nodeType={inputs.type}
  132. onHide={hideSingleRun}
  133. forms={singleRunForms}
  134. runningStatus={runningStatus}
  135. onRun={handleRun}
  136. onStop={handleStop}
  137. {...logsParams}
  138. result={<ResultPanel {...runResult} nodeInfo={nodeInfo} showSteps={false} {...logsParams} />}
  139. />
  140. )
  141. }
  142. </div>
  143. }
  144. AgentPanel.displayName = 'AgentPanel'
  145. export default memo(AgentPanel)