refactor: 移除提示词模板页面及相关接口,精简岗位/标签/用户提示词模型
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
838e986670
commit
e6662cf673
19 files changed
+55
-729
No files matched your search
+8
-64
@@ -14,14 +14,15 @@ Resolution rule (per resource type):
|
||||
* For a resource type **without** bound tags → the *default set*:
|
||||
- ``agent`` → all built-in agents (``user_id IS NULL``)
|
||||
- ``scheduled_task`` → empty (no built-in tasks)
|
||||
- ``user_prompt`` → admin templates that carry no tag
|
||||
- ``skill`` / ``recommended_question`` → *unrestricted* (handled at query
|
||||
time, never materialized — see :meth:`resolve_visible`)
|
||||
|
||||
Materialization (``sync_user``) only ever touches rows it created itself
|
||||
(``origin='position'``), so a user's own favorites / subscriptions / prompts are
|
||||
never clobbered. The skill + recommended-question visibility filters are
|
||||
computed live by :meth:`resolve_visible` and are *not* materialized.
|
||||
(``origin='position'``), so a user's own favorites / subscriptions are never
|
||||
clobbered. The skill + recommended-question visibility filters are computed live
|
||||
by :meth:`resolve_visible` and are *not* materialized. (The admin-configured
|
||||
prompts shown to users ARE the recommended questions — there is no separate
|
||||
prompt-template store.)
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -30,19 +31,18 @@ import logging
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import delete, select
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.agents.model import AgentFavoriteRow, AgentRow
|
||||
from deerflow.persistence.positions.base import PositionStore
|
||||
from deerflow.persistence.scheduled_tasks.model import ScheduledTaskSubscriptionRow
|
||||
from deerflow.persistence.tags.base import TagStore
|
||||
from deerflow.persistence.user_prompts.model import UserCustomPromptRow
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Materialized into "我的" tables by sync_user.
|
||||
_MATERIALIZED_TYPES = ("agent", "scheduled_task", "user_prompt")
|
||||
_MATERIALIZED_TYPES = ("agent", "scheduled_task")
|
||||
# Resolved live at query time (visibility filters), never materialized.
|
||||
_VISIBILITY_TYPES = ("skill", "recommended_question")
|
||||
|
||||
@@ -89,20 +89,9 @@ class PositionSyncService:
|
||||
if resource_type == "agent":
|
||||
rows = (await session.execute(select(AgentRow.id).where(AgentRow.user_id.is_(None)))).all()
|
||||
return {r[0] for r in rows}
|
||||
if resource_type == "scheduled_task":
|
||||
return set()
|
||||
if resource_type == "user_prompt":
|
||||
# Admin templates that carry no tag = global default templates.
|
||||
template_ids = {r[0] for r in (await session.execute(select(UserCustomPromptRow.id).where(UserCustomPromptRow.is_template.is_(True)))).all()}
|
||||
if not template_ids:
|
||||
return set()
|
||||
tagged = set(await self._tags.list_targets_for_tags("user_prompt", await self._all_user_prompt_tag_ids()))
|
||||
return template_ids - tagged
|
||||
# scheduled_task (and any other materialized type) defaults to empty.
|
||||
return set()
|
||||
|
||||
async def _all_user_prompt_tag_ids(self) -> list[str]:
|
||||
return [t["id"] for t in await self._tags.list_tags("user_prompt")]
|
||||
|
||||
# --------------------------------------------------------- materialize
|
||||
|
||||
async def sync_user(self, user_id: str) -> None:
|
||||
@@ -113,7 +102,6 @@ class PositionSyncService:
|
||||
async with self._sf() as session:
|
||||
await self._sync_agents(session, user_id, pid)
|
||||
await self._sync_tasks(session, user_id, pid)
|
||||
await self._sync_prompts(session, user_id, pid)
|
||||
await session.commit()
|
||||
|
||||
async def _sync_agents(self, session: AsyncSession, user_id: str, position_id: str | None) -> None:
|
||||
@@ -140,50 +128,6 @@ class PositionSyncService:
|
||||
if task_id not in target:
|
||||
await session.delete(row)
|
||||
|
||||
async def _sync_prompts(self, session: AsyncSession, user_id: str, position_id: str | None) -> None:
|
||||
target_template_ids = await self._resolve_materialized(user_id, "user_prompt", session)
|
||||
# Load the templates we need to clone, so we can copy their content.
|
||||
templates: dict[str, UserCustomPromptRow] = {}
|
||||
if target_template_ids:
|
||||
for row in (await session.execute(select(UserCustomPromptRow).where(UserCustomPromptRow.id.in_(target_template_ids), UserCustomPromptRow.is_template.is_(True)))).scalars():
|
||||
templates[row.id] = row
|
||||
existing = list(
|
||||
(await session.execute(select(UserCustomPromptRow).where(UserCustomPromptRow.user_id == user_id, UserCustomPromptRow.origin == "position"))).scalars()
|
||||
)
|
||||
existing_by_template = {r.template_id: r for r in existing if r.template_id}
|
||||
now = datetime.now(UTC)
|
||||
# Remove stale clones (template no longer granted or template deleted).
|
||||
for tpl_id, row in existing_by_template.items():
|
||||
if tpl_id not in templates:
|
||||
await session.delete(row)
|
||||
# Add / refresh clones for granted templates.
|
||||
for tpl_id, tpl in templates.items():
|
||||
row = existing_by_template.get(tpl_id)
|
||||
if row is None:
|
||||
session.add(
|
||||
UserCustomPromptRow(
|
||||
id=uuid.uuid4().hex,
|
||||
user_id=user_id,
|
||||
title=tpl.title,
|
||||
content=tpl.content,
|
||||
sort_order=tpl.sort_order,
|
||||
agent_id=tpl.agent_id,
|
||||
is_template=False,
|
||||
origin="position",
|
||||
template_id=tpl_id,
|
||||
position_id=position_id,
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
)
|
||||
else:
|
||||
# Keep the user's clone in step with the template content.
|
||||
row.title = tpl.title
|
||||
row.content = tpl.content
|
||||
row.agent_id = tpl.agent_id
|
||||
row.position_id = position_id
|
||||
row.updated_at = now
|
||||
|
||||
# ------------------------------------------------------------- fan-out
|
||||
|
||||
async def sync_position(self, position_id: str) -> int:
|
||||
|
||||
Reference in new issue
Block a user