57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
from rest_framework import viewsets
|
|
from rest_framework.decorators import action
|
|
from rest_framework.response import Response
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from apps.agents.models import Agent, AgentExecution
|
|
from apps.agents.serializers import AgentSerializer, AgentExecutionSerializer
|
|
from apps.agents.tasks import start_agent_task_mcp
|
|
|
|
|
|
class AgentViewSet(viewsets.ModelViewSet):
|
|
serializer_class = AgentSerializer
|
|
permission_classes = [IsAuthenticated]
|
|
lookup_field = 'uuid'
|
|
|
|
def get_queryset(self):
|
|
return Agent.objects.filter(user=self.request.user)
|
|
|
|
def perform_create(self, serializer):
|
|
serializer.save(user=self.request.user)
|
|
|
|
@action(detail=True, methods=['post'])
|
|
def start(self, request, uuid=None):
|
|
agent = self.get_object()
|
|
input_data = request.data.get('input_data', {})
|
|
|
|
execution = AgentExecution.objects.create(
|
|
agent=agent,
|
|
user=request.user,
|
|
input_data=input_data
|
|
)
|
|
|
|
start_agent_task_mcp.delay(str(execution.uuid))
|
|
|
|
serializer = AgentExecutionSerializer(execution)
|
|
return Response({
|
|
"status": "queued",
|
|
"execution": serializer.data,
|
|
"message": "Agent task queued for execution"
|
|
})
|
|
|
|
|
|
class AgentExecutionViewSet(viewsets.ReadOnlyModelViewSet):
|
|
serializer_class = AgentExecutionSerializer
|
|
permission_classes = [IsAuthenticated]
|
|
lookup_field = 'uuid'
|
|
|
|
def get_queryset(self):
|
|
return AgentExecution.objects.filter(user=self.request.user)
|
|
|
|
@action(detail=True, methods=['get'])
|
|
def events(self, request, uuid=None):
|
|
execution = self.get_object()
|
|
events = execution.events.all().values()
|
|
return Response({
|
|
"execution_id": str(execution.uuid),
|
|
"events": list(events)
|
|
})
|