Skip to main content

Module utils

Module utils 

Source

Enums§

SanitizeAction
Sanitize result: allow / redact (content cleaned) / discard (reject write).

Functions§

agent_source
Resolve the agent-product identity (which AI agent tool drives this binary — e.g. claude-code, codex, opencode, gemini-cli) from the INNATE_AGENT env var. The caller (MCP config / hook / shell) injects it; the binary cannot know it otherwise. Returns None when unset/blank so the agent column stays NULL (backward compatible). This is orthogonal to the access channel recorded in usage_trace.source / episodic_log.event_source (mcp/cli/hook/…). Trimmed and length-capped; intentionally not enum-constrained.
content_hash
cosine_similarity
Cosine similarity between two equal-length slices. Returns 0.0 on zero norms. Single-pass fold: computes dot product and both norms in one traversal.
dot_product
Dot product of two equal-length slices. For unit vectors this equals the cosine similarity.
estimate_tokens
Rough token estimate: 1 token ≈ 4 chars.
gen_uuid
hex
Lowercase hex encoding of a byte slice. Replaces the old format!("{:x}", …) over a digest output: RustCrypto digest 0.11 returns hybrid_array::Array, which no longer implements LowerHex.
l2_normalize
In-place L2 normalisation. Zero vectors are left unchanged (all zeros). Pre-normalising stored vectors lets vector search reduce cosine similarity to a plain dot product in its O(N) inner loop.
pack_embedding
Pack a Vec into bytes (little-endian f32 array).
sanitize
Public sanitize function used by KnowledgeBase (§二·六). Returns (cleaned_content, action).
unpack_embedding
Unpack bytes into Vec.
utc_now_iso