sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)

This commit is contained in:
gozeros committed 2026-10-21 14:51:31 +08:00
1 parent 4f7f07504a
commit 8bc8b0e082
1651 files changed
+278599 -11416

No files matched your search

@@ -14,7 +14,7 @@ import json
from datetime import UTC, datetime
from typing import Any
from sqlalchemy import select
from sqlalchemy import select, update
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from deerflow.persistence.parallel_agent_tasks.model import ParallelAgentTaskRow
@@ -57,17 +57,40 @@ class ParallelAgentTaskRepository:
data["messages"] = _loads(row.messages) or []
return data
async def list_tasks(self, user_id: str, *, limit: int = 200) -> list[dict[str, Any]]:
"""列出当前用户的任务(轻量元信息,不含 messages,供历史列表)。"""
async def list_tasks(
self,
user_id: str,
*,
limit: int = 200,
offset: int = 0,
id_prefix: str | None = None,
include_messages: bool = False,
) -> list[dict[str, Any]]:
"""List the current user's tasks, optionally including saved snapshots.
History-list callers keep the lightweight default. Server-side
projections such as the artifact library can opt into ``messages`` in
one query instead of issuing an N+1 ``get_task`` sequence. ``id_prefix``
and ``offset`` allow those projections to page only their own task
namespace without loading unrelated, potentially large transcripts.
"""
stmt = select(ParallelAgentTaskRow).where(ParallelAgentTaskRow.user_id == user_id)
if id_prefix:
stmt = stmt.where(ParallelAgentTaskRow.id.startswith(id_prefix))
stmt = (
select(ParallelAgentTaskRow)
.where(ParallelAgentTaskRow.user_id == user_id)
.order_by(ParallelAgentTaskRow.updated_at.desc())
stmt.order_by(
ParallelAgentTaskRow.updated_at.desc(),
ParallelAgentTaskRow.id.asc(),
)
.offset(offset)
.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()]
return [
self._to_dict(row, with_messages=include_messages)
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:
@@ -103,22 +126,40 @@ class ParallelAgentTaskRepository:
brief=_UNSET,
agents=_UNSET,
messages=_UNSET,
expected_updated_at: datetime | str | None = None,
) -> 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:
expected_revision = expected_updated_at
if isinstance(expected_revision, str):
try:
expected_revision = datetime.fromisoformat(expected_revision.replace("Z", "+00:00"))
except ValueError:
return None
values: dict[str, Any] = {"updated_at": datetime.now(UTC)}
if title is not _UNSET and title is not None:
values["title"] = str(title).strip()
if brief is not _UNSET:
values["brief"] = brief or None
if agents is not _UNSET:
values["agents"] = _dumps(agents)
if messages is not _UNSET:
values["messages"] = _dumps(messages)
async with self._sf() as session:
statement = update(ParallelAgentTaskRow).where(
ParallelAgentTaskRow.id == task_id,
ParallelAgentTaskRow.user_id == user_id,
)
if expected_revision is not None:
statement = statement.where(ParallelAgentTaskRow.updated_at == expected_revision)
result = await session.execute(statement.values(**values))
if result.rowcount != 1:
await session.rollback()
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)
row = await session.get(ParallelAgentTaskRow, task_id)
if row is None:
return None
return self._to_dict(row)
async def delete_task(self, task_id: str, user_id: str) -> bool: