print history each time when running
This commit is contained in:
parent
bab5cdaf2e
commit
a432cddf79
|
@ -155,9 +155,10 @@ pub async fn get_res<'a>(cli: &Client, query: String, vqd: String, config: &Conf
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
println!("\n");
|
||||||
|
|
||||||
if let Some(err) = error {
|
if let Some(err) = error {
|
||||||
eprintln!("Error while writing to session: {err:#?}");
|
eprintln!("Error while writing to session: {err:#?}");
|
||||||
eprintln!("Session may be broken.");
|
eprintln!("Session may be broken.");
|
||||||
}
|
}
|
||||||
println!("\n");
|
|
||||||
}
|
}
|
|
@ -42,6 +42,13 @@ async fn main() {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
let query = args.query.join(" ").trim().to_string();
|
let query = args.query.join(" ").trim().to_string();
|
||||||
|
|
||||||
|
{
|
||||||
|
let session = Session::create_or_restore("");
|
||||||
|
if session.is_restored() {
|
||||||
|
session.print_history();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if query.len() == 0 {
|
if query.len() == 0 {
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,13 +7,17 @@ use chrono::{DateTime, Local};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::api::{ChatChunk, ChatMessagePayload};
|
use crate::api::{ChatChunk, ChatMessagePayload};
|
||||||
|
use crate::{BLUE, GRAY, GREEN, RESET};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct Session {
|
pub struct Session {
|
||||||
session_id: String,
|
session_id: String,
|
||||||
ttl: DateTime<Local>,
|
ttl: DateTime<Local>,
|
||||||
messages: Vec<ChatMessagePayload>,
|
messages: Vec<ChatMessagePayload>,
|
||||||
vqd: String
|
vqd: String,
|
||||||
|
|
||||||
|
#[serde(skip)]
|
||||||
|
restored: bool
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Session {
|
impl Session {
|
||||||
|
@ -68,6 +72,7 @@ impl Session {
|
||||||
|
|
||||||
match inner(Self::path_for_id(id)) {
|
match inner(Self::path_for_id(id)) {
|
||||||
Ok(mut session) => {
|
Ok(mut session) => {
|
||||||
|
session.restored = true;
|
||||||
if session.is_expired() {
|
if session.is_expired() {
|
||||||
session.destroy().expect("Couldn't destroy expired session");
|
session.destroy().expect("Couldn't destroy expired session");
|
||||||
None
|
None
|
||||||
|
@ -94,11 +99,16 @@ impl Session {
|
||||||
session_id: id.into(),
|
session_id: id.into(),
|
||||||
ttl: Self::get_ttl(),
|
ttl: Self::get_ttl(),
|
||||||
messages: vec![],
|
messages: vec![],
|
||||||
vqd: vqd.into()
|
vqd: vqd.into(),
|
||||||
|
|
||||||
|
restored: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn save(&self) -> Result<(), Box<dyn std::error::Error>> {
|
fn save(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
if self.messages.len() == 0 {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
fs::write(self.path(), serde_json::to_string_pretty(self)?)?;
|
fs::write(self.path(), serde_json::to_string_pretty(self)?)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -107,6 +117,40 @@ impl Session {
|
||||||
self.ttl < Local::now()
|
self.ttl < Local::now()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_restored(&self) -> bool {
|
||||||
|
self.restored
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn print_history(&self) {
|
||||||
|
println!("{GRAY}* start restored conversation *{RESET}");
|
||||||
|
println!();
|
||||||
|
|
||||||
|
for message in self.messages.iter() {
|
||||||
|
let user = &message.role;
|
||||||
|
let msg = &message.content;
|
||||||
|
|
||||||
|
let role_color = {
|
||||||
|
if user == "user" { GREEN }
|
||||||
|
else if user == "assistant" { BLUE }
|
||||||
|
else { GRAY }
|
||||||
|
};
|
||||||
|
|
||||||
|
let short_user = {
|
||||||
|
if user == "user" { "you" }
|
||||||
|
else if user == "assistant" { "ai" }
|
||||||
|
else { user }
|
||||||
|
}.to_string();
|
||||||
|
|
||||||
|
println!("{role_color}{short_user}{RESET}\t: {msg}{RESET}");
|
||||||
|
|
||||||
|
if user == "assistant" {
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{GRAY}* end restored conversation *{RESET}");
|
||||||
|
}
|
||||||
|
|
||||||
pub fn destroy(&self) -> Result<(), Box<dyn std::error::Error>> {
|
pub fn destroy(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
Ok(fs::remove_file(self.path())?)
|
Ok(fs::remove_file(self.path())?)
|
||||||
}
|
}
|
||||||
|
@ -114,7 +158,7 @@ impl Session {
|
||||||
pub fn create_or_restore<T: Into<String>>(vqd: T) -> Self {
|
pub fn create_or_restore<T: Into<String>>(vqd: T) -> Self {
|
||||||
let session_id: String = Self::terminal_session_id();
|
let session_id: String = Self::terminal_session_id();
|
||||||
match Self::restore_with_id(&session_id) {
|
match Self::restore_with_id(&session_id) {
|
||||||
Some(session) => session,
|
Some(session) => { session },
|
||||||
None => {
|
None => {
|
||||||
let session = Self::new(&session_id, vqd);
|
let session = Self::new(&session_id, vqd);
|
||||||
session.save().expect("Couldn't save new session");
|
session.save().expect("Couldn't save new session");
|
||||||
|
|
Loading…
Reference in New Issue