1use rig_core::completion::Document;
10use rig_core::message::{Message, ToolChoice};
11use serde::{Deserialize, Serialize};
12
13#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
28pub struct RequestPatch {
29 pub preamble: Option<String>,
31 pub temperature: Option<f64>,
33 pub max_tokens: Option<u64>,
35 pub tool_choice: Option<ToolChoice>,
37 pub active_tools: Option<Vec<String>>,
39 pub additional_params: Option<serde_json::Value>,
41 pub extra_context: Vec<Document>,
43 pub history: Option<Vec<Message>>,
45}
46
47fn merge_last_wins<T>(earlier: Option<T>, later: Option<T>, field: &str) -> Option<T> {
48 match (earlier, later) {
49 (Some(_), Some(later)) => {
50 tracing::warn!(
51 patch_field = field,
52 "two hooks set the same request field; later wins"
53 );
54 Some(later)
55 }
56 (earlier, later) => later.or(earlier),
57 }
58}
59
60impl RequestPatch {
61 pub fn new() -> Self {
63 Self::default()
64 }
65
66 pub fn preamble(mut self, value: impl Into<String>) -> Self {
68 self.preamble = Some(value.into());
69 self
70 }
71
72 pub fn temperature(mut self, value: f64) -> Self {
74 self.temperature = Some(value);
75 self
76 }
77
78 pub fn max_tokens(mut self, value: u64) -> Self {
80 self.max_tokens = Some(value);
81 self
82 }
83
84 pub fn tool_choice(mut self, value: ToolChoice) -> Self {
86 self.tool_choice = Some(value);
87 self
88 }
89
90 pub fn active_tools<I, S>(mut self, values: I) -> Self
92 where
93 I: IntoIterator<Item = S>,
94 S: Into<String>,
95 {
96 self.active_tools = Some(values.into_iter().map(Into::into).collect());
97 self
98 }
99
100 pub fn additional_params(mut self, value: serde_json::Value) -> Self {
105 self.additional_params = Some(value);
106 self
107 }
108
109 pub fn extra_context<I>(mut self, values: I) -> Self
111 where
112 I: IntoIterator<Item = Document>,
113 {
114 self.extra_context.extend(values);
115 self
116 }
117
118 pub fn context(mut self, value: Document) -> Self {
120 self.extra_context.push(value);
121 self
122 }
123
124 pub fn history<I>(mut self, values: I) -> Self
126 where
127 I: IntoIterator<Item = Message>,
128 {
129 self.history = Some(values.into_iter().collect());
130 self
131 }
132
133 pub fn is_empty(&self) -> bool {
135 self.preamble.is_none()
136 && self.temperature.is_none()
137 && self.max_tokens.is_none()
138 && self.tool_choice.is_none()
139 && self.active_tools.is_none()
140 && self.additional_params.is_none()
141 && self.extra_context.is_empty()
142 && self.history.is_none()
143 }
144
145 pub fn merge(mut self, later: Self) -> Self {
150 self.extra_context.extend(later.extra_context);
151 self.additional_params = match (self.additional_params.take(), later.additional_params) {
152 (Some(base), Some(patch)) if base.is_object() && patch.is_object() => {
153 Some(rig_core::json_utils::merge(base, patch))
154 }
155 (base, patch) => patch.or(base),
156 };
157 self.preamble = merge_last_wins(self.preamble, later.preamble, "preamble");
158 self.temperature = merge_last_wins(self.temperature, later.temperature, "temperature");
159 self.max_tokens = merge_last_wins(self.max_tokens, later.max_tokens, "max_tokens");
160 self.tool_choice = merge_last_wins(self.tool_choice, later.tool_choice, "tool_choice");
161 self.history = merge_last_wins(self.history, later.history, "history");
162 self.active_tools = match (self.active_tools.take(), later.active_tools) {
163 (Some(earlier), Some(later)) => {
164 let later: std::collections::BTreeSet<_> = later.iter().collect();
165 Some(
166 earlier
167 .into_iter()
168 .filter(|name| later.contains(name))
169 .collect(),
170 )
171 }
172 (earlier, later) => earlier.or(later),
173 };
174 self
175 }
176}
177
178#[cfg(test)]
179mod tests;