mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-15 17:30:13 +08:00
Introduce session-level state management for tools that need to maintain state across conversation turns within the same session (UMO). - Add ToolSessionManager class for central per-(UMO, tool_name) state - Add ToolSessionState with set_persistent(key) support - Add is_stateful flag to FunctionTool base class - Wire session_manager through run_context in all agent runners - Update ExecuteShellTool to use framework session manager - Update CLAUDE.md with stateful tool documentation BREAKING: Tools that were manually managing session state via _sessions dict should migrate to use the framework's ToolSessionManager for proper lifecycle management and persistence support.
6.1 KiB
6.1 KiB
AstrBot - Claude Code Guidelines
AstrBot is an open-source, all-in-one Agentic personal and group chat assistant supporting multiple IM platforms (QQ, Telegram, Discord, etc.) and LLM providers.
Project Overview
- Main entry:
astrbot/__main__.pyor via CLIastrbot run - CLI commands:
astrbot/cli/commands/ - Core modules:
astrbot/core/ - Platform adapters:
astrbot/core/platform/sources/ - Star plugins:
astrbot/builtin_stars/ - Dashboard:
dashboard/(Vue.js frontend)
Development Setup
# Install dependencies
uv tool install -e . --force
# Initialize AstrBot
astrbot init
# Run development
astrbot run
# Backend only (no WebUI)
astrbot run --backend-only
# Dashboard frontend
cd dashboard && bun dev
# Run tests
uv sync --group dev && uv run pytest --cov=astrbot tests/
Code Style
Python
-
Type hints required - Use Python 3.12+ syntax:
list[str]notList[str]int | NonenotOptional[int]- Avoid
Anywhen possible
-
Path handling - Always use
pathlib.Path:from pathlib import Path # Use astrbot.core.utils.path_utils for data/temp directories from astrbot.core.utils.path_utils import get_astrbot_data_path -
Formatting - Run before committing:
ruff format . ruff check . -
Comments - Use English for all comments and docstrings
-
Imports - Use absolute imports via
astrbot.prefix
Environment Variables
When adding new environment variables:
- Use
ASTRBOT_prefix:ASTRBOT_ENABLE_FEATURE - Add to
.env.examplewith description - Update
astrbot/cli/commands/cmd_run.py:- Add to module docstring under "Environment Variables Used in Project"
- Add to
keys_to_printlist for debug output
Architecture
Core Components
astrbot/core/- Core bot functionalityastrbot/core/platform/- Platform adapter systemastrbot/core/agent/- Agent execution logicastrbot/core/star/- Plugin/Star handler systemastrbot/core/pipeline/- Message processing pipelineastrbot/cli/- Command-line interface
Important Utilities
from astrbot.core.utils.astrbot_path import (
get_astrbot_root, # AstrBot root directory
get_astrbot_data_path, # Data directory
get_astrbot_config_path, # Config directory
get_astrbot_plugin_path, # Plugin directory
get_astrbot_temp_path, # Temp directory
get_astrbot_skills_path, # Skills directory
)
Platform Adapters
Platform adapters are in astrbot/core/platform/sources/:
- Each adapter extends base platform classes
- Use
@register_platform_adapterdecorator - Events flow through
commit_event()to message queue
Star (Plugin) System
Stars are plugins in astrbot/builtin_stars/:
- Extend
Starbase class - Use decorators for command handlers:
@star.on_command,@star.on_message, etc. - Access via
contextobject
Stateful Tool Execution (Session Lifecycle)
Tools can maintain state across conversation turns within a session via ToolSessionManager.
Key classes:
ToolSessionManager(astrbot/core/agent/tool_session_manager.py) — central manager, keyed by(umo, tool_name)ToolSessionState— dict-like per-tool session state withset_persistent(key)supportFunctionTool.is_stateful— opt-in flag for stateful toolsFunctionTool.get_session_state(umo)— get/create session state dict
Usage in a tool:
@dataclass
class MyTool(FunctionTool):
is_stateful = True # declare stateful
async def call(self, context, **kwargs):
umo = context.context.event.unified_msg_origin
state = self.get_session_state(umo)
state["counter"] = state.get("counter", 0) + 1
# Mark to survive session clear:
state.set_persistent("persistent_data")
Architecture flow:
AgentContextWrapper(session_manager=ToolSessionManager())
→ ToolLoopAgentRunner.run_context.session_manager
→ executor.execute(..., session_manager=run_context.session_manager)
→ tool.call(context) # context.session_manager available
Testing
- Tests go in
tests/directory - Use
pytestwithpytest-asyncio - Coverage target:
uv run pytest --cov=astrbot tests/ - Test files:
test_*.pyor*_test.py
Git Conventions
Commit Messages
Use conventional commits:
feat: add new feature
fix: resolve bug
docs: update documentation
refactor: restructure code
test: add tests
chore: maintenance tasks
PR Guidelines
- Title: conventional commit format
- Description: English
- Target branch:
dev - Keep changes focused and atomic
Project-Specific Guidelines
- No report files - Do not add
xxx_SUMMARY.mdor similar - Componentization - Maintain clean code, avoid duplication in WebUI
- Backward compatibility - When deprecating, add warnings
- CLI help - Run
astrbot help --allto see all commands
File Organization
astrbot/
├── __main__.py # Main entry point
├── __init__.py # Package init, exports
├── cli/ # CLI commands
│ └── commands/ # Individual command modules
├── core/ # Core functionality
│ ├── agent/ # Agent execution
│ ├── platform/ # Platform adapters
│ ├── pipeline/ # Message processing
│ ├── star/ # Plugin system
│ └── config/ # Configuration
├── builtin_stars/ # Built-in plugins
├── dashboard/ # Vue.js frontend
└── utils/ # Utilities
Common Tasks
Adding a new platform adapter
- Create adapter in
astrbot/core/platform/sources/ - Extend
Platformbase class - Use
@register_platform_adapterdecorator - Implement required methods:
run(),convert_message(),meta()
Adding a new command
- Add to appropriate module in
cli/commands/ - Register with
@click.command() - Update
astrbot/cli/__main__.pyto add command
Adding a new Star handler
- Create in
astrbot/builtin_stars/or as plugin - Extend
Starclass - Use decorators:
@star.on_command(),@star.on_schedule(), etc.