utils.ts


Overview

utils.ts is a utility module designed to support the construction, manipulation, and management of nodes, edges, and operator parameters within a graph-based flow system, likely related to a RAG (Retrieval-Augmented Generation) or agent workflow application. The file provides helper functions to:

This module interacts primarily with graph data structures (nodes, edges), forms (via Ant Design's FormInstance), and domain-specific interfaces and constants representing operators, node types, and positions.


Detailed Explanations

Imports and Dependencies


Constants and Curry Functions

removeUselessDataInTheOperator

const removeUselessDataInTheOperator = curry(
  (operatorName: string, params: Record<string, unknown>) => {
    if (
      operatorName === Operator.Generate ||
      operatorName === Operator.Categorize
    ) {
      return removeUselessFieldsFromValues(params, '');
    }
    return params;
  },
);

Functions

buildAgentExceptionGoto

function buildAgentExceptionGoto(edges: Edge[], nodeId: string): string[]

buildComponentDownstreamOrUpstream

const buildComponentDownstreamOrUpstream = (
  edges: Edge[],
  nodeId: string,
  isBuildDownstream = true,
  nodes: Node[],
): string[]

buildAgentTools

function buildAgentTools(edges: Edge[], nodes: Node[], nodeId: string): { params: IAgentForm, name?: string, id?: string }

buildCategorize

function buildCategorize(edges: Edge[], nodes: Node[], nodeId: string): ICategorizeForm

buildOperatorParams

const buildOperatorParams = (operatorName: string) => pipe(removeUselessDataInTheOperator(operatorName));

isBottomSubAgent

export function isBottomSubAgent(edges: Edge[], nodeId?: string): boolean

hasSubAgentOrTool

export function hasSubAgentOrTool(edges: Edge[], nodeId?: string): boolean

buildDslComponentsByGraph

export const buildDslComponentsByGraph = (
  nodes: RAGFlowNodeType[],
  edges: Edge[],
  oldDslComponents: DSLComponents,
): DSLComponents

receiveMessageError

export const receiveMessageError = (res: any): boolean

replaceIdWithText

export const replaceIdWithText = (
  obj: Record<string, unknown> | unknown[] | unknown,
  getNameById: (id?: string) => string | undefined,
): unknown

isEdgeEqual

export const isEdgeEqual = (previous: Edge, current: Edge): boolean

buildNewPositionMap

export const buildNewPositionMap = (
  currentKeys: string[],
  previousPositionMap: Record<string, IPosition>,
): { intersectionKeys: string[], newPositionMap: Record<string, IPosition> }

isKeysEqual

export const isKeysEqual = (currentKeys: string[], previousKeys: string[]): boolean

getOperatorIndex

export const getOperatorIndex = (handleTitle: string): string | undefined

getOtherFieldValues

export const getOtherFieldValues = (
  form: FormInstance,
  formListName: string,
  field: FormListFieldData,
  latestField: string,
): any[]

generateSwitchHandleText

export const generateSwitchHandleText = (idx: number): string

getNodeDragHandle

export const getNodeDragHandle = (nodeType?: string): string | undefined

generateNodeNamesWithIncreasingIndex

export const generateNodeNamesWithIncreasingIndex = (
  name: string,
  nodes: RAGFlowNodeType[],
): string

duplicateNodeForm

export const duplicateNodeForm = (nodeData?: RAGFlowNodeType['data']): RAGFlowNodeType['data']

getDrawerWidth

export const getDrawerWidth = (): string | number

needsSingleStepDebugging

export const needsSingleStepDebugging = (label: string): boolean

getRelativePositionToIterationNode

export function getRelativePositionToIterationNode(
  nodes: RAGFlowNodeType[],
  position?: XYPosition,
): { parentId: string, position: XYPosition } | undefined

generateDuplicateNode

export const generateDuplicateNode = (
  position?: XYPosition,
  label?: string,
)

convertToStringArray

export function convertToStringArray(
  list?: Array<{ value: string | number | boolean }>,
): Array<string | number | boolean>

convertToObjectArray

export function convertToObjectArray(
  list: Array<string | number | boolean>,
): Array<{ value: string | number | boolean }>

buildCategorizeListFromObject

export const buildCategorizeListFromObject = (
  categorizeItem: ICategorizeItemResult,
): ICategorizeItem[]

buildCategorizeObjectFromList

export const buildCategorizeObjectFromList = (
  list: Array<ICategorizeItem>,
): ICategorizeItemResult

getAgentNodeTools

export function getAgentNodeTools(agentNode?: RAGFlowNodeType): IAgentForm['tools']

getAgentNodeMCP

export function getAgentNodeMCP(agentNode?: RAGFlowNodeType): IAgentForm['mcp']

mapEdgeMouseEvent

export function mapEdgeMouseEvent(
  edges: Edge[],
  edgeId: string,
  isHovered: boolean,
): Edge[]

buildBeginQueryWithObject

export function buildBeginQueryWithObject(
  inputs: Record<string, BeginQuery>,
  values: BeginQuery[],
): Record<string, BeginQuery>

Important Implementation Details and Algorithms