新增两套 persistence 模块,仿 persistence/agents/ 模板:
1. persistence/skills/
- SkillRow ORM (PK=name, owner_user_id, published, timestamps)
- SkillStore 抽象:list_visible / get_visible / get_any /
get_owned / create / ensure_legacy / update / update_admin /
delete / delete_admin
- SqlAlchemy 实现 SkillRepository
- MemorySkillStore 用于测试
- 可见性 = owner OR published OR builtin
2. persistence/notifications/
- NotificationRow ORM (id, user_id, type, title, body, payload JSON,
read, created_at)
- NotificationStore 抽象:create / list_for_user / unread_count /
mark_read / mark_all_read / delete
- SqlAlchemy 实现 NotificationRepository
- MemoryNotificationStore 用于测试
- sender.notify(user_id, type, ...) 全局接口;harness 不能 import
app.*,所以用 set_notification_store / get_notification_store
注入模式
3. persistence/models/__init__.py 注册 SkillRow + NotificationRow
后续阶段会接入路由、cascade、curator per-user 重构、前端。
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
140 lines
5.5 KiB
Python
140 lines
5.5 KiB
Python
"""SQLAlchemy-backed custom skill repository."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime
|
|
from typing import Any
|
|
|
|
from sqlalchemy import or_, select
|
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
|
|
|
from deerflow.persistence.skills.base import SkillStore
|
|
from deerflow.persistence.skills.model import SkillRow
|
|
|
|
|
|
def _row_to_dict(row: SkillRow) -> dict[str, Any]:
|
|
data = row.to_dict()
|
|
data["builtin"] = data.get("owner_user_id") is None
|
|
for key in ("created_at", "updated_at"):
|
|
value = data.get(key)
|
|
if isinstance(value, datetime):
|
|
data[key] = value.isoformat()
|
|
return data
|
|
|
|
|
|
class SkillRepository(SkillStore):
|
|
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
|
|
self._sf = session_factory
|
|
|
|
@staticmethod
|
|
def _visible_filter(user_id: str):
|
|
return or_(
|
|
SkillRow.owner_user_id.is_(None),
|
|
SkillRow.owner_user_id == user_id,
|
|
SkillRow.published.is_(True),
|
|
)
|
|
|
|
async def list_visible(self, user_id: str) -> list[dict[str, Any]]:
|
|
stmt = select(SkillRow).where(self._visible_filter(user_id)).order_by(SkillRow.created_at.asc())
|
|
async with self._sf() as session:
|
|
result = await session.execute(stmt)
|
|
return [_row_to_dict(row) for row in result.scalars()]
|
|
|
|
async def get_visible(self, name: str, user_id: str) -> dict[str, Any] | None:
|
|
stmt = select(SkillRow).where(SkillRow.name == name, self._visible_filter(user_id))
|
|
async with self._sf() as session:
|
|
row = (await session.execute(stmt)).scalar_one_or_none()
|
|
return _row_to_dict(row) if row is not None else None
|
|
|
|
async def get_any(self, name: str) -> dict[str, Any] | None:
|
|
async with self._sf() as session:
|
|
row = await session.get(SkillRow, name)
|
|
return _row_to_dict(row) if row is not None else None
|
|
|
|
async def get_owned(self, name: str, user_id: str) -> dict[str, Any] | None:
|
|
stmt = select(SkillRow).where(SkillRow.name == name, SkillRow.owner_user_id == user_id)
|
|
async with self._sf() as session:
|
|
row = (await session.execute(stmt)).scalar_one_or_none()
|
|
return _row_to_dict(row) if row is not None else None
|
|
|
|
async def create(self, data: dict[str, Any]) -> dict[str, Any]:
|
|
now = datetime.now(UTC)
|
|
row = SkillRow(
|
|
name=data["name"],
|
|
owner_user_id=data.get("owner_user_id"),
|
|
published=bool(data.get("published", False)),
|
|
created_at=now,
|
|
updated_at=now,
|
|
)
|
|
async with self._sf() as session:
|
|
session.add(row)
|
|
await session.commit()
|
|
await session.refresh(row)
|
|
return _row_to_dict(row)
|
|
|
|
async def ensure_legacy(self, data: dict[str, Any]) -> dict[str, Any]:
|
|
async with self._sf() as session:
|
|
row = await session.get(SkillRow, data["name"])
|
|
if row is None:
|
|
now = datetime.now(UTC)
|
|
row = SkillRow(
|
|
name=data["name"],
|
|
owner_user_id=data.get("owner_user_id"),
|
|
published=bool(data.get("published", True)),
|
|
created_at=now,
|
|
updated_at=now,
|
|
)
|
|
session.add(row)
|
|
elif row.owner_user_id is None and data.get("owner_user_id") is not None:
|
|
# Only patch ownership if it was previously null — never overwrite
|
|
# an existing owner record on a re-migration.
|
|
row.owner_user_id = data["owner_user_id"]
|
|
row.updated_at = datetime.now(UTC)
|
|
await session.commit()
|
|
await session.refresh(row)
|
|
return _row_to_dict(row)
|
|
|
|
async def update(self, name: str, user_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
|
|
async with self._sf() as session:
|
|
row = await session.get(SkillRow, name)
|
|
if row is None or row.owner_user_id != user_id:
|
|
return None
|
|
if "published" in data:
|
|
row.published = bool(data["published"])
|
|
row.updated_at = datetime.now(UTC)
|
|
await session.commit()
|
|
await session.refresh(row)
|
|
return _row_to_dict(row)
|
|
|
|
async def update_admin(self, name: str, data: dict[str, Any]) -> dict[str, Any] | None:
|
|
async with self._sf() as session:
|
|
row = await session.get(SkillRow, name)
|
|
if row is None:
|
|
return None
|
|
if "published" in data:
|
|
row.published = bool(data["published"])
|
|
if "owner_user_id" in data:
|
|
row.owner_user_id = data["owner_user_id"]
|
|
row.updated_at = datetime.now(UTC)
|
|
await session.commit()
|
|
await session.refresh(row)
|
|
return _row_to_dict(row)
|
|
|
|
async def delete(self, name: str, user_id: str) -> bool:
|
|
async with self._sf() as session:
|
|
row = await session.get(SkillRow, name)
|
|
if row is None or row.owner_user_id != user_id:
|
|
return False
|
|
await session.delete(row)
|
|
await session.commit()
|
|
return True
|
|
|
|
async def delete_admin(self, name: str) -> bool:
|
|
async with self._sf() as session:
|
|
row = await session.get(SkillRow, name)
|
|
if row is None:
|
|
return False
|
|
await session.delete(row)
|
|
await session.commit()
|
|
return True
|