Skip to content

Commit

Permalink
Rework typing notifications
Browse files Browse the repository at this point in the history
Previously, the bot only had rudimentary typing notification support.

It used to send a single notification when starting a long task
and did not bother with notifications anymore.
By default matrix-rust-sdk gives these notifications a validity of 4
seconds, so it would expire shortly. If the bot takes longer to respond,
you'd see the typing notification expire and wonder if a response is
coming.

Another edge case is the bot sending an answer quicker and the typing
notice still being on. Some clients (like element-web) seem to hide the
typing notice when a new message comes, so they don't experience this as
problematic.

The reworked typing notification system should be robust:

- typing notices are sent continuously, until the bot finishes doing
  work
- if the bot is performing multiple actions in a room (even for
  different people), typing notices would continue to be sent until the
  bot becomes idle
- as soon as the bot becomes idle, a "not typing anymore" notice is sent
  to clear the state
  • Loading branch information
spantaleev committed Sep 13, 2024
1 parent f2b1115 commit dd1dd78
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ base64 = "0.22.*"
# We'd rather not depend on this, but we cannot use the ruma-events EventContent macro without it.
matrix-sdk = { version = "0.7.1", default-features = false }
mxidwc = "1.0.*"
mxlink = "1.1.*"
mxlink = "1.2.*"
etke_openai_api_rust = "0.1.*"
quick_cache = "0.6.*"
regex = "1.10.*"
Expand Down
7 changes: 7 additions & 0 deletions src/bot/messaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,13 @@ impl Messaging {

let start_time = std::time::Instant::now();

let _typing_notice_guard = self
.bot
.matrix_link()
.rooms()
.start_typing_notice(message_context.room())
.await;

let event_span = tracing::error_span!("message_controller", ?controller_type);

crate::controller::dispatch_controller(&controller_type, &message_context, &self.bot)
Expand Down
7 changes: 7 additions & 0 deletions src/bot/reacting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ impl Reacting {

tracing::info!("Handling reaction via reaction controller");

let _typing_notice_guard = self
.bot
.matrix_link()
.rooms()
.start_typing_notice(message_context.room())
.await;

let event_span = tracing::error_span!("reaction_controller");

crate::controller::reaction::handle(
Expand Down
4 changes: 0 additions & 4 deletions src/controller/agent/create/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ pub async fn handle_room_local(
return Ok(());
};

message_context.room().typing_notice(true).await?;

if !try_to_ping_agent_or_complain(bot, message_context, &parsed_config.agent).await {
return Ok(());
}
Expand Down Expand Up @@ -215,8 +213,6 @@ pub async fn handle_global(
return Ok(());
};

message_context.room().typing_notice(true).await?;

if !try_to_ping_agent_or_complain(bot, message_context, &parsed_config.agent).await {
return Ok(());
}
Expand Down
4 changes: 0 additions & 4 deletions src/controller/chat_completion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,6 @@ async fn handle_stage_text_generation(
)
.await?;

_ = message_context.room().typing_notice(true).await;

let prefixes_to_strip = match controller_type {
ChatCompletionControllerType::ViaText { prefixes_to_strip } => prefixes_to_strip.clone(),
ChatCompletionControllerType::ViaAudio => vec![],
Expand Down Expand Up @@ -498,8 +496,6 @@ async fn handle_stage_speech_to_text_actual_transcribing(
.get_media_content(&media_request, true)
.await?;

_ = message_context.room().typing_notice(true).await;

let span = tracing::debug_span!(
"speech_to_text_generation",
agent_id = agent.identifier().as_string()
Expand Down
4 changes: 0 additions & 4 deletions src/controller/image/generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ pub async fn handle_image(
original_prompt.to_owned()
};

message_context.room().typing_notice(true).await?;

let span = tracing::debug_span!(
"image_generation",
agent_id = agent.identifier().as_string()
Expand Down Expand Up @@ -139,8 +137,6 @@ pub async fn handle_sticker(
return Ok(());
};

message_context.room().typing_notice(true).await?;

let span = tracing::debug_span!(
"sticker_generation",
agent_id = agent.identifier().as_string()
Expand Down
2 changes: 0 additions & 2 deletions src/controller/utils/text_to_speech.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ pub async fn generate_and_send_tts_for_message(
text_message_event_id: &OwnedEventId,
text_content: &str,
) -> bool {
_ = message_context.room().typing_notice(true).await;

let reaction_event_response = bot
.reacting()
.react_no_fail(
Expand Down

0 comments on commit dd1dd78

Please sign in to comment.