vtcode-core 0.183.1

Core library for VT Code - a Rust-based terminal coding agent
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Plugin component handlers for VT Code
//!
//! This module handles the different types of components that plugins can provide:
//! - Commands (slash commands)
//! - Agents (specialized profiles)
//! - Skills (model-invoked capabilities)
//! - Hooks (event handlers)
//! - MCP servers (Model Context Protocol)

use std::path::{Path, PathBuf};

use anyhow::Result;
use tokio::fs;

use crate::plugins::PluginManifest;

/// Handler for plugin commands (slash commands)
pub struct CommandsHandler;

impl CommandsHandler {
    /// Process plugin commands from the plugin directory
    pub async fn process_commands(plugin_path: &Path, manifest_commands: Option<Vec<String>>) -> Result<Vec<PathBuf>> {
        let mut command_files = Vec::new();

        // Add commands from manifest paths
        if let Some(manifest_paths) = manifest_commands {
            for path in manifest_paths {
                let full_path = plugin_path.join(&path);
                if fs::metadata(&full_path)
                    .await
                    .map(|metadata| metadata.is_file())
                    .unwrap_or(false)
                {
                    command_files.push(full_path);
                }
            }
        }

        // Also look for commands in the default commands/ directory
        let default_commands_dir = plugin_path.join("commands");
        if fs::metadata(&default_commands_dir)
            .await
            .map(|metadata| metadata.is_dir())
            .unwrap_or(false)
        {
            let mut entries = fs::read_dir(&default_commands_dir).await?;
            while let Some(entry) = entries.next_entry().await? {
                let path = entry.path();
                if fs::metadata(&path).await.map(|metadata| metadata.is_file()).unwrap_or(false)
                    && path.extension().is_some_and(|ext| ext == "md")
                {
                    command_files.push(path);
                }
            }
        }

        Ok(command_files)
    }
}

/// Handler for plugin agents.
pub struct AgentsHandler;

impl AgentsHandler {
    /// Process plugin agents from the plugin directory.
    ///
    /// Manifest entries must stay inside the plugin root: absolute paths and
    /// parent-directory escapes are skipped, and symlinks are never followed,
    /// so a crafted manifest cannot smuggle outside files into agent specs.
    pub async fn process_agents(plugin_path: &Path, manifest_agents: Option<Vec<String>>) -> Result<Vec<PathBuf>> {
        let mut agent_files = Vec::new();

        // Add agents from manifest paths
        if let Some(manifest_paths) = manifest_agents {
            for path in manifest_paths {
                let relative = Path::new(&path);
                if relative.is_absolute()
                    || relative
                        .components()
                        .any(|component| matches!(component, std::path::Component::ParentDir))
                {
                    continue;
                }
                let full_path = plugin_path.join(relative);
                if fs::symlink_metadata(&full_path)
                    .await
                    .map(|metadata| metadata.is_file())
                    .unwrap_or(false)
                {
                    agent_files.push(full_path);
                }
            }
        }

        // Also look for agents in the default agents/ directory
        let default_agents_dir = plugin_path.join("agents");
        if fs::symlink_metadata(&default_agents_dir)
            .await
            .map(|metadata| metadata.is_dir())
            .unwrap_or(false)
        {
            let mut entries = fs::read_dir(&default_agents_dir).await?;
            while let Some(entry) = entries.next_entry().await? {
                let path = entry.path();
                if fs::symlink_metadata(&path)
                    .await
                    .map(|metadata| metadata.is_file())
                    .unwrap_or(false)
                    && path.extension().is_some_and(|ext| ext == "md")
                {
                    agent_files.push(path);
                }
            }
        }

        Ok(agent_files)
    }
}

/// Handler for plugin skills
pub struct SkillsHandler;

impl SkillsHandler {
    /// Process plugin skills from the plugin directory
    pub async fn process_skills(plugin_path: &Path, manifest_skills: Option<Vec<String>>) -> Result<Vec<PathBuf>> {
        let mut skill_dirs = Vec::new();

        // Add skills from manifest paths
        if let Some(manifest_paths) = manifest_skills {
            for path in manifest_paths {
                let full_path = plugin_path.join(&path);
                if fs::metadata(&full_path)
                    .await
                    .map(|metadata| metadata.is_dir())
                    .unwrap_or(false)
                {
                    skill_dirs.push(full_path);
                }
            }
        }

        // Also look for skills in the default skills/ directory
        let default_skills_dir = plugin_path.join("skills");
        if fs::metadata(&default_skills_dir)
            .await
            .map(|metadata| metadata.is_dir())
            .unwrap_or(false)
        {
            let mut entries = fs::read_dir(&default_skills_dir).await?;
            while let Some(entry) = entries.next_entry().await? {
                let path = entry.path();
                if fs::metadata(&path).await.map(|metadata| metadata.is_dir()).unwrap_or(false) {
                    // Check if it contains a SKILL.md file
                    let skill_md = path.join("SKILL.md");
                    if fs::metadata(&skill_md)
                        .await
                        .map(|metadata| metadata.is_file())
                        .unwrap_or(false)
                    {
                        skill_dirs.push(path);
                    }
                }
            }
        }

        Ok(skill_dirs)
    }
}

