typesafe-system-one 0.1.1

Unofficial async Rust client for the TypeSafe AI System One API (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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//! Response types: answers, usage, models, and hand-rolled decoding with
//! dotted-path validation errors.
//!
//! Answers are decoded by hand from [`serde_json::Value`] (not via derive) so
//! that every validation failure can carry a dotted field path such as
//! `answers.tone.confidence`, per the SPEC. An answer with an unrecognized
//! `type` is not an error: it is kept as an opaque [`Answer::Unknown`] that
//! carries its raw JSON.

use std::collections::BTreeMap;

use serde_json::Value;

use crate::errors::Error;

/// The answer to a yes/no question: the probability of yes (0 to 1).
///
/// There is no confidence value for noul answers.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NoulAnswer {
    /// Probability of a yes answer, from 0 to 1. Near 0.5 indicates uncertainty.
    pub noul: f64,
}

/// The answer to a choice question.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ChoiceAnswer {
    /// The name of the option with the highest probability.
    pub choice: String,
    /// Probability of each option, from 0 to 1; the values sum to ~1.
    pub probabilities: BTreeMap<String, f64>,
    /// Confidence in the selected option, from 0 to 1.
    pub confidence: f64,
}

impl ChoiceAnswer {
    /// Returns `(option, probability)` pairs sorted by probability,
    /// descending.
    pub fn ranked(&self) -> Vec<(&str, f64)> {
        let mut ranked: Vec<(&str, f64)> = self
            .probabilities
            .iter()
            .map(|(option, probability)| (option.as_str(), *probability))
            .collect();
        ranked.sort_by(|a, b| {
            b.1.partial_cmp(&a.1)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then_with(|| a.0.cmp(b.0))
        });
        ranked
    }
}

/// The answer to a score question: a probability-weighted average of the
/// rubric levels.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ScoreAnswer {
    /// The expected score. It may fall between integer levels.
    pub score: f64,
    /// Confidence in the score, from 0 to 1.
    pub confidence: f64,
    /// The requested criteria mapped to their score levels, keyed by
    /// integer level index.
    pub legend: BTreeMap<u32, Value>,
    /// Probability of each score level, keyed by integer level index.
    pub probabilities: BTreeMap<u32, f64>,
}

/// Any answer, discriminated by its `type` field.
///
/// Unknown future types are kept losslessly as [`Answer::Unknown`].
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Answer {
    /// A yes/no answer.
    Noul(NoulAnswer),
    /// A one-of-many selection.
    Choice(ChoiceAnswer),
    /// A rubric rating.
    Score(ScoreAnswer),
    /// An answer whose `type` this client does not know, carrying its raw
    /// JSON and the type name.
    Unknown {
        /// The unrecognized `type` value.
        kind: String,
        /// The raw JSON of the answer.
        raw: Value,
    },
}

impl From<NoulAnswer> for Answer {
    fn from(answer: NoulAnswer) -> Self {
        Self::Noul(answer)
    }
}

impl From<ChoiceAnswer> for Answer {
    fn from(answer: ChoiceAnswer) -> Self {
        Self::Choice(answer)
    }
}

impl From<ScoreAnswer> for Answer {
    fn from(answer: ScoreAnswer) -> Self {
        Self::Score(answer)
    }
}

/// Token usage for a request. Both fields default to 0 when the server
/// omits them, because some gateways omit usage.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Usage {
    /// Number of billable input tokens used to evaluate the request.
    pub input_tokens: u64,
    /// Number of output tokens used to answer the questions. Currently free.
    pub output_tokens: u64,
}

/// The response to a [`SystemOneRequest`](crate::SystemOneRequest).
#[derive(Debug, Clone, PartialEq)]
pub struct SystemOneResponse {
    /// The versioned ID of the model that actually answered, which may
    /// differ from the alias supplied in the request.
    pub model: String,
    /// Answers keyed by the question IDs supplied in the request.
    pub answers: BTreeMap<String, Answer>,
    /// Token usage, defaulting to 0 per field when the server omits it.
    pub usage: Usage,
    /// The `x-typesafe-request-id` response header, when present.
    pub request_id: Option<String>,
}

