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
//! The model the CLI's `--provider`, `--model` and `--auth` flags name as
//! they apply, before it becomes the conversation model: a flag may name a
//! provider before, or without, a model it has.
use rho_providers::provider;
use crate::config::{AuthOrigin, Config, ConversationModel};
/// A provider the CLI flags named, on the auth they settled on, when they
/// named no model it has and none is set up: `rho doctor` still reports on
/// it, though no session can run on it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(in crate::app) struct ProviderWithoutModel {
pub(in crate::app) provider: String,
pub(in crate::app) auth: String,
}
/// The selection the flags build, seeded from the conversation model.
pub(super) struct CliDraft {
provider: Option<DraftProvider>,
model: Option<String>,
alias: Option<String>,
}
struct DraftProvider {
name: String,
auth: String,
auth_origin: AuthOrigin,
}
impl CliDraft {
pub(super) fn of(config: &Config) -> Self {
match &config.conversation_model {
Some(chosen) => Self {
provider: Some(DraftProvider {
name: chosen.provider.clone(),
auth: chosen.auth.clone(),
auth_origin: chosen.auth_origin,
}),
model: Some(chosen.model.clone()),
alias: chosen.alias.clone(),
},
None => Self {
provider: None,
model: None,
alias: None,
},
}
}
/// The provider named so far.
pub(super) fn provider(&self) -> Option<&str> {
self.provider.as_ref().map(|current| current.name.as_str())
}
/// The auth of the provider named so far.
pub(super) fn auth(&self) -> Option<&str> {
self.provider.as_ref().map(|current| current.auth.as_str())
}
/// Moves onto `provider` and `auth`, which the CLI picked for want of
/// `--auth` from the provider `reference` named: a retired name such as
/// `xai-oauth` chose its auth; another provider's auth was only filled
/// in, as nothing chose one for it; the same provider keeps where its
/// auth came from.
pub(super) fn take_unnamed_auth(&mut self, reference: &str, provider: &str, auth: &str) {
let auth_origin = if provider::legacy_provider_alias(reference).is_some() {
AuthOrigin::Configured
} else {
match &self.provider {
Some(current) if current.name == provider => current.auth_origin,
_ => AuthOrigin::Inferred,
}
};
self.provider = Some(DraftProvider {
name: provider.to_owned(),
auth: auth.to_owned(),
auth_origin,
});
}
/// Runs on `auth` of `provider`, which `--auth` chose; nothing while no
/// provider is named.
pub(super) fn choose_auth(&mut self, provider: &str, auth: &str) {
if let Some(current) = &mut self.provider {
current.name = provider.to_owned();
current.auth = auth.to_owned();
current.auth_origin = AuthOrigin::Configured;
}
}
/// Runs `model`, resolved from `alias` when given.
pub(super) fn set_model(&mut self, model: String, alias: Option<String>) {
self.model = Some(model);
self.alias = alias;
}
/// Runs `model`, keeping the alias it may have come from, which
/// [`Config::current_model_alias`] drops once it no longer matches.
pub(super) fn replace_model(&mut self, model: String) {
self.model = Some(model);
}
/// Makes this `config`'s conversation model: set up when it names a
/// provider and a model, else unset, with the provider it names
/// without a model, if any.
pub(super) fn commit(self, config: &mut Config) -> Option<ProviderWithoutModel> {
let (chosen, without_model) = match (self.provider, self.model) {
(Some(provider), Some(model)) => (
Some(ConversationModel {
provider: provider.name,
model,
auth: provider.auth,
auth_origin: provider.auth_origin,
alias: self.alias,
}),
None,
),
(Some(provider), None) => (
None,
Some(ProviderWithoutModel {
provider: provider.name,
auth: provider.auth,
}),
),
// A model is only ever named with its provider.
(None, _) => (None, None),
};
config.conversation_model = chosen;
without_model
}
}