/// Handler for plugin hooks
pub struct HooksHandler;

impl HooksHandler {
    /// Process plugin hooks from the plugin directory
    pub async fn process_hooks(
        plugin_path: &Path,
        manifest_hooks: Option<serde_json::Value>,
    ) -> Result<Option<PathBuf>> {
        // Check for hooks in manifest
        if let Some(hooks_config) = manifest_hooks {
            // If hooks config is a string, treat it as a path
            if let Some(path_str) = hooks_config.as_str() {
                let hooks_path = plugin_path.join(path_str);
                if fs::metadata(&hooks_path)
                    .await
                    .map(|metadata| metadata.is_file())
                    .unwrap_or(false)
                {
                    return Ok(Some(hooks_path));
                }
            }
        }

        // Look for hooks in the default hooks/ directory
        let default_hooks_path = plugin_path.join("hooks/hooks.json");
        if fs::metadata(&default_hooks_path)
            .await
            .map(|metadata| metadata.is_file())
            .unwrap_or(false)
        {
            return Ok(Some(default_hooks_path));
        }

        Ok(None)
    }
}

/// Handler for plugin MCP servers
pub struct McpServersHandler;

impl McpServersHandler {
    /// Process plugin MCP servers from the plugin directory
    pub async fn process_mcp_servers(
        plugin_path: &Path,
        manifest_mcp: Option<serde_json::Value>,
    ) -> Result<Option<PathBuf>> {
        // Check for MCP config in manifest
        if let Some(mcp_config) = manifest_mcp {
            // If MCP config is a string, treat it as a path
            if let Some(path_str) = mcp_config.as_str() {
                let mcp_path = plugin_path.join(path_str);
                if fs::metadata(&mcp_path)
                    .await
                    .map(|metadata| metadata.is_file())
                    .unwrap_or(false)
                {
                    return Ok(Some(mcp_path));
                }
            }
        }

        // Look for MCP config in the default .mcp.json file
        let default_mcp_path = plugin_path.join(".mcp.json");
        if fs::metadata(&default_mcp_path)
            .await
            .map(|metadata| metadata.is_file())
            .unwrap_or(false)
        {
            return Ok(Some(default_mcp_path));
        }

        Ok(None)
    }
}

/// Handler for plugin LSP servers
pub struct LspServersHandler;

impl LspServersHandler {
    /// Process plugin LSP servers from the plugin directory
    pub async fn process_lsp_servers(
        plugin_path: &Path,
        manifest_lsp: Option<serde_json::Value>,
    ) -> Result<Option<PathBuf>> {
        // Check for LSP config in manifest
        if let Some(lsp_config) = manifest_lsp {
            // If LSP config is a string, treat it as a path
            if let Some(path_str) = lsp_config.as_str() {
                let lsp_path = plugin_path.join(path_str);
                if fs::metadata(&lsp_path)
                    .await
                    .map(|metadata| metadata.is_file())
                    .unwrap_or(false)
                {
                    return Ok(Some(lsp_path));
                }
            }
        }

        // Look for LSP config in the default .lsp.json file
        let default_lsp_path = plugin_path.join(".lsp.json");
        if fs::metadata(&default_lsp_path)
            .await
            .map(|metadata| metadata.is_file())
            .unwrap_or(false)
        {
            return Ok(Some(default_lsp_path));
        }

        Ok(None)
    }
}

/// A comprehensive handler that processes all plugin components
pub struct PluginComponentsHandler;