impl SystemOneResponse {
    /// Returns the noul answer for `id`, when that question was answered as
    /// a noul.
    pub fn noul(&self, id: &str) -> Option<&NoulAnswer> {
        match self.answers.get(id)? {
            Answer::Noul(answer) => Some(answer),
            _ => None,
        }
    }

    /// Returns the choice answer for `id`, when that question was answered
    /// as a choice.
    pub fn choice(&self, id: &str) -> Option<&ChoiceAnswer> {
        match self.answers.get(id)? {
            Answer::Choice(answer) => Some(answer),
            _ => None,
        }
    }

    /// Returns the score answer for `id`, when that question was answered as
    /// a score.
    pub fn score(&self, id: &str) -> Option<&ScoreAnswer> {
        match self.answers.get(id)? {
            Answer::Score(answer) => Some(answer),
            _ => None,
        }
    }

    /// Iterates every `(id, noul answer)` pair.
    pub fn nouls(&self) -> impl Iterator<Item = (&str, &NoulAnswer)> {
        self.answers.iter().filter_map(|(id, answer)| match answer {
            Answer::Noul(answer) => Some((id.as_str(), answer)),
            _ => None,
        })
    }

    /// Iterates every `(id, choice answer)` pair.
    pub fn choices(&self) -> impl Iterator<Item = (&str, &ChoiceAnswer)> {
        self.answers.iter().filter_map(|(id, answer)| match answer {
            Answer::Choice(answer) => Some((id.as_str(), answer)),
            _ => None,
        })
    }

    /// Iterates every `(id, score answer)` pair.
    pub fn scores(&self) -> impl Iterator<Item = (&str, &ScoreAnswer)> {
        self.answers.iter().filter_map(|(id, answer)| match answer {
            Answer::Score(answer) => Some((id.as_str(), answer)),
            _ => None,
        })
    }
}

/// Metadata describing one available model or alias.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ModelMetadata {
    /// Model name or alias accepted by a request's `model` field.
    pub name: String,
    /// Human-readable description of the model and its capabilities.
    pub description: String,
    /// Model release date, formatted as `YYYY-MM-DD`.
    pub release_date: String,
}

/// A response validation failure with its dotted field path.
fn validation_error(
    status: u16,
    field_path: &str,
    request_id: Option<String>,
    endpoint: &str,
) -> Error {
    Error::ResponseValidation {
        status,
        field_path: field_path.to_owned(),
        request_id,
        endpoint: endpoint.to_owned(),
    }
}

/// Builds the validation error for a failed 2xx decode, including the
/// response's request ID.
fn decode_error(response: &crate::client::RawResponse, field_path: &str) -> Error {
    validation_error(
        response.status,
        field_path,
        response.request_id.clone(),
        &response.endpoint,
    )
}

/// Decodes a number field, reporting `path` when missing or not a number.
fn number(value: &Value, path: &str) -> Result<f64, String> {
    match value {
        Value::Number(number) => number.as_f64().ok_or_else(|| path.to_owned()),
        _ => Err(path.to_owned()),
    }
}

/// Decodes a string field, reporting `path` when missing or not a string.
fn string(value: &Value, path: &str) -> Result<String, String> {
    match value {
        Value::String(string) => Ok(string.clone()),
        _ => Err(path.to_owned()),
    }
}

/// Decodes a `{key: number}` map.
fn number_map(value: &Value, path: &str) -> Result<BTreeMap<String, f64>, String> {
    let Value::Object(map) = value else {
        return Err(path.to_owned());
    };
    let mut result = BTreeMap::new();
    for (key, value) in map {
        let key_path = format!("{path}.{key}");
        result.insert(key.clone(), number(value, &key_path)?);
    }
    Ok(result)
}

