sync update from LAN repo (source dhk-1 @ 3b1ec5b4)

This commit is contained in:
gozeros committed 2026-11-05 10:56:34 +08:00
1 parent da6521b6cb
commit 264e2200d6
995 files changed
+148105 -13399

No files matched your search

@@ -1,4 +1,4 @@
"""Dedicated taskId + goPath conversation-history index."""
"""Dedicated taskId + goPath conversation-history cards."""
from __future__ import annotations
@@ -1,8 +1,9 @@
"""Shared helpers for task deep-link conversation indexing.
"""Shared helpers for task deep-link conversation history.
The LangGraph thread/checkpoint remains the source of truth for messages and
execution state. This package only owns a small, dedicated lookup index so the
task workspaces never have to search/filter the global ``threads_meta`` table.
execution state. This package owns a dedicated history card so the task
workspaces never have to search, filter, or join the global ``threads_meta``
catalogue when listing a task's conversations.
"""
from __future__ import annotations
@@ -10,6 +11,12 @@ from __future__ import annotations
from typing import Any, TypedDict
TASK_DEEPLINK_GO_PATHS = frozenset({"3qfx", "rwfx", "xdfx", "b5-1"})
# Custom business mappings use their administrator-defined name as goPath.
# Keep this in lock-step with ``business_mapping.business_code``.
MAX_TASK_DEEPLINK_GO_PATH = 32
# Matches the ``task_deeplink_threads.sub_type`` column width.
MAX_TASK_DEEPLINK_SUB_TYPE = 64
class TaskDeeplinkBinding(TypedDict):
@@ -17,6 +24,21 @@ class TaskDeeplinkBinding(TypedDict):
source_task_id: str
go_path: str
agent_id: str | None
# b5-1 integrator-supplied ?type= partition; "" when the link had none.
sub_type: str
def is_valid_task_deeplink_go_path(value: str) -> bool:
"""Accept built-ins and validated custom mapping names.
The harness cannot import the app-level business-mapping store (the import
firewall deliberately forbids that), so it validates the safe wire format
here. Creation itself remains admin-only in the Gateway router.
"""
go_path = value.strip().lower()
if go_path in TASK_DEEPLINK_GO_PATHS:
return True
return bool(go_path) and len(go_path) <= MAX_TASK_DEEPLINK_GO_PATH and not go_path.endswith("-a")
def task_deeplink_binding(metadata: dict[str, Any] | None) -> TaskDeeplinkBinding | None:
@@ -26,21 +48,25 @@ def task_deeplink_binding(metadata: dict[str, Any] | None) -> TaskDeeplinkBindin
actual task id for 3qfx/rwfx and the action id for xdfx. Older callers may
omit it, in which case ``taskId`` is a safe fallback. A ``taskGoPath`` is
mandatory, deliberately preventing legacy task rows from being migrated or
accidentally mixed into the new store.
accidentally mixed into the new store. ``taskType`` (the b5-1 ``?type=``
business type) further partitions one task's history so integrator-supplied
types never mix; it is capped to the column width here.
"""
md = metadata or {}
go_path = str(md.get("taskGoPath") or "").strip().lower()
if go_path not in TASK_DEEPLINK_GO_PATHS:
if not is_valid_task_deeplink_go_path(go_path):
return None
source_task_id = str(md.get("taskId") or "").strip()
task_id = str(md.get("taskRecordId") or source_task_id).strip()
if not task_id or not source_task_id:
return None
agent_id = str(md.get("agent_id") or "").strip() or None
sub_type = str(md.get("taskType") or "").strip()[:MAX_TASK_DEEPLINK_SUB_TYPE]
return {
"task_id": task_id,
"source_task_id": source_task_id,
"go_path": go_path,
"agent_id": agent_id,
"sub_type": sub_type,
}
@@ -1,4 +1,4 @@
"""In-memory task deep-link index backed by LangGraph BaseStore."""
"""In-memory task deep-link history cards backed by LangGraph BaseStore."""
from __future__ import annotations
@@ -16,6 +16,19 @@ if TYPE_CHECKING:
TASK_DEEPLINK_THREADS_NS: tuple[str, ...] = ("task_deeplink_threads",)
def _card_record(thread: dict[str, Any], binding: dict[str, Any]) -> dict[str, Any]:
return {
"thread_id": str(thread.get("thread_id") or ""),
**binding,
"display_name": thread.get("display_name"),
"status": thread.get("status") or "idle",
"metadata": thread.get("metadata") or {},
"created_by": thread.get("user_id"),
"created_at": thread.get("created_at") or now_iso(),
"updated_at": thread.get("updated_at") or now_iso(),
}
async def sync_task_deeplink_index(store: BaseStore, thread: dict[str, Any]) -> None:
metadata = thread.get("metadata") or {}
binding = task_deeplink_binding(metadata)
@@ -26,17 +39,7 @@ async def sync_task_deeplink_index(store: BaseStore, thread: dict[str, Any]) ->
if "taskGoPath" in metadata or "taskRecordId" in metadata:
await store.adelete(TASK_DEEPLINK_THREADS_NS, thread_id)
return
await store.aput(
TASK_DEEPLINK_THREADS_NS,
thread_id,
{
"thread_id": thread_id,
**binding,
"created_by": thread.get("user_id"),
"created_at": thread.get("created_at") or now_iso(),
"updated_at": thread.get("updated_at") or now_iso(),
},
)
await store.aput(TASK_DEEPLINK_THREADS_NS, thread_id, _card_record(thread, binding))
async def delete_task_deeplink_index(store: BaseStore, thread_id: str) -> None:
@@ -46,6 +49,8 @@ async def delete_task_deeplink_index(store: BaseStore, thread_id: str) -> None:
class MemoryTaskDeeplinkThreadStore:
def __init__(self, store: BaseStore, thread_store: ThreadMetaStore) -> None:
self._store = store
# Kept for constructor compatibility with the factory. Listing no
# longer reads the generic thread catalogue.
self._thread_store = thread_store
async def list_threads(
@@ -54,28 +59,33 @@ class MemoryTaskDeeplinkThreadStore:
go_path: str,
*,
limit: int = 100,
sub_type: str | None = None,
) -> list[dict[str, Any]]:
# ``sub_type`` semantics mirror the SQL repository: provided value (even
# "") narrows to that b5-1 ?type= partition; None returns all sub-types.
conditions = {"task_id": task_id, "go_path": go_path}
if sub_type is not None:
conditions["sub_type"] = sub_type
items = await self._store.asearch(
TASK_DEEPLINK_THREADS_NS,
filter={"task_id": task_id, "go_path": go_path},
filter=conditions,
limit=limit,
)
rows: list[dict[str, Any]] = []
for item in items:
thread_id = str(item.value.get("thread_id") or "")
value = item.value or {}
thread_id = str(value.get("thread_id") or "")
if not thread_id:
continue
thread = await self._thread_store.get(thread_id, user_id=None)
if thread is None:
continue
display_name = value.get("display_name")
rows.append(
{
"thread_id": thread_id,
"status": thread.get("status", "idle"),
"created_at": thread.get("created_at", ""),
"updated_at": thread.get("updated_at", ""),
"metadata": thread.get("metadata") or {},
"values": ({"title": thread.get("display_name")} if thread.get("display_name") else {}),
"status": value.get("status") or "idle",
"created_at": value.get("created_at", ""),
"updated_at": value.get("updated_at", ""),
"metadata": value.get("metadata") or {},
"values": ({"title": display_name} if display_name else {}),
"interrupts": {},
}
)
@@ -1,4 +1,4 @@
"""Dedicated lookup rows for single-agent task deep-link conversations."""
"""Dedicated history cards for single-agent task deep-link conversations."""
from __future__ import annotations
@@ -8,15 +8,15 @@ from sqlalchemy import Index, String
from sqlalchemy.orm import Mapped, mapped_column
from deerflow.persistence.base import Base
from deerflow.persistence.types import BeijingDateTime
from deerflow.persistence.types import BeijingDateTime, PortableJSON
class TaskDeeplinkThreadRow(Base):
"""One task-workspace history entry pointing at one LangGraph thread.
"""One task-workspace history card pointing at one LangGraph thread.
The row intentionally stores no messages or title. Those remain in the
canonical checkpoint / ``threads_meta`` record and are joined by primary
key when listing a task's history.
List queries read only this table. Title / status / metadata are copied
here so the huge generic ``threads_meta`` catalogue is never joined.
Messages stay in the LangGraph checkpoint.
"""
__tablename__ = "task_deeplink_threads"
@@ -25,6 +25,17 @@ class TaskDeeplinkThreadRow(Base):
"ix_task_deeplink_threads_scope",
"task_id",
"go_path",
"sub_type",
"updated_at",
"thread_id",
),
# 3qfx/rwfx/xdfx omit sub_type. The scope index is ordered by
# sub_type before updated_at, so those listings cannot ride it for
# "newest first" without a filesort. This prefix matches that query.
Index(
"ix_task_deeplink_threads_task_go_updated",
"task_id",
"go_path",
"updated_at",
"thread_id",
),
@@ -35,8 +46,14 @@ class TaskDeeplinkThreadRow(Base):
task_id: Mapped[str] = mapped_column(String(128), nullable=False)
# Resolved real task id used by model/run context (equals task_id except xdfx).
source_task_id: Mapped[str] = mapped_column(String(128), nullable=False)
go_path: Mapped[str] = mapped_column(String(16), nullable=False)
go_path: Mapped[str] = mapped_column(String(32), nullable=False)
# b5-1 ?type= business type; "" for links without one (and for the other
# go paths, which have no type dimension).
sub_type: Mapped[str] = mapped_column(String(64), nullable=False, default="")
agent_id: Mapped[str | None] = mapped_column(String(128), nullable=True)
display_name: Mapped[str | None] = mapped_column(String(256), nullable=True)
status: Mapped[str] = mapped_column(String(20), nullable=False, default="idle")
metadata_json: Mapped[dict] = mapped_column(PortableJSON(), nullable=False, default=dict)
# Audit only. Task deep-link histories remain shared across authenticated users.
created_by: Mapped[str | None] = mapped_column(String(64), nullable=True)
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
@@ -1,11 +1,11 @@
"""SQL implementation of the dedicated task deep-link conversation index."""
"""SQL implementation of the dedicated task deep-link conversation cards."""
from __future__ import annotations
from datetime import UTC, datetime
from typing import TYPE_CHECKING, Any
from sqlalchemy import delete, select
from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from deerflow.persistence.task_deeplink_threads.base import task_deeplink_binding
@@ -15,8 +15,28 @@ if TYPE_CHECKING:
from deerflow.persistence.thread_meta.model import ThreadMetaRow
def _card_payload(thread: TaskDeeplinkThreadRow) -> dict[str, Any]:
return {
"thread_id": thread.thread_id,
"status": thread.status or "idle",
"created_at": thread.created_at,
"updated_at": thread.updated_at,
"metadata": thread.metadata_json or {},
"values": ({"title": thread.display_name} if thread.display_name else {}),
"interrupts": {},
}
def _apply_card_fields(row: TaskDeeplinkThreadRow, thread: ThreadMetaRow, now: datetime) -> None:
row.display_name = thread.display_name
row.status = thread.status or "idle"
row.metadata_json = dict(thread.metadata_json or {})
row.created_by = thread.user_id
row.updated_at = now
async def sync_task_deeplink_index(session: AsyncSession, thread: ThreadMetaRow) -> None:
"""Synchronize one index row inside the caller's thread-meta transaction."""
"""Synchronize one history card inside the caller's thread-meta transaction."""
metadata = thread.metadata_json or {}
binding = task_deeplink_binding(metadata)
@@ -30,25 +50,39 @@ async def sync_task_deeplink_index(session: AsyncSession, thread: ThreadMetaRow)
existing = await session.get(TaskDeeplinkThreadRow, thread.thread_id)
now = thread.updated_at if isinstance(thread.updated_at, datetime) else datetime.now(UTC)
if existing is None:
session.add(
TaskDeeplinkThreadRow(
thread_id=thread.thread_id,
task_id=binding["task_id"],
source_task_id=binding["source_task_id"],
go_path=binding["go_path"],
agent_id=binding["agent_id"],
created_by=thread.user_id,
created_at=(thread.created_at if isinstance(thread.created_at, datetime) else now),
updated_at=now,
)
row = TaskDeeplinkThreadRow(
thread_id=thread.thread_id,
task_id=binding["task_id"],
source_task_id=binding["source_task_id"],
go_path=binding["go_path"],
sub_type=binding["sub_type"],
agent_id=binding["agent_id"],
created_by=thread.user_id,
created_at=(thread.created_at if isinstance(thread.created_at, datetime) else now),
updated_at=now,
)
_apply_card_fields(row, thread, now)
session.add(row)
return
existing.task_id = binding["task_id"]
existing.source_task_id = binding["source_task_id"]
existing.go_path = binding["go_path"]
existing.sub_type = binding["sub_type"]
existing.agent_id = binding["agent_id"]
existing.updated_at = now
_apply_card_fields(existing, thread, now)
async def update_task_deeplink_card(session: AsyncSession, thread_id: str, **values: Any) -> None:
"""Patch a history card without reading ``threads_meta``.
Title / status / ``updated_at`` changes on ordinary chats no-op here
because those ``thread_id`` values are absent from this table.
"""
if not values:
return
await session.execute(update(TaskDeeplinkThreadRow).where(TaskDeeplinkThreadRow.thread_id == thread_id).values(**values))
async def delete_task_deeplink_index(session: AsyncSession, thread_id: str) -> None:
@@ -65,41 +99,30 @@ class TaskDeeplinkThreadRepository:
go_path: str,
*,
limit: int = 100,
sub_type: str | None = None,
) -> list[dict[str, Any]]:
"""List one task/goPath partition without scanning thread metadata JSON."""
"""List one task/goPath partition from the dedicated card table only.
# Local import avoids a package-initialization cycle: thread_meta.sql
# imports the sync helpers above for atomic dual writes.
from deerflow.persistence.thread_meta.model import ThreadMetaRow
``sub_type`` narrows the partition to one b5-1 ``?type=`` value when
provided (an explicit "" selects the no-type partition); ``None`` keeps
the untyped legacy behaviour of returning every sub-type at once.
"""
conditions = [
TaskDeeplinkThreadRow.task_id == task_id,
TaskDeeplinkThreadRow.go_path == go_path,
]
if sub_type is not None:
conditions.append(TaskDeeplinkThreadRow.sub_type == sub_type)
stmt = (
select(TaskDeeplinkThreadRow, ThreadMetaRow)
.join(
ThreadMetaRow,
ThreadMetaRow.thread_id == TaskDeeplinkThreadRow.thread_id,
)
.where(
TaskDeeplinkThreadRow.task_id == task_id,
TaskDeeplinkThreadRow.go_path == go_path,
)
select(TaskDeeplinkThreadRow)
.where(*conditions)
.order_by(
ThreadMetaRow.updated_at.desc(),
ThreadMetaRow.thread_id.desc(),
TaskDeeplinkThreadRow.updated_at.desc(),
TaskDeeplinkThreadRow.thread_id.desc(),
)
.limit(limit)
)
async with self._sf() as session:
rows = (await session.execute(stmt)).all()
return [
{
"thread_id": thread.thread_id,
"status": thread.status,
"created_at": thread.created_at,
"updated_at": thread.updated_at,
"metadata": thread.metadata_json or {},
"values": ({"title": thread.display_name} if thread.display_name else {}),
"interrupts": {},
}
for _index, thread in rows
]
rows = (await session.execute(stmt)).scalars().all()
return [_card_payload(row) for row in rows]