1use std::collections::BTreeMap;
8
9use serde::{Deserialize, Serialize};
10
11use crate::claude::manifest::{MarketplaceEntry, PluginSource};
12use crate::diagnostic::SchemaDiagnostic;
13use crate::dialect::{Dialect, DialectChoice};
14use crate::discover::{enclosing_generated_dir, payload_paths};
15use crate::dispatch::{AsPlugin, Dispatched, Layout, dispatch};
16pub use crate::dispatch::{Kind, Mirror};
17use crate::item::DefinitionKind;
18use crate::prepare::{Classification, PrepareContext};
19use crate::tree::{SubTree, TreeSource};
20
21#[derive(Debug, Clone, Default, Deserialize)]
23#[serde(deny_unknown_fields)]
24pub struct Hint {
25 #[serde(default)]
27 pub dialect: Option<Dialect>,
28 #[serde(default)]
30 pub subpath: Option<String>,
31 #[serde(default)]
33 pub plugin: Option<PluginHint>,
34}
35
36#[derive(Debug, Clone, Deserialize)]
37#[serde(deny_unknown_fields)]
38pub struct PluginHint {
39 pub entry: MarketplaceEntry,
40 #[serde(default)]
42 pub at_marketplace_root: bool,
43}
44
45#[derive(Debug, Clone, Serialize)]
46pub struct Inspection {
47 pub kind: Kind,
48 #[serde(skip_serializing_if = "Option::is_none")]
50 pub dialect: Option<DialectChoice>,
51 pub also_found: Vec<Mirror>,
53 #[serde(skip_serializing_if = "Option::is_none")]
54 pub name: Option<String>,
55 #[serde(skip_serializing_if = "Option::is_none")]
56 pub version: Option<String>,
57 #[serde(skip_serializing_if = "Option::is_none")]
58 pub description: Option<String>,
59 pub plugins: Vec<InspectedPlugin>,
61 pub items: Vec<InspectedItem>,
63 pub diagnostics: BTreeMap<String, Vec<SchemaDiagnostic>>,
64}
65
66#[derive(Debug, Clone, Serialize)]
67pub struct InspectedPlugin {
68 pub entry: MarketplaceEntry,
69 pub at_marketplace_root: bool,
70 #[serde(skip_serializing_if = "Option::is_none")]
73 pub items: Option<Vec<InspectedItem>>,
74 #[serde(skip_serializing_if = "Option::is_none")]
76 pub refused: Option<String>,
77 pub diagnostics: BTreeMap<String, Vec<SchemaDiagnostic>>,
78}
79
80#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
81#[serde(rename_all = "lowercase")]
82pub enum InspectedKind {
83 Skill,
84 Agent,
85 Command,
87}
88
89impl From<DefinitionKind> for InspectedKind {
90 fn from(kind: DefinitionKind) -> Self {
91 match kind {
92 DefinitionKind::Skill => Self::Skill,
93 DefinitionKind::Agent => Self::Agent,
94 }
95 }
96}
97
98#[derive(Debug, Clone, Serialize)]
99pub struct InspectedItem {
100 pub kind: InspectedKind,
101 pub slug: String,
102 pub source_path: String,
104 #[serde(skip_serializing_if = "Option::is_none")]
106 pub skill_root: Option<String>,
107 #[serde(skip_serializing_if = "Option::is_none")]
109 pub output_path: Option<String>,
110 #[serde(skip_serializing_if = "Vec::is_empty")]
112 pub payload: Vec<String>,
113}
114
115pub fn inspect(source: &impl TreeSource, hint: &Hint) -> Inspection {
116 match hint.subpath.as_deref().filter(|path| !path.is_empty()) {
117 Some(subpath) => inspect_root(&SubTree::new(source, subpath), hint),
118 None => inspect_root(source, hint),
119 }
120}
121
122fn inspect_root(source: &impl TreeSource, hint: &Hint) -> Inspection {
123 let dispatched = dispatch(
124 source,
125 PrepareContext::Package,
126 hint.dialect,
127 hint.plugin.as_ref().map(as_plugin),
128 None,
129 );
130 describe(source, hint.dialect, dispatched)
131}
132
133fn as_plugin(hint: &PluginHint) -> AsPlugin<'_> {
134 AsPlugin {
135 entry: &hint.entry,
136 at_marketplace_root: hint.at_marketplace_root,
137 }
138}
139
140fn describe(
141 source: &impl TreeSource,
142 declared: Option<Dialect>,
143 dispatched: Dispatched,
144) -> Inspection {
145 let mut diagnostics = dispatched.discovery.diagnostics.clone();
146 if dispatched.kind() == Kind::Invalid
148 && let Some(bytes) = &dispatched.manifest
149 {
150 let found = crate::prepare::prepare_manifest(bytes)
151 .reading
152 .package
153 .diagnostics;
154 diagnostics
155 .entry("mars.toml".to_string())
156 .or_default()
157 .extend(found);
158 }
159 let items = list_items(source, &dispatched, &mut diagnostics);
160 let (name, version, description) = match &dispatched.layout {
161 Layout::Claude(spec) => (
162 spec.name.clone(),
163 spec.version.clone(),
164 spec.description.clone(),
165 ),
166 _ => (None, None, None),
167 };
168 let plugins = match &dispatched.layout {
169 Layout::Marketplace(marketplace) => marketplace
170 .plugins
171 .iter()
172 .map(|entry| inspect_plugin(source, declared, entry))
173 .collect(),
174 _ => Vec::new(),
175 };
176 Inspection {
177 kind: dispatched.kind(),
178 dialect: dispatched.dialect().cloned(),
179 also_found: dispatched.also_found,
180 name,
181 version,
182 description,
183 plugins,
184 items,
185 diagnostics,
186 }
187}
188
189fn inspect_plugin(
192 source: &impl TreeSource,
193 declared: Option<Dialect>,
194 entry: &MarketplaceEntry,
195) -> InspectedPlugin {
196 let mut plugin = InspectedPlugin {
197 entry: entry.clone(),
198 at_marketplace_root: false,
199 items: None,
200 refused: None,
201 diagnostics: BTreeMap::new(),
202 };
203 match &entry.source {
204 PluginSource::Unsupported { reason } => {
205 plugin.refused = Some(format!("can't import from this source: {reason}"));
206 }
207 PluginSource::Remote { .. } => {}
208 PluginSource::Local { path } => {
209 plugin.at_marketplace_root = path.is_empty();
210 let root = SubTree::new(source, path.clone());
211 if let Some(generated) = enclosing_generated_dir(source, path) {
212 plugin.refused = Some(format!(
213 "`{path}` is inside `{}`, plugin output Mars built, which is never input",
214 if generated.is_empty() {
215 "."
216 } else {
217 &generated
218 }
219 ));
220 return plugin;
221 }
222 if !path.is_empty() && !source.is_dir(path) {
223 plugin.refused = Some(format!("`{path}` does not exist in the marketplace"));
224 return plugin;
225 }
226 let dispatched = dispatch(
227 &root,
228 PrepareContext::Package,
229 declared,
230 Some(AsPlugin {
231 entry,
232 at_marketplace_root: plugin.at_marketplace_root,
233 }),
234 Some(&entry.name),
235 );
236 if dispatched.kind() == Kind::Invalid {
237 plugin.refused = dispatched
238 .discovery
239 .diagnostics
240 .values()
241 .flatten()
242 .find(|diagnostic| diagnostic.is_error())
243 .map(|diagnostic| diagnostic.message.clone())
244 .or_else(|| Some("the plugin's manifest cannot be read".to_owned()));
245 }
246 plugin.items = Some(list_items(&root, &dispatched, &mut plugin.diagnostics));
247 for (path, found) in dispatched.discovery.diagnostics {
248 plugin.diagnostics.entry(path).or_default().extend(found);
249 }
250 }
251 }
252 plugin
253}
254
255pub(crate) fn list_items(
256 source: &impl TreeSource,
257 dispatched: &Dispatched,
258 diagnostics: &mut BTreeMap<String, Vec<SchemaDiagnostic>>,
259) -> Vec<InspectedItem> {
260 let discovery = &dispatched.discovery;
261 let mut payload = BTreeMap::<String, Vec<String>>::new();
262 let foreign = matches!(dispatched.classification, Classification::Foreign(_));
263 let by_owner = payload_paths(
264 source,
265 discovery,
266 foreign,
267 discovery.definitions.iter(),
268 diagnostics,
269 );
270 for (path, _) in by_owner {
271 if let Some(owner) = discovery
273 .definitions
274 .iter()
275 .filter_map(|definition| definition.skill_root.as_deref())
276 .filter(|root| root.is_empty() || path.starts_with(&format!("{root}/")))
277 .max_by_key(|root| root.len())
278 {
279 payload.entry(owner.to_owned()).or_default().push(path);
280 }
281 }
282 let mut items = discovery
283 .definitions
284 .iter()
285 .map(|definition| InspectedItem {
286 kind: definition.kind.into(),
287 slug: definition.slug.clone(),
288 source_path: definition.source_path.clone(),
289 skill_root: definition.skill_root.clone(),
290 output_path: Some(definition.output_path.clone()),
291 payload: definition
292 .skill_root
293 .as_ref()
294 .and_then(|root| payload.remove(root))
295 .unwrap_or_default(),
296 })
297 .collect::<Vec<_>>();
298 items.extend(discovery.dropped.iter().map(|command| InspectedItem {
299 kind: InspectedKind::Command,
300 slug: command.slug.clone(),
301 source_path: command.source_path.clone(),
302 skill_root: None,
303 output_path: None,
304 payload: Vec::new(),
305 }));
306 items
307}