use crate::config::Config;
use crate::retrieval::{RetrieveOptions, StrategyPreference};
#[derive(Debug, Clone)]
pub struct QueryContext {
pub(crate) query: String,
pub(crate) doc_id: Option<String>,
pub(crate) max_tokens: Option<usize>,
pub(crate) strategy: Option<StrategyPreference>,
pub(crate) include_reasoning: bool,
pub(crate) depth_limit: Option<usize>,
}
impl QueryContext {
pub fn new(query: impl Into<String>) -> Self {
Self {
query: query.into(),
doc_id: None,
max_tokens: None,
strategy: None,
include_reasoning: true,
depth_limit: None,
}
}
pub fn with_doc_id(mut self, doc_id: impl Into<String>) -> Self {
self.doc_id = Some(doc_id.into());
self
}
pub fn with_max_tokens(mut self, tokens: usize) -> Self {
self.max_tokens = Some(tokens);
self
}
pub fn with_strategy(mut self, strategy: StrategyPreference) -> Self {
self.strategy = Some(strategy);
self
}
pub fn with_include_reasoning(mut self, include: bool) -> Self {
self.include_reasoning = include;
self
}
pub fn with_depth_limit(mut self, depth: usize) -> Self {
self.depth_limit = Some(depth);
self
}
pub(crate) fn to_retrieve_options(&self, config: &Config) -> RetrieveOptions {
let mut opts = RetrieveOptions::new()
.with_top_k(config.retrieval.top_k)
.with_include_content(true)
.with_include_summaries(true);
if let Some(max_tokens) = self.max_tokens {
opts = opts.with_max_tokens(max_tokens);
}
if let Some(strategy) = &self.strategy {
opts = opts.with_strategy(strategy.clone());
}
opts
}
}
impl From<String> for QueryContext {
fn from(query: String) -> Self {
Self::new(query)
}
}
impl From<&str> for QueryContext {
fn from(query: &str) -> Self {
Self::new(query)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_query_context_new() {
let ctx = QueryContext::new("What is this?");
assert_eq!(ctx.query, "What is this?");
assert!(ctx.doc_id.is_none());
assert!(ctx.include_reasoning);
}
#[test]
fn test_query_context_from_string() {
let ctx: QueryContext = "Hello".to_string().into();
assert_eq!(ctx.query, "Hello");
}
#[test]
fn test_query_context_from_str() {
let ctx: QueryContext = "Hello".into();
assert_eq!(ctx.query, "Hello");
}
#[test]
fn test_query_context_builder() {
let ctx = QueryContext::new("test")
.with_doc_id("doc-1")
.with_max_tokens(4000)
.with_include_reasoning(false)
.with_depth_limit(5);
assert_eq!(ctx.doc_id, Some("doc-1".to_string()));
assert_eq!(ctx.max_tokens, Some(4000));
assert!(!ctx.include_reasoning);
assert_eq!(ctx.depth_limit, Some(5));
}
}