use aion_proto::WireError;
use axum::{
Json,
extract::State,
http::{StatusCode, header},
response::{IntoResponse, Response},
};
use super::auth::HttpCaller;
use super::error::HttpWireError;
use crate::ServerState;
use crate::assistant::{AssistantDescriptor, describe, embedded_assistant};
pub(crate) async fn assistant_descriptor(
State(state): State<ServerState>,
HttpCaller(_caller): HttpCaller,
) -> Result<Json<AssistantDescriptor>, HttpWireError> {
let embedded = embedded_assistant().map_err(|error| {
HttpWireError(WireError::backend(format!(
"this server carries no usable built-in assistant: {error}"
)))
})?;
let engine = state.engine().map_err(|error| {
HttpWireError(
WireError::backend(format!(
"the engine is not available to describe the built-in assistant: {error}"
))
.with_error_type("Engine"),
)
})?;
Ok(Json(describe(embedded, engine.as_ref())))
}
pub(crate) async fn assistant_document(HttpCaller(_caller): HttpCaller) -> Response {
match embedded_assistant() {
Ok(embedded) => (
[(
header::CONTENT_TYPE,
axum::http::HeaderValue::from_static("text/plain; charset=utf-8"),
)],
embedded.source(),
)
.into_response(),
Err(error) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(WireError::backend(format!(
"this server carries no usable built-in assistant: {error}"
))),
)
.into_response(),
}
}
#[cfg(test)]
#[path = "assistant_tests.rs"]
mod assistant_tests;