session_core/
request_settings.rs1use serde_json::{Map, Value};
10
11#[derive(Clone, Debug, PartialEq)]
12pub struct RequestSettings {
13 pub thinking_budgets: Option<ai::ThinkingBudgets>,
14 pub websocket_connect_timeout_ms: Result<Option<f64>, String>,
15 pub stream: Result<Option<Value>, String>,
18 pub transport: Option<String>,
20}
21
22impl Default for RequestSettings {
23 fn default() -> Self {
24 RequestSettings {
25 thinking_budgets: None,
26 websocket_connect_timeout_ms: Ok(None),
27 stream: Ok(None),
28 transport: None,
29 }
30 }
31}
32
33impl RequestSettings {
34 pub fn from_settings(settings: &Value) -> Self {
36 let Some(settings) = settings.as_object() else {
37 return RequestSettings::default();
38 };
39 RequestSettings {
40 thinking_budgets: config::keys::thinking_budgets(settings)
41 .and_then(|budgets| serde_json::from_value(budgets.clone()).ok()),
42 websocket_connect_timeout_ms: config::keys::websocket_connect_timeout_ms(settings),
43 stream: config::keys::provider_request_options(settings).map(Some),
44 transport: settings.get("transport").and_then(Value::as_str).map(str::to_string),
45 }
46 }
47
48 pub fn from_manager(settings: &config::settings::SettingsManager) -> Self {
50 Self::from_settings(&Value::Object(settings.settings().clone()))
51 }
52
53 pub fn stream_options(&self) -> Option<Value> {
56 let mut options: Map<String, Value> = match &self.stream {
57 Ok(Some(Value::Object(options))) => options.clone(),
58 _ => return None,
59 };
60 if let Some(transport) = &self.transport {
61 options.insert("transport".into(), transport.clone().into());
62 }
63 Some(Value::Object(options))
64 }
65
66 pub fn apply(&self, options: &mut ai::SimpleStreamOptions) -> Result<(), String> {
68 if options.base.websocket_connect_timeout_ms.is_none() {
69 options.base.websocket_connect_timeout_ms = self.websocket_connect_timeout_ms.clone()?;
70 }
71 if options.thinking_budgets.is_none() {
72 options.thinking_budgets = self.thinking_budgets;
73 }
74 if let Some(stream) = self.stream.clone()? {
75 let base = &mut options.base;
76 if base.timeout_ms.is_none() {
77 base.timeout_ms = stream["timeoutMs"]
78 .as_f64()
79 .filter(|t| t.is_finite() && *t >= 0.0)
80 .map(|t| t.floor() as u64);
81 }
82 if base.max_retries.is_none() {
83 base.max_retries = stream["maxRetries"].as_u64().map(|n| n.min(u64::from(u32::MAX)) as u32);
84 }
85 if base.max_retry_delay_ms.is_none() {
86 base.max_retry_delay_ms = stream["maxRetryDelayMs"].as_f64();
87 }
88 }
89 if options.base.transport.is_none() {
90 options.base.transport = self.transport.clone();
91 }
92 Ok(())
93 }
94}