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