Skip to content

Commit 3706bc1

Browse files
committed
Adding initial commit
1 parent 361c2a6 commit 3706bc1

File tree

10 files changed

+850
-0
lines changed

10 files changed

+850
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
*.so
6+
.Python
7+
build/
8+
develop-eggs/
9+
dist/
10+
downloads/
11+
eggs/
12+
.eggs/
13+
lib/
14+
lib64/
15+
parts/
16+
sdist/
17+
var/
18+
wheels/
19+
*.egg-info/
20+
.installed.cfg
21+
*.egg
22+
23+
# Environments
24+
.env**
25+
.venv
26+
env/
27+
venv/
28+
ENV/
29+
env.bak/
30+
venv.bak/
31+
32+
# IDE
33+
.idea/
34+
.vscode/
35+
*.swp
36+
*.swo
37+
38+
# Git
39+
.git
40+
.gitignore
41+
42+
# Misc
43+
.DS_Store
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# syntax=docker/dockerfile:1.3
2+
FROM python:3.12-slim
3+
COPY --from=ghcr.io/astral-sh/uv:0.6.4 /uv /uvx /bin/
4+
5+
# Install system dependencies
6+
RUN apt-get update && apt-get install -y \
7+
htop \
8+
vim \
9+
curl \
10+
tar \
11+
python3-dev \
12+
postgresql-client \
13+
build-essential \
14+
libpq-dev \
15+
gcc \
16+
cmake \
17+
netcat-openbsd \
18+
&& apt-get clean \
19+
&& rm -rf /var/lib/apt/lists/*
20+
21+
# Install tctl (Temporal CLI)
22+
RUN curl -L https://github.com/temporalio/tctl/releases/download/v1.18.1/tctl_1.18.1_linux_arm64.tar.gz -o /tmp/tctl.tar.gz && \
23+
tar -xzf /tmp/tctl.tar.gz -C /usr/local/bin && \
24+
chmod +x /usr/local/bin/tctl && \
25+
rm /tmp/tctl.tar.gz
26+
27+
RUN uv pip install --system --upgrade pip setuptools wheel
28+
29+
ENV UV_HTTP_TIMEOUT=1000
30+
31+
# Copy just the pyproject.toml file to optimize caching
32+
COPY 030_custom_activities/pyproject.toml /app/030_custom_activities/pyproject.toml
33+
34+
WORKDIR /app/030_custom_activities
35+
36+
# Install the required Python packages using uv
37+
RUN uv pip install --system .
38+
39+
# Copy the project code
40+
COPY 030_custom_activities/project /app/030_custom_activities/project
41+
42+
# Run the ACP server using uvicorn
43+
CMD ["uvicorn", "project.acp:acp", "--host", "0.0.0.0", "--port", "8000"]
44+
45+
# When we deploy the worker, we will replace the CMD with the following
46+
# CMD ["python", "-m", "run_worker"]
Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
# 030-custom-activities - 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+
030_custom_activities/
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 (`030-custom-activities`) 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 030_custom_activities python=3.12
169+
conda activate 030_custom_activities
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 030-custom-activities --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:
242+
243+
```python
244+
# In project/activities.py
245+
@activity.defn
246+
async def call_external_api(data):
247+
# HTTP requests, database operations, etc.
248+
pass
249+
```
250+
251+
### Integration with External Services
252+
253+
```bash
254+
# Add service clients
255+
agentex uv add httpx requests-oauthlib
256+
257+
# Add AI/ML libraries
258+
agentex uv add openai anthropic transformers
259+
260+
# Add database clients
261+
agentex uv add asyncpg redis
262+
```
263+
264+
265+
## Troubleshooting
266+
267+
### Common Issues
268+
269+
1. **Agent not appearing in web UI**
270+
- Check if agent is running on port 8000
271+
- Verify `ENVIRONMENT=development` is set
272+
- Check agent logs for errors
273+
274+
2. **Temporal workflow issues**
275+
- Check Temporal Web UI at http://localhost:8080
276+
- Verify Temporal server is running in backend services
277+
- Check workflow logs for specific errors
278+
279+
3. **Dependency issues**
280+
281+
- Run `agentex uv sync` to ensure all dependencies are installed
282+
- Verify temporalio is properly installed
283+
284+
285+
4. **Port conflicts**
286+
- Check if another service is using port 8000
287+
- Use `lsof -i :8000` to find conflicting processes
288+
289+
### Temporal-Specific Troubleshooting
290+
291+
1. **Workflow not starting**
292+
- Check if Temporal server is running (`docker ps`)
293+
- Verify task queue configuration in `run_worker.py`
294+
- Check workflow registration in the worker
295+
296+
2. **Activity failures**
297+
- Check activity logs in the console
298+
- Verify activity registration
299+
- Check for timeout issues
300+
301+
Happy building with Temporal! 🚀⚡

0 commit comments

Comments
 (0)