chat history added to context and patching text content

This commit is contained in:
Per Stark
2025-03-16 21:24:24 +01:00
parent bbc1cbc302
commit bc5c4b32b6
13 changed files with 141 additions and 166 deletions

View File

@@ -35,3 +35,28 @@ impl Message {
}
}
}
impl fmt::Display for MessageRole {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MessageRole::User => write!(f, "User"),
MessageRole::AI => write!(f, "AI"),
MessageRole::System => write!(f, "System"),
}
}
}
impl fmt::Display for Message {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.role, self.content)
}
}
// helper function to format a vector of messages
pub fn format_history(history: &[Message]) -> String {
history
.iter()
.map(|msg| format!("{}", msg))
.collect::<Vec<String>>()
.join("\n")
}

View File

@@ -1,6 +1,7 @@
use surrealdb::opt::PatchOp;
use uuid::Uuid;
use crate::stored_object;
use crate::{error::AppError, storage::db::SurrealDbClient, stored_object};
use super::file_info::FileInfo;
@@ -35,4 +36,24 @@ impl TextContent {
user_id,
}
}
pub async fn patch(
id: &str,
instructions: &str,
category: &str,
text: &str,
db: &SurrealDbClient,
) -> Result<(), AppError> {
let now = Utc::now();
let _res: Option<Self> = db
.update((Self::table_name(), id))
.patch(PatchOp::replace("/instructions", instructions))
.patch(PatchOp::replace("/category", category))
.patch(PatchOp::replace("/text", text))
.patch(PatchOp::replace("/updated_at", now))
.await?;
Ok(())
}
}