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.

agent-node.tsx 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import LLMLabel from '@/components/llm-select/llm-label';
  2. import { IAgentNode } from '@/interfaces/database/flow';
  3. import { Handle, NodeProps, Position } from '@xyflow/react';
  4. import { get } from 'lodash';
  5. import { memo, useMemo } from 'react';
  6. import { useTranslation } from 'react-i18next';
  7. import { AgentExceptionMethod, NodeHandleId } from '../../constant';
  8. import useGraphStore from '../../store';
  9. import { isBottomSubAgent } from '../../utils';
  10. import { CommonHandle } from './handle';
  11. import { LeftHandleStyle, RightHandleStyle } from './handle-icon';
  12. import styles from './index.less';
  13. import NodeHeader from './node-header';
  14. import { NodeWrapper } from './node-wrapper';
  15. import { ToolBar } from './toolbar';
  16. function InnerAgentNode({
  17. id,
  18. data,
  19. isConnectable = true,
  20. selected,
  21. }: NodeProps<IAgentNode>) {
  22. const edges = useGraphStore((state) => state.edges);
  23. const { t } = useTranslation();
  24. const isHeadAgent = useMemo(() => {
  25. return !isBottomSubAgent(edges, id);
  26. }, [edges, id]);
  27. const exceptionMethod = useMemo(() => {
  28. return get(data, 'form.exception_method');
  29. }, [data]);
  30. const isGotoMethod = useMemo(() => {
  31. return exceptionMethod === AgentExceptionMethod.Goto;
  32. }, [exceptionMethod]);
  33. return (
  34. <ToolBar selected={selected} id={id} label={data.label}>
  35. <NodeWrapper selected={selected}>
  36. {isHeadAgent && (
  37. <>
  38. <CommonHandle
  39. type="target"
  40. position={Position.Left}
  41. isConnectable={isConnectable}
  42. style={LeftHandleStyle}
  43. nodeId={id}
  44. id={NodeHandleId.End}
  45. ></CommonHandle>
  46. <CommonHandle
  47. type="source"
  48. position={Position.Right}
  49. isConnectable={isConnectable}
  50. className={styles.handle}
  51. style={RightHandleStyle}
  52. nodeId={id}
  53. id={NodeHandleId.Start}
  54. isConnectableEnd={false}
  55. ></CommonHandle>
  56. </>
  57. )}
  58. <Handle
  59. type="target"
  60. position={Position.Top}
  61. isConnectable={false}
  62. id={NodeHandleId.AgentTop}
  63. ></Handle>
  64. <Handle
  65. type="source"
  66. position={Position.Bottom}
  67. isConnectable={false}
  68. id={NodeHandleId.AgentBottom}
  69. style={{ left: 180 }}
  70. ></Handle>
  71. <Handle
  72. type="source"
  73. position={Position.Bottom}
  74. isConnectable={false}
  75. id={NodeHandleId.Tool}
  76. style={{ left: 20 }}
  77. ></Handle>
  78. <NodeHeader id={id} name={data.name} label={data.label}></NodeHeader>
  79. <section className="flex flex-col gap-2">
  80. <div className={'bg-bg-card rounded-sm p-1'}>
  81. <LLMLabel value={get(data, 'form.llm_id')}></LLMLabel>
  82. </div>
  83. {(isGotoMethod ||
  84. exceptionMethod === AgentExceptionMethod.Comment) && (
  85. <div className="bg-bg-card rounded-sm p-1 flex justify-between gap-2">
  86. <span className="text-text-secondary">On Failure</span>
  87. <span className="truncate flex-1 text-right">
  88. {t(`flow.${exceptionMethod}`)}
  89. </span>
  90. </div>
  91. )}
  92. </section>
  93. {isGotoMethod && (
  94. <CommonHandle
  95. type="source"
  96. position={Position.Right}
  97. isConnectable={isConnectable}
  98. className="!bg-state-error"
  99. style={{ ...RightHandleStyle, top: 94 }}
  100. nodeId={id}
  101. id={NodeHandleId.AgentException}
  102. isConnectableEnd={false}
  103. ></CommonHandle>
  104. )}
  105. </NodeWrapper>
  106. </ToolBar>
  107. );
  108. }
  109. export const AgentNode = memo(InnerAgentNode);