ceshi
This commit is contained in:
1 parent
1ffead7ee2
commit
5f11926db5
14 files changed
+2031
-94
No files matched your search
+2
@@ -45,6 +45,7 @@ from deerflow.persistence.llm_metrics.model import LlmCallMetricRow
|
||||
from deerflow.persistence.memory_config.model import UserMemoryConfigRow
|
||||
from deerflow.persistence.menu_overrides.model import MenuOverrideRow
|
||||
from deerflow.persistence.models.run_event import RunEventRow
|
||||
from deerflow.persistence.notification_templates.model import NotificationTemplateRow
|
||||
from deerflow.persistence.notifications.model import NotificationRow
|
||||
from deerflow.persistence.parallel_agent_tasks.model import ParallelAgentTaskRow
|
||||
from deerflow.persistence.position_roles.model import PositionRoleRow
|
||||
@@ -110,6 +111,7 @@ __all__ = [
|
||||
"LlmCallMetricRow",
|
||||
"MenuOverrideRow",
|
||||
"NotificationRow",
|
||||
"NotificationTemplateRow",
|
||||
"ParallelAgentTaskRow",
|
||||
"PositionRoundtableNodeRow",
|
||||
"PositionRoundtableSessionRow",
|
||||
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
"""Notification template persistence (通知模板管理)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from deerflow.persistence.notification_templates.base import (
|
||||
DuplicateNotificationTemplateNameError,
|
||||
NotificationTemplateStore,
|
||||
VersionConflictError,
|
||||
)
|
||||
from deerflow.persistence.notification_templates.memory import MemoryNotificationTemplateStore
|
||||
from deerflow.persistence.notification_templates.model import NotificationTemplateRow
|
||||
from deerflow.persistence.notification_templates.sql import NotificationTemplateRepository
|
||||
from deerflow.persistence.notification_templates.variables import (
|
||||
ALLOWED_VARIABLES,
|
||||
TemplateVariableError,
|
||||
preview_templates,
|
||||
validate_templates,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"ALLOWED_VARIABLES",
|
||||
"DuplicateNotificationTemplateNameError",
|
||||
"MemoryNotificationTemplateStore",
|
||||
"NotificationTemplateRepository",
|
||||
"NotificationTemplateRow",
|
||||
"NotificationTemplateStore",
|
||||
"TemplateVariableError",
|
||||
"VersionConflictError",
|
||||
"make_notification_template_store",
|
||||
"preview_templates",
|
||||
"validate_templates",
|
||||
]
|
||||
|
||||
|
||||
def make_notification_template_store(session_factory):
|
||||
if session_factory is None:
|
||||
return MemoryNotificationTemplateStore()
|
||||
return NotificationTemplateRepository(session_factory)
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
"""Abstract notification-template persistence API.
|
||||
|
||||
Global / shared store: no ``user_id`` scoping. The router gates every endpoint
|
||||
behind admin. ``created_by`` is audit-only.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any
|
||||
|
||||
|
||||
class DuplicateNotificationTemplateNameError(ValueError):
|
||||
"""Raised when creating/updating would collide on the unique name."""
|
||||
|
||||
|
||||
class VersionConflictError(Exception):
|
||||
"""Raised when ``expected_version`` does not match the stored version."""
|
||||
|
||||
def __init__(self, server: dict[str, Any]) -> None:
|
||||
super().__init__("version conflict")
|
||||
self.server = server
|
||||
|
||||
|
||||
class NotificationTemplateStore(ABC):
|
||||
@abstractmethod
|
||||
async def list_templates(
|
||||
self,
|
||||
*,
|
||||
keyword: str | None = None,
|
||||
enabled: bool | None = None,
|
||||
tag: str | None = None,
|
||||
page: int = 1,
|
||||
page_size: int = 20,
|
||||
) -> tuple[list[dict[str, Any]], int]:
|
||||
"""Return ``(items, total)`` newest-first with optional filters."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def get_template(self, template_id: str) -> dict[str, Any] | None:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def create_template(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Create a template. Raises ``DuplicateNotificationTemplateNameError``."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def update_template(
|
||||
self,
|
||||
template_id: str,
|
||||
data: dict[str, Any],
|
||||
*,
|
||||
expected_version: int,
|
||||
) -> dict[str, Any] | None:
|
||||
"""Update with optimistic lock. Returns None if missing.
|
||||
|
||||
Raises ``VersionConflictError`` (with ``.server``) on mismatch, or
|
||||
``DuplicateNotificationTemplateNameError`` on a name clash.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def delete_template(self, template_id: str) -> bool:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def name_exists(self, name: str, *, exclude_id: str | None = None) -> bool:
|
||||
raise NotImplementedError
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
"""In-memory notification-template store (tests / no-DB)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from deerflow.persistence.notification_templates.base import (
|
||||
DuplicateNotificationTemplateNameError,
|
||||
NotificationTemplateStore,
|
||||
VersionConflictError,
|
||||
)
|
||||
|
||||
_WRITABLE = {
|
||||
"name",
|
||||
"title_template",
|
||||
"content_template",
|
||||
"tags",
|
||||
"enabled",
|
||||
}
|
||||
|
||||
|
||||
class MemoryNotificationTemplateStore(NotificationTemplateStore):
|
||||
def __init__(self) -> None:
|
||||
self._rows: dict[str, dict[str, Any]] = {}
|
||||
|
||||
@staticmethod
|
||||
def _public(row: dict[str, Any]) -> dict[str, Any]:
|
||||
out = dict(row)
|
||||
out["tags"] = list(out.get("tags") or [])
|
||||
return out
|
||||
|
||||
def _name_taken(self, name: str, *, exclude_id: str | None = None) -> bool:
|
||||
return any(r["name"] == name and r["id"] != exclude_id for r in self._rows.values())
|
||||
|
||||
async def name_exists(self, name: str, *, exclude_id: str | None = None) -> bool:
|
||||
return self._name_taken(name, exclude_id=exclude_id)
|
||||
|
||||
def _matches(
|
||||
self,
|
||||
row: dict[str, Any],
|
||||
*,
|
||||
keyword: str | None,
|
||||
enabled: bool | None,
|
||||
tag: str | None,
|
||||
) -> bool:
|
||||
if enabled is not None and bool(row.get("enabled")) is not enabled:
|
||||
return False
|
||||
if tag is not None and tag not in (row.get("tags") or []):
|
||||
return False
|
||||
if keyword:
|
||||
needle = keyword.strip().lower()
|
||||
if needle:
|
||||
hay = " ".join(
|
||||
[
|
||||
str(row.get("name") or ""),
|
||||
str(row.get("title_template") or ""),
|
||||
str(row.get("content_template") or ""),
|
||||
]
|
||||
).lower()
|
||||
if needle not in hay:
|
||||
return False
|
||||
return True
|
||||
|
||||
async def list_templates(
|
||||
self,
|
||||
*,
|
||||
keyword: str | None = None,
|
||||
enabled: bool | None = None,
|
||||
tag: str | None = None,
|
||||
page: int = 1,
|
||||
page_size: int = 20,
|
||||
) -> tuple[list[dict[str, Any]], int]:
|
||||
rows = [
|
||||
r
|
||||
for r in self._rows.values()
|
||||
if self._matches(r, keyword=keyword, enabled=enabled, tag=tag)
|
||||
]
|
||||
rows.sort(key=lambda r: r.get("created_at", ""), reverse=True)
|
||||
total = len(rows)
|
||||
page = max(1, page)
|
||||
page_size = max(1, page_size)
|
||||
start = (page - 1) * page_size
|
||||
items = [self._public(r) for r in rows[start : start + page_size]]
|
||||
return items, total
|
||||
|
||||
async def get_template(self, template_id: str) -> dict[str, Any] | None:
|
||||
row = self._rows.get(template_id)
|
||||
return self._public(row) if row is not None else None
|
||||
|
||||
async def create_template(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
if self._name_taken(data["name"]):
|
||||
raise DuplicateNotificationTemplateNameError(data["name"])
|
||||
now = datetime.now(UTC).isoformat()
|
||||
template_id = data.get("id") or uuid.uuid4().hex[:12]
|
||||
row = {
|
||||
"id": template_id,
|
||||
"name": data["name"],
|
||||
"title_template": data["title_template"],
|
||||
"content_template": data["content_template"],
|
||||
"tags": list(data.get("tags") or []),
|
||||
"enabled": data.get("enabled", True),
|
||||
"version": 1,
|
||||
"created_by": data.get("created_by"),
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
}
|
||||
self._rows[template_id] = row
|
||||
return self._public(row)
|
||||
|
||||
async def update_template(
|
||||
self,
|
||||
template_id: str,
|
||||
data: dict[str, Any],
|
||||
*,
|
||||
expected_version: int,
|
||||
) -> dict[str, Any] | None:
|
||||
row = self._rows.get(template_id)
|
||||
if row is None:
|
||||
return None
|
||||
if int(row["version"]) != int(expected_version):
|
||||
raise VersionConflictError(self._public(row))
|
||||
new_name = data.get("name")
|
||||
if new_name is not None and new_name != row["name"] and self._name_taken(new_name, exclude_id=template_id):
|
||||
raise DuplicateNotificationTemplateNameError(new_name)
|
||||
for key, value in data.items():
|
||||
if key in _WRITABLE:
|
||||
row[key] = value
|
||||
row["version"] = int(row["version"]) + 1
|
||||
row["updated_at"] = datetime.now(UTC).isoformat()
|
||||
return self._public(row)
|
||||
|
||||
async def delete_template(self, template_id: str) -> bool:
|
||||
return self._rows.pop(template_id, None) is not None
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
"""ORM model for notification templates (通知模板管理).
|
||||
|
||||
Global / shared — no ``user_id`` scoping. ``created_by`` is audit-only.
|
||||
All reads/writes are admin-gated at the router layer.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import Boolean, Integer, String, Text, UniqueConstraint
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.types import BeijingDateTime, PortableJSON
|
||||
|
||||
|
||||
class NotificationTemplateRow(Base):
|
||||
__tablename__ = "notification_templates"
|
||||
__table_args__ = (UniqueConstraint("name", name="uq_notification_templates_name"),)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(50))
|
||||
title_template: Mapped[str] = mapped_column(String(120))
|
||||
content_template: Mapped[str] = mapped_column(Text)
|
||||
tags: Mapped[list] = mapped_column(PortableJSON(), default=list)
|
||||
enabled: Mapped[bool] = mapped_column(Boolean, default=True, index=True)
|
||||
version: Mapped[int] = mapped_column(Integer, default=1)
|
||||
created_by: Mapped[str | None] = mapped_column(String(64))
|
||||
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),
|
||||
)
|
||||
+163
@@ -0,0 +1,163 @@
|
||||
"""SQLAlchemy-backed notification-template store (global / shared)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import or_, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.notification_templates.base import (
|
||||
DuplicateNotificationTemplateNameError,
|
||||
NotificationTemplateStore,
|
||||
VersionConflictError,
|
||||
)
|
||||
from deerflow.persistence.notification_templates.model import NotificationTemplateRow
|
||||
|
||||
_WRITABLE = {
|
||||
"name",
|
||||
"title_template",
|
||||
"content_template",
|
||||
"tags",
|
||||
"enabled",
|
||||
}
|
||||
|
||||
|
||||
def _to_dict(row: NotificationTemplateRow) -> dict[str, Any]:
|
||||
data = row.to_dict()
|
||||
for key in ("created_at", "updated_at"):
|
||||
value = data.get(key)
|
||||
if isinstance(value, datetime):
|
||||
data[key] = value.isoformat()
|
||||
data["tags"] = list(data.get("tags") or [])
|
||||
return data
|
||||
|
||||
|
||||
class NotificationTemplateRepository(NotificationTemplateStore):
|
||||
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
|
||||
self._sf = session_factory
|
||||
|
||||
async def name_exists(self, name: str, *, exclude_id: str | None = None) -> bool:
|
||||
async with self._sf() as session:
|
||||
return await self._name_taken(session, name, exclude_id=exclude_id)
|
||||
|
||||
async def _name_taken(self, session: AsyncSession, name: str, *, exclude_id: str | None = None) -> bool:
|
||||
stmt = select(NotificationTemplateRow.id).where(NotificationTemplateRow.name == name)
|
||||
if exclude_id is not None:
|
||||
stmt = stmt.where(NotificationTemplateRow.id != exclude_id)
|
||||
result = await session.execute(stmt.limit(1))
|
||||
return result.first() is not None
|
||||
|
||||
def _filter_stmt(
|
||||
self,
|
||||
*,
|
||||
keyword: str | None,
|
||||
enabled: bool | None,
|
||||
tag: str | None,
|
||||
):
|
||||
stmt = select(NotificationTemplateRow)
|
||||
if enabled is not None:
|
||||
stmt = stmt.where(NotificationTemplateRow.enabled.is_(enabled))
|
||||
if keyword:
|
||||
needle = f"%{keyword.strip()}%"
|
||||
if keyword.strip():
|
||||
stmt = stmt.where(
|
||||
or_(
|
||||
NotificationTemplateRow.name.ilike(needle),
|
||||
NotificationTemplateRow.title_template.ilike(needle),
|
||||
NotificationTemplateRow.content_template.ilike(needle),
|
||||
)
|
||||
)
|
||||
# Tag filter is applied in Python (PortableJSON differs across backends).
|
||||
return stmt
|
||||
|
||||
async def list_templates(
|
||||
self,
|
||||
*,
|
||||
keyword: str | None = None,
|
||||
enabled: bool | None = None,
|
||||
tag: str | None = None,
|
||||
page: int = 1,
|
||||
page_size: int = 20,
|
||||
) -> tuple[list[dict[str, Any]], int]:
|
||||
page = max(1, page)
|
||||
page_size = max(1, page_size)
|
||||
stmt = self._filter_stmt(keyword=keyword, enabled=enabled, tag=tag)
|
||||
stmt = stmt.order_by(NotificationTemplateRow.created_at.desc())
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(stmt)
|
||||
rows = [_to_dict(r) for r in result.scalars()]
|
||||
if tag is not None:
|
||||
rows = [r for r in rows if tag in (r.get("tags") or [])]
|
||||
total = len(rows)
|
||||
start = (page - 1) * page_size
|
||||
return rows[start : start + page_size], total
|
||||
|
||||
async def get_template(self, template_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(NotificationTemplateRow, template_id)
|
||||
return _to_dict(row) if row is not None else None
|
||||
|
||||
async def create_template(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
row = NotificationTemplateRow(
|
||||
id=data.get("id") or uuid.uuid4().hex[:12],
|
||||
name=data["name"],
|
||||
title_template=data["title_template"],
|
||||
content_template=data["content_template"],
|
||||
tags=list(data.get("tags") or []),
|
||||
enabled=data.get("enabled", True),
|
||||
version=1,
|
||||
created_by=data.get("created_by"),
|
||||
)
|
||||
async with self._sf() as session:
|
||||
if await self._name_taken(session, row.name):
|
||||
raise DuplicateNotificationTemplateNameError(row.name)
|
||||
session.add(row)
|
||||
try:
|
||||
await session.commit()
|
||||
except IntegrityError as exc:
|
||||
await session.rollback()
|
||||
raise DuplicateNotificationTemplateNameError(row.name) from exc
|
||||
await session.refresh(row)
|
||||
return _to_dict(row)
|
||||
|
||||
async def update_template(
|
||||
self,
|
||||
template_id: str,
|
||||
data: dict[str, Any],
|
||||
*,
|
||||
expected_version: int,
|
||||
) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(NotificationTemplateRow, template_id)
|
||||
if row is None:
|
||||
return None
|
||||
if int(row.version) != int(expected_version):
|
||||
raise VersionConflictError(_to_dict(row))
|
||||
new_name = data.get("name")
|
||||
if new_name is not None and new_name != row.name and await self._name_taken(session, new_name, exclude_id=template_id):
|
||||
raise DuplicateNotificationTemplateNameError(new_name)
|
||||
for key, value in data.items():
|
||||
if key in _WRITABLE:
|
||||
setattr(row, key, value)
|
||||
row.version = int(row.version) + 1
|
||||
row.updated_at = datetime.now(UTC)
|
||||
try:
|
||||
await session.commit()
|
||||
except IntegrityError as exc:
|
||||
await session.rollback()
|
||||
raise DuplicateNotificationTemplateNameError(new_name or row.name) from exc
|
||||
await session.refresh(row)
|
||||
return _to_dict(row)
|
||||
|
||||
async def delete_template(self, template_id: str) -> bool:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(NotificationTemplateRow, template_id)
|
||||
if row is None:
|
||||
return False
|
||||
await session.delete(row)
|
||||
await session.commit()
|
||||
return True
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
"""Template variable validation and preview helpers for notification templates.
|
||||
|
||||
Supported placeholders (exact names, no spaces):
|
||||
{{userName}} {{taskName}} {{taskId}} {{dueDate}}
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
ALLOWED_VARIABLES: frozenset[str] = frozenset({"userName", "taskName", "taskId", "dueDate"})
|
||||
|
||||
# Captures the inner of a well-formed {{...}} pair (no nested braces).
|
||||
_VAR_RE = re.compile(r"\{\{([^{}]*)\}\}")
|
||||
# Valid identifier: letters then letters/digits (no spaces / punctuation).
|
||||
_IDENT_RE = re.compile(r"^[A-Za-z][A-Za-z0-9]*$")
|
||||
|
||||
|
||||
class TemplateVariableError(ValueError):
|
||||
"""Raised when a template contains an invalid / unsupported / unclosed variable."""
|
||||
|
||||
def __init__(self, message: str) -> None:
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
|
||||
|
||||
def validate_template_text(text: str) -> None:
|
||||
"""Validate all ``{{var}}`` occurrences in *text*.
|
||||
|
||||
Raises ``TemplateVariableError`` with a Chinese message pointing at the
|
||||
offending variable or position.
|
||||
"""
|
||||
if not text:
|
||||
return
|
||||
|
||||
i = 0
|
||||
while True:
|
||||
start = text.find("{{", i)
|
||||
if start < 0:
|
||||
break
|
||||
end = text.find("}}", start + 2)
|
||||
if end < 0:
|
||||
raise TemplateVariableError(f"未闭合的变量(位置 {start})")
|
||||
inner = text[start + 2 : end]
|
||||
token = "{{" + inner + "}}"
|
||||
if not inner or not _IDENT_RE.fullmatch(inner):
|
||||
raise TemplateVariableError(f"变量格式错误:{token}")
|
||||
if inner not in ALLOWED_VARIABLES:
|
||||
raise TemplateVariableError(f"不支持的变量:{token}")
|
||||
i = end + 2
|
||||
|
||||
|
||||
def validate_templates(*texts: str) -> None:
|
||||
"""Validate every template string; first error wins."""
|
||||
for text in texts:
|
||||
validate_template_text(text or "")
|
||||
|
||||
|
||||
def extract_variables(text: str) -> list[str]:
|
||||
"""Return ordered unique allowed-looking variable names found in *text*."""
|
||||
seen: set[str] = set()
|
||||
out: list[str] = []
|
||||
for match in _VAR_RE.finditer(text or ""):
|
||||
name = match.group(1)
|
||||
if name in seen:
|
||||
continue
|
||||
seen.add(name)
|
||||
out.append(name)
|
||||
return out
|
||||
|
||||
|
||||
def render_template(text: str, variables: dict[str, str] | None) -> tuple[str, list[str]]:
|
||||
"""Replace known placeholders; keep missing ones and report their names.
|
||||
|
||||
Returns ``(rendered, missing_variables)``. Missing names are unique and
|
||||
appear in first-seen order.
|
||||
"""
|
||||
vars_map = variables or {}
|
||||
missing: list[str] = []
|
||||
seen_missing: set[str] = set()
|
||||
|
||||
def _repl(match: re.Match[str]) -> str:
|
||||
name = match.group(1)
|
||||
if name in vars_map and vars_map[name] is not None:
|
||||
return str(vars_map[name])
|
||||
if name not in seen_missing:
|
||||
seen_missing.add(name)
|
||||
missing.append(name)
|
||||
return match.group(0)
|
||||
|
||||
rendered = _VAR_RE.sub(_repl, text or "")
|
||||
return rendered, missing
|
||||
|
||||
|
||||
def preview_templates(
|
||||
title_template: str,
|
||||
content_template: str,
|
||||
variables: dict[str, str] | None = None,
|
||||
) -> dict[str, object]:
|
||||
"""Render title + content; merge missing variable lists (order-preserving)."""
|
||||
title, missing_title = render_template(title_template, variables)
|
||||
content, missing_content = render_template(content_template, variables)
|
||||
seen: set[str] = set()
|
||||
missing: list[str] = []
|
||||
for name in missing_title + missing_content:
|
||||
if name in seen:
|
||||
continue
|
||||
seen.add(name)
|
||||
missing.append(name)
|
||||
return {"title": title, "content": content, "missing_variables": missing}
|
||||
Reference in new issue
Block a user