use serde_json::Value;
use crate::driver_registry::LlmCallConfig;
pub const OPENAI_WEBSOCKET_OPTION: &str = "openai/websocket";
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) struct WebSocketPolicy {
pub(crate) supported: bool,
pub(crate) default_on: bool,
}
impl WebSocketPolicy {
#[cfg_attr(not(feature = "responses-websocket"), allow(dead_code))]
pub(crate) fn wants(&self, config: &LlmCallConfig, background: bool) -> bool {
if !cfg!(feature = "responses-websocket") || !self.supported || background {
return false;
}
config
.driver_options
.get(OPENAI_WEBSOCKET_OPTION)
.and_then(Value::as_bool)
.unwrap_or(self.default_on)
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn config(option: Option<bool>) -> LlmCallConfig {
let mut config = LlmCallConfig::default();
if let Some(value) = option {
config
.driver_options
.insert(OPENAI_WEBSOCKET_OPTION.into(), json!(value));
}
config
}
#[test]
fn websocket_is_opt_in_and_never_used_without_support_or_in_background() {
let off = WebSocketPolicy::default();
let supported = WebSocketPolicy {
supported: true,
default_on: false,
};
let default_on = WebSocketPolicy {
supported: true,
default_on: true,
};
let compiled = cfg!(feature = "responses-websocket");
assert!(!off.wants(&config(Some(true)), false));
assert!(!supported.wants(&config(None), false));
assert_eq!(supported.wants(&config(Some(true)), false), compiled);
assert_eq!(default_on.wants(&config(None), false), compiled);
assert!(!default_on.wants(&config(Some(false)), false));
assert!(!default_on.wants(&config(Some(true)), true));
}
}