Skip to main content

artifactize_core/
config.rs

1//! Inert discovery and strict declarations.
2
3use std::{
4    collections::{BTreeMap, BTreeSet},
5    path::{Path, PathBuf},
6};
7
8use thiserror::Error;
9
10mod authored_target;
11mod backend;
12#[doc(hidden)]
13pub mod convert;
14mod declaration;
15mod discovery;
16mod eval;
17pub mod expand;
18mod identities;
19pub(crate) mod lang;
20pub mod migrate;
21mod mutex;
22mod plain;
23pub mod plugins;
24mod profile;
25pub mod rename;
26mod schemas;
27mod site;
28mod stored;
29mod tools;
30pub(crate) mod validation;
31mod workspace;
32
33pub use crate::types::{ArtifactName, EvalId};
34pub use artifactize_tools::scope::{ArtifactKind, Target, Uri};
35pub use backend::{Backend, CustomBackendName, DirectBackend, HttpProvider, Provider};
36pub use declaration::{Fingerprint, Views};
37use discovery::logical_join;
38pub use discovery::read_workspace_config;
39pub(crate) use discovery::{read_workspace_declarations, read_workspace_declarations_with};
40pub use eval::{
41    Audience, EvalDeclaration, EvalPayload, InstructionSlot, LocalEvalId, Priority, Slot,
42};
43pub use identities::{
44    ChildPrefix, EndpointId, ExecutionPinName, HubEpoch, LogicalPath, ModelId, PluginName,
45    ProfileName, ReferenceName, ToolFqn, ToolName,
46};
47pub use lang::Symbol;
48pub use mutex::MutexTag;
49pub use profile::{Profile, ProfileKind, Reasoning};
50pub use site::{DeclarationScope, ScopeReadError, Site, SiteIndex, read_sites};
51pub(crate) use site::{Evals, read_artifact_scope, read_eval_scope};
52pub use stored::{Field, StoredPayload, StoredProfile};
53pub use tools::{
54    AgentTool, Builtin, BuiltinTool, CommandKind, CommandTool, HumanBuiltinTool, HumanCommandTool,
55    HumanTool, HumanToolKind, HumanViewerTool, SessionCommand, ToolProtocol, ToolSession,
56    ViewerKind,
57};
58pub use validation::MAX_TIMEOUT_MS;
59pub(crate) use validation::{artifact_name, identifier};
60pub use workspace::{Artifact, Binding, Eval, RepoConfig, ReviewRequirement};
61
62/// Format version for artifactize configuration documents.
63pub const CONFIG_FORMAT_VERSION: u32 = 1;
64pub const CONFIG_FILE: &str = "index.artf";
65/// Gitignore-style folder patterns, at the workspace root, that discovery skips.
66pub const IGNORE_FILE: &str = ".artfignore";
67/// Bound named variants and declared input lists to keep discovery and preparation finite.
68const MAX_DECLARED_ITEMS: usize = 64;
69
70#[derive(Debug, Error)]
71#[error("{}: {message}", crate::platform::path_text(.path))]
72pub struct ConfigError {
73    pub path: PathBuf,
74    /// `line L, column C: …`, then a `note:` line per related place (a macro's call site,
75    /// the other side of a duplicate).
76    pub message: String,
77}
78
79impl ConfigError {
80    pub(crate) fn new(path: impl Into<PathBuf>, message: impl ToString) -> Self {
81        Self {
82            path: path.into(),
83            message: message.to_string(),
84        }
85    }
86}
87
88#[cfg(test)]
89mod tests;
90
91/// One definition text's Artifact and evals, read as a project with only that file: for tests
92/// of declaration rules that need no files.
93#[doc(hidden)]
94#[derive(Debug)]
95pub struct ParsedDeclaration {
96    pub evals: Vec<EvalDeclaration>,
97    pub views: Views,
98    pub tags: Vec<String>,
99    pub basis: Option<bool>,
100    pub fingerprint: Option<Fingerprint>,
101}
102
103#[doc(hidden)]
104pub fn parse_declaration(text: &str) -> Result<ParsedDeclaration, String> {
105    parse_declaration_files(&[(PathBuf::from("test.artf"), text.to_owned())])
106}
107
108/// [`parse_declaration`] for one Artifact's `.artf` file with other files beside it, such as
109/// the `.artfprofiles` file of its Agent evals' profiles; paths are relative to `.artifactize/`.
110#[doc(hidden)]
111pub fn parse_declaration_files(files: &[(PathBuf, String)]) -> Result<ParsedDeclaration, String> {
112    // Nothing is read from this root: the files are in memory, and only built-in plugins.
113    let root = std::env::temp_dir().join("artifactize-unread-project");
114    let sources = files
115        .iter()
116        .map(|(file, text)| {
117            (
118                root.join(discovery::DEFINITION_DIR).join(file),
119                text.clone(),
120            )
121        })
122        .collect();
123    let catalog = plugins::builtin_catalog();
124    let program =
125        discovery::program(&root, &sources, &catalog).map_err(|error| error.to_string())?;
126    let config =
127        discovery::build_program(root, program, catalog).map_err(|error| error.to_string())?;
128    let artifact = config
129        .artifacts
130        .into_values()
131        .next()
132        .ok_or("The text declares no Artifact.")?;
133    Ok(ParsedDeclaration {
134        evals: config
135            .evals
136            .into_iter()
137            .map(|eval| eval.declaration)
138            .collect(),
139        views: artifact.views,
140        tags: artifact.tags,
141        basis: artifact.basis,
142        fingerprint: artifact.fingerprint,
143    })
144}