This commit is contained in:
2026-03-25 16:19:28 +03:30
parent 2846db1647
commit f305e00cfe
7 changed files with 63 additions and 121 deletions
+30 -22
View File
@@ -1,6 +1,5 @@
"""Farm AI Assistant API views."""
from django.db.models import Count, OuterRef, Subquery
from django.http import Http404, HttpResponse
from rest_framework import serializers, status
from rest_framework.permissions import IsAuthenticated
@@ -36,15 +35,7 @@ class ChatListView(APIView):
responses={200: status_response("FarmAiAssistantConversationListResponse", data=ConversationListSerializer(many=True))},
)
def get(self, request):
last_message_subquery = Message.objects.filter(conversation=OuterRef("pk")).order_by("-created_at", "-id")
conversations = (
Conversation.objects.filter(owner=request.user)
.annotate(
message_count=Count("messages"),
last_message=Subquery(last_message_subquery.values("content")[:1]),
)
.order_by("-updated_at", "-created_at")
)
conversations = Conversation.objects.filter(owner=request.user).order_by("-updated_at", "-created_at")
serializer = ConversationListSerializer(conversations, many=True)
return Response({"status": "success", "data": serializer.data}, status=status.HTTP_200_OK)
@@ -139,11 +130,29 @@ class ChatView(APIView):
)
if isinstance(adapter_response.data, dict) and "body" in adapter_response.data:
conversation.save(update_fields=["updated_at"])
return HttpResponse(
adapter_response.data["body"],
assistant_content = adapter_response.data.get("body") or ""
assistant_message = Message.objects.create(
conversation=conversation,
role=Message.ROLE_ASSISTANT,
content=assistant_content,
raw_response=adapter_response.data,
)
if not conversation.title:
conversation.title = (validated.get("content", "") or assistant_content or "New chat")[:255]
conversation.save(update_fields=["title", "updated_at"])
else:
conversation.save(update_fields=["updated_at"])
return Response(
{
"conversation_id": str(conversation.uuid),
"user_message_id": str(user_message.uuid),
"assistant_message_id": str(assistant_message.uuid),
"content": assistant_content,
"content_type": adapter_response.data.get("content_type", "text/plain; charset=utf-8"),
},
status=adapter_response.status_code,
content_type=adapter_response.data.get("content_type", "text/plain; charset=utf-8"),
)
assistant_content = ""
@@ -165,23 +174,22 @@ class ChatView(APIView):
else:
conversation.save(update_fields=["updated_at"])
conversation_uuid = str(conversation.uuid)
response_data = adapter_response.data
if isinstance(response_data, dict):
response_data.setdefault("conversation_id", conversation_uuid)
data = response_data.get("data")
if isinstance(data, dict):
data.setdefault("conversation_id", str(conversation.uuid))
data.setdefault("conversation_id", conversation_uuid)
data.setdefault("user_message_id", str(user_message.uuid))
data.setdefault("assistant_message_id", str(assistant_message.uuid))
else:
response_data = {
"conversation_id": str(conversation.uuid),
"user_message_id": str(user_message.uuid),
"assistant_message_id": str(assistant_message.uuid),
"response": response_data,
}
response_data.setdefault("user_message_id", str(user_message.uuid))
response_data.setdefault("assistant_message_id", str(assistant_message.uuid))
else:
response_data = {
"conversation_id": str(conversation.uuid),
"conversation_id": conversation_uuid,
"user_message_id": str(user_message.uuid),
"assistant_message_id": str(assistant_message.uuid),
"response": response_data,