octocode 0.11.0

AI-powered code indexer with semantic search, GraphRAG knowledge graphs, and MCP server for multi-language codebases
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
// 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.

// GraphRAG relationship discovery logic

use crate::indexer::graphrag::types::{CodeNode, CodeRelationship, FunctionInfo};
use crate::indexer::graphrag::utils::{is_parent_child_relationship, symbols_match};
use crate::store::CodeBlock;
use anyhow::Result;
use std::path::Path;

pub struct RelationshipDiscovery;

impl RelationshipDiscovery {
	// Discover relationships efficiently without AI for most cases
	pub async fn discover_relationships_efficiently(
		new_files: &[CodeNode],
		all_nodes: &[CodeNode],
	) -> Result<Vec<CodeRelationship>> {
		let mut relationships = Vec::new();

		for source_file in new_files {
			// 1. Import/Export relationships (high confidence)
			for import in &source_file.imports {
				for target_file in all_nodes {
					if target_file.id == source_file.id {
						continue;
					}

					// Check if target exports what source imports
					if target_file
						.exports
						.iter()
						.any(|exp| symbols_match(import, exp))
						|| target_file
							.symbols
							.iter()
							.any(|sym| symbols_match(import, sym))
					{
						relationships.push(CodeRelationship {
							source: source_file.id.clone(),
							target: target_file.id.clone(),
							relation_type: crate::indexer::graphrag::types::RelationType::Imports,
							description: format!("Imports {} from {}", import, target_file.name),
							confidence: 0.9,
							weight: 1.0,
						});
					}
				}
			}

			// 2. Directory-based relationships (medium confidence)
			let source_dir = Path::new(&source_file.path)
				.parent()
				.map(|p| p.to_string_lossy().to_string())
				.unwrap_or_else(|| ".".to_string());

			for other_file in all_nodes {
				if other_file.id == source_file.id {
					continue;
				}

				let other_dir = Path::new(&other_file.path)
					.parent()
					.map(|p| p.to_string_lossy().to_string())
					.unwrap_or_else(|| ".".to_string());

				// Same directory relationship
				if source_dir == other_dir && source_file.language == other_file.language {
					relationships.push(CodeRelationship {
						source: source_file.id.clone(),
						target: other_file.id.clone(),
						relation_type: crate::indexer::graphrag::types::RelationType::SiblingModule,
						description: format!("Same directory: {}", source_dir),
						confidence: 0.6,
						weight: 0.5,
					});
				}
			}

			// 3. Hierarchical module relationships (high confidence)
			for other_file in all_nodes {
				if other_file.id == source_file.id {
					continue;
				}

				// Check for parent-child relationships based on path structure
				if is_parent_child_relationship(&source_file.path, &other_file.path) {
					let (parent, child) = if source_file.path.len() < other_file.path.len() {
						(&source_file.id, &other_file.id)
					} else {
						(&other_file.id, &source_file.id)
					};

					relationships.push(CodeRelationship {
						source: parent.clone(),
						target: child.clone(),
						relation_type: crate::indexer::graphrag::types::RelationType::ParentModule,
						description: "Hierarchical module relationship".to_string(),
						confidence: 0.8,
						weight: 0.7,
					});
				}
			}

			// 4. Language-specific pattern relationships
			Self::discover_language_specific_relationships(
				source_file,
				all_nodes,
				&mut relationships,
			);
		}

		// Deduplicate relationships
		relationships.sort_by(|a, b| {
			(a.source.clone(), a.target.clone(), a.relation_type.clone()).cmp(&(
				b.source.clone(),
				b.target.clone(),
				b.relation_type.clone(),
			))
		});
		relationships.dedup_by(|a, b| {
			a.source == b.source && a.target == b.target && a.relation_type == b.relation_type
		});

		Ok(relationships)
	}

