vera2-core 0.12.16-aristoddle.1

Core engine for Vera: parsing, indexing, retrieval, storage, and embedding
Documentation
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! OpenAI-compatible completion client for query expansion.

use std::collections::HashSet;
use std::time::Duration;

use anyhow::{Context, Result, anyhow};
use serde::Serialize;

const DEFAULT_TIMEOUT_SECS: u64 = 120;
const DEFAULT_MAX_ALTERNATIVES: usize = 2;
const MAX_ALLOWED_ALTERNATIVES: usize = 8;
const DEFAULT_MAX_TOKENS: u32 = 16_384;
const MIN_MAX_TOKENS: u32 = 128;
const MAX_MAX_TOKENS: u32 = 65_536;

/// Configuration for the OpenAI-compatible completion client.
#[derive(Debug, Clone)]
pub struct CompletionClientConfig {
    pub base_url: String,
    pub model_id: String,
    pub api_key: String,
    pub timeout: Duration,
    pub max_alternatives: usize,
    /// Token budget for completion output. Reasoning models may need large
    /// budgets since they emit `reasoning_content` before the final answer.
    pub max_tokens: u32,
}

impl CompletionClientConfig {
    /// Returns true when the required env vars are present.
    pub fn is_configured() -> bool {
        let has_base = std::env::var("VERA_COMPLETION_BASE_URL")
            .ok()
            .is_some_and(|v| !v.trim().is_empty());
        let has_model = std::env::var("VERA_COMPLETION_MODEL_ID")
            .ok()
            .is_some_and(|v| !v.trim().is_empty());
        has_base && has_model
    }

    /// Build config from env vars.
    ///
    /// Required: `VERA_COMPLETION_BASE_URL`, `VERA_COMPLETION_MODEL_ID`.
    /// Optional: `VERA_COMPLETION_API_KEY` (default `none`),
    /// `VERA_COMPLETION_TIMEOUT_SECS`, `VERA_COMPLETION_MAX_ALTERNATIVES`,
    /// `VERA_COMPLETION_MAX_TOKENS`.
    pub fn from_env() -> Result<Self> {
        let base_url = std::env::var("VERA_COMPLETION_BASE_URL")
            .context("VERA_COMPLETION_BASE_URL not set")?
            .trim()
            .to_string();
        let model_id = std::env::var("VERA_COMPLETION_MODEL_ID")
            .context("VERA_COMPLETION_MODEL_ID not set")?
            .trim()
            .to_string();
        let api_key = std::env::var("VERA_COMPLETION_API_KEY")
            .unwrap_or_else(|_| "none".to_string())
            .trim()
            .to_string();

        if base_url.is_empty() {
            return Err(anyhow!("VERA_COMPLETION_BASE_URL is empty"));
        }
        if model_id.is_empty() {
            return Err(anyhow!("VERA_COMPLETION_MODEL_ID is empty"));
        }

        let timeout = std::env::var("VERA_COMPLETION_TIMEOUT_SECS")
            .ok()
            .and_then(|v| v.parse::<u64>().ok())
            .filter(|v| *v > 0)
            .unwrap_or(DEFAULT_TIMEOUT_SECS);
        let max_alternatives = std::env::var("VERA_COMPLETION_MAX_ALTERNATIVES")
            .ok()
            .and_then(|v| v.parse::<usize>().ok())
            .map(|v| v.clamp(1, MAX_ALLOWED_ALTERNATIVES))
            .unwrap_or(DEFAULT_MAX_ALTERNATIVES);
        let max_tokens = std::env::var("VERA_COMPLETION_MAX_TOKENS")
            .ok()
            .and_then(|v| v.parse::<u32>().ok())
            .map(|v| v.clamp(MIN_MAX_TOKENS, MAX_MAX_TOKENS))
            .unwrap_or(DEFAULT_MAX_TOKENS);

        Ok(Self {
            base_url,
            model_id,
            api_key,
            timeout: Duration::from_secs(timeout),
            max_alternatives,
            max_tokens,
        })
    }

    fn endpoint_url(&self) -> String {
        format!("{}/chat/completions", self.base_url.trim_end_matches('/'))
    }
}

/// OpenAI-compatible chat completion client for query expansion.
#[derive(Clone)]
pub struct CompletionClient {
    client: reqwest::blocking::Client,
    config: CompletionClientConfig,
}

