use crate::{ContextError, ContextRequest, ContextSnapshot, TokenCounter, digest};
use serde::Serialize;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SelectionSignal {
Required,
LexicalMatch,
DeclarationMatch,
SemanticCandidate,
GraphExpansion,
}
#[derive(Clone, Eq, PartialEq, Serialize)]
pub struct SelectionStep {
pub root_id: String,
pub added_ids: Vec<String>,
pub signals: Vec<SelectionSignal>,
}
impl std::fmt::Debug for SelectionStep {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SelectionStep")
.field("added_documents", &self.added_ids.len())
.finish_non_exhaustive()
}
}
#[derive(Clone, Eq, PartialEq, Serialize)]
pub struct SelectionExplanation {
pub schema: &'static str,
pub snapshot_id: String,
pub request_id: String,
pub checked_graph_revision: u64,
pub tokenizer: String,
pub steps: Vec<SelectionStep>,
}
impl std::fmt::Debug for SelectionExplanation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SelectionExplanation")
.field("steps", &self.steps.len())
.finish_non_exhaustive()
}
}
impl SelectionExplanation {
pub(crate) fn new(
snapshot: &ContextSnapshot,
request: &ContextRequest,
checked_graph_revision: u64,
tokenizer: &impl TokenCounter,
steps: Vec<SelectionStep>,
) -> Result<Self, ContextError> {
Ok(Self {
schema: "cigar.context-selection-explanation.v1",
snapshot_id: snapshot.id().to_owned(),
request_id: digest("cigar.context-explanation-request.v1", request)?,
checked_graph_revision,
tokenizer: tokenizer.identity().to_owned(),
steps,
})
}
}