sync update from LAN repo (source dhk-1 @ 3e87be55)
This commit is contained in:
1 parent
ab6068a7c1
commit
42f2a26fa8
1482 files changed
+227759
-9584
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,7 +35,12 @@ 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.models import create_chat_model
|
||||
from deerflow.runtime.user_context import get_effective_user_id
|
||||
@@ -52,6 +55,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 +183,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 = {
|
||||
@@ -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,8 +714,13 @@ 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)
|
||||
@@ -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", []),
|
||||
},
|
||||
)
|
||||
@@ -840,8 +880,19 @@ 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,19 @@ 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 +979,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,
|
||||
)
|
||||
|
||||
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)
|
||||
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
|
||||
|
||||
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:
|
||||
|
||||
Reference in new issue
Block a user