rig_core/providers/moonshot/
extension.rs1use serde::Serialize;
15use serde_json::Value;
16
17use crate::completion::provider_options::reply_field;
18use crate::completion::{ExtensionOptions, ProviderExtension, ReplyExtras};
19use crate::message::Api;
20use crate::providers::anthropic::extension::MessagesStop;
21
22#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
24pub struct MoonshotExt;
25
26impl ProviderExtension for MoonshotExt {
27 const PROVIDER: &'static str = super::PROVIDER_NAME;
28 type Options = MoonshotOptions;
29 type Extras = MoonshotExtras;
30}
31
32#[non_exhaustive]
36#[derive(Clone, Debug, Default, PartialEq, Serialize)]
37pub struct MoonshotOptions {
38 #[serde(rename = "openai.chat")]
40 pub chat: MoonshotChat,
41}
42
43impl MoonshotOptions {
44 pub fn new() -> Self {
46 Self::default()
47 }
48
49 pub fn chat(mut self, chat: MoonshotChat) -> Self {
51 self.chat = chat;
52 self
53 }
54
55 pub fn thinking_keep(mut self, keep: ThinkingKeep) -> Self {
58 self.chat = std::mem::take(&mut self.chat).thinking_keep(keep);
59 self
60 }
61
62 pub fn prompt_cache_key(mut self, key: impl Into<String>) -> Self {
65 self.chat = std::mem::take(&mut self.chat).prompt_cache_key(key);
66 self
67 }
68}
69
70impl ExtensionOptions for MoonshotOptions {
71 type Ext = MoonshotExt;
72}
73
74#[non_exhaustive]
76#[derive(Clone, Debug, Default, PartialEq, Serialize)]
77pub struct MoonshotChat {
78 #[serde(skip_serializing_if = "MoonshotThinking::is_empty")]
80 pub thinking: MoonshotThinking,
81 #[serde(skip_serializing_if = "Option::is_none")]
83 pub prompt_cache_key: Option<String>,
84}
85
86#[non_exhaustive]
89#[derive(Clone, Debug, Default, PartialEq, Serialize)]
90pub struct MoonshotThinking {
91 #[serde(skip_serializing_if = "Option::is_none")]
93 pub keep: Option<ThinkingKeep>,
94}
95
96impl MoonshotThinking {
97 fn is_empty(&self) -> bool {
98 self.keep.is_none()
99 }
100}
101
102#[non_exhaustive]
104#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
105#[serde(rename_all = "lowercase")]
106pub enum ThinkingKeep {
107 All,
109}
110
111impl MoonshotChat {
112 pub fn new() -> Self {
114 Self::default()
115 }
116
117 pub fn thinking_keep(mut self, keep: ThinkingKeep) -> Self {
119 self.thinking.keep = Some(keep);
120 self
121 }
122
123 pub fn prompt_cache_key(mut self, key: impl Into<String>) -> Self {
125 self.prompt_cache_key = Some(key.into());
126 self
127 }
128}
129
130#[non_exhaustive]
132#[derive(Clone, Debug, Default, PartialEq)]
133pub struct MoonshotExtras {
134 pub choice_usage: Option<Value>,
136 pub prompt_tokens_details: Option<Value>,
138 pub stop_reason: Option<String>,
140 pub stop_sequence: Option<String>,
142}
143
144impl ReplyExtras for MoonshotExtras {
145 fn from_reply(api: &Api, raw: &Value) -> Result<Self, serde_json::Error> {
146 if let Some(stop) = MessagesStop::of(api, raw)? {
147 return Ok(Self {
148 stop_reason: stop.stop_reason,
149 stop_sequence: stop.stop_sequence,
150 ..Self::default()
151 });
152 }
153 if api.as_str() != "openai.chat" {
154 return Ok(Self::default());
155 }
156 Ok(Self {
157 choice_usage: reply_field(raw, "/choices/0/usage")?,
158 prompt_tokens_details: reply_field(raw, "/usage/prompt_tokens_details")?,
159 ..Self::default()
160 })
161 }
162}
163
164#[cfg(test)]
165mod tests;