Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions backend/openedx_ai_extensions/api/v1/workflows/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
import logging
from datetime import datetime, timezone

from django.contrib.auth.decorators import login_required
from django.core.exceptions import ValidationError
from django.http import JsonResponse, StreamingHttpResponse
from django.utils.decorators import method_decorator
from django.views import View
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey, UsageKey
from rest_framework import status
Expand Down Expand Up @@ -81,27 +79,22 @@ def get_context_from_request(request):
return validated_context


@method_decorator(login_required, name="dispatch")
@method_decorator(handle_ai_errors, name="dispatch")
class AIGenericWorkflowView(View):
class AIGenericWorkflowView(APIView):
"""
AI Workflow API endpoint
"""

permission_classes = [IsAuthenticated]

@method_decorator(handle_ai_errors)
def post(self, request):
"""Common handler for GET and POST requests"""

context = get_context_from_request(request)
workflow_profile = AIWorkflowScope.get_profile(**context)

request_body = {}
if request.body:
try:
request_body = json.loads(request.body.decode("utf-8"))
except json.JSONDecodeError as e:
raise ValidationError("Invalid JSON format in request body.") from e
action = request_body.get("action", "")
user_input = request_body.get("user_input", {})
action = request.data.get("action", "")
user_input = request.data.get("user_input", {})

result = workflow_profile.execute(
user_input=user_input,
Expand Down
6 changes: 6 additions & 0 deletions backend/openedx_ai_extensions/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
APIConnectionError,
AuthenticationError,
ContextWindowExceededError,
NotFoundError,
RateLimitError,
ServiceUnavailableError,
Timeout,
Expand All @@ -28,6 +29,11 @@
"message": "The AI service is currently unavailable due to an authentication error.",
"status": status.HTTP_500_INTERNAL_SERVER_ERROR,
},
NotFoundError: {
"code": "llm_config_error",
"message": "The AI service is misconfigured. Please check the LLM settings.",
"status": status.HTTP_500_INTERNAL_SERVER_ERROR,
},
Comment on lines +32 to +36
RateLimitError: {
"code": "rate_limit_exceeded",
"message": "The AI service is currently busy. Please try again later.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ def _execute_orchestrator_async(task_self, session_id, action, params=None):
raise

except Exception as e:
logger.error(f"Task {task_id}: Error executing {action} for session {session_id}: {str(e)}")
logger.error(
"Task %s: Error executing %s for session %s: %s",
task_id, action, session_id, str(e),
exc_info=True,
)
session.metadata['task_status'] = 'error'
session.metadata['task_error'] = str(e)
session.save(update_fields=['metadata'])
Comment on lines 104 to 112
Expand Down
8 changes: 2 additions & 6 deletions backend/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,9 @@ def test_workflows_endpoint_requires_authentication(api_client): # pylint: disa
"""
url = reverse("openedx_ai_extensions:api:v1:aiext_workflows")

# Test POST without authentication
# DRF IsAuthenticated with SessionAuthentication returns 403 (no WWW-Authenticate challenge)
response = api_client.post(url, {}, format="json")
assert response.status_code == 302 # Redirect to login

# Test GET without authentication
response = api_client.get(url)
assert response.status_code == 302 # Redirect to login
assert response.status_code == 403


@pytest.mark.django_db
Expand Down
Loading