115 lines
4.6 KiB
Python
115 lines
4.6 KiB
Python
"""Persistent AI workflow definitions, versioned revisions, and local test fixtures."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from sqlalchemy import Boolean, Column, DateTime, Index, Integer, JSON, String, Text
|
|
from sqlalchemy.sql import func
|
|
|
|
from app.core.time import to_iso8601_utc
|
|
from app.db.session import Base
|
|
|
|
|
|
class AIWorkflow(Base):
|
|
"""Managed AI Advisor workflow graph (published + optional draft revision)."""
|
|
|
|
__tablename__ = "ai_workflows"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True, index=True)
|
|
workflow_id = Column(String(80), nullable=False, unique=True, index=True)
|
|
name = Column(String(160), nullable=False)
|
|
name_en = Column(String(160), nullable=False, default="")
|
|
category = Column(String(60), nullable=False, default="advisor", index=True)
|
|
description = Column(Text, nullable=False, default="")
|
|
description_en = Column(Text, nullable=False, default="")
|
|
is_default = Column(Boolean, nullable=False, default=False, index=True)
|
|
is_builtin = Column(Boolean, nullable=False, default=True)
|
|
active_revision = Column(Integer, nullable=False, default=1)
|
|
published_graph = Column(JSON, nullable=False, default=dict)
|
|
draft_graph = Column(JSON(none_as_null=True), nullable=True)
|
|
bindings = Column(JSON, nullable=False, default=dict)
|
|
updated_by = Column(String(120), nullable=True)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
|
|
updated_at = Column(
|
|
DateTime(timezone=True),
|
|
server_default=func.now(),
|
|
onupdate=func.now(),
|
|
nullable=False,
|
|
)
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"id": self.id,
|
|
"workflow_id": self.workflow_id,
|
|
"name": self.name,
|
|
"name_en": self.name_en or self.name,
|
|
"category": self.category,
|
|
"description": self.description or "",
|
|
"description_en": self.description_en or self.description or "",
|
|
"is_default": bool(self.is_default),
|
|
"is_builtin": bool(self.is_builtin),
|
|
"active_revision": int(self.active_revision or 1),
|
|
"has_draft": self.draft_graph is not None,
|
|
"published_graph": self.published_graph or {},
|
|
"draft_graph": self.draft_graph,
|
|
"bindings": self.bindings or {},
|
|
"updated_by": self.updated_by,
|
|
"created_at": to_iso8601_utc(self.created_at),
|
|
"updated_at": to_iso8601_utc(self.updated_at),
|
|
}
|
|
|
|
|
|
class AIWorkflowRevision(Base):
|
|
"""Immutable revision history for AI workflows enabling safe rollback."""
|
|
|
|
__tablename__ = "ai_workflow_revisions"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True, index=True)
|
|
workflow_id = Column(String(80), nullable=False, index=True)
|
|
revision = Column(Integer, nullable=False)
|
|
status = Column(String(24), nullable=False, default="published", index=True)
|
|
graph_json = Column(JSON, nullable=False, default=dict)
|
|
change_summary = Column(Text, nullable=False, default="")
|
|
created_by = Column(String(120), nullable=True)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
|
|
|
|
__table_args__ = (
|
|
Index("idx_ai_workflow_revisions_wf_rev", "workflow_id", "revision"),
|
|
)
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"id": self.id,
|
|
"workflow_id": self.workflow_id,
|
|
"revision": int(self.revision),
|
|
"status": self.status,
|
|
"graph_json": self.graph_json or {},
|
|
"change_summary": self.change_summary or "",
|
|
"created_by": self.created_by,
|
|
"created_at": to_iso8601_utc(self.created_at),
|
|
}
|
|
|
|
|
|
class AIWorkflowFixture(Base):
|
|
"""Explicitly saved anonymized regression test fixture for an AI workflow."""
|
|
|
|
__tablename__ = "ai_workflow_fixtures"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True, index=True)
|
|
workflow_id = Column(String(80), nullable=False, index=True)
|
|
name = Column(String(160), nullable=False)
|
|
sample_question = Column(Text, nullable=False, default="")
|
|
context_json = Column(JSON, nullable=False, default=dict)
|
|
created_by = Column(String(120), nullable=True)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"id": self.id,
|
|
"workflow_id": self.workflow_id,
|
|
"name": self.name,
|
|
"sample_question": self.sample_question or "",
|
|
"context_json": self.context_json or {},
|
|
"created_by": self.created_by,
|
|
"created_at": to_iso8601_utc(self.created_at),
|
|
}
|