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

This commit is contained in:
gozeros committed 2026-11-05 10:56:34 +08:00
1 parent da6521b6cb
commit 264e2200d6
995 files changed
+148105 -13399

No files matched your search

@@ -6,6 +6,7 @@ from deerflow.config import get_app_config
from deerflow.config.app_config import AppConfig
from deerflow.config.model_config import WriteFileTransport
from deerflow.config.system_settings import get_write_file_compatibility_settings
from deerflow.models.chat_stream_state import needs_qwen_flash_next_integrity
from deerflow.reflection import resolve_class
from deerflow.tracing import build_tracing_callbacks
@@ -86,7 +87,6 @@ def _apply_openai_thinking_control(
model_settings_from_config: dict,
*,
thinking_enabled: bool,
reasoning_effort: object,
) -> None:
"""Inject vLLM/Qwen thinking flags when the UI toggle is on or off."""
if "extra_body" not in getattr(model_class, "model_fields", {}):
@@ -99,9 +99,6 @@ def _apply_openai_thinking_control(
"enable_thinking": True,
"chat_template_kwargs": {"enable_thinking": True},
}
mapped_effort = _map_qwen_reasoning_effort(reasoning_effort)
if mapped_effort:
enable_body["chat_template_kwargs"]["reasoning_effort"] = mapped_effort
model_settings_from_config["extra_body"] = _deep_merge_dicts(
model_settings_from_config.get("extra_body"),
enable_body,
@@ -139,7 +136,16 @@ def _enable_stream_usage_by_default(model_use_path: str, model_settings_from_con
model_settings_from_config["stream_usage"] = True
def create_chat_model(name: str | None = None, thinking_enabled: bool = False, *, app_config: AppConfig | None = None, force_disable_thinking: bool = False, **kwargs) -> BaseChatModel:
def create_chat_model(
name: str | None = None,
thinking_enabled: bool = False,
*,
app_config: AppConfig | None = None,
force_disable_thinking: bool = False,
write_file_compatibility: bool | None = None,
prompt_suffix_enabled: bool = True,
**kwargs,
) -> BaseChatModel:
"""Create a chat model instance from the config.
Args:
@@ -172,8 +178,8 @@ def create_chat_model(name: str | None = None, thinking_enabled: bool = False, *
from deerflow.models.patched_openai import PatchedChatOpenAI
model_class = PatchedChatOpenAI
model_settings_from_config = model_config.model_dump(
exclude_none=True,
model_settings_from_config = model_config.model_dump(
exclude_none=True,
exclude={
"use",
"name",
@@ -186,15 +192,37 @@ def create_chat_model(name: str | None = None, thinking_enabled: bool = False, *
"thinking",
"supports_vision",
"write_file_transport",
"tool_call_transport",
},
)
configured_write_file_transport = model_config.write_file_transport
runtime_write_file_compatibility = get_write_file_compatibility_settings()
# ``None`` follows the global switch, so a roundtable run with its own
# switch off still uses compatibility when the global switch is on.
# ``True`` is the roundtable opt-in used when the global switch is off.
compatibility_enabled = (
get_write_file_compatibility_settings().enabled
if write_file_compatibility is None
else write_file_compatibility
)
effective_write_file_transport = (
WriteFileTransport.BUFFERED_GATEWAY
if runtime_write_file_compatibility.enabled
if compatibility_enabled
else configured_write_file_transport
)
if model_config.tool_call_transport.value != "native":
from deerflow.models.patched_openai import PatchedChatOpenAI
if not issubclass(model_class, PatchedChatOpenAI):
raise ValueError(
"tool_call_transport=deepseek_dsml is supported only by "
"langchain_openai:ChatOpenAI or PatchedChatOpenAI models"
)
if model_config.use_responses_api:
raise ValueError(
"tool_call_transport=deepseek_dsml requires the OpenAI-compatible "
"chat-completions API, not use_responses_api"
)
model_settings_from_config["tool_call_transport"] = model_config.tool_call_transport.value
if effective_write_file_transport in {
WriteFileTransport.BUFFERED_GATEWAY,
WriteFileTransport.ARTIFACT_RENDERER,
@@ -235,18 +263,18 @@ def create_chat_model(name: str | None = None, thinking_enabled: bool = False, *
if model_config.thinking is not None:
merged_thinking = {**(effective_wte.get("thinking") or {}), **model_config.thinking}
effective_wte = {**effective_wte, "thinking": merged_thinking}
if thinking_enabled and has_thinking_settings:
if not model_config.supports_thinking:
raise ValueError(f"Model {name} does not support thinking. Set `supports_thinking` to true in the `config.yaml` to enable thinking.") from None
if effective_wte:
model_settings_from_config.update(effective_wte)
elif thinking_enabled and model_config.supports_thinking and model_config.use == "langchain_openai:ChatOpenAI":
# vLLM/Qwen OpenAI-compatible endpoints commonly require this explicit
# request flag before they emit delta.reasoning.
model_settings_from_config["extra_body"] = _deep_merge_dicts(
model_settings_from_config.get("extra_body"),
{"enable_thinking": True},
)
if thinking_enabled and has_thinking_settings:
if not model_config.supports_thinking:
raise ValueError(f"Model {name} does not support thinking. Set `supports_thinking` to true in the `config.yaml` to enable thinking.") from None
if effective_wte:
model_settings_from_config.update(effective_wte)
elif thinking_enabled and model_config.supports_thinking and model_config.use == "langchain_openai:ChatOpenAI":
# vLLM/Qwen OpenAI-compatible endpoints commonly require this explicit
# request flag before they emit delta.reasoning.
model_settings_from_config["extra_body"] = _deep_merge_dicts(
model_settings_from_config.get("extra_body"),
{"enable_thinking": True},
)
if not thinking_enabled:
if model_config.when_thinking_disabled is not None:
# User-provided disable settings take full precedence
@@ -267,17 +295,24 @@ def create_chat_model(name: str | None = None, thinking_enabled: bool = False, *
elif has_thinking_settings and effective_wte.get("thinking", {}).get("type"):
# Native langchain_anthropic: thinking is a direct constructor parameter
model_settings_from_config["thinking"] = {"type": "disabled"}
# The Gateway validates the database-backed capability before constructing
# the graph. Honor that server-validated runtime value without consulting a
# legacy config.yaml flag. Qwen/vLLM accepts low/medium/xhigh; normalize the
# old UI aliases defensively for internal callers that do not traverse the
# public run boundary.
requested_reasoning_effort = kwargs.get("reasoning_effort")
if not model_config.supports_reasoning_effort:
kwargs.pop("reasoning_effort", None)
if requested_reasoning_effort is not None:
model_settings_from_config.pop("reasoning_effort", None)
if _is_vllm_provider(model_config.use) or _is_qwen38_model(model_config):
mapped_effort = _map_qwen_reasoning_effort(requested_reasoning_effort)
if mapped_effort is not None:
kwargs["reasoning_effort"] = mapped_effort
_apply_openai_thinking_control(
model_config,
model_class,
model_settings_from_config,
thinking_enabled=thinking_enabled,
reasoning_effort=requested_reasoning_effort,
)
# Hard override: force-disable thinking regardless of the per-model config shape.
@@ -291,12 +326,7 @@ def create_chat_model(name: str | None = None, thinking_enabled: bool = False, *
# Only models that explicitly opt into thinking should receive vendor-specific
# disable payloads. Generic OpenAI-compatible gateways (including LiteLLM)
# may reject these otherwise-unknown fields instead of forwarding them.
if (
force_disable_thinking
and not thinking_enabled
and model_config.supports_thinking
and "extra_body" in getattr(model_class, "model_fields", {})
):
if force_disable_thinking and not thinking_enabled and model_config.supports_thinking and "extra_body" in getattr(model_class, "model_fields", {}):
model_settings_from_config["extra_body"] = _deep_merge_dicts(
model_settings_from_config.get("extra_body"),
{"enable_thinking": False, "chat_template_kwargs": {"enable_thinking": False}, "thinking": {"type": "disabled"}},
@@ -335,7 +365,23 @@ def create_chat_model(name: str | None = None, thinking_enabled: bool = False, *
if "stream_usage" in getattr(model_class, "model_fields", {}):
model_settings_from_config["stream_usage"] = True
model_instance = model_class(**kwargs, **model_settings_from_config)
# The deployment may expose the affected model under a generic provider id.
# Enable its local stream checks by either id or configured name, while
# honoring an explicit override and keeping other models on their old path.
if "strict_stream_integrity" in getattr(model_class, "model_fields", {}):
if "strict_stream_integrity" in kwargs:
model_settings_from_config["strict_stream_integrity"] = kwargs.pop("strict_stream_integrity")
elif needs_qwen_flash_next_integrity(model_config.name, model_config.model):
model_settings_from_config.setdefault("strict_stream_integrity", True)
from deerflow.models.prompt_suffix import with_model_prompt_suffix
model_instance = with_model_prompt_suffix(model_class)(
**kwargs,
**model_settings_from_config,
deerflow_prompt_model_name=name,
deerflow_prompt_suffix_enabled=prompt_suffix_enabled,
)
# Always emit a compact model lifecycle trace. In particular, the first
# streamed callback proves that the upstream endpoint accepted the request