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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ fn connect_ext_tools(prepared: &Prepared) {
for line in said {
render::print_info(&line);
}
crate::plugins::offer_engine_plugins(&prepared.workspace, &prepared.session.session_tools());
let on_connected: crate::toolbox::OnConnected =
std::sync::Arc::new(|line| crate::trace!("tool", "{line}"));
for line in prepared.session.session_tools().connect_client_servers(
Expand Down
61 changes: 61 additions & 0 deletions src/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,46 @@ fn hooks_of(plugins: &[Plugin]) -> crate::hooks::HookConfig {
all
}

/// Offer the memory engine's plugin tool servers through `tools`
/// ([`crate::toolbox::engine_plugins`]), leaving out each one a usable local
/// plugin already runs ([`local_server_keys`]) — the local one is started by
/// [`session_servers`], with its hooks and its own keys, so none runs twice.
/// Only for a session that has the engine, as every memory request is.
///
/// Called beside [`session_servers`] by `cli::connect_ext_tools` (REPL) and
/// `task_run::connect_ext_tools` (`-t`).
pub(crate) fn offer_engine_plugins(workspace: &Path, tools: &crate::toolbox::SessionTools) {
if crate::hooks::engine::Engine::locate().is_none() {
return;
}
let local = local_server_keys(workspace);
if let Some(index) = crate::toolbox::engine_plugins::fetch_index(&local) {
tools.set_engine_plugins(index);
}
}

/// The server names, as each plugin's own file writes them, of every usable
/// plugin's MCP servers for `workspace`: `db` for the server `web_db`.
#[must_use]
pub(crate) fn local_server_keys(workspace: &Path) -> Vec<String> {
server_keys(&discover_usable(workspace).plugins)
}

/// [`local_server_keys`] of these plugins.
fn server_keys(plugins: &[Plugin]) -> Vec<String> {
plugins
.iter()
.flat_map(|plugin| {
let prefix = format!("{}_", plugin.manifest.name);
plugin
.servers()
.unwrap_or_default()
.into_iter()
.filter_map(move |spec| spec.name.strip_prefix(&prefix).map(str::to_string))
})
.collect()
}

/// Every plugin's MCP servers for `workspace`, and a line for each that could
/// not be read.
#[must_use]
Expand Down Expand Up @@ -1210,6 +1250,27 @@ mod tests {
assert!(shown.contains("4 hook(s)"), "{shown}");
}

/// **A LOCAL PLUGIN'S SERVERS ARE NAMED AS ITS OWN FILE NAMES THEM**, so
/// the memory engine's copy of the same server is left out of its index
/// (`offer_engine_plugins`) and not run twice.
#[test]
fn a_local_plugins_servers_are_named_as_its_file_names_them() {
let dir = tempfile::tempdir().expect("dir");
let root = plugin(dir.path(), "review-kit", "Reviews");
write(
&root.join(".mcp.json"),
r#"{"mcpServers": {"greptile": {"command": "g"}, "db": {"url": "http://x/mcp"}}}"#,
);
let bare = plugin(dir.path(), "bare", "No servers");
let plugins = [
load_one(&root, Scope::User).expect("loads"),
load_one(&bare, Scope::User).expect("loads"),
];
let mut keys = server_keys(&plugins);
keys.sort();
assert_eq!(keys, ["db", "greptile"]);
}

#[test]
fn skills_hooks_and_servers_are_read_from_the_folder() {
let dir = tempfile::tempdir().expect("dir");
Expand Down
59 changes: 54 additions & 5 deletions src/skills.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,24 @@ pub fn skills_url() -> String {
pub fn parse(body: &str) -> Result<Skillset, String> {
let value: serde_json::Value = serde_json::from_str(body)
.map_err(|e| format!("the skill store's answer did not parse: {e}"))?;
let mut discovered: Vec<Skill> = value
.get("discovered")
.map(discovered_from)
.unwrap_or_default();
// Installed plugins' skills, which the store lists only in its `catalog`:
// named `<plugin>:<skill>`, on while their plugin is.
for skill in value
.get("catalog")
.map(discovered_from)
.unwrap_or_default()
{
if skill.name.contains(':') && !discovered.iter().any(|s| s.name == skill.name) {
discovered.push(skill);
}
}
Ok(Skillset {
managed: value.get("managed").map(managed_from).unwrap_or_default(),
discovered: value
.get("discovered")
.map(discovered_from)
.unwrap_or_default(),
discovered,
})
}

Expand Down Expand Up @@ -192,7 +204,12 @@ pub fn load(root: &Path) -> (Skillset, Source) {
let (mut set, source) = choose(fetched, root);
// Plugins' skills are folders like any installed one, read from disk
// rather than the store, so they are added after the cache is written.
set.discovered.extend(crate::plugins::session_skills());
// A plugin installed in incode wins over the store's copy of the same
// skill, as its servers win over the engine's (`plugins::offer_engine_plugins`).
let local = crate::plugins::session_skills();
set.discovered
.retain(|skill| !local.iter().any(|own| own.name == skill.name));
set.discovered.extend(local);
(set, source)
}

Expand Down Expand Up @@ -490,6 +507,38 @@ mod tests {
);
}

/// **AN INSTALLED PLUGIN'S SKILLS COME FROM THE STORE'S CATALOG** —
/// named `<plugin>:<skill>`, while their plugin is on — and nothing else
/// in the catalog is added, nor a skill already listed.
#[test]
fn plugin_skills_are_taken_from_the_catalog_while_their_plugin_is_on() {
let answer = r#"{
"managed": [],
"discovered": [{"name": "kit:shared", "description": "listed already", "path": "/a"}],
"catalog": [
{"name": "kit:review", "description": "Review a change", "enabled": true,
"path": "/plugins/kit/1.0.0/skills/review/SKILL.md", "source": "claude"},
{"name": "off:design", "description": "Its plugin is off", "enabled": false,
"path": "/plugins/off/skills/design/SKILL.md", "source": "claude"},
{"name": "aframe", "description": "Not a plugin's", "enabled": true,
"path": "/agents/skills/aframe/SKILL.md", "source": "agents"},
{"name": "kit:shared", "description": "listed twice", "enabled": true,
"path": "/b", "source": "claude"}
]}"#;
let set = parse(answer).expect("parses");
let names: Vec<&str> = set.discovered.iter().map(|s| s.name.as_str()).collect();
assert_eq!(names, ["kit:shared", "kit:review"]);
assert_eq!(set.discovered[0].path, "/a");
assert_eq!(
set.discovered[1].path,
"/plugins/kit/1.0.0/skills/review/SKILL.md"
);
assert!(
set.discovered[1].body.is_empty(),
"read only when asked for"
);
}

fn managed(surfaces: &str, enabled: &str) -> String {
format!(
r#"{{"managed":[{{"name":"S","body":"do the thing"{surfaces}{enabled}}}],"discovered":[]}}"#
Expand Down
1 change: 1 addition & 0 deletions src/task_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ fn connect_ext_tools(workspace: &Path) -> std::sync::Arc<crate::toolbox::Session
for line in said {
eprintln!("incode: {line}");
}
crate::plugins::offer_engine_plugins(workspace, &tools);
let on_connected: crate::toolbox::OnConnected =
std::sync::Arc::new(|line| crate::trace!("tool", "{line}"));
for line in tools.connect_client_servers(specs, workspace, &on_connected) {
Expand Down
Loading
Loading