Skip to content

Commit f8ac994

Browse files
authored
refactor(planner): refactor the meta planner example with the PlanNotebook class (#901)
1 parent 3b67178 commit f8ac994

File tree

19 files changed

+332
-2113
lines changed

19 files changed

+332
-2113
lines changed
Lines changed: 56 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,92 @@
11
# Meta Planner Agent Example
22

3-
An advanced AI agent example that demonstrates sophisticated task planning and execution capabilities using AgentScope. The Meta Planner breaks down complex tasks into manageable subtasks and orchestrates specialized worker agents to complete them efficiently.
3+
In this example, we demonstrate
44

5-
## Overview
5+
- how to build a planner agent that can decompose complex task into manageable subtasks and orchestrates sub-agents to
6+
complete them
7+
- how to handle the printing messages of the sub-agents properly in a multi-agent system
8+
- how to propagate interrupt events from sub agents to the main planner agent
69

7-
The Meta Planner agent is designed to handle complex, multi-step tasks that would be difficult for a simple agent to manage directly. It uses a planning-execution pattern where:
10+
Specifically, in [main.py](./main.py), a planner agent is created with the `PlanNotebook` instance to create and manage
11+
plans. It's equipped with a tool function named `create_worker` in [tool.py](./tool.py) to create sub-agents
12+
dynamically and finish the assigned subtask. The sub-agents are equipped with some basic tools, and some preset
13+
MCP servers to enhance their capabilities.
814

9-
1. **Complex tasks are decomposed** into smaller, manageable subtasks
10-
2. **Worker agents can be dynamically created** with appropriate tools for each subtask
11-
3. **Progress is tracked and managed** through a roadmap system
12-
4. **Results are coordinated** to achieve the overall goal
15+
> We suggest to use AgentScope-Studio to visualize the agent-interactions in this example.
1316
14-
This approach enables handling sophisticated workflows like data analysis, research projects, content creation, and multi-step problem solving.
17+
## Quick Start
1518

16-
## Key Features
17-
18-
- **Intelligent Task Decomposition**: Automatically breaks down complex requests into executable subtasks
19-
- **Progress Tracking**: Maintains a structured roadmap with status tracking for all subtasks
20-
- **Dynamic Worker Management**: Creates and manages specialized worker agents with relevant toolkits
21-
- **State Persistence**: Saves and restores agent state for long-running tasks
22-
- **Flexible Modes**: Can operate in simple ReAct mode or advanced planning mode based on task complexity
23-
24-
## Architecture
25-
26-
### Core Components
27-
28-
1. **MetaPlanner** (`_meta_planner.py`): The main agent class that extends ReActAgent with planning capabilities
29-
2. **Planning Tools** (`_planning_tools/`):
30-
- `PlannerNoteBook`: Manages session context and user inputs
31-
- `RoadmapManager`: Handles task decomposition and progress tracking
32-
- `WorkerManager`: Creates and manages worker agents
33-
3. **System Prompts** (`_built_in_long_sys_prompt/`): Detailed instructions for (worker) agent behavior
34-
4. **Demo Entry Point** (`main.py`): The main function to start the application with meta planner agent.
35-
36-
37-
## Prerequisites for Running This Example
38-
39-
### Required Environment Variables
19+
Install agentscope if you haven't already:
4020

4121
```bash
42-
# Anthropic API key for the Claude model
43-
export ANTHROPIC_API_KEY="your_anthropic_api_key"
44-
45-
# Tavily API key for search functionality
46-
export TAVILY_API_KEY="your_tavily_api_key"
22+
pip install agentscope
4723
```
4824

49-
### Optional Environment Variables
50-
51-
```bash
52-
# Custom working directory for agent operations (default: ./meta_agent_demo_env)
53-
export AGENT_OPERATION_DIR="/path/to/custom/working/directory"
54-
```
25+
Make sure you have set your DashScope API key as an environment variable.
5526

56-
## Usage
27+
In this example, the sub-agents are equipped with the following MCP servers, set the corresponding environment variables to activate them.
28+
If not set, the corresponding MCP will be disabled.
29+
For more details about the tools, refer to [tool.py](./tool.py). You can also add or modify the tools as needed.
5730

58-
### Basic Usage
31+
| MCP | Description | Environment Variable |
32+
|--------------------------|--------------------------------|----------------------|
33+
| AMAP MCP | Provide map related services | GAODE_API_KEY |
34+
| GitHub MCP | Search and access GitHub repos | GITHUB_TOKEN |
35+
| Microsoft Playwright MCP | Web Browser-use MCP server | - |
5936

60-
Run the agent interactively:
37+
Run the example:
6138

6239
```bash
63-
cd examples/agent/meta_planner_agent
6440
python main.py
6541
```
6642

67-
The agent will start in chat mode where you can provide complex tasks. For example:
68-
69-
- "Create a comprehensive analysis of Meta's stock performance in Q1 2025"
70-
- "Research and write a 7-day exercise plan with detailed instructions"
71-
- "Analyze the latest AI trends and create a summary report"
43+
Then you can ask the planner agent to help you complete a complex task, such as "Conduct research on AgentScope repo".
7244

45+
Note for simple questions, the planner agent may directly answer without creating sub-agents.
7346

74-
### Example Interactions
47+
## Advanced Usage
7548

76-
1. **Data Analysis Task**:
77-
```
78-
User: "Analyze the files in my directory and create a summary report"
79-
```
49+
### Handling Sub-agent Output
8050

81-
2. **Research Task**:
82-
```
83-
User: "Research Alibaba's latest quarterly results and competitive position"
84-
```
51+
In this example, the sub-agents won't print messages to the console directly (by `agent.set_console_output_enable(True)` in tool.py).
52+
Instead, its printing messages are streamlined back to the planner agent as the streaming responses of the tool function `create_worker`.
53+
By this way, we only expose the planner agent to the user, rather than multiple agents, which provides a better user experience.
54+
However, the response of the tool function `create_worker` maybe take too much context length if the sub-agent finishes the given task with a long reasoning-acting process.
8555

56+
This figure shows how the sub-agent output is displayed as tool streaming response in AgentScope-Studio:
8657

87-
## Configuration
58+
<details>
59+
<summary>Chinese</summary>
60+
<p align="center">
61+
<img src="./assets/screenshot_zh.jpg"/>
62+
</p>
63+
</details>
8864

89-
### Agent Modes
65+
<details>
66+
<summary>English</summary>
67+
<p align="center">
68+
<img src="./assets/screenshot_en.jpg"/>
69+
</p>
70+
</details>
9071

91-
The Meta Planner supports three operation modes:
9272

93-
- **`dynamic`** (default): Automatically switches between simple ReAct and planning mode based on task complexity
94-
- **`enforced`**: Always uses planning mode for all tasks
95-
- **`disable`**: Only uses simple ReAct mode (no planning capabilities)
9673

97-
### Tool Configuration
74+
Also, you can choose to expose the sub-agent to the user, and only take the structured results back to the planner agent as the tool result of `create_worker`.
9875

99-
The agent uses two main toolkits:
76+
### Propagating Interrupt Events
10077

101-
1. **Planner Toolkit**: Planning-specific tools for task decomposition and worker management
102-
2. **Worker Toolkit**: Comprehensive tools including:
103-
- Shell command execution
104-
- File operations
105-
- Web search (via Tavily)
106-
- Filesystem access (via MCP)
78+
In `ReActAgent`, when the final answer is generated from the `handle_interrupt` function, the metadata field of the return message
79+
will contain a `_is_interrupted` key with value `True` to indicate that the agent is interrupted.
10780

108-
### State Management
81+
By this field, we can propagate the interrupt event from the sub-agent to the main planner agent in the tool function `create_worker`.
82+
For user defined agent classes, you can define your own propagation logic in the `handle_interrupt` function of your agent class.
10983

110-
Agent states are automatically saved during execution:
84+
### Changing the LLM
11185

112-
- **Location**: `./agent-states/run-YYYYMMDDHHMMSS/`
113-
- **Types**:
114-
- `state-post_reasoning-*.json`: After reasoning steps
115-
- `state-post-action-{tool_name}-*.json`: After tool executions
116-
117-
118-
### State Recovery
119-
120-
If an agent gets stuck or fails:
121-
122-
1. Check the latest state file in `./agent-states/`
123-
2. Resume from the last successful state:
124-
```bash
125-
python main.py --load_state path/to/state/file.json
126-
```
127-
128-
## Advanced Customization
129-
130-
### Adding New Tools
131-
132-
1. Create tool functions following AgentScope patterns
133-
2. Register tools in the appropriate toolkit:
134-
```python
135-
worker_toolkit.register_tool_function(your_custom_tool)
136-
```
137-
138-
### Custom MCP Clients
139-
140-
Add additional MCP clients in `main.py`:
141-
142-
```python
143-
mcp_clients.append(
144-
StdIOStatefulClient(
145-
name="custom_mcp",
146-
command="npx",
147-
args=["-y", "your-mcp-server"],
148-
env={"API_KEY": "your_key"},
149-
)
150-
)
151-
```
86+
The example is built with DashScope chat model. If you want to change the model in this example, don't forget
87+
to change the formatter at the same time! The corresponding relationship between built-in models and formatters are
88+
list in [our tutorial](https://doc.agentscope.io/tutorial/task_prompt.html#id1)
15289

153-
### System Prompt Modifications
90+
## Further Reading
15491

155-
Modify prompts in `_built_in_long_sys_prompt/` to customize agent behavior.
92+
- [Plan](https://doc.agentscope.io/tutorial/task_plan.html)

examples/agent/meta_planner_agent/_built_in_long_sys_prompt/_tool_usage_rules.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

examples/agent/meta_planner_agent/_built_in_long_sys_prompt/_worker_additional_sys_prompt.md

Lines changed: 0 additions & 22 deletions
This file was deleted.

examples/agent/meta_planner_agent/_built_in_long_sys_prompt/meta_planner_sys_prompt.md

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)