open-swarm 0.1.1743364176__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.1743364176.dist-info → open_swarm-0.1.1743368545.dist-info}/METADATA +1 -1
- {open_swarm-0.1.1743364176.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.1743364176.dist-info → open_swarm-0.1.1743368545.dist-info}/WHEEL +0 -0
- {open_swarm-0.1.1743364176.dist-info → open_swarm-0.1.1743368545.dist-info}/entry_points.txt +0 -0
- {open_swarm-0.1.1743364176.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
|
@@ -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.1743364176.dist-info → open_swarm-0.1.1743368545.dist-info}/entry_points.txt
RENAMED
File without changes
|
{open_swarm-0.1.1743364176.dist-info → open_swarm-0.1.1743368545.dist-info}/licenses/LICENSE
RENAMED
File without changes
|