Skip to content

Commit 4b3d07b

Browse files
author
chaodu-agent
committed
refactor: encapsulate handle_message params into MessageContext struct
Introduce MessageContext to bundle per-message parameters, reducing AdapterRouter::handle_message from 7 parameters to 2 (adapter + ctx). - Add MessageContext struct in adapter.rs - Update cron.rs call site to construct MessageContext - Remove #[allow(clippy::too_many_arguments)] suppression Closes #536
1 parent 936f2cb commit 4b3d07b

2 files changed

Lines changed: 31 additions & 16 deletions

File tree

src/adapter.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ pub struct MessageRef {
6565
pub message_id: String,
6666
}
6767

68+
/// Bundles per-message parameters for `AdapterRouter::handle_message`.
69+
///
70+
/// Introduced to reduce parameter count and make the signature extensible
71+
/// (e.g. streaming policy, rate limit hints) without breaking call sites.
72+
pub struct MessageContext {
73+
pub thread_channel: ChannelRef,
74+
pub sender_json: String,
75+
pub prompt: String,
76+
pub extra_blocks: Vec<ContentBlock>,
77+
pub trigger_msg: MessageRef,
78+
pub other_bot_present: bool,
79+
}
80+
6881
/// Sender identity injected into prompts for downstream agent context.
6982
///
7083
/// This is **metadata for the agent** — `channel_id` always refers to the
@@ -209,34 +222,29 @@ impl AdapterRouter {
209222
/// Handle an incoming user message. The adapter is responsible for
210223
/// filtering, resolving the thread, and building the SenderContext.
211224
/// This method handles sender context injection, session management, and streaming.
212-
#[allow(clippy::too_many_arguments)]
213225
pub async fn handle_message(
214226
&self,
215227
adapter: &Arc<dyn ChatAdapter>,
216-
thread_channel: &ChannelRef,
217-
sender_json: &str,
218-
prompt: &str,
219-
extra_blocks: Vec<ContentBlock>,
220-
trigger_msg: &MessageRef,
221-
other_bot_present: bool,
228+
ctx: MessageContext,
222229
) -> Result<()> {
223230
tracing::debug!(platform = adapter.platform(), "processing message");
224231

225-
let content_blocks = Self::pack_arrival_event(sender_json, prompt, extra_blocks);
232+
let content_blocks =
233+
Self::pack_arrival_event(&ctx.sender_json, &ctx.prompt, ctx.extra_blocks);
226234

227235
let thread_key = format!(
228236
"{}:{}",
229237
adapter.platform(),
230-
thread_channel
238+
ctx.thread_channel
231239
.thread_id
232240
.as_deref()
233-
.unwrap_or(&thread_channel.channel_id)
241+
.unwrap_or(&ctx.thread_channel.channel_id)
234242
);
235243

236244
if let Err(e) = self.pool.get_or_create(&thread_key).await {
237245
let msg = format_user_error(&e.to_string());
238246
let _ = adapter
239-
.send_message(thread_channel, &format!("⚠️ {msg}"))
247+
.send_message(&ctx.thread_channel, &format!("⚠️ {msg}"))
240248
.await;
241249
error!("pool error: {e}");
242250
return Err(e);
@@ -245,7 +253,7 @@ impl AdapterRouter {
245253
let reactions = Arc::new(StatusReactionController::new(
246254
self.reactions_config.enabled,
247255
adapter.clone(),
248-
trigger_msg.clone(),
256+
ctx.trigger_msg.clone(),
249257
self.reactions_config.emojis.clone(),
250258
self.reactions_config.timing.clone(),
251259
));
@@ -256,9 +264,9 @@ impl AdapterRouter {
256264
adapter,
257265
&thread_key,
258266
content_blocks,
259-
thread_channel,
267+
&ctx.thread_channel,
260268
reactions.clone(),
261-
other_bot_present,
269+
ctx.other_bot_present,
262270
)
263271
.await;
264272

@@ -282,7 +290,7 @@ impl AdapterRouter {
282290

283291
if let Err(ref e) = result {
284292
let _ = adapter
285-
.send_message(thread_channel, &format!("⚠️ {e}"))
293+
.send_message(&ctx.thread_channel, &format!("⚠️ {e}"))
286294
.await;
287295
}
288296

src/cron.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,14 @@ async fn fire_cronjob(
361361
};
362362

363363
if let Err(e) = router
364-
.handle_message(&adapter, &reply_channel, &sender_json, &job.message, vec![], &trigger_msg, false)
364+
.handle_message(&adapter, crate::adapter::MessageContext {
365+
thread_channel: reply_channel.clone(),
366+
sender_json,
367+
prompt: job.message.clone(),
368+
extra_blocks: vec![],
369+
trigger_msg,
370+
other_bot_present: false,
371+
})
365372
.await
366373
{
367374
error!("cron handle_message error: {e}");

0 commit comments

Comments
 (0)