State API
The State API provides comprehensive state management capabilities for applications, agents, and projects. It allows you to read and update state information across different contexts.
Overview
The state module enables you to:
- Application State: Access global application state
- Agent State: Manage agent-specific state data
- Project State: Handle project-level state information
Quick Start Example
// Get application state
const appState = await codebolt.cbstate.getApplicationState();
console.log('Application state:', appState);
// Add to agent state
await codebolt.cbstate.addToAgentState('lastTask', 'data-processing');
// Get agent state
const agentState = await codebolt.cbstate.getAgentState();
console.log('Agent state:', agentState);
// Update project state
await codebolt.cbstate.updateProjectState('status', 'active');
// Get project state
const projectState = await codebolt.cbstate.getProjectState();
console.log('Project state:', projectState);
Common Use Cases
Task Tracking
Track task progress in agent state:
async function trackTask(taskId, status) {
await codebolt.cbstate.addToAgentState(`task_${taskId}`, status);
const agentState = await codebolt.cbstate.getAgentState();
return agentState;
}
Project Management
Manage project-level state:
async function updateProjectStatus(status, metadata) {
await codebolt.cbstate.updateProjectState('status', status);
await codebolt.cbstate.updateProjectState('metadata', metadata);
return await codebolt.cbstate.getProjectState();
}
Response Structure
State API functions return typed responses:
Application State:
ApplicationState: Global application state object
Agent State:
AddToAgentStateResponse: Result of adding to agent stateGetAgentStateResponse: Current agent state data
Project State:
GetProjectStateResponse: Current project stateUpdateProjectStateResponse: Result of updating project state
- getApplicationState - Gets the current application state.
- addToAgentState - Adds a key-value pair to the agent's state.
- getAgentState - Gets the current state of the agent.
- getProjectState - Gets the current project state.
- updateProjectState - Updates the project state with a key-value pair.