This commit is contained in:
Your Name
2026-10-23 10:40:43 +08:00
parent e974d3cfa5
commit a3d2d3170d
14 changed files with 1660 additions and 83 deletions
@@ -3460,20 +3460,61 @@ def _merge_stream_meta(meta: dict[str, Any], payload: Mapping[str, Any]) -> None
"""Keep observed transfer results when completion only repeats preparation.
Progress describes the latest actual request attempt, including a retry
without files. Completion may instead carry the original prepared manifest;
it is reply metadata and must not undo those observed transfer results.
without files. Terminal metadata may repeat preparation, but an explicit
failure receipt must still correct an earlier reference-sent event.
"""
live_fields = ("items", "attachment_count", "attachment_transfer", "required_sections")
terminal = str(payload.get("event") or "").lower() == "done"
event = str(payload.get("event") or "").lower()
terminal = event in {"done", "error"}
observed = meta.get("_transfer_progress_observed") is True
update = dict(payload)
sections = _required_transfer_sections(payload)
if sections is not None:
update["required_sections"] = sections
required_failure = str(payload.get("code") or payload.get("error_code") or "").startswith("REQUIRED_FILES_")
if required_failure:
update.setdefault("phase", "attachments")
update.setdefault("status", "failed")
failed_submission = required_failure or (
payload.get("phase") == "attachments" and payload.get("status") == "failed"
)
if failed_submission:
# Older servers report only the failed columns/code. That is a failed
# attachment submission, not a later generation failure. Correct known
# items without inventing sources or changing explicitly omitted files.
for key in ("items", "required_sections"):
entries = update.get(key)
if isinstance(entries, list) and (
entries or payload.get("transfer_manifest_final") is True
):
continue
previous = meta.get(key) if key == "items" else _required_transfer_sections(meta)
if isinstance(previous, list):
update[key] = [
{**item, "status": "failed"}
if isinstance(item, Mapping) and item.get("status") in {
"prepared", "uploading", "uploaded", "submitting", "submitted",
} else item
for item in previous
]
error_receipt_fields: set[str] = set()
if event == "error":
for key in ("items", "required_sections"):
entries = update.get(key)
if isinstance(entries, list) and any(
isinstance(item, Mapping) and item.get("status") in {"submitted", "failed", "omitted"}
and (key == "items" or str(item.get("key")) in dict(_REQUIRED_TRANSFER_SECTIONS))
for item in entries
):
error_receipt_fields.add(key)
if "items" in error_receipt_fields:
error_receipt_fields.update(("attachment_count", "attachment_transfer"))
if terminal and observed and payload.get("transfer_manifest_final") is not True:
for key in live_fields:
if key in meta:
if key in error_receipt_fields:
continue
if key == "required_sections" and not any(
isinstance(item, Mapping)
and str(item.get("key")) in dict(_REQUIRED_TRANSFER_SECTIONS)
@@ -3545,7 +3586,12 @@ def _transfer_description(meta: Mapping[str, Any], state: str) -> str:
visible_items: list[Mapping[str, Any]] = []
if isinstance(items, list):
kinds = {"image": "图片", "document": "文档", "audio": "音频", "custom": "附件"}
statuses = {"submitting": "提交中", "submitted": "引用已提交", "omitted": "未提交", "failed": "提交失败"}
statuses = {
"prepared": "等待上传", "uploading": "上传中", "uploaded": "已上传,待提交",
"submitting": "提交中", "submitted": "引用已提交", "omitted": "未提交", "failed": "提交失败",
}
if meta.get("attachment_transfer") == "uploaded_files":
statuses["submitted"] = "已上传并提交"
visible_items = [item for item in items if isinstance(item, Mapping)
and not _is_video_transfer_item(item)]
for index, item in enumerate(visible_items, 1):
@@ -6590,6 +6636,11 @@ class AiConsultDialog(QDialog):
"upstream": "资料已提交,正在等待 AI 正文…",
"cancelled": "本次请求已取消;上游是否停止尚未确认。",
}.get(phase, "正在处理本请求资料…")
if phase == "attachments" and payload.get("status") == "uploading":
index, total = payload.get("upload_index"), payload.get("upload_total")
state = f"正在上传附件({index}/{total})…" if (
type(index) is int and type(total) is int and 0 < index <= total
) else "正在上传附件…"
if phase == "attachments" and payload.get("status") == "submitted":
items = payload.get("items")
visible = [item for item in items if isinstance(item, Mapping)
@@ -6702,12 +6753,12 @@ class AiConsultDialog(QDialog):
self._flush_stream_chunks()
message = friendly_error(error)
error_data = getattr(error, "data", None)
if isinstance(error_data, Mapping):
_merge_stream_meta(self._stream_meta, {**error_data, "event": "error"})
cancelled = self._stream_meta.get("phase") == "cancelled" or (
isinstance(error_data, Mapping) and error_data.get("code") == "AI_ASSISTANT_CANCELLED"
)
if cancelled:
if isinstance(error_data, Mapping):
self._stream_meta.update(error_data)
self._stream_meta["phase"] = "cancelled"
message = _cancel_description(self._stream_meta)
if self._stream_worker is not None:
@@ -11,6 +11,7 @@ import pytest
from PySide6.QtCore import QEvent, QTimer
from PySide6.QtWidgets import QApplication
from doctor_workstation.core.errors import ApiBusinessError
from doctor_workstation.ui.dialogs import ai_consult as module
REQUIRED = (
@@ -207,3 +208,140 @@ def test_attachment_progress_does_not_block_first_actual_column_receipt(dialog,
assert "基本信息:无(已提交)" in text and "日常整个页面:已提交" in text
assert "图片附件1(图片):未提交" in text
assert "状态未回报" not in text
def test_actual_file_upload_progress_is_distinct_from_model_submission(dialog):
items = [{"kind": "image", "label": "图片附件1", "status": "uploaded"},
{"kind": "document", "label": "文档附件1", "status": "uploading"}]
emit(dialog, {"event": "progress", "phase": "attachments", "status": "uploading",
"upload_index": 2, "upload_total": 2, "items": items,
"attachment_transfer": "uploaded_files", "required_sections": sections("prepared")})
text = dialog._stream_bubble.transfer_status.text()
assert "正在上传附件(2/2)" in text
assert "图片附件1(图片):已上传,待提交" in text
assert "文档附件1(文档):上传中" in text
assert "生命体征:已整理,待提交" in text
emit(dialog, {"event": "done", "answer": "合成正文", "transfer_manifest_final": True,
"items": [{**item, "status": "submitted"} for item in items],
"attachment_transfer": "uploaded_files", "required_sections": sections()})
assert "图片附件1(图片):已上传并提交" in dialog._stream_bubble.transfer_status.text()
def submitted_images():
return [
{"kind": "image", "label": f"图片附件{index}", "status": "submitted"}
for index in (1, 2)
]
def submit_images(dialog):
emit(dialog, {"event": "progress", "phase": "attachments", "status": "submitted",
"items": submitted_images(), "attachment_count": 2,
"required_sections": sections()})
def fail(dialog, data):
dialog._stream_failed(dialog._generation, dialog._stream_generation, ApiBusinessError(
"合成必需资料提交失败", data=data,
))
def test_failed_progress_without_items_does_not_keep_successful_file_receipts(dialog):
submit_images(dialog)
emit(dialog, {"event": "progress", "phase": "attachments", "status": "failed",
"required_sections": sections("failed")})
text = dialog._stream_bubble.transfer_status.text()
assert "基本信息:无(提交失败)" in text
assert "图片附件1(图片):提交失败" in text
assert "图片附件2(图片):提交失败" in text
assert "引用已提交" not in text
fail(dialog, {"code": "REQUIRED_FILES_REJECTED"})
dialog._stream_finished(dialog._generation, dialog._stream_generation, dialog._stream_worker)
assert "引用已提交" not in dialog._stream_bubble.transfer_status.text()
assert not dialog._asking and not dialog._stream_completed
assert dialog.send_button.isEnabled() and dialog._stream_ended_at is not None
assert dialog._stream_meta["status"] == "failed" and dialog._stream_worker is None
def test_failed_terminal_manifest_corrects_all_observed_receipts_and_keeps_tail(dialog):
submit_images(dialog)
emit(dialog, {"event": "delta", "text": "合成已有正文"})
emit(dialog, {"event": "delta", "text": "未刷新尾段"})
failed_items = [{**item, "status": "failed"} for item in submitted_images()]
fail(dialog, {"code": "REQUIRED_FILES_REJECTED", "transfer_manifest_final": True,
"items": failed_items, "required_sections": sections("failed"),
"attachment_count": 2, "source_summary": {"diagnosis_count": 3}})
text = dialog._stream_bubble.transfer_status.text()
assert "图片附件1(图片):提交失败" in text and "引用已提交" not in text
assert "基本信息:无(提交失败)" in text and "诊单 3 条" in text
assert dialog._stream_meta["source_summary"]["required_sections"] == sections("failed")
assert dialog._stream_bubble._raw_payload.startswith("合成已有正文未刷新尾段")
assert "合成必需资料提交失败" in dialog._stream_bubble._raw_payload
assert not dialog._stream_completed and not dialog._stream_timeout_timer.isActive()
emit(dialog, {"event": "done", "answer": "不得覆盖失败", "required_sections": sections()})
assert "不得覆盖失败" not in dialog._stream_bubble._raw_payload
assert "引用已提交" not in dialog._stream_bubble.transfer_status.text()
@pytest.mark.parametrize("code", ["REQUIRED_FILES_REJECTED", "REQUIRED_FILES_UNSUPPORTED",
"REQUIRED_FILES_INVALID"])
@pytest.mark.parametrize("manifest", [{}, {"items": [], "required_sections": []}])
def test_legacy_required_file_error_without_manifest_fails_known_receipts_only(dialog, code, manifest):
submit_images(dialog)
dialog._stream_meta["items"].append(
{"kind": "document", "label": "文档附件1", "status": "omitted"},
)
fail(dialog, {"code": code, **manifest})
text = dialog._stream_bubble.transfer_status.text()
assert "图片附件1(图片):提交失败" in text and "引用已提交" not in text
assert "文档附件1(文档):未提交" in text
assert "基本信息:无(提交失败)" in text
def test_error_manifest_without_final_marker_can_correct_failed_items(dialog):
submit_images(dialog)
fail(dialog, {"items": [{**item, "status": "failed"} for item in submitted_images()],
"source_summary": {"required_sections": sections("failed")}})
text = dialog._stream_bubble.transfer_status.text()
assert "图片附件1(图片):提交失败" in text and "引用已提交" not in text
assert "基本信息:无(提交失败)" in text
def test_failure_receipt_preserves_explicit_per_item_results(dialog):
submit_images(dialog)
emit(dialog, {"event": "progress", "phase": "attachments", "status": "failed",
"required_sections": sections("failed"),
"items": [{**submitted_images()[0], "status": "failed"},
submitted_images()[1]]})
text = dialog._stream_bubble.transfer_status.text()
assert "图片附件1(图片):提交失败" in text
assert "图片附件2(图片):引用已提交" in text
def test_authoritative_failure_with_empty_manifest_can_replace_previous_items(dialog):
submit_images(dialog)
fail(dialog, {"code": "REQUIRED_FILES_INVALID", "transfer_manifest_final": True,
"items": [], "attachment_count": 0, "required_sections": sections("failed")})
text = dialog._stream_bubble.transfer_status.text()
assert "图片附件" not in text and "本请求附件通道:0 项" in text
assert "基本信息:无(提交失败)" in text
@pytest.mark.parametrize("data", [None, {"code": "UPSTREAM_STREAM_INTERRUPTED"},
{"source_summary": {"required_sections": sections("prepared")},
"items": [], "attachment_count": 0}])
def test_generation_failure_keeps_actual_submitted_receipts(dialog, data):
submit_images(dialog)
fail(dialog, data)
text = dialog._stream_bubble.transfer_status.text()
assert "图片附件1(图片):引用已提交" in text
assert "基本信息:无(已提交)" in text
assert "本请求附件通道:2 项" in text
assert not dialog._stream_completed and not dialog._asking
def test_failure_with_no_manifest_does_not_invent_sources_or_file_items(dialog):
fail(dialog, {"code": "REQUIRED_FILES_REJECTED"})
text = dialog._stream_bubble.transfer_status.text()
assert "资料栏目清单:服务器尚未回传。" in text
assert "图片附件" not in text and "无(提交失败)" not in text