rig_core/providers/minimax/
extension.rs1use 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#[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#[non_exhaustive]
38#[derive(Clone, Debug, Default, PartialEq, Serialize)]
39pub struct MiniMaxOptions {
40 #[serde(rename = "openai.chat")]
42 pub chat: MiniMaxChat,
43 #[serde(rename = "anthropic.messages")]
45 pub messages: MiniMaxMessages,
46}
47
48impl MiniMaxOptions {
49 pub fn new() -> Self {
51 Self::default()
52 }
53
54 pub fn chat(mut self, chat: MiniMaxChat) -> Self {
56 self.chat = chat;
57 self
58 }
59
60 pub fn messages(mut self, messages: MiniMaxMessages) -> Self {
62 self.messages = messages;
63 self
64 }
65
66 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 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#[non_exhaustive]
87#[derive(Clone, Debug, Default, PartialEq, Serialize)]
88pub struct MiniMaxChat {
89 #[serde(skip_serializing_if = "Option::is_none")]
92 pub reasoning_split: Option<bool>,
93}
94
95impl MiniMaxChat {
96 pub fn new() -> Self {
98 Self::default()
99 }
100
101 pub fn reasoning_split(mut self, split: bool) -> Self {
103 self.reasoning_split = Some(split);
104 self
105 }
106}
107
108#[non_exhaustive]
111#[derive(Clone, Debug, Default, PartialEq)]
112pub struct MiniMaxMessages {
113 pub metadata_user_id: Option<String>,
115}
116
117impl MiniMaxMessages {
118 pub fn new() -> Self {
120 Self::default()
121 }
122
123 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#[non_exhaustive]
143#[derive(Clone, Debug, Default, PartialEq)]
144pub struct MiniMaxExtras {
145 pub reasoning_details: Option<Vec<Value>>,
148 pub base_resp: Option<Value>,
150 pub stop_reason: Option<String>,
152 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;