feat(agent): 增加 LLM 路由与诊断日志
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from django.contrib.auth.decorators import login_required
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from django.http import FileResponse, Http404, JsonResponse
|
||||
@@ -11,6 +12,9 @@ from .events import serialize_event
|
||||
from .storage import save_uploaded_attachment, serialize_attachment
|
||||
|
||||
|
||||
logger = logging.getLogger("review_agent.file_summary.views")
|
||||
|
||||
|
||||
def _conversation_for_user(user, conversation_id: int) -> Conversation:
|
||||
conversation = Conversation.objects.filter(pk=conversation_id, user=user).first()
|
||||
if not conversation:
|
||||
@@ -27,6 +31,15 @@ def attachments(request, conversation_id: int):
|
||||
files = request.FILES.getlist("files")
|
||||
if not files:
|
||||
return JsonResponse({"error": "请选择至少一个文件。"}, status=400)
|
||||
logger.info(
|
||||
"Attachment upload request received",
|
||||
extra={
|
||||
"conversation_id": conversation.pk,
|
||||
"user_id": request.user.pk,
|
||||
"file_count": len(files),
|
||||
"filenames": [uploaded_file.name for uploaded_file in files],
|
||||
},
|
||||
)
|
||||
saved = [
|
||||
save_uploaded_attachment(
|
||||
conversation=conversation,
|
||||
@@ -35,12 +48,23 @@ def attachments(request, conversation_id: int):
|
||||
)
|
||||
for uploaded_file in files
|
||||
]
|
||||
logger.info(
|
||||
"Attachment upload request finished",
|
||||
extra={
|
||||
"conversation_id": conversation.pk,
|
||||
"attachment_ids": [attachment.pk for attachment in saved],
|
||||
},
|
||||
)
|
||||
return JsonResponse({"attachments": [serialize_attachment(item) for item in saved]})
|
||||
|
||||
queryset = FileAttachment.objects.filter(conversation=conversation).order_by(
|
||||
"original_name",
|
||||
"-version_no",
|
||||
)
|
||||
logger.info(
|
||||
"Attachment list requested",
|
||||
extra={"conversation_id": conversation.pk, "attachment_count": queryset.count()},
|
||||
)
|
||||
return JsonResponse({"attachments": [serialize_attachment(item) for item in queryset]})
|
||||
|
||||
|
||||
@@ -59,6 +83,10 @@ def attachment_detail(request, conversation_id: int, attachment_id: int):
|
||||
attachment.upload_status = FileAttachment.UploadStatus.DELETED
|
||||
attachment.is_active = False
|
||||
attachment.save(update_fields=["upload_status", "is_active"])
|
||||
logger.info(
|
||||
"Attachment deleted",
|
||||
extra={"conversation_id": conversation.pk, "attachment_id": attachment.pk},
|
||||
)
|
||||
return JsonResponse({"ok": True, "attachment": serialize_attachment(attachment)})
|
||||
|
||||
|
||||
@@ -120,12 +148,25 @@ def export_download(request, export_id: int):
|
||||
raise Http404("导出文件不存在。")
|
||||
path = Path(exported.storage_path)
|
||||
if not path.exists():
|
||||
logger.warning(
|
||||
"Export download missing file",
|
||||
extra={"export_id": exported.pk, "storage_path": exported.storage_path},
|
||||
)
|
||||
return JsonResponse({"error": "文件不存在。"}, status=404)
|
||||
content_type = (
|
||||
"text/markdown; charset=utf-8"
|
||||
if exported.export_type == ExportedSummaryFile.ExportType.MARKDOWN
|
||||
else "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||
)
|
||||
logger.info(
|
||||
"Export download started",
|
||||
extra={
|
||||
"export_id": exported.pk,
|
||||
"batch_id": exported.batch_id,
|
||||
"file_name": exported.file_name,
|
||||
"content_type": content_type,
|
||||
},
|
||||
)
|
||||
return FileResponse(
|
||||
path.open("rb"),
|
||||
as_attachment=True,
|
||||
|
||||
Reference in New Issue
Block a user