artifactize-core 0.27.1

The library behind artifactize: Artifacts, evals, reviews, the monitor and review models, and the shared operations every interface calls.
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//! Toolsets that come with artifactize or a plugin. A plugin is a folder with a `plugin.toml`
//! naming its namespace, its `.artf` files declaring that namespace's toolset and any macros,
//! and optionally a viewer page.

use std::{
    collections::BTreeMap,
    fs::File,
    io::Read,
    path::{Path, PathBuf},
    sync::Arc,
};

use serde::{Deserialize, Serialize};

use super::{ConfigError, LogicalPath, PluginName, discovery::DEFINITION_DIR};
use crate::{platform, scope};

pub(super) const DIRECTORY: &str = "plugins";
const MANIFEST: &str = "plugin.toml";
/// A viewer opens one web document. Bound what a launch copies across IPC.
const DOCUMENT_BYTES: u64 = 8 * 1024 * 1024;
/// A plugin's `.artf` files, bounded like any document a plugin carries.
const MAX_SOURCES: usize = 64;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum BuiltinViewer {
    Files,
    Markdown,
    Artifactize,
}

impl BuiltinViewer {
    // Kept as text rather than `.artf` files: any `.artf` file marks its folder as a workspace.
    const ALL: [(Self, &'static str, &'static str, &'static str); 3] = [
        (
            Self::Files,
            "files",
            "File Explorer",
            r#"// The built-in File Explorer: an Artifact's files for agents and people.
tool files = {
  agent list = builtin(op = "list")
  agent glob = builtin(op = "glob")
  agent grep = builtin(op = "grep")
  agent read = builtin(op = "read")
  human view = viewer(description = "Open @{artf self} in File Explorer.")
}
"#,
        ),
        (
            Self::Markdown,
            "markdown",
            "Markdown",
            r#"// The built-in Markdown viewer.
tool markdown = {
  agent read = builtin(op = "read")
  human view = viewer(description = "Open @{artf self} in Markdown.")
}
"#,
        ),
        (
            Self::Artifactize,
            "artifactize",
            "Artifactize Viewer",
            r#"// The built-in Artifactize Viewer of the definitions under .artifactize/.
tool artifactize = {
  agent declarations = builtin(op = "declarations", args = [])
  human view = viewer(description = "Open @{artf self} in Artifactize Viewer.")
}
"#,
        ),
    ];
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct Manifest {
    namespace: PluginName,
    title: String,
    viewer: Option<WebEntry>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct WebEntry {
    entry: LogicalPath,
}

/// A viewer selected by its plugin, never by a path supplied by a web page.
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum ViewerEntry {
    Builtin { viewer: BuiltinViewer },
    Web { html: Arc<str> },
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PluginInfo {
    #[serde(rename = "namespace")]
    pub name: PluginName,
    pub title: String,
    pub builtin: bool,
    pub viewer: bool,
}

#[derive(Debug)]
pub(crate) struct Plugin {
    pub info: PluginInfo,
    pub viewer: Option<ViewerEntry>,
    /// Its `.artf` files, by their path under `plugins/<namespace>/`.
    pub sources: Vec<(PathBuf, String)>,
}

impl Plugin {
    pub(super) fn source_files(&self) -> impl Iterator<Item = super::lang::SourceFile> + '_ {
        self.sources
            .iter()
            .map(|(path, text)| super::lang::SourceFile {
                path: PathBuf::from(DIRECTORY)
                    .join(self.info.name.as_str())
                    .join(path),
                text: text.clone(),
                origin: super::lang::Origin::Plugin(super::lang::PluginSource {
                    namespace: self.info.name.to_string(),
                    viewer: self.viewer.is_some(),
                }),
            })
    }
}

pub(crate) type Catalog = BTreeMap<PluginName, Arc<Plugin>>;

pub(crate) fn read_document(file: File) -> Result<String, String> {
    if !file
        .metadata()
        .map_err(|error| error.to_string())?
        .is_file()
    {
        return Err("Expected a regular text file.".into());
    }
    let mut buffer = String::new();
    file.take(DOCUMENT_BYTES + 1)
        .read_to_string(&mut buffer)
        .map_err(|error| error.to_string())?;
    if buffer.len() as u64 > DOCUMENT_BYTES {
        return Err("Viewer document exceeds 8 MiB.".into());
    }
    Ok(buffer)
}

fn builtins() -> impl Iterator<Item = Plugin> {
    BuiltinViewer::ALL
        .into_iter()
        .map(|(viewer, name, title, source)| Plugin {
            info: PluginInfo {
                name: name.parse().expect("a built-in plugin name"),
                title: title.into(),
                builtin: true,
                viewer: true,
            },
            viewer: Some(ViewerEntry::Builtin { viewer }),
            sources: vec![(PathBuf::from(format!("{name}.artf")), source.into())],
        })
}

fn definitions(root: &Path) -> Result<PathBuf, ConfigError> {
    let root = platform::canonicalize(root).map_err(|error| ConfigError::new(root, error))?;
    let definitions = root.join(DEFINITION_DIR);
    if !platform::paths::reusable_directory(&root, DEFINITION_DIR)
        .map_err(|error| ConfigError::new(&definitions, error))?
    {
        return Err(ConfigError::new(
            &definitions,
            "Not an Artifactize project.",
        ));
    }
    Ok(definitions)
}

/// Read one plugin folder: its manifest, its viewer page and its `.artf` files.
pub(super) fn read(directory: &Path) -> Result<Plugin, ConfigError> {
    let manifest_path = directory.join(MANIFEST);
    let source = scope::open_scoped(directory, MANIFEST)
        .map_err(|error| ConfigError::new(&manifest_path, error))?;
    let source = read_document(source).map_err(|error| ConfigError::new(&manifest_path, error))?;
    let table: toml::Table =
        toml::from_str(&source).map_err(|error| ConfigError::new(&manifest_path, error))?;
    if table.contains_key("type") || table.contains_key("views") {
        return Err(ConfigError::new(
            &manifest_path,
            "This plugin.toml is from before 0.27: it names its `namespace` now and declares its tools in an .artf file. Run artifactize config migrate in a project that has it installed, or update the plugin.",
        ));
    }
    let manifest: Manifest =
        toml::from_str(&source).map_err(|error| ConfigError::new(&manifest_path, error))?;
    super::validation::text(&manifest.title, "Plugin title")
        .map_err(|error| ConfigError::new(&manifest_path, error))?;
    let viewer = manifest
        .viewer
        .map(|web| {
            if Path::new(web.entry.as_str())
                .extension()
                .is_none_or(|extension| extension != "html")
            {
                return Err(
                    "viewer.entry must name an .html file in the plugin directory.".to_owned(),
                );
            }
            let file = scope::open_scoped(directory, web.entry.as_str())
                .map_err(|error| format!("viewer.entry {}: {error}", web.entry))?;
            Ok(ViewerEntry::Web {
                html: read_document(file)?.into(),
            })
        })
        .transpose()
        .map_err(|error| ConfigError::new(&manifest_path, error))?;
    let opened =
        platform::open_directory(directory).map_err(|error| ConfigError::new(directory, error))?;
    let mut sources = Vec::new();
    for entry in platform::read_dir(&opened).map_err(|error| ConfigError::new(directory, error))? {
        let entry = entry.map_err(|error| ConfigError::new(directory, error))?;
        let name = entry.file_name();
        if !name.as_encoded_bytes().ends_with(b".artf") {
            continue;
        }
        let path = directory.join(&name);
        if entry
            .file_type()
            .map_err(|error| ConfigError::new(&path, error))?
            != platform::FileKind::File
        {
            return Err(ConfigError::new(
                &path,
                "A plugin's definition must be a regular file.",
            ));
        }
        let text = super::discovery::read_regular_text(&path)
            .map_err(|error| ConfigError::new(&path, error))?;
        sources.push((PathBuf::from(name), text));
    }
    if sources.len() > MAX_SOURCES {
        return Err(ConfigError::new(
            directory,
            format!("A plugin holds at most {MAX_SOURCES} .artf files."),
        ));
    }
    sources.sort();
    if sources.is_empty() {
        return Err(ConfigError::new(
            directory,
            format!(
                "Plugin {} declares no tools: add an .artf file with `tool {} = {{ … }}`.",
                manifest.namespace, manifest.namespace
            ),
        ));
    }
    Ok(Plugin {
        info: PluginInfo {
            name: manifest.namespace,
            title: manifest.title,
            builtin: false,
            viewer: viewer.is_some(),
        },
        viewer,
        sources,
    })
}

/// The built-in plugins alone.
pub(crate) fn builtin_catalog() -> Catalog {
    builtins()
        .map(|plugin| (plugin.info.name.clone(), Arc::new(plugin)))
        .collect()
}

/// The built-in plugins and those installed under `.artifactize/plugins/`.
pub(crate) fn catalog(root: &Path) -> Result<Catalog, ConfigError> {
    let mut plugins: Catalog = builtins()
        .map(|plugin| (plugin.info.name.clone(), Arc::new(plugin)))
        .collect();
    let parent = root.join(DEFINITION_DIR);
    let directory = parent.join(DIRECTORY);
    if !platform::paths::reusable_directory(root, DEFINITION_DIR)
        .map_err(|error| ConfigError::new(&parent, error))?
    {
        return Ok(plugins);
    }
    if !platform::paths::reusable_directory(&parent, DIRECTORY)
        .map_err(|error| ConfigError::new(&directory, error))?
    {
        return Ok(plugins);
    }
    let opened = platform::open_directory(&directory)
        .map_err(|error| ConfigError::new(&directory, error))?;
    for entry in platform::read_dir(&opened).map_err(|error| ConfigError::new(&directory, error))? {
        let entry = entry.map_err(|error| ConfigError::new(&directory, error))?;
        let name = entry.file_name();
        if name.as_encoded_bytes().starts_with(b".") {
            continue; // Installer staging, not a discoverable plugin name.
        }
        let path = directory.join(&name);
        let kind = entry
            .file_type()
            .map_err(|error| ConfigError::new(&path, error))?;
        if kind == platform::FileKind::File {
            continue;
        }
        if kind != platform::FileKind::Directory {
            return Err(ConfigError::new(
                &path,
                "A plugin must be a real directory, not a link or special file.",
            ));
        }
        let plugin = read(&path)?;
        if name != plugin.info.name.as_str() {
            return Err(ConfigError::new(
                &path,
                "The directory name must match the plugin's namespace.",
            ));
        }
        if plugins.contains_key(&plugin.info.name) {
            return Err(ConfigError::new(
                &path,
                "Built-in plugin namespaces are reserved.",
            ));
        }
        plugins.insert(plugin.info.name.clone(), Arc::new(plugin));
    }
    Ok(plugins)
}

pub fn list(root: &Path) -> Result<Vec<PluginInfo>, ConfigError> {
    let directory = definitions(root)?;
    catalog(directory.parent().expect("workspace root"))
        .map(|plugins| plugins.values().map(|plugin| plugin.info.clone()).collect())
}

/// A plugin's files must read on their own: its toolset and macros, with the built-ins.
fn validate(plugin: &Plugin) -> Result<(), ConfigError> {
    let files = builtins()
        .chain(std::iter::once(Plugin {
            info: plugin.info.clone(),
            viewer: plugin.viewer.clone(),
            sources: plugin.sources.clone(),
        }))
        .flat_map(|plugin| plugin.source_files().collect::<Vec<_>>())
        .collect();
    // A plugin declares toolsets and macros, never profiles: no environment to read.
    let program =
        super::lang::read(files, &|_| None).map_err(|(sources, error)| sources.error(error))?;
    if !program.symbols.iter().any(|symbol| {
        symbol.kind == "tool" && !symbol.is_macro && symbol.name == plugin.info.name.as_str()
    }) {
        return Err(ConfigError::new(
            PathBuf::from(plugin.info.name.as_str()),
            format!(
                "Plugin {0} declares no `tool {0} = {{ … }}`.",
                plugin.info.name
            ),
        ));
    }
    Ok(())
}

/// Install a complete folder, so readers can never observe a half-written plugin.
pub fn add(root: &Path, source: &Path) -> Result<PluginInfo, ConfigError> {
    let definitions = definitions(root)?;
    let directory = definitions.join(DIRECTORY);
    let source = platform::canonicalize(source).map_err(|error| ConfigError::new(source, error))?;
    let plugin = read(&source)?;
    validate(&plugin)?;
    let installed = directory.join(plugin.info.name.as_str());
    if builtins().any(|builtin| builtin.info.name == plugin.info.name) {
        return Err(ConfigError::new(
            &installed,
            "Built-in plugin namespaces are reserved.",
        ));
    }
    if platform::is_within(&source, &directory) || platform::is_within(&directory, &source) {
        return Err(ConfigError::new(
            &source,
            "The source must be outside the installed plugins directory and cannot contain it.",
        ));
    }
    ensure_directory(&definitions, DIRECTORY)
        .map_err(|error| ConfigError::new(&directory, error))?;
    if platform::paths::reusable_directory(&directory, plugin.info.name.as_str())
        .map_err(|error| ConfigError::new(&installed, error))?
    {
        return Err(ConfigError::new(
            &installed,
            "Plugin already installed; remove it before adding a replacement.",
        ));
    }
    let staged = tempfile::Builder::new()
        .prefix(".install-")
        .tempdir_in(&directory)
        .map_err(|error| ConfigError::new(&directory, error))?;
    let payload = staged.path().join("plugin");
    let source =
        platform::open_directory(&source).map_err(|error| ConfigError::new(&source, error))?;
    platform::paths::copy_portable_tree(&source, &payload)
        .map_err(|error| ConfigError::new(&installed, error))?;
    let validated = read(&payload)?;
    if validated.info.name != plugin.info.name {
        return Err(ConfigError::new(
            &installed,
            "Plugin namespace changed during installation.",
        ));
    }
    platform::paths::rename_open_file(&payload, &installed)
        .map_err(|error| ConfigError::new(&installed, error))?;
    Ok(validated.info)
}

fn ensure_directory(parent: &Path, name: &str) -> std::io::Result<()> {
    if !platform::paths::reusable_directory(parent, name)? {
        match std::fs::create_dir(parent.join(name)) {
            Err(error)
                if error.kind() == std::io::ErrorKind::AlreadyExists
                    && platform::paths::reusable_directory(parent, name)? =>
            {
                Ok(())
            }
            result => result,
        }
    } else {
        Ok(())
    }
}

/// Remove an installed package, even if its manifest is no longer valid.
pub fn remove(root: &Path, name: &PluginName) -> Result<(), ConfigError> {
    let definitions = definitions(root)?;
    let directory = definitions.join(DIRECTORY);
    let path = directory.join(name.as_str());
    if builtins().any(|builtin| builtin.info.name == *name) {
        return Err(ConfigError::new(
            &path,
            "A built-in plugin cannot be removed.",
        ));
    }
    if !platform::paths::reusable_directory(&definitions, DIRECTORY)
        .and_then(|exists| {
            if exists {
                platform::paths::reusable_directory(&directory, name.as_str())
            } else {
                Ok(false)
            }
        })
        .map_err(|error| ConfigError::new(&path, error))?
    {
        return Err(ConfigError::new(&path, "Plugin is not installed."));
    }
    let staged = platform::private_tempdir_in(".remove-", &directory)
        .map_err(|error| ConfigError::new(&path, error))?;
    platform::paths::rename_open_file(&path, &staged.path().join("plugin"))
        .map_err(|error| ConfigError::new(&path, error))?;
    staged
        .close()
        .map_err(|error| ConfigError::new(&path, error))
}

/// The plugins a migration reads with: the built-ins, the current ones as installed, and each
/// one installed before 0.27 converted in memory. Returns the files a converted plugin is
/// written as: its new manifest and its toolset.
pub(super) fn migrate(root: &Path) -> Result<(Catalog, Vec<(PathBuf, String)>), ConfigError> {
    let mut plugins: Catalog = builtins()
        .map(|plugin| (plugin.info.name.clone(), Arc::new(plugin)))
        .collect();
    let mut writes = Vec::new();
    let directory = root.join(DEFINITION_DIR).join(DIRECTORY);
    if !platform::paths::reusable_directory(root, DEFINITION_DIR)
        .map_err(|error| ConfigError::new(root.join(DEFINITION_DIR), error))?
        || !platform::paths::reusable_directory(&root.join(DEFINITION_DIR), DIRECTORY)
            .map_err(|error| ConfigError::new(&directory, error))?
    {
        return Ok((plugins, writes));
    }
    let opened = platform::open_directory(&directory)
        .map_err(|error| ConfigError::new(&directory, error))?;
    for entry in platform::read_dir(&opened).map_err(|error| ConfigError::new(&directory, error))? {
        let entry = entry.map_err(|error| ConfigError::new(&directory, error))?;
        let name = entry.file_name();
        let path = directory.join(&name);
        if name.as_encoded_bytes().starts_with(b".")
            || entry
                .file_type()
                .map_err(|error| ConfigError::new(&path, error))?
                != platform::FileKind::Directory
        {
            continue;
        }
        let manifest_path = path.join(MANIFEST);
        let source = super::discovery::read_regular_text(&manifest_path)
            .map_err(|error| ConfigError::new(&manifest_path, error))?;
        let table: toml::Table =
            toml::from_str(&source).map_err(|error| ConfigError::new(&manifest_path, error))?;
        if !table.contains_key("type") && !table.contains_key("views") {
            let plugin = read(&path)?;
            plugins.insert(plugin.info.name.clone(), Arc::new(plugin));
            continue;
        }
        let invalid = |message: &str| ConfigError::new(&manifest_path, message);
        let namespace: PluginName = table
            .get("type")
            .and_then(toml::Value::as_str)
            .ok_or_else(|| invalid("An old plugin.toml names its `type`."))?
            .parse()
            .map_err(|error: String| invalid(&error))?;
        let title = table
            .get("title")
            .and_then(toml::Value::as_str)
            .ok_or_else(|| invalid("A plugin.toml has a `title`."))?
            .to_owned();
        let entry = table
            .get("viewer")
            .and_then(|viewer| viewer.get("entry"))
            .and_then(toml::Value::as_str)
            .map(str::to_owned);
        let views = serde_json::to_value(
            table
                .get("views")
                .cloned()
                .unwrap_or_else(|| toml::Table::new().into()),
        )
        .expect("TOML is JSON");
        let tools = super::convert::toolset_text(namespace.as_str(), &views, entry.is_some())
            .map_err(|error| invalid(&error))?;
        let mut manifest = toml::Table::new();
        manifest.insert("namespace".into(), namespace.to_string().into());
        manifest.insert("title".into(), title.clone().into());
        if let Some(entry) = &entry {
            let mut viewer = toml::Table::new();
            viewer.insert("entry".into(), entry.clone().into());
            manifest.insert("viewer".into(), viewer.into());
        }
        let viewer = entry
            .map(|entry| {
                let file = scope::open_scoped(&path, &entry)
                    .map_err(|error| invalid(&format!("viewer.entry {entry}: {error}")))?;
                Ok::<_, ConfigError>(ViewerEntry::Web {
                    html: read_document(file).map_err(|error| invalid(&error))?.into(),
                })
            })
            .transpose()?;
        let file = PathBuf::from(format!("{namespace}.artf"));
        let plugin = Plugin {
            info: PluginInfo {
                name: namespace.clone(),
                title,
                builtin: false,
                viewer: viewer.is_some(),
            },
            viewer,
            sources: vec![(file.clone(), tools.clone())],
        };
        validate(&plugin)?;
        writes.push((
            manifest_path.clone(),
            toml::to_string(&manifest).expect("a manifest is TOML"),
        ));
        writes.push((path.join(file), tools));
        plugins.insert(namespace, Arc::new(plugin));
    }
    Ok((plugins, writes))
}