-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquery_context.rs
More file actions
203 lines (177 loc) · 5.59 KB
/
Copy pathquery_context.rs
File metadata and controls
203 lines (177 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright (c) 2026 vectorless developers
// SPDX-License-Identifier: Apache-2.0
//! Query context for the Engine API.
//!
//! [`QueryContext`] encapsulates all parameters for a query operation,
//! supporting specific documents or entire workspace queries.
//!
//! # Example
//!
//! ```rust
//! use vectorless::client::QueryContext;
//!
//! // Query specific documents
//! let ctx = QueryContext::new("What is the total revenue?")
//! .with_doc_ids(vec!["doc-1".to_string()]);
//!
//! // Query entire workspace
//! let ctx = QueryContext::new("Explain the algorithm");
//! ```
use crate::config::Config;
use crate::retrieval::{RetrieveOptions, StrategyPreference};
/// Query scope — determines which documents to search.
#[derive(Debug, Clone)]
pub(crate) enum QueryScope {
/// Query specific documents.
Documents(Vec<String>),
/// Query all documents in the workspace.
Workspace,
}
/// Context for a query operation.
///
/// Supports two scopes:
/// - **Specific documents** — via `with_doc_ids()`
/// - **Entire workspace** — default when no scope is set
///
/// # Convenience
///
/// Implements `From<String>` and `From<&str>` for quick construction:
///
/// ```rust
/// use vectorless::client::QueryContext;
///
/// let ctx: QueryContext = "What is this?".into();
/// ```
#[derive(Debug, Clone)]
pub struct QueryContext {
/// The query text.
pub(crate) query: String,
/// Target scope.
pub(crate) scope: QueryScope,
/// Maximum tokens for the result content.
pub(crate) max_tokens: Option<usize>,
/// Retrieval strategy override.
pub(crate) strategy: Option<StrategyPreference>,
/// Whether to include the reasoning chain in the result.
pub(crate) include_reasoning: bool,
/// Maximum tree traversal depth.
pub(crate) depth_limit: Option<usize>,
}
impl QueryContext {
/// Create a new query context (defaults to workspace scope).
pub fn new(query: impl Into<String>) -> Self {
Self {
query: query.into(),
scope: QueryScope::Workspace,
max_tokens: None,
strategy: None,
include_reasoning: true,
depth_limit: None,
}
}
/// Set scope to specific documents.
///
/// Pass a single ID or multiple IDs to restrict the query
/// to those documents only.
pub fn with_doc_ids(mut self, doc_ids: Vec<String>) -> Self {
self.scope = QueryScope::Documents(doc_ids);
self
}
/// Set scope to entire workspace.
pub fn with_workspace(mut self) -> Self {
self.scope = QueryScope::Workspace;
self
}
/// Set the maximum tokens for the result content.
pub fn with_max_tokens(mut self, tokens: usize) -> Self {
self.max_tokens = Some(tokens);
self
}
/// Set the retrieval strategy.
pub fn with_strategy(mut self, strategy: StrategyPreference) -> Self {
self.strategy = Some(strategy);
self
}
/// Set whether to include the reasoning chain.
pub fn with_include_reasoning(mut self, include: bool) -> Self {
self.include_reasoning = include;
self
}
/// Set the maximum tree traversal depth.
pub fn with_depth_limit(mut self, depth: usize) -> Self {
self.depth_limit = Some(depth);
self
}
/// Convert to internal `RetrieveOptions`, merging with engine config.
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.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_single_doc_scope() {
let ctx = QueryContext::new("test").with_doc_ids(vec!["doc-1".to_string()]);
assert!(
matches!(ctx.scope, QueryScope::Documents(ref ids) if ids == &["doc-1".to_string()])
);
}
#[test]
fn test_multi_doc_scope() {
let ctx = QueryContext::new("test").with_doc_ids(vec!["a".into(), "b".into()]);
assert!(matches!(ctx.scope, QueryScope::Documents(ref ids) if ids.len() == 2));
}
#[test]
fn test_workspace_scope() {
let ctx = QueryContext::new("test");
assert!(matches!(ctx.scope, QueryScope::Workspace));
}
#[test]
fn test_builder_options() {
let ctx = QueryContext::new("test")
.with_doc_ids(vec!["doc-1".to_string()])
.with_max_tokens(4000)
.with_include_reasoning(false)
.with_depth_limit(5);
assert_eq!(ctx.max_tokens, Some(4000));
assert!(!ctx.include_reasoning);
assert_eq!(ctx.depth_limit, Some(5));
}
}