feat(shipping_notice): 添加重新上传发货通知功能

- 新增接口支持覆盖当前任务的发货通知
- 重新解析时重置任务状态,避免重复任务
- 增加库存 RPA 等待时间的复查逻辑
- 更新状态机以处理文本抽取失败的情况
- 增强测试用例以覆盖新功能
This commit is contained in:
hugo committed 2026-06-29 17:52:17 +08:00
1 parent f434402530
commit 7f1d5dfbcd
20 files changed
+691 -26

No files matched your search

@@ -52,6 +52,28 @@ def _require_user():
raise UnauthorizedError()
return user
def _maybe_recheck_inventory_rpa_wait(task: ShippingTask | None) -> None:
"""
工作台查询兜底复查库存 RPA 等待态。
常规超时/重试由 shipping worker 执行;这里仅在用户查询时发现已超时仍未推进的任务,补偿触发一次幂等复查。
"""
if not task:
return
from app.services.shipping_notice.inventory_rpa_workflow import maybe_recheck_inventory_rpa_wait
maybe_recheck_inventory_rpa_wait(task, source="workspace_query")
def _maybe_recheck_batch_inventory_rpa_wait(batch: ShippingBatch | None) -> None:
"""对批次下任务执行工作台查询兜底复查,避免 worker 异常时页面长期停在等待结果。"""
if not batch:
return
for task in batch.tasks or []:
_maybe_recheck_inventory_rpa_wait(task)
@workspace_shipping_notice_bp.route('/shipping-tasks', methods=['GET'])
def workspace_shipping_tasks():
"""列出当前用户可见的发货任务,可按批次与处理状态过滤。"""
@@ -72,6 +94,11 @@ def workspace_shipping_batches():
def workspace_shipping_conversation_batch_workspace(conversation_id: str):
"""返回一个 Agent 会话下的多批次执行工作台快照。"""
user = _require_user()
batches = ShippingBatch.query.filter(ShippingBatch.conversation_id == str(conversation_id or "").strip()).filter(
(ShippingBatch.user_id == user.id) | (ShippingBatch.user_id.is_(None))
).all()
for batch in batches:
_maybe_recheck_batch_inventory_rpa_wait(batch)
return jsonify(get_conversation_batch_workspace(conversation_id, user.id))
@workspace_shipping_notice_bp.route('/shipping-batches/create-from-notices', methods=['POST'])
@@ -138,6 +165,7 @@ def workspace_shipping_batch_detail(batch_no: str):
batch = get_batch_for_user(batch_no, user.id)
if not batch:
raise NotFoundError('Not found')
_maybe_recheck_batch_inventory_rpa_wait(batch)
focus_task = get_focus_task_for_batch(batch_no, user.id)
return jsonify({**serialize_batch_summary(batch), 'tasks': list_tasks_for_batch(batch_no, user.id), 'focus_task': serialize_task_summary(focus_task) if focus_task else None})
@@ -148,6 +176,7 @@ def workspace_shipping_batch_tasks(batch_no: str):
batch = get_batch_for_user(batch_no, user.id)
if not batch:
raise NotFoundError('Not found')
_maybe_recheck_batch_inventory_rpa_wait(batch)
return jsonify(list_tasks_for_batch(batch_no, user.id))
@workspace_shipping_notice_bp.route('/shipping-batches/<batch_no>/focus-task', methods=['GET'])
@@ -157,6 +186,7 @@ def workspace_shipping_batch_focus_task(batch_no: str):
batch = get_batch_for_user(batch_no, user.id)
if not batch:
raise NotFoundError('Not found')
_maybe_recheck_batch_inventory_rpa_wait(batch)
focus_task = get_focus_task_for_batch(batch_no, user.id)
if not focus_task:
raise NotFoundError('Focus task not found')
@@ -240,6 +270,41 @@ def workspace_create_shipping_task_from_notice():
orchestrator_started = start_task_orchestrator(task_id)
return jsonify({'status': 'ok', 'task_id': task_id, 'message': f'已上传发货通知文件并创建任务 {task_id}。通知原文已保存,后台已开始处理。' if orchestrator_started else f'已上传发货通知文件并创建任务 {task_id}。通知原文已保存。', 'data': {'task': serialize_task_summary(task) if task else {'task_no': task_id}, 'notice_upload': {'filename': upload.filename or '', **meta}, 'parse_status': 'deferred', 'orchestrator_started': orchestrator_started}})
@workspace_shipping_notice_bp.route('/shipping-tasks/<task_no>/replace-notice', methods=['POST'])
def workspace_replace_shipping_task_notice(task_no: str):
"""重新上传发货通知并覆盖当前任务,不新建批次/任务。"""
user = _require_user()
task = get_task_for_user(task_no, user.id)
if not task:
raise NotFoundError('Not found')
upload = request.files.get('file')
if not upload:
raise ValidationError('file is required')
from app.services.shipping_notice import task_workflow
from app.services.shipping_notice.notice_upload import extract_notice_text, guess_source_type, max_notice_chars, save_task_notice_upload
raw = upload.read() or b''
filename = upload.filename or 'notice'
saved_path = save_task_notice_upload(task.task_no, filename, raw)
text, meta = extract_notice_text(saved_path)
warnings = list(meta.get('warnings') or []) if isinstance(meta, dict) else []
if text:
limit = max_notice_chars()
if len(text) > limit:
text = text[:limit]
warnings.append(f'notice_text_truncated:{limit}')
task.source_type = guess_source_type(filename) or task.source_type
result = task_workflow.replace_task_notice(
task,
raw_text=text,
source_file_name=filename,
source_file_type=str(getattr(upload, 'mimetype', '') or '').strip(),
warnings=warnings,
)
data = result.setdefault('data', {})
if isinstance(data, dict):
data['notice_upload'] = {'filename': filename, **(meta if isinstance(meta, dict) else {})}
return jsonify(result)
@workspace_shipping_notice_bp.route('/shipping-tasks/<task_no>', methods=['GET'])
def workspace_shipping_task_detail(task_no: str):
"""返回指定任务的详情快照。"""
@@ -247,6 +312,7 @@ def workspace_shipping_task_detail(task_no: str):
task = get_task_for_user(task_no, user.id)
if not task:
raise NotFoundError('Not found')
_maybe_recheck_inventory_rpa_wait(task)
return jsonify(serialize_task_detail(task))
@workspace_shipping_notice_bp.route('/shipping-tasks/<task_no>/chat-actions', methods=['GET'])