use-values.ts
Overview
The use-values.ts file defines a custom React hook named useValues designed to extract and memoize a list of value keys associated with a specific tool in a graph-based agent system. It leverages a centralized graph state store to retrieve the currently selected node and tool identifiers, then finds upstream node information and its associated "MCP" (likely a domain-specific term) metadata to produce a list of relevant keys.
This hook is primarily used to provide reactive and efficient access to the keys of form data related to a selected tool on a selected node. It benefits components that need to render or manipulate these keys without duplicating graph traversal or lookup logic.
Exports
useValues()
Description
useValues is a React hook that returns an object containing an items array. This array consists of the keys from the tool-related form data found in the upstream node corresponding to the currently selected node and tool in the graph.
Parameters
This hook takes no parameters directly. Instead, it internally accesses the global graph state store to get:
clickedNodeId— The ID of the currently selected node.clickedToolId— The ID of the currently selected tool.findUpstreamNodeById— A function to retrieve an upstream node by its ID.
Returns
{
items: string[]
}
items: An array of strings representing the keys of the tool's form data. If no form data is found, this will be an empty array.
Usage Example
import { useValues } from './use-values';
function ToolValuesList() {
const { items } = useValues();
return (
<ul>
{items.length === 0 && <li>No values available</li>}
{items.map((key) => (
<li key={key}>{key}</li>
))}
</ul>
);
}
Implementation Details
State Store Access: The hook uses
useGraphStore(imported from the agent page's store module) to access the current graph-related state: the IDs of the clicked node and tool, and a function to find upstream nodes.Finding Upstream Node: It calls
findUpstreamNodeById(clickedNodeId)to retrieve the node object upstream of the clicked node.Getting MCP Data: It calls
getAgentNodeMCP(agentNode)(from utils) to obtain the "MCP list" related to the node. This MCP list is presumably an array of metadata objects related to the node's tools.Extracting Form Data: The hook searches the MCP list for an entry where
mcp_idmatches theclickedToolId. If found, it extracts thetoolsproperty from that entry, which is expected to be an object containing key-value pairs.Memoization: The hook uses React's
useMemoto avoid recomputing the returned values unless any ofclickedNodeId,clickedToolId, orfindUpstreamNodeByIdchange.Empty Check: It uses Lodash's
isEmptyfunction to check if the form data object is empty. If empty, the hook returns{ items: [] }.
Interaction with Other Parts of the System
useGraphStore: This hook depends on the global graph store to obtain the current selection and graph traversal utilities. Changes in the graph state will trigger recomputation of the hook's returned value.getAgentNodeMCP: A utility function that extracts or computes the MCP list from a given node. The hook depends on this to get tool metadata.lodash.isEmpty: Used for robust empty object checking.React Components: Components consuming this hook gain reactive access to the keys of the selected tool's form data, enabling dynamic UI rendering based on graph selection.
Summary
use-values.ts encapsulates logic to retrieve and memoize a list of keys describing form data associated with a selected tool on a selected node in a graph. It abstracts away direct store interactions and graph traversals, providing a clean and efficient API for React components.
Mermaid Diagram
flowchart TD
A[useValues Hook]
A --> B[useGraphStore]
B --> C{State}
C -->|clickedNodeId| D[Node ID]
C -->|clickedToolId| E[Tool ID]
C -->|findUpstreamNodeById| F[Function]
A --> G[findUpstreamNodeById(clickedNodeId)]
G --> H[agentNode]
A --> I[getAgentNodeMCP(agentNode)]
I --> J[mcpList]
J --> K[mcpList.find(entry where entry.mcp_id === clickedToolId)]
K --> L[formData.tools or {}]
L --> M{isEmpty(formData)}
M -->|true| N[Return { items: [] }]
M -->|false| O[Return { items: Object.keys(formData) }]
Notes
The file assumes the existence of a global graph store and utility functions from the agent page context.
The concept of "MCP" is domain-specific and should be understood from surrounding code or documentation.
The hook is optimized for React functional components using hooks and memoization best practices.