octocode 0.13.0

AI-powered code intelligence with semantic search, knowledge graphs, and built-in MCP server. Transform your codebase into a queryable knowledge graph for AI assistants.
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// Copyright 2025 Muvon Un Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;

use crate::embedding::types::EmbeddingConfig;
use crate::storage;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LLMConfig {
	pub description_model: String,
	pub relationship_model: String,
	pub ai_batch_size: usize,
	pub max_batch_tokens: usize,
	pub batch_timeout_seconds: u64,
	pub fallback_to_individual: bool,
	pub max_sample_tokens: usize,
	pub confidence_threshold: f32,
	pub architectural_weight: f32,
	pub relationship_system_prompt: String,
	pub description_system_prompt: String,
}

// NOTE: This Default implementation should NEVER be used in practice
// All LLM values must come from the config template file
// This exists only to satisfy serde's requirements for deserialization
impl Default for LLMConfig {
	fn default() -> Self {
		panic!("LLM config must be loaded from template file - defaults not allowed")
	}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphRAGConfig {
	pub enabled: bool,
	pub use_llm: bool,
	pub llm: LLMConfig,
}

// NOTE: This Default implementation should NEVER be used in practice
// All GraphRAG values must come from the config template file
// This exists only to satisfy serde's requirements for deserialization
impl Default for GraphRAGConfig {
	fn default() -> Self {
		panic!("GraphRAG config must be loaded from template file - defaults not allowed")
	}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LlmConfig {
	pub model: String,
	pub timeout: u64,
	pub temperature: f32,
	pub max_tokens: usize,
}

impl Default for LlmConfig {
	fn default() -> Self {
		Self {
			model: "openrouter:openai/gpt-4o-mini".to_string(),
			timeout: 120,
			temperature: 0.7,
			max_tokens: 4000,
		}
	}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexConfig {
	pub chunk_size: usize,
	pub chunk_overlap: usize,
	pub embeddings_batch_size: usize,

	/// Maximum tokens per batch for embeddings generation (global limit).
	/// This prevents API errors like "max allowed tokens per submitted batch is 120000".
	/// Uses tiktoken cl100k_base tokenizer for counting. Default: 100000
	pub embeddings_max_tokens_per_batch: usize,

	/// How often to flush data to storage during indexing (in batches).
	/// 1 = flush after every batch (safest, slower)
	/// 5 = flush every 5 batches (faster, less safe)
	/// Default: 1 for maximum data safety
	pub flush_frequency: usize,

	/// Require git repository for indexing (default: true)
	pub require_git: bool,

	/// Enable RaBitQ quantization for vector indexes (default: true)
	/// When enabled, uses IVF_RQ (32x compression) instead of IVF_HNSW_SQ (4x compression)
	/// RaBitQ provides better storage efficiency while maintaining good recall
	pub quantization: bool,

	/// Enable LLM-generated contextual descriptions for code chunks (default: false)
	/// When enabled, uses the configured LLM to generate natural language descriptions
	/// of code chunks at indexing time. These descriptions are prepended to chunk content
	/// before embedding (not stored), improving search recall by ~35-67%.
	/// Structural context (file path, language, symbols) is ALWAYS prepended regardless.
	#[serde(default)]
	pub contextual_descriptions: bool,

	/// Model for contextual description generation in provider:model format
	pub contextual_model: String,

	/// Number of code chunks per LLM description batch (default: 10)
	#[serde(default = "default_contextual_batch_size")]
	pub contextual_batch_size: usize,
}

impl Default for IndexConfig {
	fn default() -> Self {
		Self {
			chunk_size: 2000,
			chunk_overlap: 100,
			embeddings_batch_size: 16,
			embeddings_max_tokens_per_batch: 100000,
			flush_frequency: 2,
			require_git: true,
			quantization: true,
			contextual_descriptions: false,
			contextual_model: "openrouter:openai/gpt-4o-mini".to_string(),
			contextual_batch_size: 10,
		}
	}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RerankerConfig {
	/// Enable reranking for search results
	pub enabled: bool,
	/// Reranker model in provider:model format (e.g., "voyage:rerank-2.5")
	pub model: String,
	/// Number of candidates to retrieve before reranking
	pub top_k_candidates: usize,
	/// Number of results to return after reranking
	pub final_top_k: usize,
}

/// Hybrid search configuration for combining vector and keyword search
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HybridSearchConfig {
	/// Enable hybrid search (vector + keyword)
	pub enabled: bool,
	/// Default weight for vector similarity signal
	pub default_vector_weight: f32,
	/// Default weight for keyword matching signal
	pub default_keyword_weight: f32,
	/// Weight for keyword matches in path/filename
	pub keyword_path_weight: f32,
	/// Weight for keyword matches in content
	pub keyword_content_weight: f32,
	/// Weight for keyword matches in symbols (code blocks only)
	pub keyword_symbols_weight: f32,
	/// Weight for keyword matches in title (document blocks only)
	pub keyword_title_weight: f32,
}

impl Default for HybridSearchConfig {
	fn default() -> Self {
		panic!("Hybrid search config must be loaded from template file - defaults not allowed")
	}
}
// NOTE: This Default implementation should NEVER be used in practice
// All reranker values must come from the config template file
impl Default for RerankerConfig {
	fn default() -> Self {
		panic!("Reranker config must be loaded from template file - defaults not allowed")
	}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchConfig {
	pub max_results: usize,
	pub similarity_threshold: f32,
	pub output_format: String,
	pub max_files: usize,
	pub context_lines: usize,

	/// Maximum characters to display per code/text/doc block in search results.
	/// If 0, displays full content. Default: 1000
	pub search_block_max_characters: usize,

	/// Reranker configuration for improving search result accuracy
	pub reranker: RerankerConfig,

	/// Hybrid search configuration for combining vector and keyword search
	pub hybrid: HybridSearchConfig,
}

impl Default for SearchConfig {
	fn default() -> Self {
		panic!("Search config must be loaded from template file - defaults not allowed")
	}
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct CommitsConfig {
	/// Use LLM to generate rich descriptions of commit diffs
	pub use_llm: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
	/// Configuration version for future migrations
	#[serde(default = "default_version")]
	pub version: u32,

	#[serde(default)]
	pub llm: LlmConfig,

	#[serde(default)]
	pub index: IndexConfig,

	#[serde(default)]
	pub search: SearchConfig,

	#[serde(default)]
	pub embedding: EmbeddingConfig,

	#[serde(default)]
	pub graphrag: GraphRAGConfig,

	#[serde(default)]
	pub commits: CommitsConfig,
}

fn default_version() -> u32 {
	1
}

fn default_contextual_batch_size() -> usize {
	10
}

impl Default for Config {
	fn default() -> Self {
		Self {
			version: default_version(),
			llm: LlmConfig::default(),
			index: IndexConfig::default(),
			search: SearchConfig::default(),
			embedding: EmbeddingConfig::default(),
			// This should never be reached - template loading should provide GraphRAG config
			graphrag: GraphRAGConfig::default(),
			commits: CommitsConfig::default(),
		}
	}
}

impl Config {
	pub fn load() -> Result<Self> {
		let config_path = Self::get_system_config_path()?;

		let config = if config_path.exists() {
			let content = fs::read_to_string(&config_path)?;
			toml::from_str(&content)?
		} else {
			// Load from template first, then save to system config
			let template_config = Self::load_from_template()?;

			// Ensure the parent directory exists
			if let Some(parent) = config_path.parent() {
				if !parent.exists() {
					fs::create_dir_all(parent)?;
				}
			}

			// Save template as the new config
			let toml_content = toml::to_string_pretty(&template_config)?;
			fs::write(&config_path, toml_content)?;
			template_config
		};

		// Environment variables are handled by octolib providers automatically
		// No need to set API keys in config

		Ok(config)
	}

