Update local frontend and backend changes
This commit is contained in:
1 parent
faca2c9a2d
commit
a27206c543
35 files changed
+1756
-334
No files matched your search
@@ -47,6 +47,13 @@ class ScheduledTaskSubscribeRequest(BaseModel):
|
||||
notify_element: bool = False
|
||||
|
||||
|
||||
class ScheduledTaskRunFileUpdateRequest(BaseModel):
|
||||
content: str = Field(..., description="New text content to write into the run file")
|
||||
|
||||
|
||||
_MAX_RUN_FILE_EDIT_BYTES = 5 * 1024 * 1024 # 5 MB
|
||||
|
||||
|
||||
async def _user_id(request: Request) -> str:
|
||||
user_id = await get_current_user(request)
|
||||
if not user_id:
|
||||
@@ -184,6 +191,27 @@ async def get_scheduled_task_run_file(run_id: str, file_index: int, request: Req
|
||||
}
|
||||
|
||||
|
||||
@router.put("/runs/{run_id}/files/{file_index}")
|
||||
async def update_scheduled_task_run_file(
|
||||
run_id: str,
|
||||
file_index: int,
|
||||
body: ScheduledTaskRunFileUpdateRequest,
|
||||
request: Request,
|
||||
) -> dict:
|
||||
if len(body.content.encode("utf-8")) > _MAX_RUN_FILE_EDIT_BYTES:
|
||||
raise HTTPException(status_code=413, detail="Edited content exceeds the 5 MB limit")
|
||||
service = get_scheduled_task_service()
|
||||
user_id = await _user_id(request)
|
||||
try:
|
||||
return await service.update_run_file(user_id, run_id, file_index, body.content)
|
||||
except PermissionError as exc:
|
||||
raise HTTPException(status_code=403, detail=str(exc))
|
||||
except FileNotFoundError as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc))
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc))
|
||||
|
||||
|
||||
@router.get("/runs/{run_id}/download")
|
||||
async def download_scheduled_task_run(run_id: str, request: Request):
|
||||
service = get_scheduled_task_service()
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import json
|
||||
import logging
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.gateway.deps import get_config
|
||||
@@ -56,6 +57,20 @@ class SkillInstallResponse(BaseModel):
|
||||
success: bool = Field(..., description="Whether the installation was successful")
|
||||
skill_name: str = Field(..., description="Name of the installed skill")
|
||||
message: str = Field(..., description="Installation result message")
|
||||
overwritten: bool = Field(default=False, description="True if an existing skill was replaced")
|
||||
|
||||
|
||||
class SkillUploadValidateResponse(BaseModel):
|
||||
"""Response model for the pre-install validation endpoint."""
|
||||
|
||||
ok: bool = Field(..., description="Whether the archive passed cheap validation")
|
||||
skill_name: str = Field(..., description="Skill name parsed from frontmatter")
|
||||
description: str = Field(default="", description="Skill description parsed from frontmatter")
|
||||
license: str | None = Field(default=None, description="Skill license, if declared")
|
||||
allowed_tools: list[str] = Field(default_factory=list, description="Tools the skill may invoke")
|
||||
conflict: bool = Field(default=False, description="True if a skill with the same name already exists")
|
||||
conflict_kind: str | None = Field(default=None, description="'public' or 'custom' when conflict=true")
|
||||
warnings: list[str] = Field(default_factory=list, description="Non-fatal warnings to surface to the user")
|
||||
|
||||
|
||||
class CustomSkillContentResponse(SkillResponse):
|
||||
@@ -100,6 +115,110 @@ async def list_skills(config: AppConfig = Depends(get_config)) -> SkillsListResp
|
||||
raise HTTPException(status_code=500, detail=f"Failed to load skills: {str(e)}")
|
||||
|
||||
|
||||
_MAX_SKILL_UPLOAD_BYTES = 512 * 1024 * 1024
|
||||
_ZIP_MAGIC = b"PK\x03\x04"
|
||||
|
||||
|
||||
async def _persist_uploaded_skill(file: UploadFile) -> Path:
|
||||
"""Stream an uploaded skill archive to a temp file. Caller must unlink it.
|
||||
|
||||
Validates the leading bytes look like a ZIP archive and enforces the same
|
||||
512 MB cap that the extractor uses, so we reject obvious garbage early.
|
||||
"""
|
||||
if file.filename is None:
|
||||
raise HTTPException(status_code=400, detail="No file provided")
|
||||
suffix = Path(file.filename).suffix.lower()
|
||||
if suffix not in {".zip", ".skill"}:
|
||||
raise HTTPException(status_code=400, detail="Skill upload must be a .zip or .skill file")
|
||||
|
||||
fd, tmp_name = tempfile.mkstemp(suffix=".skill", prefix="skill-upload-")
|
||||
tmp_path = Path(tmp_name)
|
||||
total = 0
|
||||
head: bytes = b""
|
||||
try:
|
||||
with open(fd, "wb") as out:
|
||||
while True:
|
||||
chunk = await file.read(1024 * 1024)
|
||||
if not chunk:
|
||||
break
|
||||
if not head:
|
||||
head = chunk[: len(_ZIP_MAGIC)]
|
||||
total += len(chunk)
|
||||
if total > _MAX_SKILL_UPLOAD_BYTES:
|
||||
raise HTTPException(status_code=400, detail="Uploaded skill archive exceeds the 512 MB limit")
|
||||
out.write(chunk)
|
||||
except HTTPException:
|
||||
tmp_path.unlink(missing_ok=True)
|
||||
raise
|
||||
except Exception:
|
||||
tmp_path.unlink(missing_ok=True)
|
||||
raise
|
||||
|
||||
if not head.startswith(_ZIP_MAGIC):
|
||||
tmp_path.unlink(missing_ok=True)
|
||||
raise HTTPException(status_code=400, detail="Uploaded file is not a valid ZIP archive")
|
||||
return tmp_path
|
||||
|
||||
|
||||
@router.post(
|
||||
"/skills/validate",
|
||||
response_model=SkillUploadValidateResponse,
|
||||
summary="Validate Skill Archive (No Install)",
|
||||
description="Inspect an uploaded .zip/.skill archive and report parsed metadata + conflicts. Does NOT install or run the LLM security scan.",
|
||||
)
|
||||
async def validate_skill_upload(
|
||||
file: UploadFile = File(..., description="The .zip or .skill archive to validate"),
|
||||
config: AppConfig = Depends(get_config),
|
||||
) -> SkillUploadValidateResponse:
|
||||
tmp_path = await _persist_uploaded_skill(file)
|
||||
try:
|
||||
result = get_or_new_skill_storage(app_config=config).validate_skill_archive(tmp_path)
|
||||
return SkillUploadValidateResponse(**result)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
except FileNotFoundError as e:
|
||||
raise HTTPException(status_code=404, detail=str(e))
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error("Failed to validate uploaded skill: %s", e, exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=f"Failed to validate skill: {e}")
|
||||
finally:
|
||||
tmp_path.unlink(missing_ok=True)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/skills/install-upload",
|
||||
response_model=SkillInstallResponse,
|
||||
summary="Install Skill from Upload",
|
||||
description="Install a skill directly from an uploaded .zip/.skill archive. Set ?overwrite=true to replace an existing skill with the same name.",
|
||||
)
|
||||
async def install_skill_upload(
|
||||
file: UploadFile = File(..., description="The .zip or .skill archive to install"),
|
||||
overwrite: bool = False,
|
||||
config: AppConfig = Depends(get_config),
|
||||
) -> SkillInstallResponse:
|
||||
tmp_path = await _persist_uploaded_skill(file)
|
||||
try:
|
||||
storage = get_or_new_skill_storage(app_config=config)
|
||||
result = await storage.ainstall_skill_from_archive(tmp_path, overwrite=overwrite, require_skill_suffix=False)
|
||||
await refresh_skills_system_prompt_cache_async()
|
||||
return SkillInstallResponse(**result)
|
||||
except FileNotFoundError as e:
|
||||
raise HTTPException(status_code=404, detail=str(e))
|
||||
except SkillAlreadyExistsError as e:
|
||||
raise HTTPException(status_code=409, detail=str(e))
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error("Failed to install uploaded skill: %s", e, exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=f"Failed to install skill: {e}")
|
||||
finally:
|
||||
tmp_path.unlink(missing_ok=True)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/skills/install",
|
||||
response_model=SkillInstallResponse,
|
||||
|
||||
+19
-21
@@ -384,7 +384,7 @@ task(description="Oracle Cloud analysis", prompt="...", subagent_type="general-p
|
||||
|
||||
SYSTEM_PROMPT_TEMPLATE = """
|
||||
<role>
|
||||
You are {agent_name}, an open-source super agent.
|
||||
You are {agent_name}, an internal super agent.
|
||||
</role>
|
||||
|
||||
{soul}
|
||||
@@ -438,21 +438,19 @@ You are {agent_name}, an open-source super agent.
|
||||
- ❌ DO NOT skip clarification for "efficiency" - accuracy matters more than speed
|
||||
- ❌ DO NOT make assumptions when information is missing - ALWAYS ask
|
||||
- ❌ DO NOT proceed with guesses - STOP and call ask_clarification first
|
||||
- ✅ Analyze the request in thinking → Identify unclear aspects → Ask BEFORE any action
|
||||
- ✅ If you identify the need for clarification in your thinking, you MUST call the tool IMMEDIATELY
|
||||
- ✅ After calling ask_clarification, execution will be interrupted automatically
|
||||
- ✅ Wait for user response - do NOT continue with assumptions
|
||||
- ✅ When practical, provide 3-4 clear, mutually distinct options so the user can choose quickly
|
||||
- ✅ Keep options concise and actionable; the user can still provide a custom answer if none fit
|
||||
- ✅ Analyze the request in thinking → Identify unclear aspects → Ask BEFORE any action
|
||||
- ✅ If you identify the need for clarification in your thinking, you MUST call the tool IMMEDIATELY
|
||||
- ✅ After calling ask_clarification, execution will be interrupted automatically
|
||||
- ✅ Wait for user response - do NOT continue with assumptions
|
||||
|
||||
**How to Use:**
|
||||
```python
|
||||
ask_clarification(
|
||||
question="Your specific question here?",
|
||||
clarification_type="missing_info", # or other type
|
||||
context="Why you need this information", # optional but recommended
|
||||
options=["option1", "option2", "option3"] # use 3-4 options when practical
|
||||
)
|
||||
ask_clarification(
|
||||
question="Your specific question here?",
|
||||
clarification_type="missing_info", # or other type
|
||||
context="Why you need this information", # optional but recommended
|
||||
options=["option1", "option2"] # optional, for choices
|
||||
)
|
||||
```
|
||||
|
||||
**Example:**
|
||||
@@ -519,10 +517,10 @@ Recent breakthroughs in language models have also accelerated progress
|
||||
```markdown
|
||||
## Executive Summary
|
||||
|
||||
DeerFlow is an open-source AI agent framework that gained significant traction in early 2026
|
||||
[citation:GitHub Repository](https://github.com/bytedance/deer-flow). The project focuses on
|
||||
providing a production-ready agent system with sandbox execution and memory management
|
||||
[citation:DeerFlow Documentation](https://deer-flow.dev/docs).
|
||||
LangGraph is an open-source workflow orchestration framework for LLM applications
|
||||
[citation:LangGraph Repository](https://github.com/langchain-ai/langgraph). The project
|
||||
focuses on providing stateful multi-agent graphs with built-in checkpointing and streaming
|
||||
[citation:LangGraph Documentation](https://langchain-ai.github.io/langgraph/).
|
||||
|
||||
## Key Analysis
|
||||
|
||||
@@ -534,8 +532,8 @@ combined with a FastAPI gateway for REST API access [citation:FastAPI](https://f
|
||||
## Sources
|
||||
|
||||
### Primary Sources
|
||||
- [GitHub Repository](https://github.com/bytedance/deer-flow) - Official source code and documentation
|
||||
- [DeerFlow Documentation](https://deer-flow.dev/docs) - Technical specifications
|
||||
- [LangGraph Repository](https://github.com/langchain-ai/langgraph) - Official source code and documentation
|
||||
- [LangGraph Documentation](https://langchain-ai.github.io/langgraph/) - Technical specifications
|
||||
|
||||
### Media Coverage
|
||||
- [AI Trends 2026](https://techcrunch.com/ai-trends) - Industry analysis
|
||||
@@ -547,7 +545,7 @@ combined with a FastAPI gateway for REST API access [citation:FastAPI](https://f
|
||||
- The `[citation:Title](URL)` format is ONLY for inline citations within the report body
|
||||
- ❌ WRONG: `GitHub 仓库 - 官方源代码和文档` (no URL!)
|
||||
- ❌ WRONG in Sources: `[citation:GitHub Repository](url)` (citation prefix is for inline only!)
|
||||
- ✅ RIGHT in Sources: `[GitHub Repository](https://github.com/bytedance/deer-flow) - 官方源代码和文档`
|
||||
- ✅ RIGHT in Sources: `[LangGraph Repository](https://github.com/langchain-ai/langgraph) - 官方源代码和文档`
|
||||
|
||||
**WORKFLOW for Research Tasks:**
|
||||
1. Use web_search to find sources → Extract {{title, url, snippet}} from results
|
||||
@@ -839,7 +837,7 @@ def apply_prompt_template(
|
||||
|
||||
# Format the prompt with dynamic skills and memory
|
||||
prompt = SYSTEM_PROMPT_TEMPLATE.format(
|
||||
agent_name=agent_name or storage_agent_id or "DeerFlow 2.0",
|
||||
agent_name=agent_name or storage_agent_id or "CM 助手",
|
||||
soul=get_agent_soul(storage_agent_id),
|
||||
skills_section=skills_section,
|
||||
deferred_tools_section=deferred_tools_section,
|
||||
|
||||
+71
@@ -252,6 +252,7 @@ class ScheduledTaskService:
|
||||
async def get_run(self, user_id: str, run_id: str) -> dict[str, Any] | None:
|
||||
run = await self.store.get_run(run_id, user_id)
|
||||
if run is not None:
|
||||
run["can_edit"] = True
|
||||
return run
|
||||
# Subscriber visibility: look up the run unfiltered, then verify the
|
||||
# caller has a subscription on the parent task.
|
||||
@@ -260,8 +261,78 @@ class ScheduledTaskService:
|
||||
return None
|
||||
if await self.store.get_subscription(candidate["task_id"], user_id) is None:
|
||||
return None
|
||||
candidate["can_edit"] = False
|
||||
return candidate
|
||||
|
||||
async def update_run_file(
|
||||
self,
|
||||
user_id: str,
|
||||
run_id: str,
|
||||
file_index: int,
|
||||
new_content: str,
|
||||
) -> dict[str, Any]:
|
||||
"""Overwrite an archived run file in place. Owner-only.
|
||||
|
||||
Returns updated file metadata. Raises:
|
||||
- PermissionError if the caller is not the run owner (subscribers blocked)
|
||||
- FileNotFoundError if the run or file index does not exist
|
||||
- ValueError if the file is not editable (binary / not previewable)
|
||||
"""
|
||||
run = await self.store.get_run(run_id, user_id)
|
||||
if run is None:
|
||||
# Either the run doesn't exist, or the caller is a subscriber, not owner.
|
||||
candidate = await self.store.get_run_any(run_id)
|
||||
if candidate is None:
|
||||
raise FileNotFoundError(f"Run {run_id} not found")
|
||||
raise PermissionError("Only the run owner may edit result files")
|
||||
|
||||
result = run.get("result") or {}
|
||||
files = result.get("files") if isinstance(result, dict) else None
|
||||
if not isinstance(files, list) or file_index < 0 or file_index >= len(files):
|
||||
raise FileNotFoundError(f"File index {file_index} not found in run {run_id}")
|
||||
item = files[file_index]
|
||||
if not isinstance(item, dict) or not isinstance(item.get("path"), str):
|
||||
raise FileNotFoundError(f"File index {file_index} has no archive path")
|
||||
|
||||
archive_path = Path(item["path"])
|
||||
if not archive_path.exists() or not archive_path.is_file():
|
||||
raise FileNotFoundError(f"Archived file {archive_path} is no longer available")
|
||||
|
||||
# Constrain edits to text-previewable files. We mirror the same rule the
|
||||
# router uses for previews so binary files cannot be silently corrupted.
|
||||
suffix = archive_path.suffix.lower()
|
||||
mime_type = item.get("mime_type")
|
||||
is_text = (
|
||||
(isinstance(mime_type, str) and (mime_type.startswith("text/") or mime_type in {"application/json", "application/xml"}))
|
||||
or suffix in {".md", ".markdown", ".txt", ".json", ".csv", ".xml", ".yaml", ".yml", ".log"}
|
||||
)
|
||||
if not is_text:
|
||||
raise ValueError("Only text-previewable files (markdown, txt, json, etc.) can be edited")
|
||||
|
||||
# Make sure we stay inside the per-run archive root before touching disk.
|
||||
archive_root = (get_paths().base_dir / "scheduled-task-runs" / run_id).resolve()
|
||||
try:
|
||||
archive_path.resolve().relative_to(archive_root)
|
||||
except ValueError as exc:
|
||||
raise PermissionError("Refusing to edit file outside the run archive") from exc
|
||||
|
||||
archive_path.write_text(new_content, encoding="utf-8")
|
||||
new_size = archive_path.stat().st_size
|
||||
|
||||
# Reflect the new size in the run row so subsequent list/preview calls
|
||||
# report the latest figure. Other fields stay untouched.
|
||||
files[file_index] = {**item, "size": new_size}
|
||||
new_result = {**result, "files": files}
|
||||
await self.store.update_run(run_id, {"result": new_result})
|
||||
|
||||
return {
|
||||
"index": file_index,
|
||||
"name": item.get("name") or archive_path.name,
|
||||
"size": new_size,
|
||||
"mime_type": mime_type,
|
||||
"kind": item.get("kind"),
|
||||
}
|
||||
|
||||
async def delete_run(self, user_id: str, run_id: str) -> bool:
|
||||
deleted = await self.store.delete_run(run_id, user_id)
|
||||
if deleted:
|
||||
|
||||
+113
-23
@@ -91,41 +91,48 @@ class LocalSkillStorage(SkillStorage):
|
||||
tmp_path = Path(tmp_file.name)
|
||||
tmp_path.replace(target)
|
||||
|
||||
async def ainstall_skill_from_archive(self, archive_path: str | Path) -> dict:
|
||||
def _open_skill_archive(self, archive_path: str | Path, *, require_skill_suffix: bool):
|
||||
import zipfile
|
||||
|
||||
from deerflow.skills.installer import (
|
||||
SkillAlreadyExistsError,
|
||||
_move_staged_skill_into_reserved_target,
|
||||
_scan_skill_archive_contents_or_raise,
|
||||
resolve_skill_dir_from_archive,
|
||||
safe_extract_skill_archive,
|
||||
)
|
||||
from deerflow.skills.validation import _validate_skill_frontmatter
|
||||
|
||||
logger.info("Installing skill from %s", archive_path)
|
||||
path = Path(archive_path)
|
||||
if not path.is_file():
|
||||
if not path.exists():
|
||||
raise FileNotFoundError(f"Skill file not found: {archive_path}")
|
||||
raise ValueError(f"Path is not a file: {archive_path}")
|
||||
if path.suffix != ".skill":
|
||||
if require_skill_suffix and path.suffix != ".skill":
|
||||
raise ValueError("File must have .skill extension")
|
||||
|
||||
custom_dir = self._host_root / "custom"
|
||||
custom_dir.mkdir(parents=True, exist_ok=True)
|
||||
try:
|
||||
zf = zipfile.ZipFile(path, "r")
|
||||
except FileNotFoundError:
|
||||
raise FileNotFoundError(f"Skill file not found: {archive_path}") from None
|
||||
except (zipfile.BadZipFile, IsADirectoryError):
|
||||
raise ValueError("File is not a valid ZIP archive") from None
|
||||
return zf
|
||||
|
||||
def validate_skill_archive(self, archive_path: str | Path, *, require_skill_suffix: bool = False) -> dict:
|
||||
"""Inspect a skill archive WITHOUT installing it.
|
||||
|
||||
Returns a dict with ``skill_name``, ``description``, ``license``,
|
||||
``allowed_tools``, ``conflict`` (bool), ``warnings`` (list[str]).
|
||||
Raises ``ValueError`` for fatal validation problems (bad zip, bad
|
||||
frontmatter). The expensive LLM security scan is **not** invoked here.
|
||||
"""
|
||||
import re
|
||||
|
||||
import yaml
|
||||
|
||||
from deerflow.skills.installer import (
|
||||
resolve_skill_dir_from_archive,
|
||||
safe_extract_skill_archive,
|
||||
)
|
||||
from deerflow.skills.types import SKILL_MD_FILE
|
||||
from deerflow.skills.validation import _validate_skill_frontmatter
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
tmp_path = Path(tmp)
|
||||
|
||||
try:
|
||||
zf = zipfile.ZipFile(path, "r")
|
||||
except FileNotFoundError:
|
||||
raise FileNotFoundError(f"Skill file not found: {archive_path}") from None
|
||||
except (zipfile.BadZipFile, IsADirectoryError):
|
||||
raise ValueError("File is not a valid ZIP archive") from None
|
||||
|
||||
with zf:
|
||||
with self._open_skill_archive(archive_path, require_skill_suffix=require_skill_suffix) as zf:
|
||||
safe_extract_skill_archive(zf, tmp_path)
|
||||
|
||||
skill_dir = resolve_skill_dir_from_archive(tmp_path)
|
||||
@@ -136,9 +143,91 @@ class LocalSkillStorage(SkillStorage):
|
||||
if not skill_name or "/" in skill_name or "\\" in skill_name or ".." in skill_name:
|
||||
raise ValueError(f"Invalid skill name: {skill_name}")
|
||||
|
||||
skill_md_text = (skill_dir / SKILL_MD_FILE).read_text(encoding="utf-8")
|
||||
frontmatter: dict = {}
|
||||
match = re.match(r"^---\n(.*?)\n---", skill_md_text, re.DOTALL)
|
||||
if match:
|
||||
try:
|
||||
parsed = yaml.safe_load(match.group(1)) or {}
|
||||
if isinstance(parsed, dict):
|
||||
frontmatter = parsed
|
||||
except yaml.YAMLError:
|
||||
frontmatter = {}
|
||||
|
||||
allowed_tools = frontmatter.get("allowed-tools") or []
|
||||
if not isinstance(allowed_tools, list):
|
||||
allowed_tools = [str(allowed_tools)]
|
||||
else:
|
||||
allowed_tools = [str(item) for item in allowed_tools]
|
||||
|
||||
warnings: list[str] = []
|
||||
script_files = [
|
||||
path for path in skill_dir.rglob("*")
|
||||
if path.is_file() and path.relative_to(skill_dir).parts and path.relative_to(skill_dir).parts[0] == "scripts"
|
||||
]
|
||||
if script_files:
|
||||
warnings.append(f"Archive contains {len(script_files)} script file(s); they will be security-scanned at install time.")
|
||||
|
||||
conflict_custom = (self._host_root / SkillCategory.CUSTOM.value / skill_name / SKILL_MD_FILE).exists()
|
||||
conflict_public = self.public_skill_exists(skill_name)
|
||||
conflict = conflict_custom or conflict_public
|
||||
|
||||
return {
|
||||
"ok": True,
|
||||
"skill_name": skill_name,
|
||||
"description": str(frontmatter.get("description", "")).strip(),
|
||||
"license": (str(frontmatter["license"]).strip() if frontmatter.get("license") else None),
|
||||
"allowed_tools": allowed_tools,
|
||||
"conflict": conflict,
|
||||
"conflict_kind": ("public" if conflict_public else ("custom" if conflict_custom else None)),
|
||||
"warnings": warnings,
|
||||
}
|
||||
|
||||
async def ainstall_skill_from_archive(
|
||||
self,
|
||||
archive_path: str | Path,
|
||||
*,
|
||||
overwrite: bool = False,
|
||||
require_skill_suffix: bool = True,
|
||||
) -> dict:
|
||||
from deerflow.skills.installer import (
|
||||
SkillAlreadyExistsError,
|
||||
_move_staged_skill_into_reserved_target,
|
||||
_scan_skill_archive_contents_or_raise,
|
||||
resolve_skill_dir_from_archive,
|
||||
safe_extract_skill_archive,
|
||||
)
|
||||
from deerflow.skills.validation import _validate_skill_frontmatter
|
||||
|
||||
logger.info("Installing skill from %s (overwrite=%s)", archive_path, overwrite)
|
||||
|
||||
custom_dir = self._host_root / "custom"
|
||||
custom_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
tmp_path = Path(tmp)
|
||||
|
||||
with self._open_skill_archive(archive_path, require_skill_suffix=require_skill_suffix) as zf:
|
||||
safe_extract_skill_archive(zf, tmp_path)
|
||||
|
||||
skill_dir = resolve_skill_dir_from_archive(tmp_path)
|
||||
|
||||
is_valid, message, skill_name = _validate_skill_frontmatter(skill_dir)
|
||||
if not is_valid:
|
||||
raise ValueError(f"Invalid skill: {message}")
|
||||
if not skill_name or "/" in skill_name or "\\" in skill_name or ".." in skill_name:
|
||||
raise ValueError(f"Invalid skill name: {skill_name}")
|
||||
|
||||
if self.public_skill_exists(skill_name):
|
||||
raise SkillAlreadyExistsError(f"Skill '{skill_name}' conflicts with a built-in public skill and cannot be overwritten")
|
||||
|
||||
target = custom_dir / skill_name
|
||||
replaced = False
|
||||
if target.exists():
|
||||
raise SkillAlreadyExistsError(f"Skill '{skill_name}' already exists")
|
||||
if not overwrite:
|
||||
raise SkillAlreadyExistsError(f"Skill '{skill_name}' already exists")
|
||||
shutil.rmtree(target)
|
||||
replaced = True
|
||||
|
||||
await _scan_skill_archive_contents_or_raise(skill_dir, skill_name)
|
||||
|
||||
@@ -152,6 +241,7 @@ class LocalSkillStorage(SkillStorage):
|
||||
"success": True,
|
||||
"skill_name": skill_name,
|
||||
"message": f"Skill '{skill_name}' installed successfully",
|
||||
"overwritten": replaced,
|
||||
}
|
||||
|
||||
def delete_custom_skill(self, name: str, *, history_meta: dict | None = None) -> None:
|
||||
|
||||
@@ -0,0 +1,206 @@
|
||||
"""Tests for editing archived scheduled-task run files."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from deerflow.config import paths as paths_module
|
||||
from deerflow.persistence.scheduled_tasks import MemoryScheduledTaskStore
|
||||
from deerflow.runtime.scheduler.service import ScheduledTaskService
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def isolated_paths(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setenv("DEER_FLOW_HOME", str(tmp_path))
|
||||
monkeypatch.setattr(paths_module, "_paths", None)
|
||||
yield tmp_path
|
||||
monkeypatch.setattr(paths_module, "_paths", None)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def store() -> MemoryScheduledTaskStore:
|
||||
return MemoryScheduledTaskStore()
|
||||
|
||||
|
||||
def _build_service(store: MemoryScheduledTaskStore) -> ScheduledTaskService:
|
||||
service = ScheduledTaskService(app=None, store=store, poll_interval_seconds=60.0)
|
||||
service._element_client = None # noqa: SLF001
|
||||
return service
|
||||
|
||||
|
||||
def _seed_task(store: MemoryScheduledTaskStore, *, task_id: str, user_id: str, published: bool = False) -> None:
|
||||
store._tasks[task_id] = { # noqa: SLF001
|
||||
"task_id": task_id,
|
||||
"user_id": user_id,
|
||||
"scheduler_thread_id": f"thread-{user_id}",
|
||||
"name": "demo",
|
||||
"prompt": "hi",
|
||||
"schedule_text": "0 9 * * *",
|
||||
"cron_expr": "0 9 * * *",
|
||||
"timezone": "UTC",
|
||||
"enabled": True,
|
||||
"next_run_at": datetime.now(UTC),
|
||||
"execution_context": {},
|
||||
"published": published,
|
||||
"published_at": datetime.now(UTC) if published else None,
|
||||
}
|
||||
|
||||
|
||||
async def _seed_run_with_file(
|
||||
store: MemoryScheduledTaskStore,
|
||||
isolated_paths: Path,
|
||||
*,
|
||||
run_id: str,
|
||||
task_id: str,
|
||||
user_id: str,
|
||||
file_name: str,
|
||||
file_text: str,
|
||||
mime_type: str = "text/markdown",
|
||||
) -> dict[str, Any]:
|
||||
archive_dir = isolated_paths / "scheduled-task-runs" / run_id / "files"
|
||||
archive_dir.mkdir(parents=True)
|
||||
file_path = archive_dir / file_name
|
||||
file_path.write_text(file_text, encoding="utf-8")
|
||||
|
||||
file_entry = {
|
||||
"name": file_name,
|
||||
"path": str(file_path),
|
||||
"mime_type": mime_type,
|
||||
"kind": "markdown" if file_name.endswith(".md") else "artifact",
|
||||
"size": file_path.stat().st_size,
|
||||
}
|
||||
result = {"title": "demo", "content": "ignored", "format": "markdown", "files": [file_entry]}
|
||||
|
||||
scheduled_for = datetime.now(UTC)
|
||||
await store.create_run_once(
|
||||
{
|
||||
"id": run_id,
|
||||
"task_id": task_id,
|
||||
"user_id": user_id,
|
||||
"scheduler_thread_id": f"thread-{user_id}",
|
||||
"scheduled_for": scheduled_for,
|
||||
"status": "succeeded",
|
||||
"started_at": scheduled_for,
|
||||
}
|
||||
)
|
||||
await store.update_run(run_id, {"result": result, "finished_at": scheduled_for})
|
||||
return file_entry
|
||||
|
||||
|
||||
# ---------- can_edit flag on get_run ----------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_run_marks_owner_as_can_edit(store, isolated_paths):
|
||||
_seed_task(store, task_id="t1", user_id="alice", published=True)
|
||||
await _seed_run_with_file(store, isolated_paths, run_id="r1", task_id="t1", user_id="alice", file_name="a.md", file_text="# hi")
|
||||
service = _build_service(store)
|
||||
|
||||
run = await service.get_run("alice", "r1")
|
||||
assert run is not None
|
||||
assert run["can_edit"] is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_run_marks_subscriber_as_read_only(store, isolated_paths):
|
||||
_seed_task(store, task_id="t1", user_id="alice", published=True)
|
||||
await _seed_run_with_file(store, isolated_paths, run_id="r1", task_id="t1", user_id="alice", file_name="a.md", file_text="# hi")
|
||||
service = _build_service(store)
|
||||
await service.subscribe_task("bob", "t1", notify_element=False)
|
||||
|
||||
run = await service.get_run("bob", "r1")
|
||||
assert run is not None
|
||||
assert run["can_edit"] is False
|
||||
|
||||
|
||||
# ---------- update_run_file ----------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_owner_can_overwrite_md_file(store, isolated_paths):
|
||||
_seed_task(store, task_id="t1", user_id="alice")
|
||||
file_entry = await _seed_run_with_file(
|
||||
store,
|
||||
isolated_paths,
|
||||
run_id="r1",
|
||||
task_id="t1",
|
||||
user_id="alice",
|
||||
file_name="report.md",
|
||||
file_text="# original",
|
||||
)
|
||||
service = _build_service(store)
|
||||
|
||||
new_text = "# updated\n\nA new paragraph."
|
||||
meta = await service.update_run_file("alice", "r1", 0, new_text)
|
||||
|
||||
assert meta["name"] == "report.md"
|
||||
assert meta["size"] == len(new_text.encode("utf-8"))
|
||||
|
||||
# The on-disk file is overwritten.
|
||||
assert Path(file_entry["path"]).read_text(encoding="utf-8") == new_text
|
||||
|
||||
# The size in the run row is refreshed.
|
||||
run = await service.get_run("alice", "r1")
|
||||
assert run is not None
|
||||
assert run["result"]["files"][0]["size"] == meta["size"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_subscriber_cannot_edit(store, isolated_paths):
|
||||
_seed_task(store, task_id="t1", user_id="alice", published=True)
|
||||
await _seed_run_with_file(store, isolated_paths, run_id="r1", task_id="t1", user_id="alice", file_name="a.md", file_text="# hi")
|
||||
service = _build_service(store)
|
||||
await service.subscribe_task("bob", "t1", notify_element=False)
|
||||
|
||||
with pytest.raises(PermissionError):
|
||||
await service.update_run_file("bob", "r1", 0, "# evil")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_unrelated_user_gets_not_found(store, isolated_paths):
|
||||
_seed_task(store, task_id="t1", user_id="alice")
|
||||
await _seed_run_with_file(store, isolated_paths, run_id="r1", task_id="t1", user_id="alice", file_name="a.md", file_text="# hi")
|
||||
service = _build_service(store)
|
||||
|
||||
with pytest.raises(PermissionError):
|
||||
await service.update_run_file("eve", "r1", 0, "# evil")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_missing_run_returns_filenotfound(store, isolated_paths):
|
||||
service = _build_service(store)
|
||||
with pytest.raises(FileNotFoundError):
|
||||
await service.update_run_file("alice", "missing-run", 0, "# x")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_invalid_file_index_rejected(store, isolated_paths):
|
||||
_seed_task(store, task_id="t1", user_id="alice")
|
||||
await _seed_run_with_file(store, isolated_paths, run_id="r1", task_id="t1", user_id="alice", file_name="a.md", file_text="# hi")
|
||||
service = _build_service(store)
|
||||
|
||||
with pytest.raises(FileNotFoundError):
|
||||
await service.update_run_file("alice", "r1", 5, "# x")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_non_text_file_rejected(store, isolated_paths):
|
||||
_seed_task(store, task_id="t1", user_id="alice")
|
||||
await _seed_run_with_file(
|
||||
store,
|
||||
isolated_paths,
|
||||
run_id="r1",
|
||||
task_id="t1",
|
||||
user_id="alice",
|
||||
file_name="diagram.png",
|
||||
file_text="not really a png but stored as such",
|
||||
mime_type="image/png",
|
||||
)
|
||||
service = _build_service(store)
|
||||
|
||||
with pytest.raises(ValueError, match="text-previewable"):
|
||||
await service.update_run_file("alice", "r1", 0, "# x")
|
||||
@@ -0,0 +1,241 @@
|
||||
"""Tests for skill upload validation + install via the storage layer."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from deerflow.skills.installer import SkillAlreadyExistsError
|
||||
from deerflow.skills.storage.local_skill_storage import LocalSkillStorage
|
||||
|
||||
|
||||
def _make_skill_zip(
|
||||
*,
|
||||
skill_name: str = "demo-skill",
|
||||
description: str = "Use this skill when demoing things.",
|
||||
license_value: str | None = "MIT",
|
||||
allowed_tools: list[str] | None = None,
|
||||
extra_files: dict[str, str] | None = None,
|
||||
include_skill_md: bool = True,
|
||||
bad_yaml: bool = False,
|
||||
nest_under_root: bool = True,
|
||||
) -> bytes:
|
||||
"""Build an in-memory .skill ZIP archive for tests."""
|
||||
buf = io.BytesIO()
|
||||
root = f"{skill_name}/" if nest_under_root else ""
|
||||
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:
|
||||
if include_skill_md:
|
||||
if bad_yaml:
|
||||
content = "---\nname: ???: bad\n---\nhi"
|
||||
else:
|
||||
lines = ["---", f"name: {skill_name}", f"description: {description}"]
|
||||
if license_value:
|
||||
lines.append(f"license: {license_value}")
|
||||
if allowed_tools:
|
||||
lines.append("allowed-tools:")
|
||||
for tool in allowed_tools:
|
||||
lines.append(f" - {tool}")
|
||||
lines.extend(["---", "# " + skill_name, "Hello world."])
|
||||
content = "\n".join(lines)
|
||||
zf.writestr(f"{root}SKILL.md", content)
|
||||
for rel, body in (extra_files or {}).items():
|
||||
zf.writestr(f"{root}{rel}", body)
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def storage(tmp_path: Path) -> LocalSkillStorage:
|
||||
skills_root = tmp_path / "skills"
|
||||
(skills_root / "public").mkdir(parents=True)
|
||||
(skills_root / "custom").mkdir(parents=True)
|
||||
return LocalSkillStorage(host_path=str(skills_root))
|
||||
|
||||
|
||||
def _write_archive(tmp_path: Path, data: bytes, *, suffix: str = ".skill") -> Path:
|
||||
archive = tmp_path / f"upload{suffix}"
|
||||
archive.write_bytes(data)
|
||||
return archive
|
||||
|
||||
|
||||
# ---------- validate_skill_archive ----------
|
||||
|
||||
|
||||
def test_validate_returns_metadata_and_no_conflict(storage: LocalSkillStorage, tmp_path: Path):
|
||||
archive = _write_archive(
|
||||
tmp_path,
|
||||
_make_skill_zip(allowed_tools=["bash", "read_file"]),
|
||||
)
|
||||
|
||||
result = storage.validate_skill_archive(archive)
|
||||
|
||||
assert result["ok"] is True
|
||||
assert result["skill_name"] == "demo-skill"
|
||||
assert result["description"] == "Use this skill when demoing things."
|
||||
assert result["license"] == "MIT"
|
||||
assert result["allowed_tools"] == ["bash", "read_file"]
|
||||
assert result["conflict"] is False
|
||||
assert result["conflict_kind"] is None
|
||||
assert result["warnings"] == []
|
||||
|
||||
|
||||
def test_validate_flags_script_files_as_warning(storage: LocalSkillStorage, tmp_path: Path):
|
||||
archive = _write_archive(
|
||||
tmp_path,
|
||||
_make_skill_zip(extra_files={"scripts/hi.py": "print('hi')\n"}),
|
||||
)
|
||||
|
||||
result = storage.validate_skill_archive(archive)
|
||||
|
||||
assert result["ok"] is True
|
||||
assert result["warnings"] and "script" in result["warnings"][0].lower()
|
||||
|
||||
|
||||
def test_validate_detects_custom_conflict(storage: LocalSkillStorage, tmp_path: Path):
|
||||
# Pre-install one with the same name into custom/
|
||||
target = storage.get_skills_root_path() / "custom" / "demo-skill"
|
||||
target.mkdir(parents=True)
|
||||
(target / "SKILL.md").write_text(
|
||||
"---\nname: demo-skill\ndescription: existing\n---\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
archive = _write_archive(tmp_path, _make_skill_zip())
|
||||
result = storage.validate_skill_archive(archive)
|
||||
|
||||
assert result["conflict"] is True
|
||||
assert result["conflict_kind"] == "custom"
|
||||
|
||||
|
||||
def test_validate_detects_public_conflict(storage: LocalSkillStorage, tmp_path: Path):
|
||||
target = storage.get_skills_root_path() / "public" / "demo-skill"
|
||||
target.mkdir(parents=True)
|
||||
(target / "SKILL.md").write_text(
|
||||
"---\nname: demo-skill\ndescription: existing\n---\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
archive = _write_archive(tmp_path, _make_skill_zip())
|
||||
result = storage.validate_skill_archive(archive)
|
||||
|
||||
assert result["conflict"] is True
|
||||
assert result["conflict_kind"] == "public"
|
||||
|
||||
|
||||
def test_validate_rejects_bad_zip(storage: LocalSkillStorage, tmp_path: Path):
|
||||
archive = _write_archive(tmp_path, b"not a zip at all")
|
||||
with pytest.raises(ValueError, match="not a valid ZIP"):
|
||||
storage.validate_skill_archive(archive)
|
||||
|
||||
|
||||
def test_validate_rejects_missing_skill_md(storage: LocalSkillStorage, tmp_path: Path):
|
||||
archive = _write_archive(tmp_path, _make_skill_zip(include_skill_md=False))
|
||||
with pytest.raises(ValueError, match="SKILL.md"):
|
||||
storage.validate_skill_archive(archive)
|
||||
|
||||
|
||||
def test_validate_rejects_bad_frontmatter(storage: LocalSkillStorage, tmp_path: Path):
|
||||
archive = _write_archive(tmp_path, _make_skill_zip(bad_yaml=True))
|
||||
with pytest.raises(ValueError):
|
||||
storage.validate_skill_archive(archive)
|
||||
|
||||
|
||||
# ---------- ainstall_skill_from_archive (overwrite) ----------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_install_writes_to_custom_dir(
|
||||
storage: LocalSkillStorage, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
):
|
||||
# Bypass the LLM security scan so the test stays hermetic.
|
||||
async def _noop_scan(*args, **kwargs): # noqa: ANN001, ARG001
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(
|
||||
"deerflow.skills.installer._scan_skill_archive_contents_or_raise",
|
||||
_noop_scan,
|
||||
)
|
||||
|
||||
archive = _write_archive(tmp_path, _make_skill_zip())
|
||||
result = await storage.ainstall_skill_from_archive(archive, require_skill_suffix=False)
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["skill_name"] == "demo-skill"
|
||||
target = storage.get_skills_root_path() / "custom" / "demo-skill" / "SKILL.md"
|
||||
assert target.exists()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_install_conflict_without_overwrite(
|
||||
storage: LocalSkillStorage, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
):
|
||||
async def _noop_scan(*args, **kwargs): # noqa: ANN001, ARG001
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(
|
||||
"deerflow.skills.installer._scan_skill_archive_contents_or_raise",
|
||||
_noop_scan,
|
||||
)
|
||||
|
||||
archive = _write_archive(tmp_path, _make_skill_zip())
|
||||
await storage.ainstall_skill_from_archive(archive, require_skill_suffix=False)
|
||||
|
||||
with pytest.raises(SkillAlreadyExistsError):
|
||||
await storage.ainstall_skill_from_archive(archive, require_skill_suffix=False)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_install_overwrite_replaces_existing(
|
||||
storage: LocalSkillStorage, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
):
|
||||
async def _noop_scan(*args, **kwargs): # noqa: ANN001, ARG001
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(
|
||||
"deerflow.skills.installer._scan_skill_archive_contents_or_raise",
|
||||
_noop_scan,
|
||||
)
|
||||
|
||||
(tmp_path / "v1").mkdir(exist_ok=True)
|
||||
(tmp_path / "v2").mkdir(exist_ok=True)
|
||||
archive_v1 = _write_archive(tmp_path / "v1", _make_skill_zip(description="version one"))
|
||||
archive_v2 = _write_archive(tmp_path / "v2", _make_skill_zip(description="version two"))
|
||||
|
||||
await storage.ainstall_skill_from_archive(archive_v1, require_skill_suffix=False)
|
||||
await storage.ainstall_skill_from_archive(
|
||||
archive_v2, overwrite=True, require_skill_suffix=False
|
||||
)
|
||||
|
||||
skill_md = (
|
||||
storage.get_skills_root_path() / "custom" / "demo-skill" / "SKILL.md"
|
||||
).read_text(encoding="utf-8")
|
||||
assert "version two" in skill_md
|
||||
assert "version one" not in skill_md
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_install_refuses_to_overwrite_public_skill(
|
||||
storage: LocalSkillStorage, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
):
|
||||
async def _noop_scan(*args, **kwargs): # noqa: ANN001, ARG001
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(
|
||||
"deerflow.skills.installer._scan_skill_archive_contents_or_raise",
|
||||
_noop_scan,
|
||||
)
|
||||
|
||||
public_dir = storage.get_skills_root_path() / "public" / "demo-skill"
|
||||
public_dir.mkdir(parents=True)
|
||||
(public_dir / "SKILL.md").write_text(
|
||||
"---\nname: demo-skill\ndescription: built-in\n---\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
archive = _write_archive(tmp_path, _make_skill_zip())
|
||||
with pytest.raises(SkillAlreadyExistsError, match="public"):
|
||||
await storage.ainstall_skill_from_archive(
|
||||
archive, overwrite=True, require_skill_suffix=False
|
||||
)
|
||||
Reference in new issue
Block a user