Skip to content

Commit

Permalink
fix(ai): history fix (#423)
Browse files Browse the repository at this point in the history
* do not record messages without a parent message.

* removed debug logs, clippy fixes

* refactored for clean happy path

* removed dead code

* move history record logic into message record handling

* revert user -> user_id and don't updade label ts

---------

Co-authored-by: Florian Dieminger <me@fiji-flo.de>
  • Loading branch information
argl and fiji-flo authored Feb 23, 2024
1 parent e6874e6 commit 83f95a5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
11 changes: 5 additions & 6 deletions src/api/ai_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ use crate::{
db::{
self,
ai_help::{
add_help_history, add_help_history_message, create_or_increment_total,
delete_full_help_history, delete_help_history, get_count, help_history,
help_history_get_message, list_help_history, update_help_history_label, AI_HELP_LIMIT,
add_help_history_message, create_or_increment_total, delete_full_help_history,
delete_help_history, get_count, help_history, help_history_get_message,
list_help_history, update_help_history_label, AI_HELP_LIMIT,
},
model::{AIHelpHistoryMessage, AIHelpHistoryMessageInsert, Settings},
settings::get_settings,
Expand Down Expand Up @@ -207,9 +207,7 @@ fn record_question(
message_id,
parent_id,
} = help_ids;
if let Err(err) = add_help_history(&mut conn, user_id, chat_id) {
error!("AI Help log: {err}");
}

let insert = AIHelpHistoryMessageInsert {
user_id,
chat_id,
Expand Down Expand Up @@ -245,6 +243,7 @@ fn record_sources(
message_id,
parent_id,
} = help_ids;

let insert = AIHelpHistoryMessageInsert {
user_id,
chat_id,
Expand Down
58 changes: 45 additions & 13 deletions src/db/ai_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,31 +132,63 @@ pub fn add_help_history(
.do_update()
.set(ai_help_history::updated_at.eq(diesel::dsl::now))
.execute(conn)?;

Ok(())
}

pub fn add_help_history_message(
pub fn update_help_history(
conn: &mut PgConnection,
mut message: AIHelpHistoryMessageInsert,
) -> Result<NaiveDateTime, DbError> {
let updated_at = update(ai_help_history::table)
user_id: i64,
chat_id: Uuid,
) -> Result<(), DbError> {
update(ai_help_history::table)
.filter(
ai_help_history::user_id
.eq(message.user_id)
.and(ai_help_history::chat_id.eq(message.chat_id)),
.eq(user_id)
.and(ai_help_history::chat_id.eq(chat_id)),
)
.set(ai_help_history::updated_at.eq(diesel::dsl::now))
.returning(ai_help_history::updated_at)
.get_result::<NaiveDateTime>(conn)?;
message.created_at = Some(updated_at);
insert_into(ai_help_history_messages::table)
.execute(conn)?;
Ok(())
}

pub fn add_help_history_message(
conn: &mut PgConnection,
message: AIHelpHistoryMessageInsert,
) -> Result<NaiveDateTime, DbError> {
// If a `parent_id` is present, we execute an update on the help history
// record because one of these are true:
// * We created a history record at the beginning of the conversation.
// * History was switched off, we did not create a record and the update
// will simply not match/change any record.
//
// With no `parent_id`, we create a new record because at this point, history
// _is_ enabled and we are at the start of a new conversation.
let res = if message.parent_id.is_some() {
update_help_history(conn, message.user_id, message.chat_id)
} else {
add_help_history(conn, message.user_id, message.chat_id)
};
if let Err(err) = res {
error!("AI Help log: {err}");
}

let res = insert_into(ai_help_history_messages::table)
.values(&message)
.on_conflict(ai_help_history_messages::message_id)
.do_update()
.set(&message)
.execute(conn)?;
Ok(updated_at)
.returning(ai_help_history_messages::created_at)
.get_result::<NaiveDateTime>(conn);
match res {
Ok(created_at) => Ok(created_at),
// Ignore foreign key violations deliberately
// because of the edge cases described above
Err(diesel::result::Error::DatabaseError(
diesel::result::DatabaseErrorKind::ForeignKeyViolation,
_info,
)) => Ok(Utc::now().naive_utc()),
Err(e) => Err(e.into()),
}
}

pub fn help_history_get_message(
Expand Down

0 comments on commit 83f95a5

Please sign in to comment.