2026-07-05 09:28:39 +08:00
|
|
|
"""Generation orchestration service."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
2026-07-05 18:23:35 +08:00
|
|
|
from backend.dto.common import ApiError, ErrorCode
|
|
|
|
|
from backend.dto.job_dto import GenerationTask
|
2026-07-06 02:40:18 +08:00
|
|
|
from backend.services.cost_guard_service import CostGuardService
|
2026-07-05 09:28:39 +08:00
|
|
|
from backend.services.image_service import ImageService
|
2026-07-06 02:01:45 +08:00
|
|
|
from backend.services.model_provider_service import ModelProviderService
|
2026-07-06 02:47:37 +08:00
|
|
|
from backend.services.prompt_expansion_service import PromptExpansionService
|
2026-07-05 09:28:39 +08:00
|
|
|
from backend.services.queue_service import QueueService
|
|
|
|
|
from backend.services.workflow_service import WorkflowService
|
2026-07-05 15:54:48 +08:00
|
|
|
from core.event_bus import EventBus
|
2026-07-05 09:28:39 +08:00
|
|
|
from core.identity import RequestContext
|
|
|
|
|
from engines.single_flow import SingleFlowEngine
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GenerationService:
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
workflow_service: WorkflowService,
|
|
|
|
|
queue_service: QueueService,
|
|
|
|
|
image_service: ImageService,
|
|
|
|
|
single_flow_engine: SingleFlowEngine | None = None,
|
2026-07-06 02:01:45 +08:00
|
|
|
model_provider_service: ModelProviderService | None = None,
|
2026-07-06 02:40:18 +08:00
|
|
|
cost_guard_service: CostGuardService | None = None,
|
2026-07-06 02:47:37 +08:00
|
|
|
prompt_expansion_service: PromptExpansionService | None = None,
|
2026-07-05 15:54:48 +08:00
|
|
|
event_bus: EventBus | None = None,
|
2026-07-05 09:28:39 +08:00
|
|
|
):
|
|
|
|
|
self.workflow_service = workflow_service
|
|
|
|
|
self.queue_service = queue_service
|
|
|
|
|
self.image_service = image_service
|
|
|
|
|
self.single_flow_engine = single_flow_engine or SingleFlowEngine()
|
2026-07-06 02:01:45 +08:00
|
|
|
self.model_provider_service = model_provider_service
|
2026-07-06 02:40:18 +08:00
|
|
|
self.cost_guard_service = cost_guard_service
|
2026-07-06 02:47:37 +08:00
|
|
|
self.prompt_expansion_service = prompt_expansion_service
|
2026-07-05 15:54:48 +08:00
|
|
|
self.event_bus = event_bus
|
2026-07-05 09:28:39 +08:00
|
|
|
|
|
|
|
|
def generate_node(self, node_id: str, payload: dict[str, Any], ctx: RequestContext) -> dict[str, Any]:
|
|
|
|
|
node = self.workflow_service.get_node(node_id)
|
|
|
|
|
workflow = self.workflow_service.get_workflow(node.workflow_id).workflow
|
2026-07-06 02:01:45 +08:00
|
|
|
model_route = str(payload.get("model_route") or node.model_route or "image_low")
|
|
|
|
|
route_resolution = self._resolve_model_route(model_route)
|
2026-07-06 02:40:18 +08:00
|
|
|
cost_guard = self._check_cost_budget(payload, model_route, route_resolution)
|
2026-07-06 02:47:37 +08:00
|
|
|
prompt_expansion = self._expand_prompt(workflow.profile_id, node, payload)
|
2026-07-06 02:01:45 +08:00
|
|
|
generation_request = {
|
|
|
|
|
**payload,
|
|
|
|
|
"model_route": model_route,
|
|
|
|
|
"resolved_model_route": route_resolution,
|
2026-07-06 02:40:18 +08:00
|
|
|
**({"cost_guard": cost_guard} if cost_guard else {}),
|
2026-07-06 02:47:37 +08:00
|
|
|
**(
|
|
|
|
|
{
|
|
|
|
|
"prompt_expansion": prompt_expansion,
|
|
|
|
|
"expanded_prompt": prompt_expansion["prompt"],
|
|
|
|
|
"expanded_negative_prompt": prompt_expansion["negative_prompt"],
|
|
|
|
|
}
|
|
|
|
|
if prompt_expansion
|
|
|
|
|
else {}
|
|
|
|
|
),
|
2026-07-06 02:01:45 +08:00
|
|
|
}
|
2026-07-05 18:23:35 +08:00
|
|
|
idempotency_key = _optional_text(payload.get("idempotency_key"))
|
|
|
|
|
if idempotency_key:
|
|
|
|
|
existing_task = self.queue_service.get_job_by_idempotency_key(idempotency_key)
|
|
|
|
|
if existing_task is not None:
|
|
|
|
|
if existing_task.node_id != node_id:
|
|
|
|
|
raise ApiError(
|
|
|
|
|
ErrorCode.VALIDATION_FAILED,
|
|
|
|
|
"idempotency_key belongs to another node",
|
|
|
|
|
{
|
|
|
|
|
"idempotency_key": idempotency_key,
|
|
|
|
|
"node_id": node_id,
|
|
|
|
|
"existing_node_id": existing_task.node_id,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
return self._existing_generation_response(existing_task, node_id)
|
2026-07-05 09:28:39 +08:00
|
|
|
task = self.queue_service.create_task(
|
|
|
|
|
{
|
|
|
|
|
"profile_id": workflow.profile_id,
|
|
|
|
|
"workflow_id": workflow.workflow_id,
|
|
|
|
|
"node_id": node_id,
|
|
|
|
|
"status": "queued",
|
2026-07-06 02:01:45 +08:00
|
|
|
"model_route": model_route,
|
2026-07-05 18:23:35 +08:00
|
|
|
"idempotency_key": idempotency_key,
|
2026-07-06 02:01:45 +08:00
|
|
|
"request": generation_request,
|
|
|
|
|
"metadata": {
|
|
|
|
|
"source": "nodes.generate",
|
|
|
|
|
"model_route_resolution": route_resolution,
|
|
|
|
|
},
|
2026-07-05 09:28:39 +08:00
|
|
|
},
|
|
|
|
|
ctx,
|
|
|
|
|
)
|
2026-07-05 15:54:48 +08:00
|
|
|
self._publish(
|
|
|
|
|
"generation.node_queued",
|
|
|
|
|
{"workflow_id": workflow.workflow_id, "node_id": node_id, "task_id": task.task_id},
|
|
|
|
|
)
|
2026-07-05 09:28:39 +08:00
|
|
|
self.workflow_service.update_node(
|
|
|
|
|
node_id,
|
|
|
|
|
{
|
|
|
|
|
"status": "queued",
|
2026-07-06 02:01:45 +08:00
|
|
|
"model_route": model_route,
|
2026-07-05 09:28:39 +08:00
|
|
|
"metadata": {
|
|
|
|
|
**node.metadata,
|
|
|
|
|
"last_task_id": task.task_id,
|
2026-07-06 02:01:45 +08:00
|
|
|
"last_generate_request": generation_request,
|
2026-07-05 09:28:39 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
ctx,
|
|
|
|
|
)
|
|
|
|
|
self.queue_service.update_task(task.task_id, {"status": "running", "progress": 20}, ctx)
|
|
|
|
|
running_node = self.workflow_service.update_node(node_id, {"status": "running"}, ctx)
|
2026-07-05 15:54:48 +08:00
|
|
|
self._publish(
|
|
|
|
|
"generation.node_running",
|
|
|
|
|
{"workflow_id": workflow.workflow_id, "node_id": node_id, "task_id": task.task_id},
|
|
|
|
|
)
|
2026-07-06 02:01:45 +08:00
|
|
|
assets = self.single_flow_engine.generate(running_node, task, generation_request)
|
2026-07-05 09:28:39 +08:00
|
|
|
images = [
|
|
|
|
|
self.image_service.create_image(
|
|
|
|
|
{
|
|
|
|
|
"profile_id": workflow.profile_id,
|
|
|
|
|
"workflow_id": workflow.workflow_id,
|
|
|
|
|
"node_id": node_id,
|
|
|
|
|
"task_id": task.task_id,
|
|
|
|
|
"image_uri": asset.image_uri,
|
|
|
|
|
"thumbnail_uri": asset.thumbnail_uri,
|
|
|
|
|
"positive_prompt": asset.positive_prompt,
|
2026-07-06 02:47:37 +08:00
|
|
|
"negative_prompt": asset.negative_prompt,
|
2026-07-05 09:28:39 +08:00
|
|
|
"model_route": asset.model_route,
|
|
|
|
|
"seed": asset.seed,
|
|
|
|
|
"width": asset.width,
|
|
|
|
|
"height": asset.height,
|
|
|
|
|
"is_selected": index == 0,
|
|
|
|
|
"metadata": asset.metadata,
|
|
|
|
|
},
|
|
|
|
|
ctx,
|
|
|
|
|
)
|
|
|
|
|
for index, asset in enumerate(assets)
|
|
|
|
|
]
|
2026-07-06 02:14:13 +08:00
|
|
|
provider_failure = _provider_failure_from_assets([asset.metadata for asset in assets])
|
|
|
|
|
result: dict[str, Any] = {"image_ids": [image.image_id for image in images]}
|
|
|
|
|
update_payload: dict[str, Any] = {
|
|
|
|
|
"status": "succeeded",
|
|
|
|
|
"progress": 100,
|
|
|
|
|
"result": result,
|
|
|
|
|
}
|
|
|
|
|
if provider_failure:
|
|
|
|
|
result["provider_failure"] = provider_failure
|
|
|
|
|
update_payload["metadata"] = {**task.metadata, "provider_failure": provider_failure}
|
2026-07-05 09:28:39 +08:00
|
|
|
completed_task = self.queue_service.update_task(
|
|
|
|
|
task.task_id,
|
2026-07-06 02:14:13 +08:00
|
|
|
update_payload,
|
2026-07-05 09:28:39 +08:00
|
|
|
ctx,
|
|
|
|
|
)
|
|
|
|
|
completed_node = self.workflow_service.update_node(node_id, {"status": "completed"}, ctx)
|
2026-07-05 15:54:48 +08:00
|
|
|
self._publish(
|
|
|
|
|
"generation.node_completed",
|
|
|
|
|
{
|
|
|
|
|
"workflow_id": workflow.workflow_id,
|
|
|
|
|
"node_id": node_id,
|
|
|
|
|
"task_id": completed_task.task_id,
|
|
|
|
|
"image_ids": [image.image_id for image in images],
|
|
|
|
|
},
|
|
|
|
|
)
|
2026-07-05 09:28:39 +08:00
|
|
|
return {
|
|
|
|
|
"task_id": completed_task.task_id,
|
|
|
|
|
"task_status": completed_task.status,
|
|
|
|
|
"node_status": completed_node.status,
|
|
|
|
|
"images": [image.to_dict() for image in images],
|
|
|
|
|
}
|
2026-07-05 15:54:48 +08:00
|
|
|
|
2026-07-05 18:23:35 +08:00
|
|
|
def _existing_generation_response(self, task: GenerationTask, node_id: str) -> dict[str, Any]:
|
|
|
|
|
node = self.workflow_service.get_node(node_id)
|
|
|
|
|
images = []
|
|
|
|
|
for image_id in task.result.get("image_ids", []):
|
|
|
|
|
try:
|
|
|
|
|
images.append(self.image_service.get_image(str(image_id)).to_dict())
|
|
|
|
|
except ApiError as exc:
|
|
|
|
|
if exc.error_code != ErrorCode.RESOURCE_NOT_FOUND:
|
|
|
|
|
raise
|
|
|
|
|
return {
|
|
|
|
|
"task_id": task.task_id,
|
|
|
|
|
"task_status": task.status,
|
|
|
|
|
"node_status": node.status,
|
|
|
|
|
"images": images,
|
|
|
|
|
"idempotent_replay": True,
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-06 02:01:45 +08:00
|
|
|
def _resolve_model_route(self, route_name: str) -> dict[str, Any]:
|
|
|
|
|
if not self.model_provider_service:
|
|
|
|
|
return {"route_name": route_name, "status": "unresolved"}
|
|
|
|
|
resolved = self.model_provider_service.resolve_route(route_name, modality="image_generation")
|
|
|
|
|
if not resolved:
|
|
|
|
|
return {"route_name": route_name, "status": "unresolved"}
|
|
|
|
|
provider = resolved.get("provider") or {}
|
|
|
|
|
return {
|
|
|
|
|
"route_name": resolved["route_name"],
|
|
|
|
|
"route_id": resolved["route_id"],
|
|
|
|
|
"status": "resolved",
|
|
|
|
|
"provider_id": resolved["provider_id"],
|
|
|
|
|
"provider_type": provider.get("provider_type"),
|
|
|
|
|
"model_id": resolved["model_id"],
|
|
|
|
|
"modality": resolved["modality"],
|
|
|
|
|
"priority": resolved["priority"],
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-06 02:40:18 +08:00
|
|
|
def _check_cost_budget(
|
|
|
|
|
self,
|
|
|
|
|
payload: dict[str, Any],
|
|
|
|
|
model_route: str,
|
|
|
|
|
route_resolution: dict[str, Any],
|
|
|
|
|
) -> dict[str, Any] | None:
|
|
|
|
|
budget = payload.get("cost_budget") if isinstance(payload.get("cost_budget"), dict) else None
|
|
|
|
|
if not budget or not self.cost_guard_service:
|
|
|
|
|
return None
|
|
|
|
|
guard = self.cost_guard_service.check_generation_budget(
|
|
|
|
|
provider_id=route_resolution.get("provider_id"),
|
|
|
|
|
provider_model=route_resolution.get("model_id") or model_route,
|
|
|
|
|
model_route=model_route,
|
|
|
|
|
planned_task_count=1,
|
|
|
|
|
planned_image_count=_planned_image_count(payload.get("count")),
|
|
|
|
|
planned_total_tokens=int(budget.get("planned_total_tokens") or 0),
|
|
|
|
|
max_task_count=budget.get("max_task_count"),
|
|
|
|
|
max_image_count=budget.get("max_image_count"),
|
|
|
|
|
max_estimated_cost=budget.get("max_estimated_cost"),
|
|
|
|
|
days=budget.get("days"),
|
|
|
|
|
unit_prices=budget.get("unit_prices") if isinstance(budget.get("unit_prices"), dict) else None,
|
|
|
|
|
)
|
|
|
|
|
if not guard["allowed"]:
|
|
|
|
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "generation budget exceeded", guard)
|
|
|
|
|
return guard
|
|
|
|
|
|
2026-07-06 02:47:37 +08:00
|
|
|
def _expand_prompt(
|
|
|
|
|
self,
|
|
|
|
|
profile_id: str,
|
|
|
|
|
node,
|
|
|
|
|
payload: dict[str, Any],
|
|
|
|
|
) -> dict[str, Any] | None:
|
|
|
|
|
if not self.prompt_expansion_service:
|
|
|
|
|
return None
|
|
|
|
|
profile = self.workflow_service.profile_repository.get(profile_id)
|
|
|
|
|
if profile is None:
|
|
|
|
|
return None
|
|
|
|
|
return self.prompt_expansion_service.expand_for_node(profile, node, payload)
|
|
|
|
|
|
2026-07-05 15:54:48 +08:00
|
|
|
def _publish(self, event_type: str, payload: dict[str, Any]) -> None:
|
|
|
|
|
if self.event_bus:
|
|
|
|
|
self.event_bus.publish(event_type, payload)
|
2026-07-05 18:23:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _optional_text(value: Any) -> str | None:
|
|
|
|
|
if value is None:
|
|
|
|
|
return None
|
|
|
|
|
text = str(value).strip()
|
|
|
|
|
return text or None
|
2026-07-06 02:14:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _provider_failure_from_assets(metadata_items: list[dict[str, Any]]) -> dict[str, Any] | None:
|
|
|
|
|
for metadata in metadata_items:
|
|
|
|
|
failure = metadata.get("provider_error")
|
|
|
|
|
if isinstance(failure, dict):
|
|
|
|
|
return failure
|
|
|
|
|
return None
|
2026-07-06 02:40:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _planned_image_count(value: Any) -> int:
|
|
|
|
|
try:
|
|
|
|
|
count = int(value)
|
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
|
count = 1
|
|
|
|
|
return min(max(count, 1), 8)
|