Add SavedConversation::list() method

Co-Authored-By: Kyle Caverly <kyle@zed.dev>
This commit is contained in:
Nathan Sobo 2023-06-20 13:29:34 -06:00
parent 9f783944a7
commit 230b4d237e
3 changed files with 50 additions and 1 deletions

1
Cargo.lock generated
View File

@ -109,6 +109,7 @@ dependencies = [
"isahc",
"language",
"menu",
"regex",
"schemars",
"search",
"serde",

View File

@ -25,6 +25,7 @@ anyhow.workspace = true
chrono = "0.4"
futures.workspace = true
isahc.workspace = true
regex.workspace = true
schemars.workspace = true
serde.workspace = true
serde_json.workspace = true

View File

@ -1,10 +1,21 @@
pub mod assistant;
mod assistant_settings;
use anyhow::Result;
pub use assistant::AssistantPanel;
use chrono::{DateTime, Local};
use fs::Fs;
use futures::StreamExt;
use gpui::AppContext;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};
use std::{
fmt::{self, Display},
path::PathBuf,
sync::Arc,
time::SystemTime,
};
use util::paths::CONVERSATIONS_DIR;
// Data types for chat completion requests
#[derive(Debug, Serialize)]
@ -21,6 +32,42 @@ struct SavedConversation {
messages: Vec<RequestMessage>,
}
impl SavedConversation {
pub async fn list(fs: Arc<dyn Fs>) -> Result<Vec<SavedConversationMetadata>> {
let mut paths = fs.read_dir(&CONVERSATIONS_DIR).await?;
let mut conversations = Vec::<SavedConversationMetadata>::new();
while let Some(path) = paths.next().await {
let path = path?;
let pattern = r" - \d+.zed.json$";
let re = Regex::new(pattern).unwrap();
let metadata = fs.metadata(&path).await?;
if let Some((file_name, metadata)) = path
.file_name()
.and_then(|name| name.to_str())
.zip(metadata)
{
let title = re.replace(file_name, "");
conversations.push(SavedConversationMetadata {
title: title.into_owned(),
path,
mtime: metadata.mtime,
});
}
}
Ok(conversations)
}
}
struct SavedConversationMetadata {
title: String,
path: PathBuf,
mtime: SystemTime,
}
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
struct RequestMessage {
role: Role,