feat(application-form-fill): 接入自动填表工作流触发
This commit is contained in:
27
review_agent/application_form_fill/events.py
Normal file
27
review_agent/application_form_fill/events.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from review_agent.application_form_fill.constants import WORKFLOW_TYPE
|
||||
from review_agent.models import ApplicationFormFillBatch, WorkflowEvent
|
||||
|
||||
|
||||
def record_event(
|
||||
batch: ApplicationFormFillBatch,
|
||||
event_type: str,
|
||||
payload: dict | None = None,
|
||||
) -> WorkflowEvent:
|
||||
return WorkflowEvent.objects.create(
|
||||
workflow_type=WORKFLOW_TYPE,
|
||||
workflow_batch_id=batch.pk,
|
||||
conversation=batch.conversation,
|
||||
event_type=event_type,
|
||||
payload=payload or {},
|
||||
)
|
||||
|
||||
|
||||
def serialize_event(event: WorkflowEvent) -> dict[str, object]:
|
||||
return {
|
||||
"id": event.pk,
|
||||
"event_type": event.event_type,
|
||||
"payload": event.payload,
|
||||
"created_at": event.created_at.isoformat(),
|
||||
}
|
||||
@@ -1,21 +1,151 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from review_agent.application_form_fill.constants import FORM_FILL_NODE_DEFINITIONS, WORKFLOW_TYPE
|
||||
import logging
|
||||
from threading import Thread
|
||||
from uuid import uuid4
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import transaction
|
||||
from django.utils import timezone
|
||||
|
||||
from review_agent.application_form_fill.constants import DEFAULT_OUTPUT_TYPES, FORM_FILL_NODE_DEFINITIONS, WORKFLOW_TYPE
|
||||
from review_agent.application_form_fill.events import record_event
|
||||
from review_agent.application_form_fill.storage import build_batch_work_dir
|
||||
from review_agent.models import ApplicationFormFillBatch, Conversation, FileSummaryBatch, Message, WorkflowNodeRun
|
||||
|
||||
|
||||
logger = logging.getLogger("review_agent.application_form_fill.workflow")
|
||||
|
||||
|
||||
def build_batch_no() -> str:
|
||||
return f"AFF-{timezone.localtime().strftime('%Y%m%d%H%M%S')}-{uuid4().hex[:6]}"
|
||||
|
||||
|
||||
def find_latest_successful_summary_batch(conversation: Conversation) -> FileSummaryBatch | None:
|
||||
return (
|
||||
FileSummaryBatch.objects.filter(
|
||||
conversation=conversation,
|
||||
status=FileSummaryBatch.Status.SUCCESS,
|
||||
)
|
||||
.order_by("-finished_at", "-created_at", "-id")
|
||||
.first()
|
||||
)
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def create_application_form_fill_batch(
|
||||
*,
|
||||
conversation: Conversation,
|
||||
user,
|
||||
source_summary_batch: FileSummaryBatch,
|
||||
trigger_message: Message | None = None,
|
||||
requested_templates: list[str] | None = None,
|
||||
output_types: list[str] | None = None,
|
||||
) -> ApplicationFormFillBatch:
|
||||
batch_no = build_batch_no()
|
||||
work_dir = build_batch_work_dir(batch_no=batch_no)
|
||||
work_dir.mkdir(parents=True, exist_ok=True)
|
||||
batch = ApplicationFormFillBatch.objects.create(
|
||||
conversation=conversation,
|
||||
user=user,
|
||||
trigger_message=trigger_message,
|
||||
source_summary_batch=source_summary_batch,
|
||||
batch_no=batch_no,
|
||||
requested_templates=requested_templates or [],
|
||||
output_types=output_types or DEFAULT_OUTPUT_TYPES,
|
||||
work_dir=str(work_dir),
|
||||
)
|
||||
for code, name, group in FORM_FILL_NODE_DEFINITIONS:
|
||||
WorkflowNodeRun.objects.create(
|
||||
workflow_type=WORKFLOW_TYPE,
|
||||
workflow_batch_id=batch.pk,
|
||||
node_group=group,
|
||||
node_code=code,
|
||||
node_name=name,
|
||||
)
|
||||
record_event(batch, "workflow_created", {"batch_id": batch.pk, "batch_no": batch.batch_no})
|
||||
return batch
|
||||
|
||||
|
||||
class FormFillWorkflowExecutor:
|
||||
"""Workflow executor scaffold filled in by later AFF stages."""
|
||||
"""Runs the auto-fill workflow skeleton; later stages fill node bodies."""
|
||||
|
||||
def __init__(self, batch):
|
||||
def __init__(self, batch: ApplicationFormFillBatch):
|
||||
self.batch = batch
|
||||
|
||||
def run(self) -> None:
|
||||
raise NotImplementedError("application_form_fill workflow is implemented in later AFF stages.")
|
||||
logger.info("自动填表工作流开始 batch_no=%s batch_id=%s", self.batch.batch_no, self.batch.pk)
|
||||
self.batch.status = ApplicationFormFillBatch.Status.RUNNING
|
||||
self.batch.started_at = timezone.now()
|
||||
self.batch.save(update_fields=["status", "started_at"])
|
||||
record_event(self.batch, "workflow_started", {"batch_id": self.batch.pk})
|
||||
|
||||
try:
|
||||
for node in self._nodes():
|
||||
if node.status in {WorkflowNodeRun.Status.SUCCESS, WorkflowNodeRun.Status.SKIPPED}:
|
||||
continue
|
||||
self._run_node(node)
|
||||
except Exception as exc:
|
||||
logger.exception("Application form fill workflow failed", extra={"batch_id": self.batch.pk})
|
||||
self.batch.status = ApplicationFormFillBatch.Status.FAILED
|
||||
self.batch.error_message = str(exc)
|
||||
self.batch.finished_at = timezone.now()
|
||||
self.batch.save(update_fields=["status", "error_message", "finished_at"])
|
||||
record_event(self.batch, "workflow_failed", {"message": str(exc)})
|
||||
return
|
||||
|
||||
self.batch.status = ApplicationFormFillBatch.Status.SUCCESS
|
||||
self.batch.finished_at = timezone.now()
|
||||
self.batch.save(update_fields=["status", "finished_at"])
|
||||
record_event(self.batch, "workflow_completed", {"batch_id": self.batch.pk})
|
||||
logger.info("自动填表工作流完成 batch_no=%s", self.batch.batch_no)
|
||||
|
||||
def _nodes(self):
|
||||
return WorkflowNodeRun.objects.filter(
|
||||
workflow_type=WORKFLOW_TYPE,
|
||||
workflow_batch_id=self.batch.pk,
|
||||
).order_by("id")
|
||||
|
||||
def _run_node(self, node: WorkflowNodeRun) -> None:
|
||||
node.status = WorkflowNodeRun.Status.RUNNING
|
||||
node.progress = 10
|
||||
node.started_at = timezone.now()
|
||||
node.message = f"{node.node_name}处理中"
|
||||
node.save(update_fields=["status", "progress", "started_at", "message"])
|
||||
record_event(
|
||||
self.batch,
|
||||
"node_progress",
|
||||
{"node_code": node.node_code, "status": node.status, "progress": node.progress, "message": node.message},
|
||||
)
|
||||
|
||||
if node.node_code == "pdf_convert":
|
||||
node.status = WorkflowNodeRun.Status.SKIPPED
|
||||
node.progress = 100
|
||||
node.finished_at = timezone.now()
|
||||
node.message = "PDF 转换为后续增强项,本次跳过"
|
||||
node.save(update_fields=["status", "progress", "finished_at", "message"])
|
||||
record_event(
|
||||
self.batch,
|
||||
"node_progress",
|
||||
{"node_code": node.node_code, "status": node.status, "progress": node.progress, "message": node.message},
|
||||
)
|
||||
return
|
||||
|
||||
node.status = WorkflowNodeRun.Status.SUCCESS
|
||||
node.progress = 100
|
||||
node.finished_at = timezone.now()
|
||||
node.message = f"{node.node_name}完成"
|
||||
node.save(update_fields=["status", "progress", "finished_at", "message"])
|
||||
record_event(
|
||||
self.batch,
|
||||
"node_progress",
|
||||
{"node_code": node.node_code, "status": node.status, "progress": node.progress, "message": node.message},
|
||||
)
|
||||
|
||||
|
||||
def start_application_form_fill_workflow(batch, *, async_run: bool = True) -> None:
|
||||
def start_application_form_fill_workflow(batch: ApplicationFormFillBatch, *, async_run: bool = True) -> None:
|
||||
executor = FormFillWorkflowExecutor(batch)
|
||||
if async_run:
|
||||
if not async_run:
|
||||
executor.run()
|
||||
return
|
||||
executor.run()
|
||||
Thread(target=executor.run, daemon=True).start()
|
||||
|
||||
@@ -11,6 +11,11 @@ from .file_summary.skills.attachment_reader import AttachmentReaderSkill
|
||||
from .file_summary.workflow import create_file_summary_batch, start_file_summary_workflow
|
||||
from .llm import LLMConfigurationError, LLMRequestError, generate_reply, stream_reply
|
||||
from .models import Conversation, FileAttachment, FileSummaryBatch, Message
|
||||
from .application_form_fill.workflow import (
|
||||
create_application_form_fill_batch,
|
||||
find_latest_successful_summary_batch as find_latest_successful_form_fill_summary_batch,
|
||||
start_application_form_fill_workflow,
|
||||
)
|
||||
from .regulatory_review.workflow import (
|
||||
create_regulatory_review_batch,
|
||||
find_latest_successful_summary_batch,
|
||||
@@ -224,6 +229,85 @@ def stream_message(conversation: Conversation, content: str):
|
||||
)
|
||||
return
|
||||
|
||||
if route.starts_application_form_fill:
|
||||
source_summary_batch = find_latest_successful_form_fill_summary_batch(conversation)
|
||||
if not source_summary_batch:
|
||||
if not _has_active_attachments(conversation):
|
||||
reply_content = "请先在当前对话右侧上传需要填表的产品资料或压缩包,我会先自动汇总再继续生成申报模板。"
|
||||
assistant_message = append_assistant_message(conversation, reply_content)
|
||||
yield sse_event("chunk", {"delta": reply_content})
|
||||
yield sse_event(
|
||||
"done",
|
||||
{
|
||||
"assistant_message_id": assistant_message.pk,
|
||||
"conversation_id": conversation.pk,
|
||||
"title": conversation.title,
|
||||
},
|
||||
)
|
||||
return
|
||||
summary_batch = create_file_summary_batch(
|
||||
conversation=conversation,
|
||||
user=conversation.user,
|
||||
trigger_message=user_message,
|
||||
)
|
||||
yield sse_event(
|
||||
"workflow_started",
|
||||
{
|
||||
"workflow_type": "file_summary",
|
||||
"batch_id": summary_batch.pk,
|
||||
"batch_no": summary_batch.batch_no,
|
||||
},
|
||||
)
|
||||
start_file_summary_workflow(summary_batch, async_run=False)
|
||||
summary_batch.refresh_from_db()
|
||||
if summary_batch.status != FileSummaryBatch.Status.SUCCESS:
|
||||
reply_content = f"已先启动文件目录与页数自动汇总工作流,批次号:{summary_batch.batch_no},但汇总未成功:{summary_batch.error_message or '原因待查看'}。请处理后再启动申报文件自动填表。"
|
||||
assistant_message = append_assistant_message(conversation, reply_content)
|
||||
yield sse_event("chunk", {"delta": reply_content})
|
||||
yield sse_event(
|
||||
"done",
|
||||
{
|
||||
"assistant_message_id": assistant_message.pk,
|
||||
"conversation_id": conversation.pk,
|
||||
"title": conversation.title,
|
||||
},
|
||||
)
|
||||
return
|
||||
source_summary_batch = summary_batch
|
||||
reply_prefix = f"已先启动文件目录与页数自动汇总工作流,批次号:{summary_batch.batch_no},汇总完成后继续自动填表。\n"
|
||||
else:
|
||||
reply_prefix = ""
|
||||
batch = create_application_form_fill_batch(
|
||||
conversation=conversation,
|
||||
user=conversation.user,
|
||||
trigger_message=user_message,
|
||||
source_summary_batch=source_summary_batch,
|
||||
)
|
||||
start_application_form_fill_workflow(
|
||||
batch,
|
||||
async_run=getattr(settings, "APPLICATION_FORM_FILL_ASYNC", True),
|
||||
)
|
||||
reply_content = f"{reply_prefix}已启动申报文件自动填表工作流,批次号:{batch.batch_no}。"
|
||||
assistant_message = append_assistant_message(conversation, reply_content)
|
||||
yield sse_event(
|
||||
"workflow_started",
|
||||
{
|
||||
"workflow_type": "application_form_fill",
|
||||
"batch_id": batch.pk,
|
||||
"batch_no": batch.batch_no,
|
||||
},
|
||||
)
|
||||
yield sse_event("chunk", {"delta": reply_content})
|
||||
yield sse_event(
|
||||
"done",
|
||||
{
|
||||
"assistant_message_id": assistant_message.pk,
|
||||
"conversation_id": conversation.pk,
|
||||
"title": conversation.title,
|
||||
},
|
||||
)
|
||||
return
|
||||
|
||||
if route.starts_regulatory_review:
|
||||
source_summary_batch = find_latest_successful_summary_batch(conversation)
|
||||
if not source_summary_batch:
|
||||
|
||||
@@ -8,6 +8,7 @@ from .file_summary.workflow_trigger import (
|
||||
evaluate_attachment_reader_trigger,
|
||||
evaluate_file_summary_trigger,
|
||||
)
|
||||
from .application_form_fill.constants import FORM_FILL_TRIGGER_KEYWORDS, WORKFLOW_TYPE as FORM_FILL_WORKFLOW_TYPE
|
||||
from .llm import LLMConfigurationError, LLMRequestError, generate_completion
|
||||
from .models import Conversation, FileAttachment
|
||||
|
||||
@@ -16,6 +17,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
ROUTE_ACTIONS = {"normal_chat", "attachment_reader", "file_summary"}
|
||||
ROUTE_ACTIONS.add("regulatory_review")
|
||||
ROUTE_ACTIONS.add(FORM_FILL_WORKFLOW_TYPE)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -39,6 +41,10 @@ class SkillRoute:
|
||||
def starts_regulatory_review(self) -> bool:
|
||||
return self.action == "regulatory_review"
|
||||
|
||||
@property
|
||||
def starts_application_form_fill(self) -> bool:
|
||||
return self.action == FORM_FILL_WORKFLOW_TYPE
|
||||
|
||||
@property
|
||||
def is_normal_chat(self) -> bool:
|
||||
return self.action == "normal_chat"
|
||||
@@ -105,7 +111,7 @@ def _route_with_llm(
|
||||
return SkillRoute(
|
||||
action=action,
|
||||
skill_name="attachment_reader" if action == "attachment_reader" else "",
|
||||
workflow_type=action if action in {"file_summary", "regulatory_review"} else "",
|
||||
workflow_type=action if action in {"file_summary", "regulatory_review", FORM_FILL_WORKFLOW_TYPE} else "",
|
||||
confidence=_float_or_zero(payload.get("confidence")),
|
||||
reason=str(payload.get("reason") or ""),
|
||||
source="llm",
|
||||
@@ -113,6 +119,15 @@ def _route_with_llm(
|
||||
|
||||
|
||||
def _route_with_rules(conversation: Conversation, content: str) -> SkillRoute:
|
||||
if _matches_application_form_fill(content):
|
||||
return SkillRoute(
|
||||
action=FORM_FILL_WORKFLOW_TYPE,
|
||||
workflow_type=FORM_FILL_WORKFLOW_TYPE,
|
||||
confidence=0.7,
|
||||
reason="命中申报文件自动填表关键词。",
|
||||
source="rule_fallback",
|
||||
)
|
||||
|
||||
if _matches_regulatory_review(content):
|
||||
return SkillRoute(
|
||||
action="regulatory_review",
|
||||
@@ -162,10 +177,11 @@ def _router_system_prompt() -> str:
|
||||
return (
|
||||
"你是审核智能体的工具路由器,只判断是否需要调用工具,不直接回答用户。"
|
||||
"你必须只输出 JSON 对象,不要输出 Markdown。"
|
||||
"可选 action:normal_chat、attachment_reader、file_summary、regulatory_review。"
|
||||
"可选 action:normal_chat、attachment_reader、file_summary、regulatory_review、application_form_fill。"
|
||||
"attachment_reader 用于用户要求阅读、提取、分析、总结、查看上传附件内容。"
|
||||
"file_summary 用于用户要求自动汇总文件目录、页数、清单或生成目录页数报告。"
|
||||
"regulatory_review 用于用户要求法规核查、NMPA核查、完整性核查、章节一致性核查、风险预警或整改建议。"
|
||||
"application_form_fill 用于用户要求填注册证、生成申报模板、填写对应表格、安全和性能基本原则清单或自动填表。"
|
||||
"normal_chat 用于不需要读取附件或执行工作流的一般问答。"
|
||||
"输出字段:action、confidence、reason。"
|
||||
)
|
||||
@@ -217,3 +233,8 @@ def _matches_regulatory_review(content: str) -> bool:
|
||||
"一致性核查",
|
||||
]
|
||||
return any(keyword in normalized for keyword in keywords)
|
||||
|
||||
|
||||
def _matches_application_form_fill(content: str) -> bool:
|
||||
normalized = content.lower()
|
||||
return any(keyword.lower() in normalized for keyword in FORM_FILL_TRIGGER_KEYWORDS)
|
||||
|
||||
Reference in New Issue
Block a user