利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.
Python ai-video-generatorcontent-creationffmpeginstagram-reelsllmpythonshort-videosubtitlestext-to-speechtiktokvideo-automationvideo-workflowworkflow-automationyoutube-shorts
fix(material): preserve failed WaveSpeed paid prediction IDs
Rudy Celekli63d88d9b0f16
diff --git a/app/services/material.py b/app/services/material.pyindex 58afa472..b7796af6 100644--- a/app/services/material.py+++ b/app/services/material.py@@ -699,7 +699,7 @@ class WaveSpeedUnconfirmedTaskError(RuntimeError):这类异常绝不等价于“该任务失败、可以重来”:远端任务可能仍在运行或已经完成并计费。素材流程必须就此停止,不再为后续关键词提交新的付费任务,- 并把已提交的 prediction id 留在日志中供人工找回。+ 并把已提交的 prediction id 传给任务状态供人工找回。"""def __init__(self, message: str, prediction_id: str = ""):@@ -707,6 +707,14 @@ class WaveSpeedUnconfirmedTaskError(RuntimeError):self.prediction_id = prediction_id+class WaveSpeedDownloadError(RuntimeError):+ """A paid prediction completed, but its video could not be saved locally."""++ def __init__(self, message: str, prediction_id: str = ""):+ super().__init__(message)+ self.prediction_id = prediction_id++def _wavespeed_status_code(response: Any) -> int:"""读取响应状态码;测试替身或异常对象缺少该字段时按 200 处理。"""try:@@ -1020,7 +1028,7 @@ def _save_generated_video_with_retry(下载已经付费生成的产物,失败时优先重试同一个地址。重新生成一次远端任务的代价是再付一次费,所以下载抖动必须先在原地址上- 做有限次退避重试,重试耗尽才放弃该片段。+ 做有限次退避重试,重试耗尽后由调用方报告可恢复的付费任务失败。"""for attempt in range(WAVESPEED_MAX_DOWNLOAD_RETRIES + 1):try:@@ -1961,8 +1969,9 @@ def _download_videos_wavespeed_on_demand(每个关键词天然对应一个脚本片段,生成即付费:先全量生成再挑选会为用不到的片段付费。这里每生成一段就立刻下载并累计有效时长(与库存- 流程一致,按片段时长封顶),累计超过所需配音时长后不再触发新的生成- 请求。单段失败按现有素材源约定跳过并继续下一段。+ 流程一致,按片段时长封顶),累计覆盖所需配音时长后不再触发新的生成+ 请求。远端明确失败的片段可跳过;状态不明或下载失败的付费任务必须+ 保留 ID 并终止本地任务。"""video_paths: List[str] = []material_sources: list[dict[str, Any]] = []@@ -1977,19 +1986,29 @@ def _download_videos_wavespeed_on_demand(except WaveSpeedUnconfirmedTaskError as e:# 已提交的付费任务状态不明:远端可能仍在运行或已经完成并计费。# 继续为后续关键词下单会造成重复生成和重复扣费,因此就地停止,- # 并把 prediction id 留在日志里供人工在控制台找回产物。+ # 把 prediction id 交给任务层写入失败状态,供用户找回产物。logger.error("stop submitting new wavespeed tasks, the last submitted task "f"is unconfirmed: prediction_id={e.prediction_id or 'unknown'}, "f"detail={e}")- break+ _persist_material_sources(task_id, material_sources)+ raisefor item in video_items:saved_video_path = _save_generated_video_with_retry(item.url, material_directory, "wavespeed")if not saved_video_path:- continue+ source_info = (+ item.source_info if isinstance(item.source_info, dict) else {}+ )+ prediction_id = str(source_info.get("asset_id") or "").strip()+ _persist_material_sources(task_id, material_sources)+ raise WaveSpeedDownloadError(+ "WaveSpeed generated a paid video but the result could not be "+ f"downloaded: id={prediction_id or 'unknown'}",+ prediction_id=prediction_id,+ )logger.info(f"video saved: {saved_video_path}")video_paths.append(saved_video_path)try:diff --git a/app/services/task.py b/app/services/task.pyindex 29b01a59..6652623f 100644--- a/app/services/task.py+++ b/app/services/task.py@@ -762,6 +762,17 @@ def get_video_materials(details=details,)return None+ except (+ material.WaveSpeedUnconfirmedTaskError,+ material.WaveSpeedDownloadError,+ ) as exc:+ # Once a paid prediction exists, a polling or download failure must+ # leave its ID in task state. Earlier successful clips cannot turn+ # this incomplete run into an apparently completed video task.+ prediction_id = str(getattr(exc, "prediction_id", "") or "").strip()+ details = {"wavespeed_prediction_id": prediction_id} if prediction_id else None+ _mark_task_failed(task_id, "materials", str(exc), details=details)+ return Noneexcept ofox.OFoxError as exc:# 与方舟同一恢复语义:未确认状态和已生成但下载失败都对应一个可在# OFox 控制台恢复的远端任务,统一从异常携带的 task_id 写入失败状态。diff --git a/test/services/test_material.py b/test/services/test_material.pyindex 0e01866c..d4990b44 100644--- a/test/services/test_material.py+++ b/test/services/test_material.py@@ -1622,7 +1622,7 @@ class TestWaveSpeedProvider(unittest.TestCase):"""回归:某个片段的任务状态不明时,后续关键词绝不能再触发新的付费生成请求——否则第一个任务可能仍在运行/已完成,造成重复生成和额外扣费。- 已经下载成功的素材照常返回。+ 已经下载成功的素材不能让整个任务被错误地标记为完成。"""first_item = self._generated_item("term-1", "https://cdn.example.com/1.mp4")@@ -1643,17 +1643,44 @@ class TestWaveSpeedProvider(unittest.TestCase):return_value="/tmp/1.mp4",),):- result = material.download_videos(- task_id="test-wavespeed-unconfirmed",- search_terms=["term-1", "term-2", "term-3"],- source="wavespeed",- audio_duration=100,- max_clip_duration=5,- )+ with self.assertRaises(material.WaveSpeedUnconfirmedTaskError) as ctx:+ material.download_videos(+ task_id="test-wavespeed-unconfirmed",+ search_terms=["term-1", "term-2", "term-3"],+ source="wavespeed",+ audio_duration=100,+ max_clip_duration=5,+ )# term-2 抛出状态不明后立即停止,term-3 不能再产生生成请求self.assertEqual(generate.call_count, 2)- self.assertEqual(result, ["/tmp/1.mp4"])+ self.assertEqual(ctx.exception.prediction_id, "pred-stuck")++ def test_download_videos_wavespeed_stops_after_paid_download_failure(self):+ """下载耗尽重试后不能悄悄为下一个关键词再次付费。"""+ item = self._generated_item("term-1", "https://cdn.example.com/1.mp4")+ with (+ patch(+ "app.services.material.generate_videos_wavespeed",+ return_value=[item],+ ) as generate,+ patch("app.services.material.save_video", return_value="") as save,+ patch("app.services.material.time.sleep"),+ ):+ with self.assertRaises(material.WaveSpeedDownloadError) as ctx:+ material.download_videos(+ task_id="test-wavespeed-paid-download-failure",+ search_terms=["term-1", "term-2"],+ source="wavespeed",+ audio_duration=10,+ max_clip_duration=5,+ )++ self.assertEqual(ctx.exception.prediction_id, "pred-term-1")+ self.assertEqual(generate.call_count, 1)+ self.assertEqual(+ save.call_count, material.WAVESPEED_MAX_DOWNLOAD_RETRIES + 1+ )def test_download_videos_wavespeed_retries_original_download_url(self):"""diff --git a/test/services/test_task.py b/test/services/test_task.pyindex fc960403..f24f5643 100644--- a/test/services/test_task.py+++ b/test/services/test_task.py@@ -479,6 +479,36 @@ class TestTaskService(unittest.TestCase):self.assertEqual(failed_task["loomloom_run_id"], "run-1")self.assertEqual(failed_task["loomloom_listing_version_id"], "version-1")+ def test_wavespeed_paid_material_failure_keeps_prediction_id(self):+ """未确认或已付款但下载失败的任务都应在状态中保留恢复用 ID。"""+ params = VideoParams(video_subject="test", video_source="wavespeed")+ for error_type in (+ tm.material.WaveSpeedUnconfirmedTaskError,+ tm.material.WaveSpeedDownloadError,+ ):+ with self.subTest(error_type=error_type):+ state = MemoryState()+ state.update_task("wavespeed-paid-failure", progress=40)+ with (+ patch.object(tm.sm, "state", state),+ patch.object(+ tm.material,+ "download_videos",+ side_effect=error_type(+ "paid run unavailable", prediction_id="pred-123"+ ),+ ),+ ):+ result = tm.get_video_materials(+ "wavespeed-paid-failure", params, ["scene"], 10+ )++ self.assertIsNone(result)+ failed_task = state.get_task("wavespeed-paid-failure")+ self.assertEqual(failed_task["state"], tm.const.TASK_STATE_FAILED)+ self.assertEqual(failed_task["failed_stage"], "materials")+ self.assertEqual(failed_task["wavespeed_prediction_id"], "pred-123")+def test_loomloom_state_failure_does_not_abandon_paid_remote_run(self):"""状态后端不可用时仍需等待并下载已经开始计费的远端任务。"""params = VideoParams(video_subject="AI 办公", video_source="loomloom")