Repository navigation
Expand file tree
/
Copy pathcontrols.rs
More file actions
338 lines (316 loc) · 11.4 KB
/
Copy pathcontrols.rs
File metadata and controls
338 lines (316 loc) · 11.4 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//! Config and account actions for the tabbed terminal UI.
use std::sync::Arc;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use serde_json::json;
use zeroize::Zeroize;
use crate::core::runtime::CoreRuntime;
use super::ui_state::{ConfigKey, SettingsAction, UiState};
pub async fn handle_config_key(key: KeyEvent, runtime: &Arc<CoreRuntime>, ui: &mut UiState) {
if let Some(input) = ui.config_edit.as_mut() {
match key.code {
KeyCode::Esc => ui.config_edit = None,
KeyCode::Backspace => {
input.pop();
}
KeyCode::Enter => save_config(runtime, ui).await,
KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => input.push(c),
_ => {}
}
return;
}
match key.code {
KeyCode::Up => ui.config_selected = ui.config_selected.saturating_sub(1),
KeyCode::Down => {
ui.config_selected = (ui.config_selected + 1).min(ui.config_items.len() - 1)
}
KeyCode::Enter => {
ui.config_edit = Some(ui.config_items[ui.config_selected].value.clone());
}
_ => {}
}
}
async fn save_config(runtime: &Arc<CoreRuntime>, ui: &mut UiState) {
let Some(value) = ui.config_edit.take() else {
return;
};
let key = ui.config_items[ui.config_selected].key;
let (method, params) = config_update(key, value);
ui.config_status = "Saving…".to_string();
match runtime.invoke(method, params).await {
Ok(_) => {
ui.config_status = "Saved.".to_string();
refresh_config(runtime, ui).await;
}
Err(err) => ui.config_status = format!("Save failed: {err}"),
}
}
fn config_update(key: ConfigKey, value: String) -> (&'static str, serde_json::Value) {
match key {
ConfigKey::ApiUrl => (
"openhuman.config_update_model_settings",
json!({"api_url": value}),
),
ConfigKey::InferenceUrl => (
"openhuman.config_update_model_settings",
json!({"inference_url": value}),
),
ConfigKey::DefaultModel => (
"openhuman.config_update_model_settings",
json!({"default_model": value}),
),
ConfigKey::AutonomyLevel => (
"openhuman.config_update_autonomy_settings",
json!({"level": value}),
),
ConfigKey::PrivacyMode => ("openhuman.config_set_privacy_mode", json!({"mode": value})),
}
}
pub async fn refresh_config(runtime: &Arc<CoreRuntime>, ui: &mut UiState) {
let client = runtime
.invoke("openhuman.config_get_client_config", json!({}))
.await;
let autonomy = runtime
.invoke("openhuman.config_get_autonomy_settings", json!({}))
.await;
let privacy = runtime
.invoke("openhuman.config_get_privacy_mode", json!({}))
.await;
match (client, autonomy, privacy) {
(Ok(client), Ok(autonomy), Ok(privacy)) => {
let client = rpc_payload(&client);
let autonomy = rpc_payload(&autonomy);
let privacy = rpc_payload(&privacy);
for item in &mut ui.config_items {
item.value = match item.key {
ConfigKey::ApiUrl => string_at(client, &["api_url"]),
ConfigKey::InferenceUrl => string_at(client, &["inference_url"]),
ConfigKey::DefaultModel => string_at(client, &["default_model"]),
ConfigKey::AutonomyLevel => string_at(autonomy, &["level"]),
ConfigKey::PrivacyMode => string_at(privacy, &["mode"]),
};
}
ui.config_status = "Select a field and press Enter to edit.".to_string();
}
_ => ui.config_status = "Could not load one or more config sections; see Logs.".to_string(),
}
}
pub async fn handle_settings_key(key: KeyEvent, runtime: &Arc<CoreRuntime>, ui: &mut UiState) {
if let Some(token) = ui.login_token.as_mut() {
match key.code {
KeyCode::Esc => {
token.zeroize();
ui.login_token = None;
}
KeyCode::Backspace => {
token.pop();
}
KeyCode::Enter => login_with_token(runtime, ui).await,
KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => token.push(c),
_ => {}
}
return;
}
if ui.logout_confirm {
match key.code {
KeyCode::Esc | KeyCode::Char('n') => ui.logout_confirm = false,
KeyCode::Char('y') | KeyCode::Enter => logout(runtime, ui).await,
_ => {}
}
return;
}
match key.code {
KeyCode::Up => ui.settings_selected = ui.settings_selected.saturating_sub(1),
KeyCode::Down => {
ui.settings_selected = (ui.settings_selected + 1).min(SettingsAction::ALL.len() - 1)
}
KeyCode::Enter => match SettingsAction::ALL[ui.settings_selected] {
SettingsAction::ViewAccount => view_account(runtime, ui).await,
SettingsAction::Login => ui.login_token = Some(String::new()),
SettingsAction::Logout => ui.logout_confirm = true,
},
_ => {}
}
}
pub async fn refresh_auth(runtime: &Arc<CoreRuntime>, ui: &mut UiState) {
match runtime.invoke("openhuman.auth_get_state", json!({})).await {
Ok(value) => {
let state = rpc_payload(&value);
if state
.get("isAuthenticated")
.and_then(serde_json::Value::as_bool)
.unwrap_or(false)
{
let identity = string_at(state, &["userId"]);
ui.auth_summary = if identity.is_empty() {
"Signed in".to_string()
} else {
format!("Signed in · {identity}")
};
} else {
ui.auth_summary = "Signed out".to_string();
ui.account_detail.clear();
}
}
Err(err) => ui.auth_summary = format!("Account status unavailable: {err}"),
}
}
async fn view_account(runtime: &Arc<CoreRuntime>, ui: &mut UiState) {
ui.settings_status = "Refreshing account…".to_string();
match runtime.invoke("openhuman.auth_get_me", json!({})).await {
Ok(value) => {
let user = rpc_payload(&value);
ui.account_detail = account_detail(user);
ui.settings_status = "Account refreshed.".to_string();
refresh_auth(runtime, ui).await;
}
Err(err) => ui.settings_status = format!("Account refresh failed: {err}"),
}
}
async fn login_with_token(runtime: &Arc<CoreRuntime>, ui: &mut UiState) {
let mut token = ui.login_token.take().unwrap_or_default();
if token.trim().is_empty() {
ui.settings_status = "Login token cannot be empty.".to_string();
token.zeroize();
return;
}
ui.settings_status = "Signing in…".to_string();
let consumed = runtime
.invoke(
"openhuman.auth_consume_login_token",
json!({"loginToken": token.trim()}),
)
.await;
token.zeroize();
let result = match consumed {
Ok(value) => value,
Err(err) => {
ui.settings_status = format!("Login failed: {err}");
return;
}
};
let mut jwt = string_at(rpc_payload(&result), &["jwtToken"]);
if jwt.is_empty() {
ui.settings_status = "Login failed: backend returned no session token.".to_string();
return;
}
let stored = runtime
.invoke("openhuman.auth_store_session", json!({"token": jwt}))
.await;
jwt.zeroize();
match stored {
Ok(_) => {
ui.settings_status = "Signed in.".to_string();
refresh_auth(runtime, ui).await;
ui.identity_changed = true;
}
Err(err) => ui.settings_status = format!("Could not store session: {err}"),
}
}
async fn logout(runtime: &Arc<CoreRuntime>, ui: &mut UiState) {
ui.logout_confirm = false;
match runtime
.invoke("openhuman.auth_clear_session", json!({}))
.await
{
Ok(_) => {
ui.settings_status = "Signed out.".to_string();
refresh_auth(runtime, ui).await;
ui.identity_changed = true;
}
Err(err) => ui.settings_status = format!("Logout failed: {err}"),
}
}
fn rpc_payload(value: &serde_json::Value) -> &serde_json::Value {
value
.get("result")
.or_else(|| value.get("data"))
.map(rpc_payload)
.unwrap_or(value)
}
fn string_at(value: &serde_json::Value, path: &[&str]) -> String {
path.iter()
.try_fold(value, |current, key| current.get(*key))
.and_then(serde_json::Value::as_str)
.unwrap_or_default()
.to_string()
}
fn account_detail(user: &serde_json::Value) -> String {
let name = [
string_at(user, &["firstName"]),
string_at(user, &["lastName"]),
]
.into_iter()
.filter(|s| !s.is_empty())
.collect::<Vec<_>>()
.join(" ");
let identity = [string_at(user, &["email"]), string_at(user, &["username"])]
.into_iter()
.find(|value| !value.is_empty())
.unwrap_or_default();
[name, identity]
.into_iter()
.filter(|s| !s.is_empty())
.collect::<Vec<_>>()
.join(" · ")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rpc_payload_unwraps_runtime_and_api_envelopes() {
let value = json!({"result": {"data": {"mode": "standard"}}, "logs": []});
assert_eq!(rpc_payload(&value), &json!({"mode": "standard"}));
assert_eq!(string_at(rpc_payload(&value), &["mode"]), "standard");
}
#[test]
fn string_at_never_coerces_non_string_or_missing_values() {
let value = json!({"enabled": true});
assert_eq!(string_at(&value, &["enabled"]), "");
assert_eq!(string_at(&value, &["missing"]), "");
}
#[test]
fn account_detail_uses_canonical_backend_user_fields() {
let user = json!({
"firstName": "Ada",
"lastName": "Lovelace",
"email": "ada@example.test",
"username": "ada"
});
assert_eq!(account_detail(&user), "Ada Lovelace · ada@example.test");
}
#[test]
fn curated_config_fields_map_to_safe_specific_updates() {
let cases = [
(
ConfigKey::ApiUrl,
"openhuman.config_update_model_settings",
json!({"api_url": "value"}),
),
(
ConfigKey::InferenceUrl,
"openhuman.config_update_model_settings",
json!({"inference_url": "value"}),
),
(
ConfigKey::DefaultModel,
"openhuman.config_update_model_settings",
json!({"default_model": "value"}),
),
(
ConfigKey::AutonomyLevel,
"openhuman.config_update_autonomy_settings",
json!({"level": "value"}),
),
(
ConfigKey::PrivacyMode,
"openhuman.config_set_privacy_mode",
json!({"mode": "value"}),
),
];
for (key, expected_method, expected_params) in cases {
let (method, params) = config_update(key, "value".to_string());
assert_eq!(method, expected_method);
assert_eq!(params, expected_params);
}
}
}