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
fix(search): interleave channel message hits by their own recency in unified search (#4873)
gbirman7d7efa770a68
diff --git a/apps/web/src/lib/queries/soup/normalized-cache/operations.test.ts b/apps/web/src/lib/queries/soup/normalized-cache/operations.test.tsindex dc54398d9c..be2963deb7 100644--- a/apps/web/src/lib/queries/soup/normalized-cache/operations.test.ts+++ b/apps/web/src/lib/queries/soup/normalized-cache/operations.test.ts@@ -122,11 +122,14 @@ function mockSearchResult(type: string, id: string): UnifiedSearchResponseItem {type: 'chat',chat_id: id,} as unknown as UnifiedSearchResponseItem;- case 'channel':+ case 'channelMessage': {+ const [channelId, messageId] = id.split(':');return {- type: 'channel',- channel_id: id,+ type: 'channelMessage',+ channel_id: channelId,+ message_id: messageId,} as unknown as UnifiedSearchResponseItem;+ }case 'project':return { type: 'project', id } as unknown as UnifiedSearchResponseItem;default:@@ -338,11 +341,11 @@ describe('removeSearchEntities', () => {mockSearchResult('document', 'doc-1'),mockSearchResult('chat', 'chat-1'),],- [mockSearchResult('channel', 'ch-1')],+ [mockSearchResult('channelMessage', 'ch-1:msg-1')],]));- removeSearchEntities(new Set(['doc-1', 'ch-1']));+ removeSearchEntities(new Set(['doc-1', 'ch-1:msg-1']));const cached = getSearchQuery()!;expect(cached.pages[0].results).toHaveLength(1);diff --git a/apps/web/src/lib/queries/soup/normalized-cache/operations.ts b/apps/web/src/lib/queries/soup/normalized-cache/operations.tsindex 5104df0843..07ebc1a709 100644--- a/apps/web/src/lib/queries/soup/normalized-cache/operations.ts+++ b/apps/web/src/lib/queries/soup/normalized-cache/operations.ts@@ -635,7 +635,7 @@ function getSearchResultId(result: UnifiedSearchResponseItem): string {return match(result).with({ type: 'document' }, (r) => r.document_id).with({ type: 'chat' }, (r) => r.chat_id)- .with({ type: 'channel' }, (r) => r.channel_id)+ .with({ type: 'channelMessage' }, (r) => `${r.channel_id}:${r.message_id}`).with({ type: 'email' }, (r) => r.thread_id).with({ type: 'project' }, (r) => r.id).with({ type: 'call' }, (r) => r.call_id)diff --git a/apps/web/src/lib/queries/soup/transform-utils.ts b/apps/web/src/lib/queries/soup/transform-utils.tsindex adc33184f0..a5909475f3 100644--- a/apps/web/src/lib/queries/soup/transform-utils.ts+++ b/apps/web/src/lib/queries/soup/transform-utils.ts@@ -450,17 +450,34 @@ export const useSearchResponseItemMapper = () => {},];}- case 'channel': {- return mapChannelSearchResultItem(+ case 'channelMessage': {+ const channelName =+ channels().find((c) => c.id === result.channel_id)?.name ??+ blockNameToDefaultFile('channel');+ const search = getSearchData({ type: 'channel', results: [result] });+ const content = search.contentHitData?.[0]?.content ?? '';+ return [{- channel_id: result.channel_id,- channel_type: result.channel_type,- owner_id: result.owner_id,- channel_message_search_results:- result.channel_message_search_results,+ type: 'channel_message',+ id: `${result.channel_id}:${result.message_id}`,+ channelId: result.channel_id,+ channelName,+ channelType: result.channel_type as ChannelType,+ messageId: result.message_id,+ threadId: result.thread_id ?? undefined,+ target: {+ messageId: result.message_id,+ threadId: result.thread_id ?? undefined,+ },+ senderId: result.sender_id,+ content,+ name: channelName,+ ownerId: result.owner_id ?? '',+ createdAt: result.created_at,+ updatedAt: result.updated_at,+ search,},- channels()- );+ ];}case 'project': {diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts b/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.tsindex cc11d7ed96..25e08272a1 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/tools/schemas.ts@@ -378,62 +378,29 @@ export const SearchToolResponse = z.object({z.intersection(z.object({channel_id: z.string().uuid(),- channel_message_search_results: z.array(- z.object({- created_at: z- .union([z.string().datetime({ offset: true }), z.null()])- .optional(),- deleted_at: z- .union([z.string().datetime({ offset: true }), z.null()])- .optional(),- highlight: z.object({- bcc: z.array(z.string()).optional(),- cc: z.array(z.string()).optional(),- content: z.array(z.string()).optional(),- name: z.union([z.string(), z.null()]).optional(),- recipients: z.array(z.string()).optional(),- sender: z.union([z.string(), z.null()]).optional(),- user_id: z.union([z.string(), z.null()]).optional(),- }),- message_id: z- .union([z.string().uuid(), z.null()])- .optional(),- score: z.union([z.number(), z.null()]).optional(),- sender_id: z.union([z.string(), z.null()]).optional(),- thread_id: z- .union([z.string().uuid(), z.null()])- .optional(),- updated_at: z- .union([z.string().datetime({ offset: true }), z.null()])- .optional(),- })- ),channel_type: z.string(),- id: z.string().uuid(),- metadata: z- .union([- z.object({- created_at: z.string().datetime({ offset: true }),- interacted_at: z- .union([- z.string().datetime({ offset: true }),- z.null(),- ])- .optional(),- updated_at: z.string().datetime({ offset: true }),- viewed_at: z- .union([- z.string().datetime({ offset: true }),- z.null(),- ])- .optional(),- }),- z.null(),- ])+ created_at: z.string().datetime({ offset: true }),+ deleted_at: z+ .union([z.string().datetime({ offset: true }), z.null()]).optional(),+ highlight: z.object({+ bcc: z.array(z.string()).optional(),+ cc: z.array(z.string()).optional(),+ content: z.array(z.string()).optional(),+ name: z.union([z.string(), z.null()]).optional(),+ recipients: z.array(z.string()).optional(),+ sender: z.union([z.string(), z.null()]).optional(),+ user_id: z.union([z.string(), z.null()]).optional(),+ }),+ id: z.string().uuid(),+ message_id: z.string().uuid(),owner_id: z.union([z.string(), z.null()]).optional(),+ score: z.union([z.number(), z.null()]).optional(),+ sender_id: z.string(),+ thread_id: z.union([z.string().uuid(), z.null()]).optional(),+ updated_at: z.string().datetime({ offset: true }),}),- z.object({ type: z.literal('channel') })+ z.object({ type: z.literal('channelMessage') })),z.intersection(z.object({diff --git a/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.ts b/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.tsindex b4b815834c..3919553ef2 100644--- a/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.ts+++ b/apps/web/src/lib/service-clients/service-cognition/generated/tools/types.ts@@ -417,8 +417,8 @@ export type TaggedSearchResult1 =| (EmailSearchResponseItemWithMetadata & {type: 'email';})- | (ChannelSearchResponseItemWithMetadata & {- type: 'channel';+ | (ChannelMessageSearchResponseItem & {+ type: 'channelMessage';})| (ProjectSearchResponseItemWithMetadata & {type: 'project';@@ -688,85 +688,57 @@ export interface CallRecordSummary {status?: ToolCallStatus | null;}/**- * Metadata for a channel fetched from the database+ * A single channel-message content hit in the unified search response.+ * One item per matching message, timestamped by the message itself, so the+ * unified sort interleaves channel messages with other entity types by+ * their own recency.*/-export interface ChannelMetadata {- created_at: string;- interacted_at?: string | null;- updated_at: string;- viewed_at?: string | null;-}-/**- * ChannelSearchResponseItem object with channel metadata we fetch from macrodb. we don't store these- * timestamps in opensearch as they would require us to update each chat message record for the chat- * every time the chat updates (specifically for updated_at and viewed_at and interacted_at)- */-export interface ChannelSearchResponseItemWithMetadata {+export interface ChannelMessageSearchResponseItem {/**- * The id of the channel+ * The id of the channel the message belongs to*/channel_id: string;- /**- * The search results for the channel- * This may be empty if the search result match was not on content- */- channel_message_search_results: ChannelSearchResult[];/*** The type of channel*/channel_type: string;- /**- * Standardized fields that all item types will share.- * These field names are being aligned across all item types- * for consistency in our data model.- */- id: string;- /**- * Metadata from the database. None if the channel doesn't exist in the database.- */- metadata?: ChannelMetadata | null;- /**- * we don't store this for channels atm but keeping it here for consistency- */- owner_id?: string | null;-}-/**- * A channel message match for a given channel id- */-export interface ChannelSearchResult {/*** When the channel message was created- * This is only prsent if the search result is on the message content*/- created_at?: string | null;+ created_at: string;/*** When the channel message was deleted, if it has been*/deleted_at?: string | null;highlight: SearchHighlight;+ /**+ * Standardized id field shared by all item types; the channel id.+ */+ id: string;/*** The channel message id- * This is only prsent if the search result is on the message content*/- message_id?: string | null;+ message_id: string;+ /**+ * we don't store this for channels atm but keeping it here for consistency+ */+ owner_id?: string | null;/*** The score of the result*/score?: number | null;/*** The sender id- * This is only prsent if the search result is on the message content*/- sender_id?: string | null;+ sender_id: string;/*** The channel message thread id*/thread_id?: string | null;/*** When the channel message was last updated- * This is only prsent if the search result is on the message content*/- updated_at?: string | null;+ updated_at: string;}/*** A single message in the tool response.diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/channelMessageSearchResponseItem.ts b/apps/web/src/lib/service-clients/service-search/generated/models/channelMessageSearchResponseItem.tsnew file mode 100644index 0000000000..527e57695a--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/channelMessageSearchResponseItem.ts@@ -0,0 +1,44 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */+import type { ChannelMessageSearchResponseItemDeletedAt } from './channelMessageSearchResponseItemDeletedAt';+import type { ChannelMessageSearchResponseItemOwnerId } from './channelMessageSearchResponseItemOwnerId';+import type { ChannelMessageSearchResponseItemScore } from './channelMessageSearchResponseItemScore';+import type { ChannelMessageSearchResponseItemThreadId } from './channelMessageSearchResponseItemThreadId';+import type { SearchHighlight } from './searchHighlight';++/**+ * A single channel-message content hit in the unified search response.+One item per matching message, timestamped by the message itself, so the+unified sort interleaves channel messages with other entity types by+their own recency.+ */+export interface ChannelMessageSearchResponseItem {+ /** The id of the channel the message belongs to */+ channel_id: string;+ /** The type of channel */+ channel_type: string;+ /** When the channel message was created */+ created_at: string;+ /** When the channel message was deleted, if it has been */+ deleted_at?: ChannelMessageSearchResponseItemDeletedAt;+ /** The highlights for the channel message */+ highlight: SearchHighlight;+ /** Standardized id field shared by all item types; the channel id. */+ id: string;+ /** The channel message id */+ message_id: string;+ /** we don't store this for channels atm but keeping it here for consistency */+ owner_id?: ChannelMessageSearchResponseItemOwnerId;+ /** The score of the result */+ score?: ChannelMessageSearchResponseItemScore;+ /** The sender id */+ sender_id: string;+ /** The channel message thread id */+ thread_id?: ChannelMessageSearchResponseItemThreadId;+ /** When the channel message was last updated */+ updated_at: string;+}diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/channelMessageSearchResponseItemDeletedAt.ts b/apps/web/src/lib/service-clients/service-search/generated/models/channelMessageSearchResponseItemDeletedAt.tsnew file mode 100644index 0000000000..595ce3e0d9--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/channelMessageSearchResponseItemDeletedAt.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */++/**+ * When the channel message was deleted, if it has been+ */+export type ChannelMessageSearchResponseItemDeletedAt = string | null;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/channelMessageSearchResponseItemOwnerId.ts b/apps/web/src/lib/service-clients/service-search/generated/models/channelMessageSearchResponseItemOwnerId.tsnew file mode 100644index 0000000000..5ed9461142--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/channelMessageSearchResponseItemOwnerId.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */++/**+ * we don't store this for channels atm but keeping it here for consistency+ */+export type ChannelMessageSearchResponseItemOwnerId = string | null;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/channelMessageSearchResponseItemScore.ts b/apps/web/src/lib/service-clients/service-search/generated/models/channelMessageSearchResponseItemScore.tsnew file mode 100644index 0000000000..93d0864a14--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/channelMessageSearchResponseItemScore.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */++/**+ * The score of the result+ */+export type ChannelMessageSearchResponseItemScore = number | null;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/channelMessageSearchResponseItemThreadId.ts b/apps/web/src/lib/service-clients/service-search/generated/models/channelMessageSearchResponseItemThreadId.tsnew file mode 100644index 0000000000..ec16e6d7f8--- /dev/null+++ b/apps/web/src/lib/service-clients/service-search/generated/models/channelMessageSearchResponseItemThreadId.ts@@ -0,0 +1,11 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * search_service+ * OpenAPI spec version: 0.1.0+ */++/**+ * The channel message thread id+ */+export type ChannelMessageSearchResponseItemThreadId = string | null;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/index.ts b/apps/web/src/lib/service-clients/service-search/generated/models/index.tsindex d177456514..1de155e181 100644--- a/apps/web/src/lib/service-clients/service-search/generated/models/index.ts+++ b/apps/web/src/lib/service-clients/service-search/generated/models/index.ts@@ -28,6 +28,11 @@ export * from './channelFilters';export * from './channelFiltersImportance';export * from './channelFiltersOrgId';export * from './channelFiltersTeamId';+export * from './channelMessageSearchResponseItem';+export * from './channelMessageSearchResponseItemDeletedAt';+export * from './channelMessageSearchResponseItemOwnerId';+export * from './channelMessageSearchResponseItemScore';+export * from './channelMessageSearchResponseItemThreadId';export * from './channelMetadata';export * from './channelMetadataInteractedAt';export * from './channelMetadataViewedAt';diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItemOneOfOnezero.ts b/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItemOneOfOnezero.tsindex 33e528e762..24872265e6 100644--- a/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItemOneOfOnezero.ts+++ b/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItemOneOfOnezero.ts@@ -4,9 +4,8 @@* search_service* OpenAPI spec version: 0.1.0*/-import type { ChannelSearchResponseItemWithMetadata } from './channelSearchResponseItemWithMetadata';+import type { ChannelMessageSearchResponseItem } from './channelMessageSearchResponseItem';import type { UnifiedSearchResponseItemOneOfOnezeroAllOf } from './unifiedSearchResponseItemOneOfOnezeroAllOf';export type UnifiedSearchResponseItemOneOfOnezero =- ChannelSearchResponseItemWithMetadata &- UnifiedSearchResponseItemOneOfOnezeroAllOf;+ ChannelMessageSearchResponseItem & UnifiedSearchResponseItemOneOfOnezeroAllOf;diff --git a/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItemOneOfOnezeroAllOfType.ts b/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItemOneOfOnezeroAllOfType.tsindex ec78a82180..4fe9837a19 100644--- a/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItemOneOfOnezeroAllOfType.ts+++ b/apps/web/src/lib/service-clients/service-search/generated/models/unifiedSearchResponseItemOneOfOnezeroAllOfType.ts@@ -10,5 +10,5 @@ export type UnifiedSearchResponseItemOneOfOnezeroAllOfType =// eslint-disable-next-line @typescript-eslint/no-redeclareexport const UnifiedSearchResponseItemOneOfOnezeroAllOfType = {- channel: 'channel',+ channelMessage: 'channelMessage',} as const;diff --git a/apps/web/src/lib/service-clients/service-search/openapi.json b/apps/web/src/lib/service-clients/service-search/openapi.jsonindex d2125c44d0..3535ffd8ac 100644--- a/apps/web/src/lib/service-clients/service-search/openapi.json+++ b/apps/web/src/lib/service-clients/service-search/openapi.json@@ -425,6 +425,78 @@}}},+ "ChannelMessageSearchResponseItem": {+ "type": "object",+ "description": "A single channel-message content hit in the unified search response.\nOne item per matching message, timestamped by the message itself, so the\nunified sort interleaves channel messages with other entity types by\ntheir own recency.",+ "required": [+ "id",+ "channel_type",+ "channel_id",+ "message_id",+ "sender_id",+ "created_at",+ "updated_at",+ "highlight"+ ],+ "properties": {+ "channel_id": {+ "type": "string",+ "format": "uuid",+ "description": "The id of the channel the message belongs to"+ },+ "channel_type": {+ "type": "string",+ "description": "The type of channel"+ },+ "created_at": {+ "type": "string",+ "format": "date-time",+ "description": "When the channel message was created"+ },+ "deleted_at": {+ "type": ["string", "null"],+ "format": "date-time",+ "description": "When the channel message was deleted, if it has been"+ },+ "highlight": {+ "$ref": "#/components/schemas/SearchHighlight",+ "description": "The highlights for the channel message"+ },+ "id": {+ "type": "string",+ "format": "uuid",+ "description": "Standardized id field shared by all item types; the channel id."+ },+ "message_id": {+ "type": "string",+ "format": "uuid",+ "description": "The channel message id"+ },+ "owner_id": {+ "type": ["string", "null"],+ "description": "we don't store this for channels atm but keeping it here for consistency"+ },+ "score": {+ "type": ["number", "null"],+ "format": "double",+ "description": "The score of the result"+ },+ "sender_id": {+ "type": "string",+ "description": "The sender id"+ },+ "thread_id": {+ "type": ["string", "null"],+ "format": "uuid",+ "description": "The channel message thread id"+ },+ "updated_at": {+ "type": "string",+ "format": "date-time",+ "description": "When the channel message was last updated"+ }+ }+ },"ChannelMetadata": {"type": "object","description": "Metadata for a channel fetched from the database",@@ -3858,7 +3930,7 @@{"allOf": [{- "$ref": "#/components/schemas/ChannelSearchResponseItemWithMetadata"+ "$ref": "#/components/schemas/ChannelMessageSearchResponseItem"},{"type": "object",@@ -3866,7 +3938,7 @@"properties": {"type": {"type": "string",- "enum": ["channel"]+ "enum": ["channelMessage"]}}}diff --git a/crates/ai_tools/src/search/search_service/types.rs b/crates/ai_tools/src/search/search_service/types.rsindex 31ff579718..4a503c2fce 100644--- a/crates/ai_tools/src/search/search_service/types.rs+++ b/crates/ai_tools/src/search/search_service/types.rs@@ -169,7 +169,9 @@ fn item_properties(item: &UnifiedSearchResponseItem) -> Option<&Vec<SoupPropertyUnifiedSearchResponseItem::Email(i) => i.properties.as_ref(),UnifiedSearchResponseItem::Project(i) => i.properties.as_ref(),UnifiedSearchResponseItem::Call(i) => i.properties.as_ref(),- UnifiedSearchResponseItem::Channel(_) | UnifiedSearchResponseItem::Company(_) => None,+ UnifiedSearchResponseItem::ChannelMessage(_) | UnifiedSearchResponseItem::Company(_) => {+ None+ }}}@@ -180,6 +182,8 @@ fn item_properties_mut(item: &mut UnifiedSearchResponseItem) -> Option<&mut Vec<UnifiedSearchResponseItem::Email(i) => i.properties.as_mut(),UnifiedSearchResponseItem::Project(i) => i.properties.as_mut(),UnifiedSearchResponseItem::Call(i) => i.properties.as_mut(),- UnifiedSearchResponseItem::Channel(_) | UnifiedSearchResponseItem::Company(_) => None,+ UnifiedSearchResponseItem::ChannelMessage(_) | UnifiedSearchResponseItem::Company(_) => {+ None+ }}}diff --git a/crates/models_search/src/channel.rs b/crates/models_search/src/channel.rsindex 9c6ebe489f..9b37b163f1 100644--- a/crates/models_search/src/channel.rs+++ b/crates/models_search/src/channel.rs@@ -83,6 +83,39 @@ pub struct ChannelSearchResponseItemWithMetadata {pub extra: ChannelSearchResponseItem,}+/// A single channel-message content hit in the unified search response.+/// One item per matching message, timestamped by the message itself, so the+/// unified sort interleaves channel messages with other entity types by+/// their own recency.+#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]+pub struct ChannelMessageSearchResponseItem {+ /// Standardized id field shared by all item types; the channel id.+ pub id: uuid::Uuid,+ /// we don't store this for channels atm but keeping it here for consistency+ pub owner_id: Option<String>,+ /// The type of channel+ pub channel_type: String,+ /// The id of the channel the message belongs to+ pub channel_id: uuid::Uuid,+ /// The channel message id+ pub message_id: uuid::Uuid,+ /// The channel message thread id+ pub thread_id: Option<uuid::Uuid>,+ /// The sender id+ pub sender_id: String,+ /// When the channel message was created+ pub created_at: DateTime<Utc>,+ /// When the channel message was last updated+ pub updated_at: DateTime<Utc>,+ /// When the channel message was deleted, if it has been+ pub deleted_at: Option<DateTime<Utc>>,+ /// The highlights for the channel message+ pub highlight: SearchHighlight,+ /// The score of the result+ #[serde(skip_serializing_if = "Option::is_none")]+ pub score: Option<f64>,+}+/// Metadata associated with Channel Search#[derive(Debug, Serialize, Deserialize, ToSchema)]pub struct ChannelSearchMetadata {diff --git a/crates/models_search/src/unified.rs b/crates/models_search/src/unified.rsindex 4896535d0a..306c8ff377 100644--- a/crates/models_search/src/unified.rs+++ b/crates/models_search/src/unified.rs@@ -2,7 +2,7 @@ use chrono::{DateTime, Utc};use uuid::Uuid;use crate::call_record::CallRecordSearchResponseItemWithMetadata;-use crate::channel::ChannelSearchResponseItemWithMetadata;+use crate::channel::ChannelMessageSearchResponseItem;use crate::chat::ChatSearchResponseItemWithMetadata;use crate::document::DocumentSearchResponseItemWithMetadata;use crate::email::EmailSearchResponseItemWithMetadata;@@ -209,7 +209,7 @@ pub enum UnifiedSearchResponseItem {Document(DocumentSearchResponseItemWithMetadata),Chat(ChatSearchResponseItemWithMetadata),Email(EmailSearchResponseItemWithMetadata),- Channel(ChannelSearchResponseItemWithMetadata),+ ChannelMessage(ChannelMessageSearchResponseItem),Project(ProjectSearchResponseItemWithMetadata),Call(CallRecordSearchResponseItemWithMetadata),Company(crate::crm_company::CrmCompanySearchResponseItem),@@ -221,7 +221,7 @@ impl UnifiedSearchResponseItem {Self::Document(item) => item.extra.id,Self::Chat(item) => item.extra.id,Self::Email(item) => item.extra.id,- Self::Channel(item) => item.extra.id,+ Self::ChannelMessage(item) => item.channel_id,Self::Project(item) => item.extra.id,Self::Call(item) => item.extra.id,Self::Company(item) => item.id,@@ -233,18 +233,7 @@ impl UnifiedSearchResponseItem {Self::Document(item) => item.metadata.as_ref().map(|m| m.updated_at),Self::Chat(item) => item.metadata.as_ref().map(|m| m.updated_at),Self::Email(item) => Some(item.updated_at),- Self::Channel(item) => {- // Get the max updated_at from channel_message_search_results- let max_result_updated_at = item- .extra- .channel_message_search_results- .iter()- .filter_map(|r| r.updated_at)- .max();-- // Use max from results, or fall back to metadata.updated_at- max_result_updated_at.or_else(|| item.metadata.as_ref().map(|m| m.updated_at))- }+ Self::ChannelMessage(item) => Some(item.updated_at),Self::Project(item) => item.metadata.as_ref().map(|m| m.updated_at),Self::Call(item) => item.metadata.as_ref().map(|m| m.updated_at),Self::Company(item) => Some(item.updated_at),diff --git a/crates/search_service/src/api/search/channel.rs b/crates/search_service/src/api/search/channel.rsindex 3995b6485b..39645c8798 100644--- a/crates/search_service/src/api/search/channel.rs+++ b/crates/search_service/src/api/search/channel.rs@@ -17,35 +17,32 @@ use macro_user_id::user_id::MacroUserId;use model::user::UserContext;use models_search::MatchType;use models_search::channel::{- ChannelSearchRequest, ChannelSearchResponse, ChannelSearchResponseItem,- ChannelSearchResponseItemWithMetadata, ChannelSearchResult, ChannelSortTimestamp,+ ChannelMessageSearchResponseItem, ChannelSearchRequest, ChannelSearchResponse,+ ChannelSearchResponseItem, ChannelSearchResponseItemWithMetadata, ChannelSearchResult,+ ChannelSortTimestamp,};use models_search_cursor::{SearchCursorOption, SearchMethodCursor};use opensearch_client::search::channels::{ChannelSearchArgs, ChannelSortMode};use opensearch_client::search::model::SearchGotoContent;use sqlx::types::Uuid;-/// Enriches channel message search results with metadata-#[tracing::instrument(skip(ctx, results), err)]-pub(in crate::api::search) async fn enrich_channels(+/// Fetches the per-user channel history info and message deletion states+/// backing channel search enrichment. Channels without history info are the+/// caller's signal to drop the hit (no access / channel gone).+async fn fetch_channel_enrichment(ctx: &SearchHandlerState,user_id: &str,- results: Vec<opensearch_client::search::model::SearchHit>,- sort_timestamp: ChannelSortTimestamp,-) -> Result<Vec<ChannelSearchResponseItemWithMetadata>, SearchError> {- let results: Vec<opensearch_client::search::model::SearchHit> = results- .into_iter()- .filter(|r| r.entity_type == models_opensearch::SearchEntityType::Channels)- .collect();-- if results.is_empty() {- return Ok(vec![]);- }-- // Extract channel IDs from results+ results: &[opensearch_client::search::model::SearchHit],+) -> Result<+ (+ HashMap<Uuid, ChannelHistoryInfo>,+ HashMap<Uuid, Option<DateTime<Utc>>>,+ ),+ SearchError,+> {let channel_ids: Vec<Uuid> = results.iter().map(|r| r.entity_id).collect();- // Extract message IDs from results so we can flag any that have been deleted.+ // Message IDs are needed so we can flag any that have been deleted.let message_ids: Vec<Uuid> = results.iter().filter_map(|r| match &r.goto {@@ -54,7 +51,7 @@ pub(in crate::api::search) async fn enrich_channels(}).collect();- let (channel_histories, message_states) = tokio::try_join!(+ tokio::try_join!(async {comms_db_client::activity::get_activity::get_channel_history_info(&ctx.db,@@ -72,7 +69,28 @@ pub(in crate::api::search) async fn enrich_channels(.await},)- .map_err(SearchError::InternalError)?;+ .map_err(SearchError::InternalError)+}++/// Enriches channel message search results with metadata+#[tracing::instrument(skip(ctx, results), err)]+pub(in crate::api::search) async fn enrich_channels(+ ctx: &SearchHandlerState,+ user_id: &str,+ results: Vec<opensearch_client::search::model::SearchHit>,+ sort_timestamp: ChannelSortTimestamp,+) -> Result<Vec<ChannelSearchResponseItemWithMetadata>, SearchError> {+ let results: Vec<opensearch_client::search::model::SearchHit> = results+ .into_iter()+ .filter(|r| r.entity_type == models_opensearch::SearchEntityType::Channels)+ .collect();++ if results.is_empty() {+ return Ok(vec![]);+ }++ let (channel_histories, message_states) =+ fetch_channel_enrichment(ctx, user_id, &results).await?;// Construct enriched resultslet enriched_results =@@ -82,6 +100,69 @@ pub(in crate::api::search) async fn enrich_channels(Ok(enriched_results)}+/// Enriches channel message hits into one unified response item per message,+/// keeping the per-message hit order OpenSearch returned.+#[tracing::instrument(skip(ctx, results), err)]+pub(in crate::api::search) async fn enrich_channel_messages(+ ctx: &SearchHandlerState,+ user_id: &str,+ results: Vec<opensearch_client::search::model::SearchHit>,+) -> Result<Vec<ChannelMessageSearchResponseItem>, SearchError> {+ let results: Vec<opensearch_client::search::model::SearchHit> = results+ .into_iter()+ .filter(|r| r.entity_type == models_opensearch::SearchEntityType::Channels)+ .collect();++ if results.is_empty() {+ return Ok(vec![]);+ }++ let (channel_histories, message_states) =+ fetch_channel_enrichment(ctx, user_id, &results).await?;++ Ok(construct_channel_message_items(+ results,+ channel_histories,+ message_states,+ ))+}++/// Builds one per-message item per content hit. Drops hits whose channel has+/// no history info for the caller (no access / channel gone) and hits whose+/// message no longer exists in the DB (stale OpenSearch entries); soft-deleted+/// messages are kept with `deleted_at` set. The channels index carries no+/// name field, so every hit has message goto content.+pub fn construct_channel_message_items(+ search_results: Vec<opensearch_client::search::model::SearchHit>,+ channel_histories: HashMap<Uuid, ChannelHistoryInfo>,+ message_states: HashMap<Uuid, Option<DateTime<Utc>>>,+) -> Vec<ChannelMessageSearchResponseItem> {+ search_results+ .into_iter()+ .filter_map(|hit| {+ let Some(SearchGotoContent::Channels(goto)) = hit.goto else {+ return None;+ };+ let info = channel_histories.get(&hit.entity_id)?;+ let deleted_at = *message_states.get(&goto.channel_message_id)?;+ Some(ChannelMessageSearchResponseItem {+ id: hit.entity_id,+ owner_id: Some(info.user_id.clone()),+ channel_type: info.channel_type.clone(),+ channel_id: hit.entity_id,+ message_id: goto.channel_message_id,+ thread_id: goto.thread_id,+ sender_id: goto.sender_id,+ created_at: goto.created_at,+ updated_at: goto.updated_at,+ deleted_at,+ highlight: hit.highlight.into(),+ score: hit.score,+ })+ })+ .collect()+}+pub fn construct_search_result(search_results: Vec<opensearch_client::search::model::SearchHit>,channel_histories: HashMap<Uuid, ChannelHistoryInfo>,diff --git a/crates/search_service/src/api/search/channel/test.rs b/crates/search_service/src/api/search/channel/test.rsindex 1d56d19e42..a76c983084 100644--- a/crates/search_service/src/api/search/channel/test.rs+++ b/crates/search_service/src/api/search/channel/test.rs@@ -504,6 +504,153 @@ fn test_channel_history_null_viewed_at() {assert!(metadata.interacted_at.is_none());}+#[test]+fn test_construct_channel_message_items_one_item_per_hit_in_hit_order() {+ let channel_a: Uuid = "550e8400-e29b-41d4-a716-446655440000".parse().unwrap();+ let channel_b: Uuid = "550e8400-e29b-41d4-a716-446655440001".parse().unwrap();+ let msg1: Uuid = "11111111-1111-1111-1111-111111111111".parse().unwrap();+ let msg2: Uuid = "22222222-2222-2222-2222-222222222222".parse().unwrap();+ let msg3: Uuid = "33333333-3333-3333-3333-333333333333".parse().unwrap();++ // Hits arrive in global recency order, channel A's messages straddling+ // channel B's — the per-message items must preserve that order rather+ // than regroup by channel.+ let search_results = vec![+ create_test_channel_response(+ &channel_a.to_string(),+ &msg1.to_string(),+ "user1",+ Some(vec!["newest".to_string()]),+ ),+ create_test_channel_response(+ &channel_b.to_string(),+ &msg2.to_string(),+ "user2",+ Some(vec!["middle".to_string()]),+ ),+ create_test_channel_response(+ &channel_a.to_string(),+ &msg3.to_string(),+ "user3",+ Some(vec!["oldest".to_string()]),+ ),+ ];++ let mut channel_histories = HashMap::new();+ for channel_id in [channel_a, channel_b] {+ channel_histories.insert(+ channel_id,+ create_channel_history(channel_id.to_string().as_str()),+ );+ }++ let states = active_states_for(&search_results);+ let items = construct_channel_message_items(search_results, channel_histories, states);++ assert_eq!(items.len(), 3);+ assert_eq!(+ items+ .iter()+ .map(|i| (i.channel_id, i.message_id))+ .collect::<Vec<_>>(),+ vec![(channel_a, msg1), (channel_b, msg2), (channel_a, msg3)]+ );+ assert_eq!(items[0].sender_id, "user1");+ assert_eq!(items[0].owner_id.as_deref(), Some("user1"));+ assert_eq!(items[0].channel_type, "public");+ assert_eq!(items[0].id, channel_a);+ assert_eq!(+ items[0].created_at,+ DateTime::from_timestamp(1234567890, 0).unwrap()+ );+ assert_eq!(+ items[0].updated_at,+ DateTime::from_timestamp(1234567891, 0).unwrap()+ );+}++#[test]+fn test_construct_channel_message_items_drops_missing_history() {+ let known_channel: Uuid = "550e8400-e29b-41d4-a716-446655440002".parse().unwrap();+ let unknown_channel: Uuid = "550e8400-e29b-41d4-a716-446655440003".parse().unwrap();++ let search_results = vec![+ create_test_channel_response(+ &known_channel.to_string(),+ "11111111-1111-1111-1111-111111111111",+ "user1",+ Some(vec!["hit".to_string()]),+ ),+ create_test_channel_response(+ &unknown_channel.to_string(),+ "22222222-2222-2222-2222-222222222222",+ "user2",+ Some(vec!["hit".to_string()]),+ ),+ ];++ let mut channel_histories = HashMap::new();+ channel_histories.insert(+ known_channel,+ create_channel_history(known_channel.to_string().as_str()),+ );++ let states = active_states_for(&search_results);+ let items = construct_channel_message_items(search_results, channel_histories, states);++ assert_eq!(items.len(), 1);+ assert_eq!(items[0].channel_id, known_channel);+}++#[test]+fn test_construct_channel_message_items_filters_orphans_and_propagates_deleted_at() {+ let channel_uuid: Uuid = "550e8400-e29b-41d4-a716-446655440004".parse().unwrap();+ let active_message_id: Uuid = "11111111-1111-1111-1111-111111111111".parse().unwrap();+ let deleted_message_id: Uuid = "22222222-2222-2222-2222-222222222222".parse().unwrap();+ let orphan_message_id: Uuid = "33333333-3333-3333-3333-333333333333".parse().unwrap();++ let search_results = vec![+ create_test_channel_response(+ &channel_uuid.to_string(),+ &active_message_id.to_string(),+ "user1",+ Some(vec!["active".to_string()]),+ ),+ create_test_channel_response(+ &channel_uuid.to_string(),+ &deleted_message_id.to_string(),+ "user2",+ Some(vec!["deleted".to_string()]),+ ),+ create_test_channel_response(+ &channel_uuid.to_string(),+ &orphan_message_id.to_string(),+ "user3",+ Some(vec!["orphan".to_string()]),+ ),+ ];++ let mut channel_histories = HashMap::new();+ channel_histories.insert(+ channel_uuid,+ create_channel_history(channel_uuid.to_string().as_str()),+ );++ let deleted_at = DateTime::from_timestamp(1700000000, 0).unwrap();+ let mut states: HashMap<Uuid, Option<DateTime<Utc>>> = HashMap::new();+ states.insert(active_message_id, None);+ states.insert(deleted_message_id, Some(deleted_at));+ // orphan_message_id intentionally omitted to simulate a hard-deleted row.++ let items = construct_channel_message_items(search_results, channel_histories, states);++ assert_eq!(items.len(), 2, "orphan hit should be filtered out");+ assert_eq!(items[0].message_id, active_message_id);+ assert_eq!(items[0].deleted_at, None);+ assert_eq!(items[1].message_id, deleted_message_id);+ assert_eq!(items[1].deleted_at, Some(deleted_at));+}+#[test]fn test_sort_stability() {let channel_ids: Vec<Uuid> = [diff --git a/crates/search_service/src/api/search/enrich.rs b/crates/search_service/src/api/search/enrich.rsindex 2ec60bb16e..8679ca54c4 100644--- a/crates/search_service/src/api/search/enrich.rs+++ b/crates/search_service/src/api/search/enrich.rs@@ -1,17 +1,13 @@//! This module is responsible for enriching search results with metadatause models_opensearch::SearchEntityType;-use models_search::channel::ChannelSortTimestamp;use models_search::unified::UnifiedSearchResponseItem;use opensearch_client::search::model::SearchHit;-// Unified search always re-sorts channel results by created_at app-side (Message mode).-const UNIFIED_CHANNEL_SORT: ChannelSortTimestamp = ChannelSortTimestamp::Message;-use crate::api::{context::SearchHandlerState,search::{- call_record::enrich_call_records, channel::enrich_channels, chat::enrich_chats,+ call_record::enrich_call_records, channel::enrich_channel_messages, chat::enrich_chats,document::enrich_documents, email::enrich_emails, project::enrich_projects,simple::SearchError,},@@ -42,10 +38,10 @@ pub async fn enrich_search_response(.collect())}SearchEntityType::Channels => {- let response = enrich_channels(ctx, user_id, results, UNIFIED_CHANNEL_SORT).await?;+ let response = enrich_channel_messages(ctx, user_id, results).await?;Ok(response.into_iter()- .map(UnifiedSearchResponseItem::Channel)+ .map(UnifiedSearchResponseItem::ChannelMessage).collect())}SearchEntityType::Chats => {diff --git a/crates/search_service/src/api/search/unified/test.rs b/crates/search_service/src/api/search/unified/test.rsindex 7ac24a1ccf..228dbd04af 100644--- a/crates/search_service/src/api/search/unified/test.rs+++ b/crates/search_service/src/api/search/unified/test.rs@@ -2,10 +2,7 @@ use super::*;use chrono::DateTime;use models_search::{SearchHighlight,- channel::{- ChannelMetadata, ChannelSearchResponseItem, ChannelSearchResponseItemWithMetadata,- ChannelSearchResult,- },+ channel::ChannelMessageSearchResponseItem,chat::{ChatMetadata, ChatSearchResponseItem, ChatSearchResponseItemWithMetadata},document::{DocumentMetadata, DocumentSearchResponseItem, DocumentSearchResponseItemWithMetadata,@@ -15,6 +12,23 @@ use models_search::{};use sqlx::types::Uuid;+fn channel_message_item(channel_id: Uuid, message_id: Uuid, ts: i64) -> UnifiedSearchResponseItem {+ UnifiedSearchResponseItem::ChannelMessage(ChannelMessageSearchResponseItem {+ id: channel_id,+ owner_id: Some("owner1".to_string()),+ channel_type: "public".to_string(),+ channel_id,+ message_id,+ thread_id: None,+ sender_id: "sender1".to_string(),+ created_at: DateTime::from_timestamp(ts, 0).unwrap(),+ updated_at: DateTime::from_timestamp(ts, 0).unwrap(),+ deleted_at: None,+ highlight: SearchHighlight::default(),+ score: None,+ })+}+#[test]fn test_sort_unified_search_results() {// Create test UUIDs@@ -150,149 +164,28 @@ fn test_sort_unified_search_results() {}#[test]-fn test_channel_updated_at_uses_max_from_message_results() {- let channel_id = Uuid::new_v4();-- // Channel with metadata.updated_at = 1000, but message results have higher values- let channel = UnifiedSearchResponseItem::Channel(ChannelSearchResponseItemWithMetadata {- metadata: Some(ChannelMetadata {- created_at: DateTime::from_timestamp(900, 0).unwrap(),- updated_at: DateTime::from_timestamp(1000, 0).unwrap(),- viewed_at: None,- interacted_at: None,- }),- extra: ChannelSearchResponseItem {- id: channel_id,- owner_id: Some("owner1".to_string()),- channel_type: "slack".to_string(),- channel_id,- channel_message_search_results: vec![- ChannelSearchResult {- message_id: Some(Uuid::new_v4()),- thread_id: None,- sender_id: Some("sender1".to_string()),- created_at: Some(DateTime::from_timestamp(1800, 0).unwrap()),- updated_at: Some(DateTime::from_timestamp(2000, 0).unwrap()), // Second highest- deleted_at: None,- highlight: SearchHighlight::default(),- score: None,- },- ChannelSearchResult {- message_id: Some(Uuid::new_v4()),- thread_id: None,- sender_id: Some("sender2".to_string()),- created_at: Some(DateTime::from_timestamp(2900, 0).unwrap()),- updated_at: Some(DateTime::from_timestamp(3000, 0).unwrap()), // Highest - should be used- deleted_at: None,- highlight: SearchHighlight::default(),- score: None,- },- ChannelSearchResult {- message_id: Some(Uuid::new_v4()),- thread_id: None,- sender_id: Some("sender3".to_string()),- created_at: Some(DateTime::from_timestamp(1400, 0).unwrap()),- updated_at: Some(DateTime::from_timestamp(1500, 0).unwrap()), // Lowest of results- deleted_at: None,- highlight: SearchHighlight::default(),- score: None,- },- ],- },- });-- // Should return 3000 (max from message results), not 1000 (metadata)+fn test_channel_message_updated_at_is_own_timestamp() {+ let item = channel_message_item(Uuid::new_v4(), Uuid::new_v4(), 3000);assert_eq!(- channel.updated_at(),+ item.updated_at(),Some(DateTime::from_timestamp(3000, 0).unwrap()));}#[test]-fn test_channel_updated_at_falls_back_to_metadata_when_no_results() {- let channel_id = Uuid::new_v4();-- // Channel with metadata.updated_at = 1000, but no message results- let channel = UnifiedSearchResponseItem::Channel(ChannelSearchResponseItemWithMetadata {- metadata: Some(ChannelMetadata {- created_at: DateTime::from_timestamp(900, 0).unwrap(),- updated_at: DateTime::from_timestamp(1000, 0).unwrap(),- viewed_at: None,- interacted_at: None,- }),- extra: ChannelSearchResponseItem {- id: channel_id,- owner_id: Some("owner1".to_string()),- channel_type: "slack".to_string(),- channel_id,- channel_message_search_results: vec![],- },- });-- // Should return 1000 (metadata) since no message results- assert_eq!(- channel.updated_at(),- Some(DateTime::from_timestamp(1000, 0).unwrap())- );-}--#[test]-fn test_channel_updated_at_falls_back_to_metadata_when_results_have_no_updated_at() {- let channel_id = Uuid::new_v4();-- // Channel with metadata.updated_at = 1000, message results have None for updated_at- let channel = UnifiedSearchResponseItem::Channel(ChannelSearchResponseItemWithMetadata {- metadata: Some(ChannelMetadata {- created_at: DateTime::from_timestamp(900, 0).unwrap(),- updated_at: DateTime::from_timestamp(1000, 0).unwrap(),- viewed_at: None,- interacted_at: None,- }),- extra: ChannelSearchResponseItem {- id: channel_id,- owner_id: Some("owner1".to_string()),- channel_type: "slack".to_string(),- channel_id,- channel_message_search_results: vec![- ChannelSearchResult {- message_id: Some(Uuid::new_v4()),- thread_id: None,- sender_id: Some("sender1".to_string()),- created_at: Some(DateTime::from_timestamp(1800, 0).unwrap()),- updated_at: None, // No updated_at- deleted_at: None,- highlight: SearchHighlight::default(),- score: None,- },- ChannelSearchResult {- message_id: Some(Uuid::new_v4()),- thread_id: None,- sender_id: Some("sender2".to_string()),- created_at: Some(DateTime::from_timestamp(2900, 0).unwrap()),- updated_at: None, // No updated_at- deleted_at: None,- highlight: SearchHighlight::default(),- score: None,- },- ],- },- });-- // Should return 1000 (metadata) since all message results have None for updated_at- assert_eq!(- channel.updated_at(),- Some(DateTime::from_timestamp(1000, 0).unwrap())- );-}--#[test]-fn test_sort_unified_search_results_with_channel() {- // Test that channels are sorted correctly based on max message result updated_at+fn test_channel_messages_interleave_by_own_recency() {+ // Several matching messages from one channel must not cluster at the+ // channel's newest hit: each per-message item carries its own timestamp,+ // so other entities sort in between.let doc_id = Uuid::new_v4();let channel_id = Uuid::new_v4();+ let newer_msg = Uuid::new_v4();+ let older_msg = Uuid::new_v4();let results: Vec<UnifiedSearchResponseItem> = vec![- // Document with updated_at = 2000+ channel_message_item(channel_id, older_msg, 1500),+ channel_message_item(channel_id, newer_msg, 3000),+ // Document with updated_at = 2000, between the two messagesUnifiedSearchResponseItem::Document(DocumentSearchResponseItemWithMetadata {properties: None,metadata: Some(DocumentMetadata {@@ -313,40 +206,17 @@ fn test_sort_unified_search_results_with_channel() {document_search_results: vec![],},}),- // Channel with metadata.updated_at = 1000, but message result has updated_at = 3000- UnifiedSearchResponseItem::Channel(ChannelSearchResponseItemWithMetadata {- metadata: Some(ChannelMetadata {- created_at: DateTime::from_timestamp(900, 0).unwrap(),- updated_at: DateTime::from_timestamp(1000, 0).unwrap(), // Would be sorted after document if this was used- viewed_at: None,- interacted_at: None,- }),- extra: ChannelSearchResponseItem {- id: channel_id,- owner_id: Some("owner1".to_string()),- channel_type: "slack".to_string(),- channel_id,- channel_message_search_results: vec![ChannelSearchResult {- message_id: Some(Uuid::new_v4()),- thread_id: None,- sender_id: Some("sender1".to_string()),- created_at: Some(DateTime::from_timestamp(2900, 0).unwrap()),- updated_at: Some(DateTime::from_timestamp(3000, 0).unwrap()), // Should make channel sort first- deleted_at: None,- highlight: SearchHighlight::default(),- score: None,- }],- },- }),];- // Channel should be first because its message result has updated_at = 3000- let expected_ids: Vec<Uuid> = vec![channel_id, doc_id];-let results = sort_unified_search_results(results);- assert_eq!(- results.iter().map(|r| r.entity_id()).collect::<Vec<Uuid>>(),- expected_ids- );+ let message_id = |item: &UnifiedSearchResponseItem| match item {+ UnifiedSearchResponseItem::ChannelMessage(m) => Some(m.message_id),+ _ => None,+ };++ assert_eq!(results.len(), 3);+ assert_eq!(message_id(&results[0]), Some(newer_msg));+ assert_eq!(results[1].entity_id(), doc_id);+ assert_eq!(message_id(&results[2]), Some(older_msg));}