	/// Load configuration from the default template
	pub fn load_from_template() -> Result<Self> {
		// Try to load from embedded template first
		let template_content = Self::get_default_template_content()?;
		let config: Config = toml::from_str(&template_content)?;
		Ok(config)
	}

	/// Get the default template content
	fn get_default_template_content() -> Result<String> {
		// First try to read from config-templates/default.toml in the current directory
		let template_path = std::path::Path::new("config-templates/default.toml");
		if template_path.exists() {
			return Ok(fs::read_to_string(template_path)?);
		}

		// If not found, use embedded template
		Ok(include_str!("../config-templates/default.toml").to_string())
	}

	pub fn save(&self) -> Result<()> {
		let config_path = Self::get_system_config_path()?;

		// Ensure the parent directory exists
		if let Some(parent) = config_path.parent() {
			if !parent.exists() {
				fs::create_dir_all(parent)?;
			}
		}

		let toml_content = toml::to_string_pretty(self)?;
		fs::write(config_path, toml_content)?;
		Ok(())
	}

	/// Get the system-wide config file path
	/// Stored at ~/.local/share/octocode/config.toml (same level as fastembed cache)
	pub fn get_system_config_path() -> Result<PathBuf> {
		let system_storage = storage::get_system_storage_dir()?;
		Ok(system_storage.join("config.toml"))
	}

