sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)

This commit is contained in:
gozeros committed 2026-10-21 14:51:31 +08:00
1 parent 4f7f07504a
commit 8bc8b0e082
1651 files changed
+278599 -11416

No files matched your search

@@ -16,11 +16,9 @@ Usage:
"""
import asyncio
import json
import logging
import mimetypes
import shutil
import tempfile
import uuid
from collections.abc import Generator, Sequence
from dataclasses import dataclass, field
@@ -37,8 +35,14 @@ from deerflow.agents.lead_agent.prompt import apply_prompt_template
from deerflow.agents.thread_state import ThreadState
from deerflow.config.agents_config import AGENT_NAME_PATTERN
from deerflow.config.app_config import get_app_config, reload_app_config
from deerflow.config.extensions_config import ExtensionsConfig, SkillStateConfig, get_extensions_config, reload_extensions_config
from deerflow.config.extensions_config import (
ExtensionsConfig,
McpServerConfig,
patch_extensions_config,
read_extensions_config_document,
)
from deerflow.config.paths import get_paths
from deerflow.config.system_settings import get_conversation_context_settings
from deerflow.models import create_chat_model
from deerflow.runtime.user_context import get_effective_user_id
from deerflow.skills.storage import get_or_new_skill_storage
@@ -52,6 +56,11 @@ from deerflow.uploads.manager import (
upload_artifact_url,
upload_virtual_path,
)
from deerflow.utils.tool_visibility import (
internal_tool_call_ids,
is_internal_tool_name,
project_outward_messages,
)
logger = logging.getLogger(__name__)
@@ -175,24 +184,6 @@ class DeerFlowClient:
# Internal helpers
# ------------------------------------------------------------------
@staticmethod
def _atomic_write_json(path: Path, data: dict) -> None:
"""Write JSON to *path* atomically (temp file + replace)."""
fd = tempfile.NamedTemporaryFile(
mode="w",
dir=path.parent,
suffix=".tmp",
delete=False,
)
try:
json.dump(data, fd, indent=2)
fd.close()
Path(fd.name).replace(path)
except BaseException:
fd.close()
Path(fd.name).unlink(missing_ok=True)
raise
def _get_runnable_config(self, thread_id: str, **overrides) -> RunnableConfig:
"""Build a RunnableConfig for agent invocation."""
configurable = {
@@ -210,6 +201,7 @@ class DeerFlowClient:
def _ensure_agent(self, config: RunnableConfig):
"""Create (or recreate) the agent when config-dependent params change."""
cfg = config.get("configurable", {})
recent_turn_window_enabled = get_conversation_context_settings().recent_turn_window_enabled
key = (
cfg.get("model_name"),
cfg.get("thinking_enabled"),
@@ -217,6 +209,7 @@ class DeerFlowClient:
cfg.get("subagent_enabled"),
self._agent_name,
frozenset(self._available_skills) if self._available_skills is not None else None,
recent_turn_window_enabled,
)
if self._agent is not None and self._agent_config_key == key:
@@ -264,35 +257,32 @@ class DeerFlowClient:
return [{"name": tc["name"], "args": tc["args"], "id": tc.get("id")} for tc in tool_calls]
@staticmethod
def _ai_text_event(
msg_id: str | None,
text: str,
usage: dict | None,
reasoning: str = "",
) -> "StreamEvent":
"""Build a ``messages-tuple`` AI text event, preserving reasoning deltas."""
data: dict[str, Any] = {"type": "ai", "content": text, "id": msg_id}
if usage:
data["usage_metadata"] = usage
# Thinking-enabled OpenAI-compatible providers commonly put their
# streaming tokens in ``additional_kwargs.reasoning_content`` while
# ``content`` remains empty. Keep it on the wire for consumers that
# render a custom stream (such as the multi-agent roundtable).
if reasoning:
data["additional_kwargs"] = {"reasoning_content": reasoning}
return StreamEvent(type="messages-tuple", data=data)
@staticmethod
def _reasoning_text(msg: Any) -> str:
"""Return a provider reasoning delta when it is available as text."""
additional_kwargs = getattr(msg, "additional_kwargs", None) or {}
if not isinstance(additional_kwargs, dict):
return ""
reasoning = (
additional_kwargs.get("reasoning_content")
or additional_kwargs.get("reasoning")
)
return reasoning if isinstance(reasoning, str) else ""
def _ai_text_event(
msg_id: str | None,
text: str,
usage: dict | None,
reasoning: str = "",
) -> "StreamEvent":
"""Build a ``messages-tuple`` AI text event, preserving reasoning deltas."""
data: dict[str, Any] = {"type": "ai", "content": text, "id": msg_id}
if usage:
data["usage_metadata"] = usage
# Thinking-enabled OpenAI-compatible providers commonly put their
# streaming tokens in ``additional_kwargs.reasoning_content`` while
# ``content`` remains empty. Keep it on the wire for consumers that
# render a custom stream (such as the multi-agent roundtable).
if reasoning:
data["additional_kwargs"] = {"reasoning_content": reasoning}
return StreamEvent(type="messages-tuple", data=data)
@staticmethod
def _reasoning_text(msg: Any) -> str:
"""Return a provider reasoning delta when it is available as text."""
additional_kwargs = getattr(msg, "additional_kwargs", None) or {}
if not isinstance(additional_kwargs, dict):
return ""
reasoning = additional_kwargs.get("reasoning_content") or additional_kwargs.get("reasoning")
return reasoning if isinstance(reasoning, str) else ""
@staticmethod
def _ai_tool_calls_event(msg_id: str | None, tool_calls) -> "StreamEvent":
@@ -463,7 +453,13 @@ class DeerFlowClient:
for cp in checkpointer.list(config):
channel_values = dict(cp.checkpoint.get("channel_values", {}))
if "messages" in channel_values:
channel_values["messages"] = [self._serialize_message(m) if hasattr(m, "content") else m for m in channel_values["messages"]]
serialized_messages = [
self._serialize_message(m) if hasattr(m, "content") else m
for m in channel_values["messages"]
]
channel_values["messages"] = project_outward_messages(
serialized_messages
)
cfg = cp.config.get("configurable", {})
parent_cfg = cp.parent_config.get("configurable", {}) if cp.parent_config else {}
@@ -587,6 +583,7 @@ class DeerFlowClient:
# in both the final ``messages`` chunk and the values snapshot —
# count it only on whichever arrives first.
counted_usage_ids: set[str] = set()
internal_call_ids: set[str] = set()
cumulative_usage: dict[str, int] = {"input_tokens": 0, "output_tokens": 0, "total_tokens": 0}
def _account_usage(msg_id: str | None, usage: Any) -> dict | None:
@@ -642,21 +639,39 @@ class DeerFlowClient:
msg_id = getattr(msg_chunk, "id", None)
if isinstance(msg_chunk, AIMessage):
serialized_chunk = self._serialize_message(msg_chunk)
internal_call_ids.update(
internal_tool_call_ids([serialized_chunk])
)
projected_chunk = project_outward_messages(
[serialized_chunk],
known_internal_tool_call_ids=internal_call_ids,
)
text = self._extract_text(msg_chunk.content)
reasoning = self._reasoning_text(msg_chunk)
counted_usage = _account_usage(msg_id, msg_chunk.usage_metadata)
if text or reasoning:
if projected_chunk and (text or reasoning):
if msg_id:
streamed_ids.add(msg_id)
yield self._ai_text_event(msg_id, text, counted_usage, reasoning)
if msg_chunk.tool_calls:
visible_tool_calls = [
call
for call in (msg_chunk.tool_calls or [])
if not is_internal_tool_name(call.get("name"))
]
if visible_tool_calls:
if msg_id:
streamed_ids.add(msg_id)
yield self._ai_tool_calls_event(msg_id, msg_chunk.tool_calls)
yield self._ai_tool_calls_event(msg_id, visible_tool_calls)
elif isinstance(msg_chunk, ToolMessage):
if is_internal_tool_name(msg_chunk.name) or str(
msg_chunk.tool_call_id or ""
) in internal_call_ids:
internal_call_ids.discard(str(msg_chunk.tool_call_id or ""))
continue
if msg_id:
streamed_ids.add(msg_id)
yield self._tool_message_event(msg_chunk)
@@ -664,14 +679,31 @@ class DeerFlowClient:
# mode == "values"
messages = chunk.get("messages", [])
serialized_messages = [self._serialize_message(message) for message in messages]
for msg in messages:
for msg, serialized_message in zip(messages, serialized_messages):
msg_id = getattr(msg, "id", None)
if msg_id and msg_id in seen_ids:
continue
if msg_id:
seen_ids.add(msg_id)
if isinstance(msg, AIMessage):
internal_call_ids.update(
internal_tool_call_ids([serialized_message])
)
projected_message = project_outward_messages(
[serialized_message],
known_internal_tool_call_ids=internal_call_ids,
)
if not projected_message:
if isinstance(msg, AIMessage):
_account_usage(msg_id, getattr(msg, "usage_metadata", None))
elif isinstance(msg, ToolMessage):
internal_call_ids.discard(str(msg.tool_call_id or ""))
continue
# Already streamed via ``messages`` mode; only (defensively)
# capture usage here and skip re-synthesizing the event.
if msg_id and msg_id in streamed_ids:
@@ -682,13 +714,18 @@ class DeerFlowClient:
if isinstance(msg, AIMessage):
counted_usage = _account_usage(msg_id, msg.usage_metadata)
if msg.tool_calls:
yield self._ai_tool_calls_event(msg_id, msg.tool_calls)
visible_tool_calls = [
call
for call in (msg.tool_calls or [])
if not is_internal_tool_name(call.get("name"))
]
if visible_tool_calls:
yield self._ai_tool_calls_event(msg_id, visible_tool_calls)
text = self._extract_text(msg.content)
reasoning = self._reasoning_text(msg)
if text or reasoning:
yield self._ai_text_event(msg_id, text, counted_usage, reasoning)
text = self._extract_text(msg.content)
reasoning = self._reasoning_text(msg)
if text or reasoning:
yield self._ai_text_event(msg_id, text, counted_usage, reasoning)
elif isinstance(msg, ToolMessage):
yield self._tool_message_event(msg)
@@ -698,7 +735,10 @@ class DeerFlowClient:
type="values",
data={
"title": chunk.get("title"),
"messages": [self._serialize_message(m) for m in messages],
"messages": project_outward_messages(
serialized_messages,
known_internal_tool_call_ids=internal_call_ids,
),
"artifacts": chunk.get("artifacts", []),
},
)
@@ -760,7 +800,11 @@ class DeerFlowClient:
"display_name": getattr(model, "display_name", None),
"description": getattr(model, "description", None),
"supports_thinking": getattr(model, "supports_thinking", False),
"supports_reasoning_effort": getattr(model, "supports_reasoning_effort", False),
# Gateway deployments resolve this administrator-managed
# capability from their database. The embedded harness has
# no application persistence layer, so legacy YAML must
# never advertise it as enabled.
"supports_reasoning_effort": False,
}
for model in self._app_config.models
],
@@ -826,7 +870,8 @@ class DeerFlowClient:
"display_name": getattr(model, "display_name", None),
"description": getattr(model, "description", None),
"supports_thinking": getattr(model, "supports_thinking", False),
"supports_reasoning_effort": getattr(model, "supports_reasoning_effort", False),
# See list_models(): this flag is database-owned by the Gateway.
"supports_reasoning_effort": False,
}
# ------------------------------------------------------------------
@@ -840,8 +885,14 @@ class DeerFlowClient:
Dict with "mcp_servers" key mapping server name to config,
matching the Gateway API ``McpConfigResponse`` schema.
"""
config = get_extensions_config()
return {"mcp_servers": {name: server.model_dump() for name, server in config.mcp_servers.items()}}
config_path = ExtensionsConfig.resolve_config_path()
if config_path is None:
return {"mcp_servers": {}}
document = read_extensions_config_document(config_path)
raw_servers = document.get("mcpServers", {})
if not isinstance(raw_servers, dict):
raise ValueError("Extensions config 'mcpServers' must be a JSON object")
return {"mcp_servers": {name: McpServerConfig.model_validate(server).model_dump() for name, server in raw_servers.items()}}
def update_mcp_config(self, mcp_servers: dict[str, dict]) -> dict:
"""Update MCP server configurations.
@@ -863,22 +914,14 @@ class DeerFlowClient:
if config_path is None:
raise FileNotFoundError("Cannot locate extensions_config.json. Set DEER_FLOW_EXTENSIONS_CONFIG_PATH or ensure it exists in the project root.")
current_config = get_extensions_config()
def _patch_mcp_servers(config_data: dict[str, Any]) -> None:
config_data["mcpServers"] = mcp_servers
config_data = {
"mcpServers": mcp_servers,
"skills": {
name: skill.model_dump(by_alias=True, exclude_none=True)
for name, skill in current_config.skills.items()
},
}
self._atomic_write_json(config_path, config_data)
patch_extensions_config(config_path, _patch_mcp_servers)
self._agent = None
self._agent_config_key = None
reloaded = reload_extensions_config()
return {"mcp_servers": {name: server.model_dump() for name, server in reloaded.mcp_servers.items()}}
return {"mcp_servers": {name: McpServerConfig.model_validate(server).model_dump() for name, server in mcp_servers.items()}}
# ------------------------------------------------------------------
# Public API — skills management
@@ -931,26 +974,24 @@ class DeerFlowClient:
if config_path is None:
raise FileNotFoundError("Cannot locate extensions_config.json. Set DEER_FLOW_EXTENSIONS_CONFIG_PATH or ensure it exists in the project root.")
extensions_config = get_extensions_config()
current_skill_config = extensions_config.skills.get(name)
extensions_config.skills[name] = SkillStateConfig(
enabled=enabled,
display=current_skill_config.display if current_skill_config else None,
)
def _patch_skill_state(config_data: dict[str, Any]) -> None:
skill_states = config_data.setdefault("skills", {})
if not isinstance(skill_states, dict):
raise ValueError("Extensions config 'skills' must be a JSON object")
raw_skill_state = skill_states.get(name)
if raw_skill_state is None:
raw_skill_state = {}
elif not isinstance(raw_skill_state, dict):
raise ValueError(f"Extensions config state for skill '{name}' must be a JSON object")
else:
raw_skill_state = dict(raw_skill_state)
raw_skill_state["enabled"] = enabled
skill_states[name] = raw_skill_state
config_data = {
"mcpServers": {n: s.model_dump(by_alias=True) for n, s in extensions_config.mcp_servers.items()},
"skills": {
n: sc.model_dump(by_alias=True, exclude_none=True)
for n, sc in extensions_config.skills.items()
},
}
self._atomic_write_json(config_path, config_data)
patch_extensions_config(config_path, _patch_skill_state)
self._agent = None
self._agent_config_key = None
reload_extensions_config()
updated = next((s for s in get_or_new_skill_storage().load_skills(enabled_only=False) if s.name == name), None)
if updated is None: