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::{ScopeReadError, Site, read_sites};
51pub(crate) use site::{read_artifact_scope, read_eval_scope};
52pub use stored::{Field, StoredPayload, StoredProfile};
53pub use tools::{
54    AgentTool, Builtin, BuiltinTool, CommandTool, HumanBuiltinTool, HumanCommandTool, HumanTool,
55    HumanToolKind, HumanViewerTool, SessionCommand, ToolProtocol, ToolSession, ViewerKind,
56};
57pub use validation::MAX_TIMEOUT_MS;
58pub(crate) use validation::{artifact_name, identifier};
59pub use workspace::{Artifact, Binding, Eval, RepoConfig, ReviewRequirement};
60
61/// Format version for artifactize configuration documents.
62pub const CONFIG_FORMAT_VERSION: u32 = 1;
63pub const CONFIG_FILE: &str = "index.artf";
64/// Gitignore-style folder patterns, at the workspace root, that discovery skips.
65pub const IGNORE_FILE: &str = ".artfignore";
66/// Bound named variants and declared input lists to keep discovery and preparation finite.
67const MAX_DECLARED_ITEMS: usize = 64;
68
69#[derive(Debug, Error)]
70#[error("{}: {message}", crate::platform::path_text(.path))]
71pub struct ConfigError {
72    pub path: PathBuf,
73    /// `line L, column C: …`, then a `note:` line per related place (a macro's call site,
74    /// the other side of a duplicate).
75    pub message: String,
76}
77
78impl ConfigError {
79    pub(crate) fn new(path: impl Into<PathBuf>, message: impl ToString) -> Self {
80        Self {
81            path: path.into(),
82            message: message.to_string(),
83        }
84    }
85}
86
87#[cfg(test)]
88mod tests;
89
90/// One definition text's Artifact and evals, read as a project with only that file: for tests
91/// of declaration rules that need no files.
92#[doc(hidden)]
93#[derive(Debug)]
94pub struct ParsedDeclaration {
95    pub evals: Vec<EvalDeclaration>,
96    pub views: Views,
97    pub tags: Vec<String>,
98    pub basis: Option<bool>,
99    pub fingerprint: Option<Fingerprint>,
100}
101
102#[doc(hidden)]
103pub fn parse_declaration(text: &str) -> Result<ParsedDeclaration, String> {
104    parse_declaration_files(&[(PathBuf::from("test.artf"), text.to_owned())])
105}
106
107/// [`parse_declaration`] for one Artifact's `.artf` file with other files beside it, such as
108/// the `.artfprofiles` file of its Agent evals' profiles; paths are relative to `.artifactize/`.
109#[doc(hidden)]
110pub fn parse_declaration_files(files: &[(PathBuf, String)]) -> Result<ParsedDeclaration, String> {
111    // Nothing is read from this root: the files are in memory, and only built-in plugins.
112    let root = std::env::temp_dir().join("artifactize-unread-project");
113    let sources = files
114        .iter()
115        .map(|(file, text)| {
116            (
117                root.join(discovery::DEFINITION_DIR).join(file),
118                text.clone(),
119            )
120        })
121        .collect();
122    let catalog = plugins::builtin_catalog();
123    let program =
124        discovery::program(&root, &sources, &catalog).map_err(|error| error.to_string())?;
125    let config =
126        discovery::build_program(root, program, catalog).map_err(|error| error.to_string())?;
127    let artifact = config
128        .artifacts
129        .into_values()
130        .next()
131        .ok_or("The text declares no Artifact.")?;
132    Ok(ParsedDeclaration {
133        evals: config
134            .evals
135            .into_iter()
136            .map(|eval| eval.declaration)
137            .collect(),
138        views: artifact.views,
139        tags: artifact.tags,
140        basis: artifact.basis,
141        fingerprint: artifact.fingerprint,
142    })
143}