Skip to content

Commit c985ee4

Browse files
committed
Add chat workspace support and tests
1 parent 02993ad commit c985ee4

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

src/chats.rs

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::BTreeMap;
22

33
use serde::{Deserialize, Serialize};
4-
use serde_json::Value;
4+
use serde_json::{to_vec, Value};
55

66
use crate::{
77
client::Client,
@@ -275,13 +275,34 @@ impl Client<crate::reqwest::ReqwestClient> {
275275
uid: impl AsRef<str>,
276276
body: &S,
277277
) -> Result<reqwest::Response, Error> {
278-
use reqwest::header::{HeaderValue, ACCEPT, CONTENT_TYPE};
278+
use reqwest::{
279+
header::{self, HeaderValue, ACCEPT, CONTENT_TYPE},
280+
Client as HttpClient,
281+
};
282+
283+
let payload = to_vec(body).map_err(Error::ParseError)?;
284+
285+
let mut headers = header::HeaderMap::new();
286+
#[cfg(not(target_arch = "wasm32"))]
287+
headers.insert(
288+
header::USER_AGENT,
289+
header::HeaderValue::from_str(&crate::reqwest::qualified_version()).unwrap(),
290+
);
291+
#[cfg(target_arch = "wasm32")]
292+
headers.insert(
293+
header::HeaderName::from_static("x-meilisearch-client"),
294+
header::HeaderValue::from_str(&crate::reqwest::qualified_version()).unwrap(),
295+
);
296+
if let Some(api_key) = self.api_key.as_deref() {
297+
headers.insert(
298+
header::AUTHORIZATION,
299+
header::HeaderValue::from_str(&format!("Bearer {api_key}")).unwrap(),
300+
);
301+
}
279302

280-
let payload = serde_json::to_vec(body).map_err(Error::ParseError)?;
303+
let http = HttpClient::builder().default_headers(headers).build()?;
281304

282-
let response = self
283-
.http_client
284-
.inner()
305+
let response = http
285306
.post(format!(
286307
"{}/chats/{}/chat/completions",
287308
self.host,
@@ -315,14 +336,22 @@ impl Client<crate::reqwest::ReqwestClient> {
315336
#[cfg(test)]
316337
mod tests {
317338
use super::*;
318-
use crate::features::ExperimentalFeatures;
319339
use meilisearch_test_macro::meilisearch_test;
340+
use serde_json::json;
320341

321342
#[meilisearch_test]
322343
async fn chat_workspace_lifecycle(client: Client, name: String) -> Result<(), Error> {
323-
let mut features = ExperimentalFeatures::new(&client);
324-
features.set_chat_completions(true);
325-
let _ = features.update().await?;
344+
let _: serde_json::Value = client
345+
.http_client
346+
.request(
347+
&format!("{}/experimental-features", client.host),
348+
Method::Patch {
349+
query: (),
350+
body: &json!({ "chatCompletions": true }),
351+
},
352+
200,
353+
)
354+
.await?;
326355

327356
let workspace = format!("{name}-workspace");
328357

src/features.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ pub struct ExperimentalFeaturesResult {
1414
pub contains_filter: bool,
1515
pub network: bool,
1616
pub edit_documents_by_function: bool,
17-
#[serde(default)]
18-
pub chat_completions: bool,
1917
}
2018

2119
/// Struct representing the experimental features request.
@@ -47,8 +45,6 @@ pub struct ExperimentalFeatures<'a, Http: HttpClient> {
4745
pub network: Option<bool>,
4846
#[serde(skip_serializing_if = "Option::is_none")]
4947
pub edit_documents_by_function: Option<bool>,
50-
#[serde(skip_serializing_if = "Option::is_none")]
51-
pub chat_completions: Option<bool>,
5248
}
5349

5450
impl<'a, Http: HttpClient> ExperimentalFeatures<'a, Http> {
@@ -61,7 +57,6 @@ impl<'a, Http: HttpClient> ExperimentalFeatures<'a, Http> {
6157
network: None,
6258
contains_filter: None,
6359
edit_documents_by_function: None,
64-
chat_completions: None,
6560
}
6661
}
6762

@@ -145,11 +140,6 @@ impl<'a, Http: HttpClient> ExperimentalFeatures<'a, Http> {
145140
self.network = Some(network);
146141
self
147142
}
148-
149-
pub fn set_chat_completions(&mut self, chat_completions: bool) -> &mut Self {
150-
self.chat_completions = Some(chat_completions);
151-
self
152-
}
153143
}
154144

155145
#[cfg(test)]
@@ -165,7 +155,6 @@ mod tests {
165155
features.set_contains_filter(true);
166156
features.set_network(true);
167157
features.set_edit_documents_by_function(true);
168-
features.set_chat_completions(true);
169158
let _ = features.update().await.unwrap();
170159

171160
let res = features.get().await.unwrap();
@@ -174,6 +163,5 @@ mod tests {
174163
assert!(res.contains_filter);
175164
assert!(res.network);
176165
assert!(res.edit_documents_by_function);
177-
assert!(res.chat_completions);
178166
}
179167
}

src/reqwest.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ impl ReqwestClient {
4949

5050
Ok(ReqwestClient { client })
5151
}
52-
53-
pub(crate) fn inner(&self) -> &reqwest::Client {
54-
&self.client
55-
}
5652
}
5753

5854
#[cfg_attr(feature = "futures-unsend", async_trait(?Send))]

0 commit comments

Comments
 (0)