Skip to main content

rig_core/providers/xiaomimimo/
extension.rs

1//! Xiaomi MiMo's typed reply extras on its OpenAI-format and Messages APIs.
2//! MiMo has no typed request option: its web search is a server tool,
3//! declared in `additional_params.tools`.
4//!
5//! ```
6//! use rig_core::completion::CompletionResponse;
7//! use rig_core::providers::xiaomimimo::extension::XiaomiMimoExt;
8//!
9//! fn annotations(reply: &CompletionResponse) -> usize {
10//!     reply
11//!         .extras_lossy::<XiaomiMimoExt>()
12//!         .annotations
13//!         .map_or(0, |annotations| annotations.len())
14//! }
15//! # let _ = annotations;
16//! ```
17
18use serde::Serialize;
19use serde_json::Value;
20
21use crate::completion::provider_options::reply_field;
22use crate::completion::{ExtensionOptions, ProviderExtension, ReplyExtras};
23use crate::message::Api;
24use crate::providers::anthropic::extension::MessagesStop;
25
26/// Xiaomi MiMo's extension marker.
27#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
28pub struct XiaomiMimoExt;
29
30impl ProviderExtension for XiaomiMimoExt {
31    const PROVIDER: &'static str = super::PROVIDER_NAME;
32    type Options = XiaomiMimoOptions;
33    type Extras = XiaomiMimoExtras;
34}
35
36/// Xiaomi MiMo's request options: none.
37#[non_exhaustive]
38#[derive(Clone, Debug, Default, PartialEq, Serialize)]
39pub struct XiaomiMimoOptions {}
40
41impl XiaomiMimoOptions {
42    /// No option set.
43    pub fn new() -> Self {
44        Self::default()
45    }
46}
47
48impl ExtensionOptions for XiaomiMimoOptions {
49    type Ext = XiaomiMimoExt;
50}
51
52/// Xiaomi MiMo's reply fields. Each is `None` when the reply lacks it.
53#[non_exhaustive]
54#[derive(Clone, Debug, Default, PartialEq)]
55pub struct XiaomiMimoExtras {
56    /// The message's annotations, such as web-search citations.
57    /// OpenAI-format API only.
58    pub annotations: Option<Vec<Value>>,
59    /// `stop_reason`, verbatim. Messages route.
60    pub stop_reason: Option<String>,
61    /// `stop_sequence`: the stop sequence the turn ended on. Messages route.
62    pub stop_sequence: Option<String>,
63}
64
65impl ReplyExtras for XiaomiMimoExtras {
66    fn from_reply(api: &Api, raw: &Value) -> Result<Self, serde_json::Error> {
67        if let Some(stop) = MessagesStop::of(api, raw)? {
68            return Ok(Self {
69                stop_reason: stop.stop_reason,
70                stop_sequence: stop.stop_sequence,
71                ..Self::default()
72            });
73        }
74        if api.as_str() != "openai.chat" {
75            return Ok(Self::default());
76        }
77        Ok(Self {
78            annotations: reply_field(raw, "/choices/0/message/annotations")?,
79            ..Self::default()
80        })
81    }
82}
83
84#[cfg(test)]
85mod tests;