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

This commit is contained in:
gozeros committed 2026-10-21 15:06:08 +08:00
1 parent ab6068a7c1
commit 42f2a26fa8
1482 files changed
+227759 -9584

No files matched your search

@@ -0,0 +1,34 @@
"""Dedicated taskId + goPath conversation-history index."""
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from langgraph.store.base import BaseStore
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from deerflow.persistence.thread_meta.base import ThreadMetaStore
__all__ = ["make_task_deeplink_thread_store"]
def make_task_deeplink_thread_store(
session_factory: async_sessionmaker[AsyncSession] | None,
langgraph_store: BaseStore,
thread_store: ThreadMetaStore,
):
# Keep imports local: ThreadMetaStore's in-memory implementation imports
# the sync helpers from this package, so eager imports here would form a
# package-initialization cycle.
if session_factory is not None:
from deerflow.persistence.task_deeplink_threads.sql import (
TaskDeeplinkThreadRepository,
)
return TaskDeeplinkThreadRepository(session_factory)
from deerflow.persistence.task_deeplink_threads.memory import (
MemoryTaskDeeplinkThreadStore,
)
return MemoryTaskDeeplinkThreadStore(langgraph_store, thread_store)
@@ -0,0 +1,46 @@
"""Shared helpers for task deep-link conversation indexing.
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.
"""
from __future__ import annotations
from typing import Any, TypedDict
TASK_DEEPLINK_GO_PATHS = frozenset({"3qfx", "rwfx", "xdfx", "b5-1"})
class TaskDeeplinkBinding(TypedDict):
task_id: str
source_task_id: str
go_path: str
agent_id: str | None
def task_deeplink_binding(metadata: dict[str, Any] | None) -> TaskDeeplinkBinding | None:
"""Extract a valid dedicated-history binding from thread metadata.
``taskRecordId`` is the original deep-link ``?taskId=``. It equals the
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.
"""
md = metadata or {}
go_path = str(md.get("taskGoPath") or "").strip().lower()
if go_path not in TASK_DEEPLINK_GO_PATHS:
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
return {
"task_id": task_id,
"source_task_id": source_task_id,
"go_path": go_path,
"agent_id": agent_id,
}
@@ -0,0 +1,86 @@
"""In-memory task deep-link index backed by LangGraph BaseStore."""
from __future__ import annotations
from typing import TYPE_CHECKING, Any
from langgraph.store.base import BaseStore
from deerflow.persistence.task_deeplink_threads.base import task_deeplink_binding
from deerflow.utils.time import now_iso
if TYPE_CHECKING:
from deerflow.persistence.thread_meta.base import ThreadMetaStore
TASK_DEEPLINK_THREADS_NS: tuple[str, ...] = ("task_deeplink_threads",)
async def sync_task_deeplink_index(store: BaseStore, thread: dict[str, Any]) -> None:
metadata = thread.get("metadata") or {}
binding = task_deeplink_binding(metadata)
thread_id = str(thread.get("thread_id") or "")
if not thread_id:
return
if binding is None:
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(),
},
)
async def delete_task_deeplink_index(store: BaseStore, thread_id: str) -> None:
await store.adelete(TASK_DEEPLINK_THREADS_NS, thread_id)
class MemoryTaskDeeplinkThreadStore:
def __init__(self, store: BaseStore, thread_store: ThreadMetaStore) -> None:
self._store = store
self._thread_store = thread_store
async def list_threads(
self,
task_id: str,
go_path: str,
*,
limit: int = 100,
) -> list[dict[str, Any]]:
items = await self._store.asearch(
TASK_DEEPLINK_THREADS_NS,
filter={"task_id": task_id, "go_path": go_path},
limit=limit,
)
rows: list[dict[str, Any]] = []
for item in items:
thread_id = str(item.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
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 {}),
"interrupts": {},
}
)
rows.sort(
key=lambda row: (str(row.get("updated_at") or ""), row["thread_id"]),
reverse=True,
)
return rows[:limit]
@@ -0,0 +1,48 @@
"""Dedicated lookup rows for single-agent task deep-link conversations."""
from __future__ import annotations
from datetime import UTC, datetime
from sqlalchemy import Index, String
from sqlalchemy.orm import Mapped, mapped_column
from deerflow.persistence.base import Base
from deerflow.persistence.types import BeijingDateTime
class TaskDeeplinkThreadRow(Base):
"""One task-workspace history entry 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.
"""
__tablename__ = "task_deeplink_threads"
__table_args__ = (
Index(
"ix_task_deeplink_threads_scope",
"task_id",
"go_path",
"updated_at",
"thread_id",
),
)
thread_id: Mapped[str] = mapped_column(String(64), primary_key=True)
# Original URL ?taskId=. For xdfx this is the action id by product contract.
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)
agent_id: Mapped[str | None] = mapped_column(String(128), nullable=True)
# 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))
updated_at: Mapped[datetime] = mapped_column(
BeijingDateTime(),
nullable=False,
default=lambda: datetime.now(UTC),
onupdate=lambda: datetime.now(UTC),
)
@@ -0,0 +1,105 @@
"""SQL implementation of the dedicated task deep-link conversation index."""
from __future__ import annotations
from datetime import UTC, datetime
from typing import TYPE_CHECKING, Any
from sqlalchemy import delete, select
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from deerflow.persistence.task_deeplink_threads.base import task_deeplink_binding
from deerflow.persistence.task_deeplink_threads.model import TaskDeeplinkThreadRow
if TYPE_CHECKING:
from deerflow.persistence.thread_meta.model import ThreadMetaRow
async def sync_task_deeplink_index(session: AsyncSession, thread: ThreadMetaRow) -> None:
"""Synchronize one index row inside the caller's thread-meta transaction."""
metadata = thread.metadata_json or {}
binding = task_deeplink_binding(metadata)
if binding is None:
# Ordinary chats take no extra query. Only an explicitly cleared/invalid
# deep-link marker can require removal of a previously registered row.
if "taskGoPath" in metadata or "taskRecordId" in metadata:
await session.execute(delete(TaskDeeplinkThreadRow).where(TaskDeeplinkThreadRow.thread_id == thread.thread_id))
return
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,
)
)
return
existing.task_id = binding["task_id"]
existing.source_task_id = binding["source_task_id"]
existing.go_path = binding["go_path"]
existing.agent_id = binding["agent_id"]
existing.updated_at = now
async def delete_task_deeplink_index(session: AsyncSession, thread_id: str) -> None:
await session.execute(delete(TaskDeeplinkThreadRow).where(TaskDeeplinkThreadRow.thread_id == thread_id))
class TaskDeeplinkThreadRepository:
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
self._sf = session_factory
async def list_threads(
self,
task_id: str,
go_path: str,
*,
limit: int = 100,
) -> list[dict[str, Any]]:
"""List one task/goPath partition without scanning thread metadata JSON."""
# 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
stmt = (
select(TaskDeeplinkThreadRow, ThreadMetaRow)
.join(
ThreadMetaRow,
ThreadMetaRow.thread_id == TaskDeeplinkThreadRow.thread_id,
)
.where(
TaskDeeplinkThreadRow.task_id == task_id,
TaskDeeplinkThreadRow.go_path == go_path,
)
.order_by(
ThreadMetaRow.updated_at.desc(),
ThreadMetaRow.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
]