feat(apps): 新增 DeerFlow 应用脚手架,并修复 store 的 database 回退
apps/:新增「基于 DeerFlow 的应用」目录,与 backend/、frontend/ 平级, 位于「app 消费 deerflow、不反向依赖」边界的正确侧。含两种集成示例: - examples/http-chat —— HTTP Gateway (REST+SSE),含登录/CSRF/建线程/流式对话 - examples/embedded-chat —— 进程内直接调 DeerFlowClient README 说明边界规则、两种模式、鉴权流程及新建应用约定。 runtime/store:修复 make_store 缺失的 database 段回退。原先 store 工厂只读 legacy 的 checkpointer 段,导致仅配 database:postgres 时,checkpointer 走了 Postgres、但 store 仍回退 InMemoryStore(并打出误导性的「线程列表会丢失」告警, 实际线程在 threads_meta 表里、本就持久)。现对齐 checkpointer 工厂的优先级: checkpointer 段 → database 段 → InMemoryStore;postgres 分支同样剥掉 +asyncpg 方言前缀,使一个 DATABASE_URL 同时满足 SQLAlchemy 与 LangGraph 的 psycopg store。 告警文案也修正为「跨线程 store 数据会丢失」。 tests:新增 test_store_provider.py(3 例,TDD)覆盖 database→postgres 回退、 无配置时的内存回退、以及 checkpointer 段优先级。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -19,6 +19,7 @@ from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import logging
|
||||
import re
|
||||
from collections.abc import AsyncIterator
|
||||
|
||||
from langgraph.store.base import BaseStore
|
||||
@@ -80,6 +81,57 @@ async def _async_store(config) -> AsyncIterator[BaseStore]:
|
||||
raise ValueError(f"Unknown store backend type: {config.type!r}")
|
||||
|
||||
|
||||
@contextlib.asynccontextmanager
|
||||
async def _async_store_from_database(db_config) -> AsyncIterator[BaseStore]:
|
||||
"""Async context manager that constructs a Store from a unified DatabaseConfig.
|
||||
|
||||
Mirrors :func:`deerflow.runtime.checkpointer.async_provider._async_checkpointer_from_database`
|
||||
so the store and checkpointer share one ``database`` section.
|
||||
"""
|
||||
if db_config.backend == "memory":
|
||||
from langgraph.store.memory import InMemoryStore
|
||||
|
||||
logger.info("Store: using InMemoryStore (in-process, not persistent)")
|
||||
yield InMemoryStore()
|
||||
return
|
||||
|
||||
if db_config.backend == "sqlite":
|
||||
try:
|
||||
from langgraph.store.sqlite.aio import AsyncSqliteStore
|
||||
except ImportError as exc:
|
||||
raise ImportError(SQLITE_STORE_INSTALL) from exc
|
||||
|
||||
conn_str = db_config.sqlite_path
|
||||
ensure_sqlite_parent_dir(conn_str)
|
||||
async with AsyncSqliteStore.from_conn_string(conn_str) as store:
|
||||
await store.setup()
|
||||
logger.info("Store: using AsyncSqliteStore (%s)", conn_str)
|
||||
yield store
|
||||
return
|
||||
|
||||
if db_config.backend == "postgres":
|
||||
try:
|
||||
from langgraph.store.postgres.aio import AsyncPostgresStore # type: ignore[import]
|
||||
except ImportError as exc:
|
||||
raise ImportError(POSTGRES_STORE_INSTALL) from exc
|
||||
|
||||
if not db_config.postgres_url:
|
||||
raise ValueError(POSTGRES_CONN_REQUIRED)
|
||||
|
||||
# LangGraph's AsyncPostgresStore wraps psycopg and expects a libpq-style
|
||||
# conninfo (`postgresql://...`). DeerFlow's SQLAlchemy engine uses the
|
||||
# same URL with the `+asyncpg` dialect prefix — strip it so one
|
||||
# DATABASE_URL satisfies both paths (same as the checkpointer factory).
|
||||
lg_conn_str = re.sub(r"^postgresql\+\w+://", "postgresql://", db_config.postgres_url)
|
||||
async with AsyncPostgresStore.from_conn_string(lg_conn_str) as store:
|
||||
await store.setup()
|
||||
logger.info("Store: using AsyncPostgresStore")
|
||||
yield store
|
||||
return
|
||||
|
||||
raise ValueError(f"Unknown database backend: {db_config.backend!r}")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Public async context manager
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -97,18 +149,29 @@ async def make_store(app_config: AppConfig | None = None) -> AsyncIterator[BaseS
|
||||
async with make_store(app_config) as store:
|
||||
app.state.store = store
|
||||
|
||||
Yields an :class:`~langgraph.store.memory.InMemoryStore` when no
|
||||
``checkpointer`` section is configured (emits a WARNING in that case).
|
||||
Priority (mirrors the checkpointer factory):
|
||||
1. Legacy ``checkpointer:`` config section (backward compatible)
|
||||
2. Unified ``database:`` config section
|
||||
3. Default InMemoryStore (emits a WARNING)
|
||||
"""
|
||||
if app_config is None:
|
||||
app_config = get_app_config()
|
||||
|
||||
if app_config.checkpointer is None:
|
||||
from langgraph.store.memory import InMemoryStore
|
||||
|
||||
logger.warning("No 'checkpointer' section in config.yaml — using InMemoryStore for the store. Thread list will be lost on server restart. Configure a sqlite or postgres backend for persistence.")
|
||||
yield InMemoryStore()
|
||||
# Legacy: standalone checkpointer config takes precedence
|
||||
if app_config.checkpointer is not None:
|
||||
async with _async_store(app_config.checkpointer) as store:
|
||||
yield store
|
||||
return
|
||||
|
||||
async with _async_store(app_config.checkpointer) as store:
|
||||
yield store
|
||||
# Unified database config
|
||||
db_config = getattr(app_config, "database", None)
|
||||
if db_config is not None and db_config.backend != "memory":
|
||||
async with _async_store_from_database(db_config) as store:
|
||||
yield store
|
||||
return
|
||||
|
||||
# Default: in-memory
|
||||
from langgraph.store.memory import InMemoryStore
|
||||
|
||||
logger.warning("No persistent store backend configured (no 'checkpointer' or 'database' section) — using InMemoryStore. Cross-thread store data will be lost on server restart. Configure a sqlite or postgres backend for persistence.")
|
||||
yield InMemoryStore()
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
"""Tests for the async Store factory's backend selection.
|
||||
|
||||
Mirrors the checkpointer factory: when no legacy ``checkpointer`` section
|
||||
is configured but a unified ``database`` section is, the store must use
|
||||
that database backend instead of silently falling back to InMemoryStore.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from deerflow.config.database_config import DatabaseConfig
|
||||
from deerflow.runtime.store.async_provider import make_store
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def anyio_backend() -> str:
|
||||
return "asyncio"
|
||||
|
||||
|
||||
class TestStoreDatabaseFallback:
|
||||
@pytest.mark.anyio
|
||||
async def test_postgres_store_from_database_when_no_checkpointer_section(self):
|
||||
"""make_store uses AsyncPostgresStore from the database section when
|
||||
no legacy checkpointer section is present. The +asyncpg dialect prefix
|
||||
is stripped so the same DATABASE_URL satisfies both SQLAlchemy and
|
||||
LangGraph's psycopg-based store."""
|
||||
mock_config = MagicMock()
|
||||
mock_config.checkpointer = None
|
||||
mock_config.database = DatabaseConfig(
|
||||
backend="postgres",
|
||||
postgres_url="postgresql+asyncpg://postgres:pw@localhost:5432/deerflow",
|
||||
)
|
||||
|
||||
mock_store = AsyncMock()
|
||||
mock_cm = AsyncMock()
|
||||
mock_cm.__aenter__.return_value = mock_store
|
||||
mock_cm.__aexit__.return_value = False
|
||||
|
||||
mock_store_cls = MagicMock()
|
||||
mock_store_cls.from_conn_string.return_value = mock_cm
|
||||
|
||||
mock_module = MagicMock()
|
||||
mock_module.AsyncPostgresStore = mock_store_cls
|
||||
|
||||
with (
|
||||
patch("deerflow.runtime.store.async_provider.get_app_config", return_value=mock_config),
|
||||
patch.dict(sys.modules, {"langgraph.store.postgres.aio": mock_module}),
|
||||
):
|
||||
async with make_store() as store:
|
||||
assert store is mock_store
|
||||
|
||||
# dialect prefix stripped to libpq conninfo
|
||||
mock_store_cls.from_conn_string.assert_called_once_with("postgresql://postgres:pw@localhost:5432/deerflow")
|
||||
mock_store.setup.assert_awaited_once()
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_memory_when_no_checkpointer_and_no_database(self):
|
||||
"""With neither a checkpointer section nor a non-memory database,
|
||||
the store falls back to InMemoryStore."""
|
||||
from langgraph.store.memory import InMemoryStore
|
||||
|
||||
mock_config = MagicMock()
|
||||
mock_config.checkpointer = None
|
||||
mock_config.database = DatabaseConfig(backend="memory")
|
||||
|
||||
with patch("deerflow.runtime.store.async_provider.get_app_config", return_value=mock_config):
|
||||
async with make_store() as store:
|
||||
assert isinstance(store, InMemoryStore)
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_checkpointer_section_takes_precedence_over_database(self):
|
||||
"""A legacy checkpointer section still wins over the database section."""
|
||||
from deerflow.config.checkpointer_config import CheckpointerConfig
|
||||
|
||||
mock_config = MagicMock()
|
||||
mock_config.checkpointer = CheckpointerConfig(type="memory")
|
||||
mock_config.database = DatabaseConfig(
|
||||
backend="postgres",
|
||||
postgres_url="postgresql+asyncpg://postgres:pw@localhost:5432/deerflow",
|
||||
)
|
||||
|
||||
from langgraph.store.memory import InMemoryStore
|
||||
|
||||
with patch("deerflow.runtime.store.async_provider.get_app_config", return_value=mock_config):
|
||||
async with make_store() as store:
|
||||
# checkpointer.type == memory → InMemoryStore, database ignored
|
||||
assert isinstance(store, InMemoryStore)
|
||||
Reference in New Issue
Block a user