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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import type { FC } from 'react'
  2. import { memo, useMemo } from 'react'
  3. import type { NodePanelProps } from '../../types'
  4. import { AgentFeature, 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. import Split from '../_base/components/split'
  20. import MemoryConfig from '../_base/components/memory-config'
  21. const i18nPrefix = 'workflow.nodes.agent'
  22. export function strategyParamToCredientialForm(param: StrategyParamItem): CredentialFormSchema {
  23. return {
  24. ...param as any,
  25. variable: param.name,
  26. show_on: [],
  27. type: toType(param.type),
  28. tooltip: param.help,
  29. }
  30. }
  31. const AgentPanel: FC<NodePanelProps<AgentNodeType>> = (props) => {
  32. const {
  33. inputs,
  34. setInputs,
  35. currentStrategy,
  36. formData,
  37. onFormChange,
  38. isChatMode,
  39. availableNodesWithParent,
  40. availableVars,
  41. readOnly,
  42. isShowSingleRun,
  43. hideSingleRun,
  44. runningStatus,
  45. handleRun,
  46. handleStop,
  47. runResult,
  48. runInputData,
  49. setRunInputData,
  50. varInputs,
  51. outputSchema,
  52. handleMemoryChange,
  53. } = useConfig(props.id, props.data)
  54. const { t } = useTranslation()
  55. const nodeInfo = useMemo(() => {
  56. if (!runResult)
  57. return
  58. return formatTracing([runResult], t)[0]
  59. }, [runResult, t])
  60. const logsParams = useLogs()
  61. const singleRunForms = (() => {
  62. const forms: FormProps[] = []
  63. if (varInputs.length > 0) {
  64. forms.push(
  65. {
  66. label: t(`${i18nPrefix}.singleRun.variable`)!,
  67. inputs: varInputs,
  68. values: runInputData,
  69. onChange: setRunInputData,
  70. },
  71. )
  72. }
  73. return forms
  74. })()
  75. const resetEditor = useStore(s => s.setControlPromptEditorRerenderKey)
  76. return <div className='my-2'>
  77. <Field
  78. required
  79. title={t('workflow.nodes.agent.strategy.label')}
  80. className='px-4 py-2'
  81. tooltip={t('workflow.nodes.agent.strategy.tooltip')} >
  82. <AgentStrategy
  83. strategy={inputs.agent_strategy_name ? {
  84. agent_strategy_provider_name: inputs.agent_strategy_provider_name!,
  85. agent_strategy_name: inputs.agent_strategy_name!,
  86. agent_strategy_label: inputs.agent_strategy_label!,
  87. agent_output_schema: inputs.output_schema,
  88. plugin_unique_identifier: inputs.plugin_unique_identifier!,
  89. } : undefined}
  90. onStrategyChange={(strategy) => {
  91. setInputs({
  92. ...inputs,
  93. agent_strategy_provider_name: strategy?.agent_strategy_provider_name,
  94. agent_strategy_name: strategy?.agent_strategy_name,
  95. agent_strategy_label: strategy?.agent_strategy_label,
  96. output_schema: strategy!.agent_output_schema,
  97. plugin_unique_identifier: strategy!.plugin_unique_identifier,
  98. })
  99. resetEditor(Date.now())
  100. }}
  101. formSchema={currentStrategy?.parameters?.map(strategyParamToCredientialForm) || []}
  102. formValue={formData}
  103. onFormValueChange={onFormChange}
  104. nodeOutputVars={availableVars}
  105. availableNodes={availableNodesWithParent}
  106. nodeId={props.id}
  107. />
  108. </Field>
  109. <div className='px-4 py-2'>
  110. {isChatMode && currentStrategy?.features?.includes(AgentFeature.HISTORY_MESSAGES) && (
  111. <>
  112. <Split />
  113. <MemoryConfig
  114. className='mt-4'
  115. readonly={readOnly}
  116. config={{ data: inputs.memory }}
  117. onChange={handleMemoryChange}
  118. canSetRoleName={false}
  119. />
  120. </>
  121. )}
  122. </div>
  123. <div>
  124. <OutputVars>
  125. <VarItem
  126. name='text'
  127. type='String'
  128. description={t(`${i18nPrefix}.outputVars.text`)}
  129. />
  130. <VarItem
  131. name='files'
  132. type='Array[File]'
  133. description={t(`${i18nPrefix}.outputVars.files.title`)}
  134. />
  135. <VarItem
  136. name='json'
  137. type='Array[Object]'
  138. description={t(`${i18nPrefix}.outputVars.json`)}
  139. />
  140. {outputSchema.map(({ name, type, description }) => (
  141. <VarItem
  142. key={name}
  143. name={name}
  144. type={type}
  145. description={description}
  146. />
  147. ))}
  148. </OutputVars>
  149. </div>
  150. {
  151. isShowSingleRun && (
  152. <BeforeRunForm
  153. nodeName={inputs.title}
  154. nodeType={inputs.type}
  155. onHide={hideSingleRun}
  156. forms={singleRunForms}
  157. runningStatus={runningStatus}
  158. onRun={handleRun}
  159. onStop={handleStop}
  160. {...logsParams}
  161. result={<ResultPanel {...runResult} nodeInfo={nodeInfo} showSteps={false} {...logsParams} />}
  162. />
  163. )
  164. }
  165. </div>
  166. }
  167. AgentPanel.displayName = 'AgentPanel'
  168. export default memo(AgentPanel)