Merge branch 'master' into dhk-1
解决 8 处冲突: - app.py:同时挂载 public_embed + html_page_favorites 两个 router - .env:保留 dhk-1 的 CORS 配置(含 5174、ALLOW_ALL=0) - message-list.tsx:并入双方新增(onSetupWriting* 解构 + contentClassName/fitMessageRowsToContent 类型) - WorkspaceRoutes.tsx:采用 master 的路由结构(含 chats_iframe/IframeChatPage),补回 dhk-1 的 BusinessChainEditorPage 导入与 roundtable/chains 路由 - LoginPage.tsx:合并两套登录(master 的 token 换登录 + dhk-1 的 online-ticket),并集依赖数组 - ScheduledTasksPage.tsx:采用 master 的 HTML 定时任务表单(isHtmlTask 分支) - AIWritingForm.tsx:保留 dhk-1 的 useWritingForm 重构,补回 master 的笔记来源 useState;WritingFormContext.materialSource 扩展支持 'notebook' - useAIWritingStream.ts:保留 dhk-1 重构(仅 reducer/helpers,useAIWriting 已移至 AIWritingContext),不并入 master 在此文件的旧实现 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
commit
d49406d2f1
102 files changed
+6816
-6016
No files matched your search
@@ -32,6 +32,7 @@ from app.gateway.routers import (
|
||||
auth,
|
||||
channels,
|
||||
feedback,
|
||||
html_page_favorites,
|
||||
intent,
|
||||
llm_metrics,
|
||||
mcp,
|
||||
@@ -881,6 +882,7 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
|
||||
|
||||
# Public embed Q&A status polling (/api/public/embed/*)
|
||||
app.include_router(public_embed.router)
|
||||
app.include_router(html_page_favorites.router)
|
||||
|
||||
# System settings API is mounted at /api/system-settings
|
||||
app.include_router(system_settings.router)
|
||||
|
||||
@@ -41,6 +41,7 @@ _PUBLIC_EXACT_PATHS: frozenset[str] = frozenset(
|
||||
{
|
||||
"/api/v1/auth/login/local",
|
||||
"/api/v1/auth/login/username",
|
||||
"/api/v1/auth/login/token",
|
||||
"/api/v1/auth/register",
|
||||
"/api/v1/auth/logout",
|
||||
"/api/v1/auth/setup-status",
|
||||
|
||||
@@ -93,6 +93,10 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
|
||||
app.state.scheduled_task_store = make_scheduled_task_store(sf)
|
||||
|
||||
from deerflow.persistence.html_pages import make_html_page_store
|
||||
|
||||
app.state.html_page_store = make_html_page_store(sf)
|
||||
|
||||
from deerflow.persistence.llm_metrics import make_llm_metrics_store
|
||||
|
||||
app.state.llm_metrics_store = make_llm_metrics_store(sf)
|
||||
|
||||
@@ -6,10 +6,12 @@ import time
|
||||
from ipaddress import ip_address, ip_network
|
||||
from typing import Literal
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request, Response, status
|
||||
import httpx
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request, Response, status
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
from pydantic import BaseModel, EmailStr, Field, field_validator
|
||||
|
||||
from deerflow.config import get_app_config
|
||||
from deerflow.persistence.agents.base import AgentStore
|
||||
from deerflow.persistence.skills.base import SkillStore
|
||||
|
||||
@@ -174,12 +176,12 @@ def _normalize_username_local_part(username: str) -> str | None:
|
||||
"""
|
||||
raw = username.strip()
|
||||
if not raw:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="username cannot be empty")
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="用户名不能为空")
|
||||
if "@" in raw:
|
||||
return None
|
||||
normalized = "".join(ch if (ch.isalnum() or ch in "._-") else "-" for ch in raw.lower()).strip("._-")
|
||||
if not normalized:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="username is invalid")
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="用户名不合法")
|
||||
return normalized
|
||||
|
||||
|
||||
@@ -200,6 +202,117 @@ def _username_to_email(username: str) -> str:
|
||||
return f"{local}@{domain}"
|
||||
|
||||
|
||||
async def _get_or_create_user_by_username(username: str):
|
||||
"""Resolve a username to a local user, auto-registering when absent.
|
||||
|
||||
Shared by ``/login/username`` and ``/login/token`` — both turn an
|
||||
externally-supplied username into a local account and sign it in. Returns
|
||||
``(user, created)`` where ``created`` is True only when this call inserted
|
||||
the row. Mirrors the legacy domain-agnostic fallback and the canonical
|
||||
``admin`` promotion so both entry points behave identically.
|
||||
"""
|
||||
provider = get_local_provider()
|
||||
email = _username_to_email(username)
|
||||
local_part = _normalize_username_local_part(username)
|
||||
|
||||
created = False
|
||||
user = await provider.get_user_by_email(email)
|
||||
# Domain-agnostic fallback: if no row exists at the canonical
|
||||
# ``<username>@<current-default-domain>``, look for an existing user with
|
||||
# the same local part under any domain. This is the safety net that keeps
|
||||
# usernames globally unique when the default email domain changes (e.g.
|
||||
# ``local.deerflow`` → ``cm.com``) — without it, switching the default
|
||||
# would let the same username log into two different accounts.
|
||||
if user is None and local_part is not None:
|
||||
legacy = await provider.find_user_by_email_local_part(local_part)
|
||||
if legacy is not None:
|
||||
user = legacy
|
||||
logger.info("Matched username %r to legacy account %s", username, legacy.email)
|
||||
if user is None:
|
||||
try:
|
||||
role = "admin" if username == "admin" else "user"
|
||||
user = await provider.create_user(email=email, password=None, system_role=role, needs_setup=False)
|
||||
created = True
|
||||
logger.info("Auto-registered passwordless user: %s", email)
|
||||
except ValueError:
|
||||
# Concurrent first login may create the same user in parallel.
|
||||
user = await provider.get_user_by_email(email)
|
||||
if user is None and local_part is not None:
|
||||
user = await provider.find_user_by_email_local_part(local_part)
|
||||
if user is None:
|
||||
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="创建或加载用户失败")
|
||||
elif username == "admin" and user.system_role != "admin":
|
||||
# Historical accounts created before the username-based role assignment
|
||||
# may still have system_role="user" — promote the canonical admin user
|
||||
# so /login/admin behaves consistently.
|
||||
user.system_role = "admin"
|
||||
user = await provider.update_user(user)
|
||||
logger.info("Promoted historical 'admin' account to system_role=admin: %s", email)
|
||||
|
||||
return user, created
|
||||
|
||||
|
||||
async def _fetch_username_from_token(token: str) -> str:
|
||||
"""Resolve an upstream access token to a username via the configured endpoint.
|
||||
|
||||
Calls ``auth_login.token_login.token_info_url`` with the caller-supplied
|
||||
token placed directly in the ``Authorization: Bearer <token>`` header — the
|
||||
upstream reads the bearer token from there, not from a cookie. Expects
|
||||
``{"state": "200", "data": {"username": ...}}`` and returns ``data.username``.
|
||||
Any upstream failure, non-200 ``state``, or missing username raises HTTP 401.
|
||||
"""
|
||||
config = get_app_config().auth_login.token_login
|
||||
if not config.token_info_url:
|
||||
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="token 登录未配置(token_info_url 为空)")
|
||||
|
||||
# TLS verification for the (HTTPS) upstream. A custom CA bundle wins; else
|
||||
# the boolean toggle. UAT/internal hosts often need verify_ssl=false because
|
||||
# they serve self-signed or private-CA certs — without this the call dies
|
||||
# with an SSLError and every token login silently 401s.
|
||||
verify: bool | str = config.ca_cert_path if config.ca_cert_path else config.verify_ssl
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=config.timeout_seconds, verify=verify) as client:
|
||||
upstream = await client.post(
|
||||
config.token_info_url,
|
||||
headers={
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
)
|
||||
except httpx.HTTPError as exc:
|
||||
# Surface the concrete failure (SSL handshake, DNS, timeout, ...) in logs
|
||||
# so HTTPS/cert misconfiguration is diagnosable; client still sees 401.
|
||||
logger.warning(
|
||||
"Token login: upstream call to %s failed (verify=%r): %s: %s",
|
||||
config.token_info_url,
|
||||
verify,
|
||||
type(exc).__name__,
|
||||
exc,
|
||||
)
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail=AuthErrorResponse(code=AuthErrorCode.INVALID_CREDENTIALS, message="token 校验失败").model_dump())
|
||||
|
||||
if upstream.status_code != 200:
|
||||
logger.warning("Token login: upstream returned HTTP %s", upstream.status_code)
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail=AuthErrorResponse(code=AuthErrorCode.INVALID_CREDENTIALS, message="token 无效").model_dump())
|
||||
|
||||
try:
|
||||
payload = upstream.json()
|
||||
except ValueError:
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail=AuthErrorResponse(code=AuthErrorCode.INVALID_CREDENTIALS, message="token 接口返回格式异常").model_dump())
|
||||
|
||||
# Upstream signals success with the string state "200" (see API docs).
|
||||
if str(payload.get("state")) != "200":
|
||||
logger.warning("Token login: upstream state=%r msg=%r", payload.get("state"), payload.get("msg"))
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail=AuthErrorResponse(code=AuthErrorCode.INVALID_CREDENTIALS, message="token 无效或已过期").model_dump())
|
||||
|
||||
username = (payload.get("data") or {}).get("username")
|
||||
if not username or not str(username).strip():
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail=AuthErrorResponse(code=AuthErrorCode.INVALID_CREDENTIALS, message="未能从 token 解析出用户名").model_dump())
|
||||
|
||||
return str(username).strip()
|
||||
|
||||
|
||||
def _set_session_cookie(response: Response, token: str, request: Request) -> None:
|
||||
"""Set the access_token HttpOnly cookie on the response."""
|
||||
config = get_auth_config()
|
||||
@@ -301,7 +414,7 @@ def _check_rate_limit(ip: str) -> None:
|
||||
if time.time() < lock_until:
|
||||
raise HTTPException(
|
||||
status_code=429,
|
||||
detail="Too many login attempts. Try again later.",
|
||||
detail="登录尝试次数过多,请稍后再试",
|
||||
)
|
||||
del _login_attempts[ip]
|
||||
|
||||
@@ -357,7 +470,7 @@ async def login_local(
|
||||
_record_login_failure(client_ip)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail=AuthErrorResponse(code=AuthErrorCode.INVALID_CREDENTIALS, message="Incorrect email or password").model_dump(),
|
||||
detail=AuthErrorResponse(code=AuthErrorCode.INVALID_CREDENTIALS, message="邮箱或密码错误").model_dump(),
|
||||
)
|
||||
|
||||
_record_login_success(client_ip)
|
||||
@@ -371,53 +484,39 @@ async def login_local(
|
||||
|
||||
|
||||
@router.post("/login/username", response_model=UsernameLoginResponse)
|
||||
async def login_by_username(request: Request, response: Response, body: UsernameLoginRequest):
|
||||
async def login_by_username(
|
||||
request: Request,
|
||||
response: Response,
|
||||
body: UsernameLoginRequest,
|
||||
password: str | None = Query(default=None, description="Shared gate secret; required only when auth_login.username_login.require_password is on."),
|
||||
):
|
||||
"""Passwordless username login with auto-registration.
|
||||
|
||||
Rules:
|
||||
- If mapped account exists, sign in directly (no password required).
|
||||
- If account does not exist, create a regular user and sign in.
|
||||
|
||||
Password gate: when ``auth_login.username_login.require_password`` is
|
||||
enabled, callers must additionally pass ``?password=`` matching the
|
||||
configured shared secret (default ``123ewq`` when unset). The gate is a
|
||||
single shared password — it does not change which account is logged in.
|
||||
|
||||
Returns the JWT access token in the response body and also sets it as
|
||||
a session cookie for browser callers.
|
||||
"""
|
||||
provider = get_local_provider()
|
||||
email = _username_to_email(body.username)
|
||||
local_part = _normalize_username_local_part(body.username)
|
||||
username_login_config = get_app_config().auth_login.username_login
|
||||
if username_login_config.require_password:
|
||||
client_ip = _get_client_ip(request)
|
||||
_check_rate_limit(client_ip)
|
||||
if password != username_login_config.effective_password:
|
||||
_record_login_failure(client_ip)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail=AuthErrorResponse(code=AuthErrorCode.INVALID_CREDENTIALS, message="口令错误").model_dump(),
|
||||
)
|
||||
_record_login_success(client_ip)
|
||||
|
||||
created = False
|
||||
user = await provider.get_user_by_email(email)
|
||||
# Domain-agnostic fallback: if no row exists at the canonical
|
||||
# ``<username>@<current-default-domain>``, look for an existing user with
|
||||
# the same local part under any domain. This is the safety net that keeps
|
||||
# usernames globally unique when the default email domain changes (e.g.
|
||||
# ``local.deerflow`` → ``cm.com``) — without it, switching the default
|
||||
# would let the same username log into two different accounts.
|
||||
if user is None and local_part is not None:
|
||||
legacy = await provider.find_user_by_email_local_part(local_part)
|
||||
if legacy is not None:
|
||||
user = legacy
|
||||
logger.info("Matched username %r to legacy account %s", body.username, legacy.email)
|
||||
if user is None:
|
||||
try:
|
||||
role = "admin" if body.username == "admin" else "user"
|
||||
user = await provider.create_user(email=email, password=None, system_role=role, needs_setup=False)
|
||||
created = True
|
||||
logger.info("Auto-registered passwordless user: %s", email)
|
||||
except ValueError:
|
||||
# Concurrent first login may create the same user in parallel.
|
||||
user = await provider.get_user_by_email(email)
|
||||
if user is None and local_part is not None:
|
||||
user = await provider.find_user_by_email_local_part(local_part)
|
||||
if user is None:
|
||||
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to create or load user")
|
||||
elif body.username == "admin" and user.system_role != "admin":
|
||||
# Historical accounts created before the username-based role assignment
|
||||
# may still have system_role="user" — promote the canonical admin user
|
||||
# so /login/admin behaves consistently.
|
||||
user.system_role = "admin"
|
||||
user = await provider.update_user(user)
|
||||
logger.info("Promoted historical 'admin' account to system_role=admin: %s", email)
|
||||
user, created = await _get_or_create_user_by_username(body.username)
|
||||
|
||||
token = create_access_token(str(user.id), token_version=user.token_version)
|
||||
_set_session_cookie(response, token, request)
|
||||
@@ -434,6 +533,56 @@ async def login_by_username(request: Request, response: Response, body: Username
|
||||
)
|
||||
|
||||
|
||||
@router.post("/login/token", response_model=UsernameLoginResponse)
|
||||
async def login_by_token(
|
||||
request: Request,
|
||||
response: Response,
|
||||
token: str = Query(..., min_length=1, description="Upstream access token to exchange for a username."),
|
||||
):
|
||||
"""Token-exchange login.
|
||||
|
||||
Verifies an upstream access token by calling the configured
|
||||
``token_info_url`` (see ``auth_login.token_login``), reads the resolved
|
||||
username, then signs the matching local account in — auto-registering it
|
||||
when the username is not yet in the database. Gated by
|
||||
``auth_login.token_login.enabled``.
|
||||
|
||||
Returns the JWT access token in the response body and also sets it as a
|
||||
session cookie for browser callers.
|
||||
"""
|
||||
config = get_app_config().auth_login.token_login
|
||||
if not config.enabled:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="token 登录未开启")
|
||||
|
||||
client_ip = _get_client_ip(request)
|
||||
_check_rate_limit(client_ip)
|
||||
|
||||
try:
|
||||
username = await _fetch_username_from_token(token)
|
||||
except HTTPException as exc:
|
||||
if exc.status_code == status.HTTP_401_UNAUTHORIZED:
|
||||
_record_login_failure(client_ip)
|
||||
raise
|
||||
|
||||
_record_login_success(client_ip)
|
||||
|
||||
user, created = await _get_or_create_user_by_username(username)
|
||||
|
||||
access_token = create_access_token(str(user.id), token_version=user.token_version)
|
||||
_set_session_cookie(response, access_token, request)
|
||||
|
||||
return UsernameLoginResponse(
|
||||
access_token=access_token,
|
||||
token_type="bearer",
|
||||
expires_in=get_auth_config().token_expiry_days * 24 * 3600,
|
||||
user_id=str(user.id),
|
||||
email=user.email,
|
||||
system_role=user.system_role,
|
||||
needs_setup=user.needs_setup,
|
||||
created=created,
|
||||
)
|
||||
|
||||
|
||||
@router.post("/register", response_model=UserResponse, status_code=status.HTTP_201_CREATED)
|
||||
async def register(request: Request, response: Response, body: RegisterRequest):
|
||||
"""Register a new user account (always 'user' role).
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
"""HTML page favorites API.
|
||||
|
||||
A favorite bookmarks a generated ``scheduled_html_pages`` row and stores a
|
||||
reusable style/layout summary (extracted via GLM5 at favorite time) so a later
|
||||
``html_page`` task can be generated "in the style of" the favorite.
|
||||
|
||||
All routes are owner-scoped; the underlying logic lives on
|
||||
``ScheduledTaskService`` so GLM5 access and the page store stay in one place.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.gateway.deps import get_current_user
|
||||
|
||||
# Importing the ORM models here (at app-module load, before the lifespan runs
|
||||
# ``Base.metadata.create_all``) registers ``scheduled_html_pages`` +
|
||||
# ``html_page_favorites`` with the metadata so the tables are auto-created on
|
||||
# startup. Without this the store's models are only imported later (inside
|
||||
# ``langgraph_runtime``), after create_all has already run — yielding
|
||||
# "no such table: html_page_favorites".
|
||||
from deerflow.persistence.html_pages.model import ( # noqa: F401
|
||||
HtmlPageFavoriteRow,
|
||||
ScheduledHtmlPageRow,
|
||||
)
|
||||
from deerflow.runtime.scheduler import get_scheduled_task_service
|
||||
from deerflow.runtime.scheduler.service import DuplicateFavoriteError
|
||||
|
||||
router = APIRouter(prefix="/api/html-page-favorites", tags=["html-page-favorites"])
|
||||
|
||||
|
||||
class HtmlPageFavoriteCreateRequest(BaseModel):
|
||||
page_id: str = Field(..., description="scheduled_html_pages.id to bookmark")
|
||||
title: str | None = None
|
||||
description: str | None = None
|
||||
tags: list[str] | None = None
|
||||
|
||||
|
||||
async def _user_id(request: Request) -> str:
|
||||
user_id = await get_current_user(request)
|
||||
if not user_id:
|
||||
raise HTTPException(status_code=401, detail="Authentication required")
|
||||
return user_id
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def list_html_page_favorites(request: Request) -> list[dict[str, Any]]:
|
||||
service = get_scheduled_task_service()
|
||||
user_id = await _user_id(request)
|
||||
return await service.list_html_page_favorites(user_id)
|
||||
|
||||
|
||||
@router.post("")
|
||||
async def create_html_page_favorite(body: HtmlPageFavoriteCreateRequest, request: Request) -> dict[str, Any]:
|
||||
service = get_scheduled_task_service()
|
||||
user_id = await _user_id(request)
|
||||
try:
|
||||
favorite = await service.create_html_page_favorite(
|
||||
user_id,
|
||||
body.page_id,
|
||||
title=body.title,
|
||||
description=body.description,
|
||||
tags=body.tags,
|
||||
)
|
||||
except DuplicateFavoriteError:
|
||||
raise HTTPException(status_code=409, detail="该页面已收藏,请勿重复收藏")
|
||||
if favorite is None:
|
||||
raise HTTPException(status_code=404, detail="页面不存在或无权收藏")
|
||||
return favorite
|
||||
|
||||
|
||||
@router.get("/{favorite_id}")
|
||||
async def get_html_page_favorite(favorite_id: str, request: Request) -> dict[str, Any]:
|
||||
service = get_scheduled_task_service()
|
||||
user_id = await _user_id(request)
|
||||
favorite = await service.get_html_page_favorite(user_id, favorite_id)
|
||||
if favorite is None:
|
||||
raise HTTPException(status_code=404, detail=f"Favorite {favorite_id} not found")
|
||||
return favorite
|
||||
|
||||
|
||||
@router.delete("/{favorite_id}")
|
||||
async def delete_html_page_favorite(favorite_id: str, request: Request) -> dict[str, Any]:
|
||||
service = get_scheduled_task_service()
|
||||
user_id = await _user_id(request)
|
||||
deleted = await service.delete_html_page_favorite(user_id, favorite_id)
|
||||
if not deleted:
|
||||
raise HTTPException(status_code=404, detail=f"Favorite {favorite_id} not found")
|
||||
return {"success": True}
|
||||
@@ -243,6 +243,39 @@ async def public_get_run(run_id: str) -> dict[str, Any]:
|
||||
return _redact_run(run)
|
||||
|
||||
|
||||
def _html_payload(run_id: str, page: dict[str, Any]) -> dict[str, Any]:
|
||||
return {
|
||||
"run_id": run_id,
|
||||
"task_id": page.get("task_id"),
|
||||
"title": page.get("title"),
|
||||
"html": page.get("html_content"),
|
||||
"style_summary": page.get("style_summary"),
|
||||
"layout_summary": page.get("layout_summary"),
|
||||
"page_id": page.get("id"),
|
||||
"created_at": page.get("created_at"),
|
||||
}
|
||||
|
||||
|
||||
@router.get("/runs/{run_id}/html")
|
||||
async def public_get_run_html(run_id: str) -> dict[str, Any]:
|
||||
"""Return the generated HTML document for an ``html_page`` run (anonymous)."""
|
||||
service = get_scheduled_task_service()
|
||||
page = await service.get_html_page_for_run(run_id)
|
||||
if page is None:
|
||||
raise HTTPException(status_code=404, detail="该执行记录没有 HTML 页面")
|
||||
return _html_payload(run_id, page)
|
||||
|
||||
|
||||
@router.get("/{task_id}/latest-html")
|
||||
async def public_get_latest_html(task_id: str) -> dict[str, Any]:
|
||||
"""Return the most recent generated HTML page for a task (anonymous)."""
|
||||
service = get_scheduled_task_service()
|
||||
page = await service.get_latest_html_page_for_task(task_id)
|
||||
if page is None:
|
||||
raise HTTPException(status_code=404, detail="该任务暂无 HTML 页面")
|
||||
return _html_payload(str(page.get("run_id") or ""), page)
|
||||
|
||||
|
||||
@router.get("/runs/{run_id}/files/{file_index}")
|
||||
async def public_get_run_file(run_id: str, file_index: int) -> dict[str, Any]:
|
||||
"""Return a single result-file entry with its inline text content where
|
||||
|
||||
@@ -251,6 +251,29 @@ async def get_scheduled_task_run(run_id: str, request: Request) -> dict:
|
||||
return run
|
||||
|
||||
|
||||
@router.get("/runs/{run_id}/html")
|
||||
async def get_scheduled_task_run_html(run_id: str, request: Request) -> dict:
|
||||
"""Return the generated HTML document for an ``html_page`` run."""
|
||||
service = get_scheduled_task_service()
|
||||
user_id = await _user_id(request)
|
||||
run = await _load_run(service, request, run_id, user_id)
|
||||
if run is None:
|
||||
raise HTTPException(status_code=404, detail=f"Run {run_id} not found")
|
||||
page = await service.get_html_page_for_run(run_id)
|
||||
if page is None:
|
||||
raise HTTPException(status_code=404, detail="该执行记录没有 HTML 页面")
|
||||
return {
|
||||
"run_id": run_id,
|
||||
"task_id": page.get("task_id") or run.get("task_id"),
|
||||
"title": page.get("title"),
|
||||
"html": page.get("html_content"),
|
||||
"style_summary": page.get("style_summary"),
|
||||
"layout_summary": page.get("layout_summary"),
|
||||
"page_id": page.get("id"),
|
||||
"created_at": page.get("created_at"),
|
||||
}
|
||||
|
||||
|
||||
def _event_type_for_checkpoint_message(message: dict) -> str:
|
||||
"""Map a serialized LangChain message to the same ``event_type`` strings
|
||||
the run-event store uses, so the frontend's existing role labels keep
|
||||
|
||||
@@ -30,7 +30,7 @@ from deerflow.skills import Skill
|
||||
from deerflow.skills.cascade import cascade_skill_unpublished_or_deleted
|
||||
from deerflow.skills.installer import SkillAlreadyExistsError
|
||||
from deerflow.skills.security_scanner import scan_skill_content
|
||||
from deerflow.skills.storage import get_or_new_skill_storage
|
||||
from deerflow.skills.storage import SkillStorage, get_or_new_skill_storage
|
||||
from deerflow.skills.types import SKILL_MD_FILE, SkillCategory
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -210,6 +210,12 @@ class CustomSkillUpdateRequest(BaseModel):
|
||||
content: str = Field(..., description="Replacement SKILL.md content")
|
||||
|
||||
|
||||
class SkillDuplicateRequest(BaseModel):
|
||||
"""Request model for duplicating an existing skill into a new custom skill."""
|
||||
|
||||
new_name: str = Field(..., description="Name for the new custom skill copy (lowercase letters/digits, '-'/'_' separators)")
|
||||
|
||||
|
||||
class CustomSkillHistoryResponse(BaseModel):
|
||||
history: list[dict]
|
||||
|
||||
@@ -950,6 +956,121 @@ async def install_skill(
|
||||
raise HTTPException(status_code=500, detail=f"Failed to install skill: {str(e)}")
|
||||
|
||||
|
||||
def _rewrite_skill_frontmatter_name(content: str, new_name: str) -> str:
|
||||
"""Return *content* with the YAML-frontmatter ``name:`` field set to *new_name*.
|
||||
|
||||
A duplicated skill must declare the new name in its SKILL.md frontmatter or
|
||||
``validate_skill_markdown_content`` will reject it (frontmatter name must
|
||||
match the directory name). We rewrite only the first ``name:`` line inside
|
||||
the leading ``---`` block, leaving every other field byte-for-byte intact.
|
||||
"""
|
||||
match = re.match(r"^---\n(.*?)\n---", content, re.DOTALL)
|
||||
if not match:
|
||||
raise ValueError("SKILL.md is missing the leading YAML frontmatter block")
|
||||
fm = match.group(1)
|
||||
new_fm, replaced = re.subn(r"(?m)^(name:\s*).*$", rf"\g<1>{new_name}", fm, count=1)
|
||||
if replaced == 0:
|
||||
new_fm = f"name: {new_name}\n{fm}"
|
||||
return content[: match.start(1)] + new_fm + content[match.end(1) :]
|
||||
|
||||
|
||||
@router.post(
|
||||
"/skills/custom/{source_name}/duplicate",
|
||||
response_model=SkillInstallResponse,
|
||||
summary="Duplicate Skill",
|
||||
description=(
|
||||
"Copy an existing skill (built-in/public OR custom) into a brand-new custom "
|
||||
"skill named ``new_name``, owned by the caller and unpublished. All support "
|
||||
"files are copied verbatim; only the SKILL.md frontmatter name is rewritten. "
|
||||
"The source already passed the install-time security scan, so no re-scan is run."
|
||||
),
|
||||
)
|
||||
async def duplicate_skill(
|
||||
source_name: str,
|
||||
body: SkillDuplicateRequest,
|
||||
request: Request,
|
||||
config: AppConfig = Depends(get_config),
|
||||
skill_store: SkillStore = Depends(get_skill_store),
|
||||
) -> SkillInstallResponse:
|
||||
import shutil
|
||||
|
||||
try:
|
||||
user_id = _current_user_id(request)
|
||||
is_admin = _is_admin_user(request)
|
||||
storage = get_or_new_skill_storage(app_config=config)
|
||||
|
||||
source_name = SkillStorage.validate_skill_name(source_name.replace("\r\n", "").replace("\n", ""))
|
||||
new_name = SkillStorage.validate_skill_name(body.new_name.replace("\r\n", "").replace("\n", ""))
|
||||
|
||||
# Resolve the source directory (custom takes precedence over public, so a
|
||||
# user-shadowed copy is the one duplicated when both exist).
|
||||
if storage.custom_skill_exists(source_name):
|
||||
source_dir = storage.get_custom_skill_dir(source_name)
|
||||
elif storage.public_skill_exists(source_name):
|
||||
source_dir = storage.get_skills_root_path() / SkillCategory.PUBLIC.value / source_name
|
||||
else:
|
||||
raise HTTPException(status_code=404, detail=f"Skill '{source_name}' not found")
|
||||
|
||||
if new_name == source_name or storage.custom_skill_exists(new_name) or storage.public_skill_exists(new_name):
|
||||
raise HTTPException(status_code=409, detail=f"A skill named '{new_name}' already exists")
|
||||
if not is_admin:
|
||||
existing = await skill_store.get_any(new_name)
|
||||
if existing and existing.get("owner_user_id") not in (None, user_id):
|
||||
raise HTTPException(status_code=409, detail=f"Skill '{new_name}' already exists and is owned by another user")
|
||||
|
||||
dest_dir = storage.get_custom_skill_dir(new_name)
|
||||
# copytree refuses to overwrite, which is the behaviour we want — the
|
||||
# existence checks above already guarantee a clean target.
|
||||
shutil.copytree(source_dir, dest_dir)
|
||||
try:
|
||||
source_content = (dest_dir / SKILL_MD_FILE).read_text(encoding="utf-8")
|
||||
new_content = _rewrite_skill_frontmatter_name(source_content, new_name)
|
||||
SkillStorage.validate_skill_markdown_content(new_name, new_content)
|
||||
storage.write_custom_skill(new_name, SKILL_MD_FILE, new_content)
|
||||
except Exception:
|
||||
# Roll back the half-created directory so a failed copy never leaves
|
||||
# an unparseable skill on disk.
|
||||
shutil.rmtree(dest_dir, ignore_errors=True)
|
||||
raise
|
||||
|
||||
await skill_store.ensure_legacy({"name": new_name, "owner_user_id": user_id, "published": False})
|
||||
try:
|
||||
storage.append_history(
|
||||
new_name,
|
||||
{
|
||||
"action": "duplicate",
|
||||
"author": "human",
|
||||
"thread_id": None,
|
||||
"file_path": SKILL_MD_FILE,
|
||||
"prev_content": None,
|
||||
"new_content": new_content,
|
||||
"source_skill": source_name,
|
||||
},
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
from deerflow.skills.usage import mark_uploaded
|
||||
mark_uploaded(new_name)
|
||||
except Exception:
|
||||
pass
|
||||
await refresh_skills_system_prompt_cache_async()
|
||||
return SkillInstallResponse(
|
||||
success=True,
|
||||
skill_name=new_name,
|
||||
message=f"Skill '{source_name}' duplicated as '{new_name}'",
|
||||
overwritten=False,
|
||||
category=SkillCategory.CUSTOM.value,
|
||||
)
|
||||
except HTTPException:
|
||||
raise
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
except Exception as e:
|
||||
logger.error("Failed to duplicate skill %s: %s", source_name, e, exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=f"Failed to duplicate skill: {str(e)}")
|
||||
|
||||
|
||||
@router.post(
|
||||
"/skills/reconcile-custom",
|
||||
response_model=SkillReconcileResponse,
|
||||
|
||||
@@ -228,6 +228,8 @@ class ReferenceSourceResponse(BaseModel):
|
||||
id: str | None = None
|
||||
skillName: str = "backend-reference"
|
||||
mode: str = "citation"
|
||||
resultSetKey: str | None = None
|
||||
resultSetLabel: str | None = None
|
||||
title: str
|
||||
type: str = "result"
|
||||
snippet: str = ""
|
||||
|
||||
@@ -84,13 +84,23 @@ def normalize_input(raw_input: dict[str, Any] | None) -> dict[str, Any]:
|
||||
converted = []
|
||||
for msg in messages:
|
||||
if isinstance(msg, dict):
|
||||
role = msg.get("role", msg.get("type", "user"))
|
||||
content = msg.get("content", "")
|
||||
if role in ("user", "human"):
|
||||
converted.append(HumanMessage(content=content))
|
||||
else:
|
||||
# TODO: handle other message types (system, ai, tool)
|
||||
converted.append(HumanMessage(content=content))
|
||||
# Preserve additional_kwargs (e.g. uploaded `files`, `client_ts`,
|
||||
# `prompt_prefix`) and optional id/name so they round-trip through
|
||||
# the checkpoint to the UI and to UploadsMiddleware. Dropping them
|
||||
# here previously made uploaded files invisible in the chat and
|
||||
# broke the per-message ask-time timestamp.
|
||||
msg_kwargs: dict[str, Any] = {"content": content}
|
||||
extra = msg.get("additional_kwargs")
|
||||
if isinstance(extra, dict) and extra:
|
||||
msg_kwargs["additional_kwargs"] = extra
|
||||
if msg.get("id"):
|
||||
msg_kwargs["id"] = msg["id"]
|
||||
if msg.get("name"):
|
||||
msg_kwargs["name"] = msg["name"]
|
||||
# TODO: handle other message types (system, ai, tool); for now all
|
||||
# inbound dict messages are treated as human turns.
|
||||
converted.append(HumanMessage(**msg_kwargs))
|
||||
else:
|
||||
converted.append(msg)
|
||||
return {**raw_input, "messages": converted}
|
||||
|
||||
Reference in new issue
Block a user