kiromi-ai-memory 0.2.2

Local-first multi-tenant memory store engine: Markdown/text content on object storage, metadata in SQLite, plugin-shaped embedder/storage/metadata, hybrid text+vector search.
Documentation
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! Reranker plugin trait — interface stub for slice 1.
//!
//! Slice 1 ships **no implementation**. The trait exists so a future plan
//! adding hosted rerankers (Cohere, Voyage), cross-encoders, or LLM-judges
//! can land without breaking SemVer on `kiromi-ai-memory`. RRF stays
//! hard-coded inside `Memory::search` (see spec § 10).
//!
//! See spec § 12.9.

use async_trait::async_trait;

use crate::error::Result;

/// Reranks search candidates against a query.
#[async_trait]
pub trait Reranker: Send + Sync + std::fmt::Debug + 'static {
    /// Stable identifier — analogous to `Embedder::id`.
    /// Convention: `"<family>:<model>:<version>"`.
    fn id(&self) -> &str;

    /// Score each candidate against the query. Returns one `f32` per
    /// candidate, in input order. Higher is better. Implementations should
    /// L2- or sigmoid-bound the scores so callers can fuse with other
    /// rankers without scale shock.
    async fn rerank(&self, query: &str, candidates: &[&str]) -> Result<Vec<f32>>;
}

#[cfg(test)]
mod tests {
    use super::*;

    /// In-tree test impl proving the trait is object-safe and that a future
    /// plan can wire one through `Box<dyn Reranker>` without further trait
    /// surgery.
    #[derive(Debug)]
    struct ConstantReranker;

    #[async_trait]
    impl Reranker for ConstantReranker {
        fn id(&self) -> &str {
            "test:constant:v1"
        }
        async fn rerank(&self, _q: &str, candidates: &[&str]) -> Result<Vec<f32>> {
            Ok(candidates.iter().map(|_| 0.5_f32).collect())
        }
    }

    #[tokio::test]
    async fn object_safe_and_callable() {
        let boxed: Box<dyn Reranker> = Box::new(ConstantReranker);
        let s = boxed.rerank("q", &["a", "b", "c"]).await.unwrap();
        assert_eq!(s, vec![0.5, 0.5, 0.5]);
        assert_eq!(boxed.id(), "test:constant:v1");
    }
}