/// Decodes a `{"<level>": number}` map with decimal level-index keys into
/// integer keys.
fn level_number_map(value: &Value, path: &str) -> Result<BTreeMap<u32, f64>, String> {
    let Value::Object(map) = value else {
        return Err(path.to_owned());
    };
    let mut result = BTreeMap::new();
    for (key, value) in map {
        let key_path = format!("{path}.{key}");
        let level: u32 = key.parse().map_err(|_| key_path.clone())?;
        result.insert(level, number(value, &key_path)?);
    }
    Ok(result)
}

/// Decodes a `{"<level>": string|object|array}` legend map into integer keys.
fn level_value_map(value: &Value, path: &str) -> Result<BTreeMap<u32, Value>, String> {
    let Value::Object(map) = value else {
        return Err(path.to_owned());
    };
    let mut result = BTreeMap::new();
    for (key, value) in map {
        let key_path = format!("{path}.{key}");
        // Legend values are string, object, or array (OpenAPI
        // `ScoreAnswer.legend`); null, booleans, and numbers are invalid.
        if !matches!(value, Value::String(_) | Value::Object(_) | Value::Array(_)) {
            return Err(key_path);
        }
        let level: u32 = key.parse().map_err(|_| key_path.clone())?;
        result.insert(level, value.clone());
    }
    Ok(result)
}

/// Decodes one answer, reporting `answers.<id>.<field>` paths on failure.
fn decode_answer(id: &str, value: &Value) -> Result<Answer, String> {
    let Value::Object(answer) = value else {
        return Err(format!("answers.{id}"));
    };
    let answer_type = match answer.get("type") {
        Some(Value::String(answer_type)) => answer_type.as_str(),
        _ => return Err(format!("answers.{id}.type")),
    };
    let field = |name: &str| answer.get(name);
    match answer_type {
        "noul" => {
            let noul = field("noul").ok_or_else(|| format!("answers.{id}.noul"))?;
            Ok(Answer::Noul(NoulAnswer {
                noul: number(noul, &format!("answers.{id}.noul"))?,
            }))
        }
        "choice" => {
            let choice = field("choice").ok_or_else(|| format!("answers.{id}.choice"))?;
            let probabilities =
                field("probabilities").ok_or_else(|| format!("answers.{id}.probabilities"))?;
            let confidence =
                field("confidence").ok_or_else(|| format!("answers.{id}.confidence"))?;
            Ok(Answer::Choice(ChoiceAnswer {
                choice: string(choice, &format!("answers.{id}.choice"))?,
                probabilities: number_map(probabilities, &format!("answers.{id}.probabilities"))?,
                confidence: number(confidence, &format!("answers.{id}.confidence"))?,
            }))
        }
        "score" => {
            let score = field("score").ok_or_else(|| format!("answers.{id}.score"))?;
            let confidence =
                field("confidence").ok_or_else(|| format!("answers.{id}.confidence"))?;
            let legend = field("legend").ok_or_else(|| format!("answers.{id}.legend"))?;
            let probabilities =
                field("probabilities").ok_or_else(|| format!("answers.{id}.probabilities"))?;
            Ok(Answer::Score(ScoreAnswer {
                score: number(score, &format!("answers.{id}.score"))?,
                confidence: number(confidence, &format!("answers.{id}.confidence"))?,
                legend: level_value_map(legend, &format!("answers.{id}.legend"))?,
                probabilities: level_number_map(
                    probabilities,
                    &format!("answers.{id}.probabilities"),
                )?,
            }))
        }
        other => {
            tracing::warn!(
                answer_id = %id,
                answer_type = %other,
                "keeping answer with unrecognized type as Unknown"
            );
            Ok(Answer::Unknown {
                kind: other.to_owned(),
                raw: value.clone(),
            })
        }
    }
}

