Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

5.3 KB
Raw Blame
use chrono::{DateTime, Utc};
use document_sub_type::DocumentSubType;
use item_filters::DocumentFilters;
use models_soup::SoupProperty;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

use crate::{MatchType, SearchHighlight, SearchOn};

/// A document match for a given node
#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]
pub struct DocumentSearchResult {
    /// The node id for the document.
    /// This is only useful for markdown at the moment
    /// This will only be provided if the match was on content
    pub node_id: Option<String>,
    /// The highlights for the document
    pub highlight: SearchHighlight,
    /// The raw content of the document.
    /// This is only included for markdown files and will be the raw json node of the match
    pub raw_content: Option<String>,
    /// The score of the result
    #[serde(skip_serializing_if = "Option::is_none")]
    pub score: Option<f64>,
}

/// A single response item, part of the DocumentSearchResponse object
#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]
pub struct DocumentSearchResponseItem {
    /// Standardized fields that all item types will share.
    /// These field names are being aligned across all item types
    /// for consistency in our data model.
    pub id: uuid::Uuid,
    pub name: String,
    pub owner_id: String,
    /// The id of the document
    pub document_id: uuid::Uuid,
    /// The name of the document
    pub document_name: String,
    /// The file type of the document
    pub file_type: Option<String>,
    /// The sub type of the document if present.
    pub sub_type: Option<DocumentSubType>,
    /// The search results for the document
    /// This may be empty if the search result match was on the document name only
    pub document_search_results: Vec<DocumentSearchResult>,
}

/// Metadata for a document fetched from the database
#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]
pub struct DocumentMetadata {
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub viewed_at: Option<DateTime<Utc>>,
    pub project_id: Option<String>,
    pub deleted_at: Option<DateTime<Utc>>,
}

/// DocumentSearchResponseItem object with document metadata we fetch from macrodb. we don't store these
/// timestamps in opensearch as they would require us to update document page record
/// every time the document updates (specifically for updated_at and viewed_at)
#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]
pub struct DocumentSearchResponseItemWithMetadata {
    /// Metadata from the database. None if the document doesn't exist in the database.
    pub metadata: Option<DocumentMetadata>,
    /// Entity properties (assignees, status, priority, etc.) for markdown documents.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(skip)]
    pub properties: Option<Vec<SoupProperty>>,
    #[serde(flatten)]
    pub extra: DocumentSearchResponseItem,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct DocumentSearchMetadata {
    /// The document version id.
    pub document_id: String,
    /// The name of the document
    pub document_name: String,
    /// The id of the owner of the document
    pub owner_id: String,
    /// The file type of the document
    pub file_type: Option<String>,
}

/// The document search response object
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct DocumentSearchResponse {
    /// List containing results from documents
    pub results: Vec<DocumentSearchResponseItemWithMetadata>,
}

#[derive(Debug, Serialize, Deserialize, ToSchema, JsonSchema)]
pub struct SimpleDocumentSearchResponseBaseItem<T> {
    /// The document id
    pub document_id: String,
    /// The document name
    pub document_name: String,
    /// The node id
    pub node_id: String,
    /// The owner id
    pub owner_id: String,
    /// The file type
    pub file_type: String,
    #[schema(inline)]
    /// The time the document was last updated
    pub updated_at: T,
    /// The highlights on the document
    pub highlight: SearchHighlight,
    /// The raw content of the document
    pub raw_content: Option<String>,
}

pub type SimpleDocumentSearchResponseItem =
    SimpleDocumentSearchResponseBaseItem<crate::HumanReadableTimestamp>;

#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct SimpleDocumentSearchResponse {
    /// List containing results from documents.
    /// Each item in the list is for a specific page/node of a document.
    pub results: Vec<SimpleDocumentSearchResponseItem>,
}

#[derive(Serialize, Deserialize, Debug, ToSchema, JsonSchema)]
pub struct DocumentSearchRequest {
    /// The query to search for
    #[serde(skip_serializing_if = "Option::is_none")]
    pub query: Option<String>,
    /// Multiple terms to search over
    #[serde(skip_serializing_if = "Option::is_none")]
    pub terms: Option<Vec<String>>,
    /// The match type to use when searching
    pub match_type: MatchType,
    /// Search filters for documents
    #[serde(flatten)]
    pub filters: Option<DocumentFilters>,
    /// Fields to search on (Name, Content, NameContent). Defaults to Content
    #[serde(default)]
    pub search_on: SearchOn,
    /// If true, returns only 1 result per entity. False by default.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub collapse: Option<bool>,
}

#[cfg(test)]
mod test;