2026-07-05 09:39:17 +08:00
|
|
|
"""Workflow history route handlers."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
from backend.routes.common import handle_request
|
2026-07-27 11:50:08 +08:00
|
|
|
from backend.services.service_registry import create_workflow_service
|
2026-07-11 21:50:34 +08:00
|
|
|
from backend.routes.params import optional_str
|
2026-07-05 09:39:17 +08:00
|
|
|
from backend.services.history_service import HistoryService
|
|
|
|
|
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 core.security import require_authenticated, require_write
|
|
|
|
|
from database.db_helper import Database, load_database_settings
|
|
|
|
|
from database.repositories.image_repository import ImageRepository
|
|
|
|
|
from database.repositories.profile_repository import ProfileRepository
|
|
|
|
|
from database.repositories.workflow_repository import WorkflowRepository
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def adopt_node_image(
|
|
|
|
|
ctx: RequestContext,
|
|
|
|
|
node_id: str,
|
|
|
|
|
payload: dict[str, Any],
|
|
|
|
|
service: HistoryService | None = None,
|
|
|
|
|
) -> dict:
|
|
|
|
|
history_service = service or _create_history_service()
|
|
|
|
|
|
|
|
|
|
def action() -> dict[str, Any]:
|
|
|
|
|
require_write(ctx)
|
|
|
|
|
return history_service.adopt_node_image(node_id, payload, ctx).to_dict()
|
|
|
|
|
|
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def list_workflow_history(
|
|
|
|
|
ctx: RequestContext,
|
|
|
|
|
workflow_id: str,
|
|
|
|
|
query: dict[str, Any] | None = None,
|
|
|
|
|
service: HistoryService | None = None,
|
|
|
|
|
) -> dict:
|
|
|
|
|
history_service = service or _create_history_service()
|
|
|
|
|
params = query or {}
|
|
|
|
|
|
|
|
|
|
def action() -> list[dict[str, Any]]:
|
|
|
|
|
require_authenticated(ctx)
|
2026-07-11 21:50:34 +08:00
|
|
|
node_id = optional_str(params.get("node_id"))
|
2026-07-05 09:39:17 +08:00
|
|
|
return [entry.to_dict() for entry in history_service.list_workflow_history(workflow_id, node_id=node_id)]
|
|
|
|
|
|
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
|
|
|
|
|
2026-07-08 02:25:50 +08:00
|
|
|
def save_workflow_history(
|
|
|
|
|
ctx: RequestContext,
|
|
|
|
|
workflow_id: str,
|
|
|
|
|
payload: dict[str, Any],
|
|
|
|
|
service: HistoryService | None = None,
|
|
|
|
|
) -> dict:
|
|
|
|
|
history_service = service or _create_history_service()
|
|
|
|
|
|
|
|
|
|
def action() -> dict[str, Any]:
|
|
|
|
|
require_write(ctx)
|
|
|
|
|
return history_service.save_workflow_snapshot(workflow_id, payload, ctx).to_dict()
|
|
|
|
|
|
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
|
|
|
|
|
2026-07-05 09:39:17 +08:00
|
|
|
def _create_history_service() -> HistoryService:
|
|
|
|
|
database = Database(load_database_settings())
|
|
|
|
|
audit_service = AuditService(database)
|
|
|
|
|
workflow_repository = WorkflowRepository(database)
|
2026-07-27 11:50:08 +08:00
|
|
|
workflow_service = create_workflow_service(database)
|
2026-07-05 09:39:17 +08:00
|
|
|
image_service = ImageService(
|
|
|
|
|
image_repository=ImageRepository(database),
|
|
|
|
|
workflow_repository=workflow_repository,
|
|
|
|
|
audit_service=audit_service,
|
|
|
|
|
)
|
|
|
|
|
return HistoryService(
|
|
|
|
|
workflow_repository=workflow_repository,
|
|
|
|
|
workflow_service=workflow_service,
|
|
|
|
|
image_service=image_service,
|
|
|
|
|
audit_service=audit_service,
|
|
|
|
|
)
|