2026-07-05 09:28:39 +08:00
|
|
|
"""Single image workflow mock engine."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import base64
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
from backend.dto.job_dto import GenerationTask
|
|
|
|
|
from backend.dto.workflow_dto import WorkflowNode
|
|
|
|
|
from core.storage import VfsStorage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ONE_PIXEL_PNG = base64.b64decode(
|
|
|
|
|
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/p9sAAAAASUVORK5CYII="
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
|
class MockGeneratedAsset:
|
|
|
|
|
image_uri: str
|
|
|
|
|
thumbnail_uri: str
|
|
|
|
|
positive_prompt: str
|
|
|
|
|
model_route: str | None
|
|
|
|
|
width: int
|
|
|
|
|
height: int
|
|
|
|
|
seed: int
|
|
|
|
|
metadata: dict[str, Any]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SingleFlowEngine:
|
|
|
|
|
def __init__(self, storage: VfsStorage | None = None):
|
|
|
|
|
self.storage = storage or VfsStorage()
|
|
|
|
|
|
|
|
|
|
def generate(self, node: WorkflowNode, task: GenerationTask, request: dict[str, Any]) -> list[MockGeneratedAsset]:
|
|
|
|
|
count = _bounded_count(request.get("count", 1))
|
|
|
|
|
prompt = _compose_prompt(node.prompt, request.get("prompt_patch"))
|
|
|
|
|
model_route = str(request.get("model_route") or node.model_route or "image_low")
|
|
|
|
|
width, height = _dimensions(str(request.get("aspect_ratio") or "1:1"))
|
|
|
|
|
assets: list[MockGeneratedAsset] = []
|
|
|
|
|
for index in range(count):
|
|
|
|
|
seed = int(request.get("seed") or 1000 + index)
|
|
|
|
|
image_uri = f"storage://images/single/{task.task_id}_{index + 1}.png"
|
|
|
|
|
thumbnail_uri = f"storage://thumbnails/single/{task.task_id}_{index + 1}.png"
|
|
|
|
|
self._write_png(image_uri)
|
|
|
|
|
self._write_png(thumbnail_uri)
|
|
|
|
|
assets.append(
|
|
|
|
|
MockGeneratedAsset(
|
|
|
|
|
image_uri=image_uri,
|
|
|
|
|
thumbnail_uri=thumbnail_uri,
|
|
|
|
|
positive_prompt=prompt,
|
|
|
|
|
model_route=model_route,
|
|
|
|
|
width=width,
|
|
|
|
|
height=height,
|
|
|
|
|
seed=seed,
|
|
|
|
|
metadata={
|
|
|
|
|
"engine": "single_flow_mock",
|
|
|
|
|
"quality": request.get("quality", "low"),
|
|
|
|
|
"mock": True,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
return assets
|
|
|
|
|
|
|
|
|
|
def _write_png(self, uri: str) -> None:
|
|
|
|
|
path = self.storage.resolve_uri(uri)
|
|
|
|
|
Path(path).parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
Path(path).write_bytes(ONE_PIXEL_PNG)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _bounded_count(value: Any) -> int:
|
|
|
|
|
try:
|
|
|
|
|
count = int(value)
|
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
|
count = 1
|
|
|
|
|
return min(max(count, 1), 8)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _compose_prompt(base_prompt: str | None, prompt_patch: Any) -> str:
|
|
|
|
|
parts = [part.strip() for part in [base_prompt, str(prompt_patch or "")] if part and str(part).strip()]
|
|
|
|
|
return ", ".join(parts) if parts else "single image mock generation"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _dimensions(aspect_ratio: str) -> tuple[int, int]:
|
|
|
|
|
if aspect_ratio == "3:4":
|
|
|
|
|
return 768, 1024
|
|
|
|
|
if aspect_ratio == "4:3":
|
|
|
|
|
return 1024, 768
|
|
|
|
|
if aspect_ratio == "16:9":
|
|
|
|
|
return 1280, 720
|
|
|
|
|
return 1024, 1024
|