Skip to main content

typesafe_rs/types/
request.rs

1use serde::{Deserialize, Serialize};
2
3use crate::types::question::Questions;
4
5/// Request body for `POST /v1/systemone`.
6#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
7pub struct SystemOneRequest {
8    /// Content to evaluate: a string, object, or array.
9    pub state: serde_json::Value,
10    /// Model name. Empty values inherit the client's default at send time.
11    pub model: String,
12    /// Named questions; answers come back under the same keys.
13    pub questions: Questions,
14}
15
16impl SystemOneRequest {
17    /// Build a request that inherits the client's default model.
18    #[must_use]
19    pub fn new(state: serde_json::Value, questions: Questions) -> Self {
20        Self {
21            state,
22            model: String::new(),
23            questions,
24        }
25    }
26
27    /// Override the model for this request.
28    #[must_use]
29    pub fn with_model(mut self, model: impl Into<String>) -> Self {
30        self.model = model.into();
31        self
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38    use crate::types::question::Question;
39    use serde_json::json;
40
41    #[test]
42    fn serializes_state_model_questions() {
43        let mut questions = Questions::new();
44        questions.insert("urgent".into(), Question::noul("urgent?"));
45        let req = SystemOneRequest::new(json!("hello"), questions).with_model("jev-latest");
46        let value = serde_json::to_value(&req).unwrap();
47        assert_eq!(value["state"], json!("hello"));
48        assert_eq!(value["model"], "jev-latest");
49        assert_eq!(value["questions"]["urgent"]["type"], "noul");
50    }
51}