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::Builtin(tool) => Self::Builtin(HumanBuiltinTool {
133                builtin: tool.builtin,
134                description: tool.description.clone(),
135                kind: tool.kind(),
136                args: tool.args.iter().map(ToString::to_string).collect(),
137                extra: Map::new(),
138            }),
139            config::HumanTool::Command(tool) => Self::Command(HumanCommandTool {
140                description: tool.description.clone(),
141                kind: tool.kind,
142                command: crate::platform::program_text(&tool.command),
143                args: tool.args.iter().map(ToString::to_string).collect(),
144                extra: tool
145                    .timeout_ms
146                    .iter()
147                    .map(|duration| ("timeoutMs".into(), serde_json::json!(duration.as_millis())))
148                    .collect(),
149            }),
150        }
151    }
152}
153
154impl From<&config::Fingerprint> for Fingerprint {
155    fn from(source: &config::Fingerprint) -> Self {
156        match source {
157            config::Fingerprint::Graph => Self {
158                extra: Map::from_iter([("graph".into(), Value::Bool(true))]),
159                ..Default::default()
160            },
161            config::Fingerprint::Script {
162                command,
163                args,
164                files,
165                timeout_ms,
166            } => Self {
167                script: Field::Value(Script {
168                    command: crate::platform::program_text(command),
169                    args: args.iter().map(ToString::to_string).collect(),
170                    files: Field::Value(files.clone()),
171                    timeout_ms: (*timeout_ms).into(),
172                    extra: Map::new(),
173                }),
174                ..Default::default()
175            },
176            config::Fingerprint::Artifactsum { files, ignore } => Self {
177                files: Field::Value(files.clone()),
178                ignore: Field::Value(ignore.clone().into()),
179                ..Default::default()
180            },
181        }
182    }
183}