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
//! 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 it unset. 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.
//!
//! TODO(after 3.10): hold the conversation model as
//! `Option<ConversationModel>` on [`Config`] instead of its three strings,
//! empty while unset, so the type rules out reading them unset. Until
//! then, [`Config::conversation_model`] is the only way to ask whether it
//! is chosen, and every way into a session checks it before anything reads
//! the strings.
use super::{AuthOrigin, Config};
/// The conversation model someone chose.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct ConversationModel<'a> {
pub(crate) provider: &'a str,
pub(crate) model: &'a str,
pub(crate) auth: &'a str,
}
/// Whether `provider` and `model` name a model: both are set. The one
/// test of it, for the conversation model and for a model a session not
/// opened yet runs on, which names none while nobody chose one.
pub(crate) fn names_a_model(provider: &str, model: &str) -> bool {
!provider.is_empty() && !model.is_empty()
}
/// 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, or `None` while nobody has chosen one.
pub(crate) fn conversation_model(&self) -> Option<ConversationModel<'_>> {
names_a_model(&self.provider, &self.model).then_some(ConversationModel {
provider: &self.provider,
model: &self.model,
auth: &self.auth,
})
}
/// The conversation model, for a session that runs on it.
pub(crate) fn require_conversation_model(
&self,
) -> Result<ConversationModel<'_>, ModelRequired> {
self.conversation_model().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.provider = provider;
self.model = model;
self.auth = auth;
self.auth_origin = origin;
self.model_alias = None;
}
/// 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 {
let mut config = Self::default();
config.set_conversation_model(
"openai".into(),
"gpt-5.5".into(),
"api-key".into(),
AuthOrigin::Inferred,
);
config
}
/// Leaves the conversation model unset, as a machine starts.
pub(crate) fn clear_conversation_model(&mut self) {
self.provider.clear();
self.model.clear();
self.auth.clear();
self.auth_origin = AuthOrigin::Inferred;
self.model_alias = None;
}
}