From 0800e8445f042d8d463c321fa934582882c79105 Mon Sep 17 00:00:00 2001 From: Deng Liu Date: Sat, 21 Nov 2026 23:09:01 -0800 Subject: [PATCH] feat(agents): native GitHub skill browser, preview, static scan, and installer (#5157) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Settings › Agents › Skills gets Install Skill…: enter a GitHub source in the forms `npx skills add` accepts (`owner/repo`, `owner/repo@skill`, `owner/repo#ref`, `owner/repo/path[@ref]`, GitHub tree URLs, pasted skills.sh links), pick one of the skills found, preview its `SKILL.md` and file list, and install it into `~/.agents/skills` (default) or the workspace `.agents/skills` (trusted projects only). Sources resolve through GitHub's Git Trees and Blobs APIs over the existing `ide-tools` HTTP client, with no curated repository list and no Node or git dependency, retrying unauthenticated when an ambient GitHub token gets HTTP 401 on a public repository. Only the selected skill's subtree is downloaded (capped at 2 MiB / 256 files) and installed atomically through a staging folder. A static scan flags hidden or bidirectional Unicode, out-of-root symlinks (omitted on disk), executable scripts and `allowed-tools` shell grants; the user must confirm the warnings, and the backend checks that confirmation again before writing. Part of #5115 Co-authored-by: dengliu <5108773+dengliu@users.noreply.github.com> --- Cargo.lock | 1 + crates/ide-agent/Cargo.toml | 1 + crates/ide-agent/src/lib.rs | 5 + crates/ide-agent/src/skill_installer.rs | 1384 ++++++++++++++++++++ crates/ide-host/src/ops/agent.rs | 116 +- crates/ide-tools/src/process.rs | 7 + e2e/agentSkills.spec.ts | 111 ++ src-tauri/tests/commands.snap | 3 + src/bindings.ts | 67 + src/features/agents/InstallSkillDialog.tsx | 392 ++++++ src/features/agents/SkillsPage.module.css | 197 +++ src/features/agents/SkillsPage.test.tsx | 132 +- src/features/agents/SkillsPage.tsx | 20 + src/features/agents/skillsModel.ts | 24 +- src/platform/locales/text/en.json | 25 + src/platform/locales/text/zh.json | 24 + 16 files changed, 2504 insertions(+), 5 deletions(-) create mode 100644 crates/ide-agent/src/skill_installer.rs create mode 100644 src/features/agents/InstallSkillDialog.tsx diff --git a/Cargo.lock b/Cargo.lock index a26d3960a..001a3b328 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4146,6 +4146,7 @@ checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" name = "ide-agent" version = "0.1.50" dependencies = [ + "base64 0.22.1", "blake3", "dunce", "encoding_rs", diff --git a/crates/ide-agent/Cargo.toml b/crates/ide-agent/Cargo.toml index 52200335d..7ba46213c 100644 --- a/crates/ide-agent/Cargo.toml +++ b/crates/ide-agent/Cargo.toml @@ -7,6 +7,7 @@ license.workspace = true [dependencies] encoding_rs.workspace = true +base64.workspace = true blake3.workspace = true ide-vfs.workspace = true ide-core.workspace = true diff --git a/crates/ide-agent/src/lib.rs b/crates/ide-agent/src/lib.rs index de52405b1..d895de82b 100644 --- a/crates/ide-agent/src/lib.rs +++ b/crates/ide-agent/src/lib.rs @@ -23,6 +23,7 @@ mod provider; pub mod registry; pub mod security; mod shell; +pub mod skill_installer; pub mod skills; mod timeline; mod worktree; @@ -31,6 +32,10 @@ pub use catalogs::AgentCatalogs; pub use cli_update::{AgentCliOffer, AgentCliUpdateResult}; pub use external::ExternalSession; pub use model::*; +pub use skill_installer::{ + InstallRemoteSkillRequest, RemoteSkillBrowseResult, RemoteSkillCandidate, RemoteSkillFile, RemoteSkillPreview, + SkillScanWarning, SkillScanWarningKind, +}; use conversation::{Conversation, SavedSubagent, Session}; use ide_core::{IdeError, IdeResult, IpcPath}; diff --git a/crates/ide-agent/src/skill_installer.rs b/crates/ide-agent/src/skill_installer.rs new file mode 100644 index 000000000..e4d493ede --- /dev/null +++ b/crates/ide-agent/src/skill_installer.rs @@ -0,0 +1,1384 @@ +//! Native Agent Skill browser, previewer, static security scanner, and subtree installer (#5115). +//! +//! Accepts the same direct GitHub source forms `npx skills add` accepts (`owner/repo`, +//! `owner/repo@ref_or_skill`, `owner/repo#ref`, `owner/repo/subpath[@ref]`, +//! `https://github.com/owner/repo[/tree/[/]]`, and pasted +//! `https://skills.sh//[/]` links), resolves the repository tree +//! through [`ide_tools::Http`] without spawning `git` or `npx`, scans `SKILL.md` and +//! bundled files before writing to disk, and installs only the selected skill's subtree +//! into `~/.agents/skills/` (User) or `/.agents/skills/` (Workspace). + +use crate::McpScope; +use base64::Engine as _; +use ide_core::error::ErrorKind; +use ide_core::{CancellationToken, IdeError, IdeResult}; +use ide_tools::Http; +use serde::{Deserialize, Serialize}; +use specta::Type; +use std::path::{Component, Path, PathBuf}; +use std::time::{SystemTime, UNIX_EPOCH}; + +/// Maximum JSON response size for GitHub repository metadata (1 MiB). +const MAX_REPO_META_BYTES: u64 = 1 << 20; +/// Maximum JSON response size for recursive Git tree listing (8 MiB). +const MAX_TREE_JSON_BYTES: u64 = 8 << 20; +/// Maximum total unpacked size of one installed skill subtree (2 MiB). +pub const MAX_SKILL_SUBTREE_BYTES: u64 = 2 << 20; +/// Maximum single blob size inside a skill subtree (1 MiB). +const MAX_SINGLE_BLOB_BYTES: u64 = 1 << 20; +/// Maximum number of files inside one installed skill subtree. +const MAX_SKILL_FILES: usize = 256; + +const SCRIPT_EXTENSIONS: &[&str] = &[ + "sh", "bash", "zsh", "fish", "py", "ps1", "bat", "cmd", "js", "mjs", "cjs", "ts", "mts", "cts", "rb", "pl", "exe", + "dll", "so", "dylib", "wasm", +]; + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)] +#[serde(rename_all = "camelCase")] +pub struct RemoteSkillCandidate { + /// Target folder name (`` under `.agents/skills/`). + pub folder_name: String, + /// Repository-relative directory holding `SKILL.md` (`""` when at repository root). + pub subpath: String, + pub file_count: u32, + pub total_bytes: u64, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)] +#[serde(rename_all = "camelCase")] +pub struct RemoteSkillBrowseResult { + pub owner: String, + pub repo: String, + pub git_ref: String, + pub tree_sha: String, + pub truncated: bool, + pub skills: Vec, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)] +#[serde(rename_all = "camelCase")] +pub struct RemoteSkillFile { + /// Path relative to the skill folder (e.g. `"SKILL.md"`, `"scripts/check.sh"`). + pub path: String, + pub size: u64, + pub executable: bool, + pub symlink: bool, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)] +#[serde(rename_all = "camelCase")] +pub enum SkillScanWarningKind { + HiddenUnicode, + OutOfRootSymlink, + ExecutableScript, + AllowedToolsShell, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)] +#[serde(rename_all = "camelCase")] +pub struct SkillScanWarning { + pub kind: SkillScanWarningKind, + pub file: String, + pub message: String, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)] +#[serde(rename_all = "camelCase")] +pub struct RemoteSkillPreview { + pub owner: String, + pub repo: String, + pub git_ref: String, + pub tree_sha: String, + pub subpath: String, + pub skill_name: String, + pub description: String, + pub skill_markdown: String, + pub files: Vec, + pub scan_warnings: Vec, + pub already_installed_user: bool, + pub already_installed_workspace: bool, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)] +#[serde(rename_all = "camelCase")] +pub struct InstallRemoteSkillRequest { + pub owner: String, + pub repo: String, + pub git_ref: String, + pub subpath: String, + pub scope: McpScope, + /// Must be `true` when the static security scan produces any warnings. + #[serde(default)] + pub confirm_warnings: bool, + /// Must be `true` to replace an existing folder of the same name in `scope`. + #[serde(default)] + pub overwrite: bool, +} + +/// Parsed user source input (`owner/repo[/subpath][@ref]`, GitHub URL, or `skills.sh` URL). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ParsedSource { + pub owner: String, + pub repo: String, + /// Explicit `#ref`, `/tree/`, or `subpath@ref`. + pub explicit_ref: Option, + /// `@suffix` on bare `owner/repo@suffix`, which may be either a Git ref or a skill name. + pub ref_or_skill: Option, + /// Explicit repository subpath filter (normalized forward-slash relative path, no `SKILL.md` leaf). + pub subpath: Option, + /// Explicit skill folder name filter (e.g. from `https://skills.sh/owner/repo/skill`). + pub skill_filter: Option, +} + +#[derive(Clone, Debug, Deserialize)] +pub struct GitTree { + pub sha: String, + #[serde(default)] + pub truncated: bool, + pub tree: Vec, +} + +#[derive(Clone, Debug, Deserialize)] +pub struct GitTreeEntry { + pub path: String, + pub mode: String, + #[serde(rename = "type")] + pub kind: String, + pub sha: String, + #[serde(default)] + pub size: Option, +} + +#[derive(Clone, Copy, Debug)] +pub struct RepoRef<'a> { + pub owner: &'a str, + pub repo: &'a str, + pub git_ref: &'a str, +} + +pub trait SkillSourceFetcher: Send + Sync { + fn default_branch(&self, owner: &str, repo: &str, cancel: &CancellationToken) -> IdeResult; + fn git_tree(&self, target: RepoRef<'_>, cancel: &CancellationToken) -> IdeResult; + fn read_blob( + &self, + target: RepoRef<'_>, + repo_path: &str, + blob_sha: &str, + max_bytes: u64, + cancel: &CancellationToken, + ) -> IdeResult>; +} + +pub struct GitHubHttpFetcher { + json_http: Http, + unauth_json_http: Http, + raw_http: Http, +} + +impl GitHubHttpFetcher { + pub fn new(search_path: &std::ffi::OsStr) -> IdeResult { + let json_http = Http::new(search_path)?.https_only().accept("application/vnd.github+json"); + let unauth_json_http = + Http::new(search_path)?.https_only().without_github_auth().accept("application/vnd.github+json"); + let raw_http = Http::new(search_path)?.https_only().without_github_auth(); + Ok(Self { json_http, unauth_json_http, raw_http }) + } + + /// Retries unauthenticated when an expired ambient `GITHUB_TOKEN` / `gh` login returns HTTP 401 on a public repo. + fn get_github_json(&self, url: &str, max_bytes: u64, cancel: &CancellationToken) -> IdeResult { + match self.json_http.get_text_limited(url, Some(max_bytes), cancel) { + Ok(body) => Ok(body), + Err(err) if !cancel.is_cancelled() && err.message.contains("401") => { + self.unauth_json_http.get_text_limited(url, Some(max_bytes), cancel) + } + Err(err) => Err(err), + } + } +} + +#[derive(Deserialize)] +struct RepoMeta { + default_branch: String, +} + +#[derive(Deserialize)] +struct BlobResponse { + #[serde(default)] + encoding: String, + #[serde(default)] + content: String, +} + +impl SkillSourceFetcher for GitHubHttpFetcher { + fn default_branch(&self, owner: &str, repo: &str, cancel: &CancellationToken) -> IdeResult { + validate_repo_coord(owner, "owner")?; + validate_repo_coord(repo, "repository")?; + let url = format!("https://api.github.com/repos/{owner}/{repo}"); + let body = self.get_github_json(&url, MAX_REPO_META_BYTES, cancel)?; + let meta: RepoMeta = serde_json::from_str(&body) + .map_err(|e| IdeError::new(ErrorKind::Protocol, format!("{owner}/{repo}: invalid GitHub metadata: {e}")))?; + validate_git_ref(&meta.default_branch)?; + Ok(meta.default_branch) + } + + fn git_tree(&self, target: RepoRef<'_>, cancel: &CancellationToken) -> IdeResult { + let RepoRef { owner, repo, git_ref } = target; + validate_repo_coord(owner, "owner")?; + validate_repo_coord(repo, "repository")?; + validate_git_ref(git_ref)?; + let encoded_ref = encode_url_segment(git_ref); + let url = format!("https://api.github.com/repos/{owner}/{repo}/git/trees/{encoded_ref}?recursive=1"); + let body = self.get_github_json(&url, MAX_TREE_JSON_BYTES, cancel)?; + serde_json::from_str(&body) + .map_err(|e| IdeError::new(ErrorKind::Protocol, format!("{owner}/{repo}@{git_ref}: invalid Git tree: {e}"))) + } + + fn read_blob( + &self, + target: RepoRef<'_>, + repo_path: &str, + blob_sha: &str, + max_bytes: u64, + cancel: &CancellationToken, + ) -> IdeResult> { + let RepoRef { owner, repo, git_ref } = target; + validate_repo_coord(owner, "owner")?; + validate_repo_coord(repo, "repository")?; + validate_git_ref(git_ref)?; + if blob_sha.is_empty() || !blob_sha.bytes().all(|b| b.is_ascii_hexdigit()) { + return Err(IdeError::invalid("invalid Git blob SHA")); + } + // Base64 expands by ~4/3 plus JSON wrapper. + let json_limit = max_bytes.saturating_mul(2).saturating_add(4096); + let blob_url = format!("https://api.github.com/repos/{owner}/{repo}/git/blobs/{blob_sha}"); + match self.get_github_json(&blob_url, json_limit, cancel) { + Ok(text) => { + let parsed: BlobResponse = serde_json::from_str(&text) + .map_err(|e| IdeError::new(ErrorKind::Protocol, format!("invalid Git blob JSON: {e}")))?; + if parsed.encoding != "base64" { + return Err(IdeError::new( + ErrorKind::Protocol, + format!("unsupported Git blob encoding {:?}", parsed.encoding), + )); + } + let cleaned: Vec = parsed.content.bytes().filter(|b| !b.is_ascii_whitespace()).collect(); + let bytes = base64::engine::general_purpose::STANDARD + .decode(cleaned) + .map_err(|e| IdeError::new(ErrorKind::Protocol, format!("invalid base64 blob: {e}")))?; + if bytes.len() as u64 > max_bytes { + return Err(IdeError::invalid(format!("{repo_path}: exceeds {} KiB limit", max_bytes >> 10))); + } + Ok(bytes) + } + Err(api_err) => { + if cancel.is_cancelled() { + return Err(IdeError::cancelled()); + } + // Fallback for unauthenticated public GitHub API rate limits. + let encoded_ref = encode_url_segment(git_ref); + let encoded_path = repo_path.split('/').map(encode_url_segment).collect::>().join("/"); + let raw_url = format!("https://raw.githubusercontent.com/{owner}/{repo}/{encoded_ref}/{encoded_path}"); + let tmp = tempfile::NamedTempFile::new()?; + let silent = |_: ide_tools::InstallEvent| {}; + self.raw_http.download(&raw_url, tmp.path(), Some(max_bytes), cancel, &silent).map_err(|_| api_err)?; + let bytes = std::fs::read(tmp.path())?; + if bytes.len() as u64 > max_bytes { + return Err(IdeError::invalid(format!("{repo_path}: exceeds {} KiB limit", max_bytes >> 10))); + } + Ok(bytes) + } + } + } +} + +fn encode_url_segment(seg: &str) -> String { + let mut out = String::with_capacity(seg.len()); + for b in seg.bytes() { + if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.' | b'~') { + out.push(b as char); + } else { + out.push_str(&format!("%{b:02X}")); + } + } + out +} + +fn validate_repo_coord(value: &str, what: &str) -> IdeResult<()> { + if value.is_empty() + || value.len() > 100 + || value == "." + || value == ".." + || !value.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.')) + { + return Err(IdeError::invalid(format!("Invalid GitHub {what} {value:?}"))); + } + Ok(()) +} + +fn validate_git_ref(git_ref: &str) -> IdeResult<()> { + if git_ref.is_empty() + || git_ref.len() > 200 + || git_ref.starts_with('-') + || git_ref.contains("..") + || git_ref.bytes().any(|b| b.is_ascii_control() || matches!(b, b' ' | b'?' | b'#' | b'\\' | b'~' | b'^' | b':')) + { + return Err(IdeError::invalid(format!("Invalid Git ref {git_ref:?}"))); + } + Ok(()) +} + +fn normalize_subpath(raw: &str) -> IdeResult> { + let trimmed = raw.trim().trim_matches('/'); + if trimmed.is_empty() { + return Ok(None); + } + let stripped = trimmed.strip_suffix("/SKILL.md").unwrap_or(if trimmed == "SKILL.md" { "" } else { trimmed }); + if stripped.is_empty() { + return Ok(None); + } + let mut parts = Vec::new(); + for seg in stripped.split('/') { + if seg.is_empty() || seg == "." || seg == ".." || seg.contains('\\') { + return Err(IdeError::invalid(format!("Invalid skill path {raw:?}"))); + } + parts.push(seg); + } + Ok(Some(parts.join("/"))) +} + +/// Parses `owner/repo[/path][@ref]`, `owner/repo#ref`, GitHub URLs, and `skills.sh` URLs. +pub fn parse_source(input: &str) -> IdeResult { + let s = input.trim(); + if s.is_empty() { + return Err(IdeError::invalid("Enter a GitHub repository such as owner/repo or owner/repo@skill")); + } + + // Pasted https://skills.sh//[/] URL -> resolve directly from GitHub. + if let Some(rest) = s.strip_prefix("https://skills.sh/").or_else(|| s.strip_prefix("http://skills.sh/")) { + let rest = rest.split(['?', '#']).next().unwrap_or(rest).trim_matches('/'); + let parts: Vec<&str> = rest.split('/').filter(|p| !p.is_empty()).collect(); + if parts.len() < 2 || parts.len() > 3 { + return Err(IdeError::invalid("Expected https://skills.sh//[/]")); + } + validate_repo_coord(parts[0], "owner")?; + validate_repo_coord(parts[1], "repository")?; + let skill_filter = if parts.len() == 3 { + let name = parts[2].trim(); + if !valid_skill_name(name) { + return Err(IdeError::invalid(format!("Invalid skill name {name:?}"))); + } + Some(name.to_owned()) + } else { + None + }; + return Ok(ParsedSource { + owner: parts[0].to_owned(), + repo: parts[1].to_owned(), + explicit_ref: None, + ref_or_skill: None, + subpath: None, + skill_filter, + }); + } + + // Full GitHub HTTPS URL. + if let Some(rest) = s.strip_prefix("https://github.com/").or_else(|| s.strip_prefix("http://github.com/")) { + let (path_part, hash_ref) = match rest.split_once('#') { + Some((p, h)) if !h.is_empty() => (p, Some(h)), + _ => (rest.split('?').next().unwrap_or(rest), None), + }; + let parts: Vec<&str> = path_part.trim_matches('/').split('/').filter(|p| !p.is_empty()).collect(); + if parts.len() < 2 { + return Err(IdeError::invalid("Expected https://github.com//")); + } + let owner = parts[0]; + let repo = parts[1].strip_suffix(".git").unwrap_or(parts[1]); + validate_repo_coord(owner, "owner")?; + validate_repo_coord(repo, "repository")?; + if parts.len() == 2 { + if let Some(r) = hash_ref { + validate_git_ref(r)?; + } + return Ok(ParsedSource { + owner: owner.to_owned(), + repo: repo.to_owned(), + explicit_ref: hash_ref.map(str::to_owned), + ref_or_skill: None, + subpath: None, + skill_filter: None, + }); + } + if matches!(parts[2], "tree" | "blob") { + if parts.len() < 4 { + return Err(IdeError::invalid("Expected https://github.com///tree/[/]")); + } + let git_ref = parts[3]; + validate_git_ref(git_ref)?; + let subpath = if parts.len() > 4 { normalize_subpath(&parts[4..].join("/"))? } else { None }; + return Ok(ParsedSource { + owner: owner.to_owned(), + repo: repo.to_owned(), + explicit_ref: Some(git_ref.to_owned()), + ref_or_skill: None, + subpath, + skill_filter: None, + }); + } + return Err(IdeError::invalid( + "Unsupported GitHub URL path; use https://github.com//[/tree//]", + )); + } + + if s.contains("://") { + return Err(IdeError::invalid("Only GitHub (`owner/repo`) sources are supported")); + } + + // Shorthand: `owner/repo[/subpath][@ref|#ref]` or `owner/repo@skill`. + let (lhs, explicit_hash_ref) = match s.split_once('#') { + Some((l, r)) => { + validate_git_ref(r)?; + (l, Some(r.to_owned())) + } + None => (s, None), + }; + let (path_spec, at_suffix) = match lhs.rsplit_once('@') { + Some((l, r)) if explicit_hash_ref.is_none() => { + if r.is_empty() { + return Err(IdeError::invalid("Missing ref or skill name after `@`")); + } + validate_git_ref(r)?; + (l, Some(r.to_owned())) + } + _ => (lhs, None), + }; + let segs: Vec<&str> = path_spec.trim_matches('/').split('/').filter(|p| !p.is_empty()).collect(); + if segs.len() < 2 { + return Err(IdeError::invalid("Expected `/` (for example `vercel-labs/agent-skills`)")); + } + let owner = segs[0]; + let repo = segs[1].strip_suffix(".git").unwrap_or(segs[1]); + validate_repo_coord(owner, "owner")?; + validate_repo_coord(repo, "repository")?; + let subpath = if segs.len() > 2 { normalize_subpath(&segs[2..].join("/"))? } else { None }; + + // When a subpath is already present (`owner/repo/skills/foo@v1`), `@v1` is unambiguously a Git ref. + // On bare `owner/repo@suffix`, `suffix` may be either a Git ref (`main`, `v1.0.0`) or a skill name (`find-skills`). + let (explicit_ref, ref_or_skill) = if let Some(h) = explicit_hash_ref { + (Some(h), None) + } else if subpath.is_some() { + (at_suffix, None) + } else { + (None, at_suffix) + }; + + Ok(ParsedSource { + owner: owner.to_owned(), + repo: repo.to_owned(), + explicit_ref, + ref_or_skill, + subpath, + skill_filter: None, + }) +} + +fn valid_skill_name(name: &str) -> bool { + (1..=64).contains(&name.len()) && name.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-') +} + +/// Resolves `source` and lists matching `SKILL.md` directories in the repository. +pub fn browse_skills( + fetcher: &dyn SkillSourceFetcher, + source: &str, + cancel: &CancellationToken, +) -> IdeResult { + let parsed = parse_source(source)?; + let (git_ref, tree, skill_filter) = if let Some(explicit_ref) = parsed.explicit_ref { + let tree = + fetcher.git_tree(RepoRef { owner: &parsed.owner, repo: &parsed.repo, git_ref: &explicit_ref }, cancel)?; + (explicit_ref, tree, parsed.skill_filter) + } else if let Some(suffix) = parsed.ref_or_skill { + // Try `suffix` as a Git ref first; if absent, fall back to the default branch and treat `suffix` as a skill filter. + match fetcher.git_tree(RepoRef { owner: &parsed.owner, repo: &parsed.repo, git_ref: &suffix }, cancel) { + Ok(tree) => (suffix, tree, parsed.skill_filter), + Err(_) => { + if cancel.is_cancelled() { + return Err(IdeError::cancelled()); + } + let default_branch = fetcher.default_branch(&parsed.owner, &parsed.repo, cancel)?; + let tree = fetcher + .git_tree(RepoRef { owner: &parsed.owner, repo: &parsed.repo, git_ref: &default_branch }, cancel)?; + (default_branch, tree, Some(suffix)) + } + } + } else { + let default_branch = fetcher.default_branch(&parsed.owner, &parsed.repo, cancel)?; + let tree = + fetcher.git_tree(RepoRef { owner: &parsed.owner, repo: &parsed.repo, git_ref: &default_branch }, cancel)?; + (default_branch, tree, parsed.skill_filter) + }; + + let mut skill_dirs: Vec<(String, String)> = Vec::new(); + for entry in &tree.tree { + if entry.kind != "blob" { + continue; + } + if entry.path == "SKILL.md" { + let fallback = sanitize_repo_as_skill_name(&parsed.repo); + skill_dirs.push((String::new(), fallback)); + } else if let Some(dir) = entry.path.strip_suffix("/SKILL.md") + && let Some(folder) = dir.rsplit('/').next() + && !folder.starts_with('.') + { + skill_dirs.push((dir.to_owned(), folder.to_owned())); + } + } + + let mut candidates = Vec::new(); + for (dir, folder_name) in skill_dirs { + if let Some(sub) = parsed.subpath.as_deref() + && dir != sub + && !dir.starts_with(&format!("{sub}/")) + { + continue; + } + if let Some(filter) = skill_filter.as_deref() + && !folder_name.eq_ignore_ascii_case(filter) + && !dir.eq_ignore_ascii_case(filter) + { + continue; + } + let prefix = if dir.is_empty() { String::new() } else { format!("{dir}/") }; + let mut file_count = 0u32; + let mut total_bytes = 0u64; + for e in &tree.tree { + if e.kind == "blob" && (dir.is_empty() || e.path.starts_with(&prefix)) { + file_count = file_count.saturating_add(1); + total_bytes = total_bytes.saturating_add(e.size.unwrap_or(0)); + } + } + candidates.push(RemoteSkillCandidate { folder_name, subpath: dir, file_count, total_bytes }); + } + candidates.sort_by(|a, b| a.folder_name.cmp(&b.folder_name).then_with(|| a.subpath.cmp(&b.subpath))); + + if candidates.is_empty() { + return Err(IdeError::not_found(format!("No SKILL.md found in {}/{} ({git_ref})", parsed.owner, parsed.repo))); + } + + Ok(RemoteSkillBrowseResult { + owner: parsed.owner, + repo: parsed.repo, + git_ref, + tree_sha: tree.sha, + truncated: tree.truncated, + skills: candidates, + }) +} + +fn sanitize_repo_as_skill_name(repo: &str) -> String { + let mut out = String::new(); + for c in repo.chars() { + if c.is_ascii_alphanumeric() { + out.push(c.to_ascii_lowercase()); + } else if (c == '-' || c == '_' || c == '.') && !out.ends_with('-') { + out.push('-'); + } + if out.len() >= 64 { + break; + } + } + out.trim_matches('-').to_owned() +} + +struct PreparedSkillBundle { + tree_sha: String, + skill_name: String, + description: String, + skill_markdown: String, + files: Vec, + scan_warnings: Vec, + /// `(relative_path, bytes, executable)` for safe non-symlink files to write on disk; + /// `in_root_symlinks` holds `(relative_path, relative_target)` for safe internal symlinks. + regular_files: Vec<(PathBuf, Vec, bool)>, + in_root_symlinks: Vec<(PathBuf, PathBuf)>, +} + +fn fetch_and_scan_bundle( + fetcher: &dyn SkillSourceFetcher, + target: RepoRef<'_>, + subpath: &str, + cancel: &CancellationToken, +) -> IdeResult { + let RepoRef { owner, repo, git_ref } = target; + validate_repo_coord(owner, "owner")?; + validate_repo_coord(repo, "repository")?; + validate_git_ref(git_ref)?; + let normalized_subpath = normalize_subpath(subpath)?.unwrap_or_default(); + + let tree = fetcher.git_tree(target, cancel)?; + let prefix = if normalized_subpath.is_empty() { String::new() } else { format!("{normalized_subpath}/") }; + + let subtree_entries: Vec<&GitTreeEntry> = tree + .tree + .iter() + .filter(|e| { + e.kind == "blob" && if prefix.is_empty() { !e.path.is_empty() } else { e.path.starts_with(&prefix) } + }) + .collect(); + + if subtree_entries.is_empty() { + return Err(IdeError::not_found(format!( + "Skill path {:?} was not found in {owner}/{repo}@{git_ref}", + normalized_subpath + ))); + } + if subtree_entries.len() > MAX_SKILL_FILES { + return Err(IdeError::invalid(format!( + "Skill exceeds maximum of {MAX_SKILL_FILES} files ({} found)", + subtree_entries.len() + ))); + } + + let declared_total: u64 = subtree_entries.iter().map(|e| e.size.unwrap_or(0)).sum(); + if declared_total > MAX_SKILL_SUBTREE_BYTES { + return Err(IdeError::invalid(format!("Skill exceeds {} MiB size cap", MAX_SKILL_SUBTREE_BYTES >> 20))); + } + + let mut files = Vec::with_capacity(subtree_entries.len()); + let mut scan_warnings = Vec::new(); + let mut regular_files = Vec::new(); + let mut in_root_symlinks = Vec::new(); + let mut skill_markdown: Option = None; + let mut actual_total = 0u64; + + for entry in subtree_entries { + let rel_str = if prefix.is_empty() { + entry.path.as_str() + } else { + entry.path.strip_prefix(&prefix).unwrap_or(entry.path.as_str()) + }; + let rel_path = validate_rel_skill_file(rel_str)?; + let is_symlink = entry.mode == "120000"; + let is_exec_mode = entry.mode == "100755"; + + let bytes = fetcher.read_blob( + target, + &entry.path, + &entry.sha, + MAX_SINGLE_BLOB_BYTES.min(MAX_SKILL_SUBTREE_BYTES - actual_total), + cancel, + )?; + actual_total = actual_total.saturating_add(bytes.len() as u64); + if actual_total > MAX_SKILL_SUBTREE_BYTES { + return Err(IdeError::invalid(format!("Skill exceeds {} MiB size cap", MAX_SKILL_SUBTREE_BYTES >> 20))); + } + + if is_symlink { + let target_raw = String::from_utf8_lossy(&bytes).trim().to_owned(); + match resolve_in_root_symlink(&rel_path, &target_raw) { + Some(safe_target) => { + in_root_symlinks.push((rel_path.clone(), safe_target)); + } + None => { + scan_warnings.push(SkillScanWarning { + kind: SkillScanWarningKind::OutOfRootSymlink, + file: rel_str.to_owned(), + message: format!("Symlink target {target_raw:?} leaves the skill folder (omitted on install)"), + }); + } + } + files.push(RemoteSkillFile { + path: rel_str.to_owned(), + size: bytes.len() as u64, + executable: false, + symlink: true, + }); + continue; + } + + let has_shebang = bytes.starts_with(b"#!"); + let is_script_path = is_executable_or_script_path(&rel_path); + if is_exec_mode || has_shebang || is_script_path { + let reason = if is_exec_mode { + "executable file mode (100755)" + } else if has_shebang { + "script with `#!` interpreter header" + } else { + "bundled script or native binary" + }; + scan_warnings.push(SkillScanWarning { + kind: SkillScanWarningKind::ExecutableScript, + file: rel_str.to_owned(), + message: format!("Contains {reason}"), + }); + } + + if let Ok(text) = std::str::from_utf8(&bytes) { + scan_hidden_unicode(rel_str, text, &mut scan_warnings); + if rel_str == "SKILL.md" { + skill_markdown = Some(text.to_owned()); + } + } else if rel_str == "SKILL.md" { + return Err(IdeError::invalid("SKILL.md is not valid UTF-8 text")); + } + + files.push(RemoteSkillFile { + path: rel_str.to_owned(), + size: bytes.len() as u64, + executable: is_exec_mode || has_shebang, + symlink: false, + }); + regular_files.push((rel_path, bytes, is_exec_mode)); + } + + files.sort_by(|a, b| a.path.cmp(&b.path)); + regular_files.sort_by(|a, b| a.0.cmp(&b.0)); + + let Some(skill_markdown) = skill_markdown else { + return Err(IdeError::invalid("Selected folder does not contain a regular SKILL.md file")); + }; + + let folder_hint = normalized_subpath + .rsplit('/') + .next() + .filter(|s| !s.is_empty()) + .map(str::to_owned) + .unwrap_or_else(|| sanitize_repo_as_skill_name(repo)); + + let parsed_meta = parse_skill_markdown_metadata(&folder_hint, &skill_markdown)?; + for tool in &parsed_meta.shell_tools { + scan_warnings.push(SkillScanWarning { + kind: SkillScanWarningKind::AllowedToolsShell, + file: "SKILL.md".into(), + message: format!("`allowed-tools` grants shell/command execution (`{tool}`)"), + }); + } + + Ok(PreparedSkillBundle { + tree_sha: tree.sha, + skill_name: parsed_meta.name, + description: parsed_meta.description, + skill_markdown, + files, + scan_warnings, + regular_files, + in_root_symlinks, + }) +} + +struct ParsedSkillMetadata { + name: String, + description: String, + shell_tools: Vec, +} + +fn parse_skill_markdown_metadata(folder_hint: &str, markdown: &str) -> IdeResult { + let mut name: Option = None; + let mut description: Option = None; + let mut allowed_tools_raw: Vec = Vec::new(); + + let normalized = markdown.strip_prefix('\u{feff}').unwrap_or(markdown); + if let Some(after_open) = normalized.strip_prefix("---\n").or_else(|| normalized.strip_prefix("---\r\n")) { + let mut current_list_key: Option = None; + for raw_line in after_open.lines() { + let trimmed_end = raw_line.trim_end_matches('\r'); + if matches!(trimmed_end.trim(), "---" | "...") { + break; + } + let trimmed = trimmed_end.trim(); + if trimmed.is_empty() || trimmed.starts_with('#') { + continue; + } + if (trimmed_end.starts_with(' ') || trimmed_end.starts_with('\t')) + && let Some(item) = trimmed.strip_prefix('-') + { + if matches!(current_list_key.as_deref(), Some("allowed-tools" | "allowed_tools")) { + let val = unquote(item.trim()); + if !val.is_empty() { + allowed_tools_raw.push(val.to_owned()); + } + } + continue; + } + current_list_key = None; + if let Some((k, v)) = trimmed.split_once(':') { + let key = k.trim().to_ascii_lowercase(); + let val = unquote(v.trim()); + match key.as_str() { + "name" if !val.is_empty() => name = Some(val.to_owned()), + "description" if !val.is_empty() && !matches!(val, ">" | "|" | ">-" | "|-") => { + description = Some(val.to_owned()); + } + "allowed-tools" | "allowed_tools" => { + if val.is_empty() { + current_list_key = Some(key); + } else { + let inner = val.strip_prefix('[').and_then(|s| s.strip_suffix(']')).unwrap_or(val); + for token in split_allowed_tools(inner) { + allowed_tools_raw.push(token); + } + } + } + _ => {} + } + } + } + } + + let skill_name = name.unwrap_or_else(|| folder_hint.to_owned()); + if !valid_skill_name(&skill_name) { + return Err(IdeError::invalid(format!( + "Skill name {skill_name:?} must be 1-64 lowercase letters, digits, or hyphens" + ))); + } + let description = description.unwrap_or_default(); + let shell_tools = allowed_tools_raw.into_iter().filter(|t| is_shell_tool_entry(t)).collect(); + Ok(ParsedSkillMetadata { name: skill_name, description, shell_tools }) +} + +fn split_allowed_tools(raw: &str) -> Vec { + let mut out = Vec::new(); + let mut cur = String::new(); + let mut depth = 0usize; + for ch in raw.chars() { + match ch { + '(' => { + depth += 1; + cur.push(ch); + } + ')' => { + depth = depth.saturating_sub(1); + cur.push(ch); + } + ',' | ' ' if depth == 0 => { + let tok = unquote(cur.trim()); + if !tok.is_empty() { + out.push(tok.to_owned()); + } + cur.clear(); + } + _ => cur.push(ch), + } + } + let tok = unquote(cur.trim()); + if !tok.is_empty() { + out.push(tok.to_owned()); + } + out +} + +fn is_shell_tool_entry(entry: &str) -> bool { + let head = entry.split(['(', ':', '/']).next().unwrap_or(entry).trim().to_ascii_lowercase(); + matches!( + head.as_str(), + "*" | "bash" + | "sh" + | "zsh" + | "shell" + | "terminal" + | "cmd" + | "powershell" + | "pwsh" + | "run_command" + | "exec" + | "execute" + ) +} + +fn unquote(value: &str) -> &str { + for q in ['"', '\''] { + if let Some(inner) = value.strip_prefix(q).and_then(|v| v.strip_suffix(q)) { + return inner; + } + } + value +} + +fn validate_rel_skill_file(rel: &str) -> IdeResult { + if rel.is_empty() || rel.contains('\\') { + return Err(IdeError::invalid(format!("Invalid relative file path {rel:?}"))); + } + let path = Path::new(rel); + if !path.components().all(|c| matches!(c, Component::Normal(_))) { + return Err(IdeError::invalid(format!("Path {rel:?} escapes the skill folder"))); + } + Ok(path.to_path_buf()) +} + +/// Returns `Some(target_path)` only when `target_raw` is a relative link that stays strictly inside the skill root. +fn resolve_in_root_symlink(link_rel: &Path, target_raw: &str) -> Option { + if target_raw.is_empty() + || target_raw.starts_with('/') + || target_raw.starts_with('~') + || target_raw.contains('\\') + || target_raw.as_bytes().get(1) == Some(&b':') + { + return None; + } + let mut stack: Vec<&std::ffi::OsStr> = link_rel + .parent() + .map(|p| { + p.components() + .filter_map(|c| match c { + Component::Normal(s) => Some(s), + _ => None, + }) + .collect() + }) + .unwrap_or_default(); + for seg in target_raw.split('/') { + match seg { + "" | "." => {} + ".." => { + stack.pop()?; + } + _ => stack.push(std::ffi::OsStr::new(seg)), + } + } + if stack.is_empty() { + return None; + } + Some(PathBuf::from(target_raw)) +} + +fn is_executable_or_script_path(rel: &Path) -> bool { + if rel.components().next().and_then(|c| c.as_os_str().to_str()) == Some("scripts") { + return true; + } + rel.extension() + .and_then(|e| e.to_str()) + .is_some_and(|ext| SCRIPT_EXTENSIONS.iter().any(|s| ext.eq_ignore_ascii_case(s))) +} + +fn is_hidden_or_bidi_unicode(ch: char, at_file_start: bool) -> Option<&'static str> { + match ch { + '\u{202A}'..='\u{202E}' | '\u{2066}'..='\u{2069}' | '\u{061C}' | '\u{200E}' | '\u{200F}' => { + Some("bidirectional control character") + } + '\u{200B}'..='\u{200D}' | '\u{2060}' => Some("zero-width character"), + '\u{FEFF}' if !at_file_start => Some("zero-width no-break space"), + '\u{E0000}'..='\u{E007F}' | '\u{E0100}'..='\u{E01EF}' => Some("invisible Unicode tag/variation character"), + _ => None, + } +} + +fn scan_hidden_unicode(file: &str, text: &str, warnings: &mut Vec) { + for (line_idx, line) in text.lines().enumerate() { + for (char_idx, ch) in line.chars().enumerate() { + let at_start = line_idx == 0 && char_idx == 0; + if let Some(label) = is_hidden_or_bidi_unicode(ch, at_start) { + warnings.push(SkillScanWarning { + kind: SkillScanWarningKind::HiddenUnicode, + file: file.to_owned(), + message: format!("Line {} contains {} (U+{:04X})", line_idx + 1, label, ch as u32), + }); + // Report once per file to keep the warning list readable. + return; + } + } + } +} + +/// Previews a single skill from `target` (`subpath`) and runs the static security scan. +pub fn preview_skill( + fetcher: &dyn SkillSourceFetcher, + target: RepoRef<'_>, + subpath: &str, + user_home: Option<&Path>, + workspace_root: Option<&Path>, + cancel: &CancellationToken, +) -> IdeResult { + let RepoRef { owner, repo, git_ref } = target; + let bundle = fetch_and_scan_bundle(fetcher, target, subpath, cancel)?; + let already_installed_user = user_home.is_some_and(|h| h.join(".agents/skills").join(&bundle.skill_name).exists()); + let already_installed_workspace = + workspace_root.is_some_and(|w| w.join(".agents/skills").join(&bundle.skill_name).exists()); + Ok(RemoteSkillPreview { + owner: owner.to_owned(), + repo: repo.to_owned(), + git_ref: git_ref.to_owned(), + tree_sha: bundle.tree_sha, + subpath: normalize_subpath(subpath)?.unwrap_or_default(), + skill_name: bundle.skill_name, + description: bundle.description, + skill_markdown: bundle.skill_markdown, + files: bundle.files, + scan_warnings: bundle.scan_warnings, + already_installed_user, + already_installed_workspace, + }) +} + +/// Downloads only the skill's subtree, enforces static scan confirmation, and installs atomically +/// into `~/.agents/skills/` (`McpScope::User`) or `/.agents/skills/` (`McpScope::Workspace`). +pub fn install_skill( + fetcher: &dyn SkillSourceFetcher, + req: &InstallRemoteSkillRequest, + user_home: Option<&Path>, + workspace_root: Option<&Path>, + cancel: &CancellationToken, +) -> IdeResult { + let target_root = match req.scope { + McpScope::User => { + let home = user_home.ok_or_else(|| IdeError::unavailable("Cannot locate user home directory"))?; + home.join(".agents").join("skills") + } + McpScope::Workspace => { + let ws = workspace_root + .ok_or_else(|| IdeError::invalid("Open a trusted project to install workspace skills"))?; + ws.join(".agents").join("skills") + } + }; + + let bundle = fetch_and_scan_bundle( + fetcher, + RepoRef { owner: &req.owner, repo: &req.repo, git_ref: &req.git_ref }, + &req.subpath, + cancel, + )?; + if !bundle.scan_warnings.is_empty() && !req.confirm_warnings { + return Err(IdeError::invalid( + "Review and confirm the static security scan warnings before installing this skill", + )); + } + + std::fs::create_dir_all(&target_root)?; + let final_dir = target_root.join(&bundle.skill_name); + if final_dir.exists() && !req.overwrite { + return Err(IdeError::new( + ErrorKind::AlreadyExists, + format!("Skill `{}` is already installed in {}", bundle.skill_name, target_root.display()), + )); + } + + let nonce = SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or_default(); + let staging = target_root.join(format!(".staging-{}-{nonce}-{}", bundle.skill_name, std::process::id())); + let write_res = (|| -> IdeResult<()> { + std::fs::create_dir_all(&staging)?; + for (rel_path, bytes, is_exec) in &bundle.regular_files { + let dest = staging.join(rel_path); + if let Some(parent) = dest.parent() { + std::fs::create_dir_all(parent)?; + } + std::fs::write(&dest, bytes)?; + #[cfg(unix)] + if *is_exec { + use std::os::unix::fs::PermissionsExt; + let _ = std::fs::set_permissions(&dest, std::fs::Permissions::from_mode(0o755)); + } + #[cfg(not(unix))] + let _ = is_exec; + } + for (rel_link, target) in &bundle.in_root_symlinks { + let dest = staging.join(rel_link); + if let Some(parent) = dest.parent() { + std::fs::create_dir_all(parent)?; + } + #[cfg(unix)] + std::os::unix::fs::symlink(target, &dest)?; + #[cfg(not(unix))] + { + let resolved = dest.parent().unwrap_or(&staging).join(target); + if resolved.is_file() { + std::fs::copy(&resolved, &dest)?; + } + } + } + let backup = if final_dir.exists() { + let bak = target_root.join(format!(".old-{}-{nonce}", bundle.skill_name)); + std::fs::rename(&final_dir, &bak)?; + Some(bak) + } else { + None + }; + if let Err(err) = std::fs::rename(&staging, &final_dir) { + if let Some(bak) = backup { + let _ = std::fs::rename(&bak, &final_dir); + } + return Err(err.into()); + } + if let Some(bak) = backup { + let _ = std::fs::remove_dir_all(bak); + } + Ok(()) + })(); + + if write_res.is_err() { + let _ = std::fs::remove_dir_all(&staging); + } + write_res?; + Ok(final_dir) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::collections::BTreeMap; + + struct FakeFetcher { + default_branch: String, + trees: BTreeMap, + blobs: BTreeMap>, + } + + impl FakeFetcher { + fn new() -> Self { + Self { default_branch: "main".into(), trees: BTreeMap::new(), blobs: BTreeMap::new() } + } + + fn add_tree(&mut self, git_ref: &str, entries: Vec<(&str, &str, &[u8])>) { + let mut tree_entries = Vec::new(); + for (idx, (path, mode, content)) in entries.into_iter().enumerate() { + let sha = format!("{idx:040x}"); + self.blobs.insert(sha.clone(), content.to_vec()); + tree_entries.push(GitTreeEntry { + path: path.to_owned(), + mode: mode.to_owned(), + kind: "blob".into(), + sha, + size: Some(content.len() as u64), + }); + } + self.trees.insert( + git_ref.to_owned(), + GitTree { sha: "deadbeef".repeat(5), truncated: false, tree: tree_entries }, + ); + } + } + + impl SkillSourceFetcher for FakeFetcher { + fn default_branch(&self, _owner: &str, _repo: &str, _cancel: &CancellationToken) -> IdeResult { + Ok(self.default_branch.clone()) + } + + fn git_tree(&self, target: RepoRef<'_>, _cancel: &CancellationToken) -> IdeResult { + self.trees + .get(target.git_ref) + .cloned() + .ok_or_else(|| IdeError::not_found(format!("unknown ref {}", target.git_ref))) + } + + fn read_blob( + &self, + _target: RepoRef<'_>, + repo_path: &str, + blob_sha: &str, + max_bytes: u64, + _cancel: &CancellationToken, + ) -> IdeResult> { + let bytes = self.blobs.get(blob_sha).cloned().ok_or_else(|| IdeError::not_found(repo_path))?; + if bytes.len() as u64 > max_bytes { + return Err(IdeError::invalid("blob too large")); + } + Ok(bytes) + } + } + + #[test] + fn parses_npx_skills_add_inputs_and_rejects_traversal() { + let plain = parse_source("vercel-labs/agent-skills").unwrap(); + assert_eq!((plain.owner.as_str(), plain.repo.as_str(), plain.subpath), ("vercel-labs", "agent-skills", None)); + + let at_skill = parse_source("vercel-labs/agent-skills@find-skills").unwrap(); + assert_eq!(at_skill.ref_or_skill.as_deref(), Some("find-skills")); + + let subpath_ref = parse_source("acme/skills/skills/tdd@v1.2.0").unwrap(); + assert_eq!(subpath_ref.subpath.as_deref(), Some("skills/tdd")); + assert_eq!(subpath_ref.explicit_ref.as_deref(), Some("v1.2.0")); + + let gh_url = parse_source("https://github.com/acme/skills/tree/release/skills/tdd/SKILL.md").unwrap(); + assert_eq!(gh_url.explicit_ref.as_deref(), Some("release")); + assert_eq!(gh_url.subpath.as_deref(), Some("skills/tdd")); + + let skills_sh = parse_source("https://skills.sh/vercel-labs/skills/find-skills").unwrap(); + assert_eq!((skills_sh.owner.as_str(), skills_sh.repo.as_str()), ("vercel-labs", "skills")); + assert_eq!(skills_sh.skill_filter.as_deref(), Some("find-skills")); + + assert!(parse_source("acme/skills/../escape").is_err()); + assert!(parse_source("https://evil.example.com/acme/skills").is_err()); + } + + #[test] + fn browses_filters_scans_and_installs_only_selected_subtree() { + let mut fetcher = FakeFetcher::new(); + fetcher.add_tree( + "main", + vec![ + ("README.md", "100644", b"# Repo root README (must not be downloaded)\n"), + ( + "skills/clean-skill/SKILL.md", + "100644", + b"---\nname: clean-skill\ndescription: Clean markdown-only skill\n---\nFollow clean steps.\n", + ), + ("skills/clean-skill/references/guide.md", "100644", b"# Guide\n"), + ( + "skills/risky-skill/SKILL.md", + "100644", + "---\nname: risky-skill\ndescription: Runs deploy script\nallowed-tools:\n - Bash(git:*)\n - Read\n---\nHidden \u{202E}bidi text.\n".as_bytes(), + ), + ("skills/risky-skill/scripts/deploy.sh", "100755", b"#!/bin/sh\necho hi\n"), + ("skills/risky-skill/escape-link", "120000", b"../../README.md"), + ], + ); + let cancel = CancellationToken::new(); + let tmp = tempfile::tempdir().unwrap(); + let home = tmp.path().join("home"); + let ws = tmp.path().join("ws"); + + // Bare `owner/repo` lists both skills; `owner/repo@clean-skill` falls back from ref lookup to skill filter! + let all = browse_skills(&fetcher, "acme/skills", &cancel).unwrap(); + assert_eq!(all.skills.len(), 2); + let filtered = browse_skills(&fetcher, "acme/skills@clean-skill", &cancel).unwrap(); + assert_eq!(filtered.skills.len(), 1); + assert_eq!(filtered.skills[0].folder_name, "clean-skill"); + + // Clean skill has 0 warnings and installs into ~/.agents/skills/clean-skill without touching repo root files. + let clean_preview = preview_skill( + &fetcher, + RepoRef { owner: "acme", repo: "skills", git_ref: "main" }, + "skills/clean-skill", + Some(&home), + Some(&ws), + &cancel, + ) + .unwrap(); + assert!(clean_preview.scan_warnings.is_empty(), "{:?}", clean_preview.scan_warnings); + assert_eq!(clean_preview.files.len(), 2); + + let installed_dir = install_skill( + &fetcher, + &InstallRemoteSkillRequest { + owner: "acme".into(), + repo: "skills".into(), + git_ref: "main".into(), + subpath: "skills/clean-skill".into(), + scope: McpScope::User, + confirm_warnings: false, + overwrite: false, + }, + Some(&home), + Some(&ws), + &cancel, + ) + .unwrap(); + assert_eq!(installed_dir, home.join(".agents/skills/clean-skill")); + assert!(installed_dir.join("SKILL.md").is_file()); + assert!(installed_dir.join("references/guide.md").is_file()); + assert!(!home.join(".agents/skills/README.md").exists()); + + // Re-installing without `overwrite: true` is refused. + let dup_err = install_skill( + &fetcher, + &InstallRemoteSkillRequest { + owner: "acme".into(), + repo: "skills".into(), + git_ref: "main".into(), + subpath: "skills/clean-skill".into(), + scope: McpScope::User, + confirm_warnings: false, + overwrite: false, + }, + Some(&home), + Some(&ws), + &cancel, + ) + .unwrap_err(); + assert_eq!(dup_err.kind, ErrorKind::AlreadyExists); + + // Risky skill triggers all 4 static security scan categories and requires `confirm_warnings: true`. + let risky_preview = preview_skill( + &fetcher, + RepoRef { owner: "acme", repo: "skills", git_ref: "main" }, + "skills/risky-skill", + Some(&home), + Some(&ws), + &cancel, + ) + .unwrap(); + let kinds: Vec = risky_preview.scan_warnings.iter().map(|w| w.kind).collect(); + assert!(kinds.contains(&SkillScanWarningKind::HiddenUnicode), "{kinds:?}"); + assert!(kinds.contains(&SkillScanWarningKind::OutOfRootSymlink), "{kinds:?}"); + assert!(kinds.contains(&SkillScanWarningKind::ExecutableScript), "{kinds:?}"); + assert!(kinds.contains(&SkillScanWarningKind::AllowedToolsShell), "{kinds:?}"); + + let unconfirmed = install_skill( + &fetcher, + &InstallRemoteSkillRequest { + owner: "acme".into(), + repo: "skills".into(), + git_ref: "main".into(), + subpath: "skills/risky-skill".into(), + scope: McpScope::Workspace, + confirm_warnings: false, + overwrite: false, + }, + Some(&home), + Some(&ws), + &cancel, + ) + .unwrap_err(); + assert!(unconfirmed.message.contains("confirm"), "{unconfirmed:?}"); + + let ws_installed = install_skill( + &fetcher, + &InstallRemoteSkillRequest { + owner: "acme".into(), + repo: "skills".into(), + git_ref: "main".into(), + subpath: "skills/risky-skill".into(), + scope: McpScope::Workspace, + confirm_warnings: true, + overwrite: false, + }, + Some(&home), + Some(&ws), + &cancel, + ) + .unwrap(); + assert_eq!(ws_installed, ws.join(".agents/skills/risky-skill")); + assert!(ws_installed.join("SKILL.md").is_file()); + assert!(ws_installed.join("scripts/deploy.sh").is_file()); + assert!(!ws_installed.join("escape-link").exists(), "out-of-root symlink omitted on disk"); + } + + #[test] + #[ignore = "hits live GitHub API"] + fn live_github_browse_preview_and_install_smoke() { + let fetcher = GitHubHttpFetcher::new(&ide_core::tool_path::search_path()).unwrap(); + let cancel = CancellationToken::new(); + let browsed = browse_skills(&fetcher, "l0ng-ai/openidea@verify", &cancel).unwrap(); + assert_eq!(browsed.skills.len(), 1); + assert_eq!(browsed.skills[0].folder_name, "verify"); + + let tmp = tempfile::tempdir().unwrap(); + let preview = preview_skill( + &fetcher, + RepoRef { owner: &browsed.owner, repo: &browsed.repo, git_ref: &browsed.git_ref }, + &browsed.skills[0].subpath, + Some(tmp.path()), + None, + &cancel, + ) + .unwrap(); + assert_eq!(preview.skill_name, "verify"); + assert!(preview.files.iter().any(|f| f.path == "SKILL.md")); + + let dest = install_skill( + &fetcher, + &InstallRemoteSkillRequest { + owner: browsed.owner, + repo: browsed.repo, + git_ref: browsed.git_ref, + subpath: browsed.skills[0].subpath.clone(), + scope: McpScope::User, + confirm_warnings: true, + overwrite: false, + }, + Some(tmp.path()), + None, + &cancel, + ) + .unwrap(); + assert!(dest.join("SKILL.md").is_file()); + } +} diff --git a/crates/ide-host/src/ops/agent.rs b/crates/ide-host/src/ops/agent.rs index b3a43e850..c34532930 100644 --- a/crates/ide-host/src/ops/agent.rs +++ b/crates/ide-host/src/ops/agent.rs @@ -8,8 +8,9 @@ use ide_agent::{ AcpImportOutcome, AgentAcceptOutcome, AgentChange, AgentChanges, AgentChangesView, AgentCliOffer, AgentCliUpdateResult, AgentCommand, AgentContextUsage, AgentEntry, AgentEvent, AgentModelCatalog, AgentProbe, AgentPrompt, AgentProvider, AgentQueued, AgentSkillMode, AgentSkillPolicy, AgentSkillState, AgentTask, - CustomAgentInput, ExternalSession, McpExternalSource, McpImportSelection, McpProbeResult, McpScope, McpServer, - McpServerInput, McpTransport, NewAgentTask, SkillsPolicy, + CustomAgentInput, ExternalSession, InstallRemoteSkillRequest, McpExternalSource, McpImportSelection, + McpProbeResult, McpScope, McpServer, McpServerInput, McpTransport, NewAgentTask, RemoteSkillBrowseResult, + RemoteSkillPreview, SkillsPolicy, }; use ide_command::command; use ide_core::error::ErrorKind; @@ -1116,6 +1117,114 @@ pub fn agent_skills_set_mode( )) } +/// Resolves a GitHub `owner/repo[/path][@ref]` source and lists the `SKILL.md` skills in it. +#[command(trust = always)] +pub async fn agent_skills_browse( + host: &Host, + source: String, + request: RequestId, +) -> IdeResult { + let cancel = host.begin_request(request); + let search_path = ide_core::tool_path::search_path(); + let res = tokio::task::spawn_blocking(move || { + let fetcher = ide_agent::skill_installer::GitHubHttpFetcher::new(&search_path)?; + ide_agent::skill_installer::browse_skills(&fetcher, &source, &cancel) + }) + .await + .map_err(join_err); + host.end_request(request); + res? +} + +/// Fetches one skill's `SKILL.md`, file list, and static security scan warnings before installation. +#[command(trust = always)] +pub async fn agent_skills_preview( + host: &Host, + window: &str, + owner: String, + repo: String, + git_ref: String, + subpath: String, + request: RequestId, +) -> IdeResult { + let cancel = host.begin_request(request); + let ws_root = host.project(window).ok().filter(|p| !p.light).map(|p| p.root.clone()); + let home = dirs::home_dir(); + let search_path = ide_core::tool_path::search_path(); + let res = tokio::task::spawn_blocking(move || { + let fetcher = ide_agent::skill_installer::GitHubHttpFetcher::new(&search_path)?; + ide_agent::skill_installer::preview_skill( + &fetcher, + ide_agent::skill_installer::RepoRef { owner: &owner, repo: &repo, git_ref: &git_ref }, + &subpath, + home.as_deref(), + ws_root.as_deref(), + &cancel, + ) + }) + .await + .map_err(join_err); + host.end_request(request); + res? +} + +/// Installs the selected skill subtree into `~/.agents/skills` (User) or `/.agents/skills` (Workspace). +#[command(trust = always)] +pub async fn agent_skills_install( + host: &Host, + window: &str, + input: InstallRemoteSkillRequest, + request: RequestId, +) -> IdeResult> { + let project = host.project(window).ok().filter(|p| !p.light); + if input.scope == McpScope::Workspace { + host.require_trust(window)?; + } + let ws_root = project.as_ref().map(|p| p.root.clone()); + let home = dirs::home_dir(); + let cancel = host.begin_request(request); + let search_path = ide_core::tool_path::search_path(); + let install_home = home.clone(); + let install_ws = ws_root.clone(); + let res = tokio::task::spawn_blocking(move || { + let fetcher = ide_agent::skill_installer::GitHubHttpFetcher::new(&search_path)?; + ide_agent::skill_installer::install_skill( + &fetcher, + &input, + install_home.as_deref(), + install_ws.as_deref(), + &cancel, + ) + }) + .await + .map_err(join_err); + host.end_request(request); + res??; + + let event = IdeEvent::SettingsChanged { key: "agents.skills".into() }; + for p in host.projects() { + p.agents.notify_mcp_servers_changed(); + p.sink.emit(event.clone()); + } + + let policy = host_skills_policy(host, project.as_deref()); + tokio::task::spawn_blocking(move || { + Ok(ide_agent::skills::resolve( + ide_agent::skills::discover_skills_with_plugins( + &policy.workspace_roots, + home.as_deref(), + &policy.plugin_dirs, + ), + None, + &policy.workspace, + &policy.user, + policy.workspace_trusted, + )) + }) + .await + .map_err(join_err)? +} + /// The user's and the workspace's skill policies (`[agents.skills.]`). fn skills_maps( host: &Host, @@ -1364,7 +1473,10 @@ ide_command::domain!( agent_set_env, agent_set_mcp_overrides, agent_set_skill_overrides, + agent_skills_browse, + agent_skills_install, agent_skills_list, + agent_skills_preview, agent_skills_set_mode, agent_stop_background, agent_subscribe, diff --git a/crates/ide-tools/src/process.rs b/crates/ide-tools/src/process.rs index 3d6844c79..279fb01c0 100644 --- a/crates/ide-tools/src/process.rs +++ b/crates/ide-tools/src/process.rs @@ -405,6 +405,13 @@ impl Http { self } + /// Skips `GITHUB_TOKEN` / `GH_TOKEN` / `gh auth token` (e.g. retrying a public GitHub endpoint after HTTP 401). + pub fn without_github_auth(mut self) -> Self { + self.gh = None; + self.explicit_github_token = None; + self + } + fn base(&self, url: &str, cancel: &CancellationToken) -> IdeResult<(Command, Option)> { if cancel.is_cancelled() { return Err(IdeError::cancelled()); diff --git a/e2e/agentSkills.spec.ts b/e2e/agentSkills.spec.ts index f45633353..e39f33a69 100644 --- a/e2e/agentSkills.spec.ts +++ b/e2e/agentSkills.spec.ts @@ -206,6 +206,90 @@ test("manages multi-level skills in Settings and per-session overrides via Compo ); return skills; }; + handlers.agent_skills_browse = () => ({ + owner: "vercel-labs", + repo: "agent-skills", + gitRef: "main", + treeSha: "9f8e7d6c", + truncated: false, + skills: [ + { folderName: "find-skills", subpath: "skills/find-skills", fileCount: 2, totalBytes: 1420 }, + { folderName: "web-design-audit", subpath: "skills/web-design-audit", fileCount: 1, totalBytes: 860 }, + ], + }); + handlers.agent_skills_preview = ({ subpath }) => + subpath === "skills/web-design-audit" + ? { + owner: "vercel-labs", + repo: "agent-skills", + gitRef: "main", + treeSha: "9f8e7d6c", + subpath: "skills/web-design-audit", + skillName: "web-design-audit", + description: "Audit responsive layout and contrast tokens", + skillMarkdown: + "---\nname: web-design-audit\ndescription: Audit responsive layout and contrast tokens\n---\n\nCheck spacing and contrast tokens.\n", + files: [{ path: "SKILL.md", size: 860, executable: false, symlink: false }], + scanWarnings: [], + alreadyInstalledUser: false, + alreadyInstalledWorkspace: false, + } + : { + owner: "vercel-labs", + repo: "agent-skills", + gitRef: "main", + treeSha: "9f8e7d6c", + subpath: "skills/find-skills", + skillName: "find-skills", + description: "Discover reusable SKILL.md workflows and inspect bundled scripts", + skillMarkdown: + "---\nname: find-skills\ndescription: Discover reusable SKILL.md workflows and inspect bundled scripts\nallowed-tools:\n - Bash(gh:*)\n---\n\nInspect candidate SKILL.md files before installing.\n", + files: [ + { path: "SKILL.md", size: 980, executable: false, symlink: false }, + { path: "scripts/inspect.sh", size: 440, executable: true, symlink: false }, + ], + scanWarnings: [ + { + kind: "executableScript", + file: "scripts/inspect.sh", + message: "Contains executable file mode (100755)", + }, + { + kind: "allowedToolsShell", + file: "SKILL.md", + message: "`allowed-tools` grants shell/command execution (`Bash(gh:*)`)", + }, + ], + alreadyInstalledUser: false, + alreadyInstalledWorkspace: false, + }; + handlers.agent_skills_install = ({ input }) => { + const req = input as { subpath: string; scope: "user" | "workspace" }; + const installedName = req.subpath.endsWith("web-design-audit") ? "web-design-audit" : "find-skills"; + skills = [ + ...skills, + { + skill: { + name: installedName, + description: "Discover reusable SKILL.md workflows and inspect bundled scripts", + hint: null, + path: + req.scope === "workspace" + ? `/dev/openidea/.agents/skills/${installedName}` + : `/Users/dev/.agents/skills/${installedName}`, + source: req.scope === "workspace" ? "workspace" : "user", + alsoIn: [], + userInvocable: true, + disableModelInvocation: false, + valid: true, + warnings: [], + }, + mode: "auto", + origin: "none", + }, + ]; + return skills; + }; handlers.agent_set_skill_overrides = ({ overrides }) => { currentTask = { ...currentTask, skillOverrides: overrides as Record }; return currentTask; @@ -282,4 +366,31 @@ test("manages multi-level skills in Settings and per-session overrides via Compo await settingsDialog.getByRole("tab", { name: /All Sources/ }).click(); await settingsDialog.screenshot({ path: `${screenshotDir}/after-settings-skills.png` }); } + + // Open Install Skill… dialog, fetch repository, verify static scan warning gate, confirm, and install. + await settingsDialog.getByRole("button", { name: "Install Skill…" }).click(); + const installDialog = page.getByRole("dialog", { name: "Install Agent Skill" }); + await expect(installDialog).toBeVisible(); + await installDialog.getByLabel("Skill repository").fill("vercel-labs/agent-skills"); + await installDialog.getByRole("button", { name: "Fetch" }).click(); + + await expect(installDialog.getByText("Static security scan found 2 warnings")).toBeVisible(); + const installBtn = installDialog.getByRole("button", { name: "Install", exact: true }); + await expect(installBtn).toBeDisabled(); + + if (screenshotDir) { + await installDialog.screenshot({ path: `${screenshotDir}/after-install-skill-dialog.png` }); + } + + await installDialog.getByLabel("I reviewed these warnings and want to install this skill").click(); + await expect(installBtn).toBeEnabled(); + await installBtn.click(); + + await expect(installDialog).toHaveCount(0); + await settingsDialog.getByRole("tab", { name: /^User/ }).click(); + await expect(settingsDialog.getByText("find-skills")).toBeVisible(); + + if (screenshotDir) { + await settingsDialog.screenshot({ path: `${screenshotDir}/after-settings-skills-installed.png` }); + } }); diff --git a/src-tauri/tests/commands.snap b/src-tauri/tests/commands.snap index 8d25b7ad7..fc11b1a05 100644 --- a/src-tauri/tests/commands.snap +++ b/src-tauri/tests/commands.snap @@ -48,7 +48,10 @@ agent_set_env trusted project_host bin agent_set_mcp_overrides always project_host bindings sync agent_set_skill_overrides always project_host bindings sync agent_shell trusted project_host bindings async +agent_skills_browse always project_host bindings async +agent_skills_install always project_host bindings async agent_skills_list always project_host bindings async +agent_skills_preview always project_host bindings async agent_skills_set_mode always project_host bindings sync agent_stop_background always project_host bindings async agent_subscribe always project_host bindings sync diff --git a/src/bindings.ts b/src/bindings.ts index 7653741a5..c5cb0d592 100644 --- a/src/bindings.ts +++ b/src/bindings.ts @@ -150,11 +150,17 @@ export const commands = { * starting a turn; its output goes to the agent with the next message. */ agentShell: (task: string, command: string, request: RequestId) => typedError(__TAURI_INVOKE("agent_shell", { task, command, request })), + /** Resolves a GitHub `owner/repo[/path][@ref]` source and lists the `SKILL.md` skills in it. */ + agentSkillsBrowse: (source: string, request: RequestId) => typedError(__TAURI_INVOKE("agent_skills_browse", { source, request })), + /** Installs the selected skill subtree into `~/.agents/skills` (User) or `/.agents/skills` (Workspace). */ + agentSkillsInstall: (input: InstallRemoteSkillRequest, request: RequestId) => typedError(__TAURI_INVOKE("agent_skills_install", { input, request })), /** * Every skill discovered in the project, the user's folders and enabled plugins, with * the mode a session runs it with (without any session's own overrides). */ agentSkillsList: () => typedError(__TAURI_INVOKE("agent_skills_list")), + /** Fetches one skill's `SKILL.md`, file list, and static security scan warnings before installation. */ + agentSkillsPreview: (owner: string, repo: string, gitRef: string, subpath: string, request: RequestId) => typedError(__TAURI_INVOKE("agent_skills_preview", { owner, repo, gitRef, subpath, request })), /** * Sets how `name` may run in `scope`, in the settings file of that scope, and returns the * updated list. `SKILL.md` files are never touched; the default `auto` removes the entry. @@ -7791,6 +7797,18 @@ export type InstallPreview = { needsConsent: boolean, }; +export type InstallRemoteSkillRequest = { + owner: string, + repo: string, + gitRef: string, + subpath: string, + scope: McpScope, + /** Must be `true` when the static security scan produces any warnings. */ + confirmWarnings?: boolean, + /** Must be `true` to replace an existing folder of the same name in `scope`. */ + overwrite?: boolean, +}; + /** The settings folder of a JetBrains IDE (or Android Studio) of this user. */ export type InstalledIdeSettings = { /** The IDE and its version: `IntelliJ IDEA 2025.2`. */ @@ -10939,6 +10957,47 @@ export type RemoteSession = { commands: string[], }; +export type RemoteSkillBrowseResult = { + owner: string, + repo: string, + gitRef: string, + treeSha: string, + truncated: boolean, + skills: RemoteSkillCandidate[], +}; + +export type RemoteSkillCandidate = { + /** Target folder name (`` under `.agents/skills/`). */ + folderName: string, + /** Repository-relative directory holding `SKILL.md` (`""` when at repository root). */ + subpath: string, + fileCount: number, + totalBytes: number, +}; + +export type RemoteSkillFile = { + /** Path relative to the skill folder (e.g. `"SKILL.md"`, `"scripts/check.sh"`). */ + path: string, + size: number, + executable: boolean, + symlink: boolean, +}; + +export type RemoteSkillPreview = { + owner: string, + repo: string, + gitRef: string, + treeSha: string, + subpath: string, + skillName: string, + description: string, + skillMarkdown: string, + files: RemoteSkillFile[], + scanWarnings: SkillScanWarning[], + alreadyInstalledUser: boolean, + alreadyInstalledWorkspace: boolean, +}; + /** Where [`remote_connect`] is (`fraction`: 0..1 of a download / upload). */ export type RemoteStage = "connecting" | /** `devcontainer up`: building or starting the container. */ @@ -11819,6 +11878,14 @@ export type SizeSource = */ "addressGaps" | "layout"; +export type SkillScanWarning = { + kind: SkillScanWarningKind, + file: string, + message: string, +}; + +export type SkillScanWarningKind = "hiddenUnicode" | "outOfRootSymlink" | "executableScript" | "allowedToolsShell"; + export type SkipReason = /** A configuration with this name already exists. */ { kind: "nameTaken" } | diff --git a/src/features/agents/InstallSkillDialog.tsx b/src/features/agents/InstallSkillDialog.tsx new file mode 100644 index 000000000..e1a3e26e9 --- /dev/null +++ b/src/features/agents/InstallSkillDialog.tsx @@ -0,0 +1,392 @@ +import clsx from "clsx"; +import { useCallback, useEffect, useRef, useState } from "react"; +import type { + AgentSkillState, + McpScope, + RemoteSkillBrowseResult, + RemoteSkillCandidate, + RemoteSkillPreview, + RequestId, + SkillScanWarningKind, +} from "@/bindings"; +import { formatMessage, useTx } from "@/platform/i18n"; +import { commands, isCancelled, nextRequestId } from "@/platform/ipc"; +import { Button, Checkbox, Dialog, Icon, Input, Select, Tabs, T, selectableText } from "@/ui"; +import type { SkillsBackend } from "./skillsModel"; +import page from "./SettingsPage.module.css"; +import styles from "./SkillsPage.module.css"; + +type PreviewTab = "markdown" | "files"; + +function formatBytes(bytes: number): string { + if (bytes >= 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MiB`; + if (bytes >= 1024) return `${(bytes / 1024).toFixed(1)} KiB`; + return `${bytes} B`; +} + +const WARNING_KIND_LABELS: Record = { + hiddenUnicode: "Hidden Unicode", + outOfRootSymlink: "Out-of-root symlink", + executableScript: "Executable script", + allowedToolsShell: "Shell tool grant", +}; + +export function InstallSkillDialog({ + backend, + hasTrustedProject, + onInstalled, + onClose, +}: { + backend: SkillsBackend; + hasTrustedProject: boolean; + onInstalled: (next: AgentSkillState[]) => void; + onClose: () => void; +}) { + const tx = useTx(); + const [source, setSource] = useState(""); + const [scope, setScope] = useState("user"); + const [browseResult, setBrowseResult] = useState(null); + const [selectedSubpath, setSelectedSubpath] = useState(null); + const [preview, setPreview] = useState(null); + const [previewTab, setPreviewTab] = useState("markdown"); + const [confirmedWarnings, setConfirmedWarnings] = useState(false); + const [overwrite, setOverwrite] = useState(false); + const [loading, setLoading] = useState(false); + const [installing, setInstalling] = useState(false); + const [error, setError] = useState(null); + const activeReq = useRef(null); + + const cancelInFlight = useCallback(() => { + if (activeReq.current != null) { + void commands.cancelRequest?.(activeReq.current); + activeReq.current = null; + } + }, []); + + useEffect(() => () => cancelInFlight(), [cancelInFlight]); + + const loadPreview = useCallback( + async (res: RemoteSkillBrowseResult, candidate: RemoteSkillCandidate) => { + if (!backend.preview) return; + cancelInFlight(); + const req = nextRequestId(); + activeReq.current = req; + setSelectedSubpath(candidate.subpath); + setPreview(null); + setConfirmedWarnings(false); + setOverwrite(false); + setError(null); + setLoading(true); + try { + const loaded = await backend.preview(res.owner, res.repo, res.gitRef, candidate.subpath, req); + if (activeReq.current === req) { + setPreview(loaded); + activeReq.current = null; + } + } catch (e: unknown) { + if (!isCancelled(e)) { + setError(e instanceof Error ? e.message : String(e)); + } + } finally { + if (activeReq.current === req || activeReq.current == null) { + setLoading(false); + } + } + }, + [backend, cancelInFlight], + ); + + const fetchRepository = useCallback(async () => { + const trimmed = source.trim(); + if (!trimmed || !backend.browse) return; + cancelInFlight(); + const req = nextRequestId(); + activeReq.current = req; + setLoading(true); + setError(null); + setBrowseResult(null); + setPreview(null); + setSelectedSubpath(null); + setConfirmedWarnings(false); + setOverwrite(false); + try { + const res = await backend.browse(trimmed, req); + if (activeReq.current !== req) return; + activeReq.current = null; + setBrowseResult(res); + const first = res.skills[0]; + if (first) { + await loadPreview(res, first); + } else { + setLoading(false); + } + } catch (e: unknown) { + if (!isCancelled(e)) { + setError(e instanceof Error ? e.message : String(e)); + } + setLoading(false); + } + }, [backend, cancelInFlight, loadPreview, source]); + + const alreadyExists = + preview != null && (scope === "workspace" ? preview.alreadyInstalledWorkspace : preview.alreadyInstalledUser); + const needsWarningConfirm = (preview?.scanWarnings.length ?? 0) > 0; + const canInstall = + !!preview && + !loading && + !installing && + (!needsWarningConfirm || confirmedWarnings) && + (!alreadyExists || overwrite); + + const handleInstall = async () => { + if (!preview || !backend.install || !canInstall) return; + cancelInFlight(); + const req = nextRequestId(); + activeReq.current = req; + setInstalling(true); + setError(null); + try { + const next = await backend.install( + { + owner: preview.owner, + repo: preview.repo, + gitRef: preview.gitRef, + subpath: preview.subpath, + scope, + confirmWarnings: confirmedWarnings, + overwrite, + }, + req, + ); + activeReq.current = null; + onInstalled(next); + } catch (e: unknown) { + if (!isCancelled(e)) { + setError(e instanceof Error ? e.message : String(e)); + } + } finally { + setInstalling(false); + } + }; + + const handleClose = () => { + cancelInFlight(); + onClose(); + }; + + return ( + void handleInstall(), + }, + { label: "Cancel", role: "cancel", onClick: handleClose }, + ]} + > +
+

+ + Enter a GitHub repository (owner/repo, owner/repo@skill, owner/repo/path@ref, or a GitHub URL). Only the + chosen skill folder is downloaded after preview and static scan. + +

+ +
+ setSource(e.target.value)} + onKeyDown={(e) => { + if (e.key === "Enter") { + e.preventDefault(); + e.stopPropagation(); + void fetchRepository(); + } + }} + placeholder={tx("owner/repo, owner/repo@skill, or GitHub URL")} + aria-label={tx("Skill repository")} + autoFocus + /> + +
+ +
+ + Install location + +