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

@@ -4,13 +4,14 @@ from __future__ import annotations
import asyncio
import logging
from pathlib import Path
from typing import Any
from weakref import WeakValueDictionary
from langchain.tools import ToolRuntime, tool
from langgraph.typing import ContextT
from deerflow.agents.lead_agent.prompt import refresh_skills_system_prompt_cache_async
from deerflow.agents.lead_agent.prompt import refresh_one_skill_prompt_cache
from deerflow.agents.thread_state import ThreadState
from deerflow.mcp.tools import _make_sync_tool_wrapper
from deerflow.persistence.skills import SkillStore
@@ -66,6 +67,100 @@ async def _to_thread(func, /, *args, **kwargs):
return await asyncio.to_thread(func, *args, **kwargs)
_BLANK_SKILL_CONTENT = frozenset({"", "null", "none"})
_SKILL_SOURCE_BYTES = 512 * 1024
def _content_is_blank(content: str | None) -> bool:
return content is None or content.strip().lower() in _BLANK_SKILL_CONTENT
def _content_required_message(action: str, name: str) -> str:
location = f"/mnt/user-data/workspace/{name}/SKILL.md"
return (
f"content is required for {action} and was empty or null. "
f"Write the complete SKILL.md with write_file to {location}, then call "
f'skill_manage(action="{action}", name="{name}", path="{location}"). '
"Do not call this tool again with content set to null."
)
def _workspace_markdown_file(runtime: ToolRuntime[ContextT, ThreadState] | None, virtual_path: str) -> Path:
"""Map a thread virtual path to a host file and reject traversal."""
from deerflow.sandbox.tools import get_thread_data, replace_virtual_path
if not virtual_path.startswith("/mnt/user-data/"):
raise ValueError("path must be under /mnt/user-data/workspace.")
thread_data = get_thread_data(runtime)
if not thread_data:
raise ValueError("No thread workspace is available.")
host = replace_virtual_path(virtual_path, thread_data)
if host == virtual_path:
raise ValueError("path must be under /mnt/user-data/workspace.")
resolved = Path(host).resolve()
for key in ("workspace_path", "uploads_path", "outputs_path"):
base = thread_data.get(key)
if not base:
continue
root = Path(base).resolve()
try:
resolved.relative_to(root)
except ValueError:
continue
return resolved
raise ValueError("path must stay inside the thread workspace.")
async def _read_workspace_markdown(runtime: ToolRuntime[ContextT, ThreadState] | None, virtual_path: str) -> str:
target = _workspace_markdown_file(runtime, virtual_path)
def _read() -> str:
if not target.is_file():
raise ValueError(f"Skill file not found at {virtual_path}.")
data = target.read_bytes()
if len(data) > _SKILL_SOURCE_BYTES:
raise ValueError("Skill file is too large to import.")
return data.decode("utf-8")
return await _to_thread(_read)
async def _resolve_skill_markdown(
runtime: ToolRuntime[ContextT, ThreadState] | None,
name: str,
content: str | None,
path: str | None,
*,
action: str,
) -> tuple[str | None, str | None]:
"""Return markdown, or an instruction when the model omitted the body."""
if not _content_is_blank(content):
return content, None
source = (path or "").strip()
if source:
try:
return await _read_workspace_markdown(runtime, source), None
except ValueError as exc:
return None, f"{exc} {_content_required_message(action, name)}"
return None, _content_required_message(action, name)
async def _sync_managed_skill_view(name: str) -> None:
"""Copy or remove this skill in the sandbox view. A cold view still rebuilds once."""
try:
from deerflow.sandbox.skill_view import sync_skill_sandbox_skill
await _to_thread(sync_skill_sandbox_skill, name)
except Exception:
logger.debug("Failed to sync sandbox view for skill %r", name, exc_info=True)
async def _publish_managed_skill(name: str, *, removed: bool = False) -> None:
"""Refresh the prompt entry and sandbox view for this skill only."""
await refresh_one_skill_prompt_cache(name, removed=removed)
await _sync_managed_skill_view(name)
async def _bump_patch_best_effort(name: str) -> None:
"""记一次技能维护活动(patch / edit)。
@@ -240,10 +335,15 @@ async def _skill_manage_impl(
async with lock:
if action == "create":
if await _to_thread(skill_storage.custom_skill_exists, name):
# The writer always creates custom/<name>/. Stat that file instead
# of parsing every SKILL.md. Edit and delete still resolve a
# directory whose name differs from its frontmatter.
skill_file = skill_storage.get_custom_skill_file(name)
if await _to_thread(skill_file.is_file):
raise ValueError(f"Custom skill '{name}' already exists.")
content, hint = await _resolve_skill_markdown(runtime, name, content, path, action="create")
if content is None:
raise ValueError("content is required for create.")
return hint or _content_required_message("create", name)
await _to_thread(skill_storage.validate_skill_markdown_content, name, content)
scan = await _scan_or_raise(content, executable=False, location=f"{name}/{SKILL_MD_FILE}")
await _to_thread(skill_storage.write_custom_skill, name, SKILL_MD_FILE, content)
@@ -263,7 +363,7 @@ async def _skill_manage_impl(
# async stack — contextvars don't propagate into ``to_thread``.
if skill_store is not None:
await _ensure_skill_db_registered(skill_store, name, user_id)
await refresh_skills_system_prompt_cache_async()
await _publish_managed_skill(name)
return f"Created custom skill '{name}'."
if action == "edit":
@@ -271,8 +371,9 @@ async def _skill_manage_impl(
if skill_store is not None:
await _assert_agent_may_modify(skill_store, name, user_id)
await _assert_skill_is_agent_authored(name)
content, hint = await _resolve_skill_markdown(runtime, name, content, path, action="edit")
if content is None:
raise ValueError("content is required for edit.")
return hint or _content_required_message("edit", name)
await _to_thread(skill_storage.validate_skill_markdown_content, name, content)
scan = await _scan_or_raise(content, executable=False, location=f"{name}/{SKILL_MD_FILE}")
skill_file = skill_storage.get_custom_skill_file(name)
@@ -284,7 +385,7 @@ async def _skill_manage_impl(
_history_record(action="edit", file_path=SKILL_MD_FILE, prev_content=prev_content, new_content=content, thread_id=thread_id, scanner=scan),
)
await _bump_patch_best_effort(name)
await refresh_skills_system_prompt_cache_async()
await _publish_managed_skill(name)
return f"Updated custom skill '{name}'."
if action == "patch":
@@ -312,7 +413,7 @@ async def _skill_manage_impl(
_history_record(action="patch", file_path=SKILL_MD_FILE, prev_content=prev_content, new_content=new_content, thread_id=thread_id, scanner=scan),
)
await _bump_patch_best_effort(name)
await refresh_skills_system_prompt_cache_async()
await _publish_managed_skill(name)
return f"Patched custom skill '{name}' ({replacement_count} replacement(s) applied, {occurrences} match(es) found)."
if action == "delete":
@@ -366,7 +467,7 @@ async def _skill_manage_impl(
)
except Exception:
logger.debug("Cascade failed for deleted skill %r", name, exc_info=True)
await refresh_skills_system_prompt_cache_async()
await _publish_managed_skill(name, removed=True)
return f"Deleted custom skill '{name}'."
if action == "write_file":
@@ -387,6 +488,7 @@ async def _skill_manage_impl(
name,
_history_record(action="write_file", file_path=path, prev_content=prev_content, new_content=content, thread_id=thread_id, scanner=scan),
)
await _sync_managed_skill_view(name)
return f"Wrote '{path}' for custom skill '{name}'."
if action == "remove_file":
@@ -406,6 +508,7 @@ async def _skill_manage_impl(
name,
_history_record(action="remove_file", file_path=path, prev_content=prev_content, new_content=None, thread_id=thread_id, scanner={"decision": "allow", "reason": "Deletion requested."}),
)
await _sync_managed_skill_view(name)
return f"Removed '{path}' from custom skill '{name}'."
if await _to_thread(skill_storage.public_skill_exists, name):
@@ -429,8 +532,8 @@ async def skill_manage_tool(
Args:
action: One of create, patch, edit, delete, write_file, remove_file.
name: Skill name in hyphen-case.
content: New file content for create, edit, or write_file.
path: Supporting file path for write_file or remove_file.
content: Full SKILL.md text for create or edit, or file text for write_file. Never pass null. When the SKILL.md is long, omit content and pass path instead.
path: Supporting file path for write_file or remove_file. For create or edit, a /mnt/user-data/workspace SKILL.md written first with write_file.
find: Existing text to replace for patch.
replace: Replacement text for patch.
expected_count: Optional expected number of replacements for patch.