Core Features
- Persistent Storage: Save Agent, Memory, and other component states
- Simple API: Call
saveTo()/loadFrom()directly on Agent - Multiple Backends: Supports JSON files, in-memory, and custom storage
- Flexible Identification: Use simple string session IDs or custom
SessionKey
Quick Start
Session Implementations
AgentScope provides two Session implementations:JsonSession (Recommended)
Stores state as JSON files on the filesystem.- Directory format: One directory per session
{sessionId}/ - Single states:
{key}.jsonfiles - List states:
{key}.jsonlfiles (JSONL format, incremental append) - UTF-8 encoding, automatic directory creation
⚠️ Security Note:JsonSessionuses thesessionIddirectly as the session directory name. IfsessionIdcomes from untrusted sources (e.g., HTTP cookies or query parameters), an attacker could inject path traversal characters like..or path separators to read/write files outside the intended session directory. Always validate and sanitizesessionIdbefore use - only allow safe characters (alphanumeric, underscore, hyphen) and reject values containing path separators or..sequences.
InMemorySession
Stores state in memory, suitable for testing and single-process temporary scenarios.- State is lost when application restarts
- Not suitable for distributed environments
- Memory usage grows with session count
Agent State Management API
Save Operations
Load Operations
Session Management Operations
Complete Example
Custom Session
Implement theSession interface to create custom storage stores:
Data Format and Migration
Old vs New Data Format Comparison
The new Session API uses a completely different storage format that is incompatible with the oldSessionManager approach:
Old Format Example
New Format Example
memory_messages.jsonl content (one message per line):
Data Migration
Due to format incompatibility, old data cannot be directly read by the new API. If migration is needed, follow these steps:Option 1: Start Fresh (Recommended)
If old data is not important, simply delete old session files and use the new API to create new sessions:Option 2: Manual Migration
If you need to preserve historical conversation data, write a migration script:Option 3: Dual Version Parallel
During transition, keep old code for reading historical data while using new API for new sessions:Database Backend Migration
MySQL Table Structure Change
The new API uses a different table structure. If you previously usedMysqlSession, you need to migrate the table structure:
Old Table Structure:
item_index column enables true incremental list storage:
- Single states: stored with
item_index = 0 - List states: each item stored in a separate row with
item_index = 0, 1, 2, ...
- Backup old data:
- Drop old table:
- Recreate (use
createIfNotExist=truefor automatic creation):
Redis Storage Structure Change
The new API uses a different Redis key structure: Old Structure:- Clear old data (if not needed):
- New data will automatically use the new structure.
More Resources
- Complete Example: SessionExample.java
- State Documentation: state.md
- Agent Configuration: agent-config.md