sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)
This commit is contained in:
1 parent
4f7f07504a
commit
8bc8b0e082
1651 files changed
+278599
-11416
No files matched your search
+48
@@ -0,0 +1,48 @@
|
||||
"""External-task multi-agent scheduled analysis persistence (任务分析定时)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from deerflow.persistence.task_analysis.base import TaskAnalysisStore
|
||||
from deerflow.persistence.task_analysis.data_blocks import (
|
||||
DEFAULT_DATA_BLOCK_KEYS,
|
||||
DEFAULT_DATA_BLOCKS,
|
||||
merge_payload,
|
||||
)
|
||||
from deerflow.persistence.task_analysis.memory import MemoryTaskAnalysisStore
|
||||
from deerflow.persistence.task_analysis.model import (
|
||||
TaskAnalysisBindingRow,
|
||||
TaskAnalysisContentVersionRow,
|
||||
TaskAnalysisRunMessageRow,
|
||||
TaskAnalysisRunRow,
|
||||
TaskAnalysisTaskRow,
|
||||
TaskDataBlockDefRow,
|
||||
TaskDataBlockRow,
|
||||
TaskDataPeriodRow,
|
||||
TaskDataSubmissionRow,
|
||||
)
|
||||
from deerflow.persistence.task_analysis.sql import TaskAnalysisRepository
|
||||
|
||||
__all__ = [
|
||||
"DEFAULT_DATA_BLOCK_KEYS",
|
||||
"DEFAULT_DATA_BLOCKS",
|
||||
"MemoryTaskAnalysisStore",
|
||||
"TaskAnalysisBindingRow",
|
||||
"TaskAnalysisContentVersionRow",
|
||||
"TaskAnalysisRepository",
|
||||
"TaskAnalysisRunMessageRow",
|
||||
"TaskAnalysisRunRow",
|
||||
"TaskAnalysisStore",
|
||||
"TaskAnalysisTaskRow",
|
||||
"TaskDataBlockDefRow",
|
||||
"TaskDataBlockRow",
|
||||
"TaskDataPeriodRow",
|
||||
"TaskDataSubmissionRow",
|
||||
"make_task_analysis_store",
|
||||
"merge_payload",
|
||||
]
|
||||
|
||||
|
||||
def make_task_analysis_store(session_factory):
|
||||
if session_factory is None:
|
||||
return MemoryTaskAnalysisStore()
|
||||
return TaskAnalysisRepository(session_factory)
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
"""Abstract store API for the external-task scheduled analysis feature."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
|
||||
class TaskAnalysisStore(ABC):
|
||||
"""Tasks / bindings / runs / run-messages / content versions."""
|
||||
|
||||
# ── external tasks ──────────────────────────────────────────────────────
|
||||
|
||||
@abstractmethod
|
||||
async def create_task(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Create a task. Raises ValueError when source_task_id already exists."""
|
||||
|
||||
@abstractmethod
|
||||
async def get_task(self, task_id: str) -> dict[str, Any] | None: ...
|
||||
|
||||
@abstractmethod
|
||||
async def get_task_by_source(self, source_task_id: str) -> dict[str, Any] | None: ...
|
||||
|
||||
@abstractmethod
|
||||
async def list_tasks(self) -> list[dict[str, Any]]:
|
||||
"""All tasks newest first, each augmented with its binding (if any)."""
|
||||
|
||||
@abstractmethod
|
||||
async def delete_task_cascade(self, task_id: str) -> bool:
|
||||
"""Delete task + binding + runs + messages + versions. True if existed."""
|
||||
|
||||
# ── bindings ────────────────────────────────────────────────────────────
|
||||
|
||||
@abstractmethod
|
||||
async def get_binding(self, external_task_id: str) -> dict[str, Any] | None: ...
|
||||
|
||||
@abstractmethod
|
||||
async def get_binding_by_id(self, binding_id: str) -> dict[str, Any] | None: ...
|
||||
|
||||
@abstractmethod
|
||||
async def upsert_binding(self, external_task_id: str, data: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Create-or-update the 1:1 binding for a task (refresh chain snapshot)."""
|
||||
|
||||
@abstractmethod
|
||||
async def delete_binding(self, external_task_id: str) -> bool: ...
|
||||
|
||||
@abstractmethod
|
||||
async def due_bindings(self, now: datetime, *, limit: int = 20) -> list[dict[str, Any]]:
|
||||
"""Enabled bindings with next_run_at <= now (execution candidates)."""
|
||||
|
||||
@abstractmethod
|
||||
async def update_binding(self, binding_id: str, patch: dict[str, Any]) -> dict[str, Any] | None:
|
||||
"""Patch mutable fields (next_run_at / last_status / direction_notes...)."""
|
||||
|
||||
@abstractmethod
|
||||
async def last_bound_chain_id(self) -> str | None:
|
||||
"""Most recently updated binding's chain id (default option for create API)."""
|
||||
|
||||
# ── runs ────────────────────────────────────────────────────────────────
|
||||
|
||||
@abstractmethod
|
||||
async def create_run_once(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Idempotent create: same binding_id + scheduled_for returns existing row."""
|
||||
|
||||
@abstractmethod
|
||||
async def get_run(self, run_id: str) -> dict[str, Any] | None: ...
|
||||
|
||||
@abstractmethod
|
||||
async def list_runs(self, external_task_id: str, *, limit: int = 50) -> list[dict[str, Any]]: ...
|
||||
|
||||
@abstractmethod
|
||||
async def update_run(self, run_id: str, patch: dict[str, Any]) -> dict[str, Any] | None: ...
|
||||
|
||||
@abstractmethod
|
||||
async def stuck_runs(self, older_than: datetime, *, limit: int = 20) -> list[dict[str, Any]]:
|
||||
"""Runs still pending/running past ``older_than`` (watchdog recovery)."""
|
||||
|
||||
# ── run messages ────────────────────────────────────────────────────────
|
||||
|
||||
@abstractmethod
|
||||
async def append_run_messages(self, run_id: str, messages: list[dict[str, Any]]) -> int:
|
||||
"""Append batch of {seat_agent_id, stage_index, event_type, content}."""
|
||||
|
||||
@abstractmethod
|
||||
async def list_run_messages(
|
||||
self, run_id: str, *, seat: str | None = None, limit: int = 500
|
||||
) -> list[dict[str, Any]]: ...
|
||||
|
||||
# ── content versions (timeline) ─────────────────────────────────────────
|
||||
|
||||
@abstractmethod
|
||||
async def create_version(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
"""One version per run (idempotent on run_id: replaces existing)."""
|
||||
|
||||
@abstractmethod
|
||||
async def list_versions(self, external_task_id: str, *, limit: int = 50, offset: int = 0) -> list[dict[str, Any]]:
|
||||
"""Timeline versions newest first (对方详情接口数据源)."""
|
||||
|
||||
# ── data delivery: block defs / periods / submissions ────────────────────
|
||||
|
||||
@abstractmethod
|
||||
async def ensure_block_defs(self, task_id: str) -> list[dict[str, Any]]:
|
||||
"""Seed the 5 default block defs when absent (create/legacy tasks); return ordered."""
|
||||
|
||||
@abstractmethod
|
||||
async def list_block_defs(self, task_id: str) -> list[dict[str, Any]]:
|
||||
"""Block defs ordered by sort_order (empty list when never seeded)."""
|
||||
|
||||
@abstractmethod
|
||||
async def update_block_defs(self, task_id: str, updates: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
"""Patch title/sort_order per block_key. ValueError on unknown block_key."""
|
||||
|
||||
@abstractmethod
|
||||
async def open_period(
|
||||
self,
|
||||
task_id: str,
|
||||
*,
|
||||
source: str,
|
||||
run_id: str | None = None,
|
||||
opened_at: datetime | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Append a new timeline period (period_index = max+1). Idempotent on run_id.
|
||||
|
||||
``opened_at`` 默认取当前时间;演示/回填场景可显式指定刻度时刻。
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def get_period(self, task_id: str, period_index: int) -> dict[str, Any] | None: ...
|
||||
|
||||
@abstractmethod
|
||||
async def current_period(self, task_id: str) -> dict[str, Any] | None:
|
||||
"""Latest period (submissions always land here)."""
|
||||
|
||||
@abstractmethod
|
||||
async def list_periods(self, task_id: str, *, limit: int = 500) -> list[dict[str, Any]]:
|
||||
"""Periods ascending by period_index (timeline ticks)."""
|
||||
|
||||
@abstractmethod
|
||||
async def append_submission(
|
||||
self,
|
||||
task_id: str,
|
||||
period_index: int,
|
||||
block_key: str,
|
||||
payload: dict[str, Any],
|
||||
*,
|
||||
submitted_by: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Append to the submission log (seq per task+period+block)."""
|
||||
|
||||
@abstractmethod
|
||||
async def list_submissions(
|
||||
self, task_id: str, period_index: int, block_key: str, *, limit: int = 500
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Submission log for one block of one period ordered by seq (merge source)."""
|
||||
|
||||
@abstractmethod
|
||||
async def upsert_period_block(
|
||||
self, task_id: str, period_index: int, block_key: str, payload: dict[str, Any]
|
||||
) -> dict[str, Any]:
|
||||
"""Write the merged payload for (task, period, block); bumps updated_at."""
|
||||
|
||||
@abstractmethod
|
||||
async def list_task_blocks(self, task_id: str, *, limit: int = 2000) -> list[dict[str, Any]]:
|
||||
"""All merged block rows of a task ({block_key, period_index, payload, updated_at})."""
|
||||
|
||||
@abstractmethod
|
||||
async def get_setting(self, key: str) -> str | None:
|
||||
"""Read one module setting value (``None`` when absent)."""
|
||||
|
||||
@abstractmethod
|
||||
async def set_setting(self, key: str, value: str) -> None:
|
||||
"""Upsert one module setting value."""
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
"""Constants and pure helpers for the scheduled data-delivery blocks.
|
||||
|
||||
块 key 定死 ``block_1``~``block_6``(已定稿口径,不做席位↔块映射);payload 不做
|
||||
schema 校验(schema 会变),只要求是 JSON 对象。合并规则:顶层 key 合并、同 key
|
||||
后写覆盖 —— 多个智能体可各写一个块的不同部分,一个智能体也可写多个块。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
# (block_key, 默认标题);建任务时自动种 6 条,标题/排序后续可经管理端修改。
|
||||
DEFAULT_DATA_BLOCKS: tuple[tuple[str, str], ...] = (
|
||||
("block_1", "数据块一"),
|
||||
("block_2", "数据块二"),
|
||||
("block_3", "数据块三"),
|
||||
("block_4", "数据块四"),
|
||||
("block_5", "数据块五"),
|
||||
("block_6", "数据块六"),
|
||||
)
|
||||
|
||||
DEFAULT_DATA_BLOCK_KEYS: tuple[str, ...] = tuple(key for key, _title in DEFAULT_DATA_BLOCKS)
|
||||
|
||||
# 期(时间轴刻度)触发来源。
|
||||
PERIOD_TRIGGER_CRON = "cron"
|
||||
PERIOD_TRIGGER_MANUAL = "manual"
|
||||
PERIOD_TRIGGER_EXTERNAL = "external"
|
||||
|
||||
|
||||
def default_block_defs() -> list[dict[str, Any]]:
|
||||
return [
|
||||
{"block_key": key, "title": title, "sort_order": index}
|
||||
for index, (key, title) in enumerate(DEFAULT_DATA_BLOCKS, start=1)
|
||||
]
|
||||
|
||||
|
||||
def is_default_block_key(block_key: str) -> bool:
|
||||
return block_key in DEFAULT_DATA_BLOCK_KEYS
|
||||
|
||||
|
||||
def merge_payload(base: dict[str, Any], incoming: dict[str, Any]) -> dict[str, Any]:
|
||||
"""顶层 key 合并、同 key 后写覆盖(纯函数,返回新 dict)。"""
|
||||
merged = dict(base)
|
||||
merged.update(incoming)
|
||||
return merged
|
||||
+467
@@ -0,0 +1,467 @@
|
||||
"""In-memory task-analysis store (no DB configured / tests).
|
||||
|
||||
Also serves as the behavioural reference for the SQL repository: field
|
||||
normalisation (JSON encode/decode, ISO datetime) lives in ``_normalize_*``
|
||||
helpers that the SQL layer mirrors.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from deerflow.persistence.task_analysis.base import TaskAnalysisStore
|
||||
|
||||
|
||||
def _iso(value: datetime | str | None) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
if isinstance(value, datetime):
|
||||
return value.isoformat()
|
||||
return str(value)
|
||||
|
||||
|
||||
def _parse_dt(value: Any) -> datetime | None:
|
||||
if value is None or isinstance(value, datetime):
|
||||
return value
|
||||
try:
|
||||
return datetime.fromisoformat(str(value))
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def _dump(value: Any) -> str | None:
|
||||
return None if value is None else json.dumps(value, ensure_ascii=False)
|
||||
|
||||
|
||||
def _load(value: str | None, default: Any) -> Any:
|
||||
if value is None or value == "":
|
||||
return default
|
||||
try:
|
||||
return json.loads(value)
|
||||
except (TypeError, ValueError):
|
||||
return default
|
||||
|
||||
|
||||
class MemoryTaskAnalysisStore(TaskAnalysisStore):
|
||||
def __init__(self) -> None:
|
||||
self._tasks: dict[str, dict[str, Any]] = {}
|
||||
self._settings: dict[str, str] = {}
|
||||
self._bindings: dict[str, dict[str, Any]] = {} # keyed by external_task_id
|
||||
self._runs: dict[str, dict[str, Any]] = {}
|
||||
self._messages: list[dict[str, Any]] = []
|
||||
self._versions: dict[str, dict[str, Any]] = {} # keyed by run_id
|
||||
# 数据交付:块定义 / 期刻度 / 合并块 / 提交流水。
|
||||
self._block_defs: dict[str, dict[str, dict[str, Any]]] = {} # task_id -> block_key -> def
|
||||
self._periods: dict[str, list[dict[str, Any]]] = {} # task_id -> ordered by period_index
|
||||
self._task_blocks: dict[tuple[str, int, str], dict[str, Any]] = {} # (task, period, block)
|
||||
self._submissions: dict[tuple[str, int, str], list[dict[str, Any]]] = {}
|
||||
self._seq = 0
|
||||
# 绑定最近写入序号(时钟精度不足时仍能稳定判定“最近一次绑定”)。
|
||||
self._binding_rev = 0
|
||||
|
||||
def _next_id(self, prefix: str) -> str:
|
||||
self._seq += 1
|
||||
return f"{prefix}-{self._seq:06d}"
|
||||
|
||||
# ── data delivery helpers ───────────────────────────────────────────────
|
||||
|
||||
def _seed_block_defs(self, task_id: str) -> None:
|
||||
from deerflow.persistence.task_analysis.data_blocks import default_block_defs
|
||||
|
||||
existing = self._block_defs.setdefault(task_id, {})
|
||||
now = datetime.now(UTC).isoformat()
|
||||
for definition in default_block_defs():
|
||||
if definition["block_key"] in existing:
|
||||
continue
|
||||
existing[definition["block_key"]] = {
|
||||
"id": self._next_id("tdbd"),
|
||||
"task_id": task_id,
|
||||
**definition,
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
}
|
||||
|
||||
def _ordered_defs(self, task_id: str) -> list[dict[str, Any]]:
|
||||
rows = list(self._block_defs.get(task_id, {}).values())
|
||||
rows.sort(key=lambda d: (d.get("sort_order", 0), d.get("block_key", "")))
|
||||
return [dict(row) for row in rows]
|
||||
|
||||
def _period_rows(self, task_id: str) -> list[dict[str, Any]]:
|
||||
rows = list(self._periods.get(task_id, []))
|
||||
rows.sort(key=lambda p: p["period_index"])
|
||||
return rows
|
||||
|
||||
# ── tasks ───────────────────────────────────────────────────────────────
|
||||
|
||||
async def create_task(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
source = str(data["source_task_id"])
|
||||
if await self.get_task_by_source(source) is not None:
|
||||
raise ValueError(f"source_task_id already exists: {source}")
|
||||
now = datetime.now(UTC).isoformat()
|
||||
task = {
|
||||
"id": self._next_id("ta"),
|
||||
"source_task_id": source,
|
||||
"name": str(data.get("name") or ""),
|
||||
"description": data.get("description"),
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
}
|
||||
self._tasks[task["id"]] = dict(task)
|
||||
# 建任务即种 6 条数据块定义(块 key 定死,标题后续可改)。
|
||||
self._seed_block_defs(task["id"])
|
||||
return dict(task)
|
||||
|
||||
async def get_task(self, task_id: str) -> dict[str, Any] | None:
|
||||
task = self._tasks.get(task_id)
|
||||
return dict(task) if task else None
|
||||
|
||||
async def get_task_by_source(self, source_task_id: str) -> dict[str, Any] | None:
|
||||
for task in self._tasks.values():
|
||||
if task["source_task_id"] == source_task_id:
|
||||
return dict(task)
|
||||
return None
|
||||
|
||||
async def list_tasks(self) -> list[dict[str, Any]]:
|
||||
out: list[dict[str, Any]] = []
|
||||
for task in sorted(self._tasks.values(), key=lambda t: t["created_at"], reverse=True):
|
||||
row = dict(task)
|
||||
row["binding"] = dict(self._bindings[task["id"]]) if task["id"] in self._bindings else None
|
||||
out.append(row)
|
||||
return out
|
||||
|
||||
async def delete_task_cascade(self, task_id: str) -> bool:
|
||||
if task_id not in self._tasks:
|
||||
return False
|
||||
del self._tasks[task_id]
|
||||
self._bindings.pop(task_id, None)
|
||||
run_ids = [run_id for run_id, run in self._runs.items() if run["external_task_id"] == task_id]
|
||||
for run_id in run_ids:
|
||||
del self._runs[run_id]
|
||||
self._versions.pop(run_id, None)
|
||||
self._messages = [m for m in self._messages if m["run_id"] not in set(run_ids)]
|
||||
self._block_defs.pop(task_id, None)
|
||||
self._periods.pop(task_id, None)
|
||||
self._task_blocks = {
|
||||
key: row for key, row in self._task_blocks.items() if key[0] != task_id
|
||||
}
|
||||
self._submissions = {
|
||||
key: rows for key, rows in self._submissions.items() if key[0] != task_id
|
||||
}
|
||||
return True
|
||||
|
||||
# ── bindings ────────────────────────────────────────────────────────────
|
||||
|
||||
async def get_binding(self, external_task_id: str) -> dict[str, Any] | None:
|
||||
binding = self._bindings.get(external_task_id)
|
||||
return dict(binding) if binding else None
|
||||
|
||||
async def get_binding_by_id(self, binding_id: str) -> dict[str, Any] | None:
|
||||
for binding in self._bindings.values():
|
||||
if binding["id"] == binding_id:
|
||||
return dict(binding)
|
||||
return None
|
||||
|
||||
async def upsert_binding(self, external_task_id: str, data: dict[str, Any]) -> dict[str, Any]:
|
||||
existing = self._bindings.get(external_task_id)
|
||||
now = datetime.now(UTC).isoformat()
|
||||
self._binding_rev += 1
|
||||
if existing is None:
|
||||
row = {
|
||||
"id": self._next_id("tab"),
|
||||
"external_task_id": external_task_id,
|
||||
"chain_id": str(data["chain_id"]),
|
||||
"chain_snapshot": data.get("chain_snapshot") or {},
|
||||
"cron_expr": str(data.get("cron_expr") or ""),
|
||||
"timezone": str(data.get("timezone") or "Asia/Shanghai"),
|
||||
"schedule_type": str(data.get("schedule_type") or "cron"),
|
||||
"enabled": bool(data.get("enabled", True)),
|
||||
"next_run_at": _iso(data.get("next_run_at")),
|
||||
"coordinator_prompt": data.get("coordinator_prompt"),
|
||||
"direction_notes": list(data.get("direction_notes") or []),
|
||||
"last_run_at": None,
|
||||
"last_status": None,
|
||||
"model": data.get("model"),
|
||||
"created_by": data.get("created_by"),
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
"_rev": self._binding_rev,
|
||||
}
|
||||
else:
|
||||
row = dict(existing)
|
||||
row["_rev"] = self._binding_rev
|
||||
row.update({k: v for k, v in data.items() if k in {
|
||||
"chain_id", "chain_snapshot", "cron_expr", "timezone", "schedule_type", "enabled",
|
||||
"next_run_at", "coordinator_prompt", "model",
|
||||
}})
|
||||
if "direction_notes" in data:
|
||||
row["direction_notes"] = list(data["direction_notes"])
|
||||
row["updated_at"] = now
|
||||
self._bindings[external_task_id] = row
|
||||
return dict(row)
|
||||
|
||||
async def delete_binding(self, external_task_id: str) -> bool:
|
||||
return self._bindings.pop(external_task_id, None) is not None
|
||||
|
||||
async def due_bindings(self, now: datetime, *, limit: int = 20) -> list[dict[str, Any]]:
|
||||
due = []
|
||||
for binding in self._bindings.values():
|
||||
if not binding.get("enabled"):
|
||||
continue
|
||||
next_at = _parse_dt(binding.get("next_run_at"))
|
||||
if next_at is not None and next_at <= now:
|
||||
due.append(binding)
|
||||
due.sort(key=lambda b: b.get("next_run_at") or "")
|
||||
return [dict(b) for b in due[:limit]]
|
||||
|
||||
async def update_binding(self, binding_id: str, patch: dict[str, Any]) -> dict[str, Any] | None:
|
||||
for key, binding in self._bindings.items():
|
||||
if binding["id"] == binding_id:
|
||||
binding.update({k: v for k, v in patch.items() if k != "id"})
|
||||
binding["updated_at"] = datetime.now(UTC).isoformat()
|
||||
return dict(binding)
|
||||
return None
|
||||
|
||||
async def last_bound_chain_id(self) -> str | None:
|
||||
if not self._bindings:
|
||||
return None
|
||||
latest = max(self._bindings.values(), key=lambda b: b.get("_rev", 0))
|
||||
return latest.get("chain_id")
|
||||
|
||||
# ── runs ────────────────────────────────────────────────────────────────
|
||||
|
||||
async def create_run_once(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
binding_id = data["binding_id"]
|
||||
scheduled_for = _parse_dt(data["scheduled_for"]) or datetime.now(UTC)
|
||||
for run in self._runs.values():
|
||||
if run["binding_id"] == binding_id and run["scheduled_for"] == scheduled_for.isoformat():
|
||||
return dict(run)
|
||||
now = datetime.now(UTC).isoformat()
|
||||
row = {
|
||||
"id": self._next_id("tar"),
|
||||
"binding_id": binding_id,
|
||||
"external_task_id": str(data["external_task_id"]),
|
||||
"trigger": str(data.get("trigger") or "schedule"),
|
||||
"scheduled_for": scheduled_for.isoformat(),
|
||||
"started_at": now,
|
||||
"finished_at": None,
|
||||
"status": str(data.get("status") or "pending"),
|
||||
"stages_snapshot": data.get("stages_snapshot") or [],
|
||||
"stage_goals_snapshot": data.get("stage_goals_snapshot") or [],
|
||||
"seat_names_snapshot": data.get("seat_names_snapshot") or {},
|
||||
"seat_results": data.get("seat_results") or {},
|
||||
"error": data.get("error"),
|
||||
"retry_count": 0,
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
}
|
||||
self._runs[row["id"]] = row
|
||||
return dict(row)
|
||||
|
||||
async def get_run(self, run_id: str) -> dict[str, Any] | None:
|
||||
run = self._runs.get(run_id)
|
||||
return dict(run) if run else None
|
||||
|
||||
async def list_runs(self, external_task_id: str, *, limit: int = 50) -> list[dict[str, Any]]:
|
||||
rows = [r for r in self._runs.values() if r["external_task_id"] == external_task_id]
|
||||
rows.sort(key=lambda r: r["started_at"], reverse=True)
|
||||
return [dict(r) for r in rows[:limit]]
|
||||
|
||||
async def update_run(self, run_id: str, patch: dict[str, Any]) -> dict[str, Any] | None:
|
||||
run = self._runs.get(run_id)
|
||||
if run is None:
|
||||
return None
|
||||
run.update({k: v for k, v in patch.items() if k not in {"id", "binding_id"}})
|
||||
run["updated_at"] = datetime.now(UTC).isoformat()
|
||||
return dict(run)
|
||||
|
||||
async def stuck_runs(self, older_than: datetime, *, limit: int = 20) -> list[dict[str, Any]]:
|
||||
out = []
|
||||
for run in self._runs.values():
|
||||
if run["status"] in {"pending", "running"}:
|
||||
started = _parse_dt(run["started_at"])
|
||||
if started is not None and started < older_than:
|
||||
out.append(run)
|
||||
out.sort(key=lambda r: r["started_at"])
|
||||
return [dict(r) for r in out[:limit]]
|
||||
|
||||
# ── messages ────────────────────────────────────────────────────────────
|
||||
|
||||
async def append_run_messages(self, run_id: str, messages: list[dict[str, Any]]) -> int:
|
||||
base_seq = sum(1 for m in self._messages if m["run_id"] == run_id)
|
||||
added = 0
|
||||
for index, message in enumerate(messages):
|
||||
self._messages.append({
|
||||
"id": self._next_id("tam"),
|
||||
"run_id": run_id,
|
||||
"seq": base_seq + index,
|
||||
"seat_agent_id": str(message.get("seat_agent_id") or ""),
|
||||
"stage_index": int(message.get("stage_index") or 0),
|
||||
"event_type": str(message.get("event_type") or "llm.ai"),
|
||||
"content": message.get("content"),
|
||||
"created_at": datetime.now(UTC).isoformat(),
|
||||
})
|
||||
added += 1
|
||||
return added
|
||||
|
||||
async def list_run_messages(
|
||||
self, run_id: str, *, seat: str | None = None, limit: int = 500
|
||||
) -> list[dict[str, Any]]:
|
||||
rows = [m for m in self._messages if m["run_id"] == run_id]
|
||||
if seat is not None:
|
||||
rows = [m for m in rows if m["seat_agent_id"] == seat]
|
||||
rows.sort(key=lambda m: m["seq"])
|
||||
return [dict(m) for m in rows[:limit]]
|
||||
|
||||
# ── versions ────────────────────────────────────────────────────────────
|
||||
|
||||
async def create_version(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
run_id = str(data["run_id"])
|
||||
row = {
|
||||
"id": self._next_id("tav"),
|
||||
"external_task_id": str(data["external_task_id"]),
|
||||
"run_id": run_id,
|
||||
"blocks": data.get("blocks") or {},
|
||||
"created_at": datetime.now(UTC).isoformat(),
|
||||
}
|
||||
self._versions[run_id] = row
|
||||
return dict(row)
|
||||
|
||||
async def list_versions(
|
||||
self, external_task_id: str, *, limit: int = 50, offset: int = 0
|
||||
) -> list[dict[str, Any]]:
|
||||
rows = [v for v in self._versions.values() if v["external_task_id"] == external_task_id]
|
||||
rows.sort(key=lambda v: v["created_at"], reverse=True)
|
||||
return [dict(v) for v in rows[offset : offset + limit]]
|
||||
|
||||
# ── data delivery: block defs / periods / submissions ────────────────────
|
||||
|
||||
async def ensure_block_defs(self, task_id: str) -> list[dict[str, Any]]:
|
||||
self._seed_block_defs(task_id)
|
||||
return self._ordered_defs(task_id)
|
||||
|
||||
async def list_block_defs(self, task_id: str) -> list[dict[str, Any]]:
|
||||
return self._ordered_defs(task_id)
|
||||
|
||||
async def update_block_defs(self, task_id: str, updates: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
definitions = self._block_defs.setdefault(task_id, {})
|
||||
now = datetime.now(UTC).isoformat()
|
||||
for update in updates:
|
||||
block_key = str(update.get("block_key") or "")
|
||||
row = definitions.get(block_key)
|
||||
if row is None:
|
||||
raise ValueError(f"未知数据块:{block_key}")
|
||||
if update.get("title") is not None:
|
||||
row["title"] = str(update["title"])
|
||||
if update.get("sort_order") is not None:
|
||||
row["sort_order"] = int(update["sort_order"])
|
||||
row["updated_at"] = now
|
||||
return self._ordered_defs(task_id)
|
||||
|
||||
async def open_period(
|
||||
self,
|
||||
task_id: str,
|
||||
*,
|
||||
source: str,
|
||||
run_id: str | None = None,
|
||||
opened_at: datetime | None = None,
|
||||
) -> dict[str, Any]:
|
||||
if run_id:
|
||||
for period in self._periods.get(task_id, []):
|
||||
if period.get("run_id") == run_id:
|
||||
return dict(period)
|
||||
rows = self._periods.setdefault(task_id, [])
|
||||
index = (rows[-1]["period_index"] if rows else 0) + 1
|
||||
now = datetime.now(UTC).isoformat()
|
||||
row = {
|
||||
"id": self._next_id("tdp"),
|
||||
"task_id": task_id,
|
||||
"period_index": index,
|
||||
"opened_at": _iso(opened_at) or now,
|
||||
"trigger": str(source or "cron"),
|
||||
"run_id": run_id,
|
||||
"created_at": now,
|
||||
}
|
||||
rows.append(row)
|
||||
return dict(row)
|
||||
|
||||
async def get_period(self, task_id: str, period_index: int) -> dict[str, Any] | None:
|
||||
for period in self._period_rows(task_id):
|
||||
if period["period_index"] == period_index:
|
||||
return dict(period)
|
||||
return None
|
||||
|
||||
async def current_period(self, task_id: str) -> dict[str, Any] | None:
|
||||
rows = self._period_rows(task_id)
|
||||
return dict(rows[-1]) if rows else None
|
||||
|
||||
async def list_periods(self, task_id: str, *, limit: int = 500) -> list[dict[str, Any]]:
|
||||
rows = self._period_rows(task_id)
|
||||
return [dict(p) for p in rows[-limit:]]
|
||||
|
||||
async def append_submission(
|
||||
self,
|
||||
task_id: str,
|
||||
period_index: int,
|
||||
block_key: str,
|
||||
payload: dict[str, Any],
|
||||
*,
|
||||
submitted_by: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
key = (task_id, period_index, block_key)
|
||||
rows = self._submissions.setdefault(key, [])
|
||||
row = {
|
||||
"id": self._next_id("tds"),
|
||||
"task_id": task_id,
|
||||
"period_index": period_index,
|
||||
"block_key": block_key,
|
||||
"payload": dict(payload),
|
||||
"submitted_by": submitted_by,
|
||||
"seq": len(rows),
|
||||
"created_at": datetime.now(UTC).isoformat(),
|
||||
}
|
||||
rows.append(row)
|
||||
return dict(row)
|
||||
|
||||
async def list_submissions(
|
||||
self, task_id: str, period_index: int, block_key: str, *, limit: int = 500
|
||||
) -> list[dict[str, Any]]:
|
||||
rows = list(self._submissions.get((task_id, period_index, block_key), []))
|
||||
rows.sort(key=lambda s: s["seq"])
|
||||
return [dict(s) for s in rows[:limit]]
|
||||
|
||||
async def upsert_period_block(
|
||||
self, task_id: str, period_index: int, block_key: str, payload: dict[str, Any]
|
||||
) -> dict[str, Any]:
|
||||
key = (task_id, period_index, block_key)
|
||||
now = datetime.now(UTC).isoformat()
|
||||
row = self._task_blocks.get(key)
|
||||
if row is None:
|
||||
row = {
|
||||
"id": self._next_id("tdb"),
|
||||
"task_id": task_id,
|
||||
"period_index": period_index,
|
||||
"block_key": block_key,
|
||||
"payload": dict(payload),
|
||||
"updated_at": now,
|
||||
"created_at": now,
|
||||
}
|
||||
self._task_blocks[key] = row
|
||||
else:
|
||||
row["payload"] = dict(payload)
|
||||
row["updated_at"] = now
|
||||
return dict(row)
|
||||
|
||||
async def list_task_blocks(self, task_id: str, *, limit: int = 2000) -> list[dict[str, Any]]:
|
||||
rows = [dict(row) for key, row in self._task_blocks.items() if key[0] == task_id]
|
||||
rows.sort(key=lambda r: (r["period_index"], r["block_key"]))
|
||||
return rows[:limit]
|
||||
|
||||
# ── settings ────────────────────────────────────────────────────────────
|
||||
|
||||
async def get_setting(self, key: str) -> str | None:
|
||||
return self._settings.get(key)
|
||||
|
||||
async def set_setting(self, key: str, value: str) -> None:
|
||||
self._settings[key] = value
|
||||
+217
@@ -0,0 +1,217 @@
|
||||
"""ORM models for the external-task multi-agent scheduled analysis (任务分析定时).
|
||||
|
||||
Feature summary (design doc in the frontend ``official-task-analysis`` module):
|
||||
an external system pushes tasks via our create API; each task binds 1:1 to a
|
||||
business-chain orchestration plus a cron schedule; the scheduler runs the chain
|
||||
stage-by-stage (seats within a stage in parallel) and persists per-seat process
|
||||
messages plus a content "timeline" version per run. Manual runs wrap the
|
||||
roundtable Step-2 coordinator Q&A and are stored with the same message table
|
||||
(``seat_agent_id`` distinguishes seats; the coordinator uses ``__coordinator__``).
|
||||
|
||||
Tables:
|
||||
- ``task_analysis_tasks`` — external tasks (source_task_id unique)
|
||||
- ``task_analysis_bindings`` — 1:1 binding (chain snapshot + cron + notes)
|
||||
- ``task_analysis_runs`` — executions (trigger: schedule | manual)
|
||||
- ``task_analysis_run_messages`` — per-seat process messages for a run
|
||||
- ``task_analysis_content_versions`` — one timeline version per run
|
||||
- ``task_data_block_defs`` — the 5 fixed data blocks per task (title/sort editable)
|
||||
- ``task_data_periods`` — data-delivery timeline ticks (period_index per task)
|
||||
- ``task_data_blocks`` — merged payload per (task, period, block)
|
||||
- ``task_data_submissions`` — append-only submission log feeding the merge
|
||||
|
||||
JSON payloads are stored as LONGTEXT-backed JSON strings (PortableLongText),
|
||||
mirroring the sibling roundtable persistence modules for headroom.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import Boolean, Integer, String, Text, UniqueConstraint
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.types import BeijingDateTime, PortableLongText
|
||||
|
||||
|
||||
def _utcnow() -> datetime:
|
||||
return datetime.now(UTC)
|
||||
|
||||
|
||||
class TaskAnalysisTaskRow(Base):
|
||||
__tablename__ = "task_analysis_tasks"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
# 对方系统任务号(创建接口传入;唯一)。
|
||||
source_task_id: Mapped[str] = mapped_column(String(128), nullable=False, unique=True, index=True)
|
||||
name: Mapped[str] = mapped_column(String(512), nullable=False, default="")
|
||||
# 任务简介:创建时落库的存量即执行时用的版本(不回拉对方接口)。
|
||||
description: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_utcnow)
|
||||
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_utcnow, onupdate=_utcnow)
|
||||
|
||||
|
||||
class TaskAnalysisBindingRow(Base):
|
||||
__tablename__ = "task_analysis_bindings"
|
||||
__table_args__ = (UniqueConstraint("external_task_id", name="uq_task_analysis_binding_task"),)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
external_task_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
# 引用 roundtable_chains.id;执行前校验,链缺失/停用 → 跳过并标记 chain_missing。
|
||||
chain_id: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
# 执行编排快照 JSON:{seats, stages, stage_goals, coordinator_prompt}。
|
||||
# 每次执行前刷新(跟随链最新版),历史 run 用自己的 stages_snapshot 回放。
|
||||
chain_snapshot: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
cron_expr: Mapped[str] = mapped_column(String(64), nullable=False, default="")
|
||||
timezone: Mapped[str] = mapped_column(String(64), nullable=False, default="Asia/Shanghai")
|
||||
# 执行方式:cron = 按频率定时;once = 只执行一次(创建即执行,next_run_at 恒空)。
|
||||
schedule_type: Mapped[str] = mapped_column(String(16), nullable=False, default="cron", server_default="cron")
|
||||
enabled: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default="1")
|
||||
next_run_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True, index=True)
|
||||
# 绑定级总控提示(与链内 coordinator_prompt 拼接使用)。
|
||||
coordinator_prompt: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
# 问答沉淀的「执行方向备注」JSON 数组(逐条追加,注入后续每次定时执行)。
|
||||
direction_notes: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
last_run_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
|
||||
last_status: Mapped[str | None] = mapped_column(String(32), nullable=True)
|
||||
# 执行模型(席位 run 的 model_name;NULL = 系统/席位默认)。
|
||||
model: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
||||
created_by: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_utcnow)
|
||||
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_utcnow, onupdate=_utcnow)
|
||||
|
||||
|
||||
class TaskAnalysisRunRow(Base):
|
||||
__tablename__ = "task_analysis_runs"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
binding_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
external_task_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
# schedule = cron 定时执行;manual = 总控问答(对方详情页发起)。
|
||||
trigger: Mapped[str] = mapped_column(String(16), nullable=False, default="schedule")
|
||||
# 幂等键:同 binding + scheduled_for 只建一条(对齐 scheduled_tasks.create_run_once)。
|
||||
scheduled_for: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False)
|
||||
started_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_utcnow)
|
||||
finished_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
|
||||
# pending | running | succeeded | failed | partial
|
||||
status: Mapped[str] = mapped_column(String(16), nullable=False, default="pending", index=True)
|
||||
# 本次编排快照 JSON:[[seat_id,...],...](回放不受链后续修改影响)。
|
||||
stages_snapshot: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
stage_goals_snapshot: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
seat_names_snapshot: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
# 各席位结果 JSON:{agent_id: {state, duration_ms, thread_id, error}}。
|
||||
seat_results: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
error: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
retry_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default="0")
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_utcnow)
|
||||
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_utcnow, onupdate=_utcnow)
|
||||
|
||||
|
||||
class TaskAnalysisRunMessageRow(Base):
|
||||
__tablename__ = "task_analysis_run_messages"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
run_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
seq: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
||||
# 消息归属席位(agent_id);总控消息用固定标识 __coordinator__。
|
||||
seat_agent_id: Mapped[str] = mapped_column(String(128), nullable=False, default="")
|
||||
stage_index: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
||||
# input | llm.ai | tool.result | artifact(对齐现有 run messages 事件分类)。
|
||||
event_type: Mapped[str] = mapped_column(String(32), nullable=False, default="llm.ai")
|
||||
content: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_utcnow)
|
||||
|
||||
|
||||
class TaskAnalysisContentVersionRow(Base):
|
||||
__tablename__ = "task_analysis_content_versions"
|
||||
__table_args__ = (UniqueConstraint("run_id", name="uq_task_analysis_version_run"),)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
external_task_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
# 一次执行 = 一期时间线版本(1:1)。
|
||||
run_id: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
# JSON:{key: {name, seat_agent_id, content, format}} —— 对方详情的各块内容。
|
||||
blocks: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_utcnow)
|
||||
|
||||
|
||||
class TaskDataBlockDefRow(Base):
|
||||
"""数据块定义:块 key 定死 block_1~block_6,标题默认「数据块一~六」可改。"""
|
||||
|
||||
__tablename__ = "task_data_block_defs"
|
||||
__table_args__ = (UniqueConstraint("task_id", "block_key", name="uq_task_data_block_def_key"),)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
# 关联 task_analysis_tasks.id(内部任务 id)。
|
||||
task_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
block_key: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
title: Mapped[str] = mapped_column(String(128), nullable=False, default="")
|
||||
sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default="0")
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_utcnow)
|
||||
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_utcnow, onupdate=_utcnow)
|
||||
|
||||
|
||||
class TaskDataPeriodRow(Base):
|
||||
"""数据交付时间轴刻度:每个任务 period_index 从 1 递增;run_id 作幂等锚点。"""
|
||||
|
||||
__tablename__ = "task_data_periods"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("task_id", "period_index", name="uq_task_data_period_index"),
|
||||
UniqueConstraint("run_id", name="uq_task_data_period_run"),
|
||||
)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
task_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
period_index: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
opened_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_utcnow)
|
||||
# cron = 定时执行开期;manual = 手动触发/总控问答开期;external = 首次提交兜底开期。
|
||||
trigger: Mapped[str] = mapped_column(String(16), nullable=False, default="cron")
|
||||
# 触发本期的执行记录(问答/定时执行共用 run 表);提交兜底开的期没有 run。
|
||||
run_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_utcnow)
|
||||
|
||||
|
||||
class TaskDataBlockRow(Base):
|
||||
"""每期每块的合并结果(由 task_data_submissions 流水按序合并得出)。"""
|
||||
|
||||
__tablename__ = "task_data_blocks"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("task_id", "period_index", "block_key", name="uq_task_data_block_tpb"),
|
||||
)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
task_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
period_index: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
block_key: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
# JSON 对象(顶层 key 合并、同 key 后写覆盖)。
|
||||
payload: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_utcnow, onupdate=_utcnow)
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_utcnow)
|
||||
|
||||
|
||||
class TaskDataSubmissionRow(Base):
|
||||
"""入库流水(追加只读):块 payload 的一致性来源。"""
|
||||
|
||||
__tablename__ = "task_data_submissions"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
task_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
period_index: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
block_key: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
# 本次提交的 JSON 对象片段(合并语义:顶层 key 合并、同 key 后写覆盖)。
|
||||
payload: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
# 提交方(席位 agent id / 用户 id,仅审计)。
|
||||
submitted_by: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
||||
# 同 (task, period, block) 内的提交序号,从 0 递增 —— 合并顺序依据。
|
||||
seq: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default="0")
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_utcnow)
|
||||
|
||||
|
||||
class TaskAnalysisSettingRow(Base):
|
||||
"""模块级 KV 设置(当前仅默认业务链)。"""
|
||||
|
||||
__tablename__ = "task_analysis_settings"
|
||||
|
||||
key: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
value: Mapped[str] = mapped_column(PortableLongText, nullable=False, default="")
|
||||
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_utcnow, onupdate=_utcnow)
|
||||
+802
@@ -0,0 +1,802 @@
|
||||
"""SQLAlchemy-backed task-analysis store.
|
||||
|
||||
Mirrors the memory implementation's normalisation: JSON payloads
|
||||
(chain_snapshot / stages / seat_results / blocks / direction_notes) are stored
|
||||
as LONGTEXT JSON strings; datetimes are ISO strings in dict form.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import delete, func, select, update
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.task_analysis.base import TaskAnalysisStore
|
||||
from deerflow.persistence.task_analysis.model import (
|
||||
TaskAnalysisBindingRow,
|
||||
TaskAnalysisContentVersionRow,
|
||||
TaskAnalysisRunMessageRow,
|
||||
TaskAnalysisRunRow,
|
||||
TaskAnalysisSettingRow,
|
||||
TaskAnalysisTaskRow,
|
||||
TaskDataBlockDefRow,
|
||||
TaskDataBlockRow,
|
||||
TaskDataPeriodRow,
|
||||
TaskDataSubmissionRow,
|
||||
)
|
||||
|
||||
|
||||
def _iso(value: datetime | str | None) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
if isinstance(value, datetime):
|
||||
return value.isoformat()
|
||||
return str(value)
|
||||
|
||||
|
||||
def _utcnow() -> datetime:
|
||||
return datetime.now(UTC)
|
||||
|
||||
|
||||
def _parse_dt(value: Any) -> datetime | None:
|
||||
if value is None or isinstance(value, datetime):
|
||||
return value
|
||||
try:
|
||||
return datetime.fromisoformat(str(value))
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def _dump(value: Any) -> str | None:
|
||||
return None if value is None else json.dumps(value, ensure_ascii=False)
|
||||
|
||||
|
||||
def _load(value: str | None, default: Any) -> Any:
|
||||
if value is None or value == "":
|
||||
return default
|
||||
try:
|
||||
return json.loads(value)
|
||||
except (TypeError, ValueError):
|
||||
return default
|
||||
|
||||
|
||||
def _task_dict(row: TaskAnalysisTaskRow) -> dict[str, Any]:
|
||||
data = row.to_dict()
|
||||
data["created_at"] = _iso(row.created_at)
|
||||
data["updated_at"] = _iso(row.updated_at)
|
||||
return data
|
||||
|
||||
|
||||
def _binding_dict(row: TaskAnalysisBindingRow) -> dict[str, Any]:
|
||||
data = row.to_dict()
|
||||
data["chain_snapshot"] = _load(row.chain_snapshot, {})
|
||||
data["direction_notes"] = _load(row.direction_notes, [])
|
||||
for key in ("next_run_at", "last_run_at", "created_at", "updated_at"):
|
||||
data[key] = _iso(getattr(row, key))
|
||||
return data
|
||||
|
||||
|
||||
def _run_dict(row: TaskAnalysisRunRow) -> dict[str, Any]:
|
||||
data = row.to_dict()
|
||||
data["scheduled_for"] = _iso(row.scheduled_for)
|
||||
data["started_at"] = _iso(row.started_at)
|
||||
data["finished_at"] = _iso(row.finished_at)
|
||||
data["created_at"] = _iso(row.created_at)
|
||||
data["updated_at"] = _iso(row.updated_at)
|
||||
for key, default in (
|
||||
("stages_snapshot", []),
|
||||
("stage_goals_snapshot", []),
|
||||
("seat_names_snapshot", {}),
|
||||
("seat_results", {}),
|
||||
):
|
||||
data[key] = _load(getattr(row, key), default)
|
||||
return data
|
||||
|
||||
|
||||
def _message_dict(row: TaskAnalysisRunMessageRow) -> dict[str, Any]:
|
||||
data = row.to_dict()
|
||||
data["created_at"] = _iso(row.created_at)
|
||||
return data
|
||||
|
||||
|
||||
def _version_dict(row: TaskAnalysisContentVersionRow) -> dict[str, Any]:
|
||||
data = row.to_dict()
|
||||
data["blocks"] = _load(row.blocks, {})
|
||||
data["created_at"] = _iso(row.created_at)
|
||||
return data
|
||||
|
||||
|
||||
def _block_def_dict(row: TaskDataBlockDefRow) -> dict[str, Any]:
|
||||
data = row.to_dict()
|
||||
data["created_at"] = _iso(row.created_at)
|
||||
data["updated_at"] = _iso(row.updated_at)
|
||||
return data
|
||||
|
||||
|
||||
def _period_dict(row: TaskDataPeriodRow) -> dict[str, Any]:
|
||||
data = row.to_dict()
|
||||
data["opened_at"] = _iso(row.opened_at)
|
||||
data["created_at"] = _iso(row.created_at)
|
||||
return data
|
||||
|
||||
|
||||
def _task_block_dict(row: TaskDataBlockRow) -> dict[str, Any]:
|
||||
data = row.to_dict()
|
||||
data["payload"] = _load(row.payload, {})
|
||||
data["updated_at"] = _iso(row.updated_at)
|
||||
data["created_at"] = _iso(row.created_at)
|
||||
return data
|
||||
|
||||
|
||||
def _submission_dict(row: TaskDataSubmissionRow) -> dict[str, Any]:
|
||||
data = row.to_dict()
|
||||
data["payload"] = _load(row.payload, {})
|
||||
data["created_at"] = _iso(row.created_at)
|
||||
return data
|
||||
|
||||
|
||||
class TaskAnalysisRepository(TaskAnalysisStore):
|
||||
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
|
||||
self._sf = session_factory
|
||||
|
||||
# ── tasks ───────────────────────────────────────────────────────────────
|
||||
|
||||
async def create_task(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
source = str(data["source_task_id"])
|
||||
async with self._sf() as session:
|
||||
existing = await session.execute(
|
||||
select(TaskAnalysisTaskRow).where(TaskAnalysisTaskRow.source_task_id == source)
|
||||
)
|
||||
if existing.scalar_one_or_none() is not None:
|
||||
raise ValueError(f"source_task_id already exists: {source}")
|
||||
row = TaskAnalysisTaskRow(
|
||||
id=str(data.get("id") or f"ta-{source}"),
|
||||
source_task_id=source,
|
||||
name=str(data.get("name") or ""),
|
||||
description=data.get("description"),
|
||||
)
|
||||
session.add(row)
|
||||
# 建任务即种 6 条数据块定义(块 key 定死,标题后续可改)。
|
||||
from deerflow.persistence.task_analysis.data_blocks import default_block_defs
|
||||
|
||||
for definition in default_block_defs():
|
||||
session.add(
|
||||
TaskDataBlockDefRow(
|
||||
id=f"tdbd-{row.id}-{definition['block_key']}",
|
||||
task_id=row.id,
|
||||
block_key=definition["block_key"],
|
||||
title=definition["title"],
|
||||
sort_order=definition["sort_order"],
|
||||
)
|
||||
)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return _task_dict(row)
|
||||
|
||||
async def get_task(self, task_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(TaskAnalysisTaskRow, task_id)
|
||||
return _task_dict(row) if row else None
|
||||
|
||||
async def get_task_by_source(self, source_task_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
select(TaskAnalysisTaskRow).where(TaskAnalysisTaskRow.source_task_id == source_task_id)
|
||||
)
|
||||
row = result.scalar_one_or_none()
|
||||
return _task_dict(row) if row else None
|
||||
|
||||
async def list_tasks(self) -> list[dict[str, Any]]:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
select(TaskAnalysisTaskRow).order_by(TaskAnalysisTaskRow.created_at.desc())
|
||||
)
|
||||
tasks = [_task_dict(row) for row in result.scalars()]
|
||||
bindings = await session.execute(select(TaskAnalysisBindingRow))
|
||||
by_task: dict[str, dict[str, Any]] = {}
|
||||
for row in bindings.scalars():
|
||||
by_task[row.external_task_id] = _binding_dict(row)
|
||||
for task in tasks:
|
||||
task["binding"] = by_task.get(task["id"])
|
||||
return tasks
|
||||
|
||||
async def delete_task_cascade(self, task_id: str) -> bool:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(TaskAnalysisTaskRow, task_id)
|
||||
if row is None:
|
||||
return False
|
||||
run_ids = (
|
||||
(
|
||||
await session.execute(
|
||||
select(TaskAnalysisRunRow.id).where(TaskAnalysisRunRow.external_task_id == task_id)
|
||||
)
|
||||
)
|
||||
.scalars()
|
||||
.all()
|
||||
)
|
||||
await session.execute(
|
||||
delete(TaskAnalysisBindingRow).where(TaskAnalysisBindingRow.external_task_id == task_id)
|
||||
)
|
||||
await session.execute(
|
||||
delete(TaskDataBlockDefRow).where(TaskDataBlockDefRow.task_id == task_id)
|
||||
)
|
||||
await session.execute(
|
||||
delete(TaskDataPeriodRow).where(TaskDataPeriodRow.task_id == task_id)
|
||||
)
|
||||
await session.execute(
|
||||
delete(TaskDataBlockRow).where(TaskDataBlockRow.task_id == task_id)
|
||||
)
|
||||
await session.execute(
|
||||
delete(TaskDataSubmissionRow).where(TaskDataSubmissionRow.task_id == task_id)
|
||||
)
|
||||
if run_ids:
|
||||
await session.execute(
|
||||
delete(TaskAnalysisRunMessageRow).where(TaskAnalysisRunMessageRow.run_id.in_(run_ids))
|
||||
)
|
||||
await session.execute(
|
||||
delete(TaskAnalysisContentVersionRow).where(
|
||||
TaskAnalysisContentVersionRow.run_id.in_(run_ids)
|
||||
)
|
||||
)
|
||||
await session.execute(
|
||||
delete(TaskAnalysisRunRow).where(TaskAnalysisRunRow.external_task_id == task_id)
|
||||
)
|
||||
await session.delete(row)
|
||||
await session.commit()
|
||||
return True
|
||||
|
||||
# ── bindings ────────────────────────────────────────────────────────────
|
||||
|
||||
async def get_binding(self, external_task_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
select(TaskAnalysisBindingRow).where(
|
||||
TaskAnalysisBindingRow.external_task_id == external_task_id
|
||||
)
|
||||
)
|
||||
row = result.scalar_one_or_none()
|
||||
return _binding_dict(row) if row else None
|
||||
|
||||
async def get_binding_by_id(self, binding_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(TaskAnalysisBindingRow, binding_id)
|
||||
return _binding_dict(row) if row else None
|
||||
|
||||
async def upsert_binding(self, external_task_id: str, data: dict[str, Any]) -> dict[str, Any]:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
select(TaskAnalysisBindingRow).where(
|
||||
TaskAnalysisBindingRow.external_task_id == external_task_id
|
||||
)
|
||||
)
|
||||
row = result.scalar_one_or_none()
|
||||
if row is None:
|
||||
row = TaskAnalysisBindingRow(
|
||||
id=str(data.get("id") or f"tab-{external_task_id}"),
|
||||
external_task_id=external_task_id,
|
||||
chain_id=str(data["chain_id"]),
|
||||
created_by=data.get("created_by"),
|
||||
)
|
||||
session.add(row)
|
||||
row.chain_id = str(data.get("chain_id") or row.chain_id)
|
||||
row.chain_snapshot = _dump(data.get("chain_snapshot") or {})
|
||||
row.cron_expr = str(data.get("cron_expr") or row.cron_expr or "")
|
||||
row.timezone = str(data.get("timezone") or row.timezone or "Asia/Shanghai")
|
||||
if "schedule_type" in data:
|
||||
row.schedule_type = str(data.get("schedule_type") or "cron")
|
||||
row.model = data.get("model")
|
||||
if "enabled" in data:
|
||||
row.enabled = bool(data["enabled"])
|
||||
if "next_run_at" in data:
|
||||
row.next_run_at = _parse_dt(data.get("next_run_at"))
|
||||
row.coordinator_prompt = data.get("coordinator_prompt")
|
||||
if "direction_notes" in data:
|
||||
row.direction_notes = _dump(list(data["direction_notes"] or []))
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return _binding_dict(row)
|
||||
|
||||
async def delete_binding(self, external_task_id: str) -> bool:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
delete(TaskAnalysisBindingRow).where(
|
||||
TaskAnalysisBindingRow.external_task_id == external_task_id
|
||||
)
|
||||
)
|
||||
await session.commit()
|
||||
return bool(result.rowcount)
|
||||
|
||||
async def due_bindings(self, now: datetime, *, limit: int = 20) -> list[dict[str, Any]]:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
select(TaskAnalysisBindingRow)
|
||||
.where(
|
||||
TaskAnalysisBindingRow.enabled.is_(True),
|
||||
TaskAnalysisBindingRow.next_run_at.is_not(None),
|
||||
TaskAnalysisBindingRow.next_run_at <= now,
|
||||
)
|
||||
.order_by(TaskAnalysisBindingRow.next_run_at.asc())
|
||||
.limit(limit)
|
||||
)
|
||||
return [_binding_dict(row) for row in result.scalars()]
|
||||
|
||||
async def update_binding(self, binding_id: str, patch: dict[str, Any]) -> dict[str, Any] | None:
|
||||
allowed = {
|
||||
"chain_id", "cron_expr", "timezone", "schedule_type", "enabled", "next_run_at",
|
||||
"last_run_at", "last_status", "coordinator_prompt", "model",
|
||||
}
|
||||
values: dict[str, Any] = {k: v for k, v in patch.items() if k in allowed}
|
||||
if "next_run_at" in values:
|
||||
values["next_run_at"] = _parse_dt(values["next_run_at"])
|
||||
if "last_run_at" in values:
|
||||
values["last_run_at"] = _parse_dt(values["last_run_at"])
|
||||
if "direction_notes" in patch:
|
||||
values["direction_notes"] = _dump(list(patch["direction_notes"] or []))
|
||||
if not values:
|
||||
row = await self._get_binding_row(binding_id)
|
||||
return _binding_dict(row) if row else None
|
||||
async with self._sf() as session:
|
||||
await session.execute(
|
||||
update(TaskAnalysisBindingRow).where(TaskAnalysisBindingRow.id == binding_id).values(**values)
|
||||
)
|
||||
await session.commit()
|
||||
row = await session.get(TaskAnalysisBindingRow, binding_id)
|
||||
return _binding_dict(row) if row else None
|
||||
|
||||
async def _get_binding_row(self, binding_id: str) -> TaskAnalysisBindingRow | None:
|
||||
async with self._sf() as session:
|
||||
return await session.get(TaskAnalysisBindingRow, binding_id)
|
||||
|
||||
async def last_bound_chain_id(self) -> str | None:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
select(TaskAnalysisBindingRow.chain_id)
|
||||
.order_by(TaskAnalysisBindingRow.updated_at.desc())
|
||||
.limit(1)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
# ── runs ────────────────────────────────────────────────────────────────
|
||||
|
||||
async def create_run_once(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
binding_id = str(data["binding_id"])
|
||||
scheduled_for = _parse_dt(data["scheduled_for"]) or datetime.now(UTC)
|
||||
import uuid as _uuid
|
||||
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
select(TaskAnalysisRunRow).where(
|
||||
TaskAnalysisRunRow.binding_id == binding_id,
|
||||
TaskAnalysisRunRow.scheduled_for == scheduled_for,
|
||||
)
|
||||
)
|
||||
existing = result.scalar_one_or_none()
|
||||
if existing is not None:
|
||||
return _run_dict(existing)
|
||||
row = TaskAnalysisRunRow(
|
||||
id=str(data.get("id") or f"tar-{_uuid.uuid4()}"),
|
||||
binding_id=binding_id,
|
||||
external_task_id=str(data["external_task_id"]),
|
||||
trigger=str(data.get("trigger") or "schedule"),
|
||||
scheduled_for=scheduled_for,
|
||||
status=str(data.get("status") or "pending"),
|
||||
stages_snapshot=_dump(data.get("stages_snapshot") or []),
|
||||
stage_goals_snapshot=_dump(data.get("stage_goals_snapshot") or []),
|
||||
seat_names_snapshot=_dump(data.get("seat_names_snapshot") or {}),
|
||||
seat_results=_dump(data.get("seat_results") or {}),
|
||||
)
|
||||
session.add(row)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return _run_dict(row)
|
||||
|
||||
async def get_run(self, run_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(TaskAnalysisRunRow, run_id)
|
||||
return _run_dict(row) if row else None
|
||||
|
||||
async def list_runs(self, external_task_id: str, *, limit: int = 50) -> list[dict[str, Any]]:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
select(TaskAnalysisRunRow)
|
||||
.where(TaskAnalysisRunRow.external_task_id == external_task_id)
|
||||
.order_by(TaskAnalysisRunRow.started_at.desc())
|
||||
.limit(limit)
|
||||
)
|
||||
return [_run_dict(row) for row in result.scalars()]
|
||||
|
||||
async def update_run(self, run_id: str, patch: dict[str, Any]) -> dict[str, Any] | None:
|
||||
allowed = {
|
||||
"trigger", "status", "finished_at", "error", "retry_count",
|
||||
"stages_snapshot", "stage_goals_snapshot", "seat_names_snapshot", "seat_results",
|
||||
}
|
||||
values: dict[str, Any] = {}
|
||||
for key, value in patch.items():
|
||||
if key not in allowed:
|
||||
continue
|
||||
if key == "finished_at":
|
||||
values[key] = _parse_dt(value)
|
||||
elif key in {"stages_snapshot", "stage_goals_snapshot", "seat_names_snapshot", "seat_results"}:
|
||||
values[key] = _dump(value)
|
||||
else:
|
||||
values[key] = value
|
||||
if not values:
|
||||
row = await self._get_run_row(run_id)
|
||||
return _run_dict(row) if row else None
|
||||
async with self._sf() as session:
|
||||
await session.execute(
|
||||
update(TaskAnalysisRunRow).where(TaskAnalysisRunRow.id == run_id).values(**values)
|
||||
)
|
||||
await session.commit()
|
||||
row = await session.get(TaskAnalysisRunRow, run_id)
|
||||
return _run_dict(row) if row else None
|
||||
|
||||
async def _get_run_row(self, run_id: str) -> TaskAnalysisRunRow | None:
|
||||
async with self._sf() as session:
|
||||
return await session.get(TaskAnalysisRunRow, run_id)
|
||||
|
||||
async def stuck_runs(self, older_than: datetime, *, limit: int = 20) -> list[dict[str, Any]]:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
select(TaskAnalysisRunRow)
|
||||
.where(
|
||||
TaskAnalysisRunRow.status.in_(("pending", "running")),
|
||||
TaskAnalysisRunRow.started_at < older_than,
|
||||
)
|
||||
.order_by(TaskAnalysisRunRow.started_at.asc())
|
||||
.limit(limit)
|
||||
)
|
||||
return [_run_dict(row) for row in result.scalars()]
|
||||
|
||||
# ── messages ────────────────────────────────────────────────────────────
|
||||
|
||||
async def append_run_messages(self, run_id: str, messages: list[dict[str, Any]]) -> int:
|
||||
import uuid as _uuid
|
||||
|
||||
async with self._sf() as session:
|
||||
existing = (
|
||||
(
|
||||
await session.execute(
|
||||
select(TaskAnalysisRunMessageRow.id).where(
|
||||
TaskAnalysisRunMessageRow.run_id == run_id
|
||||
)
|
||||
)
|
||||
)
|
||||
.scalars()
|
||||
.all()
|
||||
)
|
||||
base_seq = len(existing)
|
||||
rows = [
|
||||
TaskAnalysisRunMessageRow(
|
||||
id=str(_uuid.uuid4()),
|
||||
run_id=run_id,
|
||||
seq=base_seq + index,
|
||||
seat_agent_id=str(message.get("seat_agent_id") or ""),
|
||||
stage_index=int(message.get("stage_index") or 0),
|
||||
event_type=str(message.get("event_type") or "llm.ai"),
|
||||
content=message.get("content"),
|
||||
)
|
||||
for index, message in enumerate(messages)
|
||||
]
|
||||
session.add_all(rows)
|
||||
await session.commit()
|
||||
return len(rows)
|
||||
|
||||
async def list_run_messages(
|
||||
self, run_id: str, *, seat: str | None = None, limit: int = 500
|
||||
) -> list[dict[str, Any]]:
|
||||
async with self._sf() as session:
|
||||
stmt = (
|
||||
select(TaskAnalysisRunMessageRow)
|
||||
.where(TaskAnalysisRunMessageRow.run_id == run_id)
|
||||
.order_by(TaskAnalysisRunMessageRow.seq.asc())
|
||||
.limit(limit)
|
||||
)
|
||||
if seat is not None:
|
||||
stmt = stmt.where(TaskAnalysisRunMessageRow.seat_agent_id == seat)
|
||||
result = await session.execute(stmt)
|
||||
return [_message_dict(row) for row in result.scalars()]
|
||||
|
||||
# ── versions ────────────────────────────────────────────────────────────
|
||||
|
||||
async def create_version(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
import uuid as _uuid
|
||||
|
||||
run_id = str(data["run_id"])
|
||||
async with self._sf() as session:
|
||||
await session.execute(
|
||||
delete(TaskAnalysisContentVersionRow).where(
|
||||
TaskAnalysisContentVersionRow.run_id == run_id
|
||||
)
|
||||
)
|
||||
row = TaskAnalysisContentVersionRow(
|
||||
id=str(_uuid.uuid4()),
|
||||
external_task_id=str(data["external_task_id"]),
|
||||
run_id=run_id,
|
||||
blocks=_dump(data.get("blocks") or {}),
|
||||
)
|
||||
session.add(row)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return _version_dict(row)
|
||||
|
||||
async def list_versions(
|
||||
self, external_task_id: str, *, limit: int = 50, offset: int = 0
|
||||
) -> list[dict[str, Any]]:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
select(TaskAnalysisContentVersionRow)
|
||||
.where(TaskAnalysisContentVersionRow.external_task_id == external_task_id)
|
||||
.order_by(TaskAnalysisContentVersionRow.created_at.desc())
|
||||
.offset(offset)
|
||||
.limit(limit)
|
||||
)
|
||||
return [_version_dict(row) for row in result.scalars()]
|
||||
|
||||
# ── data delivery: block defs / periods / submissions ────────────────────
|
||||
|
||||
async def ensure_block_defs(self, task_id: str) -> list[dict[str, Any]]:
|
||||
from deerflow.persistence.task_analysis.data_blocks import default_block_defs
|
||||
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
select(TaskDataBlockDefRow).where(TaskDataBlockDefRow.task_id == task_id)
|
||||
)
|
||||
existing_keys = {row.block_key for row in result.scalars()}
|
||||
missing = [
|
||||
definition
|
||||
for definition in default_block_defs()
|
||||
if definition["block_key"] not in existing_keys
|
||||
]
|
||||
if not missing:
|
||||
return await self._ordered_defs(session, task_id)
|
||||
for definition in missing:
|
||||
session.add(
|
||||
TaskDataBlockDefRow(
|
||||
id=f"tdbd-{task_id}-{definition['block_key']}",
|
||||
task_id=task_id,
|
||||
block_key=definition["block_key"],
|
||||
title=definition["title"],
|
||||
sort_order=definition["sort_order"],
|
||||
)
|
||||
)
|
||||
try:
|
||||
await session.commit()
|
||||
except IntegrityError:
|
||||
# 并发补种:另一个请求先种好了,沿用其结果。
|
||||
await session.rollback()
|
||||
return await self._ordered_defs(session, task_id)
|
||||
|
||||
@staticmethod
|
||||
async def _ordered_defs(session: AsyncSession, task_id: str) -> list[dict[str, Any]]:
|
||||
result = await session.execute(
|
||||
select(TaskDataBlockDefRow)
|
||||
.where(TaskDataBlockDefRow.task_id == task_id)
|
||||
.order_by(TaskDataBlockDefRow.sort_order.asc(), TaskDataBlockDefRow.block_key.asc())
|
||||
)
|
||||
return [_block_def_dict(row) for row in result.scalars()]
|
||||
|
||||
async def list_block_defs(self, task_id: str) -> list[dict[str, Any]]:
|
||||
async with self._sf() as session:
|
||||
return await self._ordered_defs(session, task_id)
|
||||
|
||||
async def update_block_defs(self, task_id: str, updates: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
select(TaskDataBlockDefRow).where(TaskDataBlockDefRow.task_id == task_id)
|
||||
)
|
||||
rows = {row.block_key: row for row in result.scalars()}
|
||||
touched = False
|
||||
for item in updates:
|
||||
block_key = str(item.get("block_key") or "")
|
||||
row = rows.get(block_key)
|
||||
if row is None:
|
||||
raise ValueError(f"未知数据块:{block_key}")
|
||||
patch: dict[str, Any] = {}
|
||||
if item.get("title") is not None:
|
||||
patch["title"] = str(item["title"])
|
||||
if item.get("sort_order") is not None:
|
||||
patch["sort_order"] = int(item["sort_order"])
|
||||
if patch:
|
||||
await session.execute(
|
||||
update(TaskDataBlockDefRow)
|
||||
.where(TaskDataBlockDefRow.id == row.id)
|
||||
.values(**patch)
|
||||
)
|
||||
touched = True
|
||||
if touched:
|
||||
await session.commit()
|
||||
return await self._ordered_defs(session, task_id)
|
||||
|
||||
async def open_period(
|
||||
self,
|
||||
task_id: str,
|
||||
*,
|
||||
source: str,
|
||||
run_id: str | None = None,
|
||||
opened_at: datetime | None = None,
|
||||
) -> dict[str, Any]:
|
||||
import uuid as _uuid
|
||||
|
||||
for _attempt in range(3):
|
||||
async with self._sf() as session:
|
||||
if run_id:
|
||||
existing = await session.execute(
|
||||
select(TaskDataPeriodRow).where(TaskDataPeriodRow.run_id == run_id)
|
||||
)
|
||||
row = existing.scalar_one_or_none()
|
||||
if row is not None:
|
||||
return _period_dict(row)
|
||||
max_index = (
|
||||
await session.execute(
|
||||
select(func.max(TaskDataPeriodRow.period_index)).where(
|
||||
TaskDataPeriodRow.task_id == task_id
|
||||
)
|
||||
)
|
||||
).scalar()
|
||||
row = TaskDataPeriodRow(
|
||||
id=f"tdp-{_uuid.uuid4()}",
|
||||
task_id=task_id,
|
||||
period_index=(max_index or 0) + 1,
|
||||
opened_at=opened_at or _utcnow(),
|
||||
trigger=str(source or "cron"),
|
||||
run_id=run_id,
|
||||
)
|
||||
session.add(row)
|
||||
try:
|
||||
await session.commit()
|
||||
except IntegrityError:
|
||||
# 并发开期撞了期号唯一键(或 run_id 幂等锚点)——重查/重试。
|
||||
await session.rollback()
|
||||
continue
|
||||
await session.refresh(row)
|
||||
return _period_dict(row)
|
||||
raise RuntimeError(f"open_period 连续冲突,task={task_id} source={source}")
|
||||
|
||||
async def get_period(self, task_id: str, period_index: int) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
select(TaskDataPeriodRow).where(
|
||||
TaskDataPeriodRow.task_id == task_id,
|
||||
TaskDataPeriodRow.period_index == period_index,
|
||||
)
|
||||
)
|
||||
row = result.scalar_one_or_none()
|
||||
return _period_dict(row) if row else None
|
||||
|
||||
async def current_period(self, task_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
select(TaskDataPeriodRow)
|
||||
.where(TaskDataPeriodRow.task_id == task_id)
|
||||
.order_by(TaskDataPeriodRow.period_index.desc())
|
||||
.limit(1)
|
||||
)
|
||||
row = result.scalar_one_or_none()
|
||||
return _period_dict(row) if row else None
|
||||
|
||||
async def list_periods(self, task_id: str, *, limit: int = 500) -> list[dict[str, Any]]:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
select(TaskDataPeriodRow)
|
||||
.where(TaskDataPeriodRow.task_id == task_id)
|
||||
.order_by(TaskDataPeriodRow.period_index.desc())
|
||||
.limit(limit)
|
||||
)
|
||||
rows = [_period_dict(row) for row in result.scalars()]
|
||||
rows.reverse() # 时间轴语义:按期号升序返回
|
||||
return rows
|
||||
|
||||
async def append_submission(
|
||||
self,
|
||||
task_id: str,
|
||||
period_index: int,
|
||||
block_key: str,
|
||||
payload: dict[str, Any],
|
||||
*,
|
||||
submitted_by: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
import uuid as _uuid
|
||||
|
||||
async with self._sf() as session:
|
||||
base_seq = (
|
||||
(
|
||||
await session.execute(
|
||||
select(func.count(TaskDataSubmissionRow.id)).where(
|
||||
TaskDataSubmissionRow.task_id == task_id,
|
||||
TaskDataSubmissionRow.period_index == period_index,
|
||||
TaskDataSubmissionRow.block_key == block_key,
|
||||
)
|
||||
)
|
||||
)
|
||||
.scalar_one()
|
||||
)
|
||||
row = TaskDataSubmissionRow(
|
||||
id=f"tds-{_uuid.uuid4()}",
|
||||
task_id=task_id,
|
||||
period_index=period_index,
|
||||
block_key=block_key,
|
||||
payload=_dump(payload),
|
||||
submitted_by=submitted_by,
|
||||
seq=int(base_seq),
|
||||
)
|
||||
session.add(row)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return _submission_dict(row)
|
||||
|
||||
async def list_submissions(
|
||||
self, task_id: str, period_index: int, block_key: str, *, limit: int = 500
|
||||
) -> list[dict[str, Any]]:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
select(TaskDataSubmissionRow)
|
||||
.where(
|
||||
TaskDataSubmissionRow.task_id == task_id,
|
||||
TaskDataSubmissionRow.period_index == period_index,
|
||||
TaskDataSubmissionRow.block_key == block_key,
|
||||
)
|
||||
.order_by(TaskDataSubmissionRow.seq.asc())
|
||||
.limit(limit)
|
||||
)
|
||||
return [_submission_dict(row) for row in result.scalars()]
|
||||
|
||||
async def upsert_period_block(
|
||||
self, task_id: str, period_index: int, block_key: str, payload: dict[str, Any]
|
||||
) -> dict[str, Any]:
|
||||
import uuid as _uuid
|
||||
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
select(TaskDataBlockRow).where(
|
||||
TaskDataBlockRow.task_id == task_id,
|
||||
TaskDataBlockRow.period_index == period_index,
|
||||
TaskDataBlockRow.block_key == block_key,
|
||||
)
|
||||
)
|
||||
row = result.scalar_one_or_none()
|
||||
if row is None:
|
||||
row = TaskDataBlockRow(
|
||||
id=f"tdb-{_uuid.uuid4()}",
|
||||
task_id=task_id,
|
||||
period_index=period_index,
|
||||
block_key=block_key,
|
||||
payload=_dump(payload),
|
||||
)
|
||||
session.add(row)
|
||||
else:
|
||||
row.payload = _dump(payload)
|
||||
row.updated_at = _utcnow()
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return _task_block_dict(row)
|
||||
|
||||
async def list_task_blocks(self, task_id: str, *, limit: int = 2000) -> list[dict[str, Any]]:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
select(TaskDataBlockRow)
|
||||
.where(TaskDataBlockRow.task_id == task_id)
|
||||
.order_by(TaskDataBlockRow.period_index.asc(), TaskDataBlockRow.block_key.asc())
|
||||
.limit(limit)
|
||||
)
|
||||
return [_task_block_dict(row) for row in result.scalars()]
|
||||
|
||||
# ── settings ────────────────────────────────────────────────────────────
|
||||
|
||||
async def get_setting(self, key: str) -> str | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(TaskAnalysisSettingRow, key)
|
||||
return row.value if row else None
|
||||
|
||||
async def set_setting(self, key: str, value: str) -> None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(TaskAnalysisSettingRow, key)
|
||||
if row is None:
|
||||
session.add(TaskAnalysisSettingRow(key=key, value=value))
|
||||
else:
|
||||
row.value = value
|
||||
await session.commit()
|
||||
Reference in new issue
Block a user