add history to backend

master
Tymon 1 year ago
parent 1a0512bc7c
commit 7bdcce22a8

25
Cargo.lock generated

@ -165,6 +165,7 @@ dependencies = [
"ai_core",
"anyhow",
"axum",
"chrono",
"once_cell",
"ron 0.8.0",
"serde",
@ -623,6 +624,18 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "frontend"
version = "0.1.0"
dependencies = [
"ai_core",
"gloo-net",
"leptos",
"serde_json",
"tracing",
"tracing-wasm",
]
[[package]]
name = "fs"
version = "0.0.5"
@ -795,18 +808,6 @@ dependencies = [
"web-sys",
]
[[package]]
name = "gui_web"
version = "0.1.0"
dependencies = [
"ai_core",
"gloo-net",
"leptos",
"serde_json",
"tracing",
"tracing-wasm",
]
[[package]]
name = "h2"
version = "0.3.18"

@ -16,3 +16,4 @@ ron = "0.8.0"
once_cell = "1.17.1"
serde = { version = "1.0.160", features = ["derive"] }
anyhow = "1.0.70"
chrono = "0.4.24"

@ -23,6 +23,7 @@ async fn main() {
let addr: SocketAddr = "0.0.0.0:3000".parse().unwrap();
let app = Router::new()
.route("/generate", post(routes::generate))
.route("/history", get(routes::get_history))
.route("/ais", get(routes::get_ais));
tracing::info!("Listening on {}", addr);

@ -1,6 +1,10 @@
use ai_core::ai::{generate as ai_generate, Params};
use ai_core::{
ai::{generate as ai_generate, Params},
history::{write_history, HistoryEntry, read_all_history},
};
use anyhow::Context;
use axum::Json;
use chrono::Local;
use serde::Deserialize;
use crate::{utils::AppError, CONFIG};
@ -20,7 +24,19 @@ pub async fn generate(Json(payload): Json<GenerationPayload>) -> Result<String,
.context(format!("Failed to get AI with name {}!", payload.ai))?
.clone();
let res = ai_generate(ai, payload.params).await?;
let res = ai_generate(ai.clone(), payload.params.clone()).await?;
if let Some(history_dir) = CONFIG.app.history_dir.clone() {
write_history(
HistoryEntry {
date: Local::now(),
selected_ai: ai.name,
params: payload.params,
result: res.clone(),
},
history_dir,
)?;
}
Ok(res.trim().to_string())
}
@ -30,3 +46,9 @@ pub async fn get_ais() -> Result<Json<serde_json::Value>, AppError> {
Ok(Json(serde_json::to_value(ais)?))
}
pub async fn get_history() -> Result<Json<Vec<HistoryEntry>>, AppError> {
let history_dir = CONFIG.app.history_dir.clone().context("No history dir has been specified!")?;
Ok(Json(read_all_history(history_dir)?))
}

@ -1,5 +1,5 @@
[package]
name = "gui_web"
name = "frontend"
version = "0.1.0"
edition = "2021"