145 行
4.2 KiB
Python
145 行
4.2 KiB
Python
"""Workflow route handlers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from backend.routes.common import handle_request
|
|
from backend.services.service_registry import create_workflow_service
|
|
from backend.services.character_workflow_service import CharacterWorkflowService
|
|
from backend.services.event_stream_service import get_default_event_bus
|
|
from backend.services.queue_service import QueueService
|
|
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.job_repository import JobRepository
|
|
from database.repositories.profile_repository import ProfileRepository
|
|
from database.repositories.workflow_repository import WorkflowRepository
|
|
|
|
|
|
def list_profile_workflows(
|
|
ctx: RequestContext,
|
|
profile_id: str,
|
|
service: WorkflowService | None = None,
|
|
) -> dict:
|
|
workflow_service = service or _create_workflow_service()
|
|
|
|
def action() -> list[dict[str, Any]]:
|
|
require_authenticated(ctx)
|
|
return [workflow.to_dict() for workflow in workflow_service.list_workflows(profile_id)]
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def create_profile_workflow(
|
|
ctx: RequestContext,
|
|
profile_id: str,
|
|
payload: dict[str, Any],
|
|
service: WorkflowService | None = None,
|
|
) -> dict:
|
|
workflow_service = service or _create_workflow_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_write(ctx)
|
|
return workflow_service.create_workflow(profile_id, payload, ctx).to_dict()
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def get_workflow(
|
|
ctx: RequestContext,
|
|
workflow_id: str,
|
|
service: WorkflowService | None = None,
|
|
) -> dict:
|
|
workflow_service = service or _create_workflow_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_authenticated(ctx)
|
|
return workflow_service.get_workflow(workflow_id).to_dict()
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def update_workflow(
|
|
ctx: RequestContext,
|
|
workflow_id: str,
|
|
payload: dict[str, Any],
|
|
service: WorkflowService | None = None,
|
|
) -> dict:
|
|
workflow_service = service or _create_workflow_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_write(ctx)
|
|
return workflow_service.update_workflow(workflow_id, payload, ctx).to_dict()
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def start_workflow(
|
|
ctx: RequestContext,
|
|
workflow_id: str,
|
|
service: WorkflowService | None = None,
|
|
) -> dict:
|
|
return _set_workflow_status(ctx, workflow_id, "running", service)
|
|
|
|
|
|
def cancel_workflow(
|
|
ctx: RequestContext,
|
|
workflow_id: str,
|
|
service: WorkflowService | None = None,
|
|
) -> dict:
|
|
workflow_service = service or _create_workflow_service()
|
|
queue_service = QueueService(
|
|
JobRepository(workflow_service.workflow_repository.database),
|
|
workflow_service.audit_service,
|
|
workflow_service.event_bus,
|
|
)
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_write(ctx)
|
|
return workflow_service.cancel_workflow(workflow_id, ctx, queue_service).to_dict()
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def archive_workflow(
|
|
ctx: RequestContext,
|
|
workflow_id: str,
|
|
service: WorkflowService | None = None,
|
|
) -> dict:
|
|
return _set_workflow_status(ctx, workflow_id, "archived", service)
|
|
|
|
|
|
def _set_workflow_status(
|
|
ctx: RequestContext,
|
|
workflow_id: str,
|
|
status: str,
|
|
service: WorkflowService | None,
|
|
) -> dict:
|
|
workflow_service = service or _create_workflow_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_write(ctx)
|
|
return workflow_service.update_workflow(workflow_id, {"status": status}, ctx).to_dict()
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def execute_character_workflow(
|
|
ctx: RequestContext,
|
|
workflow_id: str,
|
|
service: CharacterWorkflowService | None = None,
|
|
) -> dict:
|
|
character_service = service or CharacterWorkflowService(_create_workflow_service())
|
|
|
|
def action() -> dict:
|
|
require_write(ctx)
|
|
return character_service.execute_workflow(workflow_id, ctx)
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def _create_workflow_service() -> WorkflowService:
|
|
return create_workflow_service()
|