from __future__ import annotations from django.utils import timezone from review_agent.models import FeishuQuestionLog from .intent import parse_question_intent from .query import query_batch_summary def answer_question(user, text: str, *, source_type: str = FeishuQuestionLog.SourceType.SIMULATE) -> dict: parsed = parse_question_intent(text) result = query_batch_summary( user, workflow_type=parsed.get("workflow_type") or None, batch_no=parsed.get("batch_no") or None, latest=bool(parsed.get("latest")), ) status = FeishuQuestionLog.Status.SUCCESS if result.get("ok") else FeishuQuestionLog.Status.FAILED answer_summary = str(result.get("answer_summary") or "") log = FeishuQuestionLog.objects.create( system_user=user if getattr(user, "is_authenticated", False) else None, source_type=source_type, question_text=text, intent=str(parsed.get("intent") or "unknown"), query_object={ "workflow_type": parsed.get("workflow_type") or "", "batch_no": parsed.get("batch_no") or "", "latest": bool(parsed.get("latest")), }, answer_summary=answer_summary[:500], permission_result=str(result.get("permission_result") or ""), status=status, error_message="" if result.get("ok") else answer_summary, processed_at=timezone.now(), ) return {**result, "intent": parsed.get("intent"), "log_id": log.pk}