mirror of
https://github.com/langgenius/dify.git
synced 2026-11-30 00:28:01 +08:00
150 lines
6.1 KiB
Python
150 lines
6.1 KiB
Python
"""One draft-write use case for the editor and atomic DSL imports."""
|
|
|
|
from collections.abc import Callable
|
|
from contextlib import AbstractContextManager, nullcontext
|
|
from typing import Protocol
|
|
|
|
from machinery.context import RequestContext
|
|
from services.workflow.contracts import (
|
|
DraftBindingTransaction,
|
|
DraftImportResult,
|
|
DraftSyncCommand,
|
|
PreparedDraftSync,
|
|
PreparedEnvironmentVariables,
|
|
WorkflowAgentServices,
|
|
WorkflowOwner,
|
|
WorkflowSnapshot,
|
|
)
|
|
from services.workflow.snippet_policy import validate_snippet_graph_forbidden_nodes
|
|
|
|
|
|
class DraftDefinitions[BindingStore](Protocol):
|
|
def draft_sync(
|
|
self, context: RequestContext, owner: WorkflowOwner, command: DraftSyncCommand, prepared: PreparedDraftSync
|
|
) -> AbstractContextManager[DraftBindingTransaction[BindingStore]]: ...
|
|
|
|
def draft_restore(
|
|
self, context: RequestContext, owner: WorkflowOwner, source: WorkflowSnapshot
|
|
) -> AbstractContextManager[DraftBindingTransaction[BindingStore]]: ...
|
|
|
|
|
|
class DraftLifecycle(Protocol):
|
|
def prepare_sync(
|
|
self,
|
|
context: RequestContext,
|
|
owner: WorkflowOwner,
|
|
command: DraftSyncCommand,
|
|
environment: PreparedEnvironmentVariables | None,
|
|
) -> PreparedDraftSync: ...
|
|
def validate_restore(self, context: RequestContext, owner: WorkflowOwner, workflow_id: str) -> WorkflowSnapshot: ...
|
|
def draft_synced(self, context: RequestContext, owner: WorkflowOwner, workflow: WorkflowSnapshot) -> None: ...
|
|
|
|
|
|
class DraftAgentRetirement(Protocol):
|
|
def retire_unowned(self, *, tenant_id: str, agent_ids: list[str], account_id: str) -> None: ...
|
|
|
|
|
|
class DraftCreationLock(Protocol):
|
|
def __call__(self, *, tenant_id: str, app_id: str) -> AbstractContextManager[None]: ...
|
|
|
|
|
|
class WorkflowDraftService[BindingStore]:
|
|
def __init__(
|
|
self,
|
|
*,
|
|
definitions: DraftDefinitions[BindingStore],
|
|
creation_lock: DraftCreationLock,
|
|
lifecycle: DraftLifecycle,
|
|
retirement: DraftAgentRetirement,
|
|
agent_services: WorkflowAgentServices[BindingStore],
|
|
):
|
|
self._definitions = definitions
|
|
self._creation_lock = creation_lock
|
|
self._lifecycle = lifecycle
|
|
self._retirement = retirement
|
|
self._agent_services = agent_services
|
|
|
|
def sync(
|
|
self,
|
|
context: RequestContext,
|
|
owner: WorkflowOwner,
|
|
command: DraftSyncCommand,
|
|
*,
|
|
materialize: Callable[[WorkflowSnapshot], DraftImportResult] | None = None,
|
|
environment: PreparedEnvironmentVariables | None = None,
|
|
) -> WorkflowSnapshot:
|
|
"""Persist a prepared draft; reject forbidden Snippet nodes before committing.
|
|
|
|
For imported Snippets, validate the materialized graph that replaces the
|
|
command graph. A rejected graph leaves the draft transaction uncommitted.
|
|
"""
|
|
if owner.kind == "snippet" and materialize is None:
|
|
validate_snippet_graph_forbidden_nodes(command.graph)
|
|
prepared = self._lifecycle.prepare_sync(context, owner, command, environment)
|
|
initializing = (
|
|
owner.kind == "app"
|
|
and prepared.source is None
|
|
and command.check_hash
|
|
and command.unique_hash is None
|
|
and materialize is None
|
|
)
|
|
with (
|
|
self._creation_lock(tenant_id=context.active_workspace_id, app_id=owner.id)
|
|
if initializing
|
|
else nullcontext(),
|
|
self._definitions.draft_sync(context, owner, command, prepared) as transaction,
|
|
):
|
|
agents = self._agent_services(repository=transaction.bindings)
|
|
if owner.kind == "pipeline":
|
|
retired: set[str] = set()
|
|
elif materialize is None:
|
|
retired, graph = agents.synchronize_draft(
|
|
draft_workflow=transaction.workflow, account_id=context.account_id
|
|
)
|
|
transaction.replace_graph(graph)
|
|
agents.validate_agent_nodes_for_draft_sync(draft_workflow=transaction.workflow)
|
|
else:
|
|
imported = materialize(transaction.workflow)
|
|
if owner.kind == "snippet":
|
|
validate_snippet_graph_forbidden_nodes(imported.graph)
|
|
transaction.replace_graph(imported.graph)
|
|
retired = imported.retired_agents
|
|
agents.validate_agent_nodes_for_draft_sync(draft_workflow=transaction.workflow)
|
|
workflow = transaction.workflow
|
|
|
|
def completed() -> None:
|
|
self._retirement.retire_unowned(
|
|
tenant_id=context.active_workspace_id, agent_ids=sorted(retired), account_id=context.account_id
|
|
)
|
|
self._lifecycle.draft_synced(context, owner, workflow)
|
|
|
|
transaction.after_commit(completed)
|
|
return workflow
|
|
|
|
def restore(self, context: RequestContext, owner: WorkflowOwner, workflow_id: str) -> WorkflowSnapshot:
|
|
"""Restore a published draft only if its Snippet graph is allowed.
|
|
|
|
A rejected source leaves the draft and its Agent bindings unchanged.
|
|
"""
|
|
source = self._lifecycle.validate_restore(context, owner, workflow_id)
|
|
if owner.kind == "snippet":
|
|
validate_snippet_graph_forbidden_nodes(source.graph_dict)
|
|
with self._definitions.draft_restore(context, owner, source) as transaction:
|
|
retired = (
|
|
self._agent_services(repository=transaction.bindings).restore_agent_node_bindings_to_draft(
|
|
source_workflow=source, draft_workflow=transaction.workflow, account_id=context.account_id
|
|
)
|
|
if owner.kind != "pipeline"
|
|
else set()
|
|
)
|
|
workflow = transaction.workflow
|
|
|
|
def completed() -> None:
|
|
self._retirement.retire_unowned(
|
|
tenant_id=context.active_workspace_id, agent_ids=sorted(retired), account_id=context.account_id
|
|
)
|
|
self._lifecycle.draft_synced(context, owner, workflow)
|
|
|
|
transaction.after_commit(completed)
|
|
return workflow
|