impl CompletionClient {
    /// Create a client if env vars are configured, otherwise return `Ok(None)`.
    pub fn from_env_if_configured() -> Result<Option<Self>> {
        if !CompletionClientConfig::is_configured() {
            return Ok(None);
        }
        Self::from_env().map(Some)
    }

    /// Create a client from env vars.
    pub fn from_env() -> Result<Self> {
        crate::init_tls();
        let config = CompletionClientConfig::from_env()?;
        let client = reqwest::blocking::Client::builder()
            .timeout(config.timeout)
            .build()
            .context("failed to create HTTP client for completion")?;
        Ok(Self { client, config })
    }

    /// Generate alternative queries with optional codebase context from a BM25
    /// pre-filter. When `context_hints` is non-empty the LLM sees real symbol
    /// names and file paths from the index, producing more targeted rewrites.
    pub fn expand_query_with_context(
        &self,
        query: &str,
        context_hints: &[String],
    ) -> Result<Vec<String>> {
        let context_block = if context_hints.is_empty() {
            String::new()
        } else {
            format!(
                "\n\nRelevant symbols and files found in the codebase:\n{}\n\n\
                 Use these as hints to generate more targeted rewrites.",
                context_hints.join("\n")
            )
        };

        let prompt = format!(
            "Original query: {query}{context_block}\n\n\
             Decompose this into {} targeted sub-queries that each search for a \
             different code location or concept needed to answer the original query. \
             Each sub-query should target specific symbols, types, or code patterns \
             rather than rephrasing the same idea.\n\n\
             Return ONLY a JSON array of strings.",
            self.config.max_alternatives
        );

        let request = ChatCompletionRequest {
            model: &self.config.model_id,
            messages: vec![
                ChatMessage {
                    role: "system",
                    content: "You decompose code-search queries into targeted sub-queries. \
                              Each sub-query should find a different piece of code needed \
                              to answer the original question. Use concrete symbol names, \
                              type names, or file patterns when possible. \
                              Output only the requested JSON."
                        .to_string(),
                },
                ChatMessage {
                    role: "user",
                    content: prompt,
                },
            ],
            temperature: 0.2,
            max_tokens: self.config.max_tokens,
            response_format: ChatResponseFormat {
                kind: "json_object",
            },
            stream: false,
        };

        let response = self
            .client
            .post(self.config.endpoint_url())
            .header("Authorization", format!("Bearer {}", self.config.api_key))
            .header("Content-Type", "application/json")
            .json(&request)
            .send()
            .context("failed to call completion API")?;

        let status = response.status();
        if !status.is_success() {
            let message = response.text().unwrap_or_default();
            return Err(anyhow!(
                "completion API error (status {}): {}",
                status.as_u16(),
                message
            ));
        }

        let payload: serde_json::Value = response
            .json()
            .context("failed to parse completion API response")?;

        let choice = payload
            .get("choices")
            .and_then(|v| v.as_array())
            .and_then(|c| c.first())
            .ok_or_else(|| anyhow!("completion response did not include choices"))?;

        let message = choice
            .get("message")
            .ok_or_else(|| anyhow!("completion response did not include message"))?;

        let content = message
            .get("content")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .trim();

        if content.is_empty() {
            let finish_reason = choice
                .get("finish_reason")
                .and_then(|v| v.as_str())
                .unwrap_or("unknown");
            let has_reasoning = message
                .get("reasoning_content")
                .and_then(|v| v.as_str())
                .is_some_and(|v| !v.trim().is_empty());
            if has_reasoning {
                return Err(anyhow!(
                    "completion response had empty content with only reasoning_content \
                     (finish_reason={finish_reason}); increase VERA_COMPLETION_MAX_TOKENS \
                     or use a non-reasoning model"
                ));
            }
            return Err(anyhow!(
                "completion response did not include message content"
            ));
        }

        let candidates = parse_query_candidates(content, self.config.max_alternatives)?;
        if candidates.is_empty() {
            return Err(anyhow!(
                "completion response did not contain any query candidates"
            ));
        }

        Ok(candidates)
    }
}

