sync update from LAN repo (source master-yx @ cf505220)
This commit is contained in:
1 parent
e243ec3a46
commit
19d3cb96dc
114 files changed
+14787
-451
No files matched your search
+9
-5
@@ -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",
|
||||
|
||||
+21
-4
@@ -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:
|
||||
|
||||
@@ -211,6 +211,7 @@ class AppConfig(BaseModel):
|
||||
cls._check_config_version(config_data, resolved_path)
|
||||
|
||||
config_data = cls.resolve_env_variables(config_data)
|
||||
cls._resolve_relative_mount_paths(config_data, resolved_path.parent)
|
||||
cls._apply_database_defaults(config_data)
|
||||
|
||||
# Load title config if present
|
||||
@@ -272,6 +273,36 @@ class AppConfig(BaseModel):
|
||||
result = cls.model_validate(config_data)
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def _resolve_relative_mount_paths(
|
||||
cls,
|
||||
config_data: dict[str, Any],
|
||||
config_dir: Path,
|
||||
) -> None:
|
||||
"""Resolve portable sandbox mount paths beside ``config.yaml``.
|
||||
|
||||
Deployment copies frequently live at different absolute paths. A
|
||||
relative ``sandbox.mounts[*].host_path`` therefore means "relative to
|
||||
the config file", never relative to the process working directory.
|
||||
Providers continue to receive absolute paths, which keeps both the
|
||||
local path mapper and Docker bind mounts deterministic.
|
||||
"""
|
||||
sandbox = config_data.get("sandbox")
|
||||
if not isinstance(sandbox, dict):
|
||||
return
|
||||
mounts = sandbox.get("mounts")
|
||||
if not isinstance(mounts, list):
|
||||
return
|
||||
for mount in mounts:
|
||||
if not isinstance(mount, dict):
|
||||
continue
|
||||
raw_host_path = mount.get("host_path")
|
||||
if not isinstance(raw_host_path, str) or not raw_host_path.strip():
|
||||
continue
|
||||
host_path = Path(raw_host_path)
|
||||
if not host_path.is_absolute():
|
||||
mount["host_path"] = str((config_dir / host_path).resolve())
|
||||
|
||||
@classmethod
|
||||
def _apply_database_defaults(cls, config_data: dict[str, Any]) -> None:
|
||||
"""Apply config.yaml defaults for persistence when the section is absent."""
|
||||
|
||||
@@ -77,6 +77,7 @@ class LocalWikiEmbeddingConfig(BaseModel):
|
||||
base_url: str = ""
|
||||
api_key: str = Field(default="", repr=False)
|
||||
local_model_path: str = ""
|
||||
local_model_revision: str = ""
|
||||
local_cache_dir: str = ""
|
||||
local_process_isolation: bool = True
|
||||
local_threads: int = Field(default=2, ge=1, le=32)
|
||||
|
||||
+31
-3
@@ -508,11 +508,33 @@ class ModelConcurrencySettings(BaseModel):
|
||||
class LocalWikiEncoderSettings(BaseModel):
|
||||
"""Runtime override for the administrator-managed offline Wiki encoder."""
|
||||
|
||||
mode: Literal["configured", "local"] = Field(
|
||||
default="configured",
|
||||
description=(
|
||||
"Use the embedding provider from config.yaml, or explicitly override it "
|
||||
"with the administrator-managed offline BGE-M3 encoder."
|
||||
),
|
||||
)
|
||||
model_path: str = Field(
|
||||
default="",
|
||||
max_length=4096,
|
||||
description="Absolute or project-relative directory containing the offline BGE-M3 ONNX model.",
|
||||
)
|
||||
model_revision: str = Field(
|
||||
default="",
|
||||
max_length=64,
|
||||
description="Opaque revision changed whenever an offline model directory is activated.",
|
||||
)
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
|
||||
class StrategyProjectSettings(BaseModel):
|
||||
"""Global visibility controls for the strategy-project experience."""
|
||||
|
||||
taiwan_strait_demo_enabled: bool = Field(
|
||||
default=False,
|
||||
description="Whether Taiwan Strait demo entry points are visible in My Projects.",
|
||||
)
|
||||
model_config = ConfigDict(extra="ignore")
|
||||
|
||||
|
||||
@@ -532,6 +554,7 @@ class SystemSettings(BaseModel):
|
||||
concurrency_monitor: ConcurrencyMonitorSettings = Field(default_factory=ConcurrencyMonitorSettings)
|
||||
model_concurrency: ModelConcurrencySettings = Field(default_factory=ModelConcurrencySettings)
|
||||
local_wiki_encoder: LocalWikiEncoderSettings = Field(default_factory=LocalWikiEncoderSettings)
|
||||
strategy_projects: StrategyProjectSettings = Field(default_factory=StrategyProjectSettings)
|
||||
llmwiki: LlmWikiRuntimeOverride = Field(default_factory=LlmWikiRuntimeOverride)
|
||||
skill_knowledge_auto_sync: SkillKnowledgeAutoSyncSettings = Field(default_factory=SkillKnowledgeAutoSyncSettings)
|
||||
knowledge_base_product_version: Literal["v1", "v2"] = Field(
|
||||
@@ -596,7 +619,12 @@ def save_system_settings(settings: SystemSettings) -> SystemSettings:
|
||||
path = _settings_path()
|
||||
payload = settings.model_dump(mode="json")
|
||||
with _lock:
|
||||
tmp = path.with_suffix(path.suffix + ".tmp")
|
||||
tmp.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
tmp.replace(path)
|
||||
# Multiple Gateway workers share this file. A unique temporary name
|
||||
# prevents their atomic replaces from racing over one common `.tmp`.
|
||||
tmp = path.with_name(f".{path.name}.{uuid4().hex}.tmp")
|
||||
try:
|
||||
tmp.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
tmp.replace(path)
|
||||
finally:
|
||||
tmp.unlink(missing_ok=True)
|
||||
return settings
|
||||
+98
-14
@@ -57,6 +57,9 @@ class StrictWikiEmbeddingClient:
|
||||
self._local_started_at: str | None = None
|
||||
self._local_ready_at: str | None = None
|
||||
self._local_warmup_task: asyncio.Task[None] | None = None
|
||||
self._lifecycle_condition = asyncio.Condition()
|
||||
self._active_uses = 0
|
||||
self._retired = False
|
||||
|
||||
@property
|
||||
def dimensions(self) -> int | None:
|
||||
@@ -64,7 +67,21 @@ class StrictWikiEmbeddingClient:
|
||||
|
||||
@property
|
||||
def fingerprint(self) -> str:
|
||||
raw = "\0".join((self.config.provider.strip().lower(), self.config.base_url.rstrip("/").lower(), self.config.model.strip(), str(self._resolved_dimensions or "auto")))
|
||||
parts = [
|
||||
self.config.provider.strip().lower(),
|
||||
self.config.base_url.rstrip("/").lower(),
|
||||
self.config.model.strip(),
|
||||
str(self._resolved_dimensions or "auto"),
|
||||
]
|
||||
if self.config.provider == "local":
|
||||
model_path = self._local_model_path()
|
||||
parts.extend(
|
||||
(
|
||||
str(model_path) if model_path is not None else "",
|
||||
self.config.local_model_revision,
|
||||
)
|
||||
)
|
||||
raw = "\0".join(parts)
|
||||
return hashlib.sha256(raw.encode("utf-8")).hexdigest()
|
||||
|
||||
async def close(self) -> None:
|
||||
@@ -73,6 +90,15 @@ class StrictWikiEmbeddingClient:
|
||||
await self._client.aclose()
|
||||
self._client = None
|
||||
|
||||
async def retire(self) -> None:
|
||||
"""Reject future work, drain active calls, then release model resources."""
|
||||
|
||||
async with self._lifecycle_condition:
|
||||
self._retired = True
|
||||
async with self._lifecycle_condition:
|
||||
await self._lifecycle_condition.wait_for(lambda: self._active_uses == 0)
|
||||
await self.close()
|
||||
|
||||
async def reset_local_encoder(self, *, model_path: str | None = None) -> None:
|
||||
"""Stop only the embedded worker and optionally switch its model path.
|
||||
|
||||
@@ -82,27 +108,80 @@ class StrictWikiEmbeddingClient:
|
||||
"""
|
||||
warmup = self._local_warmup_task
|
||||
if warmup is not None and not warmup.done():
|
||||
warmup.cancel()
|
||||
# Local inference runs in a worker thread. Cancelling the asyncio
|
||||
# wrapper does not stop that thread and could let it publish a new
|
||||
# process/model after cleanup. Drain it before touching resources.
|
||||
await asyncio.gather(warmup, return_exceptions=True)
|
||||
self._local_warmup_task = None
|
||||
process = self._local_process
|
||||
self._local_process = None
|
||||
if process is not None:
|
||||
await asyncio.to_thread(self._stop_local_process, process)
|
||||
self._local_model = None
|
||||
self._local_state = "idle"
|
||||
self._local_error = None
|
||||
self._local_started_at = None
|
||||
self._local_ready_at = None
|
||||
if model_path is not None:
|
||||
self.config = self.config.model_copy(update={"local_model_path": model_path})
|
||||
# Wait for a non-warmup embedding batch to finish before replacing or
|
||||
# stopping the local worker. This prevents an admin upload from
|
||||
# breaking a concurrent vectorization request halfway through IPC.
|
||||
async with self._local_lock:
|
||||
process = self._local_process
|
||||
self._local_process = None
|
||||
if process is not None:
|
||||
await self._run_thread_drained(self._stop_local_process, process)
|
||||
self._local_model = None
|
||||
self._local_state = "idle"
|
||||
self._local_error = None
|
||||
self._local_started_at = None
|
||||
self._local_ready_at = None
|
||||
if model_path is not None:
|
||||
self.config = self.config.model_copy(update={"local_model_path": model_path})
|
||||
|
||||
async def _get_client(self) -> httpx.AsyncClient:
|
||||
if self._client is None:
|
||||
self._client = httpx.AsyncClient(timeout=self.config.timeout_seconds)
|
||||
return self._client
|
||||
|
||||
@staticmethod
|
||||
async def _run_thread_drained(function, *args):
|
||||
"""Do not release lifecycle locks while cancelled thread work still runs."""
|
||||
|
||||
worker = asyncio.create_task(asyncio.to_thread(function, *args))
|
||||
cancelled = False
|
||||
worker_error: BaseException | None = None
|
||||
result = None
|
||||
while not worker.done():
|
||||
try:
|
||||
result = await asyncio.shield(worker)
|
||||
except asyncio.CancelledError:
|
||||
# A caller can be cancelled more than once during shutdown.
|
||||
# Swallow every cancellation until the non-cancellable thread
|
||||
# has really stopped, then restore cancellation semantics.
|
||||
cancelled = True
|
||||
except BaseException as exc:
|
||||
worker_error = exc
|
||||
break
|
||||
if worker.done() and worker_error is None:
|
||||
try:
|
||||
result = worker.result()
|
||||
except BaseException as exc:
|
||||
worker_error = exc
|
||||
if cancelled:
|
||||
raise asyncio.CancelledError from None
|
||||
if worker_error is not None:
|
||||
raise worker_error
|
||||
return result
|
||||
|
||||
async def embed_texts(self, texts: list[str]) -> list[list[float]]:
|
||||
async with self._lifecycle_condition:
|
||||
if self._retired:
|
||||
raise WikiEmbeddingError(
|
||||
"Wiki embedding runtime was replaced; retry with the active provider",
|
||||
category="runtime_retired",
|
||||
retryable=True,
|
||||
)
|
||||
self._active_uses += 1
|
||||
try:
|
||||
return await self._embed_texts_active(texts)
|
||||
finally:
|
||||
async with self._lifecycle_condition:
|
||||
self._active_uses -= 1
|
||||
if self._active_uses == 0:
|
||||
self._lifecycle_condition.notify_all()
|
||||
|
||||
async def _embed_texts_active(self, texts: list[str]) -> list[list[float]]:
|
||||
if self.config.provider == "local":
|
||||
if not texts:
|
||||
return []
|
||||
@@ -110,7 +189,10 @@ class StrictWikiEmbeddingClient:
|
||||
self._local_state = "starting"
|
||||
self._local_started_at = self._local_started_at or datetime.now(UTC).isoformat()
|
||||
try:
|
||||
values = await asyncio.to_thread(self._embed_local, texts)
|
||||
# `to_thread` keeps running after its awaiting task is
|
||||
# cancelled. Keep the lifecycle lease and local lock until
|
||||
# the real inference call has stopped.
|
||||
values = await self._run_thread_drained(self._embed_local, texts)
|
||||
except Exception as exc:
|
||||
self._local_state = "failed"
|
||||
self._local_error = " ".join(str(exc).split())[:1000] or type(exc).__name__
|
||||
@@ -452,6 +534,8 @@ class StrictWikiEmbeddingClient:
|
||||
|
||||
def start_local_encoder(self) -> dict[str, object]:
|
||||
"""Warm the embedded CPU encoder without coupling it to app startup."""
|
||||
if self._retired:
|
||||
raise ValueError("当前本地编码器运行时已切换,请刷新状态后重试")
|
||||
if self.config.provider != "local":
|
||||
raise ValueError("当前 embedding provider 不是 local")
|
||||
if not self.config.enabled:
|
||||
|
||||
+3
-1
@@ -11,7 +11,7 @@ from uuid import uuid4
|
||||
from deerflow.config.llmwiki_config import LocalWikiIndexConfig
|
||||
from deerflow.integrations.weknora.client import WeKnoraClient
|
||||
from deerflow.integrations.weknora.local_index.chunking import chunk_wiki_page
|
||||
from deerflow.integrations.weknora.local_index.embedding import StrictWikiEmbeddingClient
|
||||
from deerflow.integrations.weknora.local_index.embedding import StrictWikiEmbeddingClient, WikiEmbeddingError
|
||||
from deerflow.integrations.weknora.local_index.normalize import normalize_wiki_page
|
||||
from deerflow.integrations.weknora.local_index.vector_cache import WikiVectorCache
|
||||
from deerflow.integrations.weknora.local_index.vector_codec import encode_vector
|
||||
@@ -137,6 +137,8 @@ class WikiSyncService:
|
||||
except Exception as exc:
|
||||
if isinstance(exc, WikiSyncLeaseLost):
|
||||
raise
|
||||
if isinstance(exc, WikiEmbeddingError) and exc.category == "runtime_retired":
|
||||
raise
|
||||
failed += 1
|
||||
try:
|
||||
fallback = normalize_wiki_page(mapping_id, summary)
|
||||
|
||||
+29
-8
@@ -6,7 +6,6 @@ from typing import Any
|
||||
|
||||
from langchain_core.messages import BaseMessage, SystemMessage
|
||||
|
||||
|
||||
_SYSTEM_SEPARATOR = "\n\n---\n\n"
|
||||
|
||||
|
||||
@@ -35,7 +34,11 @@ def normalize_langchain_messages(messages: list[BaseMessage]) -> list[BaseMessag
|
||||
return messages
|
||||
|
||||
first_non_system_index = next((idx for idx, message in enumerate(messages) if not isinstance(message, SystemMessage)), len(messages))
|
||||
if len(system_messages) == first_non_system_index:
|
||||
# A number of strict chat templates accept *one* system message, and only
|
||||
# at index zero. Two adjacent leading system messages are therefore not a
|
||||
# valid no-op case: LangChain's agent prompt followed by DeerFlow's
|
||||
# transient compaction summary must still be folded into one message.
|
||||
if len(system_messages) == 1 and first_non_system_index == 1:
|
||||
return messages
|
||||
|
||||
merged = system_messages[0].model_copy(
|
||||
@@ -46,20 +49,38 @@ def normalize_langchain_messages(messages: list[BaseMessage]) -> list[BaseMessag
|
||||
return [merged, *(message for message in messages if not isinstance(message, SystemMessage))]
|
||||
|
||||
|
||||
def normalize_model_request_messages(
|
||||
system_message: SystemMessage | None,
|
||||
messages: list[BaseMessage],
|
||||
) -> tuple[SystemMessage | None, list[BaseMessage]]:
|
||||
"""Normalize LangChain's split ``ModelRequest`` message representation.
|
||||
|
||||
``ModelRequest.messages`` is documented as excluding the system prompt.
|
||||
Transient middleware (notably context compaction) can nevertheless place a
|
||||
``SystemMessage`` in that list. Fold every such message into the dedicated
|
||||
system slot so adapters without a DeerFlow-specific payload patch also
|
||||
receive exactly one leading system message.
|
||||
"""
|
||||
if system_message is None and not any(isinstance(message, SystemMessage) for message in messages):
|
||||
return None, messages
|
||||
|
||||
combined = [system_message, *messages] if system_message is not None else messages
|
||||
normalized = normalize_langchain_messages(combined)
|
||||
if normalized and isinstance(normalized[0], SystemMessage):
|
||||
return normalized[0], normalized[1:]
|
||||
return None, normalized
|
||||
|
||||
|
||||
def _normalize_payload_message_list(messages: list[Any]) -> list[Any]:
|
||||
system_messages = [message for message in messages if isinstance(message, dict) and message.get("role") == "system"]
|
||||
if not system_messages:
|
||||
return messages
|
||||
|
||||
first_non_system_index = next(
|
||||
(
|
||||
idx
|
||||
for idx, message in enumerate(messages)
|
||||
if not (isinstance(message, dict) and message.get("role") == "system")
|
||||
),
|
||||
(idx for idx, message in enumerate(messages) if not (isinstance(message, dict) and message.get("role") == "system")),
|
||||
len(messages),
|
||||
)
|
||||
if len(system_messages) == first_non_system_index:
|
||||
if len(system_messages) == 1 and first_non_system_index == 1:
|
||||
return messages
|
||||
|
||||
merged_system = dict(system_messages[0])
|
||||
|
||||
+66
-1
@@ -25,6 +25,7 @@ message that originally carried it:
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
from typing import Any
|
||||
|
||||
from langchain_core.language_models import LanguageModelInput
|
||||
@@ -36,6 +37,65 @@ from deerflow.models.message_ordering import normalize_chat_payload_messages
|
||||
from deerflow.models.reasoning_fields import apply_reasoning_from_payload, extract_reasoning_text, fill_ui_reasoning_content, restore_reasoning_fields
|
||||
|
||||
|
||||
class OpenAICompatibleUpstreamError(RuntimeError):
|
||||
"""An OpenAI-compatible gateway reported an error inside a HTTP 200 body.
|
||||
|
||||
Several compatible gateways do not use an HTTP error status for quota,
|
||||
concurrency, or inference failures. Instead they return ``choices=null``
|
||||
(or an empty streamed chunk) together with a vendor ``base_resp`` object.
|
||||
LangChain raises a generic ``TypeError`` for the non-streaming shape and
|
||||
treats the streaming shape as an empty successful response. Raising one
|
||||
stable exception here lets DeerFlow's model middleware retry both paths.
|
||||
"""
|
||||
|
||||
def __init__(self, *, code: Any, message: str | None = None) -> None:
|
||||
self.code = code
|
||||
detail = _bounded_error_message(message)
|
||||
suffix = f": {detail}" if detail else ""
|
||||
super().__init__(f"OpenAI-compatible upstream application error (code={code}){suffix}")
|
||||
|
||||
|
||||
def _bounded_error_message(value: Any, *, limit: int = 500) -> str:
|
||||
"""Return a log-safe, bounded one-line provider message."""
|
||||
if not isinstance(value, str):
|
||||
return ""
|
||||
normalized = " ".join(value.split())
|
||||
return normalized[:limit]
|
||||
|
||||
|
||||
def _application_error(payload: Mapping[str, Any]) -> tuple[Any, str | None] | None:
|
||||
"""Extract a non-success ``base_resp`` error from a compatible payload."""
|
||||
raw = payload.get("base_resp") or payload.get("baseResp")
|
||||
if not isinstance(raw, Mapping):
|
||||
return None
|
||||
|
||||
code = raw.get("status_code")
|
||||
if code is None:
|
||||
code = raw.get("statusCode")
|
||||
if code is None:
|
||||
code = raw.get("code")
|
||||
|
||||
# MiniMax-compatible responses use 0 for success. Be conservative for
|
||||
# gateways that put informational metadata in ``base_resp``.
|
||||
normalized_code = str(code).strip().lower() if code is not None else ""
|
||||
if normalized_code in {"", "0", "200", "ok", "success"}:
|
||||
return None
|
||||
|
||||
message = raw.get("status_msg")
|
||||
if message is None:
|
||||
message = raw.get("statusMsg")
|
||||
if message is None:
|
||||
message = raw.get("message")
|
||||
return code, message if isinstance(message, str) else None
|
||||
|
||||
|
||||
def _raise_for_application_error(payload: Mapping[str, Any]) -> None:
|
||||
error = _application_error(payload)
|
||||
if error is not None:
|
||||
code, message = error
|
||||
raise OpenAICompatibleUpstreamError(code=code, message=message)
|
||||
|
||||
|
||||
class PatchedChatOpenAI(ChatOpenAI):
|
||||
"""ChatOpenAI with ``thought_signature`` preservation for Gemini thinking via OpenAI gateway.
|
||||
|
||||
@@ -108,6 +168,10 @@ class PatchedChatOpenAI(ChatOpenAI):
|
||||
base_generation_info: dict | None,
|
||||
) -> ChatGenerationChunk | None:
|
||||
"""Preserve streamed ``reasoning_content`` in ``additional_kwargs``."""
|
||||
# Some compatible gateways return a vendor error event followed by a
|
||||
# normal-looking usage chunk, all under HTTP 200. Detect the first
|
||||
# event before LangChain converts it into an empty AIMessageChunk.
|
||||
_raise_for_application_error(chunk)
|
||||
generation_chunk = super()._convert_chunk_to_generation_chunk(
|
||||
chunk,
|
||||
default_chunk_class,
|
||||
@@ -133,8 +197,9 @@ class PatchedChatOpenAI(ChatOpenAI):
|
||||
generation_info: dict | None = None,
|
||||
) -> ChatResult:
|
||||
"""Preserve non-stream ``reasoning_content`` in ``additional_kwargs``."""
|
||||
result = super()._create_chat_result(response, generation_info)
|
||||
response_dict = response if isinstance(response, dict) else response.model_dump()
|
||||
_raise_for_application_error(response_dict)
|
||||
result = super()._create_chat_result(response, generation_info)
|
||||
choices = response_dict.get("choices", [])
|
||||
|
||||
generations: list[ChatGeneration] = []
|
||||
|
||||
+2
@@ -125,6 +125,7 @@ from deerflow.persistence.skill_knowledge.model import (
|
||||
SkillKnowledgeSyncJobRow,
|
||||
)
|
||||
from deerflow.persistence.skills.model import SkillDisplayAdapterRow, SkillFavoriteRow, SkillRow
|
||||
from deerflow.persistence.strategy_agent_roles.model import StrategyAgentRoleRow
|
||||
from deerflow.persistence.tags.model import TagAssignmentRow, TagRow
|
||||
from deerflow.persistence.task_buttons.model import TaskButtonRow
|
||||
from deerflow.persistence.task_reports.model import TaskReportRow
|
||||
@@ -254,6 +255,7 @@ __all__ = [
|
||||
"SkillKnowledgeSnapshotRow",
|
||||
"SkillKnowledgeSyncItemRow",
|
||||
"SkillKnowledgeSyncJobRow",
|
||||
"StrategyAgentRoleRow",
|
||||
"TagAssignmentRow",
|
||||
"TagRow",
|
||||
"TaskButtonRow",
|
||||
|
||||
+33
-20
@@ -14,7 +14,7 @@ import json
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import select, update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.parallel_agent_tasks.model import ParallelAgentTaskRow
|
||||
@@ -59,12 +59,7 @@ class ParallelAgentTaskRepository:
|
||||
|
||||
async def list_tasks(self, user_id: str, *, limit: int = 200) -> list[dict[str, Any]]:
|
||||
"""列出当前用户的任务(轻量元信息,不含 messages,供历史列表)。"""
|
||||
stmt = (
|
||||
select(ParallelAgentTaskRow)
|
||||
.where(ParallelAgentTaskRow.user_id == user_id)
|
||||
.order_by(ParallelAgentTaskRow.updated_at.desc())
|
||||
.limit(limit)
|
||||
)
|
||||
stmt = select(ParallelAgentTaskRow).where(ParallelAgentTaskRow.user_id == user_id).order_by(ParallelAgentTaskRow.updated_at.desc()).limit(limit)
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(stmt)
|
||||
return [self._to_dict(row, with_messages=False) for row in result.scalars()]
|
||||
@@ -103,22 +98,40 @@ class ParallelAgentTaskRepository:
|
||||
brief=_UNSET,
|
||||
agents=_UNSET,
|
||||
messages=_UNSET,
|
||||
expected_updated_at: datetime | str | None = None,
|
||||
) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(ParallelAgentTaskRow, task_id)
|
||||
if row is None or row.user_id != user_id:
|
||||
expected_revision = expected_updated_at
|
||||
if isinstance(expected_revision, str):
|
||||
try:
|
||||
expected_revision = datetime.fromisoformat(expected_revision.replace("Z", "+00:00"))
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
values: dict[str, Any] = {"updated_at": datetime.now(UTC)}
|
||||
if title is not _UNSET and title is not None:
|
||||
values["title"] = str(title).strip()
|
||||
if brief is not _UNSET:
|
||||
values["brief"] = brief or None
|
||||
if agents is not _UNSET:
|
||||
values["agents"] = _dumps(agents)
|
||||
if messages is not _UNSET:
|
||||
values["messages"] = _dumps(messages)
|
||||
|
||||
async with self._sf() as session:
|
||||
statement = update(ParallelAgentTaskRow).where(
|
||||
ParallelAgentTaskRow.id == task_id,
|
||||
ParallelAgentTaskRow.user_id == user_id,
|
||||
)
|
||||
if expected_revision is not None:
|
||||
statement = statement.where(ParallelAgentTaskRow.updated_at == expected_revision)
|
||||
result = await session.execute(statement.values(**values))
|
||||
if result.rowcount != 1:
|
||||
await session.rollback()
|
||||
return None
|
||||
if title is not _UNSET and title is not None:
|
||||
row.title = str(title).strip()
|
||||
if brief is not _UNSET:
|
||||
row.brief = brief or None
|
||||
if agents is not _UNSET:
|
||||
row.agents = _dumps(agents)
|
||||
if messages is not _UNSET:
|
||||
row.messages = _dumps(messages)
|
||||
row.updated_at = datetime.now(UTC)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
row = await session.get(ParallelAgentTaskRow, task_id)
|
||||
if row is None:
|
||||
return None
|
||||
return self._to_dict(row)
|
||||
|
||||
async def delete_task(self, task_id: str, user_id: str) -> bool:
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
"""Global strategy-selection agent role registry."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from deerflow.persistence.strategy_agent_roles.base import StrategyAgentRoleStore
|
||||
from deerflow.persistence.strategy_agent_roles.memory import (
|
||||
MemoryStrategyAgentRoleStore,
|
||||
)
|
||||
from deerflow.persistence.strategy_agent_roles.model import StrategyAgentRoleRow
|
||||
from deerflow.persistence.strategy_agent_roles.sql import StrategyAgentRoleRepository
|
||||
|
||||
__all__ = [
|
||||
"MemoryStrategyAgentRoleStore",
|
||||
"StrategyAgentRoleRepository",
|
||||
"StrategyAgentRoleRow",
|
||||
"StrategyAgentRoleStore",
|
||||
"make_strategy_agent_role_store",
|
||||
]
|
||||
|
||||
|
||||
def make_strategy_agent_role_store(session_factory):
|
||||
if session_factory is None:
|
||||
return MemoryStrategyAgentRoleStore()
|
||||
return StrategyAgentRoleRepository(session_factory)
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
"""Abstract store for the global strategy-selection agent registry."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any
|
||||
|
||||
|
||||
class StrategyAgentRoleStore(ABC):
|
||||
@abstractmethod
|
||||
async def ensure_defaults(self, rows: list[dict[str, Any]]) -> int:
|
||||
"""Insert missing default rows without re-enabling disabled angles."""
|
||||
|
||||
@abstractmethod
|
||||
async def list_roles(self, *, include_disabled: bool) -> list[dict[str, Any]]:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def get_role(self, agent_id: str) -> dict[str, Any] | None:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def create_role(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def update_role(
|
||||
self,
|
||||
agent_id: str,
|
||||
*,
|
||||
enabled: bool | None = None,
|
||||
sort_order: int | None = None,
|
||||
) -> dict[str, Any] | None:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def delete_role(self, agent_id: str) -> bool:
|
||||
"""Hard-delete a registry row for create rollback only."""
|
||||
raise NotImplementedError
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
"""In-memory strategy-agent registry used by memory persistence and tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from deerflow.persistence.strategy_agent_roles.base import StrategyAgentRoleStore
|
||||
|
||||
|
||||
def _now() -> str:
|
||||
return datetime.now(UTC).isoformat()
|
||||
|
||||
|
||||
def _copy(row: dict[str, Any]) -> dict[str, Any]:
|
||||
return dict(row)
|
||||
|
||||
|
||||
class MemoryStrategyAgentRoleStore(StrategyAgentRoleStore):
|
||||
def __init__(self) -> None:
|
||||
self._rows: dict[str, dict[str, Any]] = {}
|
||||
self._lock = asyncio.Lock()
|
||||
|
||||
async def ensure_defaults(self, rows: list[dict[str, Any]]) -> int:
|
||||
inserted = 0
|
||||
async with self._lock:
|
||||
for source in rows:
|
||||
agent_id = str(source["agent_id"])
|
||||
existing = self._rows.get(agent_id)
|
||||
if existing is not None:
|
||||
# Canonical fixed roles are executable infrastructure, not
|
||||
# administrator-managed lifecycle rows. Repair stale or
|
||||
# manually-corrupted metadata on every bootstrap. Angle
|
||||
# defaults deliberately retain their enabled/order state.
|
||||
if bool(source.get("fixed", False)):
|
||||
canonical = {
|
||||
"category": str(source["category"]),
|
||||
"fixed": True,
|
||||
"enabled": bool(source.get("enabled", True)),
|
||||
"sort_order": int(source.get("sort_order", 0)),
|
||||
}
|
||||
if any(existing.get(key) != value for key, value in canonical.items()):
|
||||
existing.update(canonical)
|
||||
existing["updated_at"] = _now()
|
||||
continue
|
||||
now = _now()
|
||||
self._rows[agent_id] = {
|
||||
"agent_id": agent_id,
|
||||
"category": str(source["category"]),
|
||||
"fixed": bool(source.get("fixed", False)),
|
||||
"enabled": bool(source.get("enabled", True)),
|
||||
"sort_order": int(source.get("sort_order", 0)),
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
}
|
||||
inserted += 1
|
||||
return inserted
|
||||
|
||||
async def list_roles(self, *, include_disabled: bool) -> list[dict[str, Any]]:
|
||||
async with self._lock:
|
||||
rows = [_copy(row) for row in self._rows.values() if include_disabled or bool(row.get("enabled"))]
|
||||
rows.sort(
|
||||
key=lambda row: (
|
||||
int(row.get("sort_order", 0)),
|
||||
str(row.get("agent_id", "")),
|
||||
)
|
||||
)
|
||||
return rows
|
||||
|
||||
async def get_role(self, agent_id: str) -> dict[str, Any] | None:
|
||||
async with self._lock:
|
||||
row = self._rows.get(agent_id)
|
||||
return _copy(row) if row is not None else None
|
||||
|
||||
async def create_role(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
agent_id = str(data["agent_id"])
|
||||
async with self._lock:
|
||||
if agent_id in self._rows:
|
||||
raise ValueError(f"Strategy agent role '{agent_id}' already exists")
|
||||
now = _now()
|
||||
row = {
|
||||
"agent_id": agent_id,
|
||||
"category": str(data["category"]),
|
||||
"fixed": bool(data.get("fixed", False)),
|
||||
"enabled": bool(data.get("enabled", True)),
|
||||
"sort_order": int(data.get("sort_order", 0)),
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
}
|
||||
self._rows[agent_id] = row
|
||||
return _copy(row)
|
||||
|
||||
async def update_role(
|
||||
self,
|
||||
agent_id: str,
|
||||
*,
|
||||
enabled: bool | None = None,
|
||||
sort_order: int | None = None,
|
||||
) -> dict[str, Any] | None:
|
||||
async with self._lock:
|
||||
row = self._rows.get(agent_id)
|
||||
if row is None:
|
||||
return None
|
||||
if enabled is not None:
|
||||
row["enabled"] = bool(enabled)
|
||||
if sort_order is not None:
|
||||
row["sort_order"] = int(sort_order)
|
||||
row["updated_at"] = _now()
|
||||
return _copy(row)
|
||||
|
||||
async def delete_role(self, agent_id: str) -> bool:
|
||||
async with self._lock:
|
||||
return self._rows.pop(agent_id, None) is not None
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
"""Persistence model for the global strategy-selection agent registry."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import Boolean, Integer, String
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.types import BeijingDateTime
|
||||
|
||||
|
||||
class StrategyAgentRoleRow(Base):
|
||||
"""Classify folder-backed agents used by the strategy-selection workflow.
|
||||
|
||||
The agent's model, skills and SOUL remain authoritative in its normal
|
||||
``config.yaml`` / ``SOUL.md`` files. This small global table owns only the
|
||||
workflow classification and lifecycle flags. Keeping a disabled row (and
|
||||
the underlying agent) protects saved projects that still reference it.
|
||||
"""
|
||||
|
||||
__tablename__ = "strategy_agent_roles"
|
||||
|
||||
agent_id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
category: Mapped[str] = mapped_column(String(16), nullable=False, index=True)
|
||||
fixed: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
|
||||
enabled: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default="1", index=True)
|
||||
sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
||||
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),
|
||||
)
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
"""SQLAlchemy-backed global strategy-selection agent registry."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import delete, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.strategy_agent_roles.base import StrategyAgentRoleStore
|
||||
from deerflow.persistence.strategy_agent_roles.model import StrategyAgentRoleRow
|
||||
|
||||
|
||||
def _to_dict(row: StrategyAgentRoleRow) -> dict[str, Any]:
|
||||
return {
|
||||
"agent_id": row.agent_id,
|
||||
"category": row.category,
|
||||
"fixed": row.fixed,
|
||||
"enabled": row.enabled,
|
||||
"sort_order": row.sort_order,
|
||||
"created_at": row.created_at.isoformat() if isinstance(row.created_at, datetime) else row.created_at,
|
||||
"updated_at": row.updated_at.isoformat() if isinstance(row.updated_at, datetime) else row.updated_at,
|
||||
}
|
||||
|
||||
|
||||
class StrategyAgentRoleRepository(StrategyAgentRoleStore):
|
||||
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
|
||||
self._sf = session_factory
|
||||
|
||||
async def ensure_defaults(self, rows: list[dict[str, Any]]) -> int:
|
||||
inserted = 0
|
||||
for source in rows:
|
||||
# Commit each of the small number of defaults independently. If
|
||||
# two workers race on one primary key, rolling that save back must
|
||||
# not also discard unrelated defaults inserted by this worker.
|
||||
async with self._sf() as session:
|
||||
agent_id = str(source["agent_id"])
|
||||
existing = await session.get(StrategyAgentRoleRow, agent_id)
|
||||
if existing is not None:
|
||||
if bool(source.get("fixed", False)):
|
||||
changed = False
|
||||
canonical = {
|
||||
"category": str(source["category"]),
|
||||
"fixed": True,
|
||||
"enabled": bool(source.get("enabled", True)),
|
||||
"sort_order": int(source.get("sort_order", 0)),
|
||||
}
|
||||
for key, value in canonical.items():
|
||||
if getattr(existing, key) != value:
|
||||
setattr(existing, key, value)
|
||||
changed = True
|
||||
if changed:
|
||||
existing.updated_at = datetime.now(UTC)
|
||||
await session.commit()
|
||||
continue
|
||||
now = datetime.now(UTC)
|
||||
session.add(
|
||||
StrategyAgentRoleRow(
|
||||
agent_id=agent_id,
|
||||
category=str(source["category"]),
|
||||
fixed=bool(source.get("fixed", False)),
|
||||
enabled=bool(source.get("enabled", True)),
|
||||
sort_order=int(source.get("sort_order", 0)),
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
)
|
||||
try:
|
||||
await session.commit()
|
||||
except IntegrityError:
|
||||
# Another startup worker won this primary-key race.
|
||||
await session.rollback()
|
||||
continue
|
||||
inserted += 1
|
||||
return inserted
|
||||
|
||||
async def list_roles(self, *, include_disabled: bool) -> list[dict[str, Any]]:
|
||||
stmt = select(StrategyAgentRoleRow)
|
||||
if not include_disabled:
|
||||
stmt = stmt.where(StrategyAgentRoleRow.enabled.is_(True))
|
||||
stmt = stmt.order_by(
|
||||
StrategyAgentRoleRow.sort_order.asc(),
|
||||
StrategyAgentRoleRow.agent_id.asc(),
|
||||
)
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(stmt)
|
||||
return [_to_dict(row) for row in result.scalars()]
|
||||
|
||||
async def get_role(self, agent_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(StrategyAgentRoleRow, agent_id)
|
||||
return _to_dict(row) if row is not None else None
|
||||
|
||||
async def create_role(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
now = datetime.now(UTC)
|
||||
row = StrategyAgentRoleRow(
|
||||
agent_id=str(data["agent_id"]),
|
||||
category=str(data["category"]),
|
||||
fixed=bool(data.get("fixed", False)),
|
||||
enabled=bool(data.get("enabled", True)),
|
||||
sort_order=int(data.get("sort_order", 0)),
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
async with self._sf() as session:
|
||||
session.add(row)
|
||||
try:
|
||||
await session.commit()
|
||||
except IntegrityError as exc:
|
||||
await session.rollback()
|
||||
raise ValueError(f"Strategy agent role '{row.agent_id}' already exists") from exc
|
||||
await session.refresh(row)
|
||||
return _to_dict(row)
|
||||
|
||||
async def update_role(
|
||||
self,
|
||||
agent_id: str,
|
||||
*,
|
||||
enabled: bool | None = None,
|
||||
sort_order: int | None = None,
|
||||
) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(StrategyAgentRoleRow, agent_id)
|
||||
if row is None:
|
||||
return None
|
||||
if enabled is not None:
|
||||
row.enabled = bool(enabled)
|
||||
if sort_order is not None:
|
||||
row.sort_order = int(sort_order)
|
||||
row.updated_at = datetime.now(UTC)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return _to_dict(row)
|
||||
|
||||
async def delete_role(self, agent_id: str) -> bool:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(delete(StrategyAgentRoleRow).where(StrategyAgentRoleRow.agent_id == agent_id))
|
||||
await session.commit()
|
||||
return (result.rowcount or 0) > 0
|
||||
@@ -21,9 +21,10 @@ class InlineThinkTagFilter:
|
||||
_OPEN_TAG = "<think>"
|
||||
_CLOSE_TAG = "</think>"
|
||||
|
||||
def __init__(self) -> None:
|
||||
def __init__(self, *, allow_implicit_close: bool = False) -> None:
|
||||
self._inside_think = False
|
||||
self._pending = ""
|
||||
self._channel_decided = not allow_implicit_close
|
||||
|
||||
@staticmethod
|
||||
def _possible_tag_prefix_size(content: str, tag: str) -> int:
|
||||
@@ -44,6 +45,23 @@ class InlineThinkTagFilter:
|
||||
visible: list[str] = []
|
||||
thinking: list[str] = []
|
||||
|
||||
if not self._channel_decided:
|
||||
lowered = remaining.lower()
|
||||
open_at = lowered.find(self._OPEN_TAG)
|
||||
close_at = lowered.find(self._CLOSE_TAG)
|
||||
if close_at >= 0 and (open_at < 0 or close_at < open_at):
|
||||
# Some templates inject the opening tag outside returned
|
||||
# content. Keep the ambiguous prefix private until its close
|
||||
# marker arrives, even when that marker spans chunks.
|
||||
thinking.append(remaining[:close_at])
|
||||
remaining = remaining[close_at + len(self._CLOSE_TAG) :]
|
||||
self._channel_decided = True
|
||||
elif open_at >= 0:
|
||||
self._channel_decided = True
|
||||
else:
|
||||
self._pending = remaining
|
||||
return "", ""
|
||||
|
||||
while remaining:
|
||||
tag = self._CLOSE_TAG if self._inside_think else self._OPEN_TAG
|
||||
marker_at = remaining.lower().find(tag)
|
||||
@@ -65,8 +83,27 @@ class InlineThinkTagFilter:
|
||||
"""Compatibility helper returning only confirmed visible text."""
|
||||
return self.push_parts(chunk)[0]
|
||||
|
||||
def confirm_visible_channel(self) -> str:
|
||||
"""Stop close-only probing and return any buffered visible prelude.
|
||||
|
||||
A provider that emits a dedicated reasoning field has already told us
|
||||
how to classify its trace. Its ordinary ``content`` channel can then
|
||||
stream immediately even when the model supports thinking.
|
||||
"""
|
||||
if self._channel_decided:
|
||||
return ""
|
||||
self._channel_decided = True
|
||||
pending = self._pending
|
||||
self._pending = ""
|
||||
return pending
|
||||
|
||||
def finish_parts(self) -> tuple[str, str]:
|
||||
"""Flush the remaining visible or thinking text at end of stream."""
|
||||
if not self._channel_decided:
|
||||
tail = self._pending
|
||||
self._pending = ""
|
||||
self._channel_decided = True
|
||||
return tail, ""
|
||||
if self._inside_think:
|
||||
thinking = self._pending
|
||||
self._pending = ""
|
||||
|
||||
@@ -2,7 +2,7 @@ import pytest
|
||||
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
|
||||
|
||||
from deerflow.agents.middlewares.writing_setup_round_cap_middleware import WritingSetupRoundCapMiddleware
|
||||
from deerflow.models.message_ordering import normalize_chat_payload_messages, normalize_langchain_messages
|
||||
from deerflow.models.message_ordering import normalize_chat_payload_messages, normalize_langchain_messages, normalize_model_request_messages
|
||||
from deerflow.models.patched_openai import PatchedChatOpenAI
|
||||
|
||||
|
||||
@@ -49,6 +49,37 @@ def test_normalize_langchain_messages_moves_late_systems_before_history():
|
||||
assert normalized[1].content == "user 0"
|
||||
|
||||
|
||||
def test_normalize_chat_payload_messages_merges_adjacent_leading_systems():
|
||||
payload = {
|
||||
"messages": [
|
||||
{"role": "system", "content": "agent prompt"},
|
||||
{"role": "system", "content": "compaction summary"},
|
||||
{"role": "user", "content": "continue"},
|
||||
]
|
||||
}
|
||||
|
||||
normalize_chat_payload_messages(payload)
|
||||
|
||||
assert [message["role"] for message in payload["messages"]] == ["system", "user"]
|
||||
assert payload["messages"][0]["content"] == "agent prompt\n\n---\n\ncompaction summary"
|
||||
|
||||
|
||||
def test_normalize_model_request_messages_folds_compaction_summary_into_system_slot():
|
||||
system_message, messages = normalize_model_request_messages(
|
||||
SystemMessage(content="agent prompt"),
|
||||
[
|
||||
SystemMessage(content="compaction summary"),
|
||||
HumanMessage(content="continue"),
|
||||
],
|
||||
)
|
||||
|
||||
assert system_message is not None
|
||||
assert system_message.content == "agent prompt\n\n---\n\ncompaction summary"
|
||||
assert len(messages) == 1
|
||||
assert isinstance(messages[0], HumanMessage)
|
||||
assert all(not isinstance(message, SystemMessage) for message in messages)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("turns", [12, 25, 50])
|
||||
def test_patched_openai_payload_has_only_leading_system_after_many_turns(turns):
|
||||
model = PatchedChatOpenAI(
|
||||
|
||||
Reference in new issue
Block a user