Skip to main content

session_core/local_resources/
extensions.rs

1//! The Pi extensions TypeScript would load from a working directory and profile (`core/package-manager.ts`
2//! `resolve()`, its top-level `extensions`): the settings' `extensions` entries (paths and `!`/`+`/`-` patterns,
3//! project scope first), then `<cwd>/.pi/extensions/` (a trusted project only) and `<agentDir>/extensions/`. Pi
4//! extensions are not supported by ultron-rs (removed 2026-10-19): none is loaded, and a session's startup names
5//! the ones it ignores.
6
7use super::{LocalResources, PathMetadata, ResolvedResource, Scope, files, rank, string_list};
8use ::resources::paths::canonicalize;
9use indexmap::IndexMap;
10
11impl LocalResources {
12    /// The top-level extensions by precedence, each with whether its filters enable it (`resolve().extensions`
13    /// without the package ones: Pi packages are not supported either).
14    pub fn extensions(&self) -> Vec<ResolvedResource> {
15        let mut acc: IndexMap<String, (PathMetadata, bool)> = IndexMap::new();
16        let mut add = |path: &str, metadata: &PathMetadata, enabled: bool| {
17            if !path.is_empty() {
18                acc.entry(path.to_string())
19                    .or_insert_with(|| (metadata.clone(), enabled));
20            }
21        };
22        let global_base = self.agent_dir.clone();
23        let project_base = jsrt::path::join(&[&self.cwd, config::paths::CONFIG_DIR_NAME]);
24        let entries = |scope: Scope| {
25            let s = match scope {
26                Scope::User => self.settings.global_settings(),
27                Scope::Project => self.settings.project_settings(),
28            };
29            string_list(s.get("extensions")).unwrap_or_default()
30        };
31        for (scope, base) in [(Scope::Project, &project_base), (Scope::User, &global_base)] {
32            let list = entries(scope);
33            let (plain, patterns): (Vec<String>, Vec<String>) = list.into_iter().partition(|e| !files::is_pattern(e));
34            let resolved: Vec<String> = plain.iter().map(|p| self.resolve_from(p, base)).collect();
35            let found = files::extension_files_from_paths(&resolved);
36            let enabled = files::apply_patterns(&found, &patterns, base);
37            let m = PathMetadata {
38                source: "local".into(),
39                scope,
40                base_dir: None,
41            };
42            for f in &found {
43                add(f, &m, enabled.contains(f));
44            }
45        }
46        let mut auto = |scope: Scope, base: &str| {
47            let m = PathMetadata {
48                source: "auto".into(),
49                scope,
50                base_dir: Some(base.to_string()),
51            };
52            let overrides = entries(scope);
53            for p in files::auto_extensions(&jsrt::path::join(&[base, "extensions"])) {
54                let enabled = files::enabled_by_overrides(&p, &overrides, base);
55                add(&p, &m, enabled);
56            }
57        };
58        if self.settings.is_project_trusted() {
59            auto(Scope::Project, &project_base);
60        }
61        auto(Scope::User, &global_base);
62        let mut list: Vec<ResolvedResource> = acc
63            .into_iter()
64            .map(|(path, (metadata, enabled))| ResolvedResource {
65                path,
66                enabled,
67                metadata,
68            })
69            .collect();
70        list.sort_by_key(|r| rank(&r.metadata));
71        let mut seen = std::collections::HashSet::new();
72        list.retain(|r| seen.insert(canonicalize(&r.path)));
73        list
74    }
75}
76
77/// The startup note naming the extensions TypeScript would load here (the enabled ones of
78/// [`LocalResources::extensions`]) and ultron-rs ignores; none when there are none.
79pub fn ignored_extensions_note(resources: &LocalResources) -> Option<String> {
80    let ignored: Vec<String> = resources
81        .extensions()
82        .into_iter()
83        .filter(|r| r.enabled)
84        .map(|r| r.path)
85        .collect();
86    match ignored.len() {
87        0 => None,
88        n => Some(format!(
89            "Pi extensions are not supported by ultron-rs; ignoring {n} extension{}: {}",
90            if n == 1 { "" } else { "s" },
91            ignored.join(", ")
92        )),
93    }
94}