	pub fn get_model(&self) -> &str {
		&self.llm.model
	}

	pub fn get_timeout(&self) -> u64 {
		self.llm.timeout
	}

	pub fn get_temperature(&self) -> f32 {
		self.llm.temperature
	}

	pub fn get_max_tokens(&self) -> usize {
		self.llm.max_tokens
	}
}

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

	#[test]
	fn test_default_config() {
		// Use template loading instead of Config::default() to avoid GraphRAG panic
		let config = Config::load_from_template().expect("Failed to load template config");
		assert_eq!(config.version, 1);
		assert_eq!(config.llm.model, "openrouter:openai/gpt-4o-mini");
		assert_eq!(config.index.chunk_size, 2000);
		assert_eq!(config.search.max_results, 20);

		assert_eq!(
			config
				.embedding
				.get_active_provider()
				.expect("embedding provider should be set"),
			crate::embedding::types::EmbeddingProviderType::Voyage
		);
		// Test new GraphRAG configuration structure
		assert!(!config.graphrag.enabled);
		assert!(!config.graphrag.use_llm);
		assert_eq!(
			config.graphrag.llm.description_model,
			"openrouter:openai/gpt-4o-mini"
		);
		assert_eq!(
			config.graphrag.llm.relationship_model,
			"openrouter:openai/gpt-4o-mini"
		);
		assert_eq!(config.graphrag.llm.ai_batch_size, 8);
		assert_eq!(config.graphrag.llm.max_batch_tokens, 16384);
		assert_eq!(config.graphrag.llm.batch_timeout_seconds, 60);
		assert!(config.graphrag.llm.fallback_to_individual);
		assert_eq!(config.graphrag.llm.max_sample_tokens, 1500);
		assert_eq!(config.graphrag.llm.confidence_threshold, 0.6);
		assert_eq!(config.graphrag.llm.architectural_weight, 0.9);
		assert!(config
			.graphrag
			.llm
			.relationship_system_prompt
			.contains("expert software architect"));
		assert!(config
			.graphrag
			.llm
			.description_system_prompt
			.contains("ROLE and PURPOSE"));
	}

	#[test]
	fn test_template_loading() {
		let result = Config::load_from_template();
		assert!(result.is_ok(), "Should be able to load from template");

		let config = result.expect("Template config should load successfully");
		assert_eq!(config.version, 1);
		assert_eq!(config.llm.model, "openrouter:openai/gpt-4o-mini");
		assert_eq!(config.index.chunk_size, 2000);
		assert_eq!(config.search.max_results, 20);
		assert_eq!(config.embedding.code_model, "voyage:voyage-code-3");
		assert_eq!(config.embedding.text_model, "voyage:voyage-3.5-lite");
		// Test new GraphRAG configuration structure from template
		assert!(!config.graphrag.enabled);
		assert!(!config.graphrag.use_llm);
		assert_eq!(
			config.graphrag.llm.description_model,
			"openrouter:openai/gpt-4o-mini"
		);
		assert_eq!(
			config.graphrag.llm.relationship_model,
			"openrouter:openai/gpt-4o-mini"
		);
		assert_eq!(config.graphrag.llm.ai_batch_size, 8);
		assert_eq!(config.graphrag.llm.max_batch_tokens, 16384);
		assert_eq!(config.graphrag.llm.batch_timeout_seconds, 60);
		assert!(config.graphrag.llm.fallback_to_individual);
		assert_eq!(config.graphrag.llm.max_sample_tokens, 1500);
		assert_eq!(config.graphrag.llm.confidence_threshold, 0.6);
		assert_eq!(config.graphrag.llm.architectural_weight, 0.9);
		assert!(config
			.graphrag
			.llm
			.relationship_system_prompt
			.contains("expert software architect"));
		assert!(config
			.graphrag
			.llm
			.description_system_prompt
			.contains("ROLE and PURPOSE"));
	}

	#[test]
	#[should_panic(expected = "GraphRAG config must be loaded from template file")]
	fn test_graphrag_default_panics() {
		// Verify that GraphRAGConfig::default() panics to enforce strict config loading
		let _ = GraphRAGConfig::default();
	}
}