AxCrew provides a state management system that allows sharing data between agents and functions in your crew. The state system is accessible through the state property available on both the AxCrew instance and individual agents.
Key features of the state management system:
Shared State: All agents in a crew share the same state instance
Type Safety: State values are typed with TypeScript
Persistence: State persists across agent interactions
Function Access: Functions can access and modify the shared state
import { AxCrew } from '@amitdeshmukh/ax-crew';const crew = new AxCrew(config);// Set state valuescrew.state.set('name', 'Crew1');crew.state.set('location', 'Earth');// Get state valuesconst name = crew.state.get('name'); // 'Crew1'const location = crew.state.get('location'); // 'Earth'// Get all state valuesconst allState = crew.state.getAll(); // { name: 'Crew1', location: 'Earth' }
Individual agents also have access to the same shared state:
await crew.addAllAgents();const planner = crew.agents.get('Planner');const manager = crew.agents.get('Manager');// Set state from Planner agentplanner.state.set('plan', 'Fly to Mars');// Access the same state value from Manager agentconst plan = manager.state.get('plan'); // 'Fly to Mars'
import { AxCrew, AxCrewFunctions } from '@amitdeshmukh/ax-crew';const crew = new AxCrew(config, AxCrewFunctions);await crew.addAgentsToCrew(['ProfileAgent', 'SupportAgent']);const profileAgent = crew.agents.get('ProfileAgent');const supportAgent = crew.agents.get('SupportAgent');// Initialize workflow with user contextcrew.state.set('user.id', 'user_12345');crew.state.set('session.started', new Date().toISOString());// Step 1: Retrieve user profileconst profileResponse = await profileAgent.forward({ action: 'getUserProfile' });crew.state.set('user.profile', profileResponse.profile);// Step 2: Generate support response using profile from stateconst supportResponse = await supportAgent.forward({ action: 'generateResponse', query: "I need help with my order" // Profile is accessed from state, not passed explicitly});// Store response and complete sessioncrew.state.set('session.response', supportResponse.message);crew.state.set('session.completed', new Date().toISOString());console.log(`Response: ${crew.state.get('session.response')}`);