代码提交
This commit is contained in:
1 parent
4ac7bc21cd
commit
17cf849361
110 files changed
+17007
-518
No files matched your search
+31
@@ -0,0 +1,31 @@
|
||||
"""Workflow definition / version persistence."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from deerflow.persistence.workflows.base import (
|
||||
WorkflowDraftConflictError,
|
||||
WorkflowNotFoundError,
|
||||
WorkflowStore,
|
||||
WorkflowVersionNotFoundError,
|
||||
)
|
||||
from deerflow.persistence.workflows.memory import MemoryWorkflowStore
|
||||
from deerflow.persistence.workflows.model import WorkflowDefinitionRow, WorkflowVersionRow
|
||||
from deerflow.persistence.workflows.sql import WorkflowRepository
|
||||
|
||||
__all__ = [
|
||||
"MemoryWorkflowStore",
|
||||
"WorkflowDefinitionRow",
|
||||
"WorkflowDraftConflictError",
|
||||
"WorkflowNotFoundError",
|
||||
"WorkflowRepository",
|
||||
"WorkflowStore",
|
||||
"WorkflowVersionNotFoundError",
|
||||
"WorkflowVersionRow",
|
||||
"make_workflow_store",
|
||||
]
|
||||
|
||||
|
||||
def make_workflow_store(session_factory):
|
||||
if session_factory is None:
|
||||
return MemoryWorkflowStore()
|
||||
return WorkflowRepository(session_factory)
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
"""Abstract workflow definition / version store."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any
|
||||
|
||||
|
||||
class WorkflowDraftConflictError(Exception):
|
||||
"""Raised when ``expected_revision`` does not match the stored draft revision."""
|
||||
|
||||
def __init__(self, workflow_id: str, current_revision: int) -> None:
|
||||
super().__init__(f"draft conflict for {workflow_id}: current={current_revision}")
|
||||
self.workflow_id = workflow_id
|
||||
self.current_revision = current_revision
|
||||
|
||||
|
||||
class WorkflowNotFoundError(Exception):
|
||||
def __init__(self, workflow_id: str) -> None:
|
||||
super().__init__(f"workflow not found: {workflow_id}")
|
||||
self.workflow_id = workflow_id
|
||||
|
||||
|
||||
class WorkflowVersionNotFoundError(Exception):
|
||||
def __init__(self, version_id: str) -> None:
|
||||
super().__init__(f"workflow version not found: {version_id}")
|
||||
self.version_id = version_id
|
||||
|
||||
|
||||
class WorkflowStore(ABC):
|
||||
@abstractmethod
|
||||
async def list_definitions(
|
||||
self,
|
||||
*,
|
||||
owner_id: str | None = None,
|
||||
status: str | None = None,
|
||||
include_archived: bool = False,
|
||||
) -> list[dict[str, Any]]:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def get_definition(self, workflow_id: str, *, include_draft: bool = True) -> dict[str, Any] | None:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def create_definition(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def update_definition(self, workflow_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def archive_definition(self, workflow_id: str) -> dict[str, Any] | None:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def save_draft(
|
||||
self,
|
||||
workflow_id: str,
|
||||
*,
|
||||
expected_revision: int,
|
||||
graph: dict[str, Any],
|
||||
updated_by: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""CAS update of ``draft_graph_json``. Raises ``WorkflowDraftConflictError``."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def save_studio_draft(
|
||||
self,
|
||||
workflow_id: str,
|
||||
*,
|
||||
expected_revision: int,
|
||||
graph: dict[str, Any],
|
||||
canvas_schema: str = "{}",
|
||||
updated_by: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Atomically CAS-update BOTH the execution graph and the canvas document.
|
||||
|
||||
A single conditional UPDATE bumps ``draft_revision`` and writes
|
||||
``draft_graph_json`` + ``draft_canvas_schema_json`` together, so the two
|
||||
representations can never diverge across revisions. Raises
|
||||
``WorkflowDraftConflictError`` on a stale ``expected_revision``.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def publish_version(
|
||||
self,
|
||||
workflow_id: str,
|
||||
*,
|
||||
graph: dict[str, Any],
|
||||
graph_hash: str,
|
||||
published_by: str | None = None,
|
||||
change_note: str = "",
|
||||
canvas_schema_hash: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def list_versions(self, workflow_id: str) -> list[dict[str, Any]]:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def get_version(self, version_id: str) -> dict[str, Any] | None:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def list_published_summaries(self, *, exclude_workflow_id: str | None = None) -> list[dict[str, Any]]:
|
||||
"""Lightweight list for subworkflow resource catalog."""
|
||||
raise NotImplementedError
|
||||
+216
@@ -0,0 +1,216 @@
|
||||
"""In-memory workflow store for tests / no-DB mode."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from deerflow.persistence.workflows.base import (
|
||||
WorkflowDraftConflictError,
|
||||
WorkflowNotFoundError,
|
||||
WorkflowStore,
|
||||
)
|
||||
|
||||
|
||||
def _now() -> str:
|
||||
return datetime.now(UTC).isoformat()
|
||||
|
||||
|
||||
class MemoryWorkflowStore(WorkflowStore):
|
||||
def __init__(self) -> None:
|
||||
self._definitions: dict[str, dict[str, Any]] = {}
|
||||
self._versions: dict[str, dict[str, Any]] = {}
|
||||
|
||||
async def list_definitions(
|
||||
self,
|
||||
*,
|
||||
owner_id: str | None = None,
|
||||
status: str | None = None,
|
||||
include_archived: bool = False,
|
||||
) -> list[dict[str, Any]]:
|
||||
rows = list(self._definitions.values())
|
||||
if owner_id:
|
||||
rows = [r for r in rows if r["owner_id"] == owner_id]
|
||||
if status:
|
||||
rows = [r for r in rows if r["status"] == status]
|
||||
elif not include_archived:
|
||||
rows = [r for r in rows if r["status"] != "archived"]
|
||||
rows.sort(key=lambda r: r.get("updated_at") or "", reverse=True)
|
||||
return [
|
||||
{k: v for k, v in r.items() if k not in ("draft_graph", "draft_canvas_schema", "next_version_number")}
|
||||
for r in rows
|
||||
]
|
||||
|
||||
async def get_definition(self, workflow_id: str, *, include_draft: bool = True) -> dict[str, Any] | None:
|
||||
row = self._definitions.get(workflow_id)
|
||||
if row is None:
|
||||
return None
|
||||
out = copy.deepcopy(row)
|
||||
if not include_draft:
|
||||
out.pop("draft_graph", None)
|
||||
return out
|
||||
|
||||
async def create_definition(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
workflow_id = data.get("id") or f"wf_{uuid.uuid4().hex[:12]}"
|
||||
graph = copy.deepcopy(data.get("draft_graph") or data.get("graph") or {
|
||||
"schemaVersion": "1.0",
|
||||
"id": workflow_id,
|
||||
"name": data.get("name") or "未命名工作流",
|
||||
"nodes": [{"id": "start_1", "type": "start", "name": "开始", "config": {}}],
|
||||
"edges": [],
|
||||
"inputSchema": {"type": "object", "properties": {}},
|
||||
"outputSchema": {"type": "object", "properties": {}},
|
||||
"settings": {},
|
||||
})
|
||||
graph.setdefault("id", workflow_id)
|
||||
now = _now()
|
||||
row = {
|
||||
"id": workflow_id,
|
||||
"name": data.get("name") or graph.get("name") or "未命名工作流",
|
||||
"description": data.get("description") or "",
|
||||
"owner_id": data["owner_id"],
|
||||
"status": data.get("status") or "draft",
|
||||
"draft_revision": 0,
|
||||
"next_version_number": 1,
|
||||
"draft_graph": graph,
|
||||
"draft_canvas_schema": str(data.get("draft_canvas_schema") or "{}"),
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
}
|
||||
self._definitions[workflow_id] = row
|
||||
return copy.deepcopy(row)
|
||||
|
||||
async def update_definition(self, workflow_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
|
||||
row = self._definitions.get(workflow_id)
|
||||
if row is None:
|
||||
return None
|
||||
if "name" in data and data["name"] is not None:
|
||||
row["name"] = data["name"]
|
||||
if "description" in data and data["description"] is not None:
|
||||
row["description"] = data["description"]
|
||||
if "status" in data and data["status"] is not None:
|
||||
row["status"] = data["status"]
|
||||
row["updated_at"] = _now()
|
||||
return copy.deepcopy(row)
|
||||
|
||||
async def archive_definition(self, workflow_id: str) -> dict[str, Any] | None:
|
||||
return await self.update_definition(workflow_id, {"status": "archived"})
|
||||
|
||||
async def save_draft(
|
||||
self,
|
||||
workflow_id: str,
|
||||
*,
|
||||
expected_revision: int,
|
||||
graph: dict[str, Any],
|
||||
updated_by: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
del updated_by
|
||||
row = self._definitions.get(workflow_id)
|
||||
if row is None:
|
||||
raise WorkflowNotFoundError(workflow_id)
|
||||
if row["draft_revision"] != expected_revision:
|
||||
raise WorkflowDraftConflictError(workflow_id, row["draft_revision"])
|
||||
payload = copy.deepcopy(graph)
|
||||
payload.setdefault("id", workflow_id)
|
||||
row["draft_graph"] = payload
|
||||
row["draft_revision"] = expected_revision + 1
|
||||
if payload.get("name"):
|
||||
row["name"] = str(payload["name"])[:255]
|
||||
row["updated_at"] = _now()
|
||||
return copy.deepcopy(row)
|
||||
|
||||
async def save_studio_draft(
|
||||
self,
|
||||
workflow_id: str,
|
||||
*,
|
||||
expected_revision: int,
|
||||
graph: dict[str, Any],
|
||||
canvas_schema: str = "{}",
|
||||
updated_by: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
del updated_by
|
||||
row = self._definitions.get(workflow_id)
|
||||
if row is None:
|
||||
raise WorkflowNotFoundError(workflow_id)
|
||||
if row["draft_revision"] != expected_revision:
|
||||
raise WorkflowDraftConflictError(workflow_id, row["draft_revision"])
|
||||
payload = copy.deepcopy(graph)
|
||||
payload.setdefault("id", workflow_id)
|
||||
row["draft_graph"] = payload
|
||||
row["draft_canvas_schema"] = canvas_schema or "{}"
|
||||
row["draft_revision"] = expected_revision + 1
|
||||
if payload.get("name"):
|
||||
row["name"] = str(payload["name"])[:255]
|
||||
row["updated_at"] = _now()
|
||||
return copy.deepcopy(row)
|
||||
|
||||
async def publish_version(
|
||||
self,
|
||||
workflow_id: str,
|
||||
*,
|
||||
graph: dict[str, Any],
|
||||
graph_hash: str,
|
||||
published_by: str | None = None,
|
||||
change_note: str = "",
|
||||
canvas_schema_hash: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
definition = self._definitions.get(workflow_id)
|
||||
if definition is None:
|
||||
raise WorkflowNotFoundError(workflow_id)
|
||||
existing = [v for v in self._versions.values() if v["workflow_id"] == workflow_id]
|
||||
# Allocate from the definition's own counter (mirrors the SQL store) so
|
||||
# the number never goes backwards even if events are purged/replayed.
|
||||
counter = int(definition.get("next_version_number") or 1)
|
||||
version_number = max(counter, max((v["version_number"] for v in existing), default=0) + 1)
|
||||
definition["next_version_number"] = version_number + 1
|
||||
version_id = f"wv_{uuid.uuid4().hex[:12]}"
|
||||
version = {
|
||||
"id": version_id,
|
||||
"workflow_id": workflow_id,
|
||||
"version_number": version_number,
|
||||
"graph": copy.deepcopy(graph),
|
||||
"graph_hash": graph_hash,
|
||||
"input_schema": graph.get("inputSchema") or graph.get("input_schema") or {},
|
||||
"output_schema": graph.get("outputSchema") or graph.get("output_schema") or {},
|
||||
"published_by": published_by,
|
||||
"published_at": _now(),
|
||||
"change_note": change_note or "",
|
||||
}
|
||||
self._versions[version_id] = version
|
||||
definition["status"] = "active"
|
||||
if canvas_schema_hash:
|
||||
definition["published_canvas_schema_hash"] = str(canvas_schema_hash)[:64]
|
||||
definition["updated_at"] = _now()
|
||||
return copy.deepcopy(version)
|
||||
|
||||
async def list_versions(self, workflow_id: str) -> list[dict[str, Any]]:
|
||||
rows = [v for v in self._versions.values() if v["workflow_id"] == workflow_id]
|
||||
rows.sort(key=lambda r: r["version_number"], reverse=True)
|
||||
return [{k: v for k, v in r.items() if k != "graph"} for r in rows]
|
||||
|
||||
async def get_version(self, version_id: str) -> dict[str, Any] | None:
|
||||
row = self._versions.get(version_id)
|
||||
return copy.deepcopy(row) if row is not None else None
|
||||
|
||||
async def list_published_summaries(self, *, exclude_workflow_id: str | None = None) -> list[dict[str, Any]]:
|
||||
by_wf: dict[str, dict[str, Any]] = {}
|
||||
for version in self._versions.values():
|
||||
wf_id = version["workflow_id"]
|
||||
if exclude_workflow_id and wf_id == exclude_workflow_id:
|
||||
continue
|
||||
definition = self._definitions.get(wf_id)
|
||||
if definition is None or definition.get("status") == "archived":
|
||||
continue
|
||||
current = by_wf.get(wf_id)
|
||||
if current is None or version["version_number"] > current["version_number"]:
|
||||
by_wf[wf_id] = {
|
||||
"workflow_id": wf_id,
|
||||
"name": definition["name"],
|
||||
"version_id": version["id"],
|
||||
"version_number": version["version_number"],
|
||||
"graph_hash": version["graph_hash"],
|
||||
"published_at": version["published_at"],
|
||||
}
|
||||
return sorted(by_wf.values(), key=lambda r: r.get("published_at") or "", reverse=True)
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
"""ORM models for workflow definitions and immutable published versions."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import ForeignKey, Integer, String, Text, UniqueConstraint
|
||||
from sqlalchemy import text as sa_text
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.types import BeijingDateTime, PortableJSON, PortableLongText
|
||||
|
||||
|
||||
class WorkflowDefinitionRow(Base):
|
||||
"""Mutable workflow metadata + current draft canvas."""
|
||||
|
||||
__tablename__ = "workflow_definitions"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
description: Mapped[str] = mapped_column(Text, default="", nullable=False)
|
||||
owner_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
# draft | active | archived
|
||||
status: Mapped[str] = mapped_column(String(32), default="draft", nullable=False, index=True)
|
||||
draft_revision: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
||||
# Monotonic version allocator. "1" = no version published yet; publish takes
|
||||
# the value then CAS-bumps it, replacing the old MAX(version_number)+1 race.
|
||||
next_version_number: Mapped[int] = mapped_column(
|
||||
Integer, nullable=False, default=1, server_default=sa_text("1")
|
||||
)
|
||||
draft_graph_json: Mapped[str] = mapped_column(PortableLongText(), default="{}", nullable=False)
|
||||
# Coze canvas document (opaque JSON text). Never interpreted by the runtime;
|
||||
# the executable WorkflowGraph lives in draft_graph_json above.
|
||||
draft_canvas_schema_json: Mapped[str] = mapped_column(
|
||||
PortableLongText(), default="{}", server_default=sa_text("'{}'"), nullable=False
|
||||
)
|
||||
# sha256 of the canvas document the current published version was compiled
|
||||
# from (audit trail only; publish always freezes the execution graph).
|
||||
published_canvas_schema_hash: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=lambda: datetime.now(UTC))
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
BeijingDateTime(),
|
||||
default=lambda: datetime.now(UTC),
|
||||
onupdate=lambda: datetime.now(UTC),
|
||||
)
|
||||
|
||||
|
||||
class WorkflowVersionRow(Base):
|
||||
"""Immutable published graph snapshot. Runs bind to version id."""
|
||||
|
||||
__tablename__ = "workflow_versions"
|
||||
__table_args__ = (UniqueConstraint("workflow_id", "version_number", name="uq_workflow_versions_wf_num"),)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
workflow_id: Mapped[str] = mapped_column(
|
||||
String(64),
|
||||
ForeignKey("workflow_definitions.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
version_number: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
graph_json: Mapped[str] = mapped_column(PortableLongText(), nullable=False)
|
||||
graph_hash: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
input_schema: Mapped[dict] = mapped_column(PortableJSON(), default=dict)
|
||||
output_schema: Mapped[dict] = mapped_column(PortableJSON(), default=dict)
|
||||
published_by: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
||||
published_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=lambda: datetime.now(UTC))
|
||||
change_note: Mapped[str] = mapped_column(Text, default="", nullable=False)
|
||||
+385
@@ -0,0 +1,385 @@
|
||||
"""SQLAlchemy-backed workflow definition / version store."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import func, select, update
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.workflows.base import (
|
||||
WorkflowDraftConflictError,
|
||||
WorkflowNotFoundError,
|
||||
WorkflowStore,
|
||||
)
|
||||
from deerflow.persistence.workflows.model import WorkflowDefinitionRow, WorkflowVersionRow
|
||||
|
||||
# Concurrent publishers CAS-retry against the workflow's own counter; a handful
|
||||
# of attempts is far more than any realistic contention window needs.
|
||||
_PUBLISH_ATTEMPTS = 6
|
||||
|
||||
|
||||
def _iso(value: datetime | None) -> str | None:
|
||||
return value.isoformat() if isinstance(value, datetime) else None
|
||||
|
||||
|
||||
def _parse_graph(raw: str | dict[str, Any] | None) -> dict[str, Any]:
|
||||
if raw is None:
|
||||
return {}
|
||||
if isinstance(raw, dict):
|
||||
return raw
|
||||
text = str(raw).strip()
|
||||
if not text:
|
||||
return {}
|
||||
try:
|
||||
data = json.loads(text)
|
||||
except json.JSONDecodeError:
|
||||
return {}
|
||||
return data if isinstance(data, dict) else {}
|
||||
|
||||
|
||||
def _dump_graph(graph: dict[str, Any]) -> str:
|
||||
return json.dumps(graph, ensure_ascii=False, separators=(",", ":"), sort_keys=True)
|
||||
|
||||
|
||||
def _definition_dict(row: WorkflowDefinitionRow, *, include_draft: bool) -> dict[str, Any]:
|
||||
data = {
|
||||
"id": row.id,
|
||||
"name": row.name,
|
||||
"description": row.description or "",
|
||||
"owner_id": row.owner_id,
|
||||
"status": row.status,
|
||||
"draft_revision": row.draft_revision,
|
||||
"created_at": _iso(row.created_at),
|
||||
"updated_at": _iso(row.updated_at),
|
||||
}
|
||||
if include_draft:
|
||||
data["draft_graph"] = _parse_graph(row.draft_graph_json)
|
||||
# Canvas document as stored (opaque JSON text; "{}" when never saved).
|
||||
data["draft_canvas_schema"] = row.draft_canvas_schema_json or "{}"
|
||||
return data
|
||||
|
||||
|
||||
def _version_dict(row: WorkflowVersionRow, *, include_graph: bool = True) -> dict[str, Any]:
|
||||
data = {
|
||||
"id": row.id,
|
||||
"workflow_id": row.workflow_id,
|
||||
"version_number": row.version_number,
|
||||
"graph_hash": row.graph_hash,
|
||||
"input_schema": row.input_schema or {},
|
||||
"output_schema": row.output_schema or {},
|
||||
"published_by": row.published_by,
|
||||
"published_at": _iso(row.published_at),
|
||||
"change_note": row.change_note or "",
|
||||
}
|
||||
if include_graph:
|
||||
data["graph"] = _parse_graph(row.graph_json)
|
||||
return data
|
||||
|
||||
|
||||
class WorkflowRepository(WorkflowStore):
|
||||
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
|
||||
self._sf = session_factory
|
||||
|
||||
async def list_definitions(
|
||||
self,
|
||||
*,
|
||||
owner_id: str | None = None,
|
||||
status: str | None = None,
|
||||
include_archived: bool = False,
|
||||
) -> list[dict[str, Any]]:
|
||||
stmt = select(WorkflowDefinitionRow)
|
||||
if owner_id:
|
||||
stmt = stmt.where(WorkflowDefinitionRow.owner_id == owner_id)
|
||||
if status:
|
||||
stmt = stmt.where(WorkflowDefinitionRow.status == status)
|
||||
elif not include_archived:
|
||||
stmt = stmt.where(WorkflowDefinitionRow.status != "archived")
|
||||
stmt = stmt.order_by(WorkflowDefinitionRow.updated_at.desc())
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(stmt)
|
||||
return [_definition_dict(row, include_draft=False) for row in result.scalars()]
|
||||
|
||||
async def get_definition(self, workflow_id: str, *, include_draft: bool = True) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(WorkflowDefinitionRow, workflow_id)
|
||||
return _definition_dict(row, include_draft=include_draft) if row is not None else None
|
||||
|
||||
async def create_definition(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
workflow_id = data.get("id") or f"wf_{uuid.uuid4().hex[:12]}"
|
||||
graph = data.get("draft_graph") or data.get("graph") or {
|
||||
"schemaVersion": "1.0",
|
||||
"id": workflow_id,
|
||||
"name": data.get("name") or "未命名工作流",
|
||||
"nodes": [{"id": "start_1", "type": "start", "name": "开始", "config": {}}],
|
||||
"edges": [],
|
||||
"inputSchema": {"type": "object", "properties": {}},
|
||||
"outputSchema": {"type": "object", "properties": {}},
|
||||
"settings": {},
|
||||
}
|
||||
if isinstance(graph, dict) and not graph.get("id"):
|
||||
graph = {**graph, "id": workflow_id}
|
||||
if isinstance(graph, dict) and data.get("name") and not graph.get("name"):
|
||||
graph = {**graph, "name": data["name"]}
|
||||
row = WorkflowDefinitionRow(
|
||||
id=workflow_id,
|
||||
name=data.get("name") or graph.get("name") or "未命名工作流",
|
||||
description=data.get("description") or "",
|
||||
owner_id=data["owner_id"],
|
||||
status=data.get("status") or "draft",
|
||||
draft_revision=0,
|
||||
draft_graph_json=_dump_graph(graph),
|
||||
draft_canvas_schema_json=str(data.get("draft_canvas_schema") or "{}"),
|
||||
)
|
||||
async with self._sf() as session:
|
||||
session.add(row)
|
||||
await session.flush()
|
||||
out = _definition_dict(row, include_draft=True)
|
||||
await session.commit()
|
||||
return out
|
||||
|
||||
async def update_definition(self, workflow_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(WorkflowDefinitionRow, workflow_id)
|
||||
if row is None:
|
||||
return None
|
||||
if "name" in data and data["name"] is not None:
|
||||
row.name = data["name"]
|
||||
if "description" in data and data["description"] is not None:
|
||||
row.description = data["description"]
|
||||
if "status" in data and data["status"] is not None:
|
||||
row.status = data["status"]
|
||||
row.updated_at = datetime.now(UTC)
|
||||
await session.flush()
|
||||
out = _definition_dict(row, include_draft=True)
|
||||
await session.commit()
|
||||
return out
|
||||
|
||||
async def archive_definition(self, workflow_id: str) -> dict[str, Any] | None:
|
||||
return await self.update_definition(workflow_id, {"status": "archived"})
|
||||
|
||||
async def save_draft(
|
||||
self,
|
||||
workflow_id: str,
|
||||
*,
|
||||
expected_revision: int,
|
||||
graph: dict[str, Any],
|
||||
updated_by: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
del updated_by # audit reserved for later
|
||||
async with self._sf() as session:
|
||||
row = await session.get(WorkflowDefinitionRow, workflow_id)
|
||||
if row is None:
|
||||
raise WorkflowNotFoundError(workflow_id)
|
||||
if row.draft_revision != expected_revision:
|
||||
raise WorkflowDraftConflictError(workflow_id, row.draft_revision)
|
||||
payload = dict(graph)
|
||||
payload.setdefault("id", workflow_id)
|
||||
if row.name and not payload.get("name"):
|
||||
payload["name"] = row.name
|
||||
# Conditional UPDATE on the revision itself: two sessions that both
|
||||
# read the same revision cannot both win — the loser's WHERE clause
|
||||
# matches zero rows and surfaces a deterministic conflict instead of
|
||||
# silently overwriting the other editor's draft.
|
||||
values: dict[str, Any] = {
|
||||
"draft_graph_json": _dump_graph(payload),
|
||||
"draft_revision": WorkflowDefinitionRow.draft_revision + 1,
|
||||
"updated_at": datetime.now(UTC),
|
||||
}
|
||||
if payload.get("name"):
|
||||
values["name"] = str(payload["name"])[:255]
|
||||
result = await session.execute(
|
||||
update(WorkflowDefinitionRow)
|
||||
.where(
|
||||
WorkflowDefinitionRow.id == workflow_id,
|
||||
WorkflowDefinitionRow.draft_revision == expected_revision,
|
||||
)
|
||||
.values(**values)
|
||||
)
|
||||
if result.rowcount == 0:
|
||||
await session.rollback()
|
||||
current = await session.get(WorkflowDefinitionRow, workflow_id)
|
||||
if current is None:
|
||||
raise WorkflowNotFoundError(workflow_id)
|
||||
raise WorkflowDraftConflictError(workflow_id, current.draft_revision)
|
||||
await session.commit()
|
||||
fresh = await session.get(WorkflowDefinitionRow, workflow_id)
|
||||
assert fresh is not None
|
||||
return _definition_dict(fresh, include_draft=True)
|
||||
|
||||
async def save_studio_draft(
|
||||
self,
|
||||
workflow_id: str,
|
||||
*,
|
||||
expected_revision: int,
|
||||
graph: dict[str, Any],
|
||||
canvas_schema: str = "{}",
|
||||
updated_by: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
del updated_by # audit reserved for later
|
||||
async with self._sf() as session:
|
||||
row = await session.get(WorkflowDefinitionRow, workflow_id)
|
||||
if row is None:
|
||||
raise WorkflowNotFoundError(workflow_id)
|
||||
if row.draft_revision != expected_revision:
|
||||
raise WorkflowDraftConflictError(workflow_id, row.draft_revision)
|
||||
payload = dict(graph)
|
||||
payload.setdefault("id", workflow_id)
|
||||
if row.name and not payload.get("name"):
|
||||
payload["name"] = row.name
|
||||
# One conditional UPDATE writes graph + canvas + revision together:
|
||||
# the two representations are always from the same save, and a
|
||||
# concurrent editor loses deterministically instead of interleaving
|
||||
# a new canvas onto someone else's graph.
|
||||
values: dict[str, Any] = {
|
||||
"draft_graph_json": _dump_graph(payload),
|
||||
"draft_canvas_schema_json": canvas_schema or "{}",
|
||||
"draft_revision": WorkflowDefinitionRow.draft_revision + 1,
|
||||
"updated_at": datetime.now(UTC),
|
||||
}
|
||||
if payload.get("name"):
|
||||
values["name"] = str(payload["name"])[:255]
|
||||
result = await session.execute(
|
||||
update(WorkflowDefinitionRow)
|
||||
.where(
|
||||
WorkflowDefinitionRow.id == workflow_id,
|
||||
WorkflowDefinitionRow.draft_revision == expected_revision,
|
||||
)
|
||||
.values(**values)
|
||||
)
|
||||
if result.rowcount == 0:
|
||||
await session.rollback()
|
||||
current = await session.get(WorkflowDefinitionRow, workflow_id)
|
||||
if current is None:
|
||||
raise WorkflowNotFoundError(workflow_id)
|
||||
raise WorkflowDraftConflictError(workflow_id, current.draft_revision)
|
||||
await session.commit()
|
||||
fresh = await session.get(WorkflowDefinitionRow, workflow_id)
|
||||
assert fresh is not None
|
||||
return _definition_dict(fresh, include_draft=True)
|
||||
|
||||
async def publish_version(
|
||||
self,
|
||||
workflow_id: str,
|
||||
*,
|
||||
graph: dict[str, Any],
|
||||
graph_hash: str,
|
||||
published_by: str | None = None,
|
||||
change_note: str = "",
|
||||
canvas_schema_hash: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Allocate the version number from the workflow's own counter.
|
||||
|
||||
``MAX(version_number) + 1`` races between concurrent publishes; here the
|
||||
number is claimed by CAS-bumping ``next_version_number`` inside the same
|
||||
transaction that inserts the version row, and the unique
|
||||
``(workflow_id, version_number)`` constraint is the final safety net.
|
||||
Concurrent publishers get adjacent, distinct numbers.
|
||||
"""
|
||||
last_error: Exception | None = None
|
||||
for _ in range(_PUBLISH_ATTEMPTS):
|
||||
try:
|
||||
async with self._sf() as session:
|
||||
definition = await session.get(WorkflowDefinitionRow, workflow_id)
|
||||
if definition is None:
|
||||
raise WorkflowNotFoundError(workflow_id)
|
||||
counter_seen = int(definition.next_version_number or 1)
|
||||
# Legacy rows got the column auto-added with server_default 1
|
||||
# while versions already exist — never allocate below the max.
|
||||
latest = (
|
||||
await session.execute(
|
||||
select(func.max(WorkflowVersionRow.version_number)).where(
|
||||
WorkflowVersionRow.workflow_id == workflow_id
|
||||
)
|
||||
)
|
||||
).scalar() or 0
|
||||
number = max(counter_seen, int(latest) + 1)
|
||||
claimed = await session.execute(
|
||||
update(WorkflowDefinitionRow)
|
||||
.where(
|
||||
WorkflowDefinitionRow.id == workflow_id,
|
||||
WorkflowDefinitionRow.next_version_number == counter_seen,
|
||||
)
|
||||
.values(next_version_number=number + 1)
|
||||
)
|
||||
if claimed.rowcount == 0:
|
||||
# Another publish landed first — release nothing (the
|
||||
# counter bump never happened) and retry with fresh state.
|
||||
await session.rollback()
|
||||
last_error = None
|
||||
continue
|
||||
version = WorkflowVersionRow(
|
||||
id=f"wv_{uuid.uuid4().hex[:12]}",
|
||||
workflow_id=workflow_id,
|
||||
version_number=number,
|
||||
graph_json=_dump_graph(graph),
|
||||
graph_hash=graph_hash,
|
||||
input_schema=graph.get("inputSchema") or graph.get("input_schema") or {},
|
||||
output_schema=graph.get("outputSchema") or graph.get("output_schema") or {},
|
||||
published_by=published_by,
|
||||
change_note=change_note or "",
|
||||
)
|
||||
definition.status = "active"
|
||||
definition.updated_at = datetime.now(UTC)
|
||||
if canvas_schema_hash:
|
||||
definition.published_canvas_schema_hash = canvas_schema_hash[:64]
|
||||
session.add(version)
|
||||
await session.flush()
|
||||
out = _version_dict(version, include_graph=True)
|
||||
await session.commit()
|
||||
return out
|
||||
except IntegrityError as exc: # pragma: no cover - CAS makes this near-impossible
|
||||
last_error = exc
|
||||
continue
|
||||
if last_error is not None:
|
||||
raise last_error
|
||||
raise RuntimeError(f"failed to allocate a workflow version number for {workflow_id}")
|
||||
|
||||
async def list_versions(self, workflow_id: str) -> list[dict[str, Any]]:
|
||||
stmt = (
|
||||
select(WorkflowVersionRow)
|
||||
.where(WorkflowVersionRow.workflow_id == workflow_id)
|
||||
.order_by(WorkflowVersionRow.version_number.desc())
|
||||
)
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(stmt)
|
||||
return [_version_dict(row, include_graph=False) for row in result.scalars()]
|
||||
|
||||
async def get_version(self, version_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(WorkflowVersionRow, version_id)
|
||||
return _version_dict(row, include_graph=True) if row is not None else None
|
||||
|
||||
async def list_published_summaries(self, *, exclude_workflow_id: str | None = None) -> list[dict[str, Any]]:
|
||||
stmt = (
|
||||
select(WorkflowVersionRow, WorkflowDefinitionRow)
|
||||
.join(WorkflowDefinitionRow, WorkflowDefinitionRow.id == WorkflowVersionRow.workflow_id)
|
||||
.where(WorkflowDefinitionRow.status != "archived")
|
||||
.order_by(WorkflowVersionRow.published_at.desc())
|
||||
)
|
||||
if exclude_workflow_id:
|
||||
stmt = stmt.where(WorkflowVersionRow.workflow_id != exclude_workflow_id)
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(stmt)
|
||||
out: list[dict[str, Any]] = []
|
||||
seen: set[str] = set()
|
||||
for version, definition in result.all():
|
||||
# Latest version per workflow only.
|
||||
if version.workflow_id in seen:
|
||||
continue
|
||||
seen.add(version.workflow_id)
|
||||
out.append(
|
||||
{
|
||||
"workflow_id": definition.id,
|
||||
"name": definition.name,
|
||||
"version_id": version.id,
|
||||
"version_number": version.version_number,
|
||||
"graph_hash": version.graph_hash,
|
||||
"published_at": _iso(version.published_at),
|
||||
}
|
||||
)
|
||||
return out
|
||||
Reference in new issue
Block a user