sync update from LAN repo (source master-nj @ 067eb808)

This commit is contained in:
gozeros committed 2026-11-10 14:04:09 +08:00
1 parent 1244fb1f2f
commit 1d5dd8fc90
182 files changed
+18973 -769

No files matched your search

@@ -96,9 +96,11 @@ _BUSY_PATTERNS = (
"稍后重试",
"请稍后重试",
)
_QUOTA_PATTERNS = (
"insufficient_quota",
"quota",
_QUOTA_PATTERNS = (
"insufficient_quota",
"insufficient balance",
"token plan limit",
"quota",
"billing",
"credit",
"payment",
@@ -122,8 +124,10 @@ _AUTH_PATTERNS = (
# error wrapped as `bad_response_status_code`, or a truncated/garbled inference
# response). These are almost always transient inference-side issues and safe
# to retry.
_UPSTREAM_GARBAGE_PATTERNS = (
"bad_response_status_code",
_UPSTREAM_GARBAGE_PATTERNS = (
"openai-compatible upstream application error",
"null value for 'choices'",
"bad_response_status_code",
"bad response status",
"expecting ',' delimiter",
"expecting value",
@@ -18,7 +18,7 @@ LLM 主动写入走 ``memory`` / ``hindsight_*`` 工具,不经过本中间件。
import asyncio
import logging
from typing import TYPE_CHECKING, Any, override
from typing import Any, override
from langchain.agents import AgentState
from langchain.agents.middleware import AgentMiddleware
@@ -28,12 +28,9 @@ from langgraph.runtime import Runtime
from deerflow.agents.memory.manager import build_memory_manager
from deerflow.agents.memory.message_processing import count_user_turns, filter_messages_for_memory
from deerflow.config.memory_config import get_effective_memory_config, get_memory_config
from deerflow.config.memory_config import get_effective_memory_config
from deerflow.runtime.user_context import get_effective_user_id
if TYPE_CHECKING:
from deerflow.config.memory_config import MemoryConfig
logger = logging.getLogger(__name__)
@@ -90,17 +87,15 @@ class MemoryMiddleware(AgentMiddleware[MemoryMiddlewareState]):
state_schema = MemoryMiddlewareState
def __init__(self, agent_name: str | None = None, *, memory_config: "MemoryConfig | None" = None, injection_enabled: bool = True):
def __init__(self, agent_name: str | None = None, *, injection_enabled: bool = True):
"""初始化 MemoryMiddleware。
Args:
agent_name: 提供时记忆按 agent 隔离;None 用默认 agent。
memory_config: 显式记忆配置;省略时用全局配置。
injection_enabled: 运行时开关;``False`` 时跳过 Hindsight 动态召回注入。
"""
super().__init__()
self._agent_name = agent_name
self._memory_config = memory_config
self._injection_enabled = injection_enabled
# ---- 运行时上下文解析 -------------------------------------------------
@@ -150,10 +145,6 @@ class MemoryMiddleware(AgentMiddleware[MemoryMiddlewareState]):
return None
if not self._injection_enabled:
return None
config = self._memory_config or get_memory_config()
if not config.enabled:
return None
user_id = get_effective_user_id()
effective_config = get_effective_memory_config(user_id)
if not effective_config.enabled or not effective_config.injection.enabled:
@@ -221,13 +212,13 @@ class MemoryMiddleware(AgentMiddleware[MemoryMiddlewareState]):
Returns:
None (本中间件不改状态)。
"""
config = self._memory_config or get_memory_config()
if not config.enabled:
return None
user_id = get_effective_user_id()
effective_config = get_effective_memory_config(user_id)
if effective_config.provider != "hindsight" or not effective_config.hindsight.auto_retain:
if (
not effective_config.enabled
or effective_config.provider != "hindsight"
or not effective_config.hindsight.auto_retain
):
return None
messages = state.get("messages", [])
@@ -5,8 +5,9 @@ Unlike LangChain's stock ``SummarizationMiddleware`` (which runs in
to the checkpoint — wiping the original Q&A from both the model context *and*
the conversation the frontend renders), this middleware compresses the history
**transiently in ``wrap_model_call``**. The full original messages stay in state
(so the UI keeps showing the user's real conversation); only the message list
handed to the model for a single call is replaced with ``[summary, *recent]``.
(so the UI keeps showing the user's real conversation); only the request handed
to the model is replaced with a merged leading system context plus recent
messages.
The generated summary is cached per-thread so we don't pay an extra LLM call on
every model invocation: once a thread is summarized, the cached summary is reused
@@ -39,6 +40,8 @@ from langchain_core.messages import (
from langgraph.config import get_config
from langgraph.runtime import Runtime
from deerflow.models.message_ordering import normalize_model_request_messages
logger = logging.getLogger(__name__)
# Upper bound on the per-thread summary cache so a long-lived process serving
@@ -321,7 +324,14 @@ class DeerFlowSummarizationMiddleware(SummarizationMiddleware):
) -> ModelCallResult:
compressed = self._compressed_messages(request)
if compressed is not None:
request = request.override(messages=compressed)
system_message, messages = normalize_model_request_messages(
request.system_message,
compressed,
)
request = request.override(
system_message=system_message,
messages=messages,
)
return handler(request)
@override
@@ -332,7 +342,14 @@ class DeerFlowSummarizationMiddleware(SummarizationMiddleware):
) -> ModelCallResult:
compressed = await self._acompressed_messages(request)
if compressed is not None:
request = request.override(messages=compressed)
system_message, messages = normalize_model_request_messages(
request.system_message,
compressed,
)
request = request.override(
system_message=system_message,
messages=messages,
)
return await handler(request)
def _compressed_messages(self, request: ModelRequest) -> list[AnyMessage] | None: