typesafeai-sdk 0.2.0

Rust SDK for TypeSafe AI — typed judgments from System One models like Jev
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Question types for the TypeSafe SDK.
//!
//! Three primitives, matching the Python and JavaScript SDKs:
//!
//! - [`Noul`] — a yes/no question returning a probability in `[0, 1]`.
//! - [`Choice`] — a multiple-choice question returning a label, probabilities,
//!   and a confidence score.
//! - [`Score`] — a rubric-based question returning a numeric score, a legend,
//!   probabilities, and a confidence score.
//!
//! Each type serializes to the wire format expected by `POST /v1/systemone`:
//! a JSON object with a `"type"` discriminator and `"instructions"` /
//! `"criteria"` fields.

use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use serde_json::Value;

// ---------------------------------------------------------------------------
// Description type
// ---------------------------------------------------------------------------

/// A criterion description. Can be a string, a JSON object/array, or `null`
/// (meaning "undescribed"). Mirrors the JS SDK's `EntryType` / `Description`.
pub type Description = Option<Value>;

// ---------------------------------------------------------------------------
// Noul
// ---------------------------------------------------------------------------

/// Optional descriptions for the `true` and `false` outcomes of a [`Noul`]
/// question.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct NoulCriteria {
    /// Description of the "yes" outcome.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#true: Description,
    /// Description of the "no" outcome.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#false: Description,
}

/// A yes/no question. Returns a probability in `[0, 1]`.
///
/// Wire format: `{"type":"noul","instructions":"...","criteria":{"true":...,"false":...}}`
///
/// The `"type"` tag is added by the [`Question`] enum's `#[serde(tag = "type")]`
/// attribute; this struct only carries `instructions` and `criteria`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Noul {
    /// The question as text, a JSON object, or an array. May be `null`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub instructions: Description,
    /// Optional descriptions of the yes and no outcomes.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub criteria: Option<NoulCriteria>,
}

impl Noul {
    /// Create a new noul question with the given instructions.
    ///
    /// `instructions` can be a string, `null`, or any JSON value.
    /// Pass [`serde_json::Value::Null`] to explicitly send `null` as the
    /// instructions (the API default).
    pub fn new(instructions: impl Into<Value>) -> Self {
        Self {
            instructions: Some(instructions.into()),
            criteria: None,
        }
    }

    /// Attach criteria describing the yes/no outcomes.
    #[must_use = "the returned Noul should be used"]
    pub fn with_criteria(mut self, criteria: NoulCriteria) -> Self {
        self.criteria = Some(criteria);
        self
    }
}

// ---------------------------------------------------------------------------
// Choice
// ---------------------------------------------------------------------------

/// A multiple-choice question. Returns a selected label, per-label
/// probabilities, and a confidence score.
///
/// Wire format:
/// `{"type":"choice","instructions":"...","criteria":{"label":"desc",...}}`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Choice {
    /// The question as text, a JSON object, or an array. May be `null`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub instructions: Description,
    /// Labels mapped to descriptions. `null` means "undescribed".
    pub criteria: HashMap<String, Description>,
}

impl Choice {
    /// Create a new choice question with the given instructions and criteria.
    ///
    /// `criteria` is a map of label → description (or `None` for undescribed).
    pub fn new(instructions: impl Into<Value>, criteria: HashMap<String, Description>) -> Self {
        Self {
            instructions: Some(instructions.into()),
            criteria,
        }
    }
}

// ---------------------------------------------------------------------------
// Score
// ---------------------------------------------------------------------------

/// A rubric-based question. Returns a numeric score, a legend mapping scores
/// to descriptions, per-score probabilities, and a confidence score.
///
/// Wire format:
/// `{"type":"score","instructions":"...","criteria":["desc0","desc1",...]}`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Score {
    /// The question as text, a JSON object, or an array. May be `null`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub instructions: Description,
    /// Ordered list of descriptions indexed by score from zero. At least two
    /// entries are required. Entries may be `null` (undescribed).
    pub criteria: Vec<Description>,
}

