sync update from LAN repo (source dhk-1 @ c21a2a4a)

This commit is contained in:
gozeros committed 2026-11-10 14:04:00 +08:00
1 parent 264e2200d6
commit a1fa13a238
122 files changed
+8881 -623

No files matched your search

@@ -186,6 +186,15 @@ def current_user_id(request: Request) -> str:
return get_effective_user_id()
def _lease_seconds(settings: Any) -> int:
minutes = getattr(settings, "lease_minutes", 5)
try:
minutes = int(minutes)
except (TypeError, ValueError):
minutes = 5
return max(1, min(60, minutes)) * 60
def _store(request: Request):
return getattr(request.app.state, "roundtable_room_store", None)
@@ -232,6 +241,7 @@ async def admit_roundtable_room(
enabled=settings.enabled,
max_rooms=settings.max_rooms,
max_hold_minutes=getattr(settings, "max_hold_minutes", 30),
lease_seconds=_lease_seconds(settings),
extra_keys=extra_keys,
)
@@ -265,6 +275,7 @@ async def renew_roundtable_room(
enabled=settings.enabled,
max_rooms=settings.max_rooms,
max_hold_minutes=getattr(settings, "max_hold_minutes", 30),
lease_seconds=_lease_seconds(settings),
extra_keys=extra_keys,
)
@@ -425,6 +436,7 @@ async def enforce_roundtable_run(
enabled=settings.enabled,
max_rooms=settings.max_rooms,
max_hold_minutes=getattr(settings, "max_hold_minutes", 30),
lease_seconds=_lease_seconds(settings),
extra_keys=await backend_occupancy_keys(request),
)
if not decision.get("ok", False):
@@ -30,7 +30,7 @@ from fastapi import APIRouter, Depends, HTTPException, Query, Request
from fastapi.responses import StreamingResponse
from langchain_core.messages import HumanMessage, SystemMessage
from pydantic import BaseModel, Field
from sqlalchemy import and_, case, desc, func, not_, or_, select, text
from sqlalchemy import and_, case, desc, func, literal, not_, or_, select, text
from app.gateway.admin_query_window import (
ADMIN_QUERY_WINDOW_DAYS,
@@ -279,6 +279,41 @@ class AdminLeaderboardDimension(BaseModel):
last_seen_at: str = ""
class AdminLeaderboardDetailItem(BaseModel):
"""One row in the expanded skill, tool, or agent analytics drawer."""
name: str
count: int = 0
user_count: int = 0
total_tokens: int = 0
avg_duration_ms: int = 0
class AdminLeaderboardDetailSummary(BaseModel):
count: int = 0
item_count: int = 0
user_count: int = 0
total_tokens: int = 0
avg_duration_ms: int = 0
class AdminLeaderboardDetailResponse(BaseModel):
"""Fresh, drill-down analytics for a leaderboard dimension.
Unlike the dashboard's compact daily snapshots, this response is queried
directly. It lets an administrator inspect the entire matching dimension
(up to the explicit limit) and does not lose long-tail rows because they
were outside a daily top-N snapshot.
"""
kind: str
range_days: int
since: str
until: str
summary: AdminLeaderboardDetailSummary
items: list[AdminLeaderboardDetailItem] = Field(default_factory=list)
class AdminLeaderboardTrendPoint(BaseModel):
date: str
runs: int = 0
@@ -961,6 +996,125 @@ async def _query_run_dimensions(
]
async def _query_leaderboard_dimension_details(
session,
since: datetime,
until: datetime,
*,
kind: str,
limit: int,
exclude_user_ids: set[str],
include_scheduled: bool,
include_admins: bool,
include_failed: bool,
) -> tuple[
AdminLeaderboardDetailSummary,
list[AdminLeaderboardDetailItem],
]:
"""Load complete, fresh detail statistics for one leaderboard dimension.
The main leaderboard deliberately reads compact daily snapshots so it can
remain quick under load. The drawer is an intentional admin drill-down,
therefore it runs a bounded aggregate against the source metric tables and
returns exact unique-user counts for the selected interval.
"""
if kind in {"skill", "tool"}:
name_col = ToolCallMetricRow.skill_name if kind == "skill" else ToolCallMetricRow.tool_name
base_filters = [
_tool_time_filter(since, until),
name_col.is_not(None),
name_col != "",
]
def apply_scope(stmt):
return _apply_tool_scope(
stmt,
exclude_user_ids=exclude_user_ids,
include_admins=include_admins,
include_scheduled=include_scheduled,
)
count_col = func.count(ToolCallMetricRow.id)
user_count_col = func.count(func.distinct(ToolCallMetricRow.user_id))
total_tokens_col = literal(0)
avg_duration_col = func.coalesce(func.avg(ToolCallMetricRow.duration_ms), 0)
elif kind == "agent":
name_col = RunRow.assistant_id
base_filters = [
_run_time_filter(since, until),
name_col.is_not(None),
name_col != "",
]
def apply_scope(stmt):
return _apply_run_scope(
stmt,
user_column=RunRow.user_id,
run_id_column=RunRow.run_id,
exclude_user_ids=exclude_user_ids,
include_admins=include_admins,
include_failed=include_failed,
include_scheduled=include_scheduled,
)
count_col = func.count(RunRow.run_id)
user_count_col = func.count(func.distinct(RunRow.user_id))
total_tokens_col = func.coalesce(func.sum(RunRow.total_tokens), 0)
# Run rows have no reliable wall-clock duration. Keep this explicit at
# zero instead of inferring elapsed time from an asynchronously updated
# journal timestamp.
avg_duration_col = literal(0)
else:
raise ValueError(f"Unknown leaderboard detail kind: {kind}")
item_stmt = (
select(
name_col.label("name"),
count_col.label("count"),
user_count_col.label("user_count"),
total_tokens_col.label("total_tokens"),
avg_duration_col.label("avg_duration_ms"),
)
.where(*base_filters)
.group_by(name_col)
.order_by(count_col.desc(), name_col.asc())
.limit(limit)
)
item_stmt = apply_scope(item_stmt)
summary_stmt = select(
count_col.label("count"),
func.count(func.distinct(name_col)).label("item_count"),
user_count_col.label("user_count"),
total_tokens_col.label("total_tokens"),
avg_duration_col.label("avg_duration_ms"),
).where(*base_filters)
summary_stmt = apply_scope(summary_stmt)
item_rows = (await _execute_leaderboard_query(session, f"{kind}_detail.items", item_stmt)).all()
summary_row = (await _execute_leaderboard_query(session, f"{kind}_detail.summary", summary_stmt)).one()
summary = AdminLeaderboardDetailSummary(
count=int(summary_row[0] or 0),
item_count=int(summary_row[1] or 0),
user_count=int(summary_row[2] or 0),
total_tokens=int(summary_row[3] or 0),
avg_duration_ms=int(summary_row[4] or 0),
)
items = [
AdminLeaderboardDetailItem(
name=row[0] or "",
count=int(row[1] or 0),
user_count=int(row[2] or 0),
total_tokens=int(row[3] or 0),
avg_duration_ms=int(row[4] or 0),
)
for row in item_rows
]
return summary, items
async def _query_model_rankings(
session,
since: datetime,
@@ -1876,6 +2030,59 @@ async def get_user_leaderboard(
)
@router.get("/leaderboard/details", response_model=AdminLeaderboardDetailResponse)
async def get_user_leaderboard_details(
request: Request,
kind: str = Query(description="One of: skill, tool, agent", pattern="^(skill|tool|agent)$"),
days: int = Query(default=30, ge=1, le=ADMIN_QUERY_WINDOW_DAYS),
since: str | None = Query(default=None, description="Inclusive range start, ISO datetime"),
until: str | None = Query(default=None, description="Inclusive range end, ISO datetime"),
limit: int = Query(default=500, ge=1, le=1000),
) -> AdminLeaderboardDetailResponse:
"""Return fresh drill-down statistics for one leaderboard card.
This endpoint intentionally reads the source metric tables instead of the
asynchronous daily leaderboard snapshots. A drawer represents an explicit
admin investigation, where exact unique-user counts, all matching entries,
and complete detail rows are more useful than a compact cache.
"""
await _require_admin(request)
session_factory = get_session_factory()
if session_factory is None:
raise HTTPException(status_code=503, detail="Analytics requires database persistence")
settings = load_system_settings().leaderboard
since_dt, until_dt, range_days = resolve_admin_query_window(
days=days,
since=since,
until=until,
)
excluded, include_scheduled, include_admins, include_failed, _settings_hash = _resolve_leaderboard_snapshot_scope(settings)
async with session_factory() as session:
await _set_analytics_read_timeout(session)
summary, items = await _query_leaderboard_dimension_details(
session,
since_dt,
until_dt,
kind=kind,
limit=limit,
exclude_user_ids=excluded,
include_scheduled=include_scheduled,
include_admins=include_admins,
include_failed=include_failed,
)
return AdminLeaderboardDetailResponse(
kind=kind,
range_days=range_days,
since=_iso(since_dt),
until=_iso(until_dt),
summary=summary,
items=items,
)
@router.get("/leaderboard/snapshots", response_model=AdminLeaderboardSnapshotStatusResponse)
async def get_leaderboard_snapshot_status(
request: Request,
@@ -1,14 +1,16 @@
"""Application chrome name and logo.
"""Application chrome name, logo, and optional red-theme artwork.
Stored separately from ``generated_page_branding`` and the
``generated-page-branding/`` directory. An empty name or a missing logo leaves
the built-in title and static theme logos in place.
``generated-page-branding/`` directory. Missing custom images leave the
built-in theme artwork in place.
"""
from __future__ import annotations
import asyncio
from typing import Annotated
from uuid import uuid4
from xml.etree import ElementTree
from fastapi import APIRouter, File, Form, HTTPException, Request, UploadFile
from fastapi.responses import FileResponse
@@ -18,6 +20,8 @@ from app.gateway.routers.system_settings import _require_admin
from deerflow.config.runtime_paths import runtime_home
from deerflow.config.system_settings import (
AppIdentitySettings,
app_identity_light_red_header_background_path,
app_identity_light_red_nav_active_path,
app_identity_logo_path,
load_system_settings,
save_system_settings,
@@ -26,25 +30,48 @@ from deerflow.config.system_settings import (
router = APIRouter(prefix="/api/system-settings", tags=["system-settings"])
public_router = APIRouter(prefix="/api/public/app-identity", tags=["app-identity"])
_MAX_LOGO_BYTES = 10 * 1024 * 1024
_MAX_IMAGE_BYTES = 10 * 1024 * 1024
_PUBLIC_LOGO_PATH = "/api/public/app-identity/logo"
_PUBLIC_LIGHT_RED_HEADER_PATH = "/api/public/app-identity/light-red-header-background"
_PUBLIC_LIGHT_RED_ACTIVE_PATH = "/api/public/app-identity/light-red-nav-active"
class AppIdentityResponse(BaseModel):
system_name: str = ""
header_title_font_mode: str = "adaptive"
header_title_font_size: int = 30
logo_url: str | None = None
logo_version: str | None = None
has_logo: bool = False
light_red_header_background_url: str | None = None
light_red_header_background_version: str | None = None
has_light_red_header_background: bool = False
light_red_nav_active_url: str | None = None
light_red_nav_active_version: str | None = None
has_light_red_nav_active: bool = False
def _detect_logo(data: bytes) -> tuple[str, str]:
def _detect_image(
data: bytes,
label: str,
*,
allow_svg: bool = False,
) -> tuple[str, str]:
if data.startswith(b"\x89PNG\r\n\x1a\n"):
return ".png", "image/png"
if data.startswith(b"\xff\xd8\xff"):
return ".jpg", "image/jpeg"
if len(data) >= 12 and data[:4] == b"RIFF" and data[8:12] == b"WEBP":
return ".webp", "image/webp"
raise HTTPException(status_code=422, detail="Logo 仅支持 PNG、JPG 或 WebP 图片")
if allow_svg:
try:
root = ElementTree.fromstring(data)
except ElementTree.ParseError:
root = None
if root is not None and root.tag.rsplit("}", 1)[-1].lower() == "svg":
return ".svg", "image/svg+xml"
supported = "PNG、JPG、WebP 或 SVG" if allow_svg else "PNG、JPG 或 WebP"
raise HTTPException(status_code=422, detail=f"{label}仅支持 {supported} 图片")
def _normalize_system_name(value: str | None) -> str:
@@ -54,21 +81,61 @@ def _normalize_system_name(value: str | None) -> str:
return normalized
def _normalize_header_title_font_mode(value: str) -> str:
normalized = str(value).strip().lower()
if normalized not in {"adaptive", "fixed"}:
raise HTTPException(status_code=422, detail="系统名称字号模式仅支持自适应或固定大小")
return normalized
def _normalize_header_title_font_size(value: int) -> int:
normalized = int(value)
if normalized < 16 or normalized > 40:
raise HTTPException(status_code=422, detail="系统名称固定字号必须在 16px 到 40px 之间")
return normalized
def _response(settings: AppIdentitySettings) -> AppIdentityResponse:
path = app_identity_logo_path(settings)
version = settings.logo_version if path is not None else None
header_background_path = app_identity_light_red_header_background_path(settings)
header_background_version = (
settings.light_red_header_background_version
if header_background_path is not None
else None
)
nav_active_path = app_identity_light_red_nav_active_path(settings)
nav_active_version = (
settings.light_red_nav_active_version if nav_active_path is not None else None
)
return AppIdentityResponse(
system_name=settings.system_name,
header_title_font_mode=settings.header_title_font_mode,
header_title_font_size=settings.header_title_font_size,
logo_url=f"{_PUBLIC_LOGO_PATH}?v={version}" if version else None,
logo_version=version,
has_logo=path is not None,
light_red_header_background_url=(
f"{_PUBLIC_LIGHT_RED_HEADER_PATH}?v={header_background_version}"
if header_background_version
else None
),
light_red_header_background_version=header_background_version,
has_light_red_header_background=header_background_path is not None,
light_red_nav_active_url=(
f"{_PUBLIC_LIGHT_RED_ACTIVE_PATH}?v={nav_active_version}"
if nav_active_version
else None
),
light_red_nav_active_version=nav_active_version,
has_light_red_nav_active=nav_active_path is not None,
)
def _write_logo(data: bytes, suffix: str) -> str:
def _write_asset(data: bytes, suffix: str, prefix: str) -> str:
directory = runtime_home() / "app-identity"
directory.mkdir(parents=True, exist_ok=True)
filename = f"logo-{uuid4().hex}{suffix}"
filename = f"{prefix}-{uuid4().hex}{suffix}"
destination = directory / filename
temporary = directory / f".{filename}.tmp"
try:
@@ -79,9 +146,40 @@ def _write_logo(data: bytes, suffix: str) -> str:
return filename
async def _read_uploaded_image(
upload: UploadFile,
label: str,
*,
allow_svg: bool = False,
max_bytes: int | None = _MAX_IMAGE_BYTES,
) -> tuple[bytes, str, str]:
data = await upload.read(max_bytes + 1) if max_bytes is not None else await upload.read()
await upload.close()
if not data:
raise HTTPException(status_code=422, detail=f"{label}文件不能为空")
if max_bytes is not None and len(data) > max_bytes:
raise HTTPException(status_code=413, detail=f"{label}不能超过 10MB")
suffix, media_type = _detect_image(data, label, allow_svg=allow_svg)
return data, suffix, media_type
def _public_asset_headers(media_type: str | None) -> dict[str, str]:
headers = {"Cache-Control": "public, max-age=31536000, immutable"}
if media_type == "image/svg+xml":
headers.update(
{
"Content-Security-Policy": (
"sandbox; default-src 'none'; style-src 'unsafe-inline'; img-src data:"
),
"X-Content-Type-Options": "nosniff",
}
)
return headers
@router.get("/app-identity", response_model=AppIdentityResponse)
async def get_app_identity_settings() -> AppIdentityResponse:
"""Return the application name and uploaded logo. Any logged-in user may read it."""
"""Return the application identity settings readable by all logged-in users."""
return _response(load_system_settings().app_identity)
@@ -89,68 +187,160 @@ async def get_app_identity_settings() -> AppIdentityResponse:
@router.put("/app-identity", response_model=AppIdentityResponse)
async def update_app_identity_settings(
request: Request,
system_name: str = Form(default=""),
remove_logo: bool = Form(default=False),
logo: UploadFile | None = File(default=None),
system_name: Annotated[str, Form()] = "",
remove_logo: Annotated[bool, Form()] = False,
logo: Annotated[UploadFile | None, File()] = None,
header_title_font_mode: Annotated[str | None, Form()] = None,
header_title_font_size: Annotated[int | None, Form()] = None,
light_red_header_background: Annotated[UploadFile | None, File()] = None,
remove_light_red_header_background: Annotated[bool, Form()] = False,
light_red_nav_active: Annotated[UploadFile | None, File()] = None,
remove_light_red_nav_active: Annotated[bool, Form()] = False,
) -> AppIdentityResponse:
"""Save the application chrome name and optional logo. Admin only."""
"""Save the application chrome identity and optional red-theme artwork."""
await _require_admin(request)
if logo is not None and remove_logo:
raise HTTPException(status_code=422, detail="上传 Logo 时不能同时恢复静态 Logo")
if light_red_header_background is not None and remove_light_red_header_background:
raise HTTPException(status_code=422, detail="上传导航栏背景图时不能同时恢复默认图片")
if light_red_nav_active is not None and remove_light_red_nav_active:
raise HTTPException(status_code=422, detail="上传菜单选中样式图时不能同时恢复默认图片")
current = load_system_settings()
previous_logo = app_identity_logo_path(current.app_identity)
previous_header_background = app_identity_light_red_header_background_path(
current.app_identity
)
previous_nav_active = app_identity_light_red_nav_active_path(current.app_identity)
update: dict[str, object] = {"system_name": _normalize_system_name(system_name)}
new_logo_path = None
if logo is not None:
data = await logo.read(_MAX_LOGO_BYTES + 1)
await logo.close()
if not data:
raise HTTPException(status_code=422, detail="Logo 文件不能为空")
if len(data) > _MAX_LOGO_BYTES:
raise HTTPException(status_code=413, detail="Logo 文件不能超过 10MB")
suffix, media_type = _detect_logo(data)
filename = await asyncio.to_thread(_write_logo, data, suffix)
new_logo_path = runtime_home() / "app-identity" / filename
update.update(
{
"logo_filename": filename,
"logo_media_type": media_type,
"logo_version": uuid4().hex,
}
if header_title_font_mode is not None:
update["header_title_font_mode"] = _normalize_header_title_font_mode(header_title_font_mode)
if header_title_font_size is not None:
update["header_title_font_size"] = _normalize_header_title_font_size(header_title_font_size)
logo_asset = (
await _read_uploaded_image(logo, "Logo") if logo is not None else None
)
header_background_asset = (
await _read_uploaded_image(
light_red_header_background,
"导航栏背景图",
allow_svg=True,
max_bytes=None,
)
elif remove_logo:
update.update(
{
"logo_filename": None,
"logo_media_type": None,
"logo_version": None,
}
if light_red_header_background is not None
else None
)
nav_active_asset = (
await _read_uploaded_image(
light_red_nav_active,
"菜单选中样式图",
allow_svg=True,
max_bytes=None,
)
if light_red_nav_active is not None
else None
)
updated = current.app_identity.model_copy(update=update)
staged_paths = []
try:
if logo_asset is not None:
data, suffix, media_type = logo_asset
filename = await asyncio.to_thread(_write_asset, data, suffix, "logo")
staged_paths.append(runtime_home() / "app-identity" / filename)
update.update(
{
"logo_filename": filename,
"logo_media_type": media_type,
"logo_version": uuid4().hex,
}
)
elif remove_logo:
update.update(
{
"logo_filename": None,
"logo_media_type": None,
"logo_version": None,
}
)
if header_background_asset is not None:
data, suffix, media_type = header_background_asset
filename = await asyncio.to_thread(
_write_asset, data, suffix, "light-red-header"
)
staged_paths.append(runtime_home() / "app-identity" / filename)
update.update(
{
"light_red_header_background_filename": filename,
"light_red_header_background_media_type": media_type,
"light_red_header_background_version": uuid4().hex,
}
)
elif remove_light_red_header_background:
update.update(
{
"light_red_header_background_filename": None,
"light_red_header_background_media_type": None,
"light_red_header_background_version": None,
}
)
if nav_active_asset is not None:
data, suffix, media_type = nav_active_asset
filename = await asyncio.to_thread(
_write_asset, data, suffix, "light-red-active"
)
staged_paths.append(runtime_home() / "app-identity" / filename)
update.update(
{
"light_red_nav_active_filename": filename,
"light_red_nav_active_media_type": media_type,
"light_red_nav_active_version": uuid4().hex,
}
)
elif remove_light_red_nav_active:
update.update(
{
"light_red_nav_active_filename": None,
"light_red_nav_active_media_type": None,
"light_red_nav_active_version": None,
}
)
updated = current.app_identity.model_copy(update=update)
saved = save_system_settings(current.model_copy(update={"app_identity": updated}))
except Exception:
if new_logo_path is not None:
await asyncio.to_thread(new_logo_path.unlink, missing_ok=True)
for staged_path in staged_paths:
await asyncio.to_thread(staged_path.unlink, missing_ok=True)
raise
if previous_logo is not None and previous_logo != app_identity_logo_path(saved.app_identity):
await asyncio.to_thread(previous_logo.unlink, missing_ok=True)
previous_paths = (previous_logo, previous_header_background, previous_nav_active)
saved_paths = {
app_identity_logo_path(saved.app_identity),
app_identity_light_red_header_background_path(saved.app_identity),
app_identity_light_red_nav_active_path(saved.app_identity),
}
for previous_path in previous_paths:
if previous_path is not None and previous_path not in saved_paths:
await asyncio.to_thread(previous_path.unlink, missing_ok=True)
return _response(saved.app_identity)
@router.delete("/app-identity", response_model=AppIdentityResponse)
async def delete_app_identity_settings(request: Request) -> AppIdentityResponse:
"""Clear the application name and uploaded logo. Admin only."""
"""Clear the application identity and uploaded theme artwork. Admin only."""
await _require_admin(request)
current = load_system_settings()
previous_logo = app_identity_logo_path(current.app_identity)
previous_paths = (
app_identity_logo_path(current.app_identity),
app_identity_light_red_header_background_path(current.app_identity),
app_identity_light_red_nav_active_path(current.app_identity),
)
saved = save_system_settings(current.model_copy(update={"app_identity": AppIdentitySettings()}))
if previous_logo is not None:
await asyncio.to_thread(previous_logo.unlink, missing_ok=True)
for previous_path in previous_paths:
if previous_path is not None:
await asyncio.to_thread(previous_path.unlink, missing_ok=True)
return _response(saved.app_identity)
@@ -166,5 +356,35 @@ async def get_app_identity_logo() -> FileResponse:
path,
media_type=settings.logo_media_type or "image/png",
content_disposition_type="inline",
headers={"Cache-Control": "public, max-age=31536000, immutable"},
headers=_public_asset_headers(settings.logo_media_type),
)
@public_router.get("/light-red-header-background")
async def get_light_red_header_background() -> FileResponse:
settings = load_system_settings().app_identity
path = app_identity_light_red_header_background_path(settings)
if path is None:
raise HTTPException(status_code=404, detail="未上传红色导航栏背景图")
return FileResponse(
path,
media_type=settings.light_red_header_background_media_type or "image/png",
content_disposition_type="inline",
headers=_public_asset_headers(
settings.light_red_header_background_media_type
),
)
@public_router.get("/light-red-nav-active")
async def get_light_red_nav_active() -> FileResponse:
settings = load_system_settings().app_identity
path = app_identity_light_red_nav_active_path(settings)
if path is None:
raise HTTPException(status_code=404, detail="未上传红色菜单选中样式图")
return FileResponse(
path,
media_type=settings.light_red_nav_active_media_type or "image/png",
content_disposition_type="inline",
headers=_public_asset_headers(settings.light_red_nav_active_media_type),
)
@@ -1,7 +1,9 @@
import asyncio
import logging
import mimetypes
import os
import re
import tempfile
import zipfile
from datetime import UTC, datetime
from pathlib import Path
@@ -11,12 +13,16 @@ from urllib.parse import quote
from fastapi import APIRouter, HTTPException, Query, Request
from fastapi.responses import FileResponse, PlainTextResponse, Response
from pydantic import BaseModel, Field
from starlette.background import BackgroundTask
from app.gateway.authz import require_permission
from app.gateway.operation_timing import OperationTiming
from app.gateway.path_utils import aresolve_thread_virtual_path
from app.gateway.utils import sanitize_log_param
from deerflow.config.paths import get_paths
from deerflow.config.system_settings import generated_page_logo_path, load_system_settings
from deerflow.html_runtime.addresses import html_map_tile_url, html_runtime_base_url
from deerflow.html_runtime.offline_bundle import build_html_offline_bundle
from deerflow.runtime.thread_paths import aresolve_path_user_id
from deerflow.runtime.user_context import get_effective_user_id
from deerflow.utils.file_io import file_io
@@ -213,6 +219,44 @@ def _build_attachment_headers(filename: str, extra_headers: dict[str, str] | Non
return headers
def _remove_temporary_bundle(path: str) -> None:
try:
Path(path).unlink(missing_ok=True)
except OSError:
logger.warning("Failed to remove temporary HTML offline bundle: %s", path, exc_info=True)
async def build_html_offline_bundle_response(actual_path: Path) -> FileResponse:
"""Build and stream a portable ZIP for one generated HTML artifact."""
descriptor, temporary_name = tempfile.mkstemp(prefix="deerflow-html-offline-", suffix=".zip")
os.close(descriptor)
temporary_path = Path(temporary_name)
try:
branding = load_system_settings().generated_page_branding
logo_path = generated_page_logo_path(branding)
await file_io.run_until_complete(
build_html_offline_bundle,
actual_path,
temporary_path,
runtime_base_url=html_runtime_base_url(),
map_tile_url=html_map_tile_url(),
generated_logo_path=logo_path,
)
except BaseException:
temporary_path.unlink(missing_ok=True)
raise
download_name = f"{actual_path.stem}-离线包.zip"
return FileResponse(
temporary_path,
filename=download_name,
media_type="application/zip",
headers={"Cache-Control": "private, no-store"},
background=BackgroundTask(_remove_temporary_bundle, str(temporary_path)),
)
def is_text_file_by_content(path: Path, sample_size: int = 8192) -> bool:
"""Check if file is text by examining content for null bytes."""
try:
@@ -988,10 +1032,20 @@ async def preview_artifact(
@router.get(
"/threads/{thread_id}/artifacts/{path:path}",
summary="Get Artifact File",
description="Retrieve an artifact file generated by the AI agent. Text and binary files can be viewed inline, while active web content is always downloaded.",
description=(
"Retrieve an artifact file generated by the AI agent. Text and binary files can be viewed inline, "
"while active web content is always downloaded. HTML files can be exported with their DeerFlow "
"runtime dependencies as a portable ZIP."
),
)
@require_permission("threads", "read", owner_check=True)
async def get_artifact(thread_id: str, path: str, request: Request, download: bool = False) -> Response:
async def get_artifact(
thread_id: str,
path: str,
request: Request,
download: bool = False,
offline_bundle: bool = False,
) -> Response:
"""Get an artifact file by its path.
The endpoint automatically detects file types and returns appropriate content types.
@@ -1018,6 +1072,8 @@ async def get_artifact(thread_id: str, path: str, request: Request, download: bo
download (bool): If true, forces attachment download for file types that are
otherwise returned inline or as plain text. Active HTML/XHTML/SVG content
is always downloaded regardless of this flag.
offline_bundle (bool): If true for an HTML/XHTML file, returns a ZIP containing
``index.html`` plus the packaged DeerFlow JS/CSS/image runtime dependencies.
Example:
- Get text file inline: `/api/threads/abc123/artifacts/mnt/user-data/outputs/notes.txt`
@@ -1075,6 +1131,11 @@ async def get_artifact(thread_id: str, path: str, request: Request, download: bo
mime_type, _ = mimetypes.guess_type(actual_path)
if offline_bundle:
if actual_path.suffix.lower() not in {".htm", ".html", ".xhtml"}:
raise HTTPException(status_code=400, detail="Offline bundles are available for HTML files only")
return await build_html_offline_bundle_response(actual_path)
if download:
return FileResponse(path=actual_path, filename=actual_path.name, media_type=mime_type, headers=_build_attachment_headers(actual_path.name))
@@ -681,6 +681,9 @@ def _embed_allowed_weknora_ids(ctx: WeKnoraEmbedContext) -> tuple[str, ...]:
def _embed_context_payload(ctx: WeKnoraEmbedContext) -> dict[str, Any]:
# Keep this payload bounded: it is signed directly into the browser cookie.
# The visible knowledge-base list is hydrated only for the embedded page or
# its list API call, after the cookie has been verified.
return {
"mapping_id": ctx.mapping_id,
"weknora_id": ctx.weknora_id,
@@ -688,11 +691,79 @@ def _embed_context_payload(ctx: WeKnoraEmbedContext) -> dict[str, Any]:
"is_admin": ctx.is_admin,
"can_write": ctx.can_write,
"is_conversation_deposit": ctx.is_conversation_deposit,
"allowed_weknora_ids": list(_embed_allowed_weknora_ids(ctx)),
"exp": ctx.exp,
}
async def _with_visible_embed_knowledge_bases(
request: Request,
ctx: WeKnoraEmbedContext,
) -> WeKnoraEmbedContext:
"""Hydrate the iframe-only picker allowlist without putting it in a cookie."""
visible_rows = await _store(request).list_visible(
ctx.user_id,
scope="all",
# Mirror the regular picker. An administrator must not receive another
# user's private bases merely because the iframe uses an admin token.
is_admin=False,
)
return replace(
ctx,
allowed_weknora_ids=tuple(
dict.fromkeys(
value
for value in (
ctx.weknora_id,
*(str(item.get("weknora_id") or "") for item in visible_rows),
)
if value
)
),
)
def _embed_directory_time(value: Any) -> str | None:
if isinstance(value, datetime):
return value.isoformat()
if isinstance(value, str) and value.strip():
return value
return None
async def _local_embed_knowledge_base_directory(
request: Request,
ctx: WeKnoraEmbedContext,
) -> Response:
"""Serve the iframe picker from DeerFlow mappings, never WeKnora's full directory."""
rows = await _store(request).list_visible(
ctx.user_id,
scope="all",
# Match the normal picker: the WeKnora service-admin token must not
# make an administrator's private libraries discoverable to others.
is_admin=False,
)
items = [
{
"id": str(row.get("weknora_id") or ""),
"name": str(row.get("name") or ""),
"description": str(row.get("description") or ""),
"type": str(row.get("kb_type") or "document"),
"status": "active",
"created_at": _embed_directory_time(row.get("created_at")),
"updated_at": _embed_directory_time(row.get("updated_at")),
}
for row in rows
if str(row.get("weknora_id") or "")
]
response = _json_response(
{"success": True, "data": items, "total": len(items)},
)
response.headers["Cache-Control"] = "no-store"
return response
def _weknora_id_from_platform_path(path: str) -> str | None:
prefix = "/platform/knowledge-bases/"
if not path.startswith(prefix):
@@ -708,15 +779,13 @@ async def _switch_embed_context(
ctx: WeKnoraEmbedContext,
weknora_id: str,
) -> WeKnoraEmbedContext:
"""Resolve a permitted native-UI switch to the exact cmzs mapping.
"""Resolve a native-UI switch to the exact cmzs mapping.
The embedded WeKnora UI uses a service-admin token, so every target remains
constrained to an ID signed into this short-lived session and is checked
again against the cmzs visibility rules before its page can be proxied.
The embedded WeKnora UI uses a service-admin token, so the target must be
checked against the current cmzs visibility rules on every switch. This is
deliberately dynamic rather than a signed ID list in the browser cookie.
"""
if weknora_id not in _embed_allowed_weknora_ids(ctx):
raise HTTPException(status_code=403, detail="Knowledge base is outside the embedded session scope")
store = _store(request)
mapped = await store.get_by_weknora_id(weknora_id)
if mapped is None:
@@ -2807,13 +2876,6 @@ async def _prepare_weknora_embed_session(
) -> tuple[str, dict[str, Any]]:
_runtime_for_iframe_proxy(request)
row, user_id, is_admin = await _authorized_mapping(request, mapping_id, write=False)
visible_rows = await _store(request).list_visible(
user_id,
scope="all",
# The iframe must mirror the knowledge-base picker, not grant an
# administrator an unlisted user's private knowledge bases.
is_admin=False,
)
writable = None
if not _is_conversation_deposit_mapping(row):
writable = await _store(request).get_authorized(mapping_id, user_id, write=True, is_admin=is_admin)
@@ -2825,7 +2887,6 @@ async def _prepare_weknora_embed_session(
is_admin=is_admin,
can_write=writable is not None,
is_conversation_deposit=_is_conversation_deposit_mapping(row),
allowed_weknora_ids=tuple(dict.fromkeys(value for value in (weknora_id, *(str(item.get("weknora_id") or "") for item in visible_rows)) if value)),
exp=int(time.time()) + _WEKNORA_EMBED_TTL_SECONDS,
)
return weknora_id, _embed_context_payload(ctx)
@@ -2885,6 +2946,7 @@ async def proxy_weknora_platform_page(request: Request, proxied_path: str = "")
requested_weknora_id = _weknora_id_from_platform_path(requested)
if requested_weknora_id is not None:
selected_ctx = await _switch_embed_context(request, ctx, requested_weknora_id)
selected_ctx = await _with_visible_embed_knowledge_bases(request, selected_ctx)
response = await _proxy_weknora_request(
request,
target_base_url=_runtime_for_iframe_proxy(request).web_base_url,
@@ -2909,6 +2971,7 @@ async def proxy_weknora_platform_page(request: Request, proxied_path: str = "")
public_target = _with_public_prefix(_request_public_prefix(request), allowed_path)
return RedirectResponse(f"{public_target}?{target_query}", status_code=302)
runtime = _runtime_for_iframe_proxy(request)
ctx = await _with_visible_embed_knowledge_bases(request, ctx)
return await _proxy_weknora_request(
request,
target_base_url=runtime.web_base_url,
@@ -2958,6 +3021,8 @@ async def proxy_weknora_api(request: Request, proxied_path: str) -> Response:
if request.method.upper() == "OPTIONS":
return Response(status_code=204)
ctx = await _read_embed_context(request)
if request.method.upper() in {"GET", "HEAD"} and proxied_path.rstrip("/") == "knowledge-bases":
return await _local_embed_knowledge_base_directory(request, ctx)
runtime = _runtime_for_iframe_proxy(request)
return await _proxy_weknora_request(
request,
@@ -2982,6 +3047,8 @@ async def proxy_weknora_embed_api(request: Request, proxied_path: str) -> Respon
if request.method.upper() == "OPTIONS":
return Response(status_code=204)
ctx = await _read_embed_context(request)
if request.method.upper() in {"GET", "HEAD"} and proxied_path.rstrip("/") == "knowledge-bases":
return await _local_embed_knowledge_base_directory(request, ctx)
runtime = _runtime_for_iframe_proxy(request)
return await _proxy_weknora_request(
request,
@@ -12,7 +12,7 @@ import time
import zipfile
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
from typing import Any, Literal
from fastapi import APIRouter, Depends, File, HTTPException, Query, Request, Response, UploadFile
from fastapi.responses import FileResponse
@@ -35,6 +35,7 @@ from app.gateway.skill_catalog_cache import (
load_or_warm_skill_catalog_snapshot,
load_skill_catalog_projection,
refresh_skill_catalog_projection,
set_skill_catalog_enabled,
)
from deerflow.agents.lead_agent.prompt import refresh_skills_system_prompt_cache_async
from deerflow.config.app_config import AppConfig
@@ -369,6 +370,24 @@ class SkillRollbackRequest(BaseModel):
history_index: int = Field(default=-1, description="History entry index to restore from, defaulting to the latest change.")
class SkillVersionEntryResponse(BaseModel):
version: int
sha256: str
created_at: str
created_by: str | None = None
action: str
summary: str | None = None
based_on_version: int | None = None
active: bool = False
class SkillVersionsResponse(BaseModel):
enabled: bool
skill_name: str
active_version: int | None = None
versions: list[SkillVersionEntryResponse] = Field(default_factory=list)
def _skill_to_response(
skill: Skill,
ownership: dict[str, Any] | None = None,
@@ -1305,6 +1324,10 @@ async def install_skill_upload(
request: Request,
file: UploadFile = File(..., description="The .zip or .skill archive to install"),
overwrite: bool = False,
install_mode: Literal["iterate", "overwrite"] | None = Query(
default=None,
description="Same-name custom upload mode when versioning is enabled: iterate or overwrite.",
),
target_category: SkillCategory = SkillCategory.CUSTOM,
config: AppConfig = Depends(get_config),
skill_store: SkillStore = Depends(get_skill_store),
@@ -1339,9 +1362,31 @@ async def install_skill_upload(
target_category=target_category,
conflict_kind=validation.get("conflict_kind"),
)
if (overwrite and not can_install_as_is and not can_overwrite) or (not overwrite and not can_install_as_is):
requested_overwrite = overwrite or install_mode in {"iterate", "overwrite"}
if (requested_overwrite and not can_install_as_is and not can_overwrite) or (
not requested_overwrite and not can_install_as_is
):
raise HTTPException(status_code=409, detail=conflict_message)
if install_mode == "iterate":
from deerflow.skills.versioning import record_custom_skill_version, skill_versioning_enabled
if not skill_versioning_enabled():
raise HTTPException(status_code=409, detail="技能迭代功能未开启")
if target_category != SkillCategory.CUSTOM or not _owned_by_me or not can_overwrite:
raise HTTPException(status_code=409, detail="只有本人已有的同名自定义技能可以迭代")
overwrite = True
await asyncio.to_thread(
record_custom_skill_version,
preview_name,
storage=storage,
action="baseline",
author=user_id,
summary="同名上传迭代前的活动版本",
)
elif install_mode == "overwrite":
overwrite = True
result = await storage.ainstall_skill_from_archive(tmp_path, overwrite=overwrite, require_skill_suffix=False, target_category=target_category)
storage.invalidate_skills_cache()
try:
from deerflow.skills.usage import mark_uploaded
mark_uploaded(result["skill_name"])
@@ -1360,8 +1405,49 @@ async def install_skill_upload(
await skill_store.ensure_legacy(
{"name": result["skill_name"], "owner_user_id": user_id, "published": False}
)
try:
from deerflow.skills.versioning import (
record_custom_skill_version,
reset_custom_skill_versions,
skill_versioning_enabled,
)
if skill_versioning_enabled():
if result.get("overwritten") and install_mode == "iterate":
await asyncio.to_thread(
record_custom_skill_version,
result["skill_name"],
storage=storage,
action="upload_iteration",
author=user_id,
summary="上传同名技能并迭代为新版本",
)
elif result.get("overwritten"):
await asyncio.to_thread(
reset_custom_skill_versions,
result["skill_name"],
storage=storage,
author=user_id,
summary="上传同名技能并直接覆盖",
)
else:
await asyncio.to_thread(
record_custom_skill_version,
result["skill_name"],
storage=storage,
action="upload",
author=user_id,
summary="上传技能初始版本",
)
except Exception:
logger.exception("Failed to persist custom skill version after upload: %s", result["skill_name"])
# The package is already installed. Do not report the whole
# upload as failed (which would invite an unsafe duplicate
# retry); opening the version list lazily snapshots the current
# package and repairs the manifest.
await _refresh_catalog_snapshot(config)
await refresh_skills_system_prompt_cache_async()
await _sync_skill_view(result["skill_name"])
return SkillInstallResponse(**result)
except FileNotFoundError as e:
raise HTTPException(status_code=404, detail=str(e))
@@ -1603,6 +1689,7 @@ async def install_skill(
target_category=target_category,
require_skill_suffix=False,
)
storage.invalidate_skills_cache()
try:
from deerflow.skills.usage import mark_uploaded
mark_uploaded(result.skill_name)
@@ -1616,8 +1703,16 @@ async def install_skill(
await skill_store.ensure_legacy(
{"name": result.skill_name, "owner_user_id": user_id, "published": False}
)
await _record_custom_version(
result.skill_name,
storage=storage,
action="artifact_install",
author=user_id,
summary="从会话产物安装技能初始版本",
)
await _refresh_catalog_snapshot(config)
await refresh_skills_system_prompt_cache_async()
await _sync_skill_view(result.skill_name)
return result
except FileNotFoundError as e:
raise HTTPException(status_code=404, detail=str(e))
@@ -1730,6 +1825,14 @@ async def duplicate_skill(
mark_uploaded(new_name)
except Exception:
pass
storage.invalidate_skills_cache()
await _record_custom_version(
new_name,
storage=storage,
action="duplicate",
author=user_id,
summary=f"从技能 {source_name} 复制创建",
)
await _refresh_catalog_snapshot(config)
await refresh_skills_system_prompt_cache_async()
return SkillInstallResponse(
@@ -1784,6 +1887,13 @@ async def reconcile_custom_skills(
mark_uploaded(skill.name)
except Exception:
pass
await _record_custom_version(
skill.name,
storage=storage,
action="reconcile",
author=user_id,
summary="接管已有自定义技能并建立初始版本",
)
adopted.append(skill.name)
if adopted:
@@ -1869,8 +1979,16 @@ async def update_custom_skill(skill_name: str, request: CustomSkillUpdateRequest
scan = await scan_skill_content(request.content, executable=False, location=f"{skill_name}/{SKILL_MD_FILE}", app_config=config)
if scan.decision == "block":
raise HTTPException(status_code=400, detail=f"Security scan blocked the edit: {scan.reason}")
await _record_custom_version(
skill_name,
storage=storage,
action="baseline",
author=None,
summary="编辑前的活动版本",
)
prev_content = storage.read_custom_skill(skill_name)
storage.write_custom_skill(skill_name, SKILL_MD_FILE, request.content)
storage.invalidate_skills_cache()
await _refresh_catalog_snapshot(config)
storage.append_history(
skill_name,
@@ -1889,7 +2007,15 @@ async def update_custom_skill(skill_name: str, request: CustomSkillUpdateRequest
bump_patch(skill_name)
except Exception:
pass
await _record_custom_version(
skill_name,
storage=storage,
action="human_edit",
author=None,
summary="编辑 SKILL.md",
)
await refresh_skills_system_prompt_cache_async()
await _sync_skill_view(skill_name)
return await get_custom_skill(skill_name, config)
except HTTPException:
raise
@@ -1941,6 +2067,12 @@ async def delete_custom_skill(
await skill_store.delete_admin(skill_name)
else:
await skill_store.delete(skill_name, user_id)
try:
from deerflow.skills.versioning import delete_custom_skill_versions
await asyncio.to_thread(delete_custom_skill_versions, skill_name)
except Exception:
logger.warning("Failed to delete version history for %s", skill_name, exc_info=True)
await _refresh_catalog_snapshot(config)
try:
@@ -2279,6 +2411,108 @@ async def get_custom_skill_history(skill_name: str, config: AppConfig = Depends(
raise HTTPException(status_code=500, detail=f"Failed to read history: {str(e)}")
@router.get(
"/skills/custom/{skill_name}/versions",
response_model=SkillVersionsResponse,
summary="List Custom Skill Versions",
)
async def get_custom_skill_versions(
skill_name: str,
request: Request,
config: AppConfig = Depends(get_config),
skill_store: SkillStore = Depends(get_skill_store),
) -> SkillVersionsResponse:
"""List complete-package snapshots for one owned/admin-managed custom skill."""
try:
skill_name = SkillStorage.validate_skill_name(skill_name.replace("\r\n", "").replace("\n", ""))
await _get_editable_skill_or_raise(
skill_store,
skill_name,
_current_user_id(request),
is_admin=_is_admin_user(request),
)
storage = get_or_new_skill_storage(app_config=config)
if not storage.custom_skill_exists(skill_name):
raise HTTPException(status_code=404, detail=f"Custom skill '{skill_name}' not found")
from deerflow.skills.versioning import list_custom_skill_versions
payload = await asyncio.to_thread(
list_custom_skill_versions,
skill_name,
storage=storage,
author=_current_user_id(request),
)
return SkillVersionsResponse.model_validate(payload)
except HTTPException:
raise
except (FileNotFoundError, ValueError) as e:
raise HTTPException(status_code=404, detail=str(e))
except Exception as e:
logger.error("Failed to list versions for %s: %s", skill_name, e, exc_info=True)
raise HTTPException(status_code=500, detail=f"Failed to list skill versions: {e}")
@router.post(
"/skills/custom/{skill_name}/versions/{version}/activate",
response_model=SkillVersionsResponse,
summary="Activate Custom Skill Version",
)
async def activate_custom_skill_version_endpoint(
skill_name: str,
version: int,
request: Request,
config: AppConfig = Depends(get_config),
skill_store: SkillStore = Depends(get_skill_store),
) -> SkillVersionsResponse:
"""Switch the sole model-visible package without touching usage counters."""
try:
skill_name = SkillStorage.validate_skill_name(skill_name.replace("\r\n", "").replace("\n", ""))
await _get_editable_skill_or_raise(
skill_store,
skill_name,
_current_user_id(request),
is_admin=_is_admin_user(request),
)
storage = get_or_new_skill_storage(app_config=config)
if not storage.custom_skill_exists(skill_name):
raise HTTPException(status_code=404, detail=f"Custom skill '{skill_name}' not found")
from deerflow.skills.versioning import (
activate_custom_skill_version,
list_custom_skill_versions,
)
await asyncio.to_thread(
activate_custom_skill_version,
skill_name,
version,
storage=storage,
)
storage.invalidate_skills_cache()
await _refresh_catalog_snapshot(config)
await refresh_skills_system_prompt_cache_async()
await _sync_skill_view(skill_name)
payload = await asyncio.to_thread(
list_custom_skill_versions,
skill_name,
storage=storage,
author=_current_user_id(request),
)
return SkillVersionsResponse.model_validate(payload)
except HTTPException:
raise
except RuntimeError as e:
raise HTTPException(status_code=409, detail=str(e))
except FileNotFoundError as e:
raise HTTPException(status_code=404, detail=str(e))
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
logger.error("Failed to activate %s version %s: %s", skill_name, version, e, exc_info=True)
raise HTTPException(status_code=500, detail=f"Failed to activate skill version: {e}")
@router.post("/skills/custom/{skill_name}/rollback", response_model=CustomSkillContentResponse, summary="Rollback Custom Skill")
async def rollback_custom_skill(skill_name: str, request: SkillRollbackRequest, config: AppConfig = Depends(get_config)) -> CustomSkillContentResponse:
try:
@@ -2309,10 +2543,26 @@ async def rollback_custom_skill(skill_name: str, request: SkillRollbackRequest,
if scan.decision == "block":
storage.append_history(skill_name, history_entry)
raise HTTPException(status_code=400, detail=f"Rollback blocked by security scanner: {scan.reason}")
await _record_custom_version(
skill_name,
storage=storage,
action="baseline",
author=None,
summary="旧历史回滚前的活动版本",
)
storage.write_custom_skill(skill_name, SKILL_MD_FILE, target_content)
storage.invalidate_skills_cache()
await _refresh_catalog_snapshot(config)
storage.append_history(skill_name, history_entry)
await _record_custom_version(
skill_name,
storage=storage,
action="legacy_rollback",
author=None,
summary="从旧编辑历史恢复 SKILL.md",
)
await refresh_skills_system_prompt_cache_async()
await _sync_skill_view(skill_name)
return await get_custom_skill(skill_name, config)
except HTTPException:
raise
@@ -2775,6 +3025,28 @@ async def _sync_skill_view(name: str) -> None:
logger.warning("Failed to sync skill %s into the sandbox view", name, exc_info=True)
async def _record_custom_version(
name: str,
*,
storage: SkillStorage,
action: str,
author: str | None,
summary: str,
) -> None:
"""Capture one complete custom-skill package when versioning is enabled."""
from deerflow.skills.versioning import record_custom_skill_version
await asyncio.to_thread(
record_custom_skill_version,
name,
storage=storage,
action=action,
author=author,
summary=summary,
)
@router.put(
"/skills/{skill_name}/files/content",
response_model=SkillFileSaveResponse,
@@ -2826,7 +3098,18 @@ async def save_skill_file(
# SKILL.md edits must keep their frontmatter valid (name must match).
storage.validate_skill_markdown_content(skill.name, body.content)
if skill.category == SkillCategory.CUSTOM:
await _record_custom_version(
skill.name,
storage=storage,
action="baseline",
author=_current_user_id(request),
summary="文件编辑前的活动版本",
)
_write_skill_text_file_atomically(target, body.content)
if skill.category == SkillCategory.CUSTOM:
storage.invalidate_skills_cache()
try:
if skill.category == SkillCategory.CUSTOM:
@@ -2852,6 +3135,15 @@ async def save_skill_file(
pass
await refresh_skills_system_prompt_cache_async()
if skill.category == SkillCategory.CUSTOM:
await _record_custom_version(
skill.name,
storage=storage,
action="human_edit_file",
author=_current_user_id(request),
summary=f"编辑文件 {relative_posix}",
)
await _sync_skill_view(skill.name)
stat = target.stat()
@@ -2911,10 +3203,19 @@ async def delete_skill_file(
prev_content = target.read_text(encoding="utf-8")
except UnicodeDecodeError:
pass
if skill.category == SkillCategory.CUSTOM:
await _record_custom_version(
skill.name,
storage=get_or_new_skill_storage(app_config=config),
action="baseline",
author=_current_user_id(request),
summary="文件删除前的活动版本",
)
target.unlink()
if skill.category == SkillCategory.CUSTOM:
storage = get_or_new_skill_storage(app_config=config)
storage.invalidate_skills_cache()
try:
storage.append_history(
skill.name,
@@ -2928,6 +3229,13 @@ async def delete_skill_file(
)
except Exception as history_err: # noqa: BLE001
logger.warning("Failed to append deletion history for %s/%s: %s", skill.name, relative_posix, history_err)
await _record_custom_version(
skill.name,
storage=storage,
action="human_delete_file",
author=_current_user_id(request),
summary=f"删除文件 {relative_posix}",
)
else:
logger.info("Administrator deleted built-in skill file %s/%s", skill.name, relative_posix)
await _sync_skill_view(skill.name)
@@ -3010,6 +3318,11 @@ async def update_skill(
logger.info(f"Skills configuration updated and saved to: {config_path}")
await refresh_skills_system_prompt_cache_async()
await _sync_skill_view(skill_name)
# The list endpoint reads skills.enabled from the catalog projection.
# Write that one row before the client refetches. A missing row still
# needs the full snapshot so a newly discovered skill can appear.
if not await set_skill_catalog_enabled(skill_name, request.enabled):
await _refresh_catalog_snapshot(config)
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)
@@ -16,15 +16,20 @@ from app.gateway.deps import get_current_user_from_request, get_optional_user_fr
from deerflow.config import get_app_config
from deerflow.config.system_settings import (
BUILTIN_THEME_IDS,
AIWritingPageDescription,
AppearanceRouteParam,
AuthenticationSettings,
CallMetricsSettings,
CompactShortcutButton,
ConversationContextSettings,
CustomThemePreset,
GeneralQaPageDescriptions,
HtmlMapSettings,
LeaderboardSettings,
ModelConcurrencySettings,
ModelPromptSuffix,
PageDescriptionsSettings,
PageHeaderDescription,
PluginDomainAgentMapping,
PluginManagementSettings,
PromptPrefixSettings,
@@ -33,6 +38,7 @@ from deerflow.config.system_settings import (
RetryKeepPartialSettings,
RoundtableRoomLimitSettings,
SkillCompressionSettings,
SkillVersioningSettings,
StrategyProjectSettings,
WordHeadingNumberingStyle,
WriteFileCompatibilitySettings,
@@ -208,6 +214,91 @@ class StrategyProjectSettingsUpdateRequest(BaseModel):
taiwan_strait_demo_enabled: bool | None = None
class PageDescriptionsResponse(BaseModel):
general_qa: GeneralQaPageDescriptions = Field(default_factory=GeneralQaPageDescriptions)
knowledge_base: PageHeaderDescription = Field(default_factory=PageHeaderDescription)
ai_writing: AIWritingPageDescription = Field(default_factory=AIWritingPageDescription)
strategy_projects: PageHeaderDescription = Field(default_factory=PageHeaderDescription)
deep_research: PageHeaderDescription = Field(default_factory=PageHeaderDescription)
gongwen: PageHeaderDescription = Field(default_factory=PageHeaderDescription)
light_app_management: PageHeaderDescription = Field(default_factory=PageHeaderDescription)
light_app_list: PageHeaderDescription = Field(default_factory=PageHeaderDescription)
agents: PageHeaderDescription = Field(default_factory=PageHeaderDescription)
business_chains: PageHeaderDescription = Field(default_factory=PageHeaderDescription)
skills: PageHeaderDescription = Field(default_factory=PageHeaderDescription)
knowledge_management: PageHeaderDescription = Field(default_factory=PageHeaderDescription)
class PageDescriptionsUpdateRequest(BaseModel):
general_qa: GeneralQaPageDescriptions | None = None
knowledge_base: PageHeaderDescription | None = None
ai_writing: AIWritingPageDescription | None = None
strategy_projects: PageHeaderDescription | None = None
deep_research: PageHeaderDescription | None = None
gongwen: PageHeaderDescription | None = None
light_app_management: PageHeaderDescription | None = None
light_app_list: PageHeaderDescription | None = None
agents: PageHeaderDescription | None = None
business_chains: PageHeaderDescription | None = None
skills: PageHeaderDescription | None = None
knowledge_management: PageHeaderDescription | None = None
def _page_descriptions_response(
settings: PageDescriptionsSettings,
) -> PageDescriptionsResponse:
return PageDescriptionsResponse(
general_qa=settings.general_qa,
knowledge_base=settings.knowledge_base,
ai_writing=settings.ai_writing,
strategy_projects=settings.strategy_projects,
deep_research=settings.deep_research,
gongwen=settings.gongwen,
light_app_management=settings.light_app_management,
light_app_list=settings.light_app_list,
agents=settings.agents,
business_chains=settings.business_chains,
skills=settings.skills,
knowledge_management=settings.knowledge_management,
)
@router.get("/page-descriptions", response_model=PageDescriptionsResponse)
async def get_page_descriptions() -> PageDescriptionsResponse:
return _page_descriptions_response(load_system_settings().page_descriptions)
@router.put("/page-descriptions", response_model=PageDescriptionsResponse)
async def update_page_descriptions(
body: PageDescriptionsUpdateRequest,
request: Request,
) -> PageDescriptionsResponse:
await _require_admin(request)
current = load_system_settings()
updated = PageDescriptionsSettings(
general_qa=body.general_qa or current.page_descriptions.general_qa,
knowledge_base=body.knowledge_base or current.page_descriptions.knowledge_base,
ai_writing=body.ai_writing or current.page_descriptions.ai_writing,
strategy_projects=body.strategy_projects or current.page_descriptions.strategy_projects,
deep_research=body.deep_research or current.page_descriptions.deep_research,
gongwen=body.gongwen or current.page_descriptions.gongwen,
light_app_management=(
body.light_app_management or current.page_descriptions.light_app_management
),
light_app_list=body.light_app_list or current.page_descriptions.light_app_list,
agents=body.agents or current.page_descriptions.agents,
business_chains=body.business_chains or current.page_descriptions.business_chains,
skills=body.skills or current.page_descriptions.skills,
knowledge_management=(
body.knowledge_management or current.page_descriptions.knowledge_management
),
)
saved = save_system_settings(
current.model_copy(update={"page_descriptions": updated}),
)
return _page_descriptions_response(saved.page_descriptions)
@router.get("/ordinary-knowledge-landing", response_model=OrdinaryKnowledgeLanding)
async def get_ordinary_knowledge_landing() -> OrdinaryKnowledgeLanding:
return OrdinaryKnowledgeLanding(mode=load_system_settings().ordinary_knowledge_landing)
@@ -491,6 +582,14 @@ class SkillCompressionUpdateRequest(BaseModel):
keep_index_in_prompt: bool | None = None
class SkillVersioningResponse(BaseModel):
enabled: bool = Field(..., description="Whether custom-skill version history is active")
class SkillVersioningUpdateRequest(BaseModel):
enabled: bool
class ModelConcurrencyResponse(BaseModel):
enabled: bool = Field(..., description="Whether per-model concurrency limiting is active")
default_limit: int = Field(..., description="Fallback max concurrent runs for models without an explicit override")
@@ -532,6 +631,17 @@ class WriteFileCompatibilityUpdateRequest(BaseModel):
enabled: bool
class CallMetricsResponse(BaseModel):
enabled: bool = Field(
default=True,
description="Whether new tool and skill calls are recorded for management statistics",
)
class CallMetricsUpdateRequest(BaseModel):
enabled: bool
class RetryKeepPartialResponse(BaseModel):
enabled: bool = Field(
default=True,
@@ -579,6 +689,7 @@ class RoundtableRoomLimitResponse(BaseModel):
enabled: bool = True
max_rooms: int = 3
max_hold_minutes: int = 30
lease_minutes: int = 5
max_cycles: int = 8
write_file_compatibility_enabled: bool = False
@@ -587,6 +698,7 @@ class RoundtableRoomLimitUpdateRequest(BaseModel):
enabled: bool | None = None
max_rooms: int | None = Field(default=None, ge=1, le=20)
max_hold_minutes: int | None = Field(default=None, ge=1, le=1440)
lease_minutes: int | None = Field(default=None, ge=1, le=60)
max_cycles: int | None = Field(default=None, ge=2, le=100)
write_file_compatibility_enabled: bool | None = None
@@ -667,6 +779,32 @@ async def update_write_file_compatibility_setting(
)
@router.get("/call-metrics", response_model=CallMetricsResponse)
async def get_call_metrics_setting(request: Request) -> CallMetricsResponse:
"""Return the administrator-controlled tool/skill metrics switch."""
await _require_admin(request)
return CallMetricsResponse(enabled=load_system_settings().call_metrics.enabled)
@router.put("/call-metrics", response_model=CallMetricsResponse)
async def update_call_metrics_setting(
body: CallMetricsUpdateRequest,
request: Request,
) -> CallMetricsResponse:
"""Enable or pause new tool/skill metric writes without a restart."""
await _require_admin(request)
current = load_system_settings()
saved = save_system_settings(
current.model_copy(
update={"call_metrics": CallMetricsSettings(enabled=body.enabled)}
)
)
logger.info("Tool and skill call metrics switch changed: enabled=%s", saved.call_metrics.enabled)
return CallMetricsResponse(enabled=saved.call_metrics.enabled)
@router.get("/retry-keep-partial", response_model=RetryKeepPartialResponse)
async def get_retry_keep_partial_setting() -> RetryKeepPartialResponse:
"""Return the retry-keeps-partial display switch (readable by chat clients)."""
@@ -734,6 +872,7 @@ def _roundtable_room_limit_response(settings: RoundtableRoomLimitSettings) -> Ro
enabled=settings.enabled,
max_rooms=settings.max_rooms,
max_hold_minutes=settings.max_hold_minutes,
lease_minutes=settings.lease_minutes,
max_cycles=settings.max_cycles,
write_file_compatibility_enabled=settings.write_file_compatibility_enabled,
)
@@ -760,6 +899,8 @@ async def update_roundtable_room_limit_setting(
updates["max_rooms"] = body.max_rooms
if body.max_hold_minutes is not None:
updates["max_hold_minutes"] = body.max_hold_minutes
if body.lease_minutes is not None:
updates["lease_minutes"] = body.lease_minutes
if body.max_cycles is not None:
updates["max_cycles"] = body.max_cycles
if body.write_file_compatibility_enabled is not None:
@@ -767,10 +908,11 @@ async def update_roundtable_room_limit_setting(
updated = current.roundtable_room_limit.model_copy(update=updates)
saved = save_system_settings(current.model_copy(update={"roundtable_room_limit": updated}))
logger.info(
"Roundtable runtime limits changed: enabled=%s max_rooms=%s max_hold_minutes=%s max_cycles=%s write_file_compatibility=%s",
"Roundtable runtime limits changed: enabled=%s max_rooms=%s max_hold_minutes=%s lease_minutes=%s max_cycles=%s write_file_compatibility=%s",
saved.roundtable_room_limit.enabled,
saved.roundtable_room_limit.max_rooms,
saved.roundtable_room_limit.max_hold_minutes,
saved.roundtable_room_limit.lease_minutes,
saved.roundtable_room_limit.max_cycles,
saved.roundtable_room_limit.write_file_compatibility_enabled,
)
@@ -917,6 +1059,42 @@ async def update_skill_compression(body: SkillCompressionUpdateRequest, request:
return _skill_compression_to_response(saved.skill_compression)
@router.get("/skill-versioning", response_model=SkillVersioningResponse)
async def get_skill_versioning() -> SkillVersioningResponse:
"""Return the custom-skill versioning gate for skill detail clients."""
return SkillVersioningResponse(enabled=load_system_settings().skill_versioning.enabled)
@router.put("/skill-versioning", response_model=SkillVersioningResponse)
async def update_skill_versioning(
body: SkillVersioningUpdateRequest,
request: Request,
) -> SkillVersioningResponse:
"""Enable/disable custom-skill version capture and switching. Admin only."""
await _require_admin(request)
current = load_system_settings()
previous = current.skill_versioning.enabled
updated = SkillVersioningSettings(enabled=body.enabled)
saved = save_system_settings(current.model_copy(update={"skill_versioning": updated}))
if body.enabled and not previous:
try:
from deerflow.skills.versioning import reconcile_custom_skill_versions
actor = await get_optional_user_from_request(request)
actor_id = str(getattr(actor, "id", "") or "") or None
await asyncio.to_thread(reconcile_custom_skill_versions, author=actor_id)
except Exception:
logger.exception("Failed to capture custom-skill baselines after enabling versioning")
# The switch remains enabled. Individual skills are bootstrapped
# lazily when their version list is first opened or next changed.
logger.info("Custom skill versioning switch changed: enabled=%s", saved.skill_versioning.enabled)
return SkillVersioningResponse(enabled=saved.skill_versioning.enabled)
@router.get("/model-concurrency", response_model=ModelConcurrencyResponse)
async def get_model_concurrency(request: Request) -> ModelConcurrencyResponse:
"""Return the per-model concurrency-limit configuration. Admin only."""
@@ -19,6 +19,7 @@ from fastapi.responses import FileResponse
from pydantic import BaseModel, Field
from app.gateway.deps import get_checkpointer, get_thread_share_store, get_thread_store
from app.gateway.routers.artifacts import build_html_offline_bundle_response
from app.gateway.thread_copy import copy_thread_state
from deerflow.config.paths import get_paths
from deerflow.persistence.thread_meta.base import ThreadMetaStore
@@ -316,6 +317,7 @@ def _public_share_disposition(mime_type: str) -> str:
async def get_public_share_file(
share_code: str,
path: str,
offline_bundle: bool = False,
share_store: ThreadShareStore = Depends(get_thread_share_store),
) -> FileResponse:
"""Serve a file referenced by a view-shared conversation.
@@ -342,6 +344,11 @@ async def get_public_share_file(
if not actual_path.exists() or not actual_path.is_file():
raise HTTPException(status_code=404, detail="File not found")
if offline_bundle:
if actual_path.suffix.lower() not in {".htm", ".html", ".xhtml"}:
raise HTTPException(status_code=400, detail="Offline bundles are available for HTML files only")
return await build_html_offline_bundle_response(actual_path)
mime_type = mimetypes.guess_type(actual_path.name)[0] or "application/octet-stream"
return FileResponse(
actual_path,
@@ -252,6 +252,29 @@ async def refresh_skill_catalog_projection(
return len(catalog)
async def set_skill_catalog_enabled(name: str, enabled: bool) -> bool:
"""Persist one skill's online flag in the catalog row the list API reads.
The extensions file is the runtime source of truth. The skills page reads
``skills.enabled`` instead, so a toggle that only rewrites the file looks
unchanged after the list refreshes. Updating this single row avoids a full
catalog rebuild, which rewrites every skill and can roll the flag back if
an unrelated row fails.
"""
session_factory = get_session_factory()
if session_factory is None or not name:
return False
async with session_factory() as session:
result = await session.execute(
update(SkillRow)
.where(SkillRow.name == name)
.values(enabled=bool(enabled), updated_at=datetime.now(UTC))
)
await session.commit()
return int(result.rowcount or 0) > 0
def _build_catalog_payload(skills: list[Any], usage_entries: dict[str, Any]) -> list[dict[str, Any]]:
payload: list[dict[str, Any]] = []
defaults = {