T-565 handle relative cmhub image urls
This commit is contained in:
@@ -987,7 +987,7 @@ def _request_cmhub_cover_image_sync(
|
||||
request_elapsed = time.perf_counter() - request_started
|
||||
_emit_cmhub_metadata(on_event, data, "cover_request")
|
||||
_notify_step(on_step, "cover_parse_response")
|
||||
image_url = str(data.get("image_url") or "").strip()
|
||||
image_url = _extract_cmhub_image_url(data, runtime["base_url"])
|
||||
if not image_url:
|
||||
raise AIError("AI 返回中没有图片数据")
|
||||
_notify_step_event(
|
||||
@@ -1202,8 +1202,7 @@ def _poll_cmhub_cover_image_task(
|
||||
poll_index += 1
|
||||
continue
|
||||
if status == "succeeded":
|
||||
result = data.get("result") if isinstance(data.get("result"), dict) else {}
|
||||
image_url = str(result.get("image_url") or data.get("image_url") or "").strip()
|
||||
image_url = _extract_cmhub_image_url(data, runtime["base_url"])
|
||||
if not image_url:
|
||||
raise CMHubError("bad_response", "cmhub 生图任务成功但没有图片地址", retryable=False)
|
||||
_notify_step(on_step, "cover_parse_response")
|
||||
@@ -1362,6 +1361,40 @@ def _debug_cmhub_image_url_enabled():
|
||||
return value.strip().lower() in {"1", "true", "yes", "on", "debug"}
|
||||
|
||||
|
||||
def _extract_cmhub_image_url(data, base_url):
|
||||
candidate = _find_image_ref(data)
|
||||
if not candidate:
|
||||
return ""
|
||||
return _normalize_cmhub_image_url(candidate, base_url)
|
||||
|
||||
|
||||
def _normalize_cmhub_image_url(value, base_url):
|
||||
text = str(value or "").strip()
|
||||
if not text:
|
||||
return ""
|
||||
parts = urllib.parse.urlsplit(text)
|
||||
if parts.scheme in {"http", "https"}:
|
||||
return text
|
||||
if parts.scheme:
|
||||
raise CMHubError(
|
||||
"bad_response",
|
||||
"cmhub 生图任务返回的图片地址格式错误",
|
||||
retryable=False,
|
||||
)
|
||||
base = str(base_url or "").strip()
|
||||
if not base:
|
||||
raise CMHubError(
|
||||
"bad_response",
|
||||
"cmhub 生图任务返回了相对图片地址,但缺少 cmhub Base URL",
|
||||
retryable=False,
|
||||
)
|
||||
base_parts = urllib.parse.urlsplit(base)
|
||||
if text.startswith("//"):
|
||||
scheme = base_parts.scheme or "https"
|
||||
return f"{scheme}:{text}"
|
||||
return urllib.parse.urljoin(base.rstrip("/") + "/", text)
|
||||
|
||||
|
||||
def _download_cmhub_image_with_retry(
|
||||
url,
|
||||
connect_timeout,
|
||||
@@ -2273,7 +2306,19 @@ def _find_image_ref(value):
|
||||
candidate = image_url.get("url")
|
||||
if isinstance(candidate, str):
|
||||
return candidate
|
||||
for key in ("data", "choices", "output", "content", "images"):
|
||||
if isinstance(image_url, list):
|
||||
candidate = _find_image_ref_from_list(image_url)
|
||||
if candidate:
|
||||
return candidate
|
||||
for key in ("image_urls", "urls"):
|
||||
candidate = value.get(key)
|
||||
if isinstance(candidate, str) and candidate.strip():
|
||||
return candidate.strip()
|
||||
if isinstance(candidate, list):
|
||||
found = _find_image_ref_from_list(candidate)
|
||||
if found:
|
||||
return found
|
||||
for key in ("result", "data", "choices", "output", "content", "images", "files"):
|
||||
candidate = _find_image_ref(value.get(key))
|
||||
if candidate:
|
||||
return candidate
|
||||
@@ -2283,10 +2328,17 @@ def _find_image_ref(value):
|
||||
if candidate:
|
||||
return candidate
|
||||
elif isinstance(value, list):
|
||||
for item in value:
|
||||
candidate = _find_image_ref(item)
|
||||
if candidate:
|
||||
return candidate
|
||||
return _find_image_ref_from_list(value)
|
||||
return None
|
||||
|
||||
|
||||
def _find_image_ref_from_list(values):
|
||||
for item in values:
|
||||
if isinstance(item, str) and item.strip():
|
||||
return item.strip()
|
||||
candidate = _find_image_ref(item)
|
||||
if candidate:
|
||||
return candidate
|
||||
return None
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user