	// Discover language-specific relationships with import resolution
	fn discover_language_specific_relationships(
		source_file: &CodeNode,
		all_nodes: &[CodeNode],
		relationships: &mut Vec<CodeRelationship>,
	) {
		// First, resolve imports to create semantic relationships
		Self::discover_import_relationships(source_file, all_nodes, relationships);

		// Then add language-specific patterns as fallback
		match source_file.language.as_str() {
			"rust" => {
				Self::discover_rust_relationships(source_file, all_nodes, relationships);
			}
			"javascript" | "typescript" => {
				Self::discover_js_ts_relationships(source_file, all_nodes, relationships);
			}
			"python" => {
				Self::discover_python_relationships(source_file, all_nodes, relationships);
			}
			"go" => {
				Self::discover_go_relationships(source_file, all_nodes, relationships);
			}
			"php" => {
				Self::discover_php_relationships(source_file, all_nodes, relationships);
			}
			_ => {
				// Generic patterns for other languages
			}
		}
	}

	// Discover semantic relationships through import resolution
	pub fn discover_import_relationships(
		source_file: &CodeNode,
		all_nodes: &[CodeNode],
		relationships: &mut Vec<CodeRelationship>,
	) {
		// Create a map for quick file lookup by path
		let file_map: std::collections::HashMap<String, &CodeNode> = all_nodes
			.iter()
			.map(|node| (node.path.clone(), node))
			.collect();

		// Get all file paths for resolution
		let all_files: Vec<String> = all_nodes.iter().map(|node| node.path.clone()).collect();

		// Get language implementation for import resolution
		if let Some(lang_impl) = crate::indexer::languages::get_language(&source_file.language) {
			// Resolve each import to create direct relationships
			for import_path in &source_file.imports {
				if let Some(resolved_path) =
					lang_impl.resolve_import(import_path, &source_file.path, &all_files)
				{
					// Find the target node
					if let Some(target_node) = file_map.get(&resolved_path) {
						// Create semantic import relationship
						relationships.push(CodeRelationship {
							source: source_file.id.clone(),
							target: target_node.id.clone(),
							relation_type: crate::indexer::graphrag::types::RelationType::Imports,
							description: format!(
								"Direct import: {} -> {}",
								import_path, resolved_path
							),
							confidence: 0.95, // High confidence for resolved imports
							weight: 1.0,
						});

						// Create reverse export relationship if target exports to source
						for export_item in &target_node.exports {
							if import_path.contains(export_item) || export_item == "*" {
								relationships.push(CodeRelationship {
									source: target_node.id.clone(),
									target: source_file.id.clone(),
									relation_type:
										crate::indexer::graphrag::types::RelationType::Imports,
									description: format!(
										"Exports {} to {}",
										export_item, source_file.path
									),
									confidence: 0.9,
									weight: 0.8,
								});
							}
						}
					}
				}
			}
		}
	}

	// Rust-specific relationship patterns
	fn discover_rust_relationships(
		source_file: &CodeNode,
		all_nodes: &[CodeNode],
		relationships: &mut Vec<CodeRelationship>,
	) {
		for other_file in all_nodes {
			if other_file.id == source_file.id || other_file.language != "rust" {
				continue;
			}

			// Check for mod.rs patterns
			if source_file.name == "mod"
				&& other_file
					.path
					.starts_with(&source_file.path.replace("/mod.rs", "/"))
			{
				relationships.push(CodeRelationship {
					source: source_file.id.clone(),
					target: other_file.id.clone(),
					relation_type: crate::indexer::graphrag::types::RelationType::ParentModule,
					description: "Rust module declaration".to_string(),
					confidence: 0.8,
					weight: 0.8,
				});
			}

			// Check for lib.rs patterns
			if source_file.name == "lib" || source_file.name == "main" {
				let source_dir = Path::new(&source_file.path)
					.parent()
					.map(|p| p.to_string_lossy().to_string())
					.unwrap_or_default();
				if other_file.path.starts_with(&source_dir) {
					relationships.push(CodeRelationship {
						source: source_file.id.clone(),
						target: other_file.id.clone(),
						relation_type: crate::indexer::graphrag::types::RelationType::ParentModule,
						description: "Rust crate root relationship".to_string(),
						confidence: 0.7,
						weight: 0.6,
					});
				}
			}
		}
	}

