Skip to content

Commit

Permalink
Implement chat history support
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Jul 20, 2024
1 parent 2aedbd2 commit a6a9f19
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 58 deletions.
Binary file modified fonts/icebreaker-icons.ttf
Binary file not shown.
2 changes: 1 addition & 1 deletion src/data/assistant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ impl fmt::Display for Likes {
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct File {
pub model: Id,
pub name: String,
Expand Down
30 changes: 18 additions & 12 deletions src/data/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,13 @@ impl Chat {
Ok(storage_dir().await?.join(format!("{}.json", id.0.simple())))
}

#[allow(dead_code)]
pub async fn list() -> Result<Vec<Entry>, Error> {
let list = List::fetch().await?;

Ok(list.entries)
}

pub async fn fetch_last_opened() -> Result<Self, Error> {
let LastOpened(id) = LastOpened::fetch().await?;

pub async fn fetch(id: Id) -> Result<Self, Error> {
let bytes = fs::read(Self::path(&id).await?).await?;
let schema: Schema = task::spawn_blocking(move || serde_json::from_slice(&bytes)).await??;

Expand All @@ -52,6 +49,12 @@ impl Chat {
})
}

pub async fn fetch_last_opened() -> Result<Self, Error> {
let LastOpened(id) = LastOpened::fetch().await?;

Self::fetch(id).await
}

pub async fn create(
file: assistant::File,
title: Option<String>,
Expand Down Expand Up @@ -189,10 +192,10 @@ pub fn send(
})
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Id(Uuid);

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Entry {
pub id: Id,
pub file: assistant::File,
Expand All @@ -212,19 +215,22 @@ impl List {
async fn fetch() -> Result<Self, Error> {
let path = Self::path().await?;

let list: Self = {
let bytes = fs::read(&path).await?;
let bytes = fs::read(&path).await;

task::spawn_blocking(move || serde_json::from_slice(&bytes).ok()).await?
}
.unwrap_or_default();
let Ok(bytes) = bytes else {
return Ok(List::default());
};

let list: Self =
{ task::spawn_blocking(move || serde_json::from_slice(&bytes).ok()).await? }
.unwrap_or_default();

Ok(list)
}

async fn push(entry: Entry) -> Result<(), Error> {
let mut list = Self::fetch().await.unwrap_or_default();
list.entries.push(entry);
list.entries.insert(0, entry);

let json = task::spawn_blocking(move || serde_json::to_vec_pretty(&list)).await?;

Expand Down
8 changes: 8 additions & 0 deletions src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ pub fn clipboard<'a>() -> Text<'a> {
with_codepoint('\u{E805}')
}

pub fn collapse<'a>() -> Text<'a> {
with_codepoint('\u{E806}')
}

pub fn expand<'a>() -> Text<'a> {
with_codepoint('\u{E807}')
}

fn with_codepoint<'a>(codepoint: char) -> Text<'a> {
const FONT: Font = Font::with_name("icebreaker-icons");

Expand Down
Loading

0 comments on commit a6a9f19

Please sign in to comment.