Allow for prompt caching to work by using baibot_conversation_start_time_utc instead of baibot_now_utc

This patch introduces a new `baibot_conversation_start_time_utc`
variable which indicates the time the conversation got started.

Using `baibot_now_utc` is still possible, but given that the current
time is a moving target, its use is in conflict with prompt caching.

Because the new `baibot_conversation_start_time_utc` prompt variable
is a more reasonable default, we're now using it in all sample configs.
This commit is contained in:
Slavi Pantaleev
2024-10-03 11:48:14 +03:00
parent db9422740c
commit 85e66406dc
25 changed files with 159 additions and 25 deletions

View File

@@ -1,3 +1,4 @@
use chrono::{DateTime, Utc};
use regex::Regex;
use mxlink::matrix_sdk::ruma::OwnedUserId;
@@ -8,6 +9,7 @@ pub struct MatrixMessage {
pub message_type: MatrixMessageType,
pub message_text: String,
pub mentioned_users: Vec<OwnedUserId>,
pub timestamp: DateTime<Utc>,
}
#[derive(Clone)]

View File

@@ -234,6 +234,13 @@ pub fn convert_matrix_native_event_to_matrix_message(
text
};
let timestamp = chrono::DateTime::<chrono::Utc>::from(
matrix_native_event
.origin_server_ts()
.to_system_time()
.unwrap_or_else(std::time::SystemTime::now),
);
let mentioned_users = room_message
.mentions
.map(|m| m.user_ids.iter().map(|u| u.to_owned()).collect())
@@ -248,6 +255,7 @@ pub fn convert_matrix_native_event_to_matrix_message(
},
message_text: text,
mentioned_users,
timestamp,
})
}

View File

@@ -1,3 +1,5 @@
use chrono::{TimeZone, Utc};
use mxlink::matrix_sdk::ruma::OwnedUserId;
use crate::conversation::matrix::{
@@ -11,11 +13,14 @@ fn is_message_from_allowed_sender() {
let allowed_user_id = OwnedUserId::try_from("@user.someone:example.com").unwrap();
let unallowed_user_id = OwnedUserId::try_from("@another:example.com").unwrap();
let timestamp = Utc.with_ymd_and_hms(2024, 9, 20, 18, 34, 15).unwrap();
let bot_message = MatrixMessage {
sender_id: bot_user_id.to_owned(),
message_type: MatrixMessageType::Text,
message_text: "Hello!".to_owned(),
mentioned_users: vec![],
timestamp,
};
let allowed_user_message = MatrixMessage {
@@ -23,6 +28,7 @@ fn is_message_from_allowed_sender() {
message_type: MatrixMessageType::Text,
message_text: "Hello!".to_owned(),
mentioned_users: vec![],
timestamp,
};
let unallowed_user_message = MatrixMessage {
@@ -30,6 +36,7 @@ fn is_message_from_allowed_sender() {
message_type: MatrixMessageType::Text,
message_text: "Hello!".to_owned(),
mentioned_users: vec![],
timestamp,
};
let parsed_regex = match mxidwc::parse_pattern("@user.*:example.com") {
@@ -77,11 +84,14 @@ async fn process_matrix_messages() {
let allowed_user_id = OwnedUserId::try_from("@user.someone:example.com").unwrap();
let unallowed_user_id = OwnedUserId::try_from("@another:example.com").unwrap();
let timestamp = Utc.with_ymd_and_hms(2024, 9, 20, 18, 34, 15).unwrap();
let allowed_user_message = MatrixMessage {
sender_id: allowed_user_id.to_owned(),
message_type: MatrixMessageType::Text,
message_text: "Hello from the user!".to_owned(),
mentioned_users: vec![],
timestamp,
};
let allowed_user_message_with_prefix = MatrixMessage {
@@ -89,6 +99,7 @@ async fn process_matrix_messages() {
message_type: MatrixMessageType::Text,
message_text: "!bai Hello from the user!".to_owned(),
mentioned_users: vec![],
timestamp,
};
let allowed_user_message_with_prefix_no_space = MatrixMessage {
@@ -96,6 +107,7 @@ async fn process_matrix_messages() {
message_type: MatrixMessageType::Text,
message_text: "!baiHello from the user!".to_owned(),
mentioned_users: vec![],
timestamp,
};
let allowed_user_message_with_prefix_full_width_space = MatrixMessage {
@@ -103,6 +115,7 @@ async fn process_matrix_messages() {
message_type: MatrixMessageType::Text,
message_text: "!bai Hello from the user!".to_owned(),
mentioned_users: vec![],
timestamp,
};
let bot_message = MatrixMessage {
@@ -110,6 +123,7 @@ async fn process_matrix_messages() {
message_type: MatrixMessageType::Text,
message_text: "Hello from the bot!".to_owned(),
mentioned_users: vec![],
timestamp,
};
let allowed_user_message_with_bot_mention = MatrixMessage {
@@ -117,6 +131,7 @@ async fn process_matrix_messages() {
message_type: MatrixMessageType::Text,
message_text: "@baibot: Hello from the user!".to_owned(),
mentioned_users: vec![bot_user_id.to_owned()],
timestamp,
};
// The message text is the same as above - it mentions the bot, but the actually-mentioned user is another user.
@@ -125,6 +140,7 @@ async fn process_matrix_messages() {
message_type: MatrixMessageType::Text,
message_text: allowed_user_message_with_bot_mention.message_text.clone(),
mentioned_users: vec![allowed_user_id.to_owned()],
timestamp,
};
let unallowed_user_message = MatrixMessage {
@@ -132,6 +148,7 @@ async fn process_matrix_messages() {
message_type: MatrixMessageType::Text,
message_text: "Hello from an unallowed user!".to_owned(),
mentioned_users: vec![],
timestamp,
};
let parsed_regex = match mxidwc::parse_pattern("@user.*:example.com") {