Skip to main content

session_core/
request_settings.rs

1//! Provider request options the settings fill (sdk.ts `buildRequestOptions`, session-worker.ts
2//! `providerRequestOptions`, worker-settings.ts `transport`, and the Agent's `thinkingBudgets`): `thinkingBudgets`
3//! (the token budget each reasoning level asks for), `websocketConnectTimeoutMs` (the WebSocket transport's connect
4//! timeout), the stream options of every request (`timeoutMs`: `retry.provider.timeoutMs`, else the HTTP idle
5//! timeout; `retry.provider.maxRetries` and `maxRetryDelayMs`) and the `transport` setting. A request's own value
6//! wins (`options.x ?? settings.x`). An invalid timeout fails the request with `Invalid <setting> setting: <value>`,
7//! as TypeScript's `parseTimeoutSetting` throws while building it.
8
9use 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    /// `providerRequestOptions` (`{timeoutMs, maxRetries?, maxRetryDelayMs}`; none: no stream defaults), or why the
16    /// HTTP idle timeout is invalid.
17    pub stream: Result<Option<Value>, String>,
18    /// The `transport` setting when one is configured (`sse`, `websocket`, `websocket-cached`, `auto`).
19    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    /// From merged settings JSON.
35    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    /// From a settings manager (the worker's, read now).
49    pub fn from_manager(settings: &config::settings::SettingsManager) -> Self {
50        Self::from_settings(&Value::Object(settings.settings().clone()))
51    }
52
53    /// The stream options as the session's harness keeps them (an operation's `streamOptions`): `None` when the
54    /// settings fill none or are invalid.
55    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    /// Fill what `options` leaves unset; `Err` when a timeout setting is invalid.
67    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}