Skills: manage and import your own, switch on per chat, search instead of listing

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Richard ZinkandClaude Sonnet 5.5 committed 2026-11-17 14:28:13 +01:00
1 parent e150de3c93
commit a84a3acaa0
20 files changed
+943 -26

No files matched your search

+18 -2
View File
@@ -72,6 +72,10 @@ pub fn router(app: AppState) -> Router {
.route("/agents/tools", get(agent_tools))
.route("/agents/{name}", delete(delete_agent))
.route("/commands", get(commands))
.route("/skills", get(crate::skills_api::list))
.route("/skills/github/scan", post(crate::skills_api::scan))
.route("/skills/github/import", post(crate::skills_api::import))
.route("/skills/{name}", get(crate::skills_api::get).put(crate::skills_api::put).delete(crate::skills_api::delete))
.route("/transcribe", post(transcribe))
.route("/sessions", get(list_sessions).post(create_session))
.route("/sessions/search", get(search_sessions))
@@ -532,7 +536,7 @@ async fn commands(Extension(user): Extension<User>, State(app): State<AppState>,
let (root, cwd) = scope(&app, &user, q.cwd);
Json(json!({
"commands": prompts::commands(&app, &root, &cwd),
"skills": prompts::skills(&app, &root, &cwd),
"skills": prompts::skills(&app, &root, &cwd, Some(&user.id)),
}))
}
@@ -676,6 +680,7 @@ async fn create_session(Extension(user): Extension<User>, State(app): State<AppS
summary: String::new(),
color: dstr("color").filter(|c| codecraft_core::types::SESSION_COLORS.contains(&c.as_str())),
icon: dstr("icon").filter(|c| codecraft_core::types::SESSION_ICONS.contains(&c.as_str())),
skills: Vec::new(),
};
let s = app.save_session(s)?;
Ok(Json(json!(s)))
@@ -973,6 +978,8 @@ struct UpdateSession {
color: Option<String>,
/// Icon ("" removes it).
icon: Option<String>,
/// Skills switched on for this chat (names).
skills: Option<Vec<String>>,
}
async fn update_session(Extension(user): Extension<User>, State(app): State<AppState>, Path(id): Path<String>, Json(body): Json<UpdateSession>) -> ApiResult {
@@ -1036,6 +1043,15 @@ async fn update_session(Extension(user): Extension<User>, State(app): State<AppS
if let Some(i) = body.icon {
s.icon = (!i.is_empty()).then_some(i);
}
if let Some(list) = body.skills {
let mut seen = Vec::new();
for n in list.into_iter().map(|n| n.trim().to_string()).filter(|n| !n.is_empty() && n.len() <= 64) {
if !seen.contains(&n) && seen.len() < 20 {
seen.push(n);
}
}
s.skills = seen;
}
})?;
Ok(Json(json!(s)))
}
@@ -2436,7 +2452,7 @@ async fn job_assistant(Extension(user): Extension<User>, State(app): State<AppSt
telegram_quiet: true,
summary: String::new(),
color: None,
icon: None,
icon: None, skills: Vec::new(),
};
let s = app.save_session(s)?;
st.assistant_session = Some(s.id.clone());
+2 -2
View File
@@ -36,7 +36,7 @@ pub async fn config(Extension(user): Extension<User>, State(app): State<AppState
.filter(|c| c.description.len() < 400)
.map(|c| json!({"name": c.name, "description": c.description, "body": c.body, "agent": c.agent}))
.collect();
let skills: Vec<Value> = crate::prompts::skills(&app, &root, &root).into_iter().map(|s| json!({"name": s.name, "description": s.description})).collect();
let skills: Vec<Value> = crate::prompts::skills(&app, &root, &root, Some(&user.id)).into_iter().map(|s| json!({"name": s.name, "description": s.description})).collect();
let (direct, deferred) = app.mcp.tools(&cfg, Some(&user));
let mut mcp_tools: Vec<ToolDef> = direct.iter().map(|t| t.def()).collect();
if !deferred.is_empty() {
@@ -69,7 +69,7 @@ pub async fn config(Extension(user): Extension<User>, State(app): State<AppState
/// A server skill with its supporting text files (templates).
pub async fn skill(Extension(user): Extension<User>, State(app): State<AppState>, Path(name): Path<String>) -> ApiResult {
let root = app.user_roots(Some(&user.id)).into_iter().next().unwrap_or_else(|| app.workspace());
let sk = crate::prompts::skills(&app, &root, &root).into_iter().find(|s| s.name == name).ok_or_else(|| ApiError(StatusCode::NOT_FOUND, "skill not found".into()))?;
let sk = crate::prompts::skills(&app, &root, &root, Some(&user.id)).into_iter().find(|s| s.name == name).ok_or_else(|| ApiError(StatusCode::NOT_FOUND, "skill not found".into()))?;
let dir = sk.path.parent().map(PathBuf::from).unwrap_or_default();
let content = std::fs::read_to_string(&sk.path)?;
let mut files = Vec::new();
+3 -3
View File
@@ -881,7 +881,7 @@ fn start_phase(app: &Arc<App>, user: &User, cfg: &RepoCfg, issue: i64, phase: &s
telegram_quiet: true,
summary: String::new(),
color: None,
icon: None,
icon: None, skills: Vec::new(),
};
let session = app.save_session(session)?;
let run_id = crate::types::new_id();
@@ -1462,7 +1462,7 @@ pub async fn start_issue_chat(app: &Arc<App>, user: &User, cfg: &RepoCfg, model:
telegram_quiet: true,
summary: String::new(),
color: None,
icon: None,
icon: None, skills: Vec::new(),
};
app.save_session(session)
}
@@ -1583,7 +1583,7 @@ pub fn start_spec_chat(app: &Arc<App>, user: &User, model: &str) -> Result<crate
telegram_quiet: true,
summary: String::new(),
color: None,
icon: None,
icon: None, skills: Vec::new(),
};
app.save_session(session)
}
+1 -1
View File
@@ -335,7 +335,7 @@ pub async fn run_with(app: &Arc<App>, job: &Job, trigger: &str, payload: Option<
telegram_quiet: true,
summary: String::new(),
color: None,
icon: None,
icon: None, skills: Vec::new(),
};
let session = app.save_session(session)?;
app.store.job_started(&job.id, &session.id, now)?;
+1
View File
@@ -35,6 +35,7 @@ mod push;
mod provider;
mod reverse;
mod runner;
mod skills_api;
mod sandbox;
mod shells;
mod sshkeys;
+1 -1
View File
@@ -211,7 +211,7 @@ mod tests {
let session = crate::types::Session {
id: crate::types::new_id(), title: String::new(), cwd: dir.join("ws").display().to_string(), model: "x/y".into(), agent: "build".into(), permission_mode: "bypass".into(),
parent_id: None, owner_id: None, created_at: now, updated_at: now, todos: Value::Null, last_usage: None, clean_room: None, phase: None, worktree: None,
pinned: false, archived: false, folder: None, instructions: String::new(), telegram_quiet: true, summary: String::new(), color: None, icon: None,
pinned: false, archived: false, folder: None, instructions: String::new(), telegram_quiet: true, summary: String::new(), color: None, icon: None, skills: Vec::new(),
};
let session = app.save_session(session).unwrap();
let reg = register(&session.id);
+122 -12
View File
@@ -285,11 +285,16 @@ pub fn system_prompt(app: &App, agent: &Agent, root: &Path, cwd: &Path, model: &
s.push_str(&format!("- {server} ({n} tools, e.g. {})\n", names.join(", ")));
}
}
let skills = skills(app, root, cwd);
let skills = skills(app, root, cwd, owner.map(|u| u.id.as_str()));
if !skills.is_empty() {
s.push_str("\n# Skills\nLoad a skill with the skill tool when the task matches its description:\n");
for sk in &skills {
s.push_str(&format!("- {}: {}\n", sk.name, sk.description));
if skills.len() <= SKILL_LIST_MAX {
s.push_str("\n# Skills\nLoad a skill with the skill tool when the task matches its description:\n");
for sk in &skills {
s.push_str(&format!("- {}: {}\n", sk.name, sk.description.chars().take(160).collect::<String>()));
}
} else {
// A large library must not fill the context: only the count; the agent searches on demand.
s.push_str(&format!("\n# Skills\n{} skills (packaged instructions for kinds of task) are available but not listed. Find one with the skill tool (query=<keywords>), then load it with name=<skill>.\n", skills.len()));
}
}
if let Some(u) = owner {
@@ -381,23 +386,91 @@ pub struct Skill {
pub name: String,
pub description: String,
pub path: PathBuf,
/// Further files of the skill (relative paths, without SKILL.md).
pub files: Vec<String>,
/// Created by the user in the UI (editable there).
pub user: bool,
}
pub fn skills(app: &App, root: &Path, cwd: &Path) -> Vec<Skill> {
/// Where a user's own skills live: `<data>/user-skills/<user id>/<name>/SKILL.md` (+ more files).
pub fn user_skills_dir(app: &App, owner_id: &str) -> PathBuf {
app.data_dir().join("user-skills").join(owner_id)
}
/// Relative paths of all files below `dir` (depth <= 5, at most 200), without `SKILL.md` at the top.
pub fn skill_files(dir: &Path) -> Vec<String> {
fn walk(base: &Path, dir: &Path, depth: usize, out: &mut Vec<String>) {
let Ok(entries) = std::fs::read_dir(dir) else { return };
let mut entries: Vec<_> = entries.flatten().collect();
entries.sort_by_key(|e| e.file_name());
for e in entries {
if out.len() >= 200 {
return;
}
let p = e.path();
let Ok(ty) = e.file_type() else { continue };
if ty.is_dir() && depth < 5 {
walk(base, &p, depth + 1, out);
} else if ty.is_file() {
if let Ok(rel) = p.strip_prefix(base) {
let rel = rel.to_string_lossy().to_string();
if rel != "SKILL.md" {
out.push(rel);
}
}
}
}
}
let mut out = Vec::new();
walk(dir, dir, 0, &mut out);
out
}
pub fn skills(app: &App, root: &Path, cwd: &Path, owner_id: Option<&str>) -> Vec<Skill> {
let mut map = BTreeMap::new();
for dir in resource_dirs(app, root, cwd, "skills") {
let mut dirs: Vec<(PathBuf, bool)> = resource_dirs(app, root, cwd, "skills").into_iter().map(|d| (d, false)).collect();
if let Some(id) = owner_id {
dirs.push((user_skills_dir(app, id), true));
}
for (dir, user) in dirs {
let Ok(entries) = std::fs::read_dir(&dir) else { continue };
for e in entries.flatten() {
let p = e.path().join("SKILL.md");
let Ok(text) = std::fs::read_to_string(&p) else { continue };
let (meta, _) = frontmatter(&text);
let name = meta.get("name").cloned().unwrap_or_else(|| e.file_name().to_string_lossy().into());
map.insert(name.clone(), Skill { name, description: meta.get("description").cloned().unwrap_or_default(), path: p });
map.insert(name.clone(), Skill { name, description: meta.get("description").cloned().unwrap_or_default(), files: skill_files(&e.path()), path: p, user });
}
}
map.into_values().collect()
}
/// Adds the skills the user switched on for this chat: their full instructions are part of every turn.
pub fn with_active_skills(app: &App, mut system: String, active: &[String], root: &Path, cwd: &Path, owner_id: Option<&str>) -> String {
if active.is_empty() {
return system;
}
let all = skills(app, root, cwd, owner_id);
let mut block = String::new();
for name in active {
let Some(sk) = all.iter().find(|s| &s.name == name) else { continue };
let Ok(text) = std::fs::read_to_string(&sk.path) else { continue };
let (_, body) = frontmatter(&text);
let body: String = body.chars().take(40_000).collect();
block.push_str(&format!("\n## Skill: {}\n{}\n", sk.name, body.trim()));
if !sk.files.is_empty() {
block.push_str(&format!("\nFurther files of this skill (read them with the skill tool: name=\"{}\", file=<path>): {}\n", sk.name, sk.files.join(", ")));
}
}
if !block.is_empty() {
system.push_str(&format!("\n\n# Active skills\nThe user switched these skills on for this chat. Follow them for every task they cover.\n{block}"));
}
system
}
/// More skills than this are not listed in the system prompt (the agent searches instead).
const SKILL_LIST_MAX: usize = 12;
pub struct SkillTool;
#[async_trait]
@@ -406,23 +479,60 @@ impl Tool for SkillTool {
"skill"
}
fn description(&self) -> String {
"Load a skill (packaged instructions for a kind of task) by name. Follow the returned instructions.".into()
"Load a skill (packaged instructions for a kind of task) by name and follow the returned instructions. `query` (keywords) lists matching skills instead; `file` reads one of the skill's further files.".into()
}
fn schema(&self) -> Value {
json!({"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"]})
json!({"type": "object", "properties": {
"name": {"type": "string"},
"file": {"type": "string", "description": "relative path of a further file of the skill"},
"query": {"type": "string", "description": "keywords: lists the matching skills instead of loading one"}
}})
}
fn read_only(&self) -> bool {
true
}
async fn run(&self, input: Value, ctx: &ToolCtx) -> Result<ToolOutput> {
let owner = ctx.app.store.get_session(&ctx.session_id).ok().flatten().and_then(|s| s.owner_id).or_else(|| ctx.owner_id.clone());
let all = skills(&ctx.app, &ctx.root(), &ctx.cwd, owner.as_deref());
if let Some(q) = input.get("query").and_then(|v| v.as_str()).filter(|q| !q.trim().is_empty()) {
let words: Vec<String> = q.to_lowercase().split_whitespace().map(str::to_string).collect();
let mut hits: Vec<(usize, &Skill)> = all
.iter()
.map(|s| {
let hay = format!("{} {}", s.name, s.description).to_lowercase();
(words.iter().filter(|w| hay.contains(w.as_str())).count(), s)
})
.filter(|(n, _)| *n > 0)
.collect();
hits.sort_by_key(|(n, s)| (std::cmp::Reverse(*n), s.name.clone()));
if hits.is_empty() {
return Ok(ToolOutput::text(format!("No skill matches '{q}' ({} skills available).", all.len())));
}
let lines: Vec<String> = hits.iter().take(15).map(|(_, s)| format!("- {}: {}", s.name, s.description.chars().take(200).collect::<String>())).collect();
return Ok(ToolOutput::text(format!("Matching skills (load one with name=<skill>):\n{}", lines.join("\n"))));
}
let name = arg_str(&input, "name")?;
let Some(sk) = skills(&ctx.app, &ctx.root(), &ctx.cwd).into_iter().find(|s| s.name == name) else {
let Some(sk) = all.into_iter().find(|s| s.name == name) else {
return Ok(ToolOutput::error(format!("unknown skill '{name}'")));
};
let dir = sk.path.parent().unwrap_or(Path::new("."));
if let Some(file) = input.get("file").and_then(|v| v.as_str()).filter(|f| !f.trim().is_empty()) {
let rel = Path::new(file.trim());
if rel.is_absolute() || rel.components().any(|c| !matches!(c, std::path::Component::Normal(_))) {
return Ok(ToolOutput::error("file must be a relative path inside the skill"));
}
let Ok(real) = std::fs::canonicalize(dir.join(rel)) else { return Ok(ToolOutput::error(format!("no file '{file}' in skill '{name}'"))) };
if !real.starts_with(std::fs::canonicalize(dir)?) || !real.is_file() {
return Ok(ToolOutput::error(format!("no file '{file}' in skill '{name}'")));
}
let mut text = std::fs::read_to_string(&real).unwrap_or_else(|_| "(binary file)".into());
text.truncate(text.char_indices().nth(100_000).map(|(i, _)| i).unwrap_or(text.len()));
return Ok(ToolOutput::text(text));
}
let text = std::fs::read_to_string(&sk.path)?;
let (_, body) = frontmatter(&text);
let dir = sk.path.parent().unwrap_or(Path::new("."));
Ok(ToolOutput::text(format!("Skill '{name}' (base directory: {}):\n\n{body}", dir.display())))
let files = if sk.files.is_empty() { String::new() } else { format!("\n\nFurther files (read with name=\"{name}\" and file=<path>): {}", sk.files.join(", ")) };
Ok(ToolOutput::text(format!("Skill '{name}' (base directory: {}):\n\n{body}{files}", dir.display())))
}
}
+2 -2
View File
@@ -335,7 +335,7 @@ pub async fn run_session(app: Arc<App>, session_id: &str, cancel: CancellationTo
let owner = session.owner_id.as_deref().and_then(|id| app.store.user(id).ok().flatten());
let tools = available_tools(&app, &agent, owner.as_ref());
let req = ChatRequest {
system: prompts::with_chat_instructions(prompts::system_prompt(&app, &agent, &root, &cwd, &model_spec, &session.permission_mode, owner.as_ref()), &session.instructions),
system: prompts::with_active_skills(&app, prompts::with_chat_instructions(prompts::system_prompt(&app, &agent, &root, &cwd, &model_spec, &session.permission_mode, owner.as_ref()), &session.instructions), &session.skills, &root, &cwd, session.owner_id.as_deref()),
messages: history,
tools: tools.iter().map(|t| t.def()).collect(),
max_tokens,
@@ -903,7 +903,7 @@ fn create_child(app: &Arc<App>, ctx: &ToolCtx, agent_name: &str, description: &s
telegram_quiet: false,
summary: String::new(),
color: None,
icon: None,
icon: None, skills: Vec::new(),
};
let child = app.save_session(child)?;
let msg = Message::new(Role::User, vec![Part::Text { text: prompt }]);
+326
View File
@@ -0,0 +1,326 @@
//! The user's own skills: a folder per skill with `SKILL.md` and further text files, kept in
//! `<data>/user-skills/<user id>/<name>/`; created in the UI or imported from a GitHub repository.
use crate::api::{ApiError, ApiResult, AppState};
use crate::prompts::{frontmatter, skill_files, user_skills_dir};
use crate::store::User;
use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::{Extension, Json};
use serde::Deserialize;
use serde_json::{Value, json};
use std::path::PathBuf;
const MAX_FILES: usize = 100;
const MAX_FILE: usize = 200_000;
const MAX_TOTAL: usize = 2_000_000;
fn bad(m: impl Into<String>) -> ApiError {
ApiError(StatusCode::BAD_REQUEST, m.into())
}
/// A skill name is a folder name: lowercase letters, digits, `-` and `_`.
pub fn valid_name(n: &str) -> bool {
!n.is_empty() && n.len() <= 64 && n.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-' || c == '_') && !n.starts_with(['-', '_'])
}
/// Turn any title into a valid skill name.
pub fn slug(s: &str) -> String {
let mut out = String::new();
for c in s.to_lowercase().chars() {
if c.is_ascii_alphanumeric() {
out.push(c);
} else if !out.ends_with('-') && !out.is_empty() {
out.push('-');
}
}
out.trim_matches('-').chars().take(64).collect()
}
fn valid_rel(p: &str) -> bool {
let rel = std::path::Path::new(p);
!p.is_empty() && p.len() <= 200 && !rel.is_absolute() && rel.components().all(|c| matches!(c, std::path::Component::Normal(_)))
}
fn dir_of(app: &AppState, user: &User, name: &str) -> Result<PathBuf, ApiError> {
if !valid_name(name) {
return Err(bad("invalid skill name (lowercase letters, digits, - and _)"));
}
Ok(user_skills_dir(app, &user.id).join(name))
}
#[derive(Deserialize, Clone)]
pub struct FileBody {
path: String,
content: String,
}
/// Replace the files of a skill by `files` (must contain `SKILL.md`).
fn write_skill(dir: &std::path::Path, files: &[FileBody]) -> Result<(), ApiError> {
if !files.iter().any(|f| f.path == "SKILL.md" && !f.content.trim().is_empty()) {
return Err(bad("SKILL.md is required"));
}
if files.len() > MAX_FILES {
return Err(bad(format!("at most {MAX_FILES} files")));
}
let mut total = 0;
for f in files {
if !valid_rel(&f.path) {
return Err(bad(format!("invalid file path '{}'", f.path)));
}
if f.content.len() > MAX_FILE {
return Err(bad(format!("'{}' is too large (max {} KB)", f.path, MAX_FILE / 1000)));
}
total += f.content.len();
}
if total > MAX_TOTAL {
return Err(bad("the skill is too large (max 2 MB)"));
}
let tmp = dir.with_extension("saving");
let _ = std::fs::remove_dir_all(&tmp);
for f in files {
let p = tmp.join(&f.path);
if let Some(parent) = p.parent() {
std::fs::create_dir_all(parent)?;
}
std::fs::write(&p, &f.content)?;
}
let _ = std::fs::remove_dir_all(dir);
std::fs::rename(&tmp, dir)?;
Ok(())
}
pub async fn list(Extension(user): Extension<User>, State(app): State<AppState>) -> ApiResult {
let base = user_skills_dir(&app, &user.id);
let mut out = Vec::new();
for e in std::fs::read_dir(&base).into_iter().flatten().flatten() {
let Ok(text) = std::fs::read_to_string(e.path().join("SKILL.md")) else { continue };
let (meta, _) = frontmatter(&text);
out.push(json!({
"name": e.file_name().to_string_lossy(),
"description": meta.get("description").cloned().unwrap_or_default(),
"files": skill_files(&e.path()),
}));
}
out.sort_by(|a, b| a["name"].as_str().cmp(&b["name"].as_str()));
Ok(Json(json!({"skills": out})))
}
pub async fn get(Extension(user): Extension<User>, State(app): State<AppState>, Path(name): Path<String>) -> ApiResult {
let dir = dir_of(&app, &user, &name)?;
let main = std::fs::read_to_string(dir.join("SKILL.md")).map_err(|_| ApiError(StatusCode::NOT_FOUND, "skill not found".into()))?;
let mut files = vec![json!({"path": "SKILL.md", "content": main})];
for f in skill_files(&dir) {
let content = std::fs::read_to_string(dir.join(&f)).unwrap_or_default();
files.push(json!({"path": f, "content": content}));
}
Ok(Json(json!({"name": name, "files": files})))
}
#[derive(Deserialize)]
pub struct PutBody {
files: Vec<FileBody>,
}
pub async fn put(Extension(user): Extension<User>, State(app): State<AppState>, Path(name): Path<String>, Json(b): Json<PutBody>) -> ApiResult {
let dir = dir_of(&app, &user, &name)?;
std::fs::create_dir_all(user_skills_dir(&app, &user.id))?;
write_skill(&dir, &b.files)?;
app.audit(Some(&user), "skill.save", name, "");
Ok(Json(json!({"ok": true})))
}
pub async fn delete(Extension(user): Extension<User>, State(app): State<AppState>, Path(name): Path<String>) -> ApiResult {
let dir = dir_of(&app, &user, &name)?;
if !dir.join("SKILL.md").exists() {
return Err(ApiError(StatusCode::NOT_FOUND, "skill not found".into()));
}
std::fs::remove_dir_all(&dir)?;
app.audit(Some(&user), "skill.delete", name, "");
Ok(Json(json!({"ok": true})))
}
// --- GitHub import -------------------------------------------------------------------------
struct Source {
owner: String,
repo: String,
branch: Option<String>,
sub: String,
}
/// `https://github.com/<owner>/<repo>[/tree|blob/<branch>[/<path>]]`
fn parse_github(url: &str) -> Result<Source, ApiError> {
let rest = url.trim().strip_prefix("https://github.com/").ok_or_else(|| bad("only https://github.com/… URLs are supported"))?;
let rest = rest.split(['?', '#']).next().unwrap_or("").trim_end_matches('/');
let mut it = rest.split('/');
let (Some(owner), Some(repo)) = (it.next().filter(|s| !s.is_empty()), it.next().map(|r| r.trim_end_matches(".git")).filter(|s| !s.is_empty())) else {
return Err(bad("not a repository URL"));
};
let (mut branch, mut sub) = (None, String::new());
if matches!(it.next(), Some("tree") | Some("blob")) {
branch = it.next().map(str::to_string);
sub = it.collect::<Vec<_>>().join("/");
if sub.ends_with("SKILL.md") {
sub = sub.trim_end_matches("SKILL.md").trim_end_matches('/').to_string();
}
}
let ok = |s: &str| s.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.'));
if !ok(owner) || !ok(repo) {
return Err(bad("not a repository URL"));
}
Ok(Source { owner: owner.into(), repo: repo.into(), branch, sub })
}
fn client() -> Result<reqwest::Client, ApiError> {
reqwest::Client::builder().timeout(std::time::Duration::from_secs(30)).user_agent("codecraft-skill-import").build().map_err(|e| bad(e.to_string()))
}
/// Blobs (path, size) of the repository below `src.sub`, and the resolved branch.
async fn tree(c: &reqwest::Client, src: &Source) -> Result<(String, Vec<(String, u64)>), ApiError> {
let branch = match &src.branch {
Some(b) => b.clone(),
None => {
let v: Value = c.get(format!("https://api.github.com/repos/{}/{}", src.owner, src.repo)).send().await.map_err(|e| bad(e.to_string()))?.json().await.map_err(|e| bad(e.to_string()))?;
v["default_branch"].as_str().ok_or_else(|| bad("repository not found (is it public?)"))?.to_string()
}
};
let r = c.get(format!("https://api.github.com/repos/{}/{}/git/trees/{branch}?recursive=1", src.owner, src.repo)).send().await.map_err(|e| bad(e.to_string()))?;
if !r.status().is_success() {
return Err(bad(format!("GitHub answered {} (repository or branch not found, or rate limit)", r.status())));
}
let v: Value = r.json().await.map_err(|e| bad(e.to_string()))?;
let prefix = if src.sub.is_empty() { String::new() } else { format!("{}/", src.sub) };
let blobs = v["tree"]
.as_array()
.into_iter()
.flatten()
.filter(|e| e["type"] == "blob")
.filter_map(|e| Some((e["path"].as_str()?.to_string(), e["size"].as_u64().unwrap_or(0))))
.filter(|(p, _)| prefix.is_empty() || p.starts_with(&prefix))
.collect();
Ok((branch, blobs))
}
/// Folders that contain a `SKILL.md` (path relative to the repository root; "" = the root itself).
fn skill_dirs(blobs: &[(String, u64)]) -> Vec<String> {
let mut v: Vec<String> = blobs
.iter()
.filter_map(|(p, _)| if p == "SKILL.md" { Some(String::new()) } else { p.strip_suffix("/SKILL.md").map(str::to_string) })
.collect();
v.sort();
v.truncate(1000);
v
}
#[derive(Deserialize)]
pub struct ScanBody {
url: String,
}
/// List the skills found at a GitHub URL (nothing is imported).
pub async fn scan(Extension(_user): Extension<User>, Json(b): Json<ScanBody>) -> ApiResult {
let src = parse_github(&b.url)?;
let c = client()?;
let (branch, blobs) = tree(&c, &src).await?;
let dirs = skill_dirs(&blobs);
let list: Vec<Value> = dirs
.iter()
.map(|d| {
let name = d.rsplit('/').next().filter(|n| !n.is_empty()).unwrap_or(&src.repo);
let n = blobs.iter().filter(|(p, _)| d.is_empty() || p.starts_with(&format!("{d}/"))).count();
json!({"path": d, "name": slug(name), "files": n})
})
.collect();
Ok(Json(json!({"branch": branch, "skills": list})))
}
#[derive(Deserialize)]
pub struct ImportBody {
url: String,
/// Folders (as listed by `scan`).
paths: Vec<String>,
}
const BINARY: &[&str] = &["png", "jpg", "jpeg", "gif", "webp", "ico", "pdf", "zip", "gz", "tar", "woff", "woff2", "ttf", "otf", "mp4", "mov", "mp3", "wav", "pyc", "so", "bin", "exe"];
/// Fetch the chosen skills of a GitHub repository and save them as the user's own skills.
pub async fn import(Extension(user): Extension<User>, State(app): State<AppState>, Json(b): Json<ImportBody>) -> ApiResult {
if b.paths.is_empty() || b.paths.len() > 100 {
return Err(bad("choose between 1 and 100 skills"));
}
let src = parse_github(&b.url)?;
let c = client()?;
let (branch, blobs) = tree(&c, &src).await?;
let known = skill_dirs(&blobs);
std::fs::create_dir_all(user_skills_dir(&app, &user.id))?;
let (mut saved, mut skipped) = (Vec::new(), Vec::new());
for d in &b.paths {
if !known.contains(d) {
skipped.push(json!({"path": d, "why": "no SKILL.md there"}));
continue;
}
let name = slug(d.rsplit('/').next().filter(|n| !n.is_empty()).unwrap_or(&src.repo));
if !valid_name(&name) {
skipped.push(json!({"path": d, "why": "no usable name"}));
continue;
}
let prefix = if d.is_empty() { String::new() } else { format!("{d}/") };
let mut files = Vec::new();
let mut dropped = 0;
for (p, size) in blobs.iter().filter(|(p, _)| p.starts_with(&prefix)) {
let rel = &p[prefix.len()..];
let ext = rel.rsplit('.').next().unwrap_or("").to_lowercase();
if rel.is_empty() || !valid_rel(rel) || *size as usize > MAX_FILE || BINARY.contains(&ext.as_str()) || files.len() >= MAX_FILES || rel.starts_with(".git") {
dropped += 1;
continue;
}
// A skill at the repository root: only Markdown files, not the whole repository.
if d.is_empty() && rel != "SKILL.md" && !rel.ends_with(".md") {
dropped += 1;
continue;
}
let url = format!("https://raw.githubusercontent.com/{}/{}/{branch}/{p}", src.owner, src.repo);
let Ok(r) = c.get(&url).send().await else { dropped += 1; continue };
if !r.status().is_success() {
dropped += 1;
continue;
}
match r.text().await {
Ok(content) => files.push(FileBody { path: rel.to_string(), content }),
Err(_) => dropped += 1,
}
}
match write_skill(&user_skills_dir(&app, &user.id).join(&name), &files) {
Ok(()) => saved.push(json!({"name": name, "files": files.len(), "dropped": dropped})),
Err(e) => skipped.push(json!({"path": d, "why": e.1})),
}
}
app.audit(Some(&user), "skill.import", format!("{}/{}: {} skills", src.owner, src.repo, saved.len()), "");
Ok(Json(json!({"saved": saved, "skipped": skipped})))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn names_and_urls() {
assert!(valid_name("frontend-design"));
assert!(!valid_name("../x") && !valid_name("Front") && !valid_name(""));
assert_eq!(slug("Frontend Design!"), "frontend-design");
let s = parse_github("https://github.com/o/r/blob/main/.claude/skills/frontend/SKILL.md").ok().unwrap();
assert_eq!((s.owner.as_str(), s.repo.as_str(), s.branch.as_deref(), s.sub.as_str()), ("o", "r", Some("main"), ".claude/skills/frontend"));
let s = parse_github("https://github.com/o/r.git").ok().unwrap();
assert!(s.branch.is_none() && s.sub.is_empty());
assert!(parse_github("https://example.org/o/r").is_err());
}
#[test]
fn finds_skill_folders() {
let b = |p: &str| (p.to_string(), 10u64);
let dirs = skill_dirs(&[b("a/SKILL.md"), b("a/ref.md"), b("x/y/SKILL.md"), b("README.md")]);
assert_eq!(dirs, vec!["a".to_string(), "x/y".to_string()]);
assert!(valid_rel("ref/a.md") && !valid_rel("../a") && !valid_rel("/etc/passwd"));
}
}
+1 -1
View File
@@ -543,7 +543,7 @@ async fn new_session(app: &Arc<App>, u: &User, bot: &Bot, chat: i64, arg: &str,
telegram_quiet: false,
summary: String::new(),
color: None,
icon: None,
icon: None, skills: Vec::new(),
})?;
update(app, &u.id, |st| st.session = Some(s.id.clone()));
bot.send(chat, &format!("{} <b>{}</b> · {} · {}", tr(de, "Neue Sitzung", "New session"), esc(&s.cwd), esc(&s.agent), esc(&s.model)), None).await?;