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