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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// `Configure` appliers for the model surface: custom-model registration, the
// provider credential overlay, catalog auto-fetch, the provider/model/base_url
// pair, and the thinking level (issues #123 / #136).
//
// `include!` fragment of the `turn::daemon` module — see `commands.rs`.
impl TurnHost {
/// Issue #136: register controller-provisioned custom models before the
/// model pair is resolved.
fn configure_models(&mut self, config: &WireDaemonConfig, applied: &mut WireDaemonConfig) {
if !config.models.is_empty() {
crate::model_defaults::register_models(&config.models);
applied.models = config.models.clone();
self.runtime.model_catalog = model_catalog();
}
}
/// Issue #136: seed the credential overlay from the patch. The provider
/// falls back to the provider of the session's active model.
fn configure_api_key(&mut self, config: &WireDaemonConfig, applied: &mut WireDaemonConfig) {
if let Some(raw_key) = config.api_key.as_deref() {
let provider = config
.provider
.as_deref()
.map(str::trim)
.filter(|provider| !provider.is_empty())
.map(str::to_string)
.or_else(|| {
self.session
.kernel
.harness()
.agent()
.state()
.model
.as_ref()
.map(|model| model.provider.0.clone())
});
match provider {
Some(provider) => {
let key = raw_key.trim();
let mut keys = self
.automation
.services
.configured_api_keys
.write()
.expect("configured api keys poisoned");
if key.is_empty() {
keys.remove(&provider);
drop(keys);
applied.clear_fields.push("api_key".into());
} else {
keys.insert(provider, key.to_string());
drop(keys);
applied.api_key = Some(raw_key.to_string());
}
}
None => self.error_line("configure: api_key requires a provider"),
}
}
}
/// Issue #136: `auto_fetch_models = true` fetches the provider catalog
/// and fills an unset model from the first entry. Bounded by the fetch
/// timeout; a failure is reported without failing the rest of the patch.
async fn configure_auto_fetch_models(
&mut self,
config: &mut WireDaemonConfig,
applied: &mut WireDaemonConfig,
) {
if config.auto_fetch_models == Some(true) {
let provider = config
.provider
.as_deref()
.map(str::trim)
.filter(|provider| !provider.is_empty())
.map(str::to_string)
.or_else(|| {
self.session
.kernel
.harness()
.agent()
.state()
.model
.as_ref()
.map(|model| model.provider.0.clone())
});
let base_url = config.base_url.clone().or_else(|| {
self.runtime
.config
.read()
.expect("daemon config poisoned")
.base_url
.clone()
});
match (provider, base_url) {
(Some(provider), Some(base_url)) => {
let configured_key = self
.automation
.services
.configured_api_keys
.read()
.expect("configured api keys poisoned")
.get(&provider)
.cloned();
// Same precedence as the request path: env > configured
// (`[model] api_key`) > auth.json.
let api_key = theway_transport::auth::AuthStore::load()
.unwrap_or_default()
.resolve_for_provider_with(&provider, configured_key.as_deref());
match crate::model_fetch::fetch_models(&base_url, api_key.as_deref(), &provider)
.await
{
Ok(models) if !models.is_empty() => {
let first = models[0].id.clone();
crate::model_defaults::register_models(&models);
self.runtime.model_catalog = model_catalog();
// Fill an unset model id from the catalog; an
// explicit model in the same patch wins.
if config.model.is_none() {
config.provider.get_or_insert(provider);
config.model = Some(first);
}
applied.auto_fetch_models = Some(true);
// Report the imported catalog in GetConfig so
// clients observe what was registered.
applied.models = models;
}
Ok(_) => self
.error_line("configure: auto-fetch models returned an empty catalog"),
Err(err) => {
self.error_line(format!("configure: auto-fetch models: {err}"));
}
}
}
(None, _) => {
self.error_line("configure: auto-fetch models requires a provider")
}
(_, None) => {
self.error_line("configure: auto-fetch models requires a base_url")
}
}
}
}
/// Apply the provider/model/base_url selection of the patch.
async fn configure_model_pair(
&mut self,
config: &WireDaemonConfig,
applied: &mut WireDaemonConfig,
) {
if (config.clears("provider") && config.provider.is_none())
|| (config.clears("model") && config.model.is_none())
{
self.error_line("configure: the active provider/model cannot be cleared");
} else if config.provider.is_some() != config.model.is_some() {
self.error_line("configure: provider and model must be supplied together");
} else if config.provider.is_some()
|| config.base_url.is_some()
|| config.clears("base_url")
{
let mut model = match (config.provider.as_deref(), config.model.as_deref()) {
(Some(provider), Some(id)) => theway_llm_provider::get_model(
&theway_llm_provider::Provider::from(provider),
id,
),
_ => self.session.kernel.harness().agent().state().model.clone(),
};
if config.clears("base_url")
&& let Some(current) = model.as_ref()
{
model = theway_llm_provider::get_model(¤t.provider, ¤t.id)
.or_else(|| Some(current.clone()));
}
if let Some(model) = model.as_mut()
&& let Some(base_url) = config.base_url.as_ref()
{
model.base_url = base_url.clone();
}
match model {
Some(model) if self.apply_model(model.clone()).await => {
applied.provider = Some(model.provider.0.clone());
applied.model = Some(model.id.clone());
if model.base_url.is_empty() {
applied.clear_fields.push("base_url".into());
} else {
applied.base_url = Some(model.base_url);
}
}
Some(_) => {}
None => self.error_line("configure: no active or matching model to update"),
}
}
}
/// Apply the legacy `thinking` toggle and the full `thinking_level` value.
///
/// The level is the persisted last-choice default: it applies the exact
/// level, finer-grained than the legacy toggle.
async fn configure_thinking(
&mut self,
config: &WireDaemonConfig,
applied: &mut WireDaemonConfig,
) {
if config.thinking.is_some() || config.clears("thinking") {
let enabled = config.thinking.unwrap_or(false);
let level = if enabled {
theway_core::ThinkingLevel::High
} else {
theway_core::ThinkingLevel::Off
};
match self.session.kernel.harness().set_thinking_level(level).await {
Ok(_) if config.thinking.is_none() => applied.clear_fields.push("thinking".into()),
Ok(_) => applied.thinking = Some(enabled),
Err(err) => self.error_line(format!("configure thinking: {err}")),
}
}
if config.thinking_level.is_some() || config.clears("thinking_level") {
let requested = config.thinking_level.as_deref();
let level = match requested {
Some(raw) => match raw.parse::<theway_core::ThinkingLevel>() {
Ok(level) => Some(level),
Err(err) => {
self.error_line(format!(
"configure thinking_level: invalid level {raw:?}: {err}"
));
None
}
},
// Clearing the level falls back to the runtime default (off).
None => Some(theway_core::ThinkingLevel::Off),
};
if let Some(level) = level {
match self.session.kernel.harness().set_thinking_level(level).await {
Ok(_) if requested.is_none() => {
applied.clear_fields.push("thinking_level".into())
}
Ok(_) => applied.thinking_level = config.thinking_level.clone(),
Err(err) => self.error_line(format!("configure thinking_level: {err}")),
}
}
}
}
}