Skip to main content

rig_core/providers/moonshot/
extension.rs

1//! Moonshot's typed request options and reply extras. One entry serves its
2//! OpenAI-format API (<https://platform.kimi.ai/docs/api/chat>) and its
3//! Anthropic-format Messages API, which takes no Moonshot field of its own.
4//!
5//! ```
6//! use rig_core::completion::CompletionRequest;
7//! use rig_core::providers::moonshot::extension::MoonshotOptions;
8//!
9//! let options = MoonshotOptions::new().prompt_cache_key("session-7");
10//! let request = CompletionRequest::new("hi").provider_option(options);
11//! # let _ = request;
12//! ```
13
14use 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/// Moonshot's extension marker.
23#[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/// Moonshot's request options, by route. The field setters here write the
33/// same field as [`chat`](Self::chat), which replaces every field set
34/// before it; the Messages API takes none of them.
35#[non_exhaustive]
36#[derive(Clone, Debug, Default, PartialEq, Serialize)]
37pub struct MoonshotOptions {
38    /// Sent on the OpenAI-format API only.
39    #[serde(rename = "openai.chat")]
40    pub chat: MoonshotChat,
41}
42
43impl MoonshotOptions {
44    /// No option set.
45    pub fn new() -> Self {
46        Self::default()
47    }
48
49    /// Send `chat` on the OpenAI-format API.
50    pub fn chat(mut self, chat: MoonshotChat) -> Self {
51        self.chat = chat;
52        self
53    }
54
55    /// Keep `keep` earlier turns' thinking in context, on the OpenAI-format
56    /// API, as [`MoonshotChat::thinking_keep`].
57    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    /// Route the prompt cache by `key`, on the OpenAI-format API, as
63    /// [`MoonshotChat::prompt_cache_key`].
64    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/// The fields Moonshot's OpenAI-format API takes.
75#[non_exhaustive]
76#[derive(Clone, Debug, Default, PartialEq, Serialize)]
77pub struct MoonshotChat {
78    /// Thinking fields sent beside the mapped thinking type.
79    #[serde(skip_serializing_if = "MoonshotThinking::is_empty")]
80    pub thinking: MoonshotThinking,
81    /// Routes requests that share a prefix to the same cache.
82    #[serde(skip_serializing_if = "Option::is_none")]
83    pub prompt_cache_key: Option<String>,
84}
85
86/// The `thinking` fields Moonshot takes beside the type the generation
87/// options map.
88#[non_exhaustive]
89#[derive(Clone, Debug, Default, PartialEq, Serialize)]
90pub struct MoonshotThinking {
91    /// Which earlier turns' thinking the model keeps in context.
92    #[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/// Which earlier turns' thinking the model keeps in context.
103#[non_exhaustive]
104#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
105#[serde(rename_all = "lowercase")]
106pub enum ThinkingKeep {
107    /// Every turn's.
108    All,
109}
110
111impl MoonshotChat {
112    /// No option set.
113    pub fn new() -> Self {
114        Self::default()
115    }
116
117    /// Keep `keep` earlier turns' thinking in context.
118    pub fn thinking_keep(mut self, keep: ThinkingKeep) -> Self {
119        self.thinking.keep = Some(keep);
120        self
121    }
122
123    /// Route the prompt cache by `key`.
124    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/// Moonshot's reply fields. Each is `None` when the reply lacks it.
131#[non_exhaustive]
132#[derive(Clone, Debug, Default, PartialEq)]
133pub struct MoonshotExtras {
134    /// The first choice's own usage. OpenAI-format API only.
135    pub choice_usage: Option<Value>,
136    /// Prompt token details. OpenAI-format API only.
137    pub prompt_tokens_details: Option<Value>,
138    /// `stop_reason`, verbatim. Messages route.
139    pub stop_reason: Option<String>,
140    /// `stop_sequence`: the stop sequence the turn ended on. Messages route.
141    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;