sync update from LAN repo (source ji @ 85078a31)

This commit is contained in:
gozeros committed 2026-10-21 15:09:16 +08:00
1 parent 5d6627af50
commit e602d88695
864 files changed
+99150 -6346

No files matched your search

@@ -22,7 +22,7 @@ from app.gateway.deps import get_agent_store, get_config, get_skill_store, get_t
from app.gateway.path_utils import aresolve_thread_virtual_path
from deerflow.agents.lead_agent.prompt import refresh_skills_system_prompt_cache_async
from deerflow.config.app_config import AppConfig
from deerflow.config.extensions_config import ExtensionsConfig, SkillDisplayConfig, SkillStateConfig, get_extensions_config, reload_extensions_config
from deerflow.config.extensions_config import ExtensionsConfig, SkillDisplayConfig, get_extensions_config, patch_extensions_config
from deerflow.persistence.engine import get_engine, get_session_factory
from deerflow.persistence.skills import SkillStore
from deerflow.persistence.skills.model import SkillDisplayAdapterRow
@@ -405,13 +405,6 @@ def _skill_to_response(
)
def _dump_extensions_config(config_obj: ExtensionsConfig) -> dict[str, Any]:
return {
"mcpServers": {name: server.model_dump(by_alias=True) for name, server in config_obj.mcp_servers.items()},
"skills": {name: skill_config.model_dump(by_alias=True, exclude_none=True) for name, skill_config in config_obj.skills.items()},
}
async def _ensure_skill_display_table() -> bool:
"""Create the display adapter table when DB persistence is enabled."""
engine = get_engine()
@@ -1177,7 +1170,15 @@ async def _preview_install_target(
if owned_by_me:
return False, True, True, False, False, owner_user_id, owner_username, f"You have already uploaded skill '{skill_name}'. You may upload it again and overwrite your existing copy."
if unattributed_custom_conflict:
return False, False, False, False, False, owner_user_id, owner_username, f"Skill '{skill_name}' already exists, but its owner could not be determined. It cannot be overwritten safely."
# An on-disk custom skill with no ownership row is a leftover nobody
# can see or manage (interrupted install, restored skills dir, DB reset).
# Let the uploader replace it and claim ownership, mirroring
# /skills/reconcile-custom's adoption semantics.
message = (
f"Skill '{skill_name}' exists on disk but has no owner record (orphaned leftover that is invisible in every list). "
"Uploading again will replace it and register it under your account."
)
return False, True, False, False, False, owner_user_id, owner_username, message
return True, False, False, False, False, owner_user_id, owner_username, f"Skill '{skill_name}' can be installed."
@@ -1699,31 +1700,21 @@ async def delete_custom_skill(
def _remove_skill_state_config(skill_name: str) -> None:
"""Remove a deleted skill's enabled/display override from extensions config."""
extensions_config = get_extensions_config()
if skill_name not in extensions_config.skills:
return
config_path = ExtensionsConfig.resolve_config_path()
if config_path is None:
config_path = Path.cwd().parent / "extensions_config.json"
extensions_config.skills.pop(skill_name, None)
config_data = _dump_extensions_config(extensions_config)
tmp_path: Path | None = None
try:
with tempfile.NamedTemporaryFile(
"w",
encoding="utf-8",
delete=False,
dir=str(config_path.parent),
) as tmp_file:
json.dump(config_data, tmp_file, indent=2)
tmp_path = Path(tmp_file.name)
os.replace(tmp_path, config_path)
finally:
if tmp_path is not None and tmp_path.exists():
tmp_path.unlink()
reload_extensions_config()
def _remove(config_data: dict[str, Any]) -> None:
skill_states = config_data.get("skills")
if skill_states is None:
return
if not isinstance(skill_states, dict):
raise ValueError("Extensions config 'skills' must be a JSON object")
if skill_name not in skill_states:
return
skill_states.pop(skill_name)
patch_extensions_config(config_path, _remove)
@router.delete(
@@ -2661,7 +2652,13 @@ async def delete_skill_file(
summary="Update Skill",
description="Update a skill's enabled status by modifying the extensions_config.json file.",
)
async def update_skill(skill_name: str, request: SkillUpdateRequest, config: AppConfig = Depends(get_config)) -> SkillResponse:
async def update_skill(
skill_name: str,
request: SkillUpdateRequest,
http_request: Request,
config: AppConfig = Depends(get_config),
skill_store: SkillStore = Depends(get_skill_store),
) -> SkillResponse:
try:
skill_name = skill_name.replace("\r\n", "").replace("\n", "")
skills = get_or_new_skill_storage(app_config=config).load_skills(enabled_only=False)
@@ -2670,25 +2667,51 @@ async def update_skill(skill_name: str, request: SkillUpdateRequest, config: App
if skill is None:
raise HTTPException(status_code=404, detail=f"Skill '{skill_name}' not found")
# Administrators may manage every discovered skill. Ordinary users
# may only toggle a custom skill whose persisted ownership row names
# them as the creator. A custom directory without an ownership row is
# deliberately not claimable through this endpoint.
if not _is_admin_user(http_request):
if skill.category != SkillCategory.CUSTOM:
raise HTTPException(
status_code=403,
detail="Only administrators can update built-in skill availability",
)
user = getattr(http_request.state, "user", None)
if user is None or getattr(user, "id", None) in (None, ""):
raise HTTPException(status_code=403, detail="Authentication is required")
user_id = str(user.id)
if await skill_store.get_owned(skill_name, user_id) is None:
raise HTTPException(
status_code=403,
detail="You can only update availability for custom skills that you own",
)
config_path = ExtensionsConfig.resolve_config_path()
if config_path is None:
config_path = Path.cwd().parent / "extensions_config.json"
logger.info(f"No existing extensions config found. Creating new config at: {config_path}")
extensions_config = get_extensions_config()
current_skill_config = extensions_config.skills.get(skill_name)
extensions_config.skills[skill_name] = SkillStateConfig(
enabled=request.enabled,
display=current_skill_config.display if current_skill_config else None,
)
# Read the file afresh and patch only ``skills[name].enabled``. Do not
# round-trip through ExtensionsConfig: that resolves $ENV placeholders
# and can discard unknown fields belonging to newer deployments.
def _patch_enabled(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(skill_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 '{skill_name}' must be a JSON object")
else:
raw_skill_state = dict(raw_skill_state)
raw_skill_state["enabled"] = request.enabled
skill_states[skill_name] = raw_skill_state
config_data = _dump_extensions_config(extensions_config)
with open(config_path, "w", encoding="utf-8") as f:
json.dump(config_data, f, indent=2)
patch_extensions_config(config_path, _patch_enabled)
logger.info(f"Skills configuration updated and saved to: {config_path}")
reload_extensions_config()
await refresh_skills_system_prompt_cache_async()
skills = get_or_new_skill_storage(app_config=config).load_skills(enabled_only=False)
@@ -2789,13 +2812,6 @@ async def update_skill_display(
config_path = Path.cwd().parent / "extensions_config.json"
logger.info(f"No existing extensions config found. Creating new config at: {config_path}")
extensions_config = get_extensions_config()
current_skill_config = extensions_config.skills.get(skill_name)
extensions_config.skills[skill_name] = SkillStateConfig(
enabled=current_skill_config.enabled if current_skill_config else skill.enabled,
display=request.display,
)
display_payload = request.display.model_dump(by_alias=True) if request.display is not None else None
sample_payload = request.sample if "sample" in request.model_fields_set else ...
await _upsert_skill_display_adapter(
@@ -2804,10 +2820,27 @@ async def update_skill_display(
sample=sample_payload,
)
with open(config_path, "w", encoding="utf-8") as f:
json.dump(_dump_extensions_config(extensions_config), f, indent=2, ensure_ascii=False)
# Share the same read/modify/write lock as the availability endpoint so
# concurrent display and enabled updates cannot overwrite one another.
# Patch the raw document to preserve env placeholders and unknown keys.
def _patch_display(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(skill_name)
if raw_skill_state is None:
raw_skill_state = {"enabled": skill.enabled}
elif not isinstance(raw_skill_state, dict):
raise ValueError(f"Extensions config state for skill '{skill_name}' must be a JSON object")
else:
raw_skill_state = dict(raw_skill_state)
if display_payload is None:
raw_skill_state.pop("display", None)
else:
raw_skill_state["display"] = display_payload
skill_states[skill_name] = raw_skill_state
reload_extensions_config()
patch_extensions_config(config_path, _patch_display)
await refresh_skills_system_prompt_cache_async()
skills = get_or_new_skill_storage(app_config=config).load_skills(enabled_only=False)
updated_skill = next((s for s in skills if s.name == skill_name), None)