Skip to content

Commit 6b9b0a0

Browse files
authored
Merge pull request #601 from xerrors/feat/skills-update
feat: Enhance skill management with remote installation and file upload
2 parents 1f792fa + cc241f7 commit 6b9b0a0

File tree

13 files changed

+1021
-71
lines changed

13 files changed

+1021
-71
lines changed

backend/package/yuxi/agents/middlewares/summary_middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
from __future__ import annotations
99

10-
from pathlib import Path
1110
import uuid
1211
from collections.abc import Callable, Iterable, Mapping
1312
from functools import partial
13+
from pathlib import Path
1414
from typing import Any, Literal, cast, override
1515

1616
from langchain.agents import AgentState
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
from __future__ import annotations
2+
3+
import asyncio
4+
import os
5+
import re
6+
import shutil
7+
import tempfile
8+
from pathlib import Path
9+
from typing import TYPE_CHECKING
10+
11+
from sqlalchemy.ext.asyncio import AsyncSession
12+
from yuxi.services.skill_service import import_skill_dir, is_valid_skill_slug
13+
14+
if TYPE_CHECKING:
15+
from yuxi.storage.postgres.models_business import Skill
16+
17+
ANSI_ESCAPE_RE = re.compile(r"\x1B\[[0-?]*[ -/]*[@-~]")
18+
CONTROL_SEQUENCE_RE = re.compile(r"\x1B\][^\x07]*(?:\x07|\x1B\\)|\x1B[\(\)][A-Za-z0-9]")
19+
CLI_TIMEOUT_SECONDS = 300
20+
21+
22+
def _normalize_source(source: str) -> str:
23+
value = str(source or "").strip()
24+
if not value:
25+
raise ValueError("source 不能为空")
26+
if any(ch in value for ch in ("\n", "\r", "\x00")):
27+
raise ValueError("source 包含非法字符")
28+
return value
29+
30+
31+
def _normalize_skill_name(skill: str) -> str:
32+
value = str(skill or "").strip()
33+
if not is_valid_skill_slug(value):
34+
raise ValueError("skill 名称不合法")
35+
return value
36+
37+
38+
def _clean_cli_output(output: str) -> list[str]:
39+
cleaned = ANSI_ESCAPE_RE.sub("", output or "")
40+
cleaned = CONTROL_SEQUENCE_RE.sub("", cleaned)
41+
cleaned = cleaned.replace("\r", "\n")
42+
normalized_lines: list[str] = []
43+
for line in cleaned.splitlines():
44+
stripped = line.strip()
45+
stripped = re.sub(r"^[│┌└◇◒◐◓◑■●]+\s*", "", stripped)
46+
normalized_lines.append(stripped.strip())
47+
return normalized_lines
48+
49+
50+
def _parse_available_skills(output: str) -> list[dict[str, str]]:
51+
lines = _clean_cli_output(output)
52+
items: list[dict[str, str]] = []
53+
seen: set[str] = set()
54+
collecting = False
55+
56+
for idx, line in enumerate(lines):
57+
if not collecting:
58+
if "Available Skills" in line:
59+
collecting = True
60+
continue
61+
62+
if not line:
63+
continue
64+
if "Use --skill " in line:
65+
break
66+
if not is_valid_skill_slug(line):
67+
continue
68+
if line in seen:
69+
continue
70+
71+
description = ""
72+
next_index = idx + 1
73+
while next_index < len(lines):
74+
next_line = lines[next_index]
75+
next_index += 1
76+
if not next_line:
77+
continue
78+
if "Use --skill " in next_line:
79+
break
80+
if is_valid_skill_slug(next_line):
81+
break
82+
if next_line and next_line[0].isalpha():
83+
description = next_line
84+
else:
85+
continue
86+
break
87+
88+
seen.add(line)
89+
items.append({"name": line, "description": description})
90+
91+
return items
92+
93+
94+
async def _run_skills_cli(
95+
args: list[str],
96+
*,
97+
env: dict[str, str],
98+
cwd: str,
99+
) -> str:
100+
process = await asyncio.create_subprocess_exec(
101+
*args,
102+
cwd=cwd,
103+
env=env,
104+
stdout=asyncio.subprocess.PIPE,
105+
stderr=asyncio.subprocess.PIPE,
106+
)
107+
try:
108+
stdout, stderr = await asyncio.wait_for(process.communicate(), timeout=CLI_TIMEOUT_SECONDS)
109+
except TimeoutError:
110+
process.kill()
111+
await process.communicate()
112+
raise ValueError("skills CLI 执行超时") from None
113+
114+
output = (stdout or b"").decode("utf-8", errors="replace")
115+
error_output = (stderr or b"").decode("utf-8", errors="replace")
116+
combined = "\n".join(part for part in [output.strip(), error_output.strip()] if part)
117+
if process.returncode != 0:
118+
cleaned_lines = _clean_cli_output(combined)
119+
error_msg = "\n".join(line for line in cleaned_lines if line)[:500]
120+
raise ValueError(error_msg or "skills CLI 执行失败")
121+
return combined
122+
123+
124+
def _create_isolated_workdir() -> tuple[str, dict[str, str], str]:
125+
temp_home = tempfile.mkdtemp(prefix=".remote-skills-")
126+
env = os.environ.copy()
127+
env["HOME"] = temp_home
128+
workdir = str(Path(temp_home) / "workspace")
129+
Path(workdir).mkdir(parents=True, exist_ok=True)
130+
return temp_home, env, workdir
131+
132+
133+
async def list_remote_skills(source: str) -> list[dict[str, str]]:
134+
normalized_source = _normalize_source(source)
135+
136+
temp_home, env, workdir = _create_isolated_workdir()
137+
try:
138+
output = await _run_skills_cli(
139+
["npx", "-y", "skills", "add", normalized_source, "--list"],
140+
env=env,
141+
cwd=workdir,
142+
)
143+
finally:
144+
shutil.rmtree(temp_home, ignore_errors=True)
145+
146+
skills = _parse_available_skills(output)
147+
if not skills:
148+
raise ValueError("未发现可安装的 skills")
149+
return skills
150+
151+
152+
async def install_remote_skill(
153+
db: AsyncSession,
154+
*,
155+
source: str,
156+
skill: str,
157+
created_by: str | None,
158+
) -> Skill:
159+
normalized_source = _normalize_source(source)
160+
normalized_skill = _normalize_skill_name(skill)
161+
162+
temp_home, env, workdir = _create_isolated_workdir()
163+
try:
164+
available_skills = _parse_available_skills(
165+
await _run_skills_cli(
166+
["npx", "-y", "skills", "add", normalized_source, "--list"],
167+
env=env,
168+
cwd=workdir,
169+
)
170+
)
171+
available_names = {item["name"] for item in available_skills}
172+
if normalized_skill not in available_names:
173+
raise ValueError(f"远程仓库中不存在 skill: {normalized_skill}")
174+
175+
await _run_skills_cli(
176+
[
177+
"npx",
178+
"-y",
179+
"skills",
180+
"add",
181+
normalized_source,
182+
"--skill",
183+
normalized_skill,
184+
"-g",
185+
"-y",
186+
"--copy",
187+
],
188+
env=env,
189+
cwd=workdir,
190+
)
191+
192+
base_dir = Path(temp_home).resolve()
193+
skills_dir = base_dir / ".agents" / "skills"
194+
# Scan for the installed skill directory rather than constructing the path
195+
# from user input, to avoid path traversal concerns
196+
installed_dir = None
197+
if skills_dir.is_dir():
198+
for candidate in skills_dir.iterdir():
199+
if candidate.name == normalized_skill and candidate.is_dir():
200+
installed_dir = candidate
201+
break
202+
if installed_dir is None:
203+
raise ValueError("skills CLI 未生成预期的技能目录")
204+
205+
return await import_skill_dir(
206+
db,
207+
source_dir=installed_dir,
208+
created_by=created_by,
209+
)
210+
finally:
211+
shutil.rmtree(temp_home, ignore_errors=True)

0 commit comments

Comments
 (0)