khive_runtime/curation/embedding_text.rs
1use super::{Entity, Note, SubstrateKind, TextDocument};
2
3// ---------------------------------------------------------------------------
4// FTS document construction
5// ---------------------------------------------------------------------------
6
7/// Build the canonical text embedded for an entity on create, update, merge,
8/// and repair paths.
9///
10/// The text is the name, then each alias in the record's order, then the
11/// description when it is non-empty, joined by single spaces. An entity with
12/// no aliases gets exactly the text it got before aliases existed.
13pub fn entity_embedding_text(entity: &Entity) -> String {
14 let mut text = entity.name.clone();
15 for alias in &entity.aliases {
16 text.push(' ');
17 text.push_str(&alias.alias);
18 }
19 if let Some(description) = entity.description.as_deref().filter(|d| !d.is_empty()) {
20 text.push(' ');
21 text.push_str(description);
22 }
23 text
24}
25
26/// Build the canonical text embedded for a note when no explicit bounded
27/// embedding prefix was supplied at creation time.
28pub fn note_embedding_text(note: &Note) -> String {
29 note_embedding_text_ref(note).to_owned()
30}
31
32/// Borrow the canonical note embedding text for runtime paths that do not
33/// require ownership.
34pub(crate) fn note_embedding_text_ref(note: &Note) -> &str {
35 ¬e.content
36}
37
38/// Build the `TextDocument` for an entity. This is the single source of truth for
39/// entity FTS document shape; all write paths (create, update, merge, reindex, backfill)
40/// must go through this function so search parity is guaranteed.
41///
42/// Body rule: the body is [`entity_embedding_text`]: the name, then the
43/// entity's aliases, then the description when it is non-empty. The title is
44/// the name alone. This matches the FTS index contract: `title` and `body` are
45/// the ranked columns; `tags`, `metadata`, and `namespace` are UNINDEXED.
46///
47/// `updated_at` is taken from the entity's own timestamp so that backfill and
48/// reindex runs record the entity's actual mutation time rather than the
49/// reindex execution time.
50pub fn entity_fts_document(entity: &Entity) -> TextDocument {
51 let updated_at =
52 chrono::DateTime::from_timestamp_micros(entity.updated_at).unwrap_or_else(chrono::Utc::now);
53 TextDocument {
54 subject_id: entity.id,
55 kind: SubstrateKind::Entity,
56 record_kind: Some(entity.kind.clone()),
57 title: Some(entity.name.clone()),
58 body: entity_embedding_text(entity),
59 tags: entity.tags.clone(),
60 namespace: entity.namespace.clone(),
61 metadata: entity.properties.clone(),
62 updated_at,
63 }
64}
65
66/// Build the `TextDocument` for a note. This is the single source of truth for
67/// note FTS document shape; all write paths (create, update, reindex) must go
68/// through this function so recall parity is guaranteed. Changes here apply to
69/// every caller automatically.
70///
71/// Body rule: when the note has a `name`, prepend it to the content
72/// (`"<name> <content>"`). This matches the FTS index contract: title and body
73/// both contribute to ranking, and the name is the most salient signal.
74///
75/// `updated_at` is taken from the note's own timestamp (not `Utc::now()`) so
76/// that backfill and reindex runs record the note's actual mutation time rather
77/// than the reindex execution time.
78pub fn note_fts_document(note: &Note) -> TextDocument {
79 let body = match ¬e.name {
80 Some(n) => format!("{n} {}", note.content),
81 None => note.content.clone(),
82 };
83 let updated_at =
84 chrono::DateTime::from_timestamp_micros(note.updated_at).unwrap_or_else(chrono::Utc::now);
85 TextDocument {
86 subject_id: note.id,
87 kind: SubstrateKind::Note,
88 record_kind: Some(note.kind.clone()),
89 title: note.name.clone(),
90 body,
91 tags: vec![],
92 namespace: note.namespace.clone(),
93 metadata: note.properties.clone(),
94 updated_at,
95 }
96}