From 5e2a2fcff9dd308d6b1dd62694d22faea4a546cf Mon Sep 17 00:00:00 2001 From: zlt Date: Mon, 21 Sep 2026 18:34:05 +0800 Subject: [PATCH] feat(summary): filter history by mentioned user --- rust-agent/crates/app/src/event_handler.rs | 1 + rust-agent/crates/app/src/history_rules.rs | 47 +++++++++++++++++++ rust-agent/crates/app/src/main.rs | 10 +++- rust-agent/crates/app/src/pipeline_input.rs | 2 +- rust-agent/crates/app/src/summary_command.rs | 29 +++++++++++- rust-agent/crates/app/src/summary_pipeline.rs | 12 +++-- 6 files changed, 95 insertions(+), 6 deletions(-) diff --git a/rust-agent/crates/app/src/event_handler.rs b/rust-agent/crates/app/src/event_handler.rs index 00faa56..f527f83 100644 --- a/rust-agent/crates/app/src/event_handler.rs +++ b/rust-agent/crates/app/src/event_handler.rs @@ -184,6 +184,7 @@ pub(crate) async fn handle_platform_event( command.image_token_present, command.preview_only, ); + pipeline_options.sender_filter = command.sender_filter.clone(); if config.image_gen.enabled && !config.image_summary_enabled_for_room(&trigger.room_id) { info!(room_id = %trigger.room_id, "image summary disabled by room capability"); append_runtime_log( diff --git a/rust-agent/crates/app/src/history_rules.rs b/rust-agent/crates/app/src/history_rules.rs index 4bc6feb..64588d8 100644 --- a/rust-agent/crates/app/src/history_rules.rs +++ b/rust-agent/crates/app/src/history_rules.rs @@ -22,6 +22,18 @@ pub(crate) fn is_current_incoming(message: &IncomingMessage, incoming: &Incoming pub(crate) fn is_agent_status(message: &PlatformHistoryMessage) -> bool { is_agent_status_content(&message.content) } +pub(crate) fn matches_sender_filter(message: &PlatformHistoryMessage, filter: &str) -> bool { + let filter = normalize_sender(filter); + !filter.is_empty() + && [ + Some(message.sender_id.as_str()), + message.sender_name.as_deref(), + ] + .into_iter() + .flatten() + .any(|sender| normalize_sender(sender) == filter) +} + pub(crate) fn is_agent_status_content(content: &str) -> bool { let content = content.trim(); content.starts_with("收到 /总结") || content.starts_with("收到 #总结") @@ -34,3 +46,38 @@ fn stable_ids_match(left: &str, right: &str) -> bool { left == right || stable_id_number(left).is_some() && stable_id_number(left) == stable_id_number(right) } fn stable_id_number(value: &str) -> Option { value.rsplit_once(':').map(|(_, id)| id).unwrap_or(value).parse().ok() } + +fn normalize_sender(value: &str) -> String { + value.trim().trim_start_matches('@').to_lowercase() +} + +#[cfg(test)] +mod tests { + use chrono::{TimeZone, Utc}; + + use super::*; + + fn message(sender_id: &str, sender_name: Option<&str>) -> PlatformHistoryMessage { + PlatformHistoryMessage { + stable_id: None, + timestamp: Utc.timestamp_opt(1_700_000_000, 0).unwrap(), + sender_id: sender_id.to_string(), + sender_name: sender_name.map(str::to_string), + content: "hello".to_string(), + msg_type: "text".to_string(), + media_path: None, + thumbnail_path: None, + decoded_media_path: None, + media_decode_error: None, + is_self: false, + } + } + + #[test] + fn sender_filter_matches_name_or_id_exactly() { + let message = message("wxid_alice", Some("Alice")); + assert!(matches_sender_filter(&message, "@Alice")); + assert!(matches_sender_filter(&message, "wxid_alice")); + assert!(!matches_sender_filter(&message, "Ali")); + } +} diff --git a/rust-agent/crates/app/src/main.rs b/rust-agent/crates/app/src/main.rs index 721945f..cdc846c 100644 --- a/rust-agent/crates/app/src/main.rs +++ b/rust-agent/crates/app/src/main.rs @@ -619,7 +619,7 @@ fn record_image_cooldown_success( Ok(()) } -fn progress_message(options: PipelineOptions) -> &'static str { +fn progress_message(options: &PipelineOptions) -> &'static str { match (options.text_summary_enabled, options.image_gen_enabled) { (true, true) => "收到 /总结,正在整理群聊并生成图片。", (true, false) => "收到 /总结,正在整理文字总结。", @@ -1512,6 +1512,7 @@ mod tests { range_minutes: Some(60), image_token_present: false, preview_only: false, + sender_filter: None, }) ); } @@ -1531,6 +1532,7 @@ mod tests { range_minutes: Some(60), image_token_present: false, preview_only: false, + sender_filter: None, }) ); } @@ -1575,6 +1577,7 @@ mod tests { range_minutes: Some(120), image_token_present: false, preview_only: false, + sender_filter: None, } ); } @@ -1590,6 +1593,7 @@ mod tests { range_minutes: Some(24 * 60), image_token_present: false, preview_only: false, + sender_filter: None, } ); } @@ -1618,6 +1622,7 @@ mod tests { range_minutes: None, image_token_present: false, preview_only: false, + sender_filter: None, } ); } @@ -1633,6 +1638,7 @@ mod tests { range_minutes: None, image_token_present: true, preview_only: false, + sender_filter: None, } ); } @@ -1650,6 +1656,7 @@ mod tests { range_minutes: Some(24 * 60), image_token_present: true, preview_only: false, + sender_filter: None, } ); } @@ -1665,6 +1672,7 @@ mod tests { range_minutes: Some(60), image_token_present: true, preview_only: false, + sender_filter: None, } ); } diff --git a/rust-agent/crates/app/src/pipeline_input.rs b/rust-agent/crates/app/src/pipeline_input.rs index d2a5b8a..54bad61 100644 --- a/rust-agent/crates/app/src/pipeline_input.rs +++ b/rust-agent/crates/app/src/pipeline_input.rs @@ -150,7 +150,7 @@ pub(super) async fn prepare_pipeline_input( config: &AgentConfig, client: &PlatformWorker, room_id: &str, - options: PipelineOptions, + options: &PipelineOptions, task: Option<&OperationalTask>, mut history: Vec, ) -> Result> { diff --git a/rust-agent/crates/app/src/summary_command.rs b/rust-agent/crates/app/src/summary_command.rs index 456d841..af27aa6 100644 --- a/rust-agent/crates/app/src/summary_command.rs +++ b/rust-agent/crates/app/src/summary_command.rs @@ -2,12 +2,13 @@ use wechat_summary_core::{config::PlatformKindConfig, TriggerMatch}; -#[derive(Debug, Clone, Copy, Eq, PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq)] pub(crate) struct SummaryCommand { pub(crate) target_platform: PlatformKindConfig, pub(crate) range_minutes: Option, pub(crate) image_token_present: bool, pub(crate) preview_only: bool, + pub(crate) sender_filter: Option, } pub(crate) fn parse( @@ -33,12 +34,14 @@ pub(crate) fn parse_args( range_minutes: None, image_token_present: false, preview_only: false, + sender_filter: None, }); } let mut target_platform = default_platform; let mut image_token_present = false; let mut preview_only = false; + let mut sender_filter = None; let mut range_tokens: Vec<&str> = Vec::new(); for token in args.split_whitespace() { if let Some(platform) = PlatformKindConfig::parse_alias(token) { @@ -47,6 +50,8 @@ pub(crate) fn parse_args( image_token_present = true; } else if is_preview_token(token) { preview_only = true; + } else if let Some(sender) = token.strip_prefix('@').filter(|sender| !sender.is_empty()) { + sender_filter = Some(sender.to_string()); } else { range_tokens.push(token); } @@ -63,6 +68,7 @@ pub(crate) fn parse_args( range_minutes, image_token_present, preview_only, + sender_filter, }) } @@ -139,3 +145,24 @@ fn parse_strict_duration_minutes(tokens: &[&str]) -> Option { _ => None, } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parses_sender_filter_after_time_range() { + let command = parse_args("24h @Alice", PlatformKindConfig::Wx4py).unwrap(); + + assert_eq!(command.range_minutes, Some(24 * 60)); + assert_eq!(command.sender_filter.as_deref(), Some("Alice")); + } + + #[test] + fn parses_sender_filter_before_time_range() { + let command = parse_args("@Alice 24h", PlatformKindConfig::Wx4py).unwrap(); + + assert_eq!(command.range_minutes, Some(24 * 60)); + assert_eq!(command.sender_filter.as_deref(), Some("Alice")); + } +} diff --git a/rust-agent/crates/app/src/summary_pipeline.rs b/rust-agent/crates/app/src/summary_pipeline.rs index f7f938a..09f1cff 100644 --- a/rust-agent/crates/app/src/summary_pipeline.rs +++ b/rust-agent/crates/app/src/summary_pipeline.rs @@ -2,7 +2,7 @@ use crate::*; -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone)] pub(crate) struct PipelineOptions { pub(crate) text_summary_enabled: bool, pub(crate) image_gen_enabled: bool, @@ -13,6 +13,7 @@ pub(crate) struct PipelineOptions { pub(crate) preview_only: bool, pub(crate) detail: wechat_summary_core::config::SummaryDetail, pub(crate) media_decode_limit: Option, + pub(crate) sender_filter: Option, } #[derive(Clone)] @@ -73,6 +74,7 @@ impl PipelineOptions { .summary_detail .unwrap_or(config.text_summary.detail), media_decode_limit: None, + sender_filter: None, } } @@ -92,6 +94,7 @@ impl PipelineOptions { .summary_detail .unwrap_or(config.text_summary.detail), media_decode_limit: None, + sender_filter: None, } } } @@ -353,7 +356,7 @@ pub(crate) async fn run_summary_pipeline( if options.send_progress { if let Err(error) = client - .send_text(&trigger.room_id, progress_message(options)) + .send_text(&trigger.room_id, progress_message(&options)) .await { warn!( @@ -464,6 +467,9 @@ pub(crate) async fn run_summary_pipeline( !history_rules::is_current_trigger(message, incoming) && !history_rules::is_agent_status(message) }); + if let Some(sender_filter) = options.sender_filter.as_deref() { + history.retain(|message| history_rules::matches_sender_filter(message, sender_filter)); + } let filtered_history_len = history.len(); let removed_history_len = raw_history_len.saturating_sub(filtered_history_len); info!( @@ -485,7 +491,7 @@ pub(crate) async fn run_summary_pipeline( config, client, &trigger.room_id, - options, + &options, task, history, )