khive_pack_knowledge/
lib.rs1pub(crate) mod handlers;
4pub(crate) mod knowledge;
5mod pack;
6mod vocab;
7
8pub use pack::KnowledgePack;
9
10use khive_runtime::{KhiveRuntime, NamespaceToken, RuntimeError};
11use serde_json::{json, Value};
12
13#[derive(Debug, Clone, Copy)]
15pub struct KnowledgeReindexOptions {
16 pub atoms: bool,
18 pub sections: bool,
20 pub drop_existing: bool,
22 pub rebuild_ann: bool,
24 pub batch_size: Option<u32>,
26}
27
28pub async fn reindex_knowledge(
39 runtime: &KhiveRuntime,
40 token: &NamespaceToken,
41 opts: KnowledgeReindexOptions,
42 on_atom_progress: Option<&(dyn Fn(u64, u64) + Send + Sync)>,
43 on_section_progress: Option<&(dyn Fn(u64, u64) + Send + Sync)>,
44) -> Result<Value, RuntimeError> {
45 let mut atoms_indexed = 0u64;
46 let mut failed = 0u64;
47 let mut ann_failed = false;
48 let mut truncation_by_model = serde_json::Map::new();
49 if opts.atoms {
50 let ann = knowledge::vamana::new_shared();
51 let mut params = serde_json::Map::new();
52 params.insert("rebuild_ann".into(), Value::Bool(opts.rebuild_ann));
53 params.insert("insert_only".into(), Value::Bool(!opts.drop_existing));
54 if let Some(bs) = opts.batch_size {
55 params.insert("batch_size".into(), Value::from(bs));
56 }
57 let result = knowledge::KnowledgeHandlers::index(
58 runtime,
59 token,
60 Value::Object(params),
61 &ann,
62 on_atom_progress,
63 )
64 .await?;
65 atoms_indexed = result.get("indexed").and_then(|n| n.as_u64()).unwrap_or(0);
66 failed = result.get("failed").and_then(|n| n.as_u64()).unwrap_or(0);
67 ann_failed = result
68 .get("ann_failed")
69 .and_then(|b| b.as_bool())
70 .unwrap_or(false);
71 truncation_by_model = result
72 .get("truncation_by_model")
73 .and_then(Value::as_object)
74 .cloned()
75 .unwrap_or_default();
76 }
77
78 let mut sections_indexed = 0u64;
79 let mut sections_failed = 0u64;
80 if opts.sections {
81 let batch = opts
82 .batch_size
83 .map(|b| b as usize)
84 .unwrap_or(knowledge::util::DEFAULT_EMBED_BATCH);
85 let (indexed, _skipped, sec_failed, section_truncation) =
86 knowledge::sections_index::embed_sections(
87 runtime,
88 token,
89 opts.drop_existing,
90 batch,
91 on_section_progress,
92 None,
93 )
94 .await?;
95 sections_indexed = indexed as u64;
96 sections_failed = sec_failed as u64;
97 if section_truncation.any_truncated() {
98 let model = runtime.default_embedder_name().to_string();
99 let existing = truncation_by_model
100 .remove(&model)
101 .and_then(|value| {
102 serde_json::from_value::<khive_runtime::retrieval::EmbeddingTruncationReport>(
103 value,
104 )
105 .ok()
106 })
107 .unwrap_or_default();
108 let mut combined: khive_runtime::retrieval::EmbeddingTruncationReport = existing;
109 combined.merge(section_truncation);
110 truncation_by_model.insert(
111 model,
112 serde_json::to_value(combined)
113 .expect("EmbeddingTruncationReport serialization cannot fail"),
114 );
115 }
116 }
117
118 Ok(json!({
119 "atoms_indexed": atoms_indexed,
120 "sections_indexed": sections_indexed,
121 "failed": failed,
122 "ann_failed": ann_failed,
123 "sections_failed": sections_failed,
124 "truncation_by_model": truncation_by_model,
125 }))
126}