1use super::{Backend, ModelId, Profile, ProfileKind, Reasoning};
5use serde::{Deserialize, Deserializer, Serialize, Serializer};
6use std::time::Duration;
7
8#[derive(Debug, Clone, Default, PartialEq, Eq)]
9pub enum Field<T> {
10 #[default]
11 Missing,
12 Null,
13 Value(T),
14}
15impl<T> Field<T> {
16 pub fn value(&self) -> Option<&T> {
17 match self {
18 Self::Value(value) => Some(value),
19 _ => None,
20 }
21 }
22 pub(crate) fn missing(&self) -> bool {
23 matches!(self, Self::Missing)
24 }
25}
26impl<T: Serialize> Serialize for Field<T> {
27 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
28 match self {
29 Self::Value(value) => value.serialize(serializer),
30 _ => serializer.serialize_none(),
31 }
32 }
33}
34impl<'de, T: Deserialize<'de>> Deserialize<'de> for Field<T> {
35 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
36 Ok(Option::<T>::deserialize(deserializer)?.map_or(Self::Null, Self::Value))
37 }
38}
39impl<T> From<Option<T>> for Field<T> {
40 fn from(value: Option<T>) -> Self {
41 value.map_or(Self::Null, Self::Value)
42 }
43}
44
45#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
48pub struct StoredPayload {
49 #[serde(default, skip_serializing_if = "Field::missing")]
50 instruction: Field<String>,
51 #[serde(flatten)]
52 pub extra: serde_json::Map<String, serde_json::Value>,
53}
54
55impl StoredPayload {
56 pub fn instruction(&self) -> &str {
57 match &self.instruction {
58 Field::Value(value) => value,
59 Field::Missing | Field::Null => "",
60 }
61 }
62}
63
64impl From<&Option<super::EvalPayload>> for StoredPayload {
65 fn from(payload: &Option<super::EvalPayload>) -> Self {
66 payload.as_ref().map_or_else(
67 || Self {
68 instruction: Field::Missing,
69 extra: Default::default(),
70 },
71 Self::from,
72 )
73 }
74}
75
76impl From<&super::EvalPayload> for StoredPayload {
77 fn from(payload: &super::EvalPayload) -> Self {
78 Self {
79 instruction: Field::Value(payload.instruction.clone()),
80 extra: Default::default(),
81 }
82 }
83}
84
85#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
86#[serde(tag = "kind", rename_all = "lowercase")]
87pub enum StoredProfile {
88 Agent {
89 backend: Backend,
90 model: ModelId,
91 #[serde(default, skip_serializing_if = "Field::missing")]
92 reasoning: Field<Reasoning>,
93 #[serde(
94 rename = "timeoutMs",
95 default,
96 skip_serializing_if = "Field::missing",
97 with = "milliseconds"
98 )]
99 timeout_ms: Field<Duration>,
100 #[serde(
101 rename = "maxToolCalls",
102 default,
103 skip_serializing_if = "Field::missing"
104 )]
105 max_tool_calls: Field<u64>,
106 #[serde(rename = "maxTokens", default, skip_serializing_if = "Field::missing")]
107 max_tokens: Field<u64>,
108 },
109 Human {},
110 Runtime {
111 #[serde(serialize_with = "crate::platform::serialize_program")]
112 command: String,
113 args: Vec<String>,
114 #[serde(
115 rename = "timeoutMs",
116 default,
117 skip_serializing_if = "Field::missing",
118 with = "milliseconds"
119 )]
120 timeout_ms: Field<Duration>,
121 },
122}
123impl StoredProfile {
124 pub fn kind(&self) -> ProfileKind {
125 match self {
126 Self::Agent { .. } => ProfileKind::Agent,
127 Self::Human {} => ProfileKind::Human,
128 Self::Runtime { .. } => ProfileKind::Runtime,
129 }
130 }
131}
132impl From<&Profile> for StoredProfile {
133 fn from(profile: &Profile) -> Self {
134 match profile {
135 Profile::Agent {
136 backend,
137 model,
138 reasoning,
139 timeout_ms,
140 max_tool_calls,
141 max_tokens,
142 } => Self::Agent {
143 backend: backend.clone(),
144 model: model.clone(),
145 reasoning: (*reasoning).into(),
146 timeout_ms: (*timeout_ms).into(),
147 max_tool_calls: (*max_tool_calls).into(),
148 max_tokens: (*max_tokens).into(),
149 },
150 Profile::Human {} => Self::Human {},
151 Profile::Runtime {
152 command,
153 args,
154 timeout_ms,
155 } => Self::Runtime {
156 command: crate::platform::program_text(command),
158 args: args.iter().map(ToString::to_string).collect(),
159 timeout_ms: (*timeout_ms).into(),
160 },
161 }
162 }
163}
164mod milliseconds {
165 use super::*;
166 pub fn serialize<S: Serializer>(
167 value: &Field<Duration>,
168 serializer: S,
169 ) -> Result<S::Ok, S::Error> {
170 match value {
171 Field::Value(value) => serializer.serialize_u128(value.as_millis()),
172 _ => serializer.serialize_none(),
173 }
174 }
175 pub fn deserialize<'de, D: Deserializer<'de>>(
176 deserializer: D,
177 ) -> Result<Field<Duration>, D::Error> {
178 let value = Option::<serde_json::Number>::deserialize(deserializer)?;
180 value.map_or(Ok(Field::Null), |value| {
181 super::super::validation::timeout_number::<D::Error>(value).map(Field::Value)
182 })
183 }
184}
185
186impl From<Option<&super::EvalPayload>> for StoredPayload {
187 fn from(payload: Option<&super::EvalPayload>) -> Self {
188 payload.map_or_else(
189 || Self {
190 instruction: Field::Missing,
191 extra: Default::default(),
192 },
193 Self::from,
194 )
195 }
196}
197
198#[cfg(test)]
199mod tests;