You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.5 KiB
55 lines
1.5 KiB
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};
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct GenerationPayload {
|
|
ai: String,
|
|
params: Params,
|
|
}
|
|
|
|
pub async fn generate(Json(payload): Json<GenerationPayload>) -> Result<String, AppError> {
|
|
let ai = CONFIG
|
|
.ai
|
|
.iter()
|
|
.filter(|ai| ai.name == payload.ai)
|
|
.next()
|
|
.context(format!("Failed to get AI with name {}!", payload.ai))?
|
|
.clone();
|
|
|
|
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())
|
|
}
|
|
|
|
pub async fn get_ais() -> Result<Json<serde_json::Value>, AppError> {
|
|
let ais: Vec<String> = CONFIG.ai.iter().map(|ai| ai.name.clone()).collect();
|
|
|
|
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)?))
|
|
}
|