fix: harden prompt preview — fail fast and never block Generate (1.3.2)

With an unreachable LLM endpoint the Review-prompt modal fired duplicate
long-hanging requests and left Generate impossible. Preview resolves now
fire exactly once per scene, the H3 rewrite times out after 30s and falls
back to the deterministic template, and on failure the modal populates the
editor with raw scene text so the user can edit + Generate or retry.
This commit is contained in:
Benjiyaya
2026-08-31 23:41:17 +08:00
parent 466af9d078
commit db414f92f1
9 changed files with 152 additions and 19 deletions
+7 -4
View File
@@ -22,25 +22,28 @@ class LLMClient:
base_url: str | None = None,
model: str | None = None,
api_key: str | None = None,
timeout: float = 120.0,
) -> None:
self.base_url = (base_url or settings.llm_base_url).rstrip("/")
self.model = model or settings.llm_model
self.api_key = api_key if api_key is not None else settings.llm_api_key
# With every completion streamed (chat/chat_with_tools consume
# chat_stream), 120 s bounds the gap BETWEEN chunks, not total
# chat_stream), the timeout bounds the gap BETWEEN chunks, not total
# generation time: a thinking model streaming reasoning_content keeps
# the connection fed for as long as it genuinely works, while a dead
# server still fails fast.
self.client = httpx.AsyncClient(timeout=120.0)
# server still fails fast. Shorter timeouts (e.g. the 30 s preview
# path) trade headroom for a snappier deterministic fallback.
self.client = httpx.AsyncClient(timeout=timeout)
@classmethod
def for_role(cls, role: str) -> LLMClient:
def for_role(cls, role: str, *, timeout: float = 120.0) -> LLMClient:
"""Client for an agent role's assigned profile (active fallback)."""
profile = settings.resolve_llm_for_role(role)
return cls(
base_url=profile.get("base_url"),
model=profile.get("model"),
api_key=profile.get("api_key") if isinstance(profile.get("api_key"), str) else None,
timeout=timeout,
)
def _headers(self) -> dict[str, str]:
@@ -67,9 +67,18 @@ def _h3_subjects(
return subjects[:cap], paths[:cap]
async def _h3_rewrite(scene: dict[str, Any], subjects: list[dict[str, Any]]) -> str:
"""LLM rewrite into H3's six-section format, deterministic template on failure."""
client = LLMClient.for_role("video")
async def _h3_rewrite(
scene: dict[str, Any],
subjects: list[dict[str, Any]],
*,
timeout: float = 120.0,
) -> str:
"""LLM rewrite into H3's six-section format, deterministic template on failure.
The timeout bounds the whole wait for a dead endpoint before the template
kicks in — the preview path passes a short value so the UI fails fast.
"""
client = LLMClient.for_role("video", timeout=timeout)
try:
return await client.chat(
build_minimax_h3_ref_messages(scene, subjects), temperature=0.4
@@ -249,7 +258,9 @@ async def preview_scene_prompt(
"agent.thinking",
{"message": f"H3 prompt rewrite · scene {scene.get('order_index')}", "project_id": project_id},
)
prompt = await _h3_rewrite(scene, subjects)
# Preview is interactive — fail fast to the deterministic template
# instead of making the user wait out a dead endpoint.
prompt = await _h3_rewrite(scene, subjects, timeout=30.0)
else:
prompt = scene_video_prompt(scene, characters)
return {"prompt": prompt, "profile": profile, "from_draft": False, "based_on": based_on}