mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-05-10 18:58:21 +00:00
* fix(gateway): return ISO 8601 timestamps from threads endpoints (#2594) ThreadResponse documents created_at / updated_at as ISO timestamps, matching the LangGraph Platform schema (langgraph_sdk.schema.Thread exposes them as datetime, JSON-encoded as ISO 8601). The gateway threads router was instead emitting str(time.time()) — unix-second floats — breaking frontend new Date() parsing and producing a mixed ISO/unix wire format that also corrupted the search sort order. Centralize timestamp generation in deerflow.utils.time: - now_iso() — datetime.now(UTC).isoformat() - coerce_iso(x) — heals legacy unix-timestamp strings on read so the store converges to ISO without a one-shot migration threads.py: replace 6 time.time() call sites with now_iso(); wrap all read paths and Phase-2 checkpoint metadata with coerce_iso(); _store_upsert opportunistically heals legacy created_at on update; drop unused time import. thread_runs.py: reuse now_iso() instead of a private duplicate _now_iso(), preventing future drift between the two timestamp call sites. Tests: 9 unit tests for the helper; 5 integration tests pinning the ISO contract for create/get/patch/search and the legacy-healing path on the internal store upsert. Full suite: 2144 passed, 15 skipped, 0 failed. Closes #2594 * fix(gateway): coerce checkpoint metadata timestamps to ISO on read After the merge with main, three additional read paths in ``threads.py`` were still emitting raw ``str(metadata.get("created_at", ""))`` — ``get_thread_state``, ``update_thread_state``, and ``get_thread_history``. Same root cause as #2594: when the checkpoint metadata's ``created_at`` is a unix-second float (legacy data, or a checkpoint written by an older Gateway version), ``str(float)`` produces ``"1777252410.411327"`` and the frontend's ``new Date(...)`` returns ``Invalid Date``. The fix on the ``/threads/{id}`` GET path was already in place; these three sibling endpoints needed the same treatment. All four call sites now flow through ``coerce_iso``, so: - legacy float metadata heals to ISO on the way out, - ISO metadata passes through unchanged, - ``datetime`` instances (which the new ``coerce_iso`` branch handles explicitly) emit with the ``T`` separator instead of falling through to the space-separated ``str(datetime)`` form. Coverage added for the two endpoints not already pinned by the merge: - ``test_get_thread_state_returns_iso_for_legacy_checkpoint_metadata`` - ``test_get_thread_history_returns_iso_for_legacy_checkpoint_metadata`` Both pre-seed a checkpoint whose metadata carries the literal float from the issue body and assert the wire format is ISO.
152 lines
5.8 KiB
Python
152 lines
5.8 KiB
Python
"""In-memory ThreadMetaStore backed by LangGraph BaseStore.
|
|
|
|
Used when database.backend=memory. Delegates to the LangGraph Store's
|
|
``("threads",)`` namespace — the same namespace used by the Gateway
|
|
router for thread records.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from langgraph.store.base import BaseStore
|
|
|
|
from deerflow.persistence.thread_meta.base import ThreadMetaStore
|
|
from deerflow.runtime.user_context import AUTO, _AutoSentinel, resolve_user_id
|
|
from deerflow.utils.time import coerce_iso, now_iso
|
|
|
|
THREADS_NS: tuple[str, ...] = ("threads",)
|
|
|
|
|
|
class MemoryThreadMetaStore(ThreadMetaStore):
|
|
def __init__(self, store: BaseStore) -> None:
|
|
self._store = store
|
|
|
|
async def _get_owned_record(
|
|
self,
|
|
thread_id: str,
|
|
user_id: str | None | _AutoSentinel,
|
|
method_name: str,
|
|
) -> dict | None:
|
|
"""Fetch a record and verify ownership. Returns a mutable copy, or None."""
|
|
resolved = resolve_user_id(user_id, method_name=method_name)
|
|
item = await self._store.aget(THREADS_NS, thread_id)
|
|
if item is None:
|
|
return None
|
|
record = dict(item.value)
|
|
if resolved is not None and record.get("user_id") != resolved:
|
|
return None
|
|
return record
|
|
|
|
async def create(
|
|
self,
|
|
thread_id: str,
|
|
*,
|
|
assistant_id: str | None = None,
|
|
user_id: str | None | _AutoSentinel = AUTO,
|
|
display_name: str | None = None,
|
|
metadata: dict | None = None,
|
|
) -> dict:
|
|
resolved_user_id = resolve_user_id(user_id, method_name="MemoryThreadMetaStore.create")
|
|
now = now_iso()
|
|
record: dict[str, Any] = {
|
|
"thread_id": thread_id,
|
|
"assistant_id": assistant_id,
|
|
"user_id": resolved_user_id,
|
|
"display_name": display_name,
|
|
"status": "idle",
|
|
"metadata": metadata or {},
|
|
"values": {},
|
|
"created_at": now,
|
|
"updated_at": now,
|
|
}
|
|
await self._store.aput(THREADS_NS, thread_id, record)
|
|
return record
|
|
|
|
async def get(self, thread_id: str, *, user_id: str | None | _AutoSentinel = AUTO) -> dict | None:
|
|
return await self._get_owned_record(thread_id, user_id, "MemoryThreadMetaStore.get")
|
|
|
|
async def search(
|
|
self,
|
|
*,
|
|
metadata: dict | None = None,
|
|
status: str | None = None,
|
|
limit: int = 100,
|
|
offset: int = 0,
|
|
user_id: str | None | _AutoSentinel = AUTO,
|
|
) -> list[dict]:
|
|
resolved_user_id = resolve_user_id(user_id, method_name="MemoryThreadMetaStore.search")
|
|
filter_dict: dict[str, Any] = {}
|
|
if metadata:
|
|
filter_dict.update(metadata)
|
|
if status:
|
|
filter_dict["status"] = status
|
|
if resolved_user_id is not None:
|
|
filter_dict["user_id"] = resolved_user_id
|
|
|
|
items = await self._store.asearch(
|
|
THREADS_NS,
|
|
filter=filter_dict or None,
|
|
limit=limit,
|
|
offset=offset,
|
|
)
|
|
return [self._item_to_dict(item) for item in items]
|
|
|
|
async def check_access(self, thread_id: str, user_id: str, *, require_existing: bool = False) -> bool:
|
|
item = await self._store.aget(THREADS_NS, thread_id)
|
|
if item is None:
|
|
return not require_existing
|
|
record_user_id = item.value.get("user_id")
|
|
if record_user_id is None:
|
|
return True
|
|
return record_user_id == user_id
|
|
|
|
async def update_display_name(self, thread_id: str, display_name: str, *, user_id: str | None | _AutoSentinel = AUTO) -> None:
|
|
record = await self._get_owned_record(thread_id, user_id, "MemoryThreadMetaStore.update_display_name")
|
|
if record is None:
|
|
return
|
|
record["display_name"] = display_name
|
|
record["updated_at"] = now_iso()
|
|
await self._store.aput(THREADS_NS, thread_id, record)
|
|
|
|
async def update_status(self, thread_id: str, status: str, *, user_id: str | None | _AutoSentinel = AUTO) -> None:
|
|
record = await self._get_owned_record(thread_id, user_id, "MemoryThreadMetaStore.update_status")
|
|
if record is None:
|
|
return
|
|
record["status"] = status
|
|
record["updated_at"] = now_iso()
|
|
await self._store.aput(THREADS_NS, thread_id, record)
|
|
|
|
async def update_metadata(self, thread_id: str, metadata: dict, *, user_id: str | None | _AutoSentinel = AUTO) -> None:
|
|
record = await self._get_owned_record(thread_id, user_id, "MemoryThreadMetaStore.update_metadata")
|
|
if record is None:
|
|
return
|
|
merged = dict(record.get("metadata") or {})
|
|
merged.update(metadata)
|
|
record["metadata"] = merged
|
|
record["updated_at"] = now_iso()
|
|
await self._store.aput(THREADS_NS, thread_id, record)
|
|
|
|
async def delete(self, thread_id: str, *, user_id: str | None | _AutoSentinel = AUTO) -> None:
|
|
record = await self._get_owned_record(thread_id, user_id, "MemoryThreadMetaStore.delete")
|
|
if record is None:
|
|
return
|
|
await self._store.adelete(THREADS_NS, thread_id)
|
|
|
|
@staticmethod
|
|
def _item_to_dict(item) -> dict[str, Any]:
|
|
"""Convert a Store SearchItem to the dict format expected by callers."""
|
|
val = item.value
|
|
return {
|
|
"thread_id": item.key,
|
|
"assistant_id": val.get("assistant_id"),
|
|
"user_id": val.get("user_id"),
|
|
"display_name": val.get("display_name"),
|
|
"status": val.get("status", "idle"),
|
|
"metadata": val.get("metadata", {}),
|
|
# ``coerce_iso`` heals legacy unix-second values written by
|
|
# earlier Gateway versions that called ``str(time.time())``.
|
|
"created_at": coerce_iso(val.get("created_at", "")),
|
|
"updated_at": coerce_iso(val.get("updated_at", "")),
|
|
}
|