Skip to main content

rig_core/providers/minimax/
extension.rs

1//! MiniMax's typed request options and reply extras. One entry serves its
2//! OpenAI-format API
3//! (<https://platform.minimax.io/docs/api-reference/text-openai-api>) and its
4//! Anthropic-format Messages API; each section goes only to its route.
5//!
6//! ```
7//! use rig_core::completion::CompletionRequest;
8//! use rig_core::providers::minimax::extension::MiniMaxOptions;
9//!
10//! let options = MiniMaxOptions::new().reasoning_split(true);
11//! let request = CompletionRequest::new("hi").provider_option(options);
12//! # let _ = request;
13//! ```
14
15use serde::ser::SerializeMap;
16use serde::{Serialize, Serializer};
17use serde_json::Value;
18
19use crate::completion::provider_options::reply_field;
20use crate::completion::{ExtensionOptions, ProviderExtension, ReplyExtras};
21use crate::message::Api;
22use crate::providers::anthropic::extension::{MessagesStop, Nested};
23
24/// MiniMax's extension marker.
25#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
26pub struct MiniMaxExt;
27
28impl ProviderExtension for MiniMaxExt {
29    const PROVIDER: &'static str = super::PROVIDER_NAME;
30    type Options = MiniMaxOptions;
31    type Extras = MiniMaxExtras;
32}
33
34/// MiniMax's request options, by route. The field setters here write the
35/// same field as the section setters, which replace every field of their
36/// section set before them, and each names the one route that sends it.
37#[non_exhaustive]
38#[derive(Clone, Debug, Default, PartialEq, Serialize)]
39pub struct MiniMaxOptions {
40    /// Sent on the OpenAI-format API only.
41    #[serde(rename = "openai.chat")]
42    pub chat: MiniMaxChat,
43    /// Sent on the Anthropic-format Messages API only.
44    #[serde(rename = "anthropic.messages")]
45    pub messages: MiniMaxMessages,
46}
47
48impl MiniMaxOptions {
49    /// No option set.
50    pub fn new() -> Self {
51        Self::default()
52    }
53
54    /// Send `chat` on the OpenAI-format API.
55    pub fn chat(mut self, chat: MiniMaxChat) -> Self {
56        self.chat = chat;
57        self
58    }
59
60    /// Send `messages` on the Messages API.
61    pub fn messages(mut self, messages: MiniMaxMessages) -> Self {
62        self.messages = messages;
63        self
64    }
65
66    /// Return the reasoning apart from the content when `split`, on the
67    /// OpenAI-format API only, as [`MiniMaxChat::reasoning_split`].
68    pub fn reasoning_split(mut self, split: bool) -> Self {
69        self.chat = std::mem::take(&mut self.chat).reasoning_split(split);
70        self
71    }
72
73    /// Send `user_id` as `metadata.user_id`, on the Messages API only, as
74    /// [`MiniMaxMessages::metadata_user_id`].
75    pub fn metadata_user_id(mut self, user_id: impl Into<String>) -> Self {
76        self.messages = std::mem::take(&mut self.messages).metadata_user_id(user_id);
77        self
78    }
79}
80
81impl ExtensionOptions for MiniMaxOptions {
82    type Ext = MiniMaxExt;
83}
84
85/// The fields MiniMax's OpenAI-format API takes.
86#[non_exhaustive]
87#[derive(Clone, Debug, Default, PartialEq, Serialize)]
88pub struct MiniMaxChat {
89    /// Whether the reasoning comes back in `reasoning_details` rather than
90    /// in `<think>` tags in the content.
91    #[serde(skip_serializing_if = "Option::is_none")]
92    pub reasoning_split: Option<bool>,
93}
94
95impl MiniMaxChat {
96    /// No option set.
97    pub fn new() -> Self {
98        Self::default()
99    }
100
101    /// Return the reasoning apart from the content when `split`.
102    pub fn reasoning_split(mut self, split: bool) -> Self {
103        self.reasoning_split = Some(split);
104        self
105    }
106}
107
108/// The fields MiniMax's Messages API takes. Each unset field is left out
109/// of the body.
110#[non_exhaustive]
111#[derive(Clone, Debug, Default, PartialEq)]
112pub struct MiniMaxMessages {
113    /// `metadata.user_id`: an opaque id of the end user.
114    pub metadata_user_id: Option<String>,
115}
116
117impl MiniMaxMessages {
118    /// No option set.
119    pub fn new() -> Self {
120        Self::default()
121    }
122
123    /// Send `user_id` as `metadata.user_id`.
124    pub fn metadata_user_id(mut self, user_id: impl Into<String>) -> Self {
125        self.metadata_user_id = Some(user_id.into());
126        self
127    }
128}
129
130impl Serialize for MiniMaxMessages {
131    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
132        let Self { metadata_user_id } = self;
133        let mut map = serializer.serialize_map(None)?;
134        if let Some(user_id) = metadata_user_id {
135            map.serialize_entry("metadata", &Nested("user_id", user_id))?;
136        }
137        map.end()
138    }
139}
140
141/// MiniMax's reply fields. Each is `None` when the reply lacks it.
142#[non_exhaustive]
143#[derive(Clone, Debug, Default, PartialEq)]
144pub struct MiniMaxExtras {
145    /// The first choice's reasoning, split from the content. OpenAI-format
146    /// API only.
147    pub reasoning_details: Option<Vec<Value>>,
148    /// MiniMax's status envelope. OpenAI-format API only.
149    pub base_resp: Option<Value>,
150    /// `stop_reason`, verbatim. Messages route.
151    pub stop_reason: Option<String>,
152    /// `stop_sequence`: the stop sequence the turn ended on. Messages route.
153    pub stop_sequence: Option<String>,
154}
155
156impl ReplyExtras for MiniMaxExtras {
157    fn from_reply(api: &Api, raw: &Value) -> Result<Self, serde_json::Error> {
158        if let Some(stop) = MessagesStop::of(api, raw)? {
159            return Ok(Self {
160                stop_reason: stop.stop_reason,
161                stop_sequence: stop.stop_sequence,
162                ..Self::default()
163            });
164        }
165        if api.as_str() != "openai.chat" {
166            return Ok(Self::default());
167        }
168        Ok(Self {
169            reasoning_details: reply_field(raw, "/choices/0/message/reasoning_details")?,
170            base_resp: reply_field(raw, "/base_resp")?,
171            ..Self::default()
172        })
173    }
174}
175
176#[cfg(test)]
177mod tests;