1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! What a launch asks of the daemon session it follows: how its sessions
//! open, which one it starts in, and the settings its flags name.
/// What a launch opens its sessions with: the one it starts in, and each
/// `/new` after it.
#[derive(Clone)]
pub(crate) struct DaemonLaunch {
pub(crate) cwd: std::path::PathBuf,
/// The agent a new session runs as; a resumed one runs as it was saved.
pub(crate) agent: Option<String>,
pub(crate) setup: rho_protocol::SessionSetup,
/// The launch's settled config, which the session it starts in shows
/// its options from until its first prompt opens it in the daemon.
pub(crate) config: std::sync::Arc<crate::config::Config>,
/// The agent the launch bound, whose model policy a session `/new`
/// opens takes over `config.toml` as it is then.
pub(crate) definition: std::sync::Arc<crate::agent::AgentDefinition>,
/// The settings the launch's flags chose, whose values `config` holds:
/// every new session takes them over `config.toml`.
pub(crate) flags: crate::app::SettingOrigins,
/// `config.toml` as the launch read it ([`crate::app::read_config_file`]).
pub(crate) config_file: std::sync::Arc<crate::config::Config>,
}
impl DaemonLaunch {
/// `config.toml` as it is now, which a new session's options are
/// built from, as the daemon will read it when the session opens, and
/// the config it was settled to for the launch's agent. `None` when it
/// does not load: the daemon refuses to open a new session on it and
/// says why with its first prompt.
pub(crate) fn config_now(
&self,
) -> Option<(crate::config::Config, std::sync::Arc<crate::config::Config>)> {
let read = crate::app::read_config_file().and_then(|file| {
let settled = crate::app::config_for_new_session(&file, &self.definition)?;
Ok((settled, std::sync::Arc::new(file)))
});
read.inspect_err(|error| {
tracing::warn!(
error = format!("{error:#}"),
"could not read config.toml for a new session"
);
})
.ok()
}
/// What a choice `change` makes now is made against: `config.toml`
/// as it is, else the launch's read of it; nothing for a change that
/// moves no setting.
pub(crate) fn made_on(
&self,
change: &rho_protocol::SessionChange,
) -> Option<std::sync::Arc<crate::config::Config>> {
crate::app::SettingOrigins::moves_a_setting(change).then(|| {
crate::app::read_config_file()
.map(std::sync::Arc::new)
.unwrap_or_else(|_| std::sync::Arc::clone(&self.config_file))
})
}
}
/// Which session the TUI follows.
pub(crate) enum DaemonTarget {
/// A new session, which the daemon opens with its first prompt.
New,
/// The saved session with this id, which the daemon reopens unless it
/// has it loaded.
Resume(rho_protocol::SessionId),
}
/// A change a launch's flags ask of the session it follows.
pub(crate) enum LaunchChange {
Change(rho_protocol::SessionChange),
/// `--auth` with neither `--provider` nor `--model`: run the session's
/// own model on `auth` when `on`, the model the flag resolved to, is of
/// the same provider, so resuming keeps the model it had; else `on`,
/// as the auth is another provider's.
Auth {
auth: String,
on: rho_protocol::ModelRef,
},
}
impl LaunchChange {
/// The change to send a session that runs with `options`.
pub(super) fn of(self, options: &rho_protocol::SessionOptions) -> rho_protocol::SessionChange {
match self {
Self::Change(change) => change,
Self::Auth { auth, on } => {
let current = &options.model.current;
rho_protocol::SessionChange::Model {
model: if current.provider == on.provider {
current.clone()
} else {
on
},
auth: Some(auth),
setup: true,
}
}
}
}
}
/// The settings a session starts to be followed with.
pub(crate) enum InitialSettings {
/// As the daemon opened it.
Kept,
/// Changes the launch asked for; one the daemon refuses fails the open,
/// unless the session has a turn under way, which they wait for.
Required(Vec<LaunchChange>),
}