context.ts


Overview

The context.ts file defines multiple React Contexts used throughout the application to manage and share state related to agent workflows, chat operations, and UI interactions within a React-based flow editor environment. These contexts encapsulate functionalities such as adding nodes to a canvas, showing forms and logs in drawers, caching chat logs, and managing connection handles in a flow UI.

This file centralizes the creation of React Context objects, which provide state and functions to components deeper in the component tree without prop drilling. It leverages custom hooks (useAddNode, useShowFormDrawer, useShowLogSheet, useCacheChatLog) to pick specific functionalities for exposure via contexts.


Detailed Explanation of Contexts and Types

1. AgentFormContext

import { useContext } from 'react';
import { AgentFormContext } from './context';

const MyComponent = () => {
  const nodeType = useContext(AgentFormContext);
  if (!nodeType) return null;
  return <div>Current Node Type: {nodeType.name}</div>;
};

2. AgentInstanceContext

type AgentInstanceContextType = Pick<
  ReturnType<typeof useAddNode>,
  'addCanvasNode'
> & Pick<ReturnType<typeof useShowFormDrawer>, 'showFormDrawer'>;
const { addCanvasNode, showFormDrawer } = useContext(AgentInstanceContext);

addCanvasNode({ id: 'node1', type: 'agent' });
showFormDrawer(true);

3. AgentChatContext

type AgentChatContextType = Pick<
  ReturnType<typeof useShowLogSheet>,
  'showLogSheet'
> & { setLastSendLoadingFunc: (loading: boolean, messageId: string) => void };
const { showLogSheet, setLastSendLoadingFunc } = useContext(AgentChatContext);

showLogSheet(true);
setLastSendLoadingFunc(true, 'msg123');

4. AgentChatLogContext

type AgentChatLogContextType = Pick<
  ReturnType<typeof useCacheChatLog>,
  'addEventList' | 'setCurrentMessageId'
>;
const { addEventList, setCurrentMessageId } = useContext(AgentChatLogContext);

addEventList([{ id: 'evt1', content: 'Hello' }]);
setCurrentMessageId('evt1');

5. HandleContext

export type HandleContextType = {
  nodeId?: string;
  id?: string;
  type: HandleType;
  position: Position;
  isFromConnectionDrag: boolean;
};
const handleContext = useContext(HandleContext);

console.log(handleContext.nodeId, handleContext.type);

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class AgentFormContext {
        <<React.Context<RAGFlowNodeType | undefined>>
    }
    class AgentInstanceContext {
        +addCanvasNode()
        +showFormDrawer()
    }
    class AgentChatContext {
        +showLogSheet()
        +setLastSendLoadingFunc(loading: boolean, messageId: string)
    }
    class AgentChatLogContext {
        +addEventList(events: Event[])
        +setCurrentMessageId(messageId: string)
    }
    class HandleContext {
        +nodeId?: string
        +id?: string
        +type: HandleType
        +position: Position
        +isFromConnectionDrag: boolean
    }

    AgentInstanceContext ..> useAddNode : uses
    AgentInstanceContext ..> useShowFormDrawer : uses
    AgentChatContext ..> useShowLogSheet : uses
    AgentChatContext ..> setLastSendLoadingFunc : manages
    AgentChatLogContext ..> useCacheChatLog : uses

Summary

The context.ts file is a foundational piece in a React-based flow editor and agent chat system. It defines strongly typed React Contexts that expose selective features from various custom hooks to the component tree. These contexts facilitate node management, UI drawer visibility, chat log caching, and connection handle metadata, enabling clean and efficient state sharing and event handling across the application.

Developers working with agent flows, chat logs, or interactive flow editors will interact with these contexts to implement features such as adding nodes, showing forms, managing chats, and handling node connections.


End of Documentation for context.ts