Cotton code
131 lines · 5.3 KB · RustRaw
//! The query half of the meaning lane — turning what somebody asked into the//! vector the scan compares against, and saying plainly when it cannot.//!//! D8 the scan, D13 off by default, D14 the model change, D19/D20 the read//! surfaces and what they say when the lane cannot answer.//!//! THE ROUND TRIP IS NOT HERE (design P3). What is here is the part decided//! WITHOUT one — the switch, the two tables, the generation and an empty//! question — because `search_records` asks all four synchronously before it//! reaches for `ToolServices::embed_query`, and a tool handler cannot link//! `cotton-server`. `cotton_server::embed::query` holds the async half and//! re-exports this one, so `crate::embed::query::…` still resolves there.//!//! The engine (`search_everything_file`) is pure, synchronous and driver-in, and//! embedding a query is a network round-trip to a third party. So the round-trip//! happens HERE, once, before the engine is called, and the engine is handed a//! vector.//!//! FOUR ways this lane has nothing to say, and every one of them is an ANSWER,//! never a thrown error (D20): a tool failure makes an assistant retry, and//! three of the four are settled states rather than faults. The fourth — the//! provider being unreachable — is a fault, and it is reported as an absence//! with its reason attached rather than as a silently shorter result list. The//! text lane's hits are not withheld because the meaning lane could not run.use cotton_store::driver::Driver;use crate::embed::state::{active_embed_model, embed_tables_present};use crate::embed::switch::semantic_search_enabled;/// Names `_ai_logs.function_name` for a query embedding, apart from the index.pub const EMBED_QUERY_FUNCTION: &str = "cotton-server.embed-query";/// Why the meaning lane has nothing to say.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum MeaningLaneAbsence {    /// The owner has not turned semantic search on. The default, and it stays    /// the default: this is the largest single widening of the third-party    /// content surface Cotton makes, and it is opt-in with a paragraph (D13).    Off,    /// On, but no vector has been written yet. The indexer is a sweep, so the    /// gap between the switch and the first answer is real and worth naming    /// rather than passing off as "nothing matched".    Empty,    /// The provider said no, or this server has no embedder at all.    Unreachable,}impl MeaningLaneAbsence {    pub fn as_str(self) -> &'static str {        match self {            MeaningLaneAbsence::Off => "off",            MeaningLaneAbsence::Empty => "empty",            MeaningLaneAbsence::Unreachable => "unreachable",        }    }}/// What the lane answered.////// `Ready` carries the vector OWNED, where the engine's own `SemanticQuery`/// borrows: the vector outlives this call and the engine's does not, so the/// caller builds the borrowed struct from these two fields at the point of use.#[derive(Debug, Clone, PartialEq)]pub enum MeaningLane {    Ready {        /// The query vector, quantised exactly as the stored ones were.        vector: Vec<u8>,        /// `_embed_state.active_model` — the generation the file's vectors are.        model: String,    },    Absent {        reason: MeaningLaneAbsence,        /// One sentence, for a person or an assistant to read.        note: String,    },}impl MeaningLane {    /// The absence, built. Public because the ROUND TRIP half lives in    /// `cotton_server::embed::query` (design P3) and answers three more    /// absences of its own — an unreachable provider, a refusal, a vector that    /// would not quantise — and a second constructor there would be a second    /// shape of "the lane has nothing to say".    pub fn absent_because(reason: MeaningLaneAbsence, note: &str) -> MeaningLane {        MeaningLane::Absent {            reason,            note: note.to_string(),        }    }}/// The three settled answers, decided WITHOUT a network call.////// Split out so `search_records` can ask them synchronously and only reach for/// the provider when the file really is ready — and so the switch, the tables/// and the generation are read in ONE place rather than once per read surface.pub fn resolve_meaning_lane_offline(    db: &Driver,    workspace_id: &str,    query: &str,) -> Result<String, MeaningLane> {    if !semantic_search_enabled(db, workspace_id) {        return Err(MeaningLane::absent_because(            MeaningLaneAbsence::Off,            "Search by meaning is off for this database. The owner can turn it on under AI plan & \             keys.",        ));    }    if !embed_tables_present(db) {        return Err(MeaningLane::absent_because(            MeaningLaneAbsence::Empty,            "Search by meaning is on, but nothing has been indexed yet.",        ));    }    let Some(model) = active_embed_model(db) else {        return Err(MeaningLane::absent_because(            MeaningLaneAbsence::Empty,            "Search by meaning is on, but nothing has been indexed yet.",        ));    };    if query.trim().is_empty() {        // Not a fault and not a state: an empty question has no meaning to        // match.        return Err(MeaningLane::absent_because(            MeaningLaneAbsence::Empty,            "There was nothing to search for.",        ));    }    Ok(model)}