fix(agent): load split skill docs through read_skill

This commit is contained in:
jxxghp
2026-09-11 07:49:11 +08:00
parent b4cf40d46a
commit f5bc72ff44
32 changed files with 3527 additions and 203 deletions

View File

@@ -33,6 +33,10 @@ SKILL_CONTENT_TRUNCATION_MESSAGE = (
"SKILL.md exceeds 512 KiB; content contains only the first 512 KiB. "
"Do not use read_file to bypass this limit."
)
SUPPORTING_FILE_TRUNCATION_MESSAGE = (
"The requested supporting skill file exceeds 512 KiB; content contains only the first 512 KiB. "
"Do not use read_file to bypass this limit."
)
class SkillsState(AgentState):
@@ -56,6 +60,13 @@ class SkillToolInput(BaseModel):
...,
description="Skill name or id from the available skills list.",
)
file: Optional[str] = Field(
default=None,
description=(
"Optional supporting file path relative to the Skill directory. "
"Use this field with read_skill instead of read_file for a listed Skill document."
),
)
def _format_skill_annotations(skill: SkillMetadata) -> str:
@@ -142,7 +153,7 @@ You have access to a skills library for specialized MoviePilot workflows.
{skills_list}
When the user's request matches a skill description, call the `read_skill` tool with that skill name before taking task actions. Always use `read_skill`, never `read_file`, to load SKILL.md. The tool returns up to 512 KiB of SKILL.md plus the relative paths of all supporting files; if the body is truncated, do not use `read_file` to bypass the limit. Load only the listed supporting files that are actually needed. Do not create or rewrite skills unless the user explicitly asks for skill authoring.
When the user's request matches a skill description, call the `read_skill` tool with that skill name before taking task actions. Always use `read_skill`, never `read_file`, to load SKILL.md or a listed supporting Skill document. The first call returns up to 512 KiB of SKILL.md plus the relative paths of supporting files; load a needed supporting file by calling `read_skill` again with the same name and its relative `file` path. If a Skill document is truncated, do not use `read_file` to bypass the limit. Do not create or rewrite skills unless the user explicitly asks for skill authoring.
</skills_system>
"""
@@ -151,9 +162,10 @@ MOVIEPILOT_API_SKILL_NAME = "moviepilot-api"
SKILL_TOOL_DESCRIPTION = """Reads a MoviePilot skill by name or id.
Available skills:
Use the optional `file` argument to load a listed supporting Skill document through this tool.
{skills_catalog}
Call this tool when the user's task matches one of the available skills. It returns up to 512 KiB of SKILL.md content, metadata, and every supporting file path relative to the skill directory. If the content is truncated, do not use read_file to bypass the limit. Always use this tool instead of read_file for SKILL.md. Use read_file only for a listed supporting file when its content is needed. Do not use this for simple tasks that do not need a skill.
Call this tool when the user's task matches one of the available skills. Without `file`, it returns up to 512 KiB of SKILL.md content, metadata, and every supporting file path relative to the Skill directory. Set `file` to one listed relative path to load that supporting Skill document in the same authorized tool. If the content is truncated, do not use read_file to bypass this limit. Always use this tool instead of read_file for SKILL.md or listed supporting Skill documents. Do not use this for simple tasks that do not need a skill.
"""
@@ -330,6 +342,18 @@ class _SkillToolProvider:
decode_errors = "ignore" if truncated else "replace"
return bounded_content.decode("utf-8", errors=decode_errors), truncated
@classmethod
async def _read_supporting_file(cls, skill_path: str, relative_path: str) -> tuple[str, bool]:
"""读取 Skill 目录中已列出的辅助文档,避免绕过 Skill 权限边界。"""
supporting_files = await cls._list_supporting_files(skill_path)
if relative_path not in supporting_files:
raise ValueError(f"Skill supporting file is not listed: {relative_path}")
candidate = AsyncPath(str(Path(skill_path).parent / relative_path))
if await candidate.is_symlink() or not await candidate.is_file():
raise ValueError(f"Skill supporting file is unavailable: {relative_path}")
return await cls._read_skill_content(str(candidate))
@staticmethod
async def _list_supporting_files(skill_path: str) -> list[str]:
"""列出技能目录内除 SKILL.md 外的全部普通文件相对路径。"""
@@ -345,8 +369,8 @@ class _SkillToolProvider:
supporting_files.append(relative_path)
return sorted(supporting_files, key=str.casefold)
async def load_skill(self, name: str) -> str:
"""加载指定 Skill 的受限主体和辅助文件列表并返回 JSON 字符串。"""
async def load_skill(self, name: str, file: Optional[str] = None) -> str:
"""加载指定 Skill 主体或一个已列出的辅助文档并返回 JSON 字符串。"""
logger.info(f"加载 Skill: name={sanitize_for_host(name)}")
try:
skill = await self._find_skill(name)
@@ -359,8 +383,13 @@ class _SkillToolProvider:
ensure_ascii=False,
)
content, truncated = await self._read_skill_content(skill["path"])
supporting_files = await self._list_supporting_files(skill["path"])
if file is None:
content, truncated = await self._read_skill_content(skill["path"])
truncation_message = SKILL_CONTENT_TRUNCATION_MESSAGE if truncated else None
else:
content, truncated = await self._read_supporting_file(skill["path"], file)
truncation_message = SUPPORTING_FILE_TRUNCATION_MESSAGE if truncated else None
declared_operations = skill.get("allowed_api_operations", [])
if declared_operations:
self._api_scope_declared = True
@@ -377,10 +406,11 @@ class _SkillToolProvider:
"allowed_api_operations": declared_operations,
},
"content": content,
"loaded_file": file,
"content_limit_bytes": MAX_SKILL_CONTENT_BYTES,
"supporting_files": supporting_files,
"truncated": truncated,
"truncation_message": SKILL_CONTENT_TRUNCATION_MESSAGE if truncated else None,
"truncation_message": truncation_message,
},
ensure_ascii=False,
indent=2,

View File

@@ -74,7 +74,7 @@ You act as a proactive agent. Your goal is to fully resolve the user's media-rel
- If torrent search yields no useful result, check site scope, site health, and recognition quality before concluding that the resource is unavailable.
- Reuse the latest torrent search cache through `search.results` before `download.add` instead of repeating the same search unnecessarily.
- For a MoviePilot collection count or summary, use the corresponding `moviepilot_api` list operation with the smallest documented page; optional legacy pagination should use `page=1,count=1`. Read the exact `collection.total_count` or existing `data.total`. Do not query the database merely because list items or a tool preview were truncated. If the operation does not expose a total, continue its native pagination or report that the upstream total is unavailable instead of guessing.
- For administrator code discovery across local files, use `execute_command(action="run")` with `rg` and narrow globs or paths; large searches may be split with narrower globs, paths, or `rg --files` filters. Use `moviepilot_api` operation `storage.list` for one known local or remote storage directory, with its paging fields when more than the first page is needed. Always use `read_skill`, never `read_file`, to load a skill's SKILL.md; `read_skill` returns up to 512 KiB of the skill body and the relative paths of its supporting files in one call. If the body is truncated, do not use `read_file` to bypass the limit. Use `read_file` when an exact non-SKILL.md local file is known, including a supporting file listed by `read_skill`. If `read_file` reports truncation, continue with smaller `start_line` and `end_line` ranges instead of assuming the file ended.
- For administrator code discovery across local files, use `execute_command(action="run")` with `rg` and narrow globs or paths; large searches may be split with narrower globs, paths, or `rg --files` filters. Use `moviepilot_api` operation `storage.list` for one known local or remote storage directory, with its paging fields when more than the first page is needed. Always use `read_skill`, never `read_file`, to load a skill's SKILL.md or a listed supporting Skill document; `read_skill` returns up to 512 KiB of the skill body and the relative paths of its supporting files in one call. Call it again with the same Skill name and `file=<relative path>` when a supporting document is needed. If the body is truncated, do not use `read_file` to bypass the limit. Use `read_file` only for an exact non-SKILL.md local file that is not a Skill supporting document. If `read_file` reports truncation, continue with smaller `start_line` and `end_line` ranges instead of assuming the file ended.
- Read the relevant file before changing it, then pick the editing tool by scope. Use `apply_patch` when one logical change spans multiple files, adds new files, or deletes files: submit a single patch wrapped in `*** Begin Patch` / `*** End Patch` with `*** Add File:`, `*** Update File:`, and `*** Delete File:` sections; every context and removed line must match the current content exactly, and the whole patch is validated before any file is written. Use `edit_file` for a single localized exact replacement within one already-read file; make `old_text` unique with enough surrounding context, and use `replace_all=true` only when every match must change. Use `write_file` for one standalone new file; set `overwrite=true` only for an intentional full rewrite, and use `read_file(include_metadata=true)` plus `expected_sha256` when preserving the previously read version matters.
- When implementation depends on a Python or Node.js API, first identify the installed or locked dependency version from environment metadata, requirements, package manifests, lockfiles, local source, and type declarations. Use `rg` against the relevant package directory, `.venv`, or `node_modules` instead of scanning the entire project without bounds. If local evidence is insufficient, use `search_web` and then `browse_webpage` to read the matching version of the official documentation. Do not guess signatures from memory, mix examples from incompatible versions, or install a package only to inspect its API.
- Use structured file tools for source edits because they enforce file access boundaries and conflict checks. Never use shell redirection, inline scripts, or another tool to bypass a file-tool permission denial.