use serde::{Deserialize, Serialize};

impl SystemOneResponse {
    /// Decodes a 2xx response body, validating its structure.
    ///
    /// The body must be a JSON object with `model` (string) and `answers`
    /// (object). Each answer must be an object with a string `type` and
    /// every field its known `type` requires. Failures produce a
    /// [`Error::ResponseValidation`] with a dotted field path and the HTTP
    /// status. Unrecognized answer types are kept as [`Answer::Unknown`].
    pub(super) fn decode(response: &crate::client::RawResponse) -> Result<Self, Error> {
        let body: Value =
            serde_json::from_str(&response.body).map_err(|_| decode_error(response, ""))?;
        let Value::Object(object) = &body else {
            return Err(decode_error(response, ""));
        };
        let model = string(
            object
                .get("model")
                .ok_or_else(|| decode_error(response, "model"))?,
            "model",
        )
        .map_err(|_| decode_error(response, "model"))?;
        let answers_value = object
            .get("answers")
            .ok_or_else(|| decode_error(response, "answers"))?;
        let answers_object = match answers_value {
            Value::Object(map) => map,
            _ => return Err(decode_error(response, "answers")),
        };
        let mut answers: BTreeMap<String, Answer> = BTreeMap::new();
        for (id, answer_value) in answers_object {
            let decoded = decode_answer(id.as_str(), answer_value)
                .map_err(|field_path| decode_error(response, &field_path))?;
            answers.insert(id.clone(), decoded);
        }
        // Usage may be absent (some gateways omit it) and so may either
        // field, defaulting to 0; a present value of the wrong type is a
        // validation error, as in the Go client and the Python SDK.
        let usage = match object.get("usage") {
            None | Some(Value::Null) => Usage::default(),
            Some(Value::Object(usage)) => {
                let tokens = |key: &str| -> Result<u64, Error> {
                    match usage.get(key) {
                        None | Some(Value::Null) => Ok(0),
                        Some(value) => value
                            .as_u64()
                            .ok_or_else(|| decode_error(response, &format!("usage.{key}"))),
                    }
                };
                Usage {
                    input_tokens: tokens("input_tokens")?,
                    output_tokens: tokens("output_tokens")?,
                }
            }
            Some(_) => return Err(decode_error(response, "usage")),
        };
        Ok(Self {
            model,
            answers,
            usage,
            request_id: response.request_id.clone(),
        })
    }
}

/// Decodes a `GET /v1/models` response.
/// Requires an answer for every requested question, and that an answer of a
/// known type matches its question's type. Unknown (newer) answer types and
/// extra answer IDs pass through. Without this, a typed accessor on a
/// missing answer returns `None`, which is easy to mistake for a real "no".
pub(super) fn check_complete(
    response: &crate::client::RawResponse,
    answers: &BTreeMap<String, Answer>,
    questions: &BTreeMap<String, crate::questions::Question>,
) -> Result<(), Error> {
    use crate::questions::Question;
    // BTreeMap iteration is sorted, so the first problem reported is
    // deterministic.
    for (id, question) in questions {
        let Some(answer) = answers.get(id) else {
            return Err(decode_error(response, &format!("answers.{id}")));
        };
        let matches = matches!(
            (question, answer),
            (Question::Noul(_), Answer::Noul(_))
                | (Question::Choice(_), Answer::Choice(_))
                | (Question::Score(_), Answer::Score(_))
                | (_, Answer::Unknown { .. })
        );
        if !matches {
            return Err(decode_error(response, &format!("answers.{id}.type")));
        }
    }
    Ok(())
}