impl Score {
    /// Create a new score question with the given instructions and criteria.
    ///
    /// `criteria` must have at least two entries.
    pub fn new(instructions: impl Into<Value>, criteria: Vec<Description>) -> Self {
        Self {
            instructions: Some(instructions.into()),
            criteria,
        }
    }
}

// ---------------------------------------------------------------------------
// Question enum
// ---------------------------------------------------------------------------

/// A question of any type, identified by its `type` field.
///
/// This enum is used as the value type in the `questions` map sent to the API.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
#[non_exhaustive]
pub enum Question {
    /// A yes/no question.
    #[serde(rename = "noul")]
    Noul(Noul),
    /// A multiple-choice question.
    #[serde(rename = "choice")]
    Choice(Choice),
    /// A rubric-based score question.
    #[serde(rename = "score")]
    Score(Score),
}

impl From<Noul> for Question {
    fn from(q: Noul) -> Self {
        Question::Noul(q)
    }
}

impl From<Choice> for Question {
    fn from(q: Choice) -> Self {
        Question::Choice(q)
    }
}

impl From<Score> for Question {
    fn from(q: Score) -> Self {
        Question::Score(q)
    }
}

// ---------------------------------------------------------------------------
// Builder functions (mirrors the JS SDK's `noul()`, `choice()`, `score()`)
// ---------------------------------------------------------------------------

/// Build a yes/no question. `instructions` defaults to `null` when omitted.
///
/// # Examples
/// ```
/// use typesafeai_sdk::noul;
///
/// let q = noul("Is this about billing?");
/// let q = noul(serde_json::Value::Null); // no instructions
/// ```
pub fn noul(instructions: impl Into<Value>) -> Noul {
    Noul::new(instructions)
}

/// Build a multiple-choice question.
///
/// # Examples
/// ```
/// use typesafeai_sdk::choice;
/// use std::collections::HashMap;
///
/// let mut criteria = HashMap::new();
/// criteria.insert("calm".to_string(), None);
/// criteria.insert("angry".to_string(), None);
/// let q = choice("What is the tone?", criteria);
/// ```
pub fn choice(instructions: impl Into<Value>, criteria: HashMap<String, Description>) -> Choice {
    Choice::new(instructions, criteria)
}

/// Build a rubric-based score question. `criteria` must have at least two
/// entries.
///
/// # Examples
/// ```
/// use typesafeai_sdk::score;
///
/// let q = score("How urgent?", vec![None, Some("high".into())]);
/// ```
pub fn score(instructions: impl Into<Value>, criteria: Vec<Description>) -> Score {
    Score::new(instructions, criteria)
}

// ---------------------------------------------------------------------------
// Validation
// ---------------------------------------------------------------------------

