Skip to main content

artifactize_core/store/definitions/
authored.rs

1//! Snapshot authored declarations without round-tripping their typed fields through JSON.
2use super::*;
3
4impl From<&config::Profile> for Profile {
5    fn from(source: &config::Profile) -> Self {
6        Self {
7            known: source.into(),
8            extra: Map::new(),
9        }
10    }
11}
12
13impl From<&config::EvalDeclaration> for Declaration {
14    fn from(source: &config::EvalDeclaration) -> Self {
15        Self {
16            mutex: nonempty(&source.mutex),
17            id: Field::Value(source.id.clone()),
18            title: Field::Value(source.title.clone()),
19            profile: Field::Value(source.profile().into()),
20            profile_name: optional(source.profile_name().map(str::to_owned)),
21            profile_variants: Field::Missing,
22            payload: Field::Value(source.payload().into()),
23            inputs: nonempty(source.inputs()),
24            red_at: optional(source.red_at()),
25            pass_schema: Field::Missing,
26            fail_schema: Field::Missing,
27            extra: Map::new(),
28        }
29    }
30}
31
32fn nonempty<T: Clone>(values: &[T]) -> Field<Vec<T>> {
33    if values.is_empty() {
34        Field::Missing
35    } else {
36        Field::Value(values.to_vec())
37    }
38}
39
40fn optional<T>(value: Option<T>) -> Field<T> {
41    value.map_or(Field::Missing, Field::Value)
42}
43
44impl From<&config::Artifact> for Artifact {
45    fn from(source: &config::Artifact) -> Self {
46        let mut extra = Map::new();
47        if let Some(covering) = &source.covering_part {
48            extra.insert("coveringPart".into(), Value::String(covering.to_string()));
49        }
50        if !source.parts.is_empty() {
51            extra.insert("parts".into(), serde_json::json!(source.parts));
52        }
53        Self {
54            definition: Field::Value(source.definition.clone()),
55            kind: Field::Value(source.kind()),
56            path: Field::Value(source.path.clone()),
57            select: nonempty(source.select()),
58            nested: Field::Value(source.nested.clone()),
59            name: Field::Value(source.name.clone()),
60            tags: Field::Value(source.tags.clone()),
61            views: Field::Value((&source.views).into()),
62            basis: source.basis.into(),
63            fingerprint: source.fingerprint.as_ref().map(Fingerprint::from).into(),
64            extra,
65        }
66    }
67}
68
69impl From<&config::Views> for Views {
70    fn from(source: &config::Views) -> Self {
71        Self {
72            agent_tools: Field::Value(
73                source
74                    .agent_tools
75                    .iter()
76                    .map(|(name, tool)| (name.clone(), tool.into()))
77                    .collect(),
78            ),
79            human_tools: Field::Value(
80                source
81                    .human_tools
82                    .iter()
83                    .map(|(name, tool)| (name.clone(), tool.into()))
84                    .collect(),
85            ),
86            extra: Map::from_iter([(
87                "agentSessions".into(),
88                serde_json::json!(source.agent_sessions),
89            )]),
90        }
91    }
92}
93
94impl From<&config::AgentTool> for AgentTool {
95    fn from(source: &config::AgentTool) -> Self {
96        match source {
97            config::AgentTool::Builtin(tool) => Self::Builtin(BuiltinTool {
98                builtin: tool.builtin,
99                args: optional(tool.args.clone()),
100                description: optional(tool.description.clone()),
101                extra: Map::new(),
102            }),
103            config::AgentTool::Command(tool) => Self::Command(CommandTool {
104                description: tool.description.clone(),
105                input_schema: InputSchema(tool.input_schema.clone()),
106                protocol: tool.protocol,
107                command: crate::platform::program_text(&tool.command),
108                args: tool.args.iter().map(ToString::to_string).collect(),
109                timeout_ms: optional(tool.timeout_ms),
110                execution_paths: nonempty(&tool.execution_paths),
111                extra: tool
112                    .session
113                    .iter()
114                    .map(|session| ("session".into(), Value::String(session.to_string())))
115                    .collect(),
116            }),
117        }
118    }
119}
120
121impl From<&config::HumanTool> for HumanTool {
122    fn from(source: &config::HumanTool) -> Self {
123        match source {
124            config::HumanTool::Viewer(tool) => Self::Viewer(super::HumanViewerTool {
125                kind: tool.kind,
126                description: tool.description.clone(),
127                plugin: Some(tool.plugin.clone()),
128            }),
129            config::HumanTool::Builtin(tool) => Self::Builtin(HumanBuiltinTool {
130                builtin: tool.builtin,
131                description: tool.description.clone(),
132                kind: tool.kind(),
133                args: tool.args.iter().map(ToString::to_string).collect(),
134                extra: Map::new(),
135            }),
136            config::HumanTool::Command(tool) => Self::Command(HumanCommandTool {
137                description: tool.description.clone(),
138                kind: tool.kind,
139                command: crate::platform::program_text(&tool.command),
140                args: tool.args.iter().map(ToString::to_string).collect(),
141                extra: tool
142                    .timeout_ms
143                    .iter()
144                    .map(|duration| ("timeoutMs".into(), serde_json::json!(duration.as_millis())))
145                    .collect(),
146            }),
147        }
148    }
149}
150
151impl From<&config::Fingerprint> for Fingerprint {
152    fn from(source: &config::Fingerprint) -> Self {
153        match source {
154            config::Fingerprint::Graph => Self {
155                extra: Map::from_iter([("graph".into(), Value::Bool(true))]),
156                ..Default::default()
157            },
158            config::Fingerprint::Script {
159                command,
160                args,
161                files,
162                timeout_ms,
163            } => Self {
164                script: Field::Value(Script {
165                    command: crate::platform::program_text(command),
166                    args: args.iter().map(ToString::to_string).collect(),
167                    files: Field::Value(files.clone()),
168                    timeout_ms: (*timeout_ms).into(),
169                    extra: Map::new(),
170                }),
171                ..Default::default()
172            },
173            config::Fingerprint::Artifactsum { files, ignore } => Self {
174                files: Field::Value(files.clone()),
175                ignore: Field::Value(ignore.clone().into()),
176                ..Default::default()
177            },
178        }
179    }
180}