	// JavaScript/TypeScript-specific relationship patterns
	fn discover_js_ts_relationships(
		source_file: &CodeNode,
		all_nodes: &[CodeNode],
		relationships: &mut Vec<CodeRelationship>,
	) {
		for other_file in all_nodes {
			if other_file.id == source_file.id
				|| !["javascript", "typescript"].contains(&other_file.language.as_str())
			{
				continue;
			}

			// Check for index.js patterns
			if source_file.name == "index" {
				let source_dir = Path::new(&source_file.path)
					.parent()
					.map(|p| p.to_string_lossy().to_string())
					.unwrap_or_default();
				if other_file.path.starts_with(&source_dir) && other_file.name != "index" {
					relationships.push(CodeRelationship {
						source: source_file.id.clone(),
						target: other_file.id.clone(),
						relation_type: crate::indexer::graphrag::types::RelationType::ParentModule,
						description: "JavaScript index module relationship".to_string(),
						confidence: 0.7,
						weight: 0.6,
					});
				}
			}
		}
	}

	// Python-specific relationship patterns
	fn discover_python_relationships(
		source_file: &CodeNode,
		all_nodes: &[CodeNode],
		relationships: &mut Vec<CodeRelationship>,
	) {
		for other_file in all_nodes {
			if other_file.id == source_file.id || other_file.language != "python" {
				continue;
			}

			// Check for __init__.py patterns
			if source_file.name == "__init__" {
				let source_dir = Path::new(&source_file.path)
					.parent()
					.map(|p| p.to_string_lossy().to_string())
					.unwrap_or_default();
				if other_file.path.starts_with(&source_dir) && other_file.name != "__init__" {
					relationships.push(CodeRelationship {
						source: source_file.id.clone(),
						target: other_file.id.clone(),
						relation_type: crate::indexer::graphrag::types::RelationType::ParentModule,
						description: "Python package initialization".to_string(),
						confidence: 0.8,
						weight: 0.7,
					});
				}
			}
		}
	}
	// Go-specific relationship patterns
	fn discover_go_relationships(
		source_file: &CodeNode,
		all_nodes: &[CodeNode],
		relationships: &mut Vec<CodeRelationship>,
	) {
		for other_file in all_nodes {
			if other_file.id == source_file.id || other_file.language != "go" {
				continue;
			}

			// Check for package relationships
			let source_package = Self::extract_go_package(&source_file.path);
			let other_package = Self::extract_go_package(&other_file.path);

			if source_package == other_package && !source_package.is_empty() {
				relationships.push(CodeRelationship {
					source: source_file.id.clone(),
					target: other_file.id.clone(),
					relation_type: crate::indexer::graphrag::types::RelationType::SiblingModule,
					description: format!("Go package relationship: {}", source_package),
					confidence: 0.8,
					weight: 0.7,
				});
			}
		}
	}

	// PHP-specific relationship patterns
	fn discover_php_relationships(
		source_file: &CodeNode,
		all_nodes: &[CodeNode],
		relationships: &mut Vec<CodeRelationship>,
	) {
		for other_file in all_nodes {
			if other_file.id == source_file.id || other_file.language != "php" {
				continue;
			}

			// Check for namespace relationships
			let source_namespace = Self::extract_php_namespace(&source_file.path);
			let other_namespace = Self::extract_php_namespace(&other_file.path);

			if source_namespace == other_namespace && !source_namespace.is_empty() {
				relationships.push(CodeRelationship {
					source: source_file.id.clone(),
					target: other_file.id.clone(),
					relation_type: crate::indexer::graphrag::types::RelationType::SiblingModule,
					description: format!("PHP namespace relationship: {}", source_namespace),
					confidence: 0.8,
					weight: 0.7,
				});
			}
		}
	}

	// Helper methods for language-specific patterns

	fn extract_go_package(file_path: &str) -> String {
		if let Some(parent) = Path::new(file_path).parent() {
			if let Some(package_name) = parent.file_name() {
				return package_name.to_string_lossy().to_string();
			}
		}
		String::new()
	}

