2026-07-05 09:28:39 +08:00
|
|
|
"""Generation orchestration service."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
from backend.services.image_service import ImageService
|
|
|
|
|
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-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-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
|
|
|
|
|
task = self.queue_service.create_task(
|
|
|
|
|
{
|
|
|
|
|
"profile_id": workflow.profile_id,
|
|
|
|
|
"workflow_id": workflow.workflow_id,
|
|
|
|
|
"node_id": node_id,
|
|
|
|
|
"status": "queued",
|
|
|
|
|
"model_route": payload.get("model_route") or node.model_route,
|
|
|
|
|
"request": payload,
|
|
|
|
|
"metadata": {"source": "nodes.generate"},
|
|
|
|
|
},
|
|
|
|
|
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",
|
|
|
|
|
"model_route": payload.get("model_route") or node.model_route,
|
|
|
|
|
"metadata": {
|
|
|
|
|
**node.metadata,
|
|
|
|
|
"last_task_id": task.task_id,
|
|
|
|
|
"last_generate_request": payload,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
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-05 09:28:39 +08:00
|
|
|
assets = self.single_flow_engine.generate(running_node, task, payload)
|
|
|
|
|
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,
|
|
|
|
|
"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)
|
|
|
|
|
]
|
|
|
|
|
completed_task = self.queue_service.update_task(
|
|
|
|
|
task.task_id,
|
|
|
|
|
{
|
|
|
|
|
"status": "succeeded",
|
|
|
|
|
"progress": 100,
|
|
|
|
|
"result": {"image_ids": [image.image_id for image in images]},
|
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def _publish(self, event_type: str, payload: dict[str, Any]) -> None:
|
|
|
|
|
if self.event_bus:
|
|
|
|
|
self.event_bus.publish(event_type, payload)
|