選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

node.tsx 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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, i) => {
  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. id: `${param.name}-${i}`,
  47. providerName: value.provider_name as any,
  48. })
  49. }
  50. }
  51. if (param.type === FormTypeEnum.multiToolSelector) {
  52. const field = param.name
  53. const value = inputs.agent_parameters?.[field]?.value
  54. if (value) {
  55. (value as unknown as any[]).forEach((item, idx) => {
  56. tools.push({
  57. id: `${param.name}-${idx}`,
  58. providerName: item.provider_name,
  59. })
  60. })
  61. }
  62. }
  63. })
  64. return tools
  65. }, [currentStrategy?.parameters, inputs.agent_parameters])
  66. return <div className='mb-1 space-y-1 px-3 py-1'>
  67. {inputs.agent_strategy_name
  68. ? <SettingItem
  69. label={t('workflow.nodes.agent.strategy.shortLabel')}
  70. status={
  71. currentStrategyStatus && !currentStrategyStatus.isExistInPlugin
  72. ? 'error'
  73. : undefined
  74. }
  75. tooltip={
  76. (currentStrategyStatus && !currentStrategyStatus.isExistInPlugin)
  77. ? t('workflow.nodes.agent.strategyNotInstallTooltip', {
  78. plugin: pluginDetail?.declaration.label
  79. ? renderI18nObject(pluginDetail?.declaration.label)
  80. : undefined,
  81. strategy: inputs.agent_strategy_label,
  82. })
  83. : undefined
  84. }
  85. >
  86. {inputs.agent_strategy_label}
  87. </SettingItem>
  88. : <SettingItem label={t('workflow.nodes.agent.strategyNotSet')} />}
  89. {models.length > 0 && <Group
  90. label={<GroupLabel className='mt-1'>
  91. {t('workflow.nodes.agent.model')}
  92. </GroupLabel>}
  93. >
  94. {models.map((model) => {
  95. return <ModelBar
  96. {...model}
  97. key={model.param}
  98. />
  99. })}
  100. </Group>}
  101. {tools.length > 0 && <Group label={<GroupLabel className='mt-1'>
  102. {t('workflow.nodes.agent.toolbox')}
  103. </GroupLabel>}>
  104. <div className='grid grid-cols-10 gap-0.5'>
  105. {tools.map((tool, i) => <ToolIcon {...tool} key={tool.id + i} />)}
  106. </div>
  107. </Group>}
  108. </div>
  109. }
  110. AgentNode.displayName = 'AgentNode'
  111. export default memo(AgentNode)