sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)
This commit is contained in:
1 parent
4f7f07504a
commit
8bc8b0e082
1651 files changed
+278599
-11416
No files matched your search
+109
-23
@@ -4,19 +4,77 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from pathlib import PurePosixPath
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from langchain.tools import ToolRuntime, tool
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from deerflow.skills.storage import SkillStorage
|
||||
from deerflow.skills.types import Skill
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_ROUNDTABLE_COORDINATOR_AGENT_ID = "roundtable-coordinator"
|
||||
COORDINATOR_AGENT_IDS = frozenset(
|
||||
{_ROUNDTABLE_COORDINATOR_AGENT_ID, "task-analysis-coordinator"}
|
||||
)
|
||||
COORDINATOR_ONLY_SKILL_NAMES = frozenset({"agent_orchestration", "agent-orchestration"})
|
||||
|
||||
|
||||
class SkillPathUnavailableError(PermissionError):
|
||||
"""Raised when a model tool targets an unknown or offline skill path."""
|
||||
|
||||
|
||||
def require_enabled_skill_path(
|
||||
path: str,
|
||||
*,
|
||||
container_path: str = "/mnt/skills",
|
||||
storage: SkillStorage | None = None,
|
||||
) -> Skill:
|
||||
"""Return the discovered enabled skill containing ``path`` or deny access.
|
||||
|
||||
Matching uses the discovered directory instead of assuming it equals the
|
||||
frontmatter name. Root/category-wide paths and unregistered files fail
|
||||
closed; models discover skills through ``skill_list``/``search_skills``.
|
||||
"""
|
||||
normalized_container = container_path.replace("\\", "/").rstrip("/") or "/"
|
||||
normalized_path = path.replace("\\", "/").rstrip("/") or "/"
|
||||
if normalized_path != normalized_container and not normalized_path.startswith(f"{normalized_container}/"):
|
||||
raise SkillPathUnavailableError("Path is outside the skills mount")
|
||||
if ".." in PurePosixPath(normalized_path).parts:
|
||||
raise SkillPathUnavailableError("Skill path traversal is not allowed")
|
||||
|
||||
try:
|
||||
from deerflow.config.extensions_config import ExtensionsConfig
|
||||
from deerflow.skills.storage import get_or_new_skill_storage
|
||||
|
||||
# SkillStorage intentionally degrades open on extensions-config errors;
|
||||
# model-facing file access must instead fail closed.
|
||||
extensions = ExtensionsConfig.from_file()
|
||||
catalog = (storage or get_or_new_skill_storage()).load_skills(enabled_only=False)
|
||||
except Exception as exc:
|
||||
raise SkillPathUnavailableError("Skill availability could not be verified") from exc
|
||||
|
||||
matches: list[tuple[int, Skill]] = []
|
||||
for skill in catalog:
|
||||
skill_root = skill.get_container_path(normalized_container).replace("\\", "/").rstrip("/")
|
||||
if normalized_path == skill_root or normalized_path.startswith(f"{skill_root}/"):
|
||||
matches.append((len(skill_root), skill))
|
||||
if not matches:
|
||||
raise SkillPathUnavailableError("Skill path is unavailable")
|
||||
|
||||
skill = max(matches, key=lambda item: item[0])[1]
|
||||
category = skill.category.value if hasattr(skill.category, "value") else str(skill.category)
|
||||
if not extensions.is_skill_enabled(skill.name, category):
|
||||
raise SkillPathUnavailableError("Skill path is unavailable")
|
||||
return skill
|
||||
|
||||
|
||||
def can_access_coordinator_only_skill(agent_id: str | None, skill_name: str) -> bool:
|
||||
"""Return whether this runtime identity may discover/read a reserved orchestration skill."""
|
||||
normalized = (skill_name or "").strip().lower()
|
||||
return normalized not in COORDINATOR_ONLY_SKILL_NAMES or agent_id == _ROUNDTABLE_COORDINATOR_AGENT_ID
|
||||
return normalized not in COORDINATOR_ONLY_SKILL_NAMES or agent_id in COORDINATOR_AGENT_IDS
|
||||
|
||||
|
||||
def is_coordinator_only_skill_path(path: str) -> bool:
|
||||
@@ -66,22 +124,31 @@ def _load_agent_skill_whitelist(agent_id: str | None, extra_skills: list[str] |
|
||||
through this tool.
|
||||
- Non-empty set → only these names are visible to this agent.
|
||||
|
||||
Exception — the deep-research collector (``deep-research-collector``) has no
|
||||
whitelist: it sees every skill like the default lead agent, matching the
|
||||
runtime allowlist in ``lead_agent.agent``. Its on-disk ``skills:`` entry is
|
||||
ignored.
|
||||
|
||||
``extra_skills`` only widens the **deep-research collector** allowlist
|
||||
for one run (report-structure 检索来源). Other agents ignore it.
|
||||
"""
|
||||
if not agent_id:
|
||||
return None
|
||||
try:
|
||||
from deerflow.agents.deep_research.retrieval import COLLECTOR_AGENT_ID
|
||||
from deerflow.config.agents_config import load_agent_config
|
||||
|
||||
cfg = load_agent_config(agent_id)
|
||||
if cfg is None:
|
||||
return None
|
||||
allowed = {
|
||||
name
|
||||
for name in (cfg.skills or [])
|
||||
if can_access_coordinator_only_skill(agent_id, name)
|
||||
}
|
||||
if agent_id == COLLECTOR_AGENT_ID:
|
||||
allowed = None # collector: all skills, like the default lead agent
|
||||
else:
|
||||
allowed = {
|
||||
name
|
||||
for name in (cfg.skills or [])
|
||||
if can_access_coordinator_only_skill(agent_id, name)
|
||||
}
|
||||
except Exception:
|
||||
logger.debug("Failed to load agent config for skill_list filter", exc_info=True)
|
||||
return None
|
||||
@@ -89,8 +156,8 @@ def _load_agent_skill_whitelist(agent_id: str | None, extra_skills: list[str] |
|
||||
if extra_skills:
|
||||
from deerflow.agents.deep_research.retrieval import merge_collector_skill_allowlist
|
||||
|
||||
merged = merge_collector_skill_allowlist(agent_id, list(allowed), extra_skills)
|
||||
return set(merged or [])
|
||||
merged = merge_collector_skill_allowlist(agent_id, None if allowed is None else list(allowed), extra_skills)
|
||||
return set(merged or []) if merged is not None else None
|
||||
return allowed
|
||||
|
||||
|
||||
@@ -169,20 +236,21 @@ def skill_view_impl(name: str, agent_id: str | None, extra_skills: list[str] | N
|
||||
|
||||
storage = get_or_new_skill_storage()
|
||||
try:
|
||||
if storage.custom_skill_exists(name):
|
||||
content = storage.read_custom_skill(name)
|
||||
elif storage.public_skill_exists(name):
|
||||
skill_file = storage.get_skills_root_path()
|
||||
from pathlib import Path
|
||||
# A warm catalogue already knows each skill's file. Read that one file.
|
||||
# A cold catalogue checks the canonical directory before walking every
|
||||
# SKILL.md, which is only needed for a directory whose name differs.
|
||||
from deerflow.agents.lead_agent.prompt import _load_named_enabled_skill, get_warm_enabled_skills
|
||||
|
||||
from deerflow.skills.types import SKILL_MD_FILE
|
||||
|
||||
path = Path(skill_file) / "public" / name / SKILL_MD_FILE
|
||||
if not path.exists():
|
||||
return f"Skill '{name}' not found."
|
||||
content = path.read_text(encoding="utf-8")
|
||||
cached = get_warm_enabled_skills(storage)
|
||||
if cached is not None:
|
||||
skill = next((item for item in cached if item.name == name), None)
|
||||
else:
|
||||
skill = _load_named_enabled_skill(name)
|
||||
if skill is None:
|
||||
skill = next((item for item in storage.load_skills(enabled_only=True) if item.name == name), None)
|
||||
if skill is None:
|
||||
return f"Skill '{name}' not found."
|
||||
content = skill.skill_file.read_text(encoding="utf-8")
|
||||
except Exception as e:
|
||||
logger.debug("skill_view failed for %r: %s", name, e)
|
||||
return f"Error reading skill '{name}': {e}"
|
||||
@@ -260,7 +328,7 @@ def search_skills_impl(query: str, agent_id: str | None, top_n: int = 5, extra_s
|
||||
|
||||
try:
|
||||
storage = get_or_new_skill_storage()
|
||||
skills = storage.load_skills(enabled_only=True)
|
||||
skills = _skills_for_catalogue(storage)
|
||||
usage = all_entries()
|
||||
base_path = _resolve_skills_container_base_path()
|
||||
scored: list[tuple[int, dict]] = []
|
||||
@@ -336,11 +404,29 @@ def skill_list_tool(runtime: ToolRuntime) -> str:
|
||||
return skill_list_impl(_resolve_active_agent_id(runtime), _resolve_extra_skills(runtime))
|
||||
|
||||
|
||||
def _latest_activity(entry: dict) -> str | None:
|
||||
candidates = [entry.get("last_used_at"), entry.get("last_viewed_at"), entry.get("last_patched_at")]
|
||||
valid = [ts for ts in candidates if ts]
|
||||
if valid:
|
||||
return max(valid)
|
||||
return entry.get("created_at")
|
||||
|
||||
|
||||
def _skills_for_catalogue(storage):
|
||||
"""Use the warm prompt catalogue, and parse SKILL.md files only when it is cold."""
|
||||
from deerflow.agents.lead_agent.prompt import get_warm_enabled_skills
|
||||
|
||||
cached = get_warm_enabled_skills(storage)
|
||||
if cached is not None:
|
||||
return cached
|
||||
return storage.load_skills(enabled_only=True)
|
||||
|
||||
|
||||
def skill_list_impl(agent_id: str | None, extra_skills: list[str] | None = None) -> str:
|
||||
"""List skills visible to ``agent_id`` (None = default lead agent)."""
|
||||
from deerflow.skills.storage import get_or_new_skill_storage
|
||||
from deerflow.skills.types import SkillCategory
|
||||
from deerflow.skills.usage import STATE_ARCHIVED, all_entries, latest_activity_at
|
||||
from deerflow.skills.usage import STATE_ARCHIVED, all_entries
|
||||
|
||||
allowed = _load_agent_skill_whitelist(agent_id, extra_skills)
|
||||
|
||||
@@ -353,7 +439,7 @@ def skill_list_impl(agent_id: str | None, extra_skills: list[str] | None = None)
|
||||
|
||||
try:
|
||||
storage = get_or_new_skill_storage()
|
||||
skills = storage.load_skills(enabled_only=False)
|
||||
skills = _skills_for_catalogue(storage)
|
||||
usage = all_entries()
|
||||
result = []
|
||||
for skill in skills:
|
||||
@@ -380,7 +466,7 @@ def skill_list_impl(agent_id: str | None, extra_skills: list[str] | None = None)
|
||||
"use_count": entry.get("use_count", 0),
|
||||
"view_count": entry.get("view_count", 0),
|
||||
"patch_count": entry.get("patch_count", 0),
|
||||
"last_activity_at": latest_activity_at(skill.name),
|
||||
"last_activity_at": _latest_activity(entry),
|
||||
"pinned": entry.get("pinned", False),
|
||||
"source": entry.get("source", "upload"),
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user