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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//! Whether someone chose the conversation model.
//!
//! Nothing chooses one for a machine: a missing `config.toml`, one without
//! a `[model]` table, and a table that names only one of `provider` and
//! `model` all leave [`Config::conversation_model`] `None`. Until someone
//! sets it, through the TUI's setup, a client's setup screen, or the file,
//! every session that would run on it fails to open with
//! [`ModelRequired`]; one whose request or agent names its own model still
//! opens.
use super::{AuthOrigin, Config};
/// The conversation model someone chose.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ConversationModel {
/// The provider. Empty only while loading, or applying a wire change,
/// holds an `@alias` model that names it, until
/// `Config::resolve_model_alias` resolves the alias.
pub provider: String,
/// A concrete model id: load resolves an `@alias` to its target, the
/// only raw value it holds and only in the same window as `provider`.
pub model: String,
/// The auth profile. A change that decides whether someone chose it
/// goes through [`Self::choose_auth`] or [`Self::infer_auth`], which
/// set [`Self::auth_origin`] with it.
pub auth: String,
/// Whether someone chose [`Self::auth`] or it was filled in, which
/// decides whether a session starts on it as it is.
pub(crate) auth_origin: AuthOrigin,
/// The alias `provider`/`model` was resolved from, if any. Consult it
/// through [`Config::current_model_alias`], which drops it once the
/// selection no longer matches the alias table.
pub(crate) alias: Option<String>,
}
impl ConversationModel {
/// `provider`'s `model` on `auth`, whose origin is `auth_origin`, named
/// directly rather than through an alias.
pub(crate) fn new(
provider: impl Into<String>,
model: impl Into<String>,
auth: impl Into<String>,
auth_origin: AuthOrigin,
) -> Self {
Self {
provider: provider.into(),
model: model.into(),
auth: auth.into(),
auth_origin,
alias: None,
}
}
}
/// A session needed the conversation model, and nobody has chosen one.
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
#[error(
"no model is set up: run `rho` and choose one, pick one in rho-ui, or set `[model] provider` and `model` in config.toml"
)]
pub(crate) struct ModelRequired;
impl Config {
/// The conversation model, for a session that runs on it.
pub(crate) fn require_conversation_model(&self) -> Result<&ConversationModel, ModelRequired> {
self.conversation_model.as_ref().ok_or(ModelRequired)
}
/// Chooses `provider`'s `model` on `auth`, whose origin is `origin`, as
/// the conversation model, dropping the alias it may have come from: a
/// person's choice, or, on a session's own config, the model it opens
/// on in place of the conversation model.
pub(crate) fn set_conversation_model(
&mut self,
provider: String,
model: String,
auth: String,
origin: AuthOrigin,
) {
self.conversation_model = Some(ConversationModel::new(provider, model, auth, origin));
}
/// Runs on `provider`'s `model` on `auth`, keeping whether the auth was
/// chosen and the alias the model came from, which
/// [`Config::current_model_alias`] drops once it no longer matches: a
/// session's own config moved onto the model it runs on.
pub(crate) fn run_conversation_on(
&mut self,
provider: String,
model: String,
auth: String,
) -> &mut ConversationModel {
let (auth_origin, alias) = self
.conversation_model
.take()
.map_or((AuthOrigin::Inferred, None), |chosen| {
(chosen.auth_origin, chosen.alias)
});
self.conversation_model.insert(ConversationModel {
provider,
model,
auth,
auth_origin,
alias,
})
}
/// The conversation model, for a test that set one up.
#[cfg(test)]
pub(crate) fn expect_conversation_model(&self) -> &ConversationModel {
self.conversation_model
.as_ref()
.expect("the test sets up a conversation model")
}
/// The default config with a conversation model chosen, for tests whose
/// sessions run on one but do not care which: OpenAI's `gpt-5.5` on an
/// API key.
#[cfg(test)]
pub(crate) fn with_test_model() -> Self {
Self::with_model("openai", "gpt-5.5", "api-key")
}
/// The default config with `provider`'s `model` on `auth`, filled in
/// rather than chosen, as the conversation model.
#[cfg(test)]
pub(crate) fn with_model(
provider: impl Into<String>,
model: impl Into<String>,
auth: impl Into<String>,
) -> Self {
Self {
conversation_model: Some(ConversationModel::new(
provider,
model,
auth,
AuthOrigin::Inferred,
)),
..Self::default()
}
}
}