/// Validate a set of questions before sending.
///
/// Mirrors the JS SDK's `validateQuestions`:
/// - Questions must not be empty.
/// - Choice criteria must have at least two entries.
/// - Score criteria must be an array with at least two entries.
pub(crate) fn validate_questions(
    questions: &HashMap<String, Question>,
) -> crate::error::Result<()> {
    if questions.is_empty() {
        return Err(crate::error::TypeSafeError::new(
            crate::error::ErrorKind::Validation("At least one question is required.".to_string()),
        ));
    }

    for (name, question) in questions {
        if name.trim().is_empty() {
            return Err(crate::error::TypeSafeError::new(
                crate::error::ErrorKind::Validation(
                    "Question names cannot be empty or whitespace.".to_string(),
                ),
            ));
        }
        match question {
            Question::Choice(choice) => {
                if choice.criteria.len() < 2 {
                    return Err(crate::error::TypeSafeError::new(
                        crate::error::ErrorKind::Validation(format!(
                            "Choice question \"{name}\" has {} criteria; at least two choices are required.",
                            choice.criteria.len()
                        )),
                    ));
                }
            }
            Question::Score(score) => {
                if score.criteria.len() < 2 {
                    return Err(crate::error::TypeSafeError::new(
                        crate::error::ErrorKind::Validation(format!(
                            "Score question \"{name}\" has {} criteria; at least two scores are required.",
                            score.criteria.len()
                        )),
                    ));
                }
            }
            Question::Noul(_) => {}
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn noul_serializes_correctly() {
        let q: Question = noul("Is this about billing?").into();
        let json = serde_json::to_value(&q).unwrap();
        assert_eq!(json["type"], "noul");
        assert_eq!(json["instructions"], "Is this about billing?");
        assert!(json.get("criteria").is_none());
    }

    #[test]
    fn noul_with_criteria_serializes_correctly() {
        let q = noul("?").with_criteria(NoulCriteria {
            r#true: Some("yes means this".into()),
            r#false: None,
        });
        let json = serde_json::to_value(&q).unwrap();
        assert_eq!(json["criteria"]["true"], "yes means this");
        assert!(json["criteria"].get("false").is_none());
    }

    #[test]
    fn choice_serializes_correctly() {
        let mut criteria = HashMap::new();
        criteria.insert("calm".to_string(), None);
        criteria.insert("angry".to_string(), Some("very upset".into()));
        let q: Question = choice("Tone?", criteria).into();
        let json = serde_json::to_value(&q).unwrap();
        assert_eq!(json["type"], "choice");
        assert_eq!(json["instructions"], "Tone?");
        assert_eq!(json["criteria"]["calm"], serde_json::Value::Null);
        assert_eq!(json["criteria"]["angry"], "very upset");
    }

    #[test]
    fn score_serializes_correctly() {
        let q: Question = score("Urgency?", vec![Some("low".into()), Some("high".into())]).into();
        let json = serde_json::to_value(&q).unwrap();
        assert_eq!(json["type"], "score");
        assert_eq!(json["instructions"], "Urgency?");
        assert_eq!(json["criteria"][0], "low");
        assert_eq!(json["criteria"][1], "high");
    }

    #[test]
    fn question_enum_serializes_with_type_tag() {
        let q: Question = noul("?").into();
        let json = serde_json::to_value(&q).unwrap();
        assert_eq!(json["type"], "noul");
    }

    #[test]
    fn validate_rejects_empty_questions() {
        let questions = HashMap::new();
        let result = validate_questions(&questions);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("At least one question is required"));
    }

    #[test]
    fn validate_rejects_empty_question_name() {
        let mut questions = HashMap::new();
        questions.insert("".to_string(), Question::Noul(noul("?")));
        let result = validate_questions(&questions);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Question names cannot be empty"));
    }

    #[test]
    fn validate_rejects_whitespace_question_name() {
        let mut questions = HashMap::new();
        questions.insert("   ".to_string(), Question::Noul(noul("?")));
        let result = validate_questions(&questions);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Question names cannot be empty"));
    }

    #[test]
    fn validate_rejects_score_with_fewer_than_two_criteria() {
        let mut questions = HashMap::new();
        questions.insert(
            "q".to_string(),
            Question::Score(score("?", vec![Some("only".into())])),
        );
        let result = validate_questions(&questions);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("at least two"));
    }

    #[test]
    fn validate_rejects_choice_with_fewer_than_two_criteria() {
        let mut questions = HashMap::new();
        let mut criteria = HashMap::new();
        criteria.insert("only".to_string(), None);
        questions.insert("q".to_string(), Question::Choice(choice("?", criteria)));
        let result = validate_questions(&questions);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("at least two"));
    }

    #[test]
    fn validate_accepts_valid_questions() {
        let mut questions = HashMap::new();
        questions.insert("q".to_string(), Question::Noul(noul("?")));
        questions.insert(
            "s".to_string(),
            Question::Score(score("?", vec![Some("low".into()), Some("high".into())])),
        );
        let mut choice_criteria = HashMap::new();
        choice_criteria.insert("a".to_string(), None);
        choice_criteria.insert("b".to_string(), None);
        questions.insert(
            "c".to_string(),
            Question::Choice(choice("?", choice_criteria)),
        );
        assert!(validate_questions(&questions).is_ok());
    }
}