pub(super) fn decode_models(
    response: &crate::client::RawResponse,
) -> Result<Vec<ModelMetadata>, Error> {
    let body: Value =
        serde_json::from_str(&response.body).map_err(|_| decode_error(response, ""))?;
    let Value::Object(object) = &body else {
        return Err(decode_error(response, ""));
    };
    let Value::Array(models) = object
        .get("models")
        .ok_or_else(|| decode_error(response, "models"))?
    else {
        return Err(decode_error(response, "models"));
    };
    let mut result = Vec::with_capacity(models.len());
    for (index, model) in models.iter().enumerate() {
        // Same path format as the Go client and the Python SDK:
        // `models[0].release_date`.
        let path = format!("models[{index}]");
        let Value::Object(model) = model else {
            return Err(decode_error(response, &path));
        };
        let field = |key: &str| -> Result<String, Error> {
            let field_path = format!("{path}.{key}");
            let value = model
                .get(key)
                .ok_or_else(|| decode_error(response, &field_path))?;
            string(value, &field_path).map_err(|p| decode_error(response, &p))
        };
        result.push(ModelMetadata {
            name: field("name")?,
            description: field("description")?,
            release_date: field("release_date")?,
        });
    }
    Ok(result)
}

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

    fn raw(body: &str) -> crate::client::RawResponse {
        crate::client::RawResponse {
            status: 200,
            body: body.to_owned(),
            request_id: None,
            endpoint: "POST https://api.typesafe.ai/v1/systemone".into(),
        }
    }

    #[test]
    fn decodes_all_three_answer_kinds() {
        let body = r#"{
            "model": "jev-1.13.0",
            "answers": {
                "spam": {"type": "noul", "noul": 0.98},
                "tone": {"type": "choice", "choice": "angry", "probabilities": {"angry": 0.8, "calm": 0.1, "excited": 0.1}, "confidence": 0.9},
                "urgency": {"type": "score", "score": 1.7, "confidence": 0.9, "legend": {"0": "Can wait", "1": "This week", "2": "Today"}, "probabilities": {"0": 0.1, "1": 0.1, "2": 0.8}}
            },
            "usage": {"input_tokens": 120, "output_tokens": 12}
        }"#;
        let response = SystemOneResponse::decode(&raw(body)).unwrap();
        assert_eq!(response.model, "jev-1.13.0");
        assert_eq!(response.noul("spam").unwrap().noul, 0.98);
        let tone = response.choice("tone").unwrap();
        assert_eq!(tone.choice, "angry");
        assert_eq!(tone.confidence, 0.9);
        assert_eq!(tone.probabilities["angry"], 0.8);
        let urgency = response.score("urgency").unwrap();
        assert_eq!(urgency.score, 1.7);
        assert_eq!(urgency.legend[&2], "Today");
        assert_eq!(urgency.probabilities[&2], 0.8);
        assert_eq!(response.usage.input_tokens, 120);
        assert_eq!(response.usage.output_tokens, 12);
        assert_eq!(response.nouls().count(), 1);
        assert_eq!(response.choices().count(), 1);
        assert_eq!(response.scores().count(), 1);
        assert!(response.noul("tone").is_none());
    }

    #[test]
    fn ranked_sorts_descending() {
        let tone = ChoiceAnswer {
            choice: "angry".into(),
            probabilities: [("calm", 0.1), ("angry", 0.8), ("excited", 0.1)]
                .into_iter()
                .map(|(key, value)| (key.to_owned(), value))
                .collect(),
            confidence: 0.9,
        };
        let ranked = tone.ranked();
        assert_eq!(ranked[0], ("angry", 0.8));
        // Ties are broken alphabetically.
        assert_eq!(ranked[1].0, "calm");
        assert_eq!(ranked[2].0, "excited");
    }

    #[test]
    fn unknown_answer_type_is_kept() {
        let body = r#"{
            "model": "jev-1.13.0",
            "answers": {
                "spam": {"type": "noul", "noul": 0.98},
                "future": {"type": "vibes", "vibe": "immaculate", "confidence": 0.5}
            },
            "usage": {"input_tokens": 1, "output_tokens": 2}
        }"#;
        let response = SystemOneResponse::decode(&raw(body)).unwrap();
        match response.answers.get("future").unwrap() {
            Answer::Unknown { kind, raw } => {
                assert_eq!(kind, "vibes");
                assert_eq!(raw["vibe"], "immaculate");
            }
            other => panic!("expected Unknown, got {other:?}"),
        }
        // Typed accessors skip it.
        assert!(response.noul("future").is_none());
        assert_eq!(response.nouls().count(), 1);
    }

    #[test]
    fn missing_usage_defaults_to_zero() {
        let body = r#"{"model": "jev-1.13.0", "answers": {"a": {"type": "noul", "noul": 1.0}}}"#;
        let response = SystemOneResponse::decode(&raw(body)).unwrap();
        assert_eq!(response.usage, Usage::default());
    }

    #[test]
    fn validation_error_field_paths() {
        // Missing confidence on a choice answer.
        let body = r#"{"model": "m", "answers": {"tone": {"type": "choice", "choice": "calm", "probabilities": {"calm": 1.0}}}, "usage": {}}"#;
        let error = SystemOneResponse::decode(&raw(body)).unwrap_err();
        match error {
            Error::ResponseValidation {
                field_path, status, ..
            } => {
                assert_eq!(field_path, "answers.tone.confidence");
                assert_eq!(status, 200);
            }
            other => panic!("expected ResponseValidation, got {other:?}"),
        }
        // Non-object body.
        let error = SystemOneResponse::decode(&raw("nope")).unwrap_err();
        match error {
            Error::ResponseValidation { field_path, .. } => {
                assert_eq!(field_path, "");
            }
            other => panic!("expected ResponseValidation, got {other:?}"),
        }
        // Missing model.
        let error = SystemOneResponse::decode(&raw(r#"{"answers": {}}"#)).unwrap_err();
        match error {
            Error::ResponseValidation { field_path, .. } => {
                assert_eq!(field_path, "model");
            }
            other => panic!("expected ResponseValidation, got {other:?}"),
        }
        // Missing answers.
        let error = SystemOneResponse::decode(&raw(r#"{"model": "m"}"#)).unwrap_err();
        match error {
            Error::ResponseValidation { field_path, .. } => {
                assert_eq!(field_path, "answers");
            }
            other => panic!("expected ResponseValidation, got {other:?}"),
        }
        // Bad answer type field.
        let body = r#"{"model": "m", "answers": {"a": {"noul": 1.0}}}"#;
        let error = SystemOneResponse::decode(&raw(body)).unwrap_err();
        match error {
            Error::ResponseValidation { field_path, .. } => {
                assert_eq!(field_path, "answers.a.type");
            }
            other => panic!("expected ResponseValidation, got {other:?}"),
        }
        // Non-numeric noul.
        let body = r#"{"model": "m", "answers": {"a": {"type": "noul", "noul": "yes"}}}"#;
        let error = SystemOneResponse::decode(&raw(body)).unwrap_err();
        match error {
            Error::ResponseValidation { field_path, .. } => {
                assert_eq!(field_path, "answers.a.noul");
            }
            other => panic!("expected ResponseValidation, got {other:?}"),
        }
    }

    #[test]
    fn decodes_models() {
        let body = r#"{"models": [
            {"name": "jev-latest", "description": "General-purpose system one model.", "release_date": "2026-09-15"}
        ]}"#;
        let models = decode_models(&raw(body)).unwrap();
        assert_eq!(models.len(), 1);
        assert_eq!(models[0].name, "jev-latest");
        assert_eq!(models[0].release_date, "2026-09-15");
        let error = decode_models(&raw(r#"{}"#)).unwrap_err();
        match error {
            Error::ResponseValidation { field_path, .. } => {
                assert_eq!(field_path, "models");
            }
            other => panic!("expected ResponseValidation, got {other:?}"),
        }
    }
}