Merge branch 'dev' into hugo-shipping-notice
# Conflicts: # backend/app/api/chat.py # backend/app/services/shipping_notice/chat_actions.py
This commit is contained in:
commit
7a9c36d16b
17 files changed
+1014
-357
No files matched your search
@@ -63,31 +63,6 @@ def _ok(payload: dict):
|
||||
return ok(payload, request_id=getattr(g, 'request_id', None))
|
||||
|
||||
|
||||
def _notice_file_type(upload) -> str | None:
|
||||
"""提取上传文件 MIME type;为空时返回 None,保持响应字段稳定。"""
|
||||
return str(getattr(upload, 'mimetype', '') or '').strip() or None
|
||||
|
||||
|
||||
def _build_notice_file_summary(
|
||||
*,
|
||||
total_files: int,
|
||||
accepted_files: list[dict],
|
||||
rejected_files: list[dict],
|
||||
) -> dict[str, object]:
|
||||
"""
|
||||
构建工作台批量上传预检摘要。
|
||||
|
||||
核心逻辑:把可建任务文件与被拒文件统一汇总到 file_summary,供前端回显和测试断言使用。
|
||||
"""
|
||||
return {
|
||||
'total_files': total_files,
|
||||
'accepted_count': len(accepted_files),
|
||||
'rejected_count': len(rejected_files),
|
||||
'accepted_files': accepted_files,
|
||||
'rejected_files': rejected_files,
|
||||
}
|
||||
|
||||
|
||||
@workspace_shipping_notice_bp.route('/shipping-tasks', methods=['GET'])
|
||||
def workspace_shipping_tasks():
|
||||
"""列出当前用户可见的发货任务,可按批次与处理状态过滤。"""
|
||||
@@ -123,77 +98,27 @@ def workspace_create_shipping_batch_from_notices():
|
||||
raise ValidationError('files is required')
|
||||
from app.services.shipping_notice import tools as shipping_tools
|
||||
from app.services.shipping_notice.notice_upload import (
|
||||
build_notice_file_summary,
|
||||
cleanup_temp_notice_uploads,
|
||||
extract_notice_text,
|
||||
guess_source_type,
|
||||
max_notice_chars,
|
||||
save_temp_notice_upload,
|
||||
intake_temp_notice_uploads,
|
||||
)
|
||||
conversation_id = str(request.form.get('conversation_id') or '').strip()
|
||||
temp_request_id = str(getattr(g, 'request_id', None) or uuid.uuid4().hex)
|
||||
notice_items: list[dict] = []
|
||||
accepted_files: list[dict] = []
|
||||
rejected_files: list[dict] = []
|
||||
try:
|
||||
for index, upload in enumerate(uploads, start=1):
|
||||
raw = upload.read() or b''
|
||||
filename = upload.filename or 'notice'
|
||||
source_type = guess_source_type(filename)
|
||||
source_file_type = _notice_file_type(upload)
|
||||
text = ''
|
||||
warnings: list[str] = []
|
||||
try:
|
||||
saved_path = save_temp_notice_upload(temp_request_id, index, filename, raw)
|
||||
text, meta = extract_notice_text(saved_path)
|
||||
warnings = list(meta.get('warnings') or []) if isinstance(meta, dict) else []
|
||||
except Exception as exc:
|
||||
warnings = [f'notice_extract_failed:{exc}']
|
||||
if text:
|
||||
limit = max_notice_chars()
|
||||
if len(text) > limit:
|
||||
text = text[:limit]
|
||||
warnings.append(f'notice_text_truncated:{limit}')
|
||||
notice_items.append(
|
||||
{
|
||||
'source_type': source_type,
|
||||
'source_file_name': filename,
|
||||
'source_file_type': source_file_type,
|
||||
'raw_text': text,
|
||||
'warnings': warnings,
|
||||
}
|
||||
)
|
||||
accepted_files.append(
|
||||
{
|
||||
'filename': filename,
|
||||
'source_type': source_type,
|
||||
'source_file_type': source_file_type,
|
||||
'text_length': len(text),
|
||||
'warnings': warnings,
|
||||
}
|
||||
)
|
||||
continue
|
||||
rejected_files.append(
|
||||
{
|
||||
'filename': filename,
|
||||
'source_type': source_type,
|
||||
'source_file_type': source_file_type,
|
||||
'reason': 'empty_text',
|
||||
'message': 'notice text is empty after extraction',
|
||||
'warnings': warnings,
|
||||
}
|
||||
)
|
||||
accepted, rejected = intake_temp_notice_uploads(temp_request_id, uploads)
|
||||
finally:
|
||||
cleanup_temp_notice_uploads(temp_request_id)
|
||||
|
||||
file_summary = _build_notice_file_summary(
|
||||
file_summary = build_notice_file_summary(
|
||||
total_files=len(uploads),
|
||||
accepted_files=accepted_files,
|
||||
rejected_files=rejected_files,
|
||||
accepted=accepted,
|
||||
rejected=rejected,
|
||||
)
|
||||
notice_items = [item.to_notice_item() for item in accepted]
|
||||
if not notice_items:
|
||||
raise ValidationError(
|
||||
'all uploaded files failed extraction or contained no parseable text',
|
||||
details={'rejected_files': rejected_files, 'file_summary': file_summary},
|
||||
details={'rejected_files': file_summary['rejected_files'], 'file_summary': file_summary},
|
||||
)
|
||||
|
||||
payload = {
|
||||
@@ -327,8 +252,11 @@ def workspace_create_shipping_task_from_notice():
|
||||
if not upload:
|
||||
raise ValidationError('file is required')
|
||||
from app.services.shipping_notice import tools as shipping_tools
|
||||
from app.services.shipping_notice.notice_upload import extract_notice_text, guess_source_type, max_notice_chars, save_task_notice_upload
|
||||
source_type = guess_source_type(upload.filename or '')
|
||||
from app.services.shipping_notice.notice_upload import guess_source_type, intake_notice_attachment, notice_source_file_type
|
||||
filename = upload.filename or 'notice'
|
||||
raw = upload.read() or b''
|
||||
source_file_type = notice_source_file_type(upload)
|
||||
source_type = guess_source_type(filename)
|
||||
conversation_id = str(request.form.get('conversation_id') or '').strip()
|
||||
payload = {'user_id': user.id, 'source_type': source_type}
|
||||
if conversation_id:
|
||||
@@ -337,29 +265,25 @@ def workspace_create_shipping_task_from_notice():
|
||||
task_id = str(created.get('task_id') or '').strip()
|
||||
if not task_id:
|
||||
raise AppError(code='SYSTEM.INTERNAL_ERROR', message='failed to create task', http_status=500)
|
||||
raw = upload.read() or b''
|
||||
saved_path = save_task_notice_upload(task_id, upload.filename or 'notice', raw)
|
||||
text, meta = extract_notice_text(saved_path)
|
||||
if not text:
|
||||
intake = intake_notice_attachment(
|
||||
filename=filename,
|
||||
raw=raw,
|
||||
source_file_type=source_file_type,
|
||||
storage='task',
|
||||
task_no=task_id,
|
||||
)
|
||||
if not intake.accepted:
|
||||
from app.services.shipping_notice.state_machine import action_buttons_for_checkpoint
|
||||
return jsonify({'status': 'needs_human', 'task_id': task_id, 'message': '未能从附件抽取到可解析文本(可能是扫描件/OCR 不可用)。请复制粘贴文本,或更换清晰版文件后重试。', 'data': {'notice_upload': {'filename': upload.filename or '', **meta}}, 'human_options': ['reparse', 'reject'], 'checkpoint': CP_NOTICE_TEXT_EXTRACTION_FAILED, 'action_buttons': action_buttons_for_checkpoint(CP_NOTICE_TEXT_EXTRACTION_FAILED)})
|
||||
if text:
|
||||
limit = max_notice_chars()
|
||||
if len(text) > limit:
|
||||
text = text[:limit]
|
||||
meta.setdefault('warnings', []).append(f'notice_text_truncated:{limit}')
|
||||
task = ShippingTask.query.filter_by(task_no=task_id).first()
|
||||
if task:
|
||||
task.raw_content = text
|
||||
db.session.commit()
|
||||
return jsonify({'status': 'needs_human', 'task_id': task_id, 'message': '未能从附件抽取到可解析文本(可能是扫描件/OCR 不可用)。请复制粘贴文本,或更换清晰版文件后重试。', 'data': {'notice_upload': intake.to_notice_upload_meta()}, 'human_options': ['reparse', 'reject'], 'checkpoint': CP_NOTICE_TEXT_EXTRACTION_FAILED, 'action_buttons': action_buttons_for_checkpoint(CP_NOTICE_TEXT_EXTRACTION_FAILED)})
|
||||
task = ShippingTask.query.filter_by(task_no=task_id).first()
|
||||
if task:
|
||||
task.source_file_name = str(upload.filename or '').strip() or task.source_file_name
|
||||
task.source_file_type = str(getattr(upload, 'mimetype', '') or '').strip() or task.source_file_type
|
||||
task.raw_content = intake.text
|
||||
task.source_file_name = intake.filename or task.source_file_name
|
||||
task.source_file_type = intake.source_file_type or task.source_file_type
|
||||
db.session.commit()
|
||||
from app.services.shipping_notice.batch_orchestrator import start_task_orchestrator
|
||||
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}})
|
||||
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': intake.to_notice_upload_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):
|
||||
@@ -372,28 +296,25 @@ def workspace_replace_shipping_task_notice(task_no: str):
|
||||
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
|
||||
from app.services.shipping_notice.notice_upload import intake_notice_attachment, notice_source_file_type
|
||||
intake = intake_notice_attachment(
|
||||
filename=upload.filename or 'notice',
|
||||
raw=upload.read() or b'',
|
||||
source_file_type=notice_source_file_type(upload),
|
||||
storage='task',
|
||||
task_no=task.task_no,
|
||||
)
|
||||
task.source_type = intake.source_type 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,
|
||||
raw_text=intake.text,
|
||||
source_file_name=intake.filename,
|
||||
source_file_type=intake.source_file_type,
|
||||
warnings=list(intake.warnings),
|
||||
)
|
||||
data = result.setdefault('data', {})
|
||||
if isinstance(data, dict):
|
||||
data['notice_upload'] = {'filename': filename, **(meta if isinstance(meta, dict) else {})}
|
||||
data['notice_upload'] = intake.to_notice_upload_meta()
|
||||
return _ok(result)
|
||||
|
||||
@workspace_shipping_notice_bp.route('/shipping-tasks/<task_no>', methods=['GET'])
|
||||
|
||||
Reference in new issue
Block a user