nat.agent.auto_memory_wrapper.agent#
Attributes#
Classes#
Wraps any NAT agent to add automatic memory capture and retrieval. |
Module Contents#
- logger#
- class AutoMemoryWrapperGraph(
- inner_agent_fn: collections.abc.Callable[[nat.data_models.api_server.ChatRequest], collections.abc.Awaitable[Any]],
- memory_editor: nat.memory.interfaces.MemoryEditor,
- save_user_messages: bool = True,
- retrieve_memory: bool = True,
- save_ai_responses: bool = True,
- search_params: dict[str, Any] | None = None,
- add_params: dict[str, Any] | None = None,
Wraps any NAT agent to add automatic memory capture and retrieval.
The wrapper treats the inner agent as a black-box function that receives a ChatRequest with multiple messages (including system messages with memory context). The inner agent manages its own internal state (ReActGraphState, ReWOOGraphState, etc.) - the wrapper never sees it.
- inner_agent_fn#
- memory_editor#
- save_user_messages = True#
- retrieve_memory = True#
- save_ai_responses = True#
- search_params#
- add_params#
- _context#
- _get_user_id_from_context() str#
Extract user_id from runtime context.
Priority order: 1. user_manager.get_id() - For authenticated sessions (set via SessionManager.session()) 2. X-User-ID HTTP header - For testing/simple auth without middleware 3. “default_user” - Fallback for development/testing without authentication
- Returns:
str: The user ID for memory operations
- get_wrapper_node_count() int#
Calculate the number of wrapper nodes that will be added to the graph.
This is used to determine the recursion_limit overhead that the wrapper adds on top of the inner agent’s recursion needs.
- Returns:
int: Number of nodes in the wrapper graph (1-4 depending on configuration)
- static _langchain_message_to_nat_message(
- lc_message: langchain_core.messages.BaseMessage,
Convert LangChain message to NAT Message format.
This is necessary to construct a proper ChatRequest with all messages (including system messages with memory context) to pass to the inner agent.
- async capture_user_message_node( ) nat.agent.auto_memory_wrapper.state.AutoMemoryWrapperState#
Captures user message to memory thread
- async memory_retrieve_node( ) nat.agent.auto_memory_wrapper.state.AutoMemoryWrapperState#
Retrieves relevant memory from memory store
- async inner_agent_node( ) nat.agent.auto_memory_wrapper.state.AutoMemoryWrapperState#
Calls the inner agent with a ChatRequest containing all messages.
The inner agent receives a ChatRequest with multiple messages (including system messages with memory context), processes them using its own internal state (ReActGraphState, ReWOOGraphState, etc.), and returns a ChatResponse.
- async capture_ai_response_node( ) nat.agent.auto_memory_wrapper.state.AutoMemoryWrapperState#
Captures agent response to memory
- build_graph() langgraph.graph.state.CompiledStateGraph#
Wraps inner agent with memory nodes