use crate::ui::openai;
use crate::ui::state::AppState;
use axum::Json;
use axum::extract::State;
use axum::http::StatusCode;
use axum::response::IntoResponse;
use serde::Deserialize;
use serde_json::{Value, json};
use std::sync::Arc;
#[derive(Deserialize)]
pub struct ChatBody {
pub messages: Vec<Value>,
}
pub async fn post_chat(
State(state): State<Arc<AppState>>,
Json(body): Json<ChatBody>,
) -> impl IntoResponse {
match openai::chat(
&state.openai_api_key,
&state.model,
&state.system_prompt,
&body.messages,
)
.await
{
Ok(content) => Json(json!({ "content": content })).into_response(),
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({ "error": e.to_string() })),
)
.into_response(),
}
}