Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(material): stop unsafe image retries (#1432)

Rudy Mizrahi Celeklie15be0e98414
diff --git a/app/services/material.py b/app/services/material.py
index c6f80d01..fe774bba 100644
--- a/app/services/material.py
+++ b/app/services/material.py
@@ -43,6 +43,14 @@ class _OpenAIImageDecodeError(ValueError):
"""表示兼容接口返回的字节无法解码为图片,不包含本地文件写入故障。"""
+class OpenAIImagePaidResultError(RuntimeError):
+ """A paid image request cannot provide a usable local video material."""
+
+
+class OpenAIImageUnconfirmedError(OpenAIImagePaidResultError):
+ """A paid image request may have succeeded without returning a response."""
+
+
def _safe_public_url(value: Any) -> str | None:
"""
只保留可公开展示的 HTTP(S) 页面地址,并移除查询参数和凭据。
@@ -1337,7 +1345,7 @@ def _openai_image_download_bytes(
下载已生成图片的临时 URL。
图片已经按张计费,下载失败时优先重试原地址,而不是回退到重新生成,
- 避免为同一张图重复付费。
+ 避免为同一张图重复付费。全部重试失败时抛错,阻止外层购买下一张图。
"""
failure_detail = "no download attempt was made"
for attempt in range(1, OPENAI_IMAGE_MAX_DOWNLOAD_ATTEMPTS + 1):
@@ -1376,7 +1384,8 @@ def _openai_image_download_bytes(
failure_detail = f"HTTP {response.status_code} while downloading image"
except Exception as e:
failure_detail = (
- f"error={type(e).__name__}, detail={_redact_request_error(e, api_key)}"
+ f"error={type(e).__name__}, "
+ f"detail={_redact_request_error(e, api_key, image_url)}"
)
finally:
if response is not None:
@@ -1394,7 +1403,9 @@ def _openai_image_download_bytes(
f"{failure_detail}"
)
time.sleep(OPENAI_IMAGE_DOWNLOAD_BACKOFF_SECONDS)
- return None, failure_detail
+ raise OpenAIImagePaidResultError(
+ "generated image could not be downloaded after retries"
+ )
def _parse_openai_image_response(
@@ -1448,8 +1459,8 @@ def _request_openai_image(endpoint: str, payload: dict) -> tuple[bytes | None, s
计费安全:POST 的读超时与连接中断视为"未确认"状态——服务端可能已经
生成并扣费,只是响应没有返回,自动重新提交可能造成重复生成和重复
- 计费,因此不做重试。只有连接阶段超时(ConnectTimeout,请求确定没有
- 送达服务端)才确认没有创建生成任务,可以安全重试。
+ 计费,因此抛出专用异常终止本地任务。只有连接阶段超时(ConnectTimeout,
+ 请求确定没有送达服务端)才确认没有创建生成任务,可以安全重试。
API Key 允许为空:完全本地的 ComfyUI/SD 网关通常不需要鉴权,为空时
不发送 Authorization 头。
@@ -1487,11 +1498,11 @@ def _request_openai_image(endpoint: str, payload: dict) -> tuple[bytes | None, s
retryable = True
except Exception as e:
# 读超时/连接中断等属于"未确认"状态:服务端可能已经受理并扣费,
- # 自动重新提交可能重复生成、重复计费,交由上层跳过该关键词。
- failure_detail = (
- f"unconfirmed request error (no retry to avoid double billing): "
+ # 自动重新提交或处理后续关键词都可能重复计费,必须终止本地任务。
+ raise OpenAIImageUnconfirmedError(
+ "unconfirmed image request (no retry to avoid double billing): "
f"{type(e).__name__}, detail={_redact_request_error(e, api_key)}"
- )
+ ) from e
else:
status = int(getattr(response, "status_code", 200) or 200)
if status in OPENAI_IMAGE_KEY_ERROR_STATUS_CODES:
@@ -1648,7 +1659,7 @@ def _render_openai_image_video(image_path: str, clip_duration: int) -> str:
"""
把生成的图片渲染成 mp4 片段,复用 local 素材的"图片 → 动态片段"管线。
- 渲染失败按素材源约定返回空字符串,由调用方跳过该图片继续。
+ 渲染失败返回空字符串,由调用方停止后续付费请求。
"""
try:
return video.render_image_zoom_video(image_path, clip_duration)
@@ -1675,7 +1686,8 @@ def _download_videos_openai_image_on_demand(
与 WaveSpeed 按需生成同一付费安全语义:文生图按张计费,先全量生成再
挑选会为用不到的画面付费。每张图片生成后立即渲染成 mp4 片段并累计
有效时长(与库存流程一致,按片段时长封顶),累计达到所需配音时长后
- 不再发起新的付费请求。单张失败按素材源约定跳过并继续下一个关键词。
+ 不再发起新的付费请求。明确拒绝的单张图片可跳过;请求结果不明、付费
+ 结果下载失败或本地渲染失败时必须终止任务,避免后续关键词再次计费。
"""
if not material_directory:
# 生成图片按任务计费且不可复用,默认落在任务目录便于追溯。
@@ -1700,16 +1712,23 @@ def _download_videos_openai_image_on_demand(
return video_paths
for search_term in search_terms:
- items = generate_images_openai(
- search_term=search_term,
- minimum_duration=max_clip_duration,
- video_aspect=video_aspect,
- save_dir=material_directory,
- )
+ try:
+ items = generate_images_openai(
+ search_term=search_term,
+ minimum_duration=max_clip_duration,
+ video_aspect=video_aspect,
+ save_dir=material_directory,
+ )
+ except OpenAIImagePaidResultError:
+ _persist_material_sources(task_id, material_sources)
+ raise
for item in items:
video_file = _render_openai_image_video(item.url, max_clip_duration)
if not video_file:
- continue
+ _persist_material_sources(task_id, material_sources)
+ raise OpenAIImagePaidResultError(
+ "generated image could not be rendered locally"
+ )
logger.info(f"image material rendered: {video_file}")
video_paths.append(video_file)
try:
diff --git a/app/services/task.py b/app/services/task.py
index 93127a60..3aad8d82 100644
--- a/app/services/task.py
+++ b/app/services/task.py
@@ -773,6 +773,11 @@ def get_video_materials(
details = {"wavespeed_prediction_id": prediction_id} if prediction_id else None
_mark_task_failed(task_id, "materials", str(exc), details=details)
return None
+ except material.OpenAIImagePaidResultError as exc:
+ # A paid request with an uncertain response, failed result download,
+ # or failed local render must stop before another image order.
+ _mark_task_failed(task_id, "materials", str(exc))
+ return None
except ofox.OFoxError as exc:
# 与方舟同一恢复语义:未确认状态和已生成但下载失败都对应一个可在
# OFox 控制台恢复的远端任务,统一从异常携带的 task_id 写入失败状态。
diff --git a/test/services/test_material_openai_image.py b/test/services/test_material_openai_image.py
index 6ae33a77..d954f7d5 100644
--- a/test/services/test_material_openai_image.py
+++ b/test/services/test_material_openai_image.py
@@ -445,6 +445,36 @@ class TestOpenAIImageProvider(unittest.TestCase):
self.assertEqual(os.listdir(self.save_dir), [])
+ def test_generated_image_download_failure_does_not_buy_another_image(self):
+ """A confirmed paid result remains a task failure if its URL cannot download."""
+ signed_url = "https://cdn.example.com/generated/x.png?token=private"
+ response = _image_response(
+ {"data": [{"url": signed_url}]}
+ )
+ with (
+ patch("app.services.material.requests.post", return_value=response) as post,
+ patch(
+ "app.services.material.requests.get",
+ side_effect=requests.exceptions.ConnectionError(
+ f"download failed for {signed_url}"
+ ),
+ ) as get,
+ patch("app.services.material.time.sleep"),
+ patch("app.services.material.logger") as logger,
+ ):
+ with self.assertRaisesRegex(RuntimeError, "could not be downloaded"):
+ material.download_videos(
+ task_id="test-openai-image-download-failure",
+ search_terms=["first", "second"],
+ source="openai_image",
+ audio_duration=10,
+ max_clip_duration=5,
+ )
+
+ self.assertEqual(post.call_count, 1)
+ self.assertEqual(get.call_count, material.OPENAI_IMAGE_MAX_DOWNLOAD_ATTEMPTS)
+ self.assertNotIn("token=private", str(logger.warning.call_args_list))
+
def test_generate_images_openai_returns_empty_on_rejected_request(self):
"""业务拒绝(如内容策略)返回空结果,不做退避重试。"""
response = _image_response(
@@ -535,7 +565,7 @@ class TestOpenAIImageProvider(unittest.TestCase):
"""
读超时/连接中断属于"未确认"状态:服务端可能已经生成并扣费,只是
响应没有返回。自动重新提交会造成重复生成和重复计费,必须直接
- 失败交由上层跳过该关键词。
+ 失败并终止任务,不能继续向后续关键词提交付费请求。
"""
for error in (
requests.exceptions.ReadTimeout("read timed out"),
@@ -548,14 +578,49 @@ class TestOpenAIImageProvider(unittest.TestCase):
) as post,
patch("app.services.material.time.sleep") as sleep,
):
- results = material.generate_images_openai(
- "unconfirmed term", minimum_duration=5, save_dir=self.save_dir
- )
+ with self.assertRaisesRegex(RuntimeError, "unconfirmed"):
+ material.generate_images_openai(
+ "unconfirmed term",
+ minimum_duration=5,
+ save_dir=self.save_dir,
+ )
- self.assertEqual(results, [])
self.assertEqual(post.call_count, 1)
sleep.assert_not_called()
+ def test_download_videos_openai_image_stops_after_unconfirmed_paid_request(self):
+ """Earlier images must not hide a later ambiguous paid submission."""
+ image_response = _image_response(
+ {"data": [{"b64_json": base64.b64encode(_png_bytes()).decode("ascii")}]}
+ )
+ config.app["material_directory"] = self.save_dir
+
+ with (
+ patch(
+ "app.services.material.requests.post",
+ side_effect=[
+ image_response,
+ requests.exceptions.ReadTimeout("response lost"),
+ image_response,
+ ],
+ ) as post,
+ patch(
+ "app.services.material._render_openai_image_video",
+ return_value="/tmp/rendered.mp4",
+ ),
+ patch("app.services.material._persist_material_sources"),
+ ):
+ with self.assertRaisesRegex(RuntimeError, "unconfirmed"):
+ material.download_videos(
+ task_id="test-openai-image-unconfirmed",
+ search_terms=["first", "uncertain", "third"],
+ source="openai_image",
+ audio_duration=20,
+ max_clip_duration=5,
+ )
+
+ self.assertEqual(post.call_count, 2)
+
def test_generate_images_openai_size_defaults_and_override(self):
"""
默认按画幅取 OpenAI 官方兼容尺寸(portrait 1024x1536 /
@@ -817,10 +882,9 @@ class TestOpenAIImageProvider(unittest.TestCase):
cached_search.assert_not_called()
self.assertEqual(result, ["/tmp/img-1.png.mp4"])
- def test_download_videos_openai_image_skips_failed_segment_and_continues(self):
+ def test_download_videos_openai_image_skips_rejected_segment(self):
"""
- 单张生成失败(空结果)或渲染失败时跳过该关键词,继续为后续片段生成,
- 已成功的素材照常返回。
+ 明确拒绝的生成结果为空时跳过该关键词,继续处理下一片段。
"""
generated = {
"term-1": [], # 生成失败
@@ -832,8 +896,6 @@ class TestOpenAIImageProvider(unittest.TestCase):
return generated[search_term]
def fake_render(image_path, clip_duration):
- if "img-2" in image_path:
- return "" # term-2 渲染失败
return f"{image_path}.mp4"
with (
@@ -854,9 +916,29 @@ class TestOpenAIImageProvider(unittest.TestCase):
max_clip_duration=5,
)
- self.assertEqual(generate.call_count, 3)
- # term-2 渲染失败被跳过,只有 term-3 的片段进入成片
- self.assertEqual(result, ["/tmp/img-3.png.mp4"])
+ self.assertEqual(generate.call_count, 2)
+ self.assertEqual(result, ["/tmp/img-2.png.mp4"])
+
+ def test_download_videos_openai_image_stops_after_render_failure(self):
+ """A paid image saved locally should not trigger another purchase if render fails."""
+ generated_item = self._generated_item("first", "/tmp/img-1.png")
+ with (
+ patch(
+ "app.services.material.generate_images_openai",
+ return_value=[generated_item],
+ ) as generate,
+ patch("app.services.material._render_openai_image_video", return_value=""),
+ ):
+ with self.assertRaisesRegex(RuntimeError, "could not be rendered"):
+ material.download_videos(
+ task_id="test-openai-image-render-failure",
+ search_terms=["first", "second"],
+ source="openai_image",
+ audio_duration=10,
+ max_clip_duration=5,
+ )
+
+ self.assertEqual(generate.call_count, 1)
def test_download_videos_openai_image_skips_generation_without_audio(self):
"""配音时长非正数时直接空手返回,不为不可能凑够的任务按张付费。"""
diff --git a/test/services/test_task.py b/test/services/test_task.py
index bdee385c..cfe71fc5 100644
--- a/test/services/test_task.py
+++ b/test/services/test_task.py
@@ -542,6 +542,34 @@ class TestTaskService(unittest.TestCase):
self.assertEqual(failed_task["failed_stage"], "materials")
self.assertEqual(failed_task["wavespeed_prediction_id"], "pred-123")
+ def test_paid_openai_image_failure_stops_at_material_stage(self):
+ """Ambiguous and unusable paid image results must stay visible as failures."""
+ params = VideoParams(video_subject="test", video_source="openai_image")
+ for error_type in (
+ tm.material.OpenAIImageUnconfirmedError,
+ tm.material.OpenAIImagePaidResultError,
+ ):
+ with self.subTest(error_type=error_type):
+ state = MemoryState()
+ state.update_task("openai-image-paid-failure", progress=40)
+ with (
+ patch.object(tm.sm, "state", state),
+ patch.object(
+ tm.material,
+ "download_videos",
+ side_effect=error_type("paid image unavailable"),
+ ),
+ ):
+ result = tm.get_video_materials(
+ "openai-image-paid-failure", params, ["scene"], 10
+ )
+
+ self.assertIsNone(result)
+ failed_task = state.get_task("openai-image-paid-failure")
+ self.assertEqual(failed_task["state"], tm.const.TASK_STATE_FAILED)
+ self.assertEqual(failed_task["failed_stage"], "materials")
+ self.assertIn("paid image unavailable", failed_task["error"])
+
def test_loomloom_state_failure_does_not_abandon_paid_remote_run(self):
"""状态后端不可用时仍需等待并下载已经开始计费的远端任务。"""
params = VideoParams(video_subject="AI 办公", video_source="loomloom")