1use serde::{Deserialize, Serialize};
6
7pub use crate::types::decisions as openai;
8pub use crate::types::systemone;
9
10#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
12#[serde(untagged)]
13#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
14#[cfg_attr(feature = "protocol-schema", schema(as = DecisionSharedChoiceValue))]
15pub enum ChoiceValue {
16 String(String),
17 Bool(bool),
18}
19
20#[derive(Clone, Debug, Serialize, Deserialize)]
21#[serde(deny_unknown_fields)]
22#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
23#[cfg_attr(feature = "protocol-schema", schema(as = DecisionOpenAiRequest))]
24pub struct Request {
25 pub model: String,
26 pub input: Input,
27 pub questions: Vec<Question>,
28 #[serde(default, skip_serializing_if = "Option::is_none")]
29 pub safety_identifier: Option<String>,
30}
31
32#[derive(Clone, Debug, Serialize, Deserialize)]
33#[serde(untagged)]
34#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
35#[cfg_attr(feature = "protocol-schema", schema(as = DecisionOpenAiInput))]
36pub enum Input {
37 Text(String),
38 Messages(Vec<InputMessage>),
39}
40
41#[derive(Clone, Debug, Serialize, Deserialize)]
42#[serde(deny_unknown_fields)]
43#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
44#[cfg_attr(feature = "protocol-schema", schema(as = DecisionOpenAiInputMessage))]
45pub struct InputMessage {
46 pub role: UserRole,
47 pub content: Content,
48 #[serde(
49 default,
50 rename = "type",
51 deserialize_with = "optional_nonnull",
52 skip_serializing_if = "Option::is_none"
53 )]
54 #[cfg_attr(feature = "protocol-schema", schema(nullable = false))]
55 pub kind: Option<MessageType>,
56}
57
58#[derive(Clone, Debug, Serialize, Deserialize)]
59#[serde(rename_all = "lowercase")]
60#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
61#[cfg_attr(feature = "protocol-schema", schema(as = DecisionOpenAiUserRole))]
62pub enum UserRole {
63 User,
64}
65#[derive(Clone, Debug, Serialize, Deserialize)]
66#[serde(rename_all = "lowercase")]
67#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
68#[cfg_attr(feature = "protocol-schema", schema(as = DecisionOpenAiMessageType))]
69pub enum MessageType {
70 Message,
71}
72
73#[derive(Clone, Debug, Serialize, Deserialize)]
74#[serde(rename_all = "lowercase")]
75#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
76#[cfg_attr(feature = "protocol-schema", schema(as = DecisionOpenAiImageDetail))]
77pub enum ImageDetail {
78 Low,
79 High,
80 Auto,
81 Original,
82}
83
84#[derive(Clone, Debug, Serialize, Deserialize)]
85#[serde(untagged)]
86#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
87#[cfg_attr(feature = "protocol-schema", schema(as = DecisionOpenAiContent))]
88pub enum Content {
89 Text(String),
90 Parts(Vec<InputPart>),
91}
92
93#[derive(Clone, Debug, Serialize, Deserialize)]
94#[serde(tag = "type", rename_all = "snake_case", deny_unknown_fields)]
95#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
96#[cfg_attr(feature = "protocol-schema", schema(as = DecisionOpenAiInputPart))]
97pub enum InputPart {
98 InputText {
99 text: String,
100 },
101 InputImage {
102 image_url: String,
103 #[serde(default)]
104 detail: Option<ImageDetail>,
105 },
106}
107
108#[derive(Clone, Debug, Serialize, Deserialize)]
109#[serde(tag = "type", rename_all = "snake_case", deny_unknown_fields)]
110#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
111#[cfg_attr(feature = "protocol-schema", schema(as = DecisionOpenAiQuestion))]
112pub enum Question {
113 Predicate {
114 instructions: String,
115 #[serde(
116 default,
117 deserialize_with = "optional_nonnull",
118 skip_serializing_if = "Option::is_none"
119 )]
120 #[cfg_attr(feature = "protocol-schema", schema(nullable = false))]
121 name: Option<String>,
122 },
123 Choice {
124 instructions: String,
125 #[serde(
126 default,
127 deserialize_with = "optional_nonnull",
128 skip_serializing_if = "Option::is_none"
129 )]
130 #[cfg_attr(feature = "protocol-schema", schema(nullable = false))]
131 name: Option<String>,
132 choices: Vec<ChoiceOption>,
133 },
134 Score {
135 instructions: String,
136 #[serde(
137 default,
138 deserialize_with = "optional_nonnull",
139 skip_serializing_if = "Option::is_none"
140 )]
141 #[cfg_attr(feature = "protocol-schema", schema(nullable = false))]
142 name: Option<String>,
143 levels: Vec<ScoreLevel>,
144 },
145}
146
147#[derive(Clone, Debug, Serialize, Deserialize)]
148#[serde(deny_unknown_fields)]
149#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
150#[cfg_attr(feature = "protocol-schema", schema(as = DecisionOpenAiChoiceOption))]
151pub struct ChoiceOption {
152 pub value: ChoiceValue,
153 #[serde(
154 default,
155 deserialize_with = "optional_nonnull",
156 skip_serializing_if = "Option::is_none"
157 )]
158 #[cfg_attr(feature = "protocol-schema", schema(nullable = false))]
159 pub description: Option<String>,
160}
161
162#[derive(Clone, Debug, Serialize, Deserialize)]
163#[serde(deny_unknown_fields)]
164#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
165#[cfg_attr(feature = "protocol-schema", schema(as = DecisionOpenAiScoreLevel))]
166pub struct ScoreLevel {
167 pub label: String,
168 #[serde(
169 default,
170 deserialize_with = "optional_nonnull",
171 skip_serializing_if = "Option::is_none"
172 )]
173 #[cfg_attr(feature = "protocol-schema", schema(nullable = false))]
174 pub description: Option<String>,
175}
176
177fn optional_nonnull<'de, D: serde::Deserializer<'de>, T: Deserialize<'de>>(
178 deserializer: D,
179) -> Result<Option<T>, D::Error> {
180 T::deserialize(deserializer).map(Some)
181}
182
183#[derive(Clone, Debug, Serialize, Deserialize)]
184#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
185#[cfg_attr(feature = "protocol-schema", schema(as = DecisionOpenAiResponse))]
186pub struct Response {
187 pub model: String,
188 pub answers: Vec<Answer>,
189 pub usage: Usage,
190}
191
192#[derive(Clone, Debug, Serialize, Deserialize)]
193#[serde(tag = "type", rename_all = "snake_case")]
194#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
195#[cfg_attr(feature = "protocol-schema", schema(as = DecisionOpenAiAnswer))]
196pub enum Answer {
197 Predicate {
198 name: Option<String>,
199 probability: f64,
200 },
201 Choice {
202 name: Option<String>,
203 choice: ChoiceValue,
204 probabilities: Vec<ChoiceProbability>,
205 confidence: f64,
206 },
207 Score {
208 name: Option<String>,
209 score: f64,
210 probabilities: Vec<ScoreProbability>,
211 confidence: f64,
212 },
213 Refusal {
214 name: Option<String>,
215 },
216}
217
218#[derive(Clone, Debug, Serialize, Deserialize)]
219#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
220#[cfg_attr(feature = "protocol-schema", schema(as = DecisionOpenAiChoiceProbability))]
221pub struct ChoiceProbability {
222 pub value: ChoiceValue,
223 pub probability: f64,
224}
225#[derive(Clone, Debug, Serialize, Deserialize)]
226#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
227#[cfg_attr(feature = "protocol-schema", schema(as = DecisionOpenAiScoreProbability))]
228pub struct ScoreProbability {
229 pub value: usize,
230 pub label: String,
231 pub probability: f64,
232}
233#[derive(Clone, Debug, Serialize, Deserialize)]
234#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
235#[cfg_attr(feature = "protocol-schema", schema(as = DecisionOpenAiUsage))]
236pub struct Usage {
237 pub input_tokens: u64,
238 pub output_tokens: u64,
239 pub total_tokens: u64,
240 pub input_tokens_details: InputTokenDetails,
241 pub output_tokens_details: OutputTokenDetails,
242}
243#[derive(Clone, Debug, Serialize, Deserialize)]
244#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
245#[cfg_attr(feature = "protocol-schema", schema(as = DecisionOpenAiInputTokenDetails))]
246pub struct InputTokenDetails {
247 pub cached_tokens: u64,
248 pub cache_write_tokens: u64,
249}
250#[derive(Clone, Debug, Serialize, Deserialize)]
251#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
252#[cfg_attr(feature = "protocol-schema", schema(as = DecisionOpenAiOutputTokenDetails))]
253pub struct OutputTokenDetails {
254 pub reasoning_tokens: u64,
255}