use rho_protocol::{
methods::SessionConfigure, ClientError, SessionChange, SessionConfigureParams, SessionId,
SessionOptions,
};
use crate::app::daemon_client::Link;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum ModelChange {
Setup,
Switch,
}
pub(super) fn changes_between(
from: &SessionOptions,
to: &SessionOptions,
model: ModelChange,
) -> Vec<SessionChange> {
let mut changes = Vec::new();
if from.model.current != to.model.current || from.model.auth != to.model.auth {
changes.push(SessionChange::Model {
model: to.model.current.clone(),
auth: Some(to.model.auth.clone()),
setup: model == ModelChange::Setup,
});
}
if let Some(reasoning) = to.reasoning.as_ref().filter(|reasoning| {
from.reasoning.as_ref().map(|from| from.current) != Some(reasoning.current)
}) {
changes.push(SessionChange::Reasoning {
level: reasoning.current,
});
}
let fast =
|options: &SessionOptions| options.fast_mode.as_ref().is_some_and(|fast| fast.enabled);
if fast(from) != fast(to) {
changes.push(SessionChange::FastMode { enabled: fast(to) });
}
if from.permission_mode.current != to.permission_mode.current {
changes.push(SessionChange::PermissionMode {
mode: to.permission_mode.current,
});
}
changes
}
pub(super) struct Refused {
pub(super) change: SessionChange,
pub(super) error: ClientError,
}
pub(super) async fn configure_all(
link: &Link,
session: &SessionId,
changes: Vec<SessionChange>,
) -> Result<Option<SessionOptions>, Box<Refused>> {
let mut options = None;
for change in changes {
let params = SessionConfigureParams {
session: session.clone(),
change,
};
match link.request::<SessionConfigure>(¶ms).await {
Ok(response) => options = Some(response.options),
Err(error) => {
return Err(Box::new(Refused {
change: params.change,
error,
}))
}
}
}
Ok(options)
}
pub(super) async fn carry(
link: &Link,
session: &SessionId,
changes: Vec<SessionChange>,
) -> Option<SessionOptions> {
let mut options = None;
for change in changes {
match configure_all(link, session, vec![change]).await {
Ok(changed) => options = changed.or(options),
Err(refused) => {
tracing::warn!(error = %refused.error, "could not carry a setting to the new daemon session");
}
}
}
options
}