Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

use-get-begin-query.tsx 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { RAGFlowNodeType } from '@/interfaces/database/flow';
  2. import { DefaultOptionType } from 'antd/es/select';
  3. import get from 'lodash/get';
  4. import { useCallback, useEffect, useMemo, useState } from 'react';
  5. import { BeginId, Operator } from '../constant';
  6. import { BeginQuery } from '../interface';
  7. import useGraphStore from '../store';
  8. export const useGetBeginNodeDataQuery = () => {
  9. const getNode = useGraphStore((state) => state.getNode);
  10. const getBeginNodeDataQuery = useCallback(() => {
  11. return get(getNode(BeginId), 'data.form.query', []);
  12. }, [getNode]);
  13. return getBeginNodeDataQuery;
  14. };
  15. export const useGetBeginNodeDataQueryIsSafe = () => {
  16. const [isBeginNodeDataQuerySafe, setIsBeginNodeDataQuerySafe] =
  17. useState(false);
  18. const getBeginNodeDataQuery = useGetBeginNodeDataQuery();
  19. const nodes = useGraphStore((state) => state.nodes);
  20. useEffect(() => {
  21. const query: BeginQuery[] = getBeginNodeDataQuery();
  22. const isSafe = !query.some((q) => !q.optional && q.type === 'file');
  23. setIsBeginNodeDataQuerySafe(isSafe);
  24. }, [getBeginNodeDataQuery, nodes]);
  25. return isBeginNodeDataQuerySafe;
  26. };
  27. // exclude nodes with branches
  28. const ExcludedNodes = [
  29. Operator.Categorize,
  30. Operator.Relevant,
  31. Operator.Begin,
  32. Operator.Note,
  33. ];
  34. export const useBuildComponentIdSelectOptions = (
  35. nodeId?: string,
  36. parentId?: string,
  37. ) => {
  38. const nodes = useGraphStore((state) => state.nodes);
  39. const getBeginNodeDataQuery = useGetBeginNodeDataQuery();
  40. const query: BeginQuery[] = getBeginNodeDataQuery();
  41. // Limit the nodes inside iteration to only reference peer nodes with the same parentId and other external nodes other than their parent nodes
  42. const filterChildNodesToSameParentOrExternal = useCallback(
  43. (node: RAGFlowNodeType) => {
  44. // Node inside iteration
  45. if (parentId) {
  46. return (
  47. (node.parentId === parentId || node.parentId === undefined) &&
  48. node.id !== parentId
  49. );
  50. }
  51. return node.parentId === undefined; // The outermost node
  52. },
  53. [parentId],
  54. );
  55. const componentIdOptions = useMemo(() => {
  56. return nodes
  57. .filter(
  58. (x) =>
  59. x.id !== nodeId &&
  60. !ExcludedNodes.some((y) => y === x.data.label) &&
  61. filterChildNodesToSameParentOrExternal(x),
  62. )
  63. .map((x) => ({ label: x.data.name, value: x.id }));
  64. }, [nodes, nodeId, filterChildNodesToSameParentOrExternal]);
  65. const groupedOptions = [
  66. {
  67. label: <span>Component Output</span>,
  68. title: 'Component Output',
  69. options: componentIdOptions,
  70. },
  71. {
  72. label: <span>Begin Input</span>,
  73. title: 'Begin Input',
  74. options: query.map((x) => ({
  75. label: x.name,
  76. value: `begin@${x.key}`,
  77. })),
  78. },
  79. ];
  80. return groupedOptions;
  81. };
  82. export const useGetComponentLabelByValue = (nodeId: string) => {
  83. const options = useBuildComponentIdSelectOptions(nodeId);
  84. const flattenOptions = useMemo(
  85. () =>
  86. options.reduce<DefaultOptionType[]>((pre, cur) => {
  87. return [...pre, ...cur.options];
  88. }, []),
  89. [options],
  90. );
  91. const getLabel = useCallback(
  92. (val?: string) => {
  93. return flattenOptions.find((x) => x.value === val)?.label;
  94. },
  95. [flattenOptions],
  96. );
  97. return getLabel;
  98. };