use super::{Entity, Note, NoteFtsScalars, SubstrateKind, TextDocument};
pub fn entity_embedding_text(entity: &Entity) -> String {
match &entity.description {
Some(description) if !description.is_empty() => {
format!("{} {description}", entity.name)
}
_ => entity.name.clone(),
}
}
pub fn note_embedding_text(note: &Note) -> String {
note_embedding_text_ref(note).to_owned()
}
pub(crate) fn note_embedding_text_ref(note: &Note) -> &str {
¬e.content
}
pub fn entity_fts_document(entity: &Entity) -> TextDocument {
let updated_at =
chrono::DateTime::from_timestamp_micros(entity.updated_at).unwrap_or_else(chrono::Utc::now);
TextDocument {
subject_id: entity.id,
kind: SubstrateKind::Entity,
record_kind: Some(entity.kind.clone()),
title: Some(entity.name.clone()),
body: entity_embedding_text(entity),
tags: entity.tags.clone(),
namespace: entity.namespace.clone(),
metadata: entity.properties.clone(),
updated_at,
}
}
pub fn note_fts_document(note: &Note) -> TextDocument {
let body = match ¬e.name {
Some(n) => format!("{n} {}", note.content),
None => note.content.clone(),
};
let updated_at =
chrono::DateTime::from_timestamp_micros(note.updated_at).unwrap_or_else(chrono::Utc::now);
TextDocument {
subject_id: note.id,
kind: SubstrateKind::Note,
record_kind: Some(note.kind.clone()),
title: note.name.clone(),
body,
tags: vec![],
namespace: note.namespace.clone(),
metadata: note.properties.clone(),
updated_at,
}
}
pub(crate) fn note_fts_scalars(note: &Note) -> NoteFtsScalars {
let doc = note_fts_document(note);
NoteFtsScalars {
record_kind: doc.record_kind.unwrap_or_default(),
title: doc.title.unwrap_or_default(),
body: doc.body,
tags: "[]".to_string(),
metadata: doc
.metadata
.as_ref()
.map(|v| serde_json::to_string(v).unwrap_or_default()),
updated_at_micros: doc.updated_at.timestamp_micros(),
}
}