代码提交
This commit is contained in:
1 parent
b101ee7fc9
commit
1938c80061
42 files changed
+5552
-278
No files matched your search
+61
@@ -0,0 +1,61 @@
|
||||
"""Create parallel_agent_tasks table.
|
||||
|
||||
Saved runs of the standalone parallel multi-agent panel (Vue embeddable
|
||||
component): task name + brief + agents snapshot + the resulting message list,
|
||||
bound per user. ``brief`` / ``agents`` / ``messages`` are LONGTEXT on MySQL
|
||||
(mirror ``roundtable_drafts`` headroom) and plain TEXT elsewhere — matching
|
||||
``PortableLongText``. ``create_all`` builds the table for brand-new DBs; this
|
||||
revision patches DBs that already exist.
|
||||
|
||||
Chained after ``20260608_04`` (chain stage_goals) so the revision graph stays
|
||||
single-headed — otherwise ``alembic upgrade head`` aborts with "multiple heads"
|
||||
and the startup auto-migrate silently falls back to the create_all schema.
|
||||
|
||||
Revision ID: 20260608_05
|
||||
Revises: 20260608_04
|
||||
Create Date: 2026-06-08
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from sqlalchemy import inspect
|
||||
from sqlalchemy.dialects.mysql import LONGTEXT
|
||||
|
||||
|
||||
revision = "20260608_05"
|
||||
down_revision = "20260608_04"
|
||||
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 _has_table("parallel_agent_tasks"):
|
||||
return
|
||||
long_text = LONGTEXT() if op.get_bind().dialect.name == "mysql" else sa.Text()
|
||||
op.create_table(
|
||||
"parallel_agent_tasks",
|
||||
sa.Column("id", sa.String(length=64), primary_key=True, nullable=False),
|
||||
sa.Column("user_id", sa.String(length=64), nullable=False),
|
||||
sa.Column("title", sa.String(length=512), nullable=False, server_default=""),
|
||||
sa.Column("brief", long_text, nullable=True),
|
||||
sa.Column("agents", long_text, nullable=True),
|
||||
sa.Column("messages", long_text, nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
)
|
||||
op.create_index(
|
||||
"ix_parallel_agent_tasks_user_id", "parallel_agent_tasks", ["user_id"]
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
if _has_table("parallel_agent_tasks"):
|
||||
op.drop_index("ix_parallel_agent_tasks_user_id", table_name="parallel_agent_tasks")
|
||||
op.drop_table("parallel_agent_tasks")
|
||||
+2
@@ -27,6 +27,7 @@ from deerflow.persistence.feedback.model import FeedbackRow
|
||||
from deerflow.persistence.llm_metrics.model import LlmCallMetricRow
|
||||
from deerflow.persistence.models.run_event import RunEventRow
|
||||
from deerflow.persistence.notifications.model import NotificationRow
|
||||
from deerflow.persistence.parallel_agent_tasks.model import ParallelAgentTaskRow
|
||||
from deerflow.persistence.recommended_questions.model import RecommendedQuestionRow
|
||||
from deerflow.persistence.roundtable_chains.model import RoundtableChainRow
|
||||
from deerflow.persistence.roundtable_draft_shares.model import RoundtableDraftShareRow
|
||||
@@ -62,6 +63,7 @@ __all__ = [
|
||||
"AdminLeaderboardDailyStatRow",
|
||||
"LlmCallMetricRow",
|
||||
"NotificationRow",
|
||||
"ParallelAgentTaskRow",
|
||||
"RecommendedQuestionRow",
|
||||
"RoundtableChainRow",
|
||||
"RoundtableDraftRow",
|
||||
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
"""Parallel multi-agent task persistence."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from deerflow.persistence.parallel_agent_tasks.model import ParallelAgentTaskRow
|
||||
from deerflow.persistence.parallel_agent_tasks.sql import ParallelAgentTaskRepository
|
||||
|
||||
__all__ = [
|
||||
"ParallelAgentTaskRepository",
|
||||
"ParallelAgentTaskRow",
|
||||
"make_parallel_agent_task_store",
|
||||
]
|
||||
|
||||
|
||||
def make_parallel_agent_task_store(session_factory):
|
||||
if session_factory is None:
|
||||
return None
|
||||
return ParallelAgentTaskRepository(session_factory)
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
"""ORM model for parallel multi-agent task records.
|
||||
|
||||
A "parallel-agent task" is one saved run of the standalone *parallel multi-agent*
|
||||
panel (the Vue3 embeddable component): a task name + brief dispatched to several
|
||||
agents in parallel, plus the resulting chat/message list. Persisted per user so it
|
||||
follows the account across devices/sessions — same model as ``roundtable_drafts``.
|
||||
|
||||
``agents`` and ``messages`` are stored as JSON serialized into ``PortableLongText``
|
||||
(MySQL ``LONGTEXT``) rather than ``PortableJSON`` (MySQL ``TEXT``, 64 KB cap),
|
||||
because the parallel transcript can carry several agents' full deliveries that
|
||||
easily exceed 64 KB — mirroring ``roundtable_drafts``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import String
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.types import BeijingDateTime, PortableLongText
|
||||
|
||||
|
||||
class ParallelAgentTaskRow(Base):
|
||||
__tablename__ = "parallel_agent_tasks"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
user_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
# Task name (用户输入的任务名称) shown on the history list.
|
||||
title: Mapped[str] = mapped_column(String(512), nullable=False, default="")
|
||||
# Task brief / 简介 — the free-text task description.
|
||||
brief: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
# JSON: snapshot of the agents dispatched, [{"agent_id", "name", "avatarType"?}].
|
||||
agents: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
# JSON: the full message/dialogue list (mirrors the frontend Dialogue[] shape).
|
||||
messages: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC)
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
BeijingDateTime(),
|
||||
nullable=False,
|
||||
default=lambda: datetime.now(UTC),
|
||||
onupdate=lambda: datetime.now(UTC),
|
||||
)
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
"""SQLAlchemy-backed store for parallel multi-agent task records.
|
||||
|
||||
``agents`` / ``messages`` JSON columns are stored as serialized strings in
|
||||
``PortableLongText`` and round-tripped here with ``json.dumps`` / ``json.loads``
|
||||
— same approach as ``roundtable_drafts`` / ``roundtable_chains``.
|
||||
|
||||
All write/read paths are scoped by ``user_id`` for ownership isolation; a row
|
||||
that belongs to another user is treated as not found.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.parallel_agent_tasks.model import ParallelAgentTaskRow
|
||||
|
||||
_UNSET = object()
|
||||
|
||||
|
||||
def _loads(value: Any) -> Any:
|
||||
if not isinstance(value, str):
|
||||
return value
|
||||
if not value:
|
||||
return None
|
||||
try:
|
||||
return json.loads(value)
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def _dumps(value: Any) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
return json.dumps(value, ensure_ascii=False)
|
||||
|
||||
|
||||
class ParallelAgentTaskRepository:
|
||||
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
|
||||
self._sf = session_factory
|
||||
|
||||
@staticmethod
|
||||
def _to_dict(row: ParallelAgentTaskRow, *, with_messages: bool = True) -> dict[str, Any]:
|
||||
data: dict[str, Any] = {
|
||||
"id": row.id,
|
||||
"title": row.title,
|
||||
"brief": row.brief,
|
||||
"agents": _loads(row.agents) or [],
|
||||
"created_at": row.created_at.isoformat() if isinstance(row.created_at, datetime) else row.created_at,
|
||||
"updated_at": row.updated_at.isoformat() if isinstance(row.updated_at, datetime) else row.updated_at,
|
||||
}
|
||||
if with_messages:
|
||||
data["messages"] = _loads(row.messages) or []
|
||||
return data
|
||||
|
||||
async def list_tasks(self, user_id: str, *, limit: int = 200) -> list[dict[str, Any]]:
|
||||
"""列出当前用户的任务(轻量元信息,不含 messages,供历史列表)。"""
|
||||
stmt = (
|
||||
select(ParallelAgentTaskRow)
|
||||
.where(ParallelAgentTaskRow.user_id == user_id)
|
||||
.order_by(ParallelAgentTaskRow.updated_at.desc())
|
||||
.limit(limit)
|
||||
)
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(stmt)
|
||||
return [self._to_dict(row, with_messages=False) for row in result.scalars()]
|
||||
|
||||
async def get_task(self, task_id: str, user_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(ParallelAgentTaskRow, task_id)
|
||||
if row is None or row.user_id != user_id:
|
||||
return None
|
||||
return self._to_dict(row)
|
||||
|
||||
async def create_task(self, user_id: str, data: dict[str, Any]) -> dict[str, Any]:
|
||||
now = datetime.now(UTC)
|
||||
row = ParallelAgentTaskRow(
|
||||
id=data["id"],
|
||||
user_id=user_id,
|
||||
title=(data.get("title") or "").strip(),
|
||||
brief=(data.get("brief") or None),
|
||||
agents=_dumps(data.get("agents") or []),
|
||||
messages=_dumps(data.get("messages") or []),
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
async with self._sf() as session:
|
||||
session.add(row)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return self._to_dict(row)
|
||||
|
||||
async def update_task(
|
||||
self,
|
||||
task_id: str,
|
||||
user_id: str,
|
||||
*,
|
||||
title=_UNSET,
|
||||
brief=_UNSET,
|
||||
agents=_UNSET,
|
||||
messages=_UNSET,
|
||||
) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(ParallelAgentTaskRow, task_id)
|
||||
if row is None or row.user_id != user_id:
|
||||
return None
|
||||
if title is not _UNSET and title is not None:
|
||||
row.title = str(title).strip()
|
||||
if brief is not _UNSET:
|
||||
row.brief = brief or None
|
||||
if agents is not _UNSET:
|
||||
row.agents = _dumps(agents)
|
||||
if messages is not _UNSET:
|
||||
row.messages = _dumps(messages)
|
||||
row.updated_at = datetime.now(UTC)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return self._to_dict(row)
|
||||
|
||||
async def delete_task(self, task_id: str, user_id: str) -> bool:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(ParallelAgentTaskRow, task_id)
|
||||
if row is None or row.user_id != user_id:
|
||||
return False
|
||||
await session.delete(row)
|
||||
await session.commit()
|
||||
return True
|
||||
Reference in new issue
Block a user