feat(app): background collection frame for sites, search words and RSS feeds
Targets (none by default), interval and keep days on a new 백그라운드 수집 screen with the results; the worker collects on its own thread through the web search's address check and writes only to the data folder's collect/ (logs carry target names and counts). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -91,6 +91,25 @@ pub struct FeatureSettings {
|
||||
pub weather: bool,
|
||||
pub web_search: bool,
|
||||
pub meals: bool,
|
||||
/// Background collection (admin.spec.md, "Background collection").
|
||||
pub collect: bool,
|
||||
}
|
||||
|
||||
/// Background collection (admin.spec.md, "Background collection"); no targets by default.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct CollectSettings {
|
||||
pub keep_days: u32,
|
||||
pub targets: Vec<CollectTarget>,
|
||||
}
|
||||
|
||||
/// One thing to collect: `kind` is `site`, `search` or `rss`; `value` its address or words.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct CollectTarget {
|
||||
pub name: String,
|
||||
pub kind: String,
|
||||
pub value: String,
|
||||
pub every_minutes: u32,
|
||||
pub enabled: bool,
|
||||
}
|
||||
|
||||
/// An MCP server the brain starts (stdio). The values of `env` are in the secrets, never here.
|
||||
@@ -121,6 +140,7 @@ pub struct Settings {
|
||||
pub llm: LlmSettings,
|
||||
pub features: FeatureSettings,
|
||||
pub mcp_servers: Vec<McpServerSettings>,
|
||||
pub collect: CollectSettings,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -164,8 +184,9 @@ impl Default for Settings {
|
||||
response: PolicySettings::default(),
|
||||
memory: Map::new(),
|
||||
llm: LlmSettings { url: None, port: 8080, context: 32768 },
|
||||
features: FeatureSettings { weather: true, web_search: true, meals: true },
|
||||
features: FeatureSettings { weather: true, web_search: true, meals: true, collect: true },
|
||||
mcp_servers: vec![],
|
||||
collect: CollectSettings { keep_days: 30, targets: vec![] },
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -483,12 +504,76 @@ pub fn parse_settings(raw: &Value) -> Result<Settings, SettingsError> {
|
||||
weather: get_bool(f, "weather", "features", d.features.weather)?,
|
||||
web_search: get_bool(f, "webSearch", "features", d.features.web_search)?,
|
||||
meals: get_bool(f, "meals", "features", d.features.meals)?,
|
||||
collect: get_bool(f, "collect", "features", d.features.collect)?,
|
||||
}
|
||||
},
|
||||
mcp_servers: parse_mcp_servers(raw)?,
|
||||
collect: parse_collect(raw, &d.collect)?,
|
||||
})
|
||||
}
|
||||
|
||||
/// A collection target's name: 1-32 letters, digits, `-` or `_` in any script. It names the
|
||||
/// target's folder, so a Windows device name (`con`, `nul`, `com1`, ...) is refused.
|
||||
pub fn valid_collect_name(s: &str) -> bool {
|
||||
let n = s.chars().count();
|
||||
let lower = s.to_ascii_lowercase();
|
||||
let device = ["con", "prn", "aux", "nul"].contains(&lower.as_str())
|
||||
|| (lower.len() == 4 && (lower.starts_with("com") || lower.starts_with("lpt"))
|
||||
&& lower.as_bytes()[3].is_ascii_digit());
|
||||
(1..=32).contains(&n) && s.chars().all(|c| c.is_alphanumeric() || c == '-' || c == '_') && !device
|
||||
}
|
||||
|
||||
fn get_uint(m: Option<&Map<String, Value>>, key: &str, path: &str, default: u32, min: u32, max: u32)
|
||||
-> Result<u32, SettingsError> {
|
||||
match m.and_then(|m| m.get(key)) {
|
||||
None => Ok(default),
|
||||
Some(v) => match v.as_u64() {
|
||||
Some(n) if (min as u64..=max as u64).contains(&n) => Ok(n as u32),
|
||||
_ => err(format!("{path}.{key} must be an integer {min}-{max}")),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_collect(raw: &Map<String, Value>, d: &CollectSettings) -> Result<CollectSettings, SettingsError> {
|
||||
let c = section(raw, "collect")?;
|
||||
let keep_days = get_uint(c, "keepDays", "collect", d.keep_days, 1, 365)?;
|
||||
let list = match c.and_then(|m| m.get("targets")) {
|
||||
None => return Ok(CollectSettings { keep_days, targets: vec![] }),
|
||||
Some(Value::Array(a)) => a,
|
||||
Some(_) => return err("collect.targets must be a list"),
|
||||
};
|
||||
let mut targets: Vec<CollectTarget> = Vec::new();
|
||||
for (i, v) in list.iter().enumerate() {
|
||||
let path = format!("collect.targets[{i}]");
|
||||
let Value::Object(m) = v else { return err(format!("{path} must be an object")) };
|
||||
let name = get_str(Some(m), "name", &path, "")?.trim().to_owned();
|
||||
if !valid_collect_name(&name) {
|
||||
return err(format!("{path}.name must be 1-32 letters, digits, - or _ (not a device name like con)"));
|
||||
}
|
||||
if targets.iter().any(|t| t.name == name) {
|
||||
return err(format!("{path}.name is listed twice"));
|
||||
}
|
||||
let kind = get_str(Some(m), "kind", &path, "")?;
|
||||
let value = get_str(Some(m), "value", &path, "")?.trim().to_owned();
|
||||
let ok = match kind.as_str() {
|
||||
"site" | "rss" => (value.starts_with("http://") || value.starts_with("https://")) && value.len() <= 2000,
|
||||
"search" => (1..=200).contains(&value.chars().count()),
|
||||
_ => return err(format!("{path}.kind must be site, search or rss")),
|
||||
};
|
||||
if !ok {
|
||||
return err(format!("{path}.value must be an http(s) address (site, rss) or 1-200 characters of words (search)"));
|
||||
}
|
||||
targets.push(CollectTarget {
|
||||
name,
|
||||
kind,
|
||||
value,
|
||||
every_minutes: get_uint(Some(m), "everyMinutes", &path, 60, 15, 10080)?,
|
||||
enabled: get_bool(Some(m), "enabled", &path, true)?,
|
||||
});
|
||||
}
|
||||
Ok(CollectSettings { keep_days, targets })
|
||||
}
|
||||
|
||||
/// An MCP server name: 1-32 letters, digits, `-` or `_`, without `__` (the brain names a server's
|
||||
/// tools `<server>__<tool>`).
|
||||
fn valid_mcp_name(s: &str) -> bool {
|
||||
@@ -718,13 +803,13 @@ mod tests {
|
||||
#[test]
|
||||
fn features_and_mcp_servers_are_read_and_checked() {
|
||||
let s = parse_settings(&json!({}));
|
||||
assert_eq!(s.unwrap().features, FeatureSettings { weather: true, web_search: true, meals: true });
|
||||
assert_eq!(s.unwrap().features, FeatureSettings { weather: true, web_search: true, meals: true, collect: true });
|
||||
let s = parse_settings(&json!({
|
||||
"features": {"weather": false, "meals": false},
|
||||
"mcpServers": [{"name": "brave", "command": "npx", "args": ["-y", "brave"], "env": ["BRAVE_API_KEY", " "]},
|
||||
{"name": "time-2", "command": "uvx", "enabled": false}]
|
||||
})).unwrap();
|
||||
assert_eq!(s.features, FeatureSettings { weather: false, web_search: true, meals: false });
|
||||
assert_eq!(s.features, FeatureSettings { weather: false, web_search: true, meals: false, collect: true });
|
||||
assert_eq!(s.mcp_servers, vec![
|
||||
McpServerSettings { name: "brave".into(), command: "npx".into(), args: vec!["-y".into(), "brave".into()],
|
||||
env: vec!["BRAVE_API_KEY".into()], enabled: true },
|
||||
@@ -747,6 +832,49 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn collection_targets_are_read_and_checked() {
|
||||
let s = parse_settings(&json!({})).unwrap();
|
||||
assert_eq!(s.collect, CollectSettings { keep_days: 30, targets: vec![] });
|
||||
let s = parse_settings(&json!({"features": {"collect": false}, "collect": {"keepDays": 7, "targets": [
|
||||
{"name": "뉴스_1", "kind": "rss", "value": " https://feed.example/rss "},
|
||||
{"name": "rust", "kind": "search", "value": "rust release", "everyMinutes": 1440, "enabled": false}]}})).unwrap();
|
||||
assert!(!s.features.collect);
|
||||
assert_eq!(s.collect, CollectSettings { keep_days: 7, targets: vec![
|
||||
CollectTarget { name: "뉴스_1".into(), kind: "rss".into(), value: "https://feed.example/rss".into(),
|
||||
every_minutes: 60, enabled: true },
|
||||
CollectTarget { name: "rust".into(), kind: "search".into(), value: "rust release".into(),
|
||||
every_minutes: 1440, enabled: false },
|
||||
]});
|
||||
let t = |extra: Value| {
|
||||
let mut m = json!({"name": "a", "kind": "site", "value": "https://a.example"});
|
||||
m.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());
|
||||
json!({"collect": {"targets": [m]}})
|
||||
};
|
||||
let cases = [
|
||||
(json!({"collect": {"keepDays": 0}}), "collect.keepDays"),
|
||||
(json!({"collect": {"keepDays": 366}}), "collect.keepDays"),
|
||||
(json!({"collect": {"targets": {}}}), "collect.targets"),
|
||||
(t(json!({"name": ""})), "collect.targets[0].name"),
|
||||
(t(json!({"name": "a/b"})), "collect.targets[0].name"),
|
||||
(t(json!({"name": "../x"})), "collect.targets[0].name"),
|
||||
(t(json!({"name": "CON"})), "collect.targets[0].name"),
|
||||
(t(json!({"name": "com1"})), "collect.targets[0].name"),
|
||||
(t(json!({"kind": "video"})), "collect.targets[0].kind"),
|
||||
(t(json!({"value": "ftp://a.example"})), "collect.targets[0].value"),
|
||||
(t(json!({"kind": "search", "value": " "})), "collect.targets[0].value"),
|
||||
(t(json!({"everyMinutes": 14})), "collect.targets[0].everyMinutes"),
|
||||
(t(json!({"everyMinutes": 10081})), "collect.targets[0].everyMinutes"),
|
||||
(json!({"collect": {"targets": [{"name": "a", "kind": "rss", "value": "https://a"},
|
||||
{"name": "a", "kind": "rss", "value": "https://b"}]}}), "collect.targets[1].name"),
|
||||
];
|
||||
for (raw, key) in cases {
|
||||
let e = parse_settings(&raw).unwrap_err();
|
||||
assert!(e.0.contains(key), "{raw} -> {e}");
|
||||
}
|
||||
assert!(valid_collect_name("console") && valid_collect_name("com") && valid_collect_name("날씨-서울"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn null_ids_clear_them() {
|
||||
let s = parse_settings(&json!({"discord": {"guildId": null, "voiceChannelId": null}})).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user