open-swarm 0.1.1743362777__py3-none-any.whl → 0.1.1743368545__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {open_swarm-0.1.1743362777.dist-info → open_swarm-0.1.1743368545.dist-info}/METADATA +128 -59
- {open_swarm-0.1.1743362777.dist-info → open_swarm-0.1.1743368545.dist-info}/RECORD +7 -7
- swarm/auth.py +76 -15
- swarm/views/chat_views.py +140 -59
- {open_swarm-0.1.1743362777.dist-info → open_swarm-0.1.1743368545.dist-info}/WHEEL +0 -0
- {open_swarm-0.1.1743362777.dist-info → open_swarm-0.1.1743368545.dist-info}/entry_points.txt +0 -0
- {open_swarm-0.1.1743362777.dist-info → open_swarm-0.1.1743368545.dist-info}/licenses/LICENSE +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: open-swarm
|
3
|
-
Version: 0.1.
|
3
|
+
Version: 0.1.1743368545
|
4
4
|
Summary: Open Swarm: Orchestrating AI Agent Swarms with Django
|
5
5
|
Project-URL: Homepage, https://github.com/yourusername/open-swarm
|
6
6
|
Project-URL: Documentation, https://github.com/yourusername/open-swarm/blob/main/README.md
|
@@ -40,6 +40,7 @@ Requires-Dist: google-auth-oauthlib>=1.2.1
|
|
40
40
|
Requires-Dist: gunicorn>=21.0.0
|
41
41
|
Requires-Dist: httpx<0.26.0,>=0.25.2
|
42
42
|
Requires-Dist: jinja2>=3.1.6
|
43
|
+
Requires-Dist: jmespath>=1.0.1
|
43
44
|
Requires-Dist: openai-agents>=0.0.1
|
44
45
|
Requires-Dist: openai<2.0.0,>=1.3.0
|
45
46
|
Requires-Dist: platformdirs>=4.0.0
|
@@ -92,6 +93,11 @@ Description-Content-Type: text/markdown
|
|
92
93
|
|
93
94
|
**Open Swarm** is a Python framework for creating, managing, and deploying autonomous agent swarms. It leverages the `openai-agents` library for core agent functionality and provides a structured way to build complex, multi-agent workflows using **Blueprints**.
|
94
95
|
|
96
|
+
Open Swarm can be used in two primary ways:
|
97
|
+
|
98
|
+
1. **As a CLI Utility (`swarm-cli`):** Manage, run, and install blueprints directly on your local machine. Ideal for personal use, testing, and creating standalone agent tools. (Recommended installation: PyPI)
|
99
|
+
2. **As an API Service (`swarm-api`):** Deploy a web server that exposes your blueprints via an OpenAI-compatible REST API. Ideal for integrations, web UIs, and shared access. (Recommended deployment: Docker)
|
100
|
+
|
95
101
|
---
|
96
102
|
|
97
103
|
## Core Concepts
|
@@ -105,97 +111,160 @@ Description-Content-Type: text/markdown
|
|
105
111
|
|
106
112
|
---
|
107
113
|
|
108
|
-
## Quickstart
|
114
|
+
## Quickstart 1: Using `swarm-cli` Locally (via PyPI)
|
109
115
|
|
110
|
-
This is the
|
116
|
+
This is the recommended way to use `swarm-cli` for managing and running blueprints on your local machine.
|
111
117
|
|
112
118
|
**Prerequisites:**
|
113
|
-
*
|
114
|
-
*
|
119
|
+
* Python 3.10+
|
120
|
+
* `pip` (Python package installer)
|
115
121
|
|
116
122
|
**Steps:**
|
117
123
|
|
118
|
-
1. **
|
124
|
+
1. **Install `open-swarm` from PyPI:**
|
119
125
|
```bash
|
120
|
-
|
121
|
-
cd open-swarm
|
126
|
+
pip install open-swarm
|
122
127
|
```
|
128
|
+
*(Using a virtual environment is recommended: `python -m venv .venv && source .venv/bin/activate`)*
|
123
129
|
|
124
|
-
2. **
|
125
|
-
*
|
130
|
+
2. **Initial Configuration (First Run):**
|
131
|
+
* The first time you run a `swarm-cli` command that requires configuration (like `run` or `config`), it will automatically create a default `swarm_config.json` at `~/.config/swarm/swarm_config.json` if one doesn't exist.
|
132
|
+
* You **must** set the required environment variables (like `OPENAI_API_KEY`) in your shell for the configuration to work. Create a `.env` file in your working directory or export them:
|
126
133
|
```bash
|
127
|
-
|
134
|
+
export OPENAI_API_KEY="sk-..."
|
135
|
+
# Add other keys as needed (GROQ_API_KEY, etc.)
|
128
136
|
```
|
129
|
-
*
|
137
|
+
* You can customize the configuration further using `swarm-cli config` commands (see `USERGUIDE.md`).
|
130
138
|
|
131
|
-
3. **
|
132
|
-
*
|
139
|
+
3. **Add a Blueprint:**
|
140
|
+
* Download or create a blueprint file (e.g., `my_blueprint.py`). Example blueprints are available in the [project repository](https://github.com/matthewhand/open-swarm/tree/main/src/swarm/blueprints).
|
141
|
+
* Add it using `swarm-cli`:
|
133
142
|
```bash
|
134
|
-
|
135
|
-
|
136
|
-
* Edit `docker-compose.override.yaml` to:
|
137
|
-
* Mount any local directories containing custom blueprints you want the API server to access (e.g., uncomment and adjust the `./my_custom_blueprints:/app/custom_blueprints:ro` line).
|
138
|
-
* Make any other necessary adjustments (ports, environment variables, etc.).
|
143
|
+
# Example: Adding a downloaded blueprint file
|
144
|
+
swarm-cli add ./path/to/downloaded/blueprint_echocraft.py
|
139
145
|
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
```
|
144
|
-
This will build the image (if not already pulled/built) and start the `open-swarm` service, exposing the API on port 8000 (or the port specified in your `.env`/override).
|
146
|
+
# Example: Adding a directory containing a blueprint
|
147
|
+
swarm-cli add ./my_custom_blueprints/agent_smith --name agent_smith
|
148
|
+
```
|
145
149
|
|
146
|
-
|
147
|
-
*
|
150
|
+
4. **Run the Blueprint:**
|
151
|
+
* **Single Instruction:**
|
148
152
|
```bash
|
149
|
-
|
153
|
+
swarm-cli run echocraft --instruction "Hello from CLI!"
|
150
154
|
```
|
151
|
-
*
|
155
|
+
* **Interactive Mode:**
|
152
156
|
```bash
|
153
|
-
|
154
|
-
|
155
|
-
-d '{
|
156
|
-
"model": "echocraft",
|
157
|
-
"messages": [{"role": "user", "content": "Hello Docker!"}]
|
158
|
-
}'
|
157
|
+
swarm-cli run echocraft
|
158
|
+
# Now you can chat with the blueprint interactively
|
159
159
|
```
|
160
|
-
|
160
|
+
|
161
|
+
5. **(Optional) Install as Command:**
|
162
|
+
```bash
|
163
|
+
swarm-cli install echocraft
|
164
|
+
# Now run (ensure ~/.local/share/swarm/bin is in your PATH):
|
165
|
+
echocraft --instruction "I am a command now!"
|
166
|
+
```
|
161
167
|
|
162
168
|
---
|
163
169
|
|
164
|
-
##
|
170
|
+
## Quickstart 2: Deploying `swarm-api` Service (via Docker)
|
171
|
+
|
172
|
+
This section covers deploying the API service using Docker.
|
173
|
+
|
174
|
+
### Option A: Docker Compose (Recommended for Flexibility)
|
175
|
+
|
176
|
+
This method uses `docker-compose.yaml` and is best if you need to customize volumes, environment variables easily, or manage related services (like Redis).
|
177
|
+
|
178
|
+
**Prerequisites:**
|
179
|
+
* Docker ([Install Docker](https://docs.docker.com/engine/install/))
|
180
|
+
* Docker Compose ([Install Docker Compose](https://docs.docker.com/compose/install/))
|
181
|
+
* Git
|
182
|
+
|
183
|
+
**Steps:**
|
184
|
+
|
185
|
+
1. **Clone the Repository:** (Needed for `docker-compose.yaml` and config files)
|
186
|
+
```bash
|
187
|
+
git clone https://github.com/matthewhand/open-swarm.git
|
188
|
+
cd open-swarm
|
189
|
+
```
|
190
|
+
|
191
|
+
2. **Configure Environment:**
|
192
|
+
* Copy `cp .env.example .env` and edit `.env` with your API keys (e.g., `OPENAI_API_KEY`, `SWARM_API_KEY`).
|
193
|
+
|
194
|
+
3. **Prepare Blueprints & Config:**
|
195
|
+
* Place blueprints in `./blueprints`.
|
196
|
+
* Ensure `./swarm_config.json` exists and is configured.
|
197
|
+
|
198
|
+
4. **Configure Overrides (Optional):**
|
199
|
+
* Copy `cp docker-compose.override.yaml.example docker-compose.override.yaml`.
|
200
|
+
* Edit the override file to mount additional volumes, change ports, etc.
|
201
|
+
|
202
|
+
5. **Start the Service:**
|
203
|
+
```bash
|
204
|
+
docker compose up -d
|
205
|
+
```
|
206
|
+
|
207
|
+
6. **Verify API:** (Default port 8000)
|
208
|
+
* Models: `curl http://localhost:8000/v1/models`
|
209
|
+
* Chat: `curl http://localhost:8000/v1/chat/completions -H "Content-Type: application/json" -d '{"model": "echocraft", ...}'` (Add `-H "Authorization: Bearer <key>"` if needed).
|
210
|
+
|
211
|
+
### Option B: Direct `docker run` (Simpler for Single Container)
|
212
|
+
|
213
|
+
This method runs the pre-built image directly from Docker Hub. Good for quick tests or simple deployments without cloning the repo. Customization requires careful use of `-v` (volume) and `-e` (environment) flags.
|
214
|
+
|
215
|
+
**Prerequisites:**
|
216
|
+
* Docker ([Install Docker](https://docs.docker.com/engine/install/))
|
217
|
+
|
218
|
+
**Steps:**
|
165
219
|
|
166
|
-
|
220
|
+
1. **Prepare Local Files (If Customizing):**
|
221
|
+
* Create a directory for your blueprints (e.g., `~/my_swarm_blueprints`).
|
222
|
+
* Create your `swarm_config.json` file locally (e.g., `~/my_swarm_config.json`).
|
223
|
+
* Create a `.env` file locally (e.g., `~/swarm.env`) with your API keys (`OPENAI_API_KEY`, `SWARM_API_KEY`, etc.).
|
167
224
|
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
225
|
+
2. **Run the Container:**
|
226
|
+
```bash
|
227
|
+
docker run -d \
|
228
|
+
--name open-swarm-api \
|
229
|
+
-p 8000:8000 \
|
230
|
+
--env-file ~/swarm.env \
|
231
|
+
-v ~/my_swarm_blueprints:/app/blueprints:ro \
|
232
|
+
-v ~/my_swarm_config.json:/app/swarm_config.json:ro \
|
233
|
+
-v open_swarm_db:/app/db.sqlite3 \
|
234
|
+
--restart unless-stopped \
|
235
|
+
mhand79/open-swarm:latest
|
236
|
+
```
|
237
|
+
* `-d`: Run detached (in background).
|
238
|
+
* `--name`: Assign a name to the container.
|
239
|
+
* `-p 8000:8000`: Map host port 8000 to container port 8000 (adjust if needed).
|
240
|
+
* `--env-file`: Load environment variables from your local file.
|
241
|
+
* `-v ...:/app/blueprints:ro`: Mount your local blueprints directory (read-only). **Required** if you want to use custom blueprints.
|
242
|
+
* `-v ...:/app/swarm_config.json:ro`: Mount your local config file (read-only). **Required** for custom LLM/MCP settings.
|
243
|
+
* `-v open_swarm_db:/app/db.sqlite3`: Use a named Docker volume for the database to persist data.
|
244
|
+
* `--restart unless-stopped`: Automatically restart the container unless manually stopped.
|
245
|
+
* `mhand79/open-swarm:latest`: The image name on Docker Hub.
|
246
|
+
|
247
|
+
3. **Verify API:** (Same as Docker Compose)
|
248
|
+
* Models: `curl http://localhost:8000/v1/models`
|
249
|
+
* Chat: `curl http://localhost:8000/v1/chat/completions ...` (Add `-H "Authorization: Bearer <key>"` if needed).
|
174
250
|
|
175
|
-
|
176
|
-
* **How:** `swarm-cli run <blueprint_name> --instruction "Your single instruction"`
|
177
|
-
* **What:** Executes a blueprint managed by `swarm-cli` (located in `~/.local/share/swarm/blueprints/`) directly in the terminal. Uses configuration from `~/.config/swarm/swarm_config.json`.
|
178
|
-
* **Interactive Mode:** If you omit the `--instruction` argument (`swarm-cli run <blueprint_name>`), it will enter an interactive chat mode in the terminal.
|
179
|
-
* **Use Case:** Good for testing, debugging, interactive sessions, or running specific tasks locally without the API overhead.
|
251
|
+
---
|
180
252
|
|
181
|
-
|
182
|
-
* **How:** `swarm-cli install <blueprint_name>`, then run `<blueprint_name> --instruction "..."`
|
183
|
-
* **What:** Creates a standalone executable for a managed blueprint using PyInstaller and places it in the user's binary directory (e.g., `~/.local/bin/` or similar, ensure it's in your `PATH`).
|
184
|
-
* **Use Case:** Convenient for frequently used blueprints that act like regular command-line tools.
|
253
|
+
## Usage Modes Summary
|
185
254
|
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
255
|
+
* **`swarm-api` (via Docker or `manage.py runserver`):** Exposes blueprints as an OpenAI-compatible REST API. Ideal for integrations. Requires `SWARM_API_KEY` for security in non-local deployments.
|
256
|
+
* **`swarm-cli run` (via PyPI install):** Executes managed blueprints locally, either with a single instruction or in interactive chat mode. Good for testing and local tasks.
|
257
|
+
* **`swarm-cli install` (via PyPI install):** Creates standalone command-line executables from managed blueprints.
|
258
|
+
* **Direct Python Execution (via Git clone):** Running `uv run python <blueprint_file.py>` is mainly for development and testing individual files.
|
190
259
|
|
191
260
|
---
|
192
261
|
|
193
262
|
## Further Documentation
|
194
263
|
|
195
|
-
This README provides a high-level overview and quickstart. For more detailed information, please refer to:
|
264
|
+
This README provides a high-level overview and quickstart guides. For more detailed information, please refer to:
|
196
265
|
|
197
|
-
* **User Guide (`USERGUIDE.md`):** Detailed instructions on using `swarm-cli` commands for managing blueprints and configuration.
|
198
|
-
* **Development Guide (`DEVELOPMENT.md`):** Information for contributors and developers, including architecture details, testing strategies, project layout, and advanced topics
|
266
|
+
* **User Guide (`USERGUIDE.md`):** Detailed instructions on using `swarm-cli` commands for managing blueprints and configuration locally.
|
267
|
+
* **Development Guide (`DEVELOPMENT.md`):** Information for contributors and developers, including architecture details, testing strategies, project layout, API details, and advanced topics.
|
199
268
|
* **Example Blueprints (`src/swarm/blueprints/README.md`):** A list and description of the example blueprints included with the framework, showcasing various features and integration patterns.
|
200
269
|
|
201
270
|
---
|
@@ -1,6 +1,6 @@
|
|
1
1
|
swarm/__init__.py,sha256=aT-yyX84tiZhihp0RAXYTADCo9dhOnmolQVfn4_NQa8,46
|
2
2
|
swarm/apps.py,sha256=up4C3m2JeyXeUcH-wYeReCuiOBVJ6404w9OfaRChLwM,2568
|
3
|
-
swarm/auth.py,sha256=
|
3
|
+
swarm/auth.py,sha256=8JIk1VbBvFFwOijEJAsrx6si802ZSMGnErXvmo0izUg,5935
|
4
4
|
swarm/consumers.py,sha256=wESLamkhbi4SEZt9k3yx6eU9ufOIZMCAL-OAXjJBGXE,5056
|
5
5
|
swarm/messages.py,sha256=CwADrjlj-uVmm-so1xIZvN1UkEWdzSn_hu7slfhuS8w,6549
|
6
6
|
swarm/models.py,sha256=Ix0WEYYqza2lbOEBNesikRCs3XGUPWmqQyMWzZYUaxM,1494
|
@@ -247,14 +247,14 @@ swarm/utils/message_utils.py,sha256=oNTD7pfmnnu_Br24pR2VEX9afIZwFLwg2HJBLXs1blY,
|
|
247
247
|
swarm/utils/redact.py,sha256=L2lo927S575Ay7Jz6pUlK47Sxxzpd9IMybnDm6-WlC8,2955
|
248
248
|
swarm/views/__init__.py,sha256=AcLk0R7Y69FhIVgJK2hZs8M_gCR-h_5iqUywz89yuHM,1223
|
249
249
|
swarm/views/api_views.py,sha256=BbDEgI6Ftg-c-mMkE9DvRGZHIZ-WAZSfwqAB7j98WxM,1937
|
250
|
-
swarm/views/chat_views.py,sha256=
|
250
|
+
swarm/views/chat_views.py,sha256=6UUtEJKrM2k_wi9A6AfhbbvMYunjzpY22M6hOIXASjA,15695
|
251
251
|
swarm/views/core_views.py,sha256=iGbits6HqVXE13vEkqvOBQHGS7LZb6Gg9Hcs6KjZmmk,5255
|
252
252
|
swarm/views/message_views.py,sha256=sDUnXyqKXC8WwIIMAlWf00s2_a2T9c75Na5FvYMJwBM,1596
|
253
253
|
swarm/views/model_views.py,sha256=aAbU4AZmrOTaPeKMWtoKK7FPYHdaN3Zbx55JfKzYTRY,2937
|
254
254
|
swarm/views/utils.py,sha256=geX3Z5ZDKFYyXYBMilc-4qgOSjhujK3AfRtvbXgFpXk,3643
|
255
255
|
swarm/views/web_views.py,sha256=ExQQeJpZ8CkLZQC_pXKOOmdnEy2qR3wEBP4LLp27DPU,7404
|
256
|
-
open_swarm-0.1.
|
257
|
-
open_swarm-0.1.
|
258
|
-
open_swarm-0.1.
|
259
|
-
open_swarm-0.1.
|
260
|
-
open_swarm-0.1.
|
256
|
+
open_swarm-0.1.1743368545.dist-info/METADATA,sha256=jhENgIwI-YqL-g0gFo8loMVMWEt1qSTGlpPFbZW0IVs,13680
|
257
|
+
open_swarm-0.1.1743368545.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
258
|
+
open_swarm-0.1.1743368545.dist-info/entry_points.txt,sha256=LudR3dBqGnglrE1n2zAeWo38Ck0m57sKPW36KfK-pzo,71
|
259
|
+
open_swarm-0.1.1743368545.dist-info/licenses/LICENSE,sha256=BU9bwRlnOt_JDIb6OT55Q4leLZx9RArDLTFnlDIrBEI,1062
|
260
|
+
open_swarm-0.1.1743368545.dist-info/RECORD,,
|
swarm/auth.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
import logging
|
2
2
|
import os
|
3
3
|
from rest_framework.authentication import BaseAuthentication, SessionAuthentication
|
4
|
+
# Import BasePermission for creating custom permissions
|
5
|
+
from rest_framework.permissions import BasePermission
|
4
6
|
from rest_framework import exceptions
|
5
7
|
from django.conf import settings
|
6
8
|
from django.utils.translation import gettext_lazy as _
|
@@ -12,49 +14,108 @@ from django.contrib.auth import get_user_model
|
|
12
14
|
logger = logging.getLogger('swarm.auth')
|
13
15
|
User = get_user_model()
|
14
16
|
|
17
|
+
# ==============================================================================
|
18
|
+
# Authentication Classes (Determine *who* the user is)
|
19
|
+
# ==============================================================================
|
20
|
+
|
15
21
|
# --- Static Token Authentication ---
|
16
22
|
class StaticTokenAuthentication(BaseAuthentication):
|
17
23
|
"""
|
18
|
-
Authenticates requests based on a static API token passed in a header
|
19
|
-
|
20
|
-
|
24
|
+
Authenticates requests based on a static API token passed in a header
|
25
|
+
(Authorization: Bearer <token> or X-API-Key: <token>).
|
26
|
+
|
27
|
+
Returns (AnonymousUser, token) on success. This allows permission classes
|
28
|
+
to check request.auth to see if token authentication succeeded, even though
|
29
|
+
no specific user model is associated with the token.
|
21
30
|
"""
|
22
31
|
keyword = 'Bearer'
|
23
32
|
|
24
33
|
def authenticate(self, request):
|
25
|
-
|
34
|
+
"""
|
35
|
+
Attempts to authenticate using a static token.
|
36
|
+
"""
|
37
|
+
logger.debug("[Auth][StaticToken] Attempting static token authentication.")
|
38
|
+
# Retrieve the expected token from settings.
|
26
39
|
expected_token = getattr(settings, 'SWARM_API_KEY', None)
|
27
40
|
|
41
|
+
# If no token is configured in settings, this method cannot authenticate.
|
28
42
|
if not expected_token:
|
29
|
-
logger.error("[Auth][StaticToken] SWARM_API_KEY is not set in Django settings. Cannot
|
30
|
-
return None
|
43
|
+
logger.error("[Auth][StaticToken] SWARM_API_KEY is not set in Django settings. Cannot use static token auth.")
|
44
|
+
return None # Indicate authentication method did not run or failed pre-check
|
31
45
|
|
46
|
+
# Extract the provided token from standard Authorization header or custom X-API-Key header.
|
32
47
|
provided_token = None
|
33
48
|
auth_header = request.META.get('HTTP_AUTHORIZATION', '').split()
|
34
49
|
if len(auth_header) == 2 and auth_header[0].lower() == self.keyword.lower():
|
35
50
|
provided_token = auth_header[1]
|
36
|
-
logger.debug(
|
51
|
+
logger.debug("[Auth][StaticToken] Found token in Authorization header.")
|
37
52
|
else:
|
38
53
|
provided_token = request.META.get('HTTP_X_API_KEY')
|
39
54
|
if provided_token:
|
40
|
-
logger.debug(
|
55
|
+
logger.debug("[Auth][StaticToken] Found token in X-API-Key header.")
|
41
56
|
|
57
|
+
# If no token was found in either header, authentication fails for this method.
|
42
58
|
if not provided_token:
|
43
|
-
logger.debug("[Auth][StaticToken] No token found in headers.")
|
44
|
-
return None
|
59
|
+
logger.debug("[Auth][StaticToken] No token found in relevant headers.")
|
60
|
+
return None # Indicate authentication method did not find credentials
|
45
61
|
|
46
|
-
#
|
62
|
+
# Compare the provided token with the expected token.
|
63
|
+
# NOTE: For production, consider using a constant-time comparison function
|
64
|
+
# to mitigate timing attacks if the token is highly sensitive.
|
47
65
|
if provided_token == expected_token:
|
48
66
|
logger.info("[Auth][StaticToken] Static token authentication successful.")
|
49
|
-
#
|
50
|
-
# This
|
67
|
+
# Return AnonymousUser and the token itself as request.auth.
|
68
|
+
# This signals successful authentication via token without linking to a specific User model.
|
51
69
|
return (AnonymousUser(), provided_token)
|
52
70
|
else:
|
53
|
-
|
71
|
+
# Token was provided but did not match. Raise AuthenticationFailed.
|
72
|
+
logger.warning(f"[Auth][StaticToken] Invalid static token provided.")
|
54
73
|
raise exceptions.AuthenticationFailed(_("Invalid API Key."))
|
55
74
|
|
56
75
|
# --- Custom *Synchronous* Session Authentication ---
|
57
76
|
class CustomSessionAuthentication(SessionAuthentication):
|
58
|
-
"""
|
77
|
+
"""
|
78
|
+
Standard Django Session Authentication provided by DRF.
|
79
|
+
Relies on Django's session middleware to populate request.user.
|
80
|
+
This class itself is synchronous, but the underlying session loading
|
81
|
+
needs to be handled correctly in async views (e.g., via middleware or wrappers).
|
82
|
+
"""
|
83
|
+
# No override needed unless customizing session behavior.
|
59
84
|
pass
|
60
85
|
|
86
|
+
|
87
|
+
# ==============================================================================
|
88
|
+
# Permission Classes (Determine *if* access is allowed)
|
89
|
+
# ==============================================================================
|
90
|
+
|
91
|
+
class HasValidTokenOrSession(BasePermission):
|
92
|
+
"""
|
93
|
+
Allows access if EITHER:
|
94
|
+
1. Static token authentication succeeded (request.auth is not None).
|
95
|
+
2. Session authentication succeeded (request.user is authenticated).
|
96
|
+
"""
|
97
|
+
message = 'Authentication credentials were not provided or are invalid (Requires valid API Key or active session).'
|
98
|
+
|
99
|
+
def has_permission(self, request, view):
|
100
|
+
"""
|
101
|
+
Checks if the request has valid authentication via token or session.
|
102
|
+
"""
|
103
|
+
# Check if static token authentication was successful.
|
104
|
+
# StaticTokenAuthentication returns (AnonymousUser, token), so request.auth will be the token.
|
105
|
+
has_valid_token = getattr(request, 'auth', None) is not None
|
106
|
+
if has_valid_token:
|
107
|
+
logger.debug("[Perm][TokenOrSession] Access granted via static token (request.auth is set).")
|
108
|
+
return True
|
109
|
+
|
110
|
+
# Check if session authentication was successful.
|
111
|
+
# request.user should be populated by SessionAuthentication/AuthMiddleware.
|
112
|
+
user = getattr(request, 'user', None)
|
113
|
+
has_valid_session = user is not None and user.is_authenticated
|
114
|
+
if has_valid_session:
|
115
|
+
logger.debug(f"[Perm][TokenOrSession] Access granted via authenticated session user: {user}")
|
116
|
+
return True
|
117
|
+
|
118
|
+
# If neither condition is met, deny permission.
|
119
|
+
logger.debug("[Perm][TokenOrSession] Access denied: No valid token (request.auth=None) and no authenticated session user.")
|
120
|
+
return False
|
121
|
+
|
swarm/views/chat_views.py
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
|
2
|
+
# --- Content for src/swarm/views/chat_views.py ---
|
1
3
|
import logging
|
2
4
|
import json
|
3
5
|
import uuid
|
@@ -12,21 +14,28 @@ from django.utils.decorators import method_decorator
|
|
12
14
|
from django.views.decorators.csrf import csrf_exempt
|
13
15
|
from django.contrib.auth.decorators import login_required
|
14
16
|
from django.conf import settings
|
17
|
+
from django.urls import reverse # Needed for reverse() used in tests
|
15
18
|
|
16
19
|
from rest_framework import status
|
17
20
|
from rest_framework.views import APIView
|
18
21
|
from rest_framework.response import Response
|
19
22
|
from rest_framework.permissions import IsAuthenticated, AllowAny
|
20
23
|
from rest_framework.exceptions import ValidationError, PermissionDenied, NotFound, APIException, ParseError, NotAuthenticated
|
24
|
+
from rest_framework.request import Request # Import DRF Request
|
21
25
|
|
22
|
-
|
26
|
+
# Utility to wrap sync functions for async execution
|
27
|
+
from asgiref.sync import sync_to_async
|
23
28
|
|
24
29
|
# Assuming serializers are in the same app
|
25
30
|
from swarm.serializers import ChatCompletionRequestSerializer
|
26
31
|
# Assuming utils are in the same app/directory level
|
32
|
+
# Make sure these utils are async-safe or wrapped if they perform sync I/O
|
27
33
|
from .utils import get_blueprint_instance, validate_model_access, get_available_blueprints
|
34
|
+
# Import custom permission
|
35
|
+
from swarm.auth import HasValidTokenOrSession # Keep this import
|
28
36
|
|
29
37
|
logger = logging.getLogger(__name__)
|
38
|
+
# Specific logger for debug prints, potentially configured differently
|
30
39
|
print_logger = logging.getLogger('print_debug')
|
31
40
|
|
32
41
|
# ==============================================================================
|
@@ -34,28 +43,43 @@ print_logger = logging.getLogger('print_debug')
|
|
34
43
|
# ==============================================================================
|
35
44
|
|
36
45
|
class HealthCheckView(APIView):
|
46
|
+
""" Simple health check endpoint. """
|
37
47
|
permission_classes = [AllowAny]
|
38
|
-
def get(self, request, *args, **kwargs):
|
48
|
+
def get(self, request, *args, **kwargs):
|
49
|
+
""" Returns simple 'ok' status. """
|
50
|
+
return Response({"status": "ok"})
|
39
51
|
|
40
52
|
class ChatCompletionsView(APIView):
|
41
53
|
"""
|
42
|
-
Handles chat completion requests, compatible with OpenAI API.
|
43
|
-
|
54
|
+
Handles chat completion requests (/v1/chat/completions), compatible with OpenAI API spec.
|
55
|
+
Supports both streaming and non-streaming responses.
|
56
|
+
Uses asynchronous handling for potentially long-running blueprint operations.
|
44
57
|
"""
|
58
|
+
# Default serializer class for request validation.
|
45
59
|
serializer_class = ChatCompletionRequestSerializer
|
60
|
+
# Default permission classes are likely set in settings.py
|
61
|
+
# permission_classes = [IsAuthenticated] # Example default
|
62
|
+
|
63
|
+
# --- Internal Helper Methods (Unchanged) ---
|
46
64
|
|
47
65
|
async def _handle_non_streaming(self, blueprint_instance, messages: List[Dict[str, str]], request_id: str, model_name: str) -> Response:
|
66
|
+
""" Handles non-streaming requests. """
|
48
67
|
logger.info(f"[ReqID: {request_id}] Processing non-streaming request for model '{model_name}'.")
|
49
68
|
final_response_data = None; start_time = time.time()
|
50
69
|
try:
|
51
|
-
#
|
70
|
+
# The blueprint's run method should be an async generator.
|
52
71
|
async_generator = blueprint_instance.run(messages)
|
53
72
|
async for chunk in async_generator:
|
54
|
-
|
55
|
-
|
73
|
+
# Check if the chunk contains the expected final message list.
|
74
|
+
if isinstance(chunk, dict) and "messages" in chunk and isinstance(chunk["messages"], list):
|
75
|
+
final_response_data = chunk["messages"]
|
76
|
+
logger.debug(f"[ReqID: {request_id}] Received final data chunk.")
|
77
|
+
break # Stop after getting the final data
|
78
|
+
else:
|
79
|
+
logger.warning(f"[ReqID: {request_id}] Unexpected chunk format during non-streaming run: {chunk}")
|
56
80
|
|
57
81
|
if not final_response_data or not isinstance(final_response_data, list) or not final_response_data:
|
58
|
-
logger.error(f"[ReqID: {request_id}] Blueprint '{model_name}' did not return valid final
|
82
|
+
logger.error(f"[ReqID: {request_id}] Blueprint '{model_name}' did not return a valid final message list. Got: {final_response_data}")
|
59
83
|
raise APIException("Blueprint did not return valid data.", code=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
60
84
|
|
61
85
|
if not isinstance(final_response_data[0], dict) or 'role' not in final_response_data[0]:
|
@@ -69,73 +93,96 @@ class ChatCompletionsView(APIView):
|
|
69
93
|
except Exception as e: logger.error(f"[ReqID: {request_id}] Unexpected error during non-streaming blueprint execution: {e}", exc_info=True); raise APIException(f"Internal server error during generation: {e}", code=status.HTTP_500_INTERNAL_SERVER_ERROR) from e
|
70
94
|
|
71
95
|
async def _handle_streaming(self, blueprint_instance, messages: List[Dict[str, str]], request_id: str, model_name: str) -> StreamingHttpResponse:
|
96
|
+
""" Handles streaming requests using SSE. """
|
72
97
|
logger.info(f"[ReqID: {request_id}] Processing streaming request for model '{model_name}'.")
|
73
98
|
async def event_stream():
|
74
99
|
start_time = time.time(); chunk_index = 0
|
75
100
|
try:
|
76
|
-
logger.debug(f"[ReqID: {request_id}] Getting async generator from blueprint run...")
|
77
|
-
# *** FIX: Remove await here. run() returns the async generator directly ***
|
78
|
-
async_generator = blueprint_instance.run(messages)
|
79
|
-
logger.debug(f"[ReqID: {request_id}] Got async generator. Starting iteration...")
|
101
|
+
logger.debug(f"[ReqID: {request_id}] Getting async generator from blueprint.run()..."); async_generator = blueprint_instance.run(messages); logger.debug(f"[ReqID: {request_id}] Got async generator. Starting iteration...")
|
80
102
|
async for chunk in async_generator:
|
81
103
|
logger.debug(f"[ReqID: {request_id}] Received stream chunk {chunk_index}: {chunk}")
|
82
|
-
if not isinstance(chunk, dict) or "messages" not in chunk or not isinstance(chunk["messages"], list) or not chunk["messages"] or not isinstance(chunk["messages"][0], dict):
|
83
|
-
|
84
|
-
delta_content = chunk["messages"][0].get("content", "");
|
85
|
-
delta = {"role": "assistant"}
|
104
|
+
if not isinstance(chunk, dict) or "messages" not in chunk or not isinstance(chunk["messages"], list) or not chunk["messages"] or not isinstance(chunk["messages"][0], dict): logger.warning(f"[ReqID: {request_id}] Skipping invalid chunk format: {chunk}"); continue
|
105
|
+
delta_content = chunk["messages"][0].get("content"); delta = {"role": "assistant"}
|
86
106
|
if delta_content is not None: delta["content"] = delta_content
|
87
|
-
|
88
107
|
response_chunk = { "id": f"chatcmpl-{request_id}", "object": "chat.completion.chunk", "created": int(time.time()), "model": model_name, "choices": [{"index": 0, "delta": delta, "logprobs": None, "finish_reason": None}] }
|
89
108
|
logger.debug(f"[ReqID: {request_id}] Sending SSE chunk {chunk_index}"); yield f"data: {json.dumps(response_chunk)}\n\n"; chunk_index += 1; await asyncio.sleep(0.01)
|
90
109
|
logger.debug(f"[ReqID: {request_id}] Finished iterating stream. Sending [DONE]."); yield "data: [DONE]\n\n"; end_time = time.time(); logger.info(f"[ReqID: {request_id}] Streaming request completed in {end_time - start_time:.2f}s.")
|
91
|
-
except APIException as e:
|
92
|
-
|
93
|
-
try: yield f"data: {json.dumps(error_chunk)}\n\n"; yield "data: [DONE]\n\n"
|
94
|
-
except Exception as send_err: logger.error(f"[ReqID: {request_id}] Failed to send error chunk: {send_err}")
|
95
|
-
except Exception as e:
|
96
|
-
logger.error(f"[ReqID: {request_id}] Unexpected error during streaming blueprint execution: {e}", exc_info=True); error_msg = f"Internal server error during stream: {str(e)}"; error_chunk = {"error": {"message": error_msg, "type": "internal_error"}}
|
97
|
-
try: yield f"data: {json.dumps(error_chunk)}\n\n"; yield "data: [DONE]\n\n"
|
98
|
-
except Exception as send_err: logger.error(f"[ReqID: {request_id}] Failed to send error chunk: {send_err}")
|
110
|
+
except APIException as e: logger.error(f"[ReqID: {request_id}] API error during streaming: {e}", exc_info=True); error_msg = f"API error: {e.detail}"; error_chunk = {"error": {"message": error_msg, "type": "api_error", "code": e.status_code}}; yield f"data: {json.dumps(error_chunk)}\n\n"; yield "data: [DONE]\n\n"
|
111
|
+
except Exception as e: logger.error(f"[ReqID: {request_id}] Unexpected error during streaming: {e}", exc_info=True); error_msg = f"Internal server error: {str(e)}"; error_chunk = {"error": {"message": error_msg, "type": "internal_error"}}; yield f"data: {json.dumps(error_chunk)}\n\n"; yield "data: [DONE]\n\n"
|
99
112
|
return StreamingHttpResponse(event_stream(), content_type="text/event-stream")
|
100
113
|
|
114
|
+
# --- Restore Custom dispatch method (wrapping perform_authentication) ---
|
101
115
|
@method_decorator(csrf_exempt)
|
102
116
|
async def dispatch(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponseBase:
|
103
|
-
|
104
|
-
|
105
|
-
|
117
|
+
"""
|
118
|
+
Override DRF's dispatch method to specifically wrap the authentication step.
|
119
|
+
"""
|
120
|
+
self.args = args
|
121
|
+
self.kwargs = kwargs
|
122
|
+
drf_request: Request = self.initialize_request(request, *args, **kwargs)
|
123
|
+
self.request = drf_request
|
124
|
+
self.headers = self.default_response_headers
|
125
|
+
|
126
|
+
response = None
|
106
127
|
try:
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
128
|
+
# --- Wrap ONLY perform_authentication ---
|
129
|
+
print_logger.debug(f"User before perform_authentication: {getattr(drf_request, 'user', 'N/A')}, Auth: {getattr(drf_request, 'auth', 'N/A')}")
|
130
|
+
# This forces the synchronous DB access within perform_authentication into a thread
|
131
|
+
await sync_to_async(self.perform_authentication)(drf_request)
|
132
|
+
print_logger.debug(f"User after perform_authentication: {getattr(drf_request, 'user', 'N/A')}, Auth: {getattr(drf_request, 'auth', 'N/A')}")
|
133
|
+
# --- End wrapping ---
|
134
|
+
|
135
|
+
# Run permission and throttle checks synchronously after auth.
|
136
|
+
# These checks operate on the now-populated request.user/auth attributes.
|
137
|
+
self.check_permissions(drf_request)
|
138
|
+
print_logger.debug("Permissions check passed.")
|
139
|
+
self.check_throttles(drf_request)
|
140
|
+
print_logger.debug("Throttles check passed.")
|
141
|
+
|
142
|
+
# Find and execute the handler (e.g., post).
|
143
|
+
if drf_request.method.lower() in self.http_method_names:
|
144
|
+
handler = getattr(self, drf_request.method.lower(), self.http_method_not_allowed)
|
145
|
+
else:
|
146
|
+
handler = self.http_method_not_allowed
|
114
147
|
|
148
|
+
# IMPORTANT: Await the handler if it's async (like self.post)
|
115
149
|
if asyncio.iscoroutinefunction(handler):
|
116
|
-
response = await handler(
|
150
|
+
response = await handler(drf_request, *args, **kwargs)
|
117
151
|
else:
|
118
|
-
|
119
|
-
|
120
|
-
|
152
|
+
# Wrap sync handlers if any exist (like GET, OPTIONS).
|
153
|
+
response = await sync_to_async(handler)(drf_request, *args, **kwargs)
|
154
|
+
|
155
|
+
except Exception as exc:
|
156
|
+
# Let DRF handle exceptions to generate appropriate responses
|
157
|
+
response = self.handle_exception(exc)
|
158
|
+
|
159
|
+
# Finalize response should now receive a valid Response/StreamingHttpResponse
|
160
|
+
self.response = self.finalize_response(drf_request, response, *args, **kwargs)
|
121
161
|
return self.response
|
122
162
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
163
|
+
# --- POST Handler (Keep sync_to_async wrappers here too) ---
|
164
|
+
async def post(self, request: Request, *args: Any, **kwargs: Any) -> HttpResponseBase:
|
165
|
+
"""
|
166
|
+
Handles POST requests for chat completions. Assumes dispatch has handled auth/perms.
|
167
|
+
"""
|
168
|
+
request_id = str(uuid.uuid4())
|
169
|
+
logger.info(f"[ReqID: {request_id}] Processing POST request.")
|
170
|
+
print_logger.debug(f"[ReqID: {request_id}] User in post: {getattr(request, 'user', 'N/A')}, Auth: {getattr(request, 'auth', 'N/A')}")
|
171
|
+
|
172
|
+
# --- Request Body Parsing & Validation ---
|
173
|
+
try: request_data = request.data
|
174
|
+
except ParseError as e: logger.error(f"[ReqID: {request_id}] Invalid request body format: {e.detail}"); raise e
|
129
175
|
except json.JSONDecodeError as e: logger.error(f"[ReqID: {request_id}] JSON Decode Error: {e}"); raise ParseError(f"Invalid JSON body: {e}")
|
130
176
|
|
177
|
+
# --- Serialization and Validation ---
|
131
178
|
serializer = self.serializer_class(data=request_data)
|
132
179
|
try:
|
133
|
-
print_logger.debug(f"[ReqID: {request_id}]
|
134
|
-
# Wrap sync is_valid call
|
180
|
+
print_logger.debug(f"[ReqID: {request_id}] Validating request data: {request_data}")
|
181
|
+
# Wrap sync is_valid call as it *might* do DB lookups
|
135
182
|
await sync_to_async(serializer.is_valid)(raise_exception=True)
|
136
|
-
print_logger.debug(f"[ReqID: {request_id}]
|
137
|
-
except ValidationError as e: print_logger.error(f"[ReqID: {request_id}]
|
138
|
-
except Exception as e: print_logger.error(f"[ReqID: {request_id}]
|
183
|
+
print_logger.debug(f"[ReqID: {request_id}] Request data validation successful.")
|
184
|
+
except ValidationError as e: print_logger.error(f"[ReqID: {request_id}] Request data validation FAILED: {e.detail}"); raise e
|
185
|
+
except Exception as e: print_logger.error(f"[ReqID: {request_id}] Unexpected error during serializer validation: {e}", exc_info=True); raise APIException(f"Internal error during request validation: {e}", code=status.HTTP_500_INTERNAL_SERVER_ERROR) from e
|
139
186
|
|
140
187
|
validated_data = serializer.validated_data
|
141
188
|
model_name = validated_data['model']
|
@@ -143,20 +190,54 @@ class ChatCompletionsView(APIView):
|
|
143
190
|
stream = validated_data.get('stream', False)
|
144
191
|
blueprint_params = validated_data.get('params', None)
|
145
192
|
|
146
|
-
|
147
|
-
#
|
148
|
-
|
193
|
+
# --- Model Access Validation ---
|
194
|
+
# This function likely performs sync DB lookups, so wrap it.
|
195
|
+
print_logger.debug(f"[ReqID: {request_id}] Checking model access for user '{request.user}' and model '{model_name}'")
|
196
|
+
try:
|
197
|
+
access_granted = await sync_to_async(validate_model_access)(request.user, model_name)
|
198
|
+
except Exception as e:
|
199
|
+
logger.error(f"[ReqID: {request_id}] Error during model access validation for model '{model_name}': {e}", exc_info=True)
|
200
|
+
raise APIException("Error checking model permissions.", code=status.HTTP_500_INTERNAL_SERVER_ERROR) from e
|
201
|
+
|
149
202
|
if not access_granted:
|
150
|
-
logger.warning(f"[ReqID: {request_id}] User {request.user} denied access to model '{model_name}'.")
|
203
|
+
logger.warning(f"[ReqID: {request_id}] User '{request.user}' denied access to model '{model_name}'.")
|
151
204
|
raise PermissionDenied(f"You do not have permission to access the model '{model_name}'.")
|
205
|
+
print_logger.debug(f"[ReqID: {request_id}] Model access granted.")
|
152
206
|
|
153
|
-
|
154
|
-
|
207
|
+
# --- Get Blueprint Instance ---
|
208
|
+
# This function should ideally be async or sync-safe.
|
209
|
+
print_logger.debug(f"[ReqID: {request_id}] Getting blueprint instance for '{model_name}' with params: {blueprint_params}")
|
210
|
+
try:
|
211
|
+
blueprint_instance = await get_blueprint_instance(model_name, params=blueprint_params)
|
212
|
+
except Exception as e:
|
213
|
+
logger.error(f"[ReqID: {request_id}] Error getting blueprint instance for '{model_name}': {e}", exc_info=True)
|
214
|
+
raise APIException(f"Failed to load model '{model_name}': {e}", code=status.HTTP_500_INTERNAL_SERVER_ERROR) from e
|
155
215
|
|
156
216
|
if blueprint_instance is None:
|
157
|
-
logger.error(f"[ReqID: {request_id}] Blueprint '{model_name}' not found or failed to initialize
|
217
|
+
logger.error(f"[ReqID: {request_id}] Blueprint '{model_name}' not found or failed to initialize (get_blueprint_instance returned None).")
|
158
218
|
raise NotFound(f"The requested model (blueprint) '{model_name}' was not found or could not be initialized.")
|
159
219
|
|
160
|
-
|
161
|
-
|
220
|
+
# --- Handle Streaming or Non-Streaming Response ---
|
221
|
+
if stream:
|
222
|
+
return await self._handle_streaming(blueprint_instance, messages, request_id, model_name)
|
223
|
+
else:
|
224
|
+
return await self._handle_non_streaming(blueprint_instance, messages, request_id, model_name)
|
225
|
+
|
226
|
+
|
227
|
+
# ==============================================================================
|
228
|
+
# Simple Django Views (Example for Web UI - if ENABLE_WEBUI=True)
|
229
|
+
# ==============================================================================
|
162
230
|
|
231
|
+
@method_decorator(csrf_exempt, name='dispatch') # Apply csrf_exempt if needed
|
232
|
+
@method_decorator(login_required, name='dispatch') # Require login
|
233
|
+
class IndexView(View):
|
234
|
+
""" Renders the main chat interface page. """
|
235
|
+
def get(self, request):
|
236
|
+
""" Handles GET requests to render the index page. """
|
237
|
+
# Assuming get_available_blueprints is sync safe
|
238
|
+
available_blueprints = get_available_blueprints()
|
239
|
+
context = {
|
240
|
+
'available_blueprints': available_blueprints,
|
241
|
+
'user': request.user, # User should be available here
|
242
|
+
}
|
243
|
+
return render(request, 'index.html', context)
|
File without changes
|
{open_swarm-0.1.1743362777.dist-info → open_swarm-0.1.1743368545.dist-info}/entry_points.txt
RENAMED
File without changes
|
{open_swarm-0.1.1743362777.dist-info → open_swarm-0.1.1743368545.dist-info}/licenses/LICENSE
RENAMED
File without changes
|