Improve the chat workspace REST endpoints

This commit is contained in:
Kerollmops 2025-06-10 10:21:34 +02:00
parent bc56087a17
commit e654eddf56
No known key found for this signature in database
GPG key ID: F250A4C4E3AE5F5F
5 changed files with 72 additions and 19 deletions

View file

@ -6,9 +6,11 @@ use index_scheduler::IndexScheduler;
use meilisearch_types::deserr::query_params::Param;
use meilisearch_types::deserr::DeserrQueryParamError;
use meilisearch_types::error::deserr_codes::{InvalidIndexLimit, InvalidIndexOffset};
use meilisearch_types::error::ResponseError;
use meilisearch_types::error::{Code, ResponseError};
use meilisearch_types::index_uid::IndexUid;
use meilisearch_types::keys::actions;
use serde::{Deserialize, Serialize};
use serde_json::json;
use tracing::debug;
use utoipa::{IntoParams, ToSchema};
@ -40,11 +42,52 @@ pub struct ChatsParam {
pub fn configure(cfg: &mut web::ServiceConfig) {
cfg.service(web::resource("").route(web::get().to(list_workspaces))).service(
web::scope("/{workspace_uid}")
.service(
web::resource("")
.route(web::get().to(get_chat))
.route(web::delete().to(delete_chat)),
)
.service(web::scope("/chat/completions").configure(chat_completions::configure))
.service(web::scope("/settings").configure(settings::configure)),
);
}
pub async fn get_chat(
index_scheduler: GuardedData<ActionPolicy<{ actions::CHATS_GET }>, Data<IndexScheduler>>,
workspace_uid: web::Path<String>,
) -> Result<HttpResponse, ResponseError> {
index_scheduler.features().check_chat_completions("displaying a chat")?;
let workspace_uid = IndexUid::try_from(workspace_uid.into_inner())?;
if index_scheduler.chat_workspace_exists(&workspace_uid)? {
Ok(HttpResponse::Ok().json(json!({ "uid": workspace_uid })))
} else {
Err(ResponseError::from_msg(
format!("chat {workspace_uid} not found"),
Code::ChatWorkspaceNotFound,
))
}
}
pub async fn delete_chat(
index_scheduler: GuardedData<ActionPolicy<{ actions::CHATS_DELETE }>, Data<IndexScheduler>>,
workspace_uid: web::Path<String>,
) -> Result<HttpResponse, ResponseError> {
index_scheduler.features().check_chat_completions("deleting a chat")?;
let mut wtxn = index_scheduler.write_txn()?;
let workspace_uid = workspace_uid.into_inner();
if index_scheduler.delete_chat_settings(&mut wtxn, &workspace_uid)? {
wtxn.commit()?;
Ok(HttpResponse::NoContent().finish())
} else {
Err(ResponseError::from_msg(
format!("chat {workspace_uid} not found"),
Code::ChatWorkspaceNotFound,
))
}
}
#[derive(Deserr, Debug, Clone, Copy, IntoParams)]
#[deserr(error = DeserrQueryParamError, rename_all = camelCase, deny_unknown_fields)]
#[into_params(rename_all = "camelCase", parameter_in = Query)]

View file

@ -26,7 +26,7 @@ pub fn configure(cfg: &mut web::ServiceConfig) {
web::resource("")
.route(web::get().to(SeqHandler(get_settings)))
.route(web::patch().to(SeqHandler(patch_settings)))
.route(web::delete().to(SeqHandler(delete_settings))),
.route(web::delete().to(SeqHandler(reset_settings))),
);
}
@ -159,9 +159,9 @@ async fn patch_settings(
Ok(HttpResponse::Ok().json(settings))
}
async fn delete_settings(
async fn reset_settings(
index_scheduler: GuardedData<
ActionPolicy<{ actions::CHATS_SETTINGS_DELETE }>,
ActionPolicy<{ actions::CHATS_SETTINGS_UPDATE }>,
Data<IndexScheduler>,
>,
chats_param: web::Path<ChatsParam>,
@ -169,12 +169,12 @@ async fn delete_settings(
index_scheduler.features().check_chat_completions("using the /chats/settings route")?;
let ChatsParam { workspace_uid } = chats_param.into_inner();
// TODO do a spawn_blocking here
let mut wtxn = index_scheduler.write_txn()?;
if index_scheduler.delete_chat_settings(&mut wtxn, &workspace_uid)? {
if index_scheduler.chat_settings(&wtxn, &workspace_uid)?.is_some() {
let settings = Default::default();
index_scheduler.put_chat_settings(&mut wtxn, &workspace_uid, &settings)?;
wtxn.commit()?;
Ok(HttpResponse::NoContent().finish())
Ok(HttpResponse::Ok().json(settings))
} else {
Err(ResponseError::from_msg(
format!("Chat `{workspace_uid}` not found"),