Skip to content

Commit f52ec22

Browse files
authored
Merge pull request #8733 from senamakel/surplus-jev
feat(computer): captcha_provider selects TypeSafe or Surplus for the captcha solver
2 parents 821d9b0 + 606ba8a commit f52ec22

18 files changed

Lines changed: 290 additions & 96 deletions

File tree

‎Cargo.lock‎

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/openhuman-app/Cargo.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/openhuman-core/src/config/ops/computer.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct ComputerSettingsPatch {
1717
pub rescue_model: Option<String>,
1818
pub max_rescues: Option<u32>,
1919
pub captcha_solver: Option<bool>,
20+
pub captcha_provider: Option<String>,
2021
pub captcha_model: Option<String>,
2122
pub max_captcha_attempts: Option<u32>,
2223
}
@@ -38,6 +39,19 @@ fn parse_decision_model(raw: &str) -> Result<DecisionModel, String> {
3839
}
3940
}
4041

42+
/// The captcha solver's Jev provider in its wire spelling; empty restores
43+
/// the default (`type_safe`).
44+
fn parse_captcha_provider(raw: &str) -> Result<Option<String>, String> {
45+
match raw.trim().to_ascii_lowercase().replace('-', "_").as_str() {
46+
"" => Ok(None),
47+
"type_safe" | "typesafe" => Ok(Some("type_safe".into())),
48+
"surplus" => Ok(Some("surplus".into())),
49+
other => Err(format!(
50+
"Unsupported captcha provider '{other}'. Use type_safe or surplus"
51+
)),
52+
}
53+
}
54+
4155
fn model_id(raw: String) -> Result<Option<String>, String> {
4256
let value = raw.trim();
4357
if value.is_empty() {
@@ -76,6 +90,9 @@ pub fn patched(
7690
if let Some(enabled) = update.captcha_solver {
7791
computer.captcha_solver = enabled;
7892
}
93+
if let Some(raw) = update.captcha_provider {
94+
computer.captcha_provider = parse_captcha_provider(&raw)?;
95+
}
7996
if let Some(raw) = update.captcha_model {
8097
computer.captcha_model = model_id(raw)?;
8198
}
@@ -101,6 +118,7 @@ pub async fn apply_computer_settings(
101118
rescue_model = ?config.computer.rescue_model,
102119
max_rescues = ?config.computer.max_rescues,
103120
captcha_solver = config.computer.captcha_solver,
121+
captcha_provider = ?config.computer.captcha_provider,
104122
captcha_model = ?config.computer.captcha_model,
105123
max_captcha_attempts = ?config.computer.max_captcha_attempts,
106124
"[config] computer settings updated"

‎crates/openhuman-core/src/config/ops/computer_tests.rs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,43 @@ fn patch_turns_the_captcha_solver_on_with_its_model_and_attempts() {
109109
assert_eq!(next.max_captcha_attempts, Some(4));
110110
assert!(!ComputerConfig::default().captcha_solver, "off by default");
111111
}
112+
113+
#[test]
114+
fn patch_selects_the_captcha_provider() {
115+
let base = ComputerConfig::default();
116+
for (raw, stored) in [
117+
("surplus", Some("surplus")),
118+
(" Surplus ", Some("surplus")),
119+
("type_safe", Some("type_safe")),
120+
("Type-Safe", Some("type_safe")),
121+
("typesafe", Some("type_safe")),
122+
(" ", None),
123+
] {
124+
let next = patched(
125+
&base,
126+
ComputerSettingsPatch {
127+
captcha_provider: Some(raw.into()),
128+
..ComputerSettingsPatch::default()
129+
},
130+
)
131+
.unwrap();
132+
assert_eq!(next.captcha_provider.as_deref(), stored, "{raw:?}");
133+
}
134+
}
135+
136+
#[test]
137+
fn an_unknown_captcha_provider_is_refused_with_the_accepted_values() {
138+
let error = patched(
139+
&ComputerConfig::default(),
140+
ComputerSettingsPatch {
141+
captcha_provider: Some("open_router".into()),
142+
..ComputerSettingsPatch::default()
143+
},
144+
)
145+
.unwrap_err();
146+
assert!(error.contains("open_router"), "{error}");
147+
assert!(
148+
error.contains("type_safe") && error.contains("surplus"),
149+
"{error}"
150+
);
151+
}

‎crates/openhuman-core/src/config/schema/computer.rs‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,17 @@ pub struct ComputerConfig {
4646
/// `None` keeps the module's default (5, also its maximum).
4747
pub max_rescues: Option<u32>,
4848
/// Let a browser task try a captcha or "verify you are human" check with
49-
/// Jev's image questions before it asks a person. Needs a TypeSafe key
50-
/// (the `typesafe` credential or `TYPESAFE_API_KEY`). Off by default.
49+
/// Jev's image questions before it asks a person. Needs a key for the
50+
/// [`captcha_provider`](Self::captcha_provider): TypeSafe (the `typesafe`
51+
/// credential or `TYPESAFE_API_KEY`) or Surplus (the `surplus` credential
52+
/// or `SURPLUS_API_KEY`). Off by default.
5153
pub captcha_solver: bool,
54+
/// Where the captcha solver's Jev runs: `type_safe` (TypeSafe's own API)
55+
/// or `surplus` (Surplus Intelligence). `None` means `type_safe`.
56+
pub captcha_provider: Option<String>,
5257
/// The image-reading Jev model the captcha solver asks. `None` keeps the
53-
/// module's default (`jev-preview`).
58+
/// module's default for the provider (`jev-preview` on TypeSafe,
59+
/// `jev-1.14` on Surplus).
5460
pub captcha_model: Option<String>,
5561
/// Image challenges one try may submit before the task asks a person;
5662
/// `None` keeps the module's default (3). At most 10.

‎crates/openhuman-core/src/config/schema/computer_tests.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,15 @@ fn parses_every_decision_model_and_rescue_settings() {
2727
}
2828
assert!(toml::from_str::<ComputerConfig>("decision_model = \"jet\"").is_err());
2929
}
30+
31+
#[test]
32+
fn the_captcha_provider_defaults_to_absent_and_round_trips() {
33+
assert_eq!(ComputerConfig::default().captcha_provider, None);
34+
let config: ComputerConfig =
35+
toml::from_str("captcha_solver = true\ncaptcha_provider = \"surplus\"").unwrap();
36+
assert_eq!(config.captcha_provider.as_deref(), Some("surplus"));
37+
let saved = toml::to_string(&config).unwrap();
38+
assert!(saved.contains("captcha_provider = \"surplus\""), "{saved}");
39+
let reloaded: ComputerConfig = toml::from_str(&saved).unwrap();
40+
assert_eq!(reloaded, config);
41+
}

‎crates/openhuman-core/src/config/schemas/controllers.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ mod workspace;
1212

1313
pub use registry::{all_controller_schemas, all_registered_controllers};
1414

15+
#[cfg(test)]
16+
use agent::handle_update_computer_settings;
1517
#[cfg(test)]
1618
pub(super) use agent::{handle_get_autonomy_settings, handle_update_autonomy_settings};
1719
#[cfg(test)]

‎crates/openhuman-core/src/config/schemas/controllers/agent.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ pub(super) fn handle_update_computer_settings(
146146
rescue_model: update.rescue_model,
147147
max_rescues: update.max_rescues,
148148
captcha_solver: update.captcha_solver,
149+
captcha_provider: update.captcha_provider,
149150
captcha_model: update.captcha_model,
150151
max_captcha_attempts: update.max_captcha_attempts,
151152
};

‎crates/openhuman-core/src/config/schemas/controllers_tests.rs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,28 @@ async fn handle_get_data_paths_scopes_to_explicit_user_id() {
3131
}
3232

3333
// ── platform slug validation (finding #6) ───────────────────
34+
35+
// ── update_computer_settings captcha provider ────────────────
36+
37+
#[tokio::test]
38+
async fn update_computer_settings_saves_the_captcha_provider() {
39+
let dir = tempfile::tempdir().unwrap();
40+
let mut config = crate::config::Config::default();
41+
config.config_path = dir.path().join("config.toml");
42+
config.workspace_dir = dir.path().join("workspace");
43+
let ctx = DispatchCtx::new(crate::core::runtime::CoreContext::for_inspection(config));
44+
let mut params = Map::new();
45+
params.insert("captcha_solver".into(), Value::Bool(true));
46+
params.insert("captcha_provider".into(), Value::String("surplus".into()));
47+
handle_update_computer_settings(ctx, params).await.unwrap();
48+
let saved = std::fs::read_to_string(dir.path().join("config.toml")).unwrap();
49+
assert!(saved.contains("captcha_provider = \"surplus\""), "{saved}");
50+
51+
let schema = super::super::schema_defs::schemas("update_computer_settings");
52+
let field = schema
53+
.inputs
54+
.iter()
55+
.find(|field| field.name == "captcha_provider")
56+
.expect("captcha_provider schema input");
57+
assert!(field.comment.contains("type_safe") && field.comment.contains("surplus"));
58+
}

‎crates/openhuman-core/src/config/schemas/helpers.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ pub(super) struct ComputerSettingsUpdate {
115115
pub(super) rescue_model: Option<String>,
116116
pub(super) max_rescues: Option<u32>,
117117
pub(super) captcha_solver: Option<bool>,
118+
pub(super) captcha_provider: Option<String>,
118119
pub(super) captcha_model: Option<String>,
119120
pub(super) max_captcha_attempts: Option<u32>,
120121
}

0 commit comments

Comments
 (0)