#[derive(Debug, Serialize)]
struct ChatCompletionRequest<'a> {
    model: &'a str,
    messages: Vec<ChatMessage>,
    temperature: f32,
    max_tokens: u32,
    response_format: ChatResponseFormat,
    stream: bool,
}

#[derive(Debug, Serialize)]
struct ChatMessage {
    role: &'static str,
    content: String,
}

#[derive(Debug, Serialize)]
struct ChatResponseFormat {
    #[serde(rename = "type")]
    kind: &'static str,
}

fn parse_query_candidates(raw: &str, limit: usize) -> Result<Vec<String>> {
    // Try parsing the whole string as JSON.
    if let Ok(value) = serde_json::from_str::<serde_json::Value>(raw.trim())
        && let Some(parsed) = json_value_to_queries(&value)
    {
        return Ok(normalize_query_list(parsed, limit));
    }

    // Try extracting a JSON array from within the response (e.g. fenced code blocks).
    if let Some((start, end)) = raw.find('[').zip(raw.rfind(']'))
        && end >= start
        && let Some(candidate) = raw.get(start..=end)
        && let Ok(value) = serde_json::from_str::<serde_json::Value>(candidate)
        && let Some(parsed) = json_value_to_queries(&value)
    {
        return Ok(normalize_query_list(parsed, limit));
    }

    let preview = raw.lines().take(3).collect::<Vec<_>>().join(" ");
    Err(anyhow!(
        "completion response is not a JSON array of query strings: {}",
        preview
    ))
}

fn json_value_to_queries(value: &serde_json::Value) -> Option<Vec<String>> {
    fn extract_array(value: &serde_json::Value) -> Option<Vec<String>> {
        let array = value.as_array()?;
        let mut items = Vec::with_capacity(array.len());
        for item in array {
            items.push(item.as_str()?.to_string());
        }
        Some(items)
    }

    if let Some(parsed) = extract_array(value) {
        return Some(parsed);
    }

    // Try common wrapper keys.
    let object = value.as_object()?;
    for key in ["rewrites", "queries", "candidates", "alternatives"] {
        if let Some(parsed) = object.get(key).and_then(extract_array) {
            return Some(parsed);
        }
    }

    None
}

fn normalize_query_list(candidates: Vec<String>, limit: usize) -> Vec<String> {
    let mut seen = HashSet::new();
    let mut normalized = Vec::new();

    for candidate in candidates {
        let query = normalize_query(&candidate);
        if query.is_empty() {
            continue;
        }
        let key = query.to_ascii_lowercase();
        if seen.insert(key) {
            normalized.push(query);
        }
        if normalized.len() >= limit {
            break;
        }
    }

    normalized
}

fn normalize_query(query: &str) -> String {
    query
        .trim()
        .trim_matches('"')
        .trim_matches('`')
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_json_array() {
        let raw = r#"["auth token refresh", "jwt expiry validation", "session middleware"]"#;
        let parsed = parse_query_candidates(raw, 4).unwrap();
        assert_eq!(parsed.len(), 3);
        assert_eq!(parsed[0], "auth token refresh");
    }

    #[test]
    fn parse_json_object_with_known_key() {
        let raw = r#"{"rewrites":["auth token refresh", "jwt expiry validation"]}"#;
        let parsed = parse_query_candidates(raw, 4).unwrap();
        assert_eq!(parsed.len(), 2);
    }

    #[test]
    fn parse_fenced_json() {
        let raw = "```json\n[\"auth\", \"token refresh\"]\n```";
        let parsed = parse_query_candidates(raw, 4).unwrap();
        assert_eq!(parsed, vec!["auth", "token refresh"]);
    }

    #[test]
    fn non_json_returns_error() {
        let raw = "1. auth token refresh\n2) jwt expiry handling";
        assert!(parse_query_candidates(raw, 4).is_err());
    }

    #[test]
    fn malformed_array_bounds_return_error_without_panicking() {
        let raw = "array done] here comes [\"q1\"";
        assert!(parse_query_candidates(raw, 4).is_err());
    }

    #[test]
    fn normalize_deduplicates_case_insensitive() {
        let result = normalize_query_list(
            vec![
                "Auth Refresh".to_string(),
                "auth refresh".to_string(),
                "AUTH REFRESH".to_string(),
            ],
            5,
        );
        assert_eq!(result, vec!["Auth Refresh"]);
    }
}