	fn extract_php_namespace(file_path: &str) -> String {
		// Extract namespace from file path structure
		let path = Path::new(file_path);
		if let Some(parent) = path.parent() {
			// Convert path to namespace-like structure
			parent.to_string_lossy().replace('/', "\\")
		} else {
			String::new()
		}
	}

	// Extract function information from a code block efficiently
	pub fn extract_functions_from_block(block: &CodeBlock) -> Result<Vec<FunctionInfo>> {
		let mut functions = Vec::new();

		// Look for function patterns in symbols
		for symbol in &block.symbols {
			if symbol.contains("function_") || symbol.contains("method_") {
				// Parse the symbol to extract function info
				if let Some(function_info) = Self::parse_function_symbol(symbol, block) {
					functions.push(function_info);
				}
			}
		}

		Ok(functions)
	}

	// Parse function symbol to create FunctionInfo
	fn parse_function_symbol(symbol: &str, block: &CodeBlock) -> Option<FunctionInfo> {
		// Simple pattern matching for common function symbol formats
		// This can be expanded based on your language implementations

		symbol
			.strip_prefix("function_")
			.map(|function_name| FunctionInfo {
				name: function_name.to_string(),
				signature: format!("{}(...)", function_name), // Simplified
				start_line: block.start_line as u32,
				end_line: block.end_line as u32,
				calls: Vec::new(), // Will be populated during relationship discovery
				called_by: Vec::new(),
				parameters: Vec::new(), // Could be extracted from content if needed
				return_type: None,
			})
	}

	// Extract imports/exports efficiently based on language patterns and symbols
	pub fn extract_imports_exports_efficient(
		symbols: &[String],
		_language: &str,
		_relative_path: &str,
	) -> (Vec<String>, Vec<String>) {
		// This function is now deprecated in favor of language-specific extraction
		// during AST parsing. For backward compatibility, treat all symbols as exports
		let mut exports = Vec::new();

		for symbol in symbols {
			if !symbol.is_empty() && !symbol.starts_with("IMPORT:") {
				exports.push(symbol.clone());
			}
		}

		// Return empty imports since real import extraction happens at AST level
		(Vec::new(), exports)
	}
	// Determine file kind based on path patterns
	// Determine file kind based on path patterns
	pub fn determine_file_kind(relative_path: &str) -> String {
		if relative_path.contains("/src/") || relative_path.contains("/lib/") {
			"source_file".to_string()
		} else if relative_path.contains("/test")
			|| relative_path.contains("_test.")
			|| relative_path.contains(".test.")
		{
			"test_file".to_string()
		} else if relative_path.ends_with(".md")
			|| relative_path.ends_with(".txt")
			|| relative_path.ends_with(".rst")
		{
			"documentation".to_string()
		} else if relative_path.contains("/config") || relative_path.contains(".config") {
			"config_file".to_string()
		} else if relative_path.contains("/examples") || relative_path.contains("/demo") {
			"example_file".to_string()
		} else {
			"file".to_string()
		}
	}

	// Generate simple description without AI for speed (fallback and default)
	pub fn generate_simple_description(
		file_name: &str,
		language: &str,
		symbols: &[String],
		lines: u32,
	) -> String {
		let function_count = symbols
			.iter()
			.filter(|s| s.contains("function_") || s.contains("method_"))
			.count();
		let class_count = symbols
			.iter()
			.filter(|s| s.contains("class_") || s.contains("struct_"))
			.count();

		if function_count > 0 && class_count > 0 {
			format!(
				"{} {} file with {} functions and {} classes ({} lines)",
				file_name, language, function_count, class_count, lines
			)
		} else if function_count > 0 {
			format!(
				"{} {} file with {} functions ({} lines)",
				file_name, language, function_count, lines
			)
		} else if class_count > 0 {
			format!(
				"{} {} file with {} classes ({} lines)",
				file_name, language, class_count, lines
			)
		} else {
			format!("{} {} file ({} lines)", file_name, language, lines)
		}
	}
}