impl PluginComponentsHandler {
    /// Process all components for a plugin
    pub async fn process_all_components<P: AsRef<Path>>(
        plugin_path: P,
        manifest: &PluginManifest,
    ) -> Result<PluginComponents> {
        let path_buf = plugin_path.as_ref().to_path_buf();
        let commands = CommandsHandler::process_commands(&path_buf, manifest.commands.clone()).await?;

        let agents = AgentsHandler::process_agents(&path_buf, manifest.agents.clone()).await?;

        let skills = SkillsHandler::process_skills(&path_buf, manifest.skills.clone()).await?;

        let hooks = HooksHandler::process_hooks(
            &path_buf,
            manifest.hooks.as_ref().map(|h| match h {
                crate::plugins::manifest::HookConfig::Path(path) => serde_json::Value::String(path.clone()),
                crate::plugins::manifest::HookConfig::Inline(_) => serde_json::Value::Null, // For inline, we'll handle separately
            }),
        )
        .await?;

        let mcp_servers = McpServersHandler::process_mcp_servers(
            &path_buf,
            manifest.mcp_servers.as_ref().map(|m| match m {
                crate::plugins::manifest::McpServerConfig::Path(path) => serde_json::Value::String(path.clone()),
                crate::plugins::manifest::McpServerConfig::Inline(_) => serde_json::Value::Null, // For inline, we'll handle separately
            }),
        )
        .await?;

        let lsp_servers = LspServersHandler::process_lsp_servers(
            &path_buf,
            manifest.lsp_servers.as_ref().map(|l| match l {
                crate::plugins::manifest::LspServerConfig::Path(path) => serde_json::Value::String(path.clone()),
                crate::plugins::manifest::LspServerConfig::Inline(_) => serde_json::Value::Null, // For inline, we'll handle separately
            }),
        )
        .await?;

        Ok(PluginComponents {
            commands,
            agents,
            skills,
            hooks,
            mcp_servers,
            lsp_servers,
        })
    }
}

/// Structure containing all plugin components
pub struct PluginComponents {
    pub commands: Vec<PathBuf>,
    pub agents: Vec<PathBuf>,
    pub skills: Vec<PathBuf>,
    pub hooks: Option<PathBuf>,
    pub mcp_servers: Option<PathBuf>,
    pub lsp_servers: Option<PathBuf>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    /// Plugin root at `<dir>/plugin` with a sibling file outside it, so
    /// `../sibling.md` escapes the root while staying inside the temp dir.
    async fn plugin_fixture() -> (TempDir, PathBuf) {
        let dir = TempDir::new().expect("temp dir");
        let root = dir.path().join("plugin");
        fs::create_dir_all(root.join("agents")).await.expect("agents dir");
        fs::create_dir_all(root.join("nested")).await.expect("nested dir");
        fs::write(root.join("agents").join("ok.md"), "ok").await.expect("write ok");
        fs::write(root.join("agents").join("notes.txt"), "not markdown")
            .await
            .expect("write txt");
        fs::write(root.join("nested").join("custom.md"), "custom")
            .await
            .expect("write custom");
        fs::write(dir.path().join("sibling.md"), "outside")
            .await
            .expect("write sibling");
        (dir, root)
    }

    #[tokio::test]
    async fn manifest_agent_paths_stay_contained() {
        let (_dir, root) = plugin_fixture().await;
        let outside = _dir.path().join("sibling.md");

        let files = AgentsHandler::process_agents(
            &root,
            Some(vec![
                "nested/custom.md".to_string(),
                "../sibling.md".to_string(),
                outside.to_string_lossy().into_owned(),
                "missing.md".to_string(),
            ]),
        )
        .await
        .expect("process");

        assert!(files.iter().all(|p| p.starts_with(&root)), "all results stay under the plugin root: {files:?}");
        assert!(files.contains(&root.join("nested/custom.md")), "legit nested entry kept: {files:?}");
        assert!(files.contains(&root.join("agents/ok.md")), "default dir scan kept: {files:?}");
        assert!(
            !files.iter().any(|p| p.ends_with("sibling.md")),
            "traversal and absolute entries dropped: {files:?}"
        );
        assert!(!files.iter().any(|p| p.ends_with("notes.txt")), "non-markdown default files skipped: {files:?}");
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn manifest_agent_symlinks_are_skipped() {
        let (_dir, root) = plugin_fixture().await;
        let outside = _dir.path().join("sibling.md");
        std::os::unix::fs::symlink(&outside, root.join("agents").join("link.md")).expect("symlink");

        let files = AgentsHandler::process_agents(&root, Some(vec!["agents/link.md".to_string()]))
            .await
            .expect("process");

        assert!(
            !files.iter().any(|p| p.ends_with("link.md")),
            "symlinked entries must not escape via following: {files:?}"
        );
        assert!(files.contains(&root.join("agents/ok.md")), "regular files still collected: {files:?}");
    }
}