File size: 3,978 Bytes
2eef9ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# API Documentation

## Overview

The Multi-Agent System provides a RESTful API for task execution, workflow management, and agent coordination.

Base URL: `http://localhost:8000`

## Authentication

Currently, the API is open. For production, implement JWT or API key authentication.

## Endpoints

### Health Check

#### GET `/health`
Check if the service is healthy.

**Response:**
```json
{
  "status": "healthy",
  "version": "0.1.0"
}
```

#### GET `/ready`
Check if the service is ready to accept requests.

**Response:**
```json
{
  "status": "ready",
  "version": "0.1.0"
}
```

### Tasks

#### POST `/tasks`
Create and execute a new task.

**Request Body:**
```json
{
  "task": "Analyze the latest market trends for tech stocks",
  "task_id": "optional-custom-id"
}
```

**Response:**
```json
{
  "task_id": "550e8400-e29b-41d4-a716-446655440000",
  "task": "Analyze the latest market trends for tech stocks",
  "status": "processing"
}
```

#### GET `/tasks/{task_id}`
Get the status and results of a task.

**Response:**
```json
{
  "task_id": "550e8400-e29b-41d4-a716-446655440000",
  "task": "Analyze the latest market trends for tech stocks",
  "status": "completed"
}
```

#### DELETE `/tasks/{task_id}`
Cancel a running task.

**Response:**
```json
{
  "task_id": "550e8400-e29b-41d4-a716-446655440000",
  "status": "cancelled"
}
```

### Workflows

#### POST `/workflows/execute`
Execute a workflow with optional streaming.

**Request Body:**
```json
{
  "task": "Fetch weather data and create a report",
  "stream": false
}
```

**Response (Non-streaming):**
```json
{
  "task_id": "550e8400-e29b-41d4-a716-446655440000",
  "status": "queued",
  "result": null
}
```

**Response (Streaming):**
Server-Sent Events (SSE) with real-time updates:
```
data: {"agent": "planner", "step": 1, "status": "planning"}
data: {"agent": "executor", "step": 1, "status": "executing", "action": "web_search"}
data: {"agent": "executor", "step": 2, "status": "executing", "result": "..."}
```

#### GET `/workflows/{task_id}/status`
Get the current status of a workflow.

**Response:**
```json
{
  "task_id": "550e8400-e29b-41d4-a716-446655440000",
  "status": "running",
  "progress": 50,
  "current_agent": "executor"
}
```

## Error Handling

All errors follow this format:

```json
{
  "detail": "Error description",
  "status": 400
}
```

### Status Codes

- `200` - Success
- `201` - Created
- `400` - Bad Request
- `404` - Not Found
- `500` - Internal Server Error

## Rate Limiting

Currently not implemented. For production, consider adding:
- Request rate limits per API key
- Concurrent task limits
- Token usage tracking

## Examples

### Create and Execute Task

```bash
curl -X POST "http://localhost:8000/tasks" \
  -H "Content-Type: application/json" \
  -d '{
    "task": "Find information about Python 3.11 release notes"
  }'
```

### Stream Workflow Results

```bash
curl -X POST "http://localhost:8000/workflows/execute" \
  -H "Content-Type: application/json" \
  -d '{
    "task": "Summarize recent AI breakthroughs",
    "stream": true
  }'
```

### Check Task Status

```bash
curl "http://localhost:8000/tasks/550e8400-e29b-41d4-a716-446655440000"
```

## Python Client Example

```python
import requests

API_URL = "http://localhost:8000"

# Create task
response = requests.post(f"{API_URL}/tasks", json={
    "task": "Analyze competitor pricing strategies"
})
task_id = response.json()["task_id"]

# Get status
status_response = requests.get(f"{API_URL}/tasks/{task_id}")
print(status_response.json())

# Stream workflow
with requests.post(f"{API_URL}/workflows/execute", 
                   json={"task": "...", "stream": True}, 
                   stream=True) as response:
    for line in response.iter_lines():
        if line:
            print(line.decode('utf-8'))
```

## Interactive API Documentation

Visit `http://localhost:8000/docs` for Swagger UI with interactive testing.

Visit `http://localhost:8000/redoc` for ReDoc documentation.