155 行
6 KiB
Python
155 行
6 KiB
Python
"""Workflow history and image adoption service."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
from uuid import uuid4
|
|
|
|
from backend.dto.common import ApiError, ErrorCode
|
|
from backend.dto.workflow_dto import WorkflowHistoryEntry
|
|
from backend.services.image_service import ImageService
|
|
from backend.services.workflow_service import WorkflowService
|
|
from core.audit_service import AuditService
|
|
from core.identity import RequestContext
|
|
from database.repositories.workflow_repository import WorkflowRepository
|
|
|
|
|
|
class HistoryService:
|
|
def __init__(
|
|
self,
|
|
workflow_repository: WorkflowRepository,
|
|
workflow_service: WorkflowService,
|
|
image_service: ImageService,
|
|
audit_service: AuditService,
|
|
):
|
|
self.workflow_repository = workflow_repository
|
|
self.workflow_service = workflow_service
|
|
self.image_service = image_service
|
|
self.audit_service = audit_service
|
|
|
|
def adopt_node_image(
|
|
self,
|
|
node_id: str,
|
|
payload: dict[str, Any],
|
|
ctx: RequestContext,
|
|
) -> WorkflowHistoryEntry:
|
|
node = self.workflow_service.get_node(node_id)
|
|
workflow = self.workflow_service.get_workflow(node.workflow_id).workflow
|
|
image_id = str(payload.get("image_id") or "").strip()
|
|
if not image_id:
|
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "image_id is required", {"field": "image_id"})
|
|
image = self.image_service.get_image(image_id)
|
|
if image.node_id != node_id:
|
|
raise ApiError(
|
|
ErrorCode.VALIDATION_FAILED,
|
|
"image does not belong to this node",
|
|
{"image_id": image_id, "node_id": node_id},
|
|
)
|
|
if image.is_archived:
|
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "archived image cannot be adopted", {"image_id": image_id})
|
|
selected = self.image_service.select_image(image_id, ctx) if not image.is_selected else image
|
|
updated_node = self.workflow_service.update_node(
|
|
node_id,
|
|
{"status": "selected", "selected_image_id": selected.image_id},
|
|
ctx,
|
|
)
|
|
history = self.workflow_repository.create_history(
|
|
{
|
|
"history_id": payload.get("history_id") or new_history_id(),
|
|
"profile_id": workflow.profile_id,
|
|
"workflow_id": workflow.workflow_id,
|
|
"node_id": node_id,
|
|
"image_id": selected.image_id,
|
|
"branch_name": updated_node.branch_name,
|
|
"action": "adopt_image",
|
|
"title": payload.get("title") or updated_node.title,
|
|
"prompt": selected.positive_prompt or updated_node.prompt,
|
|
"metadata": {
|
|
"image_uri": selected.image_uri,
|
|
"thumbnail_uri": selected.thumbnail_uri,
|
|
"source": "node_adoption",
|
|
**(payload.get("metadata") or {}),
|
|
},
|
|
}
|
|
)
|
|
self.audit_service.record(
|
|
ctx=ctx,
|
|
action="adopt_image",
|
|
target_type="workflow_history",
|
|
target_id=history.history_id,
|
|
after_snapshot=history.to_dict(),
|
|
)
|
|
return history
|
|
|
|
def list_workflow_history(
|
|
self,
|
|
workflow_id: str,
|
|
node_id: str | None = None,
|
|
) -> list[WorkflowHistoryEntry]:
|
|
self.workflow_service.get_workflow(workflow_id)
|
|
if node_id:
|
|
node = self.workflow_service.get_node(node_id)
|
|
if node.workflow_id != workflow_id:
|
|
raise ApiError(
|
|
ErrorCode.VALIDATION_FAILED,
|
|
"node does not belong to this workflow",
|
|
{"workflow_id": workflow_id, "node_id": node_id},
|
|
)
|
|
return self.workflow_repository.list_history(workflow_id, node_id=node_id)
|
|
|
|
def save_workflow_snapshot(
|
|
self,
|
|
workflow_id: str,
|
|
payload: dict[str, Any],
|
|
ctx: RequestContext,
|
|
) -> WorkflowHistoryEntry:
|
|
detail = self.workflow_service.get_workflow(workflow_id)
|
|
workflow = detail.workflow
|
|
node_id = _optional_text(payload.get("node_id"))
|
|
node = None
|
|
if node_id:
|
|
node = self.workflow_service.get_node(node_id)
|
|
if node.workflow_id != workflow_id:
|
|
raise ApiError(
|
|
ErrorCode.VALIDATION_FAILED,
|
|
"node does not belong to this workflow",
|
|
{"workflow_id": workflow_id, "node_id": node_id},
|
|
)
|
|
history = self.workflow_repository.create_history(
|
|
{
|
|
"history_id": payload.get("history_id") or new_history_id(),
|
|
"profile_id": workflow.profile_id,
|
|
"workflow_id": workflow.workflow_id,
|
|
"node_id": node_id,
|
|
"image_id": payload.get("image_id"),
|
|
"branch_name": payload.get("branch_name") or workflow.current_branch or "main",
|
|
"action": payload.get("action") or "save_version",
|
|
"title": payload.get("title") or workflow.display_name,
|
|
"prompt": payload.get("prompt") or (node.prompt if node else None),
|
|
"metadata": {
|
|
"source": "workflow_snapshot",
|
|
"workflow": workflow.to_dict(),
|
|
"nodes": [item.to_dict() for item in detail.nodes] if payload.get("include_nodes", True) else [],
|
|
**(payload.get("metadata") if isinstance(payload.get("metadata"), dict) else {}),
|
|
},
|
|
}
|
|
)
|
|
self.audit_service.record(
|
|
ctx=ctx,
|
|
action="save_workflow_snapshot",
|
|
target_type="workflow_history",
|
|
target_id=history.history_id,
|
|
after_snapshot=history.to_dict(),
|
|
)
|
|
return history
|
|
|
|
|
|
def new_history_id() -> str:
|
|
return f"hist_{uuid4().hex}"
|
|
|
|
|
|
def _optional_text(value: Any) -> str | None:
|
|
if value is None:
|
|
return None
|
|
text = str(value).strip()
|
|
return text or None
|