context.ts

Overview

The context.ts file centralizes the creation and export of multiple React Contexts used throughout an application that appears to implement a flow-based interface, likely involving agents and chat functionality. These contexts provide shared state and handler functions related to agent forms, agent instances, chat interactions, chat logs, and connection handles within a React component tree.

By defining these contexts here, the file enables different components to consume and interact with shared state and functions related to nodes in a flow, chat logs, UI drawer visibility, and handle connections without prop drilling. It serves as a foundational piece for managing and distributing stateful logic in a modular and maintainable way.


Detailed Explanation of Exports

1. AgentFormContext

export const AgentFormContext = createContext<RAGFlowNodeType | undefined>(undefined);

Example Usage:

const agentForm = useContext(AgentFormContext);
if (agentForm) {
  console.log(agentForm.nodeId);
}

2. AgentInstanceContext

export const AgentInstanceContext = createContext<AgentInstanceContextType>(
  {} as AgentInstanceContextType,
);

Example Usage:

const { addCanvasNode, showFormDrawer } = useContext(AgentInstanceContext);
addCanvasNode({ id: 'node1', type: 'agent' });
showFormDrawer(true);

3. AgentChatContext

export const AgentChatContext = createContext<AgentChatContextType>(
  {} as AgentChatContextType,
);

Example Usage:

const { showLogSheet, setLastSendLoadingFunc } = useContext(AgentChatContext);
showLogSheet(true);
setLastSendLoadingFunc(true, 'msg123');

4. AgentChatLogContext

export const AgentChatLogContext = createContext<AgentChatLogContextType>(
  {} as AgentChatLogContextType,
);

Example Usage:

const { addEventList, setCurrentMessageId } = useContext(AgentChatLogContext);
addEventList(newEvents);
setCurrentMessageId('msg456');

5. HandleContext

export const HandleContext = createContext<HandleContextType>(
  {} as HandleContextType,
);

Example Usage:

const handleContext = useContext(HandleContext);
console.log(handleContext.type, handleContext.position);

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

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

Summary

The context.ts file defines and exports multiple React Contexts to manage state and functions related to agent nodes, chat UI, chat logs, and connection handles in a flow-based React application. It integrates closely with custom hooks and a flow UI library to provide a scalable, type-safe mechanism for component communication and state sharing in complex UI workflows.