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

@@ -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,21 +405,51 @@ 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()},
}
# ``checkfirst=True`` is a check-then-create operation. The in-process lock
# serializes concurrent requests in one worker; the duplicate-table fallback
# covers two Gateway workers racing during first access.
_SKILL_DISPLAY_TABLE_LOCK = asyncio.Lock()
_skill_display_table_engine: object | None = None
def _skill_display_table_already_exists(exc: BaseException) -> bool:
message = str(exc).lower()
return any(
token in message
for token in (
"already exists",
"duplicate table",
"42p07",
"error 1050",
"(1050",
)
)
async def _ensure_skill_display_table() -> bool:
"""Create the display adapter table when DB persistence is enabled."""
"""Create the display adapter table once when DB persistence is enabled."""
global _skill_display_table_engine
engine = get_engine()
sf = get_session_factory()
if engine is None or sf is None:
return False
async with engine.begin() as conn:
await conn.run_sync(SkillDisplayAdapterRow.__table__.create, checkfirst=True)
if _skill_display_table_engine is engine:
return True
async with _SKILL_DISPLAY_TABLE_LOCK:
if _skill_display_table_engine is engine:
return True
try:
async with engine.begin() as conn:
await conn.run_sync(SkillDisplayAdapterRow.__table__.create, checkfirst=True)
except Exception as exc:
# The transaction context rolled back before this exception is
# accepted as the harmless result of another worker winning.
if not _skill_display_table_already_exists(exc):
raise
logger.info("Skill display adapter table was created concurrently; continuing")
_skill_display_table_engine = engine
return True
@@ -1177,7 +1207,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."
@@ -1685,6 +1723,7 @@ async def delete_custom_skill(
)
await refresh_skills_system_prompt_cache_async()
await _sync_skill_view(skill_name)
return {"success": True}
except HTTPException:
raise
@@ -1699,31 +1738,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(
@@ -1780,6 +1809,7 @@ async def delete_builtin_skill(
agent_store=agent_store,
)
await refresh_skills_system_prompt_cache_async()
await _sync_skill_view(skill.name)
logger.info("Administrator deleted built-in skill %s", skill.name)
return {"success": True}
except HTTPException:
@@ -2491,6 +2521,20 @@ async def download_skill_file(
)
async def _sync_skill_view(name: str) -> None:
"""Copy or remove one skill in the model-facing sandbox view.
The canonical write has already succeeded. A sync failure is logged so the
save still returns; the next command that names this skill reconciles it.
"""
try:
from deerflow.sandbox.skill_view import sync_skill_sandbox_skill
await asyncio.to_thread(sync_skill_sandbox_skill, name)
except Exception:
logger.warning("Failed to sync skill %s into the sandbox view", name, exc_info=True)
@router.put(
"/skills/{skill_name}/files/content",
response_model=SkillFileSaveResponse,
@@ -2568,6 +2612,8 @@ async def save_skill_file(
pass
await refresh_skills_system_prompt_cache_async()
await _sync_skill_view(skill.name)
stat = target.stat()
return SkillFileSaveResponse(
name=skill.name,
@@ -2644,6 +2690,7 @@ async def delete_skill_file(
logger.warning("Failed to append deletion history for %s/%s: %s", skill.name, relative_posix, history_err)
else:
logger.info("Administrator deleted built-in skill file %s/%s", skill.name, relative_posix)
await _sync_skill_view(skill.name)
except HTTPException:
raise
except ValueError as e:
@@ -2661,7 +2708,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,26 +2723,53 @@ 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()
await _sync_skill_view(skill_name)
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)
@@ -2789,13 +2869,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 +2877,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)