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
feat(search): index reply-to and email contact names in opensearch (#1916)
gbirman0b66fb8efd58
diff --git a/infra/stacks/opensearch/helpers/scripts/add_reply_to_field.ts b/infra/stacks/opensearch/helpers/scripts/add_reply_to_field.tsnew file mode 100644index 0000000000..e46f0d6e9f--- /dev/null+++ b/infra/stacks/opensearch/helpers/scripts/add_reply_to_field.ts@@ -0,0 +1,85 @@+require('dotenv').config();++import { client } from '../client';+import { EMAIL_INDEX, IS_DRY_RUN } from '../constants';++async function addReplyToField(dryRun: boolean) {+ const opensearchClient = client();++ console.log('\n' + '='.repeat(60));+ console.log(+ `Add reply_to and contact name fields to emails index ${dryRun ? '(DRY-RUN MODE)' : '(LIVE MODE)'}`+ );+ console.log('='.repeat(60));++ if (dryRun) {+ console.log('\n⚠️ DRY-RUN MODE: No changes will be made');+ }++ const indexExists = (+ await opensearchClient.indices.exists({ index: EMAIL_INDEX })+ ).body;++ if (!indexExists) {+ console.log(`⚠️ Index "${EMAIL_INDEX}" does not exist. Aborting.`);+ return;+ }++ console.log('\nAdding reply_to and contact name field mappings...');+ const mappingUpdate = {+ properties: {+ reply_to: {+ type: 'keyword' as const,+ index: true,+ doc_values: true,+ },+ sender_name: {+ type: 'text' as const,+ analyzer: 'standard',+ },+ recipient_names: {+ type: 'text' as const,+ analyzer: 'standard',+ },+ cc_names: {+ type: 'text' as const,+ analyzer: 'standard',+ },+ bcc_names: {+ type: 'text' as const,+ analyzer: 'standard',+ },+ },+ };++ if (dryRun) {+ console.log('[DRY-RUN] Would add reply_to and contact name field mappings');+ } else {+ const putMappingResponse = await opensearchClient.indices.putMapping({+ index: EMAIL_INDEX,+ body: mappingUpdate,+ });++ if (!putMappingResponse.body.acknowledged) {+ throw new Error('Failed to add field mappings');+ }+ console.log('✓ reply_to and contact name field mappings added');+ }++ console.log('\n' + '='.repeat(60));+ console.log('Done!');+ console.log('='.repeat(60));++ if (dryRun) {+ console.log('\nTo run for real, set DRY_RUN=false environment variable\n');+ } else {+ console.log(+ '\n✓ reply_to and contact name fields have been added to the emails index mapping.'+ );+ console.log(+ '✓ To backfill reply_to values, run: cargo run --bin backfill_email_search\n'+ );+ }+}++addReplyToField(IS_DRY_RUN);diff --git a/infra/stacks/opensearch/helpers/scripts/create_indices.ts b/infra/stacks/opensearch/helpers/scripts/create_indices.tsindex eb227bb216..f61d3838a4 100644--- a/infra/stacks/opensearch/helpers/scripts/create_indices.ts+++ b/infra/stacks/opensearch/helpers/scripts/create_indices.ts@@ -249,6 +249,11 @@ async function createEmailIndex(opensearchClient: Client) {index: true,doc_values: true,},+ reply_to: {+ type: 'keyword',+ index: true,+ doc_values: true,+ },recipients: {type: 'keyword',index: true,@@ -264,6 +269,22 @@ async function createEmailIndex(opensearchClient: Client) {index: true,doc_values: true,},+ sender_name: {+ type: 'text',+ analyzer: 'standard',+ },+ recipient_names: {+ type: 'text',+ analyzer: 'standard',+ },+ cc_names: {+ type: 'text',+ analyzer: 'standard',+ },+ bcc_names: {+ type: 'text',+ analyzer: 'standard',+ },labels: {type: 'keyword',index: false, // do not index labelsdiff --git a/rust/cloud-storage/models_email/src/email/service/message.rs b/rust/cloud-storage/models_email/src/email/service/message.rsindex f66053d763..3f4120ef14 100644--- a/rust/cloud-storage/models_email/src/email/service/message.rs+++ b/rust/cloud-storage/models_email/src/email/service/message.rs@@ -38,6 +38,7 @@ pub struct ParsedSearchMessage {pub thread_db_id: Uuid,pub subject: Option<String>,pub from: Option<ContactInfo>,+ pub reply_to: Option<String>,pub to: Vec<ContactInfo>,pub cc: Vec<ContactInfo>,pub bcc: Vec<ContactInfo>,@@ -143,6 +144,7 @@ impl From<MessageWithBodyReplyless> for ParsedMessage {impl From<MessageWithBodyReplyless> for ParsedSearchMessage {fn from(msg: MessageWithBodyReplyless) -> Self {+ let reply_to = extract_reply_to(msg.inner.headers_json.as_ref());Self {body_parsed_linkless: get_body_parsed_linkless_for_message(&msg),db_id: msg.inner.db_id,@@ -150,6 +152,7 @@ impl From<MessageWithBodyReplyless> for ParsedSearchMessage {thread_db_id: msg.inner.thread_db_id,subject: msg.inner.subject,from: msg.inner.from,+ reply_to,to: msg.inner.to,cc: msg.inner.cc,bcc: msg.inner.bcc,@@ -167,6 +170,20 @@ impl From<MessageWithBodyReplyless> for ParsedSearchMessage {}}+fn extract_reply_to(headers_json: Option<&JsonValue>) -> Option<String> {+ let headers = headers_json?.as_array()?;+ for header in headers {+ let name = header.get("name")?.as_str()?;+ if name == "Reply-To" {+ return header+ .get("value")+ .and_then(|v| v.as_str())+ .map(|s| s.to_lowercase());+ }+ }+ None+}+/// determine if a message is an inbound message for use in latest_inbound_message_ts/// - for thread ordering purposes in the FE. (inbox view)pub fn is_inbound(msg: &Message) -> bool {diff --git a/rust/cloud-storage/opensearch_client/src/search/emails.rs b/rust/cloud-storage/opensearch_client/src/search/emails.rsindex 268b9272ba..39f946e8dd 100644--- a/rust/cloud-storage/opensearch_client/src/search/emails.rs+++ b/rust/cloud-storage/opensearch_client/src/search/emails.rs@@ -226,12 +226,27 @@ pub(crate) struct EmailIndex {pub message_id: uuid::Uuid,/// The sender of the email messagepub sender: String,+ /// The display name of the sender+ #[serde(default)]+ pub sender_name: Option<String>,+ /// The reply-to address of the email message+ #[serde(default)]+ pub reply_to: Option<String>,/// The recipients of the email messagepub recipients: Vec<String>,+ /// The display names of the recipients+ #[serde(default)]+ pub recipient_names: Vec<String>,/// The cc of the email messagepub cc: Vec<String>,+ /// The display names of the cc contacts+ #[serde(default)]+ pub cc_names: Vec<String>,/// The bcc of the email messagepub bcc: Vec<String>,+ /// The display names of the bcc contacts+ #[serde(default)]+ pub bcc_names: Vec<String>,/// The labels of the email messagepub labels: Vec<String>,/// The link id of the email messagediff --git a/rust/cloud-storage/opensearch_client/src/upsert/email.rs b/rust/cloud-storage/opensearch_client/src/upsert/email.rsindex 1fca8a7707..58d30257a0 100644--- a/rust/cloud-storage/opensearch_client/src/upsert/email.rs+++ b/rust/cloud-storage/opensearch_client/src/upsert/email.rs@@ -12,12 +12,24 @@ pub struct UpsertEmailArgs {pub message_id: String,/// The sender of the email messagepub sender: String,+ /// The display name of the sender+ #[serde(skip_serializing_if = "Option::is_none")]+ pub sender_name: Option<String>,+ /// The reply-to address of the email message+ #[serde(skip_serializing_if = "Option::is_none")]+ pub reply_to: Option<String>,/// The recipients of the email messagepub recipients: Vec<String>,+ /// The display names of the recipients+ pub recipient_names: Vec<String>,/// The cc of the email messagepub cc: Vec<String>,+ /// The display names of the cc contacts+ pub cc_names: Vec<String>,/// The bcc of the email messagepub bcc: Vec<String>,+ /// The display names of the bcc contacts+ pub bcc_names: Vec<String>,/// The labels of the email messagepub labels: Vec<String>,/// The link id of the email messagediff --git a/rust/cloud-storage/search_processing_service/src/process/email/upsert.rs b/rust/cloud-storage/search_processing_service/src/process/email/upsert.rsindex cb132fb526..b7f49ee185 100644--- a/rust/cloud-storage/search_processing_service/src/process/email/upsert.rs+++ b/rust/cloud-storage/search_processing_service/src/process/email/upsert.rs@@ -62,24 +62,42 @@ pub async fn process_upsert_message(subject: message_info.subject,sender: message_info.from+ .as_ref().context("expected from")?.to_lowercase(),+ sender_name: message_info.from.as_ref().and_then(|f| f.name.clone()),+ reply_to: message_info.reply_to,recipients: message_info.to.iter().map(|to| to.email.to_lowercase()).collect(),+ recipient_names: message_info+ .to+ .iter()+ .filter_map(|to| to.name.clone())+ .collect(),cc: message_info.cc.iter().map(|cc| cc.email.to_lowercase()).collect(),+ cc_names: message_info+ .cc+ .iter()+ .filter_map(|cc| cc.name.clone())+ .collect(),bcc: message_info.bcc.iter().map(|bcc| bcc.email.to_lowercase()).collect(),+ bcc_names: message_info+ .bcc+ .iter()+ .filter_map(|bcc| bcc.name.clone())+ .collect(),labels: message_info.labels.iter()@@ -161,22 +179,35 @@ pub async fn process_upsert_thread_message(user_id: upsert_email_thread_message.macro_user_id.clone(),thread_id: upsert_email_thread_message.thread_id.clone(),subject: message.subject,- sender: message.from.unwrap_or_default().email.to_lowercase(), // All email should have a sender+ sender: message+ .from+ .as_ref()+ .map(|f| f.email.to_lowercase())+ .unwrap_or_default(),+ sender_name: message.from.as_ref().and_then(|f| f.name.clone()),+ reply_to: message.reply_to.map(|r| r.to_lowercase()),recipients: message.to.iter().map(|to| to.email.to_lowercase()).collect(),+ recipient_names: message.to.iter().filter_map(|to| to.name.clone()).collect(),cc: message.cc.iter().map(|cc| cc.email.to_lowercase()).collect(),+ cc_names: message.cc.iter().filter_map(|cc| cc.name.clone()).collect(),bcc: message.bcc.iter().map(|bcc| bcc.email.to_lowercase()).collect(),+ bcc_names: message+ .bcc+ .iter()+ .filter_map(|bcc| bcc.name.clone())+ .collect(),labels: message.labels.iter()