Files
imphnen-backend-service/imphnen-dimentorin/src/ai_agent/infrastructure/http/handlers.rs
T

54 lines
1.4 KiB
Rust
Raw Normal View History

use super::dto::{ChatRequestDto, IndexResultDto};
use crate::ai_agent::domain::{ChatRequest, RagService};
use axum::{
Extension, extract::Path,
http::HeaderMap, response::IntoResponse,
};
use imphnen_libs::{ValidatedJson, decode_access_token};
use imphnen_utils::{ApiSuccess, AppError};
use std::sync::Arc;
use uuid::Uuid;
pub async fn post_chat(
_headers: HeaderMap,
Extension(service): Extension<Arc<dyn RagService>>,
ValidatedJson(body): ValidatedJson<ChatRequestDto>,
) -> Result<impl IntoResponse, AppError> {
let material_id = match body.material_id {
Some(m) => Some(Uuid::parse_str(&m).map_err(|_| {
AppError::BadRequestError("material_id tidak valid".into())
})?),
None => None,
};
let result = service
.chat(ChatRequest {
question: body.question,
material_id,
})
.await?;
Ok(ApiSuccess(result))
}
pub async fn post_index_material(
_headers: HeaderMap,
Extension(service): Extension<Arc<dyn RagService>>,
Path(id): Path<Uuid>,
) -> Result<impl IntoResponse, AppError> {
let indexed = service.index_material(id).await?;
Ok(ApiSuccess(IndexResultDto {
material_id: id,
chunks_indexed: indexed,
}))
}
pub async fn post_reindex_all(
_headers: HeaderMap,
Extension(service): Extension<Arc<dyn RagService>>,
) -> Result<impl IntoResponse, AppError> {
let indexed = service.reindex_all().await?;
Ok(ApiSuccess(IndexResultDto {
material_id: Uuid::nil(),
chunks_indexed: indexed,
}))
}