Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
use models_opensearch::SearchIndex;
use crate::{
Result,
error::{OpensearchClientError, OpensearchOperation},
};
/// Deletes all chat messages with the specified chat_id
#[tracing::instrument(skip(client))]
pub async fn delete_chat_by_id(
client: &opensearch::OpenSearch,
chat_id: &str,
index_override: Option<&str>,
) -> Result<()> {
let query = serde_json::json!({
"query": {
"term": {
"entity_id": chat_id
}
},
});
let index = index_override.unwrap_or(SearchIndex::Chats.as_ref());
let response = client
.delete_by_query(opensearch::DeleteByQueryParts::Index(&[index]))
.body(query)
.refresh(true) // Ensure the index reflects changes immediately
.send()
.await
.map_err(|err| {
OpensearchClientError::request_failed(OpensearchOperation::DeleteChatById, &err)
})?;
let status_code = response.status_code();
if !status_code.is_success() {
let body = response.text().await.map_err(|err| {
OpensearchClientError::deserialization_failed(OpensearchOperation::DeleteChatById, &err)
})?;
tracing::error!(
status_code = ?status_code,
body = ?body,
"error deleting chats by id"
);
return Err(OpensearchClientError::Unknown {
details: body,
operation: OpensearchOperation::DeleteChatById,
});
}
Ok(())
}
/// Deletes a particular chat message with the specified chat_id and chat_message_id
#[tracing::instrument(skip(client))]
pub async fn delete_chat_message_by_id(
client: &opensearch::OpenSearch,
chat_id: &str,
chat_message_id: &str,
index_override: Option<&str>,
) -> Result<()> {
let query = serde_json::json!({
"query": {
"bool": {
"must": [
{
"term": {
"entity_id": chat_id
}
},
{
"term": {
"chat_message_id": chat_message_id
}
}
]
}
}
});
let index = index_override.unwrap_or(SearchIndex::Chats.as_ref());
let response = client
.delete_by_query(opensearch::DeleteByQueryParts::Index(&[index]))
.body(query)
.refresh(true) // Ensure the index reflects changes immediately
.send()
.await
.map_err(|err| {
OpensearchClientError::request_failed(OpensearchOperation::DeleteChatMessageById, &err)
})?;
let status_code = response.status_code();
if !status_code.is_success() {
let body = response.text().await.map_err(|err| {
OpensearchClientError::deserialization_failed(
OpensearchOperation::DeleteChatMessageById,
&err,
)
})?;
tracing::error!(
status_code = ?status_code,
body = ?body,
"error deleting chats by id"
);
return Err(OpensearchClientError::Unknown {
details: body,
operation: OpensearchOperation::DeleteChatMessageById,
});
}
Ok(())
}
/// Deletes all chat messages with a specific user_id
#[tracing::instrument(skip(client))]
pub async fn delete_chat_by_user_id(client: &opensearch::OpenSearch, user_id: &str) -> Result<()> {
let query = serde_json::json!({
"query": {
"term": {
"user_id": user_id
}
},
});
let response = client
.delete_by_query(opensearch::DeleteByQueryParts::Index(&[
SearchIndex::Chats.as_ref()
]))
.body(query)
.refresh(true) // Ensure the index reflects changes immediately
.send()
.await
.map_err(|err| {
OpensearchClientError::request_failed(OpensearchOperation::DeleteChatByUserId, &err)
})?;
let status_code = response.status_code();
if !status_code.is_success() {
let body = response.text().await.map_err(|err| {
OpensearchClientError::deserialization_failed(
OpensearchOperation::DeleteChatByUserId,
&err,
)
})?;
tracing::error!(
status_code = ?status_code,
body = ?body,
"error deleting chats by user id"
);
return Err(OpensearchClientError::Unknown {
details: body,
operation: OpensearchOperation::DeleteChatByUserId,
});
}
Ok(())
}