Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions astrbot/core/computer/computer_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,21 @@ def parse_description(text: str) -> str:
break
if end_idx is None:
return ""
for line in lines[1:end_idx]:
if ":" not in line:
continue
key, value = line.split(":", 1)
if key.strip().lower() == "description":
return value.strip().strip('"').strip("'")
return ""

frontmatter = "\n".join(lines[1:end_idx])
try:
import yaml

Comment on lines +219 to +222
payload = yaml.safe_load(frontmatter) or dict()
except Exception:
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Outdated
return ""
Comment on lines +218 to +226
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在沙箱脚本中,使用 except Exception 来同时处理 yaml 模块可能未安装 (ImportError) 和 YAML 格式错误 (yaml.YAMLError) 的情况,虽然简洁,但过于宽泛,可能会屏蔽掉其他非预期的异常,不利于问题排查。建议将异常捕获拆分得更具体,分别处理导入错误和解析错误,这样代码意图更清晰,也更健壮。

Suggested change
try:
import yaml
payload = yaml.safe_load(frontmatter) or dict()
except Exception:
return ""
try:
import yaml
except ImportError:
return ""
try:
payload = yaml.safe_load(frontmatter) or dict()
except yaml.YAMLError:
return ""

if not isinstance(payload, dict):
return ""

description = payload.get("description", "")
if not isinstance(description, str):
return ""
return description.strip()


def load_managed_skills() -> list[str]:
Expand Down
22 changes: 15 additions & 7 deletions astrbot/core/skills/skill_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import shutil
import tempfile
import zipfile

import yaml
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path, PurePosixPath
Expand Down Expand Up @@ -69,13 +71,19 @@ def _parse_frontmatter_description(text: str) -> str:
break
if end_idx is None:
return ""
for line in lines[1:end_idx]:
if ":" not in line:
continue
key, value = line.split(":", 1)
if key.strip().lower() == "description":
return value.strip().strip('"').strip("'")
return ""

frontmatter = "\n".join(lines[1:end_idx])
try:
payload = yaml.safe_load(frontmatter) or {}
except yaml.YAMLError:
return ""
if not isinstance(payload, dict):
return ""

description = payload.get("description", "")
if not isinstance(description, str):
return ""
return description.strip()


# Regex for sanitizing paths used in prompt examples — only allow
Expand Down
33 changes: 33 additions & 0 deletions tests/test_skill_metadata_enrichment.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,39 @@ def test_parse_frontmatter_quoted_description():
assert _parse_frontmatter_description(text) == "quoted value"


def test_parse_frontmatter_multiline_literal_description():
text = (
"---\n"
"name: humanizer-zh\n"
"description: |\n"
" 去除文本中的 AI 生成痕迹。\n"
" 适用于编辑或审阅文本,使其听起来更自然。\n"
"---\n"
)
assert _parse_frontmatter_description(text) == (
"去除文本中的 AI 生成痕迹。\n适用于编辑或审阅文本,使其听起来更自然。"
)


def test_parse_frontmatter_multiline_folded_description():
text = (
"---\n"
"name: humanizer-zh\n"
"description: >\n"
" 去除文本中的 AI 生成痕迹。\n"
" 适用于编辑或审阅文本,使其听起来更自然。\n"
"---\n"
)
assert _parse_frontmatter_description(text) == (
"去除文本中的 AI 生成痕迹。 适用于编辑或审阅文本,使其听起来更自然。"
)


def test_parse_frontmatter_invalid_yaml_returns_empty():
text = "---\ndescription: [broken\n---\n"
assert _parse_frontmatter_description(text) == ""


# ---------- build_skills_prompt tests ----------


Expand Down
Loading