Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust-agent/crates/app/src/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
47 changes: 47 additions & 0 deletions rust-agent/crates/app/src/history_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("收到 #总结")
Expand All @@ -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<u64> { 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"));
}
}
10 changes: 9 additions & 1 deletion rust-agent/crates/app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => "收到 /总结,正在整理文字总结。",
Expand Down Expand Up @@ -1512,6 +1512,7 @@ mod tests {
range_minutes: Some(60),
image_token_present: false,
preview_only: false,
sender_filter: None,
})
);
}
Expand All @@ -1531,6 +1532,7 @@ mod tests {
range_minutes: Some(60),
image_token_present: false,
preview_only: false,
sender_filter: None,
})
);
}
Expand Down Expand Up @@ -1575,6 +1577,7 @@ mod tests {
range_minutes: Some(120),
image_token_present: false,
preview_only: false,
sender_filter: None,
}
);
}
Expand All @@ -1590,6 +1593,7 @@ mod tests {
range_minutes: Some(24 * 60),
image_token_present: false,
preview_only: false,
sender_filter: None,
}
);
}
Expand Down Expand Up @@ -1618,6 +1622,7 @@ mod tests {
range_minutes: None,
image_token_present: false,
preview_only: false,
sender_filter: None,
}
);
}
Expand All @@ -1633,6 +1638,7 @@ mod tests {
range_minutes: None,
image_token_present: true,
preview_only: false,
sender_filter: None,
}
);
}
Expand All @@ -1650,6 +1656,7 @@ mod tests {
range_minutes: Some(24 * 60),
image_token_present: true,
preview_only: false,
sender_filter: None,
}
);
}
Expand All @@ -1665,6 +1672,7 @@ mod tests {
range_minutes: Some(60),
image_token_present: true,
preview_only: false,
sender_filter: None,
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion rust-agent/crates/app/src/pipeline_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<crate::platform::PlatformHistoryMessage>,
) -> Result<Option<PreparedPipelineInput>> {
Expand Down
29 changes: 28 additions & 1 deletion rust-agent/crates/app/src/summary_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i64>,
pub(crate) image_token_present: bool,
pub(crate) preview_only: bool,
pub(crate) sender_filter: Option<String>,
}

pub(crate) fn parse(
Expand All @@ -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) {
Expand All @@ -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);
}
Expand All @@ -63,6 +68,7 @@ pub(crate) fn parse_args(
range_minutes,
image_token_present,
preview_only,
sender_filter,
})
}

Expand Down Expand Up @@ -139,3 +145,24 @@ fn parse_strict_duration_minutes(tokens: &[&str]) -> Option<i64> {
_ => 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"));
}
}
12 changes: 9 additions & 3 deletions rust-agent/crates/app/src/summary_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<usize>,
pub(crate) sender_filter: Option<String>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -73,6 +74,7 @@ impl PipelineOptions {
.summary_detail
.unwrap_or(config.text_summary.detail),
media_decode_limit: None,
sender_filter: None,
}
}

Expand All @@ -92,6 +94,7 @@ impl PipelineOptions {
.summary_detail
.unwrap_or(config.text_summary.detail),
media_decode_limit: None,
sender_filter: None,
}
}
}
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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!(
Expand All @@ -485,7 +491,7 @@ pub(crate) async fn run_summary_pipeline(
config,
client,
&trigger.room_id,
options,
&options,
task,
history,
)
Expand Down
Loading