categorize-node.tsx
Overview
categorize-node.tsx defines a React functional component named CategorizeNode used within a node-based flow or graph editor interface. This component visually represents a "Categorize" node, which is part of a larger workflow or data processing system, likely related to categorization tasks driven by a language model (LLM).
The component integrates with the @xyflow/react library, which provides flowchart/node editor capabilities, enabling users to create and connect nodes visually. It handles rendering the node UI, including input/output connection handles, theming, and dynamic display of categorization endpoints based on the node's data.
Detailed Explanation
Component: CategorizeNode
export function CategorizeNode({
id,
data,
selected,
}: NodeProps<ICategorizeNode>)
Description
The CategorizeNode component renders a customized node with:
A single input handle on the left.
Multiple output handles on the right, dynamically positioned based on node data.
A header displaying the node's name and label.
A label showing which LLM (Language Learning Model) is associated with the node's form data.
This component is designed to work within a flow editor, allowing users to connect this node to others via the handles.
Parameters
id(string): Unique identifier for this node instance in the flow.data(ICategorizeNode): Typed data object containing node-specific properties, includingname,label, and form details such asllm_id.selected(boolean): Indicates if the node is currently selected in the UI, affecting styling.
These props conform to the NodeProps generic type from @xyflow/react.
Returns
JSX element: A styled React section representing the node, including handles, header, and content.
Usage Example
import { CategorizeNode } from './categorize-node';
// Sample node data
const nodeData: ICategorizeNode = {
name: 'Categorization Node 1',
label: 'Category Selector',
form: {
llm_id: 'gpt-4',
},
// ...other properties as defined in ICategorizeNode interface
};
<CategorizeNode
id="node-1"
data={nodeData}
selected={true}
/>
Implementation Details
Key Functional Elements
Handles:
Input handle (
type="target", positioned on the left).Multiple output handles (
type="source", positioned on the right), generated dynamically using thepositionsarray.
Dynamic Handle Positioning:
Uses a custom hook
useBuildCategorizeHandlePositionswhich calculates the vertical positions (topproperty) and text labels for each output handle based ondataandid. This allows flexible handle layout depending on node content.
Theming:
Uses a custom
useThemehook to apply light/dark mode styling.Conditional CSS classes apply based on the theme and selection status.
Node Header:
Uses a separate
NodeHeadercomponent to render the node's title and label prominently.
LLM Label:
Displays the LLM associated with the node form via the
LLMLabelcomponent, showing which language model is involved.
Styling:
Styling is imported from a CSS module (
index.less) providing scoped styles.Uses
classnameslibrary to conditionally apply styles.Custom inline style
RightHandleStyleis applied to right handles for consistent appearance.
External Dependencies
@xyflow/react: Provides node/handle components and types for building flowchart nodes.antd: Flex layout component for vertical stacking with spacing.lodash.get: Safely accesses nested properties likedata.form.llm_id.Local components/hooks:
LLMLabel: Displays LLM identifier.NodeHeader: Renders node title and label.useBuildCategorizeHandlePositions: Computes output handle positions.useTheme: Provides theme context.RightHandleStyle: Style constants for right-positioned handles.
Interaction with Other Parts of the System
Flow Editor Integration:
This component is designed to be used inside a flow editor canvas managed by@xyflow/react. It relies on node IDs and data passed by the flow infrastructure.Node Data Model:
The node data adheres to theICategorizeNodeinterface, which is part of the system's domain model representing categorization logic.Theming and Styling:
The component adapts based on theme context from the application's theme provider.Dynamic Handle Positions:
The node interacts withuseBuildCategorizeHandlePositionshook to calculate how many output handles to display and their positions, reflecting dynamic node configuration.LLM Integration:
Displays the LLM linked with the node, indicating integration with language model selection components.
Visual Diagram
componentDiagram
direction LR
CategorizeNode -- uses --> Handle
CategorizeNode -- uses --> NodeHeader
CategorizeNode -- uses --> LLMLabel
CategorizeNode -- uses --> useTheme
CategorizeNode -- uses --> useBuildCategorizeHandlePositions
CategorizeNode -- uses --> classNames
CategorizeNode -- uses --> lodash.get
CategorizeNode -- styled by --> styles(index.less)
Handle <|-- LeftInputHandle
Handle <|-- RightOutputHandle
Summary
The CategorizeNode component is a reusable UI node element for a flow editor that represents a categorization step in a data or language model-driven workflow. It features dynamic connection points, theme-aware styling, and integration with LLM identifiers. Its modular design and use of hooks and subcomponents make it highly adaptable within the larger application’s node-based flow system.