Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

panel.tsx 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. agent_parameters: {},
  99. })
  100. resetEditor(Date.now())
  101. }}
  102. formSchema={currentStrategy?.parameters?.map(strategyParamToCredientialForm) || []}
  103. formValue={formData}
  104. onFormValueChange={onFormChange}
  105. nodeOutputVars={availableVars}
  106. availableNodes={availableNodesWithParent}
  107. nodeId={props.id}
  108. />
  109. </Field>
  110. <div className='px-4 py-2'>
  111. {isChatMode && currentStrategy?.features?.includes(AgentFeature.HISTORY_MESSAGES) && (
  112. <>
  113. <Split />
  114. <MemoryConfig
  115. className='mt-4'
  116. readonly={readOnly}
  117. config={{ data: inputs.memory }}
  118. onChange={handleMemoryChange}
  119. canSetRoleName={false}
  120. />
  121. </>
  122. )}
  123. </div>
  124. <div>
  125. <OutputVars>
  126. <VarItem
  127. name='text'
  128. type='String'
  129. description={t(`${i18nPrefix}.outputVars.text`)}
  130. />
  131. <VarItem
  132. name='files'
  133. type='Array[File]'
  134. description={t(`${i18nPrefix}.outputVars.files.title`)}
  135. />
  136. <VarItem
  137. name='json'
  138. type='Array[Object]'
  139. description={t(`${i18nPrefix}.outputVars.json`)}
  140. />
  141. {outputSchema.map(({ name, type, description }) => (
  142. <VarItem
  143. key={name}
  144. name={name}
  145. type={type}
  146. description={description}
  147. />
  148. ))}
  149. </OutputVars>
  150. </div>
  151. {
  152. isShowSingleRun && (
  153. <BeforeRunForm
  154. nodeName={inputs.title}
  155. nodeType={inputs.type}
  156. onHide={hideSingleRun}
  157. forms={singleRunForms}
  158. runningStatus={runningStatus}
  159. onRun={handleRun}
  160. onStop={handleStop}
  161. {...logsParams}
  162. result={<ResultPanel {...runResult} nodeInfo={nodeInfo} showSteps={false} {...logsParams} />}
  163. />
  164. )
  165. }
  166. </div>
  167. }
  168. AgentPanel.displayName = 'AgentPanel'
  169. export default memo(AgentPanel)