node.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { type FC, memo, useMemo } from 'react'
  2. import type { NodeProps } from '../../types'
  3. import type { AgentNodeType } from './types'
  4. import { SettingItem } from '../_base/components/setting-item'
  5. import { Group, GroupLabel } from '../_base/components/group'
  6. import type { ToolIconProps } from './components/tool-icon'
  7. import { ToolIcon } from './components/tool-icon'
  8. import useConfig from './use-config'
  9. import { useTranslation } from 'react-i18next'
  10. import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
  11. import { useRenderI18nObject } from '@/hooks/use-i18n'
  12. import { ModelBar } from './components/model-bar'
  13. const AgentNode: FC<NodeProps<AgentNodeType>> = (props) => {
  14. const { inputs, currentStrategy, currentStrategyStatus, pluginDetail } = useConfig(props.id, props.data)
  15. const renderI18nObject = useRenderI18nObject()
  16. const { t } = useTranslation()
  17. const models = useMemo(() => {
  18. if (!inputs) return []
  19. // if selected, show in node
  20. // if required and not selected, show empty selector
  21. // if not required and not selected, show nothing
  22. const models = currentStrategy?.parameters
  23. .filter(param => param.type === FormTypeEnum.modelSelector)
  24. .reduce((acc, param) => {
  25. const item = inputs.agent_parameters?.[param.name]?.value
  26. if (!item) {
  27. if (param.required) {
  28. acc.push({ param: param.name })
  29. return acc
  30. }
  31. else { return acc }
  32. }
  33. acc.push({ provider: item.provider, model: item.model, param: param.name })
  34. return acc
  35. }, [] as Array<{ param: string } | { provider: string, model: string, param: string }>) || []
  36. return models
  37. }, [currentStrategy, inputs])
  38. const tools = useMemo(() => {
  39. const tools: Array<ToolIconProps> = []
  40. currentStrategy?.parameters.forEach((param) => {
  41. if (param.type === FormTypeEnum.toolSelector) {
  42. const field = param.name
  43. const value = inputs.agent_parameters?.[field]?.value
  44. if (value) {
  45. tools.push({
  46. providerName: value.provider_name as any,
  47. })
  48. }
  49. }
  50. if (param.type === FormTypeEnum.multiToolSelector) {
  51. const field = param.name
  52. const value = inputs.agent_parameters?.[field]?.value
  53. if (value) {
  54. (value as unknown as any[]).forEach((item) => {
  55. tools.push({
  56. providerName: item.provider_name,
  57. })
  58. })
  59. }
  60. }
  61. })
  62. return tools
  63. }, [currentStrategy?.parameters, inputs.agent_parameters])
  64. return <div className='mb-1 px-3 py-1 space-y-1'>
  65. {inputs.agent_strategy_name
  66. ? <SettingItem
  67. label={t('workflow.nodes.agent.strategy.shortLabel')}
  68. status={
  69. currentStrategyStatus && !currentStrategyStatus.isExistInPlugin
  70. ? 'error'
  71. : undefined
  72. }
  73. tooltip={
  74. (currentStrategyStatus && !currentStrategyStatus.isExistInPlugin)
  75. ? t('workflow.nodes.agent.strategyNotInstallTooltip', {
  76. plugin: pluginDetail?.declaration.label
  77. ? renderI18nObject(pluginDetail?.declaration.label)
  78. : undefined,
  79. strategy: inputs.agent_strategy_label,
  80. })
  81. : undefined
  82. }
  83. >
  84. {inputs.agent_strategy_label}
  85. </SettingItem>
  86. : <SettingItem label={t('workflow.nodes.agent.strategyNotSet')} />}
  87. {models.length > 0 && <Group
  88. label={<GroupLabel className='mt-1'>
  89. {t('workflow.nodes.agent.model')}
  90. </GroupLabel>}
  91. >
  92. {models.map((model) => {
  93. return <ModelBar
  94. {...model}
  95. key={model.param}
  96. />
  97. })}
  98. </Group>}
  99. {tools.length > 0 && <Group label={<GroupLabel className='mt-1'>
  100. {t('workflow.nodes.agent.toolbox')}
  101. </GroupLabel>}>
  102. <div className='grid grid-cols-10 gap-0.5'>
  103. {tools.map(tool => <ToolIcon {...tool} key={Math.random()} />)}
  104. </div>
  105. </Group>}
  106. </div>
  107. }
  108. AgentNode.displayName = 'AgentNode'
  109. export default memo(AgentNode)