|
| 1 | +# example-tutorial - AgentEx Temporal Agent Template |
| 2 | + |
| 3 | +This is a starter template for building asynchronous agents with the AgentEx framework and Temporal. It provides a basic implementation of the Agent 2 Client Protocol (ACP) with Temporal workflow support to help you get started quickly. |
| 4 | + |
| 5 | +## What You'll Learn |
| 6 | + |
| 7 | +- **Tasks**: A task is a grouping mechanism for related messages. Think of it as a conversation thread or a session. |
| 8 | +- **Messages**: Messages are communication objects within a task. They can contain text, data, or instructions. |
| 9 | +- **ACP Events**: The agent responds to four main events: |
| 10 | + - `task_received`: When a new task is created |
| 11 | + - `task_message_received`: When a message is sent within a task |
| 12 | + - `task_approved`: When a task is approved |
| 13 | + - `task_canceled`: When a task is canceled |
| 14 | +- **Temporal Workflows**: Long-running processes that can handle complex state management and async operations |
| 15 | + |
| 16 | +## Running the Agent |
| 17 | + |
| 18 | +1. Run the agent locally: |
| 19 | +```bash |
| 20 | +agentex agents run --manifest manifest.yaml |
| 21 | +``` |
| 22 | + |
| 23 | +The agent will start on port 8000 and print messages whenever it receives any of the ACP events. |
| 24 | + |
| 25 | +## What's Inside |
| 26 | + |
| 27 | +This template: |
| 28 | +- Sets up a basic ACP server with Temporal integration |
| 29 | +- Handles each of the required ACP events |
| 30 | +- Provides a foundation for building complex async agents |
| 31 | +- Includes Temporal workflow and activity definitions |
| 32 | + |
| 33 | +## Next Steps |
| 34 | + |
| 35 | +For more advanced agent development, check out the AgentEx tutorials: |
| 36 | + |
| 37 | +- **Tutorials 00-08**: Learn about building synchronous agents with ACP |
| 38 | +- **Tutorials 09-10**: Learn how to use Temporal to power asynchronous agents |
| 39 | + - Tutorial 09: Basic Temporal workflow setup |
| 40 | + - Tutorial 10: Advanced Temporal patterns and best practices |
| 41 | + |
| 42 | +These tutorials will help you understand: |
| 43 | +- How to handle long-running tasks |
| 44 | +- Implementing state machines |
| 45 | +- Managing complex workflows |
| 46 | +- Best practices for async agent development |
| 47 | + |
| 48 | +## The Manifest File |
| 49 | + |
| 50 | +The `manifest.yaml` file is your agent's configuration file. It defines: |
| 51 | +- How your agent should be built and packaged |
| 52 | +- What files are included in your agent's Docker image |
| 53 | +- Your agent's name and description |
| 54 | +- Local development settings (like the port your agent runs on) |
| 55 | +- Temporal worker configuration |
| 56 | + |
| 57 | +This file is essential for both local development and deployment of your agent. |
| 58 | + |
| 59 | +## Project Structure |
| 60 | + |
| 61 | +``` |
| 62 | +example_tutorial/ |
| 63 | +├── project/ # Your agent's code |
| 64 | +│ ├── __init__.py |
| 65 | +│ ├── acp.py # ACP server and event handlers |
| 66 | +│ ├── workflow.py # Temporal workflow definitions |
| 67 | +│ ├── activities.py # Temporal activity definitions |
| 68 | +│ └── run_worker.py # Temporal worker setup |
| 69 | +├── Dockerfile # Container definition |
| 70 | +├── manifest.yaml # Deployment config |
| 71 | +├── dev.ipynb # Development notebook for testing |
| 72 | +
|
| 73 | +└── pyproject.toml # Dependencies (uv) |
| 74 | +
|
| 75 | +``` |
| 76 | + |
| 77 | +## Development |
| 78 | + |
| 79 | +### 1. Customize Event Handlers |
| 80 | +- Modify the handlers in `acp.py` to implement your agent's logic |
| 81 | +- Add your own tools and capabilities |
| 82 | +- Implement custom state management |
| 83 | + |
| 84 | +### 2. Test Your Agent with the Development Notebook |
| 85 | +Use the included `dev.ipynb` Jupyter notebook to test your agent interactively: |
| 86 | + |
| 87 | +```bash |
| 88 | +# Start Jupyter notebook (make sure you have jupyter installed) |
| 89 | +jupyter notebook dev.ipynb |
| 90 | + |
| 91 | +# Or use VS Code to open the notebook directly |
| 92 | +code dev.ipynb |
| 93 | +``` |
| 94 | + |
| 95 | +The notebook includes: |
| 96 | +- **Setup**: Connect to your local AgentEx backend |
| 97 | +- **Task creation**: Create a new task for the conversation |
| 98 | +- **Event sending**: Send events to the agent and get responses |
| 99 | +- **Async message subscription**: Subscribe to server-side events to receive agent responses |
| 100 | +- **Rich message display**: Beautiful formatting with timestamps and author information |
| 101 | + |
| 102 | +The notebook automatically uses your agent name (`example-tutorial`) and demonstrates the agentic ACP workflow: create task → send event → subscribe to responses. |
| 103 | + |
| 104 | +### 3. Develop Temporal Workflows |
| 105 | +- Edit `workflow.py` to define your agent's async workflow logic |
| 106 | +- Modify `activities.py` to add custom activities |
| 107 | +- Use `run_worker.py` to configure the Temporal worker |
| 108 | + |
| 109 | +### 4. Manage Dependencies |
| 110 | + |
| 111 | + |
| 112 | +You chose **uv** for package management. Here's how to work with dependencies: |
| 113 | + |
| 114 | +```bash |
| 115 | +# Add new dependencies |
| 116 | +agentex uv add requests openai anthropic |
| 117 | + |
| 118 | +# Add Temporal-specific dependencies (already included) |
| 119 | +agentex uv add temporalio |
| 120 | + |
| 121 | +# Install/sync dependencies |
| 122 | +agentex uv sync |
| 123 | + |
| 124 | +# Run commands with uv |
| 125 | +uv run agentex agents run --manifest manifest.yaml |
| 126 | +``` |
| 127 | + |
| 128 | +**Benefits of uv:** |
| 129 | +- Faster dependency resolution and installation |
| 130 | +- Better dependency isolation |
| 131 | +- Modern Python packaging standards |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | +### 5. Configure Credentials |
| 136 | +- Add any required credentials to your manifest.yaml |
| 137 | +- For local development, create a `.env` file in the project directory |
| 138 | +- Use `load_dotenv()` only in development mode: |
| 139 | + |
| 140 | +```python |
| 141 | +import os |
| 142 | +from dotenv import load_dotenv |
| 143 | + |
| 144 | +if os.environ.get("ENVIRONMENT") == "development": |
| 145 | + load_dotenv() |
| 146 | +``` |
| 147 | + |
| 148 | +## Local Development |
| 149 | + |
| 150 | +### 1. Start the Agentex Backend |
| 151 | +```bash |
| 152 | +# Navigate to the backend directory |
| 153 | +cd agentex |
| 154 | + |
| 155 | +# Start all services using Docker Compose |
| 156 | +make dev |
| 157 | + |
| 158 | +# Optional: In a separate terminal, use lazydocker for a better UI (everything should say "healthy") |
| 159 | +lzd |
| 160 | +``` |
| 161 | + |
| 162 | +### 2. Setup Your Agent's requirements/pyproject.toml |
| 163 | +```bash |
| 164 | +agentex uv sync [--group editable-apy] |
| 165 | +source .venv/bin/activate |
| 166 | + |
| 167 | +# OR |
| 168 | +conda create -n example_tutorial python=3.12 |
| 169 | +conda activate example_tutorial |
| 170 | +pip install -r requirements.txt |
| 171 | +``` |
| 172 | +### 3. Run Your Agent |
| 173 | +```bash |
| 174 | +# From this directory |
| 175 | +export ENVIRONMENT=development && [uv run] agentex agents run --manifest manifest.yaml |
| 176 | +``` |
| 177 | +4. **Interact with your agent** |
| 178 | + |
| 179 | +Option 0: CLI (deprecated - to be replaced once a new CLI is implemented - please use the web UI for now!) |
| 180 | +```bash |
| 181 | +# Submit a task via CLI |
| 182 | +agentex tasks submit --agent example-tutorial --task "Your task here" |
| 183 | +``` |
| 184 | + |
| 185 | +Option 1: Web UI |
| 186 | +```bash |
| 187 | +# Start the local web interface |
| 188 | +cd agentex-web |
| 189 | +make dev |
| 190 | + |
| 191 | +# Then open http://localhost:3000 in your browser to chat with your agent |
| 192 | +``` |
| 193 | + |
| 194 | +## Development Tips |
| 195 | + |
| 196 | +### Environment Variables |
| 197 | +- Set environment variables in project/.env for any required credentials |
| 198 | +- Or configure them in the manifest.yaml under the `env` section |
| 199 | +- The `.env` file is automatically loaded in development mode |
| 200 | + |
| 201 | +### Local Testing |
| 202 | +- Use `export ENVIRONMENT=development` before running your agent |
| 203 | +- This enables local service discovery and debugging features |
| 204 | +- Your agent will automatically connect to locally running services |
| 205 | + |
| 206 | +### Temporal-Specific Tips |
| 207 | +- Monitor workflows in the Temporal Web UI at http://localhost:8080 |
| 208 | +- Use the Temporal CLI for advanced workflow management |
| 209 | +- Check workflow logs for debugging async operations |
| 210 | + |
| 211 | +### Debugging |
| 212 | +- Check agent logs in the terminal where you ran the agent |
| 213 | +- Use the web UI to inspect task history and responses |
| 214 | +- Monitor backend services with `lzd` (LazyDocker) |
| 215 | +- Use Temporal Web UI for workflow debugging |
| 216 | + |
| 217 | +### To build the agent Docker image locally (normally not necessary): |
| 218 | + |
| 219 | +1. Build the agent image: |
| 220 | +```bash |
| 221 | +agentex agents build --manifest manifest.yaml |
| 222 | +``` |
| 223 | + |
| 224 | +## Advanced Features |
| 225 | + |
| 226 | +### Temporal Workflows |
| 227 | +Extend your agent with sophisticated async workflows: |
| 228 | + |
| 229 | +```python |
| 230 | +# In project/workflow.py |
| 231 | +@workflow.defn |
| 232 | +class MyWorkflow(BaseWorkflow): |
| 233 | + async def complex_operation(self): |
| 234 | + # Multi-step async operations |
| 235 | + # Error handling and retries |
| 236 | + # State management |
| 237 | + pass |
| 238 | +``` |
| 239 | + |
| 240 | +### Custom Activities |
| 241 | +Add custom activities for external operations. **Important**: Always specify appropriate timeouts (recommended: 10 minutes): |
| 242 | + |
| 243 | +```python |
| 244 | +# In project/activities.py |
| 245 | +from datetime import timedelta |
| 246 | +from temporalio import activity |
| 247 | +from temporalio.common import RetryPolicy |
| 248 | + |
| 249 | +@activity.defn(name="call_external_api") |
| 250 | +async def call_external_api(data): |
| 251 | + # HTTP requests, database operations, etc. |
| 252 | + pass |
| 253 | + |
| 254 | +# In your workflow, call it with a timeout: |
| 255 | +result = await workflow.execute_activity( |
| 256 | + "call_external_api", |
| 257 | + data, |
| 258 | + start_to_close_timeout=timedelta(minutes=10), # Recommended: 10 minute timeout |
| 259 | + heartbeat_timeout=timedelta(minutes=1), # Optional: heartbeat monitoring |
| 260 | + retry_policy=RetryPolicy(maximum_attempts=3) # Optional: retry policy |
| 261 | +) |
| 262 | + |
| 263 | +# Don't forget to register your custom activities in run_worker.py: |
| 264 | +# all_activities = get_all_activities() + [your_custom_activity_function] |
| 265 | +``` |
| 266 | + |
| 267 | +### Integration with External Services |
| 268 | + |
| 269 | +```bash |
| 270 | +# Add service clients |
| 271 | +agentex uv add httpx requests-oauthlib |
| 272 | + |
| 273 | +# Add AI/ML libraries |
| 274 | +agentex uv add openai anthropic transformers |
| 275 | + |
| 276 | +# Add database clients |
| 277 | +agentex uv add asyncpg redis |
| 278 | +``` |
| 279 | + |
| 280 | + |
| 281 | +## Troubleshooting |
| 282 | + |
| 283 | +### Common Issues |
| 284 | + |
| 285 | +1. **Agent not appearing in web UI** |
| 286 | + - Check if agent is running on port 8000 |
| 287 | + - Verify `ENVIRONMENT=development` is set |
| 288 | + - Check agent logs for errors |
| 289 | + |
| 290 | +2. **Temporal workflow issues** |
| 291 | + - Check Temporal Web UI at http://localhost:8080 |
| 292 | + - Verify Temporal server is running in backend services |
| 293 | + - Check workflow logs for specific errors |
| 294 | + |
| 295 | +3. **Dependency issues** |
| 296 | + |
| 297 | + - Run `agentex uv sync` to ensure all dependencies are installed |
| 298 | + - Verify temporalio is properly installed |
| 299 | + |
| 300 | + |
| 301 | +4. **Port conflicts** |
| 302 | + - Check if another service is using port 8000 |
| 303 | + - Use `lsof -i :8000` to find conflicting processes |
| 304 | + |
| 305 | +### Temporal-Specific Troubleshooting |
| 306 | + |
| 307 | +1. **Workflow not starting** |
| 308 | + - Check if Temporal server is running (`docker ps`) |
| 309 | + - Verify task queue configuration in `run_worker.py` |
| 310 | + - Check workflow registration in the worker |
| 311 | + |
| 312 | +2. **Activity failures** |
| 313 | + - Check activity logs in the console |
| 314 | + - Verify activity registration |
| 315 | + - Check for timeout issues |
| 316 | + |
| 317 | +Happy building with Temporal! 🚀⚡ |
0 commit comments