Merge remote-tracking branch 'origin/master' into xuhd
# Conflicts: # frontend-web/.env.production # frontend-web/.env.sit # frontend-web/package.json # offline-backend-20260512/backend/config.yaml
This commit is contained in:
commit
1668c1da87
177 files changed
+21030
-2738
No files matched your search
@@ -56,6 +56,44 @@ def _is_admin_user(request: Request) -> bool:
|
||||
return getattr(user, "system_role", None) == "admin"
|
||||
|
||||
|
||||
def _email_local_part(email: str | None) -> str | None:
|
||||
"""Return the username portion (before ``@``) of an email address."""
|
||||
if not email:
|
||||
return None
|
||||
at = email.find("@")
|
||||
return email[:at] if at > 0 else email
|
||||
|
||||
|
||||
async def _attach_owner_usernames(responses: list["SkillResponse"]) -> list["SkillResponse"]:
|
||||
"""Resolve each custom skill's ``owner_user_id`` to a friendly username.
|
||||
|
||||
One lookup per distinct owner id; tolerates deleted accounts and an
|
||||
uninitialised auth provider by leaving ``owner_username`` as None.
|
||||
"""
|
||||
owner_ids = {r.owner_user_id for r in responses if r.owner_user_id}
|
||||
if not owner_ids:
|
||||
return responses
|
||||
try:
|
||||
from app.gateway.deps import get_local_provider
|
||||
|
||||
provider = get_local_provider()
|
||||
except Exception:
|
||||
return responses
|
||||
names: dict[str, str] = {}
|
||||
for uid in owner_ids:
|
||||
try:
|
||||
user = await provider.get_user(uid)
|
||||
except Exception:
|
||||
user = None
|
||||
local = _email_local_part(getattr(user, "email", None)) if user is not None else None
|
||||
if local:
|
||||
names[uid] = local
|
||||
for resp in responses:
|
||||
if resp.owner_user_id:
|
||||
resp.owner_username = names.get(resp.owner_user_id)
|
||||
return responses
|
||||
|
||||
|
||||
def _can_view_skill_source(
|
||||
category: SkillCategory,
|
||||
owner_user_id: str | None,
|
||||
@@ -111,6 +149,7 @@ class SkillResponse(BaseModel):
|
||||
"""Response model for skill information."""
|
||||
|
||||
name: str = Field(..., description="Name of the skill")
|
||||
name_zh: str | None = Field(default=None, description="用户配置或大模型生成的中文名称")
|
||||
description: str = Field(..., description="Description of what the skill does")
|
||||
license: str | None = Field(None, description="License information")
|
||||
category: SkillCategory = Field(..., description="Category of the skill (public or custom)")
|
||||
@@ -125,6 +164,7 @@ class SkillResponse(BaseModel):
|
||||
featured: bool = Field(default=False, description="Whether this skill is pinned to the top of the list (sort only)")
|
||||
source: str = Field(default="upload", description="Origin: 'agent' for agent-created, 'upload' for user-uploaded")
|
||||
owner_user_id: str | None = Field(default=None, description="Custom skill owner (null = built-in or legacy)")
|
||||
owner_username: str | None = Field(default=None, description="Publisher username (email local part). Null for built-in skills.")
|
||||
published: bool = Field(default=False, description="Whether the owner has made this skill visible to other users")
|
||||
builtin: bool = Field(default=False, description="True when owner_user_id is null (no human owner)")
|
||||
detail: str | None = Field(default=None, description="Human-authored long-form detail text (Markdown), editable by owner/admin")
|
||||
@@ -267,11 +307,13 @@ def _skill_to_response(
|
||||
published = False
|
||||
builtin = skill.category != SkillCategory.CUSTOM
|
||||
detail: str | None = None
|
||||
name_zh: str | None = None
|
||||
if ownership is not None:
|
||||
owner_user_id = ownership.get("owner_user_id")
|
||||
published = bool(ownership.get("published", False))
|
||||
builtin = bool(ownership.get("builtin", owner_user_id is None))
|
||||
detail = ownership.get("detail")
|
||||
name_zh = ownership.get("name_zh")
|
||||
|
||||
display = None if display_override is _UNSET else display_override
|
||||
if display_override is _UNSET:
|
||||
@@ -284,6 +326,7 @@ def _skill_to_response(
|
||||
|
||||
return SkillResponse(
|
||||
name=skill.name,
|
||||
name_zh=name_zh,
|
||||
description=skill.description,
|
||||
license=skill.license,
|
||||
category=skill.category,
|
||||
@@ -697,7 +740,8 @@ async def _attach_and_filter_tags(
|
||||
resp.tags = assignments.get(resp.name, [])
|
||||
if search:
|
||||
needle = search.strip().lower()
|
||||
responses = [r for r in responses if needle in r.name.lower()]
|
||||
# Match the English name OR the Chinese display name so 中文 search works.
|
||||
responses = [r for r in responses if needle in r.name.lower() or (r.name_zh or "").lower().find(needle) >= 0]
|
||||
if tag_ids:
|
||||
wanted = set(tag_ids)
|
||||
responses = [r for r in responses if any(t.get("id") in wanted for t in r.tags)]
|
||||
@@ -726,7 +770,7 @@ async def list_skills(
|
||||
config: AppConfig = Depends(get_config),
|
||||
skill_store: SkillStore = Depends(get_skill_store),
|
||||
tag_store: TagStore = Depends(get_tag_store),
|
||||
search: str | None = Query(default=None, description="Case-insensitive fuzzy match on skill name"),
|
||||
search: str | None = Query(default=None, description="Case-insensitive fuzzy match on skill name or Chinese name (name_zh)"),
|
||||
tag_ids: list[str] | None = Query(default=None, description="Keep skills carrying any of these tags (OR)"),
|
||||
sort: str = Query(
|
||||
default="time_desc",
|
||||
@@ -786,6 +830,7 @@ async def list_skills(
|
||||
)
|
||||
)
|
||||
responses = await _attach_and_filter_tags(tag_store, responses, search, tag_ids)
|
||||
responses = await _attach_owner_usernames(responses)
|
||||
responses = _sort_skill_responses(responses, sort)
|
||||
return SkillsListResponse(skills=responses)
|
||||
except HTTPException:
|
||||
@@ -1176,6 +1221,7 @@ async def list_custom_skills(
|
||||
display_sample=adapter.get("sample") if adapter else None,
|
||||
)
|
||||
)
|
||||
responses = await _attach_owner_usernames(responses)
|
||||
return SkillsListResponse(skills=responses)
|
||||
except Exception as e:
|
||||
logger.error("Failed to list custom skills: %s", e, exc_info=True)
|
||||
@@ -2309,6 +2355,324 @@ async def update_skill_detail(
|
||||
raise HTTPException(status_code=500, detail=f"Failed to update skill detail: {str(e)}")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 技能中文名称(用户可配置 + 大模型自动生成 + 一键补充)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
_SKILL_NAME_ZH_SYSTEM_PROMPT = (
|
||||
"你是一名中文产品命名助手。根据给定技能的英文名与功能描述,生成一个简洁、自然、"
|
||||
"易懂的中文名称。要求:2 到 10 个汉字;准确概括技能用途;不要包含标点、引号、英文、"
|
||||
"解释或多余文字;只输出中文名称本身。"
|
||||
)
|
||||
|
||||
|
||||
class SkillNameZhModelError(Exception):
|
||||
"""The LLM call backing skill-name generation failed (a *model-side* problem).
|
||||
|
||||
Distinguishes a genuine model failure (timeout / API error / unbuildable
|
||||
model) — which must be surfaced to the user as "大模型报错导致无法补全" — from
|
||||
the model merely returning an unusable name (callers get ``None`` instead).
|
||||
"""
|
||||
|
||||
|
||||
def _clean_generated_name_zh(raw: str) -> str | None:
|
||||
"""Normalise an LLM-produced Chinese name: first line, strip quotes/punct, cap length."""
|
||||
if not raw:
|
||||
return None
|
||||
text = str(raw).strip()
|
||||
# Take the first non-empty line only.
|
||||
for line in text.splitlines():
|
||||
line = line.strip()
|
||||
if line:
|
||||
text = line
|
||||
break
|
||||
# Strip wrapping quotes / common trailing punctuation.
|
||||
text = text.strip().strip("\"'“”‘’`「」《》【】()()[]").strip()
|
||||
text = text.rstrip("。.,,、!!??;;::")
|
||||
if not text:
|
||||
return None
|
||||
# Keep it short — guard against the model returning a sentence.
|
||||
if len(text) > 20:
|
||||
text = text[:20]
|
||||
return text or None
|
||||
|
||||
|
||||
def _resolve_skill_name_zh_model_name(app_config: AppConfig, model_name: str | None) -> str | None:
|
||||
"""Pick the model name for skill-name generation.
|
||||
|
||||
An explicit ``model_name`` (user-selected in the UI) wins; otherwise fall
|
||||
back to the curator/security-scan model setting (itself falling back to the
|
||||
main model when unset).
|
||||
"""
|
||||
if model_name and model_name.strip():
|
||||
return model_name.strip()
|
||||
if app_config.skill_evolution and app_config.skill_evolution.curator:
|
||||
return app_config.skill_evolution.curator.model_name
|
||||
return None
|
||||
|
||||
|
||||
async def _generate_skill_name_zh(skill: Skill, app_config: AppConfig, model: Any = None, *, model_name: str | None = None) -> str | None:
|
||||
"""Generate a Chinese display name for ``skill`` via the LLM.
|
||||
|
||||
An explicit ``model_name`` lets the caller pick the model; otherwise it
|
||||
reuses the curator/security-scan model setting (falls back to the main
|
||||
model). Returns ``None`` only when the model replies but the output cannot be
|
||||
normalised into a short Chinese name. Raises :class:`SkillNameZhModelError`
|
||||
when the model itself fails (cannot be built / times out / API error), so the
|
||||
caller can tell the user it was a *model* problem.
|
||||
"""
|
||||
user_prompt = (
|
||||
f"技能英文名:{skill.name}\n"
|
||||
f"功能描述:{skill.description or '(无描述)'}\n\n"
|
||||
"请为该技能起一个中文名称。"
|
||||
)
|
||||
if model is None:
|
||||
try:
|
||||
from deerflow.models import create_chat_model
|
||||
|
||||
resolved = _resolve_skill_name_zh_model_name(app_config, model_name)
|
||||
model = create_chat_model(name=resolved, thinking_enabled=False, app_config=app_config)
|
||||
except Exception as e:
|
||||
logger.warning("Skill name_zh model build failed for %s", skill.name, exc_info=True)
|
||||
raise SkillNameZhModelError(str(e) or "无法初始化所选大模型") from e
|
||||
try:
|
||||
response = await asyncio.wait_for(
|
||||
model.ainvoke(
|
||||
[
|
||||
{"role": "system", "content": _SKILL_NAME_ZH_SYSTEM_PROMPT},
|
||||
{"role": "user", "content": user_prompt},
|
||||
],
|
||||
config={"run_name": "skill_name_zh"},
|
||||
),
|
||||
timeout=60,
|
||||
)
|
||||
except asyncio.TimeoutError as e:
|
||||
logger.warning("Skill name_zh LLM call timed out for %s", skill.name)
|
||||
raise SkillNameZhModelError("大模型响应超时(60 秒)") from e
|
||||
except Exception as e:
|
||||
logger.warning("Skill name_zh LLM call failed for %s", skill.name, exc_info=True)
|
||||
raise SkillNameZhModelError(str(e) or "大模型调用失败") from e
|
||||
return _clean_generated_name_zh(str(getattr(response, "content", "") or ""))
|
||||
|
||||
|
||||
async def _persist_skill_name_zh(
|
||||
skill: Skill,
|
||||
name_zh: str | None,
|
||||
skill_store: SkillStore,
|
||||
user_id: str,
|
||||
*,
|
||||
is_admin: bool,
|
||||
) -> str | None:
|
||||
"""Persist ``name_zh`` into the ``skills`` DB table (never the on-disk SKILL.md).
|
||||
|
||||
Authorization mirrors the ``detail`` annotation: a custom skill is editable by
|
||||
its owner or an admin; a built-in / public skill only by an admin (its DB row
|
||||
is materialized on demand). Returns the stored value.
|
||||
"""
|
||||
if skill.category == SkillCategory.CUSTOM:
|
||||
_record, mode = await _get_editable_skill_or_raise(skill_store, skill.name, user_id, is_admin=is_admin)
|
||||
if mode == "admin":
|
||||
updated = await skill_store.update_admin(skill.name, {"name_zh": name_zh})
|
||||
else:
|
||||
updated = await skill_store.update(skill.name, user_id, {"name_zh": name_zh})
|
||||
else:
|
||||
if not is_admin:
|
||||
raise HTTPException(status_code=403, detail="Only admin users can edit built-in skill Chinese names")
|
||||
if await skill_store.get_any(skill.name) is None:
|
||||
await skill_store.ensure_legacy({"name": skill.name, "owner_user_id": None, "published": True})
|
||||
updated = await skill_store.update_admin(skill.name, {"name_zh": name_zh})
|
||||
|
||||
if updated is None:
|
||||
raise HTTPException(status_code=500, detail="Failed to persist skill Chinese name")
|
||||
return updated.get("name_zh")
|
||||
|
||||
|
||||
class SkillNameZhRequest(BaseModel):
|
||||
"""Request body for setting a skill's Chinese display name."""
|
||||
|
||||
name_zh: str = Field(default="", description="中文名称;传空字符串表示清空")
|
||||
|
||||
|
||||
class SkillNameZhResponse(BaseModel):
|
||||
"""Response model for a single skill's Chinese-name update."""
|
||||
|
||||
name: str = Field(..., description="Skill name")
|
||||
name_zh: str | None = Field(default=None, description="中文名称(为空表示已清空)")
|
||||
|
||||
|
||||
class SkillNameZhBulkResponse(BaseModel):
|
||||
"""Response model for the one-click bulk fill of missing Chinese names."""
|
||||
|
||||
generated: list[SkillNameZhResponse] = Field(default_factory=list, description="本次成功生成中文名的技能")
|
||||
failed: list[str] = Field(default_factory=list, description="生成失败的技能英文名")
|
||||
skipped: int = Field(default=0, description="已有中文名而被跳过的技能数")
|
||||
model_error: str | None = Field(default=None, description="若有技能因大模型报错而失败,这里给出代表性的报错信息")
|
||||
|
||||
|
||||
class GenerateSkillNameZhRequest(BaseModel):
|
||||
"""Optional body for skill-name generation: pick the model to use."""
|
||||
|
||||
model_name: str | None = Field(default=None, description="可选:指定用于生成的大模型名称;留空使用默认配置")
|
||||
|
||||
|
||||
def _can_edit_skill_name_zh(skill: Skill, record: dict[str, Any] | None, user_id: str, *, is_admin: bool) -> bool:
|
||||
"""Whether ``user_id`` may set this skill's Chinese name (mirrors detail auth)."""
|
||||
if is_admin:
|
||||
return True
|
||||
if skill.category != SkillCategory.CUSTOM:
|
||||
return False
|
||||
return record is not None and record.get("owner_user_id") == user_id
|
||||
|
||||
|
||||
@router.put(
|
||||
"/skills/{skill_name}/name-zh",
|
||||
response_model=SkillNameZhResponse,
|
||||
summary="Set Skill Chinese Name",
|
||||
description="设置技能的中文名称(存数据库,不改源文件)。自定义技能由其所有者或管理员编辑;内置/公开技能仅管理员可编辑。传空字符串表示清空,回退到使用英文名。",
|
||||
)
|
||||
async def set_skill_name_zh(
|
||||
skill_name: str,
|
||||
body: SkillNameZhRequest,
|
||||
request: Request,
|
||||
config: AppConfig = Depends(get_config),
|
||||
skill_store: SkillStore = Depends(get_skill_store),
|
||||
) -> SkillNameZhResponse:
|
||||
try:
|
||||
skill_name = skill_name.replace("\r\n", "").replace("\n", "")
|
||||
user_id = _current_user_id(request)
|
||||
is_admin = _is_admin_user(request)
|
||||
skills = get_or_new_skill_storage(app_config=config).load_skills(enabled_only=False)
|
||||
skill = next((s for s in skills if s.name == skill_name), None)
|
||||
if skill is None:
|
||||
raise HTTPException(status_code=404, detail=f"Skill '{skill_name}' not found")
|
||||
name_zh = body.name_zh.strip() or None
|
||||
stored = await _persist_skill_name_zh(skill, name_zh, skill_store, user_id, is_admin=is_admin)
|
||||
return SkillNameZhResponse(name=skill_name, name_zh=stored)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error("Failed to set name_zh for %s: %s", skill_name, e, exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=f"Failed to set skill Chinese name: {str(e)}")
|
||||
|
||||
|
||||
@router.post(
|
||||
"/skills/{skill_name}/name-zh/generate",
|
||||
response_model=SkillNameZhResponse,
|
||||
summary="Generate Skill Chinese Name",
|
||||
description="用大模型为该技能生成一个中文名称并保存到数据库。可在请求体里通过 model_name 指定使用的大模型。",
|
||||
)
|
||||
async def generate_skill_name_zh(
|
||||
skill_name: str,
|
||||
request: Request,
|
||||
body: GenerateSkillNameZhRequest | None = None,
|
||||
config: AppConfig = Depends(get_config),
|
||||
skill_store: SkillStore = Depends(get_skill_store),
|
||||
) -> SkillNameZhResponse:
|
||||
try:
|
||||
body = body or GenerateSkillNameZhRequest()
|
||||
skill_name = skill_name.replace("\r\n", "").replace("\n", "")
|
||||
user_id = _current_user_id(request)
|
||||
is_admin = _is_admin_user(request)
|
||||
skills = get_or_new_skill_storage(app_config=config).load_skills(enabled_only=False)
|
||||
skill = next((s for s in skills if s.name == skill_name), None)
|
||||
if skill is None:
|
||||
raise HTTPException(status_code=404, detail=f"Skill '{skill_name}' not found")
|
||||
try:
|
||||
name_zh = await _generate_skill_name_zh(skill, config, model_name=body.model_name)
|
||||
except SkillNameZhModelError as e:
|
||||
raise HTTPException(status_code=502, detail=f"大模型报错,无法补全技能中文名:{e}")
|
||||
if not name_zh:
|
||||
raise HTTPException(status_code=502, detail="大模型未能生成有效的中文名称,请稍后重试或手动填写")
|
||||
stored = await _persist_skill_name_zh(skill, name_zh, skill_store, user_id, is_admin=is_admin)
|
||||
return SkillNameZhResponse(name=skill_name, name_zh=stored)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error("Failed to generate name_zh for %s: %s", skill_name, e, exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=f"Failed to generate skill Chinese name: {str(e)}")
|
||||
|
||||
|
||||
@router.post(
|
||||
"/skills/name-zh/generate-missing",
|
||||
response_model=SkillNameZhBulkResponse,
|
||||
summary="Bulk Generate Missing Skill Chinese Names",
|
||||
description="管理员一键为库中所有尚未配置中文名称的技能生成中文名(存数据库)。可在请求体里通过 model_name 指定使用的大模型。",
|
||||
)
|
||||
async def generate_missing_skill_names_zh(
|
||||
request: Request,
|
||||
body: GenerateSkillNameZhRequest | None = None,
|
||||
config: AppConfig = Depends(get_config),
|
||||
skill_store: SkillStore = Depends(get_skill_store),
|
||||
) -> SkillNameZhBulkResponse:
|
||||
try:
|
||||
from deerflow.models import create_chat_model
|
||||
|
||||
body = body or GenerateSkillNameZhRequest()
|
||||
user_id = _current_user_id(request)
|
||||
is_admin = _is_admin_user(request)
|
||||
# 一键补充技能中文名仅对管理员开放。
|
||||
if not is_admin:
|
||||
raise HTTPException(status_code=403, detail="仅管理员可一键补充技能中文名")
|
||||
skills = get_or_new_skill_storage(app_config=config).load_skills(enabled_only=False)
|
||||
|
||||
# Admin sees every row, so existing name_zh values come from the full set.
|
||||
records = {r["name"]: r for r in await skill_store.list_all()}
|
||||
|
||||
pending: list[Skill] = []
|
||||
skipped = 0
|
||||
for skill in skills:
|
||||
record = records.get(skill.name)
|
||||
if record and record.get("name_zh"):
|
||||
skipped += 1
|
||||
continue
|
||||
pending.append(skill)
|
||||
|
||||
generated: list[SkillNameZhResponse] = []
|
||||
failed: list[str] = []
|
||||
model_error: str | None = None
|
||||
if pending:
|
||||
# Build the model once and reuse it for every skill. A build failure
|
||||
# is a model problem — fail fast with a clear message rather than
|
||||
# silently producing zero results.
|
||||
try:
|
||||
resolved_name = _resolve_skill_name_zh_model_name(config, body.model_name)
|
||||
model = create_chat_model(name=resolved_name, thinking_enabled=False, app_config=config)
|
||||
except Exception as e:
|
||||
logger.warning("Failed to build model for bulk name_zh generation", exc_info=True)
|
||||
raise HTTPException(status_code=502, detail=f"大模型报错,无法补全技能中文名:{str(e) or '无法初始化所选大模型'}")
|
||||
|
||||
# Generate concurrently with a small bound to avoid overloading the model.
|
||||
semaphore = asyncio.Semaphore(4)
|
||||
|
||||
async def _one(skill: Skill) -> tuple[Skill, str | None, str | None]:
|
||||
async with semaphore:
|
||||
try:
|
||||
return skill, await _generate_skill_name_zh(skill, config, model=model), None
|
||||
except SkillNameZhModelError as e:
|
||||
return skill, None, str(e)
|
||||
|
||||
results = await asyncio.gather(*[_one(s) for s in pending])
|
||||
for skill, name_zh, err in results:
|
||||
if err and model_error is None:
|
||||
model_error = err
|
||||
if not name_zh:
|
||||
failed.append(skill.name)
|
||||
continue
|
||||
try:
|
||||
stored = await _persist_skill_name_zh(skill, name_zh, skill_store, user_id, is_admin=is_admin)
|
||||
generated.append(SkillNameZhResponse(name=skill.name, name_zh=stored))
|
||||
except HTTPException:
|
||||
# Permission / persistence issue for this one skill — skip it.
|
||||
failed.append(skill.name)
|
||||
|
||||
return SkillNameZhBulkResponse(generated=generated, failed=failed, skipped=skipped, model_error=model_error)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error("Failed to bulk-generate skill Chinese names: %s", e, exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=f"Failed to bulk-generate skill Chinese names: {str(e)}")
|
||||
|
||||
|
||||
class SkillPinRequest(BaseModel):
|
||||
pinned: bool = Field(..., description="Whether to pin or unpin the skill")
|
||||
|
||||
|
||||
Reference in new issue
Block a user