use std::collections::HashMap;
use serde::Deserialize;
use serde_json::{json, Value};
use uuid::Uuid;
use khive_brain_core::{resolve_consumer_profile, ConsumerKind};
use khive_runtime::{KhiveRuntime, NamespaceToken, RequestIdentity, RuntimeError, VerbRegistry};
use khive_storage::EdgeRelation;
use crate::KnowledgePack;
use khive_runtime::deser_params as deser;
fn short_id(uuid: Uuid) -> String {
uuid.as_hyphenated().to_string().chars().take(8).collect()
}
pub(crate) async fn resolve_uuid(
s: &str,
runtime: &KhiveRuntime,
token: &NamespaceToken,
) -> Result<Uuid, RuntimeError> {
runtime.resolve_uuid_or_prefix(token, s).await
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct LearnParams {
#[serde(default)]
name: Option<String>,
#[serde(default, alias = "content")]
description: Option<String>,
#[serde(default)]
domain: Option<String>,
#[serde(default)]
tags: Option<Vec<String>>,
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct CiteParams {
concept_id: String,
source_id: String,
#[serde(default)]
weight: Option<f64>,
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct TopicParams {
#[serde(default)]
domain: Option<String>,
#[serde(default)]
query: Option<String>,
#[serde(default)]
limit: Option<u32>,
}
impl KnowledgePack {
pub(crate) async fn handle_learn(
&self,
token: &NamespaceToken,
params: Value,
) -> Result<Value, RuntimeError> {
let p: LearnParams = deser(params)?;
let name = match p.name.as_deref().map(str::trim).filter(|s| !s.is_empty()) {
Some(n) => n.to_string(),
None => {
let src = p.description.as_deref().unwrap_or("").trim().to_string();
if src.is_empty() {
return Err(RuntimeError::InvalidInput(
"name must not be empty (provide 'name' or 'content')".to_string(),
));
}
if src.chars().count() <= 60 {
src.clone()
} else {
let byte_limit = src
.char_indices()
.nth(60)
.map(|(i, _)| i)
.unwrap_or(src.len());
let boundary = src[..byte_limit]
.rfind(char::is_whitespace)
.unwrap_or(byte_limit);
src[..boundary].trim_end().to_string()
}
}
};
let domain_norm: Option<String> = p
.domain
.as_ref()
.map(|d| d.trim().to_lowercase())
.filter(|d| !d.is_empty());
let properties = domain_norm.as_ref().map(|d| json!({ "domain": d }));
let mut tags = p.tags.unwrap_or_default();
if let Some(d) = &domain_norm {
if !tags.contains(d) {
tags.push(d.clone());
}
}
let (entity, embedding_report) = self
.runtime
.core()
.create_entity_with_embedding_report(
token,
"concept",
None,
&name,
p.description.as_deref(),
properties,
tags.clone(),
)
.await?;
let mut response = json!({
"id": short_id(entity.id),
"full_id": entity.id.as_hyphenated().to_string(),
"kind": "concept",
"name": entity.name,
"description": entity.description,
"domain": domain_norm,
"tags": entity.tags,
"namespace": entity.namespace,
});
if embedding_report.any_truncated() {
response["warnings"] =
json!([khive_runtime::retrieval::EMBEDDING_INPUT_TRUNCATED_WARNING]);
}
Ok(response)
}
pub(crate) async fn handle_cite(
&self,
token: &NamespaceToken,
params: Value,
) -> Result<Value, RuntimeError> {
let p: CiteParams = deser(params)?;
let core = self.runtime.core();
let concept_id = resolve_uuid(&p.concept_id, &core, token).await?;
let source_id = resolve_uuid(&p.source_id, &core, token).await?;
let weight = p.weight.unwrap_or(1.0).clamp(0.0, 1.0);
let edge = core
.link(
token,
concept_id,
source_id,
EdgeRelation::IntroducedBy,
weight,
None,
)
.await?;
Ok(json!({
"id": short_id(edge.id.0),
"full_id": edge.id.0.as_hyphenated().to_string(),
"relation": "introduced_by",
"concept_id": concept_id.as_hyphenated().to_string(),
"source_id": source_id.as_hyphenated().to_string(),
"weight_requested": p.weight,
"weight": weight,
}))
}
pub(crate) async fn handle_topic(
&self,
token: &NamespaceToken,
params: Value,
) -> Result<Value, RuntimeError> {
let p: TopicParams = deser(params)?;
let core = self.runtime.core();
let limit = p.limit.unwrap_or(20).min(100);
let domain_filter = p
.domain
.as_deref()
.map(|d| d.trim().to_lowercase())
.filter(|d| !d.is_empty());
if let Some(ref query) = p.query {
let hits = core
.hybrid_search(
token,
query,
None,
limit * 4,
Some("concept"),
None,
&[],
None,
)
.await?;
let hit_ids: Vec<Uuid> = hits.iter().map(|h| h.entity_id).collect();
let entity_map: std::collections::HashMap<Uuid, _> = if !hit_ids.is_empty() {
core.get_entities_by_ids(token, &hit_ids)
.await?
.into_iter()
.map(|e| (e.id, e))
.collect()
} else {
std::collections::HashMap::new()
};
let filtered: Vec<_> = hits
.into_iter()
.filter_map(|h| {
let entity = entity_map.get(&h.entity_id)?;
if let Some(ref d) = domain_filter {
if !entity.tags.iter().any(|t| t.eq_ignore_ascii_case(d)) {
return None;
}
}
Some((h, entity))
})
.collect();
let candidate_window_count = filtered.len();
let results: Vec<Value> = filtered
.into_iter()
.take(limit as usize)
.map(|(h, entity)| {
let mut item = json!({
"id": short_id(entity.id),
"full_id": entity.id.as_hyphenated().to_string(),
"name": entity.name,
"description": entity.description,
"tags": entity.tags,
"score": h.score.to_f64(),
});
if let Some(snippet) = h.snippet {
item["snippet"] = serde_json::Value::String(snippet);
}
item
})
.collect();
Ok(json!({ "results": results, "candidate_window_count": candidate_window_count }))
} else {
let total = core
.count_entities_tagged(token, Some("concept"), domain_filter.as_deref())
.await?;
let entities = core
.list_entities_tagged(token, Some("concept"), domain_filter.as_deref(), limit, 0)
.await?;
let results: Vec<Value> = entities
.into_iter()
.map(|e| {
json!({
"id": short_id(e.id),
"full_id": e.id.as_hyphenated().to_string(),
"name": e.name,
"description": e.description,
"tags": e.tags,
})
})
.collect();
Ok(json!({ "results": results, "total": total }))
}
}
}
impl KnowledgePack {
pub(crate) async fn resolve_compose_type_weights(
&self,
registry: &VerbRegistry,
token: &NamespaceToken,
) -> HashMap<String, f32> {
let ns = token.namespace().as_str().to_string();
if let Some(ref profile_id) = self.brain_profile {
if let Some(weights) = load_profile_type_weights(registry, profile_id, token).await {
return weights;
}
}
if let Some(profile_id) =
resolve_consumer_profile(registry, token, ConsumerKind::KnowledgeCompose).await
{
if let Some(weights) = load_profile_type_weights(registry, &profile_id, token).await {
return weights;
}
}
if let Ok(states) = self.section_posteriors.lock() {
if let Some(state) = states.get(&ns) {
return state
.deterministic_weights()
.into_iter()
.map(|(st, w)| (st.as_str().to_string(), w as f32))
.collect();
}
}
khive_brain_core::SectionPosteriorState::default()
.deterministic_weights()
.into_iter()
.map(|(st, w)| (st.as_str().to_string(), w as f32))
.collect()
}
}
async fn load_profile_type_weights(
registry: &VerbRegistry,
profile_id: &str,
token: &NamespaceToken,
) -> Option<HashMap<String, f32>> {
let namespace = token.namespace().as_str();
let result = registry
.dispatch_with_identity(
"brain.profile",
json!({ "profile_id": profile_id, "namespace": namespace }),
Some(RequestIdentity::from_token(token)),
)
.await
.ok()?;
let sections = result.get("section_posteriors")?.as_object()?;
let weights: HashMap<String, f32> = sections
.iter()
.filter_map(|(name, val)| {
let w = val.get("weight")?.as_f64()? as f32;
Some((name.clone(), w))
})
.collect();
if weights.is_empty() {
None
} else {
Some(weights)
}
}
#[cfg(test)]
#[path = "handlers_tests.rs"]
mod tests;