Skip to main content

relay_knowledge/domain/graph/retrieval/
policy.rs

1use std::{error::Error, fmt};
2
3use serde::{Deserialize, Serialize};
4
5/// Freshness policy for hybrid retrieval.
6#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
7#[serde(rename_all = "snake_case")]
8pub enum FreshnessPolicy {
9    #[default]
10    AllowStale,
11    WaitUntilFresh,
12    GraphOnly,
13}
14
15/// Retrieval path used to satisfy a query.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
17#[serde(rename_all = "snake_case")]
18pub enum RetrievalMode {
19    Hybrid,
20    GraphOnly,
21}
22
23/// Rerank backend requested for the hybrid retrieval candidate set.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
25#[serde(rename_all = "snake_case")]
26pub enum RerankMode {
27    Local,
28    External,
29    Disabled,
30}
31
32impl RerankMode {
33    /// Parses a stable environment/config value.
34    pub fn parse(value: &str) -> Result<Self, RerankModeError> {
35        match value.trim().to_ascii_lowercase().as_str() {
36            "local" => Ok(Self::Local),
37            "external" => Ok(Self::External),
38            "disabled" => Ok(Self::Disabled),
39            other => Err(RerankModeError {
40                value: other.to_owned(),
41            }),
42        }
43    }
44
45    /// Stable configuration label.
46    pub const fn as_str(self) -> &'static str {
47        match self {
48            Self::Local => "local",
49            Self::External => "external",
50            Self::Disabled => "disabled",
51        }
52    }
53}
54
55/// Invalid rerank backend mode supplied by runtime configuration.
56#[derive(Debug, Clone, PartialEq, Eq)]
57pub struct RerankModeError {
58    pub value: String,
59}
60
61impl fmt::Display for RerankModeError {
62    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
63        write!(
64            formatter,
65            "rerank backend '{}' must be local, external, or disabled",
66            self.value
67        )
68    }
69}
70
71impl Error for RerankModeError {}
72
73#[cfg(test)]
74#[path = "policy_tests.rs"]
75mod tests;