muna 0.0.32

Run prediction functions in your Rust apps.
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
/*
*   Muna
*   Copyright © 2026 NatML Inc. All Rights Reserved.
*/

mod schema;

pub use schema::*;

use std::collections::HashMap;
use std::sync::Arc;

use indexmap::IndexMap;
use serde_json::Value as Json;
use tokio::sync::RwLock;

use crate::beta::utils::get_parameter;
use crate::client::Result;
use crate::MunaError;
use crate::services::{PredictionService, PredictorService};
use crate::types::{Acceleration, Dtype, Signature, Value};

/// Cached predictor metadata for fast System One evaluation.
struct DelegateInfo {
    state_param_name: String,
    questions_param_name: String,
    response_param_idx: usize,
}

/// Experimental TypeSafe-compatible System One client.
#[derive(Clone)]
pub struct TypeSafeClient {
    predictors: PredictorService,
    predictions: PredictionService,
    cache: Arc<RwLock<HashMap<String, DelegateInfo>>>,
}

impl TypeSafeClient {

    pub fn new(
        predictors: PredictorService,
        predictions: PredictionService
    ) -> Self {
        Self {
            predictors,
            predictions,
            cache: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Evaluate a state against a map of typed questions.
    ///
    /// # Arguments
    /// * `params` - Decision model tag, state, questions, acceleration, and devices.
    pub async fn system_one(
        &self,
        params: SystemOneCreateParams
    ) -> Result<SystemOneResponse> {
        validate_questions(&params.questions)?;
        let model = params.model.as_str();
        let acceleration = params.acceleration.unwrap_or(Acceleration::LocalAuto);
        {
            let needs_create = !self.cache.read().await.contains_key(model);
            if needs_create {
                let info = self.create_delegate_info(model).await?;
                self.cache
                    .write()
                    .await
                    .entry(model.to_string())
                    .or_insert(info);
            }
        }
        let cache = self.cache.read().await;
        let info = &cache[model];
        let input_map = bind_inputs(params.state, &params.questions, info)?;
        let response_param_idx = info.response_param_idx;
        drop(cache);
        let prediction = self
            .predictions
            .create(model, Some(input_map), Some(acceleration), params.devices.as_deref(), None, None)
            .await?;
        if let Some(ref error) = prediction.error {
            return Err(MunaError::from_prediction_error(error.clone()));
        }
        let results = prediction
            .results
            .ok_or_else(|| MunaError::Prediction(format!("{model} returned no results")))?;
        let Some(Value::Dict(output)) = results.get(response_param_idx) else {
            return Err(MunaError::Prediction(format!(
                "{model} returned a non-dict System One response"
            )));
        };
        let output: PredictorOutput = serde_json::from_value(Json::Object(output.clone()))
            .map_err(|e| MunaError::Prediction(format!(
                "{model} returned an invalid System One response: {e}"
            )))?;
        Ok(SystemOneResponse {
            model: model.to_string(),
            answers: order_answers(&params.questions, output.answers),
            usage: output.usage,
        })
    }

    async fn create_delegate_info(&self, tag: &str) -> Result<DelegateInfo> {
        let predictor = self.predictors.retrieve(tag).await?.ok_or_else(|| {
            MunaError::Prediction(format!(
                "{tag} cannot be used with TypeSafe System One API because \
                the predictor could not be found. Check that your access key \
                is valid and that you have access to the predictor."
            ))
        })?;
        resolve_delegate_info(tag, &predictor.signature)
    }
}

fn resolve_delegate_info(
    tag: &str,
    signature: &Signature
) -> Result<DelegateInfo> {
    let required_count = signature
        .inputs
        .iter()
        .filter(|p| !p.optional.unwrap_or(false))
        .count();
    if required_count != 2 {
        return Err(MunaError::Prediction(format!(
            "{tag} cannot be used with TypeSafe System One API because \
            it does not have exactly two required input parameters."
        )));
    }
    let find_input = |dtype: Dtype, dtype_name: &str, denotation: &str| {
        get_parameter(&signature.inputs, &[dtype], Some(denotation))
            .1
            .map(|p| p.name.clone())
            .ok_or_else(|| MunaError::Prediction(format!(
                "{tag} cannot be used with TypeSafe System One API because \
                it has no `{dtype_name}` input with a `{denotation}` denotation."
            )))
    };
    let state_param_name = find_input(
        Dtype::List,
        "list",
        "typesafe.systemone.state"
    )?;
    let questions_param_name = find_input(
        Dtype::Dict,
        "dict",
        "typesafe.systemone.questions"
    )?;
    let response_param_idx = signature
        .outputs
        .iter()
        .position(|param| {
            param.dtype == Some(Dtype::Dict) &&
            param.schema
                .as_ref()
                .and_then(|s| s.get("title"))
                .and_then(|v| v.as_str()) == Some("SystemOneResponse")
        })
        .ok_or_else(|| MunaError::Prediction(format!(
            "{tag} cannot be used with TypeSafe System One API because \
            it has no `SystemOneResponse` output."
        )))?;
    Ok(DelegateInfo { state_param_name, questions_param_name, response_param_idx })
}

fn bind_inputs(
    state: Json,
    questions: &IndexMap<String, Question>,
    info: &DelegateInfo
) -> Result<HashMap<String, Value>> {
    // `list` is the most general state shape: a string or object is a
    // one-item list, an array passes through unchanged.
    let state = match state {
        Json::Array(items) => items,
        Json::String(_) | Json::Object(_) => vec![state],
        _ => return Err(MunaError::InvalidInput(
            "`state` must be a string, object, or array.".into()
        )),
    };
    let Json::Object(questions) = serde_json::to_value(questions)? else {
        return Err(MunaError::InvalidInput("`questions` must be an object.".into()));
    };
    Ok(HashMap::from([
        (info.state_param_name.clone(), Value::List(state)),
        (info.questions_param_name.clone(), Value::Dict(questions)),
    ]))
}

fn order_answers(
    questions: &IndexMap<String, Question>,
    mut answers: IndexMap<String, Answer>
) -> IndexMap<String, Answer> {
    // Predictor dicts cross the FFI as `serde_json::Map`, which sorts keys,
    // so restore the caller's question order. Unknown ids keep their place
    // after the known ones.
    let mut ordered: IndexMap<String, Answer> = questions
        .keys()
        .filter_map(|id| answers.shift_remove_entry(id))
        .collect();
    ordered.extend(answers);
    ordered
}

fn validate_questions(questions: &IndexMap<String, Question>) -> Result<()> {
    if questions.is_empty() {
        return Err(MunaError::InvalidInput(
            "`questions` must contain at least one question.".into()
        ));
    }
    for (id, question) in questions {
        match question {
            Question::Choice { criteria, .. } if !(1..=255).contains(&criteria.len()) => {
                return Err(MunaError::InvalidInput(format!(
                    "`questions.{id}.criteria` must have between 1 and 255 options."
                )));
            }
            Question::Score { criteria, .. } if !(2..=10).contains(&criteria.len()) => {
                return Err(MunaError::InvalidInput(format!(
                    "`questions.{id}.criteria` must have between 2 and 10 levels."
                )));
            }
            _ => {}
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    fn signature(
        inputs: Json,
        outputs: Json
    ) -> Signature {
        serde_json::from_value(json!({ "inputs": inputs, "outputs": outputs })).unwrap()
    }

    fn decision_signature() -> Signature {
        signature(
            json!([
                {
                    "name": "context",
                    "dtype": "list",
                    "denotation": "typesafe.systemone.state"
                },
                {
                    "name": "schema",
                    "dtype": "dict",
                    "denotation": "typesafe.systemone.questions"
                }
            ]),
            json!([
                {
                    "name": "result",
                    "dtype": "dict",
                    "schema": { "title": "SystemOneResponse" }
                }
            ])
        )
    }

    fn questions(value: Json) -> IndexMap<String, Question> {
        serde_json::from_value(value).unwrap()
    }

    #[test]
    fn deserializes_wire_request_and_ignores_router_fields() {
        // Parsed from text, as a server receives it: `json!` would build a
        // key-sorted `serde_json::Map` and lose the question order.
        let params: SystemOneCreateParams = serde_json::from_str(r#"{
            "model": "@bespokelabs/nimble-9b",
            "state": { "ticket": "I was charged twice." },
            "questions": {
                "route": { "type": "choice", "instructions": "Where should this go?",
                           "criteria": { "billing": "money", "bug": null } },
                "urgency": { "type": "score", "instructions": "How urgent?",
                             "criteria": ["routine", "today", "urgent"] },
                "escalate": { "type": "noul", "instructions": "Escalate?",
                              "criteria": { "true": "Needs a human", "false": "Bot can handle it" } }
            },
            "provider": { "allow_fallbacks": true },
            "session_id": "session-1234"
        }"#).unwrap();
        assert_eq!(
            params.questions.keys().collect::<Vec<_>>(),
            ["route", "urgency", "escalate"]
        );
        assert!(validate_questions(&params.questions).is_ok());
        let round_trip = serde_json::to_value(&params.questions["escalate"]).unwrap();
        assert_eq!(round_trip["type"], "noul");
        assert_eq!(round_trip["criteria"]["true"], "Needs a human");
    }

    #[test]
    fn parses_mixed_response_in_order() {
        let output: PredictorOutput = serde_json::from_value(json!({
            "answers": {
                "route": {
                    "type": "choice",
                    "choice": "billing",
                    "confidence": 0.99,
                    "probabilities": { "billing": 0.99, "bug": 0.0, "account": 0.01 }
                },
                "urgency": {
                    "type": "score",
                    "score": 2.1,
                    "confidence": 0.7,
                    "legend": { "0": "routine", "1": "today" },
                    "probabilities": { "0": 0.4, "1": 0.6 }
                },
                "escalate": { "type": "noul", "noul": 0.94 }
            },
            "usage": { "input_tokens": 412, "output_tokens": 3 }
        })).unwrap();
        let questions: IndexMap<String, Question> = serde_json::from_str(r#"{
            "route": { "type": "choice", "instructions": "Where?", "criteria": { "billing": null } },
            "urgency": { "type": "score", "instructions": "How urgent?", "criteria": ["routine", "today"] },
            "escalate": { "type": "noul", "instructions": "Escalate?" }
        }"#).unwrap();
        let answers = order_answers(&questions, output.answers);
        assert_eq!(
            answers.keys().collect::<Vec<_>>(),
            ["route", "urgency", "escalate"]
        );
        assert!(matches!(&answers["escalate"], Answer::Noul(a) if a.noul == 0.94));
        assert!(matches!(&answers["route"], Answer::Choice(a) if a.choice == "billing"));
        assert!(matches!(&answers["urgency"], Answer::Score(a) if a.probabilities["1"] == 0.6));
        assert_eq!(output.usage.input_tokens, Some(412));
    }

    #[test]
    fn rejects_out_of_range_criteria() {
        let empty = IndexMap::new();
        assert!(matches!(validate_questions(&empty), Err(MunaError::InvalidInput(_))));
        let one_level = questions(json!({
            "q": { "type": "score", "instructions": "Rate.", "criteria": ["only"] }
        }));
        assert!(matches!(validate_questions(&one_level), Err(MunaError::InvalidInput(_))));
        let eleven_levels = questions(json!({
            "q": { "type": "score", "instructions": "Rate.", "criteria": (0..11).collect::<Vec<_>>() }
        }));
        assert!(matches!(validate_questions(&eleven_levels), Err(MunaError::InvalidInput(_))));
        let no_options = questions(json!({
            "q": { "type": "choice", "instructions": "Pick.", "criteria": {} }
        }));
        assert!(matches!(validate_questions(&no_options), Err(MunaError::InvalidInput(_))));
    }

    #[test]
    fn rejects_unknown_noul_criteria_keys() {
        let result = serde_json::from_value::<IndexMap<String, Question>>(json!({
            "q": {
                "type": "noul",
                "instructions": "Yes?",
                "criteria": { "maybe": "unsure" }
            }
        }));
        assert!(result.is_err());
    }

    #[test]
    fn binds_by_role_not_by_name() {
        let info = resolve_delegate_info("@test/decide", &decision_signature()).unwrap();
        let questions = questions(json!({
            "escalate": { "type": "noul", "instructions": "Escalate?" }
        }));
        let map = bind_inputs(json!("Charged twice."), &questions, &info).unwrap();
        assert!(matches!(
            map.get("context"),
            Some(Value::List(s)) if s == &vec![json!("Charged twice.")]
        ));
        assert!(matches!(
            map.get("schema"),
            Some(Value::Dict(q)) if q["escalate"]["type"] == "noul"
        ));
        assert_eq!(info.response_param_idx, 0);
    }

    #[test]
    fn state_becomes_a_list() {
        let info = resolve_delegate_info("@test/decide", &decision_signature()).unwrap();
        let questions = questions(json!({ "q": { "type": "noul", "instructions": "Yes?" } }));
        let state_of = |state: Json| match bind_inputs(state, &questions, &info).unwrap().remove("context") {
            Some(Value::List(items)) => items,
            _ => panic!("state is not a list"),
        };
        assert_eq!(state_of(json!({ "a": 1 })), vec![json!({ "a": 1 })]);
        assert_eq!(state_of(json!(["x", { "b": 2 }])), vec![json!("x"), json!({ "b": 2 })]);
        assert!(matches!(
            bind_inputs(json!(42), &questions, &info),
            Err(MunaError::InvalidInput(_))
        ));
    }

    #[test]
    fn rejects_predictors_without_the_contract() {
        let missing_questions = signature(
            json!([
                {
                    "name": "context",
                    "dtype": "list",
                    "denotation": "typesafe.systemone.state"
                },
                {
                    "name": "other",
                    "dtype": "dict"
                }
            ]),
            json!([
                {
                    "name": "result",
                    "dtype": "dict",
                    "schema": { "title": "SystemOneResponse" }
                }
            ])
        );
        assert!(resolve_delegate_info("@test/a", &missing_questions).is_err());
        let missing_output = signature(
            json!([
                {
                    "name": "context",
                    "dtype": "list",
                    "denotation": "typesafe.systemone.state"
                },
                {
                    "name": "schema",
                    "dtype": "dict",
                    "denotation": "typesafe.systemone.questions"
                }
            ]),
            json!([{ "name": "result", "dtype": "dict", "schema": { "title": "Other" } }])
        );
        assert!(resolve_delegate_info("@test/b", &missing_output).is_err());
    }
}