feat: 标签管理功能 + 问答前缀对用户隐藏
标签管理: - 新增 tags/tag_assignments 持久化层、迁移、/api/tags 路由 - 标签分智能体/技能/定时任务三类, 仅同类型可互打, 仅 admin 可增删改与打标签 - 三类资源列表支持按名称模糊检索与按标签筛选(走后端查询参数) - 前端新增标签管理页、TagPickerDialog(点击即时绑定)、彩色标签 chip 问答前缀: - 前缀不再拼入消息 content, 改存 additional_kwargs 由 PromptPrefixMiddleware 在调模型前临时注入, 用户界面看不到前缀但模型仍收到 - 修正 thread_existed 闸门导致首轮前缀被跳过的问题 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
aeba15eb16
commit
646cb8caeb
47 files changed
+2180
-109
No files matched your search
+12
-7
@@ -8,9 +8,10 @@ from deerflow.agents.lead_agent.prompt import apply_prompt_template
|
||||
from deerflow.agents.memory.summarization_hook import memory_flush_hook
|
||||
from deerflow.agents.middlewares.clarification_middleware import ClarificationMiddleware
|
||||
from deerflow.agents.middlewares.loop_detection_middleware import LoopDetectionMiddleware
|
||||
from deerflow.agents.middlewares.memory_middleware import MemoryMiddleware
|
||||
from deerflow.agents.middlewares.setup_agent_approval_middleware import SetupAgentApprovalMiddleware
|
||||
from deerflow.agents.middlewares.subagent_limit_middleware import SubagentLimitMiddleware
|
||||
from deerflow.agents.middlewares.memory_middleware import MemoryMiddleware
|
||||
from deerflow.agents.middlewares.prompt_prefix_middleware import PromptPrefixMiddleware
|
||||
from deerflow.agents.middlewares.setup_agent_approval_middleware import SetupAgentApprovalMiddleware
|
||||
from deerflow.agents.middlewares.subagent_limit_middleware import SubagentLimitMiddleware
|
||||
from deerflow.agents.middlewares.summarization_middleware import BeforeSummarizationHook, DeerFlowSummarizationMiddleware
|
||||
from deerflow.agents.middlewares.title_middleware import TitleMiddleware
|
||||
from deerflow.agents.middlewares.todo_middleware import TodoMiddleware
|
||||
@@ -257,6 +258,10 @@ def _build_middlewares(
|
||||
resolved_app_config = app_config or get_app_config()
|
||||
middlewares = build_lead_runtime_middlewares(app_config=resolved_app_config, lazy_init=True)
|
||||
|
||||
# Inject the admin prompt prefix into the model request (kept out of the
|
||||
# persisted/displayed message so the user never sees it).
|
||||
middlewares.append(PromptPrefixMiddleware())
|
||||
|
||||
# Add summarization middleware if enabled
|
||||
summarization_middleware = _create_summarization_middleware(app_config=resolved_app_config)
|
||||
if summarization_middleware is not None:
|
||||
@@ -304,10 +309,10 @@ def _build_middlewares(
|
||||
if custom_middlewares:
|
||||
middlewares.extend(custom_middlewares)
|
||||
|
||||
middlewares.append(SetupAgentApprovalMiddleware())
|
||||
|
||||
# ClarificationMiddleware should always be last
|
||||
middlewares.append(ClarificationMiddleware())
|
||||
middlewares.append(SetupAgentApprovalMiddleware())
|
||||
|
||||
# ClarificationMiddleware should always be last
|
||||
middlewares.append(ClarificationMiddleware())
|
||||
return middlewares
|
||||
|
||||
|
||||
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
"""Middleware that injects the admin-configured prompt prefix at model-call time.
|
||||
|
||||
The prompt-prefix feature lets an admin prepend a fixed instruction to the
|
||||
user's question before it reaches the LLM. To keep the prefix invisible in the
|
||||
chat UI, the Gateway does **not** concatenate it into the persisted
|
||||
``HumanMessage.content``; instead it tags the first human message with
|
||||
``additional_kwargs["prompt_prefix"]`` (see ``app.gateway.services``).
|
||||
|
||||
This middleware reads that tag and prepends the prefix to the message content
|
||||
*only in the request handed to the model*. The persisted state — and therefore
|
||||
what the frontend renders — keeps the clean, prefix-free content.
|
||||
"""
|
||||
|
||||
from collections.abc import Awaitable, Callable
|
||||
from typing import override
|
||||
|
||||
from langchain.agents import AgentState
|
||||
from langchain.agents.middleware import AgentMiddleware
|
||||
from langchain.agents.middleware.types import ModelCallResult, ModelRequest, ModelResponse
|
||||
from langchain_core.messages import HumanMessage
|
||||
|
||||
PROMPT_PREFIX_KEY = "prompt_prefix"
|
||||
|
||||
|
||||
class PromptPrefixMiddleware(AgentMiddleware[AgentState]):
|
||||
"""Prepends ``additional_kwargs['prompt_prefix']`` to human messages for the LLM only.
|
||||
|
||||
The transformation is applied to the model request copy, never written back
|
||||
to state, so it is naturally idempotent across the multiple model calls of a
|
||||
single turn.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def _apply(messages: list) -> list | None:
|
||||
"""Return a patched copy with prefixes prepended, or None if nothing to do."""
|
||||
patched: list | None = None
|
||||
for idx, msg in enumerate(messages):
|
||||
if not isinstance(msg, HumanMessage):
|
||||
continue
|
||||
raw_prefix = (getattr(msg, "additional_kwargs", None) or {}).get(PROMPT_PREFIX_KEY)
|
||||
prefix = str(raw_prefix).strip() if raw_prefix else ""
|
||||
if not prefix:
|
||||
continue
|
||||
content = msg.content
|
||||
if isinstance(content, str):
|
||||
new_content: object = f"{prefix}\n\n{content}".strip("\n")
|
||||
elif isinstance(content, list):
|
||||
# Structured content (text blocks + media): prepend a text block.
|
||||
new_content = [{"type": "text", "text": prefix}, *content]
|
||||
else:
|
||||
new_content = f"{prefix}\n\n{content!s}"
|
||||
if patched is None:
|
||||
patched = list(messages)
|
||||
# model_copy preserves the message id so streaming dedup is unaffected.
|
||||
patched[idx] = msg.model_copy(update={"content": new_content})
|
||||
return patched
|
||||
|
||||
@override
|
||||
def wrap_model_call(
|
||||
self,
|
||||
request: ModelRequest,
|
||||
handler: Callable[[ModelRequest], ModelResponse],
|
||||
) -> ModelCallResult:
|
||||
patched = self._apply(request.messages)
|
||||
if patched is not None:
|
||||
request = request.override(messages=patched)
|
||||
return handler(request)
|
||||
|
||||
@override
|
||||
async def awrap_model_call(
|
||||
self,
|
||||
request: ModelRequest,
|
||||
handler: Callable[[ModelRequest], Awaitable[ModelResponse]],
|
||||
) -> ModelCallResult:
|
||||
patched = self._apply(request.messages)
|
||||
if patched is not None:
|
||||
request = request.override(messages=patched)
|
||||
return await handler(request)
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
"""Create tags and tag_assignments tables.
|
||||
|
||||
Revision ID: 20260515_01
|
||||
Revises: 20260514_02
|
||||
Create Date: 2026-05-15
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from sqlalchemy import inspect
|
||||
|
||||
|
||||
revision = "20260515_01"
|
||||
down_revision = "20260514_02"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def _has_table(table_name: str) -> bool:
|
||||
inspector = inspect(op.get_bind())
|
||||
return table_name in inspector.get_table_names()
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
if not _has_table("tags"):
|
||||
op.create_table(
|
||||
"tags",
|
||||
sa.Column("id", sa.String(64), primary_key=True),
|
||||
sa.Column("name", sa.String(256), nullable=False),
|
||||
sa.Column("tag_type", sa.String(32), nullable=False),
|
||||
sa.Column("color", sa.String(16), nullable=False, server_default="gray"),
|
||||
sa.Column("description", sa.String(1024), nullable=False, server_default=""),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.UniqueConstraint("tag_type", "name", name="uq_tags_type_name"),
|
||||
)
|
||||
op.create_index("ix_tags_tag_type", "tags", ["tag_type"])
|
||||
|
||||
if not _has_table("tag_assignments"):
|
||||
op.create_table(
|
||||
"tag_assignments",
|
||||
sa.Column("id", sa.String(64), primary_key=True),
|
||||
sa.Column("tag_id", sa.String(64), nullable=False),
|
||||
sa.Column("target_type", sa.String(32), nullable=False),
|
||||
sa.Column("target_id", sa.String(64), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.UniqueConstraint("tag_id", "target_type", "target_id", name="uq_tag_assignments_tag_target"),
|
||||
)
|
||||
op.create_index("ix_tag_assignments_tag_id", "tag_assignments", ["tag_id"])
|
||||
op.create_index("ix_tag_assignments_target_type", "tag_assignments", ["target_type"])
|
||||
op.create_index("ix_tag_assignments_target_id", "tag_assignments", ["target_id"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
if _has_table("tag_assignments"):
|
||||
op.drop_table("tag_assignments")
|
||||
if _has_table("tags"):
|
||||
op.drop_table("tags")
|
||||
+3
@@ -30,6 +30,7 @@ from deerflow.persistence.scheduled_tasks.model import (
|
||||
SchedulerThreadRow,
|
||||
)
|
||||
from deerflow.persistence.skills.model import SkillRow
|
||||
from deerflow.persistence.tags.model import TagAssignmentRow, TagRow
|
||||
from deerflow.persistence.thread_meta.model import ThreadMetaRow
|
||||
from deerflow.persistence.user.model import UserRow
|
||||
|
||||
@@ -47,6 +48,8 @@ __all__ = [
|
||||
"ScheduledTaskSubscriptionRow",
|
||||
"SchedulerThreadRow",
|
||||
"SkillRow",
|
||||
"TagAssignmentRow",
|
||||
"TagRow",
|
||||
"ThreadMetaRow",
|
||||
"UserRow",
|
||||
]
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
"""Persistence layer for tags and tag-to-resource assignments."""
|
||||
|
||||
from deerflow.persistence.tags.base import TagStore
|
||||
from deerflow.persistence.tags.memory import MemoryTagStore
|
||||
from deerflow.persistence.tags.model import TAG_TYPES
|
||||
from deerflow.persistence.tags.sql import TagRepository
|
||||
|
||||
__all__ = ["TagStore", "MemoryTagStore", "TagRepository", "TAG_TYPES", "make_tag_store"]
|
||||
|
||||
|
||||
def make_tag_store(session_factory):
|
||||
if session_factory is None:
|
||||
return MemoryTagStore()
|
||||
return TagRepository(session_factory)
|
||||
@@ -0,0 +1,73 @@
|
||||
"""Abstract persistence API for tags and tag-to-resource assignments."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any
|
||||
|
||||
|
||||
class TagStore(ABC):
|
||||
# ------------------------------------------------------------------ tags
|
||||
|
||||
@abstractmethod
|
||||
async def list_tags(self, tag_type: str | None = None) -> list[dict[str, Any]]:
|
||||
"""List tags, optionally filtered by ``tag_type``.
|
||||
|
||||
Each record includes a derived ``usage_count`` (number of assignments).
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def get_tag(self, tag_id: str) -> dict[str, Any] | None:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def create_tag(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Create a tag. ``data`` must carry ``id``, ``name``, ``tag_type``.
|
||||
|
||||
Raises ``ValueError`` if ``(tag_type, name)`` is not unique.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def update_tag(self, tag_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
|
||||
"""Update ``name`` / ``color`` / ``description``. ``tag_type`` is immutable.
|
||||
|
||||
Returns ``None`` if the tag does not exist. Raises ``ValueError`` on a
|
||||
name collision within the type.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def delete_tag(self, tag_id: str) -> bool:
|
||||
"""Delete a tag and cascade-remove all of its assignments."""
|
||||
raise NotImplementedError
|
||||
|
||||
# ----------------------------------------------------------- assignments
|
||||
|
||||
@abstractmethod
|
||||
async def get_target_tags(self, target_type: str, target_id: str) -> list[dict[str, Any]]:
|
||||
"""Return the tags currently assigned to one resource."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def set_target_tags(self, target_type: str, target_id: str, tag_ids: list[str]) -> list[dict[str, Any]]:
|
||||
"""Replace the full set of tags on a resource.
|
||||
|
||||
Every id in ``tag_ids`` must reference an existing tag whose
|
||||
``tag_type`` equals ``target_type``; otherwise raises ``ValueError``.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def list_assignments(self, target_type: str, target_ids: list[str]) -> dict[str, list[dict[str, Any]]]:
|
||||
"""Bulk-fetch tags for many resources at once (avoids N+1 on list pages).
|
||||
|
||||
Returns ``{target_id: [tag, ...]}`` for ids that have at least one tag.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def unassign_all(self, target_type: str, target_id: str) -> int:
|
||||
"""Drop every tag assignment for a resource. Returns the count removed."""
|
||||
raise NotImplementedError
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
"""In-memory ``TagStore`` implementation for tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from deerflow.persistence.tags.base import TagStore
|
||||
|
||||
|
||||
def _now() -> str:
|
||||
return datetime.now(UTC).isoformat()
|
||||
|
||||
|
||||
def _copy(record: dict[str, Any]) -> dict[str, Any]:
|
||||
return copy.deepcopy(record)
|
||||
|
||||
|
||||
class MemoryTagStore(TagStore):
|
||||
def __init__(self) -> None:
|
||||
self._tags: dict[str, dict[str, Any]] = {}
|
||||
# assignments keyed by surrogate id
|
||||
self._assignments: dict[str, dict[str, Any]] = {}
|
||||
|
||||
# ------------------------------------------------------------------ tags
|
||||
|
||||
def _usage_count(self, tag_id: str) -> int:
|
||||
return sum(1 for a in self._assignments.values() if a["tag_id"] == tag_id)
|
||||
|
||||
def _with_count(self, record: dict[str, Any]) -> dict[str, Any]:
|
||||
out = _copy(record)
|
||||
out["usage_count"] = self._usage_count(record["id"])
|
||||
return out
|
||||
|
||||
async def list_tags(self, tag_type: str | None = None) -> list[dict[str, Any]]:
|
||||
rows = [r for r in self._tags.values() if tag_type is None or r["tag_type"] == tag_type]
|
||||
rows.sort(key=lambda r: r["created_at"])
|
||||
return [self._with_count(r) for r in rows]
|
||||
|
||||
async def get_tag(self, tag_id: str) -> dict[str, Any] | None:
|
||||
record = self._tags.get(tag_id)
|
||||
return self._with_count(record) if record is not None else None
|
||||
|
||||
async def create_tag(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
tag_id = data["id"]
|
||||
name = data["name"]
|
||||
tag_type = data["tag_type"]
|
||||
if any(r["tag_type"] == tag_type and r["name"] == name for r in self._tags.values()):
|
||||
raise ValueError(f"Tag '{name}' already exists for type '{tag_type}'")
|
||||
record = {
|
||||
"id": tag_id,
|
||||
"name": name,
|
||||
"tag_type": tag_type,
|
||||
"color": data.get("color") or "gray",
|
||||
"description": data.get("description") or "",
|
||||
"created_at": _now(),
|
||||
"updated_at": _now(),
|
||||
}
|
||||
self._tags[tag_id] = record
|
||||
return self._with_count(record)
|
||||
|
||||
async def update_tag(self, tag_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
|
||||
record = self._tags.get(tag_id)
|
||||
if record is None:
|
||||
return None
|
||||
new_name = data.get("name", record["name"])
|
||||
if any(r["id"] != tag_id and r["tag_type"] == record["tag_type"] and r["name"] == new_name for r in self._tags.values()):
|
||||
raise ValueError(f"Tag '{new_name}' already exists for type '{record['tag_type']}'")
|
||||
if "name" in data:
|
||||
record["name"] = data["name"]
|
||||
if "color" in data:
|
||||
record["color"] = data["color"]
|
||||
if "description" in data:
|
||||
record["description"] = data["description"]
|
||||
record["updated_at"] = _now()
|
||||
return self._with_count(record)
|
||||
|
||||
async def delete_tag(self, tag_id: str) -> bool:
|
||||
if tag_id not in self._tags:
|
||||
return False
|
||||
del self._tags[tag_id]
|
||||
for aid in [aid for aid, a in self._assignments.items() if a["tag_id"] == tag_id]:
|
||||
del self._assignments[aid]
|
||||
return True
|
||||
|
||||
# ----------------------------------------------------------- assignments
|
||||
|
||||
def _tags_for(self, target_type: str, target_id: str) -> list[dict[str, Any]]:
|
||||
tag_ids = [a["tag_id"] for a in self._assignments.values() if a["target_type"] == target_type and a["target_id"] == target_id]
|
||||
records = [self._tags[tid] for tid in tag_ids if tid in self._tags]
|
||||
records.sort(key=lambda r: r["name"])
|
||||
return [self._with_count(r) for r in records]
|
||||
|
||||
async def get_target_tags(self, target_type: str, target_id: str) -> list[dict[str, Any]]:
|
||||
return self._tags_for(target_type, target_id)
|
||||
|
||||
async def set_target_tags(self, target_type: str, target_id: str, tag_ids: list[str]) -> list[dict[str, Any]]:
|
||||
unique_ids = list(dict.fromkeys(tag_ids))
|
||||
for tid in unique_ids:
|
||||
tag = self._tags.get(tid)
|
||||
if tag is None:
|
||||
raise ValueError(f"Tag '{tid}' does not exist")
|
||||
if tag["tag_type"] != target_type:
|
||||
raise ValueError(f"Tag '{tid}' is type '{tag['tag_type']}', cannot tag a '{target_type}' resource")
|
||||
# drop existing, then re-add
|
||||
for aid in [aid for aid, a in self._assignments.items() if a["target_type"] == target_type and a["target_id"] == target_id]:
|
||||
del self._assignments[aid]
|
||||
for tid in unique_ids:
|
||||
aid = f"{tid}:{target_type}:{target_id}"
|
||||
self._assignments[aid] = {
|
||||
"id": aid,
|
||||
"tag_id": tid,
|
||||
"target_type": target_type,
|
||||
"target_id": target_id,
|
||||
"created_at": _now(),
|
||||
}
|
||||
return self._tags_for(target_type, target_id)
|
||||
|
||||
async def list_assignments(self, target_type: str, target_ids: list[str]) -> dict[str, list[dict[str, Any]]]:
|
||||
wanted = set(target_ids)
|
||||
out: dict[str, list[dict[str, Any]]] = {}
|
||||
for a in self._assignments.values():
|
||||
if a["target_type"] != target_type or a["target_id"] not in wanted:
|
||||
continue
|
||||
tag = self._tags.get(a["tag_id"])
|
||||
if tag is None:
|
||||
continue
|
||||
out.setdefault(a["target_id"], []).append(self._with_count(tag))
|
||||
for tags in out.values():
|
||||
tags.sort(key=lambda r: r["name"])
|
||||
return out
|
||||
|
||||
async def unassign_all(self, target_type: str, target_id: str) -> int:
|
||||
victims = [aid for aid, a in self._assignments.items() if a["target_type"] == target_type and a["target_id"] == target_id]
|
||||
for aid in victims:
|
||||
del self._assignments[aid]
|
||||
return len(victims)
|
||||
@@ -0,0 +1,47 @@
|
||||
"""ORM models for tags and tag-to-resource assignments.
|
||||
|
||||
Two tables:
|
||||
|
||||
- ``tags`` — a tag belongs to exactly one ``tag_type`` (``agent`` / ``skill`` /
|
||||
``scheduled_task``). Tag names are unique *within* a type.
|
||||
- ``tag_assignments`` — many-to-many link between a tag and a target resource.
|
||||
``target_type`` is denormalized from the tag so the table is queryable by
|
||||
``(target_type, target_id)`` without a join. The API layer enforces the
|
||||
invariant ``target_type == tag.tag_type``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import DateTime, String, UniqueConstraint
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from deerflow.persistence.base import Base
|
||||
|
||||
# Allowed tag/target types. Kept here so persistence + API share one source.
|
||||
TAG_TYPES: tuple[str, ...] = ("agent", "skill", "scheduled_task")
|
||||
|
||||
|
||||
class TagRow(Base):
|
||||
__tablename__ = "tags"
|
||||
__table_args__ = (UniqueConstraint("tag_type", "name", name="uq_tags_type_name"),)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(256), nullable=False)
|
||||
tag_type: Mapped[str] = mapped_column(String(32), nullable=False, index=True)
|
||||
color: Mapped[str] = mapped_column(String(16), nullable=False, default="gray")
|
||||
description: Mapped[str] = mapped_column(String(1024), nullable=False, default="")
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, default=lambda: datetime.now(UTC))
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, default=lambda: datetime.now(UTC), onupdate=lambda: datetime.now(UTC))
|
||||
|
||||
|
||||
class TagAssignmentRow(Base):
|
||||
__tablename__ = "tag_assignments"
|
||||
__table_args__ = (UniqueConstraint("tag_id", "target_type", "target_id", name="uq_tag_assignments_tag_target"),)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
tag_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
target_type: Mapped[str] = mapped_column(String(32), nullable=False, index=True)
|
||||
target_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, default=lambda: datetime.now(UTC))
|
||||
@@ -0,0 +1,187 @@
|
||||
"""SQLAlchemy-backed tag + tag-assignment repository."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import delete, func, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.tags.base import TagStore
|
||||
from deerflow.persistence.tags.model import TagAssignmentRow, TagRow
|
||||
|
||||
|
||||
def _tag_to_dict(row: TagRow, usage_count: int = 0) -> 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["usage_count"] = usage_count
|
||||
return data
|
||||
|
||||
|
||||
class TagRepository(TagStore):
|
||||
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
|
||||
self._sf = session_factory
|
||||
|
||||
@staticmethod
|
||||
async def _usage_counts(session: AsyncSession, tag_ids: list[str]) -> dict[str, int]:
|
||||
if not tag_ids:
|
||||
return {}
|
||||
stmt = select(TagAssignmentRow.tag_id, func.count()).where(TagAssignmentRow.tag_id.in_(tag_ids)).group_by(TagAssignmentRow.tag_id)
|
||||
result = await session.execute(stmt)
|
||||
return {tag_id: count for tag_id, count in result.all()}
|
||||
|
||||
# ------------------------------------------------------------------ tags
|
||||
|
||||
async def list_tags(self, tag_type: str | None = None) -> list[dict[str, Any]]:
|
||||
stmt = select(TagRow)
|
||||
if tag_type is not None:
|
||||
stmt = stmt.where(TagRow.tag_type == tag_type)
|
||||
stmt = stmt.order_by(TagRow.created_at.asc())
|
||||
async with self._sf() as session:
|
||||
rows = list((await session.execute(stmt)).scalars())
|
||||
counts = await self._usage_counts(session, [r.id for r in rows])
|
||||
return [_tag_to_dict(r, counts.get(r.id, 0)) for r in rows]
|
||||
|
||||
async def get_tag(self, tag_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(TagRow, tag_id)
|
||||
if row is None:
|
||||
return None
|
||||
counts = await self._usage_counts(session, [tag_id])
|
||||
return _tag_to_dict(row, counts.get(tag_id, 0))
|
||||
|
||||
async def create_tag(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
now = datetime.now(UTC)
|
||||
row = TagRow(
|
||||
id=data["id"],
|
||||
name=data["name"],
|
||||
tag_type=data["tag_type"],
|
||||
color=data.get("color") or "gray",
|
||||
description=data.get("description") or "",
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
async with self._sf() as session:
|
||||
session.add(row)
|
||||
try:
|
||||
await session.commit()
|
||||
except IntegrityError as exc:
|
||||
await session.rollback()
|
||||
raise ValueError(f"Tag '{data['name']}' already exists for type '{data['tag_type']}'") from exc
|
||||
await session.refresh(row)
|
||||
return _tag_to_dict(row, 0)
|
||||
|
||||
async def update_tag(self, tag_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(TagRow, tag_id)
|
||||
if row is None:
|
||||
return None
|
||||
if "name" in data:
|
||||
row.name = data["name"]
|
||||
if "color" in data:
|
||||
row.color = data["color"]
|
||||
if "description" in data:
|
||||
row.description = data["description"]
|
||||
row.updated_at = datetime.now(UTC)
|
||||
try:
|
||||
await session.commit()
|
||||
except IntegrityError as exc:
|
||||
await session.rollback()
|
||||
raise ValueError(f"Tag '{data.get('name')}' already exists for type '{row.tag_type}'") from exc
|
||||
await session.refresh(row)
|
||||
counts = await self._usage_counts(session, [tag_id])
|
||||
return _tag_to_dict(row, counts.get(tag_id, 0))
|
||||
|
||||
async def delete_tag(self, tag_id: str) -> bool:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(TagRow, tag_id)
|
||||
if row is None:
|
||||
return False
|
||||
await session.execute(delete(TagAssignmentRow).where(TagAssignmentRow.tag_id == tag_id))
|
||||
await session.delete(row)
|
||||
await session.commit()
|
||||
return True
|
||||
|
||||
# ----------------------------------------------------------- assignments
|
||||
|
||||
async def _tags_for(self, session: AsyncSession, target_type: str, target_id: str) -> list[dict[str, Any]]:
|
||||
stmt = (
|
||||
select(TagRow)
|
||||
.join(TagAssignmentRow, TagAssignmentRow.tag_id == TagRow.id)
|
||||
.where(TagAssignmentRow.target_type == target_type, TagAssignmentRow.target_id == target_id)
|
||||
.order_by(TagRow.name.asc())
|
||||
)
|
||||
rows = list((await session.execute(stmt)).scalars())
|
||||
counts = await self._usage_counts(session, [r.id for r in rows])
|
||||
return [_tag_to_dict(r, counts.get(r.id, 0)) for r in rows]
|
||||
|
||||
async def get_target_tags(self, target_type: str, target_id: str) -> list[dict[str, Any]]:
|
||||
async with self._sf() as session:
|
||||
return await self._tags_for(session, target_type, target_id)
|
||||
|
||||
async def set_target_tags(self, target_type: str, target_id: str, tag_ids: list[str]) -> list[dict[str, Any]]:
|
||||
unique_ids = list(dict.fromkeys(tag_ids))
|
||||
async with self._sf() as session:
|
||||
if unique_ids:
|
||||
found = list((await session.execute(select(TagRow).where(TagRow.id.in_(unique_ids)))).scalars())
|
||||
by_id = {r.id: r for r in found}
|
||||
for tid in unique_ids:
|
||||
tag = by_id.get(tid)
|
||||
if tag is None:
|
||||
raise ValueError(f"Tag '{tid}' does not exist")
|
||||
if tag.tag_type != target_type:
|
||||
raise ValueError(f"Tag '{tid}' is type '{tag.tag_type}', cannot tag a '{target_type}' resource")
|
||||
await session.execute(
|
||||
delete(TagAssignmentRow).where(
|
||||
TagAssignmentRow.target_type == target_type,
|
||||
TagAssignmentRow.target_id == target_id,
|
||||
)
|
||||
)
|
||||
now = datetime.now(UTC)
|
||||
for tid in unique_ids:
|
||||
session.add(
|
||||
TagAssignmentRow(
|
||||
id=uuid.uuid4().hex,
|
||||
tag_id=tid,
|
||||
target_type=target_type,
|
||||
target_id=target_id,
|
||||
created_at=now,
|
||||
)
|
||||
)
|
||||
await session.commit()
|
||||
return await self._tags_for(session, target_type, target_id)
|
||||
|
||||
async def list_assignments(self, target_type: str, target_ids: list[str]) -> dict[str, list[dict[str, Any]]]:
|
||||
if not target_ids:
|
||||
return {}
|
||||
stmt = (
|
||||
select(TagAssignmentRow.target_id, TagRow)
|
||||
.join(TagRow, TagAssignmentRow.tag_id == TagRow.id)
|
||||
.where(TagAssignmentRow.target_type == target_type, TagAssignmentRow.target_id.in_(target_ids))
|
||||
.order_by(TagRow.name.asc())
|
||||
)
|
||||
async with self._sf() as session:
|
||||
rows = (await session.execute(stmt)).all()
|
||||
tag_rows = [tag for _, tag in rows]
|
||||
counts = await self._usage_counts(session, [t.id for t in tag_rows])
|
||||
out: dict[str, list[dict[str, Any]]] = {}
|
||||
for target_id, tag in rows:
|
||||
out.setdefault(target_id, []).append(_tag_to_dict(tag, counts.get(tag.id, 0)))
|
||||
return out
|
||||
|
||||
async def unassign_all(self, target_type: str, target_id: str) -> int:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
delete(TagAssignmentRow).where(
|
||||
TagAssignmentRow.target_type == target_type,
|
||||
TagAssignmentRow.target_id == target_id,
|
||||
)
|
||||
)
|
||||
await session.commit()
|
||||
return result.rowcount or 0
|
||||
Reference in new issue
Block a user