fix/various fixes
This commit is contained in:
@@ -19,22 +19,51 @@ _PLACEHOLDER_DESCRIPTION_RE = re.compile(
|
||||
|
||||
|
||||
def normalize_image_asset(asset: dict[str, Any] | None) -> dict[str, Any] | None:
|
||||
"""归一化单条图片 dict。
|
||||
|
||||
- 兼容旧正文中的 {{{{IMAGE:…}}}} / {{IMAGE:…}} 占位符(需能解析出 description)。
|
||||
- 新模型:插图不嵌入 markdown,可无占位符;已完成且带 url/storage_key 即可通过,
|
||||
description 缺省时用「插图」;pending/processing 至少要有 description、占位符或 prompt。
|
||||
"""
|
||||
if not isinstance(asset, dict):
|
||||
return None
|
||||
|
||||
placeholder = _as_non_empty_string(asset.get("placeholder"))
|
||||
description = _as_non_empty_string(
|
||||
asset.get("description")
|
||||
) or _extract_description_from_placeholder(placeholder)
|
||||
if not placeholder or not description:
|
||||
ph_in = _as_non_empty_string(asset.get("placeholder"))
|
||||
desc_in = _as_non_empty_string(asset.get("description"))
|
||||
desc_from_ph = _extract_description_from_placeholder(ph_in) if ph_in else None
|
||||
merged_description = desc_in or desc_from_ph
|
||||
|
||||
prompt_s = _as_non_empty_string(asset.get("prompt"))
|
||||
error_s = _as_optional_string(asset.get("error"))
|
||||
url_s = _as_optional_string(asset.get("url"))
|
||||
storage_key_s = _as_optional_string(asset.get("storage_key"))
|
||||
has_url_or_key = bool(url_s or storage_key_s)
|
||||
|
||||
status = _as_non_empty_string(asset.get("status")) or IMAGE_STATUS_PENDING
|
||||
|
||||
if ph_in and merged_description:
|
||||
placeholder_out = ph_in
|
||||
description_out = merged_description
|
||||
elif status == IMAGE_STATUS_COMPLETED and has_url_or_key:
|
||||
placeholder_out = ph_in or ""
|
||||
description_out = merged_description or "插图"
|
||||
elif status in (IMAGE_STATUS_PENDING, IMAGE_STATUS_PROCESSING):
|
||||
if not (merged_description or ph_in or prompt_s):
|
||||
return None
|
||||
placeholder_out = ph_in or ""
|
||||
description_out = merged_description or prompt_s or "插图"
|
||||
elif status == IMAGE_STATUS_FAILED:
|
||||
if not (merged_description or ph_in or error_s):
|
||||
return None
|
||||
placeholder_out = ph_in or ""
|
||||
description_out = merged_description or "插图"
|
||||
else:
|
||||
return None
|
||||
|
||||
normalized = dict(asset)
|
||||
normalized["index"] = _coerce_int(asset.get("index"), default=0)
|
||||
normalized["placeholder"] = placeholder
|
||||
normalized["description"] = description
|
||||
|
||||
status = _as_non_empty_string(asset.get("status")) or IMAGE_STATUS_PENDING
|
||||
normalized["placeholder"] = placeholder_out
|
||||
normalized["description"] = description_out
|
||||
if status not in VALID_IMAGE_STATUSES:
|
||||
normalized["status"] = IMAGE_STATUS_FAILED
|
||||
normalized["error"] = asset.get("error") or f"invalid image status: {status}"
|
||||
|
||||
Reference in New Issue
Block a user